|
- <?php
- require_once __DIR__ . '/../HotelLib.php';
-
- /**
- * Created by PhpStorm.
- * User: luocj
- * Date: 2016/12/14
- * Time: 11:05
- */
- class LibStatisticalReport extends HotelLib
- {
- //
- function hotelSale($date_type, $start_date, $end_date, $province, $city, $hotel_name, $supplier_id, $org_sale_id)
- {
- $obj = new LibStatisticalReport;
- $before_date = $obj->getBeforeDate($start_date, $end_date);
- // 前月日期
- if ($before_date) {
- $before_start_date = $before_date[0];
- $before_end_date = $before_date[1];
- }
-
- // 1为预订日期,2为入住日期,3为离店日期
- if (!$date_type) {
- $date_type_sql = "";
- $date_before_type_sql = "";
- } elseif ($date_type == 1) {
- $date_type_sql = "and (a.create_time between'{$start_date} 00:00:00' and '{$end_date} 23:59:59')";
- $date_before_type_sql = "and (a.create_time between'{$before_start_date} 00:00:00' and '{$before_end_date} 23:59:59')";
- } elseif ($date_type == 2) {
- $date_type_sql = "and (a.prod_start_station_date between'{$start_date} 00:00:00' and '{$end_date} 23:59:59')";
- $date_before_type_sql = "and (a.prod_start_station_date between'{$before_start_date} 00:00:00' and '{$before_end_date} 23:59:59')";
- } elseif ($date_type == 3) {
- $date_type_sql = "and (a.prod_end_station_date between'{$start_date} 00:00:00' and '{$end_date} 23:59:59')";
- $date_before_type_sql = "and (a.prod_end_station_date between'{$before_start_date} 00:00:00' and '{$before_end_date} 23:59:59')";
- }
- // 省市区
- if ($province == 0) {
- $province_sql = "";
- } else {
- $province_sql = "and c.top_area_id = {$province}";
- }
- if ($city == 0) {
- $city_sql = "";
- } else {
- $city_sql = "and (c.parent_area_id = {$city} or c.AREA_ID = {$city})";
- }
- // 酒店名称
- if ($hotel_name == '') {
- $hotel_name_sql = "";
- } else {
- $hotel_name_sql = "and a.parent_prod_name like '%{$hotel_name}%'";
- }
- //供应商名称
- if ($supplier_id == 0) {
- $supplier_id_sql = '';
- } else {
- $supplier_id_sql = "and a.prod_top_org_id = {$supplier_id}";
- }
- //销售渠道
- if ($org_sale_id == 0) {
- $org_sale_id_sql = "";
- } else {
- $org_sale_id_sql = "and a.outside_sale_org_id = {$org_sale_id}";
- }
- //查询当前日期
- $sql = "select hotel_name, count(order_id) as order_num, sum(jianyecount) as jianyecount, sum(all_order_price) as all_order_price, sum(all_base_price) as all_base_price, sum(all_total_commission) as all_total_commission, sum(all_profit) as all_profit
- from
- (
- select a.parent_prod_name as hotel_name, a.order_id, a.order_price as all_order_price, count(b.order_id) as jianyecount, a.order_price-a.total_commission-a.profit_value as all_base_price, a.total_commission as all_total_commission, a.profit_value as all_profit
- from order_main a,order_main b inner join base_area_view c on b.prod_start_station_area_id = c.area_id
- where a.order_id =b.parent_order_id and a.order_status in (313,198,314,147) and a.order_prod_type = 25 and b.order_prod_type = 26 and a.cancel_flag = 0 and b.cancel_flag= 0 {$date_type_sql} {$province_sql} {$city_sql} {$hotel_name_sql} {$supplier_id_sql} {$org_sale_id_sql}
- group by hotel_name,a.order_id
- ) as t
- group by hotel_name order by jianyecount desc";
- writeLog('当日财务报表' . $sql);
- $rowset = $this->DBTool->queryBySql($sql);
- $data = array();
- $data['code'] = $rowset['code'];
- $data['info'] = $rowset['info'];
- $data['rowset'] = $rowset['rowset'];
- //查询环比日期
- $sql_before = "select hotel_name, count(order_id) as order_num, sum(jianyecount) as jianyecount, sum(all_order_price) as all_order_price, sum(all_base_price) as all_base_price, sum(all_total_commission) as all_total_commission, sum(all_profit) as all_profit
- from
- (
- select a.parent_prod_name as hotel_name, a.order_id, a.order_price as all_order_price, count(b.order_id) as jianyecount, a.order_price-a.total_commission-a.profit_value as all_base_price, a.total_commission as all_total_commission, a.profit_value as all_profit
- from order_main a,order_main b inner join base_area_view c on b.prod_start_station_area_id = c.area_id
- where a.order_id =b.parent_order_id and a.order_status in (313,198,314,147) and a.order_prod_type = 25 and b.order_prod_type = 26 and a.cancel_flag = 0 and b.cancel_flag= 0 {$date_before_type_sql} {$province_sql} {$city_sql} {$hotel_name_sql} {$supplier_id_sql} {$org_sale_id_sql}
- group by hotel_name,a.order_id
- ) as t
- group by hotel_name order by jianyecount desc";
- writeLog('环比财务报表' . $sql_before);
- $rowset_before = $this->DBTool->queryBySql($sql_before);
- $temp_rowset1 = array();
- foreach ($rowset_before['rowset'] as $k => $v) {
- $temp_rowset1[$v['hotel_name']] = $v;
- }
- foreach ($data['rowset'] as $k => $v) {
- if (isset($temp_rowset1[$v['hotel_name']])) {
- $data['rowset'][$k]['order_num_before'] = $temp_rowset1[$v['hotel_name']]['order_num'];
- $data['rowset'][$k]['jianyecount_before'] = $temp_rowset1[$v['hotel_name']]['jianyecount'];
- $data['rowset'][$k]['all_order_price_before'] = $temp_rowset1[$v['hotel_name']]['all_order_price'];
- $data['rowset'][$k]['all_base_price_before'] = $temp_rowset1[$v['hotel_name']]['all_base_price'];
- $data['rowset'][$k]['all_total_commission_before'] = $temp_rowset1[$v['hotel_name']]['all_total_commission'];
- $data['rowset'][$k]['all_profit_before'] = $temp_rowset1[$v['hotel_name']]['all_profit'];
- }
- }
- //得总计;
- $temp_all = array();
- $temp_all['order_num'] = 0;
- $temp_all['jianyecount'] = 0;
- $temp_all['all_order_price'] = 0;
- $temp_all['all_base_price'] = 0;
- $temp_all['all_total_commission'] = 0;
- $temp_all['all_profit'] = 0;
- $temp_all['all_order_price_before'] = 0;
- $temp_all['order_num_before'] = 0;
- $temp_all['jianyecount_before'] = 0;
- $temp_all['all_base_price_before'] = 0;
- $temp_all['all_total_commission_before'] = 0;
- $temp_all['all_profit_before'] = 0;
- foreach ($data['rowset'] as $v) {
- if (isset($v['order_num'])) {
- $temp_all['order_num'] += $v['order_num'];
- }
- if (isset($v['jianyecount'])) {
- $temp_all['jianyecount'] += $v['jianyecount'];
- }
- if (isset($v['all_order_price'])) {
- $temp_all['all_order_price'] += $v['all_order_price'];
- }
- if (isset($v['all_base_price'])) {
- $temp_all['all_base_price'] += $v['all_base_price'];
- }
- if (isset($v['all_total_commission'])) {
- $temp_all['all_total_commission'] += $v['all_total_commission'];
- }
- if (isset($v['all_order_price_before'])) {
- $temp_all['all_order_price_before'] += $v['all_order_price_before'];
- }
- if (isset($v['jianyecount_before'])) {
- $temp_all['jianyecount_before'] += $v['jianyecount_before'];
- }
- if (isset($v['all_base_price_before'])) {
- $temp_all['all_base_price_before'] += $v['all_base_price_before'];
- }
- if (isset($v['all_total_commission_before'])) {
- $temp_all['all_total_commission_before'] += $v['all_total_commission_before'];
- }
- if (isset($v['all_profit_before'])) {
- $temp_all['all_profit_before'] += $v['all_profit_before'];
- }
- if (isset($v['order_num_before'])) {
- $temp_all['order_num_before'] += $v['order_num_before'];
- }
- if (isset($v['all_profit'])) {
- $temp_all['all_profit'] += $v['all_profit'];
- }
- $data['sum'] = $temp_all;
- }
- return $data;
- }
-
- //获取同比日期函数
- function getBeforeDate($start_date, $end_date)
- {
- //判断所选日期是否为月头和月尾,如果是则比较上一整月,如果不是则减去当月月数-1拼上原来的天数。
- //当月月头
- $day_first = date("Y-m-01", strtotime("$start_date"));
- //当月月尾
- $day_last = date("Y-m-t", strtotime("$start_date"));
- if ($start_date == $day_first && $end_date == $day_last) {
- $timestamp = strtotime($start_date);
- $arr = getdate($timestamp);
- if ($arr['mon'] == 1) {
- $year = $arr['year'] - 1;
- $month = $arr['mon'] + 11;
- $firstday = $year . '-' . $month . '-01';
- $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
- } else {
- $firstday = date('Y-m-01', strtotime(date('Y', $timestamp) . '-' . (date('m', $timestamp) - 1) . '-01'));
- $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
- }
- } else {
- $start_arr = getdate(strtotime($start_date));
- $end_arr = getdate(strtotime($end_date));
- if ($start_arr['mon'] == 1) {
- $start_year = $start_arr['year'] - 1;
- $start_month = $start_arr['mon'] + 11;
- } else {
- $start_year = $start_arr['year'];
- $start_month = $start_arr['mon'] - 1;
- }
- if ($end_arr['mon'] == 1) {
- $end_year = $end_arr['year'] - 1;
- $end_month = $end_arr['mon'] + 11;
- } else {
- $end_year = $end_arr['year'];
- $end_month = $end_arr['mon'] - 1;
- }
- $firstday = $start_year . '-' . $start_month . '-' . date('d', strtotime("$start_date"));
- $lastday = $end_year . '-' . $end_month . '-' . date('d', strtotime("$end_date"));
- }
- return array($firstday, $lastday);
- }
- }
-
- //$obj = new LibStatisticalReport();
- //$data = $obj->hotelSale(1, '2016-12-01', '2016-12-31', '', '', '', '', '');
- //echo json_encode($data);
|