| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Modules\ReportingModule; |
| 4 |
|
| 5 |
use FluentCart\App\Helpers\Helper; |
| 6 |
use FluentCart\App\Helpers\Status; |
| 7 |
use FluentCart\App\Models\Order; |
| 8 |
use FluentCart\Framework\Support\Arr; |
| 9 |
|
| 10 |
class OrdersReport |
| 11 |
{ |
| 12 |
/** |
| 13 |
* Get Order Stats by a date range which will contain |
| 14 |
* total_orders, paid_orders, total_paid_order_items, total_paid_amounts |
| 15 |
* @param string $fromDate datetime string for from date for the report |
| 16 |
* @param string $toData datetime string for from date for the report. Default: Today's Date Time |
| 17 |
* @param bool $withCompare if provide the compare values or not |
| 18 |
* @return array contains |
| 19 |
*/ |
| 20 |
public function getOrderStats($fromDate, $toDate = false, $withCompare = false) |
| 21 |
{ |
| 22 |
if (!$toDate) { |
| 23 |
$toDate = gmdate('Y-m-d 23:59:59', current_time('timestamp')); |
| 24 |
} |
| 25 |
|
| 26 |
if (!$fromDate) { |
| 27 |
$fromDate = '1970-01-01 00:00:00'; |
| 28 |
} |
| 29 |
|
| 30 |
$params = [ |
| 31 |
"payment_status" => ["column" => "payment_status", "operator" => "in", "value" => Status::getTransactionSuccessStatuses()], |
| 32 |
"created_at" => ["column" => "created_at", "operator" => "between", "value" => [$fromDate, $toDate]] |
| 33 |
]; |
| 34 |
|
| 35 |
$currentOrders = Order::search(Arr::only($params, ['created_at']))->count(); |
| 36 |
|
| 37 |
$stat = Order::search($params) |
| 38 |
->selectRaw('count(*) as paid_orders') |
| 39 |
->selectRaw('sum(total_amount) as paid_amounts') |
| 40 |
->get()->first(); |
| 41 |
|
| 42 |
|
| 43 |
$paidOrders = $stat->paid_orders; |
| 44 |
$paidOrderItems = $stat->paid_orders; |
| 45 |
$paidAmounts = $stat->paid_amounts; |
| 46 |
|
| 47 |
if ($withCompare) { |
| 48 |
$dayDiff = strtotime($toDate) - strtotime($fromDate); |
| 49 |
$fromDate = gmdate('Y-m-d 00:00:00', strtotime($fromDate) - $dayDiff); |
| 50 |
$toDate = gmdate('Y-m-d 23:59:59', strtotime($toDate) - $dayDiff); |
| 51 |
Arr::set($params, 'created_at.value', [$fromDate, $toDate]); |
| 52 |
$prevCurrentOrders = Order::search(Arr::only($params, ['created_at']))->count(); |
| 53 |
$prevPaidOrders = Order::search($params)->count(); |
| 54 |
$prevPaidOrderItems = Order::search($params)->count(); |
| 55 |
$prevPaidAmounts = Order::search($params)->sum('total_amount'); |
| 56 |
} |
| 57 |
|
| 58 |
return [ |
| 59 |
'total_orders' => [ |
| 60 |
'title' => __('All Orders', 'fluent-cart'), |
| 61 |
'current_count' => $currentOrders, |
| 62 |
'compare_count' => ($withCompare) ? $prevCurrentOrders : null |
| 63 |
], |
| 64 |
'paid_orders' => [ |
| 65 |
'title' => __('Paid Orders', 'fluent-cart'), |
| 66 |
'current_count' => $paidOrders, |
| 67 |
'compare_count' => ($withCompare) ? $prevPaidOrders : null |
| 68 |
], |
| 69 |
'total_paid_order_items' => [ |
| 70 |
'title' => __('Paid Order Items', 'fluent-cart'), |
| 71 |
'current_count' => $paidOrderItems, |
| 72 |
'compare_count' => ($withCompare) ? $prevPaidOrderItems : null |
| 73 |
], |
| 74 |
'total_paid_amounts' => [ |
| 75 |
'title' => __('Order Value (Paid)', 'fluent-cart'), |
| 76 |
'current_count' => $paidAmounts, |
| 77 |
'compare_count' => ($withCompare) ? $prevPaidAmounts : null, |
| 78 |
'is_cents' => true |
| 79 |
] |
| 80 |
]; |
| 81 |
} |
| 82 |
} |
| 83 |
|