GoodsSalesController.php 17 KB

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