您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

409 行
18 KiB

  1. <?php
  2. namespace backend\modules\motorcade\models;
  3. use Yii;
  4. use yii\base\Model;
  5. use yii\data\ActiveDataProvider;
  6. use yii\db\Expression;
  7. /**
  8. * searchOrder represents the model behind the search form about `backend\modules\motorcade\models\BusOrder`.
  9. *
  10. * @property integer $end_status_name
  11. * @property integer $depart_name
  12. * @property integer $date_from
  13. * @property integer $date_to
  14. * @property integer $bus_ids 所有出车单号
  15. * @property integer $bus_order_count 出车数量
  16. * @property integer $bus_supplier_id
  17. *
  18. */
  19. class searchOrder extends BusOrder
  20. {
  21. const DATE_TYPE_RUN = 1; //出车日期
  22. const DATE_TYPE_END = 2; //收车日期
  23. public static $date_type_array = [self::DATE_TYPE_RUN => '出车日期', self::DATE_TYPE_END => '收车日期']; //查询日期类型
  24. public static $client_type_array = ['' => '类型', self::CLIENT_TYPE_COMPANY => '企业', self::CLIENT_TYPE_PERSONAL => '个人'];
  25. public static $line_type_array = [self::LINE_TYPE_PROVINCE => '省际', self::LINE_TYPE_CITY => '市内'];
  26. public static $finance_type_array = [
  27. '' => '请选择',
  28. self::FINANCE_LOCK_OPEN => '未关账',
  29. self::FINANCE_LOCK_LOCKED => '已关账',
  30. // self::STATUS_END_NOTHING => '未结算',
  31. self::STATUS_END_WAITING_CONFIRM => '待核对',
  32. self::STATUS_END_WAITING_FINISH => '待结算',
  33. self::STATUS_END_FINISH => '已结算'];
  34. public $client_type; //查询客户类型
  35. public $date_from; //查询日期 起始日期
  36. public $date_to; //查询日期 结束日期
  37. public $client_id; //查询客户ID
  38. public $date_end; //收车日期
  39. public $pay_type_name; //支付方式
  40. public $end_status_name;//结算状态
  41. public $depart_name;
  42. public $bus_supplier_id; //车辆车队
  43. public $bus_cost_id;
  44. public $bus_ids;
  45. public $bus_order_count;
  46. // public $bus_order_finance_status; //查询订单的财务结算关账状态
  47. /**
  48. * @inheritdoc
  49. */
  50. public function rules()
  51. {
  52. return [
  53. [['date_type', 'date_from', 'date_to', 'client_type', 'use_bus_org_id', 'date_type', 'send_type', 'bus_order_outcome_finance',
  54. 'run_bus_id', 'end_status', 'outcome_end_status', 'bus_number', 'date_end','bus_cost_id', 'bus_order_finance', 'bus_supplier_id',
  55. 'send_cost'], 'safe']
  56. ];
  57. }
  58. /**
  59. * @inheritdoc
  60. */
  61. public function scenarios()
  62. {
  63. // bypass scenarios() implementation in the parent class
  64. $scenarios = Model::scenarios();
  65. //开关账
  66. $scenarios['condom'] = ['finance_lock'];
  67. //对账单的操作:核对确认、完成结算、取消对账
  68. $scenarios['end_status'] = ['end_status'];
  69. return $scenarios;
  70. }
  71. /**
  72. * 查询应收、应付订单
  73. *
  74. * @param array $params
  75. *
  76. * @return ActiveDataProvider
  77. */
  78. public function search($params)
  79. {
  80. $query = searchOrder::find();
  81. // add conditions that should always apply here
  82. $dataProvider = new ActiveDataProvider([
  83. 'query' => $query,
  84. 'pagination' => [
  85. 'pagesize' => 15
  86. ],
  87. 'sort' => false,
  88. ]);
  89. $this->load($params);
  90. $query->orderBy('bus_order.run_date desc, bus_order.bus_number');
  91. $query->joinWith('user')->joinWith('department.settleType');
  92. if ($this->send_type == 1) {
  93. $query->joinWith('outcomeEndType');
  94. } else {
  95. $query->joinWith('endType');
  96. }
  97. $query->joinWith('expense');
  98. // if($this->send_type){
  99. $query->joinWith('bus')->joinWith('bus.org');
  100. // }
  101. $query->leftJoin('bus_cost','bus_order.run_bus_id=bus_cost.RUN_BUS_ID AND bus_cost.CANCEL_FLAG=0');
  102. $query->addSelect([
  103. "bus_order.id", "bus_order.run_bus_id", "bus_order.bus_number", "bus_order.create_time", "itinerary_name", "end_status", "finance_lock",
  104. "outcome_end_status",
  105. "bus_order.run_date", "if(bus_cost_type = " . searchOrder::BUS_COST_TYPE_ALL . ",bus_cost,bus_order.saled_count*bus_cost) as bus_cost", "bus_cost_type", "bus_order.saled_count",
  106. "settleType.TYPE_NAME as pay_type_name",
  107. ($this->send_type ? "oet.TYPE_NAME" : "et.TYPE_NAME") . " as end_status_name",
  108. "date_add(
  109. bus_order.run_date,
  110. INTERVAL day_num - 1 DAY
  111. ) as date_end",
  112. ($this->send_type ? "SUPPLIER_NAME" : "depart_name") . ' as depart_name',
  113. 'base_bus.ORG_ID as bus_supplier_id','base_bus.bus_no',
  114. // "sum(if(run_bus_expenses.expense_subject_id = 462 or isnull(run_bus_expenses.expense_subject_id = 462),bus_order.send_cost,0))
  115. "IFNULL(bus_order.send_cost, 0)
  116. + sum(if(bus_order.send_type = 0 and expense_subject_id in (462,463,464,465,466,467),run_bus_expenses.expense_value,0)) AS send_cost", 'GROUP_CONCAT(bus_cost.ID) as bus_cost_id']);
  117. //搜索客户类型,由于comboSelect功能有限,只能分3个下拉列表
  118. if (is_array($this->use_bus_org_id)) {
  119. if ($this->client_type === '') {
  120. $this->use_bus_org_id = $this->use_bus_org_id[0];
  121. } elseif ($this->client_type == self::CLIENT_TYPE_COMPANY) {
  122. $query->joinWith('department');
  123. //如果没有值,只查询出所有的 企业
  124. if ($this->use_bus_org_id[1] == '') {
  125. $query->andFilterWhere(['depart_type' => self::CLIENT_TYPE_COMPANY]);
  126. }
  127. $this->use_bus_org_id = $this->use_bus_org_id[1];
  128. } elseif ($this->client_type == self::CLIENT_TYPE_PERSONAL) {
  129. $query->joinWith('department');
  130. //如果没有值,只查询出所有的 个人
  131. if ($this->use_bus_org_id[2] == '') {
  132. $query->andFilterWhere(['depart_type' => self::CLIENT_TYPE_PERSONAL]);
  133. }
  134. $this->use_bus_org_id = $this->use_bus_org_id[2];
  135. }
  136. }
  137. $attr_status = $this->send_type == 1 ? 'outcome_end_status' : 'end_status';
  138. //结算状态,开关账都放在一起了
  139. if ($this->$attr_status !== '' && $this->$attr_status !== null && in_array($this->$attr_status, [self::FINANCE_LOCK_OPEN, self::FINANCE_LOCK_LOCKED])) {
  140. $query->andFilterWhere(['finance_lock' => $this->$attr_status, $attr_status => self::STATUS_END_NOTHING]);
  141. } else {
  142. $query->andFilterWhere([$attr_status => $this->$attr_status]);
  143. }
  144. //日期类型
  145. if ($this->date_type == 1) { //出车日期
  146. $query->andFilterWhere(['between', 'bus_order.run_date', $this->date_from, $this->date_to]);
  147. } elseif ($this->date_type == 2) { //收车日期
  148. $query->andFilterWhere(['between', 'date_add(bus_order.run_date,interval day_num - 1 day)', $this->date_from, $this->date_to]);
  149. }
  150. // grid filtering conditions
  151. $query->andFilterWhere([
  152. 'bus_order.cancel_flag' => 0,
  153. 'bus_order.use_bus_org_id' => $this->use_bus_org_id,
  154. 'bus_order.finance_status' => $this->finance_status,
  155. 'bus_order.run_status' => BusOrder::STATUS_RUN_FINISH, //出车完成的才显示
  156. 'bus_order.finance_lock' => $this->finance_lock,
  157. 'base_user.main_corp_id2' => $this->login_user->MAIN_CORP_ID2,
  158. 'bus_order.bus_order_finance' => $this->bus_order_finance,
  159. 'bus_order.bus_order_outcome_finance' => $this->bus_order_outcome_finance,
  160. 'bus_order.send_type' => $this->send_type,
  161. 'base_bus.org_id' => $this->bus_supplier_id,
  162. ]);
  163. $query
  164. ->andFilterWhere(['like', 'itinerary_name', $this->itinerary_name])
  165. ->andFilterWhere(['like', 'bus_order.bus_number', trim($this->bus_number)]);
  166. $query->groupBy('bus_order.bus_number');
  167. $sql = $query -> createCommand() -> getSql();
  168. return $dataProvider;
  169. }
  170. public function attributeLabels()
  171. {
  172. return array_merge(
  173. parent::attributeLabels(),
  174. [
  175. 'id' => '编号',
  176. 'date_type' => '日期选择',
  177. 'date_from' => '起始日期',
  178. 'date_to' => '结束日期',
  179. 'run_date' => '出车日期',
  180. 'date_end' => '收车日期',
  181. 'client_type' => '客户',
  182. 'depart_name' => $this->send_type == 1 ? '供应商' : '客户',
  183. 'client_id' => '用车单位',
  184. 'bus_number' => '出车单号',
  185. 'bus_cost' => '车价',
  186. 'send_cost' => $this->send_type == 1 ? '成本车价' : '运营成本',
  187. 'pay_type_name' => '支付方式',
  188. 'end_status' => '结算状态',
  189. 'end_status_name' => '结算状态',
  190. 'bus_no' => '车牌号',
  191. 'bus_cost_id' => '成本编号'
  192. ]);
  193. }
  194. /**
  195. * User: wangxj
  196. *
  197. * 开关账状态
  198. * end_status 未结账状态可以取消关账,其他状态都不能取消关账
  199. *
  200. * @param $type
  201. * @return bool
  202. */
  203. public function condomAction($type)
  204. {
  205. $attr_status = $this->send_type ? 'outcome_end_status' : 'end_status';
  206. $sql_update = "UPDATE bus_order o";
  207. $sql_join = " LEFT JOIN base_user u ON o.create_user_id = u.id
  208. LEFT JOIN `bus_department` d ON o.`use_bus_org_id` = d.`ID` ";
  209. $sql_set = " SET o.finance_lock = " . ($type == 'close' ? '1' : '0');
  210. $sql_where = " o.cancel_flag=0 AND u.main_corp_id2=" . Yii::$app->user->identity->MAIN_CORP_ID2;
  211. $sql_where .= ($this->date_type == 1
  212. ? ' and o.run_date between ' : ' and date_add(
  213. bus_order.run_date,
  214. INTERVAL day_num - 1 DAY
  215. ) between ') . " '" . $this->date_from . "' AND '" . $this->date_to . "'" .
  216. ($this->bus_number != '' ? ' and o.bus_number like "%' . trim($this->bus_number) . '%"' : '') .
  217. " and o.run_status = " . BusOrder::STATUS_RUN_FINISH . " and o.end_status = " . BusOrder::STATUS_END_NOTHING
  218. . " and o.outcome_end_status = " . BusOrder::STATUS_END_NOTHING; //出车完成的才操作
  219. //客户条件
  220. if ($this->use_bus_org_id != '') {
  221. $sql_where .= ' and use_bus_org_id = ' . $this->use_bus_org_id;
  222. } elseif ($this->client_type != '') {
  223. $sql_where .= ' and d.depart_type = ' . $this->client_type;
  224. }
  225. //结算状态条件
  226. if ($this->$attr_status !== '') {
  227. $sql_where .= in_array($this->$attr_status, [BusOrder::STATUS_END_FINISH, BusOrder::STATUS_END_WAITING_FINISH, BusOrder::STATUS_END_WAITING_CONFIRM])
  228. ? ' and o.' . $attr_status . ' = ' . $this->$attr_status
  229. : ' and o.finance_lock=' . $this->$attr_status;
  230. }
  231. //应付
  232. $sql = $sql_update . $sql_join . $sql_set . ' where ' . $sql_where;
  233. $sql .= $this->send_type ? ' and o.send_type=1' : '';
  234. $orders = BusOrder::find()->leftJoin('base_user u', 'o.create_user_id = u.id ')
  235. ->from('bus_order o')
  236. ->leftJoin('`bus_department` d', 'o.`use_bus_org_id` = d.`ID`')
  237. ->andWhere($sql_where . ' and o.run_bus_id <> 0')
  238. ->all();
  239. $result = true;
  240. if (!empty($orders) && $type == 'close') {
  241. foreach ($orders as $order) {
  242. /* @var $order BusOrder */
  243. $result = $result && $order->syncOneBusOrder();
  244. }
  245. }
  246. $result = $result && Yii::$app->db->createCommand($sql)->execute();
  247. return $result;
  248. }
  249. /**
  250. * User: wangxj
  251. *
  252. * 生成对账单的数据,因为是根据查询条件生成对账单,所以函数放这里
  253. * 每一个客户一条记录
  254. *
  255. */
  256. public function getOrderFinanceOrder()
  257. {
  258. $query = searchOrder::find();
  259. $query->joinWith('user')->joinWith('bus');
  260. $query->addSelect([
  261. "count(DISTINCT bus_order.bus_number) as bus_order_count",
  262. "GROUP_CONCAT(bus_order.id) as bus_ids",
  263. "sum(if(bus_cost_type = 477,bus_cost,saled_count*bus_cost)) as bus_cost",
  264. "sum(send_cost) send_cost",
  265. "send_type",
  266. "base_bus.org_id bus_supplier_id",
  267. ($this->send_type ? "base_bus.org_id" : "bus_order.use_bus_org_id") . ' as use_bus_org_id']);
  268. if (is_array($this->use_bus_org_id)) {
  269. if ($this->client_type === '') {
  270. $this->use_bus_org_id = $this->use_bus_org_id[0];
  271. } elseif ($this->client_type == self::CLIENT_TYPE_COMPANY) {
  272. $query->joinWith('department');
  273. //如果没有值,只查询出所有的 企业
  274. if ($this->use_bus_org_id[1] == '') {
  275. $query->andFilterWhere(['depart_type' => self::CLIENT_TYPE_COMPANY]);
  276. }
  277. $this->use_bus_org_id = $this->use_bus_org_id[1];
  278. } elseif ($this->client_type == self::CLIENT_TYPE_PERSONAL) {
  279. $query->joinWith('department');
  280. //如果没有值,只查询出所有的 个人
  281. if ($this->use_bus_org_id[2] == '') {
  282. $query->andFilterWhere(['depart_type' => self::CLIENT_TYPE_PERSONAL]);
  283. }
  284. $this->use_bus_org_id = $this->use_bus_org_id[2];
  285. }
  286. }
  287. //日期类型
  288. if ($this->date_type == 1) { //出车日期
  289. $query->andFilterWhere(['between', 'bus_order.run_date', $this->date_from, $this->date_to]);
  290. } elseif ($this->date_type == 2) { //收车日期
  291. $query->andFilterWhere(['between', 'date_add(bus_order.run_date,interval day_num - 1 day)', $this->date_from, $this->date_to]);
  292. }
  293. // grid filtering conditions
  294. $query->andFilterWhere([
  295. 'bus_order.cancel_flag' => 0,
  296. 'bus_order.run_status' => BusOrder::STATUS_RUN_FINISH, //出车完成的才显示
  297. 'bus_order.' . ($this->send_type == 1 ? 'outcome_end_status' : 'end_status') => BusOrder::STATUS_END_NOTHING,
  298. 'bus_order.finance_lock' => 1,
  299. 'bus_order.send_type' => $this->send_type,
  300. 'base_user.main_corp_id2' => Yii::$app->user->identity->MAIN_CORP_ID2,
  301. ]);
  302. if ($this->send_type) {
  303. $query->andFilterWhere(['base_bus.org_id' => $this->bus_supplier_id]);
  304. $query->groupBy('bus_supplier_id');
  305. } else {
  306. $query->groupBy('bus_order.use_bus_org_id');
  307. }
  308. // $sql = $query->createCommand();
  309. return $query->all();
  310. }
  311. /**
  312. * 生成应付对账单
  313. *
  314. * @return array|\yii\db\ActiveRecord[]
  315. *
  316. * @author wangxj
  317. */
  318. public function getOutcomeOrderFinanceOrder()
  319. {
  320. $query = searchOrder::find();
  321. $query->joinWith('user')->joinWith('bus');
  322. $query->addSelect([
  323. "count(DISTINCT bus_order.bus_number) as bus_order_count",
  324. "GROUP_CONCAT(bus_order.id) as bus_ids",
  325. "sum(if(bus_cost_type = 477,bus_cost,saled_count*bus_cost)) as bus_cost",
  326. "sum(send_cost) send_cost",
  327. "send_type",
  328. "base_bus.org_id bus_supplier_id",
  329. "base_bus.org_id as use_bus_org_id"]);
  330. if (is_array($this->use_bus_org_id)) {
  331. if ($this->client_type === '') {
  332. $this->use_bus_org_id = $this->use_bus_org_id[0];
  333. } elseif ($this->client_type == self::CLIENT_TYPE_COMPANY) {
  334. $query->joinWith('department');
  335. //如果没有值,只查询出所有的 企业
  336. if ($this->use_bus_org_id[1] == '') {
  337. $query->andFilterWhere(['depart_type' => self::CLIENT_TYPE_COMPANY]);
  338. }
  339. $this->use_bus_org_id = $this->use_bus_org_id[1];
  340. } elseif ($this->client_type == self::CLIENT_TYPE_PERSONAL) {
  341. $query->joinWith('department');
  342. //如果没有值,只查询出所有的 个人
  343. if ($this->use_bus_org_id[2] == '') {
  344. $query->andFilterWhere(['depart_type' => self::CLIENT_TYPE_PERSONAL]);
  345. }
  346. $this->use_bus_org_id = $this->use_bus_org_id[2];
  347. }
  348. }
  349. //日期类型
  350. if ($this->date_type == 1) { //出车日期
  351. $query->andFilterWhere(['between', 'bus_order.run_date', $this->date_from, $this->date_to]);
  352. } elseif ($this->date_type == 2) { //收车日期
  353. $query->andFilterWhere(['between', 'date_add(bus_order.run_date,interval day_num - 1 day)', $this->date_from, $this->date_to]);
  354. }
  355. // grid filtering conditions
  356. $query->andFilterWhere([
  357. 'bus_order.cancel_flag' => 0,
  358. 'bus_order.run_status' => BusOrder::STATUS_RUN_FINISH, //出车完成的才显示
  359. 'bus_order.' . ($this->send_type == 1 ? 'outcome_end_status' : 'end_status') => BusOrder::STATUS_END_NOTHING,
  360. 'bus_order.finance_lock' => 1,
  361. 'bus_order.send_type' => $this->send_type,
  362. 'base_user.main_corp_id2' => Yii::$app->user->identity->MAIN_CORP_ID2,
  363. ]);
  364. if ($this->send_type) {
  365. $query->andFilterWhere(['base_bus.org_id' => $this->bus_supplier_id]);
  366. $query->groupBy('bus_supplier_id');
  367. } else {
  368. $query->groupBy('bus_order.use_bus_org_id');
  369. }
  370. // $sql = $query->createCommand();
  371. return $query->all();
  372. }
  373. }