Files
liche/api/controllers/wxapp/licheb/CusorderV2.php
T
2022-05-05 11:52:45 +08:00

957 lines
48 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 CusorderV2 extends Wxapp{
private $biz_id = '';
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');
$this->load->model('receiver/order/receiver_orders_v2_model','orders_model');
$this->load->model('receiver/order/receiver_order_datas_model');
$this->load->model('receiver/order/receiver_order_status_model');
$this->load->model('receiver/order/receiver_order_deliverys_model','order_deliverys_model');
$this->load->model('receiver/order/receiver_order_loans_model', 'order_loans_model');
$this->load->model('receiver/order/receiver_order_bills_model','order_bills_model');
$this->load->model('auto/auto_series_model');
$this->load->model('auto/auto_attr_model');
$this->load->model('auto/auto_cars_model');
$this->load->model('auto/auto_business_model');
$this->load->model("biz/biz_model");
$this->load->model('items/items_model');
$this->load->model('sys/sys_finance_model');
$this->load->model('sys/sys_city_model');
$this->load->model('app/liche/app_liche_users_model');
$this->load->model('app/liche/app_liche_orders_model');
$this->load->library('receiver/orders_entity');
$this->load->library('receiver/orders_v2_entity');
$this->load->library('receiver/customers_entity');
$this->load->library('receiver/order_datas_entity');
$this->load->library('receiver/orders_status_entity');
$this->biz_id = $this->get_biz_id();
}
protected function get(){
$id = $this->input_param('id');
if($id){
return $this->detail($id);
}else{
return $this->lists();
}
}
//创建订单
protected function post(){
$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');
$deposit = $this->input_param('deposit');
$payway = $this->input_param('payway');
$main_type = $this->input_param('main_type');
$name = trim($this->input_param('name'));
$mobile = trim($this->input_param('mobile'));
$cardid = trim($this->input_param('cardid'));
$owner_name = trim($this->input_param('owner_name'));
$owner_mobile = trim($this->input_param('owner_mobile'));
$owner_cardid = trim($this->input_param('owner_cardid'));
$company = $this->input_param('company');
$c_credit = $this->input_param('credit');
$delry_time = $this->input_param('delry_time');
$finance_id = $this->input_param('finance_id');
$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_arr = $this->input_param('srv_arr');
$fines = $this->input_param('fines');
$address = $this->input_param('address');
$business_id = $this->input_param('business_id'); //商务政策id
$car_city_id = $this->input_param('c_city_id');//上牌城市
$pro = $this->input_param('pro'); //省市数组
$sa = $this->input_param('sa'); //补充协商
$if_local_bill = $this->input_param('if_local_bill');//是否需要开具本地发票
$row = $this->customers_model->get(['id'=>$cus_id]);
$series_row = $this->auto_series_model->get(['id'=>$car_id]);
$business_row = $this->auto_business_model->get(['id'=>$business_id]);
if(!$row || !$series_row ||!$delry_time || !$name || !$mobile || !$business_row || !$deposit){
throw new Exception('参数错误', ERR_PARAMS_ERROR);
}
if($if_local_bill && !$car_city_id){
throw new Exception('请填写上牌城市', ERR_PARAMS_ERROR);
}
if($main_type){//公司
if(!$company || !$c_credit){
throw new Exception('请填写公司信息', ERR_PARAMS_ERROR);
}
}else{ //个人
if(!$cardid || !$owner_name || !$owner_mobile || !$owner_cardid){
throw new Exception('参数错误', ERR_PARAMS_ERROR);
}
//同个品牌一个身份证只能下一单
$where = [
'brand_id' => $series_row['brand_id'],
"JSON_EXTRACT(info_json,'$.c_owner_cardid')='{$owner_cardid}'" => null,
'status>=' => 0
];
if($this->orders_model->count($where)){
throw new Exception('一个客户只能享受一次国补,请联系管理员', ERR_PARAMS_ERROR);
}
}
if($deposit<2000){
throw new Exception('定金不得少于2000', ERR_PARAMS_ERROR);
}
$biz = $this->biz_model->get(['id'=>$row['biz_id']],'id,city_id,type');
$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 = $business_row['price_car'] - $business_row['price_floor']; //可优惠金额
if($can_dis_money>0){
if($disc_money && $business_row['price_floor'] > $business_row['price_car']-$disc_money){
throw new Exception('车辆售价不得低于车辆底价,请重新填写优惠信息', API_CODE_FAIL);
}
}else{
if($disc_money) throw new Exception('当前车型没有优惠额度', API_CODE_FAIL);
}
if(!$payway && $finance_id){ //分期
$finance_row = $this->sys_finance_model->get(['id'=>$finance_id,'status'=>1]);
if(!$finance_row){
throw new Exception('参数错误', ERR_PARAMS_ERROR);
}
}
$data = [
'sid' => create_order_no(350200,$this->app_key),
'customer_id' => $row['id'],
'name' => $name ? $name : $row['name'],
'mobile' => $mobile ? $mobile : $row['mobile'],
'biz_id' => $this->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,
'sale_id' => $this->session['uid'],
'c_time' => time()
];
$owner_name && $data['owner_name'] = $owner_name;
$owner_mobile && $data['owner_mobile'] = $owner_mobile;
$row['rid'] && $data['clue_id'] = $row['rid'];
$payway && $data['payway'] = 1;
$if_fine && $data['if_fine'] = 1;
$main_type && $data['main_type'] = 1;
$delry_time && $data['delry_time'] = $delry_time;
$fines && $data['fines'] = json_encode($fines,JSON_UNESCAPED_UNICODE);
$info_json = [];
if($pro){
$info_json['pro'] = $pro;
$address = implode(' ',$pro).$address;
}
$address && $info_json['c_address'] = $address;
$cardid && $info_json['c_cardid'] = $cardid;
$owner_cardid && $info_json['c_owner_cardid'] = $owner_cardid;
$c_credit && $info_json['c_credit'] = $c_credit;
$company && $info_json['c_company'] = $company;
$business_id && $info_json['business_id'] = $business_id;
$car_city_id && $info_json['car_city_id'] = $car_city_id;
$info_json && $data['info_json'] = json_encode($info_json,JSON_UNESCAPED_UNICODE);
$finance_row && $data['finance_id'] = $finance_row['id'];
$if_local_bill && $data['if_local_bill'] = 1;
//获取金额json数据
$orders_entity = new orders_v2_entity();
$color_arr = json_decode($business_row['colors'],true);
$price_color = in_array($color_id,$color_arr) ? $business_row['price_color'] : 0;
$price_coplus = $main_type ? $business_row['price_coplus'] : 0;
$money_json = [
'price_car' => $business_row['price_car'],
'price_book' => $deposit ? $deposit : 0,
'price_insure' => $car_row['price_insure'],
'price_fine' => $car_row['price_fine'],
'price_discount' =>$disc_money ? $disc_money : 0,
'price_intention' => 0,
'price_fine_discount' => $disc_fine_money ? $disc_fine_money : 0,
'price_fine_select' => 0,
'price_color' => $price_color,
'price_coplus' => $price_coplus,
];
if($srv_arr){
foreach ($srv_arr as $item) {
$money_json[$item['key']] = $item['price'];
$item['id'] == 1 && $data['if_insure'] = 1;
$item['id'] == 2 && $data['if_num'] = 1;
}
}
$data['srv_ids'] = $srv_arr ? json_encode($srv_arr,JSON_UNESCAPED_UNICODE) : "";
if($fines){
foreach ($fines as $item) {
if($item['price']){
$money_json['price_fine_select'] += $item['price'];
}
}
}
$data['car_json'] = $data['money_json'] = json_encode($money_json,JSON_UNESCAPED_UNICODE);
$this->ck_money($money_json,$srv_arr,$biz);
$o_id = $this->orders_model->add($data);
if(is_numeric($o_id)){
$userinfo = $this->app_liche_users_model->get(['mobile'=>$data['mobile']],'id');
$this->orders_v2_entity->add_book_order($o_id,$userinfo['id']);
if($payway){ //全款服务费和尾款订单
$this->orders_v2_entity->add_order($o_id,$userinfo['id']);
}
//更新客户下单时间
$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'], '生成订单', 6);
//补充协商
$sa && $this->order_datas_entity->up_data($o_id,['sa'=>$sa]);
return ['id'=>$o_id,'pay_img'=>$orders_entity->pay_img($o_id)];
}else{
throw new Exception('创建失败', ERR_PARAMS_ERROR);
}
}
//创建意向金订单
protected function post_inten(){
$cus_id = $this->input_param('cus_id');
$name = $this->input_param('name');
$mobile = $this->input_param('mobile');
$cardid = $this->input_param('cardid');
$owner_name = $this->input_param('owner_name');
$owner_mobile = $this->input_param('owner_mobile');
$owner_cardid = $this->input_param('owner_cardid');
$main_type = $this->input_param('main_type');
$company = $this->input_param('company');
$c_credit = $this->input_param('credit');
$address = $this->input_param('address');
$car_city_id = $this->input_param('c_city_id');//上牌城市
$pro = $this->input_param('pro'); //省市数组
$inten_money = floatval($this->input_param('inten_money'));//意向金
if(!$name || !$mobile|| !$inten_money){
throw new Exception('参数错误', ERR_PARAMS_ERROR);
}
if($main_type){
if(!$company || !$c_credit){
throw new Exception('请填写公司信息', ERR_PARAMS_ERROR);
}
}else{
if( !$cardid || !$owner_name || !$owner_mobile || !$owner_cardid){
throw new Exception('参数错误', ERR_PARAMS_ERROR);
}
}
$row = $this->customers_model->get(['id'=>$cus_id]);
if($inten_money > 2000){
throw new Exception('意向金不得高于2000', ERR_PARAMS_ERROR);
}
$data = [
'customer_id' => $cus_id,
'sid' => create_order_no(350200,$this->app_key),
'name' => $name ? $name : $row['name'],
'mobile' => $mobile ? $mobile : $row['mobile'],
'biz_id' => $this->biz_id,
'sale_id' => $this->session['uid'],
'c_time' => time()
];
$main_type && $data['main_type'] = 1;
$owner_name && $data['owner_name'] = $owner_name;
$owner_mobile && $data['owner_mobile'] = $owner_mobile;
$info_json = [];
if($pro){
$info_json['pro'] = $pro;
$address = implode(' ',$pro).$address;
}
$address && $info_json['c_address'] = $address;
$cardid && $info_json['c_cardid'] = $cardid;
$owner_cardid && $info_json['c_owner_cardid'] = $owner_cardid;
$company && $info_json['c_company'] = $company;
$c_credit && $info_json['c_credit'] = $c_credit;
$car_city_id && $info_json['car_city_id'] = $car_city_id;
$info_json && $data['info_json'] = json_encode($info_json,JSON_UNESCAPED_UNICODE);
$money_json['price_intention'] = $inten_money ? $inten_money : 0;
$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){
throw new Exception('创建失败', ERR_PARAMS_ERROR);
}
//更新客户下单时间
$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'], '生成订单', 6);
//意向金订单
$orders_entity = new orders_v2_entity();
if($inten_money){
$userinfo = $this->app_liche_users_model->get(['mobile'=>$data['mobile']],'id');
$orders_entity->c_intention($o_id,$userinfo['id'],$inten_money);
}
return ['id'=>$o_id,'pay_img'=>$orders_entity->pay_img($o_id)];
}
//修改订单信息
protected function put(){
$id = $this->input_param('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');
$payway = $this->input_param('payway');
$main_type = $this->input_param('main_type');
$delry_time = $this->input_param('delry_time');
$finance_id = $this->input_param('finance_id');
$deposit = floatval($this->input_param('deposit'));
$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_arr = $this->input_param('srv_arr');
$fines = $this->input_param('fines');
$business_id = $this->input_param('business_id'); //商务政策id
$sa = $this->input_param('sa'); //补充协商
$if_local_bill = $this->input_param('if_local_bill');//是否需要开具本地发票
$row = $this->orders_model->get(['id'=>$id]);
$series_row = $this->auto_series_model->get(['id'=>$car_id]);
$biz = $this->biz_model->get(['id'=>$row['biz_id']],'id,type,city_id');
$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']);
$info_json = json_decode($row['info_json'],true);
if($v_id==$row['v_id']){ //修改不同车型才修改商务政策id
$business_id = $info_json['business_id'];
}
$business_row = $this->auto_business_model->get(['id'=>$business_id]);
if(!$row || !$series_row || !$car_row || !$business_row){
throw new Exception('参数错误', ERR_PARAMS_ERROR);
}
if($deposit<2000){
throw new Exception('定金不得少于2000', ERR_PARAMS_ERROR);
}
$can_dis_money = $car_row['price_car'] - $car_row['price_floor']; //可优惠金额
if($can_dis_money>0){
if($disc_money && $car_row['price_floor'] > $car_row['price_car']-$disc_money){
throw new Exception('车辆售价不得低于车辆底价,请重新填写优惠信息', API_CODE_FAIL);
}
}else{
if($disc_money) throw new Exception('当前车型没有优惠额度', API_CODE_FAIL);
}
if(!$payway && $finance_id){ //分期
$finance_row = $this->sys_finance_model->get(['id'=>$finance_id,'status'=>1]);
if(!$finance_row){
throw new Exception('参数错误', ERR_PARAMS_ERROR);
}
}
if(!$main_type){//个人
//同个品牌一个身份证只能下一单
$where = [
'brand_id' => $series_row['brand_id'],
"JSON_EXTRACT(info_json,'$.c_owner_cardid')='{$info_json['c_owner_cardid']}'" => null,
'status>=' => 0,
'id!=' => $id,
];
if($this->orders_model->count($where)){
throw new Exception('一个客户只能享受一次国补,请联系管理员', ERR_PARAMS_ERROR);
}
}
$data = [
'brand_id' => $series_row['brand_id'],
's_id' => $series_row['id'],
'v_id' => $v_id,
'cor_id' => $color_id,
'incor_id' => $incolor_id,
'payway' => $payway ? 1 : 0,
'if_insure' => 0,
'if_num' => 0,
'if_local_bill' => $if_local_bill ? 1 : 0
];
$main_type && $data['main_type'] = 1;
$delry_time && $data['delry_time'] = $delry_time;
$finance_row && $data['finance_id'] = $finance_row['id'];
$data['if_fine'] = $if_fine ? 1:0;
$fines && $data['fines'] = json_encode($fines,JSON_UNESCAPED_UNICODE);
$color_arr = json_decode($business_row['colors'],true);
//获取金额json数据
$money_json = json_decode($row['money_json'],true);
$data['car_json'] = json_encode($money_json,JSON_UNESCAPED_UNICODE);
$money_json['price_car'] = $business_row['price_car'];
$money_json['price_book'] = $deposit;
$money_json['price_insure'] = $car_row['price_insure'];
$money_json['price_fine'] = $car_row['price_fine'];
$money_json['price_discount'] = $disc_money;
$money_json['price_fine_discount'] = $disc_fine_money;
$money_json['price_color'] = in_array($color_id,$color_arr) ? $business_row['price_color'] : 0;
$money_json['price_coplus'] = $main_type ? $business_row['price_coplus'] : 0;
if($srv_arr){
$money_json['price_finance'] = $money_json['fee_carno'] = 0;
foreach ($srv_arr as $item) {
$money_json[$item['key']] = $item['price'];
$item['id'] == 1 && $data['if_insure'] = 1;
$item['id'] == 2 && $data['if_num'] = 1;
}
}
$data['srv_ids'] = $srv_arr ? json_encode($srv_arr,JSON_UNESCAPED_UNICODE) : "";
if($fines){
$money_json['price_fine_select'] = 0;
foreach ($fines as $item) {
if($item['price']){
$money_json['price_fine_select'] += $item['price'];
}
}
}
if($row['c_time']>=strtotime('2022-04-08 14:43:00')){
$this->ck_money($money_json,$srv_arr,$biz);
}
$data['money_json'] = json_encode($money_json,JSON_UNESCAPED_UNICODE);
$info_json['business_id'] = $business_id;
$data['info_json'] = json_encode($info_json,JSON_UNESCAPED_UNICODE);
$result = $this->orders_model->update($data,['id'=>$row['id']]);
if($result){
$this->orders_v2_entity->edit_order($data,$row);
//补充协商
$sa && $this->order_datas_entity->up_data($row['id'],['sa'=>$sa]);
throw new Exception('修改成功', API_CODE_SUCCESS);
}else{
throw new Exception('修改失败', ERR_PARAMS_ERROR);
}
}
private function detail($id){
$row = $this->orders_model->get(['id'=>$id]);
if(!$row){
throw new Exception('订单不存在', ERR_PARAMS_ERROR);
}
$orders_entity = new Orders_v2_entity();
$money_json = json_decode($row['money_json'],true);
$info_json = json_decode($row['info_json'],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'] : '';
$car_data = [];
if($row['brand_id'] && $row['s_id'] && $row['v_id']){
$car_data['车辆名称'] = $brand['name'].$series['name'];
$car_data['车辆级别'] = $version;
$car_data['车身颜色'] = $color;
$car_data['平台售价'] = $money_json['price_car'];
$money_json['price_coplus'] && $car_data['公司加价'] = sprintf("%.2f",$money_json['price_coplus']);
$money_json['price_color'] && $car_data['颜色加价'] = sprintf("%.2f",$money_json['price_color']);
$car_data['定金'] = sprintf("%.2f",$money_json['price_book']);
$car_price_list = [];
$money_json['price_discount'] && $car_price_list['优惠'] = sprintf("%.2f",$money_json['price_discount']);
$car_data['最终售价'] =[
'list' => $car_price_list,
'value' => sprintf("%.2f",$orders_entity->total_price($row['id']))
];
//服务费
$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']] = is_numeric($val['money']) ? sprintf("%.2f",$val['money']):'';
}
if($row['fines']){
$fines = json_decode($row['fines'],true);
$fines_list = [];
foreach ($fines as $item) {
$fines_list['list'][$item['txt']] = $item['price'] ? sprintf("%.2f",$item['price']) : '';
}
$fines_list['value'] = $money_json['price_fine_select'] ? sprintf("%.2f",$money_json['price_fine_select']) : 0.00;
$car_data['委托代办']['list']['精品选装'] = $fines_list;
}
}
$car_data['需开具本地发票'] = $row['if_local_bill'] ? '需要' : '不需要';
}
if(!$row['payway'] && $row['brand_id']){
$finance_row = $this->sys_finance_model->get(['id'=>$row['finance_id']],'title');
$car_data['按揭信息'] = $finance_row['title'] ? $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']);
}
if($info_json['pro']){
$pro_str = implode(" ",$info_json['pro']);
$info_json['c_address'] = str_replace($pro_str," ",$info_json['c_address']);
}
$data = [
'id' => $id,
'name' => $row['name'],
'mobile' => $row['mobile'],
'payway' => $row['brand_id'] ? $row['payway'] : '',
'car_data' => $car_data,
'pay_status' => $pay_status,
'pay_img' => $orders_entity->pay_img($row['id']),
'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'],
'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'] : '',
'deposit' => $money_json['price_book'] ? sprintf("%.2f",$money_json['price_book']): 0,
'disc_money' => $money_json['price_discount'] ? sprintf("%.2f",$money_json['price_discount']) : '',
'disc_fine_money' => $money_json['price_fine_discount'] ? sprintf("%.2f",$money_json['price_fine_discount']) : 0,
'srv_arr' => $row['srv_ids'] ? json_decode($row['srv_ids'],true) : [],
'cus_id' => $row['customer_id'],
'status' => $row['status'],
'if_fine' => $row['if_fine'] ? 1 : 0,
'main_type' => intval($row['main_type']),
'fines' => $row['fines'] ? json_decode($row['fines'],true) : [],
'address' => $info_json['c_address'] ? $info_json['c_address'] : '',
'pro' => $info_json['pro'] ? $info_json['pro'] : [],
'c_city_id' => $info_json['car_city_id'],
'if_local_bill' => $row['if_local_bill'],
];
if($row['main_type']){
$info_json['c_credit'] && $data['credit'] = $info_json['c_credit'];
$info_json['c_company'] && $data['company'] = $info_json['c_company'];
}else{
$data['owner_name'] = $row['owner_name'];
$data['owner_mobile'] = $row['owner_mobile'];
$data['owner_cardid'] = $info_json['c_owner_cardid'];
}
$data['cardid'] = $info_json['c_cardid'];
$inten_money && $data['inten_money'] = $inten_money;
//开票文件
$bill = $this->order_bills_model->get(['o_id'=>$row['id']],'file');
$data['bill_img'] = $bill['file'] ? build_qiniu_image_url($bill['file']) : '';
//选择随车物品
$ckcar_status = 0;
if($this->receiver_order_status_model->count(['o_id'=>$row['id'],'pid_status'=>3,'status'=>1]) && !$this->receiver_order_status_model->count(['o_id'=>$row['id'],'pid_status'=>4,'status'=>1])){
$ckcar_status = 1;
}
$delivery = $this->order_deliverys_model->get(['o_id'=>$row['id']],'status,info,tool,remark');
$ckcar_data = [
'info' => $delivery['info'] ? explode(',',$delivery['info']) : [],
'tool' => $delivery['tool'] ? explode(',',$delivery['tool']) : [],
'remark' => $delivery['remark'] ? $delivery['remark'] : ''
];
$data['ckcar_status'] = $ckcar_status;
$data['ckcar_data'] = $ckcar_data;
$price_book = $this->app_liche_orders_model->count(['o_id'=>$row['id'],'type'=>1,'status'=>1]); //定金是否支付
$data['price_book_status'] = $price_book ? true : false;
$price_srv = $this->app_liche_orders_model->count(['o_id'=>$row['id'],'type'=>2,'status'=>1]); //服务费是否支付
$data['price_srv_status'] = $price_srv ? true : false;
$price_last = $this->app_liche_orders_model->count(['o_id'=>$row['id'],'type'=>3,'status'=>1]); //尾款是否支付
$data['price_last_status'] = $price_last ? true : false;
$data['edit_status'] = $price_last ? 0 : 1;
$data['loan_status'] = true;
$loan_row = $this->order_loans_model->get(['o_id'=>$row['id']]);
$data['notify_file'] = [
'src' => $loan_row['notify_file'] ? build_qiniu_image_url($loan_row['notify_file']) : '',
'value' => $loan_row['notify_file'] ? $loan_row['notify_file'] : ''
];
$data['lend_file'] = [
'src' => $loan_row['lend_file'] ? build_qiniu_image_url($loan_row['lend_file']) : '',
'value' => $loan_row['lend_file'] ? $loan_row['lend_file'] : ''
];
$data['price_loan'] = $money_json['price_loan'] ? $money_json['price_loan'] : 0.00;
$data['num'] = $loan_row['num'] ? $loan_row['num'] : 0;
$data['admin_name'] = '';
if($row['sale_id']){
$admin_row = $this->app_user_model->get(['id'=>$row['sale_id']],'uname');
$data['admin_name'] = $admin_row['uname'];
}
//获取补充协议
$order_data = $this->receiver_order_datas_model->get(['o_id'=>$row['id']],'sa');
$data['sa'] = $order_data['sa'] ? $order_data['sa'] : '';
return $data;
}
private function lists(){
$group_id = $this->session['group_id'];
$uid = $this->session['uid'];
$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 = [
'status>=' => 0,
'biz_id' => $this->biz_id,
];
$group_id == 1 && $where['sale_id'] = $uid;//销售
if($group_id == 4 && $this->biz_id!=1){
$where['brand_id!='] = 3; //渠道经理过滤
}
if($keyword){
$where["(name='{$keyword}' or owner_name='{$keyword}')"] = null;
}
if($status == 11 ){ //进行中
$where['status'] = 0;
}elseif($status == 12 ){ //已完成
$where['status'] = 1;
}else{
strlen($status) && $where['status'] = $status;
}
$fileds = 'id,customer_id,name,mobile,brand_id,s_id,v_id,cor_id,incor_id,payway,status,c_time,money_json,if_num,info_json,main_type,owner_name,owner_mobile,item_id';
$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');
//旧订单数据
$old_rows = [];
$old_ids = array_unique(array_column($rows,'id'));
if($old_ids){
$ids_str = implode(',',$old_ids);
$where = [
"id in ({$ids_str})" => null,
];
$old_rows = $this->receiver_orders_model->map('id','status',$where,'','','','id,status');
}
$item_map = [];
$item_arr_ids = array_unique(array_column($rows,'item_id'));
if($item_arr_ids){
$str_ids = implode(',',$item_arr_ids);
$where = [
"id in ({$str_ids})" => null
];
$item_map = $this->items_model->map('id','vin',$where,'','','','id,vin');
}
//获取订单资料
$order_datas = $this->receiver_order_datas_model->get_map_by_oids(array_column($rows,'id'),'id,o_id,cardida,business_licence,car_img,register_img,ins_img,other_img');
$status_arr = $this->orders_model->get_status();
$old_status_arr = $this->receiver_orders_model->get_status();
foreach($rows as $key=>$val){
$info_json = json_decode($val['info_json'],true);
if($val['id']>=Orders_v2_entity::V2_START_ID){
$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'] : '';
$title = "{$brand_name}{$serie_name}-{$color}-{$version}";
$img_status = $this->order_datas_entity->data_status($order_datas[$val['id']][0]);
$ck_user_status = $this->receiver_order_status_model->count(['o_id'=>$val['id'],'pid_status'=>4,'status'=>2]); //用户已确认
$ck_admin_status = $this->receiver_order_status_model->count(['o_id'=>$val['id'],'pid_status'=>4,'status'=>1]); //顾问确认已确认
$ck_status = [
['value'=>'顾问确认','selected'=>$ck_admin_status?true:false],
['value'=>'用户确认','selected'=>$ck_user_status?true:false]
];
$other_data = [
'品牌车型' => ['type'=> 'text','value'=>$title,'bg_color'=>''],
'付款方式' => ['type'=> 'text','value'=>$val['payway']?'全款':'分期','bg_color'=>''],
];
$book_is_pay = $this->app_liche_orders_model->get(['o_id'=>$val['id'],'type'=>1]); //定金
$int_is_pay = $this->app_liche_orders_model->get(['o_id'=>$val['id'],'type'=>4]); //意向金
$p_row = $this->app_liche_orders_model->get(['o_id'=>$val['id'],'pid'=>0],'id'); //父订单
$last_row = $this->app_liche_orders_model->get(['o_id'=>$val['id'],'pid'=>$p_row['id'],'type'=>3],'id'); //尾款订单
//除去尾款已支付金额
$total_pay = $this->app_liche_orders_model->sum('total_price',['pid'=>$p_row['id'],'o_id'=>$val['id'],'status'=>1,'id!='=>$last_row['id']]);
//已支付尾款
$last_pay = $this->app_liche_orders_model->sum('total_price',['pid'=>$last_row['id'],'status'=>1]);
$is_pay_price = $total_pay['total_price'] + $last_pay['total_price'];
$need_pay = $this->orders_v2_entity->recevable_price($val['id'],false); //需要支付车款
$need_last_pay = $need_pay - $money_json['price_book'];
if($int_is_pay['status']){
$price_book_text = $int_is_pay['total_price']."(已支付)";
$other_data['意向金金额'] = ['type'=> 'text','value'=>$price_book_text,'bg_color'=>''];
}
if($book_is_pay){
$book_is_pay_text = $book_is_pay['status'] ? '已支付':'未支付';
$price_book_text = $money_json['price_book']."($book_is_pay_text)";
$other_data['定金金额'] = ['type'=> 'text','value'=>$price_book_text,'bg_color'=>''];
}
$last_is_pay_text = $is_pay_price >= $need_pay ? '已支付':'未支付';
$last_text = $need_last_pay."($last_is_pay_text)";
$other_data['尾款金额'] = ['type'=> 'text','value'=>$last_text,'bg_color'=>''];
if($int_is_pay['status'] || $book_is_pay['status'] || $this->app_liche_orders_model->count(['o_id'=>$val['id'],'type'=>6,'status'=>1])){
if(!$val['payway']){
$other_data['分期办理'] = ['type'=> 'text','value'=>$this->orders_status_entity->status_cn($val['id'],1),'bg_color'=>''];
}
$pc_cn = '待分配';
if($this->orders_status_entity->get_finish($val['id'],2,1)){ //已配车
$pc_cn = $item_map[$val['item_id']];
}
$other_data['车辆分配'] = ['type'=> 'text','value'=>$pc_cn,'bg_color'=>''];
$other_data['发票'] = ['type'=> 'text','value'=>$this->orders_status_entity->status_cn($val['id'],3),'bg_color'=>''];
if($val['main_type']){//公司
$other_data['营业执照'] = ['type'=> 'text','value'=>$img_status['business_licence']['text'],'bg_color'=>$img_status['business_licence']['color']];
}else{
$other_data['身份证'] = ['type'=> 'text','value'=>$img_status['cardida']['text'],'bg_color'=>$img_status['cardida']['color']];
}
$other_data['登记证'] = ['type'=> 'text','value'=>$img_status['register_img']['text'],'bg_color'=>$img_status['register_img']['color']];
$other_data['行驶证'] = ['type'=> 'text','value'=>$img_status['car_img']['text'],'bg_color'=>$img_status['car_img']['color']];
$other_data['保单'] = ['type'=> 'text','value'=>$img_status['ins_img']['text'],'bg_color'=>$img_status['ins_img']['color']];
$other_data['交车合照'] = ['type'=> 'text','value'=>$img_status['other_img']['text'],'bg_color'=>$img_status['other_img']['color']];
$other_data['交付'] = ['type'=> 'checkbox','value'=>'','lists' => $ck_status,'bg_color'=>''];
}
$other_data['订单日期'] = ['type'=> 'text','value'=>date('Y-m-d',$val['c_time']),'bg_color'=>''];
}else{
$price_intention = $money_json['price_intention'] ? $money_json['price_intention'] : '';
$other_data = [
'意向金' => ['type'=> 'text','value'=> $price_intention,'bg_color'=>''],
'订单日期' => ['type'=> 'text','value'=>date('Y-m-d',$val['c_time']),'bg_color'=>''],
];
}
$lists[] = [
'id' => $val['id'],
'cus_id' => $val['customer_id'],
'name' => $val['name'],
'mobile' => $val['mobile'],
'owner_name' => $val['main_type'] ? '' : $val['owner_name'],
'owner_mobile' => $val['main_type'] ? '' : $val['owner_mobile'],
'company' => $val['main_type'] ? $info_json['c_company'] : '',
'status_name' => $status_arr[$val['status']],
'other_data' => $other_data,
];
}else{//旧订单
$status = $old_rows[$val['id']] ? $old_rows[$val['id']] : 0;
$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,
'定金金额' => $money_json['price_book'],
'订单日期' => 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'],
'cus_id' => $val['customer_id'],
'name' => $val['name'],
'mobile' => $val['mobile'],
'owner_name' => '',
'owner_mobile' => '',
'company' => '',
'status_name' => $old_status_arr[$status],
'other_data' => $other_data,
'remark' => $this->orders_entity->get_remark($status)
];
}
}
}
$data = [
'list' => $lists,
'total' => $count
];
return $data;
}
//获取客户订单
protected function get_customer(){
$customer_id = intval($this->input_param('customer_id'));
if (!$customer_id) {
throw new Exception('参数错误', ERR_PARAMS_ERROR);
}
$where = ['status>=' => 0, 'customer_id' => $customer_id];
$total = $this->orders_model->count($where);
$lists = [];
if ($total) {
$rows = $this->orders_model->select($where, 'id desc', 0, 0, 'id,name,mobile,owner_name,owner_mobile,brand_id,s_id,v_id');
//品牌车型
$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'));
$attrs = $this->auto_attr_model->get_map_by_ids($version_arr, 'id,title');
foreach ($rows as $key => $val) {
$car_name = $name = $mobile = '';
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'] : '';
$version = isset($attrs[$val['v_id']]) ? $attrs[$val['v_id']][0]['title'] : '';
$car_name = "{$brand_name}{$serie_name}-{$version}";
}
$lists[] = [
'id' => $val['id'],
'car_name' => $car_name,
'name' => $val['owner_name'] ? $val['owner_name'] : $val['name'],
'mobile' => $val['owner_mobile'] ? $val['owner_mobile'] : $val['mobile']
];
}
}
$data = [
'list' => $lists,
'total' => $total
];
return $data;
}
//订单列表头部
protected function get_tabs(){
$lists = [
['key' =>11 ,'name' => '进行中'],
['key' =>12 ,'name' => '已完成'],
];
return $lists;
}
//修改用户基本信息
protected function put_info(){
$id = $this->input_param('id');
$owner_name = trim($this->input_param('owner_name'));
$owner_mobile = trim($this->input_param('owner_mobile'));
$owner_cardid = trim($this->input_param('owner_cardid'));
$company = $this->input_param('company');
$c_credit = $this->input_param('credit');
$address = $this->input_param('address');
$car_city_id = $this->input_param('c_city_id');
$pro = $this->input_param('pro');
$row = $this->orders_model->get(['id'=>$id]);
if(!$row){
throw new Exception('参数错误', ERR_PARAMS_ERROR);
}
$data = [];
$owner_name && $data['owner_name'] = $owner_name;
$owner_mobile && $data['owner_mobile'] = $owner_mobile;
$info_json = json_decode($row['info_json'],true);
$owner_cardid && $info_json['c_owner_cardid'] = $owner_cardid;
$c_credit && $info_json['c_credit'] = $c_credit;
$company && $info_json['c_company'] = $company;
if($pro){
$info_json['pro'] = $pro;
$address = implode(' ',$pro).$address;
}
$address && $info_json['c_address'] = $address;
$car_city_id && $info_json['car_city_id'] = $car_city_id;
$info_json && $data['info_json'] = json_encode($info_json,JSON_UNESCAPED_UNICODE);
$result = $this->orders_model->update($data,['id'=>$row['id']]);
if($result){
if($this->receiver_order_contracts_model->count(['o_id'=>$row['id'],'type in (0,1)'=>null])){
$this->receiver_order_contracts_model->delete(['o_id'=>$row['id'],'type in (0,1)'=>null]);
}
throw new Exception('修改成功', API_CODE_SUCCESS);
}else{
throw new Exception('修改失败', ERR_PARAMS_ERROR);
}
}
protected function put_status(){
$id = $this->input_param('id');
$row = $this->orders_model->get(['id'=>$id]);
if(!$row){
throw new Exception('订单不存在', ERR_PARAMS_ERROR);
}
$is_pay = $this->app_liche_orders_model->count(['o_id'=>$row['id'],'status'=>1]);
if($is_pay){
throw new Exception('修改失败', ERR_PARAMS_ERROR);
}
$result = $this->orders_model->update(['status'=>-1],['id'=>$row['id']]);
if($result){
$this->app_liche_orders_model->update(['status'=>-1],['o_id'=>$row['id']]);
throw new Exception('修改成功', API_CODE_SUCCESS);
}else{
throw new Exception('修改失败', ERR_PARAMS_ERROR);
}
}
//判断金额
protected function ck_money($money_json,$srv_arr,$biz){
if($biz['id']==70){
$fee_carno_limit = 700;
$price_finance_limit = 1000;
}elseif($biz['id']==63){
$fee_carno_limit = 600;
$price_finance_limit = 0;
}else{
$fee_carno_limit = 1000;
$price_finance_limit = 1000;
}
$total_limit = $fee_carno_limit+$price_finance_limit;
if($biz['type']==1){ //直营店
$srv_ids = array_column($srv_arr,'id');
if(!in_array(1,$srv_ids)){
throw new Exception('保险必填', ERR_PARAMS_ERROR);
}
if(!in_array(2,$srv_ids)){
throw new Exception('上牌必填', ERR_PARAMS_ERROR);
}
if($money_json['fee_carno'] && $money_json['price_finance']){//上牌和金融都有填写
$ff_price = $money_json['fee_carno'] + $money_json['price_finance'];
if($ff_price<$total_limit){
throw new Exception('上牌和金融不得低于'.$total_limit, ERR_PARAMS_ERROR);
}
}elseif($money_json['fee_carno'] || $money_json['price_finance']){ //上牌和金融只填写一项
if($money_json['fee_carno']<$fee_carno_limit && !$money_json['price_finance']){
throw new Exception('上牌不得低于'.$fee_carno_limit, ERR_PARAMS_ERROR);
}
if(!$money_json['fee_carno'] && $money_json['price_finance']<$price_finance_limit){
throw new Exception('金融不得低于'.$price_finance_limit, ERR_PARAMS_ERROR);
}
}else{ //挂牌和金融都没填写
throw new Exception('上牌和金融不得低于1000', ERR_PARAMS_ERROR);
}
}
}
}