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.
 
 
 
 
simple-yewu/frontend/controllers/Gift2Controller.php

591 lines
17 KiB

<?php
namespace frontend\controllers;
use common\libs\MyLib;
use common\models\CarGiftT;
use common\models\EmsT;
use common\models\Gift2T;
use common\models\GiftGroupT;
use common\models\GiftT;
use common\models\GiftTicket2T;
use common\models\GiftTicketT;
use common\models\GiftType2T;
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 Gift2Controller 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 actionGift1Index()
{
$request = Yii::$app->request;
$code = $request->get('code');
$car_no = $request->get('car_no');
$status = $request->get('status',6);
$type_id = $request->get('type_id',0);
$page = $request->get('page',1);
if($page < 1) $page = 1;
$query = GiftTicketT::find()
->orderBy('id DESC');
if($code != '') {
$query = $query->andWhere('code="'.$code.'"');
}
if($car_no != '') {
$query = $query->andWhere('car_no like "'.$car_no.'"');
}
if($status != '' && $status != 6) {
$query = $query->andWhere('status='.$status);
}
if($type_id > 0) {
$query = $query->andWhere('type_id='.$type_id);
}
// 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();
$type_items = GiftType2T::find()->all();
$page_info = MyLib::getPageInfo($pagination);
return $this->render('gift1-index',[
'items' => $items,
'code' => $code,
'car_no' => $car_no,
'page' => $page,
'page_info' => $page_info,
'status' => $status,
'type_items' => $type_items,
'type_id' => $type_id,
]);
}
public function actionGift2Index()
{
return $this->render('gift2-index');
}
public function actionIndex()
{
$request = Yii::$app->request;
$code = $request->get('code');
$car_no = $request->get('car_no');
$page = $request->get('page',1);
if($page < 1) $page = 1;
$query = GiftTicketT::find()
->orderBy('id DESC');
if($code != '') {
$query = $query->andWhere('code="'.$code.'"');
}
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('index',[
'items' => $items,
'code' => $code,
'car_no' => $car_no,
'page' => $page,
'page_info' => $page_info
]);
}
public function actionAdd()
{
$request = Yii::$app->request;
$type_items = GiftType2T::find()->all();
return $this->render('add',[
'type_items' => $type_items,
]);
}
public function actionAddSave()
{
Yii::$app->response->format = Response::FORMAT_JSON;
$request = Yii::$app->request;
$result = array();
$result['success'] = false;
$result['msg'] = '保存失败';
if($request->isPost) {
$min_code = $request->post('min_code');
$max_code = $request->post('max_code');
$type_id = $request->post('type_id');
if($min_code == '') {
$result['msg'] = '请输入开始编号!';
return $result;
}
if($max_code == '') {
$result['msg'] = '请输入结束编号!';
return $result;
}
if($type_id == 0) {
$result['msg'] = '请选择类型!';
return $result;
}
$row = null;
for($i = $min_code; $i <= $max_code; $i++) {
$row = GiftTicketT::findOne(['code'=>$i,'type_id'=>$type_id]);
if(!$row) {
$row = new GiftTicketT();
$row->code = $i;
$row->type_id = $type_id;
$row->input_date = date('Y-m-d H:i:s');
$row->op_id = $this->my->id;
$row->status = 0;
$row->save();
}
}
$result['success'] = true;
$result['msg'] = '保存成功';
}
return $result;
}
public function actionOutput()
{
$request = Yii::$app->request;
$type_items = GiftType2T::find()->all();
$gift_group_items = GiftGroupT::find()->all();
return $this->render('output',[
'type_items' => $type_items,
'gift_group_items' => $gift_group_items,
]);
}
public function actionOutputSave()
{
Yii::$app->response->format = Response::FORMAT_JSON;
$request = Yii::$app->request;
$result = array();
$result['success'] = false;
$result['msg'] = '保存失败';
if($request->isPost) {
$codes = $request->post('codes',array());
$types = $request->post('types',array());
$car_no = $request->post('car_no');
$tran = GiftTicketT::getDb()->beginTransaction();
try {
foreach($types as $index => $type_id) {
if($type_id == 0) continue;
$ticket_info = GiftTicketT::findOne(['code'=>$codes[$index],'type_id'=>$type_id]);
if(!$ticket_info) {
$result['msg'] = '第'.($index+1).'组礼品编号不存在!';
return $result;
}
if($ticket_info->status != 0) {
$result['msg'] = '第'.($index+1).'组礼品编号已经出库!';
return $result;
}
$ticket_info->group_id = 0;
$ticket_info->car_no = $car_no;
$ticket_info->status = 1;
$ticket_info->use_date = date('Y-m-d H:i:s');
$ticket_info->op_id = $this->my->id;
if(!$ticket_info->save()) {
var_dump($ticket_info->errors);
}
}
$tran->commit();
} catch(\Exception $e) {
$tran->rollBack();
throw $e;
}
$result['success'] = true;
$result['msg'] = '保存成功';
}
return $result;
}
public function actionEdit()
{
$request = Yii::$app->request;
$id = $request->get('id',0);
$back_params = $request->get('back_params');
if($id > 0) {
$info = GiftTicketT::findOne(['id'=>$id]);
} else {
$info = new GiftTicketT();
}
$type_items = GiftTypeT::find()->all();
return $this->render('edit',[
'info' => $info,
'type_items' => $type_items,
'back_params' => $back_params,
]);
}
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');
$status = $request->post('status');
$car_no = $request->post('car_no');
$ticket_info = GiftTicketT::findOne(['id'=>$id]);
if(!$ticket_info) {
$result['msg'] = '礼品券不存在!';
return $result;
}
$ticket_info->car_no = $car_no;
$ticket_info->status = $status;
$ticket_info->use_date = date('Y-m-d H:i:s');
$ticket_info->op_id = $this->my->id;
$ticket_info->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 = GiftTicketT::findOne(['id'=>$id]);
if($row->status == 0) {
$row->delete();
$result['success'] = true;
$result['msg'] = '删除成功';
}
}
}
return $result;
}
public function actionTicket2List()
{
$request = Yii::$app->request;
$code = $request->get('code');
$car_no = $request->get('car_no');
$type_id = $request->get('type_id',0);
$page = $request->get('page',1);
if($page < 1) $page = 1;
$query = GiftTicketT::find()
->where('status=1')
->orderBy('id DESC');
if($code != '') {
$query = $query->andWhere('code="'.$code.'"');
}
if($car_no != '') {
$query = $query->andWhere('car_no like "'.$car_no.'"');
}
if($type_id > 0) {
$query = $query->andWhere('type_id='.$type_id);
}
// 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();
$type_items = GiftType2T::find()->all();
$page_info = MyLib::getPageInfo($pagination);
return $this->render('ticket2-list',[
'items' => $items,
'code' => $code,
'car_no' => $car_no,
'page' => $page,
'page_info' => $page_info,
'type_items' => $type_items,
'type_id' => $type_id,
]);
}
public function actionTicket2Output()
{
$request = Yii::$app->request;
$id = $request->get('id',0);
if($id == 0) {
echo '无效礼券';
exit;
}
$ticket_info = GiftTicketT::findOne(['id'=>$id]);
$gift_items = Gift2T::find()->all();
$type_items = GiftType2T::find()->all();
return $this->render('ticket2-output',[
'ticket_info' => $ticket_info,
'gift_items' => $gift_items,
'type_items' => $type_items
]);
}
public function actionTicket2OutputSave()
{
Yii::$app->response->format = Response::FORMAT_JSON;
$request = Yii::$app->request;
$result = array();
$result['success'] = false;
$result['msg'] = '保存失败';
if($request->isPost) {
$ticket_id = $request->post('ticket_id');
$gift_id = (int)$request->post('gift_id');
$send_type = $request->post('send_type');
$send_no = $request->post('send_no');
$ticket_info = GiftTicketT::findOne(['id'=>$ticket_id]);
if(!$ticket_info) {
$result['msg'] = '礼品编号不存在!';
return $result;
}
if($gift_id == 0) {
$result['msg'] = '请选择礼品!';
return $result;
}
$ticket2_info = new GiftTicket2T();
$ticket2_info->ticket_id = $ticket_info->id;
$ticket2_info->gift_id = $gift_id;
$ticket2_info->send_type = $send_type;
$ticket2_info->send_no = $send_no;
$ticket2_info->status = 1;
$ticket2_info->op_id = $this->my->id;
$ticket2_info->use_date = date('Y-m-d H:i:s');
$ticket2_info->save();
$ticket_info->status = 2;
$ticket_info->use_date;
$ticket_info->op_id = $this->my->id;
$ticket_info->save();
//礼品
$gift_info = Gift2T::findOne(['id'=>$gift_id]);
$gift_info->total = $gift_info->total - 1;
$gift_info->save();
$result['success'] = true;
$result['msg'] = '保存成功';
}
return $result;
}
public function actionTicket2Edit()
{
$request = Yii::$app->request;
$id = $request->get('id',0);
if($id > 0) {
$info = GiftTicket2T::findOne(['id'=>$id]);
} else {
$info = new GiftTicket2T();
}
$gift_items = GiftT::find()->all();
$type_items = GiftTypeT::find()->all();
return $this->render('ticket2-edit',[
'info' => $info,
'gift_items' => $gift_items,
'type_items' => $type_items
]);
}
public function actionTicket2Save()
{
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);
$status = (int)$request->post('status');
$row = GiftTicket2T::findOne(['id'=>$id]);
$gift_info = $row->gift;
$gift_info->total = $gift_info->total + 1;
$gift_info->save();
$row->status = 3;
$row->use_date = date('Y-m-d H:i:s');
$row->save();
$result['success'] = true;
$result['msg'] = '保存成功';
}
return $result;
}
public function actionGift2List()
{
$request = Yii::$app->request;
$name = $request->get('name');
$query = Gift2T::find();
if($name != '') {
$query = $query->andWhere(['like','name',$name]);
}
$items = $query->all();
return $this->render('gift2-list',[
'items' => $items,
'name' => $name
]);
}
public function actionGift2Edit()
{
$request = Yii::$app->request;
$id = $request->get('id',0);
if($id > 0) {
$info = Gift2T::findOne(['id'=>$id]);
} else {
$info = new GiftT();
}
return $this->render('gift2-edit',[
'info' => $info
]);
}
public function actionGift2Save()
{
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);
$name = $request->post('name');
$price = $request->post('price');
$remark = $request->post('remark');
$total = (int)$request->post('total');
if($name == '') {
$result['msg'] = '请输入名称!';
return $result;
}
$row = null;
if($id > 0) {
$row = Gift2T::findOne(['id'=>$id]);
} else {
$row = new Gift2T();
}
$row->name = $name;
$row->price = $price;
$row->remark = $remark;
$row->total = $total;
$row->save();
$result['success'] = true;
$result['msg'] = '保存成功';
}
return $result;
}
public function actionGift2Delete()
{
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 = Gift2T::findOne(['id'=>$id]);
$row->delete();
$result['success'] = true;
$result['msg'] = '删除成功';
}
}
return $result;
}
public function actionStastics()
{
$gift_type_items = GiftType2T::find()->all();
$gift_items = Gift2T::find()->all();
return $this->render('stastics',[
'gift_type_items' => $gift_type_items,
'gift_items' => $gift_items,
]);
}
}