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.
 
 
 
 
 
 

1366 satır
58 KiB

  1. <?php
  2. namespace backend\modules\motorcade\controllers;
  3. use backend\modules\motorcade\components\BaseComponent;
  4. use backend\modules\motorcade\components\LogComponent;
  5. use backend\modules\motorcade\models\BaseDriver;
  6. use common\models\Utils;
  7. use function GuzzleHttp\Psr7\str;
  8. use yii\base\Exception;
  9. use backend\modules\motorcade\models\BusDepartment;
  10. use backend\modules\motorcade\models\BusDocLog;
  11. use backend\modules\motorcade\models\BusItinerary;
  12. use backend\modules\motorcade\models\BusOilDoc;
  13. use backend\modules\motorcade\models\BusOrderSearch;
  14. use backend\modules\motorcade\models\BusOrderStatusLog;
  15. use backend\modules\motorcade\models\BusReport;
  16. use backend\modules\motorcade\models\RunBusExpenses;
  17. use backend\modules\zzcs\models\BaseResource;
  18. use backend\modules\zzcs\models\RunBus;
  19. use common\components\zHttp;
  20. use common\models\BaseMainCorporation;
  21. use Yii;
  22. use backend\modules\motorcade\models\BusOrder;
  23. use backend\modules\motorcade\models\BaseBus;
  24. use yii\data\ActiveDataProvider;
  25. use yii\data\SqlDataProvider;
  26. use yii\helpers\ArrayHelper;
  27. use yii\helpers\Json;
  28. use yii\web\NotFoundHttpException;
  29. use yii\filters\AccessControl;
  30. use yii\filters\VerbFilter;
  31. use backend\modules\api\controllers\DriverController;
  32. use backend\modules\api\models\RunBusExpenses as BusExpense;
  33. /**
  34. * BusController implements the CRUD actions for BusOrder model.
  35. */
  36. class BusController extends BaseController
  37. {
  38. public $layout = "@backend/modules/motorcade/views/layouts/iframe_new";
  39. /**
  40. * @inheritdoc
  41. */
  42. public function behaviors()
  43. {
  44. return [
  45. 'access' => [
  46. 'class' => AccessControl::className(),
  47. 'rules' => [
  48. [
  49. 'actions' => ['index', 'add-bus-view', 'add-bus', 'detail', 'car-list', 'depart', 'add-bus-depart', 'search',
  50. 'cancel', 'resend', 'line-status', 'cancel1', 'bus-dispatch', 'save-bus', 'save-finance', 'send', 'test',
  51. 'sure', 'reject', 'reset', 'bus-resource', 'send-outside', 'batch', 'order-copy', 'sync-one'], 'allow' => true,
  52. ],
  53. [
  54. 'actions' => ['sync-bus-order', 'sync-bus-order-tem', 'simulation-bus', 'end-bus'], 'allow' => true,
  55. ],
  56. ],
  57. ],
  58. 'verbs' => [
  59. 'class' => VerbFilter::className(),
  60. 'actions' => [
  61. 'delete' => ['POST'],
  62. 'cancel' => ['POST'],
  63. ],
  64. ],
  65. ];
  66. }
  67. /**
  68. * Lists all BusOrder models.
  69. * @return mixed
  70. */
  71. public function actionIndex()
  72. {
  73. $searchModel = new BusOrderSearch();
  74. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  75. $dataProvider->pagination->pageSize = 10;
  76. return $this->render('index', [
  77. 'searchModel' => $searchModel,
  78. 'dataProvider' => $dataProvider,
  79. ]);
  80. }
  81. /**
  82. * Creates a new BusOrder model.
  83. * If creation is successful, the browser will be redirected to the 'view' page.
  84. * @return mixed
  85. */
  86. public function actionCreate()
  87. {
  88. $model = new BusOrder();
  89. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  90. return $this->redirect(['view', 'id' => $model->id]);
  91. } else {
  92. return $this->render('create', [
  93. 'model' => $model,
  94. ]);
  95. }
  96. }
  97. /**
  98. * User: wangxj
  99. *
  100. * 订单详情
  101. *
  102. * @param integer $id
  103. *
  104. * @return string
  105. */
  106. public function actionDetail($id)
  107. {
  108. $model = $this->findModel($id);
  109. $data['bus_type'] = $model->getBusType(); //用车类型
  110. $data['bus_depart'] = $model->getBusdepartment();
  111. $data['bus_log'] = $model->getLogInfo($model->bus_number);
  112. $single = ArrayHelper::index($data['bus_depart']['data'], 'ID', 'settle_type');
  113. $data['bus_depart']['data'] = ArrayHelper::map($data['bus_depart']['data'], 'ID', 'depart_name');
  114. if (!isset($data['bus_depart']['data'][$model->use_bus_org_id])) {
  115. $tmpModel = BusDepartment::findOne($model->use_bus_org_id);
  116. if ($tmpModel != null) {
  117. $data['bus_depart']['data'] += [$tmpModel->ID => $tmpModel->depart_name];
  118. }
  119. }
  120. //用车单位结算类型为单结
  121. $single = isset($single['292']) ? $single['292'] : [];
  122. $single_arr = [];
  123. if (!empty($single)) {
  124. $single_arr = array_keys($single);
  125. }
  126. $data['bus_depart']['single'] = $single_arr;
  127. return $this->render('detail', [
  128. 'model' => $model,
  129. 'data' => $data
  130. ]);
  131. }
  132. /**
  133. * User: wangxj
  134. *
  135. * 取消出车,只能通过ajax调用
  136. *
  137. * @params $id integer
  138. *
  139. * @return string
  140. */
  141. public function actionCancel()
  142. {
  143. $post = Yii::$app->request->post();
  144. if (isset($post['id'])) {
  145. $model = $this->findModel(Yii::$app->request->post('id'));
  146. //订单状态不可取消
  147. if ($model->run_status > 436) {
  148. $result = ['code' => 1, 'data' => ['msg' => '订单当前状态不可取消']];
  149. } else {
  150. $model->run_status = 439;
  151. $model->send_bus_res_id = null;
  152. $model->send_bus_driver_res_id = null;
  153. $model->send_type = 0;
  154. $model->setScenario('cancel');
  155. if ($model->save()) {
  156. //如果是从巴士同步过来的出车任务,run_id不为0,取消出车任务要操作run_bus表
  157. if ($model->run_id != 0) {
  158. $run_bus = RunBus::findOne(['cancel_flag' => 0, 'run_id' => $model->run_id, 'bus_order_id' => $model->bus_order_id]);
  159. if ($run_bus) {
  160. $run_bus->SEND_STATUS = 144;
  161. $run_bus->SEND_BUS_RES_ID = 0;
  162. $run_bus->SEND_BUS_NO = '';
  163. $run_bus->SEND_BUS_DRIVER_RES_ID = 0;
  164. $run_bus->SEND_DRIVER_NAME = '';
  165. $run_bus->SEND_DRIVER_MOBILE = '';
  166. $run_bus->save();
  167. }
  168. }
  169. $result = ['code' => 0, 'data' => ''];
  170. LogComponent::addLog($model->bus_number, LOG_TYPE_ORDER, '取消出车任务,取消成功', 'run_status', $model->run_status);
  171. } else {
  172. $result = ['code' => 1, 'data' => $model->getFirstErrors()];
  173. }
  174. }
  175. } else {
  176. $result = ['code' => 400, 'data' => ['msg' => '缺少参数']];
  177. }
  178. $test = Json::encode($result);
  179. return $test;
  180. }
  181. /**
  182. * Finds the BaseBus model based on its primary key value.
  183. * If the model is not found, a 404 HTTP exception will be thrown.
  184. * @param integer $id
  185. * @return BusOrder the loaded model
  186. * @throws NotFoundHttpException if the model cannot be found
  187. */
  188. protected function findModel($id)
  189. {
  190. if (($model = BusOrder::findOne($id)) !== null) {
  191. $model->run_status_name = BusOrder::bus_order_status_array[$model->run_status];
  192. $model->send_status_name = BusOrder::bus_order_status_array[$model->send_status];
  193. $model->finance_status_name = BusOrder::bus_order_status_array[$model->finance_status];
  194. $model->end_status_name = BusOrder::bus_order_status_array[$model->end_status];
  195. return $model;
  196. } else {
  197. throw new NotFoundHttpException('The requested page does not exist.');
  198. }
  199. }
  200. /**
  201. * User:Steven
  202. * 保存出车任务
  203. *
  204. * @return string
  205. */
  206. public function actionAddBus()
  207. {
  208. $bus_order = new BusOrder();
  209. $res = false;
  210. if (Yii::$app->request->isPost) {
  211. $bus_order->load(Yii::$app->request->post());
  212. $BusIt = array_values(Yii::$app->request->post('BusItinerary'));
  213. $bus_order->itinerary_list = [];
  214. foreach ($BusIt as $key => $item) {
  215. $BusItinerary = new BusItinerary();
  216. $BusItinerary->setAttributes($item);
  217. // if ($BusItinerary->validate()) {
  218. // $res = false;
  219. // }
  220. $bus_order->itinerary_list[] = $BusItinerary;
  221. }
  222. $res = $bus_order->addBus();
  223. if ($res) {
  224. Yii::$app->session->setFlash('success', "出车任务添加成功!");
  225. } else {
  226. Yii::$app->session->setFlash('error', "出车任务添加失败!");
  227. }
  228. // $bus_order = new BusOrder();
  229. // $BusItinerary = new BusItinerary();
  230. // $bus_order->itinerary_list[] = $BusItinerary;
  231. }
  232. if (empty($bus_order->itinerary_list)) {
  233. $BusItinerary = new BusItinerary();
  234. $bus_order->itinerary_list[] = $BusItinerary;
  235. }
  236. $bus_list = BaseBus::find()->select(['BUS_ID', 'BUS_NO'])->where(['CANCEL_FLAG' => 0, 'BUS_STATE' => BaseBus::BUS_STATUS_NORMAL, 'ORG_ID' => Yii::$app->user->identity->ORG_ID, 'BUS_BELONG' => BaseBus::BUS_BELONG_OWN])->asArray()->all();
  237. $driver_list = BaseDriver::find()->select(['DRIVER_ID', 'DRIVER_NAME'])->where(['CANCEL_FLAG' => 0, 'DRIVER_STATUS' => BaseDriver::DRIVER_STATUS_NORMAL, 'ORG_ID' => Yii::$app->user->identity->ORG_ID])->asArray()->all();
  238. $data['data']['result'] = $res;
  239. $data['data']['model'] = $bus_order;
  240. $data['data']['bus_list'] = $bus_list;
  241. $data['data']['driver_list'] = $driver_list;
  242. $data['data']['bus_type'] = $bus_order->getBusType(); //用车类型
  243. $data['data']['bus_depart'] = $bus_order->getBusdepartment();
  244. return $this->render('bus', ['view' => 'addBus', 'data' => $data]);
  245. }
  246. /**
  247. * User:Steven
  248. * 车辆调度
  249. *
  250. * @return string
  251. */
  252. public function actionBusDispatch()
  253. {
  254. $searchModel = new BusOrderSearch();
  255. $base_bus = new BaseBus();
  256. $data['data']['order_list'] = $searchModel->search(Yii::$app->request->queryParams);
  257. $data['data']['searchModel'] = $searchModel;
  258. $key_driver = isset($_GET['key_driver']) ? $_GET['key_driver'] : '';
  259. $key_bus = isset($_GET['key_bus']) ? $_GET['key_bus'] : '';
  260. $key = isset($_GET['key']) ? $_GET['key'] : '';
  261. $key1 = isset($_GET['key1']) ? $_GET['key1'] : '';
  262. $date = isset($_GET['date']) ? $_GET['date'] : '';
  263. $data['data']['active'] = isset($_GET['active']) ? $_GET['active'] : 'bus';
  264. $data['data']['bus_list'] = $base_bus->GetBusList($key_bus, $key, $date);
  265. $data['data']['convoy'] = $base_bus->getConvoy();
  266. $data['data']['driver'] = $base_bus->getDriver($key_driver, $date, $key1);
  267. $data['data']['searchDate'] = $date;
  268. $data['data']['driver_bus_search'] = ['key_driver' => $key_driver, 'key_bus' => $key_bus, 'key' => $key];
  269. if (Yii::$app->request->isAjax)
  270. return $this->renderPartial('bus', ['view' => 'busDispatch', 'data' => $data]);
  271. else
  272. return $this->render('bus', ['view' => 'busDispatch', 'data' => $data]);
  273. }
  274. /**
  275. * User:Steven
  276. * 添加用车单位
  277. *
  278. * @return string
  279. */
  280. public function actionAddBusDepart()
  281. {
  282. $bus_depart = new BusDepartment();
  283. if (Yii::$app->request->isPost) {
  284. $bus_depart->cancel_flag = 0;
  285. $bus_depart->main_cooperation_id = Yii::$app->user->identity->MAIN_CORP_ID2;
  286. $bus_depart->load(Yii::$app->request->post());
  287. $result = $bus_depart->save();
  288. if ($result) {
  289. $result = ['code' => 0, 'data' => $result];
  290. } else {
  291. $error = $bus_depart->getErrors();
  292. $result = ['code' => 1, 'data' => $error];
  293. }
  294. return Json::encode($result);
  295. }
  296. return true;
  297. }
  298. public function actionCarList($op = 'ready')
  299. {
  300. $searchModel = new BusOrderSearch();
  301. $search = Yii::$app->request->queryParams;
  302. if (empty($search)) {
  303. $search['BusOrderSearch']['start_date'] = date('Y-m-d', strtotime('this week'));
  304. $search['BusOrderSearch']['end_date'] = date('Y-m-d', strtotime('last day next week'));
  305. }
  306. $dataProvider = $searchModel->searchList($search);
  307. return $this->render('carList', [
  308. 'searchModel' => $searchModel,
  309. 'dataProvider' => $dataProvider,
  310. ]
  311. );
  312. }
  313. /**
  314. * User:Steven
  315. * Desc:复制出车功能
  316. * @return string
  317. */
  318. public function actionOrderCopy()
  319. {
  320. $model = new BusOrder();
  321. $bus_order_id = Yii::$app->request->get('id');
  322. if (Yii::$app->request->isPost) {
  323. $conn = Yii::$app->db;
  324. $param = Yii::$app->request->post();
  325. $res = $this->dataService($param);
  326. // $ori_order = BusOrder::findOne(['id' => $bus_order_id]);
  327. $ori_order = BusOrder::find()->select(['create_user_id', 'bus_number', 'use_bus_org_id', 'motorcade_id', 'task_type', 'bus_type_res_id', 'seat_count', 'itinerary_id', 'itinerary_name', 'line_type', 'day_num',
  328. 'bus_cost_type', 'bus_cost', 'remark', 'send_type', 'send_cost', 'send_bus_res_id', 'send_bus_driver_res_id', 'contacts', 'tels'])->where(['id' => $bus_order_id])->one();
  329. $transaction = $conn->beginTransaction(); //开始事务
  330. try {
  331. foreach ($res as $item) {
  332. $bus_order = new BusOrder();
  333. $bus_order->setAttributes($ori_order->getAttributes());
  334. $bus_order->create_time = date('Y-m-d H:i:s');
  335. $bus_order->run_date = $item['run_date'];
  336. $bus_order->start_time = $item['start_time'];
  337. $bus_order->saled_count = 0;
  338. $get_unique_id = "select func_get_unique_id(5,1) as unique_id"; //得到新的出车任务ID
  339. $data_id = $conn->createCommand($get_unique_id)->queryAll();
  340. $bus_order->bus_number = $data_id[0]['unique_id'];
  341. $bus_order->itinerary_id = $data_id[0]['unique_id'];
  342. $bus_order->run_bus_id = 0;
  343. $bus_order->run_id = 0;
  344. $bus_order->bus_order_id = 0;
  345. $bus_order->run_status = 434; //出车状态
  346. $bus_order->finance_status = 444; //报账状态
  347. $bus_order->send_status = 441;
  348. if (!isset($param['choosebus']) || $param['choosebus'] != 1) { //不需要同时复制司机和车辆
  349. $bus_order->send_cost = null;
  350. $bus_order->send_bus_res_id = null;
  351. $bus_order->send_bus_driver_res_id = null;
  352. $bus_order->send_status = 440;
  353. }
  354. $rs = BusItinerary::find()->select(['create_user_id', 'station_res_id', 'station_seq_id', 'station_name', 'station_address', 'Longitude', 'Latitude', 'inout_type', 'day_seq_id', 'start_time'])
  355. ->where(['itinerary_id' => $ori_order->itinerary_id])->orderBy('station_seq_id')->asArray()->all();
  356. foreach ($rs as $key => $val) {
  357. $itinerary = new BusItinerary();
  358. if (!$itinerary->load($val, '')) {
  359. throw new \Exception();
  360. }
  361. $itinerary->create_time = date('Y-m-d H:i:s');
  362. $diff_time = $this->timediff(date('Y-m-d', strtotime('+' . ($val['day_seq_id'] - 1) . 'days')) . ' ' . $val['start_time'] . ':00', date('Y-m-d') . ' ' . $rs[0]['start_time'] . ':00');
  363. $new_time = date('Y-m-d H:i:s', strtotime("+{$diff_time['day']} day +{$diff_time['hour']}hour +{$diff_time['min']} minute", strtotime($item['run_date'] . $item['start_time'] . ':00')));
  364. $itinerary->start_time = date('H:i', strtotime($new_time));
  365. $itinerary->itinerary_id = $data_id[0]['unique_id'];
  366. if (!$itinerary->save()) { //如果执行失败,手动抛出异常
  367. throw new \Exception();
  368. }
  369. }
  370. if (!$bus_order->save(false)) { //如果执行失败,手动抛出异常
  371. throw new \Exception();
  372. }
  373. //添加操作日志
  374. $msg = "复制出车任务,出车单号{$bus_order->bus_number},原单单号:{$ori_order['bus_number']}";
  375. if ($bus_order->send_bus_res_id != '') {
  376. $bus = BaseBus::findOne($bus_order->send_bus_res_id);
  377. $msg .= '同时复制车辆:' . $bus->BUS_NO . ';';
  378. }
  379. if ($bus_order->send_bus_driver_res_id != '') {
  380. $driver = BaseDriver::findOne($bus_order->send_bus_driver_res_id);
  381. $msg .= '同时复制司机:' . $driver->DRIVER_NAME;
  382. }
  383. LogComponent::addLog($bus_order->bus_number, LOG_TYPE_ORDER, $msg, 'bus_number', $bus_order->bus_number, 0);
  384. }
  385. $transaction->commit(); //提交
  386. return "<script>z.pjaxFinish('success','复制成功!','#float-div');</script>";
  387. } catch (Exception $e) {
  388. $transaction->rollBack();
  389. return "<script>z.pjaxFinish('error','复制失败!','#float-div');</script>";
  390. }
  391. }
  392. return $this->renderPartial('_order_copy', [
  393. 'model' => $model
  394. ]);
  395. }
  396. public function timediff($begin_time, $end_time)
  397. {
  398. $begin_time = strtotime($begin_time);
  399. $end_time = strtotime($end_time);
  400. if ($begin_time < $end_time) {
  401. $starttime = $begin_time;
  402. $endtime = $end_time;
  403. } else {
  404. $starttime = $end_time;
  405. $endtime = $begin_time;
  406. }
  407. $timediff = $endtime - $starttime;
  408. $days = intval($timediff / 86400);
  409. $remain = $timediff % 86400;
  410. $hours = intval($remain / 3600);
  411. $remain = $remain % 3600;
  412. $mins = intval($remain / 60);
  413. $secs = $remain % 60;
  414. $res = array("day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs);
  415. return $res;
  416. }
  417. /**
  418. * Created by PhpStorm.
  419. * NOTES:日期处理
  420. * User: Steven
  421. * Date: 2018/3/15
  422. * Time: 14:29
  423. * Class dataService
  424. * @param $param
  425. * @return array
  426. */
  427. private function dataService($param)
  428. {
  429. $count = 0;
  430. foreach ($param as $key => $value) {
  431. if (strstr($key, 'start_date') != false) {
  432. $count++;
  433. }
  434. }
  435. $order_data = array();
  436. for ($i = 0; $i < $count; $i++) {
  437. $start_date = $param['start_date' . $i];
  438. $end_date = $param['end_date' . $i];
  439. $week = isset($param['child_cb' . $i]) ? $param['child_cb' . $i] : array(0, 1, 2, 3, 4, 5, 6);
  440. $date_arr = $this->createDateRangeArray($start_date, $end_date, $week);
  441. foreach ($date_arr as $item) {
  442. foreach ($param['time' . $i] as $val)
  443. $order_data[] = array(
  444. 'run_date' => $item,
  445. 'start_time' => $val,
  446. );
  447. }
  448. }
  449. return $order_data;
  450. }
  451. /**
  452. * User:Steven
  453. * Desc:
  454. * @param $strDateFrom
  455. * @param $strDateTo
  456. * @return array
  457. */
  458. private function createDateRangeArray($strDateFrom, $strDateTo, $week_arr)
  459. {
  460. $aryRange = array();
  461. $iDateFrom = mktime(1, 0, 0, substr($strDateFrom, 5, 2), substr($strDateFrom, 8, 2), substr($strDateFrom, 0, 4));
  462. $iDateTo = mktime(1, 0, 0, substr($strDateTo, 5, 2), substr($strDateTo, 8, 2), substr($strDateTo, 0, 4));
  463. if ($iDateTo >= $iDateFrom) {
  464. if (in_array(date("w", $iDateFrom), $week_arr)) {
  465. array_push($aryRange, date('Y-m-d', $iDateFrom));//first entry
  466. }
  467. while ($iDateFrom < $iDateTo) {
  468. $iDateFrom += 86400;//add 24 hours
  469. $number_wk = date("w", $iDateFrom);
  470. if (in_array($number_wk, $week_arr)) {
  471. array_push($aryRange, date('Y-m-d', $iDateFrom));
  472. }
  473. }
  474. }
  475. return $aryRange;
  476. }
  477. //线路牌确认
  478. public function actionLineStatus()
  479. {
  480. $obj = new BusOrder();
  481. $data = $obj->modifyLineSafe();
  482. echo json_encode($data);
  483. }
  484. //列表页取消订单
  485. public function actionCancel1()
  486. {
  487. $obj = new BusOrder();
  488. $data = $obj->cancelBusOrder();
  489. echo json_encode($data);
  490. }
  491. /**
  492. * User: wangxj
  493. *
  494. * 变更出车需求,只允许ajax提交
  495. *
  496. * @params
  497. *
  498. * @return string
  499. */
  500. public function actionSaveBus($id)
  501. {
  502. if (Yii::$app->request->isAjax) {
  503. $model = $this->findModel($id);
  504. $model->load(Yii::$app->request->post());
  505. if ($model->validate()) {
  506. $arr = [];
  507. $line = true;
  508. $errArray = [];
  509. foreach (Yii::$app->request->post('BusItinerary') as $key => $item) {
  510. if (isset($item['id']) && $item['id'] != '') {
  511. $tmpModel = BusItinerary::findOne(['id' => $item['id']]);
  512. $tmpModel->setOldAttribute('cancel_flag', 1);
  513. } else
  514. $tmpModel = new BusItinerary();
  515. $tmpModel->load($item, '');
  516. $tmpModel['itinerary_id'] = $model->bus_number;
  517. $tmpModel['station_seq_id'] = $key;
  518. $tmpModel['cancel_flag'] = 0;
  519. if ($line && $tmpModel->validate()) {
  520. $arr[] = $tmpModel;
  521. } else {
  522. if (count($tmpModel->getFirstErrors()) > 0)
  523. $errArray[] = $tmpModel->getFirstErrors();
  524. $line = false;
  525. }
  526. //取第一个站点的时间保存发车时间
  527. if ($key == 0) {
  528. $model->start_time = $item['start_time'];
  529. }
  530. }
  531. if ($line) {
  532. $seat_res_model = BaseResource::findOne(['ID' => $model->bus_type_res_id]);
  533. $model->seat_count = $seat_res_model->getResNum();
  534. $model->save();
  535. //修改时,先置旧数据cancel_flag全为0,
  536. BusItinerary::setCancel($model->bus_number);
  537. foreach ($arr as $item) {
  538. $item->save();
  539. }
  540. $result = ['code' => 0, 'data' => []];
  541. //::todo 具体变更了哪些字段还需要写函数判断
  542. LogComponent::addLog($model->bus_number, LOG_TYPE_ORDER, '出车需求变更', 'send_status', $model->send_status);
  543. //给司机端发送提醒信息
  544. $data = [
  545. 'action' => 'send_driver_msg',
  546. 'msg_type' => 4,
  547. 'bus_number' => $model->bus_number
  548. ];
  549. zHttp::httpRequest('http://' . CS1_DOMAIN . '/api/driver/', $data);
  550. } else {
  551. $result = ['code' => 1, 'data' => $errArray];
  552. }
  553. } else {
  554. $result = ['code' => 1, 'data' => $model->getFirstErrors()];
  555. }
  556. return Json::encode($result);
  557. } else {
  558. throw new NotFoundHttpException('The requested page does not exist.');
  559. }
  560. }
  561. /**
  562. * User: wangxj
  563. *
  564. * 变更出车需求,报账,只允许ajax提交
  565. *
  566. * @param $id integer
  567. *
  568. * @return string
  569. */
  570. public function actionSaveFinance($id)
  571. {
  572. if (Yii::$app->request->isAjax) {
  573. //外部派车在数据库中有RunBusExpenses,但是这里无法修改RunBusExpenses记录,且不会传数据到些函数
  574. $runBusExpenses = Yii::$app->request->post('RunBusExpenses');
  575. if ($runBusExpenses || Yii::$app->request->post('BusOrder')) {
  576. $line = true;
  577. $errArray = [];
  578. $transaction = Yii::$app->db->beginTransaction();
  579. try {
  580. //保存收入
  581. $tmpData = Yii::$app->request->post('BusOrder');
  582. $bus_order = BusOrder::findOne($tmpData['id']);
  583. if ($bus_order->load($tmpData, '')) {
  584. $bus_order->save();
  585. }
  586. //保存报账
  587. if (empty(!$runBusExpenses)) {
  588. foreach ($runBusExpenses as $key => $item) {
  589. $tmpModel = RunBusExpenses::findOne(['ID' => $key, 'cancel_flag' => 0]);
  590. // $tmpModel->load($item, false);
  591. if ($tmpModel != null) {
  592. $tmpModel->setAttributes($item);
  593. //如果是路桥费且是ETC支付,并且变更了值需要更新ETC消费记录
  594. if ($tmpModel->expense_subject_id == RunBusExpenses::SUBJECT_BRIDGE &&
  595. $tmpModel->expense_type == RunBusExpenses::PAY_TYPE_ETC &&
  596. $tmpModel->doc_log_id != 0
  597. ) {
  598. $log = BusDocLog::findOne($tmpModel->doc_log_id);
  599. if ($tmpModel->start_value < $tmpModel->end_value) {
  600. //起始值小于结束值
  601. $errArray[] = ['end_value' => '结束值不能大于起始值'];
  602. $line = false;
  603. } elseif ($log && $log->OPERATION_TYPE == BusDocLog::OPERATION_TYPE_PAID
  604. ) {
  605. // $log->CANCEL_FLAG = 1;
  606. $log->VALUE = $tmpModel->start_value - $tmpModel->end_value;
  607. if ($log->VALUE != $log->getOldAttribute('VALUE')) {
  608. $log->NOTE = '报账变更数据';
  609. $log->OPERATION_USER = Yii::$app->user->id;
  610. $doc = BusOilDoc::findOne($log->CARD_ID);
  611. if ($doc) {
  612. $doc->CARD_BALANCE = $doc->CARD_BALANCE - $log->VALUE + $log->getOldAttribute('VALUE');
  613. $log->save(false);
  614. $doc->save(false);
  615. }
  616. }
  617. }
  618. }
  619. $tmpModel->save();
  620. /* @var $bus_order BusOrder */
  621. //已审核或驳回也可以修改报账数据,但是报账状态要变更为待审核
  622. $bus_order = BusOrder::findOne($id);
  623. $bus_order->finance_status = BusOrder::STATUS_FINANCE_WAITING;
  624. $bus_order->save(false);
  625. if (count($tmpModel->getFirstErrors()) > 0) {
  626. $errArray[] = $tmpModel->getFirstErrors();
  627. $line = false;
  628. }
  629. }
  630. }
  631. }
  632. if ($line && !$bus_order->hasErrors()) {
  633. $transaction->commit();
  634. } else {
  635. $transaction->rollBack();
  636. }
  637. } catch (Exception $exception) {
  638. $transaction->rollBack();
  639. throw new Exception('数据库错误', 500);
  640. }
  641. if ($line) {
  642. $result = ['code' => 0, 'data' => []];
  643. } else {
  644. $result = ['code' => 1, 'data' => $errArray];
  645. }
  646. } else {
  647. throw new NotFoundHttpException('The requested page does not exist.');
  648. }
  649. return Json::encode($result);
  650. } else {
  651. throw new NotFoundHttpException('The requested page does not exist.');
  652. }
  653. }
  654. /**
  655. * User: wangxj
  656. *
  657. * 调派车辆司机,如果是同步的出车任务,需要修改run_bus表
  658. *
  659. * @params $type string 司机或车辆
  660. * @params $id integer bus_order id
  661. *
  662. * @return string
  663. */
  664. public function actionSend($type, $id, $res_id)
  665. {
  666. if (Yii::$app->request->isAjax) {
  667. $model = $this->findModel($id);
  668. //出车状态
  669. if ($model->run_status == 434) {
  670. // 调度状态不为已驳回
  671. if ($model->send_status != 443) {
  672. //车辆或司机的res_id写入对应字段
  673. $model->$type = $res_id;
  674. //如果司机和车辆都调度了,修改状态为 待确认
  675. if ($model->send_bus_res_id !== null && $model->send_bus_driver_res_id !== null)
  676. $model->send_status = 441;
  677. //如果是外部用车,只依据是否传回了outside_value没有进行数据验证,需要多查询一次
  678. if (isset($_POST['outside_value'])) {
  679. $model->send_cost = floatval($_POST['outside_value']);
  680. $model->send_type = 1;
  681. } elseif ($type == 'send_bus_res_id') {
  682. $model->send_type = 0;
  683. //内部派车,成本默认为0
  684. $model->send_cost = 0;
  685. // $model->send_cost = $model->bus_cost;
  686. }
  687. if ($type == 'send_bus_res_id') {//车辆
  688. $res = BaseBus::findOne(['BUS_ID' => $res_id]);
  689. $res_name = $res->BUS_NO;
  690. } else {
  691. $res = BaseDriver::findOne(['DRIVER_ID' => $res_id]);
  692. $res_name = $res->DRIVER_NAME;
  693. }
  694. //修改run_bus表,好像只需要调用存储过程 SP_VEHICLE_DISPATCH('".$USER_ID."','".$BUS_ID."','".$RES_ID."')
  695. if ($model->task_type == BusOrder::TASK_TYPE_SYNC) {
  696. $run_bus = RunBus::findOne(['cancel_flag' => 0, 'id' => $model->run_bus_id]);
  697. if ($run_bus) {
  698. if ($type == 'send_bus_res_id') {//车辆
  699. // $sql_proc = "call SP_VEHICLE_DISPATCH('" . Yii::$app->user->id . "','" . $model->run_bus_id . "','" . $res_id . "')";
  700. // $conn = Yii::$app->db;
  701. // $data = $conn->createCommand($sql_proc)->queryAll();
  702. // if ($data[0]['errorcode'] != 0) {
  703. // $model->addError('id', $data[0]['errorinfo']);
  704. //// $flag = false;
  705. //// $errMessage = $data[0]['errorinfo'];
  706. // }
  707. // $run_bus->SEND_BUS_ORG_ID = $res->ORG_ID;
  708. // $run_bus->SEND_BUS_RES_ID = $res->BUS_ID;
  709. // $run_bus->SEND_BUS_TYPE_RES_ID = $res->BUS_TYPE_RES_ID;
  710. // $run_bus->SEND_BUS_NO = $res->BUS_NO;
  711. $url = 'http://' . CS1_DOMAIN . '/api/change-seat/change-bus-for-run-bus';
  712. $params = array(
  713. 'user_id' => Yii::$app->user->id,
  714. 'bus_id' => $res_id,
  715. 'run_bus_id' => $model->run_bus_id
  716. );
  717. $result = Utils::httpRequest($url, $params);
  718. $result = json_decode($result, true);
  719. if (!isset($result['code']) || $result['code'] != 0) {
  720. $result['code'] = "1";
  721. $result['data'] = empty($result['info']) ? '接口异常' : $result['info'];
  722. return Json::encode($result);
  723. }
  724. } elseif ($type == 'send_bus_driver_res_id') {
  725. $run_bus->SEND_BUS_DRIVER_RES_ID = $res_id;
  726. $run_bus->SEND_DRIVER_NAME = $res->DRIVER_NAME;
  727. $run_bus->SEND_DRIVER_MOBILE = $res->PHONE_NO;
  728. if (!$run_bus->save()) {
  729. $tmpKey = array_keys($run_bus->getFirstErrors())[0];
  730. $model->addError('id', $run_bus->getFirstError($tmpKey));
  731. }
  732. }
  733. }
  734. }
  735. if (empty($model->getErrors()) && $model->save()) {
  736. if ($model->task_type == BusOrder::TASK_TYPE_CUSTOM && empty($model->getErrors())) {
  737. $model->updateRunBus();
  738. }
  739. $result = ['code' => 0, 'data' => $res_name];
  740. LogComponent::addLog($model->bus_number, LOG_TYPE_ORDER, '调派' . $model->getAttributeLabel($type) . ":{$res_name} 成功", 'send_status', $model->send_status);
  741. } else {
  742. $result = ['code' => 1, 'data' => $model->getFirstErrors()];
  743. }
  744. } else {
  745. $result = ['code' => 2, 'data' => ['msg' => $model->getAttributeLabel($type) . '调派失败,已驳回状态下不能调度']];
  746. }
  747. } else {
  748. $result = ['code' => 1, 'data' => ['msg' => $model->getAttributeLabel($type) . '调派失败']];
  749. }
  750. return Json::encode($result);
  751. } else {
  752. throw new NotFoundHttpException('The requested page does not exist.');
  753. }
  754. }
  755. /**
  756. * User: wangxj
  757. *
  758. * 判断是否外部用车
  759. *
  760. * @params $res_id 车id
  761. *
  762. * @return string json
  763. */
  764. public function actionSendOutside($res_id)
  765. {
  766. // $res = BaseBus::findOne(['BUS_ID' => $res_id]);
  767. $res = BaseBus::find()
  768. ->select(['a.BUS_BELONG', 'b.START_DATE', 'b.EXPIRE_DATE'])
  769. ->leftJoin('base_document b', 'a.BUS_ID=b.DOC_ID AND b.DOC_TYPE = 1 AND b.DOC_STATUS =1 AND b.PID=0 AND b.CANCEL_FLAG = 0 AND b.MT_STATUS in(591, 592)')
  770. ->from('base_bus a')
  771. ->where(['a.CANCEL_FLAG' => 0, 'a.BUS_ID' => $res_id])
  772. ->asArray()->one();
  773. if ($res != null) {
  774. // 如果两个都不为空,提示两次
  775. if ($res['BUS_BELONG'] != BaseBus::BUS_BELONG_OWN && isset($res['START_DATE'])) {
  776. return json_encode(array('code' => 3, 'start_date' => $res['START_DATE']));
  777. } elseif ($res['BUS_BELONG'] != BaseBus::BUS_BELONG_OWN) { // 只提示外部车辆
  778. return json_encode(array('code' => 1));
  779. } elseif (isset($res['START_DATE'])) // 只提示维保日期
  780. {
  781. return json_encode(array('code' => 2, 'start_date' => $res['START_DATE']));
  782. }
  783. }
  784. return 0;
  785. }
  786. /**
  787. * @Author wanglg
  788. * @Desc 判断维保时间
  789. */
  790. public function actionOutTime()
  791. {
  792. }
  793. public function actionSureRun($id, $attribute)
  794. {
  795. return $this->sure($id, $attribute);
  796. }
  797. public function actionSureFinance($id, $attribute)
  798. {
  799. return $this->sure($id, $attribute);
  800. }
  801. /**
  802. * User: wangxj
  803. *
  804. * 确认派车派司机 报账审核通过 结算通过 442已调度 446已审核 449已结算
  805. *
  806. * @params $id integer 出车订单id
  807. *
  808. * @return string
  809. */
  810. public function actionSure($id, $attribute)
  811. {
  812. $flag = true;
  813. if (Yii::$app->getRequest()->isAjax) {
  814. $model = $this->findModel($id);
  815. switch ($attribute) {
  816. case 'send_status':
  817. $status = 442;
  818. break;
  819. case 'finance_status':
  820. $status = 446;
  821. break;
  822. case 'end_status':
  823. $status = 449;
  824. break;
  825. default:
  826. throw new NotFoundHttpException('无法被验证的状态类型');
  827. }
  828. $tran = Yii::$app->db->beginTransaction();
  829. try {
  830. if ($model) {
  831. $already_send_count = $model->send_bus_count;//add by qius on 2017.04.19
  832. $beforeValue = $model->$attribute;
  833. if ($attribute == 'send_status') {
  834. if ($model->send_bus_res_id && $model->send_bus_driver_res_id) {
  835. $model->$attribute = $status;
  836. $model->run_status = 435;
  837. $model->send_bus_count = $already_send_count + 1;//add by qius on 2017.04.19
  838. if (!$model->save())
  839. $flag = false;
  840. //调用http://cs1.zhizhuchuxing.com/api/driver/接口
  841. $data = [
  842. 'action' => 'send_driver_msg',
  843. 'msg_type' => 3,
  844. 'bus_number' => $model->bus_number
  845. ];
  846. // zHttp::httpRequest('http://' . CS1_DOMAIN . '/api/driver/', $data);
  847. $post1 = $data;
  848. //判断是否需要发送短信
  849. if ($model->run_id != 0) {
  850. $today_day = date("Y-m-d");
  851. $current_hm = date("H:i");
  852. $current_time = $today_day . " " . $current_hm;
  853. $tomorrow_day = date("Y-m-d", strtotime("+1 days"));
  854. $limit_time = date("Y-m-d H:i", strtotime("+1 hours"));
  855. $run_start_time = $model->run_date . " " . $model->start_time;
  856. // if ($run_start_time > $current_time && (($model->run_date == $tomorrow_day && $current_hm > "18:30") || $model->run_date == $today_day)) {
  857. if ($run_start_time < $limit_time) {
  858. $data = [
  859. 'action' => 'send_bus_change_message',
  860. 'action' => 'send_bus_change_message',
  861. 'run_id' => $model->run_id,
  862. 'bus_order_id' => $model->bus_order_id,
  863. 'send_bus_count' => $already_send_count,
  864. 'itinerary_name' => $model->itinerary_name,
  865. 'new_bus_no' => $model->bus->BUS_NO,
  866. 'run_start_time' => $run_start_time
  867. ];
  868. // zHttp::httpRequest('http://' . CS1_DOMAIN . '/api/msg/send-bus-change-message', $data);
  869. $post2 = $data;
  870. }
  871. }
  872. //调度成功,需要写memcache
  873. $one = [
  874. 'bus_number' => $model->bus_number, //出车单号
  875. 'bus_no' => $model->bus->BUS_NO, //车牌号
  876. 'driver_name' => $model->driver->DRIVER_NAME, //司机姓名
  877. 'line_name' => $model->itinerary_name, //线路名称
  878. 'run_date' => date('Y-m-d H:i:s'), //确认调度的时间,目前确认调度的时间
  879. ];
  880. if ($flag)
  881. $cacheData = $one;
  882. $result = ['code' => 0, 'data' => ''];
  883. } else {
  884. $result = ['code' => 1, 'data' => ['msg' => '无法确认']];
  885. }
  886. } else {
  887. $model->$attribute = $status;
  888. //如果是报账审核通过,结算状态由之前的无状态变为未结算
  889. if ($attribute == 'finance_status') {
  890. $model->end_status = 448;
  891. }
  892. $model->save();
  893. $result = ['code' => 0, 'data' => ''];
  894. }
  895. if ($result['code'] == 0 && $flag) {
  896. //如果是从巴士同步过来的出车任务,run_id不为0,要操作run_bus表
  897. if ($model->run_id != 0) {
  898. $run_bus = RunBus::findOne(['cancel_flag' => 0, 'run_id' => $model->run_id, 'bus_order_id' => $model->bus_order_id]);
  899. if ($run_bus) {
  900. $run_bus->SEND_STATUS = 143;
  901. if (!$run_bus->save(false)) {
  902. $flag = false;
  903. }
  904. }
  905. }
  906. $type = BusOrderStatusLog::$_type_array[strtoupper($attribute)];
  907. $msg = $attribute == 'send_status' ? '确认调度,确认调度成功' : ($attribute == 'finance_status' ? '出车完报账待审核,审核通过' : '结算审核');
  908. BaseComponent::addStatusLog($model->bus_number, $type, $beforeValue, $model->$attribute, $msg);
  909. }
  910. } else {
  911. $result = ['code' => 1, 'data' => ['msg' => '无法确认']];
  912. }
  913. } catch (Exception $e) {
  914. $tran->rollBack();
  915. $result = ['code' => 1, 'data' => ['msg' => '无法确认']];
  916. }
  917. if ($flag) {
  918. $tran->commit();
  919. //调用http://cs1.zhizhuchuxing.com/api/driver/接口
  920. if (isset($post1)) {
  921. zHttp::httpRequest('http://' . CS1_DOMAIN . '/api/driver/', $post1);
  922. }
  923. //判断是否需要发送短信
  924. if (isset($post2)) {
  925. zHttp::httpRequest('http://' . CS1_DOMAIN . '/api/msg/send-bus-change-message', $post2);
  926. }
  927. if (isset($cacheData)) {
  928. $cache = Yii::$app->cache;
  929. $send_sure = $cache->get('send_sure_' . $model->login_user->MAIN_CORP_ID2);
  930. if ($send_sure === false) {
  931. // $data is not found in cache, calculate it from scratch
  932. $data = [$cacheData];
  933. // store $data in cache so that it can be retrieved next time
  934. $cache->set('send_sure_' . $model->login_user->MAIN_CORP_ID2, $data);
  935. } else {
  936. $send_sure[] = $cacheData;
  937. $cache->set('send_sure_' . $model->login_user->MAIN_CORP_ID2, $send_sure);
  938. }
  939. }
  940. }
  941. return Json::encode($result);
  942. } else {
  943. throw new NotFoundHttpException('The requested page does not exist.');
  944. }
  945. }
  946. /**
  947. * User: wangxj
  948. *
  949. * 驳回 send_status 443 finance_status 447 end_status 482
  950. *
  951. * @params $id integer 出车订单id
  952. *
  953. * @return string
  954. */
  955. public function actionReject($id, $msg, $attribute)
  956. {
  957. if (Yii::$app->getRequest()->isAjax) {
  958. $model = $this->findModel($id);
  959. switch ($attribute) {
  960. case 'send_status':
  961. $status = 443;
  962. break;
  963. case 'finance_status':
  964. $status = 447;
  965. break;
  966. case 'end_status':
  967. $status = 482;
  968. break;
  969. default:
  970. throw new NotFoundHttpException('无法被验证的状态类型');
  971. }
  972. if ($model) {
  973. $beforeValue = $model->$attribute;
  974. if ($attribute == 'send_status') {
  975. $model->send_status = 443;
  976. $model->send_msg = $msg;
  977. $model->send_type = 0;
  978. $model->send_bus_driver_res_id = '';
  979. $model->send_bus_res_id = '';
  980. $model->save();
  981. $result = ['code' => 0, 'data' => ''];//修改run_bus表
  982. if ($model->run_id != 0) {
  983. $run_bus = RunBus::findOne(['cancel_flag' => 0, 'run_id' => $model->run_id, 'bus_order_id' => $model->bus_order_id]);
  984. if ($run_bus) {
  985. //车辆
  986. $run_bus->SEND_BUS_ORG_ID = '';
  987. $run_bus->SEND_BUS_RES_ID = '';
  988. $run_bus->SEND_BUS_TYPE_RES_ID = '';
  989. $run_bus->SEND_BUS_NO = '';
  990. //司机
  991. $run_bus->SEND_BUS_DRIVER_RES_ID = '';
  992. $run_bus->SEND_DRIVER_NAME = '';
  993. $run_bus->SEND_DRIVER_MOBILE = '';
  994. $run_bus->save();
  995. }
  996. }
  997. } else {
  998. $model->$attribute = $status;
  999. $model->send_msg = $msg;
  1000. $model->save();
  1001. $result = ['code' => 0, 'data' => ''];
  1002. //报账驳回调用接口
  1003. if ($attribute == 'finance_status') {
  1004. $data = [
  1005. 'action' => 'send_driver_msg',
  1006. 'msg_type' => 2,
  1007. 'bus_number' => $model->bus_number
  1008. ];
  1009. zHttp::httpRequest('http://' . CS1_DOMAIN . '/api/driver/', $data);
  1010. }
  1011. }
  1012. } else {
  1013. $result = ['code' => 1, 'data' => ['msg' => '驳回失败']];
  1014. }
  1015. if ($result['code'] == 0) { //驳回成功,更新BusOrderStatusLog
  1016. $type = BusOrderStatusLog::$_type_array[strtoupper($attribute)];
  1017. BaseComponent::addStatusLog($model->bus_number, $type, $beforeValue, $model->$attribute, '驳回出车任务,原因:' . $msg);
  1018. }
  1019. return Json::encode($result);
  1020. } else {
  1021. throw new NotFoundHttpException('The requested page does not exist.');
  1022. }
  1023. }
  1024. /**
  1025. * User: wangxj
  1026. *
  1027. * 重置调度
  1028. *
  1029. * @params $id integer 出车订单id
  1030. *
  1031. * @param $id
  1032. * @param string $attribute
  1033. * @return string
  1034. * @throws NotFoundHttpException
  1035. */
  1036. public function actionReset($id, $attribute = 'send_status')
  1037. {
  1038. if (Yii::$app->getRequest()->isAjax) {
  1039. $model = $this->findModel($id);
  1040. if ($model && in_array($model->run_status, [434, 435, 436])) {
  1041. $model->send_status = 440;
  1042. $model->run_status = 434;
  1043. $model->send_bus_res_id = '';
  1044. $model->send_bus_driver_res_id = '';
  1045. $model->send_type = 0;
  1046. $model->send_cost = 0;
  1047. $model->save();
  1048. $result = ['code' => 0, 'data' => ''];
  1049. //如果是从巴士同步过来的出车任务,run_id不为0,取消出车任务要操作run_bus表
  1050. if ($model->run_id != 0) {
  1051. $run_bus = RunBus::findOne(['cancel_flag' => 0, 'run_id' => $model->run_id, 'bus_order_id' => $model->bus_order_id]);
  1052. if ($run_bus) {
  1053. $run_bus->SEND_STATUS = 144;
  1054. //车辆
  1055. $run_bus->SEND_BUS_ORG_ID = '';
  1056. $run_bus->SEND_BUS_RES_ID = '';
  1057. $run_bus->SEND_BUS_TYPE_RES_ID = '';
  1058. $run_bus->SEND_BUS_NO = '';
  1059. //司机
  1060. $run_bus->SEND_BUS_DRIVER_RES_ID = '';
  1061. $run_bus->SEND_DRIVER_NAME = '';
  1062. $run_bus->SEND_DRIVER_MOBILE = '';
  1063. $run_bus->save();
  1064. $run_bus->save();
  1065. }
  1066. }
  1067. } else {
  1068. $result = ['code' => 1, 'data' => ['msg' => BusOrder::bus_order_status_array[$model->run_status] . '状态无法重置']];
  1069. }
  1070. if ($result['code'] == 0) {
  1071. $type = BusOrderStatusLog::$_type_array[strtoupper($attribute)];
  1072. $beforeValue = $model->oldAttributes[$attribute];
  1073. BaseComponent::addStatusLog($model->bus_number, $type, $beforeValue, $model->$attribute, '重置调度信息,重置成功');
  1074. }
  1075. return Json::encode($result);
  1076. } else {
  1077. throw new NotFoundHttpException('The requested page does not exist.');
  1078. }
  1079. }
  1080. /**
  1081. * User:Steven
  1082. * 同步CS车次记录为出车任务
  1083. */
  1084. public function actionSyncBusOrder()
  1085. {
  1086. $model = new BusOrder();
  1087. $model->SyncBusOrder();
  1088. }
  1089. /**
  1090. * User:Steven
  1091. * 临时
  1092. * 自定义同步CS车次记录为出车任务
  1093. */
  1094. public function actionSyncBusOrderTem()
  1095. {
  1096. $model = new BusOrder();
  1097. $model->SyncBusOrder(false);
  1098. }
  1099. /**
  1100. * User:Steven
  1101. *
  1102. * 资源信息管理--基础配置
  1103. * @return string
  1104. */
  1105. public function actionBusResource()
  1106. {
  1107. \Yii::$app->view->title = '品牌管理';
  1108. //品牌列表
  1109. $brand_list = BaseResource::find()->select(['res_id', 'res_name'])->where(['res_type_id' => 134, 'cancel_flag' => 0])->asArray()->orderBy(['update_time' => SORT_DESC])->all();
  1110. return $this->render('bus', ['view' => 'busResource', 'data' => $brand_list]);
  1111. }
  1112. /**
  1113. * User:Steven
  1114. *
  1115. * 资源信息管理--基础配置
  1116. * @return string
  1117. */
  1118. public function actionBusManager()
  1119. {
  1120. return $this->render('bus', ['view' => 'busResource', 'data' => '']);
  1121. }
  1122. /**
  1123. * 同步数据时站点信息错误, 更新错误数据
  1124. * User: Steven
  1125. */
  1126. public function actionTest()
  1127. {
  1128. $buso = new BusOrder();
  1129. $res = $buso->test();
  1130. }
  1131. /**
  1132. * User: wangxj
  1133. *
  1134. * 批量操作,确认 驳回 重置
  1135. *
  1136. * @return string
  1137. */
  1138. public function actionBatch()
  1139. {
  1140. $data = Yii::$app->request->post();
  1141. $type = $data['type'];
  1142. $list = $data['list'];
  1143. $count = count($list);
  1144. $count_s = 0;
  1145. $count_e = 0;
  1146. foreach ($list as $item) {
  1147. $model = BusOrder::findOne($item);
  1148. if ($model !== null) {
  1149. //调度确认
  1150. if ($type == 'sure') {
  1151. $today_day = date("Y-m-d");
  1152. $current_hm = date("H:i");
  1153. $current_time = $today_day . " " . $current_hm;
  1154. $tomorrow_day = date("Y-m-d", strtotime("+1 days"));
  1155. if ($model->send_status == BusOrder::STATUS_SEND_WAITING_SURE) {
  1156. $model->send_status = BusOrder::STATUS_SEND_SURE;
  1157. $model->run_status = BusOrder::STATUS_RUN_READY;
  1158. $already_send_count = $model->send_bus_count;
  1159. $model->send_bus_count = $already_send_count + 1;
  1160. if ($model->save()) {
  1161. //发送短信
  1162. if ($model->run_id != 0) {
  1163. $run_start_time = $model->run_date . " " . $model->start_time;
  1164. if ($run_start_time > $current_time && (($model->run_date == $tomorrow_day && $current_hm > "18:30") || $model->run_date == $today_day)) {
  1165. $run_bus = RunBus::findOne(['cancel_flag' => 0, 'run_id' => $model->run_id, 'bus_order_id' => $model->bus_order_id]);
  1166. $post1 = [
  1167. 'action' => 'send_driver_msg',
  1168. 'msg_type' => 3,
  1169. 'bus_number' => $model->bus_number
  1170. ];
  1171. zHttp::httpRequest('http://' . CS1_DOMAIN . '/api/driver/', $post1);
  1172. $post2 = [
  1173. 'action' => 'send_bus_change_message',
  1174. 'run_id' => $model->run_id,
  1175. 'bus_order_id' => $model->bus_order_id,
  1176. 'send_bus_count' => $already_send_count,
  1177. 'itinerary_name' => $model->itinerary_name,
  1178. 'new_bus_no' => $run_bus->SEND_BUS_NO,
  1179. 'run_start_time' => $run_start_time
  1180. ];
  1181. zHttp::httpRequest('http://' . CS1_DOMAIN . '/api/msg/send-bus-change-message', $post2);
  1182. }
  1183. }
  1184. $count_s++;
  1185. }
  1186. }
  1187. }
  1188. //驳回
  1189. if ($type == 'reject') {
  1190. }
  1191. //重置
  1192. if ($type == 'reset') {
  1193. }
  1194. }
  1195. }
  1196. Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  1197. if ($count_s == $count) {
  1198. return ['status' => 0, 'list' => $list, 'type' => 'success', 'msg' => "确认成功:{$count_s} 个出车任务完成调度确认"];
  1199. } else if ($count_s != 0) {
  1200. $count_e = $count - $count_s;
  1201. return ['status' => 1, 'list' => $list, 'type' => 'success', 'msg' => "确认成功:{$count_s} 个出车任务完成调度确认", 'fail' => "确认失败:{$count_e} 个出车任务确认失败"];
  1202. } else if ($count_s == 0) {
  1203. return ['status' => 2, 'list' => $list, 'type' => 'error', 'msg' => "确认失败:{$count} 个出车任务确认失败"];
  1204. }
  1205. }
  1206. /**
  1207. * 同步单个出车任务
  1208. *
  1209. * @param $run_bus_id_str string 多任务用逗号分隔
  1210. * @return string
  1211. * @author wangxj
  1212. */
  1213. public function actionSyncOne($run_bus_id_str)
  1214. {
  1215. $arr = explode(',', $run_bus_id_str);
  1216. if (!empty($arr)) {
  1217. $flag_error = false; //是否有失败记录
  1218. $flag_success = false; //成功过一次
  1219. $error_list = [];
  1220. foreach ($arr as $item) {
  1221. $bus_order = BusOrder::findOne(['cancel_flag' => 0, 'run_bus_id' => $item]);
  1222. if ($bus_order == null) {
  1223. $flag_error = true;
  1224. $error_list[] = ['run_bus_id' => $item, 'msg' => '无法找到出车任务'];
  1225. } else {
  1226. if ($bus_order->finance_lock == 1 || $bus_order->outcome_finance_lock == 1) {
  1227. $error_list[] = ['run_bus_id' => $item, 'msg' => '已经关账'];
  1228. continue;
  1229. }
  1230. $tmp_sync = $bus_order->syncOneBusOrder();
  1231. $flag_success = $tmp_sync ? true : $flag_success;
  1232. if (!$tmp_sync) {
  1233. $error_list[] = ['run_bus_id' => $item, 'msg' => '同步失败'];
  1234. }
  1235. }
  1236. }
  1237. return json_encode(['flag' => !$flag_error && $flag_success, 'msg' => '', 'error_list' => $error_list]);
  1238. } else {
  1239. return json_encode(['flag' => false, 'msg' => '无效参数']);
  1240. }
  1241. }
  1242. /**
  1243. * Author:Steven
  1244. * @param bus_id 车辆ID
  1245. * Desc:模拟司机端进行出车任务
  1246. */
  1247. public function actionSimulationBus()
  1248. {
  1249. //获取页面数据请求接口
  1250. $bus_res_id = Yii::$app->request->get('bus_id');
  1251. $run_bus_expenses = new BusExpense();
  1252. $data['end_value'] = $run_bus_expenses->getExpenseMaxEndValue($bus_res_id, 462);// 用于显示上次行驶结束后的里程数
  1253. if (Yii::$app->request->isPost) {
  1254. $res = Yii::$app->runAction('api/driver/run-start');
  1255. return $res;
  1256. }
  1257. return $this->render('simulation_bus', [
  1258. 'data' => $data
  1259. ]);
  1260. }
  1261. /**
  1262. * @Author wanglg
  1263. * @Desc 模拟司机端结束用车
  1264. * @param bus_number 出车单号
  1265. * @return int|mixed|string|\yii\console\Response
  1266. */
  1267. public function actionEndBus()
  1268. {
  1269. $bus_number = Yii::$app->request->get('bus_number');
  1270. // $run_bus_expenses = new BusExpense();
  1271. $data = RunBusExpenses::find()->select('start_value')->where(['bus_number' => $bus_number, 'cancel_flag' => 0, 'expense_subject_id' => 462])->asArray()->one(); //用于显示上次行驶结束后的里程数
  1272. if (Yii::$app->request->isPost) {
  1273. $res = Yii::$app->runAction('api/driver/run-end');
  1274. return $res;
  1275. }
  1276. return $this->render('simulation_end', [
  1277. 'data' => $data
  1278. ]);
  1279. }
  1280. }