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.
 
 
 
 

211 lines
10 KiB

  1. <?php
  2. require_once __DIR__ . '/../HotelLib.php';
  3. /**
  4. * Created by PhpStorm.
  5. * User: luocj
  6. * Date: 2016/12/14
  7. * Time: 11:05
  8. */
  9. class LibStatisticalReport extends HotelLib
  10. {
  11. //
  12. function hotelSale($date_type, $start_date, $end_date, $province, $city, $hotel_name, $supplier_id, $org_sale_id)
  13. {
  14. $obj = new LibStatisticalReport;
  15. $before_date = $obj->getBeforeDate($start_date, $end_date);
  16. // 前月日期
  17. if ($before_date) {
  18. $before_start_date = $before_date[0];
  19. $before_end_date = $before_date[1];
  20. }
  21. // 1为预订日期,2为入住日期,3为离店日期
  22. if (!$date_type) {
  23. $date_type_sql = "";
  24. $date_before_type_sql = "";
  25. } elseif ($date_type == 1) {
  26. $date_type_sql = "and (a.create_time between'{$start_date} 00:00:00' and '{$end_date} 23:59:59')";
  27. $date_before_type_sql = "and (a.create_time between'{$before_start_date} 00:00:00' and '{$before_end_date} 23:59:59')";
  28. } elseif ($date_type == 2) {
  29. $date_type_sql = "and (a.prod_start_station_date between'{$start_date} 00:00:00' and '{$end_date} 23:59:59')";
  30. $date_before_type_sql = "and (a.prod_start_station_date between'{$before_start_date} 00:00:00' and '{$before_end_date} 23:59:59')";
  31. } elseif ($date_type == 3) {
  32. $date_type_sql = "and (a.prod_end_station_date between'{$start_date} 00:00:00' and '{$end_date} 23:59:59')";
  33. $date_before_type_sql = "and (a.prod_end_station_date between'{$before_start_date} 00:00:00' and '{$before_end_date} 23:59:59')";
  34. }
  35. // 省市区
  36. if ($province == 0) {
  37. $province_sql = "";
  38. } else {
  39. $province_sql = "and c.top_area_id = {$province}";
  40. }
  41. if ($city == 0) {
  42. $city_sql = "";
  43. } else {
  44. $city_sql = "and (c.parent_area_id = {$city} or c.AREA_ID = {$city})";
  45. }
  46. // 酒店名称
  47. if ($hotel_name == '') {
  48. $hotel_name_sql = "";
  49. } else {
  50. $hotel_name_sql = "and a.parent_prod_name like '%{$hotel_name}%'";
  51. }
  52. //供应商名称
  53. if ($supplier_id == 0) {
  54. $supplier_id_sql = '';
  55. } else {
  56. $supplier_id_sql = "and a.prod_top_org_id = {$supplier_id}";
  57. }
  58. //销售渠道
  59. if ($org_sale_id == 0) {
  60. $org_sale_id_sql = "";
  61. } else {
  62. $org_sale_id_sql = "and a.outside_sale_org_id = {$org_sale_id}";
  63. }
  64. //查询当前日期
  65. $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
  66. from
  67. (
  68. 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
  69. from order_main a,order_main b inner join base_area_view c on b.prod_start_station_area_id = c.area_id
  70. 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}
  71. group by hotel_name,a.order_id
  72. ) as t
  73. group by hotel_name order by jianyecount desc";
  74. writeLog('当日财务报表' . $sql);
  75. $rowset = $this->DBTool->queryBySql($sql);
  76. $data = array();
  77. $data['code'] = $rowset['code'];
  78. $data['info'] = $rowset['info'];
  79. $data['rowset'] = $rowset['rowset'];
  80. //查询环比日期
  81. $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
  82. from
  83. (
  84. 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
  85. from order_main a,order_main b inner join base_area_view c on b.prod_start_station_area_id = c.area_id
  86. 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}
  87. group by hotel_name,a.order_id
  88. ) as t
  89. group by hotel_name order by jianyecount desc";
  90. writeLog('环比财务报表' . $sql_before);
  91. $rowset_before = $this->DBTool->queryBySql($sql_before);
  92. $temp_rowset1 = array();
  93. foreach ($rowset_before['rowset'] as $k => $v) {
  94. $temp_rowset1[$v['hotel_name']] = $v;
  95. }
  96. foreach ($data['rowset'] as $k => $v) {
  97. if (isset($temp_rowset1[$v['hotel_name']])) {
  98. $data['rowset'][$k]['order_num_before'] = $temp_rowset1[$v['hotel_name']]['order_num'];
  99. $data['rowset'][$k]['jianyecount_before'] = $temp_rowset1[$v['hotel_name']]['jianyecount'];
  100. $data['rowset'][$k]['all_order_price_before'] = $temp_rowset1[$v['hotel_name']]['all_order_price'];
  101. $data['rowset'][$k]['all_base_price_before'] = $temp_rowset1[$v['hotel_name']]['all_base_price'];
  102. $data['rowset'][$k]['all_total_commission_before'] = $temp_rowset1[$v['hotel_name']]['all_total_commission'];
  103. $data['rowset'][$k]['all_profit_before'] = $temp_rowset1[$v['hotel_name']]['all_profit'];
  104. }
  105. }
  106. //得总计;
  107. $temp_all = array();
  108. $temp_all['order_num'] = 0;
  109. $temp_all['jianyecount'] = 0;
  110. $temp_all['all_order_price'] = 0;
  111. $temp_all['all_base_price'] = 0;
  112. $temp_all['all_total_commission'] = 0;
  113. $temp_all['all_profit'] = 0;
  114. $temp_all['all_order_price_before'] = 0;
  115. $temp_all['order_num_before'] = 0;
  116. $temp_all['jianyecount_before'] = 0;
  117. $temp_all['all_base_price_before'] = 0;
  118. $temp_all['all_total_commission_before'] = 0;
  119. $temp_all['all_profit_before'] = 0;
  120. foreach ($data['rowset'] as $v) {
  121. if (isset($v['order_num'])) {
  122. $temp_all['order_num'] += $v['order_num'];
  123. }
  124. if (isset($v['jianyecount'])) {
  125. $temp_all['jianyecount'] += $v['jianyecount'];
  126. }
  127. if (isset($v['all_order_price'])) {
  128. $temp_all['all_order_price'] += $v['all_order_price'];
  129. }
  130. if (isset($v['all_base_price'])) {
  131. $temp_all['all_base_price'] += $v['all_base_price'];
  132. }
  133. if (isset($v['all_total_commission'])) {
  134. $temp_all['all_total_commission'] += $v['all_total_commission'];
  135. }
  136. if (isset($v['all_order_price_before'])) {
  137. $temp_all['all_order_price_before'] += $v['all_order_price_before'];
  138. }
  139. if (isset($v['jianyecount_before'])) {
  140. $temp_all['jianyecount_before'] += $v['jianyecount_before'];
  141. }
  142. if (isset($v['all_base_price_before'])) {
  143. $temp_all['all_base_price_before'] += $v['all_base_price_before'];
  144. }
  145. if (isset($v['all_total_commission_before'])) {
  146. $temp_all['all_total_commission_before'] += $v['all_total_commission_before'];
  147. }
  148. if (isset($v['all_profit_before'])) {
  149. $temp_all['all_profit_before'] += $v['all_profit_before'];
  150. }
  151. if (isset($v['order_num_before'])) {
  152. $temp_all['order_num_before'] += $v['order_num_before'];
  153. }
  154. if (isset($v['all_profit'])) {
  155. $temp_all['all_profit'] += $v['all_profit'];
  156. }
  157. $data['sum'] = $temp_all;
  158. }
  159. return $data;
  160. }
  161. //获取同比日期函数
  162. function getBeforeDate($start_date, $end_date)
  163. {
  164. //判断所选日期是否为月头和月尾,如果是则比较上一整月,如果不是则减去当月月数-1拼上原来的天数。
  165. //当月月头
  166. $day_first = date("Y-m-01", strtotime("$start_date"));
  167. //当月月尾
  168. $day_last = date("Y-m-t", strtotime("$start_date"));
  169. if ($start_date == $day_first && $end_date == $day_last) {
  170. $timestamp = strtotime($start_date);
  171. $arr = getdate($timestamp);
  172. if ($arr['mon'] == 1) {
  173. $year = $arr['year'] - 1;
  174. $month = $arr['mon'] + 11;
  175. $firstday = $year . '-' . $month . '-01';
  176. $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
  177. } else {
  178. $firstday = date('Y-m-01', strtotime(date('Y', $timestamp) . '-' . (date('m', $timestamp) - 1) . '-01'));
  179. $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
  180. }
  181. } else {
  182. $start_arr = getdate(strtotime($start_date));
  183. $end_arr = getdate(strtotime($end_date));
  184. if ($start_arr['mon'] == 1) {
  185. $start_year = $start_arr['year'] - 1;
  186. $start_month = $start_arr['mon'] + 11;
  187. } else {
  188. $start_year = $start_arr['year'];
  189. $start_month = $start_arr['mon'] - 1;
  190. }
  191. if ($end_arr['mon'] == 1) {
  192. $end_year = $end_arr['year'] - 1;
  193. $end_month = $end_arr['mon'] + 11;
  194. } else {
  195. $end_year = $end_arr['year'];
  196. $end_month = $end_arr['mon'] - 1;
  197. }
  198. $firstday = $start_year . '-' . $start_month . '-' . date('d', strtotime("$start_date"));
  199. $lastday = $end_year . '-' . $end_month . '-' . date('d', strtotime("$end_date"));
  200. }
  201. return array($firstday, $lastday);
  202. }
  203. }
  204. //$obj = new LibStatisticalReport();
  205. //$data = $obj->hotelSale(1, '2016-12-01', '2016-12-31', '', '', '', '', '');
  206. //echo json_encode($data);