StatKindController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\goods\classes\KindClass;
  4. use bizHd\item\classes\ItemClass;
  5. use bizHd\order\classes\OrderClass;
  6. use bizHd\order\classes\OrderGoodsClass;
  7. use bizHd\part\classes\PartClass;
  8. use bizHd\part\classes\PartItemClass;
  9. use bizHd\work\classes\WorkClass;
  10. use common\components\dateUtil;
  11. use Yii;
  12. use common\components\util;
  13. class StatKindController extends BaseController
  14. {
  15. //各渠道收入 ssh 20220711
  16. public function actionProfile()
  17. {
  18. $get = Yii::$app->request->get();
  19. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  20. $startTime = $get['startTime'] ?? '';
  21. $endTime = $get['endTime'] ?? '';
  22. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  23. $start = $period['startTime'];
  24. $end = $period['endTime'];
  25. $currentStartDate = date('Y-m-d', strtotime($start));
  26. $currentEndDate = date('Y-m-d', strtotime($end));
  27. $currentStartTime = $currentStartDate . ' 00:00:00';
  28. $currentEndTime = $currentEndDate . ' 23:59:59';
  29. $mainId = $this->mainId;
  30. $kindList = KindClass::getAllByCondition(['mainId' => $mainId, 'flower' => 1, 'delStatus' => 0, 'status' => 1], null, '*', 'id');
  31. $kindStat = [];
  32. $where = ['mainId' => $mainId, 'status' => 1];
  33. $where['finishTime'] = ['between', [$currentStartTime, $currentEndTime]];
  34. $workList = WorkClass::getAllByCondition($where, null, '*', null, true);
  35. if (!empty($workList)) {
  36. foreach ($workList as $work) {
  37. $kindId = $work->kindId ?? 0;
  38. if (empty($kindId)) {
  39. continue;
  40. }
  41. $kindName = $kindList[$kindId]['name'] ?? '';
  42. $num = $work->num ?? 0;
  43. $flowerNum = $work->flowerNum ?? 0;
  44. $totalFlowerNum = bcmul($flowerNum, $num);
  45. if (isset($kindStat[$kindId]['makeNum'])) {
  46. $kindStat[$kindId]['makeNum'] = bcadd($kindStat[$kindId]['makeNum'], $num);
  47. $kindStat[$kindId]['flowerNum'] = bcadd($totalFlowerNum, $kindStat[$kindId]['flowerNum']);
  48. } else {
  49. $kindStat[$kindId]['makeNum'] = $num;
  50. $kindStat[$kindId]['flowerNum'] = $totalFlowerNum;
  51. $kindStat[$kindId]['kindName'] = $kindName;
  52. if (isset($kindStat[$kindId]['saleNum']) == false) {
  53. $kindStat[$kindId]['saleNum'] = 0;
  54. }
  55. }
  56. }
  57. }
  58. $where = ['mainId' => $mainId, 'payStatus' => 1, 'payTime' => ['between', [$currentStartTime, $currentEndTime]]];
  59. $orderList = OrderClass::getAllByCondition($where, null, '*', null, true);
  60. if (!empty($orderList)) {
  61. foreach ($orderList as $order) {
  62. $orderSn = $order->orderSn ?? '';
  63. $sonList = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  64. if (!empty($sonList)) {
  65. foreach ($sonList as $son) {
  66. $flower = $son->flower ?? 0;
  67. if ($flower == 0) {
  68. continue;
  69. }
  70. $kindId = $son->kindId ?? 0;
  71. if (empty($kindId)) {
  72. continue;
  73. }
  74. $num = $son->num ?? 0;
  75. $kindName = $kindList[$kindId]['name'] ?? '';
  76. if (isset($kindStat[$kindId]['saleNum'])) {
  77. $kindStat[$kindId]['saleNum'] = bcadd($kindStat[$kindId]['saleNum'], $num);
  78. } else {
  79. $kindStat[$kindId]['saleNum'] = $num;
  80. $kindStat[$kindId]['kindName'] = $kindName;
  81. if (isset($kindStat[$kindId]['makeNum']) == false) {
  82. $kindStat[$kindId]['makeNum'] = 0;
  83. $kindStat[$kindId]['flowerNum'] = 0;
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
  90. $itemList = ItemClass::getAllByCondition(['mainId' => $mainId, 'delStatus' => 0], null, 'id,name,mainId,delStatus,ratio', 'id');
  91. $partStat = [];
  92. $partList = PartClass::getAllByCondition(['mainId' => $mainId, 'addTime' => ['between', [$currentStartTime, $currentEndTime]]], null, '*', null, true);
  93. if (!empty($partList)) {
  94. foreach ($partList as $part) {
  95. $orderSn = $part->orderSn ?? '';
  96. $partItemList = PartItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  97. if (!empty($partItemList)) {
  98. foreach ($partItemList as $pItem) {
  99. $productId = $pItem->productId ?? 0;
  100. $ratio = $itemList[$productId]['ratio'] ?? 10;
  101. $ratioType = $itemList[$productId]['ratioType'] ?? 0;
  102. if ($ratioType == 1) {
  103. continue;
  104. }
  105. $name = $itemList[$productId]['name'] ?? '';
  106. $num = $pItem->itemNum ?? 0;
  107. $smallNum = bcmul($ratio, $num);
  108. if (isset($partStat[$productId]['num'])) {
  109. $partStat[$productId]['num'] = bcadd($partStat[$productId]['num'], $num);
  110. $partStat[$productId]['smallNum'] = bcadd($partStat[$productId]['smallNum'], $smallNum);
  111. } else {
  112. $partStat[$productId]['num'] = $num;
  113. $partStat[$productId]['smallNum'] = $smallNum;
  114. $partStat[$productId]['name'] = $name;
  115. }
  116. }
  117. }
  118. }
  119. }
  120. util::success(['kindStat' => $kindStat, 'partStat' => $partStat]);
  121. }
  122. }