GoodsSalesController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. $premisesId = $request->get('premises_id', '');
  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. $searchGoodsIds = Goods::where('goods_classify', $classify)->pluck('goods_id')->toArray();
  29. } elseif (!empty($categoryId)) {
  30. $categorySuperPath = SysCategory::where('category_id', $categoryId)->value('category_super_path');
  31. $searchCategoryIds = [];
  32. if (!empty($category)) {
  33. $searchCategoryIds = SysCategory::where('category_super_path', 'like', $categorySuperPath . '%')->pluck('category_id')->toArray();
  34. }
  35. $searchCategoryIds[] = $categoryId;
  36. $searchGoodsIds = Goods::whereIn('join_goods_category_id', $searchCategoryIds)->pluck('goods_id')->toArray();
  37. }
  38. $dataInOuts = DataInout::where('data_inout_status', 'VALID')
  39. ->where('data_inout_classify', 'IN')
  40. ->whereBetween('data_inout_addtimes', $days)
  41. ->when(!empty($payType), function ($query) use ($payType) {
  42. $query->where('data_inout_pay_type', $payType);
  43. })->when(!empty($goodsName), function ($query) use ($goodsName) {
  44. $query->where('join_data_inout_object_json', 'like', "'" . $goodsName . "'");
  45. })->select('join_data_inout_object_json')
  46. ->get()
  47. ->toArray();
  48. $goods = [];
  49. $goodsIds = [];
  50. $statistics = [
  51. 'total' => 0,
  52. 'amount' => 0,
  53. 'goods'=>['total' => 0,'amount' => 0],
  54. 'service'=>['total' => 0,'amount' => 0],
  55. 'chnmed'=>['total' => 0,'amount' => 0],
  56. 'chnncd'=>['total' => 0,'amount' => 0],
  57. 'package'=>['total' => 0,'amount' => 0],
  58. 'meals'=>['total' => 0,'amount' => 0],
  59. 'dishes'=>['total' => 0,'amount' => 0],
  60. 'vip'=>['total' => 0,'amount' => 0],
  61. 'recharge'=>['total' => 0,'amount' => 0],
  62. 'combine'=>['total' => 0,'amount' => 0],
  63. 'partner'=>['total' => 0,'amount' => 0],
  64. 'referrer'=>['total' => 0,'amount' => 0],
  65. ];
  66. foreach ($dataInOuts as $dataInOut) {
  67. if (empty($dataInOut['join_data_inout_object_json'])) {
  68. continue;
  69. }
  70. $intoObjectJson = json_decode($dataInOut['join_data_inout_object_json'], true);
  71. if (empty($intoObjectJson['order'])) {
  72. continue;
  73. }
  74. foreach ($intoObjectJson['order'] as $order) {
  75. if (empty($order['goods'])) {
  76. continue;
  77. }
  78. foreach ($order['goods'] as $good) {
  79. $goodKey = $good['goods_id'];
  80. if (!empty($good['goods_sku_id'])) {
  81. $goodKey .= '_' . $good['goods_sku_id'];
  82. }
  83. if (!key_exists($goodKey, $goods)) {
  84. $goodsIds[] = $good['goods_id'];
  85. $goods[$goodKey] = [
  86. 'goods_id' => $good['goods_id'],
  87. 'goods_name' => $good['goods_name'],
  88. 'goods_sku_id' => $good['goods_sku_id'] ?? '',
  89. 'order_sheet_num' => floatval($good['order_sheet_num']),
  90. 'order_sheet_pay' => floatval($good['order_sheet_pay'])
  91. ];
  92. } else {
  93. $goods[$goodKey]['order_sheet_num'] += $good['order_sheet_num'];
  94. $goods[$goodKey]['order_sheet_pay'] += $good['order_sheet_pay'];
  95. }
  96. $statistics['total'] = round($good['order_sheet_num'] + $statistics['total'],2);
  97. $statistics['amount'] = round($good['order_sheet_pay'] + $statistics['amount'],2);
  98. $orderClassify = strtolower($order['classify']);
  99. $statistics[$orderClassify]['total'] = $good['order_sheet_num'] + $statistics[$orderClassify]['total'];
  100. $statistics[$orderClassify]['amount'] = $good['order_sheet_pay'] + $statistics[$orderClassify]['amount'];
  101. }
  102. }
  103. }
  104. $statistics['service']['total'] = $statistics['service']['total'] + $statistics['chnmed']['total'] + $statistics['chnncd']['total'];
  105. $statistics['service']['amount'] = $statistics['service']['amount'] + $statistics['chnmed']['total'] + $statistics['chnncd']['amount'];
  106. // 交集
  107. if (!empty($classify) || !empty($categoryId)) {
  108. $goodsIds = array_intersect($goodsIds, $searchGoodsIds);
  109. $goods = array_filter($goods, function ($item) use ($goodsIds) {
  110. return in_array($item['goods_id'], $goodsIds);
  111. });
  112. }
  113. // 排序
  114. usort($goods, function ($a, $b) {
  115. return $b['order_sheet_num'] <=> $a['order_sheet_num'];
  116. });
  117. // 分页
  118. $total = count($goods);
  119. $start = ($page - 1) * $pageSize;
  120. $rows = array_slice($goods, $start, $pageSize);
  121. foreach ($rows as &$row) {
  122. $rowGoods = Goods::with([
  123. 'category',
  124. 'skuOne' => function ($query) use ($row) {
  125. $query->where('goods_sku_id', $row['goods_sku_id']);
  126. }
  127. ])->where('goods_id', $row['goods_id'])->first();
  128. $row['goods_classify'] = !empty($rowGoods->goods_classify) ? $rowGoods->goods_classify : '';
  129. $row['goods_sales_price'] = !empty($rowGoods->goods_sales_price) ? $rowGoods->goods_sales_price : '';
  130. $row['category'] = !empty($rowGoods->category) ? $rowGoods->category : '';
  131. if (!empty($rowGoods->skuOne)) {
  132. $row['goods_sku_title'] = GoodsSkuService::getSkuTitle($rowGoods->skuOne->goods_sku_specs_json);
  133. $row['goods_sku_sales_price'] = $rowGoods->skuOne->goods_sku_sales_price;
  134. }
  135. $row['order_sheet_pay'] = sprintf('%.2f', $row['order_sheet_pay']);
  136. }
  137. return json_success('', [
  138. 'page' => $page,
  139. 'pageSize' => $pageSize,
  140. 'total' => $total,
  141. 'rows' => $rows,
  142. 'statistics' => $statistics
  143. ]);
  144. }
  145. public function listOld(Request $request)
  146. {
  147. $page = $request->get('page', 1);
  148. $pageSize = $request->get('pageSize', 20);
  149. $days = $request->get('days', []);
  150. $classify = $request->get('classify', '');
  151. $categoryId = $request->get('category_id', '');
  152. $goodsName = $request->get('goods_name', '');
  153. $premisesId = $request->get('premises_id', '');
  154. $payType = $request->get('pay_type', '');
  155. if (!empty($days)) {
  156. $days[0] = strtotime($days[0]);
  157. $days[1] = strtotime($days[1]);
  158. if (date('m', $days[0]) != date('m', $days[1])) {
  159. return json_fail('暂不支持跨月查询');
  160. }
  161. $month = date('Ym', $days[0]);
  162. $days[1] = strtotime(date('Y-m-d', $days[1]) . " 23:59:59");
  163. } else {
  164. $month = date('Ym');
  165. }
  166. $categorySearchIds = '';
  167. if (!empty($categoryId)) {
  168. $categorySearchPath = SysCategory::where('category_id', $categoryId)->value('category_super_path');
  169. $categorySearchPath .= '#' . $categoryId . '#';
  170. $categorySearchIds = SysCategory::where('category_super_path', 'like', $categorySearchPath . '%')->pluck('category_id')->toArray();
  171. $categorySearchIds = array_merge($categorySearchIds, [$categoryId]);
  172. $categorySearchIds = implode(',', $categorySearchIds);
  173. }
  174. $recordStart = ($page - 1) * 20;
  175. $where = "where 1";
  176. if (!empty($days)) {
  177. $where .= " and di.data_income_addtimes between {$days[0]} and {$days[1]}";
  178. }
  179. if (!empty($classify)) {
  180. $where .= " and di.data_income_classify = '{$classify}'";
  181. }
  182. if (!empty($categorySearchIds)) {
  183. $where .= " and g.join_goods_category_id in({$categorySearchIds})";
  184. }
  185. if (!empty($goodsName)) {
  186. $where .= " and g.goods_name like '%{$goodsName}%' ";
  187. }
  188. if (!empty($premisesId)) {
  189. $where .= " and di.join_data_income_dept_id = {$premisesId}";
  190. }
  191. if (!empty($payType)) {
  192. $where .= " and di.data_income_pay_type = '{$payType}'";
  193. }
  194. $totalSql = "
  195. select count(*) as total from (
  196. select
  197. data_income_classify
  198. from app_data_income_{$month} as di
  199. left join app_goods as g on REPLACE(JSON_EXTRACT(di.join_data_income_object_json,'$.sheet[0].goods_id'),'\"','') = g.goods_id
  200. {$where}
  201. group by JSON_EXTRACT(join_data_income_object_json,'$.sheet[0].goods_id'), JSON_EXTRACT(join_data_income_object_json,'$.sheet[0].goods_sku_id'),data_income_classify
  202. ) COUNT
  203. ";
  204. $total = Db::select($totalSql);
  205. $sql = "select
  206. g.join_goods_category_id,di.data_income_classify,g.goods_id,g.goods_name,g.goods_classify, gs.goods_sku_specs_json,gs.goods_sku_id,gs.goods_sku_sales_price,MAX(di.data_income_addtimes) as addtimes,
  207. SUM(JSON_EXTRACT(di.join_data_income_object_json,'$.sheet[0].order_sheet_num')) as num,SUM(di.data_income_amount) as amount,COUNT(*) as count
  208. from app_data_income_{$month} as di
  209. left join app_goods as g on REPLACE(JSON_EXTRACT(di.join_data_income_object_json,'$.sheet[0].goods_id'),'\"','') = CAST(g.goods_id AS CHAR)
  210. left join app_goods_sku as gs on JSON_EXTRACT(di.join_data_income_object_json,'$.sheet[0].goods_sku_id') = gs.goods_sku_id
  211. {$where}
  212. group by g.goods_id,gs.goods_sku_id,di.data_income_classify
  213. order by addtimes desc
  214. limit " . $recordStart . ',' . $pageSize;
  215. $rows = Db::select($sql);
  216. $rows = json_decode(json_encode($rows), true);
  217. $categoryIds = array_unique(array_filter(array_column($rows, 'join_goods_category_id')));
  218. $categorys = SysCategory::whereIn('category_id', $categoryIds)->pluck('category_name', 'category_id')->toArray();
  219. foreach ($rows as &$row) {
  220. if (!empty($row['join_goods_category_id']) && isset($categorys[$row['join_goods_category_id']])) {
  221. $row['category_name'] = $categorys[$row['join_goods_category_id']];
  222. }
  223. if (!empty($row['goods_sku_specs_json'])) {
  224. $specsJson = json_decode($row['goods_sku_specs_json'], true);
  225. $skuTitle = '';
  226. foreach ($specsJson as $k => $value) {
  227. $value = is_array($value) ? $value[0] : $value;
  228. $skuTitle .= $k . ':' . $value . ';';
  229. }
  230. $row['sku'] = rtrim($skuTitle, ';');
  231. }
  232. }
  233. return json_success('', [
  234. 'page' => $page,
  235. 'pageSize' => $pageSize,
  236. 'total' => $total[0]->total,
  237. 'rows' => $rows
  238. ]);
  239. }
  240. public function statistics(Request $request)
  241. {
  242. $days = $request->get('days', []);
  243. $classify = $request->get('classify', '');
  244. $categoryId = $request->get('category_id', '');
  245. $goodsName = $request->get('goods_name', '');
  246. $premisesId = $request->get('premises_id', '');
  247. $payType = $request->get('pay_type', '');
  248. if (!empty($days)) {
  249. $days[0] = strtotime($days[0]);
  250. $days[1] = strtotime($days[1]);
  251. if (date('m', $days[0]) != date('m', $days[1])) {
  252. return json_fail('暂不支持跨月查询');
  253. }
  254. $month = date('Ym', $days[0]);
  255. $days[1] = strtotime(date('Y-m-d', $days[1]) . " 23:59:59");
  256. } else {
  257. $month = date('Ym');
  258. }
  259. $categorySearchIds = '';
  260. if (!empty($categoryId)) {
  261. $categorySearchPath = SysCategory::where('category_id', $categoryId)->value('category_super_path');
  262. $categorySearchPath .= '#' . $categoryId . '#';
  263. $categorySearchIds = SysCategory::where('category_super_path', 'like', $categorySearchPath . '%')->pluck('category_id')->toArray();
  264. $categorySearchIds = array_merge($categorySearchIds, [$categoryId]);
  265. $categorySearchIds = implode(',', $categorySearchIds);
  266. }
  267. $where = "where 1";
  268. if (!empty($days)) {
  269. $where .= " and di.data_income_addtimes between {$days[0]} and {$days[1]}";
  270. }
  271. if (!empty($classify)) {
  272. $where .= " and di.data_income_classify = '{$classify}'";
  273. }
  274. if (!empty($categorySearchIds)) {
  275. $where .= " and g.join_goods_category_id in({$categorySearchIds})";
  276. }
  277. if (!empty($goodsName)) {
  278. $where .= " and g.goods_name like '%{$goodsName}%' ";
  279. }
  280. if (!empty($premisesId)) {
  281. $where .= " and di.join_data_income_dept_id = {$premisesId}";
  282. }
  283. if (!empty($payType)) {
  284. $where .= " and di.data_income_pay_type = '{$payType}'";
  285. }
  286. $statisticsSql = "
  287. select
  288. SUM(JSON_EXTRACT(join_data_income_object_json,'$.sheet[0].order_sheet_num')) as num,SUM(data_income_amount) as amount,COUNT(*) as count
  289. from app_data_income_{$month} as di
  290. left join app_goods as g on REPLACE(JSON_EXTRACT(di.join_data_income_object_json,'$.sheet[0].goods_id'),'\"','') = g.goods_id
  291. {$where}
  292. ";
  293. $statistics = [];
  294. $category = ['all', 'entity', 'service', 'package'];
  295. foreach ($category as $item) {
  296. $execSql = $statisticsSql;
  297. if ($item == 'entity') {
  298. $execSql .= " and data_income_classify in('GOODS')";
  299. } elseif ($item == 'service') {
  300. $execSql .= " and data_income_classify in('MEALS','CHNMED')";
  301. } elseif ($item == 'package') {
  302. $execSql .= " and data_income_classify = 'PACKAGE'";
  303. }
  304. $res = Db::select($execSql . ' limit 1');
  305. $statistics[$item] = $res[0];
  306. }
  307. return json_success('', $statistics);
  308. }
  309. }