SplitSale.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. namespace app\command;
  3. use think\console\Command;
  4. use think\console\Input;
  5. use think\console\Output;
  6. use think\Exception;
  7. use think\facade\Cache;
  8. use think\facade\Db;
  9. class SplitSale extends Command
  10. {
  11. private $i = 0;//各种编码的自增变量
  12. private $sale_insert = [];//wsm_sale_caixiao的新增数据
  13. private $cgd_insert = [];//wsm_cgd_caixiao的新增数据
  14. private $noble_metal = [1 => '18K', 2 => '24K', 3 => '白银'];//贵金属种类对应文本
  15. private $cgd_key = 0;//新增到wsm_cgd_caixiao的数组下标,从0开始
  16. private $order_source = 8;//支付渠道
  17. protected function configure()
  18. {
  19. $this->setName('split_sale')->setDescription('销售订单拆分');
  20. parent::configure();
  21. }
  22. protected function execute(Input $input, Output $output)
  23. {
  24. $date=date("Y-m-d H:i:s");
  25. try {
  26. $key = 'split_sale_';
  27. $rs = Cache::store('redis')->get($key);
  28. if ($rs) return true;
  29. Cache::store('redis')->set($key, 1, 60 * 5);
  30. Db::startTrans();
  31. try {
  32. $data = Db::name('sale')
  33. ->alias('a')
  34. ->leftJoin('platform b', 'b.id=a.platform_id')
  35. ->leftJoin('good_proof c', 'c.id=a.proof_id')
  36. ->field('a.*,b.platform_name,c.proof_url')
  37. ->where([
  38. ['a.is_del', '=', 0],
  39. ['a.updatetime', '>=', date('Y-m-d H:i:s', time() - 5 * 60)],
  40. ['a.pay_id', '<>', 0]
  41. ])
  42. ->cursor();
  43. // $cgd_insert_tmp_data=[];
  44. foreach ($data as $sale) {
  45. //补充商品信息
  46. if ($sale['order_type'] == 3) {
  47. //咨询相关
  48. $good = Db::name('consult_bids')
  49. ->field('b.noble_metal,c.brand_name brand,d.unit,a.cost_desc,a.good_weight noble_weight,a.tax,a.delivery_day,b.lead_time')
  50. ->alias('a')
  51. ->leftJoin('good_basic b', 'b.is_del=0 AND b.spuCode=a.spuCode')
  52. ->leftJoin('brand c', 'c.id=a.brand_id')
  53. ->leftJoin('unit d', 'd.id=a.unit_id')
  54. ->where(['a.is_del' => 0, 'a.bidNo' => $sale['zxNo']])
  55. ->findOrEmpty();
  56. } elseif ($sale['order_type'] == 4) {
  57. //报备单
  58. $good = Db::name('filing')
  59. ->field('a.noble_metal,c.brand_name brand,d.unit,a.cost_desc,a.gold_weight noble_weight,a.tax,a.delivery_day,0 lead_time')
  60. ->alias('a')
  61. ->leftJoin('brand c', 'c.id=a.brand_id')
  62. ->leftJoin('unit d', 'd.id=a.unit_id')
  63. ->where(['a.is_del' => 0, 'a.orderCode' => $sale['orderCode']])
  64. ->findOrEmpty();
  65. } else {
  66. $good = Db::name('good')
  67. ->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')
  68. ->alias('b')
  69. ->leftJoin('brand c', 'c.id=b.brand_id')
  70. ->leftJoin('unit d', 'd.id=b.good_unit')
  71. ->where(['b.is_del' => 0, 'b.spuCode' => $sale['good_code']])
  72. ->findOrEmpty();
  73. }
  74. //贵金属分类转换文本
  75. $good['noble_metal'] = $this->noble_metal[$good['noble_metal'] ?? 0] ?? '';
  76. //相关数据合并
  77. $sale = array_merge($sale, $good);
  78. //支付渠道相关信息
  79. $pay_rates = Db::name('pay_log')
  80. ->where(['is_del' => 0, 'pay_id' => $sale['pay_id'], 'orderCode' => $sale['orderCode']])
  81. ->field(true)
  82. ->order(['weight' => 'desc'])
  83. ->cursor();
  84. //关联的采购单信息
  85. $cgd = Db::name('order_num')
  86. ->alias('a')
  87. ->field('b.*,c.addtime bktime,c.apply_id bkcreater')
  88. ->leftJoin('purchease_order b', 'b.cgdNo=a.cgdNo')
  89. ->leftJoin('purchease c', 'c.bk_code=b.bkcode')
  90. ->where('a.orderCode', $sale['orderCode'])
  91. ->findOrEmpty();
  92. //采购总金额和销售总金额
  93. $cgd_total = $sale_total = $sale['total_price'];
  94. //客户
  95. $customer = ['No' => $sale['customer_code'], 'name' => Db::name('customer_info')->where('companyNo', $sale['customer_code'])->value('companyName', '')];
  96. //供应商
  97. $supplier = ['No' => $sale['supplierNo'], 'name' => Db::name('business')->where('companyNo', $sale['supplierNo'])->value('company', '')];
  98. foreach ($pay_rates as $pay_rate) {
  99. //生成新的采购单号和销售单号
  100. $cgdNo = makeNo('CG');
  101. $cgdNo = substr($cgdNo, 0, -2) . str_pad($this->i, 2, '0', STR_PAD_LEFT);
  102. $orderCode = makeNo('QR');
  103. $orderCode = substr($orderCode, 0, -2) . str_pad($this->i, 2, '0', STR_PAD_LEFT);
  104. $this->i++;
  105. $sale_total = $cgd_total;
  106. $cgd_total = bcsub($cgd_total, bcmul($sale['total_price'] ?? 0, $pay_rate['rate'], 3), 2);
  107. //只处理采购单
  108. if (strtoupper($pay_rate['companyNo']) == 'KH') {
  109. //此时尚未生成销售单,存销售单号无意义 吧?
  110. // if ($pay_rate['is_cgd'] == 1) $this->_handle_cgd_caixiao($cgd, $sale, $pay_rate, $cgdNo, $orderCode, $cgd_total);
  111. if ($pay_rate['is_cgd'] == 1) $this->_handle_cgd_caixiao($cgd, $sale, $pay_rate, $cgdNo, '', $cgd_total);
  112. } elseif (strtoupper($pay_rate['companyNo']) == 'GYS') {
  113. //只处理销售单
  114. //供应商事先覆盖
  115. $supplier = ['No' => $sale['supplierNo'], 'name' => Db::name('business')->where('companyNo', $sale['supplierNo'])->value('company', '')];
  116. if ($pay_rate['is_qrd'] == 1) {
  117. //此时生成的供应商的销售单,应该关联原始采购单号
  118. $this->_handle_sale_caixiao($sale, $orderCode, $cgd['cgdNo']??'', $sale_total,$customer, $supplier);
  119. }
  120. } else {
  121. //供应商事先覆盖
  122. $supplier = ['No' => $pay_rate['companyNo'], 'name' => $pay_rate['companyName']];
  123. //需要生成销售单
  124. if ($pay_rate['is_qrd'] == 1) $this->_handle_sale_caixiao($sale, $orderCode, $cgdNo, $sale_total, $customer, $supplier);
  125. //需要生成采购单
  126. if ($pay_rate['is_cgd'] == 1) $this->_handle_cgd_caixiao($cgd, $sale, $pay_rate, $cgdNo, $orderCode, $cgd_total);
  127. //客户事后覆盖
  128. $customer = ['No' => $pay_rate['companyNo'], 'name' => $pay_rate['companyName']];
  129. }
  130. //把自己覆盖到上一个记录的供应商记录中
  131. if (isset($this->cgd_insert[$this->cgd_key - 1])) {
  132. if (strtoupper($pay_rate['companyNo']) == 'KH') continue;
  133. elseif (strtoupper($pay_rate['companyNo']) == 'GYS') $supplierNo = $sale['supplierNo'];
  134. else $supplierNo = $pay_rate['companyNo'];
  135. $supplier = Db::name('supplier')
  136. ->field('id,code,name,person,personid')
  137. ->where('code', $supplierNo)
  138. ->findOrEmpty();
  139. $this->cgd_insert[$this->cgd_key - 1]['supplierNo'] = $supplierNo;
  140. $this->cgd_insert[$this->cgd_key - 1]['supplier_name'] = $supplier['name'] ?? '';
  141. $this->cgd_insert[$this->cgd_key - 1]['supplier_persion'] = $supplier['person'] ?? '';
  142. $this->cgd_insert[$this->cgd_key - 1]['supplier_persionid'] = $supplier['personid'] ?? 0;
  143. if(isset($supplier['personid'])) $this->cgd_insert[$this->cgd_key - 1]['depart']=get_company_name_by_uid($supplier['personid'] ?? 0);
  144. }
  145. }
  146. //清空该变量,以防止多个销售单覆盖数据的情况
  147. // $cgd_insert_tmp_data = array_merge($cgd_insert_tmp_data, $this->cgd_insert);
  148. //批量新增改为单次新增
  149. if ($this->cgd_insert) Db::name('cgd_caixiao')->insertAll($this->cgd_insert);
  150. $this->cgd_insert=[];
  151. $this->cgd_key=0;
  152. $output->writeln( "[$date] 订单{$sale['orderCode']}处理结束");
  153. }
  154. // if ($cgd_insert_tmp_data) Db::name('cgd_caixiao')->insertAll($cgd_insert_tmp_data);
  155. if ($this->sale_insert) Db::name('sale_caixiao')->insertAll($this->sale_insert);
  156. Db::commit();
  157. $output->writeln( "[$date] 订单处理结束");
  158. } catch (Exception $e) {
  159. Db::rollback();
  160. $output->writeln( "[$date] 事务回滚:" . $e->getMessage() . '||' . $e->getFile() . '||' . $e->getLine());
  161. }
  162. Cache::store('redis')->set($key, 0);
  163. } catch (Exception $exception) {
  164. $output->writeln( "[$date] 脚本执行出错:" . $exception->getMessage() . '||' . $exception->getFile() . '||' . $exception->getLine());
  165. }
  166. }
  167. //构建销售单
  168. private function _handle_sale_caixiao(array $sale = [], string $orderCode = '', string $cgdNo = '', float $sale_total = 0.00, array $customer = [], array $supplier = [])
  169. {
  170. $sale_price = $sale['good_num'] > 0 ? bcdiv($sale_total, $sale['good_num'], 3) : 0;
  171. $tmp_sale = [
  172. 'origin_price' => $sale['origin_price'] ?? 0,
  173. 'sale_price' => round($sale_price, 2),
  174. 'total_price' => $sale_total,
  175. 'customer_code' => $customer['No'] ?? '',
  176. 'customer_name' => $customer['name'] ?? '',
  177. 'supplierNo' => $supplier['No'] ?? '',
  178. 'supplier_name' => $supplier['name'] ?? '',
  179. 'tax' => $sale['tax'] ?? '',
  180. 'post_fee' => $sale['post_fee'] ?? 0,
  181. 'is_diff' => $sale['is_diff'] ?? 0,
  182. 'send_num' => $sale['send_num'] ?? 0,
  183. 'wsend_num' => $sale['wsend_num'] ?? 0,
  184. 'th_num' => $sale['th_num'] ?? 0,
  185. 'send_type' => $sale['send_type'] ?? 0,
  186. 'gold_price' => $sale['gold_price'] ?? 0,
  187. 'cost_price' => $sale['cost_price'] ?? 0,
  188. 'status' => $sale['status'] ?? 0,
  189. 'workNo' => $sale['workNo'] ?? '',
  190. 'poNo' => $sale['platform_order'] ?? '',
  191. 'updatetime' => $sale['updatetime'],
  192. 'delivery_day' => $sale['delivery_day'] ?? 0,
  193. 'th_fee' => round(bcmul($sale_price, $sale['th_num'] ?? 0, 3), 2),
  194. 'cost_fee' => $sale['cost_price'] ?? 0,
  195. 'diff_fee' => $sale['diff_fee'] ?? 0,
  196. 'diff_weight' => $sale['diff_weight'] ?? 0,
  197. 'send_status' => $sale['send_status'] ?? 0,
  198. ];
  199. $tmp = Db::name('sale_caixiao')
  200. ->field('id')
  201. ->where(['oldCode' => $sale['orderCode'], 'customer_code' => $customer['No']])
  202. ->findOrEmpty();
  203. if (!empty($tmp)) {
  204. Db::name('sale_caixiao')
  205. ->where('id', $tmp['id'])
  206. ->update($tmp_sale);
  207. } else {
  208. $this->sale_insert[] = array_merge($tmp_sale, [
  209. 'orderCode' => $orderCode,
  210. 'apply_id' => $sale['apply_id'] ?? 0,
  211. 'apply_name' => $sale['apply_name'] ?? '',
  212. 'order_type' => $sale['order_type'] ?? 0,
  213. 'order_source' => $this->order_source,
  214. 'platform_id' => $sale['platform_name'] ?? '',
  215. 'good_code' => $sale['good_code'] ?? '',
  216. 'cat_id' => $sale['cat_id'] ?? 0,
  217. 'cat_name' => json_encode($this->_get_cat_list($sale['cat_id']), JSON_UNESCAPED_UNICODE),
  218. 'good_name' => $sale['good_name'] ?? '',
  219. 'good_num' => $sale['good_num'] ?? 0,
  220. 'good_type' => $sale['good_type'] ?? 0,
  221. 'is_activity' => $sale['is_activity'] ?? 0,
  222. 'is_stock' => $sale['is_stock'] ?? 0,
  223. 'arrive_time' => $sale['arrive_timefvc'] ?? '',
  224. 'zxNo' => $sale['zxNo'] ?? '',
  225. 'proof_id' => $sale['proof_id'] ?? 0,
  226. 'proof_url' => $sale['proof_url'] ?? '',
  227. 'other_orderNo' => $sale['other_orderNo'],
  228. 'paytime' => $sale['paytime'] ?? '',
  229. 'use_order' => $sale['use_order'],
  230. 'good_weight' => $sale['good_weight'] ?? 0,
  231. 'addtime' => $sale['addtime'],
  232. 'noble_metal' => $sale['noble_metal'] ?? '',
  233. 'brand' => $sale['brand'] ?? '',
  234. 'unit' => $sale['unit'] ?? '',
  235. 'cost_desc' => $sale['cost_desc'] ?? '',
  236. 'noble_weight' => $sale['noble_weight'] ?? 0,
  237. 'lead_time' => $sale['lead_time'] ?? 0,
  238. 'depart' => get_company_name_by_uid($sale['apply_id'] ?? 0) ?? '',
  239. 'cgdNo' => $cgdNo,
  240. 'pay_id' => $sale['pay_id'],
  241. 'oldCode' => $sale['orderCode'],
  242. ]);
  243. }
  244. }
  245. //构建采购单
  246. private function _handle_cgd_caixiao(array $cgd = [], array $sale = [], array $pay_rate = [], string $cgdNo = '', string $orderCode = '', float $cgd_total = 0.00)
  247. {
  248. $good_price = $sale['good_num'] > 0 ? bcdiv($cgd_total, $sale['good_num'], 3) : 0;
  249. $tmp_cgd = [
  250. 'cgder' => $sale['cgder'] ?? '',
  251. 'cgder_id' => $sale['cgderid'] ?? 0,
  252. 'good_price' =>round($good_price,2),
  253. 'total_fee' => $cgd_total,
  254. 'pakage_fee' => $cgd['pakge_fee'] ?? 0,
  255. 'open_fee' => $cgd['open_fee'] ?? 0,
  256. 'cert_fee' => $cgd['cert_fee'] ?? 0,
  257. 'delivery_fee' => $cgd['delivery_fee'] ?? 0,
  258. 'mark_fee' => $cgd['mark_fee'] ?? 0,
  259. 'teach_fee' => $cgd['teach_fee'] ?? 0,
  260. 'nake_fee' => $cgd['nake_fee'] ?? 0,
  261. 'demo_fee' => $cgd['demo_fee'] ?? 0,
  262. 'weight' => $cgd['weight'] ?? 0,
  263. 'diff_weight' => $sale['diff_weight'] ?? 0,
  264. 'diff_fee' => $sale['diff_fee'] ?? 0,
  265. 'gold_price' => $sale['gold_price'] ?? 0,
  266. 'send_num' => $sale['send_num'] ?? 0,
  267. 'wsend_num' => $sale['wsend_num'] ?? 0,
  268. 'status' => $cgd['status'] ?? '',
  269. 'order_type' => $sale['order_type'],
  270. 'order_source' => $this->order_source,
  271. 'good_type' => $sale['good_type'] ?? '',
  272. 'last_time' => $cgd['last_time'] ?? '',
  273. 'send_type' => $sale['send_type'] ?? '',
  274. 'send_status' => $sale['send_status'] ?? '',
  275. 'tax' => $sale['tax'] ?? '',
  276. 'th_num' => $sale['th_num'] ?? 0,
  277. 'th_fee' => round(bcmul($good_price, $sale['th_num'] ?? 0, 3), 2),
  278. 'updatetime' => $sale['updatetime'],
  279. ];
  280. $tmp = Db::name('cgd_caixiao')
  281. ->field('id')
  282. ->where(['oldCode' => $sale['orderCode'], 'companyNo' => $pay_rate['companyNo']])
  283. ->findOrEmpty();
  284. if (!empty($tmp)) {
  285. Db::name('cgd_caixiao')
  286. ->where('id', $tmp['id'])
  287. ->update($tmp_cgd);
  288. } else {
  289. $this->cgd_insert[$this->cgd_key] = array_merge($tmp_cgd, [
  290. 'cgdNo' => $cgdNo,
  291. 'bkcode' => $cgd['bkcode'] ?? '',
  292. 'wsm_code' => $cgd['wsm_code'] ?? '',
  293. 'depart' => get_company_name_by_uid($sale['cgder_id'] ?? 0) ?? '',
  294. 'qrdNo' => $orderCode,
  295. 'spuCode' => $cgd['spuCode'] ?? $sale['good_code'],
  296. 'good_name' => $sale['good_name'],
  297. 'skuCode' => $cgd['skuCode'] ?? '',
  298. 'good_num' => $sale['good_num'] ?? 0,
  299. 'cat_name' => json_encode($this->_get_cat_list($sale['cat_id']), JSON_UNESCAPED_UNICODE),
  300. 'companyNo' => $pay_rate['companyNo'],
  301. 'companyName' => $pay_rate['companyName'],
  302. 'supplierNo' => '',
  303. 'supplier_name' => '',
  304. 'bktime' => $cgd['bktime'] ?? '',
  305. 'bkcreater' => $cgd['bkcreater'] ?? '',
  306. 'noble_metal' => $sale['noble_metal'] ?? '',
  307. 'brand' => $sale['brand'] ?? '',
  308. 'unit' => $sale['unit'] ?? '',
  309. 'cost_desc' => $sale['cost_desc'] ?? '',
  310. 'noble_weight' => $sale['noble_weight'] ?? '',
  311. // 'tax' => $sale['tax'] ?? '',
  312. 'is_stock' => $sale['is_stock'],
  313. 'delivery_day' => $sale['delivery_day'] ?? 0,
  314. 'lead_time' => $sale['lead_time'] ?? 0,
  315. 'is_diff' => $sale['is_diff'],
  316. 'addtime' => $sale['addtime'],
  317. 'supplier_persion' => '',
  318. 'supplier_persionid' => '',
  319. 'pay_id' => $sale['pay_id'],
  320. 'oldCode' => $sale['orderCode']
  321. ]);
  322. $this->cgd_key++;
  323. }
  324. }
  325. //获取分类层级信息,id、cat_name(分类名称)和fund_code(财务核算码)
  326. private function _get_cat_list($var, $data = [])
  327. {
  328. $str = Db::name('cat')
  329. ->field('id,cat_name,fund_code,pid')
  330. ->where(['id' => $var])
  331. ->findOrEmpty();
  332. if ($str == false) return [];
  333. $vmn = [];
  334. $vmn['id'] = $str['id'];
  335. $vmn['cat_name'] = $str['cat_name'];
  336. $vmn['fund_code'] = $str['fund_code'];
  337. array_unshift($data, $vmn);
  338. if ($str['pid'] == 0) return $data;
  339. else return $this->_get_cat_list($str['pid'], $data);
  340. }
  341. }