Files
liche/common/libraries/receiver/Orders_entity.php
T
2021-09-01 10:36:34 +08:00

379 lines
16 KiB
PHP

<?php
/**
* 订单
*/
class Orders_entity{
private $ci;
const SRV_MCH_ID = '1612636924'; //收取服务费商户号 厦门狸车服务
public function __construct(){
$this->ci = & get_instance();
}
/*
* 获取合同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;
default:
}
if($path){
$res = $url.$path.'?id='.$id.'&wxapp='.$wxapp;
}
return [$res,$title];
}
/**
* 签完成协议后操作
* @param $oid int 订单id
* @param $app_id int 小程序id
* @param $userinfo array 小程序用户信息
*/
public function sign_after($oid,$app_id,$userinfo){
$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/receiver_customers_model','customers_model');
$this->ci->load->model('apporder/order_purchase_model');
$row = $this->ci->orders_model->get(['id'=>$oid]);
if(!$row){
return false;
}
$customers = $this->ci->customers_model->get(['id'=>$row['rid']],'rid');
$ifpay = false;
$ifpay = $this->ci->order_purchase_model->count(['status>'=>1,'item_id'=>$oid,'app_id',$app_id,'app_uid'=>$userinfo['uid']]);
if($customers['rid'] && !$ifpay){ //判断线索是否支付定金
$ifpay = $this->ci->order_purchase_model->count(['type'=>3,'status'=>2,'cf_id'=>$customers['rid'],'app_id'=>$app_id,'app_uid'=>$userinfo['uid']]);
}
if($ifpay){
$this->ci->signs_model->update(['status'=>2],['o_id'=>$oid]);
if($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');
}
//判断下一步是否存在
if(!$this->ci->next_model->get(['o_id'=>$row['id']])){
$res = $this->ci->orders_model->update(['status'=>$status],['id'=>$row['id']]);
if($res){
$this->ci->next_model->add(['o_id'=>$row['id'],'c_time'=>time()]);
}
}else{
$res = $this->ci->orders_model->update(['status'=>2],['id'=>$row['id']]);
}
}else{
$this->ci->signs_model->update(['status'=>1],['o_id'=>$row['id']]);
$res = $this->c_order(self::SRV_MCH_ID,$row,$app_id,$userinfo);
}
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){
$this->ci->load->model('apporder/order_purchase_model');
$this->ci->load->model('auto/auto_series_model');
$this->ci->load->model('auto/auto_brand_model');
$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){
$this->ci->load->model('receiver/order/receiver_order_contracts_model');
$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){
$this->ci->load->model('receiver/order/receiver_orders_model','orders_model');
$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拨打电话)
* @param $cf_platform string 来源 (wxapp小程序 admin后台)
*/
public function add_log($o_id,$uid,$uname,$content,$type='',$cf_platform='wxapp'){
$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;
$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/order/receiver_orders_model','orders_model');
$this->ci->load->model('receiver/receiver_customers_model','customers_model');
$this->ci->load->model('receiver/receiver_clues_model','clues_model');
$this->ci->load->model('apporder/order_purchase_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;
}
//确认车辆完成创建两个支付订单
public function check_finish($oid,$app_id,$userinfo){
$this->ci->load->model('receiver/order/receiver_orders_model','orders_model');
$this->ci->load->model('apporder/order_purchase_model');
$this->ci->load->model('auto/auto_series_model');
$this->ci->load->model('auto/auto_brand_model');
$this->ci->load->model('auto/auto_cars_model');
$this->ci->load->model("biz/biz_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_company_model");
$this->ci->load->model('sys/sys_city_model');
$row = $this->ci->orders_model->get(['id'=>$oid]);
if(!$row){
return false;
}
$brand = $this->ci->auto_brand_model->get(['id'=>$row['brand_id']],'name');
$series = $this->ci->auto_series_model->get(['id'=>$row['s_id']],'name');
$car_json = json_decode($row['car_json'],true);
$color = isset($car_json['color']) ? $car_json['color'] : '';
$jsondata['car'] = $car_json;
if($color['jsondata']['img']){
$jsondata['cover'] = $color['jsondata']['img'];
}
//获取门店信息
$biz = $this->ci->biz_model->get(['id'=>$row['biz_id']],'company_id,city_id');
$company = $this->ci->sys_company_model->get(['id'=>$biz['company_id']]);
//获取服务包
$packs = $this->ci->package_model->get(['id'=>$row['pack_id']],'srv_ids');
$services = [];
if($packs['srv_ids']){
$services = $this->ci->services_model->select(["id in ({$packs['srv_ids']})"],'','','','title,field_name');
}
$attrs = "{$row['cor_id']}_{$row['v_id']}_{$row['incor_id']}";
$car = $this->ci->auto_cars_model->get(['attrs'=>$attrs,'s_id'=>$row['s_id']]);
//获取挂牌价
if($row['brand_id']==3){ //狸车品牌写死挂牌费用0.01 测试用
$car['fee_carno'] = 0.01;
}else{
$city = $this->ci->sys_city_model->get(['city_id'=>$biz['city_id']],'fee_carno');
$car['fee_carno'] = $city['fee_carno'];
}
$srv_price = 0;
if($services){
foreach($services as $key=>$val){
$field_arr = explode('.',$val['field_name']);
if($car[$field_arr[1]]){
$srv_price += $car[$field_arr[1]];
}
}
}
if($row['payway']){ //全款
$to_com_price = $car['price_car']; //给销售公司金额 裸车价格
$to_srv_price = $car['price_car'] + $srv_price - $to_com_price - $row['deposit'] - $car['price_insure']; //给服务公司金额 裸车价格+服务费-给销售公司金额-定金-保险
$to_srv_price = $to_srv_price>0 ? $to_srv_price : 0;
}else{ //分期
$to_com_price = $car['first_pay']; //给销售公司金额
$to_srv_price = $car['first_pay'] + $srv_price - $to_com_price - $row['deposit'] - $car['price_insure']; //给服务公司的金额 首付+服务费-给销售公司-定金-保险
$to_srv_price = $to_srv_price>0 ? $to_srv_price : 0;
}
$add_data = [];
if($to_srv_price>0){
$order_type = 5;
$sid = create_order_no(350200,'liche',1,$order_type);
$add_data[] = [
'app_id' => $app_id,
'app_uid' => $userinfo['uid'],
'sid' => $sid,
'mch_id' => self::SRV_MCH_ID,
'item_id' => $row['id'],
'item_title' => $brand['name'].$series['name'],
'item_num' => 1,
'type' => $order_type,
'item_price' => $to_srv_price,
'total_price' => $to_srv_price,
'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' => $row['id']
];
}
if($to_com_price>0){
$order_type = 6;
$sid = create_order_no(350200,'liche',1,$order_type);
$add_data[] = [
'app_id' => $app_id,
'app_uid' => $userinfo['uid'],
'sid' => $sid,
'mch_id' => $company['wx_mchid'],
'item_id' => $row['id'],
'item_title' => $brand['name'].$series['name'],
'item_num' => 1,
'type' => $order_type,
'item_price' => $to_com_price,
'total_price' => $to_com_price,
'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' => $row['id']
];
}
$result = false;
if($add_data){
$result = $this->ci->order_purchase_model->add_batch($add_data);
}
return $result;
}
}
?>