488 lines
21 KiB
PHP
488 lines
21 KiB
PHP
<?php
|
||
defined('WXAPP_APP') or exit('No direct script access allowed');
|
||
|
||
require_once APPPATH . 'controllers/wxapp/Wxapp.php';
|
||
|
||
class Cusorder 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', 'orders_model');
|
||
// $this->load->model('receiver/order/receiver_order_datas_model');
|
||
|
||
$this->load->model('auto/auto_brand_model');
|
||
$this->load->model('auto/auto_series_model');
|
||
$this->load->model('auto/auto_cars_model');
|
||
|
||
$this->load->model("biz/biz_model");
|
||
|
||
$this->load->library('receiver/customers_entity');
|
||
$this->load->library('receiver/order_datas_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 = $this->input_param('color');
|
||
$in_color = $this->input_param('in_color');
|
||
$payway = $this->input_param('payway');
|
||
$main_type = $this->input_param('main_type');
|
||
$name = trim($this->input_param('name'));
|
||
$cardid = trim($this->input_param('cardid'));
|
||
$delry_time = $this->input_param('delry_time');
|
||
$if_num = $this->input_param('if_num');
|
||
$if_insure = $this->input_param('if_insure');
|
||
$register_amount = $this->input_param('register_amount'); //上牌费
|
||
//贷款相关参数
|
||
$downpayment_type = $this->input_param('downpayment_type');
|
||
$loan_amount = $this->input_param('loan_amount');
|
||
$loan_periods = $this->input_param('loan_periods');
|
||
$monthly_payment = $this->input_param('monthly_payment');
|
||
$confirm_amount = $this->input_param('confirm_amount');
|
||
$discount_amount = $this->input_param('discount_amount');
|
||
|
||
$row = $this->customers_model->get(['id' => $cus_id]);
|
||
$car_row = $this->auto_cars_model->get(['id' => $car_id]);
|
||
if (!$row || !$car_row || !$delry_time || !$name || !$cardid) {
|
||
throw new Exception('参数错误', ERR_PARAMS_ERROR);
|
||
}
|
||
$data = [
|
||
'sid' => create_order_no(350200, $this->app_key),
|
||
'customer_id' => $row['id'],
|
||
'name' => $name,
|
||
'mobile' => $row['mobile'],
|
||
'card_id' => $cardid,
|
||
'biz_id' => $this->biz_id,
|
||
'brand_id' => $car_row['brand_id'],
|
||
'series_id' => $car_row['series_id'],
|
||
'car_id' => $car_id,
|
||
'admin_id' => $this->session['uid'],
|
||
'sale_id' => $this->session['uid'],
|
||
'order_time' => date('Y-m-d H:i:s'),
|
||
'c_time' => time()
|
||
];
|
||
$row['rid'] && $data['clue_id'] = $row['rid'];
|
||
$payway && $data['payway'] = 1;
|
||
$main_type && $data['main_type'] = 1;
|
||
$delry_time && $data['delry_time'] = $delry_time;
|
||
$color && $data['color'] = $color;
|
||
$in_color && $data['in_color'] = $in_color;
|
||
$if_num && $data['if_num'] = 1;
|
||
$if_insure && $data['if_insure'] = 1;
|
||
$money_json = [];
|
||
$money_json['confirm_amount'] = $confirm_amount ? $confirm_amount : 0;
|
||
$money_json['discount_amount'] = $discount_amount ? $discount_amount : 0;
|
||
if (!$payway) { //贷款
|
||
$data['downpayment_type'] = $downpayment_type;
|
||
$money_json['loan_amount'] = $loan_amount ? $loan_amount : 0;
|
||
$money_json['loan_periods'] = $loan_periods ? $loan_periods : 0;
|
||
$money_json['monthly_payment'] = $monthly_payment ? $monthly_payment : 0;
|
||
}
|
||
if ($if_num) {
|
||
$money_json['register_amount'] = $register_amount ? $register_amount : 0;
|
||
}
|
||
$data['money_json'] = json_encode($money_json, JSON_UNESCAPED_UNICODE);
|
||
$o_id = $this->orders_model->add($data);
|
||
if (is_numeric($o_id)) {
|
||
//更新客户
|
||
$this->customers_model->update(['status' => 2, '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);
|
||
return ['id' => $o_id];
|
||
} else {
|
||
throw new Exception('创建失败', ERR_PARAMS_ERROR);
|
||
}
|
||
}
|
||
|
||
protected function put()
|
||
{
|
||
$id = $this->input_param('id');
|
||
$car_id = $this->input_param('car_id');
|
||
$color = $this->input_param('color');
|
||
$in_color = $this->input_param('in_color');
|
||
$payway = $this->input_param('payway');
|
||
$delry_time = $this->input_param('delry_time');
|
||
$if_num = $this->input_param('if_num');
|
||
$if_insure = $this->input_param('if_insure');
|
||
$register_amount = $this->input_param('register_amount'); //上牌费
|
||
//贷款相关参数
|
||
$downpayment_type = $this->input_param('downpayment_type');
|
||
$loan_amount = $this->input_param('loan_amount');
|
||
$loan_periods = $this->input_param('loan_periods');
|
||
$monthly_payment = $this->input_param('monthly_payment');
|
||
$confirm_amount = $this->input_param('confirm_amount');
|
||
$discount_amount = $this->input_param('discount_amount');
|
||
|
||
$row = $this->orders_model->get(['id' => $id]);
|
||
$car_row = $this->auto_cars_model->get(['id' => $car_id]);
|
||
if (!$row || !$car_row || !$delry_time) {
|
||
throw new Exception('参数错误', ERR_PARAMS_ERROR);
|
||
}
|
||
$data = [
|
||
'brand_id' => $car_row['brand_id'],
|
||
'series_id' => $car_row['series_id'],
|
||
'car_id' => $car_id,
|
||
'payway' => $payway ? 1 : 0
|
||
];
|
||
$delry_time && $data['delry_time'] = $delry_time;
|
||
$color && $data['color'] = $color;
|
||
$in_color && $data['in_color'] = $in_color;
|
||
$data['if_num'] = $if_num ? 1 : 0;
|
||
$data['if_insure'] = $if_insure ? 1 : 0;
|
||
$money_json = json_decode($row['money_json'], true);
|
||
$money_json['confirm_amount'] = $confirm_amount ? $confirm_amount : 0;
|
||
$money_json['discount_amount'] = $discount_amount ? $discount_amount : 0;
|
||
if (!$payway) { //贷款
|
||
$data['downpayment_type'] = $downpayment_type;
|
||
$money_json['loan_amount'] = $loan_amount ? $loan_amount : 0;
|
||
$money_json['loan_periods'] = $loan_periods ? $loan_periods : 0;
|
||
$money_json['monthly_payment'] = $monthly_payment ? $monthly_payment : 0;
|
||
}
|
||
if ($if_num) {
|
||
$money_json['register_amount'] = $register_amount ? $register_amount : 0;
|
||
}
|
||
$data['money_json'] = json_encode($money_json, JSON_UNESCAPED_UNICODE);
|
||
$result = $this->orders_model->update($data, ['id' => $row['id']]);
|
||
if ($result) {
|
||
throw new Exception('修改成功', API_CODE_SUCCESS);
|
||
} else {
|
||
throw new Exception('修改失败', ERR_PARAMS_ERROR);
|
||
}
|
||
}
|
||
|
||
//修改用户基本信息
|
||
protected function put_info()
|
||
{
|
||
$id = $this->input_param('id');
|
||
|
||
$row = $this->orders_model->get(['id' => $id]);
|
||
if (!$row) {
|
||
throw new Exception('参数错误', ERR_PARAMS_ERROR);
|
||
}
|
||
|
||
$name = trim($this->input_param('name'));
|
||
$cardid = trim($this->input_param('cardid'));
|
||
$data['name'] = $name;
|
||
$data['card_id'] = $cardid;
|
||
|
||
$result = $this->orders_model->update($data, ['id' => $row['id']]);
|
||
if ($result) {
|
||
throw new Exception('修改成功', API_CODE_SUCCESS);
|
||
} else {
|
||
throw new Exception('修改失败', ERR_PARAMS_ERROR);
|
||
}
|
||
}
|
||
|
||
//获取客户订单
|
||
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,brand_id,series_id,car_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, 'series_id'));
|
||
$series = $this->auto_series_model->get_map_by_ids($series_arr, 'id,name');
|
||
foreach ($rows as $key => $val) {
|
||
$car_name = $name = $mobile = '';
|
||
$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()
|
||
{
|
||
$group_id = $this->session['group_id'];
|
||
$uid = $this->session['uid'];
|
||
$where['status'] = 0;
|
||
if ($group_id == 1) {
|
||
$where['admin_id'] = $uid;//销售
|
||
} else if ($group_id == 2 || $group_id == 3) {//店长/老板
|
||
$where["(biz_id = {$this->biz_id} OR admin_id = {$uid})"] = null;
|
||
} else if ($group_id == 4) {
|
||
$where['biz_id'] = $this->biz_id;
|
||
$this->biz_id != 1 && $where['brand_id!='] = 3; //渠道经理过滤
|
||
}
|
||
$d_count = $this->orders_model->count($where);
|
||
$where['status'] = 1;
|
||
$f_count = $this->orders_model->count($where);
|
||
$lists = [
|
||
['key' => 11, 'name' => "进行中({$d_count})"],
|
||
['key' => 12, 'name' => "已完成({$f_count})"],
|
||
];
|
||
$data['filters'] = [
|
||
'type' => [
|
||
['key' => 'fq', 'name' => '按揭办理'],
|
||
['key' => 'pc', 'name' => '车辆匹配'],
|
||
['key' => 'kp', 'name' => '发票开具'],
|
||
['key' => 'jf', 'name' => '交付确认'],
|
||
],
|
||
];
|
||
$data['tabs'] = $lists;
|
||
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');
|
||
$brand_id = $this->input_param('brand_id');
|
||
$s_id = $this->input_param('car_id');
|
||
$v_id = $this->input_param('v_id');
|
||
$page = $this->input_param('page');
|
||
$size = $this->input_param('size');
|
||
$type = $this->input_param('type');
|
||
$order_s_time = $this->input_param('order_s_time');
|
||
$order_e_time = $this->input_param('order_e_time');
|
||
$admin_ids = $this->input_param('admin_ids');//多选销售人员
|
||
!$page && $page = 1;
|
||
!$size && $size = 10;
|
||
$tab_id = intval($this->input_param('visit_tab_id'));
|
||
if ($tab_id) {//数据看版数据
|
||
if ($tab_id == 2) {//今日
|
||
$s_c_time = date('Y-m-01', strtotime(date("Y-m-d"))) . ' 00:00:00';
|
||
$e_c_time = date('Y-m-d', strtotime("$s_c_time +1 month -1 day")) . ' 23:59:59';
|
||
} else {//本月
|
||
$s_c_time = date('Y-m-d') . ' 00:00:00';
|
||
$e_c_time = date('Y-m-d') . ' 23:59:59';
|
||
}
|
||
if ($status == 6) {
|
||
$status = '';
|
||
$where = ['order_time>=' => $s_c_time, 'order_time<=' => $e_c_time];
|
||
} else if ($status == 8) {
|
||
$status = 2;
|
||
$where = ['refund_time>=' => $s_c_time, 'refund_time<=' => $e_c_time];
|
||
}
|
||
} else {
|
||
$where = ['status!=' => 2];
|
||
}
|
||
$admin_ids && $where["admin_id in ({$admin_ids})"] = null;
|
||
$brand_id && $where['brand_id'] = $brand_id;
|
||
$s_id && $where['s_id'] = $s_id;
|
||
$v_id && $where['v_id'] = $v_id;
|
||
if ($group_id == 1) {
|
||
$where['admin_id'] = $uid;//销售
|
||
} else if ($group_id == 2 || $group_id == 3) {//店长/老板
|
||
$where["(biz_id = {$this->biz_id} OR admin_id = {$uid})"] = null;
|
||
} else if ($group_id == 4) {
|
||
$where['biz_id'] = $this->biz_id;
|
||
$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 {
|
||
if (strlen($status)) {
|
||
$where['status'] = $status;
|
||
} else {
|
||
$where['status>='] = 0;
|
||
}
|
||
}
|
||
if ($type) {
|
||
unset($where['status']);
|
||
$where['status>='] = 0;
|
||
$where['brand_id>'] = 0;
|
||
if ($type == 'fq') {
|
||
|
||
} elseif ($type == 'pc') {
|
||
|
||
} elseif ($type == 'kp') {
|
||
|
||
} elseif ($type == 'jf') {
|
||
|
||
}
|
||
}
|
||
//下定时间
|
||
if ($order_s_time && $order_e_time) {
|
||
$where["order_time >="] = date('Y-m-d 00:00:00', strtotime($order_s_time));
|
||
$where["order_time <="] = date('Y-m-d 00:00:00', strtotime($order_e_time));
|
||
}
|
||
$count = $this->orders_model->count($where);
|
||
$lists = $map_users = [];
|
||
if ($count) {
|
||
$rows = $this->orders_model->select($where, 'id desc', $page, $size);
|
||
//销售顾问
|
||
$str_user_ids = implode(',', array_unique(array_column($rows, 'admin_id')));
|
||
$str_user_ids && $map_users = $this->app_user_model->map('id', 'uname', ["id in({$str_user_ids})" => null]);
|
||
//品牌车型
|
||
$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, 'series_id'));
|
||
$series = $this->auto_series_model->get_map_by_ids($series_arr, 'id,name');
|
||
//车系车型
|
||
$car_arr = array_unique(array_column($rows, 'car_id'));
|
||
$cars = $this->auto_cars_model->get_map_by_ids($car_arr, 'id,name');
|
||
|
||
//获取订单资料
|
||
// $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,insurance_img,business_img');
|
||
$order_datas = [];
|
||
$status_arr = $this->orders_model->get_status();
|
||
$allot = $this->get_allot();
|
||
foreach ($rows as $key => $val) {
|
||
$money_json = json_decode($val['money_json'], true);
|
||
$brand_name = isset($brands[$val['brand_id']]) ? $brands[$val['brand_id']][0]['name'] : '';
|
||
$serie_name = isset($series[$val['series_id']]) ? $series[$val['series_id']][0]['name'] : '';
|
||
$car_name = isset($cars[$val['car_id']]) ? $cars[$val['car_id']][0]['name'] : '';
|
||
|
||
$title = "{$brand_name}-{$serie_name}";
|
||
$other_data = [
|
||
'品牌' => ['type' => 'text', 'value' => $title, 'bg_color' => ''],
|
||
'车型' => ['type' => 'text', 'value' => $car_name, 'bg_color' => ''],
|
||
'付款方式' => ['type' => 'text', 'value' => $val['payway'] ? '全款' : '按揭', 'bg_color' => ''],
|
||
];
|
||
$other_data['订单合同'] = ['type' => 'text', 'value' => '未上传', 'bg_color' => Order_datas_entity::COLOR_UN_UPLOAD];
|
||
$other_data['付款凭证'] = ['type' => 'text', 'value' => '未上传', 'bg_color' => Order_datas_entity::COLOR_UN_UPLOAD];
|
||
$other_data['客户身份证'] = ['type' => 'text', 'value' => '未上传', 'bg_color' => Order_datas_entity::COLOR_UN_UPLOAD];
|
||
$val['order_time'] != '0000-00-00 00:00:00' && $other_data['下定时间'] = ['type' => 'text', 'value' => date('Y-m-d', strtotime($val['order_time'])), 'bg_color' => ''];
|
||
$val['delry_time'] != '0000-00-00 00:00:00' && $other_data['期望交付'] = ['type' => 'text', 'value' => date('Y-m-d', strtotime($val['delry_time'])), 'bg_color' => ''];
|
||
if ($map_users[$val['admin_id']]) {
|
||
$other_data['销售顾问'] = ['type' => 'text', 'value' => $map_users[$val['admin_id']], 'bg_color' => ''];
|
||
}
|
||
$lists[] = [
|
||
'id' => $val['id'],
|
||
'cus_id' => $val['customer_id'],
|
||
'name' => $val['name'],
|
||
'mobile' => $val['mobile'],
|
||
'company' => 111,
|
||
'status_name' => $status_arr[$val['status']],
|
||
'other_data' => $other_data,
|
||
'allot' => $allot,
|
||
];
|
||
|
||
}
|
||
}
|
||
$data = [
|
||
'list' => $lists,
|
||
'total' => $count
|
||
];
|
||
return $data;
|
||
}
|
||
|
||
private function detail($id)
|
||
{
|
||
$row = $this->orders_model->get(['id' => $id]);
|
||
if (!$row) {
|
||
throw new Exception('订单不存在', ERR_PARAMS_ERROR);
|
||
}
|
||
$money_json = json_decode($row['money_json'], true);
|
||
|
||
$brand = $this->auto_brand_model->get(['id' => $row['brand_id']], 'name');
|
||
$series = $this->auto_series_model->get(['id' => $row['series_id']], 'name');
|
||
$car = $this->auto_cars_model->get(['id' => $row['car_id']], 'name');
|
||
$payway = $row['payway'];
|
||
|
||
$car_data = [];
|
||
$car_data['车辆品牌'] = $brand['name'] . $series['name'];
|
||
$car_data['车型'] = $car['name'];
|
||
$row['color'] && $car_data['车身颜色'] = $row['color'];
|
||
$row['in_color'] && $car_data['内饰颜色'] = $row['in_color'];
|
||
$car_data['付款方式'] = $payway ? '全款' : '分期';
|
||
$car_data['期望交付时间'] = date('Y-m-d', strtotime($row['delry_time']));
|
||
if (!$payway) { // 分期
|
||
$car_data['首付款方式'] = $this->orders_model->downpayment_type($row['downpayment_type']);
|
||
$car_data['贷款额度'] = $money_json['loan_amount'];
|
||
$car_data['贷款期数'] = $money_json['loan_periods'];
|
||
$car_data['月供'] = $money_json['monthly_payment'];
|
||
}
|
||
$car_data['是否店内上牌'] = $row['if_num'] ? '是' : '否';
|
||
$car_data['是否店内投保'] = $row['if_insure'] ? '是' : '否';
|
||
if ($row['if_num']) {
|
||
$car_data['上牌费'] = $money_json['register_amount'] ? $money_json['register_amount'] : 0;
|
||
}
|
||
$money_json['confirm_amount'] && $car_data['定金'] = $money_json['confirm_amount'];
|
||
$money_json['discount_amount'] && $car_data['车身优惠'] = $money_json['discount_amount'];
|
||
|
||
$data = [
|
||
'id' => $id,
|
||
'name' => $row['name'],
|
||
'mobile' => $row['mobile'],
|
||
'payway' => $row['brand_id'] ? $row['payway'] : '',
|
||
'main_type' => intval($row['main_type']),
|
||
'brand_id' => $row['brand_id'],
|
||
'series_id' => $row['series_id'],
|
||
'car_id' => $row['car_id'],
|
||
'cus_id' => $row['customer_id'],
|
||
'color' => $row['color'],
|
||
'in_color' => $row['in_color'],
|
||
'status' => $row['status'],
|
||
'car_data' => $car_data,
|
||
'if_num' => $row['if_num'],
|
||
'if_insure' => $row['if_insure'],
|
||
'delry_time' => date('Y-m-d', strtotime($row['delry_time'])),
|
||
'money_json' => json_decode($row['money_json'], true),
|
||
'downpayment_type' => $row['downpayment_type'],
|
||
'cardid' => $row['card_id'],
|
||
'edit_info_status' => $row['status'] > 0 ? false : true,
|
||
'edit_order_status' => $row['status'] > 1 ? false : true,
|
||
];
|
||
//图片信息
|
||
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* Notes:判断分配客户权限
|
||
* Created on: 2022/7/27 10:02
|
||
* Created by: dengbw
|
||
* @return int
|
||
*/
|
||
private function get_allot()
|
||
{
|
||
$allot = 1;
|
||
$group_id = $this->session['group_id'];
|
||
if ($group_id == 1) {//销售不可分配用户
|
||
$allot = 0;
|
||
}
|
||
return $allot;
|
||
}
|
||
}
|