NowReportHandle.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. //
  43. case 'D':
  44. $res = $this->D($info['start'], $info['end']);
  45. break;
  46. default:
  47. throw new Exception('暂不支持这个报表');
  48. }
  49. $file = excelSaveFile($res, $info['name'] . date('YmdHis'));
  50. Db::name('exec')
  51. ->where(['id' => $info['id'], 'status' => 1])//status==1 待处理
  52. ->update([
  53. 'status' => 2, //status==2 处理完成
  54. 'down_url' => $file,
  55. 'updatetime' => date('Y-m-d H:i:s'),
  56. 'expiretime' => date('Y-m-d H:i:s', strtotime('+7 day'))
  57. ]);
  58. $output->writeln('【' . $info['id'] . '】该预约记录处理成功');
  59. } else $output->writeln('没有可供处理的报表预约记录');
  60. } catch (Exception $exception) {
  61. $output->writeln($exception->getMessage() . '|' . $exception->getFile() . '|' . $exception->getLine());
  62. }
  63. }
  64. //退货台账-业务口径
  65. private function A(string $start_date = '', string $end_date = '')
  66. {
  67. $data = Db::name('sale_return')
  68. ->alias('sr')
  69. ->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')
  70. ->leftJoin('platform p', 'p.id = sr.platform_id')
  71. ->leftJoin('order_return or', 'or.orderCode = sr.orderCode')
  72. ->leftJoin('customer_info c', 'c.companyNo = or.customer_code')
  73. ->leftJoin('sale s', 's.orderCode = sr.orderCode')
  74. ->leftJoin('business b', 'b.companyNo = s.supplierNo')
  75. ->leftJoin('depart_user du1', 'du1.uid = sr.apply_id')
  76. ->leftJoin('company_item ci1', 'ci1.id = du1.itemid')
  77. ->leftJoin('depart_user du2', 'du2.uid = s.apply_id')
  78. ->leftJoin('company_item ci2', 'ci2.id = du2.itemid')
  79. ->where('sr.is_del', 0)
  80. ->whereBetween('sr.addtime', [$start_date, $end_date])
  81. ->order('sr.addtime', 'desc')
  82. ->cursor();
  83. $all_sale_return_status = [1 => '待业务审批', 2 => '待专员审批', 3 => '待主管审批', 4 => '退货完成', 5 => '业务驳回', 6 => '采购驳回', 7 => '专员审批不通过'];
  84. $all_sale_order_type = [1 => '备库', 2 => '非库存', 3 => '咨询采反', 4 => '项目采反', 5 => '平台部订单销售',];
  85. $list = [];
  86. foreach ($data as $value) {
  87. $value['流程进度'] = isset($all_sale_return_status[$value['流程进度']]) ? $all_sale_return_status[$value['流程进度']] : '';
  88. $value['确认单类型'] = isset($all_sale_order_type[$value['确认单类型']]) ? $all_sale_order_type[$value['确认单类型']] : '';
  89. $top = made($value['cat_id']);
  90. $value['一级分类'] = isset($top[0]['name']) ? $top[0]['name'] : '';
  91. if (!empty($value['itemid'])) {
  92. $customer_org1 = get_top_customer_org($value['itemid']);
  93. foreach ($customer_org1 as $vv) {
  94. switch ($vv['level']) {
  95. case 1:
  96. $value['一级组织'] = $vv['name'];
  97. break;
  98. case 2:
  99. $value['二级组织'] = $vv['name'];
  100. break;
  101. case 3:
  102. $value['三级组织'] = $vv['name'];
  103. break;
  104. }
  105. }
  106. }
  107. unset($value['cat_id']);
  108. unset($value['itemid']);
  109. yield $list[] = $value;
  110. }
  111. return $list;
  112. }
  113. //库存预警汇总表
  114. private function B(string $start_date = '', string $end_date = '')
  115. {
  116. $data = Db::name('good_stock')
  117. ->alias('gs')
  118. ->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')
  119. ->leftJoin('purchease_in pi', 'pi.wsm_code = gs.wsm_code AND pi.status=4')
  120. ->leftJoin('purchease_order po', 'po.cgdNo = pi.cgdNo')
  121. ->leftJoin('warehouse_info wi', 'wi.wsm_code = gs.wsm_code')
  122. ->leftJoin('business b', 'b.companyNo = po.companyNo')
  123. ->leftJoin('good_basic gb', 'gb.spuCode = gs.spuCode')
  124. ->where('gs.is_del', 0)
  125. ->whereBetween('gs.addtime', [$start_date, $end_date])
  126. ->order('gs.addtime', 'desc')
  127. ->cursor();
  128. $list = [];
  129. foreach ($data as $value) {
  130. $value['一级分类'] = implode('/', array_column(made($value['cat_id']), 'name'));
  131. $value['成本单价'] = Db::name('good_nake')
  132. ->where('spuCode', $value['产品编码'])
  133. ->order('min_num', 'asc')
  134. ->value('nake_fee', '');
  135. $value['可用库存金额'] = bcmul((string)$value['可用库存数量'] ?? '0', (string)$value['成本单价'] ?? '0', 2);
  136. $value['当前库存金额'] = bcmul((string)$value['当前库存量'] ?? '0', (string)$value['成本单价'] ?? '0', 2);
  137. if (!empty($value['最近一次入库时间'])) {
  138. $value['保质期时间'] = date('Y-m-d H:i:s', strtotime($value['最近一次入库时间']) + 31536000);//365天之后
  139. $value['库存天数'] = bcdiv((string)(time() - strtotime($value['最近一次入库时间'])), (string)(24 * 3600));//365天之后
  140. }
  141. unset($value['cat_id']);
  142. yield $list[] = $value;
  143. }
  144. return $list;
  145. }
  146. //退货台账
  147. private function C(string $start_date = '', string $end_date = '')
  148. {
  149. $data = Db::name('sale_return')
  150. ->alias('sr')
  151. ->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')
  152. ->leftJoin('platform p', 'p.id = sr.platform_id')
  153. ->leftJoin('order_return or', 'or.orderCode = sr.orderCode')
  154. ->leftJoin('customer_info c', 'c.companyNo = or.customer_code')
  155. ->leftJoin('sale s', 's.orderCode = sr.orderCode')
  156. ->leftJoin('business b', 'b.companyNo = s.supplierNo')
  157. ->leftJoin('depart_user du2', 'du2.uid = s.apply_id')
  158. ->leftJoin('company_item ci2', 'ci2.id = du2.itemid')
  159. ->leftJoin('good_platform gp', 'gp.skuCode = s.skuCode')
  160. ->leftJoin('good_basic gb', 'gb.spuCode = gp.spuCode')
  161. ->leftJoin('unit u', 'u.id = gb.good_unit')
  162. ->leftJoin('order_num on', 'on.orderCode =sr.orderCode')
  163. ->leftJoin('cat cat', 'cat.id =s.cat_id')
  164. ->leftJoin('purchease_order po', 'po.cgdNo =on.cgdNo')
  165. ->leftJoin('purchease_in pi', 'pi.cgdNo =on.cgdNo')
  166. ->leftJoin('supplier supp', 'supp.code =s.supplierNo')
  167. ->leftJoin('warehouse_info wi', 'wi.wsm_code =po.wsm_code')
  168. ->where('sr.is_del', 0)
  169. ->whereBetween('sr.addtime', [$start_date, $end_date])
  170. ->order('sr.addtime', 'desc')
  171. ->cursor();
  172. $all_sale_return_status = [1 => '待业务审批', 2 => '待专员审批', 3 => '待主管审批', 4 => '退货完成', 5 => '业务驳回', 6 => '采购驳回', 7 => '专员审批不通过'];
  173. $all_sendtype = [1 => '公司自提', 2 => '供应商包邮'];
  174. $list = [];
  175. foreach ($data as $value) {
  176. $value['流程进度'] = isset($all_sale_return_status[$value['流程进度']]) ? $all_sale_return_status[$value['流程进度']] : '';
  177. $value['发货方式'] = isset($all_sendtype[$value['发货方式']]) ? $all_sendtype[$value['发货方式']] : '';
  178. $top = made($value['cat_id']);
  179. $value['一级分类'] = isset($top[0]['name']) ? $top[0]['name'] : '';
  180. $value['二级分类'] = isset($top[1]['name']) ? $top[1]['name'] : '';
  181. $value['三级分类'] = isset($top[2]['name']) ? $top[2]['name'] : '';
  182. if (!empty($value['itemid'])) {
  183. $customer_org1 = get_top_customer_org($value['itemid']);
  184. foreach ($customer_org1 as $vv) {
  185. switch ($vv['level']) {
  186. case 1:
  187. $value['一级组织'] = $vv['name'];
  188. break;
  189. case 2:
  190. $value['二级组织'] = $vv['name'];
  191. break;
  192. case 3:
  193. $value['三级组织'] = $vv['name'];
  194. break;
  195. }
  196. }
  197. }
  198. unset($value['cat_id']);
  199. unset($value['itemid']);
  200. yield $list[] = $value;
  201. }
  202. return $list;
  203. }
  204. public function D($start,$end){
  205. $list =Db::name("order_out")->alias('woo')->leftJoin("sale a","a.orderCode=woo.orderCode")
  206. ->leftJoin("order_back wor","wor.outCode=woo.outCode and status=4")
  207. ->leftJoin("order_num won","won.orderCode=a.orderCode")
  208. ->leftJoin("purchease_order wpo","wpo.cgdNo=won.cgdNo")
  209. ->leftJoin("good_basic wgb","wgb.spuCode=wpo.spuCode")
  210. ->leftJoin("good_zixun wgz","wgz.spuCode=wpo.spuCode")
  211. ->leftJoin("supplier ws","ws.code=wpo.supplierNo")
  212. ->where('woo.status',">=", 2)
  213. ->whereBetween('woo.addtime', [$start, $end])
  214. ->field(" year(`a`.`addtime`) AS `年`,month(`a`.`addtime`) AS `月`,dayofmonth(`a`.`addtime`) AS `日`,a.supplierNo
  215. as '公司名称', a.apply_id '业务部门名称' ,a.apply_name as '业务人员',a.order_type as '订单类型',a.good_type as '商品类型',woo.outCode '发货编号',
  216. `woo`.`send_num` AS `本次发货数量`,
  217. `woo`.`sendtime` AS `本次发货时间`,
  218. `woo`.`orderCode` AS `确认单编号`,
  219. `a`.`good_code` AS `商品编号`,'' as '一级分类','' as '二级分类',a.cat_id as '三级分类',
  220. '' as '财务核算编码',a.good_name as '商品名称', ifnull( `wgb`.`good_unit`, `wgz`.`good_unit` ) as '单位',
  221. `a`.`good_num` AS `下单数量`,
  222. `a`.`total_price` AS `销售货款`,
  223. `wpo`.`cgdNo` AS `采购单单号`,
  224. `wpo`.`cgder` AS `采购员`,
  225. `wpo`.`nake_fee` AS `采购裸价`,
  226. `wpo`.`mark_fee` AS `加标费`,
  227. `wpo`.`pakge_fee` AS `包装费`,
  228. `wpo`.`cert_fee` AS `证书费`,
  229. `wpo`.`open_fee` AS `开模费`,
  230. `wpo`.`teach_fee` AS `工艺费`,
  231. `wpo`.`delivery_fee` AS `物流费`,
  232. `wpo`.`good_price` AS `成本合计`,
  233. `wpo`.`total_fee` AS `采购货款`,
  234. ifnull( `wgb`.`tax`, `wgz`.`tax` ) AS `采购税点`,
  235. `ws`.`name` AS `供应商名称`,
  236. `wgb`.`is_stock` as '是否库存品'
  237. if (`wgb`.`is_stock`=1,wpo.bkcode,wpo.cgdNo) as '发货方式',
  238. ifnull( `wor`.`thNo`, '' ) AS `退货编号`,
  239. ifnull( `wor`.`return_num`, '' ) AS `退货数量`,
  240. round(( ifnull( `wor`.`return_num`, 0 ) * `a`.`sale_price` ), 2 ) as '退货金额',
  241. '' as '售前退货数量',
  242. `a`.`sale_price` as '售前退货金额'
  243. ")->cursor();
  244. $order_tyepe=["库存销售",'非库存销售','咨询销售','项目销售','平台库存销售','平台非库存销售'];
  245. $good_type=["常规商品",'赠品','样品'];
  246. foreach($list as $value){
  247. $value['公司名称'] = Db::name("business")->where(["companyNo"=>$value['公司名称']])->value('company','');
  248. $value['业务部门名称'] = Db::name("depart_user")->alias("a")->leftJoin("company_item ci","a.itemid=ci.id")
  249. ->where(["a.uid"=>$value['业务部门名称'],"status"=>1,"is_del"=>0])->value('ci.name','');
  250. $value['订单类型'] = $order_tyepe[ $value['订单类型']-1];
  251. $value['商品类型'] = $good_type[ $value['商品类型']-1];
  252. $cat =made($value['三级分类']);
  253. $value['财务核算编码'] =Db::name("cat")->where(["id"=>$value['三级分类']])->value("fund_code",'');
  254. $value['一级分类']=isset($cat[0]['name'])?$cat[0]['name']:"";
  255. $value['二级分类']=isset($cat[1]['name'])?$cat[1]['name']:"";
  256. $value['三级分类']=isset($cat[2]['name'])?$cat[2]['name']:"";
  257. $value['单位']=Db::name("unit")->where(["id"=>$value['单位']])->value("unit",'');
  258. if($value['is_stock']==1){
  259. $sendtype = Db::name("purchease_order")->alias("a")->leftJoin("purchease_in b","a.cgdNo=b.cgdNo")
  260. ->where(["bkcode"=>$value['发货方式'],"order_type"=>[1,5]])->order("b.addtime desc")->value("b
  261. .sendtype",'');
  262. }else{
  263. $sendtype = Db::name("purchease_order")->alias("a")->leftJoin("purchease_in b","a.cgdNo=b.cgdNo")
  264. ->where(["cgdNo"=>$value['发货方式']])->order("b.addtime desc")->value("b.sendtype",'');
  265. }
  266. $value['发货方式'] = $sendtype=1 ? '包邮':$sendtype=2 ? '自提':"";
  267. $value['售前退货数量'] = Db::name("sale_return")->where(["orderCode"=>$value['确认单编号']])->sum('num');
  268. $value['售前退货金额'] =round($value['售前退货数量']* $value['售前退货金额'],2);
  269. yield $list[] = $value;
  270. }
  271. return $list;
  272. }
  273. }