GoodsSalesController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. namespace app\admin\controller\finance;
  3. use app\admin\service\goods\GoodsService;
  4. use app\admin\service\goods\GoodsSkuService;
  5. use app\model\DataInout;
  6. use app\model\Goods;
  7. use app\model\OrderSheet;
  8. use app\model\SysCategory;
  9. use support\Db;
  10. use support\Request;
  11. class GoodsSalesController
  12. {
  13. public function list(Request $request)
  14. {
  15. $page = $request->get('page', 1);
  16. $pageSize = $request->get('pageSize', 20);
  17. $days = $request->get('days', [date('Y-m-01 00:00:00'), date('Y-m-t 23:59:59')]);
  18. $classify = $request->get('classify', '');
  19. $categoryId = $request->get('category_id', '');
  20. $goodsName = $request->get('goods_name', '');
  21. $payType = $request->get('pay_type', '');
  22. if (!empty($days)) {
  23. $days[0] = strtotime($days[0]);
  24. $days[1] = strtotime($days[1]);
  25. }
  26. $searchGoodsIds = [];
  27. if (!empty($classify)) {
  28. if ($classify == 'SERVICE') {
  29. $classify = ['SERVICE', 'CHNMED', 'CHNNCD'];
  30. } else {
  31. $classify = [$classify];
  32. }
  33. $searchGoodsIds = Goods::whereIn('goods_classify', $classify)->pluck('goods_id')->toArray();
  34. }
  35. if (!empty($categoryId)) {
  36. $categorySuperPath = SysCategory::where('category_id', $categoryId)->value('category_super_path');
  37. if (empty($categorySuperPath)) {
  38. $categorySuperPath = '#' . $categoryId . '#';
  39. }
  40. $searchCategoryIds = SysCategory::where('category_super_path', 'like', $categorySuperPath . '%')->pluck('category_id')->toArray();
  41. $searchCategoryIds[] = $categoryId;
  42. // 按产品类型筛出来的ID
  43. $oldSearchGoodsIds = $searchGoodsIds;
  44. $searchGoodsIds = Goods::whereIn('join_goods_category_id', $searchCategoryIds)->pluck('goods_id')->toArray();
  45. if (!empty($oldSearchGoodsIds)) {
  46. $searchGoodsIds = array_intersect($searchGoodsIds, $oldSearchGoodsIds);
  47. }
  48. }
  49. if (!empty($goodsName)) {
  50. // 按产品类型和分类筛出来的ID
  51. $oldSearchGoodsIds = $searchGoodsIds;
  52. $searchGoodsIds = Goods::where('goods_name', 'like', '%' . $goodsName . '%')->pluck("goods_id")->toArray();
  53. if (!empty($oldSearchGoodsIds)) {
  54. $searchGoodsIds = array_intersect($searchGoodsIds, $oldSearchGoodsIds);
  55. }
  56. }
  57. $dataInOuts = DataInout::where('data_inout_status', 'VALID')
  58. ->where('data_inout_classify', 'IN')
  59. ->when(!empty($days), function ($query) use ($days) {
  60. $query->whereBetween('data_inout_addtimes', $days);
  61. })->when(!empty($payType), function ($query) use ($payType) {
  62. $query->where('data_inout_pay_type', $payType);
  63. })->select('join_data_inout_object_json')
  64. ->get()
  65. ->toArray();
  66. $goods = [];
  67. $goodsIds = [];
  68. $statistics = [
  69. 'total' => 0,
  70. 'amount' => 0,
  71. 'goods' => ['total' => 0, 'amount' => 0],
  72. 'service' => ['total' => 0, 'amount' => 0],
  73. 'chnmed' => ['total' => 0, 'amount' => 0],
  74. 'chnncd' => ['total' => 0, 'amount' => 0],
  75. 'package' => ['total' => 0, 'amount' => 0],
  76. 'meals' => ['total' => 0, 'amount' => 0],
  77. 'dishes' => ['total' => 0, 'amount' => 0],
  78. 'vip' => ['total' => 0, 'amount' => 0],
  79. 'recharge' => ['total' => 0, 'amount' => 0],
  80. 'combine' => ['total' => 0, 'amount' => 0],
  81. 'partner' => ['total' => 0, 'amount' => 0],
  82. 'referrer' => ['total' => 0, 'amount' => 0],
  83. 'franchisee' => ['total' => 0, 'amount' => 0],
  84. 'card' => ['total' => 0, 'amount' => 0],
  85. ];
  86. foreach ($dataInOuts as $dataInOut) {
  87. if (empty($dataInOut['join_data_inout_object_json'])) {
  88. continue;
  89. }
  90. $intoObjectJson = json_decode($dataInOut['join_data_inout_object_json'], true);
  91. if (empty($intoObjectJson['order'])) {
  92. continue;
  93. }
  94. foreach ($intoObjectJson['order'] as $order) {
  95. if (empty($order['goods'])) {
  96. continue;
  97. }
  98. foreach ($order['goods'] as $good) {
  99. if ((!empty($classify) || !empty($categoryId) || !empty($goodsName)) && !in_array($good['goods_id'], $searchGoodsIds)) {
  100. continue;
  101. }
  102. $goodKey = $good['goods_id'];
  103. if (!empty($good['goods_sku_id'])) {
  104. $goodKey .= '_' . $good['goods_sku_id'];
  105. }
  106. if (!key_exists($goodKey, $goods)) {
  107. $goodsIds[] = $good['goods_id'];
  108. $goods[$goodKey] = [
  109. 'goods_id' => $good['goods_id'],
  110. 'goods_name' => $good['goods_name'],
  111. 'goods_sku_id' => $good['goods_sku_id'] ?? '',
  112. 'order_sheet_num' => floatval($good['order_sheet_num']),
  113. 'order_sheet_pay' => floatval($good['order_sheet_pay'])
  114. ];
  115. } else {
  116. $goods[$goodKey]['order_sheet_num'] += $good['order_sheet_num'];
  117. $goods[$goodKey]['order_sheet_pay'] += $good['order_sheet_pay'];
  118. }
  119. $statistics['total'] = round($good['order_sheet_num'] + $statistics['total'], 2);
  120. $statistics['amount'] = round($good['order_sheet_pay'] + $statistics['amount'], 2);
  121. $orderClassify = strtolower($order['classify']);
  122. $statistics[$orderClassify]['total'] = round($good['order_sheet_num'] + $statistics[$orderClassify]['total'], 2);
  123. $statistics[$orderClassify]['amount'] = round($good['order_sheet_pay'] + $statistics[$orderClassify]['amount'], 2);
  124. }
  125. }
  126. }
  127. $statistics['service']['total'] = $statistics['service']['total'] + $statistics['chnmed']['total'] + $statistics['chnncd']['total'];
  128. $statistics['service']['amount'] = $statistics['service']['amount'] + $statistics['chnmed']['total'] + $statistics['chnncd']['amount'];
  129. // 交集
  130. if (!empty($classify) || !empty($categoryId)) {
  131. $goodsIds = array_intersect($goodsIds, $searchGoodsIds);
  132. $goods = array_filter($goods, function ($item) use ($goodsIds) {
  133. return in_array($item['goods_id'], $goodsIds);
  134. });
  135. }
  136. // 排序
  137. usort($goods, function ($a, $b) {
  138. return $b['order_sheet_num'] <=> $a['order_sheet_num'];
  139. });
  140. // 分页
  141. $total = count($goods);
  142. $start = ($page - 1) * $pageSize;
  143. $rows = array_slice($goods, $start, $pageSize);
  144. foreach ($rows as &$row) {
  145. $rowGoods = Goods::with([
  146. 'category',
  147. 'skuOne' => function ($query) use ($row) {
  148. $query->where('goods_sku_id', $row['goods_sku_id']);
  149. }
  150. ])->where('goods_id', $row['goods_id'])->first();
  151. $row['goods_classify'] = !empty($rowGoods->goods_classify) ? $rowGoods->goods_classify : '';
  152. $row['goods_sales_price'] = !empty($rowGoods->goods_sales_price) ? $rowGoods->goods_sales_price : '';
  153. $row['category'] = !empty($rowGoods->category) ? $rowGoods->category : '';
  154. if (!empty($rowGoods->skuOne)) {
  155. $row['goods_sku_title'] = GoodsSkuService::getSkuTitle($rowGoods->skuOne->goods_sku_specs_json);
  156. $row['goods_sku_sales_price'] = $rowGoods->skuOne->goods_sku_sales_price;
  157. }
  158. $row['order_sheet_pay'] = sprintf('%.2f', $row['order_sheet_pay']);
  159. }
  160. return json_success('', [
  161. 'page' => $page,
  162. 'pageSize' => $pageSize,
  163. 'total' => $total,
  164. 'rows' => $rows,
  165. 'statistics' => $statistics
  166. ]);
  167. }
  168. public function info(Request $request)
  169. {
  170. $goodsId = $request->get('goods_id');
  171. $skuId = $request->get('sku_id');
  172. if (!$goodsId) {
  173. return json_fail("参数异常");
  174. }
  175. $goods = Goods::with([
  176. 'sku' => function ($query) use ($skuId) {
  177. $query->where('goods_sku_id', $skuId);
  178. },
  179. 'running' => function ($query) {
  180. $query->select('join_running_goods_id', 'goods_running_storage', 'goods_running_sale');
  181. },
  182. 'detail' => function ($query) {
  183. $query->select('join_detail_goods_id', 'goods_detail_slider_json');
  184. },
  185. 'user' => function ($query) {
  186. $query->select('user_id', 'user_name');
  187. },
  188. 'updateUser' => function ($query) {
  189. $query->select('user_id', 'user_name');
  190. }
  191. ])->where('goods_id', $goodsId)
  192. ->first();
  193. $goods = $goods ?? [];
  194. if (!empty($goods['goods_cover'])) {
  195. $goods['goods_cover'] = getenv('STORAGE_DOMAIN') . $goods['goods_cover'];
  196. }
  197. $sliderArr = [];
  198. if (!empty($goods['detail']) && !empty($goods['detail']['goods_detail_slider_json'])) {
  199. $sliderJson = json_decode($goods['detail']['goods_detail_slider_json'], true);
  200. $sliders = explode(',', $sliderJson['slider']);
  201. foreach ($sliders as $slider) {
  202. $sliderArr[] = getenv('STORAGE_DOMAIN') . $slider;
  203. }
  204. }
  205. $goods['slider'] = $sliderArr;
  206. if (!empty($goods['user'])) {
  207. $goods['creator_username'] = $goods['user']['user_name'];
  208. }
  209. if (!empty($goods['updateUser'])) {
  210. $goods['updator_username'] = $goods['updateUser']['user_name'];
  211. }
  212. if (!empty($goods['sku'])) {
  213. foreach ($goods['sku'] as &$sku) {
  214. if (!empty($sku['goods_sku_storage_json']) && !in_array($goods['goods_classify'], ['MEALS', 'PACKAGE'])) {
  215. $storageJson = json_decode($sku['goods_sku_storage_json'], true);
  216. $sku['storage'] = $storageJson['storage'];
  217. } else {
  218. $sku['storage'] = !empty($goods['running']) ? $goods['running']['goods_running_storage'] : 0;
  219. }
  220. }
  221. }
  222. return json_success('success', $goods);
  223. }
  224. public function exportGoodsSales(Request $request)
  225. {
  226. $days = $request->get('days', [date('Y-m-01 00:00:00'), date('Y-m-t 23:59:59')]);
  227. $classify = $request->get('classify', '');
  228. $categoryId = $request->get('category_id', '');
  229. $goodsName = $request->get('goods_name', '');
  230. $payType = $request->get('pay_type', '');
  231. $paramsGoodsIds = $request->get('goods_ids');
  232. if (!empty($days)) {
  233. $days[0] = strtotime($days[0]);
  234. $days[1] = strtotime($days[1]);
  235. }
  236. $searchGoodsIds = [];
  237. if (!empty($classify)) {
  238. if ($classify == 'SERVICE') {
  239. $classify = ['SERVICE', 'CHNMED', 'CHNNCD'];
  240. } else {
  241. $classify = [$classify];
  242. }
  243. $searchGoodsIds = Goods::whereIn('goods_classify', $classify)->pluck('goods_id')->toArray();
  244. }
  245. if (!empty($categoryId)) {
  246. $categorySuperPath = SysCategory::where('category_id', $categoryId)->value('category_super_path');
  247. if (empty($categorySuperPath)) {
  248. $categorySuperPath = '#' . $categoryId . '#';
  249. }
  250. $searchCategoryIds = SysCategory::where('category_super_path', 'like', $categorySuperPath . '%')->pluck('category_id')->toArray();
  251. $searchCategoryIds[] = $categoryId;
  252. // 按产品类型筛出来的ID
  253. $oldSearchGoodsIds = $searchGoodsIds;
  254. $searchGoodsIds = Goods::whereIn('join_goods_category_id', $searchCategoryIds)->pluck('goods_id')->toArray();
  255. if (!empty($oldSearchGoodsIds)) {
  256. $searchGoodsIds = array_intersect($searchGoodsIds, $oldSearchGoodsIds);
  257. }
  258. }
  259. if (!empty($goodsName)) {
  260. // 按产品类型和分类筛出来的ID
  261. $oldSearchGoodsIds = $searchGoodsIds;
  262. $searchGoodsIds = Goods::where('goods_name', 'like', '%' . $goodsName . '%')->pluck("goods_id")->toArray();
  263. if (!empty($oldSearchGoodsIds)) {
  264. $searchGoodsIds = array_intersect($searchGoodsIds, $oldSearchGoodsIds);
  265. }
  266. }
  267. $dataInOuts = DataInout::where('data_inout_status', 'VALID')
  268. ->where('data_inout_classify', 'IN')
  269. ->when(!empty($days), function ($query) use ($days) {
  270. $query->whereBetween('data_inout_addtimes', $days);
  271. })->when(!empty($payType), function ($query) use ($payType) {
  272. $query->where('data_inout_pay_type', $payType);
  273. })->select('join_data_inout_object_json')
  274. ->get()
  275. ->toArray();
  276. $goods = [];
  277. $goodsIds = [];
  278. foreach ($dataInOuts as $dataInOut) {
  279. if (empty($dataInOut['join_data_inout_object_json'])) {
  280. continue;
  281. }
  282. $intoObjectJson = json_decode($dataInOut['join_data_inout_object_json'], true);
  283. if (empty($intoObjectJson['order'])) {
  284. continue;
  285. }
  286. foreach ($intoObjectJson['order'] as $order) {
  287. if (empty($order['goods'])) {
  288. continue;
  289. }
  290. foreach ($order['goods'] as $good) {
  291. if ((!empty($classify) || !empty($categoryId) || !empty($goodsName)) && !in_array($good['goods_id'], $searchGoodsIds)) {
  292. continue;
  293. }
  294. $goodKey = $good['goods_id'];
  295. if (!empty($good['goods_sku_id'])) {
  296. $goodKey .= '_' . $good['goods_sku_id'];
  297. }
  298. if (!key_exists($goodKey, $goods)) {
  299. $goodsIds[] = $good['goods_id'];
  300. $goods[$goodKey] = [
  301. 'goods_id' => $good['goods_id'],
  302. 'goods_name' => $good['goods_name'],
  303. 'goods_sku_id' => $good['goods_sku_id'] ?? '',
  304. 'order_sheet_num' => floatval($good['order_sheet_num']),
  305. 'order_sheet_pay' => floatval($good['order_sheet_pay'])
  306. ];
  307. } else {
  308. $goods[$goodKey]['order_sheet_num'] += $good['order_sheet_num'];
  309. $goods[$goodKey]['order_sheet_pay'] += $good['order_sheet_pay'];
  310. }
  311. }
  312. }
  313. }
  314. // 交集
  315. if (!empty($classify) || !empty($categoryId) || !empty($paramsGoodsIds)) {
  316. $goodsIds = array_intersect($goodsIds, $searchGoodsIds);
  317. if (!empty($paramsGoodsIds) && !empty($goodsIds)){
  318. $goodsIds = array_intersect($goodsIds,$paramsGoodsIds);
  319. }else if (!empty($paramsGoodsIds) && empty($goodsIds)){
  320. $goodsIds = $paramsGoodsIds;
  321. }
  322. $goods = array_filter($goods, function ($item) use ($goodsIds) {
  323. return in_array($item['goods_id'], $goodsIds);
  324. });
  325. }
  326. // 排序
  327. usort($goods, function ($a, $b) {
  328. return $b['order_sheet_num'] <=> $a['order_sheet_num'];
  329. });
  330. $dataArray = [];
  331. $data = [];
  332. foreach ($goods as $row) {
  333. $data['goods_name'] = $row['goods_name'];
  334. $data['order_sheet_num'] = $row['order_sheet_num'];
  335. $rowGoods = Goods::with([
  336. 'category',
  337. 'skuOne' => function ($query) use ($row) {
  338. $query->where('goods_sku_id', $row['goods_sku_id']);
  339. }
  340. ])->where('goods_id', $row['goods_id'])->first();
  341. $data['goods_classify'] = !empty($rowGoods->goods_classify) ? GoodsService::$goodsClassify[$rowGoods->goods_classify] : '--';;
  342. $data['goods_sales_price'] = !empty($rowGoods->goods_sales_price) ? $rowGoods->goods_sales_price : '--';
  343. $data['category'] = !empty($rowGoods->category) ? $rowGoods->category : '--';
  344. if (!empty($rowGoods->skuOne)) {
  345. $data['goods_sku_title'] = GoodsSkuService::getSkuTitle($rowGoods->skuOne->goods_sku_specs_json);
  346. $data['goods_sku_sales_price'] = $rowGoods->skuOne->goods_sku_sales_price;
  347. }
  348. if(!empty($rowGoods->category)){
  349. $data['goods_category'] = $rowGoods->category->category_name;
  350. }
  351. $data['order_sheet_pay'] = sprintf('%.2f', $row['order_sheet_pay']);
  352. $dataArray[] = $data;
  353. }
  354. return json_success('', $dataArray);
  355. }
  356. }