SplitSale.php 17 KB

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