|
- <?php
- /**
- * Created by PhpStorm.
- * User: wangxj
- * Date: 2017/6/22
- * Time: 19:31
- */
-
- namespace backend\modules\motorcade\controllers;
-
- use backend\common\Utils;
- use backend\modules\motorcade\components\BaseComponent;
- use backend\modules\motorcade\components\LogComponent;
- use backend\modules\motorcade\models\BusOrder;
- use backend\modules\motorcade\models\BusOrderStatusLog;
- use Yii;
- use yii\db\Exception;
- use yii\web\Controller;
- use backend\modules\motorcade\models\BusProtocolFee;
- use backend\modules\motorcade\models\BaseBusProtocol;
-
- /* @property integer $user_id */
- class AutoController extends Controller
- {
- private $user_id = 1; //定时任务的user_id
-
-
- /**
- * 根据挂靠协议,定时生成费用期数据
- */
- public function actionAutoCreateProtocol()
- {
- $protocols = BaseBusProtocol::find()
- ->where(['CANCEL_FLAG' => 0,])
- ->andWhere("'" . date('Y-m-d') . "' between DATE_FROM and DATE_TO")
- ->groupBy(['BUS_ID'])
- ->orderBy('DATE_TO DESC')
- ->all();
- if (!empty($protocols)) {
- $date = date('Y-m');
- $tran = Yii::$app->db->beginTransaction();
- foreach ($protocols as $pro) {
- /* @var $pro \backend\modules\motorcade\models\BaseBusProtocol */
- //每一个协议,生成一个挂靠费用,先查看是对应车辆否已经有协议对应生成的费用了
- $oldFee = BusProtocolFee::findOne(['CANCEL_FLAG' => 0, 'BUS_ID' => $pro->BUS_ID, 'RUN_DATE' => $date]);
- if ($oldFee === null) {
- $fee = new BusProtocolFee();
- $fee->CREATE_USER_ID = $this->user_id;
- $fee->UPDATE_USER_ID = $this->user_id;
- $fee->BUS_ID = $pro->BUS_ID;
- $fee->COMPANY_NAME = $pro->COMPANY_NAME;
- $fee->RUN_DATE = $date;
- $fee->PAY_TYPE = $pro->PAY_TYPE;
- $fee->PAY_PRICE = $pro->PAY_PRICE;
- $fee->save();
- }
- }
- $tran->commit();
- }
- }
-
- /**
- * 出车任务如果是外部车辆,司机没有用App,自动完成出车流程。进入报账审核流程
- * 影响收车日期为前一天的出车任务,
- * @return string
- */
- public function actionAutoFinishBusOrder($startDate = '', $endDate = '')
- {
- $startDate = $startDate === '' ? date('Y-m-d', strtotime('yesterday')) : $startDate;
- $endDate = $endDate === '' ? date('Y-m-d') : $endDate;
- $day_list = Utils::getRangeDate($startDate, $endDate);
- foreach ($day_list as $date) {
- $busOrders = BusOrder::findAll(['cancel_flag' => 0, 'finance_status' => BusOrder::STATUS_FINANCE_NOT, //未报账
- 'send_type' => 1, //外部派车
- 'date_add(bus_order.run_date,interval day_num - 1 day)' => $date]); //收车日期为昨天
- $failed = [];
- if (!empty($busOrders)) {
- foreach ($busOrders as $order) {
- $tran = Yii::$app->db->beginTransaction();
- try {
- /* @var $order \backend\modules\motorcade\models\BusOrder */
- $order->run_status = BusOrder::STATUS_RUN_FINISH;
- $order->finance_status = BusOrder::STATUS_FINANCE_WAITING;
- $order->update_user_id = $this->user_id;
- //操作日志
- LogComponent::addLog($order->bus_number,
- LogComponent::LOG_TYPE_FO,
- '自动完成出车',
- 'run_status',
- $order->run_status,
- $order->getOldAttribute('run_status'),
- $this->user_id);
- //状态日志
- BaseComponent::addStatusLog($order->bus_number,
- BusOrderStatusLog::$_type_array['RUN_STATUS'],
- $order->getOldAttribute('run_status'),
- $order->run_status,
- '自动完成出车',
- '外部用车自动完成出车',
- $this->user_id);
- if ($order->save()) {
- $tran->commit();
- } else {
- $tran->rollBack();
- $failed[] = $order->bus_number . ' ' . \GuzzleHttp\json_encode($order->getFirstErrors());
- }
- } catch (Exception $e) {
- $tran->rollBack();
- $failed[] = $order->bus_number . ' ' . $e->getMessage();
- }
- }
- }
- }
-
- if (!empty($failed)) {
- return '外部派车结束任务失败:' . implode(',', $failed);
- } else {
- return '';
- }
- }
- }
|