Add total cost field in sales order report magento - magento

I have tried to get total cost in sales order report but without success. I can see the related data in the "sales_order_aggregated_created" and "sales_order_aggregated_updated" tables in database but unable to see total cost of orders. The data is being managed through app/code/core/Mage/Sales/Model/Resource/Order/collection.php
Please help if any one have idea to get the same value.

Finally I have done with the same. I have made corrections in three files to get the total cost field in sales order report. First of all copy this three core files to local folder.
1.Add total cost app/code/local/Mage/Sales/Model/Resource/Report/Order/Createdat.php in columns array around line number 95
'total_base_cost' => new Zend_Db_Expr('SUM(oi.total_base_cost)')
Dont forget to add same column (total_base_cost) in sales_order_aggregated_created table in databse. Now the $columns would be as below:
$columns = array(
// convert dates from UTC to current admin timezone
'period' => $periodExpr,
'store_id' => 'o.store_id',
'order_status' => 'o.status',
'orders_count' => new Zend_Db_Expr('COUNT(o.entity_id)'),
'total_qty_ordered' => new Zend_Db_Expr('SUM(oi.total_qty_ordered)'),
'total_base_cost' => new Zend_Db_Expr('SUM(oi.total_base_cost)'),
'total_qty_invoiced' => new Zend_Db_Expr('SUM(oi.total_qty_invoiced)'),
'total_income_amount' => new Zend_Db_Expr(
sprintf('SUM((%s - %s) * %s)',
$adapter->getIfNullSql('o.base_grand_total', 0),
$adapter->getIfNullSql('o.base_total_canceled',0),
$adapter->getIfNullSql('o.base_to_global_rate',0)
)
),
and add same column in $cols array to get the value from sales/order_item table around line number 204
'total_base_cost' => new Zend_Db_Expr('SUM(base_cost)')
now $cols array is:
$cols = array(
'order_id' => 'order_id',
'total_qty_ordered' => new Zend_Db_Expr("SUM(qty_ordered - {$qtyCanceledExpr})"),
'total_base_cost' => new Zend_Db_Expr('SUM(base_cost)'),
'total_qty_invoiced' => new Zend_Db_Expr('SUM(qty_invoiced)'),
);
2. Add same column in app/code/local/Mage/Sales/Model/Resource/Report/Order/Collection.php arround line 87
'total_base_cost' => 'SUM(total_base_cost)',
now $this->_selected array will be as below:
$this->_selectedColumns = array(
'period' => $this->_periodFormat,
'orders_count' => 'SUM(orders_count)',
'total_qty_ordered' => 'SUM(total_qty_ordered)',
'total_base_cost' => 'SUM(total_base_cost)',
'total_qty_invoiced' => 'SUM(total_qty_invoiced)',
'total_income_amount' => 'SUM(total_income_amount)',
'total_revenue_amount' => 'SUM(total_revenue_amount)',
'total_profit_amount' => 'SUM(total_profit_amount)',
'total_invoiced_amount' => 'SUM(total_invoiced_amount)',
'total_canceled_amount' => 'SUM(total_canceled_amount)',
'total_paid_amount' => 'SUM(total_paid_amount)',
'total_refunded_amount' => 'SUM(total_refunded_amount)',
'total_tax_amount' => 'SUM(total_tax_amount)',
'total_tax_amount_actual' => 'SUM(total_tax_amount_actual)',
'total_shipping_amount' => 'SUM(total_shipping_amount)',
'total_shipping_amount_actual' => 'SUM(total_shipping_amount_actual)',
'total_discount_amount' => 'SUM(total_discount_amount)',
'total_discount_amount_actual' => 'SUM(total_discount_amount_actual)',
);
3. Final display the column in sales order report grid by adding the same column in the file app/code/local/Mage/Adminhtml/Block/Report/Sales/Sales/Grid.php
$this->addColumn('total_base_cost', array(
'header' => Mage::helper('sales')->__('Cost'),
'type' => 'currency',
'currency_code' => $currencyCode,
'index' => 'total_base_cost',
'total' => 'sum',
'sortable' => false,
'rate' => $rate,
));
It may help someone else who wants to add the same column.

Related

Magento 1.9: sort grid view

I've added a column to my gridview and it suppose to show remaining days of an event.
$this->addColumn('calculate_days', array(
'header' => Mage::helper('myodule')->__('Remaining'),
'type' => 'options',
'width' => '200px',
'options' => $options,
'frame_callback' => array($this, 'getRemaining'),
'filter_condition_callback' => array($this, '_calculateFilter')
));
getRemaining method:
public function getRemaining($value, $row, $column)
{
// some calculation to find out remaining days based on some conditions
return $days;
}
If I don't specify the index key, clicking on the Remaining column does not work. So I've added index key to addColumn like bellow:
$this->addColumn('calculate_days', array(
'header' => Mage::helper('myodule')->__('Remaining'),
'type' => 'options',
'width' => '200px',
'index' => 'created_at',
'options' => $options,
'frame_callback' => array($this, 'getRemaining'),
'filter_condition_callback' => array($this, '_calculateFilter')
));
now the sort is WORK but not correctly. when I specify the index, sort applied by index column(in this case will sort by created_at) but I want to sort by remaining days.
what should I do to solve this problem? or is there any sort callback?
Why don't you add this value as a new field in this grid? And then update this field values each day by a cronjob.
Then you will have in a native way the items sorted and also you avoid to recalculate each value each time you filter o see this grid.
Hope it helps,
Regards

Laravel 4 Database insert Error - preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

In database seeder, I just want to insert some hard-coded data to the table.
$Pro1_id = DB::table('projects')->select('id')->where('projectName', '=', 'Project A')->get();
$data1_1 = array(
'id' => 1,
'projectID' => $Pro1_id,
'attributeID' => 1,
'levelID' => 2,
'percentage' => 20,
'risk_value' => 25186.86311,
'expectation_value' => 706455.9401,
);
$data1_2 = array(
'projectID' => $Pro1_id,
'attributeID' => 2,
'levelID' => 1,
'percentage' => 60,
'risk_value' => 530351.3397,
'expectation_value' => 392207.1248,
);
$data1 = [$data1_1, $data1_2];
DB::table('Mapping')->insert($data1);
However, I got the error:
[ErrorException] preg_replace(): Parameter mismatch, pattern is a
string while replacement is an array
It is so weird, because I did the same to another table, it worked.
DB::table('projects')->insert(array(
array(
'id' => Webpatser\Uuid\Uuid::generate(),
'projectName' => 'Project A',
'creator_id' => $pro1_creatorID,
'create_at' => \Carbon\Carbon::now()->toDateString(),
'lastEditor_id' => $pro1_creatorID,
'edit_at' => \Carbon\Carbon::now()->toDateString(),
'utility' => 1.597119661,
'exponential' => 4.94,
'projectValue' => 1225090.39
),
array(
'id' => Webpatser\Uuid\Uuid::generate(),
'projectName' => 'Project B',
'creator_id' => $pro2_creatorID,
'create_at' => \Carbon\Carbon::create(2014, 12, 12)->toDateString(),
'lastEditor_id' => $pro2_creatorID,
'edit_at' => \Carbon\Carbon::create(2014, 12, 12)->toDateString(),
'utility' => 1.754989409,
'exponential' => 5.78,
'projectValue' => 293760.36
),
array(
'id' => Webpatser\Uuid\Uuid::generate(),
'projectName' => 'Project C',
'creator_id' => $pro3_creatorID,
'create_at' => \Carbon\Carbon::create(2013, 10, 21)->toDateString(),
'lastEditor_id' => $pro3_creatorID,
'edit_at' => \Carbon\Carbon::create(2013, 10, 21)->toDateString(),
'utility' => 1.423114267,
'exponential' => 4.15,
'projectValue' => 1461924.67
)
)
);
I really don't understand why inserting into projects table works, but the one of the mapping table does NOT work.
They are exactly the same method.
I think your code is correct but when you insert the id in array, you are doing the wrong way.
$Pro1_id = DB::table('projects')->select('id')->where('projectName', '=', 'Project A')->get();
Here, $Pro1_id is Collection that contain value return from your query. Sometimes it might be one, but sometimes it might be 2 or 3.... So , your are doing the wrong way when you are inserting the id in the array. So , use foreach loop like this :
foreach($Pro1_id as $pro){
DB::table('Mapping')->insert(array(
'id' => 1,
'projectID' => $pro->id,
'attributeID' => 1,
'levelID' => 2,
'percentage' => 20,
'risk_value' => 25186.86311,
'expectation_value' => 706455.9401,
));
}
For simple , get returns Collection and is rather supposed to fetch multiple rows.
For more info . Check this

Cakephp How to Make Request data contain only the selected field values

Iam Using Cakephp 2.4.6 version. Currently I am facing a problem with some fields in the forms. I have a group of horizontal controls in which the first control is a checkbox which hold the id value.if id is selected then only i want to get all other controls in that row.
is there any way other than getting all values into ajax and send thorough ajax.
TFmPlanProgram' => array(
(int) 0 => array(
't_fm_program_id' => '42',
'number_of_time' => '10'
),
(int) 1 => array(
't_fm_program_id' => '43',
'number_of_time' => '10'
),
(int) 2 => array(
't_fm_program_id' => '44',
'number_of_time' => '15'
),
(int) 3 => array(
't_fm_program_id' => '0',
'number_of_time' => ''
),
(int) 4 => array(
't_fm_program_id' => '0',
'number_of_time' => ''
),
This is my array. in that you can see that 3 and 4 having no id value. but it is passed to the server. i want to pass only the selected ID's.
You can try this before calling $this->TFmPlanProgram->save();
foreach($this->request->data['TFmPlanProgram'] as $key => $value){
if(empty($value['number_of_time'])){
unset($this->request->data['TFmPlanProgram'][$key]);
}
}

How to get the custom values and image in Magento in Topmenu?

I'm using magento 1.7.0.2. I have add the custom value in database. But how to retrive the custom value and image in Topmanu. I have tried in below mentioned code in the palce of 'my_attribute' to replace my attribute, but i din't get the result.
Model: Mage_Catalog_Model_Observer
Method: _addCategoriesToMenu()
$categoryData = array(
'name' => $category->getName(),
'id' => $nodeId,
//'url' => Mage::helper('catalog/category')->getCategoryUrl($category),
'is_active' => $this->_isActiveMenuCategory($category),
'my_attribute' => $category->getData('my_attribute') // Add our data in...
);
When i print the array i'll get this,
Array ( [name] => Matelas [id] => category-node-31 [is_active] => 1 [my_attribute] => )
Can any one guide me, Thanks in advance...
I am guessing you mean you have added a new custom attribute to the Category entity?
Becuase you are dealing with a Node_collection the full category object won't be loaded, try loading the full object to get what you're after:
$cat = Mage::getModel('catalog/category')->load($category->getId());
$categoryData = array(
name' => $category->getName(),
'id' => $nodeId,
//'url' => Mage::helper('catalog/category')->getCategoryUrl($category),
'is_active' => $this->_isActiveMenuCategory($category),
'my_attribute' => $cat->getData('my_attribute') // Add our data in...
);

Add product with custom option to existing Order

$quoteItem = Mage::getModel('sales/quote_item')->setProduct($product)
->setQuote(Mage::getModel('sales/quote')->load($order->getQuoteId()));
$orderItem = Mage::getModel('sales/convert_quote')->itemToOrderItem($quoteItem)->setProduct($product);
this is the code i use to add a simple product to existing order , but i am having issues adding custom option product to the order.
$quoteItem->addOption(new Varien_Object(
array(
'product' => $quoteItem->getProduct(),
'code' => 'option_ids',
'value' => 1 // 45,46,55
)
));
$quoteItem->addOption(new Varien_Object(
array(
'product' => $quoteItem->getProduct(),
'code' => 'option_1', //45
'value' => 2 // ‘White’
)
));
after the first line , but no success.
Any help please.
Thanks
There is one method to change exitsting order:
http://prattski.com/2013/04/22/magento-adding-items-to-existing-orders/

Resources