Files
liche/admin/controllers/receiver/Orders.php
T
xiaoyu 07cf724038 sid
2023-07-15 20:36:12 +08:00

977 lines
45 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Orders extends HD_Controller
{
public function __construct()
{
parent::__construct();
$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_loans_model', 'order_loans_model');
$this->load->model('receiver/order/receiver_order_ckcars_model', 'order_ckcars_model');
$this->load->model('receiver/order/receiver_order_bills_model', 'order_bills_model');
$this->load->model('receiver/order/receiver_order_agents_model', 'order_agents_model');
$this->load->model('receiver/order/receiver_order_deliverys_model', 'order_deliverys_model');
$this->load->model('receiver/order/receiver_order_contracts_model', 'order_contracts_model');
$this->load->model('receiver/receiver_customers_model', 'customers_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_finance_model');
//$this->load->model('sys/sys_finance_model');
$this->load->model('sys/sys_supplier_model');
$this->load->model('sys/sys_company_model');
$this->load->model('app/liche/app_liche_orders_model');
$this->load->library('receiver/orders_entity');
$this->load->library('OrdersList');
}
public function index()
{
$this->lists();
}
public function lists()
{
$params = $this->input->get();
$statusAry = $this->orders_model->statusAry();
$status_arr = array();
foreach ($statusAry as $key => $value) {
if (!$value['show']) {
continue;
}
$cate = $where = array();
$t2 = 'lc_receiver_orders';
if ($key == 7) {
$where_c = ["$t2.status<>" => -1];
} else {
$where_c = ["$t2.status not in(-1,7)" => null];
}
$where_c["{$t2}.brand_id<>3"] = null;//狸车品牌不显示
$where_c["{$t2}.biz_id<>1"] = null;//biz_id=1不显示
if ($key == 6 || $key == 7) {
$count1 = $this->orders_model->count(array("status" => $key, "biz_id<>" => 1, "brand_id<>" => 3));
} else {
if ($key == 0) {
$t1 = 'lc_receiver_order_signs';
$order_model = $this->order_signs_model;
} else if ($key == 1) {
$t1 = 'lc_receiver_order_loans';
$order_model = $this->order_loans_model;
} else if ($key == 2) {
$t1 = 'lc_receiver_order_ckcars';
$order_model = $this->order_ckcars_model;
} else if ($key == 3) {
$t1 = 'lc_receiver_order_bills';
$order_model = $this->order_bills_model;
$where_c["$t1.status>"] = 0;
} else if ($key == 4) {
$t1 = 'lc_receiver_order_agents';
$order_model = $this->order_agents_model;
} else if ($key == 5) {
$t1 = 'lc_receiver_order_deliverys';
$order_model = $this->order_deliverys_model;
} else {
$t1 = 'lc_receiver_order_signs';
$order_model = $this->order_signs_model;
}
$count1 = $order_model->count_order($where_c, $t2);
if ($value['list']) {
foreach ($value['list'] as $key2 => $value2) {
$where_c["$t1.status"] = $key2;
$count2 = $order_model->count_order($where_c, $t2);
$cate[] = array("id" => $key2, "name" => $value2, "count" => $count2);
}
}
}
$status_arr[] = array("id" => $key, "name" => $value['name'], "cate" => $cate, "count" => $count1);
}
$params['count_all'] = $this->orders_model->count(array("status>=" => 0, "biz_id<>" => 1, "brand_id<>" => 3));//全部
$params['list_type'] = 'all';
$result = $this->orderslist->lists($params['status_pid'], $params);
$this->data = $result;
$this->data['status_arr'] = $status_arr;
return $this->show_view($result['view'], true);
}
public function get()
{
$status_arr = $this->orders_model->statusAry();
$id = $this->input->get('id');
$row = $this->orders_model->get(['id' => $id]);
//$sign = $this->order_signs_model->get(['o_id' => $row['id']], 'status');
$customers = $this->customers_model->get(['id' => $row['rid']]);
if (!$row) {
return $this->show_json(SYS_CODE_FAIL, '订单不存在!');
}
if ($customers['of_id']) {//客户来源
$of_ary = $this->customers_model->offlineSources($customers['of_id']);
$of_title = $of_ary['name'];
$customers['of2_id'] && $of_title .= '-' . $of_ary['list'][$customers['of2_id']];
$row['of_title'] = $of_title;
}
$sub_status_cn = $this->orderslist->sub_status_cn($row['id'],$row['status']);
$sub_status_cn && $sub_status_cn = "-".$sub_status_cn;
$money_json = json_decode($row['money_json'], true);
$deposit = $row['deposit'];
$jsondata = $row['jsondata'] ? json_decode($row['jsondata'], true) : array();
$row['price_discount'] = $money_json['price_discount'];
$sale_price = $money_json['price_discount'] ? $row['price'] - $money_json['price_discount'] : $row['price'];
$row['sale_price'] = number_format($sale_price, 2);
$row['info_json'] = json_decode($row['info_json'], true);
$row['status'] = intval($row['status']);
$row['price'] = number_format($row['price'], 2);
$row['deposit'] = number_format($deposit, 2);
$row['c_time'] = date('Y-m-d', $row['c_time']);
$row['sign_status_name'] = $status_arr[$row['status']]['name'].$sub_status_cn;
$row['payway'] = intval($row['payway']);
$row['item_id'] = intval($row['item_id']);
$row['cardid'] = $row['info_json']['cardid'] ? $row['info_json']['cardid'] : $row['info_json']['c_cardid'];
$row['address'] = $row['info_json']['c_address'] ? $row['info_json']['c_address'] : '';
$row['entrust_name'] = $row['info_json']['entrust_name'] ? $row['info_json']['entrust_name'] : '';
$row['entrust_mobile'] = $row['info_json']['entrust_mobile'] ? $row['info_json']['entrust_mobile'] : '';
$row['entrust_idcard'] = $row['info_json']['entrust_idcard'] ? $row['info_json']['entrust_idcard'] : '';
$row['credit'] = $row['info_json']['c_credit'] ? $row['info_json']['c_credit'] : '';
$row['note'] = $jsondata['note'] ? $jsondata['note'] : '';
//获取车信息
$brand = $this->auto_brand_model->get(['id' => $row['brand_id']], 'name');
$serie = $this->auto_series_model->get(['id' => $row['s_id']], 'name');
$row['brand_name'] = $brand['name'];
$row['s_name'] = $serie['name'];
$attr_arr = [$row['v_id'], $row['cor_id'], $row['incor_id']];
$attr = $this->auto_attr_model->get_map_by_ids($attr_arr, 'id,title');
$row['v_name'] = isset($attr[$row['v_id']]) ? $attr[$row['v_id']][0]['title'] : '';
$row['cor_name'] = isset($attr[$row['cor_id']]) ? $attr[$row['cor_id']][0]['title'] : '';
$row['incor_name'] = isset($attr[$row['incor_id']]) ? $attr[$row['incor_id']][0]['title'] : '';
//获取选择车辆vin
if ($row['item_id']) {
$this->load->model('items/items_model', 'mdItems');
$row_goods = $this->mdItems->get(array('id' => $row['item_id']));
$row['vin'] = $row_goods['vin'];
}
$row['delry_time'] = $row['delry_time'] != '0000-00-00 00:00:00'
? str_replace(" 00:00:00", "", $row['delry_time']) : '';
//获取车辆服务费
$srv_info = $this->orders_entity->order_srv_money($row['id'], 1);
$srv_info['total'] = number_format($srv_info['total'], 2);
$srv_detail = "";
if ($srv_info['list']) {
foreach ($srv_info['list'] as $key => $val) {
$d = $srv_detail ? ',' : '';
$srv_detail .= $d . $val['title'] . ":" . number_format($val['money'], 2);
}
$money_json['price_fine_discount']>0 && $srv_detail.=',精品尊享包优惠:'.$money_json['price_fine_discount'];
}
$srv_info['detail'] = $srv_detail;
$row['srv_info'] = $srv_info;
//获取贷款信息
if (!$row['payway']) {
$auto_finance_row = $this->auto_finance_model->get(['id' => $row['finance_id']], 'fin_id,num');
$loan = $this->order_loans_model->get(['o_id' => $row['id']]);
$loan['status'] = intval($loan['status']);
$loan['notify_file'] = $loan['notify_file'] ? build_qiniu_image_url($loan['notify_file']) : '';
$loan['notify_file_name'] = $loan['notify_file'] ? end(explode('/', $loan['notify_file'])) : '';
$loan['lend_file'] = $loan['lend_file'] ? build_qiniu_image_url($loan['lend_file']) : '';
$loan['lend_file_name'] = $loan['lend_file'] ? end(explode('/', $loan['lend_file'])) : '';
//$loan['finance_rows'] = $this->sys_finance_model->select(['status' => 1], '', 0, 0, 'id,title');
//$loan['finance_rows_nums'] = $this->sys_finance_model->get_nums();
$loan['finance_rows'] = $this->sys_supplier_model->select(['status' => 1, 'type'=>3], '', 0, 0, 'id,title');
$loan['finance_rows_nums'] = $this->auto_finance_model->get_nums();
$loan['price_loan'] = $money_json['price_loan'] ? $money_json['price_loan'] : '';
$loan['first_pay'] = 0;
if ($money_json['price_loan']) {
$srv_money = $this->orders_entity->order_srv_money($row['id']);
if ($srv_money > $deposit) {
$loan['first_pay'] = $sale_price - $money_json['price_loan'];
} else {
$loan['first_pay'] = $sale_price - $money_json['price_loan'] - $deposit;
}
}
!$loan['num'] && $loan['num'] = 12;
$loan['loan_time'] = $loan['loan_time'] != '0000-00-00 00:00:00' ? date('Y-m-d', strtotime($loan['loan_time'])) : '';
$row['loan_info'] = $loan;
}
//获取开票
$bill = $this->order_bills_model->get(['o_id' => $row['id']], 'status,file,cardidA,cardidB,business_licence');
$bill['file'] = $bill['file'] ? build_qiniu_image_url($bill['file']) : '';
$bill['file_type'] = $bill['file'] && strstr($bill['file'], '.pdf') ? 'pdf' : 'img';
$bill['cardidA'] = $bill['cardidA'] ? array('value' => $bill['cardidA'], 'src' => build_qiniu_image_url($bill['cardidA'])) : array('value' => '', 'src' => '');
$bill['cardidB'] = $bill['cardidB'] ? array('value' => $bill['cardidB'], 'src' => build_qiniu_image_url($bill['cardidB'])) : array('value' => '', 'src' => '');
$bill['business_licence'] = $bill['business_licence'] ? array('value' => $bill['business_licence'], 'src' => build_qiniu_image_url($bill['business_licence'])) : array('value' => '', 'src' => '');
$bill['status'] = intval($bill['status']);
$row_goods['company_id'] && $item_company = $this->sys_company_model->get(['id'=>$row_goods['company_id']],'id,short');
if(!$item_company){
$item_html = '车辆归属<b>未选择</b>';
}else{
$item_html = "车辆公司归属<b>【{$item_company['short']}】</b>";
}
$pay_order = $this->app_liche_orders_model->get(['type'=>3,'status'=>1,'o_id'=>$row['id']],'mch_id');
$pay_order && $pay_compay = $this->sys_company_model->get(['wx_mchid' => $pay_order['mch_id']],'id,short');
$pay_html = '';
if($pay_compay){
$ck_html = $pay_compay['id'] == $item_company['id'] ? '<b class="am-text-success">一致</b>' : '<b class="am-text-warning">不一致</b>';
$pay_html = "与尾款收款公司<b>【{$pay_compay['short']}】</b> {$ck_html}";
}
$bill['warning'] = $item_html. $pay_html;
$row['bill'] = $bill;
//获取合同文件
$contracts = $this->order_contracts_model->select(['o_id' => $row['id'], 'status' => 1], '', '', '', 'type,file,status');
$contract_data = [];
if ($contracts) {
foreach ($contracts as $key => $val) {
$contract_data[$val['type']] = [
'type' => $val['type'],
'status_name' => $val['status'] ? '已签约' : '未签约'
];
}
}
$row['contract_data'] = $contract_data;
//获取销售员
if ($row['admin_id']) {
$this->load->model('app/licheb/app_licheb_users_model', 'licheb_users_model');
$this->load->model("biz/biz_model");
$row_admin = $this->licheb_users_model->get(array('id' => $row['admin_id']));
$biz_id = $row_admin['biz_id'];
$row_biz = array();
if ($biz_id) {
$row_biz = $this->biz_model->get(array('id' => $biz_id));
}
$admin_name = $row_admin ? "{$row_biz['biz_name']} {$row_admin['uname']}" : "";
$row['admin_name'] = $admin_name;
}
//获取代办信息
$agent = $this->order_agents_model->get(['o_id' => $row['id']]);
$imgs = $car_imgs = $register_img = [];
$company = $ins_risk = $business_risk = '';
$if_listed_db = $if_ins_db = 0;
if ($agent) {
$agent['if_ins'] = intval($agent['if_ins']);
$agent['ins_time'] = $agent['ins_time'] != '0000-00-00 00:00:00' ? date('Y-m-d', strtotime($agent['ins_time'])) : '';
$agent_jsondata = $agent['jsondata'] ? json_decode($agent['jsondata'], true) : '';
if ($agent_jsondata) {
$company = $agent_jsondata['company'];
$ins_risk = $agent_jsondata['ins_risk'];
$business_risk = $agent_jsondata['business_risk'];
$if_listed_db = intval($agent_jsondata['if_listed_db']);
$if_ins_db = intval($agent_jsondata['if_ins_db']);
if ($agent_jsondata['register_img']) {
$file_name = substr($agent_jsondata['register_img'], strrpos($agent['car_img'], '/') + 1);
$register_img[] = array(
'src' => build_qiniu_image_url($agent_jsondata['register_img']),
'value' => $agent_jsondata['register_img'],
'title' => $file_name
);
}
}
if ($agent['car_img']) {
$file_name = substr($agent['car_img'], strrpos($agent['car_img'], '/') + 1);
$car_imgs[] = array(
'src' => build_qiniu_image_url($agent['car_img']),
'value' => $agent['car_img'],
'title' => $file_name
);
}
if ($agent['ins_img']) {
$ins_img = json_decode($agent['ins_img']);
foreach ($ins_img as $val) {
$file_name = substr($val, strrpos($val, '/') + 1);
$imgs[] = [
'src' => build_qiniu_image_url($val),
'value' => $val,
'title' => $file_name
];
}
}
} else {
$agent['if_ins'] = 0;
$agent['car_img_src'] = '';
$agent['car_img'] = '';
}
$agent['company'] = $company;
$agent['ins_risk'] = $ins_risk;
$agent['business_risk'] = $business_risk;
$agent['if_listed_db'] = $if_listed_db;
$agent['if_ins_db'] = $if_ins_db;
$row['agent'] = $agent;
$row['bx_imgs'] = $imgs;
$row['car_imgs'] = $car_imgs;
$row['register_img'] = $register_img;
$info = $row;
$this->data['info'] = $info;
$this->data['_title'] = '订单详情';
return $this->show_view('receiver/orders/get', true);
}
//添加单条数据
public function add()
{
}
//编辑单条数据
public function edit()
{
$params = $this->input->post();
if (!$params['id']) {
return $this->show_json(SYS_CODE_FAIL, '参数错误!');
}
$result = '';
if ($params['type'] == 1) {//修改备注
$re = $this->orders_model->get(['id' => $params['id']]);
$jsondata = json_decode($re['jsondata'], true);
$jsondata['note'] = $params['note'];
$upd = array('jsondata' => json_encode($jsondata, JSON_UNESCAPED_UNICODE));
$result = $this->orders_model->update($upd, ['id' => $params['id']]);
} else if ($params['type'] == 2) {//修改营业执照
if (!$params['business_licence']) {
return $this->show_json(SYS_CODE_FAIL, '请上传营业执照');
}
if (!$this->order_bills_model->get(['o_id' => $params['id']])) {
$this->order_bills_model->add(['o_id' => $params['id'], 'c_time' => time()]);
}
$result = $this->order_bills_model->update(array('business_licence' => $params['business_licence']), ['o_id' => $params['id']]);
}
if ($result) {
return $this->show_json(SYS_CODE_SUCCESS, '保存成功');
} else {
return $this->show_json(SYS_CODE_FAIL, '保存失败');
}
}
/**
* 更新身份证信息
* @return bool
*/
function edit_cardID()
{
$o_id = $this->input->post('o_id');
$cardidA = $this->input->post('cardidA');
$cardidB = $this->input->post('cardidB');
if (!$this->order_bills_model->get(['o_id' => $o_id])) {
$this->order_bills_model->add(['o_id' => $o_id, 'c_time' => time()]);
}
if (!$cardidA || !$cardidB) {
return $this->show_json(SYS_CODE_FAIL, '请上传身份证照片');
}
$this->load->library('TcOrc');
$cardidA_src = build_qiniu_image_url($cardidA);
$result = $this->tcorc->IdentityCard($cardidA_src);
if (!$result['code']) {
return $this->show_json(SYS_CODE_FAIL, '身份证校验不通过');
} else {
//校验姓名
$uname = $result['data']['Name'];
$row_order = $this->orders_model->get(['id' => $o_id]);
if ($uname != $row_order['name']) {
return $this->show_json(SYS_CODE_FAIL, '身份证姓名与客户不一致');
}
}
$upd = array(
'cardidA' => $cardidA,
'cardidB' => $cardidB,
'status' => 1
);
$result = $this->order_bills_model->update($upd, ['o_id' => $o_id]);
if ($result) {
return $this->show_json(SYS_CODE_SUCCESS, '保存成功');
} else {
return $this->show_json(SYS_CODE_FAIL, '保存失败');
}
}
//删除单条数据
public function del()
{
}
//批量操作(默认修改状态)
public function batch()
{
}
//导出数据列表
public function export()
{
$params = $this->input->get();
$params['page'] = 1;
$params['size'] = 10000;
$indexs = [];
if ($params['export_type']) {
$result = $this->orderslist->export_data($params['status_pid'], $params);
$fileName = '保险明细';
$data = $result['data'];
$indexs = $result['indexs'];
} else {
$fieldAry = $this->orderslist->get_fields($params['status_pid'], 1);
$fieldAry['admin_name'] = ['title'=>'销售顾问'];
foreach ($fieldAry as $key => $value) {
$indexs[$key] = $value['title'];
}
$indexs['admin_name'] = '销售顾问';
$result = $this->orderslist->lists($params['status_pid'], $params);
$fileName = $result['_title'];
foreach ($result['lists'] as $key => $value) {
$temp = array();
foreach ($fieldAry as $key2 => $value2) {
$temp[$key2] = $value[$key2];
}
$data[] = $temp;
}
array_unshift($data, $indexs);
}
$this->load->library('excel');
return $this->excel->out_csv($data, $indexs, $fileName . "_" . date('YmdHis'));
}
//修改分期
public function edit_loan()
{
$params = $this->input->post();
$info = $params['info'];
$oid = $params['id'] ? $params['id'] : $info['id'];
$file = $_FILES['file'];
$row = $this->order_loans_model->get(['o_id' => $oid]);
if (!$row) {
$this->order_loans_model->add(['o_id' => $oid, 'c_time' => time()]);
$row = $this->order_loans_model->get(['o_id' => $oid]);
}
$update = [];
if ($file) { //上传按揭通知函
$path = FCPATH . 'temp/';
if (!file_exists($path)) {
$oldumask = umask(0);
mkdir($path, 0777, true);
umask($oldumask);
}
$file_name = md5($file['name'] . uniqid()) . '.' . end(explode('.', $file['name']));
$tmp = $path . $file_name;
move_uploaded_file($file['tmp_name'], $tmp);
if (!filesize($tmp)) {
return $this->show_json(SYS_CODE_FAIL, '上传文件失败!');
}
$this->load->library('qiniu');
$res = $this->qiniu->save($file_name, file_get_contents($tmp));
unlink($tmp);
if (!$res) {
return $this->show_json(SYS_CODE_FAIL, '上传文件失败!');
}
$size = getimagesize($res['url']);
if ($params['type']) {
$update['lend_file'] = $res['file'];
} else {
$update['notify_file'] = $res['file'];
}
$this->data['file_url'] = build_qiniu_image_url($res['file']);
}
strlen($params['status']) && $update['status'] = $params['status'];
if ($params['status'] == 1) { //审核通过
$finance_id = $info['finance_id'];
$this->orders_model->update(['finance_id' => $finance_id], ['id' => $row['o_id']]);
} elseif ($params['status'] == 2) { //审核通过
if (!$row['notify_file']) {
return $this->show_json(SYS_CODE_FAIL, '请先上传按揭通知函!');
}
$finance_id = $info['finance_id'];
$price_loan = $info['loan_info']['price_loan'];
$update['num'] = $info['loan_info']['num'];
if (!$finance_id || !$price_loan || !$update['num']) {
return $this->show_json(SYS_CODE_FAIL, '请填写完整信息!');
}
$order_row = $this->orders_model->get(['id' => $row['o_id']], 'id,mobile,money_json');
$money_json = json_decode($order_row['money_json'], true);
$money_json['price_loan'] = $price_loan;
$this->orders_model->update(['finance_id' => $finance_id, 'money_json' => json_encode($money_json, JSON_UNESCAPED_UNICODE)], ['id' => $row['o_id']]);
//生成购车订单
$this->load->model('app/liche/app_liche_users_model');
$userinfo = $this->app_liche_users_model->get(['mobile'=>$order_row['mobile']]);
$this->orders_entity->check_finish_v2($order_row['id'],$userinfo);
} elseif ($params['status'] == 3) { //完成按揭
if (!$row['lend_file']) {
return $this->show_json(SYS_CODE_FAIL, '请先上传按揭放款函!');
}
$loan_time = $info['loan_info']['loan_time'];
if (!$loan_time) {
return $this->show_json(SYS_CODE_FAIL, '请选择放款时间!');
}
$update['loan_time'] = date('Y-m-d H:i:s', strtotime($loan_time));
}
$result = $this->order_loans_model->update($update, ['id' => $row['id']]);
if ($result) {
if ($params['status'] == 2) { //设置等待放款
$this->orders_model->update(['status' => 2], ['id' => $row['o_id']]);
$ckcar = $this->order_ckcars_model->get(['o_id' => $row['o_id']]);
if(!$ckcar){
$replace = [
'o_id' => $row['o_id'],
'status' => 0,
'c_time' => time()
];
$this->order_ckcars_model->add($replace);
}
} elseif ($params['status'] == 3) { //设置完成按揭
$order_row = $this->orders_model->get(['id' => $row['o_id']], 'status');
if ($order_row['status'] > 2) {
$agent_rows = $this->order_agents_model->get(['o_id' => $oid]); //判断是否代办完成
if ($agent_rows && $agent_rows['status']) {
$this->orders_model->update(['status' => 5], ['id' => $oid]);
if (!$this->order_deliverys_model->count(['o_id' => $oid])) {
$this->order_deliverys_model->add(['o_id' => $oid, 'c_time' => time]);
}
}
} else {
$this->orders_model->update(['status' => 2], ['id' => $row['o_id']]);
$ckcar = $this->order_ckcars_model->get(['o_id' => $row['o_id']]);
$replace = [
'o_id' => $row['o_id'],
'status' => 0,
'c_time' => time()
];
$ckcar && $replace['id'] = $ckcar['id'];
$this->order_ckcars_model->replace($replace);
}
}
}
$this->data['params'] = $params;
return $result ? $this->show_json(SYS_CODE_SUCCESS, '保存成功') : $this->show_json(SYS_CODE_FAIL, '保存失败');
}
public function edit_item()
{
$this->load->model("items/items_model");
$id = $this->input->post('id');
$item_id = $this->input->post('item_id');
$row = $this->orders_model->get(['id' => $id]);
$ckcars = $this->order_ckcars_model->get(['o_id' => $id]);
if (!$row) {
return $this->show_json(SYS_CODE_FAIL, '参数错误!');
}
if (!$item_id) return $this->show_json(SYS_CODE_FAIL, '请选择车辆!');
if ($row['status'] > 3) {
return $this->show_json(SYS_CODE_FAIL, '已开票,不能修改!');
}
$res = $this->orders_model->update(['item_id' => $item_id], ['id' => $id]);
if (!$res) {
return $this->show_json(SYS_CODE_FAIL, '保存失败');
}
if ($row['item_id'] && $row['item_id'] != $item_id) {
$this->items_model->update(['status' => 1], ['id' => $row['item_id']]);
}
$this->items_model->update(['status' => 2], ['id' => $item_id]);
if (!$ckcars) {
$add_data = [
'o_id' => $id,
'status' => 1,
'c_time' => time()
];
$result = $this->order_ckcars_model->add($add_data);
} else {
$result = $this->order_ckcars_model->update(['status' => 1], ['id' => $ckcars['id']]);
}
if ($result) {
return $this->show_json(SYS_CODE_SUCCESS, '保存成功');
} else {
return $this->show_json(SYS_CODE_FAIL, '保存失败');
}
}
//查看发票信息
public function get_bill()
{
$oid = $this->input->get('oid');
$row = $this->orders_model->get(['id' => $oid]);
$bill = $this->order_bills_model->get(['o_id' => $oid]);
if (!$row) {
return $this->show_json(SYS_CODE_FAIL, '参数错误!');
}
if (!$bill && $row['status'] >= 3) {
$add_data = [
'o_id' => $row['id'],
'c_time' => time()
];
$bill['id'] = $this->order_bills_model->add($add_data);
}
$bill['src_file'] = $bill['file'] ? build_qiniu_image_url($bill['file']) : '';
$bill['bill_time'] = $row['bill_time'] != '0000-00-00 00:00:00' ? $row['bill_time'] : '';
$this->data['bill'] = $bill;
return $this->show_view('receiver/orders/get_bill');
}
//编辑发票信息
public function edit_bill()
{
$id = $this->input->post('id');
$money = $this->input->post('money');
$file = $this->input->post('file');
$money && $update['money'] = $money;
$file && $update['file'] = $file;
$result = $this->order_bills_model->update($update, ['id' => $id]);
if ($result) {
//更新开票时间
$row = $this->order_bills_model->get(['id' => $id]);
$row_order = $this->orders_model->get(array('id' => $row['o_id']));
if ($row_order) {
$this->load->model('items/items_model');
$bill_time = date('Y-m-d H:i:s');
$upd = array('bill_time' => $bill_time);
$where = array('id' => $row_order['item_id']);
$ret = $this->items_model->update($upd, $where);
if ($ret) { //添加开票日志
$this->load->library('entity/items_entity');
$re_i = $this->items_model->get(['id' => $row_order['item_id']]);
$params = array('item_id' => $row_order['item_id'], 'type' => 3, 'uid' => $this->uid, 'uname' => $this->username
, 'com_time' => $bill_time, 'biz_id' => $re_i['biz_id'], 'addr_id' => $re_i['addr_id']);
$this->items_entity->add_log($params);
}
$this->orders_model->update(['bill_time' => $bill_time], ['id' => $row['o_id']]);
}
return $this->show_json(SYS_CODE_SUCCESS, '保存成功');
} else {
return $this->show_json(SYS_CODE_FAIL, '保存失败');
}
}
//确定交款
public function edit_jk()
{
$oid = $this->input->post('oid');
$row = $this->orders_model->get(['id' => $oid]);
if (!$row) {
return $this->show_json(SYS_CODE_FAIL, '参数错误!');
}
if ($row['status'] != 3) {
return $this->show_json(SYS_CODE_FAIL, '修改失败,该订单不处于开票阶段');
}
$bill = $this->order_bills_model->get(['o_id' => $oid]);
if (!$bill['file'] || !$bill['money']) {
return $this->show_json(SYS_CODE_FAIL, '未上传开票文件或未填写到账金额!');
}
if (!$bill['cardidA'] && !$bill['business_licence']) {
return $this->show_json(SYS_CODE_FAIL, '未上传用户身份证或营业执照!');
}
$where = [
'o_id' => $oid,
'status' => 1,
'pay_price>' => 0
];
$sum = $this->app_liche_orders_model->sum('total_price', $where); //已收金额
$need_price = $this->orders_entity->recevable_price($oid); //应收金额
if ($sum['total_price'] < $need_price) {
return $this->show_json(SYS_CODE_FAIL, '修改失败,已收金额小于应收金额!');
}
$this->order_bills_model->update(['status' => 2], ['o_id' => $oid]);
$this->orders_model->update(['status' => 4], ['id' => $bill['o_id']]);
if (!$this->order_agents_model->get(['o_id' => $oid])) {
$this->order_agents_model->add(['o_id' => $oid, 'c_time' => time]);
}
return $this->show_json(SYS_CODE_SUCCESS, '保存成功');
}
//保存代办服务
public function edit_agent()
{
$params = $this->input->post();
$row = $this->orders_model->get(['id' => $params['oid']]);
if (!$row) {
return $this->show_json(SYS_CODE_FAIL, '参数错误!');
}
$agent = $this->order_agents_model->get(['o_id' => $row['id']]);
$jsondata = json_decode($agent['jsondata'],true);
$update = [];
$update['car_num'] = $params['car_num'];
$update['ins_time'] = $params['ins_time'] ? date('Y-m-d H:i:s', strtotime($params['ins_time'])) : '0000-00-00 00:00:00';
$update['car_img'] = $params['car_img'];
$update['if_ins'] = $params['ins_risk'] ? 1 : 0;
$jsondata['if_listed_db'] = intval($params['if_listed_db']);
$jsondata['if_ins_db'] = intval($params['if_ins_db']);
$jsondata['company'] = $params['company'];
$jsondata['ins_risk'] = $params['ins_risk'];
$jsondata['business_risk'] = $params['business_risk'];
$jsondata['register_img'] = $params['register_img'];
if ($params['bx_imgs']) {
$this->load->library('qiniuorc');
$imgs = explode(',', $params['bx_imgs']);
$update['ins_img'] = json_encode($imgs, JSON_UNESCAPED_UNICODE);
$old_ins_img = json_encode(json_decode($agent['ins_img'],true),JSON_UNESCAPED_UNICODE);
if($update['ins_img']!=$old_ins_img && is_array($imgs)){
//图片识别
foreach ($imgs as $key => $val) {
$url = build_qiniu_image_url($val);
$res = $this->qiniuorc->car_insure($url);
if($res['code']){
$company = $res['data']['保险公司'];
if(count($res['data']['投保险种'])>3){ //商业
$business_risk = (float)$res['data']['合计保费'];
}else{ //交强险
$ins_risk = (float)$res['data']['合计保费'];
}
$json_orc_ins[] = $res['data'];
}
}
}
if(!$params['company'] && $company){
$jsondata['company'] = $company;
}
if(!$params['ins_risk'] && $ins_risk){
$jsondata['ins_risk'] = $ins_risk;
}
if(!$params['business_risk'] && $business_risk){
$jsondata['business_risk'] = $business_risk;
}
$jsondata['ins_orc'] = $json_orc_ins;
}else{
$update['ins_img'] = null;
}
$update['jsondata'] = json_encode($jsondata, JSON_UNESCAPED_UNICODE);
if ($agent) {
if($agent['status']<0 && $row['status']>=4){
$update['status'] = 0;
}
$result = $this->order_agents_model->update($update, ['id' => $agent['id']]);
} else {
$update['o_id'] = $row['id'];
$update['c_time'] = time();
$result = $this->order_agents_model->add($update);
}
if ($result) {
return $this->show_json(SYS_CODE_SUCCESS, '保存成功');
} else {
return $this->show_json(SYS_CODE_FAIL, '保存失败');
}
}
//确定代办
public function edit_agentfh()
{
$oid = $this->input->post('oid');
$row = $this->orders_model->get(['id' => $oid]);
if (!$row) {
return $this->show_json(SYS_CODE_FAIL, '参数错误!');
}
if ($row['status'] < 4) {
return $this->show_json(SYS_CODE_FAIL, '修改失败,该订单不处于代办阶段');
}
$loan = $this->order_loans_model->get(['o_id' => $oid]);
if (!$row['payway'] && $loan['status'] != 3) { //分期且分期未完成
return $this->show_json(SYS_CODE_FAIL, '修改失败,请先完成按揭');
}
$this->order_agents_model->update(['status' => 1], ['o_id' => $oid]);
$row['status'] == 4 && $this->orders_model->update(['status' => 5], ['id' => $oid]);
if (!$this->order_deliverys_model->count(['o_id' => $oid])) {
$this->order_deliverys_model->add(['o_id' => $oid, 'c_time' => time]);
}
return $this->show_json(SYS_CODE_SUCCESS, '保存成功');
}
//获取合同图片
public function get_cimgs()
{
$type = $this->input->get('type');
$oid = $this->input->get('oid');
$row = $this->order_contracts_model->get(['type' => $type, 'o_id' => $oid]);
$info['title'] = '相册标题';
$info['id'] = '222';
$info['start'] = 0;
$data = [];
if ($row) {
$imgs = json_decode($row['imgs'], true);
foreach ($imgs as $key => $val) {
$data[] = [
'alt' => '图片名',
'pid' => $key,
'src' => build_qiniu_image_url($val),
'thumb' => build_qiniu_image_url($val)
];
}
}
$info['data'] = $data;
$this->data['info'] = $info;
return $this->show_json(SYS_CODE_SUCCESS, '保存成功');
}
//支付列表
public function get_paylog()
{
$params = $this->input->get();
if ($params['d_type'] == 'ajax') {
$page = $params['page'] = $params['page'] ? intval($params['page']) : 1;
$size = $params['size'] = $params['size'] ? intval($params['size']) : 10;
$list = [];
$p_row = $this->app_liche_orders_model->get(['o_id'=>$params['id'],'pid'=>0],'id');
$where = [
'o_id' => $params['id'],
'status' => 1,
'pay_price>' => 0
];
$count = $this->app_liche_orders_model->count($where);
if ($count) {
$rows = $this->app_liche_orders_model->select($where, 'id desc', $page, $size, 'id,total_price,type,pay_time,mch_id,descrip,status,pid');
$type_arr = $this->app_liche_orders_model->get_type_arr();
foreach ($rows as $item) {
$company = '';
if ($item['mch_id']) {
$company = $this->sys_company_model->get(['wx_mchid' => $item['mch_id']], 'short');
}
$list[] = [
'id'=> $item['id'],
'money' => $item['total_price'],
'type_cn' => $type_arr[$item['type']],
'pay_time' => $item['pay_time'],
'company' => $company ? $company['short'] : '',
'descrip' => $item['descrip'],
];
}
}
$sum = $this->app_liche_orders_model->sum('total_price', ['o_id'=>$params['id'],'status'=>1,'pay_price>'=>0]);
$this->data['pay_price'] = $sum['total_price'];
$this->data['need_price'] = $this->orders_entity->recevable_price($params['id']);
$this->data['list'] = $list;
$hasNext = ceil($count / $size) > $page ? 1 : 0;
$this->data['commonPage'] = array('page' => $page, 'pageLimit' => $size, 'pageCount' => $count, 'hasNext' => $hasNext);
return $this->show_json(SYS_CODE_SUCCESS, '操作成功');
}
$this->data['id'] = $this->input->get('id');
return $this->show_view('receiver/orders/paylog');
}
//添加支付日志
public function add_paylog()
{
$id = $this->input->get_post('id');
$row = $this->orders_model->get(['id' => $id]);
$srv_price = $this->orders_entity->order_srv_money($row['id']);
if ($this->input->method() == 'post') {
$money = $this->input->post('money');
$pay_time = $this->input->post('pay_time');
$wx_mchid = $this->input->post('wx_mchid');
$descrip = $this->input->post('descrip');
$type = $this->input->post('type');
if (!$money || !$pay_time || !$row || !$wx_mchid) {
return $this->show_json(SYS_CODE_FAIL, '参数错误');
}
$p_row = $this->app_liche_orders_model->get(['pid'=>0,'o_id'=>$row['id']]);
$this->load->helper('order');
$sid = create_order_no(350200, 'liche');
$data = [
'o_id' => $row['id'],
'sid' => $sid,
'mch_id' => $wx_mchid,
'brand_id' => $row['brand_id'],
's_id' => $row['s_id'],
'v_id' => $row['v_id'],
'cor_id' => $row['cor_id'],
'incor_id' => $row['incor_id'],
'total_price' => $money,
'pay_price' => $money,
'type' => $type,
'status' => 0,
'c_time' => time()
];
$p_row['uid'] && $data['uid'] = $p_row['uid'];
$p_row['entrust_uid'] && $data['entrust_uid'] = $p_row['entrust_uid'];
$descrip && $data['descrip'] = $descrip;
if($type==1){ //定金
$data['pid'] = $p_row['id'];
$this->app_liche_orders_model->update(['status'=>-1],['type'=>1,'status'=>0,'o_id'=>$row['id']]);
}elseif($type==2){ //服务费
if(!$srv_price){
return $this->show_json(SYS_CODE_FAIL, '该订单不需要支付服务费');
}
$this->app_liche_orders_model->update(['status'=>-1],['type'=>2,'status'=>0,'o_id'=>$row['id']]);
$data['pid'] = $p_row['id'];
}elseif($type==3){ //尾款
$last_p_row = $this->app_liche_orders_model->get(['type'=>3,'pid'=>$p_row['id']]);
if(!$last_p_row){
return $this->show_json(SYS_CODE_FAIL, '该订单未到支付尾款阶段');
}
$data['pid'] = $last_p_row['id'];
$this->app_liche_orders_model->update(['status'=>-1],['type'=>3,'status'=>0,'o_id'=>$row['id'],'pid'=>$last_p_row['id']]);
//获取已支付尾款
$pay_sum = $this->app_liche_orders_model->sum('pay_price',['type'=>3,'status'=>1,'o_id'=>$row['id'],'pid'=>$last_p_row['id']]);
if($pay_sum['pay_price']+$money>$last_p_row['total_price']){
return $this->show_json(SYS_CODE_FAIL, '请输入正确尾款金额');
}
}elseif($type==5){
$data['status'] = 1;
$data['pay_time'] = $pay_time;
}else{
return $this->show_json(SYS_CODE_FAIL, '请选择正确订单类型');
}
$res = $this->app_liche_orders_model->add($data);
if (!$res) {
return $this->show_json(SYS_CODE_FAIL, '保存失败');
}
if($type!=5){
$this->load->service('apporder/payment_service', array('app_id' => 1));
$result = $this->payment_service->after_pay_liche($sid,$money,'',$pay_time);
}
//更新客户为订单客户
$this->customers_model->update(['status'=>2],['id'=>$row['rid']]);
return $this->show_json(SYS_CODE_SUCCESS, '保存成功');
}
$ck_row = $this->order_ckcars_model->get(['o_id'=>$id]);
$sign_row = $this->order_signs_model->get(['o_id'=>$id]);
$companys = $this->sys_company_model->select(['status' => 1, 'wx_mchid!=' => ''], 'id desc', '', '', 'id,wx_mchid,short');
$type = [];
if(!$row['status'] && $sign_row['status']==1){
$type[] = ['id'=>1,'name'=>'定金'];
}
if($row['status']==2 && $ck_row['status']==2){
$srv_price >0 && $type[] = ['id'=>2,'name'=>'服务费'];
$type[] = ['id'=>3,'name'=>'尾款'];
}
$type[] = ['id'=>5,'name'=>'后台添加'];
$this->data['type'] = $type;
$this->data['companys'] = $companys;
$this->data['id'] = $this->input->get('id');
return $this->show_view('receiver/orders/add_paylog');
}
//取消配车
public function edit_ckcar(){
$this->load->model('items/items_model');
$id = $this->input->post('id');
$row = $this->orders_model->get(['id'=>$id]);
if(!$row || !$row['item_id']){
return $this->show_json(SYS_CODE_FAIL, '参数错误');
}
$bill_row = $this->order_bills_model->get(['o_id'=>$id]);
$car_row = $this->order_ckcars_model->get(['o_id'=>$id]);
if($row['status']>3 || $bill_row['status']==2){
return $this->show_json(SYS_CODE_FAIL, '订单已开票,不能修改');
}
$res = $this->orders_model->update(['item_id'=>0,'status'=>2],['id'=>$row['id']]);
if($res){
$this->items_model->update(['status' => 1,'bill_time'=>'0000-00-00 00:00:00'], ['id' => $row['item_id']]);
//用户确认签名,删除重签签名
$con_row = $this->order_contracts_model->get(['o_id'=>$id,'type'=>2]);
if($con_row){
$this->order_contracts_model->delete(['o_id'=>$id,'type'=>2]);
$this->order_ckcars_model->update(['status'=>0],['id'=>$car_row['id']]);
}
return $this->show_json(SYS_CODE_SUCCESS, '保存成功');
}else{
return $this->show_json(SYS_CODE_FAIL, '保存失败');
}
}
}