getWhere($post); if ($getWhere['param']['current_page'] == 1) { /**==========获取记录数和总金额============**/ $sql_1 = " select sum(y.cnt) 'cnt',format(sum(y.total_price),2) 'total_price',format(sum(y.total_commission),2) 'total_commission', format(sum(y.reparations_price),2) 'reparations_price' FROM ( SELECT COUNT(1) cnt,ifnull(SUM(a.order_title_all_price),0) 'total_price', ifnull(SUM(order_title_all_affi),0) 'total_commission', IFNULL(SUM(c.reparations),0) 'reparations_price' from order_title a LEFT JOIN order_finance_status b on a.order_title_id = b.order_id and b.type =1 LEFT JOIN order_finance_reparations c on a.order_title_id = c.order_id WHERE {$getWhere['ziyou_where']} union ALL SELECT count(1) cnt,ifnull(sum(x.ORDER_PRICE),0) total_price, ifnull(sum(x.TOTAL_COMMISSION),0) 'total_commission', ifnull(sum(x.reparations_price),0) 'reparations_price' FROM ( SELECT a.ORDER_PRICE,a.TOTAL_COMMISSION, (SELECT MAX(RUN_DATE) from order_main WHERE ORDER_ID = a.PARENT_ORDER_ID) 'run_date', IFNULL(b.gathering_status,1) 'status', FORMAT(IFNULL((SELECT reparations from order_finance_reparations WHERE order_id=a.ORDER_ID),0),2) as 'reparations_price' FROM order_main a LEFT JOIN order_finance_status b ON a.ORDER_ID = b.order_id and b.type =1 WHERE {$getWhere['order_where']} ) x ) y "; $getTotal = $this->query($sql_1); $data['page']['total_count'] = $getTotal[0]['cnt']; $data['page']['total_page'] = (string)ceil($getTotal[0]['cnt'] / $getWhere['param']['page_size']); $data['sum_info'] = $getTotal[0]; } /**==========获取列表============**/ $offset = ($getWhere['param']['current_page'] - 1) * $getWhere['param']['page_size']; $sql_2 = " select z.* FROM ( SELECT x.*,'' AS 'prodName' from ( SELECT a.order_title_id 'order_id', a.outside_order_no,a.create_time, a.order_title_prod_name 'parent_prod_name', '' as 'order_description', (SELECT SUPPLIER_NAME from base_supplier WHERE ID = a.outside_sale_org_id) 'supplier_name', FORMAT(a.order_title_all_price,2) 'order_price',FORMAT(a.order_title_all_affi,2) 'total_commission', IFNULL(b.gathering_status,1) 'status', CASE IFNULL(b.gathering_status,1) WHEN 1 THEN '待结算' WHEN 2 THEN '已关账' WHEN 3 THEN '结算中' WHEN 4 THEN '已结算' ELSE '待结算' END AS 'status_des' , a.start_date 'run_date', FORMAT(IFNULL(c.reparations,0),2) 'reparations_price', '巴士自由行' as 'prod_type_des', '' as 'prodNum' from order_title a LEFT JOIN order_finance_status b on a.order_title_id = b.order_id and b.type =1 LEFT JOIN order_finance_reparations c on a.order_title_id = c.order_id WHERE {$getWhere['ziyou_where']} ) x GROUP BY x.order_id ) z UNION ALL SELECT a.ORDER_ID 'order_id',a.OUTSIDE_ORDER_NO 'outside_order_no',a.CREATE_TIME 'create_time', (SELECT CONCAT(PARENT_PROD_NAME, '
',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', a.ORDER_DESCRIPTION 'order_description', (SELECT SUPPLIER_NAME from base_supplier WHERE ID = a.OUTSIDE_SALE_ORG_ID) 'supplier_name', format(a.ORDER_PRICE,2) 'order_price',format(a.TOTAL_COMMISSION,2) 'total_commission',IFNULL(b.gathering_status,1) 'status', CASE IFNULL(b.gathering_status,1) WHEN 1 THEN '待结算' WHEN 2 THEN '已关账' WHEN 3 THEN '结算中' WHEN 4 THEN '已结算' ELSE '待结算' END AS 'status_des' , (SELECT MAX(RUN_DATE) from order_main WHERE PARENT_ORDER_ID = a.ORDER_ID) 'run_date', FORMAT(IFNULL((SELECT reparations from order_finance_reparations WHERE order_id=a.ORDER_ID),0),2) as 'reparations_price', CASE WHEN a.ORDER_PROD_TYPE in (25,26) THEN '酒店' WHEN a.ORDER_PROD_TYPE in (81,82,38,369) THEN '车票' WHEN a.ORDER_PROD_TYPE in (258,282,311) THEN '门票' else '' END as 'prod_type_des', '' as 'prodNum', '' as 'prodName' from order_main a LEFT JOIN order_finance_status b on a.ORDER_ID = b.order_id and b.type =1 WHERE {$getWhere['order_where']} ORDER BY create_time desc LIMIT {$getWhere['param']['page_size']} offset $offset; "; $list = $this->query($sql_2); /**==========设置返回值============**/ if (empty($list) == false) { foreach ($list as $key => $value) { if (empty($value['order_description'])) { continue; } $list[$key]['prodNum'] = 0; $list[$key]['prodName'] = ''; $scripTionArr = explode('|', $value['order_description']); foreach ($scripTionArr as $vl) { if (empty($vl)) { continue; } $vls = explode(',', $vl); if (empty($vls['2'])) { $list[$key]['prodNum'] = $vls[0]; } else { $list[$key]['prodNum'] = $vls['2']; $list[$key]['prodName'] = $vls['0']; } } unset($list[$key]['order_description']); } } $json['code'] = '0'; $json['info'] = '获取列表成功'; $data['order_list'] = $list; $data['page']['page_size'] = $getWhere['param']['page_size']; $data['page']['current_page'] = $getWhere['param']['current_page']; $json['data'] = $data; return $json; } /** * @param $post * @return mixed */ public function closesAmount($post) { $getWhere = $this->getWhere($post); /**============判断是否可以关账===================**/ $sql_1 = " select sum(y.cnt) 'cnt' FROM ( SELECT count(1) cnt FROM ( SELECT a.ORDER_PRICE,a.TOTAL_COMMISSION, (SELECT MAX(RUN_DATE) from order_main WHERE ORDER_ID = a.PARENT_ORDER_ID) 'run_date', IFNULL(b.gathering_status,1) 'status', FORMAT(IFNULL((SELECT reparations from order_finance_reparations WHERE order_id=a.ORDER_ID),0),2) as 'reparations_price' FROM order_main a LEFT JOIN order_finance_status b ON a.ORDER_ID = b.order_id and b.type =1 WHERE {$getWhere['order_where']} and IFNULL(b.gathering_status,1) != 1 )x UNION ALL SELECT COUNT(1) cnt from order_title a LEFT JOIN order_finance_status b on a.order_title_id = b.order_id and b.type =1 WHERE {$getWhere['ziyou_where']} and IFNULL(b.gathering_status,1) != 1 ) y "; $getTotal = $this->query($sql_1); if ($getTotal['0']['cnt'] >= 1) { $json['code'] = '1'; $json['info'] = '关账失败!所选订单中有非待结算状态订单!'; return $json; } /**==========插入新记录=============**/ $sql1 = " INSERT into order_finance_status(order_id,update_time, type) SELECT x.order_id,NOW(),1 from ( SELECT a.ORDER_ID 'order_id' from order_main a LEFT JOIN order_finance_status b on a.ORDER_ID = b.order_id and b.type =1 WHERE {$getWhere['order_where']} and b.gathering_status is null union ALL SELECT a.order_title_id 'order_id' from order_title a LEFT JOIN order_finance_status b on a.order_title_id = b.order_id and b.type =1 LEFT JOIN order_finance_reparations c on a.order_title_id = c.order_id WHERE {$getWhere['ziyou_where']} and b.gathering_status is null )x "; $this->insert($sql1); /**==========更新旧新记录=============**/ $sql2 = "UPDATE order_title a LEFT JOIN order_finance_status b on a.order_title_id = b.order_id and b.type =1 LEFT JOIN order_finance_reparations c on a.order_title_id = c.order_id SET b.gathering_status = 2 WHERE {$getWhere['ziyou_where']}; UPDATE order_main a JOIN order_finance_status b on a.ORDER_ID = b.order_id and b.type =1 SET b.gathering_status = 2 WHERE {$getWhere['order_where']} "; $this->exec($sql2); $json['code'] = '0'; $json['info'] = '已关账'; return $json; } //生成对账单 public function addBalance($post) { $getWhere = $this->getWhere($post); $check = $this->checkCanCloses($getWhere); if ($check == false) { $json['code'] = '1'; $json['info'] = '关账失败!所选订单中有非关账状态订单!'; return $json; } /**============生成对账单===================**/ $time = microtime(true) * 10000; $sql = " INSERT into order_finance_balance_account(org_id,org_name,price,commission_price, reparations_price,type,batch_id,start_date,end_date,account_bank,account_name,account_num,sett_frequency,order_type) SELECT s.org_id,s.org_name,sum(price),sum(affi),sum(reparations),2 as 'type',concat('{$time}','',s.org_id) 'batch_id', '{$getWhere['param']['start_date']}' as 'start_date','{$getWhere['param']['end_date']}' as 'end_date', s.ACCOUNT_BANK,s.ACCOUNT_NAME,s.ACCOUNT_NUM,s.TYPE_NAME,1 FROM ( SELECT x.org_id,(SELECT SUPPLIER_NAME from base_supplier WHERE ID = x.org_id) 'org_name', x.price,x.affi,x.reparations, c.ACCOUNT_BANK,c.ACCOUNT_NAME,c.ACCOUNT_NUM,d.TYPE_NAME from( SELECT a.outside_sale_org_id 'org_id',SUM(a.order_title_all_price) 'price',SUM(a.order_title_all_affi) 'affi', IFNULL(SUM(c.reparations),0) 'reparations' from order_title a LEFT JOIN order_finance_status b on a.order_title_id = b.order_id and b.type =1 LEFT JOIN order_finance_reparations c on a.order_title_id = c.order_id WHERE {$getWhere['ziyou_where']} GROUP BY a.outside_sale_org_id ) x LEFT join base_supplier c on c.ID=x.org_id LEFT JOIN dict_type d on c.SETT_FREQUENCY = d.ID UNION ALL SELECT t.* from( SELECT x.org_id,x.org_name, sum(x.price) 'price',sum(x.commission_price) 'affi',sum(x.reparations_price) 'reparations', c.ACCOUNT_BANK,c.ACCOUNT_NAME,c.ACCOUNT_NUM,d.TYPE_NAME FROM ( SELECT a.OUTSIDE_SALE_ORG_ID 'org_id', (SELECT SUPPLIER_NAME from base_supplier WHERE ID = a.OUTSIDE_SALE_ORG_ID) 'org_name', format(a.ORDER_PRICE,2) 'price', format(a.TOTAL_COMMISSION,2) 'commission_price', FORMAT(IFNULL((SELECT reparations from order_finance_reparations WHERE order_id=a.ORDER_ID),0),2) as 'reparations_price', 2 as 'type', IFNULL(b.gathering_status,1) 'status', (SELECT MAX(RUN_DATE) from order_main WHERE ORDER_ID = a.PARENT_ORDER_ID) 'run_date' from order_main a LEFT JOIN order_finance_status b on a.ORDER_ID = b.order_id and b.type =1 WHERE {$getWhere['order_where']} and b.gathering_status = 2 ) x LEFT join base_supplier c on c.ID=x.org_id LEFT JOIN dict_type d on c.SETT_FREQUENCY = d.ID GROUP BY x.org_id ) as t ) as s GROUP BY s.org_id "; $this->insert($sql); $sql2 = " UPDATE order_title a LEFT JOIN order_finance_status b on a.order_title_id = b.order_id and b.type =1 LEFT JOIN order_finance_reparations c on a.order_title_id = c.order_id SET b.gathering_status = 3 ,b.gathering_batch_id = CONCAT('{$time}','',a.outside_sale_org_id) WHERE {$getWhere['ziyou_where']}; UPDATE order_finance_status t1 INNER JOIN ( SELECT a.OUTSIDE_SALE_ORG_ID 'org_id', a.ORDER_ID from order_main a LEFT JOIN order_finance_status b on a.ORDER_ID = b.order_id and b.type =1 WHERE {$getWhere['order_where']} and b.gathering_status = 2 ) t2 on t1.order_id = t2.ORDER_ID set t1.gathering_batch_id = CONCAT('{$time}','',t2.org_id) ,t1.gathering_status = 3 "; $this->edit($sql2); $json['code'] = '0'; $json['info'] = '已生成对账单'; return $json; } //取消关账 public function cancelCloses($post) { $getWhere = $this->getWhere($post); $check = $this->checkCanCloses($getWhere); if ($check == false) { $json['code'] = '1'; $json['info'] = '关账失败!所选订单中有非关账状态订单!'; return $json; } /**============关联对账单记录==============**/ $sql2 = " UPDATE order_title a LEFT JOIN order_finance_status b on a.order_title_id = b.order_id and b.type =1 LEFT JOIN order_finance_reparations c on a.order_title_id = c.order_id SET b.gathering_status = 1 WHERE {$getWhere['ziyou_where']} and b.gathering_status = 2; UPDATE order_main a LEFT JOIN order_finance_status b on a.ORDER_ID = b.order_id and b.type =1 SET b.gathering_status = 1 WHERE {$getWhere['order_where']} and b.gathering_status = 2; "; $this->edit($sql2); $json['code'] = '0'; $json['info'] = '已取消关账'; return $json; } //获取查询条件 public function getWhere($post) { /**==========接收参数============**/ $param = array( 'date_type' => empty($post['date_type']) ? '' : $post['date_type'],//时间类型 'start_date' => empty($post['start_date']) ? '' : $post['start_date'],//开始时间 'end_date' => empty($post['end_date']) ? '' : $post['end_date'],//接收时间 'org_id' => empty($post['org_id']) ? '' : $post['org_id'],//渠道商ID 'settlement_status' => empty($post['settlement_status']) ? '0' : $post['settlement_status'],//结算状态值 'prod_type' => empty($post['prod_type']) ? '0' : $post['prod_type'],//订单产品类型 'more_search' => empty($post['more_search ']) ? '' : $post['more_search '],//更多查询条件 'current_page' => empty($post['current_page']) ? '1' : $post['current_page'],//当前页 'page_size' => empty($post['page_size']) ? '10' : $post['page_size'],//每页展示条数 ); /**==========拼接查询条件============**/ /***********************自由行查询**********************************/ $ziyou_where = " a.order_title_status in (3) and a.cancel_flag = 0 "; if (!empty($param['prod_type']) and $param['prod_type'] != 'ziyou') { $ziyou_where .= ' and a.cancel_flag = 1'; } //出游时间 if ($param['date_type'] == '2' && $param['start_date']) { $ziyou_where .= " and a.start_date >= '{$param['start_date']}'";//大于等于起始日期 } if ($param['date_type'] == '2' && $param['end_date']) { $ziyou_where .= " and a.start_date < date_add('{$param['end_date']}', INTERVAL 1 day)";//大于等于起始日期 } //收款状态 if ($param['settlement_status']) { $ziyou_where .= " and IFNULL(b.gathering_status,1) = {$param['settlement_status']} "; } //预定时间 if ($param['date_type'] == '1' && $param['start_date']) { $ziyou_where .= " and a.create_date >= '{$param['start_date']}'";//大于等于起始日期 } if ($param['date_type'] == '1' && $param['end_date']) { $ziyou_where .= " and a.create_date < date_add('{$param['end_date']}', INTERVAL 1 day)";//小于结束日期的第二天凌晨 } //渠道商ID if ($param['org_id']) { $ziyou_where .= " and a.outside_sale_org_id = '{$param['org_id']}'"; } //更多查询 if ($param['more_search']) { $ziyou_where .= " and (a.order_title_id like '%{$param['more_search']}%' or a.order_title_prod_name like '%{$param['more_search']}%')"; } /***********************========订单查询========*************************************/ $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.PARENT_ORDER_ID = 0 and a.order_title_id=0 ";//获取所有已完成的有效的主订单 if (!empty($param['prod_type']) and $param['prod_type'] == 'ziyou') { $order_where .= ' and a.order_title_id=1'; } elseif (!empty($param['prod_type'])) { $order_where .= " and a.ORDER_PROD_TYPE in ({$param['prod_type']})"; } //结算时间 if ($param['date_type'] == '2' && $param['start_date']) { $order_where .= " and (SELECT MIN(RUN_DATE) from order_main WHERE a.ORDER_ID = PARENT_ORDER_ID) >= '{$param['start_date']}'";//大于等于起始日期 } if ($param['date_type'] == '2' && $param['end_date']) { $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)";//小于日期 } //收款状态 if ($param['settlement_status']) { $order_where .= " and IFNULL(b.payment_status,1) = {$param['settlement_status']}"; } //预定时间 if ($param['date_type'] == '1' && $param['start_date']) { $order_where .= " and a.CREATE_TIME >= DATE_FORMAT('{$param['start_date']}' ,'%Y-%m-%d %H:%i:%s')";//大于等于起始日期 } if ($param['date_type'] == '1' && $param['end_date']) { $order_where .= " and a.CREATE_TIME < date_add(DATE_FORMAT('{$param['end_date']}' ,'%Y-%m-%d %H:%i:%s'), INTERVAL 1 day)";//小于结束日期的第二天凌晨 } //渠道商ID if ($param['org_id']) { $order_where .= " and a.OUTSIDE_SALE_ORG_ID = '{$param['org_id']}'"; } //更多查询 if ($param['more_search']) { $order_where .= " and (a.ORDER_ID like '%{$param['more_search']}%' or a.PARENT_PROD_NAME like '%{$param['more_search']}%')"; } return array('order_where' => $order_where, 'ziyou_where' => $ziyou_where, 'param' => $param); } /**========检测是否可以生成对账单或取消关账=========**/ public function checkCanCloses($getWhere) { /**============判断是否可以生成对账单===================**/ $sql_1 = " select sum(x.cnt) cnt from( SELECT COUNT(1) cnt from order_title a LEFT JOIN order_finance_status b on a.order_title_id = b.order_id and b.type =1 WHERE {$getWhere['ziyou_where']} and IFNULL(b.gathering_status,1) != 2 union all SELECT count(1) cnt FROM order_main a LEFT JOIN order_finance_status b ON a.ORDER_ID = b.order_id and b.type =1 WHERE {$getWhere['order_where']} and IFNULL(b.gathering_status,1) != 2 )x "; $getTotal = $this->query($sql_1); if ($getTotal['0']['cnt'] >= 1) { return false; } return true; } /*********************对账单列表****************************************/ //获取账单总记录数 public function getBalance($post) { $param = array( 'current_page' => empty($post['current_page']) ? '1' : $post['current_page'],//当前页 'page_size' => empty($post['page_size']) ? '10' : $post['page_size'],//每页展示条数 'org_id' => empty($post['org_id']) ? '' : $post['org_id'],//供应商ID 'batch_id' => empty($post['batch_id']) ? '' : $post['batch_id'],//对账单号 'status' => empty($post['status']) ? '' : $post['status'],//对账状态1待核对,2待结算,3已结算,4已取消 ); $where = array(); $where[] = ' type = 2 '; if (!empty($param['org_id'])) { $where[] = " org_id = {$param['org_id']} "; } if (!empty($param['batch_id'])) { $where[] = " batch_id like '%{$param['batch_id']}%' "; } if (!empty($param['status'])) { $where[] = " status = {$param['status']} "; } $whereStr = ''; if (count($where)) { $whereStr .= implode(' and ', $where); } /**============第一页时返回总记录数和总页数==============**/ if ($param['current_page'] == 1) { $sql_1 = "SELECT count(1) cnt FROM order_finance_balance_account WHERE $whereStr "; $getTotal = $this->query($sql_1); $data['page']['total_count'] = $getTotal[0]['cnt']; $data['page']['total_page'] = (string)ceil($getTotal[0]['cnt'] / $param['page_size']); } /**============获取分页列表数据==============**/ $offset = ($param['current_page'] - 1) * $param['page_size']; $sql_2 = "SELECT id,org_id,org_name,price,commission_price,reparations_price,date,start_date,end_date, account_bank,account_num,account_name,sett_frequency,batch_id, CASE status WHEN 1 THEN '待核对' WHEN 2 THEN '待结算' WHEN 3 THEN '已结算' WHEN 4 THEN '已取消' ELSE 'error' END as 'status_des', status from order_finance_balance_account WHERE $whereStr ORDER BY id DESC LIMIT {$param['page_size']} OFFSET {$offset}"; $list = $this->query($sql_2); $json['code'] = '0'; $json['info'] = '获取列表成功'; $data['order_list'] = $list; $data['page']['page_size'] = $param['page_size']; $data['page']['current_page'] = $param['current_page']; $json['data'] = $data; return $json; } /**取消对账**/ public function cancelBalance($post) { $id = empty($post['id']) ? '' : $post['id'];//账单ID //校验对账单状态 $flag = $this->checkBalanceStatus($id, array(1)); if ($flag == false) { $json['code'] = '2'; $json['info'] = '失败!待核对的账单才可以执行取消操作!'; return $json; } //更改状态 $sql = " update order_finance_balance_account a join order_finance_status b on a.batch_id=b.gathering_batch_id set a.`status` =4,b.gathering_status = 2 WHERE a.`status` in(1) and a.id = $id "; $flag = $this->edit($sql); if ($flag) { $json['code'] = '0'; $json['info'] = '取消成功!'; } else { $json['code'] = '1'; $json['info'] = '取消失败!'; } return $json; } /**确认对账**/ public function confirmedBalance($post) { $id = empty($post['id']) ? '' : $post['id'];//账单ID //校验对账单状态 $flag = $this->checkBalanceStatus($id, array(1)); if ($flag == false) { $json['code'] = '2'; $json['info'] = '失败!待核对的账单才可以确认核对!'; return $json; } //更改状态 $sql = " update order_finance_balance_account a set a.`status` =2 WHERE a.`status` = 1 and a.id = $id "; $flag = $this->edit($sql); if ($flag) { $json['code'] = '0'; $json['info'] = '成功!'; } else { $json['code'] = '1'; $json['info'] = '失败!'; } return $json; } //确认结算 public function balancing($post) { $id = empty($post['id']) ? '' : $post['id'];//账单ID //校验对账单状态 $flag = $this->checkBalanceStatus($id, array(2)); if ($flag == false) { $json['code'] = '2'; $json['info'] = '失败!已核对的账单才可以结算!'; return $json; } //更改状态 $sql = " update order_finance_balance_account a join order_finance_status b on a.batch_id=b.gathering_batch_id set a.`status` =3,b.gathering_status =4 WHERE a.`status` in(2) and a.id = $id "; $flag = $this->edit($sql); if ($flag) { $json['code'] = '0'; $json['info'] = '成功!'; } else { $json['code'] = '1'; $json['info'] = '失败!'; } return $json; } //导出对账订单记录 public function excelBalance() { $id = empty($_GET['id']) ? '' : $_GET['id'];//账单ID $sql1 = "select org_name,start_date,end_date,order_type from order_finance_balance_account WHERE id = {$id}"; $info = $this->query($sql1); if (empty($info)) { return false; } $sql = "select z.* from ( SELECT x.order_id '订单ID',x.outside_order_no '渠道订单ID',x.start_date '出游时间', x.create_time '预定时间','' as 'order_description', x.order_title_prod_name '产品名称','巴士自由行' as '产品类型', '' as '房型/票种', 0 as '间夜/票数', x.order_title_all_price '订单金额',x.order_title_all_affi '佣金',x.reparations '赔款', '{$info[0]['org_name']}' as '供应商' from ( SELECT a.order_title_id 'order_id' ,a.outside_order_no ,a.start_date , a.create_time,a.order_title_prod_name ,a.order_title_all_price ,a.order_title_all_affi , IFNULL((SELECT reparations from order_finance_reparations WHERE order_id=a.order_title_id),0) as 'reparations' from order_title a JOIN order_finance_status b on a.order_title_id = b.order_id and b.type =1 join order_finance_balance_account d on b.gathering_batch_id = d.batch_id LEFT JOIN order_finance_reparations c on a.order_title_id = c.order_id WHERE d.id={$id} ) x GROUP BY x.order_id UNION ALL SELECT c.ORDER_ID '订单ID',c.OUTSIDE_ORDER_NO '渠道订单ID', (SELECT MAX(RUN_DATE) from order_main WHERE PARENT_ORDER_ID = c.ORDER_ID) '出游时间', c.CREATE_TIME '预定时间', c.ORDER_DESCRIPTION 'order_description', (SELECT CONCAT(PARENT_PROD_NAME, '
',PROD_START_STATION_RES_NAME,' - ',PROD_END_STATION_RES_NAME) from order_main WHERE PARENT_ORDER_ID = c.ORDER_ID LIMIT 1) '产品名称', CASE WHEN c.ORDER_PROD_TYPE in (25,26) THEN '酒店' WHEN c.ORDER_PROD_TYPE in (81,82,38,369) THEN '车票' WHEN c.ORDER_PROD_TYPE in (258,282,311) THEN '门票' else '' END as '产品类型', '' as '房型/票种',0 as '间夜/票数', c.ORDER_PRICE '订单金额',c.TOTAL_COMMISSION '佣金', IFNULL((SELECT reparations from order_finance_reparations WHERE order_id=c.ORDER_ID),0) as '赔款', a.org_name '供应商' from order_finance_balance_account a JOIN order_finance_status b on a.batch_id = b.gathering_batch_id and b.type =1 JOIN order_main c on b.order_id=c.ORDER_ID LEFT JOIN order_finance_reparations d on b.order_id=d.order_id WHERE a.id={$id} )z ORDER BY z.出游时间 "; $list = $this->query($sql); if (empty($list) == false) { foreach ($list as $key => $value) { $order_description = $value['order_description']; unset($list[$key]['order_description']); if(empty($order_description)) { continue; } $list[$key]['间页/票数'] = 0; $list[$key]['房型/票种'] = ''; $scripTionArr = explode('|', $order_description); foreach ($scripTionArr as $vl) { if (empty($vl)) { continue; } $vls = explode(',', $vl); if (empty($vls['2'])) { $list[$key]['间页/票数'] = $vls[0]; } else { $list[$key]['间页/票数'] = $vls['2']; $list[$key]['房型/票种'] = $vls['0']; } } } } /**======生成excel=========**/ ob_clean(); $fileName = '【' . $info[0]['org_name'] . '】应收账单-' . $info[0]['start_date'] . '~' . $info[0]['end_date'] . '.csv'; $fileName = iconv("utf-8", "gb2312", $fileName); header("Content-type:text/csv"); header("Content-Disposition:attachment;filename=" . $fileName); header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); header('Expires:0'); header('Pragma:public'); $data = ''; foreach ($list as $key => $val) { if ($key == 0) { $data .= implode(',', array_keys($val)) . "\n"; } $data .= '"' . implode('","', array_values($val)) . "\"\n"; } $data = chr(0xEF) . chr(0xBB) . chr(0xBF) . $data; echo $data; return ''; } //检测对账单状态 public function checkBalanceStatus($id, $status) { $sql = "SELECT `status` from order_finance_balance_account WHERE id = $id"; $info = $this->query($sql); if (empty($info[0]['status']) || !in_array($info[0]['status'], $status)) { return false; } return true; } }