I'm trying to pull out the sales totals for quarter 1. I can't seem to get this to work. Any suggestions?
$total = 0;
$orders = Mage::getModel('sales_order/collection')
->addAttributeToSelect('*')
->addAttributeToFilter('created_at', array(
'from' => '2012-01-01',
'to' => '2012-03-31'));
foreach ($orders as $order) {
$total += $order->getGrandTotal();
}
echo $total;
You're not getting the collection properly. There are multiple ways to do this, looks like you might've combined a couple methods:
Mage::getModel('sales/order')->getCollection() or
Mage::getResourceModel('sales/order_collection')
However, if all you really want is to sum the single attribute grand_total it is way more efficient to build your own query rather that loading the entire sales order collection:
$db = Mage::getSingleton('core/resource')->getConnection('core_read');
$salesTable = Mage::getSingleton('core/resource')->getTableName('sales/order');
list($total) = $db->fetchCol(
$db->select()
->from($salesTable, array('grand_total' => 'SUM(grand_total)'))
->where('DATE(created_at) >= ?', '2012-01-01')
->where('DATE(created_at) <= ?', '2012-03-31')
);
echo $total;
Related
I have the next issue - I have a table invoices and a table with receipts. An invoice is created by an agent and I want to get the sold for each agent but the numbers are wrong.
Here is what I've tried:
$agents = Agent::get();
$invoices_receipts_agent = array();
foreach ($agents as $agent) {
$payment_invoice = 0;
$payment_recepit = 0;
$id_agent = $agent->id_agent;
$invoices = Invoice::whereAgent_id($id_agent)->get();
foreach ($invoices as $invoice) {
$payment_invoice = $payment_invoice + $invoice->total_pay;
$recepits = Recepit::whereInvoice_id($invoice->id_invoice)->get();
if (count($recepits) > 0) {
foreach ($recepits as $recepit) {
$payment_recepit = $payment_recepit + $recepit->amount_payd;
}
}
}
$total = $payment_invoice - $payment_recepit;
$total_agents = ['name' => $agent->name, 'total' => $total];
array_push($invoices_receipts_agent, $total_agents);
}
I made a test and created two invoices for the agent with ID 5
First invoice: 10
Second invoice : 20
Total invoices: 30
Then I did a recepit for the second invoice and found the expected total:
Total: 10 + 20 - 20 = 10 (correct total)
And that's great, but I have an agent with 3600 invoices and something is wrong the total. The total (total = invoices - recepits) is too big, but I can't figure out why.
Extra detail: the fields for the numbers are float.
First of all, you have an easier way to handle this issue using Eloquent Relationships.
In this case, can define One-to-Many relationship between Agent and Invoice as:
class Agent {
...
function invoices(){
return $this->hasMany('App-Namespace\Invoice')
}
}
...and define the inverse relationship on Invoice.
Then, you must do same between Invoice and Receipt models, since an Invoice can have one to many receipts.
So, if Agents table primary key is id you could say:
$agent = Agent::find($agent_id)->invoices->get();
...to get an agent invoices; or:
$invoice = Invoice::find($invoice_id)->receipts->get();
...to get all receipts for a specific invoice.
And finally implement your code:
$agents = Agent::all();
$invoices_receipts_agent = array();
foreach ($agents as $agent) {
$payment_invoice = 0;
$invoices = $agent->invoices->get();
foreach ($invoices as $invoice){
$payment_invoice += $invoice->total_pay;
$payment_receipt += $invoice->receipts->sum('amount_paid');
}
$total = $payment_invoice + $payment_receipt;
$invoices_receipts_agent[] = ['name' => $agent->name, 'total' => $total];
}
Note: I have used sum collection function to get sum of values of column amount_paid of a particular receipt. You could do same to get sum of total_pay column of an invoice like:
$total_paid = $agent->invoices()->sum('total_pay');
Can you confirm it this current version of your code?
I see some typos like in:
$recepits = Recepit::whereInvoice_id($invoice->id_invoice)->get();
if (count($receipts) > 0) {
Here we have issues:
Variable is named $recepits, but then called in next line with other name ($receipts).
$receipts should be a collection (result of Eloquent query) not an array. So to get count you have to say: $receipts->count()
If this is your final code, those are definitly error which are affecting your result.
$categories = PostCategoryQuery::create()->find();
$categories_array = [];
foreach ($categories as $k => $category):
$posts_count = PostQuery::create()->filterByPostCategory($category)->count();
$categories_array[$k]['category'] = $category->getName();
$categories_array[$k]['count'] = $posts_count;
endforeach;
uasort($categories_array, "sortByCount");
function sortByCount($a, $b) {
return $a['count'] > $b['count'] ? -1 : 1;
}
I want to get 'categories' array order by numbers of its related posts, I think there must be a way to make my code shorter, hoping get some suggestions, thanks~
If in your ORM you could use a simple query with group by you simply could perform a select like
select category, count(*)
from post_category
inner join post on post_category.category_id = post.category_id
group by category
order by count(*)
and then your code will be reduced to the query..
I don't know your model exactly, but I would recommend something like that:
$categories_array = PostQuery::create('pq')
->joinPostCategory('pc')
->withColumn('COUNT(pq.id)', 'count')
->groupBy('pc.id')
->select(array('pc.name', 'count'))
->orderBy('count')
->find();
// Should return PropelArrayCollection(
// array('pc.name' => 'category1', 'count' => 123),
// array('pc.name' => 'category2', 'count' => 234)
// )
This way you avoid object hydrating as you do only select the columns that you need.
Is it possible to return the total count along with the results ?
Currently using this DQL query to get my array results but I also need to return a total count with each result.
$dql = "SELECT a, a.id, a.status, a.departDate, a.flightNum, a.departHour, a.arrivedHour, a.convocHour, a.convocPlace, a.customerPax, a.customerNote, a.providerPax,a.providerNote
FROM AppBundle:Assistance a GROUP BY a.id";
$query = $this->getEntityManager()->createQuery($dql)
->setFirstResult($skip)
->setMaxResults($take);
$paginator = new Paginator($query, $fetchJoinCollection = true);
$c = count($paginator);
dump($paginator->getIterator()->getArrayCopy());
return $paginator->getIterator()->getArrayCopy();
I don't think is possible, to get results and total count in one go, basically under the hood, doctrine perform two queries:
First one to get items:
SELECT * FROM tbl
// and optional clausules
WHERE tbl.active = 1
GROUP BY tbl.type
ORDER BY tbl.rank
// taking one page
LIMIT 10
OFFSET 20
second one for count:
SELECT COUNT(*) FROM tbl
// and optional clousules
WHERE tbl.active = 1
GROUP BY tbl.type
You can check that in symfony2 profiler.
So you have few options to choose:
Return array or some kind of wrapper object
return array("count" => count($paginator), "items" => $paginator->getIterator()->getArrayCopy());
or set it manually for each item if you really need that
$count = count($paginator);
$items = $paginator->getIterator()->getArrayCopy();
foreach($items as $item) {
$item->setCount($count);
}
or why don't just return $paginator object?
You can also use KnpPaginatorBundle:
public function getItemsAction(Request $request)
{
$itemsQuery = $this->repository->getItems();
$current = $request->get('page', 1);
$limit = $request->get('limit', 10);
$pagination = $this->paginator->paginate($itemsQuery, $current, $limit);
$pagerResult = [
'count' => $pagination->getTotalItemCount(),
'items' => $pagination->getItems(),
'limit' => $pagination->getItemNumberPerPage(),
'current' => $pagination->getCurrentPageNumber()
];
return new JsonResponse($pagerResult);
}
I'm trying to total a bill balance with a program I'm working on in PHP.
The code I use to pull the pricing is as such.
public function PackagePricing($arg) {
$query = <<<SQL
SELECT packageID
FROM customer_packages
WHERE active = :true
AND customerID = :arg
SQL;
$resource = $this->db->db->prepare( $query );
$resource->execute( array (
":true" => 1,
":arg" => 1,
));
foreach($resource as $row) {
self::GetPricing($row['packageID']);
}
}
public function GetPricing($arg) {
$query = <<<SQL
SELECT price
FROM products
WHERE id = :arg
SQL;
$resource = $this->db->db->prepare( $query );
$resource->execute( array (
":arg" => $arg,
));
$totalBill = 0;
foreach($resource as $row) {
$totalBill+= $row['price'];
}
echo $totalBill;
}
Now by my understanding this should work, but what I'm getting in turn is:
On the right you can see the billing total and rather than totaling out it's giving me each individually.
The error seems quite obvious. Here's your sequence in different words :
Get all packages ID.
Foreach package ID, get the price of the item (only one result is returned)
For that single result, add up all the prices (you only get one). Print it and go back to 2.
What you see is not 2 prices that have been added as strings in some sort of way. You simply prints subsequently 2 different prices. 10 & 30
GetPricing should return a price, and the foreach loop that calls it should make the sum.
$total = 0;
foreach($resource as $row)
{
$total += self::GetPricing($row['packageID']);
}
Hope this helps.
i'm trying to save or update, when retrivieng mutliple records, but can't get it work..
When doing this, at first it look like it worked:
$c = Customer::select('Name', 'City');
$c[0]->update(array('Name' => 'new Name'));
var_dump($c[0]['Name']);
But after a refresh and test it like that, i still see the oldname:
$c = Customer::select('Name', 'City');
var_dump($c[0]['Name']);
Somebody knows what i'm doing wrog?
i Have retrieve a json thing:
data[0][Name]:Afrifield Corporation
data[0][City]:Maidstone
data[1][Name]:Amann Informatik AG
data[1][City]:Reinach BL
data[2][Name]:Antarcticopy
data[2][City]:Antwerpen
if also have the columns Name and City which i want to update with this retrieved array..
so last question there is no faster solution than:
$custNo = Customer::select('Name', 'No_')->get();
for ($i = 0; $i < count($custNo); $i++) {
$c = Customer::select('Name', 'City')->where('No_','=',$custNo[$i]->No_);
$c->update(array('Name' => $input['data'][$i]['Name']));
}
No_ is my PrimaryKey i also set this in the model class..
Instead of get() and $c[0], try using first(). You also need to re-fetch after updating. And it's object, not an array. So $c['Name'] won't work unless you alter it to an array.
$customers = Customer::select('Name', 'City')->where('No_','=','DKT000142')->get();
foreach ($customers as $customer) {
$customer->update(array('Name' => 'new Name'));
//you need to re-fetch for the updated data, assuming you have an id field
$currentCustomer = Customer::find($customer->id);
echo var_dump($currentCustomer->Name);
}
Edit: Also for mass updating, you can try something like:
$update = Customer::where('No_','=','DKT000142')->update(array('Name' => 'new Name'));
Edit2: To both get mass update and get updated records, this is the best approach I can think of:
//First, we mass-update
$update = Customer::where('No_','=','DKT000142')->update(array('Name' => 'new Name'));
//Then we get the updated data and loop:
$customers = Customer::select('Name', 'City')->where('No_','=','DKT000142')->get();
foreach ($customers as $customer) {
echo var_dump($currentCustomer->Name);
}
$c[0]->name = "New name";
$c[0]->save();