NowReportHandle.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\command;
  4. use think\console\Command;
  5. use think\console\Input;
  6. use think\console\Output;
  7. use think\Exception;
  8. use think\facade\Cache;
  9. use think\facade\Db;
  10. //处理报表预约记录,生成报表文件
  11. class NowReportHandle extends Command
  12. {
  13. //redis队列的key,在ReportReserve控制器中也有定义,要同步修改(轻易不要修改)
  14. private $key = 'nowreport';
  15. protected function configure()
  16. {
  17. // 指令配置
  18. $this->setName('now_report')
  19. ->setDescription('定时处理报表预约,生成报表文件');
  20. }
  21. //处理报表预约记录,生成报表文件
  22. protected function execute(Input $input, Output $output)
  23. {
  24. try {
  25. $info = Cache::store('redis')->handler()->rpop($this->key);
  26. if ($info) {
  27. $info = json_decode($info, true);
  28. //不同的方法处理不同的脚本
  29. switch ($info['code']) {
  30. //退货台账-业务口径
  31. case 'A':
  32. $res = $this->A($info['start'], $info['end']);
  33. break;
  34. //库存日报及预警汇总表
  35. case 'B':
  36. $res = $this->B($info['start'], $info['end']);
  37. break;
  38. //退货台账
  39. case 'C':
  40. $res = $this->C($info['start'], $info['end']);
  41. break;
  42. default:
  43. throw new Exception('暂不支持这个报表');
  44. }
  45. $file = excelSaveFile($res, $info['name'] . date('YmdHis'));
  46. Db::name('exec')
  47. ->where(['id' => $info['id'], 'status' => 1])//status==1 待处理
  48. ->update([
  49. 'status' => 2, //status==2 处理完成
  50. 'down_url' => $file,
  51. 'updatetime' => date('Y-m-d H:i:s'),
  52. 'expiretime' => date('Y-m-d H:i:s', strtotime('+7 day'))
  53. ]);
  54. $output->writeln('【' . $info['id'] . '】该预约记录处理成功');
  55. } else $output->writeln('没有可供处理的报表预约记录');
  56. } catch (Exception $exception) {
  57. $output->writeln($exception->getMessage() . '|' . $exception->getFile() . '|' . $exception->getLine());
  58. }
  59. }
  60. //退货台账-业务口径
  61. private function A(string $start_date = '', string $end_date = '')
  62. {
  63. $data = Db::name('sale_return')
  64. ->alias('sr')
  65. ->field('sr.returnCode 退货单号,sr.status 流程进度,sr.addtime 退货发起日期,b.company 公司名称, ci1.name 退货人所在部门,sr.apply_name 退货人,sr.num 退货数量,sr.total_fee 退货销售货款,sr.remark 退货备注,c.companyName 客户名称,or.customer_code 客户编码,"" 一级组织,"" 二级组织,"" 三级组织,p.platform_name 平台名称,s.poNo PO编号,sr.orderCode 确认单编号,s.order_type 确认单类型,s.addtime 确认单下单时间,ci2.name 业务人员所在部门,s.apply_name 业务人员,sr.orderCode 确认单产品编号,sr.good_name 产品名称,sr.good_code 产品编码,"" 一级分类,s.cat_id,c.itemid')
  66. ->leftJoin('platform p', 'p.id = sr.platform_id')
  67. ->leftJoin('order_return or', 'or.orderCode = sr.orderCode')
  68. ->leftJoin('customer_info c', 'c.companyNo = or.customer_code')
  69. ->leftJoin('sale s', 's.orderCode = sr.orderCode')
  70. ->leftJoin('business b', 'b.companyNo = s.supplierNo')
  71. ->leftJoin('depart_user du1', 'du1.uid = sr.apply_id')
  72. ->leftJoin('company_item ci1', 'ci1.id = du1.itemid')
  73. ->leftJoin('depart_user du2', 'du2.uid = s.apply_id')
  74. ->leftJoin('company_item ci2', 'ci2.id = du2.itemid')
  75. ->where('sr.is_del', 0)
  76. ->whereBetween('sr.addtime', [$start_date, $end_date])
  77. ->order('sr.addtime', 'desc')
  78. ->cursor();
  79. $all_sale_return_status = [1 => '待业务审批', 2 => '待专员审批', 3 => '待主管审批', 4 => '退货完成', 5 => '业务驳回', 6 => '采购驳回', 7 => '专员审批不通过'];
  80. $all_sale_order_type = [1 => '备库', 2 => '非库存', 3 => '咨询采反', 4 => '项目采反', 5 => '平台部订单销售',];
  81. $list = [];
  82. foreach ($data as $value) {
  83. $value['流程进度'] = isset($all_sale_return_status[$value['流程进度']]) ? $all_sale_return_status[$value['流程进度']] : '';
  84. $value['确认单类型'] = isset($all_sale_order_type[$value['确认单类型']]) ? $all_sale_order_type[$value['确认单类型']] : '';
  85. $top = made($value['cat_id']);
  86. $value['一级分类'] = isset($top[0]['name']) ? $top[0]['name'] : '';
  87. if (!empty($value['itemid'])) {
  88. $customer_org1 = get_top_customer_org($value['itemid']);
  89. foreach ($customer_org1 as $vv) {
  90. switch ($vv['level']) {
  91. case 1:
  92. $value['一级组织'] = $vv['name'];
  93. break;
  94. case 2:
  95. $value['二级组织'] = $vv['name'];
  96. break;
  97. case 3:
  98. $value['三级组织'] = $vv['name'];
  99. break;
  100. }
  101. }
  102. }
  103. unset($value['cat_id']);
  104. unset($value['itemid']);
  105. yield $list[] = $value;
  106. }
  107. return $list;
  108. }
  109. //库存预警汇总表
  110. private function B(string $start_date = '', string $end_date = '')
  111. {
  112. $data = Db::name('good_stock')
  113. ->alias('gs')
  114. ->field('b.company 公司名称,pi.updatetime 最近一次入库时间,pi.apply_name 采购员,gs.spuCode 产品编码,"" 一级分类,gb.good_name 产品名称,"" 成本单价,gs.wait_out_stock 待出库量,gs.wait_in_stock 待入库量,gs.usable_stock 可用库存数量,gs.total_stock 当前库存量,"" 可用库存金额,"" 当前库存金额,"" 保质期时间,"" 库存天数,po.supplier_name 供应商名称,wi.name 仓库名称,gb.cat_id')
  115. ->leftJoin('purchease_in pi', 'pi.wsm_code = gs.wsm_code AND pi.status=4')
  116. ->leftJoin('purchease_order po', 'po.cgdNo = pi.cgdNo')
  117. ->leftJoin('warehouse_info wi', 'wi.wsm_code = gs.wsm_code')
  118. ->leftJoin('business b', 'b.companyNo = po.companyNo')
  119. ->leftJoin('good_basic gb', 'gb.spuCode = gs.spuCode')
  120. ->where('gs.is_del', 0)
  121. ->whereBetween('gs.addtime', [$start_date, $end_date])
  122. ->order('gs.addtime', 'desc')
  123. ->cursor();
  124. $list = [];
  125. foreach ($data as $value) {
  126. $value['一级分类'] = implode('/', array_column(made($value['cat_id']), 'name'));
  127. $value['成本单价'] = Db::name('good_nake')
  128. ->where('spuCode', $value['产品编码'])
  129. ->order('min_num', 'asc')
  130. ->value('nake_fee', '');
  131. $value['可用库存金额'] = round($value['可用库存数量'] * $value['成本单价'], 2);
  132. $value['当前库存金额'] = round($value['当前库存量'] * $value['成本单价'], 2);
  133. if (!empty($value['最近一次入库时间'])) {
  134. $value['保质期时间'] = date('Y-m-d H:i:s', strtotime($value['最近一次入库时间']) + 31536000);//365天之后
  135. $value['库存天数'] = bcdiv((string)(time() - strtotime($value['最近一次入库时间'])), (string)(24 * 3600));//365天之后
  136. }
  137. unset($value['cat_id']);
  138. yield $list[] = $value;
  139. }
  140. return $list;
  141. }
  142. //退货台账
  143. private function C(string $start_date = '', string $end_date = '')
  144. {
  145. $data = Db::name('sale_return')
  146. ->alias('sr')
  147. ->field('DATE_FORMAT(sr.addtime,"%Y") 年,DATE_FORMAT(sr.addtime,"%m") 月,DATE_FORMAT(sr.addtime,"%d") 日,b.company 公司名称,sr.returnCode 退货单号,sr.status 流程进度,ci2.name 业务人员所在部门,s.apply_name 业务人员,sr.orderCode 订单编号,p.platform_name 平台名称,s.workNo 平台订单号,"" 一级组织,"" 二级组织,"" 三级组织,c.companyName 客户名称,gp.spuCode 产品编码,sr.good_name 产品名称,"" 一级分类,"" 二级分类,"" 三级分类,cat.fund_code 财务核算码,u.unit 单位,sr.num 退货数量,s.sale_price 销售单价,round(sale_price*sr.num,2) 退货销售货款,sr.remark 退货备注,on.cgdNo 采购单号,po.cgder 采购员,po.nake_fee 成本裸价,po.pakge_fee 包装费,po.delivery_fee 物流费,po.cert_fee 证书费,po.mark_fee 加标费,po.open_fee 开模费,po.diff_weight 工差,po.diff_fee 采购工差金额,po.good_price 采购单价,round(sr.num*po.good_price-po.diff_fee,2) 退货采购货款,gb.tax 采购税点,supp.name 供应商名称,wi.name 库房名称,pi.sendtype 发货方式,supp.pay_type 结算方式,c.itemid,s.cat_id')
  148. ->leftJoin('platform p', 'p.id = sr.platform_id')
  149. ->leftJoin('order_return or', 'or.orderCode = sr.orderCode')
  150. ->leftJoin('customer_info c', 'c.companyNo = or.customer_code')
  151. ->leftJoin('sale s', 's.orderCode = sr.orderCode')
  152. ->leftJoin('business b', 'b.companyNo = s.supplierNo')
  153. ->leftJoin('depart_user du2', 'du2.uid = s.apply_id')
  154. ->leftJoin('company_item ci2', 'ci2.id = du2.itemid')
  155. ->leftJoin('good_platform gp', 'gp.skuCode = s.skuCode')
  156. ->leftJoin('good_basic gb', 'gb.spuCode = gp.spuCode')
  157. ->leftJoin('unit u', 'u.id = gb.good_unit')
  158. ->leftJoin('order_num on', 'on.orderCode =sr.orderCode')
  159. ->leftJoin('cat cat', 'cat.id =s.cat_id')
  160. ->leftJoin('purchease_order po', 'po.cgdNo =on.cgdNo')
  161. ->leftJoin('purchease_in pi', 'pi.cgdNo =on.cgdNo')
  162. ->leftJoin('supplier supp', 'supp.code =s.supplierNo')
  163. ->leftJoin('warehouse_info wi', 'wi.wsm_code =po.wsm_code')
  164. ->where('sr.is_del', 0)
  165. ->whereBetween('sr.addtime', [$start_date, $end_date])
  166. ->order('sr.addtime', 'desc')
  167. ->cursor();
  168. $all_sale_return_status = [1 => '待业务审批', 2 => '待专员审批', 3 => '待主管审批', 4 => '退货完成', 5 => '业务驳回', 6 => '采购驳回', 7 => '专员审批不通过'];
  169. $all_sendtype = [1 => '公司自提', 2 => '供应商包邮'];
  170. $list = [];
  171. foreach ($data as $value) {
  172. $value['流程进度'] = isset($all_sale_return_status[$value['流程进度']]) ? $all_sale_return_status[$value['流程进度']] : '';
  173. $value['发货方式'] = isset($all_sendtype[$value['发货方式']]) ? $all_sendtype[$value['发货方式']] : '';
  174. $top = made($value['cat_id']);
  175. $value['一级分类'] = isset($top[0]['name']) ? $top[0]['name'] : '';
  176. $value['二级分类'] = isset($top[1]['name']) ? $top[1]['name'] : '';
  177. $value['三级分类'] = isset($top[2]['name']) ? $top[2]['name'] : '';
  178. if (!empty($value['itemid'])) {
  179. $customer_org1 = get_top_customer_org($value['itemid']);
  180. foreach ($customer_org1 as $vv) {
  181. switch ($vv['level']) {
  182. case 1:
  183. $value['一级组织'] = $vv['name'];
  184. break;
  185. case 2:
  186. $value['二级组织'] = $vv['name'];
  187. break;
  188. case 3:
  189. $value['三级组织'] = $vv['name'];
  190. break;
  191. }
  192. }
  193. }
  194. unset($value['cat_id']);
  195. unset($value['itemid']);
  196. yield $list[] = $value;
  197. }
  198. return $list;
  199. }
  200. }