You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

286 line
10 KiB

  1. <?php
  2. namespace backend\modules\api\models;
  3. use yii\db\ActiveRecord;
  4. use Yii;
  5. /**
  6. * This is the model class for table "bus_cost".
  7. *
  8. * @property integer $ID
  9. * @property integer $CANCEL_FLAG
  10. * @property integer $CREATE_USER_ID
  11. * @property string $CREATE_TIME
  12. * @property integer $UPDATE_USER_ID
  13. * @property string $UPDATE_TIME
  14. * @property integer $RUN_ID
  15. * @property integer $BUS_ORDER_ID
  16. * @property integer $COST_MOTORCADE_ID
  17. * @property integer $BUS_ORG_ID
  18. * @property integer $BUS_ID
  19. * @property integer $DRIVER_ID
  20. * @property integer $GUIDE_ID
  21. * @property integer $SEAT_COUNT
  22. * @property integer $SALED_COUNT
  23. * @property integer $REAL_TOTAL_COUNT
  24. * @property integer $COST_TYPE
  25. * @property string $COST_PRICE
  26. * @property string $TOTAL_COST_PRICE
  27. * @property integer $CHECK_STATUS
  28. * @property string $START_TIME
  29. * @property integer $LINE_ID
  30. * @property integer $RUN_BUS_ID
  31. * @property integer $CONFIRM_USER_ID
  32. * @property string $CONFIRM_TIME
  33. */
  34. class BusCost extends ActiveRecord
  35. {
  36. /**
  37. * @inheritdoc
  38. */
  39. public static function tableName()
  40. {
  41. return 'bus_cost';
  42. }
  43. /**
  44. * @inheritdoc
  45. */
  46. public function rules()
  47. {
  48. return [
  49. [['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'],
  50. [['COST_PRICE', 'TOTAL_COST_PRICE'], 'number'],
  51. [['MEMO'], 'string'],
  52. [['CREATE_TIME', 'UPDATE_TIME', 'CONFIRM_TIME'], 'string', 'max' => 20],
  53. [['START_TIME'], 'string', 'max' => 17],
  54. ];
  55. }
  56. /**
  57. * @inheritdoc
  58. */
  59. public function attributeLabels()
  60. {
  61. return [
  62. 'ID' => 'ID',
  63. 'CANCEL_FLAG' => 'Cancel Flag',
  64. 'CREATE_USER_ID' => '创建人',
  65. 'CREATE_TIME' => 'Create Time',
  66. 'UPDATE_USER_ID' => '审核确认人',
  67. 'UPDATE_TIME' => 'Update Time',
  68. 'RUN_ID' => '班次id',
  69. 'BUS_ORDER_ID' => '车次号',
  70. 'COST_MOTORCADE_ID' => '成本id车队 对应suplier_id',
  71. 'BUS_ORG_ID' => '车次渠道id',
  72. 'BUS_ID' => '车辆id',
  73. 'DRIVER_ID' => '司机id',
  74. 'GUIDE_ID' => '导游id',
  75. 'SEAT_COUNT' => '座位数',
  76. 'SALED_COUNT' => '已售座位数',
  77. 'REAL_TOTAL_COUNT' => '最终实际上车人数(该字段主要用于司机端)',
  78. 'COST_TYPE' => '结算类型 对应dict_type.id',
  79. 'COST_PRICE' => '成本单价',
  80. 'TOTAL_COST_PRICE' => '成本总价',
  81. 'CHECK_STATUS' => '审核状态',
  82. 'START_TIME' => '出发时间',
  83. 'LINE_ID' => '线路id',
  84. 'MEMO' => '备忘',
  85. 'RUN_BUS_ID' => '班次车次唯一id',
  86. 'CONFIRM_USER_ID' => '审核人id',
  87. 'CONFIRM_TIME' => '审核时间',
  88. ];
  89. }
  90. /**
  91. * Function Description:自动添加成本信息
  92. * Function Name: addCostByRunBus
  93. * @param array $run_bus_list 班次车次列表
  94. *
  95. * @return int
  96. *
  97. * @author 张帅
  98. */
  99. public function addCostByRunBus($run_bus_list)
  100. {
  101. //获取所有的线路最长里程
  102. $all_line_distance = $this->getLineDistance();
  103. //获取所有成本基数
  104. $all_base_cost = $this->getBaseBusCost();
  105. $current_time = date('Y-m-d H:i:s');
  106. #region 1.整理要插入的成本数据
  107. foreach ($run_bus_list as $key => $vel) {
  108. $vel += [
  109. 'CANCEL_FLAG' => 0,
  110. 'CREATE_USER_ID' => 2,
  111. 'CREATE_TIME' => $current_time,
  112. 'UPDATE_USER_ID' => 2,
  113. 'UPDATE_TIME' => $current_time,
  114. ];
  115. $vel['START_TIME'] = $vel['RUN_DATE'] . ' ' . $vel['RUN_TIME'];
  116. if ($vel['COST_TYPE'] == 478) {
  117. $vel['TOTAL_COST_PRICE'] = $vel['COST_PRICE'] * $vel['SALED_COUNT'];
  118. } else {
  119. // $line_distance = isset($all_line_distance[$vel['LINE_ID']]) ? $all_line_distance[$vel['LINE_ID']] : 0;
  120. // $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;
  121. // if( $line_distance == 0 || $base_cost == 0 ) {
  122. // $vel['TOTAL_COST_PRICE'] = $vel['COST_PRICE'];
  123. // } else {
  124. // $vel['COST_PRICE'] = round($line_distance * $base_cost);
  125. // $vel['TOTAL_COST_PRICE'] = round($line_distance * $base_cost);
  126. // }
  127. $vel['TOTAL_COST_PRICE'] = $vel['COST_PRICE'];
  128. }
  129. $vel['CHECK_STATUS'] = 444;
  130. $vel['CONFIRM_USER_ID'] = 0;
  131. $vel['CONFIRM_TIME'] = '';
  132. //穿梭车以按座结算及按座结算人数为0的默认审核通过
  133. if( ( $vel['LINE_TYPE'] == 256 && $vel['COST_TYPE'] == 478 ) || ( $vel['COST_TYPE'] == 478 && $vel['SALED_COUNT'] == 0 ) ) {
  134. $vel['CHECK_STATUS'] = 445;
  135. $vel['CONFIRM_USER_ID'] = 0;
  136. $vel['CONFIRM_TIME'] = '';
  137. }
  138. unset($vel['LINE_TYPE']);
  139. unset($vel['RUN_DATE']);
  140. unset($vel['RUN_TIME']);
  141. unset($vel['SEND_BUS_TYPE_RES_ID']);
  142. $run_bus_list[$key] = $vel;
  143. }
  144. #endregion
  145. #region 2.插入数据
  146. $result = Yii::$app->db->createCommand()->batchInsert(self::tableName(),
  147. ['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'],
  148. $run_bus_list)->execute();
  149. #endregion
  150. return $result;
  151. }
  152. /**
  153. * Function Description:根据班次车次获取成本总计及实际人数
  154. * Function Name: getBusCostCount
  155. *
  156. * @return array|bool|\yii\db\ActiveRecord[]
  157. *
  158. * @author 张帅
  159. */
  160. public function getBusCostCount()
  161. {
  162. #region 获取最近修改过的成本记录
  163. $run_bus_arr = self::find()
  164. ->select([
  165. 'run_bus_id',//班次车次id
  166. ])
  167. ->from(self::tableName() . ' as c')
  168. ->where([
  169. 'and',
  170. ['>=', "SUBSTRING(update_time,1,10)", date('Y-m-d', strtotime("-10 day"))],
  171. ['<=', "SUBSTRING(update_time,1,10)", date('Y-m-d')],
  172. ])
  173. ->groupBy('run_bus_id')
  174. ->asArray()->all();
  175. #endregion
  176. if (count($run_bus_arr) == 0) {
  177. return false;
  178. }
  179. $run_bus_id_arr = [];
  180. foreach ($run_bus_arr as $key => $vel) {
  181. $run_bus_id_arr[] = $vel['run_bus_id'];
  182. }
  183. #region 获取成本总价及实际车座数
  184. $bus_cost_list = self::find()
  185. ->select([
  186. 'run_bus_id' => 'c.run_bus_id',//班次车次id
  187. 'total_base_price' => "sum(c.total_cost_price)",//总成本
  188. 'run_id' => 'c.run_id',//班次id
  189. 'bus_order_id' => 'c.bus_order_id',//车次id
  190. 'cost_check_status' => "min(check_status)",//总成本
  191. 'total_count' => RunBus::find()->select('saled_count')->where('id = c.run_bus_id')->limit(1),
  192. ])
  193. ->from(self::tableName() . ' as c')
  194. // ->leftJoin("opera_line as ol", " ol.line_id=c.line_id ")
  195. ->where([
  196. 'and',
  197. ['in', 'run_bus_id', $run_bus_id_arr],
  198. ])
  199. ->groupBy('run_bus_id')
  200. ->asArray()->all();
  201. #endregion
  202. if (count($bus_cost_list) == 0) {
  203. return false;
  204. }
  205. #region 获取成本单价
  206. $result = [];
  207. foreach ($bus_cost_list as $key => $vel) {
  208. $vel['total_count'] == 0 ? $vel['total_count'] = 1 : true;
  209. $result[$vel['run_id'] . '-' . $vel['bus_order_id']] = $vel;
  210. //未审核的成本不计入
  211. //if( $vel['cost_check_status'] >= 445 ) {
  212. $result[$vel['run_id'] . '-' . $vel['bus_order_id']]['base_price'] = round($vel['total_base_price'] / $vel['total_count'], 2);
  213. //} else {
  214. // $result[$vel['run_id'] . '-' . $vel['bus_order_id']]['base_price'] = 0;
  215. //}
  216. }
  217. #endregion
  218. return $result;
  219. }
  220. /**
  221. * Function Description:获取所有线路总里程
  222. * Function Name: getLineDistance
  223. *
  224. * @return array|bool|\yii\db\ActiveRecord[]
  225. *
  226. * @author Redstop
  227. */
  228. public function getLineDistance() {
  229. $row = self::find()->select("line_id,sum(distance) as all_distance")->from("opera_station")->where("cancel_flag=0")->groupBy("line_id")->asArray()->all();
  230. if( $row == false ) {
  231. return false;
  232. }
  233. $return_array = array();
  234. foreach( $row as $row_info ) {
  235. $row_line_id = $row_info["line_id"];
  236. $return_array[$row_line_id] = $row_info["all_distance"];
  237. }
  238. return $return_array;
  239. }
  240. /**
  241. * Function Description:获取所有默认成本基准
  242. * Function Name: getLineDistance
  243. *
  244. * @return array|bool|\yii\db\ActiveRecord[]
  245. *
  246. * @author Redstop
  247. */
  248. public function getBaseBusCost() {
  249. $row = self::find()->select("supplier_id,bus_res_id,base_cost")->from("base_bus_cost")->where("id>0")->asArray()->all();
  250. if( $row == false ) {
  251. return false;
  252. }
  253. $return_array = array();
  254. foreach( $row as $row_info ) {
  255. $supplier_id = $row_info["supplier_id"];
  256. $bus_res_id = $row_info["bus_res_id"];
  257. if( !isset($return_array[$supplier_id]) ) {
  258. $return_array[$supplier_id] = array();
  259. }
  260. $return_array[$supplier_id][$bus_res_id] = $row_info["base_cost"];
  261. }
  262. return $return_array;
  263. }
  264. }