Skip to content

Instantly share code, notes, and snippets.

@oliverlundquist
Created January 19, 2016 02:40
Show Gist options
  • Save oliverlundquist/6f7c7c5f95e6b3cc7b70 to your computer and use it in GitHub Desktop.
Save oliverlundquist/6f7c7c5f95e6b3cc7b70 to your computer and use it in GitHub Desktop.
<?php
function monthly($from, $to)
{
$months = $this->monthsBetween($from, $to);
$salesData = [];
if (count($months) > 6) {
return null;
}
foreach ($months as $month) {
$orderIds = json_decode(json_encode($this->getOrderIds($month)), true);
if (count($orderIds) > 0) {
$orderTotal = (array)$this->sqlDatabaseManager
->table('order_totals')
->select($this->sqlDatabaseManager->raw('
CAST(SUM(`cost`) as UNSIGNED) as `cost`,
CAST(SUM(`subtotal`) as UNSIGNED) as `subtotal`,
CAST(SUM(`shipping_fee`) as UNSIGNED) as `shipping_fee`,
CAST(SUM(`payment_fee`) as UNSIGNED) as `payment_fee`,
CAST(SUM(`wrapping_total`) as UNSIGNED) as `wrapping_total`,
CAST(SUM(`sum`) as UNSIGNED) as `sum`,
CAST(SUM(`points_discount`) as UNSIGNED) as `points_discount`,
CAST(SUM(`total`) as UNSIGNED) as `total`
'))
->whereIn('order_id', $orderIds)
->orderBy($this->sort, $this->direction)
->skip($this->offset)
->take($this->limit)
->first();
$orderTotal['date'] = $month;
$salesData[] = $orderTotal;
}
}
return $salesData;
}
function monthsBetween($from, $to) {
$from = (int)strtotime($from);
$to = (int)strtotime($to);
$dates = $from < $to ? [$from, $to] : [$to, $from];
$fromDate = date('Y-m', $dates[0]);
$toDate = date('Y-m', $dates[1]);
// calculate months between
$months = [];
while (true) {
$currentDate = date('Y-m', (int)strtotime('+' . $monthsBetween . ' month', (int)strtotime($fromDate)));
if ($currentDate === $toDate || count($months) > 6) {
return $months;
}
$months[] = $currentDate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment