GoodsSalesController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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', [date('Y-m-01 00:00:00'), date('Y-m-t 23:59:59')]);
  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. if (!empty($goodsName)) {
  49. // 按产品类型和分类筛出来的ID
  50. $oldSearchGoodsIds = $searchGoodsIds;
  51. $searchGoodsIds = Goods::where('goods_name', 'like', '%' . $goodsName . '%')->pluck("goods_id")->toArray();
  52. if (!empty($oldSearchGoodsIds)) {
  53. $searchGoodsIds = array_intersect($searchGoodsIds, $oldSearchGoodsIds);
  54. }
  55. }
  56. $dataInOuts = DataInout::where('data_inout_status', 'VALID')
  57. ->where('data_inout_classify', 'IN')
  58. ->when(!empty($days), function ($query) use ($days) {
  59. $query->whereBetween('data_inout_addtimes', $days);
  60. })->when(!empty($payType), function ($query) use ($payType) {
  61. $query->where('data_inout_pay_type', $payType);
  62. })->select('join_data_inout_object_json')
  63. ->get()
  64. ->toArray();
  65. $goods = [];
  66. $goodsIds = [];
  67. $statistics = [
  68. 'total' => 0,
  69. 'amount' => 0,
  70. 'goods' => ['total' => 0, 'amount' => 0],
  71. 'service' => ['total' => 0, 'amount' => 0],
  72. 'chnmed' => ['total' => 0, 'amount' => 0],
  73. 'chnncd' => ['total' => 0, 'amount' => 0],
  74. 'package' => ['total' => 0, 'amount' => 0],
  75. 'meals' => ['total' => 0, 'amount' => 0],
  76. 'dishes' => ['total' => 0, 'amount' => 0],
  77. 'vip' => ['total' => 0, 'amount' => 0],
  78. 'recharge' => ['total' => 0, 'amount' => 0],
  79. 'combine' => ['total' => 0, 'amount' => 0],
  80. 'partner' => ['total' => 0, 'amount' => 0],
  81. 'referrer' => ['total' => 0, 'amount' => 0],
  82. ];
  83. foreach ($dataInOuts as $dataInOut) {
  84. if (empty($dataInOut['join_data_inout_object_json'])) {
  85. continue;
  86. }
  87. $intoObjectJson = json_decode($dataInOut['join_data_inout_object_json'], true);
  88. if (empty($intoObjectJson['order'])) {
  89. continue;
  90. }
  91. foreach ($intoObjectJson['order'] as $order) {
  92. if (empty($order['goods'])) {
  93. continue;
  94. }
  95. foreach ($order['goods'] as $good) {
  96. if ((!empty($classify) || !empty($categoryId) || !empty($goodsName)) && !in_array($good['goods_id'], $searchGoodsIds)) {
  97. continue;
  98. }
  99. $goodKey = $good['goods_id'];
  100. if (!empty($good['goods_sku_id'])) {
  101. $goodKey .= '_' . $good['goods_sku_id'];
  102. }
  103. if (!key_exists($goodKey, $goods)) {
  104. $goodsIds[] = $good['goods_id'];
  105. $goods[$goodKey] = [
  106. 'goods_id' => $good['goods_id'],
  107. 'goods_name' => $good['goods_name'],
  108. 'goods_sku_id' => $good['goods_sku_id'] ?? '',
  109. 'order_sheet_num' => floatval($good['order_sheet_num']),
  110. 'order_sheet_pay' => floatval($good['order_sheet_pay'])
  111. ];
  112. } else {
  113. $goods[$goodKey]['order_sheet_num'] += $good['order_sheet_num'];
  114. $goods[$goodKey]['order_sheet_pay'] += $good['order_sheet_pay'];
  115. }
  116. $statistics['total'] = round($good['order_sheet_num'] + $statistics['total'], 2);
  117. $statistics['amount'] = round($good['order_sheet_pay'] + $statistics['amount'], 2);
  118. $orderClassify = strtolower($order['classify']);
  119. $statistics[$orderClassify]['total'] = round($good['order_sheet_num'] + $statistics[$orderClassify]['total'], 2);
  120. $statistics[$orderClassify]['amount'] = round($good['order_sheet_pay'] + $statistics[$orderClassify]['amount'], 2);
  121. }
  122. }
  123. }
  124. $statistics['service']['total'] = $statistics['service']['total'] + $statistics['chnmed']['total'] + $statistics['chnncd']['total'];
  125. $statistics['service']['amount'] = $statistics['service']['amount'] + $statistics['chnmed']['total'] + $statistics['chnncd']['amount'];
  126. // 交集
  127. if (!empty($classify) || !empty($categoryId)) {
  128. $goodsIds = array_intersect($goodsIds, $searchGoodsIds);
  129. $goods = array_filter($goods, function ($item) use ($goodsIds) {
  130. return in_array($item['goods_id'], $goodsIds);
  131. });
  132. }
  133. // 排序
  134. usort($goods, function ($a, $b) {
  135. return $b['order_sheet_num'] <=> $a['order_sheet_num'];
  136. });
  137. // 分页
  138. $total = count($goods);
  139. $start = ($page - 1) * $pageSize;
  140. $rows = array_slice($goods, $start, $pageSize);
  141. foreach ($rows as &$row) {
  142. $rowGoods = Goods::with([
  143. 'category',
  144. 'skuOne' => function ($query) use ($row) {
  145. $query->where('goods_sku_id', $row['goods_sku_id']);
  146. }
  147. ])->where('goods_id', $row['goods_id'])->first();
  148. $row['goods_classify'] = !empty($rowGoods->goods_classify) ? $rowGoods->goods_classify : '';
  149. $row['goods_sales_price'] = !empty($rowGoods->goods_sales_price) ? $rowGoods->goods_sales_price : '';
  150. $row['category'] = !empty($rowGoods->category) ? $rowGoods->category : '';
  151. if (!empty($rowGoods->skuOne)) {
  152. $row['goods_sku_title'] = GoodsSkuService::getSkuTitle($rowGoods->skuOne->goods_sku_specs_json);
  153. $row['goods_sku_sales_price'] = $rowGoods->skuOne->goods_sku_sales_price;
  154. }
  155. $row['order_sheet_pay'] = sprintf('%.2f', $row['order_sheet_pay']);
  156. }
  157. return json_success('', [
  158. 'page' => $page,
  159. 'pageSize' => $pageSize,
  160. 'total' => $total,
  161. 'rows' => $rows,
  162. 'statistics' => $statistics
  163. ]);
  164. }
  165. public function info(Request $request)
  166. {
  167. $goodsId = $request->get('goods_id');
  168. $skuId = $request->get('sku_id');
  169. if (!$goodsId) {
  170. return json_fail("参数异常");
  171. }
  172. $goods = Goods::with([
  173. 'sku' => function ($query) use ($skuId) {
  174. $query->where('goods_sku_id', $skuId);
  175. },
  176. 'running' => function ($query) {
  177. $query->select('join_running_goods_id', 'goods_running_storage', 'goods_running_sale');
  178. },
  179. 'detail' => function ($query) {
  180. $query->select('join_detail_goods_id', 'goods_detail_slider_json');
  181. },
  182. 'user' => function ($query) {
  183. $query->select('user_id', 'user_name');
  184. },
  185. 'updateUser' => function ($query) {
  186. $query->select('user_id', 'user_name');
  187. }
  188. ])->where('goods_id', $goodsId)
  189. ->first();
  190. $goods = $goods ?? [];
  191. if (!empty($goods['goods_cover'])) {
  192. $goods['goods_cover'] = getenv('STORAGE_DOMAIN') . $goods['goods_cover'];
  193. }
  194. $sliderArr = [];
  195. if (!empty($goods['detail']) && !empty($goods['detail']['goods_detail_slider_json'])) {
  196. $sliderJson = json_decode($goods['detail']['goods_detail_slider_json'], true);
  197. $sliders = explode(',', $sliderJson['slider']);
  198. foreach ($sliders as $slider) {
  199. $sliderArr[] = getenv('STORAGE_DOMAIN') . $slider;
  200. }
  201. }
  202. $goods['slider'] = $sliderArr;
  203. if (!empty($goods['user'])) {
  204. $goods['creator_username'] = $goods['user']['user_name'];
  205. }
  206. if (!empty($goods['updateUser'])) {
  207. $goods['updator_username'] = $goods['updateUser']['user_name'];
  208. }
  209. if (!empty($goods['sku'])) {
  210. foreach ($goods['sku'] as &$sku) {
  211. if (!empty($sku['goods_sku_storage_json']) && !in_array($goods['goods_classify'], ['MEALS', 'PACKAGE'])) {
  212. $storageJson = json_decode($sku['goods_sku_storage_json'], true);
  213. $sku['storage'] = $storageJson['storage'];
  214. } else {
  215. $sku['storage'] = !empty($goods['running']) ? $goods['running']['goods_running_storage'] : 0;
  216. }
  217. }
  218. }
  219. return json_success('success', $goods);
  220. }
  221. }