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.
 
 
 
 

656 lines
28 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: admin
  5. * Date: 2017/5/22
  6. * Time: 16:45
  7. */
  8. class payment extends base
  9. {
  10. //获取应收订单列表
  11. public function getList($post)
  12. {
  13. $getWhere = $this->getWhere($post);
  14. if ($getWhere['param']['current_page'] == 1) {
  15. /**==========获取记录数和总金额============**/
  16. $sql_1 = "
  17. SELECT SUM(x.cnt) 'cnt',FORMAT(SUM(x.base_price),2) 'total_price',SUM(x.reparations_price) 'reparations_price'
  18. FROM (
  19. SELECT COUNT(1) cnt,SUM(a.TOTAL_COST_PRICE) 'base_price',
  20. SUM(
  21. (SELECT IFNULL(SUM(v.reparations),0) from order_main u
  22. JOIN order_finance_reparations v on u.order_id = v.order_id
  23. WHERE u.RUN_ID = a.RUN_ID and u.RUN_BUS_ORDER_ID = a.BUS_ORDER_ID
  24. )
  25. )
  26. as 'reparations_price'
  27. from bus_cost a
  28. LEFT JOIN order_finance_status b on a.ID = b.order_id and b.type=2
  29. LEFT JOIN opera_line d ON a.LINE_ID = d.LINE_ID
  30. WHERE {$getWhere['bus_where']}
  31. union ALL
  32. SELECT COUNT(1) cnt,SUM(a.BASE_PRICE) 'base_price',
  33. IFNULL((SELECT reparations from order_finance_reparations WHERE order_id=a.ORDER_ID),0) 'reparations_price'
  34. from order_main a
  35. LEFT JOIN order_finance_status b ON a.ORDER_ID = b.order_id and b.type=1
  36. WHERE {$getWhere['order_where']}
  37. ) x
  38. ";
  39. $getTotal = $this->query($sql_1);
  40. $data['page']['total_count'] = $getTotal[0]['cnt'];
  41. $data['page']['total_page'] = (string)ceil($getTotal[0]['cnt'] / $getWhere['param']['page_size']);
  42. $data['sum_info'] = $getTotal[0];
  43. }
  44. /**==========获取列表============**/
  45. $offset = ($getWhere['param']['current_page'] - 1) * $getWhere['param']['page_size'];
  46. $sql_2 = "
  47. SELECT a.ID 'order_id','' as 'outside_order_no',a.START_TIME 'create_time',
  48. d.LINE_NAME 'parent_prod_name','' as 'order_prod_type',
  49. (SELECT SUPPLIER_NAME FROM base_supplier WHERE a.BUS_ORG_ID = ID) 'supplier_name' ,
  50. a.TOTAL_COST_PRICE 'base_price',
  51. (SELECT IFNULL(SUM(v.reparations),0) from order_main u
  52. JOIN order_finance_reparations v on u.order_id = v.order_id
  53. WHERE u.RUN_ID = a.RUN_ID and u.RUN_BUS_ORDER_ID = a.BUS_ORDER_ID
  54. ) as 'reparations_price',
  55. IFNULL(b.payment_status,1) 'status',
  56. CASE IFNULL(b.payment_status,1)
  57. WHEN 1 THEN '待结算'
  58. WHEN 2 THEN '已关账'
  59. WHEN 3 THEN '结算中'
  60. WHEN 4 THEN '已结算'
  61. ELSE '待结算' END AS 'status_des' ,
  62. a.START_TIME as 'run_date',
  63. '巴士' as 'prod_type_des',
  64. '' as 'order_description',
  65. '' as 'prodNum',
  66. '' as 'prodName'
  67. from bus_cost a
  68. LEFT JOIN opera_line d ON a.LINE_ID = d.LINE_ID
  69. LEFT JOIN order_finance_status b on a.ID = b.order_id and b.type=2
  70. WHERE {$getWhere['bus_where']}
  71. union ALL
  72. SELECT a.ORDER_ID 'order_id',a.OUTSIDE_ORDER_NO,a.CREATE_TIME 'create_time',
  73. (SELECT CONCAT(PARENT_PROD_NAME, '<br />',PROD_START_STATION_RES_NAME,' - ',PROD_END_STATION_RES_NAME) from order_main WHERE PARENT_ORDER_ID = a.ORDER_ID LIMIT 1) 'parent_prod_name',
  74. a.ORDER_PROD_TYPE 'order_prod_type',
  75. (SELECT SUPPLIER_NAME from base_supplier WHERE ID = a.PROD_TOP_ORG_ID) 'supplier_name',
  76. a.BASE_PRICE 'base_price',
  77. IFNULL((SELECT reparations from order_finance_reparations WHERE order_id=a.ORDER_ID),0) 'reparations_price',
  78. IFNULL(b.payment_status,1) 'status',
  79. CASE IFNULL(b.payment_status,1)
  80. WHEN 1 THEN '待结算'
  81. WHEN 2 THEN '已关账'
  82. WHEN 3 THEN '结算中'
  83. WHEN 4 THEN '已结算'
  84. ELSE '待结算' END AS 'status_des' ,
  85. (SELECT MAX(RUN_DATE) from order_main WHERE a.ORDER_ID = PARENT_ORDER_ID) 'run_date',
  86. CASE
  87. WHEN a.ORDER_PROD_TYPE in (25,26) THEN '酒店'
  88. WHEN a.ORDER_PROD_TYPE in (81,82,38,369) THEN '车票'
  89. WHEN a.ORDER_PROD_TYPE in (258,282,311) THEN '门票'
  90. else '' END as 'prod_type_des',
  91. a.ORDER_DESCRIPTION 'order_description',
  92. '' as 'prodNum',
  93. '' as 'prodName'
  94. from order_main a
  95. LEFT JOIN order_finance_status b ON a.ORDER_ID = b.order_id and b.type=1
  96. WHERE {$getWhere['order_where']}
  97. LIMIT {$getWhere['param']['page_size']} offset $offset;
  98. ";
  99. $list = $this->query($sql_2);
  100. /**==========设置返回值============**/
  101. if (empty($list) == false) {
  102. foreach ($list as $key => $value) {
  103. if(empty($value['order_description'])) {
  104. continue;
  105. }
  106. $list[$key]['prodNum'] = 0;
  107. $list[$key]['prodName'] = '';
  108. $scripTionArr = explode('|', $value['order_description']);
  109. foreach ($scripTionArr as $vl) {
  110. if (empty($vl)) {
  111. continue;
  112. }
  113. $vls = explode(',', $vl);
  114. if (empty($vls['2'])) {
  115. $list[$key]['prodNum'] = $vls[0];
  116. } else {
  117. $list[$key]['prodNum'] = $vls['2'];
  118. $list[$key]['prodName'] = $vls['0'];
  119. }
  120. }
  121. unset($list[$key]['order_description']);
  122. }
  123. }
  124. $json['code'] = '0';
  125. $json['info'] = '获取列表成功';
  126. $data['order_list'] = $list;
  127. $data['page']['page_size'] = $getWhere['param']['page_size'];
  128. $data['page']['current_page'] = $getWhere['param']['current_page'];
  129. $json['data'] = $data;
  130. return $json;
  131. }
  132. /**
  133. * @param $post
  134. * @return mixed
  135. */
  136. public function closesAmount($post)
  137. {
  138. $getWhere = $this->getWhere($post);
  139. $sql_1 = "
  140. SELECT SUM(x.cnt) cnt
  141. FROM (
  142. SELECT COUNT(1) cnt
  143. from bus_cost a
  144. LEFT JOIN order_finance_status b on a.ID = b.order_id and b.type=2
  145. LEFT JOIN opera_line d ON a.LINE_ID = d.LINE_ID
  146. WHERE {$getWhere['bus_where']}
  147. and IFNULL(b.payment_status,1) != 1
  148. union ALL
  149. SELECT COUNT(1) cnt
  150. from order_main a
  151. LEFT JOIN order_finance_status b ON a.ORDER_ID = b.order_id and b.type=1
  152. WHERE {$getWhere['order_where']}
  153. and IFNULL(b.payment_status,1) != 1
  154. ) x
  155. ";
  156. $getTotal = $this->query($sql_1);
  157. if ($getTotal['0']['cnt'] >= 1) {
  158. $json['code'] = '1';
  159. $json['info'] = '关账失败!所选订单中有非待结算状态订单!';
  160. return $json;
  161. }
  162. /**==========插入新记录=============**/
  163. $sql1 = "
  164. INSERT into order_finance_status(order_id, update_time, type)
  165. SELECT z.* FROM (
  166. SELECT a.ID,NOW(),2
  167. from bus_cost a
  168. LEFT JOIN order_finance_status b on a.ID = b.order_id and b.type=2
  169. LEFT JOIN opera_line d ON a.LINE_ID = d.LINE_ID
  170. WHERE {$getWhere['bus_where']}
  171. and b.payment_status is null
  172. union ALL
  173. SELECT a.ORDER_ID,NOW(),1
  174. from order_main a
  175. LEFT JOIN order_finance_status b ON a.ORDER_ID = b.order_id and b.type=1
  176. WHERE {$getWhere['order_where']}
  177. and b.payment_status is null
  178. ) z
  179. ";
  180. $this->insert($sql1);
  181. /**==========更新旧新记录=============**/
  182. $sql2 = "
  183. update order_main a
  184. JOIN order_finance_status b ON a.ORDER_ID = b.order_id and b.type=1
  185. set b.payment_status = 2
  186. WHERE {$getWhere['order_where']};
  187. update bus_cost a
  188. JOIN order_finance_status b on a.ID = b.order_id and b.type=2
  189. LEFT JOIN opera_line d ON a.LINE_ID = d.LINE_ID
  190. SET b.payment_status = 2
  191. WHERE {$getWhere['bus_where']};
  192. ";
  193. $this->exec($sql2);
  194. $json['code'] = '0';
  195. $json['info'] = '已关账';
  196. return $json;
  197. }
  198. //生成对账单
  199. public function addBalance($post)
  200. {
  201. $getWhere = $this->getWhere($post);
  202. $check = $this->checkCanCloses($getWhere);
  203. if ($check == false) {
  204. $json['code'] = '1';
  205. $json['info'] = '关账失败!所选订单中有非关账状态订单!';
  206. return $json;
  207. }
  208. /**============生成对账单===================**/
  209. $time = microtime(true) * 10000;
  210. $sql = "
  211. INSERT into order_finance_balance_account(org_id,org_name,price,
  212. reparations_price,type,batch_id,start_date,end_date,account_bank,account_name,account_num,sett_frequency,order_type)
  213. SELECT x.org_id,(SELECT SUPPLIER_NAME from base_supplier WHERE ID = x.org_id) 'org_name',
  214. SUM(x.price),SUM(x.reparations),1,concat('{$time}','',x.org_id),'{$getWhere['param']['start_date']}','{$getWhere['param']['end_date']}',
  215. c.ACCOUNT_BANK,c.ACCOUNT_NAME,c.ACCOUNT_NUM,d.TYPE_NAME,1
  216. from(
  217. SELECT a.BUS_ORG_ID 'org_id',SUM(a.TOTAL_COST_PRICE) 'price',
  218. (SELECT IFNULL(SUM(v.reparations),0) from order_main u
  219. JOIN order_finance_reparations v on u.order_id = v.order_id
  220. WHERE u.RUN_ID = a.RUN_ID and u.RUN_BUS_ORDER_ID = a.BUS_ORDER_ID
  221. ) as 'reparations'
  222. from bus_cost a
  223. LEFT JOIN opera_line d ON a.LINE_ID = d.LINE_ID
  224. left JOIN order_finance_status b on a.ID = b.order_id and b.type=2
  225. WHERE {$getWhere['bus_where']}
  226. GROUP BY a.BUS_ORG_ID
  227. union ALL
  228. SELECT a.PROD_TOP_ORG_ID 'org_id',SUM(a.BASE_PRICE) 'price',IFNULL(SUM(d.reparations),0) 'reparations'
  229. from order_main a
  230. left JOIN order_finance_status b ON a.ORDER_ID = b.order_id and b.type=1
  231. LEFT JOIN order_finance_reparations d on a.order_id=d.order_id
  232. WHERE {$getWhere['order_where']}
  233. GROUP BY a.PROD_TOP_ORG_ID
  234. )x
  235. LEFT join base_supplier c on c.ID=x.org_id
  236. LEFT JOIN dict_type d on c.SETT_FREQUENCY = d.ID
  237. GROUP BY x.org_id
  238. ";
  239. $this->insert($sql);
  240. /**============关联对账单记录==============**/
  241. $sql2 = "
  242. update order_main a
  243. JOIN order_finance_status b ON a.ORDER_ID = b.order_id and b.type=1
  244. set b.payment_status = 3 ,b.payment__batch_id = CONCAT('{$time}','',a.PROD_TOP_ORG_ID)
  245. WHERE {$getWhere['order_where']};
  246. update bus_cost a
  247. JOIN order_finance_status b on a.ID = b.order_id and b.type=2
  248. LEFT JOIN opera_line d ON a.LINE_ID = d.LINE_ID
  249. SET b.payment_status = 3 ,b.payment__batch_id = CONCAT('{$time}','',a.BUS_ORG_ID)
  250. WHERE {$getWhere['bus_where']};
  251. ";
  252. $this->edit($sql2);
  253. $json['code'] = '0';
  254. $json['info'] = '已生成对账单';
  255. return $json;
  256. }
  257. //取消关账
  258. public function cancelCloses($post)
  259. {
  260. $getWhere = $this->getWhere($post);
  261. $check = $this->checkCanCloses($getWhere);
  262. if ($check == false) {
  263. $json['code'] = '1';
  264. $json['info'] = '关账失败!所选订单中有非关账状态订单!';
  265. return $json;
  266. }
  267. /**============取消关账==============**/
  268. $sql2 = "
  269. update order_main a
  270. JOIN order_finance_status b ON a.ORDER_ID = b.order_id and b.type=1
  271. set b.payment_status = 1
  272. WHERE {$getWhere['order_where']};
  273. update bus_cost a
  274. JOIN order_finance_status b on a.ID = b.order_id and b.type=2
  275. LEFT JOIN opera_line d ON a.LINE_ID = d.LINE_ID
  276. SET b.payment_status = 1
  277. WHERE {$getWhere['bus_where']};
  278. ";
  279. $this->edit($sql2);
  280. $json['code'] = '0';
  281. $json['info'] = '已取消关账';
  282. return $json;
  283. }
  284. //获取查询条件
  285. public function getWhere($post)
  286. {
  287. /**==========接收参数============**/
  288. $param = array(
  289. 'date_type' => empty($post['date_type']) ? '' : $post['date_type'],//时间类型
  290. 'start_date' => empty($post['start_date']) ? '' : $post['start_date'],//开始时间
  291. 'end_date' => empty($post['end_date']) ? '' : $post['end_date'],//接收时间
  292. 'org_id' => empty($post['org_id']) ? '' : $post['org_id'],//渠道商ID
  293. 'settlement_status' => empty($post['settlement_status']) ? '0' : $post['settlement_status'],//结算状态值
  294. 'prod_type' => empty($post['prod_type']) ? '0' : $post['prod_type'],//订单产品类型
  295. 'more_search' => empty($post['more_search ']) ? '' : $post['more_search '],//更多查询条件
  296. 'current_page' => empty($post['current_page']) ? '1' : $post['current_page'],//当前页
  297. 'page_size' => empty($post['page_size']) ? '10' : $post['page_size'],//每页展示条数
  298. );
  299. /**==========拼接订单的查询条件============**/
  300. $order_where = " a.CANCEL_FLAG=0 and (a.order_status in(146,147,314) or a.CHANNEL_ORDER_STATUS in (551,554,553)) and a.ORDER_PROD_TYPE not in (81,82,38,369) and a.PARENT_ORDER_ID = 0 ";//获取所有已完成的有效的主订单
  301. //结算时间
  302. if ($param['date_type'] == '2' && $param['start_date']) {
  303. $order_where .= " and (SELECT MIN(RUN_DATE) from order_main WHERE a.ORDER_ID = PARENT_ORDER_ID) >= '{$param['start_date']}'";//大于等于起始日期
  304. }
  305. if ($param['date_type'] == '2' && $param['end_date']) {
  306. $order_where .= " and (SELECT MIN(RUN_DATE) from order_main WHERE a.ORDER_ID = PARENT_ORDER_ID) < date_add('{$param['end_date']}', INTERVAL 1 day)";//小于日期
  307. }
  308. //收款状态
  309. if ($param['settlement_status']) {
  310. $order_where .= " and IFNULL(b.payment_status,1) = {$param['settlement_status']}";
  311. }
  312. //预定时间
  313. if ($param['date_type'] == '1' && $param['start_date']) {
  314. $order_where .= " and a.CREATE_TIME >= DATE_FORMAT('{$param['start_date']}' ,'%Y-%m-%d %H:%i:%s')";//大于等于起始日期
  315. }
  316. if ($param['date_type'] == '1' && $param['end_date']) {
  317. $order_where .= " and a.CREATE_TIME < date_add(DATE_FORMAT('{$param['end_date']}' ,'%Y-%m-%d %H:%i:%s'), INTERVAL 1 day)";//小于结束日期的第二天凌晨
  318. }
  319. //渠道商ID
  320. if ($param['org_id']) {
  321. $order_where .= " and a.PROD_TOP_ORG_ID = '{$param['org_id']}'";
  322. }
  323. //订单产品类型
  324. if ($param['prod_type']) {
  325. $order_where .= " and a.ORDER_PROD_TYPE in ({$param['prod_type']})";
  326. }
  327. //更多查询
  328. if ($param['more_search']) {
  329. $order_where .= " and (a.ORDER_ID like '%{$param['more_search']}%' or a.PARENT_PROD_NAME like '%{$param['more_search']}%')";
  330. }
  331. /**==========拼接巴士bus的查询条件============**/
  332. $bus_where = " a.TOTAL_COST_PRICE > 0 ";//获取所有巴士
  333. //结算-预定时间 巴士的出发时间即预定时间
  334. if ($param['start_date']) {
  335. $bus_where .= " and a.START_TIME >= '{$param['start_date']}'";//大于等于起始日期
  336. }
  337. if ($param['end_date']) {
  338. $bus_where .= " and a.START_TIME < date_add('{$param['end_date']}', INTERVAL 1 day)";//小于日期
  339. }
  340. //收款状态
  341. if ($param['settlement_status']) {
  342. $bus_where .= " and IFNULL(b.payment_status,1) = {$param['settlement_status']}";
  343. }
  344. //渠道商ID
  345. if ($param['org_id']) {
  346. $bus_where .= " and and a.BUS_ORG_ID = '{$param['org_id']}'";
  347. }
  348. //订单产品类型
  349. if (!empty($param['prod_type']) && $param['prod_type'] != '81,82,38,369') {//不是车的则不统计
  350. $bus_where .= " and a.ID = 0 ";
  351. }
  352. //更多查询
  353. if ($param['more_search']) {
  354. $bus_where .= " and (a.id like '%{$param['more_search']}%' or d.LINE_NAME like '%{$param['more_search']}%')";
  355. }
  356. return array('order_where' => $order_where, 'bus_where' => $bus_where, 'param' => $param);
  357. }
  358. //检测是否可以生成对账单或取消关账
  359. public function checkCanCloses($getWhere)
  360. {
  361. /**============判断是否可以生成对账单===================**/
  362. $sql_1 = "
  363. SELECT SUM(x.cnt) cnt
  364. FROM (
  365. SELECT COUNT(1) cnt
  366. from bus_cost a
  367. LEFT JOIN order_finance_status b on a.ID = b.order_id and b.type=2
  368. LEFT JOIN opera_line d ON a.LINE_ID = d.LINE_ID
  369. WHERE {$getWhere['bus_where']}
  370. and IFNULL(b.payment_status,1) != 2
  371. union ALL
  372. SELECT COUNT(1) cnt
  373. from order_main a
  374. LEFT JOIN order_finance_status b ON a.ORDER_ID = b.order_id and b.type=1
  375. WHERE {$getWhere['order_where']}
  376. and IFNULL(b.payment_status,1) != 2
  377. ) x
  378. ";
  379. $getTotal = $this->query($sql_1);
  380. if ($getTotal['0']['cnt'] >= 1) {
  381. return false;
  382. }
  383. return true;
  384. }
  385. /////*********************对账单列表***********************/
  386. //获取账单总记录数
  387. public function getBalance($post)
  388. {
  389. $param = array(
  390. 'current_page' => empty($post['current_page']) ? '1' : $post['current_page'],//当前页
  391. 'page_size' => empty($post['page_size']) ? '10' : $post['page_size'],//每页展示条数
  392. 'org_id' => empty($post['org_id']) ? '' : $post['org_id'],//供应商名称
  393. 'batch_id' => empty($post['batch_id']) ? '' : $post['batch_id'],//对账单号
  394. 'status' => empty($post['status']) ? '' : $post['status'],//对账状态1待核对,2待结算,3已结算,4已取消
  395. );
  396. $where = array();
  397. $where[] = ' type = 1 ';
  398. if (!empty($param['org_id'])) {
  399. $where[] = " org_id = {$param['org_id']} ";
  400. }
  401. if (!empty($param['batch_id'])) {
  402. $where[] = " batch_id like '%{$param['batch_id']}%' ";
  403. }
  404. if (!empty($param['status'])) {
  405. $where[] = " status = {$param['status']} ";
  406. }
  407. $whereStr = '';
  408. if (count($where)) {
  409. $whereStr .= implode(' and ', $where);
  410. }
  411. /**============第一页时返回总记录数和总页数==============**/
  412. if ($param['current_page'] == 1) {
  413. $sql_1 = "SELECT count(1) cnt
  414. FROM order_finance_balance_account
  415. WHERE $whereStr
  416. ";
  417. $getTotal = $this->query($sql_1);
  418. $data['page']['total_count'] = $getTotal[0]['cnt'];
  419. $data['page']['total_page'] = (string)ceil($getTotal[0]['cnt'] / $param['page_size']);
  420. }
  421. /**============获取分页列表数据==============**/
  422. $offset = ($param['current_page'] - 1) * $param['page_size'];
  423. $sql_2 = "SELECT id,org_id,org_name,price,commission_price,reparations_price,date,start_date,end_date,
  424. account_bank,account_num,account_name,sett_frequency,batch_id,
  425. CASE status
  426. WHEN 1 THEN '待核对'
  427. WHEN 2 THEN '待结算'
  428. WHEN 3 THEN '已结算'
  429. WHEN 4 THEN '已取消'
  430. ELSE 'error' END as 'status_des',
  431. status
  432. from order_finance_balance_account
  433. WHERE $whereStr
  434. ORDER BY id DESC
  435. LIMIT {$param['page_size']} OFFSET {$offset}";
  436. $list = $this->query($sql_2);
  437. $json['code'] = '0';
  438. $json['info'] = '获取列表成功';
  439. $data['order_list'] = $list;
  440. $data['page']['page_size'] = $param['page_size'];
  441. $data['page']['current_page'] = $param['current_page'];
  442. $json['data'] = $data;
  443. return $json;
  444. }
  445. //取消对账
  446. public function cancelBalance($post)
  447. {
  448. $id = empty($post['id']) ? '' : $post['id'];//账单ID
  449. //校验对账单状态
  450. $flag = $this->checkBalanceStatus($id, array(1));
  451. if ($flag == false) {
  452. $json['code'] = '2';
  453. $json['info'] = '失败!待核对的账单才可以执行取消操作!';
  454. return $json;
  455. }
  456. //更改状态
  457. $sql = "
  458. update order_finance_balance_account a
  459. join order_finance_status b on a.batch_id=b.payment__batch_id
  460. set a.`status` =4,b.payment_status = 2
  461. WHERE a.`status` in(1) and a.id = $id
  462. ";
  463. $flag = $this->edit($sql);
  464. if ($flag) {
  465. $json['code'] = '0';
  466. $json['info'] = '取消成功!';
  467. } else {
  468. $json['code'] = '1';
  469. $json['info'] = '取消失败!';
  470. }
  471. return $json;
  472. }
  473. //确认对账
  474. public function confirmedBalance($post)
  475. {
  476. $id = empty($post['id']) ? '' : $post['id'];//账单ID
  477. //校验对账单状态
  478. $flag = $this->checkBalanceStatus($id, array(1));
  479. if ($flag == false) {
  480. $json['code'] = '2';
  481. $json['info'] = '失败!待核对的账单才可以确认核对!';
  482. return $json;
  483. }
  484. //更改状态
  485. $sql = "
  486. update order_finance_balance_account a
  487. set a.`status` =2
  488. WHERE a.`status` = 1 and a.id = $id
  489. ";
  490. $flag = $this->edit($sql);
  491. if ($flag) {
  492. $json['code'] = '0';
  493. $json['info'] = '成功!';
  494. } else {
  495. $json['code'] = '1';
  496. $json['info'] = '失败!';
  497. }
  498. return $json;
  499. }
  500. //确认结算
  501. public function balancing($post)
  502. {
  503. $id = empty($post['id']) ? '' : $post['id'];//账单ID
  504. //校验对账单状态
  505. $flag = $this->checkBalanceStatus($id, array(2));
  506. if ($flag == false) {
  507. $json['code'] = '2';
  508. $json['info'] = '失败!已核对的账单才可以结算!';
  509. return $json;
  510. }
  511. //更改状态
  512. $sql = "
  513. update order_finance_balance_account a
  514. join order_finance_status b on a.batch_id=b.payment__batch_id
  515. set a.`status` =3,b.payment_status =4
  516. WHERE a.`status` in(2) and a.id = $id
  517. ";
  518. $flag = $this->edit($sql);
  519. if ($flag) {
  520. $json['code'] = '0';
  521. $json['info'] = '成功!';
  522. } else {
  523. $json['code'] = '1';
  524. $json['info'] = '失败!';
  525. }
  526. return $json;
  527. }
  528. //导出对账订单记录
  529. public function excelBalance()
  530. {
  531. $id = empty($_GET['id']) ? '' : $_GET['id'];//账单ID
  532. $sql1 = "select org_name,start_date,end_date,order_type from order_finance_balance_account WHERE id = {$id}";
  533. $info = $this->query($sql1);
  534. if (empty($info)) {
  535. return false;
  536. }
  537. $sql = "
  538. select z.* FROM (
  539. SELECT c.ID '订单ID','' as '渠道订单ID',c.START_TIME '出游时间',a.start_date '预定时间',
  540. '' as 'order_description',
  541. d.LINE_NAME '产品名称','巴士成本' as '产品类型','' as '房型/票种',0 as '间夜/票数',
  542. c.TOTAL_COST_PRICE '成本',
  543. (SELECT IFNULL(SUM(v.reparations),0) from order_main u
  544. JOIN order_finance_reparations v on u.order_id = v.order_id
  545. WHERE u.RUN_ID = c.RUN_ID and u.RUN_BUS_ORDER_ID = c.BUS_ORDER_ID
  546. )
  547. as '赔款',
  548. (SELECT SUPPLIER_NAME FROM base_supplier WHERE c.BUS_ORG_ID = ID) '供应商'
  549. from order_finance_balance_account a
  550. JOIN order_finance_status b on a.batch_id = b.payment__batch_id and b.type=2
  551. JOIN bus_cost c on b.order_id = c.ID
  552. LEFT JOIN opera_line d on c.LINE_ID = d.LINE_ID
  553. WHERE c.ID={$id}
  554. UNION ALL
  555. SELECT
  556. c.ORDER_ID '订单ID',c.OUTSIDE_ORDER_NO '渠道订单ID',
  557. (SELECT MAX(RUN_DATE) from order_main WHERE c.ORDER_ID = PARENT_ORDER_ID) '出游时间',
  558. c.CREATE_TIME '预定时间',
  559. c.ORDER_DESCRIPTION 'order_description',
  560. (SELECT CONCAT(PARENT_PROD_NAME, '<br />',PROD_START_STATION_RES_NAME,' - ',PROD_END_STATION_RES_NAME) from order_main WHERE PARENT_ORDER_ID = c.ORDER_ID LIMIT 1) '产品名称',
  561. CASE
  562. WHEN c.ORDER_PROD_TYPE in (25,26) THEN '酒店'
  563. WHEN c.ORDER_PROD_TYPE in (81,82,38,369) THEN '车票'
  564. WHEN c.ORDER_PROD_TYPE in (258,282,311) THEN '门票'
  565. else '' END as '产品类型',
  566. '' as '房型/票种',0 as '间夜/票数',
  567. c.ORDER_PRICE '订单金额',
  568. IFNULL((SELECT reparations from order_finance_reparations WHERE order_id=c.ORDER_ID),0) as '赔款',
  569. a.org_name '供应商'
  570. from order_finance_balance_account a
  571. JOIN order_finance_status b on a.batch_id = b.payment__batch_id and b.type=1
  572. JOIN order_main c on b.order_id = c.ORDER_ID
  573. WHERE a.id={$id}
  574. ) z
  575. ORDER BY z.出游时间
  576. ";
  577. $list = $this->query($sql);
  578. if (empty($list[0]['订单ID'])) {
  579. return false;
  580. }
  581. if (empty($list) == false) {
  582. foreach ($list as $key => $value) {
  583. $order_description = $value['order_description'];
  584. unset($list[$key]['order_description']);
  585. if(empty($order_description)) {
  586. continue;
  587. }
  588. $list[$key]['间页/票数'] = 0;
  589. $list[$key]['房型/票种'] = '';
  590. $scripTionArr = explode('|', $order_description);
  591. foreach ($scripTionArr as $vl) {
  592. if (empty($vl)) {
  593. continue;
  594. }
  595. $vls = explode(',', $vl);
  596. if (empty($vls['2'])) {
  597. $list[$key]['间页/票数'] = $vls[0];
  598. } else {
  599. $list[$key]['间页/票数'] = $vls['2'];
  600. $list[$key]['房型/票种'] = $vls['0'];
  601. }
  602. }
  603. }
  604. }
  605. /**======生成excel=========**/
  606. ob_clean();
  607. $fileName = '【' . $info[0]['org_name'] . '】应付账单-' . $info[0]['start_date'] . '~' . $info[0]['end_date'] . '.csv';
  608. $fileName = iconv("utf-8", "gb2312", $fileName);
  609. header("Content-type:text/csv");
  610. header("Content-Disposition:attachment;filename=" . $fileName);
  611. header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
  612. header('Expires:0');
  613. header('Pragma:public');
  614. $data = '';
  615. foreach ($list as $key => $val) {
  616. if ($key == 0) {
  617. $data .= implode(',', array_keys($val)) . "\n";
  618. }
  619. $data .= '"' . implode('","', array_values($val)) . "\"\n";
  620. }
  621. $data = chr(0xEF) . chr(0xBB) . chr(0xBF) . $data;
  622. echo $data;
  623. return '';
  624. }
  625. //检测对账单状态
  626. public function checkBalanceStatus($id, $status)
  627. {
  628. $sql = "SELECT `status`
  629. from order_finance_balance_account
  630. WHERE id = $id";
  631. $info = $this->query($sql);
  632. if (empty($info[0]['status']) || !in_array($info[0]['status'], $status)) {
  633. return false;
  634. }
  635. return true;
  636. }
  637. }