GoodsSalesController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace app\admin\controller\finance;
  3. use app\model\OrderSheet;
  4. use app\model\SysCategory;
  5. use support\Db;
  6. use support\Request;
  7. class GoodsSalesController
  8. {
  9. public function list(Request $request)
  10. {
  11. $page = $request->get('page', 1);
  12. $pageSize = $request->get('pageSize', 20);
  13. $days = $request->get('days', []);
  14. $classify = $request->get('classify','');
  15. $categoryId = $request->get('category_id','');
  16. $goodsName = $request->get('goods_name','');
  17. $premisesId = $request->get('premises_id','');
  18. $payType = $request->get('pay_type','');
  19. if (!empty($days)) {
  20. $days[0] = strtotime($days[0]);
  21. $days[1] = strtotime($days[1]);
  22. if (date('m', $days[0]) != date('m', $days[1])) {
  23. return json_fail('暂不支持跨月查询');
  24. }
  25. $month = date('Ym', $days[0]);
  26. $days[1] = strtotime(date('Y-m-d', $days[1]) . " 23:59:59");
  27. } else {
  28. $month = date('Ym');
  29. }
  30. $categorySearchIds = '';
  31. if (!empty($categoryId)){
  32. $categorySearchPath = SysCategory::where('category_id',$categoryId)->value('category_super_path');
  33. $categorySearchPath .= '#'. $categoryId.'#';
  34. $categorySearchIds = SysCategory::where('category_super_path','like',$categorySearchPath.'%')->pluck('category_id')->toArray();
  35. $categorySearchIds = array_merge($categorySearchIds,[$categoryId]);
  36. $categorySearchIds = implode(',',$categorySearchIds);
  37. }
  38. $recordStart = ($page - 1) * 20;
  39. $where = "where 1";
  40. if (!empty($days)) {
  41. $where .= " and di.data_income_addtimes between {$days[0]} and {$days[1]}";
  42. }
  43. if (!empty($classify)){
  44. $where .= " and di.data_income_classify = '{$classify}'";
  45. }
  46. if (!empty($categorySearchIds)){
  47. $where .= " and g.join_goods_category_id in({$categorySearchIds})";
  48. }
  49. if (!empty($goodsName)){
  50. $where .= " and g.goods_name like '%{$goodsName}%' ";
  51. }
  52. if (!empty($premisesId)){
  53. $where .= " and di.join_data_income_dept_id = {$premisesId}";
  54. }
  55. if (!empty($payType)){
  56. $where .= " and di.data_income_pay_type = '{$payType}'";
  57. }
  58. $totalSql = "
  59. select count(*) as total from (
  60. select
  61. data_income_classify
  62. from app_data_income_{$month} as di
  63. left join app_goods as g on REPLACE(JSON_EXTRACT(di.join_data_income_object_json,'$.sheet[0].goods_id'),'\"','') = g.goods_id
  64. {$where}
  65. 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
  66. ) COUNT
  67. ";
  68. $total = Db::select($totalSql);
  69. $sql = "select
  70. 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,
  71. 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
  72. from app_data_income_{$month} as di
  73. 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)
  74. 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
  75. {$where}
  76. group by g.goods_id,gs.goods_sku_id,di.data_income_classify
  77. order by addtimes desc
  78. limit " . $recordStart . ',' . $pageSize;
  79. $rows = Db::select($sql);
  80. $rows = json_decode(json_encode($rows), true);
  81. $categoryIds = array_unique(array_filter(array_column($rows, 'join_goods_category_id')));
  82. $categorys = SysCategory::whereIn('category_id', $categoryIds)->pluck('category_name', 'category_id')->toArray();
  83. foreach ($rows as &$row) {
  84. if (!empty($row['join_goods_category_id']) && isset($categorys[$row['join_goods_category_id']])) {
  85. $row['category_name'] = $categorys[$row['join_goods_category_id']];
  86. }
  87. if (!empty($row['goods_sku_specs_json'])) {
  88. $specsJson = json_decode($row['goods_sku_specs_json'], true);
  89. $skuTitle = '';
  90. foreach ($specsJson as $k => $value) {
  91. $value = is_array($value) ? $value[0] : $value;
  92. $skuTitle .= $k . ':' . $value . ';';
  93. }
  94. $row['sku'] = rtrim($skuTitle, ';');
  95. }
  96. }
  97. return json_success('', [
  98. 'page' => $page,
  99. 'pageSize' => $pageSize,
  100. 'total' => $total[0]->total,
  101. 'rows' => $rows
  102. ]);
  103. }
  104. public function statistics(Request $request)
  105. {
  106. $days = $request->get('days', []);
  107. $classify = $request->get('classify','');
  108. $categoryId = $request->get('category_id','');
  109. $goodsName = $request->get('goods_name','');
  110. $premisesId = $request->get('premises_id','');
  111. $payType = $request->get('pay_type','');
  112. if (!empty($days)) {
  113. $days[0] = strtotime($days[0]);
  114. $days[1] = strtotime($days[1]);
  115. if (date('m', $days[0]) != date('m', $days[1])) {
  116. return json_fail('暂不支持跨月查询');
  117. }
  118. $month = date('Ym', $days[0]);
  119. $days[1] = strtotime(date('Y-m-d', $days[1]) . " 23:59:59");
  120. } else {
  121. $month = date('Ym');
  122. }
  123. $categorySearchIds = '';
  124. if (!empty($categoryId)){
  125. $categorySearchPath = SysCategory::where('category_id',$categoryId)->value('category_super_path');
  126. $categorySearchPath .= '#'. $categoryId.'#';
  127. $categorySearchIds = SysCategory::where('category_super_path','like',$categorySearchPath.'%')->pluck('category_id')->toArray();
  128. $categorySearchIds = array_merge($categorySearchIds,[$categoryId]);
  129. $categorySearchIds = implode(',',$categorySearchIds);
  130. }
  131. $where = "where 1";
  132. if (!empty($days)) {
  133. $where .= " and di.data_income_addtimes between {$days[0]} and {$days[1]}";
  134. }
  135. if (!empty($classify)){
  136. $where .= " and di.data_income_classify = '{$classify}'";
  137. }
  138. if (!empty($categorySearchIds)){
  139. $where .= " and g.join_goods_category_id in({$categorySearchIds})";
  140. }
  141. if (!empty($goodsName)){
  142. $where .= " and g.goods_name like '%{$goodsName}%' ";
  143. }
  144. if (!empty($premisesId)){
  145. $where .= " and di.join_data_income_dept_id = {$premisesId}";
  146. }
  147. if (!empty($payType)){
  148. $where .= " and di.data_income_pay_type = '{$payType}'";
  149. }
  150. $statisticsSql = "
  151. select
  152. SUM(JSON_EXTRACT(join_data_income_object_json,'$.sheet[0].order_sheet_num')) as num,SUM(data_income_amount) as amount,COUNT(*) as count
  153. from app_data_income_{$month} as di
  154. left join app_goods as g on REPLACE(JSON_EXTRACT(di.join_data_income_object_json,'$.sheet[0].goods_id'),'\"','') = g.goods_id
  155. {$where}
  156. ";
  157. $statistics = [];
  158. $category = ['all', 'entity', 'service', 'package'];
  159. foreach ($category as $item) {
  160. $execSql = $statisticsSql;
  161. if ($item == 'entity') {
  162. $execSql .= " and data_income_classify in('GOODS')";
  163. } elseif ($item == 'service') {
  164. $execSql .= " and data_income_classify in('MEALS','CHNMED')";
  165. } elseif ($item == 'package') {
  166. $execSql .= " and data_income_classify = 'PACKAGE'";
  167. }
  168. $res = Db::select($execSql . ' limit 1');
  169. $statistics[$item] = $res[0];
  170. }
  171. return json_success('', $statistics);
  172. }
  173. }