123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- <?php
- namespace app\admin\controller;
- use app\admin\model\OrderImportFromC as OIFCModel;
- use Exception;
- use think\facade\Config;
- use think\facade\Db;
- use think\facade\Validate;
- //C端订单导入及处理类
- class OrderImport extends Base
- {
- //订单导入
- public function import()
- {
- $param = $this->request->only(['token', 'list'], 'post', 'trim');
- $val_params = Validate::rule(['token' => 'require', 'list' => 'require|array']);
- if (!$val_params->check($param)) throw new \think\Exception($val_params->getError());
- Db::startTrans();
- try {
- $userinfo = GetUserInfo($param['token']);
- $createrid = isset($user["data"]['id']) ? $userinfo["data"]['id'] : 0;
- $creater = isset($user["data"]['nickname']) ? $userinfo["data"]['nickname'] : "";
- $insert_data = [];
- foreach ($param['list'] as $key => $value) {
- $value['createrid'] = $createrid;
- $value['creater'] = $creater;
- $insert_data[] = $value;
- }
- $res = Db::name('order_import_from_c')
- ->limit(100)
- ->insertAll($insert_data);
- if ($res) {
- Db::commit();
- return app_show(0, "导入成功,共" . (string)$res . '条记录');
- } else {
- Db::rollback();
- return error_show(1005, '导入失败');
- }
- } catch (Exception $exception) {
- Db::rollback();
- return error_show(1005, $exception->getMessage());
- }
- }
- //查看订单录入列表
- public function getImportList()
- {
- $param = $this->request->only([
- 'status' => 0,
- 'platform_code' => '',
- 'po_code' => '',
- 'platform_time_start' => '',
- 'platform_time_end' => '',
- 'addtime_start' => '',
- 'addtime_end' => '',
- 'plat_code' => '',
- 'page' => 1,
- 'size' => 15,
- ], 'post', 'trim');
- $where = [];
- if ($param['status']) $where[] = ['c.status', '=', $param['status']];
- if ($param['platform_code']) $where[] = ['c.platform_code', 'like', '%' . $param['platform_code'] . '%'];
- if ($param['po_code']) $where[] = ['c.po_code', 'like', '%' . $param['po_code'] . '%'];
- if ($param['platform_time_start'] && $param['platform_time_end']) $where[] = ['c.platform_time', 'between', [$param['platform_time_start'], $param['platform_time_end']]];
- if ($param['addtime_start'] && $param['addtime_end']) $where[] = ['c.addtime', 'between', [$param['addtime_start'], $param['addtime_end']]];
- if ($param['plat_code']) $where[] = ['c.plat_code', 'like', '%' . $param['plat_code'] . '%'];
- $db = OIFCModel::alias('c')->where($where);
- $total = $db->count('id');
- $list = $db
- ->page($param['page'], $param['size'])
- ->order('id', 'desc')
- ->select()
- ->toArray();
- return app_show(0, "获取成功", ['list' => $list, 'count' => $total]);
- }
- //查看订单录入详情
- public function getImportInfo()
- {
- $id = $this->request->post('id/d', 0, 'trim');
- $info = OIFCModel::alias('c')
- ->field('c.*,ce.*,b.company,ci.companyName CustomerCompanyName,s.name supplierName,a.activity_code')
- ->leftJoin('order_import_from_c_extend ce', 'ce.order_import_from_c_id=c.id')
- ->leftJoin('business b', 'b.companyNo=ce.companyNo AND b.is_del=0')
- ->leftJoin('customer_info ci', 'ci.companyNo=ce.customer_code AND ci.is_del=0')
- ->leftJoin('supplier s', 's.code=ce.supplierNo AND s.is_del=0')
- ->leftJoin('activity_info a', 'a.id=ce.activity_id AND a.is_del=0')
- ->where('c.id', $id)
- ->find();
- if (!empty($info)) {
- $info['platform_names'] = Db::name('platform')
- ->whereIn('platform_code', $info['platform_codes'])
- ->where(['is_del' => 0, 'status' => 1])
- ->column('platform_name');
- $info['addr_name'] = GetAddr($info['addr_code']);
- $res['id'] = $info['id'];
- $res['status'] = $info['status'];
- $res['createrid'] = $info['createrid'];
- $res['creater'] = $info['creater'];
- $res['updateid'] = $info['updateid'];
- $res['updater'] = $info['updater'];
- //导入字段
- $res['import'] = [
- 'platform_code' => $info['platform_code'],
- 'platform_time' => $info['platform_time'],
- 'plat_code' => $info['plat_code'],
- 'sale_source' => $info['sale_source'],
- 'price' => $info['price'],
- 'num' => $info['num'],
- 'before_discount_all_price' => $info['before_discount_all_price'],
- 'activity_name' => $info['activity_name'],
- 'mode' => $info['mode'],
- 'order_remark' => $info['order_remark'],
- 'contactor' => $info['contactor'],
- 'mobile' => $info['mobile'],
- 'addr' => $info['addr'],
- ];
- //确认字段
- $res['confirm'] = $info;
- //用户编辑字段
- $user_update = Db::name('order_import_from_c_extend')
- ->alias('ce')
- ->field('ce.*,b.company,ci.companyName CustomerCompanyName,s.name supplierName,a.activity_code')
- ->leftJoin('business b', 'b.companyNo=ce.companyNo AND b.is_del=0')
- ->leftJoin('customer_info ci', 'ci.companyNo=ce.customer_code AND ci.is_del=0')
- ->leftJoin('supplier s', 's.code=ce.supplierNo AND s.is_del=0')
- ->leftJoin('activity_info a', 'a.id=ce.activity_id AND a.is_del=0')
- ->where(['ce.order_import_from_c_id' => $id, 'ce.is_del' => 1])
- ->order('ce.addtime', 'desc')
- ->find();
- if (empty($user_update)) $res['user_update'] = $res['confirm'];
- else $res['user_update'] = array_merge($res['confirm'], $user_update);
- return app_show(0, '请求成功', $res);
- } else return error_show(1005, '系统尚未解析完成,无法查看详情,请等待');
- }
- //用户确认订单数据(编辑)
- public function confirmImportData()
- {
- //每次编辑,都新增一条记录到extend表,同时原纪录作废,确保同一个order_import_from_c_id下只有一个正常的记录
- $param = $this->request->only([
- 'id',
- 'platform_code',
- 'po_code',
- 'platform_time',
- 'sale_source',
- 'plat_code',
- 'price',
- 'num',
- 'before_discount_all_price',
- 'activity_id',
- 'activity_name',
- 'mode',
- 'order_remark',
- 'contactor',
- 'mobile',
- 'addr',
- 'addr_code',
- ], 'post', 'trim');
- $val = Validate::rule([
- 'id' => 'require',
- 'platform_code' => 'require',
- 'po_code' => 'require',
- 'platform_time' => 'require',
- 'sale_source' => 'require',
- 'plat_code' => 'require',
- 'price' => 'require',
- 'num' => 'require',
- 'before_discount_all_price' => 'require',
- 'mode' => 'require',
- 'contactor' => 'require',
- 'mobile' => 'require',
- 'addr' => 'require',
- 'addr_code' => 'require',
- ]);
- if (!$val->check($param)) return error_show(1004, $val->getError());
- Db::startTrans();
- try {
- $rs = Db::name('order_import_from_c')
- ->where(['id' => $param['id'], 'is_del' => OIFCModel::$is_del_normal, 'status' => OIFCModel::$status_wait_confirm])
- ->find();
- if (empty($rs)) throw new \think\Exception('该记录不存在或不允许用户确认');
- $userinfo = GetUserInfo($param['token']);
- Db::name('order_import_from_c')
- ->where(['id' => $param['id'], 'is_del' => OIFCModel::$is_del_normal, 'status' => OIFCModel::$status_wait_confirm])
- ->update([
- 'platform_code' => $param['platform_code'],
- 'po_code' => $param['po_code'],
- 'platform_time' => $param['platform_time'],
- 'sale_source' => $param['sale_source'],
- 'plat_code' => $param['plat_code'],
- 'price' => $param['price'],
- 'num' => $param['num'],
- 'before_discount_all_price' => $param['before_discount_all_price'],
- 'activity_name' => $param['activity_name'],
- 'mode' => $param['mode'],
- 'order_remark' => $param['order_remark'],
- 'contactor' => $param['contactor'],
- 'mobile' => $param['mobile'],
- 'addr' => $param['addr'],
- 'status' => OIFCModel::$status_wait_relation,
- 'updatetime' => date('Y-m-d H:i:s')
- ]);
- Db::name('order_import_from_c_extend')
- ->where(['order_import_from_c_id' => $param['id'], 'is_del' => 0])
- ->update(['is_del' => 1]);
- Db::name('order_import_from_c_extend')
- ->insert([
- 'order_import_from_c_id' => $param['id'],
- 'addr_code' => $param['addr_code'],
- 'activity_id' => $param['activity_id'],
- 'addtime' => date('Y-m-d H:i:s'),
- 'createrid' => isset($user["data"]['id']) ? $userinfo["data"]['id'] : 0,
- 'creater' => isset($user["data"]['nickname']) ? $userinfo["data"]['nickname'] : '',
- ]);
- Db::commit();
- return app_show(0, '修改成功');
- } catch (\think\Exception $exception) {
- Db::rollback();
- return error_show(1004, $exception->getMessage());
- }
- }
- //批量关联库存
- public function relationStockBatch()
- {
- $param = $this->request->only(['ids', 'plat_code'], 'post', 'trim');
- $val = Validate::rule([
- 'ids' => 'require',
- 'plat_code|平台商品编号' => 'require',
- ]);
- if (!$val->check($param)) return error_show(1004, $val->getError());
- $info = Db::name('order_import_from_c_extend')
- ->field('id,spuCode,companyNo')
- ->whereIn('id', $param['ids'])
- ->find();
- //判断库存是否充足
- $num = Db::name('order_import_from_c')
- ->whereIn('id', $param['ids'])
- ->where(['is_del' => OIFCModel::$is_del_normal, 'status' => OIFCModel::$status_stock_not_enough])
- ->sum('num');
- $wsm_code = Db::name('warehouse_info')
- ->where(['companyNo' => $info['companyNo'], 'is_del' => 0])
- ->value('wsm_code');
- $usable_stock = Db::name('good_stock')
- ->where(['spuCode' => $info['spuCode'], 'wsm_code' => $wsm_code, 'is_del' => 0])
- ->value('usable_stock', 0);
- if ($usable_stock < $num) return error_show(1005, '可用库存只有' . $usable_stock . ',库存不足,关联失败');
- else {
- $userinfo = GetUserInfo($this->post['token']);
- $res = Db::name('order_import_from_c')
- ->whereIn('id', $param['ids'])
- ->where(['is_del' => OIFCModel::$is_del_normal, 'status' => OIFCModel::$status_stock_not_enough])
- ->update([
- 'status' => OIFCModel::$status_wait_relation,
- 'updatetime' => date('Y-m-d H:i:s'),
- 'updateid' => isset($user["data"]['id']) ? $userinfo["data"]['id'] : 0,
- 'updater' => isset($user["data"]['nickname']) ? $userinfo["data"]['nickname'] : '',
- ]);
- if ($res) return app_show(0, '批量关联库存成功');
- else return error_show(1005, '批量关联库存失败');
- }
- }
- //【辅助1】获取线上商品详情
- public function getOnlineGoodInfoByPlatcode()
- {
- $plat_code = isset($this->post['plat_code']) && $this->post['plat_code'] != "" ? trim($this->post['plat_code']) : "";
- if ($plat_code == "") {
- return error_show(1005, "参数plat_code不能为空");
- }
- $good_platform = Db::name("good_platform")->where(['plat_code' => $plat_code, "is_del" => 0])->find();
- if ($good_platform == false) {
- return error_show(1004, "未找到商品数据");
- }
- $data = Db::name("good")->where(["spuCode" => $good_platform['spuCode'], "is_del" => 0])->find();
- if ($data == false) {
- return error_show(1004, "未找到商品数据");
- }
- $plat = Db::name("platform")->where(["id" => $good_platform['platform_code']])->find();
- $data['skuCode'] = $good_platform['skuCode'];
- $data['platform_code'] = $good_platform['platform_code'];
- $data['platform_code_en'] = isset($plat['platform_code']) ? $plat['platform_code'] : "";
- $data['platform_name'] = isset($plat['platform_name']) ? $plat['platform_name'] : "";
- $data['online_reason'] = $good_platform['online_reason'];
- $data['online_time'] = $good_platform['online_time'];
- $data['online_remark'] = $good_platform['online_remark'];
- $data['exam_status'] = $good_platform['exam_status'];
- $data['is_online'] = $good_platform['is_online'];
- $data['plat_code'] = $good_platform['plat_code'];
- $data['exclusive'] = makeExcluse($data['is_exclusive']);
- $unit = Db::name("unit")->where(["id" => $data['good_unit']])->find();
- $data['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
- $data['cat_info'] = made($data['cat_id'], []);
- $spec = Db::name("good_spec")->where(["spuCode" => $good_platform['spuCode'], "is_del" => 0])->select()->toArray();
- $supplier = Db::name("supplier")->where(["code" => $data['supplierNo']])->find();
- $data['supplierName'] = isset($supplier['name']) ? $supplier['name'] : "";
- $noble = Config::get('noble');
- $data['noble_name'] = isset($data['noble_metal']) && $data['noble_metal'] != 0 ? $noble[$data['noble_metal']] : "";
- $company = Db::name("business")->where(["companyNo" => $data['companyNo']])->find();
- $data['company'] = isset($company['company']) ? $company['company'] : "";
- if ($data['brand_id'] != 0) {
- $brand = Db::name("brand")->where(["id" => $data['brand_id']])->find();
- $data["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
- } else {
- $data["brand_name"] = "";
- $data["brand_id"] = "";
- }
- $data['origin_place_cn'] = "";
- $data['delivery_place_cn'] = "";
- if ($data['delivery_place'] !== "") {
- $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
- list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $data['delivery_place']);
- $data['delivery_place_cn'] = GetAddr(json_encode($place));
- }
- if ($data['origin_place'] !== "") {
- $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
- list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $data['origin_place']);
- $data['origin_place_cn'] = GetAddr(json_encode($place));
- }
- $excluse = makeExcluse($data['is_exclusive']);
- $data['exclusive'] = $excluse;
- $data["good_info_img"] = $data['good_info_img'] != "" ? $data['good_info_img'] : [];
- $data["good_img"] = $data['good_img'] != "" ? $data['good_img'] : [];
- $speclist = [];
- if (!empty($spec)) {
- foreach ($spec as $value) {
- $temp = [];
- $temp['id'] = $value['id'];
- $temp['spuCode'] = $value['spuCode'];
- $temp['spec_id'] = $value['spec_id'];
- $temp['spec_value_id'] = $value['spec_value_id'];
- $temp['is_del'] = $value['is_del'];
- $sp = Db::name("specs")->where(["id" => $value['spec_id']])->find();
- $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
- $spv = Db::name("spec_value")->where(["id" => $value['spec_value_id']])->find();
- $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
- $speclist[] = $temp;
- }
- }
- $data["speclist"] = empty($speclist) ? [] : $speclist;
- $ladder = Db::name("good_ladder")->where(['skuCode' => $good_platform['spuCode'], "is_del" => 0])->select()->toArray();
- $data["ladderlist"] = !empty($ladder) ? $ladder : [];
- $nakelist = Db::name("good_nake")->where(['spuCode' => $good_platform['spuCode'], "is_del" => 0])->select()->toArray();
- $cat_top_list = made($data['cat_id']);
- $cat_top_id = isset($cat_top_list[0]['id']) ? $cat_top_list[0]['id'] : 0;
- $nakearry = [];
- if (!empty($nakelist)) {
- //实时金价
- $gold_price = Db::name("gold_price1")->where(["type" => $data['noble_metal'], "status" => 1, "is_del" => 0])->order("addtime desc")->value('price', 0);
- foreach ($nakelist as $value) {
- $value['sale_price'] = $value['nake_total'];
- //计算最终售价
- if ($data['is_gold_price'] == 1 && $cat_top_id == 6) {
- //普通商品:直接用财务提交的售价字段;
- //贵金属( 商品重量* 最新金价 + 工艺费(财务定价提交的)* 商品重量+包装费+加标费+证书费+产品裸价+其他费用)=商品库商品直接下单的售价
- $value['sale_price'] = ($data['weight'] * $gold_price + $value['cost_fee'] * $data['weight'] + $value['package_fee'] + $value['mark_fee'] + $value['cert_fee'] + $value['nake_fee'] + $value['other_fee']);
- }
- $nakearry[] = $value;
- }
- }
- $proof = Db::name("good_proof")->where(["spuCode" => $good_platform['spuCode'], "is_del" => 0])->order("updatetime desc")->field("id,proof_type,proof_url")->find();
- $data['proof'] = isset($proof) && $proof != false ? $proof : [];
- $data["nakelist"] = $nakearry;
- //补充采购员字段
- //成本表里的创建人,也是线上商品的采购人,为了防止混淆,给creater取个别名
- $temp_basic_info = Db::name('good_basic')
- ->field('id,createrid purchase_id,creater purchase')
- ->where(['spuCode' => $data['spuCode'], 'is_del' => 0])
- ->find();
- $data['purchase_id'] = $temp_basic_info['purchase_id'];
- $data['purchase'] = $temp_basic_info['purchase'];
- return app_show(0, "获取成功", $data);
- }
- //【辅助2】接口2-获取销售渠道备选数据:入参所在平台Id,响应:销售渠道备选数据
- public function getSourceDataByPlatformId()
- {
- $platform_id = $this->request->post('platform_id/d', 0, 'trim');
- $list = Db::name('platform_source')
- ->field('id,platform_id,source')
- ->where(['is_del' => 0, 'platform_id' => $platform_id])
- ->order('id', 'desc')
- ->select()
- ->toArray();
- return app_show(0, '获取成功', $list);
- }
- }
|