|
- <?php
-
- namespace backend\modules\api\models;
-
- use backend\modules\motorcade\models\BusDocLog;
- use backend\modules\motorcade\models\BusOilDoc;
- use common\models\zModel;
- use yii\data\ActiveDataProvider;
- use Yii;
- use yii\base\Exception;
-
- /**
- * This is the model class for table "bus_expenses".
- *
- * @property integer $ID
- * @property integer $create_user_id
- * @property string $create_time
- * @property integer $update_user_id
- * @property string $update_time
- * @property integer $cancel_flag
- * @property integer bus_id
- * @property integer $expense_subject_id
- * @property integer $expense_type
- * @property string $expense_price
- * @property string $receive_value
- * @property string $expense_destination
- * @property string $start_value
- * @property string $end_value
- */
- class BusExpenses extends zModel
- {
- public $card_no;
- public $start_date;
- public $end_date;
- public $record_type;
- public $cost_type;
- public $bind_bus;
-
- public function load($post, $formName = null)
- {
- $this->card_no = isset($post['card_no']) ? $post['card_no'] : '';
- $this->start_date = isset($post['start_date']) ? $post['start_date'] : date('Y-m-01');
- $this->end_date = isset($post['end_date']) ? $post['end_date'] : date('Y-m-t');
- $this->bind_bus = isset($post['bind_bus']) ? $post['bind_bus'] : '';
- $this->record_type = isset($post['record_type']) ? $post['record_type'] : '';
- $this->cost_type = isset($post['cost_type']) ? $post['cost_type'] : '';
- return parent::load($post);
- }
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return 'bus_expenses';
- }
-
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['create_user_id', 'create_time', 'update_user_id', 'cancel_flag', 'expense_destination'], 'required'],
- [['create_user_id', 'update_user_id', 'cancel_flag', 'bus_id', 'expense_subject_id', 'expense_type'], 'integer'],
- [['update_time'], 'safe'],
- [['expense_price', 'start_value', 'end_value', 'doc_id'], 'number'],
- [['create_time', 'receive_value', 'expense_destination'], 'string', 'max' => 255],
- ];
- }
-
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'ID' => 'ID',
- 'create_user_id' => 'Create User ID',
- 'create_time' => 'Create Time',
- 'update_user_id' => 'Update User ID',
- 'update_time' => 'Update Time',
- 'cancel_flag' => 'Cancel Flag',
- 'bus_id' => '车辆ID',
- 'expense_subject_id' => '支出科目(如:加油,充电) 对应dict_type.id 479加油 480 充电',
- 'expense_type' => '支出方式 对应dict_type.id',
- 'expense_price' => '支付金额',
- 'receive_value' => '量(如油量、电量)',
- 'expense_destination' => '支出去向',
- 'start_value' => '记录车辆节点开始时状态属性值(如:加油前里程数)',
- 'end_value' => '记录车辆节点开始时状态属性值',
- ];
- }
-
- public function getbase_bus()
- {
- return $this->hasOne(BaseBus::className(), ['BUS_ID' => 'bus_id']);
- }
-
- public function getdict_type()
- {
- return $this->hasOne(DictType::className(), ['ID' => 'expense_type']);
- }
-
- public function getbus_doc_log()
- {
- return $this->hasOne(BusDocLog::className(), ['ID' => 'doc_id']);
- }
-
- public function getbase_driver()
- {
- return $this->hasOne(BaseDriver::className(), ['DRIVER_ID' => 'create_user_id']);
- }
-
-
-
- public function getList()
- {
- $query = BusExpenses::find();
- $query->innerJoinWith(BaseBus::tableName());
- $query->innerJoinWith([BusDocLog::tableName()]);
- $query->leftJoin(BusOilDoc::tableName(),'bus_doc_log.CARD_ID=bus_oil_doc.ID');
- $query->andFilterWhere(['bus_expenses.cancel_flag' => 0]);
- $query->andFilterWhere(['between', 'bus_doc_log.OPERATION_DATE', $this->start_date, $this->end_date]);
- $query->andFilterWhere(['like', 'CARD_NO', $this->card_no]);
- $query->andFilterWhere(['bus_oil_doc.MAIN_CORP_ID'=> $this->login_user->MAIN_CORP_ID2]);
- $query->andFilterWhere(['bus_doc_log.cancel_flag'=> 0]);
- $query->orderBy('OPERATION_DATE desc');
- if ($this->bind_bus !== '') {
- $query->andFilterWhere(['base_bus.BUS_ID' => $this->bind_bus]);
- }
-
- $dataProvider = new ActiveDataProvider([
- 'query' => $query,
- 'pagination' => [
- 'pagesize' => 15,
- ],
- 'sort' => false
- // 'sort' => [
- // 'defaultOrder' =>
- // [
- // 'bus_doc_log.OPERATION_DATE' => SORT_DESC,
- // ],
- // 'attributes' => [
- // 'base_bus.BUS_NO',
- // 'bus_doc_log.OPERATION_DATE',
- // 'receive_value',
- // 'expense_price',
- // 'start_value',
- // 'dict_type.TYPE_NAME',
- // 'bus_doc_log.bus_oil_doc.CARD_NO',
- // 'expense_destination',
- // 'base_driver.DRIVER_NAME',
- // ]
- // ]
- ]);
- return $dataProvider;
- }
-
- /**
- * Function Description:加油
- * Function Name: addOil
- * @param int $driver_id 司机id
- * @param int $bus_id 巴士id
- * @param string $expense_destination 支出去向
- * @param double $start_value 开始里程
- * @param double $receive_value 油量
- * @param double $expense_price 支付金额
- *
- * @return bool
- *
- * @author 张帅
- */
- public function addOil($driver_id, $bus_id, $expense_destination, $start_value, $receive_value, $expense_price)
- {
- $this->writeLog( "↓↓↓↓↓开始加油处理:".$driver_id."|".$bus_id."|".$expense_destination."|".$start_value."|".$receive_value."|".$expense_price );
- #region 1.修改数据
- $transaction = Yii::$app->db->beginTransaction();
- try {
- //(1)补充上次加油的残缺数据
- $pre_oil = self::find()->where(['bus_id' => $bus_id, 'cancel_flag' => 0, 'expense_subject_id' => 479])->orderBy(['id' => SORT_DESC])->asArray()->one();
- if (count($pre_oil) > 0) {
- $pre_oil = self::findOne(['ID' => $pre_oil['ID']]);
- $pre_oil->attributes = ['update_user_id' => $driver_id, 'update_time' => date('Y-m-d H:i:s', time()), 'end_value' => $start_value];
- $res = $pre_oil->update();
- if (!$res) {
- $this->writeLog( "补充上次加油的残缺数据失败:".$pre_oil['ID']."|".$driver_id."|".$start_value );
- throw new Exception('输入数据不符合格式');
- }
- }
-
- //(2插入bus_doc_log)
- $bus_oil_doc_info = BusOilDoc::findOne( ['BIND_BUS' => $bus_id, 'CARD_TYPE' => 1] );
- if( false != $bus_oil_doc_info && isset($bus_oil_doc_info["ID"]) ) {
- $card_balance = $bus_oil_doc_info["CARD_BALANCE"] - $expense_price;
- $bus_doc_log = new BusDocLog();
- $bus_doc_log->attributes = [
- 'CREATE_USER_ID' => $driver_id,
- 'CREATE_TIME' => date("Y-m-d H:i:s"),
- 'UPDATE_USER_ID' => $driver_id,
- 'UPDATE_TIME' => date("Y-m-d H:i:s"),
- 'CARD_ID' => $bus_oil_doc_info["ID"],
- 'BUS_ID' => $bus_oil_doc_info['BIND_BUS'],
- 'OPERATION_TYPE' => 601,
- 'OPERATION_DRIVER' => $driver_id,
- 'OPERATION_DATE' => date("Y-m-d"),
- 'VALUE' => $expense_price,
- 'VALUE_OLD' => $bus_oil_doc_info["CARD_BALANCE"],
- 'NOTE' => "APP司机加油报账"
- ];
- $bus_doc_log->save();
- $doc_log_id = $bus_doc_log->ID;
-
- if( $expense_price > 0 ) {
- $bus_oil_doc_info->attributes = ['CARD_BALANCE' => $card_balance, 'UPDATE_TIME' => date('Y-m-d H:i:s', time()), 'UPDATE_USER_ID' => $driver_id];
- $bus_oil_doc_info->update();
- }
- } else {
- $doc_log_id = 0;
- }
-
-
- //(3)录入加油数据
- $value = [
- 'create_user_id' => $driver_id,
- 'create_time' => date('Y-m-d H:i:s', time()),
- 'update_user_id' => $driver_id,
- 'update_time' => date('Y-m-d H:i:s', time()),
- 'cancel_flag' => 0,
- 'bus_id' => $bus_id,
- 'expense_subject_id' => 479,
- 'expense_type' => 483,
- 'expense_price' => $expense_price,
- 'receive_value' => $receive_value,
- 'expense_destination' => $expense_destination,
- 'start_value' => $start_value,
- 'doc_id' => $doc_log_id,
- ];
- $this->attributes = $value;
- $res = $this->insert();
- if (!$res) {
- $this->writeLog( "录入加油数据失败:".$driver_id."|".$bus_id."|".$expense_price."|".$receive_value."|".$expense_destination."|".$start_value."|".$doc_log_id );
- throw new Exception('输入数据不符合格式');
- }
-
- //(4.更新油价)
- $bus_one = BaseBus::findOne(['bus_id' => $bus_id]);
- $new_oil_price = round(($expense_price / $receive_value), 2);
- $bus_one->attributes = ['OIL_PRICE' => $new_oil_price];
- $res = $bus_one->update();
- if ( $res === false ) {
- $this->writeLog( "更新油价失败:".$expense_price."/".$receive_value."=".$new_oil_price );
- throw new Exception('输入数据不符合格式');
- }
-
- $transaction->commit();
- $result = true;
- } catch (Exception $e) {
- # 回滚事务
- $result = false;
- }
- if( $result ) {
- $this->writeLog( "↑↑↑↑↑开始加油处理成功:".$driver_id."|".$bus_id."|".$expense_destination."|".$start_value."|".$receive_value."|".$expense_price."|".$doc_log_id );
- } else {
- $this->writeLog( "↑↑↑↑↑加油处理失败:".$driver_id."|".$bus_id."|".$expense_destination."|".$start_value."|".$receive_value."|".$expense_price."|".$doc_log_id );
- }
- #endregion
- return $result;
- #endregion
- }
-
- protected function writeLog($string)
- {
- $string = date('Y-m-d H:i:s') . PHP_EOL . $string . PHP_EOL;
- if (!file_exists(__DIR__ . '/../../../runtime/logs/DriveApp')) {
- mkdir(__DIR__ . '/../../../runtime/logs/DriveApp', 0777, true);
- }
- file_put_contents(__DIR__ . '/../../../runtime/logs/DriveApp/' . date('Y-m-d') . '.log', $string, FILE_APPEND);
- }
-
- }
|