Files
liche/api/controllers/wxapp/licheb/Cusorder.php
T
2021-12-01 17:30:44 +08:00

570 lines
25 KiB
PHP

<?php
defined('WXAPP_APP') OR exit('No direct script access allowed');
/**
* Created by Vim
* User: lcc
* Date: 2021/07/09
* Time: 14:08
*/
require_once APPPATH.'controllers/wxapp/Wxapp.php';
class Cusorder extends Wxapp{
function __construct($inputs, $app_key){
parent::__construct($inputs, $app_key);
$this->login_white = array();//登录白名单
$this->check_status = array();//用户状态校验
$this->check_mobile = array();//需要手机号
$this->check_headimg =array();//授权微信信息
$this->load->model('receiver/receiver_customers_model','customers_model');
$this->load->model('receiver/order/receiver_orders_model','orders_model');
$this->load->model('receiver/order/receiver_order_signs_model','order_signs_model');
$this->load->model('receiver/order/receiver_order_bills_model','order_bills_model');
$this->load->model('receiver/order/receiver_order_deliverys_model','order_deliverys_model');
$this->load->model('receiver/order/receiver_order_ckcars_model','order_ckcars_model');
$this->load->model('receiver/order/receiver_order_contracts_model','order_contracts_model');
$this->load->model('receiver/receiver_fine_model');
$this->load->model('receiver/receiver_services_model','services_model');
$this->load->model('receiver/receiver_service_package_model','package_model');
$this->load->model('auto/auto_series_model');
$this->load->model('auto/auto_brand_model');
$this->load->model('auto/auto_attr_model');
$this->load->model('auto/auto_cars_model');
$this->load->model('apporder/order_purchase_model');
$this->load->model('app/liche/app_liche_orders_model');
$this->load->model("biz/biz_model");
$this->load->model('sys/sys_city_model');
$this->load->model('sys/sys_finance_model');
$this->load->library('receiver/orders_entity');
$this->load->library('receiver/customers_entity');
}
protected function get(){
$id = $this->input_param('id');
if($id){
return $this->detail($id);
}else{
return $this->lists();
}
}
//创建订单
protected function post(){
$biz_id = $this->session['new_biz_id'] ? $this->session['new_biz_id'] : intval($this->session['biz_id']);
$cus_id = $this->input_param('cus_id');
$car_id = $this->input_param('car_id');
$color_id = $this->input_param('color_id');
$incolor_id = $this->input_param('incolor_id');
$v_id = $this->input_param('v_id');
$price = $this->input_param('price');
$deposit = $this->input_param('deposit');
$payway = $this->input_param('payway');
$main_type = $this->input_param('main_type');
$ifentrust = $this->input_param('ifentrust');
$entrust_name = $this->input_param('entrust_name');
$entrust_idcard = $this->input_param('entrust_idcard');
$entrust_mobile = $this->input_param('entrust_mobile');
$name = $this->input_param('name');
$mobile = $this->input_param('mobile');
$bak_mobile = $this->input_param('bak_mobile');
$address = $this->input_param('address');
$cardid = $this->input_param('cardid');
$delry_time = $this->input_param('delry_time');
$finance_id = $this->input_param('finance_id');
$inten_money = floatval($this->input_param('inten_money'));//意向金
$disc_money = floatval($this->input_param('disc_money'));//优惠金额
$disc_fine_money = floatval($this->input_param('disc_fine_money'));//精品优惠金额
$if_fine = $this->input_param('if_fine');
$srv_ids = $this->input_param('srv_ids');
$fine_ids = $this->input_param('fine_ids');
$row = $this->customers_model->get(['id'=>$cus_id]);
$series_row = $this->auto_series_model->get(['id'=>$car_id]);
if(!$price || !$deposit){
throw new Exception('车辆价格参数未配置', ERR_PARAMS_ERROR);
}
if(!$row || !$series_row || !$cardid || !$address ||!$delry_time){
throw new Exception('参数错误', ERR_PARAMS_ERROR);
}
if($inten_money > $deposit){
throw new Exception('意向金不得高于定金', ERR_PARAMS_ERROR);
}
$biz = $this->biz_model->get(['id'=>$row['biz_id']],'city_id,type');
if($biz['type'] == 1 && !count($srv_ids)){ //直营店必须选择一个代办
throw new Exception('请选择代办项目', ERR_PARAMS_ERROR);
}
//判断是否存在未完成流程
$omobile = $mobile ? $mobile : $row['mobile'];
$o_order = $this->orders_model->get(['mobile'=>$omobile,'status>='=>0,'status<'=>6]);
if($o_order){
$this->orders_model->update(['status'=>-1],['id'=>$o_order['id']]);
// throw new Exception('该手机号用户存在未完成订单', API_CODE_FAIL);
}
$car_row = $this->auto_cars_model->get(['brand_id'=>$series_row['brand_id'],'s_id'=>$series_row['id'],'v_id'=>$v_id],'',$biz['city_id']);
if(!$car_row){
throw new Exception('参数错误', API_CODE_FAIL);
}
$can_dis_money = $car_row['price_car'] - $car_row['price_floor']; //可优惠金额
if($can_dis_money>0){
if(is_array($fine_ids) && $fine_ids){
$fine_id_str = implode(',',$fine_ids);
$fine_rows = $this->receiver_fine_model->select(["id in ({$fine_id_str})" => null],'','','','id,price');
foreach($fine_rows as $item){
$total_dis_money += $item['price'];
}
}
if($disc_money && $car_row['price_floor'] > $price-$total_dis_money){
throw new Exception('车辆售价不得低于车辆底价,请重新填写优惠信息', API_CODE_FAIL);
}
}else{
if($disc_money) throw new Exception('当前车型没有优惠额度', API_CODE_FAIL);
}
$total_dis_money = $disc_money; //总优惠金额包含赠送精品
if(!$payway){ //分期
$finance_row = $this->sys_finance_model->get(['id'=>$finance_id,'status'=>1]);
if(!$finance_row){
throw new Exception('参数错误', ERR_PARAMS_ERROR);
}
}
$where = [
"id in ($v_id,$color_id,$incolor_id)" => null
];
$attr_row = $this->auto_attr_model->map('id','',$where);
if($attr_row[$color_id]){
$color_row = $attr_row[$color_id][0];
$color_row['jsondata'] = json_decode($color_row['jsondata'],true);
}
if($attr_row[$incolor_id]){
$incolor_row = $attr_row[$incolor_id][0];
$incolor_row['jsondata'] = json_decode($incolor_row['jsondata'],true);
}
if($attr_row[$v_id]){
$version_row = $attr_row[$v_id][0];
$version_row['jsondata'] = json_decode($version_row['jsondata'],true);
}
$car_json = [
'c_id' => $color_id,
'inc_id' => $incolor_id,
'v_id' => $v_id,
];
$data = [
'rid' => $cus_id,
'sid' => create_order_no(350200,$this->app_key),
'name' => $name ? $name : $row['name'],
'mobile' => $mobile ? $mobile : $row['mobile'],
'biz_id' => $biz_id,
'brand_id' => $series_row['brand_id'],
's_id' => $series_row['id'],
'v_id' => $v_id,
'cor_id' => $color_id,
'incor_id' => $incolor_id,
'admin_id' => $this->session['uid'],
'car_json' => json_encode($car_json,JSON_UNESCAPED_UNICODE),
'price' => $price,
'deposit' => $deposit,
'c_time' => time()
];
$bak_mobile && $data['bak_mobile'] = $bak_mobile;
$payway && $data['payway'] = 1;
$main_type && $data['main_type'] = 1;
$delry_time && $data['delry_time'] = $delry_time;
$srv_ids && $data['srv_ids'] = implode(',',$srv_ids);
$fine_ids && $data['fine_ids'] = implode(',',$fine_ids);
$info_json = [];
if($ifentrust){
$data['ifentrust'] = 1;
$info_json['entrust_name'] = $entrust_name;
$info_json['entrust_idcard'] = $entrust_idcard;
$info_json['entrust_mobile'] = $entrust_mobile;
}
$cardid && $info_json['c_cardid'] = $cardid;
$address && $info_json['c_address'] = $address;
$info_json && $data['info_json'] = json_encode($info_json,JSON_UNESCAPED_UNICODE);
$jsondata = [];
$jsondata['if_fine'] = $if_fine ? 1 : 0;
$data['jsondata'] = json_encode($jsondata,JSON_UNESCAPED_UNICODE);
$finance_row && $data['finance_id'] = $finance_row['id'];
//获取金额json数据
$orders_entity = new Orders_entity();
$money_json = [
'price_car' => $car_row['price_car'],
'price_book' => $car_row['price_book'],
'price_insure' => $car_row['price_insure'],
'price_fine' => $car_row['price_fine'],
'price_discount' =>$disc_money ? $disc_money : 0,
'price_intention' => $inten_money ? $inten_money : 0,
'price_finance' => $series_row['brand_id'] == 4 ? $orders_entity::PRICE_FINANCE_NZ : $orders_entity::PRICE_FINANCE,
'price_fine_discount' => $disc_fine_money ? $disc_fine_money : 0
];
//获取挂牌价
if($series_row['brand_id']==3){ //狸车品牌写死挂牌费用0.01 测试用
$money_json['fee_carno'] = 0.01;
}else{
$city = $this->sys_city_model->get(['city_id'=>$biz['city_id']],'fee_carno');
$money_json['fee_carno'] = $city['fee_carno'];
}
$data['money_json'] = json_encode($money_json,JSON_UNESCAPED_UNICODE);
$row['rid'] && $data['clue_id'] = $row['rid'];
$o_id = $this->orders_model->add($data);
if($o_id){
$sign_data = [
'o_id' => $o_id,
'c_time' => time()
];
$this->order_signs_model->add($sign_data);
//意向金订单
if($inten_money){
$this->load->model('app/liche/app_liche_users_model');
$userinfo = $this->app_liche_users_model->get(['mobile'=>$mobile]);
$this->orders_entity->c_intention($o_id,$userinfo,$inten_money);
}
//更新客户下单时间
$this->customers_model->update(array('order_time' => date('Y-m-d H:i:s')),['id'=>$cus_id]);
$this->customers_entity->add_log($cus_id, $this->session['uid'], $this->session['uname'], "新增订单");
//增加分销记录
$this->load->library('entity/deal_entity',['app_id'=>1]);
$this->deal_entity->deal_log($o_id);
return ['id'=>$o_id,'admin_img' => $orders_entity::API_ADMIN_IMG,'pay_img'=>$orders_entity::API_CREATE_PAY_IMG];
}else{
throw new Exception('创建失败', ERR_PARAMS_ERROR);
}
}
//修改订单信息
protected function put(){
$id = $this->input_param('id');
$payway = $this->input_param('payway');
$pack_id = $this->input_param('pack_id');
$row = $this->orders_model->get(['id'=>$id]);
if(!$row){
throw new Exception('订单不存在', ERR_PARAMS_ERROR);
}
$result = false;
$up_data = [];
if(strlen($payway)|| $pack_id){
if(strlen($payway)){ //修改付款类型
$pay_order = $this->app_liche_orders_model->count(['app_id'=>1,'status>'=>1,'o_id'=>$id]);
if($pay_order){
throw new Exception('修改失败,已付款', ERR_PARAMS_ERROR);
}
$up_data['payway'] = $payway;
}
$pack_id && $up_data['pack_id'] = $pack_id;
}
if($up_data){
$result = $this->orders_model->update($up_data,['id'=>$id]);
}
if($result){
throw new Exception('修改成功', API_CODE_SUCCESS);
}else{
throw new Exception('修改失败', ERR_PARAMS_ERROR);
}
}
//订单列表头部
protected function get_tabs(){
$lists = [
['key' =>10 ,'name' => '进行中'],
['key' =>11 ,'name' => '已完成'],
];
return $lists;
}
//订单列表
private function lists(){
$group_id = $this->session['group_id'];
$uid = $this->session['uid'];
$biz_id = $this->session['new_biz_id'] ? $this->session['new_biz_id'] : intval($this->session['biz_id']);
$keyword = $this->input_param('keyword');
$status = $this->input_param('status');
$page = $this->input_param('page');
$size = $this->input_param('size');
!$page && $page = 1;
!$size && $size = 10;
$where = [
'biz_id' => $biz_id
];
$group_id == 1 && $where['admin_id'] = $uid;//销售
if($group_id == 4 && $biz_id!=1){
$where['brand_id!='] = 3; //渠道经理过滤
}
if($keyword){
$where["(name='{$keyword}' or mobile='{$keyword}')"] = null;
}
if($status == 11){ //已完成
$where['status'] = 6;
}elseif($status == 10 ){ //进行中
$where['status>='] = 0;
$where['status!='] = 6;
}else{
strlen($status) && $where['status'] = $status;
}
$fileds = 'id,name,mobile,brand_id,s_id,v_id,cor_id,incor_id,deposit,payway,status,c_time,money_json,srv_ids';
$count = $this->orders_model->count($where);
$lists = [];
if($count){
$rows = $this->orders_model->select($where,'id desc',$page,$size,$fileds);
//品牌车型
$brand_arr = array_unique(array_column($rows,'brand_id'));
$brands = $this->auto_brand_model->get_map_by_ids($brand_arr,'id,name');
//车系车型
$series_arr = array_unique(array_column($rows,'s_id'));
$series = $this->auto_series_model->get_map_by_ids($series_arr,'id,name');
//获取车辆属性
$version_arr = array_unique(array_column($rows,'v_id'));
$color_arr = array_unique(array_column($rows,'cor_id'));
$attrs_arr = array_merge($version_arr,$color_arr);
$attrs = $this->auto_attr_model->get_map_by_ids($attrs_arr,'id,title');
$status_arr = $this->orders_model->get_status();
foreach($rows as $key=>$val){
$money_json = json_decode($val['money_json'],true);
if($val['brand_id']){ //已选择车型
$brand_name = isset($brands[$val['brand_id']]) ? $brands[$val['brand_id']][0]['name'] : '';
$serie_name = isset($series[$val['s_id']]) ? $series[$val['s_id']][0]['name'] : '';
$color = isset($attrs[$val['cor_id']]) ? $attrs[$val['cor_id']][0]['title'] : '';
$version = isset($attrs[$val['v_id']]) ? $attrs[$val['v_id']][0]['title'] : '';
//是否需要代办
$srv_arr = explode(',',$val['srv_ids']);
$db_title = in_array(2,$srv_arr) ? '需要' : '';
$other_data = [
'品牌车型' => "{$brand_name}{$serie_name}-{$color}-{$version}",
'付款方式' => $val['payway']?'全款':'分期',
'代办车牌' => $db_title,
'定金金额' => $val['deposit'],
'订单日期' => date('Y-m-d',$val['c_time']),
];
}else{
$other_data = [
'意向金' => $money_json['price_intention'] ? $money_json['price_intention'] : '',
'订单日期' => date('Y-m-d',$val['c_time']),
];
}
$lists[] = [
'id' => $val['id'],
'name' => $val['name'],
'mobile' => mobile_asterisk($val['mobile']),
'status_name' => $status_arr[$val['status']],
'other_data' => $other_data,
'remark' => $this->orders_entity->get_remark($val['status'])
];
}
}
$data = [
'list' => $lists,
'total' => $count
];
return $data;
}
//订单详情
private function detail($id){
$row = $this->orders_model->get(['id'=>$id]);
if(!$row){
throw new Exception('订单不存在', ERR_PARAMS_ERROR);
}
$orders_entity = new Orders_entity();
$money_json = json_decode($row['money_json'],true);
$info_json = json_decode($row['info_json'],true);
$jsondata = json_decode($row['jsondata'],true);
$brand = $this->auto_brand_model->get(['id'=>$row['brand_id']],'name');
$series = $this->auto_series_model->get(['id'=>$row['s_id']],'name');
$color = $this->auto_attr_model->get(['id'=>$row['cor_id']],'title');
$version = $this->auto_attr_model->get(['id'=>$row['v_id']],'title');
$color = $color ? $color['title'] : '';
$version = $version ? $version['title'] : '';
//贷款信息
$loan_data = [];
$loan_status = 0;
if(!$row['payway'] && $row['status']>=1){
$this->load->model('receiver/order/receiver_order_loans_model','order_loans_model');
$loan = $this->order_loans_model->get(['o_id'=>$row['id']]);
if($loan){
$loan_data = [
'id' => $loan['id'],
'bank' => $loan['title'],
'year' => $loan['year'],
'money' => $loan['first_price']
];
}
$loan_status = $loan['status']>0 ? 2 : 1;
}
$pack_title = '';
if($row['srv_ids']){
$srv_rows = $this->services_model->select(["id in ({$row['srv_ids']})"=>null],'','','','title');
$srv_title_arr = array_column($srv_rows,'title');
$jsondata['if_fine'] && $srv_title_arr[] = '精品尊享包';
$srv_rows && $pack_title = implode('+',$srv_title_arr);
}
$car_data = [];
if($row['brand_id'] && $row['s_id'] && $row['v_id']){
$car_data['车辆名称'] = $brand['name'].$series['name'];
$car_data['车辆名称'] = $brand['name'].$series['name'];
$car_data['车辆级别'] = $version;
$car_data['颜色'] = $color;
$car_data['车辆平台售价'] = $money_json['price_discount'] ? $row['price']-$money_json['price_discount'] : $row['price'];
$money_json['price_discount'] && $car_data['优惠金额'] = $money_json['price_discount'];
$car_data['定金'] = $row['deposit'];
$car_data['代办包'] = $pack_title;
//服务费
$srv_data = $orders_entity->order_srv_money($row['id'],1);
if($srv_data['total']>0){
$car_data['代办费']['value'] = sprintf('%.2f',$srv_data['total']);
foreach($srv_data['list'] as $val){
$car_data['代办费']['list'][$val['title']] = sprintf('%.2f',$val['money']);
}
}
if($row['ifentrust']){
$car_data['是否委托'] = '是';
$car_data['委托人姓名'] = $info_json['entrust_name'] ? $info_json['entrust_name'] : '';
$car_data['委托人手机号'] = $info_json['entrust_mobile'] ? $info_json['entrust_mobile'] : '';
$car_data['委托人身份证'] = $info_json['entrust_idcard'] ? $info_json['entrust_idcard'] : '';
}
if($row['main_type']){
$car_data['购车主体'] = '公司';
}
}
$info_json['c_cardid'] && $car_data['身份证号'] = $info_json['c_cardid'];
$info_json['c_address'] && $car_data['地址'] = $info_json['c_address'];
if(!$row['payway']){
$finance_row = $this->sys_finance_model->get(['id'=>$row['finance_id']],'title');
$car_data['金融产品'] = $finance_row['title'];
}
//是否存在意向金订单
$inten_row = $this->app_liche_orders_model->get(['o_id'=>$row['id'],'type'=>4,'status>='=>0]);
$pay_status = 0;
$inten_money = 0;
if($inten_row && $inten_row['status']==0){
$pay_status = 1;
}
if($inten_row['status']==1){//已支付意向金
$inten_money = floatval($inten_row['total_price']);
}
//开票信息
$bill_status = 0;
$bill_data = [];
if($row['status']>=3){
$bill = $this->order_bills_model->get(['o_id'=>$row['id']]);
if($bill){
$bill_data = [
'id' => $bill['id'],
'carid_a' => $bill['cardidA'] ? build_qiniu_image_url($bill['cardidA']) : '',
'carid_a_path' => $bill['cardidA'] ? $bill['cardidA'] : '',
'carid_b' => $bill['cardidB'] ? build_qiniu_image_url($bill['cardidB']) : '',
'carid_b_path' => $bill['cardidB'] ? $bill['cardidB'] : '',
'business_licence' => $bill['business_licence'] ? build_qiniu_image_url($bill['business_licence']) : '',
'business_licence_path' => $bill['business_licence'] ? $bill['business_licence'] : '',
'bill_img' => $bill['file'] ? build_qiniu_image_url($bill['file']) : ''
];
}
$bill_status = $bill['status']==2 ? 2 : 1;
}
//确认车辆状态
$delivery = $this->order_deliverys_model->get(['o_id'=>$row['id']],'status,info,tool,remark');
$ckcar_status = 0;
$ckcar_data = [
'info' => $delivery['info'] ? explode(',',$delivery['info']) : [],
'tool' => $delivery['tool'] ? explode(',',$delivery['tool']) : [],
'remark' => $delivery['remark'] ? $delivery['remark'] : ''
];
if($row['status']==5&&in_array($delivery['status'],[0,1])){
$ckcar_status = 1;
}
$data = [
'id' => $id,
'name' => $row['name'],
'mobile' => mobile_asterisk($row['mobile']),
'admin_img' => $orders_entity::API_ADMIN_IMG,
'payway' => $row['brand_id'] ? $row['payway'] : '',
'car_data' => $car_data,
'pay_status' => $pay_status,
'pay_img' => $orders_entity::API_PAY_IMG,
'loan_status' => $loan_status,
'loan_data' => $loan_data,
'bill_status' => $bill_status,
'bill_data' => $bill_data,
'ckcar_status' => $ckcar_status,
'ckcar_data' => $ckcar_data,
'pack_id' => $row['pack_id'],
'brand_id' => $row['brand_id'],
'car_id' => $row['s_id'],
'v_id' => $row['v_id'],
'color_id' => $row['cor_id'],
'incolor_id' => $row['incor_id'],
'ifentrust' => $row['brand_id'] ? $row['ifentrust'] : '',
'entrust_name' => $info_json['entrust_name'] ? $info_json['entrust_name']:'',
'entrust_idcard' => $info_json['entrust_idcard'] ? $info_json['entrust_idcard']:'',
'delry_time' => $row['delry_time']!='0000-00-00 00:00:00' ? date('Y-m-d',strtotime($row['delry_time'])):'',
'finance_id' => !$row['payway'] ? $row['finance_id'] : '',
'disc_money' => $money_json['price_discount'] ? $money_json['price_discount'] : '',
'srv_ids' => $row['srv_ids'] ? explode(',',$row['srv_ids']) : [],
'fine_ids' => $row['fine_ids'] ? explode(',',$row['fine_ids']) : [],
'cus_id' => $row['rid'],
'edit_status' => $row['status'] > 2 ? 0 : 1,
'status' => $row['status'],
'my_img' => $orders_entity::API_CREATE_PAY_IMG,
'if_fine' => $jsondata['if_fine'] ? 1 : 0,
'disc_fine_money' => $money_json['price_fine_discount'] ? $money_json['price_fine_discount'] : 0,
'main_type' => intval($row['main_type']),
];
$inten_money && $data['inten_money'] = $inten_money;
return $data;
}
//确认交付
protected function put_ckcar(){
$id = $this->input_param('id');
$remark = $this->input_param('remark');
$info = $this->input_param('info');
$tool = $this->input_param('tool');
if(!count($info) || !count($tool)){
throw new Exception('参数错误', ERR_PARAMS_ERROR);
}
$row = $this->order_deliverys_model->get(['o_id'=>$id]);
if($row['status']>1){
throw new Exception('当前不可修改', ERR_PARAMS_ERROR);
}
$update = [
'status' => 1,
'info' => implode(',',$info),
'tool' => implode(',',$tool),
'remark' => $remark ? $remark : ''
];
$result = $this->order_deliverys_model->update($update,['id'=>$row['id']]);
if($result){
//$this->orders_model->update(['status'=>6],['id'=>$row['o_id']]);
////完成分销
//$this->load->library('entity/deal_entity',['app_id'=>1]);
//$this->deal_entity->deal_log_finish($row['o_id']);
throw new Exception('修改成功', API_CODE_SUCCESS);
}else{
throw new Exception('修改失败', ERR_PARAMS_ERROR);
}
}
}