海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
4.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\AdminSupplier\Metrics\Examples;
  3. use App\Common\DataTime;
  4. use App\Common\OrderStatus;
  5. use App\Models\Order;
  6. use App\Models\OrderProductItem;
  7. use Carbon\Carbon;
  8. use Dcat\Admin\Admin;
  9. use Dcat\Admin\Widgets\Metrics\Round;
  10. use Illuminate\Http\Request;
  11. class ProductOrders extends Round
  12. {
  13. /**
  14. * 初始化卡片内容
  15. */
  16. protected function init()
  17. {
  18. parent::init();
  19. $this->title('订单状态统计');
  20. $this->chartLabels(['已付定金', '已付款', '已完成']);
  21. $this->dropdown([
  22. //'day' => '本日',
  23. 'week' => '本周',
  24. 'month' => '本月',
  25. 'year' => '今年',
  26. ]);
  27. }
  28. /**
  29. * 处理请求
  30. *
  31. * @param Request $request
  32. *
  33. * @return mixed|void
  34. */
  35. public function handle(Request $request)
  36. {
  37. $orders = OrderProductItem::query()
  38. ->with('product')
  39. ->where('supplier_id', Admin::user()->id);
  40. switch ($request->get('option')) {
  41. case 'day':
  42. $orders->whereDate('created_at', Carbon::today());
  43. break;
  44. case 'week':
  45. $time = DataTime::beginAndEnd('week');
  46. $orders->whereBetween('created_at', $time);
  47. break;
  48. case 'month':
  49. $orders->whereMonth('created_at', Carbon::now()->month);
  50. break;
  51. case 'year':
  52. $orders->whereYear('created_at', Carbon::now()->year);
  53. break;
  54. default:
  55. $time = DataTime::beginAndEnd('week');
  56. $orders->whereBetween('created_at', $time);
  57. }
  58. $count = clone $orders;
  59. $payEarnest = clone $orders;
  60. $pay = clone $orders;
  61. $success = clone $orders;
  62. $payEarnest = $payEarnest->whereHas('order', function ($query) {
  63. $query->whereIn('status', [
  64. OrderStatus::PAY_EARNEST
  65. ]);
  66. })
  67. ->count();
  68. $pay = $pay->whereHas('order', function ($query) {
  69. $query->whereIn('status',[
  70. OrderStatus::PAID,
  71. OrderStatus::PAID_RETAINAGE,
  72. OrderStatus::OFFLINE_PAID,
  73. ]);
  74. })
  75. ->count();
  76. $success = $success->whereHas('order', function ($query) {
  77. $query->whereIn('status', [
  78. OrderStatus::SUCCESS
  79. ]);
  80. })
  81. ->count();
  82. $count = $count->whereHas('order', function ($query) {
  83. $query->whereIn('status', [
  84. OrderStatus::PAY_EARNEST,
  85. OrderStatus::PAID,
  86. OrderStatus::PAID_RETAINAGE,
  87. OrderStatus::OFFLINE_PAID,
  88. OrderStatus::SUCCESS
  89. ]);
  90. })
  91. ->count();
  92. // 卡片内容
  93. $this->withContent($payEarnest, $pay, $success);
  94. $payEarnestPer = $count > 0 ? bcdiv($payEarnest, $count, 2) * 100 : 0;
  95. $payPer = $count > 0 ? bcdiv($pay, $count, 2) * 100 : 0;
  96. $successPer = $count > 0 ? bcdiv($success, $count, 2) * 100 : 0;
  97. // 图表数据
  98. $this->withChart([$payEarnestPer, $payPer, $successPer]);
  99. // 总数
  100. $this->chartTotal('总计', $count);
  101. }
  102. /**
  103. * 设置图表数据.
  104. *
  105. * @param array $data
  106. *
  107. * @return \App\AdminSupplier\Metrics\Examples\ProductOrders
  108. */
  109. public function withChart(array $data)
  110. {
  111. return $this->chart([
  112. 'series' => $data,
  113. ]);
  114. }
  115. /**
  116. * 卡片内容.
  117. *
  118. * @param int $finished
  119. * @param int $pending
  120. * @param int $rejected
  121. *
  122. * @return $this
  123. */
  124. public function withContent($finished, $pending, $rejected)
  125. {
  126. return $this->content(
  127. <<<HTML
  128. <div class="col-12 d-flex flex-column flex-wrap text-center" style="max-width: 220px">
  129. <div class="chart-info d-flex justify-content-between mb-1 mt-2" >
  130. <div class="series-info d-flex align-items-center">
  131. <i class="fa fa-circle-o text-bold-700 text-primary"></i>
  132. <span class="text-bold-600 ml-50">已付定金</span>
  133. </div>
  134. <div class="product-result">
  135. <span>{$finished}</span>
  136. </div>
  137. </div>
  138. <div class="chart-info d-flex justify-content-between mb-1">
  139. <div class="series-info d-flex align-items-center">
  140. <i class="fa fa-circle-o text-bold-700 text-warning"></i>
  141. <span class="text-bold-600 ml-50">已付款</span>
  142. </div>
  143. <div class="product-result">
  144. <span>{$pending}</span>
  145. </div>
  146. </div>
  147. <div class="chart-info d-flex justify-content-between mb-1">
  148. <div class="series-info d-flex align-items-center">
  149. <i class="fa fa-circle-o text-bold-700 text-danger"></i>
  150. <span class="text-bold-600 ml-50">已完成</span>
  151. </div>
  152. <div class="product-result">
  153. <span>{$rejected}</span>
  154. </div>
  155. </div>
  156. </div>
  157. HTML
  158. );
  159. }
  160. }