Files
liche/common/libraries/receiver/Orders_entity.php
T
2021-12-24 15:13:48 +08:00

787 lines
36 KiB
PHP

<?php
/**
* 订单
*/
class Orders_entity{
const SRV_MCH_ID = '1612636924'; //收取服务费商户号 厦门狸车服务
const PRICE_FINANCE = 1000; //金融服务费写死
const PRICE_FINANCE_NZ = 2000; //哪吒金融服务费
const API_ADMIN_IMG = 'https://img.liche.cn/liche/2021/08/7725fc9ab0dc0ba1/f3c84cf3b805bac4.png'; //api 客服二维码
const API_PAY_IMG = 'https://img.liche.cn/liche/1634022931.png'; //api 意向金跳转支付二维码
const API_CREATE_PAY_IMG = 'https://img.liche.cn/liche/2021/11/65b34b962f5f06b3/62e7d28d77bfe404.png'; //api 狸车我的页面
const ODER_ID = 527; //订单id大于100之后定金都到销售公司
private $ci;
private $order_row;
public function __construct(){
$this->ci = & get_instance();
$this->ci->load->model('receiver/receiver_customers_model','customers_model');
$this->ci->load->model('receiver/order/receiver_orders_model','orders_model');
$this->ci->load->model('receiver/order/receiver_order_signs_model','signs_model');
$this->ci->load->model('receiver/order/receiver_order_contracts_model');
$this->ci->load->model('receiver/receiver_service_package_model','package_model');
$this->ci->load->model('receiver/receiver_services_model','services_model');
$this->ci->load->model('sys/sys_city_model');
$this->ci->load->model("sys/sys_company_model");
$this->ci->load->model('apporder/order_purchase_model');
$this->ci->load->model('auto/auto_brand_model');
$this->ci->load->model('auto/auto_series_model');
$this->ci->load->model("biz/biz_model");
$this->ci->load->model('app/liche/app_liche_orders_model');
}
/*
* 获取合同h5地址
* @param $id int 订单id
* @param $type int 合同类型(0整车合同 1代理协议 2确定信息 3交接信息)
*/
public function get_contract_h5($id,$type,$wxapp=''){
$url = http_host_com('api');
$res = $path = '';
switch($type){
case 0;
$path = '/wxapp/licheb/protocol/car';
$title = '车辆买卖合同';
break;
case 1:
$path = '/wxapp/licheb/protocol/agent';
$title = '委托服务协议';
break;
case 2:
$path = '/wxapp/licheb/protocol/car_ck';
$title = '车辆信息确认单';
break;
case 3:
$path = '/wxapp/licheb/protocol/car_fh';
$title = '车辆交付表';
break;
case 4:
$path = '/wxapp/licheb/protocol/car_sp';
$title = '车辆买卖合同补充协议书';
break;
case 5:
$path = '/wxapp/licheb/protocol/agent_sp';
$title = '委托服务补充协议书';
break;
default:
}
if($path){
$res = $url.$path.'?id='.$id.'&wxapp='.$wxapp;
}
return [$res,$title];
}
/**
* 签完成协议后操作
* @param $oid int 订单id
* @param $userinfo array 小程序用户信息
*/
public function sign_after_v2($oid,$userinfo){
$uid = $userinfo['id'] ? $userinfo['id'] : 0;
$this->order_row = $this->ci->orders_model->get(['id'=>$oid]);
if(!$this->order_row){
return false;
}
$pay_money = $this->ci->app_liche_orders_model->get(['o_id'=>$oid,'type'=>4,'status'=>1]); //已支付意向金订单
if(!$pay_money){//未支付意向订单设置删除
$this->ci->app_liche_orders_model->update(['status'=>-1],['o_id'=>$oid,'type'=>4,'status'=>0]);
}
$money_json = json_decode($this->order_row['money_json'],true);
$deposit_count = $this->ci->app_liche_orders_model->count(['o_id'=>$oid,'type'=>1,'status'=>1]); //定金已支付
$need_pay_money = $money_json['price_book'] - $pay_money['total_price'];
if($need_pay_money > 0 && !$deposit_count){
$this->ci->signs_model->update(['status'=>1],['o_id'=>$this->order_row['id']]);
$srv_money = $this->order_srv_money($oid);
//获取门店信息
$biz = $this->ci->biz_model->get(['id'=>$this->order_row['biz_id']],'company_id,srv_company_id');
if($this->order_row['brand_id']==5){ //品牌哪吒 销售公司改为泉州狸车
$biz['company_id'] = 2;
}
if($srv_money < $money_json['price_book'] || $pay_money || $oid > self::ODER_ID){ //服务费小于支付定金或有意向金订单 给销售公司 id大于100之后定金都到销售公司
$company = $this->ci->sys_company_model->get(['id'=>$biz['company_id']],'wx_mchid');
$mch_id = $company['wx_mchid'];
}else{ //以后废除
$mch_id = self::SRV_MCH_ID;
if($biz['srv_company_id']){
$company = $this->ci->sys_company_model->get(['id'=>$biz['srv_company_id']],'wx_mchid');
$company['wx_mchid'] && $mch_id = $company['wx_mchid'];
}
}
$entrust_user = $this->entrust_user($this->order_row['id']);
$p_row = $this->ci->app_liche_orders_model->get(['o_id'=>$this->order_row['id'],'pid'=>0,'status>='=>0]);
if(!$p_row){
$sid = create_order_no(350200,'liche',1,0);
$add_data = [
'o_id' => $this->order_row['id'],
'uid' => $uid,
'sid' => $sid,
'pid' => 0,
'brand_id' => $this->order_row['brand_id'],
's_id' => $this->order_row['s_id'],
'v_id' => $this->order_row['v_id'],
'cor_id' => $this->order_row['cor_id'],
'incor_id' => $this->order_row['incor_id'],
'c_time' => time()
];
$entrust_user['id'] && $add_data['entrust_uid'] = $entrust_user['id'];
$pid = $this->ci->app_liche_orders_model->add($add_data);
}else{
$pid = $p_row['id'];
}
$res = false;
$sub_row = $this->ci->app_liche_orders_model->get(['o_id'=>$this->order_row['id'],'type'=>1,'status>='=>0]);
if($pid && !$sub_row){
$sid = create_order_no(350200,'liche',1,1);
$sub_data = [
'o_id' => $this->order_row['id'],
'uid' => $uid,
'sid' => $sid,
'mch_id' => $mch_id,
'pid' => $pid,
'type' => 1,
'brand_id' => $this->order_row['brand_id'],
's_id' => $this->order_row['s_id'],
'v_id' => $this->order_row['v_id'],
'cor_id' => $this->order_row['cor_id'],
'incor_id' => $this->order_row['incor_id'],
'total_price' => $need_pay_money,
'c_time' => time()
];
$entrust_user['id'] && $sub_data['entrust_uid'] = $entrust_user['id'];
$res = $this->ci->app_liche_orders_model->add($sub_data);
}
}else{
if($pay_money['status'] || $deposit_count){
$this->ci->signs_model->update(['status'=>2],['o_id'=>$oid]);
if($this->order_row['payway']){//全款
$status = 2;
$this->ci->load->model('receiver/order/receiver_order_ckcars_model','next_model');
}else{
$status = 1;
$this->ci->load->model('receiver/order/receiver_order_loans_model','next_model');
}
$this->ci->orders_model->update(['status'=>$status],['id'=>$this->order_row['id']]);
//判断下一步是否存在
if(!$this->ci->next_model->get(['o_id'=>$this->order_row['id']])){
$this->ci->next_model->add(['o_id'=>$this->order_row['id'],'c_time'=>time()]);
}
}
}
return $res;
}
/**
* 创建定金消费订单
* @param $mch_id string 微信商户号
* @param $oder array 订单数据
* @param $app_id int 应用id
* @param $userinfo array 用户信息
* return boolean
*/
public function c_order($mch_id,$order,$app_id,$userinfo){
$brand = $this->ci->auto_brand_model->get(['id'=>$order['brand_id']],'name');
$series = $this->ci->auto_series_model->get(['id'=>$order['s_id']],'name');
$car_json = json_decode($order['car_json'],true);
$color = isset($car_json['color']) ? $car_json['color'] : '';
$sid = create_order_no(350200,'liche',1,2);
$jsondata['car'] = $car_json;
if($color['jsondata']['img']){
$jsondata['cover'] = $color['jsondata']['img'];
}
$add_data = [
'app_id' => $app_id,
'app_uid' => $userinfo['uid'],
'sid' => $sid,
'mch_id' => $mch_id,
'item_id' => $order['id'],
'item_title' => $brand['name'].$series['name'],
'item_num' => 1,
'type' => 4,
'item_price' => $order['deposit'],
'total_price' => $order['deposit'],
'uname' => $userinfo['nickname'],
'mobile' => $userinfo['mobile'],
'payway' => 1,
'status' => 1,
'status_detail' => 11,
'jsondata' => json_encode($jsondata,JSON_UNESCAPED_UNICODE),
'c_time' => time(),
'cf_id' => $order['id']
];
$result = $this->ci->order_purchase_model->add($add_data);
return $result;
}
/*
* 获取签名图片
* @param $oid int 订单id
* @param $type array 类型
*/
public function get_sign_imgs($oid,$type){
$where = [
'o_id' => $oid,
'status' => 1
];
$types = implode(',',$type);
if($types){
$where["type in ($types)"] = null;
}
$contracts = $this->ci->receiver_order_contracts_model->select($where,'type asc','','','imgs');
$data = [];
if($contracts){
foreach($contracts as $key => $val){
$imgs = json_decode($val['imgs'],true);
foreach($imgs as $item){
$data[] = build_qiniu_image_url($item);
}
}
}
return $data;
}
/**
* 更新订单用户信息
* @param $oid int 订单id
* @param $info array() 腾讯云识别返回参数
*/
public function up_info($oid,$info){
$row = $this->ci->orders_model->get(['id'=>$oid]);
if(!$row){
return false;
}
$info_json = json_decode($row['info_json'],true);
$info['Name'] && $info_json['name'] = $info['Name'];
$info['Sex'] && $info_json['sex'] = $info['Sex'];
$info['Nation'] && $info_json['nation'] = $info['Nation'];
$info['Birth'] && $info_json['birth'] = date('Y-m-d',strtotime($info['Birth']));
$info['Address'] && $info_json['address'] = $info['Address'];
$info['IdNum'] && $info_json['cardid'] = $info['IdNum'];
$update = [
'info_json' => json_encode($info_json,JSON_UNESCAPED_UNICODE)
];
$result = $this->ci->orders_model->update($update,['id'=>$row['id']]);
return $result;
}
/**
* 添加日志
* @param $o_id int 订单id
* @param $uid int 操作用户id
* @param $uname int 操作用户名
* @param $content string 日志内容
* @param $type int 操作类型 (0普通日志 1短信 2拨打电话 3修改订单信息)
* @param $cf_platform string 来源 (wxapp小程序 admin后台)
*/
public function add_log($o_id,$uid,$uname,$content,$type='',$cf_platform='wxapp',$jsondata = ''){
$this->ci->load->model('receiver/order/receiver_order_oplogs_model','order_oplogs_model');
$add_data = [
'order_id' => $o_id,
'uid' => $uid,
'log' => $content,
'c_time' => time()
];
$uname && $add_data['uname'] = $uname;
$type && $add_data['type'] = $type;
$cf_platform && $add_data['cf_platform'] = $cf_platform;
$jsondata && $add_data['log_json'] = $jsondata;
$result = $this->ci->order_oplogs_model->add($add_data);
return $result;
}
/**
* 确认交车
* @param $oid int 订单id
* @param $app_id int 小程序id
* return boolean
*/
public function finish_after($oid,$app_id){
$this->ci->load->model('receiver/receiver_clues_model','clues_model');
$row = $this->ci->orders_model->get(['id'=>$oid]);
if(!$row){
return false;
}
$customers = $this->ci->customers_model->get(['id'=>$row['rid']],'rid');
$clues = $this->ci->clues_model->get(['id'=>$customers['rid']],'mobile,recommend_id');
$res = false;
//获取线索生成的支付订单
$clu_order = $this->ci->order_purchase_model->get(['type'=>3,'status'=>2,'cf_id'=>$customers['rid'],'app_id'=>$app_id]);
if($clu_order){
$res = $this->ci->order_purchase_model->update(['status'=>3],['id'=>$clu_order['id']]);
}
//获取合同生成的支付订单
//$o_order = $this->ci->order_purchase_model->get(['type'=>4,'status'=>2,'cf_id'=>$row['id'],'app_id'=>$app_id]);
//if($o_order){
$res = $this->ci->order_purchase_model->update(['status'=>3],['item_id'=>$oid,'app_id'=>$app_id,'status'=>2]);
//}
if($clues['recommend_id']){ //推荐人获得佣金
$this->ci->load->library('entity/account_entity');
$account_entity = new Account_entity($app_id,['order_id'=>$row['sid']]);
$account = $account_entity->get_account($clues['recommend_id'],'user');
if(!$account){
$a_id = $account_entity->new_account($clues['recommend_id'],'user');
}else{
$a_id = $account['id'];
}
$money=500;
$log_msg = $clues['mobile'].'成交';
$sql_arr = $account_entity->charge($a_id,$money,$log_msg);
$this->ci->load->helper('db');
$ret = trans_sql($sql_arr);
}
return $res;
}
/**
* 获取订单服务费
* @param $oid int 订单id
* @param $res_type int 返回数据格式
* return float
*/
public function order_srv_money($oid,$res_type = ''){
if(!$this->order_row){
$this->order_row = $this->ci->orders_model->get(['id'=>$oid]);
}
$money_json = json_decode($this->order_row['money_json'],true);
$services = [];
if($this->order_row['srv_ids']){
$services = $this->ci->services_model->select(["id in ({$this->order_row['srv_ids']})"=>null],'','','','title,field_name');
}
$srv_price = 0;
$list = [];//明细
if($services){
foreach($services as $key=>$val){
$field_arr = explode('.',$val['field_name']);
if(isset($money_json[$field_arr[1]]) && $field_arr[1]!='price_insure'){ //保险费不收
$srv_price += $money_json[$field_arr[1]];
$list[] = [
'title' => $val['title'],
'money' => $money_json[$field_arr[1]]
];
}
}
}
$jsondata = json_decode($this->order_row['jsondata'],true);
if($jsondata['if_fine']){ //选择精品包
$fine_price = $money_json['price_fine'];
if($money_json['price_fine_discount']){ //优惠精品价格
$fine_price = $fine_price - $money_json['price_fine_discount'];
}
$srv_price += $fine_price;
$list[] = [
'title' => '精品尊享包',
'money' => $fine_price
];
}
$result['total'] = $srv_price;
$result['list'] = $list;
return $res_type ? $result:$srv_price;
}
//确认车辆完成创建两个支付订单
public function check_finish_v2($oid,$userinfo){
$this->ci->load->helper('order');
$uid = $userinfo['id'] ? $userinfo['id'] : 0;
$this->order_row = $this->ci->orders_model->get(['id'=>$oid]);
$p_row = $this->ci->app_liche_orders_model->get(['pid'=>0,'o_id'=>$oid,'status>='=>0],'id'); //父订单
if(!$this->order_row || !$p_row){
return false;
}
if($this->ci->app_liche_orders_model->count(['status>='=>0,'type'=>3,'o_id'=>$oid])){ //存在尾款订单
return true;
}
//获取门店信息
$biz = $this->ci->biz_model->get(['id'=>$this->order_row['biz_id']],'company_id,srv_company_id');
if($this->order_row['brand_id']==5){ //品牌哪吒 销售公司改为泉州狸车
$biz['company_id'] = 2;
}
$company = $this->ci->sys_company_model->get(['id'=>$biz['company_id']]);
$srv_company = $this->ci->sys_company_model->get(['id'=>$biz['srv_company_id']]);
$pay_money = $this->ci->app_liche_orders_model->get(['o_id'=>$oid,'type'=>4,'status'=>1,'o_id!='=>253]); //已支付意向金订单
$srv_money = $this->order_srv_money($oid); //服务费
$money_json = json_decode($this->order_row['money_json'],true);
$money_json['price_discount'] && $money_json['price_car'] = $money_json['price_car'] - $money_json['price_discount'];
if(!$this->order_row['payway'] && !$money_json['price_loan']){//贷款未填写首付
return false;
}
if($oid > self::ODER_ID){
$to_srv_price = $srv_money;
if($this->order_row['payway']){ //全款
$to_com_price = $money_json['price_car'] - $money_json['price_book'];
}else{ //分期
$to_com_price = $money_json['price_car']-$money_json['price_loan'] - $money_json['price_book'];
}
$to_com_price = $to_com_price>0 ? $to_com_price : 0;
}else{ //以后废除
if($this->order_row['payway']){ //全款
if($srv_money < $money_json['price_book'] || $pay_money){ //服务费小于定金
$to_srv_price = $srv_money;
$to_com_price = $money_json['price_car'] - $money_json['price_book'];
$to_com_price = $to_com_price>0 ? $to_com_price : 0;
}else{
$to_com_price = $money_json['price_car']; //给销售公司金额 裸车价格
$to_srv_price = $srv_money - $money_json['price_book']; //给服务公司金额 裸车价格+服务费-给销售公司金额-定金
$to_srv_price = $to_srv_price>0 ? $to_srv_price : 0;
}
}else{ //分期
if($srv_money < $money_json['price_book'] || $pay_money){ //服务费小于定金
$to_srv_price = $srv_money;
$to_com_price = $money_json['price_car']-$money_json['price_loan'] - $money_json['price_book'];
$to_com_price = $to_com_price>0 ? $to_com_price : 0;
}else{
$to_com_price = $money_json['price_car']-$money_json['price_loan']; //给销售公司金额
$to_srv_price = $srv_money - $money_json['price_book']; //给服务公司的金额 服务费-定金
$to_srv_price = $to_srv_price>0 ? $to_srv_price : 0;
}
}
}
$add_data = [];
$entrust_user = $this->entrust_user($oid);
if($to_srv_price>0 && !$this->ci->app_liche_orders_model->count(['o_id'=>$this->order_row['id'],'type'=>2,'status>='=>0])){
$order_type = 2;
$sid = create_order_no(350200,'liche',1,$order_type);
$add_data[] = [
'o_id' => $this->order_row['id'],
'uid' => $uid,
'entrust_uid' => $entrust_user['id'] ? $entrust_user['id'] : 0,
'sid' => $sid,
'mch_id' => $srv_company ? $srv_company['wx_mchid'] : self::SRV_MCH_ID,
'pid' => $p_row['id'],
'brand_id' => $this->order_row['brand_id'],
's_id' => $this->order_row['s_id'],
'v_id' => $this->order_row['v_id'],
'cor_id' => $this->order_row['cor_id'],
'incor_id' => $this->order_row['incor_id'],
'total_price' => $to_srv_price,
'type' => $order_type,
'c_time' => time()
];
}
if($to_com_price>0 && !$this->ci->app_liche_orders_model->count(['o_id'=>$this->order_row['id'],'type'=>3,'status>='=>0])){
$order_type = 3;
$sid = create_order_no(350200,'liche',1,$order_type);
$add_data[] = [
'o_id' => $this->order_row['id'],
'uid' => $uid,
'entrust_uid' => $entrust_user['id'] ? $entrust_user['id'] : 0,
'sid' => $sid,
'mch_id' => $company['wx_mchid'],
'pid' => $p_row['id'],
'brand_id' => $this->order_row['brand_id'],
's_id' => $this->order_row['s_id'],
'v_id' => $this->order_row['v_id'],
'cor_id' => $this->order_row['cor_id'],
'incor_id' => $this->order_row['incor_id'],
'total_price' => $to_com_price,
'type' => $order_type,
'c_time' => time()
];
}
$result = false;
if($add_data){
$result = $this->ci->app_liche_orders_model->add_batch($add_data);
}
return $result;
}
//创建意向金订单
public function c_intention($oid,$userinfo,$money){
$this->order_row = $this->ci->orders_model->get(['id'=>$oid]);
if(!$this->order_row){
return false;
}
$uid = $userinfo['id'] ? $userinfo['id'] : 0;
$p_row = $this->ci->app_liche_orders_model->get(['o_id'=>$this->order_row['id'],"(uid={$uid} or entrust_uid={$uid})"=>null,'pid'=>0,'status>='=>0]);
$entrust_user = $this->entrust_user($oid);
if(!$p_row){
$sid = create_order_no(350200,'liche',1,0);
$add_data = [
'o_id' => $this->order_row['id'],
'uid' => $uid,
'entrust_uid' => $entrust_user['id'] ? $entrust_user['id'] : 0,
'sid' => $sid,
'pid' => 0,
'brand_id' => $this->order_row['brand_id'],
's_id' => $this->order_row['s_id'],
'v_id' => $this->order_row['v_id'],
'cor_id' => $this->order_row['cor_id'],
'incor_id' => $this->order_row['incor_id'],
'c_time' => time()
];
$pid = $this->ci->app_liche_orders_model->add($add_data);
}else{
$pid = $p_row['id'];
}
$res = false;
$sub_row = $this->ci->app_liche_orders_model->get(['o_id'=>$this->order_row['id'],"(uid={$uid} or entrust_uid={$uid})"=>null,'type'=>4,'status>='=>0]);
if($pid && !$sub_row){
//获取门店信息
$biz = $this->ci->biz_model->get(['id'=>$this->order_row['biz_id']],'company_id,srv_company_id');
$company = $this->ci->sys_company_model->get(['id'=>$biz['company_id']],'wx_mchid');
$mch_id = $company['wx_mchid'];
$sid = create_order_no(350200,'liche',1,4);
$sub_data = [
'o_id' => $this->order_row['id'],
'uid' => $uid,
'entrust_uid' => $entrust_user['id'] ? $entrust_user['id'] : 0,
'sid' => $sid,
'mch_id' => $mch_id,
'pid' => $pid,
'type' => 4,
'brand_id' => $this->order_row['brand_id'],
's_id' => $this->order_row['s_id'],
'v_id' => $this->order_row['v_id'],
'cor_id' => $this->order_row['cor_id'],
'incor_id' => $this->order_row['incor_id'],
'total_price' => $money,
'c_time' => time()
];
$res = $this->ci->app_liche_orders_model->add($sub_data);
}
return $res;
}
/**
* 根据订单状态获取备注
* @param $status int 状态值
*/
public function get_remark($status){
$config = [
0 => '点击“邀请签名”,请客户签字并缴交定金',
1 => '请及时将办理进度及资料发送给平台客服',
2 => '请邀请客户至狸车小程序签字确认车辆',
3 => '请及时上传客户身份证/营业执照',
4 => '请及时上传行驶证、登记证、保单等资料',
5 => '请完成车辆交付信息,“确认交付”后请客户签字确认'
];
return $config[$status] ? $config[$status] : '';
}
/*
* 修改完订单信息后操作
* @param array() $new_data 修改后的数据
* @param array() $row 修改前数据
*/
public function edit_order($new_data,$row,$uid,$uname){
//更新购车订单车辆信息
$up_data = [
'brand_id' => $new_data['brand_id'],
's_id' => $new_data['s_id'],
'v_id' => $new_data['v_id'],
'cor_id' => $new_data['cor_id'],
'incor_id' => $new_data['incor_id'],
];
$this->ci->app_liche_orders_model->update($up_data,['o_id'=>$row['id']]);
$sign_row = $this->ci->signs_model->get(['o_id'=>$row['id']]);
if($sign_row && $row['status']>0){
$diff_key_arr = ['s_id','v_id','cor_id','incor_id','payway','srv_ids']; //修改其中一项需要签补充协议
$need_resign = false;
foreach($diff_key_arr as $item){
if($new_data[$item]!=$row[$item]){
$need_resign = true;
break;
}
}
//修改优惠金额
$money_json = json_decode($row['money_json'],true);
$new_money_json = json_decode($new_data['money_json'],true);
$jsondata = json_decode($row['jsondata'],true);
$new_jsondata = json_decode($new_data['jsondata'],true);
if($money_json['price_discount']!=$new_money_json['price_discount']){
$need_resign = true;
}
//修改精品
if($jsondata['if_fine']!=$new_jsondata['if_fine']){
$need_resign = true;
}
if($jsondata['if_fine'] && $new_jsondata['if_fine']){
$price_fine = $money_json['price_fine_discount'] ? $money_json['fine_price'] - $money_json['price_fine_discount'] : $money_json['fine_price'];
$new_price_fine = $new_money_json['price_fine_discount'] ? $new_money_json['fine_price'] - $new_money_json['price_fine_discount'] : $new_money_json['fine_price'];
if($price_fine!=$new_price_fine){
$need_resign = true;
}
}
if($new_data['payway']!=$row['payway']){ //修改付款方式
$this->ci->load->model('receiver/order/receiver_order_loans_model');
$this->ci->receiver_order_loans_model->delete(['o_id'=>$row['id']]);
}
if($need_resign){
$content = '修改订单信息';
$jsondata = json_encode($row,JSON_UNESCAPED_UNICODE);
$this->add_log($row['id'],$uid,$uname,$content,3,'wxapp',$jsondata); //添加日志
$this->ci->receiver_order_contracts_model->delete(['o_id'=>$row['id'],'type in (4,5)'=>null]); //删除补充合同
$this->ci->signs_model->update(['status'=>0],['id'=>$sign_row['id']]);
$this->ci->orders_model->update(['status'=>0],['id'=>$row['id']]);
//未签合同删除旧合同文件并且删除未支付订单
$this->ci->app_liche_orders_model->delete(['o_id'=>$row['id'],'status'=>0,'pid!='=>0,'type!='=>3]);
}
}else{
if($sign_row){ //未签合同删除旧合同文件并且删除未支付订单
$this->ci->receiver_order_contracts_model->delete(['o_id'=>$row['id']]);
$this->ci->signs_model->update(['status'=>0],['o_id'=>$row['id']]);
//全部未支付删除所有订单
if(!$this->ci->app_liche_orders_model->count(['o_id'=>$row['id'],'status'=>1])){
$this->ci->app_liche_orders_model->delete(['o_id'=>$row['id'],'status'=>0]);
}else{
$this->ci->app_liche_orders_model->delete(['o_id'=>$row['id'],'status'=>0,'pid!='=>0]);
}
}else{
$this->ci->signs_model->add(['o_id'=>$row['id'],'c_time'=>time()]);
}
}
}
/**
* 完成补充协议签名
* @param int $oid 订单id
* return boolean
*/
public function finish_sp($oid){
!$this->order_row && $this->order_row = $this->ci->orders_model->get(['id'=>$oid]);
if(!$this->order_row){
return false;
}
$this->ci->load->model('receiver/order/receiver_order_ckcars_model');
$this->ci->signs_model->update(['status'=>2],['o_id'=>$oid]);
if($this->order_row['payway']){//全款
$res = $this->ci->orders_model->update(['status'=>2],['id'=>$oid]);
if(!$this->ci->receiver_order_ckcars_model->get(['o_id'=>$oid])){
$this->ci->receiver_order_ckcars_model->add(['o_id'=>$oid,'c_time'=>time()]);
}
}else{
$this->ci->load->model('receiver/order/receiver_order_loans_model');
if($this->ci->receiver_order_loans_model->get(['o_id'=>$oid])){
if($this->ci->receiver_order_ckcars_model->get(['o_id'=>$oid])) {
$res = $this->ci->orders_model->update(['status'=>2],['id'=>$oid]);
}else{
$res = $this->ci->orders_model->update(['status'=>1],['id'=>$oid]);
}
}else{
$res = $this->ci->orders_model->update(['status'=>1],['id'=>$oid]);
$this->ci->receiver_order_loans_model->add(['o_id'=>$oid,'c_time'=>time()]);
}
}
$money_json = json_decode($this->order_row['money_json'],true);
$money_json['price_discount'] && $money_json['price_car'] = $money_json['price_car'] - $money_json['price_discount'];
$p_row = $this->ci->app_liche_orders_model->get(['o_id'=>$this->order_row['id'],'pid'=>0,'status>='=>0]);
//获取门店信息
$biz = $this->ci->biz_model->get(['id'=>$this->order_row['biz_id']],'company_id,srv_company_id');
if($this->order_row['brand_id']==5){ //品牌哪吒 销售公司改为泉州狸车
$biz['company_id'] = 2;
}
$company = $this->ci->sys_company_model->get(['id'=>$biz['company_id']]);
$srv_company = $this->ci->sys_company_model->get(['id'=>$biz['srv_company_id']]);
//重新生成购车订单
$srv_money = $this->order_srv_money($oid);
$old_srv_money = $this->ci->app_liche_orders_model->sum('total_price',['o_id'=>$oid,'status'=>1,'type'=>2]);
if($srv_money>$old_srv_money['total_price'] && $srv_money){ //多出服务费
$order_type = 2;
$add_data[] = [
'o_id' => $this->order_row['id'],
'uid' => $p_row['uid'],
'entrust_uid' => $p_row['entrust_uid'],
'sid' => create_order_no(350200,'liche',1,$order_type),
'mch_id' => $srv_company ? $srv_company['wx_mchid'] : self::SRV_MCH_ID,
'pid' => $p_row['id'],
'brand_id' => $this->order_row['brand_id'],
's_id' => $this->order_row['s_id'],
'v_id' => $this->order_row['v_id'],
'cor_id' => $this->order_row['cor_id'],
'incor_id' => $this->order_row['incor_id'],
'total_price' => $srv_money-$old_srv_money['total_price'],
'type' => $order_type,
'c_time' => time()
];
}
if($this->order_row['payway']){ //全款
$to_com_price = $money_json['price_car'] - $money_json['price_book']; //车价-定金
}else{ //分期
$to_com_price = $money_json['price_car']-$money_json['price_loan'] - $money_json['price_book']; //车价-贷款首付-定金
}
$to_com_price = $to_com_price>0 ? $to_com_price : 0;
if($to_com_price){
$last_row = $this->ci->app_liche_orders_model->get(['o_id'=>$oid,'type'=>3,'pid'=>$p_row['id']]);//尾款父订单
$old_last_money = $this->ci->app_liche_orders_model->sum('total_price',['o_id'=>$oid,'type'=>3,'pid'=>$last_row['id'],'status'=>1]); //已支付尾款
if($last_row){ //存在尾款订单
if($to_com_price>$last_row['total_price']){ //尾款高出
$this->ci->app_liche_orders_model->update(['status'=>0,'total_price'=>$to_com_price],['id'=>$last_row['id']]);
}
if($to_com_price<$last_row['total_price'] && $to_com_price>$old_last_money['total_price']){ //低于旧订单并且已支付要小于新尾款金额
$this->ci->app_liche_orders_model->update(['status'=>0,'total_price'=>$to_com_price],['id'=>$last_row['id']]);
}
}else{
$order_type = 3;
$add_data[] = [
'o_id' => $this->order_row['id'],
'uid' => $p_row['uid'],
'entrust_uid' => $p_row['entrust_uid'],
'sid' => create_order_no(350200,'liche',1,$order_type),
'mch_id' => $company['wx_mchid'],
'pid' => $p_row['id'],
'brand_id' => $this->order_row['brand_id'],
's_id' => $this->order_row['s_id'],
'v_id' => $this->order_row['v_id'],
'cor_id' => $this->order_row['cor_id'],
'incor_id' => $this->order_row['incor_id'],
'total_price' => $to_com_price,
'type' => $order_type,
'c_time' => time()
];
}
}
if($add_data){
$this->ci->app_liche_orders_model->add_batch($add_data);
}
return $res;
}
/**
* 计算订单应收金额
* @param $oid int 订单id
* return float
*/
public function recevable_price($oid){
!$this->order_row && $this->order_row = $this->ci->orders_model->get(['id'=>$oid]);
if(!$this->order_row){
return false;
}
$money_json = json_decode($this->order_row['money_json'],true);
$money_json['price_discount'] && $money_json['price_car'] = $money_json['price_car'] - $money_json['price_discount'];
$srv_money = $this->order_srv_money($oid);
$recevable_price = $money_json['price_car'] + $srv_money;
if(!$this->order_row['payway']){ //分期
$recevable_price -= $money_json['price_loan'];
}
return $recevable_price;
}
/**
* 获取委托代理用户信息
* @param $oid int 订单id
* return array
*/
public function entrust_user($oid){
$this->ci->load->model('app/liche/app_liche_users_model');
!$this->order_row && $this->order_row = $this->ci->orders_model->get(['id'=>$oid]);
if(!$this->order_row){
return [];
}
$info_json = json_decode($this->order_row['info_json'],true);
if($this->order_row['ifentrust'] && $info_json['entrust_mobile']){
$user = $this->ci->app_liche_users_model->get(['mobile'=>$info_json['entrust_mobile']]);
return $user;
}else{
return [];
}
}
}
?>