<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Magento Coder &#187; Reports</title>
	<atom:link href="http://magentocoder.jigneshpatel.co.in/category/reports/feed/" rel="self" type="application/rss+xml" />
	<link>http://magentocoder.jigneshpatel.co.in</link>
	<description>hacks &#38; solutions for Magento Ecommerce development</description>
	<lastBuildDate>Sun, 19 Jun 2011 16:13:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Create custom Reports in Magento Admin</title>
		<link>http://magentocoder.jigneshpatel.co.in/create-custom-reports-in-magento-admin/</link>
		<comments>http://magentocoder.jigneshpatel.co.in/create-custom-reports-in-magento-admin/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 09:26:36 +0000</pubDate>
		<dc:creator>Jignesh</dc:creator>
				<category><![CDATA[Reports]]></category>

		<guid isPermaLink="false">http://magentocoder.jigneshpatel.co.in/?p=60</guid>
		<description><![CDATA[Want to create a custom report in Magento Admin? After taking help from some forums &#38; all I was able to generate a new Report the way I wanted. I was looking to generate the Report for the Products sold along with the name of the Artist to whom the product belongs to. These are the steps to be followed / I followed. 1. The title of the report is: &#8216;Artist Sold Works&#8217;. To add [...]]]></description>
			<content:encoded><![CDATA[<p>Want to create a custom report in Magento Admin?</p>
<p>After taking help from some forums &amp; all I was able to generate a new Report the way I wanted.</p>
<p>I was looking to generate the Report for the Products sold along with the name of the Artist to whom the product belongs to.</p>
<p>These are the steps to be followed / I followed.</p>
<p>1. The title of the report is: &#8216;Artist Sold Works&#8217;. To add the new item under the Reports -&gt; Products.</p>
<p>Open the &#8216;app/code/code/Mage/Reports/etc/config.xml&#8217;</p>
<p>Add the followind code in the &#8216;children of &#8216;products&#8217; (near line 221).</p>
<pre class="brush:php">
	&lt;title&gt;Artist Sold Works&lt;/title&gt;
	adminhtml/report_product/artistsold
</pre>
<p>Add the followind code in the  of</p>
<p>(near line 370).</p>
<pre class="brush:php">
		&lt;title&gt;Artists Sold Works&lt;/title&gt;
</pre>
<p>2. Copy files</p>
<p>app/code/core/Mage/Adminhtml/Block/Report/Product/Sold.php to app/code/core/Mage/Adminhtml/Block/Report/Product/Artistsold.php.</p>
<p>3. Copy directories</p>
<p>app/code/core/Mage/Adminhtml/Block/Report/Product/Sold   to</p>
<p>app/code/core/Mage/Adminhtml/Block/Report/Product/Artistsold</p>
<p>app/code/core/Mage/Reports/Model/Mysql4/Product/Sold   to</p>
<p>app/code/core/Mage/Reports/Model/Mysql4/Product/Artistsold</p>
<p>4. In the file Artistsold.php, change the class name from</p>
<p>Mage_Adminhtml_Block_Report_Product_Sold to Mage_Adminhtml_Block_Report_Product_Artistsold.</p>
<p>Change the lines</p>
<pre class="brush:php">        $this-&gt;_controller = 'report_product_sold';
        $this-&gt;_headerText = Mage::helper('reports')-&gt;__('Products Ordered');
</pre>
<p>to</p>
<pre class="brush:php">        $this-&gt;_controller = 'report_product_artistsold';
        $this-&gt;_headerText = Mage::helper('reports')-&gt;__('Artist Sold Works');
</pre>
<p>5. Add/Modify the columns in the</p>
<p>app/code/core/Mage/Adminhtml/Block/Report/Product/Artistsold/Grid.php</p>
<p>Here in my case:</p>
<pre class="brush:php">
        $this-&gt;addColumn('artistId', array(
            'header'    =&gt;Mage::helper('reports')-&gt;__('Artist'),
            'width'     =&gt;'120px',
            'index'     =&gt;'artistname',
        ));		

        $this-&gt;addColumn('sale_percentage', array(
            'header'    =&gt;Mage::helper('reports')-&gt;__('Artist Share'),
            'width'     =&gt;'60px',
            'index'     =&gt;'sale_percentage',
            'align'     =&gt;'right'
        ));

        $this-&gt;addColumn('base_price_total', array(
            'header'    =&gt;Mage::helper('reports')-&gt;__('Total Product Base Price ($)'),
            'width'     =&gt;'60px',
            'index'     =&gt;'base_price_total',
            'align'     =&gt;'right',
            'total'     =&gt;'sum',
            'type'      =&gt;'number'

        ));

        $this-&gt;addColumn('artist_earned', array(
            'header'    =&gt;Mage::helper('reports')-&gt;__('Artist Earned ($)'),
            'width'     =&gt;'60px',
            'index'     =&gt;'artist_earned',
            'align'     =&gt;'right',
            'total'     =&gt;'sum',
            'type'      =&gt;'number'
        ));
</pre>
<p>6. Add new functions to</p>
<p>app/code/core/Mage/Adminhtml/controllers/Report/ProductController.php</p>
<pre class="brush:php">    public function artistsoldAction()
    {
        $this-&gt;_initAction()
            -&gt;_setActiveMenu('report/product/artistsold')
            -&gt;_addBreadcrumb(Mage::helper('reports')-&gt;__('Artists Sold Works'), Mage::helper('reports')-&gt;__('Artists Sold Works'))
            -&gt;_addContent($this-&gt;getLayout()-&gt;createBlock('adminhtml/report_product_artistsold'))
            -&gt;renderLayout();
    }
</pre>
<p>7.  Open the file</p>
<p>app/code/core/Mage/Reports/Model/Mysql4/Product/Artistsold/Collection.php.</p>
<p>Rename the class name from</p>
<p>Mage_Reports_Model_Mysql4_Product_Sold_Collection  to</p>
<p>Mage_Reports_Model_Mysql4_Product_Artistsold_Collection</p>
<p>Customize the function setDateRange() in the  as per your need.</p>
<p>Here in my case:</p>
<pre class="brush:php">    public function setDateRange($frmdate, $todate)
    {
        $this-&gt;_reset()
            -&gt;addAttributeToSelect('*')
            -&gt;addOrderedQtyForArtistSold($frmdate,$todate);
		return $this;
    }
</pre>
<p>8. To get the new fields, to alter the sql query I copied the function addOrderedQty() to addOrderedQtyForArtistSold() in the file</p>
<p>app/code/core/Mage/Reports/Model/Mysql4/Product/Collection.php</p>
<p>And I did changes in the functions as per my need to get the extra columns.</p>
<p>Here in my case:</p>
<pre class="brush:php">
	public function addOrderedQtyForArtistSold($frm = '', $to = '')
    {
		if(key_exists('report',$_SESSION)) {
    		$artistId = $_SESSION['report']['artistid'];
		}
		else {
			$artistId ='';
		}

        $qtyOrderedTableName = $this-&gt;getTable('sales/order_item');
        $qtyOrderedFieldName = 'qty_ordered';

        $productIdTableName = $this-&gt;getTable('sales/order_item');
        $productIdFieldName = 'product_id';

		$productEntityIntTable = (string)Mage::getConfig()-&gt;getTablePrefix() . 'catalog_product_entity_varchar';
		$adminUserTable = $this-&gt;getTable('admin_user');
		$artistsTable = $this-&gt;getTable('appartists');
		$eavAttributeTable = $this-&gt;getTable('eav/attribute');

        $compositeTypeIds = Mage::getSingleton('catalog/product_type')-&gt;getCompositeTypes();

        # This was added by Dev1 to get the configurable items in the list &amp; not to get the simple products
        $compositeTypeIds = Array (
						    '0' =&gt; 'grouped',
						    '1' =&gt; 'simple',
						    '2' =&gt; 'bundle'
							);

        $productTypes = $this-&gt;getConnection()-&gt;quoteInto(' AND (e.type_id NOT IN (?))', $compositeTypeIds);

        if ($frm != '' &amp;&amp; $to != '') {
            $dateFilter = " AND `order`.created_at BETWEEN '{$frm}' AND '{$to}'";
        } else {
            $dateFilter = "";
        }

        $this-&gt;getSelect()-&gt;reset()-&gt;from(
           array('order_items' =&gt; $qtyOrderedTableName),
           array('ordered_qty' =&gt; "SUM(order_items.{$qtyOrderedFieldName})",'base_price_total' =&gt; "SUM(order_items.price)")
        );

        $order = Mage::getResourceSingleton('sales/order');

        $stateAttr = $order-&gt;getAttribute('state');
        if ($stateAttr-&gt;getBackend()-&gt;isStatic()) {

            $_joinCondition = $this-&gt;getConnection()-&gt;quoteInto(
                'order.entity_id = order_items.order_id AND order.state&lt;&gt;?', Mage_Sales_Model_Order::STATE_CANCELED
            );
            $_joinCondition .= $dateFilter;

            $this-&gt;getSelect()-&gt;joinInner(
                array('order' =&gt; $this-&gt;getTable('sales/order')),
                $_joinCondition,
                array()
            );
        } else {

            $_joinCondition = 'order.entity_id = order_state.entity_id';
            $_joinCondition .= $this-&gt;getConnection()-&gt;quoteInto(' AND order_state.attribute_id=? ', $stateAttr-&gt;getId());
            $_joinCondition .= $this-&gt;getConnection()-&gt;quoteInto(' AND order_state.value&lt;&gt;? ', Mage_Sales_Model_Order::STATE_CANCELED);

            $this-&gt;getSelect()
                -&gt;joinInner(
                    array('order' =&gt; $this-&gt;getTable('sales/order')),
                    'order.entity_id = order_items.order_id' . $dateFilter,
                    array())
                -&gt;joinInner(
                    array('order_state' =&gt; $stateAttr-&gt;getBackend()-&gt;getTable()),
                    $_joinCondition,
                    array());
        }

        $this-&gt;getSelect()
            -&gt;joinInner(array('e' =&gt; $this-&gt;getProductEntityTableName()),
                "e.entity_id = order_items.{$productIdFieldName}")
             -&gt;group('e.entity_id')
            -&gt;having('ordered_qty &gt; 0');

        $artistIdConcat = $artistId != '' ? " AND artistId=$artistId" : "";

        $this-&gt;getSelect()
            -&gt;joinInner(
                array('pei' =&gt; $productEntityIntTable),
                "e.entity_id = pei.entity_id",
                array())
            -&gt;joinInner(
                array('ea' =&gt; $eavAttributeTable),
                "pei.attribute_id=ea.attribute_id AND ea.attribute_code='artistid'",
                array())
            -&gt;joinInner(
                array('au' =&gt; $adminUserTable),
                "au.user_id=pei.value",
                array("artistname" =&gt; "CONCAT(firstname, ' ',lastname)"))
            -&gt;joinInner(
                array('ar' =&gt; $artistsTable),
                "ar.artistId=au.user_id".$artistIdConcat,
                array("sale_percentage" =&gt; "CONCAT(sale_percentage,'%')","artist_earned" =&gt; "((SUM(order_items.price)) * (sale_percentage)) / 100"));

        return $this;
    }
</pre>
<p><a href="http://magentocoder.jigneshpatel.co.in/wp-content/uploads/2009/09/Artist_sold_works_report.png" target="_blank"></p>
<p><img class="alignnone size-full wp-image-66" title="Artist_sold_works_report" src="http://magentocoder.jigneshpatel.co.in/wp-content/uploads/2009/09/Artist_sold_works_report.png" alt="Artist_sold_works_report" width="600" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://magentocoder.jigneshpatel.co.in/create-custom-reports-in-magento-admin/feed/</wfw:commentRss>
		<slash:comments>46</slash:comments>
		</item>
	</channel>
</rss>

