PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.25
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.25
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / app / Modules / ReportingModule / OrdersReport.php

OrdersReport.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.25, at app/Modules/ReportingModule/OrdersReport.php

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