Magento custom reports: Get data from database - php

I need some help.
I created a custom report in Magento.
Now I want to list all products wich have been ordered in a month in my grid.
In my report are the following columns: SKU, Name, Ordered Quantity and Base Cost.
In the column "Ordered Quantity" I want to show how often a product has been ordered.
In the column "Base Cost" I want to show the total base costs (ordered quantity * base cost).
With the following code I get so correct product names and skus.
The other columns are not correct.
Can someone help me?
$this->getSelect()->reset()
->from(
array('order_items' => $this->getTable('sales/order_item')),
array(
'ordered_qty' => 'SUM(order_items.qty_ordered)',
'order_items_name' => 'order_items.name',
'base_cost' => 'SUM(order_items.base_cost)',
'sku' => 'order_items.sku'
))
->where("created_at BETWEEN '".$from."' AND '".$to."'")
->where('parent_item_id IS NULL')
->group('order_items.product_id')
->having('SUM(order_items.qty_ordered) > ?', 0)
->order(
array(
'SUM(order_items.qty_ordered) DESC'
));

Here is my solution:
$this->getSelect()->reset()
->from(
array('order_items' => $this->getTable('sales/order_item')),
array(
'ordered_qty' => 'order_items.qty_ordered',
'order_items_name' => 'order_items.name',
'vendor' => 'attrval.value',
'base_cost' => '(SUM(order_items.qty_ordered) * order_items.base_cost)',
'sku' => 'order_items.sku'
))
->joinLeft(array('p' => 'catalog_product_entity'), 'order_items.product_id = p.entity_id')
->joinLeft(array('eav' => 'eav_attribute'), 'p.entity_type_id = eav.entity_type_id')
->joinLeft(array('attr' =>'eav_attribute_option'), 'attr.attribute_id = eav.attribute_id')
->joinLeft(array('attrval' =>'eav_attribute_option_value'), 'attrval.option_id = attr.option_id')
->where("eav.attribute_code='vendor'")
->where("order_items.created_at BETWEEN '".$from."' AND '".$to."'")
->where('parent_item_id IS NULL')
->group('order_items.product_id')
->having('SUM(order_items.qty_ordered) > ?', 0)
->order(
array(
'SUM(order_items.qty_ordered) DESC'
));
It includes an additional custom attribute called 'vendor'.

To outputting the raw SQL query see Output raw SQL query from Magento collection
Format field, you can use price or currency
see http://code007.wordpress.com/2012/07/16/grid-column-types-in-magento/
$this->addColumn('some_column_id', array(
'header' => Mage::helper('core')->__('Some column name'),
'index' => 'some_column_index',
'type' => '???',
));
Types
action
checkbox
concat
country
currency
date
datetime
input
interface
ip
longtext
massaction
number
options
price
radio
select
store
text
theme
wrapline
See /app/code/core/Mage/Adminhtml/Block/Widget/Grid/Column/Renderer folder.
To make your own grid types see http://mydons.com/how-to-create-custom-column-renderer-in-magento-grid/.

Related

how to add country column in prestashop AdminCustomersControllerCore

i want to add an extra column in AdminCustomersControllerCore so that it will show a new column in my back office manage customer page.
Use the hook actionControllerNameListingFieldsModifier to add fields.
Every admin controller calls this hook before generating the list/table.
Hook::exec('action'.$this->controller_name.'ListingFieldsModifier', array(
'select' => &$this->_select,
'join' => &$this->_join,
'where' => &$this->_where,
'group_by' => &$this->_group,
'order_by' => &$this->_orderBy,
'order_way' => &$this->_orderWay,
'fields' => &$this->fields_list,
));
You will need to modify select and fields variable to include your own. You might need to modify join variable as well if you have data in a custom table.

Prestashop admin panel custom filter for comma-separated column

here my comma separated column is shops(eg. 1,2,3,4), and with the call back i am already displaying the multiple shops names. with call back i am showing the related shop-names(values shows eg. Shop1,Shop2,Shop3,Shop4).
is there a way i can filter it with the values i am displaying.
$this->fields_list = array(
'id_push' => array('title' => $this->l('ID')),
'shops' => array('title' => $this->l('Shop(s)'),'callback' => 'getShopName','type'=>'editable')
);
You should include a concatenated shop name field in your controller SELECT. Then you should specify filter_key parameter in your shops fieldlist field. Something like this:
$this->_select = ' a.`correct_field_name` AS `shopnames_custom_field`';
$this->fields_list = array(
'id_push' => array('title' => $this->l('ID')),
'shops' => array('title' => $this->l('Shop(s)'),'callback' => 'getShopName','type'=>'editable', 'filter_key' => 'shopnames_custom_field')
);
If this solution does not work you should modify getList function to custom filter results.
Good luck

Implementing a custom filter in SugarCRM based on relationship ID

I've defined a many-to-many relationship between Accounts and a custom module (Tags). Tags have a subpanel in Accounts; however, I need a custom filter on Accounts that will show all the Accounts records related to the Tag name you type in.
/custom/Extension/modules/Accounts/Ext/clients/base/basic/filterAccountsByTag.php
$viewdefs['Account']['base']['filter']['basic']['filters'][] = array(
'id' => 'filterAccountsByTag',
'name' => 'LBL_FILTER_ACCOUNTS_BY_TAG',
'filter_definition' => array(
array(
'custom_tags_accountscustom_tags_ida' => array(
'$equals' => ' ',
),
),
array(
'name' => ''
)
),
'editable' => false,
'is_template' => false
);
To sum it up, I want to display the primary module results based upon a filter from a sub-module. Has anyone been through this? This is not a relate field; it's a relationship.
I hope this below article will help you.
Apply initial filter in relate fields and relationship fields with quick(popup) search sugarcrm 7.x

adding quantity to related products in magento backend

I'm new to magento and trying to add quantity column to the grid in related products tab (edit product -> related products).
this is what I did:
overwrite related.php file:
Mage\Adminhtml\Block\Catalog\Product\Edit\Tab\Related.php
added to _prepareCollection() method this code:
$collection->joinField(
'qty',
'cataloginventory_stock_item',
'qty',
'product_id = entity_id',
'{{table}}.stock_id = 1',
'left'
);
and added to _prepareColumns() methods this code:
$this->addColumn('qty',
array(
'header' => Mage::helper('catalog')->__('QTY'),
'width' => 80,
'index' => 'qty'
Now I can see the new column but the quantity is float number (for example 100.00) and I can't filter results based on my new QTY column.
My questions:
is that all I need to add column or I have to do some thing else??
how to display QTY in integer format (for example 100 not 100.00)??
why I can't filter results based on the QTY??
any idea will be appreciated, Thanks in advance..
Quantity as integer format
'getter' => array($this, 'getFormattedQty')
public function getFormattedQty($row)
{
return intVal($row->getQtyOrdered());
}
To cover the three questions simply add the 'type' option with the value set to 'number' in the to _prepareColumns() method. Example Below:
$this->addColumn('qty', array(
'header' => Mage::helper('catalog')->__('QTY'),
'type' => 'number',
'width' => 80,
'index' => 'qty'
));
This will set the value as a whole number or integer rather than a float and will let you filter for a particular range.
I have used this myself to add the QTY to the Associated Products grid.

CakePHP - Find parent based on child

I have three tables companies, products and prices.
Companies hasMany Products
Products hasMany Prices
I'm currently trying to search these tables for products that are less than a certain price, the code I'm trying is:
$results = $this->Product->find('all', array(
'conditions' => array(
'Price.price <=' 10
)
));
For some reason using this code cakephp this brings back an error:
Unknown column 'Price.price' in 'where clause'
I think this is because products have multiple prices (it's joining with the companies table not the prices table), can anyone tell me why this is going wrong and know how to get it working?
Note: After getting this working, I want to be able to find products based on criteria from the prices and products tables, then display data from all 3 in a results page.
I thought about first searching the prices table and then searching the products table, but I believe there must be a more efficient way?
You may use adhoc-joins for these types of queries. Cake doesn't do joins for 1:n or n:n relationships, so you have to specify it manually.
$results = $this->Product->find('all', array(
'joins' => array(
array(
'alias' => 'Price',
'table' => 'prices',
'foreignKey' => false,
'conditions' => array('Price.product_id = Product.id'),
),
),
'conditions' => array(
'Price.price <=' => 10,
),
));
In your place I would do this:
$results = $this->Product->Price->find('all', array(
'recursive' => 2,
'conditions' => array(
'Price.price <=' => 10
)
));
HasMany does not joins table but belongsTo does.

Categories