|
- <?php
-
- namespace backend\modules\motorcade\controllers;
-
- use backend\modules\api\models\BaseResource;
- use backend\modules\api\models\BusExpenses;
- use backend\modules\api\models\DictType;
- use backend\modules\motorcade\models\BaseBus;
- use backend\modules\motorcade\models\BaseDocument;
- use backend\modules\motorcade\models\BaseDriver;
- use common\components\zPhpExcel;
- use common\models\BaseAreaView;
- use backend\modules\motorcade\models\BusDocLog;
- use Yii;
- use backend\modules\motorcade\models\BusOilDoc;
- use yii\db\Exception;
- use yii\web\NotFoundHttpException;
-
- /**
- * DocController implements the CRUD actions for BusOilDoc model.
- */
- class DocController extends BaseController
- {
- // public $accessControl = true;
- public $layout = "@backend/modules/motorcade/views/layouts/iframe_new";
-
- /**
- * Displays a single BusOilDoc model.
- * @param integer $id
- * @return mixed
- */
- public function actionView($id)
- {
- return $this->render('view', [
- 'model' => $this->findModel($id),
- ]);
- }
-
- public function actionCreateOil()
- {
- return $this->create(1);
- }
-
- public function actionCreateEtc()
- {
- return $this->create(2);
- }
-
- /**
- * Creates a new BusOilDoc model.
- * If creation is successful, the browser will be redirected to the 'view' page.
- * @param $type string 类型
- * @return mixed
- */
- public function create($type)
- {
- $model = new BusOilDoc();
- $login_user = $model->login_user;
- $bus_list = BaseBus::getBusListByOrg($login_user->ORG_ID);
- $area_list = [];
- if ($type == $model::CARD_TYPE_ETC) {
- $area_list = BaseAreaView::getProvince();
- }
- $model->MAIN_CORP_ID = $login_user->MAIN_CORP_ID2;
- $model->ORG_ID = $login_user->ORG_ID;
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
- // Yii::$app->session->setFlash('success', '添加成功!');
- return "<script>pjaxFinish('添加成功');</script>";
- // return $this->redirect(['index', 'type' => $model->CARD_TYPE]);
- } else {
- $model->CARD_STATUS = 0;
- $model->CARD_TYPE = $type;
- return $this->renderPartial('create', [
- 'model' => $model,
- 'data' => [
- 'bus_list' => ['0' => '不绑定'] + $bus_list,
- 'area_list' => $area_list]
- ]);
- }
- }
-
- public function actionModifyMaintain($id, $op)
- {
- $model = BaseDocument::findOne($id);
- $model2 = BaseDocument::findOne(['PID' => $id]);
- $model->setScenario('add_mt');
- $model2->setScenario('add_mt');
- if ($op == 'modify' && $model->load(Yii::$app->request->post()) && $model2->load(Yii::$app->request->post()) && $model->save() && $model2->save()) {
- Yii::$app->session->setFlash('success', '修改成功!');
- return "<script>pjaxFinish('修改成功');</script>";
- } elseif ($op == 'sure') {
- $model2->MT_STATUS = BaseDocument::ZHIXING;
- $model->MT_STATUS = BaseDocument::ZHIXING;
- $model->save();
- $model2->save();
- // Yii::$app->session->setFlash('success', '确认成功!');
- return "<script>pjaxFinish('确认成功');</script>";
- } elseif ($op == 'cancel') {
- $model2->MT_STATUS = BaseDocument::QUXIAO;
- $model->MT_STATUS = BaseDocument::QUXIAO;
- $model->save();
- $model2->save();
- return "<script>pjaxFinish('取消成功');</script>";
- } else {
- $data['view'] = 'add_mt';
- return $this->renderPartial('base', [
- 'data' => $data,
- 'model' => $model
- ]);
- }
-
-
- }
-
- public function actionAddMaintain()
- {
- $model = new BaseDocument(['scenario' => 'add_mt']);
- $request = Yii::$app -> request -> post();
-
- if($request)
- {
- // $request['BaseDocument']['SERVICE_COMPANY'] = BaseDocument::SERVICE_COMPANY[$request['BaseDocument']['SERVICE_COMPANY']];
- if ($model->load($request)) {
- $transaction = Yii::$app->db->beginTransaction();
- try {
- $model2 = new BaseDocument(['scenario' => 'add_mt']);
- $model->MT_STATUS = BaseDocument::QUEREN;
- $model->DOC_TYPE = 1;
- $model->IMG_OPTION = 1;
- // $model->RES_ID = 0;
- $model->save();
- $model2->setAttributes($model->getAttributes());
- $model2->PID = $model->ID;
- $model2->IMG_OPTION = 2;
- $model2->VALUE = '';
- $model2->SEQ_ID = 0;
- // $model2->RES_ID = 0;
- $model2->DOC_STATUS = 1;
- $model2->MT_STATUS = BaseDocument::QUEREN;
- $model2->save();
- $transaction->commit();
- return "<script>pjaxFinish('添加成功');</script>";
- } catch (Exception $exception) {
- $transaction->rollBack();
- throw new \yii\base\Exception('数据库错误', 500);
- }
- }
- }
- else {
- $model->START_DATE = date('Y-m-d');
- $model->DOC_OPTION = BaseDocument::MAINTAIN;
- $data['view'] = 'add_mt';
- return $this->renderPartial('base', [
- 'data' => $data,
- 'model' => $model
- ]);
- }
-
- }
-
- /**
- * Updates an existing BusOilDoc model.
- * If update is successful, the browser will be redirected to the 'view' page.
- * @param integer $id
- * @return mixed
- */
- public function actionUpdate($id)
- {
- $model = $this->findModel($id);
- /* @var $login_user \common\models\User */
- $login_user = Yii::$app->user->identity;
- $bus_list = BaseBus::getBusListByOrg($login_user->ORG_ID);
- if ($model->BIND_BUS > 0 && !isset($bus_list[$model->BIND_BUS])) {
- $bus = BaseBus::findOne([$model->BIND_BUS]);
- if ($bus !== null) {
- $bus_list = [$bus->BUS_ID => $bus->BUS_NO] + $bus_list;
- }
- }
- $area_list = [];
- if ($model->CARD_TYPE == $model::CARD_TYPE_ETC) {
- $area_list = BaseAreaView::getProvince();
- }
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
- Yii::$app->session->setFlash('success', '修改成功!');
- return "<script>pjaxFinish('修改成功');</script>";
- } else {
- return $this->renderPartial('update', [
- 'model' => $model,
- 'data' => [
- 'bus_list' => ['0' => '不绑定'] + $bus_list,
- 'area_list' => $area_list]
- ]);
- }
- }
-
- public function actionBalanceOil($id)
- {
- return $this->balance($id);
- }
-
- public function actionBalanceEtc($id)
- {
- return $this->balance($id);
- }
-
- /**
- * User: wangxj
- *
- * 修改余额
- *
- * @param $id
- * @throws
- *
- * @return string
- */
- public function balance($id)
- {
- $model = $this->findModel($id);
-
- // $select_list = []
- $log = new BusDocLog(['scenario' => 'balance']);
- if ($log->load(Yii::$app->request->post()) && $log->validate()) {
- $amount = $log->OPERATION_TYPE == BusDocLog::OPERATION_MINUS ? -$log->VALUE : $log->VALUE;
- $transaction = Yii::$app->db->beginTransaction();
- try {
- $log->OPERATION_DATE = date('Y-m-d');
- $log->OPERATION_USER = Yii::$app->user->id;
- if ($log->save()) {
- $model->CARD_BALANCE += $amount; // $log->OPERATION_TYPE == BusDocLog::OPERATION_MINUS?($model->CARD_BALANCE - $log->VALUE): ($model->CARD_BALANCE + $log->VALUE);
-
- if ($model->save())
- $transaction->commit();
- }
- // Yii::$app->session->setFlash('success', '修改成功!');
- return "<script>pjaxFinish('修改成功');</script>";
- } catch (Exception $exception) {
- $transaction->rollBack();
- throw new \yii\base\Exception('数据库错误', 500);
- }
- } else {
- $log->VALUE_OLD = $model->CARD_BALANCE;
- $log->CARD_ID = $model->ID;
- return $this->renderPartial('_balance', [
- 'model' => $model,
- 'data' => [
- 'log' => $log
- ]
- ]);
- }
- }
-
- public function actionChargeOil($id_str)
- {
- return $this->charge('油卡', $id_str);
- }
-
- public function actionChargeEtc($id_str)
- {
- return $this->charge('ETC', $id_str);
- }
-
- /**
- * User: wangxj
- *
- * 充值
- *
- * @param $type
- * @param $id_str
- *
- * @return string
- * @throws
- */
- public function charge($type, $id_str)
- {
- $id_array = explode(',', $id_str);
- if (empty($id_array)) {
- throw new \yii\base\Exception('数据有误', 400);
- }
-
- $log = new BusDocLog(['scenario' => 'charge']);
- if ($log->load(Yii::$app->request->post())) {
- $log->OPERATION_USER = Yii::$app->user->id;
- $transaction = Yii::$app->db->beginTransaction();
- try {
- foreach ($id_array as $item) {
- $model = BusOilDoc::findOne($item);
- if ($model != null) {
- $tmp_log = new BusDocLog(['scenario' => 'charge']);
- $tmp_log->setAttributes($log->getAttributes());
- $tmp_log->VALUE_OLD = $model->CARD_BALANCE;
- $tmp_log->OPERATION_TYPE = BusDocLog::OPERATION_TYPE_CHARGE;
- $tmp_log->CARD_ID = (string)$model->ID;
- $tmp_log->save();
- $model->CARD_BALANCE += $log->VALUE; // $log->OPERATION_TYPE == BusDocLog::OPERATION_MINUS?($model->CARD_BALANCE - $log->VALUE): ($model->CARD_BALANCE + $log->VALUE);
- $model->save();
- }
- }
- $transaction->commit();
- Yii::$app->session->setFlash('success', '充值成功!');
- return "<script>pjaxFinish('修改成功');</script>";
- } catch (Exception $exception) {
- $transaction->rollBack();
- throw new \yii\base\Exception('数据库错误', 500);
- }
- } else {
- $log->CARD_ID = $id_str;
- $log->OPERATION_DATE = date('Y-m-d');
- return $this->renderPartial('_charge', [
- 'data' => [
- 'type' => $type,
- 'log' => $log
- ]
- ]);
- }
- }
-
- /**
- * Deletes an existing BusOilDoc model.
- * If deletion is successful, the browser will be redirected to the 'index' page.
- * @param integer $id
- * @return mixed
- */
- public function actionDelete($id)
- {
- $this->findModel($id)->delete();
-
- return $this->redirect(['index']);
- }
-
- /**
- * User: wangxj
- *
- * 启用或停用油卡或ETC
- *
- * @param $id
- * @return string;
- */
- public function actionDisable($id)
- {
- $model = $this->findModel($id);
- if ($model->CARD_STATUS == 1) {
- $model->CARD_STATUS = 0;
- $model->save(false);
- Yii::$app->session->setFlash('success', '启用成功');
- return "<script>pjaxFinish('启用成功');</script>";
- } else {
- $model->CARD_STATUS = 1;
- $model->save(false);
- Yii::$app->session->setFlash('success', '停用成功');
- return "<script>pjaxFinish('停用成功');</script>";
- }
- // return $this->redirect(['index', 'type' => $model->CARD_TYPE]);
-
-
- }
-
- /**
- * Finds the BusOilDoc model based on its primary key value.
- * If the model is not found, a 404 HTTP exception will be thrown.
- * @param integer $id
- * @return BusOilDoc the loaded model
- * @throws NotFoundHttpException if the model cannot be found
- */
- protected function findModel($id)
- {
- if (($model = BusOilDoc::findOne($id)) !== null) {
- $model->hasOne(BaseBus::className(), ['BUS_ID' => 'BIND_BUS']);
- return $model;
- } else {
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
-
- public function actionEtc()
- {
- return $this->index(2);
- }
-
- public function actionOil()
- {
- return $this->index(1);
- }
-
- /**
- * type为1则为油卡管理 ,2为ETC管理
- * @param $type integer
- * @return string
- */
- public function index($type)
- {
- $obj = new BusOilDoc();
- if (Yii::$app->request->isPost) {
- $data = Yii::$app->request->post('BusOilDoc');
- $data['type'] = $type;
- $obj->load($data);
- } elseif (Yii::$app->request->isGet) {
- $data = Yii::$app->request->get('BusOilDoc');
- $data['type'] = $type;
- $obj->load($data);
- }
- $dataProvider = $obj->getList();
- $data['view'] = 'list';
- return $this->render('base', [
- 'data' => $data,
- 'dataProvider' => $dataProvider
- ]);
- }
-
- public function actionDetail()
- {
- $obj = new BusDocLog();
- //只有详情页日期需要特殊处理为 前30天至今天
- if (!isset($_REQUEST['start_date'])) {
- $_GET['end_date'] = date('Y-m-d');
- $_GET['start_date'] = date("Y-m-d", strtotime("-30 day"));
- }
- $obj->load(Yii::$app->request->get());
- $dataProvider = $obj->getDetail();
- $model = BusOilDoc::find()
- ->joinWith(BaseBus::tableName())
- ->joinWith(BaseResource::tableName())
- ->joinWith(BusDocLog::tableName())
- ->andFilterWhere(['bus_oil_doc.ID' => Yii::$app->request->get('id')])
- ->one();
- $data['view'] = 'detail';
- return $this->render('base', [
- 'model' => $model,
- 'dataProvider' => $dataProvider,
- 'data' => $data,
- ]);
- }
-
- public function actionOilHistory()
- {
- return $this->record(1, 1);
- }
-
- public function actionEtcHistory()
- {
- return $this->record(1, 2);
- }
-
- public function actionRecordOil()
- {
- return $this->record(2, 1);
- }
-
- public function actionRecordEtc()
- {
- return $this->record(2, 2);
- }
-
- public function record($cost_type, $record_type)
- {
- $data = Yii::$app->request->get('BusOilDoc');
- if ($cost_type == 1) {
- $obj = new BusDocLog();
- $obj->load(Yii::$app->request->get());
- $obj->cost_type = $cost_type;
- $obj->record_type = $record_type;
- $dataProvider = $obj->getDetail();
- } elseif ($cost_type == 2) {
- if ($record_type == 1) {
- $obj = new BusExpenses();
- $obj->load(Yii::$app->request->get());
- $obj->cost_type = $cost_type;
- $obj->record_type = $record_type;
- $dataProvider = $obj->getList();
- } elseif ($record_type == 2) {
- $obj = new BusDocLog();
- $obj->load(Yii::$app->request->get());
- $obj->cost_type = $cost_type;
- $obj->record_type = $record_type;
- $dataProvider = $obj->getCost();
- }
- }
-
- $data['view'] = 'record';
- $data['type'] = $record_type;
- $data['cost_type'] = $cost_type;
-
- return $this->render('base', [
- 'data' => $data,
- 'dataProvider' => $dataProvider
- ]);
- }
-
- public function actionMaintainList()
- {
- $obj = new BaseDocument();
- $obj->load(Yii::$app->request->get());
- $dataProvider = $obj->getMaintainList();
- $data['view'] = '_maintain_list';
- return $this->render('base', [
- 'data' => $data,
- 'dataProvider' => $dataProvider
- ]);
- }
-
- public function actionExport()
- {
- $search = Yii::$app->request->post();
- $param = $search['BusOilDoc'];
- $param['bus_id'] = $search['bus_id'];
- $obj = new BaseDocument();
- $obj->load($param, '');
- $dataProvider = $obj->getMaintainList();
- $dataProvider->pagination = false;
- $data = $dataProvider->getModels();
-
- ini_set("memory_limit", "-1");
- $objPHPExcel = new zPhpExcel(); //实例化PHPExcel类
- $objSheet = $objPHPExcel->getActiveSheet(); //获取当前活动sheet的操作对象
- $objSheet->setTitle('维保管理列表');
- $objPHPExcel->file_name = date('Y-m-d');
- // 定义要需要导出的字段
- $order_sort = ['IN_ID', 'BUS_NO', 'TYPE', 'START_DATE', 'DRIVER_NAME', 'SERVICE_COMPANY', 'VALUE', 'STATUS'];
-
- //设置表头
- $a_k = range('A', 'H');
- foreach ($a_k as $key => $item) {
- $name = $obj->getAttributeLabel($order_sort[$key]);
- $objSheet->setCellValue($item . '1', $obj->getAttributeLabel($order_sort[$key]));
- }
-
- foreach ($data as $key => $datum) {
- foreach ($a_k as $index => $item) {
- if ($item == 'A')
- $objSheet->setCellValue($item . ($key + 2), $key + 1);
- elseif($item == 'B')
- $objSheet->setCellValue($item . ($key + 2), $datum-> bus -> $order_sort[$index]);
- elseif($item == 'C')
- {
- $name = DictType::findOne($datum -> DOC_OPTION)['TYPE_NAME'];
- $objSheet->setCellValue($item . ($key + 2), $name);
- }elseif ($item == 'E')
- {
- $driver_name = BaseDriver::findOne($datum -> NAME)['DRIVER_NAME'];
- $objSheet->setCellValue($item . ($key + 2), $driver_name);
- }elseif($item == 'H')
- {
- $status_name = DictType::findOne($datum -> MT_STATUS)['TYPE_NAME'];
- $objSheet->setCellValue($item . ($key + 2), $status_name);
- }
- else
- $objSheet->setCellValue($item . ($key + 2), $datum-> $order_sort[$index]);
- }
- }
-
- //设置宽度
- $objPHPExcel->setColumnSize(range('B', 'H'), [20, 10, 20, 10, 30, 10, 15]);
- $objPHPExcel->output();
- }
-
-
- }
|