WriteOffController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace app\admin\controller\finance;
  3. use app\common\Util;
  4. use app\model\SysCategory;
  5. use app\model\SysDept;
  6. use support\Db;
  7. use support\Request;
  8. class WriteOffController
  9. {
  10. public function list(Request $request)
  11. {
  12. $page = $request->get('page',1);
  13. $pageSize = $request->get('pageSize',20);
  14. $days = $request->get('days',[]);
  15. $salePremisesId = $request->get('sale_premises_id','');
  16. $writeOffPremisesId = $request->get('write_off_premises_id','');
  17. $categoryId = $request->get('category_id','');
  18. $goodsName = $request->get('goods_name','');
  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. if(!Util::schema()->hasTable("data_used_{$month}")){
  31. return json_fail('暂无数据');
  32. }
  33. $categorySearchIds = '';
  34. if (!empty($categoryId)){
  35. $categorySearchPath = SysCategory::where('category_id',$categoryId)->value('category_super_path');
  36. $categorySearchPath .= '#'. $categoryId.'#';
  37. $categorySearchIds = SysCategory::where('category_super_path','like',$categorySearchPath.'%')->pluck('category_id')->toArray();
  38. $categorySearchIds = array_merge($categorySearchIds,[$categoryId]);
  39. $categorySearchIds = implode(',',$categorySearchIds);
  40. }
  41. $where = " where 1 ";
  42. if (!empty($days)) {
  43. $where .= "and du.data_used_addtimes between {$days[0]} and {$days[1]} ";
  44. }
  45. if(!empty($salePremisesId)){
  46. $where .= " and JSON_EXTRACT(du.join_data_used_object_json,'$.salesman_premises_id') = {$salePremisesId} ";
  47. }
  48. if (!empty($writeOffPremisesId)){
  49. $where .= " and du.join_data_used_dept_id = {$writeOffPremisesId}";
  50. }
  51. if (!empty($categorySearchIds)){
  52. $where .= " and g.join_goods_category_id in({$categorySearchIds})";
  53. }
  54. if (!empty($goodsName)){
  55. $where .= " and g.goods_name like '%{$goodsName}%' ";
  56. }
  57. $totalSql = "
  58. select
  59. count(*) as total
  60. from app_data_used_{$month} as du
  61. left join app_goods as g on JSON_EXTRACT(du.join_data_used_object_json,'$.goods_id') = g.goods_id
  62. {$where}";
  63. $total = Db::select($totalSql);
  64. $recordStart = ($page - 1) * 20;
  65. $sql = "select
  66. g.goods_name,g.goods_sales_price,g.join_goods_category_id,gs.goods_sku_specs_json,wu.user_name as wu_username, su.user_name as su_username,du.data_used_addtimes,
  67. JSON_EXTRACT(du.join_data_used_object_json,'$.charge.charge_premises') as write_off_premises, su.join_user_dept_id as premises_id,du.data_used_count as nbr
  68. from app_data_used_{$month} as du
  69. left join app_goods as g on JSON_EXTRACT(du.join_data_used_object_json,'$.goods_id') = g.goods_id
  70. left join app_goods_sku as gs on JSON_EXTRACT(du.join_data_used_object_json,'$.goods_sku_id') = gs.goods_sku_id
  71. left join app_sys_user as wu on du.join_data_used_user_id = wu.user_id
  72. left join app_sys_user as su on JSON_EXTRACT(du.join_data_used_object_json,'$.salesman_id') = su.user_id
  73. {$where}
  74. order by du.data_used_addtimes desc
  75. limit " . $recordStart . ',' . $pageSize;
  76. $rows = Db::select($sql);
  77. foreach ($rows as &$row){
  78. $row->salesman_premises = '';
  79. if (!empty($row->premises_id) && $row->premises_id != 0){
  80. $premises = SysDept::where('dept_id',$row->premises_id)
  81. ->select('dept_id','dept_name')
  82. ->first();
  83. $row->salesman_premises = !empty($premises) ? $premises->dept_name : '';
  84. }
  85. if(!empty($row->goods_sku_specs_json)){
  86. $specsJson = json_decode($row->goods_sku_specs_json,true);
  87. $skuTitle = '';
  88. foreach($specsJson as $key => $item){
  89. if(is_array($item)){
  90. $item = implode('',$item);
  91. }
  92. $skuTitle .= $key.':'.$item.',';
  93. }
  94. $row->sku_title = rtrim($skuTitle,',');
  95. }
  96. $categoryClassify = 'SERVICE';
  97. $row->category = '';
  98. if(!empty($row->join_goods_category_id)){
  99. $category = SysCategory::where('category_id',$row->join_goods_category_id)->select('category_name','category_classify')->first();
  100. $row->category = $category->category_name ?? '';
  101. $categoryClassify = $category->category_classify ?? 'SERVICE';
  102. }
  103. $row->write_off_premises = trim($row->write_off_premises,"\"");
  104. $row->data_used_addtimes = date('Y-m-d H:i:s',$row->data_used_addtimes);
  105. }
  106. return json_success('', [
  107. 'page' => $page,
  108. 'pageSize' => $pageSize,
  109. 'total' => $total[0]->total,
  110. 'rows' => $rows
  111. ]);
  112. }
  113. public function statistics(Request $request)
  114. {
  115. $days = $request->get('days',[]);
  116. $salePremisesId = $request->get('sale_premises_id','');
  117. $writeOffPremisesId = $request->get('write_off_premises_id','');
  118. $categoryId = $request->get('category_id','');
  119. $goodsName = $request->get('goods_name','');
  120. if (!empty($days)) {
  121. $days[0] = strtotime($days[0]);
  122. $days[1] = strtotime($days[1]);
  123. if (date('m', $days[0]) != date('m', $days[1])) {
  124. return json_fail('暂不支持跨月查询');
  125. }
  126. $month = date('Ym', $days[0]);
  127. $days[1] = strtotime(date('Y-m-d', $days[1]) . " 23:59:59");
  128. } else {
  129. $month = date('Ym');
  130. }
  131. if(!Util::schema()->hasTable("data_used_{$month}")){
  132. return json_fail('暂无数据');
  133. }
  134. $categorySearchIds = '';
  135. if (!empty($categoryId)){
  136. $categorySearchPath = SysCategory::where('category_id',$categoryId)->value('category_super_path');
  137. $categorySearchPath .= '#'. $categoryId.'#';
  138. $categorySearchIds = SysCategory::where('category_super_path','like',$categorySearchPath.'%')->pluck('category_id')->toArray();
  139. $categorySearchIds = array_merge($categorySearchIds,[$categoryId]);
  140. $categorySearchIds = implode(',',$categorySearchIds);
  141. }
  142. $where = " where 1 ";
  143. if (!empty($days)) {
  144. $where .= "and du.data_used_addtimes between {$days[0]} and {$days[1]} ";
  145. }
  146. if(!empty($salePremisesId)){
  147. $where .= " and JSON_EXTRACT(du.join_data_used_object_json,'$.salesman_premises_id') = {$salePremisesId} ";
  148. }
  149. if (!empty($writeOffPremisesId)){
  150. $where .= " and du.join_data_used_dept_id = {$writeOffPremisesId}";
  151. }
  152. if (!empty($categorySearchIds)){
  153. $where .= " and g.join_goods_category_id in({$categorySearchIds})";
  154. }
  155. if (!empty($goodsName)){
  156. $where .= " and g.goods_name like '%{$goodsName}%' ";
  157. }
  158. $sql = "select
  159. SUM(data_used_count) as total,data_used_classify
  160. from app_data_used_{$month} as du
  161. left join app_goods as g on JSON_EXTRACT(du.join_data_used_object_json,'$.goods_id') = g.goods_id
  162. {$where}
  163. group by data_used_classify";
  164. dump($sql);
  165. $rows = Db::select($sql);
  166. $rows = json_decode(json_encode($rows),true);
  167. //核销归类 ORDER-PACKAGE=套包订单|ORDER-DISHES=点餐订单|ROLE-SERVICE=角色权益服务卡|ROLE-GOODS=角色权益产品卡|APPOINTMENT=后付费预约|ORDER=预付费订单
  168. $statistics = ['service'=>0,'meals'=>0,'quota'=>0,'package'=>0,'goods'=>0,'appointment'=>0];
  169. foreach ($rows as &$row){
  170. if($row['data_used_classify'] == 'ORDER-DISHES'){
  171. $statistics['meals'] = $row['total'];
  172. }elseif($row['data_used_classify'] == 'ORDER-PACKAGE'){
  173. $statistics['package'] = $row['total'];
  174. }elseif($row['data_used_classify'] == 'ROLE-SERVICE'){
  175. $statistics['quota'] = $row['total'];
  176. }elseif($row['data_used_classify'] == 'ROLE-GOODS'){
  177. $statistics['goods'] = $row['total'];
  178. }elseif($row['data_used_classify'] == 'APPOINTMENT'){
  179. $statistics['appointment'] = $row['total'];
  180. }elseif($row['data_used_classify'] == 'ORDER'){
  181. $statistics['service'] = $row['total'];
  182. }
  183. }
  184. return json_success('',$statistics);
  185. }
  186. public function records()
  187. {
  188. $sql = "select a.appointment_id as benefit_id,a.appointment_addtimes as addtimes,m.member_mobile,c.member_cert_name,b.member_benefit_name as benefit_name,a.appointment_done_json as used_json,a.appointment_done_datetime from app_appointment as a
  189. left join app_member as m on m.member_id = a.join_appointment_member_id
  190. left join app_member_cert as c on c.join_cert_member_id = a.join_appointment_member_id
  191. left join app_member_benefit as b on b.join_benefit_member_id = a.join_appointment_member_id
  192. UNION
  193. select q.member_quota_id as benefit_id,q.member_quota_addtimes as addtimes,q.member_quota_used_json as used_json, q.member_quota_name as benefit_name,m.member_mobile,c.member_cert_name from app_member_quota as q
  194. left join app_member as m on m.member_id = q.join_quota_member_id
  195. left join app_member_cert as c on c.join_cert_member_id = q.join_quota_member_id
  196. order by addtimes desc limit 0,10";
  197. $records = Db::select($sql);
  198. dump($records);
  199. }
  200. }