Dashboard.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace Webkul\Admin\Helpers;
  3. use Illuminate\Database\Eloquent\Collection as EloquentCollection;
  4. use Illuminate\Support\Carbon;
  5. use Illuminate\Support\Collection;
  6. use Webkul\Admin\Helpers\Reporting\Customer;
  7. use Webkul\Admin\Helpers\Reporting\Product;
  8. use Webkul\Admin\Helpers\Reporting\Sale;
  9. use Webkul\Admin\Helpers\Reporting\Visitor;
  10. class Dashboard
  11. {
  12. /**
  13. * Create a controller instance.
  14. *
  15. * @return void
  16. */
  17. public function __construct(
  18. protected Sale $saleReporting,
  19. protected Product $productReporting,
  20. protected Customer $customerReporting,
  21. protected Visitor $visitorReporting
  22. ) {}
  23. /**
  24. * Returns the overall statistics.
  25. */
  26. public function getOverAllStats(): array
  27. {
  28. return [
  29. 'total_customers' => $this->customerReporting->getTotalCustomersProgress(),
  30. 'total_orders' => $this->saleReporting->getTotalOrdersProgress(),
  31. 'total_sales' => $this->saleReporting->getTotalSalesProgress(),
  32. 'avg_sales' => $this->saleReporting->getAverageSalesProgress(),
  33. 'total_unpaid_invoices' => [
  34. 'total' => $total = $this->saleReporting->getTotalPendingInvoicesAmount(),
  35. 'formatted_total' => core()->formatBasePrice($total),
  36. ],
  37. ];
  38. }
  39. /**
  40. * Returns the today statistics.
  41. */
  42. public function getTodayStats(): array
  43. {
  44. $orders = $this->saleReporting->getTodayOrders();
  45. $orders = $orders->map(function ($order) {
  46. return [
  47. 'id' => $order->id,
  48. 'increment_id' => $order->id,
  49. 'status' => $order->status,
  50. 'status_label' => $order->status_label,
  51. 'payment_method' => core()->getConfigData('sales.payment_methods.'.$order->payment->method.'.title'),
  52. 'base_grand_total' => $order->base_grand_total,
  53. 'formatted_base_grand_total' => core()->formatBasePrice($order->base_grand_total),
  54. 'channel_name' => $order->channel_name,
  55. 'customer_email' => $order->customer_email,
  56. 'customer_name' => $order->customer_full_name,
  57. 'items' => view('admin::sales.orders.items', compact('order'))->render(),
  58. 'billing_address' => $order?->billing_address->city.($order?->billing_address->country ? ', '.core()->country_name($order?->billing_address->country) : ''),
  59. 'created_at' => $order->created_at->format('d M Y, H:i:s'),
  60. ];
  61. });
  62. return [
  63. 'total_sales' => $this->saleReporting->getTodaySalesProgress(),
  64. 'total_orders' => $this->saleReporting->getTodayOrdersProgress(),
  65. 'total_customers' => $this->customerReporting->getTodayCustomersProgress(),
  66. 'orders' => $orders,
  67. ];
  68. }
  69. /**
  70. * Returns the today statistics.
  71. *
  72. * @return \Illuminate\Database\Eloquent\Collection
  73. */
  74. public function getStockThresholdProducts()
  75. {
  76. $products = $this->productReporting->getStockThresholdProducts(5);
  77. $products = $products->map(function ($product) {
  78. return [
  79. 'id' => $product->product_id,
  80. 'sku' => $product->product->sku,
  81. 'name' => $product->product->name,
  82. 'price' => $product->product->price,
  83. 'formatted_price' => core()->formatBasePrice($product->product->price),
  84. 'total_qty' => $product->total_qty,
  85. 'image' => $product->product->base_image_url,
  86. ];
  87. });
  88. return $products;
  89. }
  90. /**
  91. * Returns sales statistics.
  92. */
  93. public function getSalesStats(): array
  94. {
  95. return [
  96. 'total_orders' => $this->saleReporting->getTotalOrdersProgress(),
  97. 'total_sales' => $this->saleReporting->getTotalSalesProgress(),
  98. 'over_time' => $this->saleReporting->getCurrentTotalSalesOverTime(),
  99. ];
  100. }
  101. /**
  102. * Returns visitors statistics.
  103. */
  104. public function getVisitorStats(): array
  105. {
  106. return [
  107. 'total' => $this->visitorReporting->getTotalVisitorsProgress(),
  108. 'unique' => $this->visitorReporting->getTotalUniqueVisitorsProgress(),
  109. 'over_time' => $this->visitorReporting->getCurrentTotalVisitorsOverTime(),
  110. ];
  111. }
  112. /**
  113. * Returns top selling products statistics.
  114. */
  115. public function getTopSellingProducts(): Collection
  116. {
  117. return $this->productReporting->getTopSellingProductsByRevenue(5);
  118. }
  119. /**
  120. * Returns top customers statistics.
  121. */
  122. public function getTopCustomers(): EloquentCollection
  123. {
  124. $customers = $this->customerReporting->getCustomersWithMostSales(5);
  125. $customers->map(function ($customer) {
  126. $customer->formatted_total = core()->formatBasePrice($customer->total);
  127. });
  128. return $customers;
  129. }
  130. /**
  131. * Get the start date.
  132. *
  133. * @return \Carbon\Carbon
  134. */
  135. public function getStartDate(): Carbon
  136. {
  137. return $this->saleReporting->getStartDate();
  138. }
  139. /**
  140. * Get the end date.
  141. *
  142. * @return \Carbon\Carbon
  143. */
  144. public function getEndDate(): Carbon
  145. {
  146. return $this->saleReporting->getEndDate();
  147. }
  148. /**
  149. * Returns date range
  150. */
  151. public function getDateRange(): string
  152. {
  153. return $this->getStartDate()->format('d M').' - '.$this->getEndDate()->format('d M');
  154. }
  155. }