SplitSale.php 17 KB

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