You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
510 lines
16 KiB
510 lines
16 KiB
5 years ago
|
<?php
|
||
|
|
||
|
namespace frontend\controllers;
|
||
|
|
||
|
use common\libs\MyLib;
|
||
|
use common\models\CarGiftT;
|
||
|
use common\models\EmsT;
|
||
|
use common\models\GiftT;
|
||
|
use common\models\GiftTypeT;
|
||
|
use common\models\GroupT;
|
||
|
use common\models\InsurerTypeT;
|
||
|
use common\models\OrderT;
|
||
|
use common\models\StrategyT;
|
||
|
use common\models\UserT;
|
||
|
use Yii;
|
||
|
use yii\data\Pagination;
|
||
|
use yii\web\Response;
|
||
|
|
||
|
class GiftController extends \yii\web\Controller
|
||
|
{
|
||
|
public $my = null;
|
||
|
|
||
|
public function init()
|
||
|
{
|
||
|
parent::init();
|
||
|
|
||
|
$cookie = Yii::$app->request->cookies;
|
||
|
$user_id = MyLib::encrypt($cookie->get('aid'),'DECODE');
|
||
|
|
||
|
if($user_id != 0)
|
||
|
{
|
||
|
$this->my = UserT::findOne(['id'=>$user_id]);
|
||
|
} else {
|
||
|
Yii::$app->response->redirect('/common/login')->send();
|
||
|
exit;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function actionIndex()
|
||
|
{
|
||
|
$request = Yii::$app->request;
|
||
|
$name = $request->get('name');
|
||
|
|
||
|
$query = GiftT::find();
|
||
|
if($name != '') {
|
||
|
$query = $query->andWhere(['like','name',$name]);
|
||
|
}
|
||
|
$items = $query->all();
|
||
|
|
||
|
return $this->render('index',[
|
||
|
'items' => $items,
|
||
|
'name' => $name
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
public function actionEdit()
|
||
|
{
|
||
|
$request = Yii::$app->request;
|
||
|
$id = $request->get('id',0);
|
||
|
if($id > 0) {
|
||
|
$info = GiftT::findOne(['id'=>$id]);
|
||
|
} else {
|
||
|
$info = new GiftT();
|
||
|
}
|
||
|
|
||
|
$type_items = GiftTypeT::find()
|
||
|
->all();
|
||
|
|
||
|
return $this->render('edit',[
|
||
|
'info' => $info,
|
||
|
'type_items' => $type_items
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
public function actionSave()
|
||
|
{
|
||
|
Yii::$app->response->format = Response::FORMAT_JSON;
|
||
|
$request = Yii::$app->request;
|
||
|
$result = array();
|
||
|
$result['success'] = false;
|
||
|
$result['msg'] = '保存失败';
|
||
|
|
||
|
if($request->isPost) {
|
||
|
$id = $request->post('id',0);
|
||
|
$type_id = $request->post('type_id');
|
||
|
$name = $request->post('name');
|
||
|
$price = $request->post('price');
|
||
|
$remark = $request->post('remark');
|
||
|
$total = (int)$request->post('total');
|
||
|
$total_lock = (int)$request->post('total_lock');
|
||
|
|
||
|
if($name == '') {
|
||
|
$result['msg'] = '请输入名称!';
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
$row = null;
|
||
|
if($id > 0) {
|
||
|
$row = GiftT::findOne(['id'=>$id]);
|
||
|
} else {
|
||
|
$row = new GiftT();
|
||
|
}
|
||
|
$row->type_id = $type_id;
|
||
|
$row->name = $name;
|
||
|
$row->price = $price;
|
||
|
$row->remark = $remark;
|
||
|
$row->total = $total;
|
||
|
$row->total_lock = $total_lock;
|
||
|
$row->save();
|
||
|
|
||
|
$result['success'] = true;
|
||
|
$result['msg'] = '保存成功';
|
||
|
}
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
public function actionDelete()
|
||
|
{
|
||
|
Yii::$app->response->format = Response::FORMAT_JSON;
|
||
|
$request = Yii::$app->request;
|
||
|
$result = array();
|
||
|
$result['success'] = false;
|
||
|
$result['msg'] = '删除失败';
|
||
|
|
||
|
if($request->isPost) {
|
||
|
$id = $request->post('id',0);
|
||
|
|
||
|
if($id > 0) {
|
||
|
$row = GiftT::findOne(['id'=>$id]);
|
||
|
$row->unlinkAll('strategys',true);
|
||
|
$row->delete();
|
||
|
|
||
|
$result['success'] = true;
|
||
|
$result['msg'] = '删除成功';
|
||
|
}
|
||
|
}
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
public function actionCarAddSave()
|
||
|
{
|
||
|
Yii::$app->response->format = Response::FORMAT_JSON;
|
||
|
$request = Yii::$app->request;
|
||
|
$result = array();
|
||
|
$result['success'] = false;
|
||
|
$result['msg'] = '添加失败';
|
||
|
|
||
|
if($request->isPost) {
|
||
|
$car_id = $request->post('car_id');
|
||
|
$order_id = $request->post('order_id');
|
||
|
$gift_id = $request->post('gift_id');
|
||
|
|
||
|
if($gift_id == 0) {
|
||
|
$result['msg'] = '请选择礼品';
|
||
|
return $result;
|
||
|
}
|
||
|
$gift_info = new CarGiftT();
|
||
|
$gift_info->car_id = $car_id;
|
||
|
$gift_info->order_id = $order_id;
|
||
|
$gift_info->user_id = $this->my->id;
|
||
|
$gift_info->gift_id = $gift_id;
|
||
|
$gift_info->submit_time = time();
|
||
|
$gift_info->status = 0;
|
||
|
$gift_info->type = 2;
|
||
|
$gift_info->save();
|
||
|
|
||
|
$order_info = $gift_info->order;
|
||
|
$order_info->check_gift = 1;
|
||
|
$order_info->save();
|
||
|
|
||
|
$result['success'] = true;
|
||
|
$result['msg'] = '添加成功';
|
||
|
}
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
public function actionCarDeleteSave()
|
||
|
{
|
||
|
Yii::$app->response->format = Response::FORMAT_JSON;
|
||
|
$request = Yii::$app->request;
|
||
|
$result = array();
|
||
|
$result['success'] = false;
|
||
|
$result['msg'] = '删除失败';
|
||
|
|
||
|
if($request->isPost) {
|
||
|
$gift_id = $request->post('gift_id');
|
||
|
$gift_info = CarGiftT::findOne(['id'=>$gift_id]);
|
||
|
if($gift_info->status == 0) {
|
||
|
$order_id = $gift_info->order_id;
|
||
|
$gift_info->delete();
|
||
|
$total = CarGiftT::find()
|
||
|
->where('order_id='.$order_id.' and (status=0 or status=2)')
|
||
|
->count();
|
||
|
if($total > 0) {
|
||
|
$order_info = OrderT::findOne(['id'=>$order_id]);
|
||
|
$order_info->check_gift = 1;
|
||
|
$order_info->save();
|
||
|
}
|
||
|
} else {
|
||
|
$gift_info->status = 2;
|
||
|
$gift_info->save();
|
||
|
$order_info = $gift_info->order;
|
||
|
$order_info->check_gift = 1;
|
||
|
$order_info->save();
|
||
|
}
|
||
|
|
||
|
$result['success'] = true;
|
||
|
$result['msg'] = '删除成功';
|
||
|
}
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
public function actionAjaxCarIndex()
|
||
|
{
|
||
|
Yii::$app->response->format = Response::FORMAT_JSON;
|
||
|
$request = Yii::$app->request;
|
||
|
$car_id = $request->get('car_id');
|
||
|
$type = $request->get('type',0);
|
||
|
|
||
|
$result = array();
|
||
|
$result['success'] = false;
|
||
|
$result['msg'] = '读取失败';
|
||
|
|
||
|
$query = CarGiftT::find()
|
||
|
->where(['car_id'=>$car_id])
|
||
|
// ->andWhere('type=2')
|
||
|
->orderBy('id ASC');
|
||
|
|
||
|
if($type == 0)
|
||
|
$query = $query->andWhere('type=2');
|
||
|
$items = $query->all();
|
||
|
|
||
|
$html = $this->renderPartial('ajax-car-index',[
|
||
|
'car_id' => $car_id,
|
||
|
'items' => $items
|
||
|
]);
|
||
|
$result['success'] = true;
|
||
|
$result['html'] = $html;
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
public function actionAjaxCompanyIndex()
|
||
|
{
|
||
|
Yii::$app->response->format = Response::FORMAT_JSON;
|
||
|
$request = Yii::$app->request;
|
||
|
$car_id = $request->get('car_id');
|
||
|
$order_id = $request->get('order_id');
|
||
|
$total1_clear = $request->get('total1_clear');
|
||
|
$type = $request->get('type',0);
|
||
|
|
||
|
$result = array();
|
||
|
$result['success'] = false;
|
||
|
$result['msg'] = '读取失败';
|
||
|
|
||
|
$order_info = OrderT::findOne(['id'=>$order_id]);
|
||
|
|
||
|
//处理全部范围,没有最低费用限制和最高消费
|
||
|
$query = StrategyT::find()
|
||
|
->where('min_money=0 and group_id=0 and max_money=0')
|
||
|
->orderBy('id ASC');
|
||
|
// echo $query->createCommand()->rawSql."\r\n";
|
||
|
$items = $query->all();
|
||
|
if($total1_clear > 0) {
|
||
|
//处理全部范围,没有最低,只有最高消费
|
||
|
$query = StrategyT::find()
|
||
|
->where('min_money=0 and group_id=0 and max_money>='.$total1_clear)
|
||
|
->orderBy('id ASC');
|
||
|
// echo $query->createCommand()->rawSql."\r\n";
|
||
|
if($query->count() > 0)
|
||
|
$items = array_merge($items,$query->all());
|
||
|
//处理全部范围,有最低,没有最高消费
|
||
|
$query = StrategyT::find()
|
||
|
->where('min_money>0 and min_money<='.$total1_clear.' and group_id=0 and max_money=0')
|
||
|
->orderBy('id ASC');
|
||
|
// echo $query->createCommand()->rawSql."\r\n";
|
||
|
if($query->count() > 0)
|
||
|
$items = array_merge($items,$query->all());
|
||
|
//处理全部范围,有最低,有最高消费
|
||
|
$query = StrategyT::find()
|
||
|
->where('min_money>0 and min_money<='.$total1_clear.' and group_id=0 and max_money>='.$total1_clear)
|
||
|
->orderBy('id ASC');
|
||
|
// echo $query->createCommand()->rawSql."\r\n";
|
||
|
if($query->count())
|
||
|
$items = array_merge($items,$query->all());
|
||
|
}
|
||
|
//处理指定范围,没有最低,没有最高消费
|
||
|
$query = StrategyT::find()
|
||
|
->where('min_money=0 and group_id>0 and max_money=0')
|
||
|
->orderBy('id ASC');
|
||
|
// echo $query->createCommand()->rawSql."\r\n";
|
||
|
$tmp_items = $query->all();
|
||
|
foreach($tmp_items as $item) {
|
||
|
$group_info = GroupT::findOne(['id'=>$item->group_id]);
|
||
|
if($group_info->hasChild($order_info->user->group_id)) {
|
||
|
$items[] = $item;
|
||
|
}
|
||
|
if($group_info->id == $order_info->user->group_id) {
|
||
|
$items[] = $item;
|
||
|
}
|
||
|
}
|
||
|
if($total1_clear > 0) {
|
||
|
//处理指定范围,没有最低,有最高消费
|
||
|
$query = StrategyT::find()
|
||
|
->where('min_money=0 and group_id>0 and max_money>='.$total1_clear)
|
||
|
->orderBy('id ASC');
|
||
|
// echo $query->createCommand()->rawSql."\r\n";
|
||
|
$tmp_items = $query->all();
|
||
|
foreach($tmp_items as $item) {
|
||
|
$group_info = GroupT::findOne(['id'=>$item->group_id]);
|
||
|
if($group_info->hasChild($order_info->user->group_id)) {
|
||
|
$items[] = $item;
|
||
|
}
|
||
|
if($group_info->id == $order_info->user->group_id) {
|
||
|
$items[] = $item;
|
||
|
}
|
||
|
}
|
||
|
//处理指定范围,有最低,没有最高消费
|
||
|
$query = StrategyT::find()
|
||
|
->where('min_money>0 and min_money<='.$total1_clear.' and group_id>0 and max_money=0')
|
||
|
->orderBy('id ASC');
|
||
|
// echo $query->createCommand()->rawSql."\r\n";
|
||
|
$tmp_items = $query->all();
|
||
|
foreach($tmp_items as $item) {
|
||
|
$group_info = GroupT::findOne(['id'=>$item->group_id]);
|
||
|
if($group_info->hasChild($order_info->user->group_id)) {
|
||
|
$items[] = $item;
|
||
|
}
|
||
|
if($group_info->id == $order_info->user->group_id) {
|
||
|
$items[] = $item;
|
||
|
}
|
||
|
}
|
||
|
//处理指定范围,有最低,有最高消费
|
||
|
$query = StrategyT::find()
|
||
|
->where('min_money>0 and min_money<='.$total1_clear.' and group_id>0 and max_money>='.$total1_clear)
|
||
|
->orderBy('id ASC');
|
||
|
// echo $query->createCommand()->rawSql."\r\n";
|
||
|
$tmp_items = $query->all();
|
||
|
foreach($tmp_items as $item) {
|
||
|
$group_info = GroupT::findOne(['id'=>$item->group_id]);
|
||
|
if($group_info->hasChild($order_info->user->group_id)) {
|
||
|
$items[] = $item;
|
||
|
}
|
||
|
if($group_info->id == $order_info->user->group_id) {
|
||
|
$items[] = $item;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//获取已经得到的礼品
|
||
|
$tmp_query = CarGiftT::find()
|
||
|
->where('car_id='.$car_id.' and order_id='.$order_id.' and strategy_id>0');
|
||
|
// echo $tmp_query->createCommand()->rawSql."<br>";
|
||
|
$car_gift_items = $tmp_query->all();
|
||
|
|
||
|
$order_gift_items = array();
|
||
|
foreach($car_gift_items as $item) {
|
||
|
$order_gift_items[] = $item->strategy_id.'-'.$item->gift_id;
|
||
|
}
|
||
|
|
||
|
$html = $this->renderPartial('ajax-company-index',[
|
||
|
'car_id' => $car_id,
|
||
|
'order_id' => $order_id,
|
||
|
'items' => $items,
|
||
|
'order_gift_items' => $order_gift_items
|
||
|
]);
|
||
|
|
||
|
$result['success'] = true;
|
||
|
$result['html'] = $html;
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
public function actionMngList()
|
||
|
{
|
||
|
$request = Yii::$app->request;
|
||
|
$car_man = $request->get('car_man');
|
||
|
$phone = $request->get('phone');
|
||
|
$car_no = $request->get('car_no');
|
||
|
$page = $request->get('page',1);
|
||
|
if($page < 1) $page = 1;
|
||
|
|
||
|
$query = OrderT::find()
|
||
|
->where('status_id>1 and check_gift=1')
|
||
|
->orderBy('submit_date DESC, id DESC');
|
||
|
if($car_man != '') {
|
||
|
$query = $query->andWhere('car_man="'.$car_man.'"');
|
||
|
}
|
||
|
if($phone != '') {
|
||
|
$query = $query->andWhere('link_phone="'.$phone.'"');
|
||
|
}
|
||
|
if($car_no != '') {
|
||
|
$query = $query->andWhere('car_no="'.$car_no.'"');
|
||
|
}
|
||
|
// echo $query->createCommand()->rawSql;
|
||
|
$total = $query->count();
|
||
|
|
||
|
$pagination = new Pagination(['totalCount' => $total,'pageSize'=>20]);
|
||
|
$pagination->setPage($page-1);
|
||
|
|
||
|
$query = $query->offset($pagination->offset)->limit($pagination->limit);
|
||
|
$items = $query->all();
|
||
|
|
||
|
$page_info = MyLib::getPageInfo($pagination);
|
||
|
|
||
|
return $this->render('mng-list',[
|
||
|
'items' => $items,
|
||
|
'car_man' => $car_man,
|
||
|
'phone' => $phone,
|
||
|
'car_no' => $car_no,
|
||
|
'page' => $page,
|
||
|
'page_info' => $page_info
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
public function actionMngListInfo()
|
||
|
{
|
||
|
$request = Yii::$app->request;
|
||
|
$id = $request->get('id',0);
|
||
|
$order_info = OrderT::findOne(['id'=>$id]);
|
||
|
$car_info = $order_info->car;
|
||
|
|
||
|
//快递
|
||
|
$ems_items = EmsT::find()
|
||
|
->where(['car_id'=>$car_info->id,'order_id'=>$order_info->id])
|
||
|
->orderBy('id ASC')
|
||
|
->all();
|
||
|
//礼品
|
||
|
$gift_items = CarGiftT::find()
|
||
|
->where('order_id='.$order_info->id)
|
||
|
->orderBy('strategy_id DESC')
|
||
|
->all();
|
||
|
|
||
|
$insurer_type_items = InsurerTypeT::find()
|
||
|
->all();
|
||
|
|
||
|
return $this->render('mng-list-info',[
|
||
|
'car_info' => $car_info,
|
||
|
'insurer_type_items' => $insurer_type_items,
|
||
|
'order_info' => $order_info,
|
||
|
'ems_items' => $ems_items,
|
||
|
'gift_items' => $gift_items
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
|
||
|
public function actionOkSave()
|
||
|
{
|
||
|
Yii::$app->response->format = Response::FORMAT_JSON;
|
||
|
$request = Yii::$app->request;
|
||
|
$result = array();
|
||
|
$result['success'] = false;
|
||
|
$result['msg'] = '出库失败';
|
||
|
|
||
|
if($request->isPost) {
|
||
|
$gift_ids = $request->post('gift_ids',array());
|
||
|
$order_id = $request->post('order_id');
|
||
|
|
||
|
foreach($gift_ids as $id) {
|
||
|
$item = CarGiftT::findOne(['id'=>$id]);
|
||
|
if($item->status == 0) {
|
||
|
$item->status = 1;
|
||
|
$item->save();
|
||
|
}
|
||
|
}
|
||
|
$total = CarGiftT::find()
|
||
|
->where('order_id='.$order_id.' and status=0')
|
||
|
->count();
|
||
|
if($total == 0) {
|
||
|
$order_info = OrderT::findOne(['id'=>$order_id]);
|
||
|
$order_info->check_gift = 0;
|
||
|
$order_info->save();
|
||
|
}
|
||
|
$result['success'] = true;
|
||
|
$result['msg'] = '出库成功';
|
||
|
}
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
public function actionCancelSave()
|
||
|
{
|
||
|
Yii::$app->response->format = Response::FORMAT_JSON;
|
||
|
$request = Yii::$app->request;
|
||
|
$result = array();
|
||
|
$result['success'] = false;
|
||
|
$result['msg'] = '退货失败';
|
||
|
|
||
|
if($request->isPost) {
|
||
|
$gift_ids = $request->post('gift_ids',array());
|
||
|
$order_id = $request->post('order_id');
|
||
|
|
||
|
foreach($gift_ids as $id) {
|
||
|
$item = CarGiftT::findOne(['id'=>$id]);
|
||
|
if($item->status == 2) {
|
||
|
$item->delete();
|
||
|
}
|
||
|
}
|
||
|
$total = CarGiftT::find()
|
||
|
->where('order_id='.$order_id.' and status=0')
|
||
|
->count();
|
||
|
if($total == 0) {
|
||
|
$order_info = OrderT::findOne(['id'=>$order_id]);
|
||
|
$order_info->check_gift = 0;
|
||
|
$order_info->save();
|
||
|
}
|
||
|
$result['success'] = true;
|
||
|
$result['msg'] = '退货成功';
|
||
|
}
|
||
|
return $result;
|
||
|
}
|
||
|
}
|