Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

121 řádky
5.0 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wangxj
  5. * Date: 2017/6/22
  6. * Time: 19:31
  7. */
  8. namespace backend\modules\motorcade\controllers;
  9. use backend\common\Utils;
  10. use backend\modules\motorcade\components\BaseComponent;
  11. use backend\modules\motorcade\components\LogComponent;
  12. use backend\modules\motorcade\models\BusOrder;
  13. use backend\modules\motorcade\models\BusOrderStatusLog;
  14. use Yii;
  15. use yii\db\Exception;
  16. use yii\web\Controller;
  17. use backend\modules\motorcade\models\BusProtocolFee;
  18. use backend\modules\motorcade\models\BaseBusProtocol;
  19. /* @property integer $user_id */
  20. class AutoController extends Controller
  21. {
  22. private $user_id = 1; //定时任务的user_id
  23. /**
  24. * 根据挂靠协议,定时生成费用期数据
  25. */
  26. public function actionAutoCreateProtocol()
  27. {
  28. $protocols = BaseBusProtocol::find()
  29. ->where(['CANCEL_FLAG' => 0,])
  30. ->andWhere("'" . date('Y-m-d') . "' between DATE_FROM and DATE_TO")
  31. ->groupBy(['BUS_ID'])
  32. ->orderBy('DATE_TO DESC')
  33. ->all();
  34. if (!empty($protocols)) {
  35. $date = date('Y-m');
  36. $tran = Yii::$app->db->beginTransaction();
  37. foreach ($protocols as $pro) {
  38. /* @var $pro \backend\modules\motorcade\models\BaseBusProtocol */
  39. //每一个协议,生成一个挂靠费用,先查看是对应车辆否已经有协议对应生成的费用了
  40. $oldFee = BusProtocolFee::findOne(['CANCEL_FLAG' => 0, 'BUS_ID' => $pro->BUS_ID, 'RUN_DATE' => $date]);
  41. if ($oldFee === null) {
  42. $fee = new BusProtocolFee();
  43. $fee->CREATE_USER_ID = $this->user_id;
  44. $fee->UPDATE_USER_ID = $this->user_id;
  45. $fee->BUS_ID = $pro->BUS_ID;
  46. $fee->COMPANY_NAME = $pro->COMPANY_NAME;
  47. $fee->RUN_DATE = $date;
  48. $fee->PAY_TYPE = $pro->PAY_TYPE;
  49. $fee->PAY_PRICE = $pro->PAY_PRICE;
  50. $fee->save();
  51. }
  52. }
  53. $tran->commit();
  54. }
  55. }
  56. /**
  57. * 出车任务如果是外部车辆,司机没有用App,自动完成出车流程。进入报账审核流程
  58. * 影响收车日期为前一天的出车任务,
  59. * @return string
  60. */
  61. public function actionAutoFinishBusOrder($startDate = '', $endDate = '')
  62. {
  63. $startDate = $startDate === '' ? date('Y-m-d', strtotime('yesterday')) : $startDate;
  64. $endDate = $endDate === '' ? date('Y-m-d') : $endDate;
  65. $day_list = Utils::getRangeDate($startDate, $endDate);
  66. foreach ($day_list as $date) {
  67. $busOrders = BusOrder::findAll(['cancel_flag' => 0, 'finance_status' => BusOrder::STATUS_FINANCE_NOT, //未报账
  68. 'send_type' => 1, //外部派车
  69. 'date_add(bus_order.run_date,interval day_num - 1 day)' => $date]); //收车日期为昨天
  70. $failed = [];
  71. if (!empty($busOrders)) {
  72. foreach ($busOrders as $order) {
  73. $tran = Yii::$app->db->beginTransaction();
  74. try {
  75. /* @var $order \backend\modules\motorcade\models\BusOrder */
  76. $order->run_status = BusOrder::STATUS_RUN_FINISH;
  77. $order->finance_status = BusOrder::STATUS_FINANCE_WAITING;
  78. $order->update_user_id = $this->user_id;
  79. //操作日志
  80. LogComponent::addLog($order->bus_number,
  81. LogComponent::LOG_TYPE_FO,
  82. '自动完成出车',
  83. 'run_status',
  84. $order->run_status,
  85. $order->getOldAttribute('run_status'),
  86. $this->user_id);
  87. //状态日志
  88. BaseComponent::addStatusLog($order->bus_number,
  89. BusOrderStatusLog::$_type_array['RUN_STATUS'],
  90. $order->getOldAttribute('run_status'),
  91. $order->run_status,
  92. '自动完成出车',
  93. '外部用车自动完成出车',
  94. $this->user_id);
  95. if ($order->save()) {
  96. $tran->commit();
  97. } else {
  98. $tran->rollBack();
  99. $failed[] = $order->bus_number . ' ' . \GuzzleHttp\json_encode($order->getFirstErrors());
  100. }
  101. } catch (Exception $e) {
  102. $tran->rollBack();
  103. $failed[] = $order->bus_number . ' ' . $e->getMessage();
  104. }
  105. }
  106. }
  107. }
  108. if (!empty($failed)) {
  109. return '外部派车结束任务失败:' . implode(',', $failed);
  110. } else {
  111. return '';
  112. }
  113. }
  114. }