SplitSale.php 18 KB

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