SplitSale.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. namespace app\command;
  3. use app\admin\common\User;
  4. use app\admin\model\Platform;use app\model\ComonOrder;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. class SplitSale extends Command
  11. {
  12. private $i = 0;//各种编码的自增变量
  13. private $sale_insert = [];//wsm_sale_caixiao的新增数据
  14. private $cgd_insert = [];//wsm_cgd_caixiao的新增数据
  15. private $noble_metal = [1 => '18K', 2 => '24K', 3 => '白银'];//贵金属种类对应文本
  16. private $cgd_key = 0;//新增到wsm_cgd_caixiao的数组下标,从0开始
  17. private $order_source = 8;//支付渠道
  18. private $Tcode = '';
  19. private $comOrder=null;
  20. protected function configure()
  21. {
  22. $this->setName('split_sale')->setDescription('销售订单拆分');
  23. parent::configure();
  24. }
  25. protected function execute(Input $input, Output $output)
  26. {
  27. $key = 'split_sale_';
  28. $rs = Cache::store('redis')->inc($key);
  29. if ($rs!=1){
  30. Cache::store('redis')->dec($key);
  31. return true;
  32. }
  33. try {
  34. $this->comOrder=new ComonOrder();
  35. Db::startTrans();
  36. try {
  37. $data = Db::name('sale')
  38. ->alias('a')
  39. ->leftJoin('platform b', 'b.id=a.platform_id')
  40. ->leftJoin('good_proof c', 'c.id=a.proof_id')
  41. ->field('a.*,b.platform_name,b.use_type,c.proof_url')
  42. ->where([
  43. ['a.is_del', '=', 0],
  44. ['a.updatetime', '>=', date('Y-m-d H:i:s', time() - 30 * 60)],
  45. ['a.pay_id', '<>', 0]
  46. ])->cursor();
  47. $userCommon = User::getIns();
  48. $commonOrder=[];//处理采购销售同一数据
  49. $updatePrice=true;
  50. foreach ($data as $sale) {
  51. //处理采购销售同一数据
  52. $tempOrder=[
  53. "cxCode"=>$sale["orderCode"],
  54. "tyCode"=>makeNo("TY"),
  55. "orderCode"=>"",
  56. "cgdNo"=>"",
  57. "order_type"=>$sale['order_type'],
  58. "order_source"=>$sale['order_source'],
  59. "platform_type"=>Platform::where("id",$sale['platform_id'])->value("use_type",0),
  60. "goodNo"=>$sale['good_code'],
  61. "goodName"=>$sale['good_name'],
  62. "apply_id"=>$sale['apply_id'],
  63. "apply_name"=>$sale['apply_name'],
  64. "customerName"=>"",
  65. "customerNo"=>"",
  66. "supplierNo"=>'',
  67. "supplierName"=>'',
  68. "status"=>$sale['th_num']==$sale['good_num']?3:-1,
  69. "companyName"=>$sale['supplierName'],
  70. "companyNo"=>$sale['supplierNo'],
  71. ];
  72. if( strtotime($sale['addtime']) >= strtotime('2023-07-01 00:00:00'))$this->Tcode ='F';
  73. else $this->Tcode ='';
  74. if( strtotime($sale['addtime']) >= strtotime('2024-12-12 23:59:59')) $updatePrice=false;
  75. //补充商品信息
  76. if ($sale['order_type'] == 3 ||$sale['order_type'] == 4) {
  77. //咨询相关
  78. $good = Db::name('good_zixun')->alias("b")
  79. ->field('b.noble_metal,c.brand_name brand,d.unit,b.craft_desc cost_desc,b.good_weight noble_weight,b.tax,0 delivery_day,0 lead_time')
  80. ->leftJoin('brand c', 'c.id=b.brand_id')
  81. ->leftJoin('unit d', 'd.id=b.good_unit')
  82. ->where(['b.spuCode' => $sale['good_code']])
  83. ->findOrEmpty();
  84. } else {
  85. $good = Db::name('good')
  86. ->field('b.noble_metal,c.brand_name brand,d.unit,b.craft_desc cost_desc,b.noble_weight,b.tax,b.delivery_day,b.lead_time')
  87. ->alias('b')
  88. ->leftJoin('brand c', 'c.id=b.brand_id')
  89. ->leftJoin('unit d', 'd.id=b.good_unit')
  90. ->where(['b.is_del' => 0, 'b.spuCode' => $sale['good_code']])
  91. ->findOrEmpty();
  92. }
  93. $sale['plan_price'] = Db::name("good_nake")->where(["spuCode"=>$sale['good_code'],"is_del"=>0])
  94. ->order("min_num asc")->value("nake_total",0);
  95. $sale['plan_total'] = round($sale['good_num'] * $sale['plan_price'],2);
  96. //贵金属分类转换文本
  97. $good['noble_metal'] = $this->noble_metal[$good['noble_metal'] ?? 0] ?? '';
  98. //相关数据合并
  99. $sale = array_merge($sale, $good);
  100. //支付渠道相关信息
  101. $pay_rates = Db::name('pay_log')
  102. ->where(['is_del' => 0, 'pay_id' => $sale['pay_id'], 'orderCode' => $sale['orderCode']])
  103. ->where( 'companyNo', 'not in',["KH","GYS"])
  104. ->field(true)
  105. ->order(['weight' => 'desc'])
  106. ->select()
  107. ->toArray();
  108. //关联的采购单信息
  109. $cgd = Db::name('order_num')
  110. ->alias('a')
  111. ->field('b.*,c.addtime bktime,c.apply_id bkcreater')
  112. ->leftJoin('purchease_order b', 'b.cgdNo=a.cgdNo')
  113. ->leftJoin('purchease c', 'c.bk_code=b.bkcode')
  114. ->where('a.orderCode', $sale['orderCode'])
  115. ->findOrEmpty();
  116. //采购总金额和销售总金额
  117. if($updatePrice) $cgd_total = $sale_total = $sale['total_price'];
  118. else {
  119. $cgd_price = $sale_price = $sale['sale_price'];
  120. $cgd_total = $sale_total = $sale['total_price'];
  121. }
  122. //客户
  123. $customer = ['No' => $sale['customer_code'], 'name' => $sale['customerName']];
  124. foreach ($pay_rates as $keys=>$pay_rate) {
  125. //生成新的采购单号和销售单号
  126. $cgdNo = makeNo($this->Tcode.'CG');
  127. $tempOrder['cgdNo'] = $cgdNo ;
  128. $cgdNo = substr($cgdNo, 0, -2) . str_pad($this->i, 2, '0', STR_PAD_LEFT);
  129. $orderCode = makeNo($this->Tcode.'QR');
  130. $orderCode = substr($orderCode, 0, -2) . str_pad($this->i, 2, '0', STR_PAD_LEFT);
  131. $this->i++;
  132. //计算渠道业务公司采购单单价。
  133. if($updatePrice)$cgd_total = bcsub($cgd_total, bcmul($sale['total_price'] ?? "0", round($pay_rate['rate'] / 100, 4), 5), 5);
  134. else {
  135. $cgd_price = bcsub($cgd_price, bcmul($sale_price ?? "0", round($pay_rate['rate'] / 100, 4), 8), 8);
  136. $cgd_total =bcmul($cgd_price, $sale['good_num'], 8);
  137. }
  138. //此次销售单业务公司信息
  139. $supplier = ['No' => $pay_rate['companyNo'], 'name' => $pay_rate['companyName'],"pay_name"=>$pay_rate['pay_name']];
  140. $sale['origin_price'] = $sale['good_num'] > 0 ? bcdiv($cgd_total, $sale['good_num'], 8) : "0";
  141. $this->_handle_sale_caixiao($sale, $orderCode, $cgdNo, $sale_total, $customer, $supplier);
  142. $tempOrder['customerName'] = $pay_rate['companyName'];
  143. $tempOrder['customerNo'] = $pay_rate['companyNo'];
  144. $this->_handle_cgd_caixiao($cgd, $sale, $pay_rate, $cgdNo, $orderCode, $cgd_total,$tempOrder);
  145. //下一个渠道公司/原业务公司的 客户信息
  146. $customer= ['No' => $pay_rate['companyNo'], 'name' => $pay_rate['companyName']];
  147. if($updatePrice) $sale_total =$cgd_total;
  148. else {
  149. $sale_price =$cgd_price;
  150. $sale_total = bcmul($cgd_price, $sale['good_num'], 8);
  151. } //下一个渠道公司/原业务公司的 销售价未此次的采购单价
  152. //判断是否是最后一个渠道遍历。最后一次需要额外生成一个销售单给原业务公司;
  153. if($keys == count($pay_rates)-1){
  154. $orderCode = makeNo($this->Tcode.'QR');
  155. $tempOrder['orderCode'] = $orderCode ;
  156. $orderCode = substr($orderCode, 0, -2) . str_pad($this->i, 2, '0', STR_PAD_LEFT);
  157. $supplier = ['No' => $sale['supplierNo'], 'name' => $sale['supplierName'],"pay_name"=>$pay_rate['pay_name']];
  158. if(!isset($cgd['cgdNo']) && $sale['is_stock']==1 ){
  159. $origin_total =Db::name('child_bn')
  160. ->where([['orderCode','=',$sale['orderCode']],['total_bn_num','>',0],['is_del','=',0]])
  161. ->field("sum(origin_price*total_bn_num) total_price,sum(total_bn_num) num")->findOrEmpty();
  162. if(empty($origin_total)==false && $origin_total['num']>0)$sale['origin_price'] = bcdiv($origin_total['total_price'],$origin_total['num'],8);
  163. else $sale['origin_price']=0;
  164. }else{
  165. $sale['origin_price'] = $cgd['good_price'];
  166. }
  167. $this->_handle_sale_caixiao($sale, $orderCode, $cgd['cgdNo'] ?? '', $sale_total,$customer, $supplier,$tempOrder);
  168. }
  169. //把自己覆盖到上一个记录的供应商记录中
  170. if (isset($this->cgd_insert[$this->cgd_key - 1])) {
  171. $temp = $userCommon->handle('hqInfo', ['code' =>$supplier['No']]);
  172. $supplier = $temp['data'] ?? [];
  173. $this->cgd_insert[$this->cgd_key - 1]['supplierNo'] =$supplier['relation_code'];
  174. $this->cgd_insert[$this->cgd_key - 1]['supplier_name'] = $supplier['relation_name'];
  175. //处理采购销售同一数据 业务公司的供应商编号数据
  176. $tempOrder['supplierName'] =$supplier['relation_name'];
  177. $tempOrder['supplierNo'] = $supplier['relation_code'];
  178. $temps = $userCommon->handle('sInfo', ['code' =>$supplier['relation_code']]);
  179. $suppliers = $temps['data'] ?? [];
  180. $this->cgd_insert[$this->cgd_key - 1]['supplier_persion'] = $suppliers['person'] ?? '';
  181. $this->cgd_insert[$this->cgd_key - 1]['supplier_persionid'] = $suppliers['personid'] ?? 0;
  182. }
  183. }
  184. //清空该变量,以防止多个销售单覆盖数据的情况
  185. // $cgd_insert_tmp_data = array_merge($cgd_insert_tmp_data, $this->cgd_insert);
  186. //批量新增改为单次新增
  187. if($this->cgd_insert) Db::name('cgd_caixiao')->insertAll($this->cgd_insert);
  188. $this->cgd_insert = [];
  189. $this->cgd_key = 0;
  190. $date=date('Y-m-d H:i:s');
  191. echo "【{$date}】订单:{$sale['orderCode']}拆单 \r\n";
  192. if(strtotime($sale['addtime']) >= strtotime('2024-02-01 00:00:00'))$commonOrder[]= $tempOrder;
  193. }
  194. // if ($cgd_insert_tmp_data) Db::name('cgd_caixiao')->insertAll($cgd_insert_tmp_data);
  195. if ($this->sale_insert) Db::name('sale_caixiao')->insertAll($this->sale_insert);
  196. Db::commit();
  197. $this->OrderSave($commonOrder);
  198. // $output->writeln('处理完成');
  199. }catch (\Exception $e) {
  200. Db::rollback();
  201. $output->writeln('事务回滚:' . $e->getMessage() . '||' . $e->getFile() . '||' . $e->getLine());
  202. }
  203. } catch (\Exception $exception) {
  204. $output->writeln('脚本执行出错,' . $exception->getMessage() . '||' . $exception->getFile() . '||' . $exception->getLine());
  205. }
  206. Cache::store('redis')->dec($key);
  207. }
  208. //构建销售单
  209. private function _handle_sale_caixiao(array $sale = [], string $orderCode = '', string $cgdNo = '', string $sale_total ="0.00", array $customer = [], array $supplier = [],array &$tempOrder=[])
  210. {
  211. $sale_price = $sale['good_num'] > 0 ? bcdiv($sale_total, $sale['good_num'], 8) : 0;
  212. $origin_total =round($sale['origin_price']*$sale['good_num'],8);
  213. $tmp_sale = [
  214. 'manager'=>$sale['manager'],
  215. 'managerid'=>$sale['managerid'],
  216. 'origin_price' =>round( $sale['origin_price'] ?? 0, 8),
  217. 'origin_plan_price' =>round( $sale['plan_price'] ?? 0, 8),
  218. 'total_origin_price' =>$origin_total,
  219. 'total_origin_price_plan' => $sale['plan_total'],
  220. 'pay_source' => $supplier['pay_name'],
  221. 'sale_price' => round($sale_price, 8),
  222. 'total_price' => $sale_total,
  223. 'post_fee' => $sale['post_fee'] ?? 0,
  224. 'is_diff' => $sale['is_diff'] ?? 0,
  225. 'send_num' => $sale['send_num'] ?? 0,
  226. 'wsend_num' => $sale['wsend_num'] ?? 0,
  227. 'workNo' => $sale['workNo'] ?? '',
  228. 'poNo' => $sale['platform_order'] ?? '',
  229. 'other_orderNo' => $sale['other_orderNo'],
  230. 'th_num' => $sale['th_num'] ?? 0,
  231. 'send_type' => $sale['send_type'] ?? 0,
  232. 'gold_price' => $sale['gold_price'] ?? 0,
  233. 'cost_price' => $sale['cost_price'] ?? 0,
  234. 'status' => $sale['status'] ?? 0,
  235. 'open_type'=>$sale['open_type'],
  236. 'updatetime' => $sale['updatetime'],
  237. 'delivery_day' => $sale['delivery_day'] ?? 0,
  238. 'th_fee' => round(bcmul($sale_price, $sale['th_num'] ?? "0", 3), 2),
  239. 'cost_fee' => $sale['cost_price'] ?? 0,
  240. 'diff_fee' => $sale['diff_fee'] ?? 0,
  241. 'tax' => $sale['tax'] ?? '',
  242. 'diff_weight' => $sale['diff_weight'] ?? 0,
  243. 'send_status' => $sale['send_status'] ?? 0,
  244. ];
  245. $tmp = Db::name('sale_caixiao')
  246. ->field('id,orderCode')
  247. ->where(['oldCode' => $sale['orderCode'], 'supplierNo' =>$supplier['No'] ?? '', 'customer_code' =>$customer['No'] ?? ''])
  248. ->findOrEmpty();
  249. if (!empty($tmp)) {
  250. Db::name('sale_caixiao')
  251. ->where('id', $tmp['id'])
  252. ->update($tmp_sale);
  253. if(isset($tempOrder['orderCode']))$tempOrder['orderCode']= $tmp['orderCode'];
  254. } else {
  255. if(isset($tempOrder['orderCode']))$tempOrder['orderCode']= $orderCode;
  256. $this->sale_insert[] = array_merge($tmp_sale, [
  257. 'orderCode' => $orderCode,
  258. 'apply_id' => $sale['apply_id'] ?? 0,
  259. 'apply_name' => $sale['apply_name'] ?? '',
  260. 'order_type' => $sale['order_type'] ?? 0,
  261. 'order_source' => $this->order_source,
  262. 'platform_id' => $sale['platform_id'] ?? '',
  263. 'platform_type' => $sale['use_type'] ?? '',
  264. "platform_name"=>$sale['platform_name']??"",
  265. 'good_code' => $sale['good_code'] ?? '',
  266. 'cat_id' => $sale['cat_id'] ?? 0,
  267. 'cat_name' => json_encode($this->_get_cat_list($sale['cat_id']), JSON_UNESCAPED_UNICODE),
  268. 'good_name' => $sale['good_name'] ?? '',
  269. 'good_num' => $sale['good_num'] ?? 0,
  270. 'good_type' => $sale['good_type'] ?? 0,
  271. 'is_activity' => $sale['is_activity'] ?? 0,
  272. 'is_stock' => $sale['is_stock'] ?? 0,
  273. 'arrive_time' => $sale['arrive_timefvc'] ?? '',
  274. 'customer_code' => $customer['No'] ?? '',
  275. 'customer_name' => $customer['name'] ?? '',
  276. 'supplierNo' => $supplier['No'] ?? '',
  277. 'supplier_name' => $supplier['name'] ?? '',
  278. 'zxNo' => $sale['zxNo'] ?? '',
  279. 'proof_id' => $sale['proof_id'] ?? 0,
  280. 'proof_url' => $sale['proof_url'] ?? '',
  281. 'other_orderNo' => $sale['other_orderNo'],
  282. 'paytime' => $sale['paytime'] ?? '',
  283. 'workNo' => $sale['workNo'] ?? '',
  284. 'poNo' => $sale['platform_order'] ?? '',
  285. 'use_order' => $sale['use_order'],
  286. 'good_weight' => $sale['good_weight'] ?? 0,
  287. 'addtime' => $sale['addtime'],
  288. 'noble_metal' => $sale['noble_metal'] ?? '',
  289. 'brand' => $sale['brand'] ?? '',
  290. 'unit' => $sale['unit'] ?? '',
  291. 'cost_desc' => $sale['cost_desc'] ?? '',
  292. 'noble_weight' => $sale['noble_weight'] ?? 0,
  293. 'tax' => $sale['tax'] ?? '',
  294. 'lead_time' => $sale['lead_time'] ?? 0,
  295. 'depart' => isset($sale['apply_id']) ? get_company_name_by_uid($sale['apply_id']) : '',
  296. 'cgdNo' => $cgdNo,
  297. 'total_origin_price' => $origin_total,
  298. 'total_origin_price_plan' => $sale['plan_total'],
  299. 'pay_id' => $sale['pay_id'],
  300. 'pay_source' => $supplier['pay_name'],
  301. 'oldCode' => $sale['orderCode'],
  302. ]);
  303. }
  304. }
  305. //构建采购单
  306. private function _handle_cgd_caixiao(array $cgd = [], array $sale = [], array $pay_rate = [], string $cgdNo = '',
  307. string $orderCode = '', string $cgd_total ="0.00",array &$tempOrder=[])
  308. {
  309. $good_price = $sale['good_num'] > 0 ? bcdiv($cgd_total, $sale['good_num'], 8) : 0;
  310. $tmp_cgd = [
  311. 'good_price' => round($good_price, 8),
  312. 'total_fee' => $cgd_total,
  313. 'origin_total' => $cgd_total,
  314. 'pakage_fee' => $cgd['pakge_fee'] ?? 0,
  315. 'open_fee' => $cgd['open_fee'] ?? 0,
  316. 'cert_fee' => $cgd['cert_fee'] ?? 0,
  317. 'delivery_fee' => $cgd['delivery_fee'] ?? 0,
  318. 'mark_fee' => $cgd['mark_fee'] ?? 0,
  319. 'teach_fee' => $cgd['teach_fee'] ?? 0,
  320. 'nake_fee' => $cgd['nake_fee'] ?? 0,
  321. 'demo_fee' => $cgd['demo_fee'] ?? 0,
  322. 'weight' => $cgd['weight'] ?? 0,
  323. 'diff_weight' => $sale['diff_weight'] ?? 0,
  324. 'diff_fee' => $sale['diff_fee'] ?? 0,
  325. 'gold_price' => $sale['gold_price'] ?? 0,
  326. 'send_num' => $sale['send_num'] ?? 0,
  327. 'wsend_num' => $sale['wsend_num'] ?? 0,
  328. 'status' => $cgd['status'] ?? 3,
  329. 'order_type' => $sale['order_type'],
  330. 'order_source' => $this->order_source,
  331. 'good_type' => $sale['good_type'] ?? '',
  332. 'last_time' => $cgd['last_time'] ?? '',
  333. 'send_type' => $sale['send_type'] ?? '',
  334. 'send_status' => $sale['send_status'] ?? '',
  335. "open_type"=>$sale['open_type'],
  336. 'th_num' => $sale['th_num'] ?? 0,
  337. 'th_fee' => round(bcmul($good_price, $sale['th_num'] ?? "0", 8), 2),
  338. 'updatetime' => $sale['updatetime'],
  339. ];
  340. $tmp = Db::name('cgd_caixiao')
  341. ->field('id,cgdNo,supplier_name,supplierNo')
  342. ->where(['oldCode' => $sale['orderCode'], 'companyNo' => $pay_rate['companyNo']])
  343. ->findOrEmpty();
  344. if (!empty($tmp)) {
  345. Db::name('cgd_caixiao')
  346. ->where('id', $tmp['id'])
  347. ->update($tmp_cgd);
  348. if(isset($tempOrder['cgdNo'])){
  349. $tempOrder['cgdNo']= $tmp['cgdNo'];
  350. $tempOrder['supplierName'] =$tmp['supplier_name'];
  351. $tempOrder['supplierNo'] = $tmp['supplierNo'];
  352. }
  353. } else {
  354. if(isset($tempOrder['cgdNo']))$tempOrder['cgdNo']=$cgdNo;
  355. $this->cgd_insert[$this->cgd_key] = array_merge($tmp_cgd, [
  356. 'cgdNo' => $cgdNo,
  357. 'bkcode' => $cgd['bkcode'] ?? '',
  358. 'wsm_code' => $cgd['wsm_code'] ?? '',
  359. 'cgder' => $sale['cgder'] ?? '',
  360. 'cgder_id' => $sale['cgderid'] ?? 0,
  361. 'depart' => isset($sale['cgderid']) ? get_company_name_by_uid($sale['cgderid']) : '',
  362. 'qrdNo' => $orderCode,
  363. 'platform_id' => $sale['platform_id'] ?? '',
  364. 'platform_name'=>$sale['platform_name']??'',
  365. 'spuCode' => $cgd['spuCode'] ?? $sale['good_code'],
  366. 'good_name' => $sale['good_name'],
  367. 'skuCode' => $cgd['skuCode'] ?? '',
  368. 'good_num' => $sale['good_num'] ?? 0,
  369. 'cat_name' => json_encode($this->_get_cat_list($sale['cat_id']), JSON_UNESCAPED_UNICODE),
  370. 'companyNo' => $pay_rate['companyNo'],
  371. 'companyName' => $pay_rate['companyName'],
  372. 'supplierNo' => $sale['supplierNo'],
  373. 'supplier_name' => $sale['supplierName'],
  374. 'bktime' => $cgd['bktime'] ?? '',
  375. 'bkcreater' => $cgd['bkcreater'] ?? '',
  376. 'noble_metal' => $sale['noble_metal'] ?? '',
  377. 'brand' => $sale['brand'] ?? '',
  378. 'unit' => $sale['unit'] ?? '',
  379. 'cost_desc' => $sale['cost_desc'] ?? '',
  380. 'noble_weight' => $sale['noble_weight'] ?? '',
  381. 'tax' => $sale['tax'] ?? '',
  382. 'is_stock' => $sale['is_stock'],
  383. 'delivery_day' => $sale['delivery_day'] ?? 0,
  384. 'lead_time' => $sale['lead_time'] ?? 0,
  385. 'is_diff' => $sale['is_diff'],
  386. 'addtime' => $sale['addtime'],
  387. 'supplier_persion' => '',
  388. 'supplier_persionid' => '',
  389. 'pay_id' => $sale['pay_id'],
  390. 'oldCode' => $sale['orderCode']
  391. ]);
  392. $this->cgd_key++;
  393. }
  394. }
  395. //获取分类层级信息,id、cat_name(分类名称)和fund_code(财务核算码)
  396. private function _get_cat_list($var, $data = [])
  397. {
  398. $str = Db::name('cat')
  399. ->field('id,cat_name,fund_code,pid')
  400. ->where(['id' => $var])
  401. ->findOrEmpty();
  402. if ($str == false) return [];
  403. $vmn = [];
  404. $vmn['id'] = $str['id'];
  405. $vmn['cat_name'] = $str['cat_name'];
  406. $vmn['fund_code'] = $str['fund_code'];
  407. array_unshift($data, $vmn);
  408. if ($str['pid'] == 0) return $data;
  409. else return $this->_get_cat_list($str['pid'], $data);
  410. }
  411. //处理采购销售同一数据 录入结算 统一数据表中
  412. protected function OrderSave($order){
  413. if(empty($order)) return ;
  414. foreach ($order as &$item){
  415. $com = $this->comOrder->where('cxCode',$item['cxCode'])->findOrEmpty();
  416. if($com->isEmpty()==false){
  417. $item['id'] = $com->id;
  418. $item['tyCode'] = $com->tyCode;
  419. if(in_array($com->status,[0,1,2]))$item['status']=$com->status;
  420. }else{
  421. $item['id']=null;
  422. }
  423. }
  424. $this->comOrder->saveAll($order);
  425. }
  426. }