ctms/ctms-admin/controllers/OrderpreController.php
2025-04-10 23:19:13 +08:00

863 lines
31 KiB
PHP
Executable File

<?php
/**
* @Author: fm453
* @Date: 2018-08-17 19:39:41
* @Email: fm453@lukegzs.com
*/
namespace backend\controllers;
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\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 OrderpreController extends Common
{
//替代常规的_construct 析构函数;其他方法调用前执行
public function init()
{
parent::init();
}
//主界面
public function actionIndex()
{
global $_HI,$_FM;
return $this->render('../layouts/dev', []);
}
//订单列表
public function actionList()
{
global $_HI,$_FM;
$pid = $this->pid;
$session = Yii::$app->session;
$model = new AcOrderPre();
$where = [];
$where[]='and';
$where[] = ['=','pid',$pid];
$post = Yii::$app->request->post();
$get = Yii::$app->request->get();
$searchSession = 'orderPre::search';
if (isset($_GET['reset']) && $_GET['reset']==1) {
$post['search'] = [];
}
if (isset($post['search'])) {
$search = $post['search'];
$session->set($searchSession, $search);
} elseif ($session->get($searchSession)) {
$search = $session->get($searchSession);
}
$search['car_no'] = isset($search['car_no']) ? trim($search['car_no']) : '';
if (!empty($search['car_no'])) {
$where[] = ['=','car_no',$search['car_no']];
}
$search['car_title'] = isset($search['car_title']) ? trim($search['car_title']) : '';
if (!empty($search['car_title'])) {
$where[] = ['LIKE','car_no',$search['car_no']];
}
$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 : '';
}
$search['start_province'] = isset($search['start_province']) ? trim($search['start_province']) : '';
if (!empty($search['start_province'])) {
$where[] = ['=','start_province',$search['start_province']];
}
$search['start_city'] = isset($search['start_city']) ? trim($search['start_city']) : '';
if (!empty($search['start_city'])) {
$where[] = ['LIKE','start_city',$search['start_city']];
}
$search['aim_province'] = isset($search['aim_province']) ? trim($search['aim_province']) : '';
if (!empty($search['aim_province'])) {
$where[] = ['=','aim_province',$search['aim_province']];
}
$search['aim_city'] = isset($search['aim_city']) ? trim($search['aim_city']) : '';
if (!empty($search['aim_city'])) {
$where[] = ['LIKE','aim_city',$search['aim_city']];
}
$search['from_name'] = isset($search['from_name']) ? trim($search['from_name']) : '';
if (!empty($search['from_name'])) {
$where[] = ['LIKE','from_name',$search['from_name']];
}
$search['from_phone'] = isset($search['from_phone']) ? trim($search['from_phone']) : '';
if (!empty($search['from_phone'])) {
$where[] = ['LIKE','from_phone',$search['from_phone']];
}
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']);
}
//计划日期筛选
$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','date',$start,$end];
}
//按指定时段筛选
elseif (!empty($search['sign_after']) && !empty($search['sign_before'])) {
$start = strtotime($search['sign_after']);
$end = strtotime($search['sign_before']);
$where[] = ['between','date',$start,$end];
}
//按指定时间临界点筛选(以不早于为优先筛选条件)
elseif (!empty($search['sign_after'])) {
$start = strtotime($search['sign_after']);
$where[] = ['>=','date',$start];
} elseif (!empty($search['sign_before'])) {
$end = strtotime($search['sign_before']);
$where[] = ['<=','date',$end];
}
if (isset($search['onlyme']) && $search['onlyme']==1) {
$where[] = ['=','create_by',Yii::$app->user->identity->id];
}
$deleted = 0;
if (isset($_GET['deleted'])) {
$deleted = isset($_GET['deleted']) ? ((int)$_GET['deleted']>=0 ? 1 : 0) : 0;
}
$where[]=['=','deleted',$deleted];
$data = $model->find()->where($where);
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '20']);
$_orderby = 'id DESC';
if (isset($search['orderby']) && $search['orderby']=='date') {
$_orderby = 'date DESC,id DESC';
}
$res = $data->offset($pages->offset)->limit($pages->limit)->orderby($_orderby)->all();
$status = Yii::$app->params['OrderPreStatus'];
$sids = $eids = $uids = $mids = [];
if ($res) {
foreach ($res as $r) {
$sids[$r->store_id] = $r->store_id;
$mids[$r->from_mid] = $r->from_mid;
$uids[$r->from_uid] = $r->from_uid;
$eids[$r->employee_id] = $r->employee_id;
}
}
$model = new AcStore();
$where = [];
$where['pid'] = Yii::$app->session->get('pid');
$_stores = $model->find()->where(['in','id',$sids])->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 User();
// $users = [];
// $_users = $model->find()->where(['in','id',$mids])->all();
// foreach($_users as $s){
// $s = $s->toArray();
// $users[$s['id']] = $s;
// }
//
// $model = new AcUserExt();
// $exts = [];
// $_exts = $model->find()->where(['in','mid',$mids])->andwhere(['pid'=>$pid,'deleted'=>0])->indexBy('id')->all();
// foreach($_exts as $ext){
// $ext = $ext->toArray();
// if(isset($users[$s['id']])){
// if($ext['mobile']==$users[$s['id']]['mobile']){
// $exts[$ext['mid']][$ext['key']] = $ext['value'];
// }
// }
// }
$model = new Fans();
$users = [];
$_users = $model->find()->where(['in','id',$uids])->all();
foreach ($_users as $s) {
$s = $s->toArray();
$users[$s['id']] = $s;
}
$model = new AcFansExt();
$exts = [];
$_exts = $model->find()->where(['in','mid',$mids])->andwhere(['pid'=>$pid,'deleted'=>0])->indexBy('id')->all();
foreach ($_exts as $ext) {
$ext = $ext->toArray();
if (isset($users[$s['id']])) {
if ($ext['mobile']==$users[$s['id']]['mobile']) {
$exts[$ext['mid']][$ext['key']] = $ext['value'];
}
}
}
$orders = [];
foreach ($res as $s) {
$s = $s->toArray();
if (isset($exts[$s['from_mid']])) {
foreach ($exts[$s['from_mid']] as $col=>$val) {
$s[$col] = $val;
}
}
$orders[$s['id']] = $s;
}
if (isset($get['export']) && $get['export']=='all') {
$this->export($orders, $stores, $users, $exts, $status);
die;
}
return $this->render('list', [
'orders' => $orders,
'pager' => $pages,
'stores'=>$stores,
'employees'=>$employees,
'exts'=>$exts,
'users'=>$users,
'status'=>$status,
'search'=>$search,
'totalCount'=>$data->count()
]);
}
//导出数据
public function export($orders, $stores, $users, $exts, $status)
{
/* 输入到CSV文件 */
$html = "\xEF\xBB\xBF";//UTF8标记
$html .= "\n";
/* 输出表头 */
$filter = array(
'id' => '序号',
'car_no' => '车牌/架号',
'car_title' => '品牌车型',
'from_name' => '联系人',
'from_mobile' => '联系电话',
'date' => '计划时间',
'start_where' => '始发地',
'end_where' => '目的地',
'store' => '网点',
'employee' => '经办人',
'receptionist' => '接待员',
'bind' => '介绍人',
'car_value' => '车辆保价',
'price' => '报价',
'car' => '客户备注'
);
foreach ($filter as $key => $title) {
$html .= $title . ",\t";
}
$html .= "\n";//换行
$htmldata = "";
foreach ($orders as $vo) {
foreach ($filter as $key => $title) {
switch ($key) {
case 'date':
$htmldata .= date('Y年m月d日', $vo['date']).",\t";
break;
case 'start_where':
$htmldata .= isset(Yii::$app->params['regionAreas'][$vo['start_province']]) ? Yii::$app->params['regionAreas'][$vo['start_province']] : '';
$htmldata .= '-';
$htmldata .= isset($vo['start_city']) ? $vo['start_city'] : '';
$htmldata .= ",\t";
break;
case 'end_where':
$htmldata .= isset(Yii::$app->params['regionAreas'][$vo['aim_province']]) ? Yii::$app->params['regionAreas'][$vo['aim_province']] : '';
$htmldata .= '-';
$htmldata .= isset($vo['aim_city']) ? $vo['aim_city'] : '';
$htmldata .= ",\t";
break;
case 'store':
$htmldata .= isset($stores[$vo['store_id']]['title']) ? $stores[$vo['store_id']]['title'] : '';
$htmldata .= ",\t";
break;
case 'employee':
$htmldata .= isset($employees[$vo['employee_id']]['name']) ? $employees[$vo['employee_id']]['name'] : '';
$htmldata .= ",\t";
break;
case 'car':
$htmldata .= strip_tags(htmlspecialchars_decode($vo['car'])).",\t";
break;
case 'status':
$htmldata .= $status[$vo['status_code']].",\t";
break;
case 'price':
case 'car_value':
$htmldata .= isset($vo[$key]) ? (int)$vo[$key] : 0;
$htmldata .= ",\t";
break;
default:
$htmldata .= isset($vo[$key]) ? $vo[$key] : '';
$htmldata .= ",\t";
break;
}
}
$htmldata .= "\n";
}
$html .=$htmldata;
/* 输出XLS文件 */
header("Content-type:text/xls");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:text/comma-separated-values;charset=UTF-8");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");
header("Content-Transfer-Encoding:binary");
header("Content-Disposition:attachment; filename=预约列表".date('ymd').".xls");
echo $html;
}
//选择匹配
public function actionSelect()
{
global $_HI,$_FM;
$pid = Yii::$app->session->get('pid');
$session = Yii::$app->session;
$model = new AcOrderPre();
$where = [];
$where[]='and';
$where[] = ['=','pid',$pid];
$post = Yii::$app->request->post();
$get = Yii::$app->request->get();
$searchSession = 'orderPre::search';
if (isset($_GET['reset']) && $_GET['reset']==1) {
$post['search'] = [];
}
if (isset($post['search'])) {
$search = $post['search'];
$session->set($searchSession, $search);
} elseif ($session->get($searchSession)) {
$search = $session->get($searchSession);
}
$search['car_no'] = isset($search['car_no']) ? trim($search['car_no']) : '';
if (!empty($search['car_no'])) {
$where[] = ['=','car_no',$search['car_no']];
}
$search['car_title'] = isset($search['car_title']) ? trim($search['car_title']) : '';
if (!empty($search['car_title'])) {
$where[] = ['LIKE','car_no',$search['car_no']];
}
$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 : '';
}
$search['start_province'] = isset($search['start_province']) ? trim($search['start_province']) : '';
if (!empty($search['start_province'])) {
$where[] = ['=','start_province',$search['start_province']];
}
$search['start_city'] = isset($search['start_city']) ? trim($search['start_city']) : '';
if (!empty($search['start_city'])) {
$where[] = ['LIKE','start_city',$search['start_city']];
}
$search['aim_province'] = isset($search['aim_province']) ? trim($search['aim_province']) : '';
if (!empty($search['aim_province'])) {
$where[] = ['=','aim_province',$search['aim_province']];
}
$search['aim_city'] = isset($search['aim_city']) ? trim($search['aim_city']) : '';
if (!empty($search['aim_city'])) {
$where[] = ['LIKE','aim_city',$search['aim_city']];
}
$search['from_name'] = isset($search['from_name']) ? trim($search['from_name']) : '';
if (!empty($search['from_name'])) {
$where[] = ['LIKE','from_name',$search['from_name']];
}
$search['from_phone'] = isset($search['from_phone']) ? trim($search['from_phone']) : '';
if (!empty($search['from_phone'])) {
$where[] = ['LIKE','from_phone',$search['from_phone']];
}
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']);
}
//计划日期筛选
$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','date',$start,$end];
}
//按指定时段筛选
elseif (!empty($search['sign_after']) && !empty($search['sign_before'])) {
$start = strtotime($search['sign_after']);
$end = strtotime($search['sign_before']);
$where[] = ['between','date',$start,$end];
}
//按指定时间临界点筛选(以不早于为优先筛选条件)
elseif (!empty($search['sign_after'])) {
$start = strtotime($search['sign_after']);
$where[] = ['>=','date',$start];
} elseif (!empty($search['sign_before'])) {
$end = strtotime($search['sign_before']);
$where[] = ['<=','date',$end];
}
if (isset($search['onlyme']) && $search['onlyme']==1) {
$where[] = ['=','create_by',Yii::$app->user->identity->id];
}
$deleted = 0;
if (isset($_GET['deleted'])) {
$deleted = isset($_GET['deleted']) ? ((int)$_GET['deleted']>=0 ? 1 : 0) : 0;
}
$where[]=['=','deleted',$deleted];
$data = $model->find()->where($where);
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '20']);
$_orderby = 'id DESC';
if (isset($search['orderby']) && $search['orderby']=='date') {
$_orderby = 'date DESC,id DESC';
}
$res = $data->offset($pages->offset)->limit($pages->limit)->orderby($_orderby)->all();
$status = Yii::$app->params['OrderPreStatus'];
$sids = $eids = $uids = $mids = [];
if ($res) {
foreach ($res as $r) {
$sids[$r->store_id] = $r->store_id;
$mids[$r->from_mid] = $r->from_mid;
$uids[$r->from_uid] = $r->from_uid;
$eids[$r->employee_id] = $r->employee_id;
}
}
$model = new AcStore();
$where = [];
$where['pid'] = Yii::$app->session->get('pid');
$_stores = $model->find()->where(['in','id',$sids])->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',$uids])->all();
foreach ($_users as $s) {
$s = $s->toArray();
$users[$s['id']] = $s;
}
$model = new AcFansExt();
$exts = [];
$_exts = $model->find()->where(['in','mid',$mids])->andwhere(['pid'=>$pid,'deleted'=>0])->indexBy('id')->all();
foreach ($_exts as $ext) {
$ext = $ext->toArray();
if (isset($users[$s['id']])) {
if ($ext['mobile']==$users[$s['id']]['mobile']) {
$exts[$ext['mid']][$ext['key']] = $ext['value'];
}
}
}
$orders = [];
foreach ($res as $s) {
$s = $s->toArray();
if (isset($exts[$s['from_mid']])) {
foreach ($exts[$s['from_mid']] as $col=>$val) {
$s[$col] = $val;
}
}
$orders[$s['id']] = $s;
}
$callback = !empty(Yii::$app->request->get('callback')) ? Yii::$app->request->get('callback') : 'order';
return $this->render('select', [
'orders' => $orders,
'pager' => $pages,
'employees'=>$employees,
'stores'=>$stores,
'exts'=>$exts,
'users'=>$users,
'status'=>$status,
'search'=>$search,
'callback'=>$callback
]);
}
public function actionNew()
{
$status = Yii::$app->params['OrderPreStatus'];
$AcOrder = new AcOrderPre();
$AcOrder->status_code = 0;
$detail = $AcOrder->toArray();
return $this->render('modify', ['status'=>$status,'detail'=>$detail]);
}
public function actionEdit()
{
$pid = Yii::$app->session->get('pid');
$status = Yii::$app->params['OrderPreStatus'];
$post = Yii::$app->request->post();
$id = Yii::$app->request->get('id');
$AcOrderPre = new AcOrderPre();
$order = $AcOrderPre->findOne($id);
$detail = $order->toArray();
$model = new AcStorePre();
$store = $model->findOne($detail['store_id']);
if ($store) {
$detail['store_title'] = $store->title;
}
$model = new AcEmployee();
$employee = $model->findOne($detail['employee_id']);
if ($employee) {
$detail['employee_title'] = $employee->name;
}
$model = new Fans();
$user = $model->findOne($detail['from_uid']);
$model = new AcFansExt();
$exts = [];
$_exts = $model->find()->where(['mid'=>$detail['from_uid'],'pid'=>$pid,'deleted'=>0])->indexBy('id')->all();
foreach ($_exts as $ext) {
$ext = $ext->toArray();
if (isset($users[$s['id']])) {
if ($ext['mobile']==$user->mobile) {
$exts[$ext['mid']][$ext['key']] = $ext['value'];
}
}
}
$op = Yii::$app->request->get('op');
$tpl = 'modify';
if ($op=='print') {
$tpl = 'print';
$sn = '00000000000'; //11位
$sn = substr($sn, 0, 11-strlen($id));
$sn .=$id;
$detail['sn'] = $sn;
}
return $this->render(
$tpl,
[
'status'=>$status,
'detail'=>$detail,
'store'=>$store,
'employee'=>$employee,
'id'=>$id
]
);
}
/*复制订单*/
public function actionCopy()
{
$pid = Yii::$app->session->get('pid');
$status = Yii::$app->params['OrderStatus'];
$post = Yii::$app->request->post();
$id = Yii::$app->request->get('id');
$AcOrder = new AcOrderPre();
$order = $AcOrder->findOne($id);
$detail = $order->toArray();
$detail['remark'] = '';
$detail['car'] = '';
unset($detail['id']);
$model = new AcStore();
$store = $model->findOne($detail['store_id']);
if ($store) {
$detail['store_title'] = $store->title;
}
$model = new AcEmployee();
$employee = $model->findOne($detail['employee_id']);
if ($employee) {
$detail['employee_title'] = $employee->name;
}
$tpl = 'modify';
return $this->render(
$tpl,
[
'status'=>$status,
'detail'=>$detail,
'store'=>$store,
'employee'=>$employee,
'id'=>0
]
);
}
//保存运单信息
public function actionSave()
{
$post = Yii::$app->request->post();
$id = (int)$post['id'];
$pid = Yii::$app->session->get('pid');
$AcOrderPre = new AcOrderPre();
if ($id) {
$order = $AcOrderPre->findOne($id);
}
//格式化数据 为空的项则不修改
//目标键=》POST键
$cols = ['start_province'=>'start_province','start_city'=>'start_city','aim_province'=>'aim_province','aim_city'=>'aim_city','car_title'=>'car_title','car_no'=>'car_no','bind'=>'bind','from_mobile'=>'from_mobile','from_name'=>'from_name','receptionist'=>'receptionist'];
foreach ($cols as $col=>$key) {
if (isset($post[$key]) && $post[$key] !='') {
$data[$col] = trim($post[$key]);
}
}
$cols = ['store_id'=>'store_id','employee_id'=>'employee_id','from_mid'=>'from_mid','car_value'=>'car_value','from_uid'=>'from_uid','from_gender'=>'from_gender'];
foreach ($cols as $col=>$key) {
$data[$col] = 0;
if (isset($post[$key]) && $post[$key] !='') {
$data[$col] = (int)$post[$key];
}
}
$cols = ['remark'=>'remark','car'=>'car'];
foreach ($cols as $col=>$key) {
if (isset($post[$key]) && $post[$key] !='') {
$data[$col] = htmlspecialchars($post[$key]);
}
}
$sign_date = isset($post['date']) ? strtotime($post['date']) : '';
$data['date'] = !$sign_date ? time() : $sign_date;
//部分规则校验
$has_err = true;
$data['create_at'] = time();
$data['update_at'] = $data['create_at'];
$data['pid'] = $pid;
$model = new AcEmployee();
$data['employee_id'] = $data['employee_id'] ?? 0;
$employee = $model->findOne($data['employee_id']);
if ($employee) {
$data['store_id'] = $employee->store_id;
$data['status_code'] = 1; //员工跟进中
$data['receptionist'] = $data['receptionist'] ?? $employee->name;
}
//保存资料
if ($id) {
unset($data['create_at']);
$res = $AcOrderPre->updateAll($data, ['id'=>$id]);
} else {
foreach ($data as $key=>$val) {
$AcOrderPre->$key = $val;
}
$res = $AcOrderPre->save();
$id = $AcOrderPre->attributes['id']; //获取插入后id
}
if ($res) {
if ($has_err) {
\Yii::$app->getSession()->setFlash('success', '订单保存成功,但有部分数据录入错误,请根据情况检查更新!');
} else {
\Yii::$app->getSession()->setFlash('success', '订单保存成功!');
}
return $this->redirect(['edit','id'=>$id]);
} else {
\Yii::$app->getSession()->setFlash('warning', '订单保存失败!');
return $this->goBack();
}
}
public function actionDelete()
{
$get = Yii::$app->request->get();
$post = Yii::$app->request->post();
$id = (int)$get['id'];
$return = [];
$model = new AcOrderPre();
$order = $model->findOne($id);
if (!$order) {
$return['msg'] = '订单数据不存在';
$return['errorcode'] = 404;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
} else {
if ($order->deleted>0) {
$return['msg'] = '订单已被删除了';
$return['errorcode'] = 101;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
if ($order->status_code!=0) {
$return['msg'] = '订单非初始状态,不可删除;如果该订单不正确,可取消操作';
$return['errorcode'] = 102;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
}
// $data = $where = [];
// $where['pid'] = Yii::$app->session->get('pid');
// $where['id'] = $id;
// $data['update_at'] = time();
// $data['deleted'] = 1;
//
// $model->id = $id;
// $res = $model->updateAll($data, $where);
$order->deleted = 1;
$order->update_at = time();
$res = $order->save();
$return = [];
$return['url'] = Url::toRoute('order/list');
$return['timeout'] = 3; //3秒后自动跳转
$return['status'] = 0;
if ($res) {
$return['msg'] = '订单删除成功';
$return['errorcode'] = 200;
$return['data'] = $id;
} else {
$return['msg'] = '订单删除失败';
$return['errorcode'] = 0;
}
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
public function actionAjax()
{
$get = Yii::$app->request->get();
$post = Yii::$app->request->post();
$id = (int)$get['id'];
$data = $where = [];
$where['id'] = $id;
$data['update_at'] = time();
$model = new AcOrderPre();
$res = $model->findOne($id);
$return = [];
$return['timeout'] = 1; //几秒后自动跳转
$return['status'] = 0;
$return['ajax'] = 1;
if (!$res) {
$return['msg'] = '订单数据不存在';
$return['errorcode'] = 404;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
} else {
$order = $res;
if ($order->deleted>0) {
$return['msg'] = '订单已被删除了,无法操作';
$return['errorcode'] = 404;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
}
switch ($get['do']) {
case 'cancel':
if ($order->status_code>1) {
$return['msg'] = '订单已进入处理流程,无法操作取消';
$return['errorcode'] = 403;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
if ($order->status_code==-1) {
$return['msg'] = '订单已经是被取消状态';
$return['errorcode'] = 100;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
$data['status_code'] = -1;
$model->id = $id;
$res = $model->updateAll($data, $where);
if ($res) {
$return['msg'] = '订单取消成功';
$return['errorcode'] = 200;
$return['data'] = ['css'=>'btn-info'];
} else {
$return['msg'] = '订单取消失败';
$return['errorcode'] = 0;
}
break;
default:
break;
}
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
}