|
@@ -0,0 +1,360 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\command;
|
|
|
+
|
|
|
+use think\console\Command;
|
|
|
+use think\console\Input;
|
|
|
+use think\console\Output;
|
|
|
+use think\Exception;
|
|
|
+use think\facade\Db;
|
|
|
+
|
|
|
+class SplitSale extends Command
|
|
|
+{
|
|
|
+ private $i = 0;//各种编码的自增变量
|
|
|
+ private $date = '';//当前日期
|
|
|
+ private $sale_insert = [];//wsm_sale_caixiao的新增数据
|
|
|
+ private $cgd_insert = [];//wsm_cgd_caixiao的新增数据
|
|
|
+ private $noble_metal = [1 => '18K', 2 => '24K', 3 => '白银'];//贵金属种类对应文本
|
|
|
+
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ $this->date = date('Y-m-d H:i:s');
|
|
|
+ parent::__construct();
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function configure()
|
|
|
+ {
|
|
|
+ $this->setName('split_sale')->setDescription('销售订单拆分');
|
|
|
+ parent::configure();
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function execute(Input $input, Output $output)
|
|
|
+ {
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ Db::startTrans();
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ $data = Db::name('sale')
|
|
|
+ ->field(true)
|
|
|
+ ->where([
|
|
|
+ ['is_del', '=', 0],
|
|
|
+ ['updatetime', '>=', date('Y-m-d H:i:s', time() - 5 * 60)],
|
|
|
+ ['pay_id', '<>', 0]
|
|
|
+ ])
|
|
|
+ ->lock(true)
|
|
|
+ ->select()
|
|
|
+ ->toArray();
|
|
|
+
|
|
|
+ $all_orderCode = array_column($data, 'orderCode');
|
|
|
+
|
|
|
+ //已经拆分过的原订单号
|
|
|
+ $exist_sale_code = Db::name('sale_caixiao')
|
|
|
+ ->whereIn('oldCode', $all_orderCode)
|
|
|
+ ->column('id', 'oldCode');
|
|
|
+
|
|
|
+ foreach ($data as $sale) {
|
|
|
+
|
|
|
+ //补充商品信息
|
|
|
+ if ($sale['order_type'] == 3) {
|
|
|
+ //咨询相关
|
|
|
+ $good = Db::name('consult_bids')
|
|
|
+ ->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')
|
|
|
+ ->alias('a')
|
|
|
+ ->leftJoin('good_basic b', 'b.is_del=0 AND b.spuCode=a.spuCode')
|
|
|
+ ->leftJoin('brand c', 'c.id=a.brand_id')
|
|
|
+ ->leftJoin('unit d', 'd.id=a.unit_id')
|
|
|
+ ->where(['a.is_del' => 0, 'a.bidNo' => $sale['zxNo']])
|
|
|
+ ->findOrEmpty();
|
|
|
+ } elseif ($sale['order_type'] == 4) {
|
|
|
+ //报备单
|
|
|
+ $good = Db::name('filing')
|
|
|
+ ->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')
|
|
|
+ ->alias('a')
|
|
|
+ ->leftJoin('brand c', 'c.id=a.brand_id')
|
|
|
+ ->leftJoin('unit d', 'd.id=a.unit_id')
|
|
|
+ ->where(['a.is_del' => 0, 'a.orderCode' => $sale['orderCode']])
|
|
|
+ ->findOrEmpty();
|
|
|
+ } else {
|
|
|
+ $good = Db::name('good')
|
|
|
+ ->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')
|
|
|
+ ->alias('b')
|
|
|
+ ->leftJoin('brand c', 'c.id=b.brand_id')
|
|
|
+ ->leftJoin('unit d', 'd.id=b.good_unit')
|
|
|
+ ->where(['b.is_del' => 0, 'b.spuCode' => $sale['good_code']])
|
|
|
+ ->findOrEmpty();
|
|
|
+ }
|
|
|
+
|
|
|
+ //贵金属分类转换文本
|
|
|
+ $good['noble_metal'] = $this->noble_metal[$good['noble_metal']] ?? '';
|
|
|
+
|
|
|
+ //相关数据合并
|
|
|
+ $sale = array_merge($sale, $good);
|
|
|
+
|
|
|
+ //订单回款通道各公司税点
|
|
|
+ $pay_rates = Db::name('pay_rate')
|
|
|
+ ->where(['is_del' => 0, 'status' => 1, 'pay_id' => $sale['pay_id']])
|
|
|
+ ->field(true)
|
|
|
+ ->order(['weight' => 'desc'])
|
|
|
+ ->cursor();
|
|
|
+
|
|
|
+ //关联的采购单信息
|
|
|
+ $cgd = Db::name('order_num')
|
|
|
+ ->alias('a')
|
|
|
+ ->field('b.*,c.addtime bktime,c.apply_id bkcreater')
|
|
|
+ ->leftJoin('purchease_order b', 'b.cgdNo=a.cgdNo')
|
|
|
+ ->leftJoin('purchease c', 'c.bk_code=b.bkcode')
|
|
|
+ ->where('a.orderCode', $sale['orderCode'])
|
|
|
+ ->findOrEmpty();
|
|
|
+
|
|
|
+ foreach ($pay_rates as $pay_rate) {
|
|
|
+
|
|
|
+ //采购总金额和销售总金额
|
|
|
+ $cgd_total = $sale_total = $cgd['total_fee'] ?? $sale['total_price'];
|
|
|
+
|
|
|
+ //生成新的采购单号和销售单号
|
|
|
+ $cgdNo = makeNo('CG');
|
|
|
+ $cgdNo = substr($cgdNo, 0, -2) . str_pad($this->i, 2, '0', STR_PAD_LEFT);
|
|
|
+ $orderCode = makeNo('QR');
|
|
|
+ $orderCode = substr($orderCode, 0, -2) . str_pad($this->i, 2, '0', STR_PAD_LEFT);
|
|
|
+
|
|
|
+ $this->i++;
|
|
|
+
|
|
|
+ //只处理采购单
|
|
|
+ if (strtoupper($pay_rate['companyNo']) == 'KH') {
|
|
|
+ $pay_rate['companyNo'] = $sale['customer_code'];
|
|
|
+ $pay_rate['companyName'] = Db::name('customer_info')
|
|
|
+ ->where('companyNo', $sale['customer_code'])
|
|
|
+ ->value('companyName', '');
|
|
|
+
|
|
|
+ if ($pay_rate['is_cgd'] == 1) $this->_handle_cgd_caixiao($cgd, $sale, $pay_rate, $cgdNo, $orderCode, $cgd_total);
|
|
|
+
|
|
|
+ } elseif (strtoupper($pay_rate['companyNo']) == 'GYS') {
|
|
|
+ //只处理销售单
|
|
|
+ $pay_rate['companyNo'] = $sale['supplierNo'];
|
|
|
+ $pay_rate['companyName'] = Db::name('business')
|
|
|
+ ->where('companyNo', $sale['supplierNo'])
|
|
|
+ ->value('company', '');
|
|
|
+ if ($pay_rate['is_qrd'] == 1) $this->_handle_sale_caixiao($exist_sale_code, $sale, $orderCode, $cgdNo, $pay_rate, $sale_total);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ //需要生成销售单
|
|
|
+ if ($pay_rate['is_qrd'] == 1) $this->_handle_sale_caixiao($exist_sale_code, $sale, $orderCode, $cgdNo, $pay_rate, $sale_total);
|
|
|
+
|
|
|
+ //需要生成采购单
|
|
|
+ if ($pay_rate['is_cgd'] == 1) $this->_handle_cgd_caixiao($cgd, $sale, $pay_rate, $cgdNo, $orderCode, $cgd_total);
|
|
|
+ }
|
|
|
+
|
|
|
+ $sale_total = $cgd_total;
|
|
|
+ $cgd_total = round(bcsub($cgd_total, bcmul($cgd['total_fee'], $pay_rate['rate'], 3), 3), 2);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($this->cgd_insert) Db::name('cgd_caixiao')->insertAll($this->cgd_insert);
|
|
|
+ if ($this->sale_insert) Db::name('sale_caixiao')->insertAll($this->sale_insert);
|
|
|
+
|
|
|
+ Db::commit();
|
|
|
+
|
|
|
+ $output->writeln('处理完成');
|
|
|
+
|
|
|
+ } catch (Exception $e) {
|
|
|
+ Db::rollback();
|
|
|
+ $output->writeln('事务回滚:' . $e->getMessage() . '||' . $e->getFile() . '||' . $e->getLine());
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception $exception) {
|
|
|
+ $output->writeln('脚本执行出错,' . $exception->getMessage() . '||' . $exception->getFile() . '||' . $exception->getLine());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //构建销售单
|
|
|
+ private function _handle_sale_caixiao(array $exist_sale_code = [], array $sale = [], string $orderCode = '', string $cgdNo = '', array $pay_rate = [], float $sale_total = 0.00)
|
|
|
+ {
|
|
|
+
|
|
|
+ $tmp_sale = [
|
|
|
+ 'origin_price' => $sale['origin_price'] ?? 0,
|
|
|
+ 'sale_price' => $sale['good_num'] > 0 ? round(bcdiv($sale_total, $sale['good_num'], 1), 0) : 0,
|
|
|
+ 'total_price' => $sale_total,
|
|
|
+ 'post_fee' => $sale['post_fee'] ?? 0,
|
|
|
+ 'is_diff' => $sale['is_diff'] ?? 0,
|
|
|
+ 'send_num' => $sale['send_num'] ?? 0,
|
|
|
+ 'wsend_num' => $sale['wsend_num'] ?? 0,
|
|
|
+ 'th_num' => $sale['th_num'] ?? 0,
|
|
|
+ 'send_type' => $sale['send_type'] ?? 0,
|
|
|
+ 'gold_price' => $sale['gold_price'] ?? 0,
|
|
|
+ 'cost_price' => $sale['cost_price'] ?? 0,
|
|
|
+ 'status' => $sale['status'] ?? 0,
|
|
|
+ 'updatetime' => $this->date,
|
|
|
+ 'delivery_day' => $sale['delivery_day'] ?? 0,
|
|
|
+ 'th_fee' => $sale['th_fee'] ?? 0,
|
|
|
+ 'cost_fee' => $sale['cost_price'] ?? 0,
|
|
|
+ 'diff_fee' => $sale['diff_fee'] ?? 0,
|
|
|
+ 'diff_weight' => $sale['diff_weight'] ?? 0,
|
|
|
+ 'send_status' => $sale['send_status'] ?? 0,
|
|
|
+ ];
|
|
|
+
|
|
|
+ if (isset($exist_sale_code[$sale['orderCode']])) {
|
|
|
+ Db::name('sale_caixiao')
|
|
|
+ ->where('id', $exist_sale_code[$sale['orderCode']])
|
|
|
+ ->update($tmp_sale);
|
|
|
+ } else {
|
|
|
+ $supplier_name = Db::name('business')
|
|
|
+ ->where('companyNo', $sale['supplierNo'] ?? '')
|
|
|
+ ->value('company', '');
|
|
|
+ $this->sale_insert[] = array_merge($tmp_sale, [
|
|
|
+ 'orderCode' => $orderCode,
|
|
|
+ 'apply_id' => $sale['apply_id'] ?? 0,
|
|
|
+ 'apply_name' => $sale['apply_name'] ?? '',
|
|
|
+ 'order_type' => $sale['order_type'] ?? 0,
|
|
|
+ 'order_source' => $sale['order_source'] ?? 0,
|
|
|
+ 'platform_id' => $sale['platform_id'] ?? 0,
|
|
|
+ 'good_code' => $sale['good_code'] ?? '',
|
|
|
+ 'cat_id' => $sale['cat_id'] ?? 0,
|
|
|
+ 'cat_name' => json_encode($this->_get_cat_list($sale['cat_id']), JSON_UNESCAPED_UNICODE),
|
|
|
+ 'good_name' => $sale['good_name'] ?? '',
|
|
|
+ 'good_num' => $sale['good_num'] ?? 0,
|
|
|
+ 'good_type' => $sale['good_type'] ?? 0,
|
|
|
+ 'is_activity' => $sale['is_activity'] ?? 0,
|
|
|
+ 'is_stock' => $sale['is_stock'] ?? 0,
|
|
|
+ 'arrive_time' => $sale['arrive_timefvc'] ?? '',
|
|
|
+ 'customer_code' => $pay_rate['companyNo'],
|
|
|
+ 'customer_name' => $pay_rate['companyName'],
|
|
|
+ 'supplierNo' => $sale['supplierNo'] ?? '',
|
|
|
+ 'supplier_name' => $supplier_name,
|
|
|
+ 'zxNo' => $sale['zxNo'] ?? '',
|
|
|
+ 'proof_id' => $sale['proof_id'] ?? 0,
|
|
|
+ 'proof_url' => Db::name('good_proof')->where('id', $sale['proof_id'] ?? 0)->value('proof_url', ''),
|
|
|
+ 'other_orderNo' => $sale['other_orderNo'],
|
|
|
+ 'paytime' => $sale['paytime'] ?? '',
|
|
|
+ 'workNo' => $sale['workNo'] ?? '',
|
|
|
+ 'poNo' => $sale['poNo'] ?? '',
|
|
|
+ 'use_order' => $sale['use_order'],
|
|
|
+ 'good_weight' => $sale['good_weight'] ?? 0,
|
|
|
+ 'addtime' => $this->date,
|
|
|
+ 'noble_metal' => $sale['noble_metal'] ?? '',
|
|
|
+ 'brand' => $sale['brand'] ?? '',
|
|
|
+ 'unit' => $sale['unit'] ?? '',
|
|
|
+ 'cost_desc' => $sale['cost_desc'] ?? '',
|
|
|
+ 'noble_weight' => $sale['noble_weight'] ?? 0,
|
|
|
+ 'tax' => $sale['tax'] ?? '',
|
|
|
+ 'lead_time' => $sale['lead_time'] ?? 0,
|
|
|
+ 'depart' => get_company_name_by_uid($sale['apply_id'] ?? 0) ?? '',
|
|
|
+ 'cgdNo' => $cgdNo,
|
|
|
+ 'pay_id' => $sale['pay_id'],
|
|
|
+ 'oldCode' => $sale['orderCode'],
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //构建采购单
|
|
|
+ private function _handle_cgd_caixiao(array $cgd = [], array $sale = [], array $pay_rate = [], string $cgdNo = '', string $orderCode = '', float $cgd_total = 0.00)
|
|
|
+ {
|
|
|
+
|
|
|
+ $tmp_cgd = [
|
|
|
+ 'good_price' => $cgd['good_price'] ?? '',
|
|
|
+ 'total_fee' => $cgd_total,
|
|
|
+ 'pakage_fee' => $cgd['pakge_fee'] ?? 0,
|
|
|
+ 'open_fee' => $cgd['open_fee'] ?? 0,
|
|
|
+ 'cert_fee' => $cgd['cert_fee'] ?? 0,
|
|
|
+ 'delivery_fee' => $cgd['delivery_fee'] ?? 0,
|
|
|
+ 'mark_fee' => $cgd['mark_fee'] ?? 0,
|
|
|
+ 'teach_fee' => $cgd['teach_fee'] ?? 0,
|
|
|
+ 'nake_fee' => $cgd['nake_fee'] ?? 0,
|
|
|
+ 'demo_fee' => $cgd['demo_fee'] ?? 0,
|
|
|
+ 'weight' => $cgd['weight'] ?? 0,
|
|
|
+ 'diff_weight' => $cgd['diff_weight'] ?? 0,
|
|
|
+ 'diff_fee' => $cgd['diff_fee'] ?? 0,
|
|
|
+ 'gold_price' => $cgd['gold_price'] ?? 0,
|
|
|
+ 'send_num' => $cgd['send_num'] ?? 0,
|
|
|
+ 'wsend_num' => $cgd['wsend_num'] ?? 0,
|
|
|
+ 'status' => $cgd['status'] ?? '',
|
|
|
+ 'order_type' => $cgd['order_type'] ?? '',
|
|
|
+ 'order_source' => $cgd['order_source'] ?? '',
|
|
|
+ 'good_type' => $cgd['order_source'] ?? '',
|
|
|
+ 'last_time' => $cgd['last_time'] ?? '',
|
|
|
+ 'send_type' => $sale['send_type'] ?? '',
|
|
|
+ 'send_status' => $sale['send_status'] ?? '',
|
|
|
+ 'th_num' => $cgd['th_num'] ?? 0,
|
|
|
+ 'th_fee' => $cgd['th_fee'] ?? 0,
|
|
|
+ 'updatetime' => $this->date,
|
|
|
+ ];
|
|
|
+
|
|
|
+ if (isset($exist_sale_code[$sale['orderCode']])) {
|
|
|
+ Db::name('cgd_caixiao')
|
|
|
+ ->where([
|
|
|
+ 'oldCode' => $sale['orderCode'],
|
|
|
+ 'companyNo' => $pay_rate['companyNo']
|
|
|
+ ])
|
|
|
+ ->update($tmp_cgd);
|
|
|
+ } else {
|
|
|
+
|
|
|
+ $supplier = Db::name('supplier')
|
|
|
+ ->field('id,name,code,person,personid')
|
|
|
+ ->where('code', $cgd['supplierNo'] ?? '')
|
|
|
+ ->findOrEmpty();
|
|
|
+
|
|
|
+ $this->cgd_insert[] = array_merge($tmp_cgd, [
|
|
|
+ 'cgdNo' => $cgdNo,
|
|
|
+ 'bkcode' => $cgd['bkcode'] ?? '',
|
|
|
+ 'wsm_code' => $cgd['wsm_code'] ?? '',
|
|
|
+ 'cgder' => $cgd['cgder'] ?? '',
|
|
|
+ 'cgder_id' => $cgd['cgder_id'] ?? 0,
|
|
|
+ 'depart' => get_company_name_by_uid($cgd['cgder_id'] ?? 0) ?? '',
|
|
|
+ 'qrdNo' => $orderCode,
|
|
|
+ 'spuCode' => $cgd['spuCode'] ?? '',
|
|
|
+ 'good_name' => $cgd['good_name'] ?? '',
|
|
|
+ 'skuCode' => $cgd['skuCode'] ?? '',
|
|
|
+ 'good_num' => $cgd['good_num'] ?? '',
|
|
|
+ 'cat_name' => json_encode($this->_get_cat_list($sale['cat_id']), JSON_UNESCAPED_UNICODE),
|
|
|
+ 'companyNo' => $pay_rate['companyNo'],
|
|
|
+ 'companyName' => $pay_rate['companyName'],
|
|
|
+ 'supplierNo' => $cgd['supplierNo'] ?? '',
|
|
|
+ 'supplier_name' => $cgd['supplier_name'] ?? '',
|
|
|
+ 'bktime' => $cgd['bktime'] ?? '',
|
|
|
+ 'bkcreater' => $cgd['bkcreater'] ?? '',
|
|
|
+ 'noble_metal' => $sale['noble_metal'] ?? '',
|
|
|
+ 'brand' => $sale['brand'] ?? '',
|
|
|
+ 'unit' => $sale['unit'] ?? '',
|
|
|
+ 'cost_desc' => $sale['cost_desc'] ?? '',
|
|
|
+ 'noble_weight' => $sale['noble_weight'] ?? '',
|
|
|
+ 'tax' => $sale['tax'] ?? '',
|
|
|
+ 'is_stock' => $sale['is_stock'],
|
|
|
+ 'delivery_day' => $sale['delivery_day'] ?? 0,
|
|
|
+ 'lead_time' => $sale['lead_time'] ?? 0,
|
|
|
+ 'is_diff' => $sale['is_diff'],
|
|
|
+ 'addtime' => $this->date,
|
|
|
+ 'supplier_persion' => $supplier['person'],
|
|
|
+ 'supplier_persionid' => $supplier['personid'],
|
|
|
+ 'pay_id' => $sale['pay_id'],
|
|
|
+ 'oldCode' => $sale['orderCode']
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //获取分类层级信息,id、cat_name(分类名称)和fund_code(财务核算码)
|
|
|
+ private function _get_cat_list($var, $data = [])
|
|
|
+ {
|
|
|
+ $str = Db::name('cat')
|
|
|
+ ->field('id,cat_name,fund_code,pid')
|
|
|
+ ->where(['id' => $var])
|
|
|
+ ->findOrEmpty();
|
|
|
+ if ($str == false) return [];
|
|
|
+
|
|
|
+ $vmn = [];
|
|
|
+ $vmn['id'] = $str['id'];
|
|
|
+ $vmn['name'] = $str['cat_name'];
|
|
|
+ $vmn['fund_code'] = $str['fund_code'];
|
|
|
+ array_unshift($data, $vmn);
|
|
|
+ if ($str['pid'] == 0) return $data;
|
|
|
+ else return $this->_get_cat_list($str['pid'], $data);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|