In: Reports
7 Sep 2009Want to create a custom report in Mageto Admin?
After taking help from some forums & all I was able to generate a new Report the way I wanted.
I was looking to generate the Report for the Products sold alog 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: ‘Artist Sold Works’. To add the new item under the Reports -> Products.
Open the ‘app/code/code/Mage/Reports/etc/config.xml’
Add the followind code in the ‘children of ‘products’ (near line 221).
<title>Artist Sold Works</title> adminhtml/report_product/artistsold
Add the followind code in the
<title>Artists Sold Works</title>
2. Copy files
app/code/core/Mage/Adminhtml/Block/Report/Product/Sold.php to app/code/core/Mage/Adminhtml/Block/Report/Product/Artistsold.php.
3. Copy directories
app/code/core/Mage/Adminhtml/Block/Report/Product/Sold to
app/code/core/Mage/Adminhtml/Block/Report/Product/Artistsold
app/code/core/Mage/Reports/Model/Mysql4/Product/Sold to
app/code/core/Mage/Reports/Model/Mysql4/Product/Artistsold
4. In the file Artistsold.php, change the class name from
Mage_Adminhtml_Block_Report_Product_Sold to Mage_Adminhtml_Block_Report_Product_Artistsold.
Change the lines
$this->_controller = 'report_product_sold';
$this->_headerText = Mage::helper('reports')->__('Products Ordered');
to
$this->_controller = 'report_product_artistsold';
$this->_headerText = Mage::helper('reports')->__('Artist Sold Works');
5. Add/Modify the columns in the
app/code/core/Mage/Adminhtml/Block/Report/Product/Artistsold/Grid.php
Here in my case:
$this->addColumn('artistId', array(
'header' =>Mage::helper('reports')->__('Artist'),
'width' =>'120px',
'index' =>'artistname',
));
$this->addColumn('sale_percentage', array(
'header' =>Mage::helper('reports')->__('Artist Share'),
'width' =>'60px',
'index' =>'sale_percentage',
'align' =>'right'
));
$this->addColumn('base_price_total', array(
'header' =>Mage::helper('reports')->__('Total Product Base Price ($)'),
'width' =>'60px',
'index' =>'base_price_total',
'align' =>'right',
'total' =>'sum',
'type' =>'number'
));
$this->addColumn('artist_earned', array(
'header' =>Mage::helper('reports')->__('Artist Earned ($)'),
'width' =>'60px',
'index' =>'artist_earned',
'align' =>'right',
'total' =>'sum',
'type' =>'number'
));
6. Add new functions to
app/code/core/Mage/Adminhtml/controllers/Report/ProductController.php
public function artistsoldAction()
{
$this->_initAction()
->_setActiveMenu('report/product/artistsold')
->_addBreadcrumb(Mage::helper('reports')->__('Artists Sold Works'), Mage::helper('reports')->__('Artists Sold Works'))
->_addContent($this->getLayout()->createBlock('adminhtml/report_product_artistsold'))
->renderLayout();
}
7. Open the file
app/code/core/Mage/Reports/Model/Mysql4/Product/Artistsold/Collection.php.
Rename the class name from
Mage_Reports_Model_Mysql4_Product_Sold_Collection to
Mage_Reports_Model_Mysql4_Product_Artistsold_Collection
Customize the function setDateRange() in the as per your need.
Here in my case:
public function setDateRange($frmdate, $todate)
{
$this->_reset()
->addAttributeToSelect('*')
->addOrderedQtyForArtistSold($frmdate,$todate);
return $this;
}
8. To get the new fields, to alter the sql query I copied the function addOrderedQty() to addOrderedQtyForArtistSold() in the file
app/code/core/Mage/Reports/Model/Mysql4/Product/Collection.php
And I did changes in the functions as per my need to get the extra columns.
Here in my case:
public function addOrderedQtyForArtistSold($frm = '', $to = '')
{
if(key_exists('report',$_SESSION)) {
$artistId = $_SESSION['report']['artistid'];
}
else {
$artistId ='';
}
$qtyOrderedTableName = $this->getTable('sales/order_item');
$qtyOrderedFieldName = 'qty_ordered';
$productIdTableName = $this->getTable('sales/order_item');
$productIdFieldName = 'product_id';
$productEntityIntTable = (string)Mage::getConfig()->getTablePrefix() . 'catalog_product_entity_varchar';
$adminUserTable = $this->getTable('admin_user');
$artistsTable = $this->getTable('appartists');
$eavAttributeTable = $this->getTable('eav/attribute');
$compositeTypeIds = Mage::getSingleton('catalog/product_type')->getCompositeTypes();
# This was added by Dev1 to get the configurable items in the list & not to get the simple products
$compositeTypeIds = Array (
'0' => 'grouped',
'1' => 'simple',
'2' => 'bundle'
);
$productTypes = $this->getConnection()->quoteInto(' AND (e.type_id NOT IN (?))', $compositeTypeIds);
if ($frm != '' && $to != '') {
$dateFilter = " AND `order`.created_at BETWEEN '{$frm}' AND '{$to}'";
} else {
$dateFilter = "";
}
$this->getSelect()->reset()->f rom(
array('order_items' => $qtyOrderedTableName),
array('ordered_qty' => "SUM(order_items.{$qtyOrderedFieldName})",'base_price_total' => "SUM(order_items.price)")
);
$order = Mage::getResourceSingleton('sales/order');
$stateAttr = $order->getAttribute('state');
if ($stateAttr->getBackend()->isStatic()) {
$_joinCondition = $this->getConnection()->quoteInto(
'order.entity_id = order_items.order_id AND order.state<>?', Mage_Sales_Model_Order::STATE_CANCELED
);
$_joinCondition .= $dateFilter;
$this->getSelect()->joinInner(
array('order' => $this->getTable('sales/order')),
$_joinCondition,
array()
);
} else {
$_joinCondition = 'order.entity_id = order_state.entity_id';
$_joinCondition .= $this->getConnection()->quoteInto(' AND order_state.attribute_id=? ', $stateAttr->getId());
$_joinCondition .= $this->getConnection()->quoteInto(' AND order_state.value<>? ', Mage_Sales_Model_Order::STATE_CANCELED);
$this->getSelect()
->joinInner(
array('order' => $this->getTable('sales/order')),
'order.entity_id = order_items.order_id' . $dateFilter,
array())
->joinInner(
array('order_state' => $stateAttr->getBackend()->getTable()),
$_joinCondition,
array());
}
$this->getSelect()
->joinInner(array('e' => $this->getProductEntityTableName()),
"e.entity_id = order_items.{$productIdFieldName}")
->group('e.entity_id')
->having('ordered_qty > 0');
$artistIdConcat = $artistId != '' ? " AND artistId=$artistId" : "";
$this->getSelect()
->joinInner(
array('pei' => $productEntityIntTable),
"e.entity_id = pei.entity_id",
array())
->joinInner(
array('ea' => $eavAttributeTable),
"pei.attribute_id=ea.attribute_id AND ea.attribute_code='artistid'",
array())
->joinInner(
array('au' => $adminUserTable),
"au.user_id=pei.value",
array("artistname" => "CONCAT(firstname, ' ',lastname)"))
->joinInner(
array('ar' => $artistsTable),
"ar.artistId=au.user_id".$artistIdConcat,
array("sale_percentage" => "CONCAT(sale_percentage,'%')","artist_earned" => "((SUM(order_items.price)) * (sale_percentage)) / 100"));
return $this;
}
Note: Please remove the space the line 38 in box above for the word ‘f rom’. I was not able to post the article if I keep the correct word.
Hello, Welcome to the Magento Coder.
I am Jignesh Patel, a Web Developer.
About Jignesh
22 Responses to Create custom Reports in Magento Admin
sam
September 8th, 2009 at 7:46 pm
looks good, will let you know if site still functional after my hacks
mtness
September 30th, 2009 at 9:23 pm
Hi there!
Thank you very much for that insightful post.
I’m diving deeper into the subject right now, and found some stuff which will sureley be of interest to you:
To make the changes future compatible, you need to apply the changes in the “local” directory rather than “core”, which will be overwritten when Magento updates are applied. Found here:
http://www.magentocommerce.com/boards/viewthread/29535/#t104176
I will keep you informed about my progress.
Kind regards,
mtness
admin
September 30th, 2009 at 9:53 pm
Hi Mtness,
I will try to switch to ‘local’ instead of ‘core’. Let me know if you get something.
Thanks.
mtness
October 1st, 2009 at 4:20 pm
Hi there again!
I will try to describe you the problems I have run into, maybe you might be able to point me in the right direction.
I want to extend the “sales report” so that i have additional columns that show the SUM per payment Method.
As I see it, the table “sales_order” does not contain this information, and i have currently no idea where to place a join or whatever to get that information.
The file I altered to display an extra column is:
app\code\local\Mage\Reports\Model\Mysql4\Order\Collection.php
Would be nice to hear from you!
Kind regards,
mtness
salo
October 1st, 2009 at 5:41 pm
Jignesh Patel, please share your
app/code/core/Mage/Adminhtml/Block/Report/Product/Artistsold/Grid.php and app/code/core/Mage/Reports/Model/Mysql4/Product/Artistsold/Collection.php
admin
October 1st, 2009 at 6:34 pm
Hi Salo,
I have updated the Post. Hope that will help you.
eunecte
October 13th, 2009 at 6:55 pm
Hello,
With the help of this I managed to change a sales report. But in the grid.php how do I retrieve the value of a “dropdown-attribute”. in the report it is always the ID of the option that shows up but not the value.
Thank You
eunecte
benak
December 3rd, 2009 at 3:05 am
I have the same problem as ennecte, does anyone manage to display dropdown attribute value ??? Like manufacturer attribute ?
Please help !!
Thank you
stereoscott
December 8th, 2009 at 11:06 am
Thank you so much for posting this. It was a huge help to me and I’m sure many others! I’m interested to know how you added your own custom filter in the grid view (for artist).
john
January 3rd, 2010 at 7:41 am
so i’m bumping down the road to getting this as “local” code as opposed to in the “core”. I will post my findings if/when i’m successful. my only question so far is about your attributes. i’m assuming that you have 2 attributes for artistid and sale_percentage? are these the code names in the database? i think the other two columns are calculated?
admin
January 3rd, 2010 at 7:38 pm
@john, I have actually a separate Module called Artists, where I can manage them so for each artist, there are some attributes. sale_percentage is one of them. So its an attribute for an Artist. On the other hand, artist_id is the attribute for the products. So this artist_id for product is related with the id of an artist in the Artists module.
Mike
January 8th, 2010 at 6:53 am
@mtness: Were you able to get move the custom report code into the local pool? If so, what steps did you take?
Thanks,
Mike
Ruben
February 3rd, 2010 at 10:31 pm
Hi, you made a good tutorial to do nice custom reports.
Can you give me some lights how to do a search filter, like you show in the image, to return only the artist we want?
cheers!
Raj
February 24th, 2010 at 11:44 am
Great Tutorial Mate!
But can you please describe little bit more if we want to add a big module in Administration!
Actually i am building this http://www.getmybrand.com website and need to add new module within its front end and back end!
Any help would be appreciated!
Regards,
Raj
admin
February 24th, 2010 at 2:33 pm
@Raj,
You can try the module creator. Using that you can create a new Module, which will be very basic.
Then you will need to customize it based on your needs.
Om
March 2nd, 2010 at 7:58 pm
Hi,
can anybody help me to develop a module in admin so that some specific users can add products.
like in my case i have created the wholeseller users and give them permission on my custom module.
i need to just a duplicate product management which can give facility to add/edit delete products on attribute basis.
thanks
Andrea
March 15th, 2010 at 9:28 pm
Hej man,
I am trying to develop a custom report for my company. So i managed to modify one of the existing reports but can not succeed in adding some extra search criteria as you added the Author. Your code was of great help for me as it pointed me where to search for solving the problem. I have one question for you, where is the following session set:
if(key_exists(‘report’,$_SESSION)) {
$artistId = $_SESSION['report']['artistid'];
}
my report is local and i have placed the if statement the same class as you did but seems can not find the report into the SESSION. So do you have any idea?? Any help would be greatly appreciated. Thanks in advance
shehz
April 11th, 2010 at 10:43 am
Hi
Thanks for the tutorial . I followed all your steps and created the fields that you created. I manage to get the menu under product
But When I click on Artist Sold Works . It gives me 404 error Page not found.
Can you please guide me what am I doing wrong
Thanks
admin
April 19th, 2010 at 10:02 pm
@shehz, I guess you might not configure the xml files properly.
rija
April 26th, 2010 at 4:54 pm
when i try
if(key_exists(’report’,$_SESSION)) {
$artistId = $_SESSION['report']['artistid'];
}
$artistId not exist
Cas you help me
Pedro Victor
June 15th, 2010 at 12:57 pm
Somebody can do the report in local?, if yes please can you give me more information, I’m new in Magento and I want do local reports, not in core
thanks
Kapil
June 29th, 2010 at 12:31 pm
Hey thanks for a wonderful tutorials .. tried in Local but wasnt successful so will be waiting for it form you guys and Admin ..
Also i wanted to have the new added fields to not only show in the Grid but also Export (to CSV & Excel) the same set of data with new field values
so incase if anyone else need to do the same, here are the simple changes :
-> In ProductController.php Copy exportSoldCsvAction & exportSoldExcelAction methods to the same file and rename it to say exportArtistSoldCsvAction & exportArtistSoldExcelAction,
-> then you may change the $fileName
-> then change the createBlock to path as ‘adminhtml/report_product_artistsold_grid’
-> then in your app/code/core/Mage/Adminhtml/Block/Report/Product/Artistsold/Grid.php file
change the line
From :
$this->addExportType(‘*/*/exportSoldCsv’, Mage::helper(‘reports’)->__(‘CSV’));
$this->addExportType(‘*/*/exportSoldExcel’, Mage::helper(‘reports’)->__(‘Excel’));
To:
$this->addExportType(‘*/*/exportArtistSoldCsv’, Mage::helper(‘reports’)->__(‘CSV’));
$this->addExportType(‘*/*/exportArtistSoldExcel’, Mage::helper(‘reports’)->__(‘Excel’));