SplitSale.php 17 KB

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