SplitSale.php 17 KB

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