SplitSale.php 18 KB

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