|
- <?php
-
- namespace backend\modules\api\models;
-
- use yii\db\ActiveRecord;
- use Yii;
-
- /**
- * This is the model class for table "bus_cost".
- *
- * @property integer $ID
- * @property integer $CANCEL_FLAG
- * @property integer $CREATE_USER_ID
- * @property string $CREATE_TIME
- * @property integer $UPDATE_USER_ID
- * @property string $UPDATE_TIME
- * @property integer $RUN_ID
- * @property integer $BUS_ORDER_ID
- * @property integer $COST_MOTORCADE_ID
- * @property integer $BUS_ORG_ID
- * @property integer $BUS_ID
- * @property integer $DRIVER_ID
- * @property integer $GUIDE_ID
- * @property integer $SEAT_COUNT
- * @property integer $SALED_COUNT
- * @property integer $REAL_TOTAL_COUNT
- * @property integer $COST_TYPE
- * @property string $COST_PRICE
- * @property string $TOTAL_COST_PRICE
- * @property integer $CHECK_STATUS
- * @property string $START_TIME
- * @property integer $LINE_ID
- * @property integer $RUN_BUS_ID
- * @property integer $CONFIRM_USER_ID
- * @property string $CONFIRM_TIME
- */
- class BusCost extends ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return 'bus_cost';
- }
-
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['CANCEL_FLAG', 'CREATE_USER_ID', 'UPDATE_USER_ID', 'RUN_ID', 'BUS_ORDER_ID', 'COST_MOTORCADE_ID', 'BUS_ORG_ID', 'BUS_ID', 'DRIVER_ID', 'GUIDE_ID', 'SEAT_COUNT', 'SALED_COUNT', 'REAL_TOTAL_COUNT', 'COST_TYPE', 'CHECK_STATUS', 'LINE_ID', 'RUN_BUS_ID', 'CONFIRM_USER_ID'], 'integer'],
- [['COST_PRICE', 'TOTAL_COST_PRICE'], 'number'],
- [['MEMO'], 'string'],
- [['CREATE_TIME', 'UPDATE_TIME', 'CONFIRM_TIME'], 'string', 'max' => 20],
- [['START_TIME'], 'string', 'max' => 17],
- ];
- }
-
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'ID' => 'ID',
- 'CANCEL_FLAG' => 'Cancel Flag',
- 'CREATE_USER_ID' => '创建人',
- 'CREATE_TIME' => 'Create Time',
- 'UPDATE_USER_ID' => '审核确认人',
- 'UPDATE_TIME' => 'Update Time',
- 'RUN_ID' => '班次id',
- 'BUS_ORDER_ID' => '车次号',
- 'COST_MOTORCADE_ID' => '成本id车队 对应suplier_id',
- 'BUS_ORG_ID' => '车次渠道id',
- 'BUS_ID' => '车辆id',
- 'DRIVER_ID' => '司机id',
- 'GUIDE_ID' => '导游id',
- 'SEAT_COUNT' => '座位数',
- 'SALED_COUNT' => '已售座位数',
- 'REAL_TOTAL_COUNT' => '最终实际上车人数(该字段主要用于司机端)',
- 'COST_TYPE' => '结算类型 对应dict_type.id',
- 'COST_PRICE' => '成本单价',
- 'TOTAL_COST_PRICE' => '成本总价',
- 'CHECK_STATUS' => '审核状态',
- 'START_TIME' => '出发时间',
- 'LINE_ID' => '线路id',
- 'MEMO' => '备忘',
- 'RUN_BUS_ID' => '班次车次唯一id',
- 'CONFIRM_USER_ID' => '审核人id',
- 'CONFIRM_TIME' => '审核时间',
- ];
- }
-
- /**
- * Function Description:自动添加成本信息
- * Function Name: addCostByRunBus
- * @param array $run_bus_list 班次车次列表
- *
- * @return int
- *
- * @author 张帅
- */
- public function addCostByRunBus($run_bus_list)
- {
- //获取所有的线路最长里程
- $all_line_distance = $this->getLineDistance();
- //获取所有成本基数
- $all_base_cost = $this->getBaseBusCost();
- $current_time = date('Y-m-d H:i:s');
- #region 1.整理要插入的成本数据
- foreach ($run_bus_list as $key => $vel) {
- $vel += [
- 'CANCEL_FLAG' => 0,
- 'CREATE_USER_ID' => 2,
- 'CREATE_TIME' => $current_time,
- 'UPDATE_USER_ID' => 2,
- 'UPDATE_TIME' => $current_time,
- ];
- $vel['START_TIME'] = $vel['RUN_DATE'] . ' ' . $vel['RUN_TIME'];
- if ($vel['COST_TYPE'] == 478) {
- $vel['TOTAL_COST_PRICE'] = $vel['COST_PRICE'] * $vel['SALED_COUNT'];
- } else {
- // $line_distance = isset($all_line_distance[$vel['LINE_ID']]) ? $all_line_distance[$vel['LINE_ID']] : 0;
- // $base_cost = ( isset($all_base_cost[$vel['BUS_ORG_ID']]) && isset($all_base_cost[$vel['BUS_ORG_ID']][$vel['SEND_BUS_TYPE_RES_ID']]) ) ? $all_base_cost[$vel['BUS_ORG_ID']][$vel['SEND_BUS_TYPE_RES_ID']] : 0;
- // if( $line_distance == 0 || $base_cost == 0 ) {
- // $vel['TOTAL_COST_PRICE'] = $vel['COST_PRICE'];
- // } else {
- // $vel['COST_PRICE'] = round($line_distance * $base_cost);
- // $vel['TOTAL_COST_PRICE'] = round($line_distance * $base_cost);
- // }
- $vel['TOTAL_COST_PRICE'] = $vel['COST_PRICE'];
- }
- $vel['CHECK_STATUS'] = 444;
- $vel['CONFIRM_USER_ID'] = 0;
- $vel['CONFIRM_TIME'] = '';
- //穿梭车以按座结算及按座结算人数为0的默认审核通过
- if( ( $vel['LINE_TYPE'] == 256 && $vel['COST_TYPE'] == 478 ) || ( $vel['COST_TYPE'] == 478 && $vel['SALED_COUNT'] == 0 ) ) {
- $vel['CHECK_STATUS'] = 445;
- $vel['CONFIRM_USER_ID'] = 0;
- $vel['CONFIRM_TIME'] = '';
- }
-
- unset($vel['LINE_TYPE']);
- unset($vel['RUN_DATE']);
- unset($vel['RUN_TIME']);
- unset($vel['SEND_BUS_TYPE_RES_ID']);
- $run_bus_list[$key] = $vel;
- }
- #endregion
-
- #region 2.插入数据
- $result = Yii::$app->db->createCommand()->batchInsert(self::tableName(),
- ['RUN_ID', 'BUS_ORDER_ID', 'BUS_ID', 'BUS_ORG_ID', 'COST_MOTORCADE_ID', 'DRIVER_ID', 'GUIDE_ID', 'SEAT_COUNT', 'SALED_COUNT', 'REAL_TOTAL_COUNT', 'COST_TYPE', 'COST_PRICE', 'LINE_ID', 'RUN_BUS_ID', 'CANCEL_FLAG', 'CREATE_USER_ID', 'CREATE_TIME', 'UPDATE_USER_ID', 'UPDATE_TIME', 'START_TIME', 'TOTAL_COST_PRICE', 'CHECK_STATUS', 'CONFIRM_USER_ID', 'CONFIRM_TIME'],
- $run_bus_list)->execute();
- #endregion
-
-
- return $result;
- }
-
- /**
- * Function Description:根据班次车次获取成本总计及实际人数
- * Function Name: getBusCostCount
- *
- * @return array|bool|\yii\db\ActiveRecord[]
- *
- * @author 张帅
- */
- public function getBusCostCount()
- {
- #region 获取最近修改过的成本记录
- $run_bus_arr = self::find()
- ->select([
- 'run_bus_id',//班次车次id
- ])
- ->from(self::tableName() . ' as c')
- ->where([
- 'and',
- ['>=', "SUBSTRING(update_time,1,10)", date('Y-m-d', strtotime("-10 day"))],
- ['<=', "SUBSTRING(update_time,1,10)", date('Y-m-d')],
- ])
- ->groupBy('run_bus_id')
- ->asArray()->all();
- #endregion
-
- if (count($run_bus_arr) == 0) {
- return false;
- }
-
- $run_bus_id_arr = [];
-
- foreach ($run_bus_arr as $key => $vel) {
- $run_bus_id_arr[] = $vel['run_bus_id'];
- }
-
- #region 获取成本总价及实际车座数
- $bus_cost_list = self::find()
- ->select([
- 'run_bus_id' => 'c.run_bus_id',//班次车次id
- 'total_base_price' => "sum(c.total_cost_price)",//总成本
- 'run_id' => 'c.run_id',//班次id
- 'bus_order_id' => 'c.bus_order_id',//车次id
- 'cost_check_status' => "min(check_status)",//总成本
- 'total_count' => RunBus::find()->select('saled_count')->where('id = c.run_bus_id')->limit(1),
- ])
- ->from(self::tableName() . ' as c')
- // ->leftJoin("opera_line as ol", " ol.line_id=c.line_id ")
- ->where([
- 'and',
- ['in', 'run_bus_id', $run_bus_id_arr],
- ])
- ->groupBy('run_bus_id')
- ->asArray()->all();
- #endregion
-
- if (count($bus_cost_list) == 0) {
- return false;
- }
-
- #region 获取成本单价
- $result = [];
- foreach ($bus_cost_list as $key => $vel) {
- $vel['total_count'] == 0 ? $vel['total_count'] = 1 : true;
- $result[$vel['run_id'] . '-' . $vel['bus_order_id']] = $vel;
- //未审核的成本不计入
- //if( $vel['cost_check_status'] >= 445 ) {
- $result[$vel['run_id'] . '-' . $vel['bus_order_id']]['base_price'] = round($vel['total_base_price'] / $vel['total_count'], 2);
- //} else {
- // $result[$vel['run_id'] . '-' . $vel['bus_order_id']]['base_price'] = 0;
- //}
- }
- #endregion
- return $result;
- }
-
- /**
- * Function Description:获取所有线路总里程
- * Function Name: getLineDistance
- *
- * @return array|bool|\yii\db\ActiveRecord[]
- *
- * @author Redstop
- */
- public function getLineDistance() {
- $row = self::find()->select("line_id,sum(distance) as all_distance")->from("opera_station")->where("cancel_flag=0")->groupBy("line_id")->asArray()->all();
- if( $row == false ) {
- return false;
- }
- $return_array = array();
- foreach( $row as $row_info ) {
- $row_line_id = $row_info["line_id"];
- $return_array[$row_line_id] = $row_info["all_distance"];
- }
- return $return_array;
- }
-
- /**
- * Function Description:获取所有默认成本基准
- * Function Name: getLineDistance
- *
- * @return array|bool|\yii\db\ActiveRecord[]
- *
- * @author Redstop
- */
- public function getBaseBusCost() {
- $row = self::find()->select("supplier_id,bus_res_id,base_cost")->from("base_bus_cost")->where("id>0")->asArray()->all();
- if( $row == false ) {
- return false;
- }
- $return_array = array();
- foreach( $row as $row_info ) {
- $supplier_id = $row_info["supplier_id"];
- $bus_res_id = $row_info["bus_res_id"];
- if( !isset($return_array[$supplier_id]) ) {
- $return_array[$supplier_id] = array();
- }
- $return_array[$supplier_id][$bus_res_id] = $row_info["base_cost"];
- }
- return $return_array;
- }
-
-
- }
|