Report.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. <?php
  2. namespace app\command;
  3. use think\console\Command;
  4. use think\console\Input;
  5. use think\console\input\Argument;
  6. use think\console\input\Option;
  7. use think\console\Output;
  8. use think\Exception;
  9. use think\facade\Cache;use think\facade\Db;
  10. class Report extends Command
  11. {
  12. //相关字段的文字转换
  13. private $key='Reportexec';
  14. private $param=[];
  15. private $qrdType = [1 => '库存品', 2 => '非库存品', 3 => '采购反馈'];
  16. private $qrdSource = [1 => '直接下单', 2 => '竞价转单', 3 => '项目转单', 4 => '平台导入', 5 => '有赞平台', 6 => '售后补换货',7=>'报备转单',8=>'支付渠道拆单',9=>"采销补录",10=>"结算补录"];
  17. private $cgdSource = [0=>"备库单",1 => '直接下单', 2 => '竞价转单', 3 => '项目转单', 4 => '平台导入', 5 => '有赞平台', 6 => '售后补换货',7=>'报备转单',
  18. 8=>'支付渠道拆单',9=>"采销补录",10=>"结算补录"];
  19. private $sendType = [1 => '包邮', 2 => '自提'];
  20. private $pay_status = [1 => '未回', 2 => '部分回款', 3 => '已回'];
  21. private $inv_status = [1 => '未开', 2 => '部分开票', 3 => '已开'];
  22. private $cgd_inv_status = [1 => '未回', 2 => '部分回票', 3 => '已回'];
  23. private $cgd_pay_status = [1 => '未付', 2 => '部分付款', 3 => '已付'];
  24. private $invoice_pool_status = [1 => '财务审核通过', 2 => '待财务上传发票', 3 => '金税开票中/验票中', 4 => '开票成功', 5 => '开票失败', 6 => '发票退票/废弃', 7 => '取消申请', 8 => '财务驳回', 9 => '验票失败'];
  25. private $cgdType = [1 => '库存', 2 => '非库存', 3 => '咨询'];
  26. private $invoice_return_status = [0 => '待财务审核', 1 => '退票中', 2 => '退票成功', 3 => '审核驳回', 4 => '退票失败'];
  27. private $sendStatus = [1=>'未发货',2=>'部分发货',3=>'全部发货'];
  28. private $TagName = [1=>'采购单付款',2=>'采购单回票',3=>'销售回款',4=>"销售开票"];
  29. protected function configure()
  30. {
  31. // 指令配置
  32. $this->setName('report')
  33. ->setDescription('the report command');
  34. }
  35. protected function execute(Input $input, Output $output)
  36. {
  37. // 指令输出
  38. $Command = Cache::store('redis')->handler()->get('ExecCommand');
  39. if($Command>=3) return '';
  40. $info = Cache::store('redis')->handler()->rpop($this->key);
  41. if($info==false) return '';
  42. Cache::store('redis')->handler()->incrby('ExecCommand',1);
  43. Db::startTrans();
  44. try{
  45. $info = json_decode($info,true);
  46. ini_set ('memory_limit', '2048M') ;
  47. $date=date("Y-m-d H:i:s");
  48. $param = Db::name("exec_log")->where("id",$info['id'])->findOrEmpty();
  49. if(isset($param) && !empty($param)){
  50. if($param['is_del']==1) throw new \Exception("脚本记录已删除");
  51. switch ($param['action']){
  52. case 'A':
  53. $method="A";
  54. $file ='销售发票申请信息导出';
  55. break;
  56. case 'B':
  57. $method="B";
  58. $file ='进项发票登记导出';
  59. break;
  60. case 'C':
  61. $method="C";
  62. $file ='资金认领导出';
  63. break;
  64. case 'D':
  65. $method="D";
  66. $file = '回款核销明细表';
  67. break;
  68. case 'E':
  69. $method="E";
  70. $file='回票明细表';
  71. break;
  72. case 'F':
  73. $method="F";
  74. $file='经营分析报表';
  75. break;
  76. case 'G':
  77. $method="G";
  78. $file='收入成本明细表';
  79. break;
  80. case 'I':
  81. $method="I";
  82. $file='应收台账表';
  83. break;
  84. case 'J':
  85. $method="J";
  86. $file='用友销票表';
  87. break;
  88. case 'K':
  89. $method="K";
  90. $file='采购单明细表';
  91. break;
  92. case 'M':
  93. $method="M";
  94. $file='对账单汇总表';
  95. break;
  96. case 'N':
  97. $method="N";
  98. $file='退货台账';
  99. break;
  100. case 'Q':
  101. $method="Q";
  102. $file='产品部门销售业绩';
  103. break;
  104. case 'R':
  105. $method="R";
  106. $file='采购部门销售业绩';
  107. break;
  108. default:
  109. $file='暂无数据';
  110. $method="";
  111. break;
  112. }
  113. if ($method == '') throw new Exception('该报表暂无对应的处理方法');
  114. $data =$this->$method($param);
  115. $url = excelSaveFile($data, $file . date('_YmdHis'));//加时间戳,防止同名文件覆盖
  116. Db::name('exec_log')
  117. ->where(['id' => $param['id'], 'status' => 4])//status==1 待处理
  118. ->update([
  119. 'status' => 2, //status==2 处理完成
  120. 'down_url' => $url,
  121. 'updatetime' => date('Y-m-d H:i:s')
  122. ]);
  123. $end = date("Y-m-d H:i:s");
  124. $output->writeln("[$date]【{$param['name']}】[{$param['apply_name']}]预约记录处理完成 end:【{$end}】");
  125. }
  126. }catch (\Exception $e){
  127. Db::name('exec_log')
  128. ->where(['id' => $info['id'], 'status' =>4])//status==1 待处理
  129. ->update([
  130. 'status' => 3, //status==2 处理失败
  131. 'down_url' => '',
  132. "remark"=>$e->getMessage(),
  133. 'updatetime' => date('Y-m-d H:i:s')
  134. ]);
  135. $output->writeln("[$date]【{$info['name']}】[{$info['apply_name']}]预约记录处理失败 message:【{$e->getMessage()}|{$e->getFile()}|{$e->getCode()}】");
  136. }
  137. Cache::store('redis')->handler()->decrby('ExecCommand',1);
  138. Db::commit();
  139. }
  140. //销售发票申请信息导出
  141. private function A($param=[])
  142. {
  143. // $param = $this->request->only(['start' => '', 'end' => '', 'status' => '', 'inv_type' => '', 'inv_out' => '', 'invNo' => '', 'relaComNo' => '', 'inv_number' => '', 'inv_code' => '', 'inv_company' => '', 'buyer_name' => '', 'apply_id' => '', 'apply_name' => '',], 'post', 'trim');
  144. $where = [['a.is_del', '=', 0], ['e.status', 'in', [1, 2]]];
  145. if (($param['start'] != '') && ($param['end'] != '')) $where[] = ['a.addtime', 'between', [$param['start'], $param['end']]];
  146. if (($param['companyNo'] != '') && ($param['companyNo'] != '')) $where[] = ["a.inv_out","=",$param['companyNo']];
  147. $open_type = [1 => '金税开票', 2 => '金税线下', 3 => '纯线下'];//开票方式
  148. $inv_type = config('invoiceType.invoiceName');//开票种类(发票类型)
  149. $list = Db::name('invoice_pool')
  150. ->alias('a')
  151. ->field('a.inv_out 卖方公司编码,
  152. a.inv_company 卖方公司名称,
  153. a.invNo 申请编号,
  154. f.department 申请部门,
  155. a.apply_name 申请人,
  156. b.buyer_title 发票买方公司名称,
  157. concat(b.buyer_code," ") 发票买方企业纳税识别号,
  158. b.buyer_addr 发票买方企业地址,
  159. b.buyer_mobile 发票买方企业联系方式,
  160. b.buyer_bank 发票买方企业收款银行,
  161. concat(b.buyer_bankNo," ") 发票买方企业财务账户,
  162. d.orderCode 公司订单号,
  163. b.buyer_title 订单企业客户,
  164. d.goodName 订单商品名,
  165. concat(g.inv_cat_code," ") 税收分类编码,
  166. g.inv_cat_name 税收分类名称,
  167. d.goodName 发票明细商品名称,
  168. CONCAT(d.catName,"*",d.goodName) 发票货物或应税劳务、服务名称,
  169. d.unitName 单位,
  170. d.goodNum 数量,
  171. d.goodPrice 单价,
  172. d.tax 税率,
  173. d.totalTax 总价,
  174. a.inv_type 开票种类,
  175. a.open_type 开票方式,
  176. a.exam_remark 申请备注,
  177. a.remark 发票备注,
  178. if(f.cxCode="",f.sequenceNo,f.cxCode) 销售主单号,
  179. f.goodPrice 商品单价')
  180. ->leftJoin('invoice_pool_info b', 'a.invNo=b.invNo')
  181. ->leftJoin('invoice_ticket c', 'a.invNo=c.invNo AND c.type=0 AND c.status=1')
  182. ->leftJoin('invoice_good d', 'd.invNo=a.invNo')
  183. ->leftJoin('assoc e', 'e.viceCode=d.invNo AND e.orderCode=d.orderCode AND e.is_del=0')
  184. ->leftJoin('qrd_info f', 'f.sequenceNo=e.orderCode')
  185. ->leftJoin('good g', 'g.spuCode=d.goodNo')
  186. ->where($where)
  187. ->order('a.addtime desc')
  188. ->cursor();
  189. foreach ($list as $value){
  190. $value["开票方式"] = $open_type[$value["开票方式"]] ?? '';
  191. $value["开票种类"] = $inv_type[$value["开票种类"]] ?? '';
  192. yield $value;
  193. }
  194. }
  195. //进项发票登记导出
  196. private function B($param=[])
  197. {
  198. // $param = $this->request->only(['start' => '', 'end' => '', 'invType' => '', 'company' => '', 'relaComNo' => '', 'supplierNo' => '', 'hpNo' => '', 'payNo' => '', 'invoiceType' => '', 'status' => '', 'invoiceNumber' => '', 'checkApi' => '', 'invoiceCode' => '', 'open_start' => '', 'open_end' => '', 'apply_id' => '', 'apply_name' => ''], 'post', 'trim');
  199. $where = [['a.is_del', '=', 0], ['b.is_del', '=', 0]];
  200. if (($param['start'] != '') && ($param['end'] != '')) $where[] = ['a.addtime', 'between', [$param['start'], $param['end']]];
  201. if (($param['companyNo'] != '') && ($param['companyNo'] != '')) $where[] = ["b.companyNo","=",$param['companyNo']];
  202. $status = [1 => '待系统验证', 2 => '买方公司审核', 3 => '待买方公司认证', 4 => '认证成功', 5 => '验证失败', 6 => '买方审核驳回', 7 => '认证失败', 8 => '回票流程终止', 9 => '验证超次数', 10 => '回票已退'];//认证状态
  203. $invoiceType = config('invoiceType.invoiceName');//开票种类(发票类型)
  204. $list = Db::name('pay_invoice')
  205. ->alias('a')
  206. ->leftJoin('pay b', 'a.payNo=b.payNo and b.status=2')
  207. ->leftJoin('invoice_info c', 'c.hpNo=a.hpNo AND c.status=1')
  208. ->field('"" 序号,
  209. a.payNo 对账编号,
  210. a.hpNo 回票申请编号,
  211. DATE_FORMAT(a.addtime,\'%Y\') 年,
  212. DATE_FORMAT(a.addtime,\'%m\') 月,
  213. a.invoiceNumber 发票号,
  214. c.total 总额,
  215. c.subtotal_amount 金额,
  216. c.subtotal_tax 税额,
  217. a.open_time 开票日期,
  218. b.supplierName 供应商,
  219. a.invoiceType 发票类型,
  220. "" as 业务类型,
  221. a.status 认证状态,
  222. a.updatetime 认证时间,
  223. a.remark 备注')
  224. ->where($where)
  225. ->order('a.addtime desc')
  226. ->cursor();
  227. $i=1;
  228. foreach ($list as $item) {
  229. $item['序号'] = $i;
  230. $item['业务类型'] = '采购回票';
  231. $item['认证状态'] = $status[$item['认证状态']]??"";
  232. $item['发票类型'] = $invoiceType[$item['发票类型']]??"";
  233. $i++;
  234. yield $item;
  235. }
  236. }
  237. //资金认领导出
  238. private function C($param=[])
  239. {
  240. // $param = $this->request->only(['start' => '', 'end' => '', 'name' => '', 'bank' => '', 'status' => '', 'tradNo' => '', 'company' => '', 'userd_lower' => '', 'used_upper' => ''], 'post', 'trim');
  241. $where = [['a.is_del', '=', 0]];
  242. if (($param['start'] != '') && ($param['end'] != '')) $where[] = ['b.addtime', 'between', [$param['start'],$param['end']]];
  243. if (($param['companyNo'] != '') && ($param['companyNo'] != '')) $where[] = ["a.companyNo","=",$param['companyNo']];
  244. $status = [1 => '未认领', 2 => '部分认领', 3 => '全部认领'];//状态
  245. $b_status = [1 => '待审批', 2 => '审批通过', 3 => '审批驳回', 4 => '退款', 5 => '解除认领'];//认领审批状态
  246. $list = Db::name('trade')
  247. ->alias('a')
  248. ->field('
  249. a.companyNo 收款方公司编码,
  250. a.trade_in 收款单位名称,
  251. a.trade_account 付款银行单位账号,
  252. a.trade_out 付款银行单位名称,
  253. a.trade_used 付款备注,
  254. a.trade_time 交易时间,
  255. a.tradNo 资金编号,
  256. a.status 状态,
  257. a.total_fee 收入金额,
  258. a.balance 未认领金额,
  259. b.total_fee 已认领金额,
  260. b.logNo 资金认领编号,
  261. b.customerNo 认领企业,
  262. c.orderCode 订单编号,
  263. d.cxCode 销售主单号,
  264. d.poCode 平台编号,
  265. d.goodName 产品名称,
  266. d.ownerName 资金创建人,
  267. c.apply_name 认领创建人,
  268. b.status 认领审批状态,
  269. b.remark 驳回原因')
  270. ->leftJoin('trade_pool b', 'b.tradNo=a.tradNo and b.is_del=0')
  271. ->leftJoin('assoc c', 'c.viceCode=b.logNo and c.type=2 and c.status<>3')
  272. ->leftJoin('qrd_info d', 'd.sequenceNo=c.orderCode')
  273. ->where($where)
  274. ->order('a.trade_time desc')
  275. ->cursor();
  276. foreach ($list as $value){
  277. $value['状态'] = $status[$value['状态']] ?? '';
  278. $value['认领审批状态'] = $b_status[$value['认领审批状态']] ?? '';
  279. $value["付款银行单位账号"] .=' ';
  280. $value["认领企业"] =Db::name("customer_info")->where(["companyNo"=>$value["认领企业"]])->value("companyName","");
  281. yield $value;
  282. }
  283. }
  284. //回款核销明细表
  285. private function D($param=[])
  286. {
  287. // $param = $this->request->only(['company' => '', 'start' => '', 'end' => ''], 'post', 'trim');
  288. $where = [['a.is_del', '=', 0],['b.status',">=",2],["b.type","=",2]];
  289. if (($param['start'] != '') && ($param['end'] != '')) $where[] = ['b.addtime', 'between', [$param['start'], $param['end']]];
  290. if ($param['companyNo'] != '') $where[] = ['a.companyNo', '=', $param['companyNo']];
  291. $data = Db::name('assoc')
  292. ->alias('b')
  293. ->field('DATE_FORMAT(b.addtime,\'%Y\') 年,DATE_FORMAT(b.addtime,\'%m\') 月,DATE_FORMAT(b.addtime,\'%d\') 日,a.companyName 业务公司名称,a.companyNo 业务公司编号,b.viceCode 资金认领编号,c.tradNo 资金编号,c.total_fee 本次核销金额,c.addtime 本次核销时间,c.status 资金认领状态,a.sequenceNo 确认单编号,a.department 业务员部门,a.ownerName 业务员,a.platName 平台类型,a.qrdSource 确认单类型,a.qrdType 商品类型,a.poCode 平台订单号,a.customerAttr 客户属性,a.branch 分公司,a.customerName 客户名称,a.firstCat 一级分类,a.goodName 产品名称,round(a.tax/100,2) 税点,(a.goodNum-a.thNum) 下单数量,a.goodPrice 销售单价,(a.totalPrice-a.th_fee) 销售总额')
  294. ->leftJoin('qrd_info a', 'b.orderCode=a.sequenceNo')
  295. ->leftJoin('trade_pool c', 'c.logNo=b.viceCode')
  296. ->where($where)
  297. ->order(['a.id' => 'desc'])
  298. ->cursor();
  299. //资金认领状态
  300. $status = [1 => '待审批', 2 => '审批通过', 3 => '审批驳回', 4 => '已解除认领', 5 => '已取消认领'];
  301. // $list = [];
  302. foreach ($data as $value) {
  303. $value['资金认领状态'] = $status[$value['资金认领状态']] ?? '';
  304. $value['确认单类型'] = $this->qrdSource[$value['确认单类型']] ?? '';
  305. $value['商品类型'] = $this->qrdType[$value['商品类型']] ?? '';
  306. yield $value;
  307. }
  308. }
  309. //回票明细表
  310. private function E($param=[])
  311. {
  312. // $param = $this->request->only(['company' => '', 'start' => '', 'end' => ''], 'post', 'trim');
  313. $where = [['a.is_del', '=', 0],['a.status', 'in',[3,4]],['b.is_del', '=', 0], ['c.is_del', '=', 0], ['d.is_del', '=', 0]];
  314. if (($param['start'] != '') && ($param['end'] != '')) $where[] = ['a.check_time', 'between', [$param['start'], $param['end']]];
  315. if ($param['companyNo'] != '') $where[] = ['b.companyNo', '=', $param['companyNo']];
  316. $data = Db::name('pay_invoice')
  317. ->alias('a')
  318. ->field('d.cgdTime 采购单下单日期,
  319. b.companyName 业务公司名称,
  320. b.companyNo 业务公司编号,
  321. a.hpNo 对账回票编号,
  322. a.payNo 对账单号,
  323. c.cgdNo 采购单编号,
  324. a.check_time 本次回票时间,
  325. a.invoiceNumber 发票号,
  326. a.inv_amount 本次回票金额,
  327. d.totalPrice 采购单回票金额,
  328. "" 不含税采购成本,
  329. "" 税额,
  330. d.companyName 公司回票抬头,
  331. d.goodNo 商品编号,
  332. d.firstCat 一级分类,
  333. d.fundCode 核算码,
  334. d.goodName 商品名称,
  335. d.goodNum 商品数量,
  336. d.goodUnit 单位,
  337. d.cgdSource 采购单类型,
  338. d.ownerName 采购员,
  339. d.supplierName 供应商名称,
  340. d.supplierNo 供应商编号,
  341. d.goodType 商品类型,
  342. round(d.tax/100,2) 税率,
  343. d.packPrice 包装费,
  344. d.certPrice 证书费,
  345. d.markPrice 加标费,
  346. d.openPrice 开模费,
  347. d.costPrice 成本工艺费,
  348. d.deliveryPrice 物流费,
  349. d.barePrice 成本单价,
  350. d.diff_weight 工差,
  351. d.diff_fee 工差金额,
  352. d.goodPrice 单价,
  353. d.totalPrice 采购总货款,
  354. d.qrdCode 确认单编号,
  355. d.bkCode 备库编号,
  356. d.thNum 退货数量,
  357. d.th_fee 退货金额,
  358. a.exam_remark 回票审核备注,
  359. a.remark 回票申请备注')
  360. ->leftJoin('pay b', 'a.payNo=b.payNo')
  361. ->leftJoin('pay_info c', 'c.payNo=a.payNo and c.status=1 and c.is_del=0')
  362. ->leftJoin('cgd_info d', 'd.sequenceNo=c.cgdNo')
  363. ->where($where)
  364. ->order(['a.id' => 'desc'])
  365. ->cursor();
  366. //采购单类型
  367. // $cgdSource = [1 => '直接下单', 2 => '咨询', 3 => '项目', 4 => '平台', 5 => '有赞'];
  368. //商品类型
  369. $goodType = [1 => '正常商品', 2 => '赠品', 3 => '样品'];
  370. // $list = [];
  371. foreach ($data as $value) {
  372. $value['采购单类型'] = $this->cgdSource[$value['采购单类型']] ?? '';
  373. $value['商品类型'] = $goodType[$value['商品类型']] ?? '';
  374. $value['不含税采购成本'] = round($value['采购总货款']/(1+$value['税率']),2) ;
  375. $value['税额'] = round($value['不含税采购成本']*$value['税率'],2) ;
  376. yield $value;
  377. }
  378. }
  379. //经营分析报表
  380. private function F($param=[])
  381. {
  382. // $param = $this->request->only(['company' => '', 'start' => '', 'end' => ''], 'post', 'trim');
  383. $where = [['a.is_del', '=', 0]];
  384. if (($param['start'] != '') && ($param['end'] != '')) $where[] = ['a.createdTime', 'between', [$param['start'], $param['end']]];
  385. if ($param['companyNo'] != '') $where[] = ['a.companyNo', '=', $param['companyNo']];
  386. $data = Db::name('qrd_info')
  387. ->alias('a')
  388. ->field('
  389. DATE_FORMAT(a.createdTime,\'%Y\') 年,
  390. DATE_FORMAT(a.createdTime,\'%m\') 月,
  391. DATE_FORMAT(a.createdTime,\'%d\') 日,
  392. a.companyName 业务公司名称,
  393. a.companyNo 业务公司编号,
  394. a.department 销售部门,
  395. a.ownerName 销售员,
  396. a.qrdSource 订单来源,
  397. a.pay_source 支付来源,
  398. "" 活动类型,
  399. a.sequenceNo 确认单编号,
  400. if(a.cxCode="",a.sequenceNo,a.cxCode) 销售订单号,
  401. b.bkCode 备库单编号,
  402. b.sequenceNo 采购单单号,
  403. a.platName 平台类型,
  404. a.poCode 平台订单号,
  405. a.customerAttr 客户属性,
  406. a.branch 分公司,
  407. a.customerName 客户名称,
  408. a.goodNo 商品编码,
  409. a.firstCat 一级分类,
  410. a.secCat 二级分类,
  411. a.thirdCat 三级分类,
  412. a.goodName 产品名称,
  413. a.goodUnit 单位,
  414. a.goodNum 下单数量,
  415. a.goodPrice 销售单价,
  416. (a.totalPrice+a.th_fee) 销售总额,
  417. round(a.tax/100,2) 税点,
  418. b.ownerName 采购员,
  419. b.goodPrice 采购裸价,
  420. b.markPrice 加标费,
  421. b.packPrice 包装费,
  422. b.certPrice 证书费,
  423. b.openPrice 开模费,
  424. b.costPrice 工艺费,
  425. b.deliveryPrice 物流费,
  426. b.goodPrice 采购单价合计,
  427. (ifnull(b.totalPrice,a.total_origin_price)+ifnull(b.th_fee,0)) 采购总货款,
  428. (a.totalPrice -ifnull(b.totalPrice,a.total_origin_price)) 毛利润,
  429. round((a.totalPrice -ifnull(b.totalPrice,a.total_origin_price))/a.totalPrice,2) 毛利率,
  430. round(b.tax/100,2) 采购税点,
  431. round(b.totalPrice/(1+ round(b.tax/100,2)),2) 不含税采购成本,
  432. a.qrdType 库存性质,
  433. b.supplierName 供应商名称,
  434. b.sendType 发货方式,
  435. a.thNum 退货数量,
  436. a.fundCode 财务核算编码')
  437. ->leftJoin('cgd_info b', 'b.sequenceNo=a.cgdNo and b.is_del=0')
  438. ->where($where)
  439. ->order(['a.id' => 'desc'])
  440. ->cursor();
  441. $list = [];
  442. foreach ($data as $value) {
  443. $value['订单来源'] = $this->qrdSource[$value['订单来源']] ?? '';
  444. $value['库存性质'] = $this->qrdType[$value['库存性质']] ?? '';
  445. $value['发货方式'] = $this->sendType[$value['发货方式']] ?? '';
  446. $value['毛利润'] = round(bcsub($value['销售总额'], $value['采购总货款'], 3), 2);
  447. $value['毛利率'] = $value['销售总额'] == 0 ? 0 : round(bcmul(bcsub(1, bcdiv($value['采购总货款'], $value['销售总额'], 5),5), 100,2), 2) . '%';
  448. $value['不含税采购成本'] = round(bcdiv($value['采购总货款'], bcadd(1,$value['采购税点'],3), 3), 2);
  449. yield $value;
  450. }
  451. }
  452. //收入成本明细表
  453. private function G($param=[])
  454. {
  455. // $param = $this->request->only(['company' => '', 'start' => '', 'end' => ''], 'post', 'trim');
  456. $where = [['a.is_del', '=', 0], ['a.status', '>=', 4]];
  457. if (($param['start'] != '') && ($param['end'] != '')) $where[] = ['c.open_date', 'between', [$param['start'],$param['end']]];
  458. if ($param['companyNo'] != '') $where[] = ['a.inv_out', '=', $param['companyNo']];
  459. $data = Db::name('invoice_pool')
  460. ->alias('a')
  461. ->field('
  462. a.invNo 发票申请编号,
  463. m.orderCode 确认单编号,
  464. if(e.cxCode="",m.orderCode,e.cxCode) 销售主单编号,
  465. e.qrdSource 销售来源,
  466. b.seller_title 销售方抬头,
  467. e.department 业务部门,
  468. e.ownerName 业务人员,
  469. m.goodNum 本次开票数量,
  470. m.totalTax 本次开票金额,
  471. e.goodPrice 销售单价,
  472. c.open_date 本次开票时间,
  473. c.inv_number 发票号,
  474. m.tax 开票税点,
  475. a.status 开票状态,
  476. e.customerName 客户名称,
  477. e.fundCode 财务核算码,
  478. e.firstCat 一级分类,
  479. e.secCat 二级分类,
  480. e.thirdCat 三级分类,
  481. e.goodNo 产品编号,
  482. e.goodName 产品名称,
  483. f.sequenceNo 采购单编号,
  484. f.supplierName 卖出方公司,
  485. f.sendType 发货方式,
  486. e.qrdType 商品类型,
  487. f.ownerName 采购员,
  488. ifnull(f.goodNum,e.goodNum) 采购下单数量,
  489. f.barePrice 采购裸价,
  490. f.markPrice 加标费,
  491. f.packPrice 包装费,
  492. f.certPrice 证书费,
  493. f.openPrice 开模费,
  494. f.costPrice 工艺费,
  495. f.deliveryPrice 物流费,
  496. f.goodPrice 成本合计,
  497. ifnull(f.totalPrice,e.total_origin_price) 采购货款,
  498. round(f.tax/100,2) 采购单税点,
  499. g.returnCode 退票编号,
  500. if(g.returnCode<>"",a.inv_value,"") 退票金额,
  501. g.status 退票状态,
  502. g.return_type 退票方式,
  503. f.thNum 退货数量,
  504. f.th_fee 退货金额
  505. ')
  506. ->leftJoin('invoice_pool_info b', 'b.invNo=a.invNo')
  507. ->leftJoin('invoice_ticket c', 'c.invNo=a.invNo and c.type=0 and c.status in (0,1,2,3)')
  508. ->leftJoin("invoice_good m","m.invNo=a.invNo and m.invtype=0 and m.is_del=0")
  509. // ->leftJoin('assoc d', 'd.viceCode=a.invNo and d.type=1')
  510. ->leftJoin('qrd_info e', 'e.sequenceNo=m.orderCode')
  511. ->leftJoin('cgd_info f', 'f.sequenceNo=e.cgdNo')
  512. ->leftJoin('invoice_return g', 'g.invNo=a.invNo and g.status=2')
  513. ->where($where)
  514. ->order(['a.id' => 'desc'])
  515. ->cursor();
  516. $list = [];
  517. foreach ($data as $value) {
  518. $value['开票状态'] = $this->invoice_pool_status[$value['开票状态']] ?? '';
  519. $value['发货方式'] = $this->sendType[$value['发货方式']] ?? '';
  520. $value['商品类型'] = $this->qrdType[$value['商品类型']] ?? '';
  521. $value['退票状态'] = $this->invoice_return_status[$value['退票状态']] ?? '';
  522. $value['销售来源'] = $this->qrdSource[$value['销售来源']] ?? '';
  523. yield $value;
  524. }
  525. }
  526. //应收台账表
  527. private function I($param=[])
  528. {
  529. // $param = $this->request->only(['company' => '', 'start' => '', 'end' => ''], 'post', 'trim');
  530. $where = [['a.is_del', '=', 0]];
  531. if (($param['start'] != '') && ($param['end'] != '')) $where[] = ['a.createdTime', 'between', [$param['start'], $param['end']]];
  532. if ($param['companyNo'] != '') $where[] = ['a.companyNo', '=', $param['companyNo']];
  533. $data = Db::name('qrd_info')
  534. ->alias('a')
  535. ->field('
  536. a.createdTime 下单时间,
  537. a.companyName 业务公司名称,
  538. a.companyNo 业务公司编号,
  539. a.sequenceNo 业务订单编号,
  540. if(a.cxCode="",a.sequenceNo,a.cxCode) 销售订单主编号,
  541. a.department 销售部门,
  542. a.ownerName 销售员,
  543. a.qrdSource 订单来源,
  544. a.platName 平台类型,
  545. a.poCode 平台订单号,
  546. a.customerName 客户名称,
  547. a.customerAttr 客户属性,
  548. a.branch 客户分公司,
  549. a.firstCat 一级分类,
  550. a.goodName 产品名称,
  551. round(a.tax/100,2) 税点,
  552. (a.goodNum - a.thNum) 下单数量,
  553. a.goodPrice 销售单价,
  554. a.totalPrice 销售总额,
  555. a.th_fee 退货总额,
  556. a.pay_status 回款状态,
  557. a.apay_fee 总已回款,
  558. a.pay_fee 回款中,
  559. a.wpay_fee 总未回款,
  560. a.pay_tag_fee 回款标签金额,
  561. a.inv_status 开票状态,
  562. a.ainv_fee 总已开票,
  563. a.inv_fee 开票中,
  564. a.winv_fee 总未开票,
  565. a.inv_tag_fee 开票标签金额,
  566. a.invtime 最近开票时间,
  567. "" 业务提票时间,
  568. a.paytime 最近回款时间,
  569. 0 账期,
  570. 0 比率,
  571. "" 订单是否超期,
  572. "" 开票是否超期,
  573. a.sendStatus 发货状态,
  574. "" 最近一次发货时间
  575. ')
  576. ->where($where)
  577. ->order(['a.id' => 'desc'])
  578. ->cursor();
  579. // echo Db::name('qrd_info')->getLastSql();die;
  580. $time = time();
  581. foreach ($data as $value) {
  582. // $value['订单来源'] = $this->qrdSource[$value['订单来源']] ?? '';
  583. // $value['回款状态'] = $this->pay_status[$value['回款状态']] ?? '';
  584. // $value['开票状态'] = $this->inv_status[$value['开票状态']] ?? '';
  585. // $value['发货状态'] = $this->sendStatus[$value['发货状态']] ?? '';
  586. $value['业务提票时间'] =Db::name("assoc")->where(["type"=>1,"status"=>[1,2],"is_del"=>0,'orderCode'=>$value['业务订单编号']])
  587. ->order("id desc")->value('addtime','');
  588. //计算账期
  589. $value['账期'] = bcdiv(bcsub($time,strtotime($value['最近回款时间']==''?$value['下单时间']:$value['最近回款时间']), 3), 86400, 0);
  590. $value['比率'] = round(bcdiv($value['账期'], 30, 4),3);
  591. $value['订单是否超期'] = $value['比率'] > 6 ? '是' : '否';
  592. $value['开票是否超期'] = round(bcdiv(bcsub($time, strtotime($value['最近开票时间']==''?$value['下单时间']:$value['最近开票时间']), 1), 86400, 1)
  593. ) >
  594. 15 ? '是' : '否';
  595. $value['最近一次发货时间'] = Db::table('fh_source')
  596. ->whereIn('qrdNo', [$value['业务订单编号'], $value['销售订单主编号']])
  597. ->order(['id' => 'desc'])
  598. ->value('send_date', '');
  599. $value['销售回款'] = Db::name("tag_log")->alias("a")->leftJoin("order_tag b","a.tag_id=b.id")
  600. ->where(["a.code"=>$value['业务订单编号'],"a.status"=>1,"b.type"=>3])->value("b.tag_name",'');
  601. $value['销售开票'] = Db::name("tag_log")->alias("a")->leftJoin("order_tag b","a.tag_id=b.id")
  602. ->where(["a.code"=>$value['业务订单编号'],"a.status"=>1,"b.type"=>4])->value("b.tag_name",'');
  603. if($value['订单来源']==10)$value['最近一次发货时间']=$value['下单时间'];
  604. $value['订单来源'] = $this->qrdSource[$value['订单来源']] ?? '';
  605. $value['回款状态'] = $this->pay_status[$value['回款状态']] ?? '';
  606. $value['开票状态'] = $this->inv_status[$value['开票状态']] ?? '';
  607. $value['发货状态'] = $this->sendStatus[$value['发货状态']] ?? '';
  608. yield $value;
  609. }
  610. }
  611. //用友销票表
  612. private function J($param=[])
  613. {
  614. // $param = $this->request->only(['company' => '', 'start' => '', 'end' => ''], 'post', 'trim');
  615. $where = [['a.is_del', '=', 0],["a.status","in",[3,4]], ['e.is_del','=', 0]];
  616. if (($param['start'] != '') && ($param['end'] != '')) $where[] = ['a.check_time', 'between', [$param['start'], $param['end']]];
  617. if ($param['companyNo'] != '') $where[] = ['e.companyNo', '=', $param['companyNo']];
  618. $data = Db::name('pay_invoice')
  619. ->alias('a')
  620. ->field('
  621. e.companyName 回票业务公司名称,
  622. a.hpNo 回票编码,
  623. e.supplierName 供应商名称,
  624. a.invoiceNumber 发票号,
  625. b.item_list,
  626. "" 发票货物或应税劳务、服务名称,
  627. "" 回票数量,
  628. "" 本次回票价款,
  629. "" 本次回票税额,
  630. "" 回票税率,
  631. b.total 本次回票总金额,
  632. a.check_time 本次回票时间
  633. ')
  634. ->leftJoin('invoice_info b', 'b.hpNo=a.hpNo and b.status=1')
  635. ->leftJoin('pay e', 'e.payNo=a.payNo')
  636. ->where($where)
  637. ->order(['a.id' => 'desc'])
  638. ->cursor();
  639. $list = [];
  640. foreach ($data as $value) {
  641. $item_list = json_decode($value['item_list'], true);
  642. unset($value['item_list']);
  643. foreach ($item_list as $item) {
  644. yield $list[] = array_merge($value, [
  645. '发票货物或应税劳务、服务名称' => $item['name'] ?? '',
  646. '回票数量' => $item['quantity'] ?? '',
  647. '本次回票价款' => $item['amount'] ?? '',
  648. '本次回票税额' => $item['tax'] ?? '',
  649. '回票税率' => $item['tax_rate'] ?? '',
  650. ]);
  651. }
  652. }
  653. // return $list;
  654. }
  655. /** 采购单管理数据
  656. * @throws \PHPExcel_Exception
  657. * @throws \PHPExcel_Reader_Exception
  658. * @throws \PHPExcel_Writer_Exception
  659. * @throws \think\db\exception\DataNotFoundException
  660. * @throws \think\db\exception\DbException
  661. * @throws \think\db\exception\ModelNotFoundException
  662. */
  663. private function K($param=[])
  664. {
  665. // $param = $this->request->only(['companyNo' => '', 'start' => '', 'end' => '', "supplierNo" => ''], 'post', 'trim');
  666. $where = [];
  667. if (($param['start'] != '') && ($param['end'] != '')) $where[] = ['createdTime', 'between', [$param['start'], $param['end']]];
  668. if ($param['companyNo'] != '') $where[] = ['companyNo', '=', $param['companyNo']];
  669. $data = Db::name("cgd_info")->where($where)
  670. ->field("
  671. sequenceNo '采购单编号',
  672. companyName '业务企业',
  673. supplierName '供应商名称',
  674. ownerName '采购员',
  675. department '采购部门',
  676. createdTime '采购时间',
  677. cgdType '采购单类型',
  678. if(qrdCode='',bkCode,qrdCode) '确认单编号/备库单编号',
  679. if(cxCode='',if(qrdCode='',bkCode,qrdCode),cxCode) '销售单主单号',
  680. goodNo '商品编号',
  681. goodName '商品名称',
  682. firstCat '一级分类',
  683. secCat '二级分类',
  684. thirdCat '三级分类',
  685. fundCode '核算码',
  686. round(tax/100,2) '税率',
  687. barePrice '裸价',
  688. markPrice '加标费',
  689. certPrice '证书费',
  690. openPrice '开模费',
  691. packPrice '包装费',
  692. costPrice '工艺费',
  693. deliveryPrice '物流费',
  694. goodPrice '单价',
  695. isStock '是否库存',
  696. diff_fee '工差金额',
  697. goodNum '商品数量',
  698. totalPrice '总价',
  699. sendStatus '发货状态',
  700. '' as '对账编号',
  701. '' as '付款状态',
  702. '' as '回票状态'
  703. ")->cursor();
  704. $list = [];
  705. foreach ($data as $value) {
  706. $value['采购单类型'] = $this->cgdType[$value['采购单类型']];
  707. $payinfo = Db::name("pay_info")->alias("a")
  708. ->leftJoin("pay b", "a.payNo=b.payNo and b.status=2")
  709. ->where(["a.status" => 1, "a.is_del" => 0,'cgdNo'=>$value['采购单编号']])
  710. ->field("b.payNo,b.inv_status,b.pay_status")->find();
  711. $value['对账编号'] = $payinfo['payNo'] ?? "";
  712. $value['付款状态'] = $this->cgd_pay_status[$payinfo['pay_status'] ?? "1"];
  713. $value['回票状态'] = $this->cgd_inv_status[$payinfo['inv_status'] ?? "1"];
  714. $value['是否库存'] = $value['是否库存']==1?'是':'否';
  715. $value['发货状态'] = $this->sendStatus[$value['发货状态']];
  716. yield $value;
  717. }
  718. }
  719. /**
  720. * 对账单汇总表
  721. */
  722. private function M($param=[])
  723. {
  724. $where = [["is_del", "=", 0], ["status", "=", 2]];
  725. if (($param['start'] != '') && ($param['end'] != '')) $where[] = ['addtime', 'between', [$param['start'], $param['end']]];
  726. if ($param['companyNo'] != '') $where[] = ['companyNo', '=', $param['companyNo']];
  727. $data = Db::name("pay")->where($where)
  728. ->field("
  729. companyName '业务公司',
  730. apply_name '申请人',
  731. addtime '申请时间',
  732. supplierName '供应商名称',
  733. '' as '付款时间',
  734. payNo '对账编号',
  735. total_fee '对账总额',
  736. apay_fee '已付款',
  737. pay_fee '付款中',
  738. wpay_fee '未付款',
  739. pay_status '付款状态',
  740. pay_tag_fee '付款加签金额',
  741. pay_tag as '付款加签时间',
  742. '' as '付款标签名称',
  743. ainv_fee '已回票',
  744. inv_fee '回票中',
  745. winv_fee '未回票',
  746. inv_status '回票状态',
  747. inv_tag_fee '回票加签金额',
  748. inv_tag as '回票加签时间',
  749. '' as '回票标签名称',
  750. remark '备注'
  751. ")->cursor();
  752. foreach ($data as $value) {
  753. $tag = Db::name("tag_log")->alias("a")->leftJoin("order_tag b", "a.tag_id=b.id")->where(["a.code" =>
  754. $value['对账编号'], "a.status" => 1])->column("a.addtime,b.tag_name", "b.type");
  755. $value['付款加签时间'] = $tag[1]['addtime'] ?? "";
  756. $value['回票加签时间'] = $tag[2]['addtime'] ?? "";
  757. $value['回票标签名称'] = $tag[2]['tag_name'] ?? "";
  758. $value['付款标签名称'] = $tag[1]['tag_name']?? "";
  759. $value['付款状态'] = $this->pay_status[$value['付款状态']];
  760. $value['回票状态'] = $this->inv_status[$value['回票状态']];
  761. $value['付款时间'] = Db::name("pay_payment")->where(['payNo'=>$value['对账编号'],"is_del"=>0])->order("id desc")
  762. ->value('return_time','');
  763. yield $value;
  764. }
  765. }
  766. //退货台账
  767. private function N($param=[]){
  768. $where = [];
  769. if (($param['start'] != '') && ($param['end'] != '')) $where[] = ['a.createtime', 'between', [$param['start'],$param['end']]];
  770. if ($param['companyNo'] != '') $where[] = ['b.companyNo', '=', $param['companyNo']];
  771. $list = Db::name("th_source")
  772. ->alias("a")
  773. ->leftJoin("qrd_info b","a.th_qrdNo=b.sequenceNo or a.th_qrdNo=b.cxCode")
  774. ->leftJoin("cgd_info c","b.cgdNo=c.sequenceNo")
  775. ->where($where)
  776. ->field("
  777. DATE_FORMAT( a.createtime, '%Y' ) 年,
  778. DATE_FORMAT( a.createtime, '%m' ) 月,
  779. DATE_FORMAT( a.createtime, '%d' ) 日,
  780. b.companyName 退货公司名称,
  781. b.companyNo 退货公司编号,
  782. b.department 销售部门,
  783. b.ownerName 销售员,
  784. b.qrdSource 销售来源,
  785. b.pay_source 支付渠道,
  786. '' 活动类型,
  787. b.sequenceNo 订单编号,
  788. if(b.cxCode='',b.sequenceNo,b.cxCode) 销售订单编号,
  789. a.thNo 退货单号,
  790. b.cgdNo 采购单号,
  791. b.platName 平台类型,
  792. b.poCode 平台订单号,
  793. b.customerAttr 客户属性,
  794. b.area 分公司,
  795. b.customerName 客户名称,
  796. b.goodNo 商品编号,
  797. b.firstCat 一级分类,
  798. b.secCat 二级分类,
  799. b.thirdCat 三级分类,
  800. b.goodName 商品名称,
  801. b.goodUnit 商品单位,
  802. a.th_num 退货数量,
  803. b.goodPrice 销售单价,
  804. a.th_qrd_fee 退货金额,
  805. ifnull(b.tax,0)/100 税点,
  806. c.ownerName 采购员,
  807. c.barePrice 采购裸价,
  808. c.markPrice 加标费,
  809. c.packPrice 包装费,
  810. c.certPrice 证书费,
  811. c.openPrice 开模费,
  812. c.costPrice 工艺费,
  813. c.deliveryPrice 物流费,
  814. c.goodPrice 采购单价,
  815. round(a.th_num * ifnull(c.goodPrice,b.total_origin_price/b.goodNum),2) 退货采购货款,
  816. ifnull(c.tax,0)/100 采购税点,
  817. if(b.isStock=1,'是','否') 是否库存,
  818. c.supplierName 供应商名称,
  819. c.sendType 发货方式,
  820. if(a.is_th=1,'是','否') 供应商是否同意退货,
  821. '' as 付款方式,
  822. b.fundCode 财务核算码,
  823. a.th_remark 退货备注,
  824. c.bkCode 备库单号
  825. ")->cursor();
  826. foreach ($list as $value) {
  827. $value['销售来源']= $this->qrdSource[$value['销售来源']]??"";
  828. $value['发货方式']= $this->sendType[$value['发货方式']]??"";
  829. yield $value;
  830. }
  831. }
  832. //产品部门销售业绩
  833. private function Q($param=[]){
  834. $where = [["is_del","=",0],["status","in",[0,1,2]]];
  835. if (($param['start'] != '') && ($param['end'] != '')) $where[] = ['addtime', 'between', [date("Y-m-d 00:00:00",strtotime($param['start'])),date("Y-m-d 23:59:59",
  836. strtotime($param['end']))]];
  837. // if ($param['companyNo'] != '') $where[] = ['supplierNo', '=', $param['companyNo']];
  838. $Db= Db::connect("mysql_wsm");
  839. $list =$Db->name("sale")->field("orderCode 销售单号,apply_name 业务人员,apply_id 业务部门,cat_id 分类,total_price 产品货款总额,addtime 日期")
  840. ->where($where)
  841. ->cursor();
  842. foreach ($list as $value){
  843. $value['业务部门'] = $Db->name("depart_user")->alias("a")->leftjoin("company_item b","a.itemid=b.id")
  844. ->where(["a.is_del"=>0,"a.status"=>1,"a.uid"=>$value['业务部门']])->value("b.name",'');
  845. $value['分类'] = $Db->name("cat")->where(["id"=>$value['分类']])->value("search","");
  846. yield $value;
  847. }
  848. }
  849. //采购部门销售业绩
  850. private function R($param=[]){
  851. $where = [["a.is_del","=",0],["a.status","=",2],["b.is_stock","=",0]];
  852. if (($param['start'] != '') && ($param['end'] != '')) $where[] = ['a.sendtime', 'between', [date("Y-m-d 00:00:00",
  853. strtotime($param['start'])),date("Y-m-d 23:59:59",strtotime($param['end']))]];
  854. // if ($param['companyNo'] != '') $where[] = ['supplierNo', '=', $param['companyNo']];
  855. $Db= Db::connect("mysql_wsm");
  856. $list =$Db->name("order_out")->alias("a")
  857. ->leftjoin("sale b","a.orderCode=b.orderCode")
  858. ->leftjoin("order_send c","a.outCode=c.outCode")
  859. ->leftjoin("purchease_order d","c.cgdNo=d.cgdNo")
  860. ->field("
  861. a.outCode 发货单号,
  862. c.cgdNo 采购单号,
  863. a.orderCode 销售单号,
  864. b.platform_id 平台类型,
  865. d.cgder_id 业务部门,
  866. d.cgder 采购人员,
  867. b.order_source 咨询类型,
  868. a.sendtime 发货时间,
  869. b.cat_id `一级分类`,
  870. b.good_name 商品名称,
  871. a.post_name 物流公司,
  872. a.post_code 物流单号,
  873. a.send_num 本次发货数量,
  874. a.send_num*b.sale_price 本次发货金额,
  875. b.sale_price 销售单价,
  876. b.good_num-b.th_num 销售数量,
  877. b.total_price-b.th_fee 销售金额,
  878. d.supplier_name `供应商名称`
  879. ")
  880. ->where($where)
  881. ->cursor();
  882. foreach ($list as $value){
  883. $value['业务部门'] = $Db->name("depart_user")->alias("a")->leftjoin("company_item b","a.itemid=b.id")
  884. ->where(["a.is_del"=>0,"a.status"=>1,"a.uid"=>$value['业务部门']])->value("b.name",'');
  885. $value['一级分类'] = $Db->name("cat")->where(["id"=>$value['一级分类']])->value("search","");
  886. $value['平台类型'] = $Db->name("platform")->where(["id"=>$value['平台类型']])->value("platform_name","");
  887. $value['咨询类型']=$this->qrdSource[$value['咨询类型']]??"";
  888. $value['物流单号'].='';
  889. yield $value;
  890. }
  891. }
  892. }