123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- <?php
- namespace app\command;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\Exception;
- use think\facade\Cache;
- use think\facade\Db;
- class SplitSale extends Command
- {
- private $i = 0;//各种编码的自增变量
- private $sale_insert = [];//wsm_sale_caixiao的新增数据
- private $cgd_insert = [];//wsm_cgd_caixiao的新增数据
- private $noble_metal = [1 => '18K', 2 => '24K', 3 => '白银'];//贵金属种类对应文本
- private $cgd_key = 0;//新增到wsm_cgd_caixiao的数组下标,从0开始
- private $order_source = 8;//支付渠道
- protected function configure()
- {
- $this->setName('split_sale')->setDescription('销售订单拆分');
- parent::configure();
- }
- protected function execute(Input $input, Output $output)
- {
- $date=date("Y-m-d H:i:s");
- try {
- $key = 'split_sale_';
- $rs = Cache::store('redis')->get($key);
- if ($rs) return true;
- Cache::store('redis')->set($key, 1, 60 * 5);
- Db::startTrans();
- try {
- $data = Db::name('sale')
- ->alias('a')
- ->leftJoin('platform b', 'b.id=a.platform_id')
- ->leftJoin('good_proof c', 'c.id=a.proof_id')
- ->field('a.*,b.platform_name,c.proof_url')
- ->where([
- ['a.is_del', '=', 0],
- ['a.updatetime', '>=', date('Y-m-d H:i:s', time() - 5 * 60)],
- ['a.pay_id', '<>', 0]
- ])
- ->cursor();
- // $cgd_insert_tmp_data=[];
- 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'] ?? 0] ?? '';
- //相关数据合并
- $sale = array_merge($sale, $good);
- //支付渠道相关信息
- $pay_rates = Db::name('pay_log')
- ->where(['is_del' => 0, 'pay_id' => $sale['pay_id'], 'orderCode' => $sale['orderCode']])
- ->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();
- //采购总金额和销售总金额
- $cgd_total = $sale_total = $sale['total_price'];
- //客户
- $customer = ['No' => $sale['customer_code'], 'name' => Db::name('customer_info')->where('companyNo', $sale['customer_code'])->value('companyName', '')];
- //供应商
- $supplier = ['No' => $sale['supplierNo'], 'name' => Db::name('business')->where('companyNo', $sale['supplierNo'])->value('company', '')];
- foreach ($pay_rates as $pay_rate) {
- //生成新的采购单号和销售单号
- $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++;
- $sale_total = $cgd_total;
- $cgd_total = bcsub($cgd_total, bcmul($sale['total_price'] ?? 0, $pay_rate['rate'], 3), 2);
- //只处理采购单
- if (strtoupper($pay_rate['companyNo']) == 'KH') {
- //此时尚未生成销售单,存销售单号无意义 吧?
- // if ($pay_rate['is_cgd'] == 1) $this->_handle_cgd_caixiao($cgd, $sale, $pay_rate, $cgdNo, $orderCode, $cgd_total);
- if ($pay_rate['is_cgd'] == 1) $this->_handle_cgd_caixiao($cgd, $sale, $pay_rate, $cgdNo, '', $cgd_total);
- } elseif (strtoupper($pay_rate['companyNo']) == 'GYS') {
- //只处理销售单
- //供应商事先覆盖
- $supplier = ['No' => $sale['supplierNo'], 'name' => Db::name('business')->where('companyNo', $sale['supplierNo'])->value('company', '')];
- if ($pay_rate['is_qrd'] == 1) {
- //此时生成的供应商的销售单,应该关联原始采购单号
- $this->_handle_sale_caixiao($sale, $orderCode, $cgd['cgdNo']??'', $sale_total,$customer, $supplier);
- }
- } else {
- //供应商事先覆盖
- $supplier = ['No' => $pay_rate['companyNo'], 'name' => $pay_rate['companyName']];
- //需要生成销售单
- if ($pay_rate['is_qrd'] == 1) $this->_handle_sale_caixiao($sale, $orderCode, $cgdNo, $sale_total, $customer, $supplier);
- //需要生成采购单
- if ($pay_rate['is_cgd'] == 1) $this->_handle_cgd_caixiao($cgd, $sale, $pay_rate, $cgdNo, $orderCode, $cgd_total);
- //客户事后覆盖
- $customer = ['No' => $pay_rate['companyNo'], 'name' => $pay_rate['companyName']];
- }
- //把自己覆盖到上一个记录的供应商记录中
- if (isset($this->cgd_insert[$this->cgd_key - 1])) {
- if (strtoupper($pay_rate['companyNo']) == 'KH') continue;
- elseif (strtoupper($pay_rate['companyNo']) == 'GYS') $supplierNo = $sale['supplierNo'];
- else $supplierNo = $pay_rate['companyNo'];
- $supplier = Db::name('supplier')
- ->field('id,code,name,person,personid')
- ->where('code', $supplierNo)
- ->findOrEmpty();
- $this->cgd_insert[$this->cgd_key - 1]['supplierNo'] = $supplierNo;
- $this->cgd_insert[$this->cgd_key - 1]['supplier_name'] = $supplier['name'] ?? '';
- $this->cgd_insert[$this->cgd_key - 1]['supplier_persion'] = $supplier['person'] ?? '';
- $this->cgd_insert[$this->cgd_key - 1]['supplier_persionid'] = $supplier['personid'] ?? 0;
- if(isset($supplier['personid'])) $this->cgd_insert[$this->cgd_key - 1]['depart']=get_company_name_by_uid($supplier['personid'] ?? 0);
- }
- }
- //清空该变量,以防止多个销售单覆盖数据的情况
- // $cgd_insert_tmp_data = array_merge($cgd_insert_tmp_data, $this->cgd_insert);
- //批量新增改为单次新增
- if ($this->cgd_insert) Db::name('cgd_caixiao')->insertAll($this->cgd_insert);
- $this->cgd_insert=[];
- $this->cgd_key=0;
- $output->writeln( "[$date] 订单{$sale['orderCode']}处理结束");
- }
- // if ($cgd_insert_tmp_data) Db::name('cgd_caixiao')->insertAll($cgd_insert_tmp_data);
- if ($this->sale_insert) Db::name('sale_caixiao')->insertAll($this->sale_insert);
- Db::commit();
- $output->writeln( "[$date] 订单处理结束");
- } catch (Exception $e) {
- Db::rollback();
- $output->writeln( "[$date] 事务回滚:" . $e->getMessage() . '||' . $e->getFile() . '||' . $e->getLine());
- }
- Cache::store('redis')->set($key, 0);
- } catch (Exception $exception) {
- $output->writeln( "[$date] 脚本执行出错:" . $exception->getMessage() . '||' . $exception->getFile() . '||' . $exception->getLine());
- }
- }
- //构建销售单
- private function _handle_sale_caixiao(array $sale = [], string $orderCode = '', string $cgdNo = '', float $sale_total = 0.00, array $customer = [], array $supplier = [])
- {
- $sale_price = $sale['good_num'] > 0 ? bcdiv($sale_total, $sale['good_num'], 3) : 0;
- $tmp_sale = [
- 'origin_price' => $sale['origin_price'] ?? 0,
- 'sale_price' => round($sale_price, 2),
- 'total_price' => $sale_total,
- 'customer_code' => $customer['No'] ?? '',
- 'customer_name' => $customer['name'] ?? '',
- 'supplierNo' => $supplier['No'] ?? '',
- 'supplier_name' => $supplier['name'] ?? '',
- 'tax' => $sale['tax'] ?? '',
- '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,
- 'workNo' => $sale['workNo'] ?? '',
- 'poNo' => $sale['platform_order'] ?? '',
- 'updatetime' => $sale['updatetime'],
- 'delivery_day' => $sale['delivery_day'] ?? 0,
- 'th_fee' => round(bcmul($sale_price, $sale['th_num'] ?? 0, 3), 2),
- 'cost_fee' => $sale['cost_price'] ?? 0,
- 'diff_fee' => $sale['diff_fee'] ?? 0,
- 'diff_weight' => $sale['diff_weight'] ?? 0,
- 'send_status' => $sale['send_status'] ?? 0,
- ];
- $tmp = Db::name('sale_caixiao')
- ->field('id')
- ->where(['oldCode' => $sale['orderCode'], 'customer_code' => $customer['No']])
- ->findOrEmpty();
- if (!empty($tmp)) {
- Db::name('sale_caixiao')
- ->where('id', $tmp['id'])
- ->update($tmp_sale);
- } else {
- $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' => $this->order_source,
- 'platform_id' => $sale['platform_name'] ?? '',
- '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'] ?? '',
- 'zxNo' => $sale['zxNo'] ?? '',
- 'proof_id' => $sale['proof_id'] ?? 0,
- 'proof_url' => $sale['proof_url'] ?? '',
- 'other_orderNo' => $sale['other_orderNo'],
- 'paytime' => $sale['paytime'] ?? '',
- 'use_order' => $sale['use_order'],
- 'good_weight' => $sale['good_weight'] ?? 0,
- 'addtime' => $sale['addtime'],
- 'noble_metal' => $sale['noble_metal'] ?? '',
- 'brand' => $sale['brand'] ?? '',
- 'unit' => $sale['unit'] ?? '',
- 'cost_desc' => $sale['cost_desc'] ?? '',
- 'noble_weight' => $sale['noble_weight'] ?? 0,
- '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)
- {
- $good_price = $sale['good_num'] > 0 ? bcdiv($cgd_total, $sale['good_num'], 3) : 0;
- $tmp_cgd = [
- 'cgder' => $sale['cgder'] ?? '',
- 'cgder_id' => $sale['cgderid'] ?? 0,
- 'good_price' =>round($good_price,2),
- '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' => $sale['diff_weight'] ?? 0,
- 'diff_fee' => $sale['diff_fee'] ?? 0,
- 'gold_price' => $sale['gold_price'] ?? 0,
- 'send_num' => $sale['send_num'] ?? 0,
- 'wsend_num' => $sale['wsend_num'] ?? 0,
- 'status' => $cgd['status'] ?? '',
- 'order_type' => $sale['order_type'],
- 'order_source' => $this->order_source,
- 'good_type' => $sale['good_type'] ?? '',
- 'last_time' => $cgd['last_time'] ?? '',
- 'send_type' => $sale['send_type'] ?? '',
- 'send_status' => $sale['send_status'] ?? '',
- 'tax' => $sale['tax'] ?? '',
- 'th_num' => $sale['th_num'] ?? 0,
- 'th_fee' => round(bcmul($good_price, $sale['th_num'] ?? 0, 3), 2),
- 'updatetime' => $sale['updatetime'],
- ];
- $tmp = Db::name('cgd_caixiao')
- ->field('id')
- ->where(['oldCode' => $sale['orderCode'], 'companyNo' => $pay_rate['companyNo']])
- ->findOrEmpty();
- if (!empty($tmp)) {
- Db::name('cgd_caixiao')
- ->where('id', $tmp['id'])
- ->update($tmp_cgd);
- } else {
- $this->cgd_insert[$this->cgd_key] = array_merge($tmp_cgd, [
- 'cgdNo' => $cgdNo,
- 'bkcode' => $cgd['bkcode'] ?? '',
- 'wsm_code' => $cgd['wsm_code'] ?? '',
- 'depart' => get_company_name_by_uid($sale['cgder_id'] ?? 0) ?? '',
- 'qrdNo' => $orderCode,
- 'spuCode' => $cgd['spuCode'] ?? $sale['good_code'],
- 'good_name' => $sale['good_name'],
- 'skuCode' => $cgd['skuCode'] ?? '',
- 'good_num' => $sale['good_num'] ?? 0,
- 'cat_name' => json_encode($this->_get_cat_list($sale['cat_id']), JSON_UNESCAPED_UNICODE),
- 'companyNo' => $pay_rate['companyNo'],
- 'companyName' => $pay_rate['companyName'],
- 'supplierNo' => '',
- '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' => $sale['addtime'],
- 'supplier_persion' => '',
- 'supplier_persionid' => '',
- 'pay_id' => $sale['pay_id'],
- 'oldCode' => $sale['orderCode']
- ]);
- $this->cgd_key++;
- }
- }
- //获取分类层级信息,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['cat_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);
- }
- }
|