ctms/ctms-api/controllers/gm/v1/OrderController.php
fm453 4b842ebf3d ADD:添加后台管理端接口;
DEL:删除不必要的头部注释;
FIX:修正若干已知错误;
2025-06-30 09:40:13 +08:00

1234 lines
39 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace api\controllers\gm\v1;
use Yii;
use yii\data\Pagination;
use yii\helpers\Url;
use addons\models\AcCar;
use addons\models\AcCarOwner;
use addons\models\AcCarBrand;
use addons\models\AcCarSeries;
use addons\models\AcStore;
use addons\models\AcEmployee;
use addons\models\AcOrder;
use addons\models\AcOrderPre;
use addons\models\AcOrderStatus;
use addons\models\AcTruck;
use addons\models\AcDriver;
use addons\models\AcPlat;
use common\models\CVcode;
use addons\models\AcContact;
use common\models\Member as User;
use addons\models\AcUserExt;
use common\models\Fans;
use addons\models\AcFansExt;
class OrderController extends Common
{
protected $addtionWhere = [];
//附加特定的查询条件在查询search()时引用
public function beforeAction($action)
{
if (!$this->pid) {
$this->result('您正使用本系统内部接口,禁止非法链接使用!');
}
return parent::beforeAction($action);
}
public function actionIndex()
{
$apis = [
'list' => '搜索运单',
'detail' => '运单详情',
'get-status'=>'获取运单单状态字典',
'add' => '创建运单',
'edit' => '编辑运单',
'cancel'=>'取消',
'delete'=>'删除',
'today' => '今日运单',
'yesterday' => '昨日运单',
'qiantian' => '前日运单',
'future' => '近期运单',
'nocheck' => '待验车运单',
'week' => '本周运单',
'preweek' => '上周运单',
'month' => '本月运单',
'premonth' => '上月运单',
'dongbei' => '东三省运单',
'area1' => '京津冀运单',
'area2' => '川渝云贵运单',
'area3' => '江浙沪运单',
'mine' => '我的运单', //指派我或者我创建的
'tonghang' => '同行运单',
'ajax'=>[
'truckCar'=>'转待承运'
]
];
$this->result('您正使用CMTS-GM系统订单管理接口', $apis, 200);
}
//订单列表
public function actionList()
{
$s = $this->search();
$res = $s['res'];
if (!$res) {
$this->result('没有查询到相应的数据!', [], 0);
}
$data = $s['data'];
$this->showOrders($res, $data);
}
public function actionGetStatus()
{
$status = Yii::$app->params['OrderPreStatus'];
$this->result('查询成功!', $status, 200);
}
public function actionGetPayments()
{
$payments = $this->getPayments();
$this->result('查询成功!', $payments, 200);
}
//订单列表-昨日运单
public function actionYesterday()
{
//构建查询条件
$search = [];
//签约时间筛选
$date = strtotime('-1 day');
$day = date('Y-m-d', $date);
$s = $day . " 00:00:00";
$start = strtotime($s);
$e = $day . " 23:59:59";
$end = strtotime($e);
$search['sign_after'] = $end;
$search['sign_before'] = $start;
//执行查询
$s = $this->search($search);
$res = $s['res'];
if (!$res) {
$this->result('没有符合条件的结果');
}
$data = $s['data'];
$this->showOrders($res, $data);
}
//订单列表-今日运单
public function actionToday()
{
//构建查询条件
$search = [];
//签约时间筛选
$day = date('Y-m-d');
$s = $day . " 00:00:00";
$start = strtotime($s);
$e = $day . " 23:59:59";
$end = strtotime($e);
$search['sign_after'] = $end;
$search['sign_before'] = $start;
//执行查询
$s = $this->search($search);
$res = $s['res'];
if (!$res) {
$this->result('今日暂无运单');
}
$data = $s['data'];
$this->showOrders($res, $data);
}
//订单列表-前天运单
public function actionQiantian()
{
//构建查询条件
$search = [];
//签约时间筛选
$date = strtotime('-2 day');
$day = date('Y-m-d', $date);
$s = $day . " 00:00:00";
$start = strtotime($s);
$e = $day . " 23:59:59";
$end = strtotime($e);
$search['sign_after'] = $end;
$search['sign_before'] = $start;
//执行查询
$s = $this->search($search);
$res = $s['res'];
if (!$res) {
$this->result('没有符合条件的结果');
}
$data = $s['data'];
$this->showOrders($res, $data);
}
//订单列表-本周运单
public function actionWeek()
{
//构建查询条件
$search = [];
//签约时间筛选
$week = date('w'); //今天周几(0~6);
$days = $week - 1;
if ($week == 0) {
$date = strtotime('-6' . ' day'); //几天前
} else {
$date = strtotime('-' . $days . ' day'); //几天前
}
$day = date('Y-m-d', $date);
$s = $day . " 00:00:00";
$start = strtotime($s);
$e = $day . " 23:59:59";
$end = strtotime($e);
$search['sign_after'] = $end;
$search['sign_before'] = $start;
//执行查询
$s = $this->search($search);
$res = $s['res'];
if (!$res) {
$this->result('没有符合条件的结果');
}
$data = $s['data'];
$this->showOrders($res, $data);
}
//订单列表-上周运单
public function actionPreweek()
{
//构建查询条件
$search = [];
//签约时间筛选
$week = date('w'); //今天周几(0~6);
$days = 7 + ($week - 1);
if ($week == 0) {
$date = strtotime('-13' . ' day'); //几天前
$date_end = strtotime('-7' . ' day'); //几天前
} else {
$date = strtotime('-' . $days . ' day'); //几天前
$date_end = strtotime('-' . $week . ' day'); //几天前
}
$day = date('Y-m-d', $date);
$s = $day . " 00:00:00";
$start = strtotime($s);
$day = date('Y-m-d', $date_end);
$e = $day . " 23:59:59";
$end = strtotime($e);
$search['sign_after'] = $end;
$search['sign_before'] = $start;
//执行查询
$s = $this->search($search);
$res = $s['res'];
if (!$res) {
$this->result('没有符合条件的结果');
}
$data = $s['data'];
$this->showOrders($res, $data);
}
//订单列表-本月运单
public function actionMonth()
{
//构建查询条件
$search = [];
//签约时间筛选
$month = date('Y-m');
$s = $month . "-01 00:00:00";
$start = strtotime($s);
$last_day = strtotime("last day of " . $month);
$e = date("Y-m-d", $last_day) . " 23:59:59";
$end = strtotime($e);
$search['sign_after'] = $end;
$search['sign_before'] = $start;
//执行查询
$s = $this->search($search);
$res = $s['res'];
if (!$res) {
$this->result('没有符合条件的结果');
}
$data = $s['data'];
$this->showOrders($res, $data);
}
//订单列表-上月运单
public function actionPremonth()
{
//构建查询条件
$search = [];
//签约时间筛选
$month = date('Y-m', strtotime("-1 month"));
$s = $month . "-01 00:00:00";
$start = strtotime($s);
$last_day = strtotime("last day of " . $month);
$e = date("Y-m-d", $last_day) . " 23:59:59";
$end = strtotime($e);
$search['sign_after'] = $end;
$search['sign_before'] = $start;
//执行查询
$s = $this->search($search);
$res = $s['res'];
if (!$res) {
$this->result('没有符合条件的结果');
}
$data = $s['data'];
$this->showOrders($res, $data);
}
//订单列表-东北运单(黑吉辽方向)(包含部分内蒙古的)
public function actionDongbei()
{
//构建查询条件
$search = [];
//省份城市筛选
$search['provinces'] = ['210000', '220000', '230000']; //辽,吉,黑
$search['citys'] = ['海拉尔', '齐齐哈尔'];
//执行查询
$s = $this->search($search);
$res = $s['res'];
if (!$res) {
$this->result('没有符合条件的结果');
}
$data = $s['data'];
$this->showOrders($res, $data);
}
//订单列表-京津冀运单(包含部分内蒙古的)
public function actionArea1()
{
//构建查询条件
$search = [];
//省份城市筛选
$search['provinces'] = ['110000', '120000', '130000']; //京,津,冀
$search['citys'] = ['包头', '呼和浩特'];
//执行查询
$s = $this->search($search);
$res = $s['res'];
if (!$res) {
$this->result('没有符合条件的结果');
}
$data = $s['data'];
$this->showOrders($res, $data);
}
//订单列表-川渝云贵运单
public function actionArea2()
{
//构建查询条件
$search = [];
//省份城市筛选
$search['provinces'] = ['500000', '510000', '520000', '530000']; //渝\川\贵\云
//执行查询
$s = $this->search($search);
$res = $s['res'];
if (!$res) {
$this->result('没有符合条件的结果');
}
$data = $s['data'];
$this->showOrders($res, $data);
}
//订单列表-江浙沪运单
public function actionArea3()
{
//构建查询条件
$search = [];
//省份城市筛选
$search['provinces'] = ['310000', '320000', '330000']; //沪、江、浙
//执行查询
$s = $this->search($search);
$res = $s['res'];
if (!$res) {
$this->result('没有符合条件的结果');
}
$data = $s['data'];
$this->showOrders($res, $data);
}
//订单列表-未验车运单
public function actionNocheck()
{
//构建查询条件
$search = [];
$search['is_check'] = 0;
//执行查询
$s = $this->search($search);
$res = $s['res'];
if (!$res) {
$this->result('没有符合条件的结果');
}
$data = $s['data'];
$this->showOrders($res, $data);
}
//订单列表-我的运单(我经手或我创建的)
public function actionMine()
{
//构建查询条件
$search = [];
//创建者或关联员工筛选
$orWhere = [];
$orWhere[] = 'or';
$orWhere[] = ['=', 'create_by', $this->user_id];
$orWhere[] = ['=', 'employee_id', $this->employee_id];
$this->addtionWhere = $orWhere; //特别约定的条件格式
//执行查询
$s = $this->search($search);
$res = $s['res'];
if (!$res) {
$this->result('没有符合条件的结果');
}
$data = $s['data'];
$this->showOrders($res, $data);
}
public function actionDetail()
{
$order = $this->preUpdate();
$this->showOrder($order);
}
public function actionAdd()
{
//数据预检查、编排
$pre = $this->preSave('add');
if(!$pre['data']) $this->result('数据预检查未通过,保存失败', $pre['data'], 100);
//保存资料
$model = new AcOrder();
foreach ($pre['data'] as $key => $val) {
$model->$key = $val;
}
$res = $model->save();
$msg = '数据保存失败!';
if(!$res) $this->result($msg, [], 100);
$msg = '数据保存成功!';
if($pre['err']) $msg .= $pre['err_msg'];
$return = [];
$return['id']= $model->attributes['id']; //获取插入后id;
$this->result($msg,$return, 200);
}
public function actionEdit()
{
$detail = $this->preUpdate();
$res = $this->userPrivilege($this->user_info,'orderEdit',$detail);
if(!$res) $this->result('您没有操作权限!',[],403);
//数据预检查、编排
$pre = $this->preSave('edit');
if(!$pre['data']) $this->result('数据预检查未通过,保存失败', $pre['data'], 100);
//保存资料
$data = $pre['data'];
foreach ($data as $key=>$val) {
$detail->$key = $val;
}
$res = $detail->save();
$msg = '数据编辑失败!';
if(!$res) $this->result($msg, [], 100);
$msg = '数据编辑成功!';
if($pre['err']) $msg .= $pre['err_msg'];
$return = [];
$return['data']= $detail;
$this->result($msg,$return, 200);
}
public function actionCancel()
{
$order = $this->preUpdate();
$res = $this->userPrivilege($this->user_info,'orderEdit',$order);
if(!$res) $this->result('您没有操作权限!',[],403);
if ($order->is_cwqr > 0) $this->result('订单已入账,请先联系财务销账后再取消!',[],403);
if ($order->status_code <= 0 || $order->status_code >= 99) {
$this->result('订单已取消或已经完成,不允许操作!',[],403);
}
$order->status_code = -1;
$res = $order->save();
if($res) $this->result('已取消!', $order, 200);
$this->result('数据保存失败!', $res, 0);
}
public function actionDelete()
{
$detail = $this->preUpdate();
$res = $this->userPrivilege($this->user_info,'orderDelete',$detail);
if(!$res) $this->result('您没有操作权限!',[],403);
if ($detail->deleted != 0) {
$this->result('数据已被删除过,操作无效', [], 400);
}
$data = [];
$data['update_at'] = time();
$data['deleted'] = $detail->deleted + 1;
//保存资料
foreach ($data as $key=>$val) {
$detail->$key = $val;
}
$res = $detail->save();
$msg = '运单记录删除失败!';
if(!$res) $this->result($msg, [], 100);
$msg = '运单记录删除成功!';
$this->result($msg,[], 200);
}
/**
* AJAX方式操作运单
* @return void
*/
public function actionAjax()
{
$detail = $this->preUpdate();
$get = Yii::$app->request->get();
$msg = '';
$errorCode = 0;
$_time = time();
$detail->update_at = $_time;
$res = true;
switch ($get['do']) {
case 'truckCar':
//转待承运状态
if ($detail->status_code == -1) {
$msg = '订单已被取消,不能承运';
$errorCode = 404;
$this->result($msg,[],$errorCode);
}
if ($detail->status_code != 1) {
$msg = '订单当前非初始状态(协议签订),不可作承运处理';
$errorCode = 404;
$this->result($msg,[],$errorCode);
}
$detail->status_code = 5;
$res = $detail->save();
if($res){
$msg = '订单已转为待承运状态';
$errorCode = 200;
}else{
$msg = '订单标记失败,请稍后再试';
$errorCode = 0;
}
break;
default:
break;
}
$this->result($msg, [], $errorCode);
}
/**
* 订单搜索
* @param $search [] 预置搜索条件
* @param $sql string 直接使用SQL语句,需要配合$args一同使用传入后search一切条件无效
* @param array $args sql的参数
* @return array
*/
private function search($search = [], $sql = '', $args = [])
{
$pid = $this->pid;
$return = [];
$model = new AcOrder();
$page = $this->page;
$pageSize = $this->pageSize;
if ($sql && $args) {
$data = $model->find()->where($sql, $args);
} else {
$where = $orWhere = [];
$where[] = 'and';
$where[] = ['=', 'pid', $pid];
$deleted = 0;
$where[] = ['=', 'deleted', $deleted];
$where[] = ['=', 'is_turnover', 0]; //排除交车单
$post = $this->postdata;
$search = $search ?? $post['search'];
//车牌号使用完整精准搜索皖A88888
$CarModel = new AcCar();
if (!empty($search['carno'])) {
$search['carno'] = trim($search['carno']);
$sql = 'CONCAT(num_p,num_area,num_left) = :carno or num_frame LIKE :carno';
$cars = $CarModel->find()->where($sql, [':carno' => $search['carno']])->limit(5)->offset(0)->all();
//最多仅显示可匹配车牌号的前5个
if (empty($cars)) {
$return['code'] = 400;
$return['msg'] = '未查询到相关车辆';
return ['res' => FALSE, 'data' => $return];
} else {
$car_ids = [];
foreach ($cars as $car) {
$car_ids[] = $car->id;
}
$where[] = ['IN', 'car_id', $car_ids];
}
}
if (!empty($search['city'])) {
$search['city'] = trim($search['city']);
$where[] = ['LIKE', 'aim_city', $search['city']];
}
if (!empty($search['phone'])) {
$search['phone'] = trim($search['phone']);
$orwhere = ['or', ['LIKE', 'sender_mobiles', $search['phone']], ['LIKE', 'receiver_mobiles', $search['phone']]];
}
$search['employee'] = isset($search['employee']) ? (int)$search['employee'] : '';
$EmployeeModel = new AcEmployee();
if (!empty($search['employee'])) {
$where[] = ['=', 'employee_id', $search['employee']];
$employee = $EmployeeModel->findOne($search['employee']);
$search['employee_title'] = isset($employee->name) ? $employee->name : '';
}
$search['store_id'] = isset($search['store_id']) ? (int)$search['store_id'] : '';
if (empty($search['store_id'])) {
$search['store_id'] = isset($get['store_id']) ? (int)$get['store_id'] : 0;
}
$StoreModel = new AcStore();
if (!empty($search['store_id'])) {
$where[] = ['=', 'store_id', $search['store_id']];
$store = $StoreModel->findOne($search['store_id']);
$search['store_title'] = isset($store->title) ? $store->title : '';
}
$search['from_mid'] = isset($search['from_mid']) ? (int)$search['from_mid'] : '';
$UserModel = new User();
if (!empty($search['from_mid'])) {
$where[] = ['=', 'from_mid', $search['from_mid']];
$user = $UserModel->findById(['id' => $search['from_mid']]);
$search['from_mtitle'] = isset($user->username) ? $user->username : '';
}
$provinces = $search['provinces'] ?? [];
if ($provinces) {
$_orwhere = [
'or',
['IN', 'aim_province', $provinces],
['IN', 'start_province', $provinces]
];
$orWhere[] = $_orwhere;
} else {
$search['start_province'] = isset($search['start_province']) ? trim($search['start_province']) : '';
if (!empty($search['start_province'])) {
$where[] = ['=', 'start_province', $search['start_province']];
}
$search['aim_province'] = isset($search['aim_province']) ? trim($search['aim_province']) : '';
if (!empty($search['aim_province'])) {
$where[] = ['=', 'aim_province', $search['aim_province']];
}
}
$citys = $search['citys'] ?? [];
if ($citys) {
$_orwhere = [
'or',
['OR LIKE', 'aim_city', $citys],
['OR LIKE', 'start_city', $citys]
];
$orWhere[] = $_orwhere;
} else {
$search['aim_city'] = isset($search['aim_city']) ? trim($search['aim_city']) : '';
if (!empty($search['aim_city'])) {
$where[] = ['LIKE', 'aim_city', $search['aim_city']];
}
$search['start_city'] = isset($search['start_city']) ? trim($search['start_city']) : '';
if (!empty($search['start_city'])) {
$where[] = ['LIKE', 'start_city', $search['start_city']];
}
}
$search['signer'] = isset($search['signer']) ? trim($search['signer']) : '';
$search['wtdw'] = isset($search['wtdw']) ? trim($search['wtdw']) : '';
$search['weituo'] = isset($search['weituo']) ? (int)$search['weituo'] : 0;
if ($search['weituo']) {
$where[] = ['=', 'weituo', $search['weituo']];
} else if (!empty($search['wtdw'])) {
$where[] = ['LIKE', 'wtdw', $search['wtdw']];
} else if (!empty($search['signer'])) {
$where[] = ['LIKE', 'signer', $search['signer']];
}
$search['sender_name'] = isset($search['sender_name']) ? trim($search['sender_name']) : '';
if (!empty($search['sender_name'])) {
$where[] = ['LIKE', 'sender_name', $search['sender_name']];
}
$search['receiver_name'] = isset($search['receiver_name']) ? trim($search['receiver_name']) : '';
if (!empty($search['receiver_name'])) {
$where[] = ['LIKE', 'receiver_name', $search['receiver_name']];
}
$search['bind'] = isset($search['bind']) ? trim($search['bind']) : '';
if (!empty($search['bind'])) {
$where[] = ['LIKE', 'bind', $search['bind']];
}
if (isset($search['status_code']) && $search['status_code'] != 'all') {
$search['status_code'] = (int)$search['status_code'];
$where[] = ['=', 'status_code', $search['status_code']];
} else {
unset($search['status_code']);
}
if (isset($search['is_holdon'])) {
$search['is_holdon'] = (int)$search['is_holdon'];
$where[] = ['=', 'is_holdon', $search['is_holdon']];
}
if (isset($search['is_over'])) {
$search['is_over'] = (int)$search['is_over'];
$where[] = ['=', 'is_over', $search['is_over']];
}
if (isset($search['is_commission'])) {
$search['is_commission'] = (int)$search['is_commission'];
$where[] = ['=', 'is_commission', $search['is_commission']];
}
//运单是否通过了财务确认
$search['is_cwqr'] = $search['is_cwqr'] ?? ($get['is_cwqr'] ?? '');
if ($search['is_cwqr'] != '') {
$search['is_cwqr'] = (int)$search['is_cwqr'];
$where[] = ['=', 'is_cwqr', $search['is_cwqr']];
}
//签约时间筛选
$search['sign_m'] = isset($search['sign_m']) ? $search['sign_m'] : '';
$search['sign_after'] = isset($search['sign_after']) ? $search['sign_after'] : '';
$search['sign_before'] = isset($search['sign_before']) ? $search['sign_before'] : '';
//按月份筛选
if (!empty($search['sign_m'])) {
$start = strtotime($search['sign_m']);
$s = date("Y-m-d H:i:s", $start);
$ldm = strtotime("last day of " . $s);
$m = date("Y-m-d", $ldm) . " 23:59:59";
$end = strtotime($m);
$where[] = ['between', 'sign_date', $start, $end];
} //按指定时段筛选
else if (!empty($search['sign_after']) && !empty($search['sign_before'])) {
$start = strtotime($search['sign_after']);
$end = strtotime($search['sign_before']);
$where[] = ['between', 'sign_date', $start, $end];
} //按指定时间临界点筛选(以不早于为优先筛选条件)
else if (!empty($search['sign_after'])) {
$start = strtotime($search['sign_after']);
$where[] = ['>=', 'sign_date', $start];
} else if (!empty($search['sign_before'])) {
$end = strtotime($search['sign_before']);
$where[] = ['<=', 'sign_date', $end];
}
//仅查看我自己的
if (isset($search['onlyme']) && $search['onlyme'] == 1) {
$where[] = ['=', 'create_by', $this->user_id];
}
//带序列号搜索TBD后期考虑改为精准唯一搜索
if (isset($search['sn']) && $search['sn'] != 'undefined' && $search['sn']) {
$where[] = ['LIKE', 'sn', $search['sn']];
}
//引用设定特别约定的查询条件(不允许从客户端直接传入)
if ($this->addtionWhere) {
$orWhere[] = $this->addtionWhere;
}
$data = $model->find()->where($where);
//要确保$orWhere的每一个子键都是['or',['=',字段,值],['=',字段,值]]的方式or,=等符号是约定俗成的
if ($orWhere) {
foreach ($orWhere as $v) {
$data = $data->andWhere($v);
}
}
}
$pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $pageSize]);
$pages->setPage($page - 1, TRUE); //设置分页的当前页面值
$_orderby = 'sign_date DESC,id DESC';
$res = $data->offset($pages->offset)->limit($pages->limit)->orderby($_orderby)->all();
return ['res' => $res, 'data' => $data];
}
/*
订单数据显示格式化
*/
private function showOrders($orderRes, $data)
{
$status = Yii::$app->params['OrderStatus'];
$cids = $sids = $eids = $mids = [];
if ($orderRes) {
foreach ($orderRes as $r) {
$cids[$r->car_id] = $r->car_id;
$sids[$r->store_id] = $r->store_id;
$eids[$r->employee_id] = $r->employee_id;
$mids[$r->from_mid] = $r->from_mid;
}
}
$model = new AcStore();
$_stores = $model->find()->where(['in', 'id', $sids])->indexBy('id')->all();
$stores = [];
foreach ($_stores as $s) {
$stores[$s->id] = $s->toArray();
}
$model = new AcEmployee();
$employees = [];
$_employees = $model->find()->where(['in', 'id', $eids])->indexBy('id')->all();
foreach ($_employees as $s) {
$employees[$s->id] = $s->toArray();
}
$model = new Fans();
$users = [];
$_users = $model->find()->where(['in', 'id', $mids])->all();
foreach ($_users as $s) {
$s = $s->toArray();
$users[$s['id']] = $s;
}
$model = new AcCar();
$cars = [];
$_cars = $model->find()->where(['in', 'id', $cids])->indexBy('id')->all();
$bids = $sids = [];
foreach ($_cars as $s) {
$bids[] = $s->brand_id;
$sids[] = $s->series_id;
$cars[$s->id] = $s->toArray();
}
unset($s);
$model = new AcCarBrand();
$brands = $model->find()->where(['in', 'id', $bids])->indexBy('id')->all();
$model = new AcCarSeries();
$series = $model->find()->where(['in', 'id', $sids])->indexBy('id')->all();
if ($cars) {
foreach ($cars as &$car) {
$car['title2'] = '';
if (isset($brands[$car['brand_id']])) {
$car['title2'] .= $brands[$car['brand_id']]['title'];
}
if (isset($series[$car['series_id']])) {
$car['title2'] .= $series[$car['series_id']]['title'];
}
$car['number2'] = $car['num_frame'];
if ($car['num_p']) {
$car['number2'] = $car['num_p'];
}
if ($car['num_area']) {
$car['number2'] .= $car['num_area'];
}
if ($car['num_left']) {
$car['number2'] .= $car['num_left'];
}
}
}
unset($car);
$orders = [];
foreach ($orderRes as $s) {
$car = [];
if ($s->car_id) {
$car = $cars[$s->car_id];
}
$s = $s->toArray();
$weekdays = [0 => '日', 1 => '一', 2 => '二', 3 => '三', 4 => '四', 5 => '五', 6 => '六'];
$wk = date('w', $s['sign_date']);
$s['signdate'] = date('Y-m-d ', $s['sign_date']) . '星期' . $weekdays[$wk];
$s['from_province'] = $s['start_province'] ? Yii::$app->params['regionAreas'][$s['start_province']] : '';
$s['to_province'] = $s['aim_province'] ? Yii::$app->params['regionAreas'][$s['aim_province']] : '';
$s['car_number'] = $car['number2'] ?? '';
$s['car_title'] = $car['title'] ?? '';
$s['car_title'] = $s['car_title'] ? $s['car_title'] : ($car['title2'] ?? '');
$s['status'] = $status[$s['status_code']];
$orders[] = $s;
}
unset($s);
$return = [];
$return['code'] = 200;
$return['msg'] = '订单查询成功!';
$return['data'] = [
'total' => $data->count(),
'orders' => $orders,
'employees' => $employees,
'stores' => $stores,
'users' => $users,
'page' => $this->page
];
$this->result($return['msg'], $return['data'], $return['code']);
}
/*
* 数据更新前的预检查,返回对应关联数据
* 必须确保get与post数据中均包含需更新的数据id且一致
* */
private function preUpdate()
{
$id = (int)Yii::$app->request->get('id');
if(!$id) $this->result('请求错误未携带ID参数');
$post = $this->postdata;
if($post['id'] != $id) $this->result('传参id与请求数据不匹配',[],403);
$model = new AcOrder();
$res = $model->findOne($id);
if(!$res) $this->result('未查询到相应数据',[],404);
if($res->pid != $this->pid) $this->result('非本平台数据,不允许操作',[],401);
return $res;
}
/*
* 数据保存前的预检查(查重、参数校验等)
* 要保存的数据[],直接从post中取出
* @op操作类型add,edit……
* 校验机制:
* 编辑数据时必须用get方式传入参数id并与post进来的数据id进行比对只有一致时才能继续;
* 查重:禁止录入重复数据
* 返回:校验重组后的数据
* */
private function preSave($op)
{
$post = $this->postdata;
$data = [];
$_time = time();
//格式化数据
//目标键=》POST键
switch ($op) {
case 'add':
$data['create_by'] = $this->user_id;
$data['status_code'] = 1; //订单处于创建待审核状态
$data['create_at'] = $_time;
$data['update_at'] = $_time;
break;
case 'edit':
$id = (int)Yii::$app->request->get('id');
$_id = (int)$post['id'];
if(!$id != $_id) $this->result('id参数不匹配请检查');
$data['update_at'] = $_time;
break;
}
$data['pid'] = $this->pid;
$cols = ['start_province' => 'start_province', 'start_city' => 'start_city', 'aim_province' => 'aim_province', 'aim_city' => 'aim_city', 'aim_address' => 'aim_address',
'signer' => 'signer', 'bind' => 'bind', 'wtdw' => "wtdw",
'sender_name' => 'sender_name', 'sender_idcard' => 'sender_idcard', 'sender_mobiles' => 'sender_mobiles',
'receiver_name' => 'receiver_name', 'receiver_idcard' => 'receiver_idcard', 'receiver_mobiles' => 'receiver_mobiles',
'car_extra' => 'car_extra'];
foreach ($cols as $col=>$key) {
if (isset($post[$key]) && $post[$key] !='') {
$data[$col] = trim($post[$key]);
}
}
$cols = ['car_id'=>'car_id','car_value' => 'car_value', 'fee_insurance' => 'fee_insurance', 'fee_truck' => 'fee_truck', 'fee_deliver' => 'fee_deliver', 'fee_pickup' => 'fee_pickup', 'fee_urgent' => 'fee_urgent', 'fee_extra' => 'fee_extra', 'fee_total' => 'fee_total', 'pay_send' => 'pay_send', 'pay_receive' => 'pay_receive', 'not_insurance' => 'not_insurance', 'weituo' => 'weituo'];
foreach ($cols as $col => $key) {
$data[$col] = isset($post[$key]) ? (int)$post[$key] : 0;
}
//图片组处理
$col = $key = 'thumbs';
if (isset($post[$key]) && !empty($post[$key])) {
$imgs = $post[$key]; //数组格式
$imgs = array_unique($imgs);
$imgs = json_encode($imgs);
$data[$col] = $imgs;
}
$col = $key = 'remark';
if (isset($post[$key]) && $post[$key] != '') {
$data[$col] = htmlspecialchars($post[$key]);
}
$sign_date = isset($post['sign_date']) ? strtotime($post['sign_date']) : '';
$data['sign_date'] = $sign_date ? $sign_date : $_time;
//部分规则校验
$has_err = false;
$_err = [];
$data['is_cwqr'] = 0; //修改财务确认状态为待确认
$DefaultCarInsuranceValueMax =Yii::$app->params['DefaultCarInsuranceValueMax'];
/*不投保的保额不超过20W保费为0*/
if ($data['not_insurance']) {
if ($data['fee_insurance']) {
$data['fee_insurance']=0;
$has_err = true;
$_err[] = '勾选了不投保已强制修改保费为0';
}
if ($data['car_value']>$DefaultCarInsuranceValueMax) {
$data['car_value'] = $DefaultCarInsuranceValueMax;
$has_err = true;
$_err[] = '勾选了不投保,默认最高赠送的运输险保额不超过'.$DefaultCarInsuranceValueMax.'万元,已自动强制校正';
}
}
/*运费校验*/
$fee_total = $data['fee_truck']+$data['fee_insurance']+$data['fee_deliver']+$data['fee_pickup']+$data['fee_urgent']+$data['fee_extra'];
$fee_pay = $data['pay_send']+$data['pay_receive'];
if (!$data['fee_truck']) {
$has_err = true;
$_err[] = '请注意,运费未正确填写!';
}
if (!$data['pay_send']&&!$data['pay_receive']) {
$has_err = true;
$_err[] = '请注意,到收运费及起运地预收运费均未填写!';
}
if ($fee_total !=$data['fee_total']) {
$has_err = true;
$_err[] = '请注意,所填运费、保费、提车费、送车费、额外费用、加急费等各项费用汇总的值与所填写的总费用不一致,请检查并更新';
}
if ($fee_pay !=$data['fee_total']) {
$has_err = true;
$_err[] = '请注意,所填起运地预付运费、到收运费的汇总值与所填写的总费用不一致,请检查并更新';
}
/*门店归属*/
if($data['employee_id']){
$employeeModel = new AcEmployee();
$employee = $employeeModel->findOne($this->employee_id);
if(!$employee){
$has_err = true;
$_err[] = '指定的员工信息并不存在,已自动清除关联';
$data['employee_id'] = 0;
}
if ($employee && !$data['store_id']) {
$data['store_id'] = $employee->store_id;
}
}
//查询联系人信息是否在库
$sender_mobiles = isset($data['sender_mobiles']) ? $data['sender_mobiles'] : null;
$receiver_mobiles = isset($data['receiver_mobiles']) ? $data['receiver_mobiles'] : null;
if (isMobile($sender_mobiles)) {
$this->contactorToFans($sender_mobiles,$data);
}
if ($receiver_mobiles && $receiver_mobiles != $sender_mobiles) {
if (isMobile($receiver_mobiles)) {
$this->contactorToFans($receiver_mobiles,$data);
}
}
//生成现付说明
$payments = $this->getPayments();
if ($post['pay_send']) {
$txt = $post['payto'] . '收款' . $post['pay_send'] . '元(' . $payments[$post['payment']] . '';
if ($data['remark']) {
$data['remark'] .= ' ; ' . $txt;
} else {
$data['remark'] = $txt;
}
}
//没有指定车辆ID时生成车辆信息(确保前端传入了必须的参数isTempCarnocarno_leftcarno_frame,carno_p,carno_area,carno_left,carno_title
if(!$data['car_id']){
$carModel = new AcCar();
$where = [];
if ($post['isTempCarno']) {
$post['carno_left'] .= '临';
}
if (empty($post['carno_frame'])) {
$where['num_p'] = $post['carno_p'];
$where['num_area'] = $post['carno_area'];
$where['num_left'] = $post['carno_left'];
}
$post['carno_frame'] = $post['carno_frame'] ?? '';
if (!empty($post['carno_frame'])) {
$where['num_frame'] = $post['carno_frame'];
}
$car = $carModel->find()->where($where)->one();
if (empty($car)) {
$carModel->pid = $this->pid;
$carModel->num_frame = $post['carno_frame'];
$carModel->num_p = $post['carno_p'];
$carModel->num_area = $post['carno_area'];
$carModel->num_left = $post['carno_left'];
$carModel->title = $post['carno_title'];
$carModel->status_code = 1;
$carModel->create_at = $_time;
$carModel->update_at = $_time;
$carModel->save();
$car_id = $carModel->attributes['id'];
} else {
$car_id = $car->id;
}
$data['car_id'] = $car_id;
}
$return = ['data'=>$data,'err'=>$has_err,'err_msg'=>$_err];
return $data;
}
/*
* 将联系人信息转存入表同时存入主数据库的fans表
* */
private function contactorToFans($mobile,$data)
{
$_time = time();
$res = false;
//查询联系人信息是否在库
$contactModel = new AcContact();
$contactor = $contactModel->find()->where(['mobile'=>$mobile,'pid'=>$this->pid])->one();
if($contactor){
$name = $data['sender_name'] ?? $contactor->name;
$idcard = $data['idcard'] ?? $contactor->idcard;
if($name != $contactor->name || $idcard != $contactor->idcard){
$contactor->update_at = $_time;
$contactor->name = $name;
$contactor->idcard = $idcard;
$res = $contactor->save();
}
}else{
$contactModel->name = $data['sender_name'] ?? '';
$contactModel->mobile = $data['sender_mobiles'];
$contactModel->pid = $this->pid;
$contactModel->idcard = $data['sender_idcard'] ?? '';
$contactModel->create_at = $_time;
$contactModel->status_code = 1;
$res = $contactModel->save();
}
//框架核心数据表Fans
$userModel = new Fans();
$user = $userModel->findByUsername($mobile);
if (!$user) {
$userModel->mobile = $mobile;
$userModel->email = $mobile.'@hiluker.com';
$userModel->setPassword(Yii::$app->params['defaultFansPassword']);
$userModel->generateAuthKey();
$userModel->created_at = $_time;
$userModel->from = 'ctms';
$userModel->save();
}
return $res;
}
private function showOrder($order)
{
$detail = $order->toArray();
$model = new Fans();
$fans = $model->findOne($detail['from_mid']);
$user = $fans->toArray();
$model = new AcFansExt();
$_exts = $model->find()->where(['mid' => $detail['from_mid'],'mobile'=>$fans->mobile, 'pid' => $this->pid, 'deleted' => 0])->indexBy('id')->all();
foreach ($_exts as $ext) {
$ext = $ext->toArray();
$user[$ext['key']] = $ext['value'];
}
$detail['_user'] = $user;
if ($detail['store_id']) {
$model = new AcStore();
$store = $model->findOne($detail['store_id']);
if ($store) {
$detail['store_title'] = $store->title;
$detail['_store'] = $store->toArray();
}
}
if ($detail['employee_id']) {
$model = new AcEmployee();
$employee = $model->findOne($detail['employee_id']);
if ($employee) {
$detail['employee_title'] = $employee->name;
$detail['_employee'] = $employee->toArray();
}
}
$detail['car_owner_id'] = $detail['car_brand_id'] = $detail['car_series_id'] = 0;
$model = new AcCar();
$car = $model->findOne($detail['car_id']);
if ($car) {
$detail['car_owner_id'] = $car->owner_id;
$detail['car_brand_id'] = $car->brand_id;
$detail['car_series_id'] = $car->series_id;
$detail['car_number'] = $car->num_p . $car->num_area . $car->num_left;
$detail['car_number'] = $detail['car_number'] ?: $car->num_frame;
$detail['_car'] = $car->toArray();
}
$AcCarOwner = new AcCarOwner();
$owner = $AcCarOwner->findOne($detail['car_owner_id']);
if ($owner) {
$_owner = User::findOne($detail['car_owner_id']);
$detail['car_owner'] = $_owner->username . '【' . $_owner->mobile . '】';
$detail['_car_owner'] = $_owner->toArray();
}
$AcCarBrand = new AcCarBrand();
$brand = $AcCarBrand->findOne($detail['car_brand_id']);
if ($brand) {
$detail['car_title'] = $brand->title;
}
$AcCarSeries = new AcCarSeries();
$series = $AcCarSeries->findOne($detail['car_series_id']);
if ($series) {
$detail['car_title'] .= $series->title;
}
$sn = '00000000000'; //11位
$sn = substr($sn, 0, 11 - strlen($order->id));
$sn .= $order->id;
$detail['sn'] = $detail['sn'] ? $detail['sn'] : $sn;
$weekdays = [0 => '日', 1 => '一', 2 => '二', 3 => '三', 4 => '四', 5 => '五', 6 => '六'];
$wk = date('w', $detail['date']);
$detail['signdate'] = date('Y-m-d 星期' . $weekdays[$wk], $detail['sign_date']);
$detail['from_province'] = isset($detail['start_province']) ? Yii::$app->params['regionAreas'][$detail['start_province']] : '';
$detail['to_province'] = isset($detail['aim_province']) ? Yii::$app->params['regionAreas'][$detail['aim_province']] : '';
$detail['remark'] = $detail['remark'] || '';
$detail['remark'] = trim($detail['remark']) ? htmlspecialchars_decode(trim($detail['remark'])) : '';
$imgDir = Yii::getAlias('@upload');
$imgKeys = ['driving_lisence', 'thumb_f', 'thumb_b', 'thumb_l', 'thumb_r'];
foreach ($imgKeys as $k) {
$detail[$k] = $detail[$k] ? $detail[$k] : '/pics/nopic.jpg';
$url_pre = substr($detail[$k], 0, 4);
if ($url_pre == '/pic') {
$detail[$k . '_url'] = Url::to('@upload' . $detail[$k], $this->imgHttp);
} else if ($url_pre == 'http') {
$detail[$k . '_url'] = $detail[$k];
}
}
$detail['thumbs'] = $detail['thumbs'] ? json_decode($detail['thumbs']) : [];
if ($detail['thumbs']) {
$thumbs = $detail['thumbs'];
$thumbs_url = [];
foreach ($thumbs as $k => $v) {
$url_pre = substr($v, 0, 4);
if ($url_pre == '/pic') {
$v_url = Url::to('@upload' . $v, $this->imgHttp);
} else if ($url_pre == 'http') {
$v_url = $v;
}
$thumbs_url[] = ['src' => $v, 'url' => $v_url];
}
$detail['thumbs_url'] = $thumbs_url;
}
$this->result('订单查询成功!', $detail, 200);
}
private function getPayments(){
$payments = [
'wechat' => '微信',
'alipay' => '支付宝',
'cash' => '现金',
'bankcard' => '银行卡',
'bankunion' => '银联',
'others' => '其他'
];
return $payments;
}
}