GoodsSalesController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace app\admin\controller\finance;
  3. use app\admin\service\goods\GoodsSkuService;
  4. use app\model\DataInout;
  5. use app\model\Goods;
  6. use app\model\OrderSheet;
  7. use app\model\SysCategory;
  8. use support\Db;
  9. use support\Request;
  10. class GoodsSalesController
  11. {
  12. public function list(Request $request)
  13. {
  14. $page = $request->get('page', 1);
  15. $pageSize = $request->get('pageSize', 20);
  16. $days = $request->get('days', []);
  17. $classify = $request->get('classify', '');
  18. $categoryId = $request->get('category_id', '');
  19. $goodsName = $request->get('goods_name', '');
  20. $payType = $request->get('pay_type', '');
  21. if (!empty($days)) {
  22. $days[0] = strtotime($days[0]);
  23. $days[1] = strtotime($days[1]);
  24. }
  25. $searchGoodsIds = [];
  26. if (!empty($classify)) {
  27. if ($classify == 'SERVICE') {
  28. $classify = ['SERVICE', 'CHNMED', 'CHNNCD'];
  29. } else {
  30. $classify = [$classify];
  31. }
  32. $searchGoodsIds = Goods::whereIn('goods_classify', $classify)->pluck('goods_id')->toArray();
  33. }
  34. if (!empty($categoryId)) {
  35. $categorySuperPath = SysCategory::where('category_id', $categoryId)->value('category_super_path');
  36. if (empty($categorySuperPath)) {
  37. $categorySuperPath = '#' . $categoryId . '#';
  38. }
  39. $searchCategoryIds = SysCategory::where('category_super_path', 'like', $categorySuperPath . '%')->pluck('category_id')->toArray();
  40. $searchCategoryIds[] = $categoryId;
  41. // 按产品分类筛出来的ID
  42. $oldSearchGoodsIds = $searchGoodsIds;
  43. $searchGoodsIds = Goods::whereIn('join_goods_category_id', $searchCategoryIds)->pluck('goods_id')->toArray();
  44. if (!empty($oldSearchGoodsIds)){
  45. $searchGoodsIds = array_intersect($searchGoodsIds,$oldSearchGoodsIds);
  46. }
  47. }
  48. $dataInOuts = DataInout::where('data_inout_status', 'VALID')
  49. ->where('data_inout_classify', 'IN')
  50. ->whereBetween('data_inout_addtimes', $days)
  51. ->when(!empty($payType), function ($query) use ($payType) {
  52. $query->where('data_inout_pay_type', $payType);
  53. })->when(!empty($goodsName), function ($query) use ($goodsName) {
  54. $query->where('join_data_inout_object_json', 'like', "'" . $goodsName . "'");
  55. })->select('join_data_inout_object_json')
  56. ->get()
  57. ->toArray();
  58. $goods = [];
  59. $goodsIds = [];
  60. $statistics = [
  61. 'total' => 0,
  62. 'amount' => 0,
  63. 'goods' => ['total' => 0, 'amount' => 0],
  64. 'service' => ['total' => 0, 'amount' => 0],
  65. 'chnmed' => ['total' => 0, 'amount' => 0],
  66. 'chnncd' => ['total' => 0, 'amount' => 0],
  67. 'package' => ['total' => 0, 'amount' => 0],
  68. 'meals' => ['total' => 0, 'amount' => 0],
  69. 'dishes' => ['total' => 0, 'amount' => 0],
  70. 'vip' => ['total' => 0, 'amount' => 0],
  71. 'recharge' => ['total' => 0, 'amount' => 0],
  72. 'combine' => ['total' => 0, 'amount' => 0],
  73. 'partner' => ['total' => 0, 'amount' => 0],
  74. 'referrer' => ['total' => 0, 'amount' => 0],
  75. ];
  76. foreach ($dataInOuts as $dataInOut) {
  77. if (empty($dataInOut['join_data_inout_object_json'])) {
  78. continue;
  79. }
  80. $intoObjectJson = json_decode($dataInOut['join_data_inout_object_json'], true);
  81. if (empty($intoObjectJson['order'])) {
  82. continue;
  83. }
  84. foreach ($intoObjectJson['order'] as $order) {
  85. if (empty($order['goods'])) {
  86. continue;
  87. }
  88. foreach ($order['goods'] as $good) {
  89. $goodKey = $good['goods_id'];
  90. if (!empty($good['goods_sku_id'])) {
  91. $goodKey .= '_' . $good['goods_sku_id'];
  92. }
  93. if (!key_exists($goodKey, $goods)) {
  94. $goodsIds[] = $good['goods_id'];
  95. $goods[$goodKey] = [
  96. 'goods_id' => $good['goods_id'],
  97. 'goods_name' => $good['goods_name'],
  98. 'goods_sku_id' => $good['goods_sku_id'] ?? '',
  99. 'order_sheet_num' => floatval($good['order_sheet_num']),
  100. 'order_sheet_pay' => floatval($good['order_sheet_pay'])
  101. ];
  102. } else {
  103. $goods[$goodKey]['order_sheet_num'] += $good['order_sheet_num'];
  104. $goods[$goodKey]['order_sheet_pay'] += $good['order_sheet_pay'];
  105. }
  106. $statistics['total'] = round($good['order_sheet_num'] + $statistics['total'], 2);
  107. $statistics['amount'] = round($good['order_sheet_pay'] + $statistics['amount'], 2);
  108. $orderClassify = strtolower($order['classify']);
  109. $statistics[$orderClassify]['total'] = round($good['order_sheet_num'] + $statistics[$orderClassify]['total'], 2);
  110. $statistics[$orderClassify]['amount'] = round($good['order_sheet_pay'] + $statistics[$orderClassify]['amount'], 2);
  111. }
  112. }
  113. }
  114. $statistics['service']['total'] = $statistics['service']['total'] + $statistics['chnmed']['total'] + $statistics['chnncd']['total'];
  115. $statistics['service']['amount'] = $statistics['service']['amount'] + $statistics['chnmed']['total'] + $statistics['chnncd']['amount'];
  116. // 交集
  117. if (!empty($classify) || !empty($categoryId)) {
  118. $goodsIds = array_intersect($goodsIds, $searchGoodsIds);
  119. $goods = array_filter($goods, function ($item) use ($goodsIds) {
  120. return in_array($item['goods_id'], $goodsIds);
  121. });
  122. }
  123. // 排序
  124. usort($goods, function ($a, $b) {
  125. return $b['order_sheet_num'] <=> $a['order_sheet_num'];
  126. });
  127. // 分页
  128. $total = count($goods);
  129. $start = ($page - 1) * $pageSize;
  130. $rows = array_slice($goods, $start, $pageSize);
  131. foreach ($rows as &$row) {
  132. $rowGoods = Goods::with([
  133. 'category',
  134. 'skuOne' => function ($query) use ($row) {
  135. $query->where('goods_sku_id', $row['goods_sku_id']);
  136. }
  137. ])->where('goods_id', $row['goods_id'])->first();
  138. $row['goods_classify'] = !empty($rowGoods->goods_classify) ? $rowGoods->goods_classify : '';
  139. $row['goods_sales_price'] = !empty($rowGoods->goods_sales_price) ? $rowGoods->goods_sales_price : '';
  140. $row['category'] = !empty($rowGoods->category) ? $rowGoods->category : '';
  141. if (!empty($rowGoods->skuOne)) {
  142. $row['goods_sku_title'] = GoodsSkuService::getSkuTitle($rowGoods->skuOne->goods_sku_specs_json);
  143. $row['goods_sku_sales_price'] = $rowGoods->skuOne->goods_sku_sales_price;
  144. }
  145. $row['order_sheet_pay'] = sprintf('%.2f', $row['order_sheet_pay']);
  146. }
  147. return json_success('', [
  148. 'page' => $page,
  149. 'pageSize' => $pageSize,
  150. 'total' => $total,
  151. 'rows' => $rows,
  152. 'statistics' => $statistics
  153. ]);
  154. }
  155. }