25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

557 satır
18 KiB

  1. <?php
  2. namespace backend\modules\motorcade\controllers;
  3. use backend\modules\api\models\BaseResource;
  4. use backend\modules\api\models\BusExpenses;
  5. use backend\modules\api\models\DictType;
  6. use backend\modules\motorcade\models\BaseBus;
  7. use backend\modules\motorcade\models\BaseDocument;
  8. use backend\modules\motorcade\models\BaseDriver;
  9. use common\components\zPhpExcel;
  10. use common\models\BaseAreaView;
  11. use backend\modules\motorcade\models\BusDocLog;
  12. use Yii;
  13. use backend\modules\motorcade\models\BusOilDoc;
  14. use yii\db\Exception;
  15. use yii\web\NotFoundHttpException;
  16. /**
  17. * DocController implements the CRUD actions for BusOilDoc model.
  18. */
  19. class DocController extends BaseController
  20. {
  21. // public $accessControl = true;
  22. public $layout = "@backend/modules/motorcade/views/layouts/iframe_new";
  23. /**
  24. * Displays a single BusOilDoc model.
  25. * @param integer $id
  26. * @return mixed
  27. */
  28. public function actionView($id)
  29. {
  30. return $this->render('view', [
  31. 'model' => $this->findModel($id),
  32. ]);
  33. }
  34. public function actionCreateOil()
  35. {
  36. return $this->create(1);
  37. }
  38. public function actionCreateEtc()
  39. {
  40. return $this->create(2);
  41. }
  42. /**
  43. * Creates a new BusOilDoc model.
  44. * If creation is successful, the browser will be redirected to the 'view' page.
  45. * @param $type string 类型
  46. * @return mixed
  47. */
  48. public function create($type)
  49. {
  50. $model = new BusOilDoc();
  51. $login_user = $model->login_user;
  52. $bus_list = BaseBus::getBusListByOrg($login_user->ORG_ID);
  53. $area_list = [];
  54. if ($type == $model::CARD_TYPE_ETC) {
  55. $area_list = BaseAreaView::getProvince();
  56. }
  57. $model->MAIN_CORP_ID = $login_user->MAIN_CORP_ID2;
  58. $model->ORG_ID = $login_user->ORG_ID;
  59. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  60. // Yii::$app->session->setFlash('success', '添加成功!');
  61. return "<script>pjaxFinish('添加成功');</script>";
  62. // return $this->redirect(['index', 'type' => $model->CARD_TYPE]);
  63. } else {
  64. $model->CARD_STATUS = 0;
  65. $model->CARD_TYPE = $type;
  66. return $this->renderPartial('create', [
  67. 'model' => $model,
  68. 'data' => [
  69. 'bus_list' => ['0' => '不绑定'] + $bus_list,
  70. 'area_list' => $area_list]
  71. ]);
  72. }
  73. }
  74. public function actionModifyMaintain($id, $op)
  75. {
  76. $model = BaseDocument::findOne($id);
  77. $model2 = BaseDocument::findOne(['PID' => $id]);
  78. $model->setScenario('add_mt');
  79. $model2->setScenario('add_mt');
  80. if ($op == 'modify' && $model->load(Yii::$app->request->post()) && $model2->load(Yii::$app->request->post()) && $model->save() && $model2->save()) {
  81. Yii::$app->session->setFlash('success', '修改成功!');
  82. return "<script>pjaxFinish('修改成功');</script>";
  83. } elseif ($op == 'sure') {
  84. $model2->MT_STATUS = BaseDocument::ZHIXING;
  85. $model->MT_STATUS = BaseDocument::ZHIXING;
  86. $model->save();
  87. $model2->save();
  88. // Yii::$app->session->setFlash('success', '确认成功!');
  89. return "<script>pjaxFinish('确认成功');</script>";
  90. } elseif ($op == 'cancel') {
  91. $model2->MT_STATUS = BaseDocument::QUXIAO;
  92. $model->MT_STATUS = BaseDocument::QUXIAO;
  93. $model->save();
  94. $model2->save();
  95. return "<script>pjaxFinish('取消成功');</script>";
  96. } else {
  97. $data['view'] = 'add_mt';
  98. return $this->renderPartial('base', [
  99. 'data' => $data,
  100. 'model' => $model
  101. ]);
  102. }
  103. }
  104. public function actionAddMaintain()
  105. {
  106. $model = new BaseDocument(['scenario' => 'add_mt']);
  107. $request = Yii::$app -> request -> post();
  108. if($request)
  109. {
  110. // $request['BaseDocument']['SERVICE_COMPANY'] = BaseDocument::SERVICE_COMPANY[$request['BaseDocument']['SERVICE_COMPANY']];
  111. if ($model->load($request)) {
  112. $transaction = Yii::$app->db->beginTransaction();
  113. try {
  114. $model2 = new BaseDocument(['scenario' => 'add_mt']);
  115. $model->MT_STATUS = BaseDocument::QUEREN;
  116. $model->DOC_TYPE = 1;
  117. $model->IMG_OPTION = 1;
  118. // $model->RES_ID = 0;
  119. $model->save();
  120. $model2->setAttributes($model->getAttributes());
  121. $model2->PID = $model->ID;
  122. $model2->IMG_OPTION = 2;
  123. $model2->VALUE = '';
  124. $model2->SEQ_ID = 0;
  125. // $model2->RES_ID = 0;
  126. $model2->DOC_STATUS = 1;
  127. $model2->MT_STATUS = BaseDocument::QUEREN;
  128. $model2->save();
  129. $transaction->commit();
  130. return "<script>pjaxFinish('添加成功');</script>";
  131. } catch (Exception $exception) {
  132. $transaction->rollBack();
  133. throw new \yii\base\Exception('数据库错误', 500);
  134. }
  135. }
  136. }
  137. else {
  138. $model->START_DATE = date('Y-m-d');
  139. $model->DOC_OPTION = BaseDocument::MAINTAIN;
  140. $data['view'] = 'add_mt';
  141. return $this->renderPartial('base', [
  142. 'data' => $data,
  143. 'model' => $model
  144. ]);
  145. }
  146. }
  147. /**
  148. * Updates an existing BusOilDoc model.
  149. * If update is successful, the browser will be redirected to the 'view' page.
  150. * @param integer $id
  151. * @return mixed
  152. */
  153. public function actionUpdate($id)
  154. {
  155. $model = $this->findModel($id);
  156. /* @var $login_user \common\models\User */
  157. $login_user = Yii::$app->user->identity;
  158. $bus_list = BaseBus::getBusListByOrg($login_user->ORG_ID);
  159. if ($model->BIND_BUS > 0 && !isset($bus_list[$model->BIND_BUS])) {
  160. $bus = BaseBus::findOne([$model->BIND_BUS]);
  161. if ($bus !== null) {
  162. $bus_list = [$bus->BUS_ID => $bus->BUS_NO] + $bus_list;
  163. }
  164. }
  165. $area_list = [];
  166. if ($model->CARD_TYPE == $model::CARD_TYPE_ETC) {
  167. $area_list = BaseAreaView::getProvince();
  168. }
  169. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  170. Yii::$app->session->setFlash('success', '修改成功!');
  171. return "<script>pjaxFinish('修改成功');</script>";
  172. } else {
  173. return $this->renderPartial('update', [
  174. 'model' => $model,
  175. 'data' => [
  176. 'bus_list' => ['0' => '不绑定'] + $bus_list,
  177. 'area_list' => $area_list]
  178. ]);
  179. }
  180. }
  181. public function actionBalanceOil($id)
  182. {
  183. return $this->balance($id);
  184. }
  185. public function actionBalanceEtc($id)
  186. {
  187. return $this->balance($id);
  188. }
  189. /**
  190. * User: wangxj
  191. *
  192. * 修改余额
  193. *
  194. * @param $id
  195. * @throws
  196. *
  197. * @return string
  198. */
  199. public function balance($id)
  200. {
  201. $model = $this->findModel($id);
  202. // $select_list = []
  203. $log = new BusDocLog(['scenario' => 'balance']);
  204. if ($log->load(Yii::$app->request->post()) && $log->validate()) {
  205. $amount = $log->OPERATION_TYPE == BusDocLog::OPERATION_MINUS ? -$log->VALUE : $log->VALUE;
  206. $transaction = Yii::$app->db->beginTransaction();
  207. try {
  208. $log->OPERATION_DATE = date('Y-m-d');
  209. $log->OPERATION_USER = Yii::$app->user->id;
  210. if ($log->save()) {
  211. $model->CARD_BALANCE += $amount; // $log->OPERATION_TYPE == BusDocLog::OPERATION_MINUS?($model->CARD_BALANCE - $log->VALUE): ($model->CARD_BALANCE + $log->VALUE);
  212. if ($model->save())
  213. $transaction->commit();
  214. }
  215. // Yii::$app->session->setFlash('success', '修改成功!');
  216. return "<script>pjaxFinish('修改成功');</script>";
  217. } catch (Exception $exception) {
  218. $transaction->rollBack();
  219. throw new \yii\base\Exception('数据库错误', 500);
  220. }
  221. } else {
  222. $log->VALUE_OLD = $model->CARD_BALANCE;
  223. $log->CARD_ID = $model->ID;
  224. return $this->renderPartial('_balance', [
  225. 'model' => $model,
  226. 'data' => [
  227. 'log' => $log
  228. ]
  229. ]);
  230. }
  231. }
  232. public function actionChargeOil($id_str)
  233. {
  234. return $this->charge('油卡', $id_str);
  235. }
  236. public function actionChargeEtc($id_str)
  237. {
  238. return $this->charge('ETC', $id_str);
  239. }
  240. /**
  241. * User: wangxj
  242. *
  243. * 充值
  244. *
  245. * @param $type
  246. * @param $id_str
  247. *
  248. * @return string
  249. * @throws
  250. */
  251. public function charge($type, $id_str)
  252. {
  253. $id_array = explode(',', $id_str);
  254. if (empty($id_array)) {
  255. throw new \yii\base\Exception('数据有误', 400);
  256. }
  257. $log = new BusDocLog(['scenario' => 'charge']);
  258. if ($log->load(Yii::$app->request->post())) {
  259. $log->OPERATION_USER = Yii::$app->user->id;
  260. $transaction = Yii::$app->db->beginTransaction();
  261. try {
  262. foreach ($id_array as $item) {
  263. $model = BusOilDoc::findOne($item);
  264. if ($model != null) {
  265. $tmp_log = new BusDocLog(['scenario' => 'charge']);
  266. $tmp_log->setAttributes($log->getAttributes());
  267. $tmp_log->VALUE_OLD = $model->CARD_BALANCE;
  268. $tmp_log->OPERATION_TYPE = BusDocLog::OPERATION_TYPE_CHARGE;
  269. $tmp_log->CARD_ID = (string)$model->ID;
  270. $tmp_log->save();
  271. $model->CARD_BALANCE += $log->VALUE; // $log->OPERATION_TYPE == BusDocLog::OPERATION_MINUS?($model->CARD_BALANCE - $log->VALUE): ($model->CARD_BALANCE + $log->VALUE);
  272. $model->save();
  273. }
  274. }
  275. $transaction->commit();
  276. Yii::$app->session->setFlash('success', '充值成功!');
  277. return "<script>pjaxFinish('修改成功');</script>";
  278. } catch (Exception $exception) {
  279. $transaction->rollBack();
  280. throw new \yii\base\Exception('数据库错误', 500);
  281. }
  282. } else {
  283. $log->CARD_ID = $id_str;
  284. $log->OPERATION_DATE = date('Y-m-d');
  285. return $this->renderPartial('_charge', [
  286. 'data' => [
  287. 'type' => $type,
  288. 'log' => $log
  289. ]
  290. ]);
  291. }
  292. }
  293. /**
  294. * Deletes an existing BusOilDoc model.
  295. * If deletion is successful, the browser will be redirected to the 'index' page.
  296. * @param integer $id
  297. * @return mixed
  298. */
  299. public function actionDelete($id)
  300. {
  301. $this->findModel($id)->delete();
  302. return $this->redirect(['index']);
  303. }
  304. /**
  305. * User: wangxj
  306. *
  307. * 启用或停用油卡或ETC
  308. *
  309. * @param $id
  310. * @return string;
  311. */
  312. public function actionDisable($id)
  313. {
  314. $model = $this->findModel($id);
  315. if ($model->CARD_STATUS == 1) {
  316. $model->CARD_STATUS = 0;
  317. $model->save(false);
  318. Yii::$app->session->setFlash('success', '启用成功');
  319. return "<script>pjaxFinish('启用成功');</script>";
  320. } else {
  321. $model->CARD_STATUS = 1;
  322. $model->save(false);
  323. Yii::$app->session->setFlash('success', '停用成功');
  324. return "<script>pjaxFinish('停用成功');</script>";
  325. }
  326. // return $this->redirect(['index', 'type' => $model->CARD_TYPE]);
  327. }
  328. /**
  329. * Finds the BusOilDoc model based on its primary key value.
  330. * If the model is not found, a 404 HTTP exception will be thrown.
  331. * @param integer $id
  332. * @return BusOilDoc the loaded model
  333. * @throws NotFoundHttpException if the model cannot be found
  334. */
  335. protected function findModel($id)
  336. {
  337. if (($model = BusOilDoc::findOne($id)) !== null) {
  338. $model->hasOne(BaseBus::className(), ['BUS_ID' => 'BIND_BUS']);
  339. return $model;
  340. } else {
  341. throw new NotFoundHttpException('The requested page does not exist.');
  342. }
  343. }
  344. public function actionEtc()
  345. {
  346. return $this->index(2);
  347. }
  348. public function actionOil()
  349. {
  350. return $this->index(1);
  351. }
  352. /**
  353. * type为1则为油卡管理 ,2为ETC管理
  354. * @param $type integer
  355. * @return string
  356. */
  357. public function index($type)
  358. {
  359. $obj = new BusOilDoc();
  360. if (Yii::$app->request->isPost) {
  361. $data = Yii::$app->request->post('BusOilDoc');
  362. $data['type'] = $type;
  363. $obj->load($data);
  364. } elseif (Yii::$app->request->isGet) {
  365. $data = Yii::$app->request->get('BusOilDoc');
  366. $data['type'] = $type;
  367. $obj->load($data);
  368. }
  369. $dataProvider = $obj->getList();
  370. $data['view'] = 'list';
  371. return $this->render('base', [
  372. 'data' => $data,
  373. 'dataProvider' => $dataProvider
  374. ]);
  375. }
  376. public function actionDetail()
  377. {
  378. $obj = new BusDocLog();
  379. //只有详情页日期需要特殊处理为 前30天至今天
  380. if (!isset($_REQUEST['start_date'])) {
  381. $_GET['end_date'] = date('Y-m-d');
  382. $_GET['start_date'] = date("Y-m-d", strtotime("-30 day"));
  383. }
  384. $obj->load(Yii::$app->request->get());
  385. $dataProvider = $obj->getDetail();
  386. $model = BusOilDoc::find()
  387. ->joinWith(BaseBus::tableName())
  388. ->joinWith(BaseResource::tableName())
  389. ->joinWith(BusDocLog::tableName())
  390. ->andFilterWhere(['bus_oil_doc.ID' => Yii::$app->request->get('id')])
  391. ->one();
  392. $data['view'] = 'detail';
  393. return $this->render('base', [
  394. 'model' => $model,
  395. 'dataProvider' => $dataProvider,
  396. 'data' => $data,
  397. ]);
  398. }
  399. public function actionOilHistory()
  400. {
  401. return $this->record(1, 1);
  402. }
  403. public function actionEtcHistory()
  404. {
  405. return $this->record(1, 2);
  406. }
  407. public function actionRecordOil()
  408. {
  409. return $this->record(2, 1);
  410. }
  411. public function actionRecordEtc()
  412. {
  413. return $this->record(2, 2);
  414. }
  415. public function record($cost_type, $record_type)
  416. {
  417. $data = Yii::$app->request->get('BusOilDoc');
  418. if ($cost_type == 1) {
  419. $obj = new BusDocLog();
  420. $obj->load(Yii::$app->request->get());
  421. $obj->cost_type = $cost_type;
  422. $obj->record_type = $record_type;
  423. $dataProvider = $obj->getDetail();
  424. } elseif ($cost_type == 2) {
  425. if ($record_type == 1) {
  426. $obj = new BusExpenses();
  427. $obj->load(Yii::$app->request->get());
  428. $obj->cost_type = $cost_type;
  429. $obj->record_type = $record_type;
  430. $dataProvider = $obj->getList();
  431. } elseif ($record_type == 2) {
  432. $obj = new BusDocLog();
  433. $obj->load(Yii::$app->request->get());
  434. $obj->cost_type = $cost_type;
  435. $obj->record_type = $record_type;
  436. $dataProvider = $obj->getCost();
  437. }
  438. }
  439. $data['view'] = 'record';
  440. $data['type'] = $record_type;
  441. $data['cost_type'] = $cost_type;
  442. return $this->render('base', [
  443. 'data' => $data,
  444. 'dataProvider' => $dataProvider
  445. ]);
  446. }
  447. public function actionMaintainList()
  448. {
  449. $obj = new BaseDocument();
  450. $obj->load(Yii::$app->request->get());
  451. $dataProvider = $obj->getMaintainList();
  452. $data['view'] = '_maintain_list';
  453. return $this->render('base', [
  454. 'data' => $data,
  455. 'dataProvider' => $dataProvider
  456. ]);
  457. }
  458. public function actionExport()
  459. {
  460. $search = Yii::$app->request->post();
  461. $param = $search['BusOilDoc'];
  462. $param['bus_id'] = $search['bus_id'];
  463. $obj = new BaseDocument();
  464. $obj->load($param, '');
  465. $dataProvider = $obj->getMaintainList();
  466. $dataProvider->pagination = false;
  467. $data = $dataProvider->getModels();
  468. ini_set("memory_limit", "-1");
  469. $objPHPExcel = new zPhpExcel(); //实例化PHPExcel类
  470. $objSheet = $objPHPExcel->getActiveSheet(); //获取当前活动sheet的操作对象
  471. $objSheet->setTitle('维保管理列表');
  472. $objPHPExcel->file_name = date('Y-m-d');
  473. // 定义要需要导出的字段
  474. $order_sort = ['IN_ID', 'BUS_NO', 'TYPE', 'START_DATE', 'DRIVER_NAME', 'SERVICE_COMPANY', 'VALUE', 'STATUS'];
  475. //设置表头
  476. $a_k = range('A', 'H');
  477. foreach ($a_k as $key => $item) {
  478. $name = $obj->getAttributeLabel($order_sort[$key]);
  479. $objSheet->setCellValue($item . '1', $obj->getAttributeLabel($order_sort[$key]));
  480. }
  481. foreach ($data as $key => $datum) {
  482. foreach ($a_k as $index => $item) {
  483. if ($item == 'A')
  484. $objSheet->setCellValue($item . ($key + 2), $key + 1);
  485. elseif($item == 'B')
  486. $objSheet->setCellValue($item . ($key + 2), $datum-> bus -> $order_sort[$index]);
  487. elseif($item == 'C')
  488. {
  489. $name = DictType::findOne($datum -> DOC_OPTION)['TYPE_NAME'];
  490. $objSheet->setCellValue($item . ($key + 2), $name);
  491. }elseif ($item == 'E')
  492. {
  493. $driver_name = BaseDriver::findOne($datum -> NAME)['DRIVER_NAME'];
  494. $objSheet->setCellValue($item . ($key + 2), $driver_name);
  495. }elseif($item == 'H')
  496. {
  497. $status_name = DictType::findOne($datum -> MT_STATUS)['TYPE_NAME'];
  498. $objSheet->setCellValue($item . ($key + 2), $status_name);
  499. }
  500. else
  501. $objSheet->setCellValue($item . ($key + 2), $datum-> $order_sort[$index]);
  502. }
  503. }
  504. //设置宽度
  505. $objPHPExcel->setColumnSize(range('B', 'H'), [20, 10, 20, 10, 30, 10, 15]);
  506. $objPHPExcel->output();
  507. }
  508. }