WriteOffController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. ssd.dept_id as ssd_dept_id, ssd.dept_name as ssd_dept_name
  69. from app_data_used_{$month} as du
  70. left join app_goods as g on REPLACE(JSON_EXTRACT(du.join_data_used_object_json,'$.goods_id'),'\"','') = CAST(g.goods_id AS CHAR)
  71. left join app_goods_sku as gs on JSON_EXTRACT(du.join_data_used_object_json,'$.goods_sku_id') = gs.goods_sku_id
  72. left join app_sys_user as wu on du.join_data_used_user_id = wu.user_id
  73. left join app_sys_user as su on JSON_EXTRACT(du.join_data_used_object_json,'$.salesman_id') = su.user_id
  74. left join app_sys_dept as ssd on JSON_EXTRACT(du.join_data_used_object_json,'$.salesman_premises_id') = ssd.dept_id
  75. {$where}
  76. order by du.data_used_addtimes desc
  77. limit " . $recordStart . ',' . $pageSize;
  78. $rows = Db::select($sql);
  79. foreach ($rows as &$row){
  80. $row->salesman_premises = '';
  81. if (!empty($row->premises_id) && $row->premises_id != 0){
  82. $premises = SysDept::where('dept_id',$row->premises_id)
  83. ->select('dept_id','dept_name')
  84. ->first();
  85. $row->salesman_premises = !empty($premises) ? $premises->dept_name : '';
  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 $key => $item){
  91. if(is_array($item)){
  92. $item = implode('',$item);
  93. }
  94. $skuTitle .= $key.':'.$item.',';
  95. }
  96. $row->sku_title = rtrim($skuTitle,',');
  97. }
  98. $categoryClassify = 'SERVICE';
  99. $row->category = '';
  100. if(!empty($row->join_goods_category_id)){
  101. $category = SysCategory::where('category_id',$row->join_goods_category_id)->select('category_name','category_classify')->first();
  102. $row->category = $category->category_name ?? '';
  103. $categoryClassify = $category->category_classify ?? 'SERVICE';
  104. }
  105. $row->write_off_premises = trim($row->write_off_premises,"\"");
  106. $row->data_used_addtimes = date('Y-m-d H:i:s',$row->data_used_addtimes);
  107. }
  108. return json_success('', [
  109. 'page' => $page,
  110. 'pageSize' => $pageSize,
  111. 'total' => $total[0]->total,
  112. 'rows' => $rows
  113. ]);
  114. }
  115. public function statistics(Request $request)
  116. {
  117. $days = $request->get('days',[]);
  118. $salePremisesId = $request->get('sale_premises_id','');
  119. $writeOffPremisesId = $request->get('write_off_premises_id','');
  120. $categoryId = $request->get('category_id','');
  121. $goodsName = $request->get('goods_name','');
  122. if (!empty($days)) {
  123. $days[0] = strtotime($days[0]);
  124. $days[1] = strtotime($days[1]);
  125. if (date('m', $days[0]) != date('m', $days[1])) {
  126. return json_fail('暂不支持跨月查询');
  127. }
  128. $month = date('Ym', $days[0]);
  129. $days[1] = strtotime(date('Y-m-d', $days[1]) . " 23:59:59");
  130. } else {
  131. $month = date('Ym');
  132. }
  133. if(!Util::schema()->hasTable("data_used_{$month}")){
  134. return json_fail('暂无数据');
  135. }
  136. $categorySearchIds = '';
  137. if (!empty($categoryId)){
  138. $categorySearchPath = SysCategory::where('category_id',$categoryId)->value('category_super_path');
  139. $categorySearchPath .= '#'. $categoryId.'#';
  140. $categorySearchIds = SysCategory::where('category_super_path','like',$categorySearchPath.'%')->pluck('category_id')->toArray();
  141. $categorySearchIds = array_merge($categorySearchIds,[$categoryId]);
  142. $categorySearchIds = implode(',',$categorySearchIds);
  143. }
  144. $where = " where 1 ";
  145. if (!empty($days)) {
  146. $where .= "and du.data_used_addtimes between {$days[0]} and {$days[1]} ";
  147. }
  148. if(!empty($salePremisesId)){
  149. $where .= " and JSON_EXTRACT(du.join_data_used_object_json,'$.salesman_premises_id') = {$salePremisesId} ";
  150. }
  151. if (!empty($writeOffPremisesId)){
  152. $where .= " and du.join_data_used_dept_id = {$writeOffPremisesId}";
  153. }
  154. if (!empty($categorySearchIds)){
  155. $where .= " and g.join_goods_category_id in({$categorySearchIds})";
  156. }
  157. if (!empty($goodsName)){
  158. $where .= " and g.goods_name like '%{$goodsName}%' ";
  159. }
  160. $sql = "select
  161. SUM(data_used_count) as total,data_used_classify
  162. from app_data_used_{$month} as du
  163. left join app_goods as g on JSON_EXTRACT(du.join_data_used_object_json,'$.goods_id') = g.goods_id
  164. {$where}
  165. group by data_used_classify";
  166. $rows = Db::select($sql);
  167. $rows = json_decode(json_encode($rows),true);
  168. //核销归类 ORDER-PACKAGE=套包订单|ORDER-DISHES=点餐订单|ROLE-SERVICE=角色权益服务卡|ROLE-GOODS=角色权益产品卡|APPOINTMENT=后付费预约|ORDER=预付费订单
  169. $statistics = ['service'=>0,'meals'=>0,'quota'=>0,'package'=>0,'goods'=>0,'appointment'=>0];
  170. foreach ($rows as &$row){
  171. if($row['data_used_classify'] == 'ORDER-DISHES'){
  172. $statistics['meals'] = $row['total'];
  173. }elseif($row['data_used_classify'] == 'ORDER-PACKAGE'){
  174. $statistics['package'] = $row['total'];
  175. }elseif($row['data_used_classify'] == 'ROLE-SERVICE'){
  176. $statistics['quota'] = $row['total'];
  177. }elseif($row['data_used_classify'] == 'ROLE-GOODS'){
  178. $statistics['goods'] = $row['total'];
  179. }elseif($row['data_used_classify'] == 'APPOINTMENT'){
  180. $statistics['appointment'] = $row['total'];
  181. }elseif($row['data_used_classify'] == 'ORDER'){
  182. $statistics['service'] = $row['total'];
  183. }
  184. }
  185. return json_success('',$statistics);
  186. }
  187. public function records()
  188. {
  189. $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
  190. left join app_member as m on m.member_id = a.join_appointment_member_id
  191. left join app_member_cert as c on c.join_cert_member_id = a.join_appointment_member_id
  192. left join app_member_benefit as b on b.join_benefit_member_id = a.join_appointment_member_id
  193. UNION
  194. 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
  195. left join app_member as m on m.member_id = q.join_quota_member_id
  196. left join app_member_cert as c on c.join_cert_member_id = q.join_quota_member_id
  197. order by addtimes desc limit 0,10";
  198. $records = Db::select($sql);
  199. dump($records);
  200. }
  201. }