SplitSale.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. private $Tcode = '';
  19. protected function configure()
  20. {
  21. $this->setName('split_sale')->setDescription('销售订单拆分');
  22. parent::configure();
  23. }
  24. protected function execute(Input $input, Output $output)
  25. {
  26. try {
  27. $key = 'split_sale_';
  28. $rs = Cache::store('redis')->inc($key);
  29. if ($rs!=1){
  30. Cache::store('redis')->dec($key);
  31. return true;
  32. }
  33. Db::startTrans();
  34. try {
  35. $data = Db::name('sale')
  36. ->alias('a')
  37. ->leftJoin('platform b', 'b.id=a.platform_id')
  38. ->leftJoin('good_proof c', 'c.id=a.proof_id')
  39. ->field('a.*,b.platform_name,b.use_type,c.proof_url')
  40. ->where([
  41. ['a.is_del', '=', 0],
  42. ['a.updatetime', '>=', date('Y-m-d H:i:s', time() - 5 * 60)],
  43. ['a.pay_id', '<>', 0]
  44. ])->cursor();
  45. $userCommon = User::getIns();
  46. foreach ($data as $sale) {
  47. if( strtotime($sale['addtime']) >= strtotime('2023-07-01 00:00:00'))$this->Tcode ='F';
  48. else $this->Tcode ='';
  49. //补充商品信息
  50. if ($sale['order_type'] == 3 ||$sale['order_type'] == 4) {
  51. //咨询相关
  52. $good = Db::name('good_zixun')->alias("b")
  53. ->field('b.noble_metal,c.brand_name brand,d.unit,b.craft_desc cost_desc,b.good_weight noble_weight,b.tax,0 delivery_day,0 lead_time')
  54. ->leftJoin('brand c', 'c.id=b.brand_id')
  55. ->leftJoin('unit d', 'd.id=b.good_unit')
  56. ->where(['b.spuCode' => $sale['good_code']])
  57. ->findOrEmpty();
  58. } else {
  59. $good = Db::name('good')
  60. ->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')
  61. ->alias('b')
  62. ->leftJoin('brand c', 'c.id=b.brand_id')
  63. ->leftJoin('unit d', 'd.id=b.good_unit')
  64. ->where(['b.is_del' => 0, 'b.spuCode' => $sale['good_code']])
  65. ->findOrEmpty();
  66. }
  67. $sale['plan_price'] = Db::name("good_nake")->where(["spuCode"=>$sale['good_code'],"is_del"=>0])
  68. ->order("min_num asc")->value("nake_total",0);
  69. $sale['plan_total'] = round($sale['good_num'] * $sale['plan_price'],2);
  70. //贵金属分类转换文本
  71. $good['noble_metal'] = $this->noble_metal[$good['noble_metal'] ?? 0] ?? '';
  72. //相关数据合并
  73. $sale = array_merge($sale, $good);
  74. //支付渠道相关信息
  75. $pay_rates = Db::name('pay_log')
  76. ->where(['is_del' => 0, 'pay_id' => $sale['pay_id'], 'orderCode' => $sale['orderCode']])
  77. ->where( 'companyNo', 'not in',["KH","GYS"])
  78. ->field(true)
  79. ->order(['weight' => 'desc'])
  80. ->select()
  81. ->toArray();
  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' => $sale['customerName']];
  94. foreach ($pay_rates as $keys=>$pay_rate) {
  95. //生成新的采购单号和销售单号
  96. $cgdNo = makeNo($this->Tcode.'CG');
  97. $cgdNo = substr($cgdNo, 0, -2) . str_pad($this->i, 2, '0', STR_PAD_LEFT);
  98. $orderCode = makeNo($this->Tcode.'QR');
  99. $orderCode = substr($orderCode, 0, -2) . str_pad($this->i, 2, '0', STR_PAD_LEFT);
  100. $this->i++;
  101. //计算渠道业务公司采购单单价。
  102. $cgd_total = bcsub($cgd_total, bcmul($sale['total_price'] ?? 0, round($pay_rate['rate'] / 100, 4), 5), 5);
  103. //此次销售单业务公司信息
  104. $supplier = ['No' => $pay_rate['companyNo'], 'name' => $pay_rate['companyName'],"pay_name"=>$pay_rate['pay_name']];
  105. $this->_handle_sale_caixiao($sale, $orderCode, $cgdNo, $sale_total, $customer, $supplier);
  106. $this->_handle_cgd_caixiao($cgd, $sale, $pay_rate, $cgdNo, $orderCode, $cgd_total);
  107. $sale['origin_price'] = $sale['good_num'] > 0 ? bcdiv($cgd_total, $sale['good_num'], 5) : 0;
  108. //下一个渠道公司/原业务公司的 客户信息
  109. $customer= ['No' => $pay_rate['companyNo'], 'name' => $pay_rate['companyName']];
  110. $sale_total =$cgd_total; //下一个渠道公司/原业务公司的 销售价未此次的采购单价
  111. //判断是否是最后一个渠道遍历。最后一次需要额外生成一个销售单给原业务公司;
  112. if($keys == count($pay_rates)-1){
  113. $orderCode = makeNo($this->Tcode.'QR');
  114. $orderCode = substr($orderCode, 0, -2) . str_pad($this->i, 2, '0', STR_PAD_LEFT);
  115. $supplier = ['No' => $sale['supplierNo'], 'name' => $sale['supplierName'],"pay_name"=>$pay_rate['pay_name']];
  116. $this->_handle_sale_caixiao($sale, $orderCode, $cgd['cgdNo'] ?? '', $sale_total, $customer, $supplier);
  117. }
  118. //把自己覆盖到上一个记录的供应商记录中
  119. if (isset($this->cgd_insert[$this->cgd_key - 1])) {
  120. $temp = $userCommon->handle('hqInfo', ['code' =>$supplier['No']]);
  121. $supplier = $temp['data'] ?? [];
  122. $this->cgd_insert[$this->cgd_key - 1]['supplierNo'] =$supplier['relation_code'];
  123. $this->cgd_insert[$this->cgd_key - 1]['supplier_name'] = $supplier['relation_name'];
  124. $temps = $userCommon->handle('sInfo', ['code' =>$supplier['relation_code']]);
  125. $suppliers = $temps['data'] ?? [];
  126. $this->cgd_insert[$this->cgd_key - 1]['supplier_persion'] = $suppliers['person'] ?? '';
  127. $this->cgd_insert[$this->cgd_key - 1]['supplier_persionid'] = $suppliers['personid'] ?? 0;
  128. }
  129. }
  130. //清空该变量,以防止多个销售单覆盖数据的情况
  131. // $cgd_insert_tmp_data = array_merge($cgd_insert_tmp_data, $this->cgd_insert);
  132. //批量新增改为单次新增
  133. if($this->cgd_insert) Db::name('cgd_caixiao')->insertAll($this->cgd_insert);
  134. $this->cgd_insert = [];
  135. $this->cgd_key = 0;
  136. }
  137. // if ($cgd_insert_tmp_data) Db::name('cgd_caixiao')->insertAll($cgd_insert_tmp_data);
  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')->dec($key);
  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. $sale_price = $sale['good_num'] > 0 ? bcdiv($sale_total, $sale['good_num'], 5) : 0;
  154. $tmp_sale = [
  155. 'manager'=>$sale['manager'],
  156. 'managerid'=>$sale['managerid'],
  157. 'origin_price' =>round( $sale['origin_price'] ?? 0, 2),
  158. 'origin_plan_price' =>round( $sale['plan_price'] ?? 0, 2),
  159. 'total_origin_price' => round($sale['origin_price']*$sale['good_num'],2),
  160. 'total_origin_price_plan' => $sale['plan_total'],
  161. 'pay_source' => $supplier['pay_name'],
  162. 'sale_price' => round($sale_price, 2),
  163. 'total_price' => $sale_total,
  164. 'post_fee' => $sale['post_fee'] ?? 0,
  165. 'is_diff' => $sale['is_diff'] ?? 0,
  166. 'send_num' => $sale['send_num'] ?? 0,
  167. 'wsend_num' => $sale['wsend_num'] ?? 0,
  168. 'th_num' => $sale['th_num'] ?? 0,
  169. 'send_type' => $sale['send_type'] ?? 0,
  170. 'gold_price' => $sale['gold_price'] ?? 0,
  171. 'cost_price' => $sale['cost_price'] ?? 0,
  172. 'status' => $sale['status'] ?? 0,
  173. 'updatetime' => $sale['updatetime'],
  174. 'delivery_day' => $sale['delivery_day'] ?? 0,
  175. 'th_fee' => round(bcmul($sale_price, $sale['th_num'] ?? 0, 3), 2),
  176. 'cost_fee' => $sale['cost_price'] ?? 0,
  177. 'diff_fee' => $sale['diff_fee'] ?? 0,
  178. 'diff_weight' => $sale['diff_weight'] ?? 0,
  179. 'send_status' => $sale['send_status'] ?? 0,
  180. ];
  181. $tmp = Db::name('sale_caixiao')
  182. ->field('id')
  183. ->where(['oldCode' => $sale['orderCode'], 'supplierNo' =>$supplier['No'] ?? ''])
  184. ->findOrEmpty();
  185. if (!empty($tmp)) {
  186. Db::name('sale_caixiao')
  187. ->where('id', $tmp['id'])
  188. ->update($tmp_sale);
  189. } else {
  190. $this->sale_insert[] = array_merge($tmp_sale, [
  191. 'orderCode' => $orderCode,
  192. 'apply_id' => $sale['apply_id'] ?? 0,
  193. 'apply_name' => $sale['apply_name'] ?? '',
  194. 'order_type' => $sale['order_type'] ?? 0,
  195. 'order_source' => $this->order_source,
  196. 'platform_id' => $sale['platform_name'] ?? '',
  197. 'platform_type' => $sale['use_type'] ?? '',
  198. 'good_code' => $sale['good_code'] ?? '',
  199. 'cat_id' => $sale['cat_id'] ?? 0,
  200. 'cat_name' => json_encode($this->_get_cat_list($sale['cat_id']), JSON_UNESCAPED_UNICODE),
  201. 'good_name' => $sale['good_name'] ?? '',
  202. 'good_num' => $sale['good_num'] ?? 0,
  203. 'good_type' => $sale['good_type'] ?? 0,
  204. 'is_activity' => $sale['is_activity'] ?? 0,
  205. 'is_stock' => $sale['is_stock'] ?? 0,
  206. 'arrive_time' => $sale['arrive_timefvc'] ?? '',
  207. 'customer_code' => $customer['No'] ?? '',
  208. 'customer_name' => $customer['name'] ?? '',
  209. 'supplierNo' => $supplier['No'] ?? '',
  210. 'supplier_name' => $supplier['name'] ?? '',
  211. 'zxNo' => $sale['zxNo'] ?? '',
  212. 'proof_id' => $sale['proof_id'] ?? 0,
  213. 'proof_url' => $sale['proof_url'] ?? '',
  214. 'other_orderNo' => $sale['other_orderNo'],
  215. 'paytime' => $sale['paytime'] ?? '',
  216. 'workNo' => $sale['workNo'] ?? '',
  217. 'poNo' => $sale['platform_order'] ?? '',
  218. 'use_order' => $sale['use_order'],
  219. 'good_weight' => $sale['good_weight'] ?? 0,
  220. 'addtime' => $sale['addtime'],
  221. 'noble_metal' => $sale['noble_metal'] ?? '',
  222. 'brand' => $sale['brand'] ?? '',
  223. 'unit' => $sale['unit'] ?? '',
  224. 'cost_desc' => $sale['cost_desc'] ?? '',
  225. 'noble_weight' => $sale['noble_weight'] ?? 0,
  226. 'tax' => $sale['tax'] ?? '',
  227. 'lead_time' => $sale['lead_time'] ?? 0,
  228. 'depart' => isset($sale['apply_id']) ? get_company_name_by_uid($sale['apply_id']) : '',
  229. 'cgdNo' => $cgdNo,
  230. 'total_origin_price' => round($sale['origin_price']*$sale['good_num'],2),
  231. 'total_origin_price_plan' => $sale['plan_total'],
  232. 'pay_id' => $sale['pay_id'],
  233. 'pay_source' => $supplier['pay_name'],
  234. 'oldCode' => $sale['orderCode'],
  235. ]);
  236. }
  237. }
  238. //构建采购单
  239. private function _handle_cgd_caixiao(array $cgd = [], array $sale = [], array $pay_rate = [], string $cgdNo = '', string $orderCode = '', float $cgd_total = 0.00)
  240. {
  241. $good_price = $sale['good_num'] > 0 ? bcdiv($cgd_total, $sale['good_num'], 5) : 0;
  242. $tmp_cgd = [
  243. 'good_price' => round($good_price, 2),
  244. 'total_fee' => $cgd_total,
  245. 'pakage_fee' => $cgd['pakge_fee'] ?? 0,
  246. 'open_fee' => $cgd['open_fee'] ?? 0,
  247. 'cert_fee' => $cgd['cert_fee'] ?? 0,
  248. 'delivery_fee' => $cgd['delivery_fee'] ?? 0,
  249. 'mark_fee' => $cgd['mark_fee'] ?? 0,
  250. 'teach_fee' => $cgd['teach_fee'] ?? 0,
  251. 'nake_fee' => $cgd['nake_fee'] ?? 0,
  252. 'demo_fee' => $cgd['demo_fee'] ?? 0,
  253. 'weight' => $cgd['weight'] ?? 0,
  254. 'diff_weight' => $sale['diff_weight'] ?? 0,
  255. 'diff_fee' => $sale['diff_fee'] ?? 0,
  256. 'gold_price' => $sale['gold_price'] ?? 0,
  257. 'send_num' => $sale['send_num'] ?? 0,
  258. 'wsend_num' => $sale['wsend_num'] ?? 0,
  259. 'status' => $cgd['status'] ?? '',
  260. 'order_type' => $sale['order_type'],
  261. 'order_source' => $this->order_source,
  262. 'good_type' => $sale['good_type'] ?? '',
  263. 'last_time' => $cgd['last_time'] ?? '',
  264. 'send_type' => $sale['send_type'] ?? '',
  265. 'send_status' => $sale['send_status'] ?? '',
  266. 'th_num' => $sale['th_num'] ?? 0,
  267. 'th_fee' => round(bcmul($good_price, $sale['th_num'] ?? 0, 3), 2),
  268. 'updatetime' => $sale['updatetime'],
  269. ];
  270. $tmp = Db::name('cgd_caixiao')
  271. ->field('id')
  272. ->where(['oldCode' => $sale['orderCode'], 'companyNo' => $pay_rate['companyNo']])
  273. ->findOrEmpty();
  274. if (!empty($tmp)) {
  275. Db::name('cgd_caixiao')
  276. ->where('id', $tmp['id'])
  277. ->update($tmp_cgd);
  278. } else {
  279. $this->cgd_insert[$this->cgd_key] = array_merge($tmp_cgd, [
  280. 'cgdNo' => $cgdNo,
  281. 'bkcode' => $cgd['bkcode'] ?? '',
  282. 'wsm_code' => $cgd['wsm_code'] ?? '',
  283. 'cgder' => $sale['cgder'] ?? '',
  284. 'cgder_id' => $sale['cgderid'] ?? 0,
  285. 'depart' => isset($sale['cgderid']) ? get_company_name_by_uid($sale['cgderid']) : '',
  286. 'qrdNo' => $orderCode,
  287. 'spuCode' => $cgd['spuCode'] ?? $sale['good_code'],
  288. 'good_name' => $sale['good_name'],
  289. 'skuCode' => $cgd['skuCode'] ?? '',
  290. 'good_num' => $sale['good_num'] ?? 0,
  291. 'cat_name' => json_encode($this->_get_cat_list($sale['cat_id']), JSON_UNESCAPED_UNICODE),
  292. 'companyNo' => $pay_rate['companyNo'],
  293. 'companyName' => $pay_rate['companyName'],
  294. 'supplierNo' => $sale['supplierNo'],
  295. 'supplier_name' => $sale['supplierName'],
  296. 'bktime' => $cgd['bktime'] ?? '',
  297. 'bkcreater' => $cgd['bkcreater'] ?? '',
  298. 'noble_metal' => $sale['noble_metal'] ?? '',
  299. 'brand' => $sale['brand'] ?? '',
  300. 'unit' => $sale['unit'] ?? '',
  301. 'cost_desc' => $sale['cost_desc'] ?? '',
  302. 'noble_weight' => $sale['noble_weight'] ?? '',
  303. 'tax' => $sale['tax'] ?? '',
  304. 'is_stock' => $sale['is_stock'],
  305. 'delivery_day' => $sale['delivery_day'] ?? 0,
  306. 'lead_time' => $sale['lead_time'] ?? 0,
  307. 'is_diff' => $sale['is_diff'],
  308. 'addtime' => $sale['addtime'],
  309. 'supplier_persion' => '',
  310. 'supplier_persionid' => '',
  311. 'pay_id' => $sale['pay_id'],
  312. 'oldCode' => $sale['orderCode']
  313. ]);
  314. $this->cgd_key++;
  315. }
  316. }
  317. //获取分类层级信息,id、cat_name(分类名称)和fund_code(财务核算码)
  318. private function _get_cat_list($var, $data = [])
  319. {
  320. $str = Db::name('cat')
  321. ->field('id,cat_name,fund_code,pid')
  322. ->where(['id' => $var])
  323. ->findOrEmpty();
  324. if ($str == false) return [];
  325. $vmn = [];
  326. $vmn['id'] = $str['id'];
  327. $vmn['cat_name'] = $str['cat_name'];
  328. $vmn['fund_code'] = $str['fund_code'];
  329. array_unshift($data, $vmn);
  330. if ($str['pid'] == 0) return $data;
  331. else return $this->_get_cat_list($str['pid'], $data);
  332. }
  333. }