VM暫存
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

697 行
31KB

  1. <?php
  2. /*
  3. file: Excel_model.php 匯出報表專用
  4. */
  5. class Excel_model extends CI_Model
  6. {
  7. function __construct()
  8. {
  9. parent::__construct();
  10. $this->load->database();
  11. $this->now_str = date('Y-m-d H:i:s');
  12. ini_set('max_execution_time','300');
  13. ini_set('memory_limit','512M');
  14. // 載入 excel
  15. $this->load->library('excel');
  16. }
  17. public function init($vars)
  18. {
  19. $this->vars = $vars;
  20. }
  21. // 會員名單報表
  22. public function export_members()
  23. {
  24. trigger_error(EXPORT_LOG_TITLE. '..start..' . __FUNCTION__);
  25. // 讀入廠站資料
  26. $sql = "
  27. select
  28. members.member_name as member_name,
  29. members.lpr as lpr,
  30. members.contract_no as contract_no,
  31. members.start_date as start_date,
  32. members.end_date as end_date,
  33. members.amt as amt,
  34. members.update_time as update_time,
  35. members.member_attr,
  36. members.fee_period,
  37. members.mobile_no,
  38. members.deposit,
  39. members.suspended,
  40. members.locked,
  41. members.valid_time
  42. from members
  43. ORDER BY update_time DESC
  44. ";
  45. $results = $this->db->query($sql)->result_array();
  46. if(empty($results))
  47. {
  48. trigger_error(EXPORT_LOG_TITLE.'..no data..' . $this->db->last_query());
  49. return false;
  50. }
  51. //$total_count = $this->db->query($sql)->num_rows();
  52. // 產生 Excel
  53. $objPHPExcel = new PHPExcel();
  54. $objPHPExcel->setActiveSheetIndex(0);
  55. $col_A_mapping = array('col_name' => 'A', 'col_title' => '會員名稱', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  56. $col_B_mapping = array('col_name' => 'B', 'col_title' => '車牌號碼', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  57. $col_C_mapping = array('col_name' => 'C', 'col_title' => '合約代碼', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  58. $col_D_mapping = array('col_name' => 'D', 'col_title' => '開始時間', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  59. $col_E_mapping = array('col_name' => 'E', 'col_title' => '結束時間', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  60. $col_F_mapping = array('col_name' => 'F', 'col_title' => '租金', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  61. $col_G_mapping = array('col_name' => 'G', 'col_title' => '最後更新時間', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  62. $col_H_mapping = array('col_name' => 'H', 'col_title' => '身份', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  63. $col_I_mapping = array('col_name' => 'I', 'col_title' => '繳期', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  64. $col_J_mapping = array('col_name' => 'J', 'col_title' => '電話', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  65. $col_K_mapping = array('col_name' => 'K', 'col_title' => '押金', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  66. $col_L_mapping = array('col_name' => 'L', 'col_title' => '停權 (營管操作)', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  67. $col_M_mapping = array('col_name' => 'M', 'col_title' => '鎖車 (會員操作)', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  68. $col_N_mapping = array('col_name' => 'N', 'col_title' => '有效期限 (審核後更新)', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  69. $raw_index = 1;
  70. $objPHPExcel->getActiveSheet()->setTitle('下載');
  71. $objPHPExcel->getActiveSheet()->setCellValue($col_A_mapping['col_name'].$raw_index, $col_A_mapping['col_title']);
  72. $objPHPExcel->getActiveSheet()->setCellValue($col_B_mapping['col_name'].$raw_index, $col_B_mapping['col_title']);
  73. $objPHPExcel->getActiveSheet()->setCellValue($col_C_mapping['col_name'].$raw_index, $col_C_mapping['col_title']);
  74. $objPHPExcel->getActiveSheet()->setCellValue($col_D_mapping['col_name'].$raw_index, $col_D_mapping['col_title']);
  75. $objPHPExcel->getActiveSheet()->setCellValue($col_E_mapping['col_name'].$raw_index, $col_E_mapping['col_title']);
  76. $objPHPExcel->getActiveSheet()->setCellValue($col_F_mapping['col_name'].$raw_index, $col_F_mapping['col_title']);
  77. $objPHPExcel->getActiveSheet()->setCellValue($col_G_mapping['col_name'].$raw_index, $col_G_mapping['col_title']);
  78. $objPHPExcel->getActiveSheet()->setCellValue($col_H_mapping['col_name'].$raw_index, $col_H_mapping['col_title']);
  79. $objPHPExcel->getActiveSheet()->setCellValue($col_I_mapping['col_name'].$raw_index, $col_I_mapping['col_title']);
  80. $objPHPExcel->getActiveSheet()->setCellValue($col_J_mapping['col_name'].$raw_index, $col_J_mapping['col_title']);
  81. $objPHPExcel->getActiveSheet()->setCellValue($col_K_mapping['col_name'].$raw_index, $col_K_mapping['col_title']);
  82. $objPHPExcel->getActiveSheet()->setCellValue($col_L_mapping['col_name'].$raw_index, $col_L_mapping['col_title']);
  83. $objPHPExcel->getActiveSheet()->setCellValue($col_M_mapping['col_name'].$raw_index, $col_M_mapping['col_title']);
  84. $objPHPExcel->getActiveSheet()->setCellValue($col_N_mapping['col_name'].$raw_index, $col_N_mapping['col_title']);
  85. $warning_style = array(
  86. //'alignment' => array('horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER),
  87. 'font' => array(
  88. 'bold' => true,
  89. 'color' => array('rgb' => 'FF0000'),
  90. 'size' => 16,
  91. 'name' => 'Verdana'
  92. )
  93. );
  94. $hq_info = $this->vars['mcache']->get('info');
  95. $count = 0;
  96. foreach($results as $rows)
  97. {
  98. $raw_index += 1;
  99. $member_name = $rows['member_name'];
  100. $lpr = $rows['lpr'];
  101. $contract_no = $rows['contract_no'] ? $rows['contract_no'] : '';
  102. $start_date = $rows['start_date'];
  103. $end_date = $rows['end_date'];
  104. $amt = $rows['amt'] ? $rows['amt'] : '0';
  105. $update_time = $rows['update_time'];
  106. $member_attr = ( empty($hq_info['member_attr']) || empty($rows['member_attr']) || empty($hq_info['member_attr'][$rows['member_attr']]) ) ? '無' : $hq_info['member_attr'][$rows['member_attr']];
  107. $fee_period = ( empty($hq_info['period_name']) || empty($rows['fee_period']) || empty($hq_info['period_name'][$rows['fee_period']]) ) ? '無' : $hq_info['period_name'][$rows['fee_period']];
  108. $mobile_no = $rows['mobile_no'];
  109. $deposit = $rows['deposit'];
  110. $suspended = (empty($rows['suspended'])) ? '無' : '已停權';
  111. $locked = (empty($rows['locked'])) ? '無' : '已鎖車';
  112. $valid_time = $rows['valid_time'];
  113. $count++;
  114. $objPHPExcel->getActiveSheet()->setCellValueExplicit($col_A_mapping['col_name'].$raw_index, $member_name, $col_A_mapping['col_type']);
  115. $objPHPExcel->getActiveSheet()->setCellValueExplicit($col_B_mapping['col_name'].$raw_index, $lpr, $col_B_mapping['col_type']);
  116. $objPHPExcel->getActiveSheet()->setCellValueExplicit($col_C_mapping['col_name'].$raw_index, $contract_no, $col_C_mapping['col_type']);
  117. $objPHPExcel->getActiveSheet()->setCellValueExplicit($col_D_mapping['col_name'].$raw_index, $start_date, $col_D_mapping['col_type']);
  118. $objPHPExcel->getActiveSheet()->setCellValueExplicit($col_E_mapping['col_name'].$raw_index, $end_date, $col_E_mapping['col_type']);
  119. $objPHPExcel->getActiveSheet()->setCellValueExplicit($col_F_mapping['col_name'].$raw_index, $amt, $col_F_mapping['col_type']);
  120. $objPHPExcel->getActiveSheet()->setCellValueExplicit($col_G_mapping['col_name'].$raw_index, $update_time, $col_G_mapping['col_type']);
  121. $objPHPExcel->getActiveSheet()->setCellValueExplicit($col_H_mapping['col_name'].$raw_index, $member_attr, $col_H_mapping['col_type']);
  122. $objPHPExcel->getActiveSheet()->setCellValueExplicit($col_I_mapping['col_name'].$raw_index, $fee_period, $col_I_mapping['col_type']);
  123. $objPHPExcel->getActiveSheet()->setCellValueExplicit($col_J_mapping['col_name'].$raw_index, $mobile_no, $col_J_mapping['col_type']);
  124. $objPHPExcel->getActiveSheet()->setCellValueExplicit($col_K_mapping['col_name'].$raw_index, $deposit, $col_K_mapping['col_type']);
  125. $objPHPExcel->getActiveSheet()->setCellValueExplicit($col_L_mapping['col_name'].$raw_index, $suspended, $col_L_mapping['col_type']);
  126. $objPHPExcel->getActiveSheet()->setCellValueExplicit($col_M_mapping['col_name'].$raw_index, $locked, $col_M_mapping['col_type']);
  127. $objPHPExcel->getActiveSheet()->setCellValueExplicit($col_N_mapping['col_name'].$raw_index, $valid_time, $col_N_mapping['col_type']);
  128. // 設定 style
  129. if($valid_time != $end_date)
  130. {
  131. $objPHPExcel->getActiveSheet()->getStyle($col_N_mapping['col_name'].$raw_index)->applyFromArray($warning_style);
  132. }
  133. }
  134. // 網站下載
  135. $filename_prefix = iconv('UTF-8', 'Big5', '會員資料 - '. STATION_NAME);
  136. $filename_postfix = iconv('UTF-8', 'Big5', '(現況)');
  137. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  138. header('Content-Type: application/vnd.ms-excel');
  139. header('Content-Disposition: attachment;filename="' . $filename_prefix. ' - ' . $filename_postfix . '.xlsx');
  140. header('Cache-Control: max-age=0');
  141. $objWriter->save('php://output');
  142. trigger_error(EXPORT_LOG_TITLE . '..completed..' . __FUNCTION__ . '|count:' . $count);
  143. return true;
  144. }
  145. // 進出記錄報表
  146. public function export_cario_data($query_year, $query_month)
  147. {
  148. ini_set('max_execution_time','300');
  149. ini_set('memory_limit','512M');
  150. trigger_error(EXPORT_LOG_TITLE. '..start..' . __FUNCTION__ . "|{$query_year},{$query_month}");
  151. // 讀入廠站資料
  152. $sql = "
  153. SELECT
  154. cario.obj_id AS plate_no,
  155. cario.in_time as in_time,
  156. cario.out_time as out_time,
  157. members.member_name as member_name,
  158. CONCAT( FLOOR(HOUR(TIMEDIFF(cario.in_time, cario.out_time)) / 24), ' 日 ',
  159. MOD(HOUR(TIMEDIFF(cario.in_time, cario.out_time)), 24), ' 時 ',
  160. MINUTE(TIMEDIFF(cario.in_time, cario.out_time)), ' 分') as time_period
  161. FROM cario
  162. left join members on cario.member_no = members.member_no
  163. WHERE cario.err = 0 and cario.obj_id != 'NONE'
  164. and YEAR(cario.in_time) = {$query_year} and MONTH(cario.in_time) = {$query_month}
  165. and cario.out_time is not null
  166. ORDER BY cario.in_time ASC
  167. ";
  168. $results = $this->db->query($sql)->result_array();
  169. if(empty($results))
  170. {
  171. trigger_error(EXPORT_LOG_TITLE.'..no data..' . $this->db->last_query());
  172. return false;
  173. }
  174. //$total_count = $this->db->query($sql)->num_rows();
  175. // 產生 Excel
  176. $objPHPExcel = new PHPExcel();
  177. $objPHPExcel->setActiveSheetIndex(0);
  178. $col_A_mapping = array('col_name' => 'A', 'col_title' => '車牌號碼', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  179. $col_B_mapping = array('col_name' => 'B', 'col_title' => '進場時間', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  180. $col_C_mapping = array('col_name' => 'C', 'col_title' => '離場日期', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  181. $col_D_mapping = array('col_name' => 'D', 'col_title' => '停車時數', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  182. $col_E_mapping = array('col_name' => 'E', 'col_title' => '場站名稱', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  183. $col_F_mapping = array('col_name' => 'F', 'col_title' => '會員名稱', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  184. $raw_index = 1;
  185. $objPHPExcel->getActiveSheet()->setTitle('下載');
  186. $objPHPExcel->getActiveSheet()->setCellValue($col_A_mapping['col_name'].$raw_index, $col_A_mapping['col_title']);
  187. $objPHPExcel->getActiveSheet()->setCellValue($col_B_mapping['col_name'].$raw_index, $col_B_mapping['col_title']);
  188. $objPHPExcel->getActiveSheet()->setCellValue($col_C_mapping['col_name'].$raw_index, $col_C_mapping['col_title']);
  189. $objPHPExcel->getActiveSheet()->setCellValue($col_D_mapping['col_name'].$raw_index, $col_D_mapping['col_title']);
  190. $objPHPExcel->getActiveSheet()->setCellValue($col_E_mapping['col_name'].$raw_index, $col_E_mapping['col_title']);
  191. $objPHPExcel->getActiveSheet()->setCellValue($col_F_mapping['col_name'].$raw_index, $col_F_mapping['col_title']);
  192. $count = 0;
  193. foreach($results as $rows)
  194. {
  195. $raw_index += 1;
  196. $plate_no = $rows['plate_no'];
  197. $in_time = $rows['in_time'];
  198. $out_time = $rows['out_time'];
  199. $time_period = $rows['time_period'];
  200. $member_name = $rows['member_name'] ? $rows['member_name'] : '臨停';
  201. $count++;
  202. $objPHPExcel->getActiveSheet()->setCellValueExplicit($col_A_mapping['col_name'].$raw_index, $plate_no, $col_A_mapping['col_type']);
  203. $objPHPExcel->getActiveSheet()->setCellValueExplicit($col_B_mapping['col_name'].$raw_index, $in_time, $col_B_mapping['col_type']);
  204. $objPHPExcel->getActiveSheet()->setCellValueExplicit($col_C_mapping['col_name'].$raw_index, $out_time, $col_C_mapping['col_type']);
  205. $objPHPExcel->getActiveSheet()->setCellValueExplicit($col_D_mapping['col_name'].$raw_index, $time_period, $col_D_mapping['col_type']);
  206. $objPHPExcel->getActiveSheet()->setCellValueExplicit($col_E_mapping['col_name'].$raw_index, STATION_NAME, $col_E_mapping['col_type']);
  207. $objPHPExcel->getActiveSheet()->setCellValueExplicit($col_F_mapping['col_name'].$raw_index, $member_name, $col_F_mapping['col_type']);
  208. }
  209. // 儲存檔案
  210. /*
  211. $filename_prefix = iconv('UTF-8', 'Big5', '車牌號碼進出記錄 - '. STATION_NAME);
  212. $filename_postfix = iconv('UTF-8', 'Big5', $query_year . '年' .$query_month.'月份');
  213. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  214. $objWriter->save(EXPORT_BASE.$filename_prefix.' - '.$filename_postfix.'.xlsx');
  215. */
  216. // 網站下載
  217. $filename_prefix = iconv('UTF-8', 'Big5', '車牌號碼進出記錄 - '. STATION_NAME);
  218. $filename_postfix = iconv('UTF-8', 'Big5', $query_year . '年' .$query_month.'月份');
  219. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  220. header('Content-Type: application/vnd.ms-excel');
  221. header('Content-Disposition: attachment;filename="' . $filename_prefix. ' - ' . $filename_postfix . '.xlsx');
  222. header('Cache-Control: max-age=0');
  223. $objWriter->save('php://output');
  224. trigger_error(EXPORT_LOG_TITLE . '..completed..' . __FUNCTION__ . '|count:' . $count);
  225. return true;
  226. }
  227. // 分時統計表
  228. public function export_cario_summery($station_name, $query_year, $query_month, $addr='', $phone_no='')
  229. {
  230. trigger_error(EXPORT_LOG_TITLE. '..start..' . __FUNCTION__ . "|{$station_name},{$query_year},{$query_month},{$addr},{$phone_no}");
  231. $sql = "
  232. SELECT
  233. cario.in_time AS in_time,
  234. cario.out_time AS out_time,
  235. cario.member_no as member_no
  236. FROM cario
  237. WHERE cario.err = 0 and cario.obj_id != 'NONE'
  238. and cario.out_time is not null
  239. and
  240. (
  241. (
  242. YEAR(cario.in_time) = {$query_year} and MONTH(cario.in_time) = {$query_month}
  243. )
  244. OR
  245. (
  246. YEAR(cario.out_time) = {$query_year} and MONTH(cario.out_time) = {$query_month}
  247. )
  248. )
  249. ORDER BY cario.in_time asc
  250. ";
  251. $results = $this->db->query($sql)->result_array();
  252. if(empty($results))
  253. {
  254. trigger_error(EXPORT_LOG_TITLE.'..no data..' . $this->db->last_query());
  255. return false;
  256. }
  257. // PART 1: 產生分時陣列
  258. $KEY_SHEET_INDEX = 'sheet_index'; // 所在 sheet index
  259. $KEY_TITLE_INDEX = 'title_index'; // 所在 title incex
  260. $KEY_VALUE_INDEX = 'value_index'; // 所在 value incex
  261. $KEY_VALUE = 'value'; // value
  262. $KEY_MEMBER = 1;// 1:代表會員, 0:代表臨停
  263. $KEY_NONE = 0;
  264. $results_summary = array();
  265. $this_day = new DateTime("{$query_year}-{$query_month}");
  266. $this_day_first = $this_day->format('Y-m-01');
  267. $this_day_last = $this_day->format('Y-m-t');
  268. $day_first_time = new DateTime($this_day_first. '00:00:00');
  269. $day_last_time = new DateTime($this_day_last. '23:59:59');
  270. trigger_error(EXPORT_LOG_TITLE. '..' . __FUNCTION__ . "|{$this_day_first},{$this_day_last}");
  271. foreach($results as $rows)
  272. {
  273. $member_no_key = $rows['member_no'] > 0 ? $KEY_MEMBER : $KEY_NONE;
  274. // fetch input
  275. $in_time = new DateTime($rows['in_time']);
  276. $out_time = new DateTime($rows['out_time']);
  277. $in_time_hour_str = $in_time->format('Y-m-d H:00:00');
  278. $out_time_hour_str = $out_time->format('Y-m-d H:59:59');
  279. // trim by limit
  280. $day_start = ($in_time < $day_first_time) ? $day_first_time : new DateTime($in_time_hour_str);
  281. $day_end = ($out_time > $day_last_time) ? $day_last_time : new DateTime($out_time_hour_str);
  282. // loop
  283. $day_period = new DatePeriod($day_start, DateInterval::createFromDateString('1 hours'), $day_end);
  284. foreach ($day_period as $day)
  285. {
  286. $key_lv1 = $day->format('Y-m-d'); // 日期
  287. $key_lv2 = $day->format('H'); // 小時
  288. $key_lv3 = $member_no_key; // 身份
  289. $column_index_0 = 'D';
  290. $row_index_0 = 9;
  291. if(!array_key_exists($key_lv1, $results_summary))
  292. {
  293. $results_summary[$key_lv1] = array();
  294. }
  295. if(!array_key_exists($key_lv2, $results_summary[$key_lv1]))
  296. {
  297. $results_summary[$key_lv1][$key_lv2] = array();
  298. // 計算位置 (資料)
  299. for($column_index_key_lv1 = $column_index_0, $i = 0 ; $i < ((intval($day->format('d')) - 1) % 7) ; $i++)
  300. {
  301. $column_index_key_lv1++;
  302. $column_index_key_lv1++;
  303. }
  304. $results_summary[$key_lv1][$key_lv2][$KEY_NONE] = array();
  305. $results_summary[$key_lv1][$key_lv2][$KEY_NONE][$KEY_SHEET_INDEX] = floor(intval($day->format('d') - 1) / 7) + 1; // 指定 sheet index
  306. $results_summary[$key_lv1][$key_lv2][$KEY_NONE][$KEY_TITLE_INDEX] = $column_index_key_lv1.$row_index_0; // 指定 title index
  307. $results_summary[$key_lv1][$key_lv2][$KEY_MEMBER] = $results_summary[$key_lv1][$key_lv2][$KEY_NONE]; // copy
  308. }
  309. if(!array_key_exists($KEY_VALUE_INDEX, $results_summary[$key_lv1][$key_lv2][$key_lv3]))
  310. {
  311. // 計算位置 (資料)
  312. for($column_index_key_lv1 = $column_index_0, $i = 0 ; $i < ((intval($day->format('d')) - 1) % 7) ; $i++)
  313. {
  314. $column_index_key_lv1++;
  315. $column_index_key_lv1++;
  316. }
  317. if($key_lv3 == $KEY_NONE)
  318. {
  319. $column_index_key_lv1++; // 臨停
  320. }
  321. $results_summary[$key_lv1][$key_lv2][$key_lv3][$KEY_VALUE_INDEX] = $column_index_key_lv1. ($row_index_0 + intval($key_lv2) + 2);
  322. $results_summary[$key_lv1][$key_lv2][$key_lv3][$KEY_VALUE] = 0; // 初始值
  323. }
  324. $results_summary[$key_lv1][$key_lv2][$key_lv3][$KEY_VALUE] += 1;
  325. }
  326. }
  327. // PART 2: 產生EXCEL
  328. // 產生 Excel
  329. $inputFileName = FILE_BASE . "excel/sample2.xlsx";
  330. trigger_error(__FUNCTION__ . "..read: {$inputFileName}.." . file_exists($inputFileName));
  331. try
  332. {
  333. $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
  334. $objReader = PHPExcel_IOFactory::createReader($inputFileType);
  335. $objPHPExcel = $objReader->load($inputFileName);
  336. } catch(Exception $e)
  337. {
  338. die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
  339. }
  340. $objPHPExcel->setActiveSheetIndex(0);
  341. $sheet = $objPHPExcel->getActiveSheet();
  342. $sheet->setTitle('tmp');
  343. $title_style = array(
  344. 'alignment' => array(
  345. 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  346. ),
  347. 'font' => array(
  348. 'bold' => true,
  349. //'color' => array('rgb' => 'FF0000'),
  350. 'size' => 20,
  351. 'name' => 'Verdana'
  352. )
  353. );
  354. $lpr_style = array(
  355. 'alignment' => array(
  356. 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
  357. )
  358. );
  359. $basic_style = array(
  360. 'alignment' => array(
  361. 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
  362. )
  363. );
  364. // 標題
  365. $sheet_data_title = $station_name . $query_year. '年'. $query_month .'月份停車場分時統計表';
  366. $sheet->setCellValueExplicit('A1', $sheet_data_title, PHPExcel_Cell_DataType::TYPE_STRING);
  367. $sheet->getStyle('A1')->applyFromArray($title_style);
  368. // 停車場名稱
  369. $sheet->setCellValueExplicit('E2', $station_name, PHPExcel_Cell_DataType::TYPE_STRING);
  370. $sheet->getStyle('E2')->applyFromArray($basic_style);
  371. // 地址
  372. $sheet->setCellValueExplicit('E3', $addr, PHPExcel_Cell_DataType::TYPE_STRING);
  373. $sheet->getStyle('E3')->applyFromArray($basic_style);
  374. // 電話
  375. $sheet->setCellValueExplicit('E4', $phone_no, PHPExcel_Cell_DataType::TYPE_STRING);
  376. $sheet->getStyle('E4')->applyFromArray($basic_style);
  377. // 複製 sheet
  378. $sheet_base = $objPHPExcel->getActiveSheet();
  379. $sheet1 = clone $sheet_base;
  380. $sheet1->setTitle($query_month. '月1號至7號');
  381. $objPHPExcel->addSheet($sheet1);
  382. $sheet2 = clone $sheet_base;
  383. $sheet2->setTitle($query_month. '月8號至14號');
  384. $objPHPExcel->addSheet($sheet2);
  385. $sheet3 = clone $sheet_base;
  386. $sheet3->setTitle($query_month. '月15號至21號');
  387. $objPHPExcel->addSheet($sheet3);
  388. $sheet4 = clone $sheet_base;
  389. $sheet4->setTitle($query_month. '月22號至28號');
  390. $objPHPExcel->addSheet($sheet4);
  391. $sheet5 = clone $sheet_base;
  392. $sheet5->setTitle($query_month. '月底');
  393. $objPHPExcel->addSheet($sheet5);
  394. foreach ($results_summary as $key_lv1 => $results_summary_lv1)
  395. {
  396. $lv1_count = 0;
  397. foreach ($results_summary_lv1 as $key_lv2 => $results_summary_lv2)
  398. {
  399. if(empty($results_summary[$key_lv1][$key_lv2][$KEY_NONE][$KEY_SHEET_INDEX])) continue;
  400. // 設定 日期值
  401. if($lv1_count == 0)
  402. {
  403. $title_index = $results_summary[$key_lv1][$key_lv2][$KEY_NONE][$KEY_TITLE_INDEX];
  404. if(!empty($title_index))
  405. {
  406. $sheet_index = $results_summary[$key_lv1][$key_lv2][$KEY_NONE][$KEY_SHEET_INDEX];
  407. $objPHPExcel->setActiveSheetIndex($sheet_index);
  408. $objPHPExcel->getActiveSheet()->setCellValueExplicit($title_index, $key_lv1, PHPExcel_Cell_DataType::TYPE_STRING);
  409. }
  410. }
  411. $lv1_count++;
  412. // 設定 資料值
  413. $member_value_index = $results_summary[$key_lv1][$key_lv2][$KEY_MEMBER][$KEY_VALUE_INDEX];
  414. $member_value = $results_summary[$key_lv1][$key_lv2][$KEY_MEMBER][$KEY_VALUE];
  415. if(!empty($member_value_index))
  416. {
  417. $objPHPExcel->getActiveSheet()->setCellValueExplicit($member_value_index, $member_value, PHPExcel_Cell_DataType::TYPE_STRING);
  418. }
  419. $none_value_index = $results_summary[$key_lv1][$key_lv2][$KEY_NONE][$KEY_VALUE_INDEX];
  420. $none_value = $results_summary[$key_lv1][$key_lv2][$KEY_NONE][$KEY_VALUE];
  421. if(!empty($none_value_index))
  422. {
  423. $objPHPExcel->getActiveSheet()->setCellValueExplicit($none_value_index, $none_value, PHPExcel_Cell_DataType::TYPE_STRING);
  424. }
  425. }
  426. }
  427. // 儲存檔案
  428. $filename_prefix = iconv('UTF-8', 'Big5', '分時統計表 - '. $station_name);
  429. $filename_postfix = iconv('UTF-8', 'Big5', $query_year . '年' .$query_month.'月份');
  430. $sheetCount = $objPHPExcel->getSheetCount();
  431. $objPHPExcel->removeSheetByIndex(0);
  432. /*
  433. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  434. $objWriter->save(EXPORT_BASE.$filename_prefix.' - '.$filename_postfix.'.xlsx');
  435. trigger_error(EXPORT_LOG_TITLE . '..' . __FUNCTION__ . '..completed..');
  436. return true;
  437. */
  438. // 網站下載
  439. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  440. header('Content-Type: application/vnd.ms-excel');
  441. header('Content-Disposition: attachment;filename="' . $filename_prefix. ' - ' . $filename_postfix . '.xlsx');
  442. header('Cache-Control: max-age=0');
  443. $objWriter->save('php://output');
  444. return true;
  445. }
  446. // 進出記錄表
  447. public function export_cario_report($station_name, $query_year, $query_month, $addr='', $phone_no='', $member_attr=0)
  448. {
  449. trigger_error(EXPORT_LOG_TITLE. '..start..' . __FUNCTION__ . "|{$station_name},{$query_year},{$query_month},{$addr},{$phone_no}");
  450. $where_member_attr = !empty($member_attr) ? " and members.member_attr = {$member_attr} " : "";
  451. $sql = "
  452. SELECT
  453. cario.obj_id AS plate_no,
  454. cario.in_time as in_time,
  455. cario.out_time as out_time,
  456. members.member_name as member_name,
  457. CONCAT( FLOOR(HOUR(TIMEDIFF(cario.in_time, cario.out_time)) / 24), ' 日 ',
  458. MOD(HOUR(TIMEDIFF(cario.in_time, cario.out_time)), 24), ' 時 ',
  459. MINUTE(TIMEDIFF(cario.in_time, cario.out_time)), ' 分') as time_period
  460. FROM cario
  461. left join members on cario.obj_id = members.lpr
  462. WHERE cario.err = 0 and cario.obj_id != 'NONE'
  463. and YEAR(cario.in_time) = {$query_year} and MONTH(cario.in_time) = {$query_month}
  464. and cario.out_time is not null
  465. {$where_member_attr}
  466. ORDER BY cario.in_time ASC
  467. ";
  468. $results = $this->db->query($sql)->result_array();
  469. if(empty($results))
  470. {
  471. trigger_error(EXPORT_LOG_TITLE.'..no data..' . $this->db->last_query());
  472. return false;
  473. }
  474. // PART 2: 產生EXCEL
  475. // 產生 Excel
  476. $inputFileName = FILE_BASE . "excel/sample1.xlsx";
  477. trigger_error(__FUNCTION__ . "..read: {$inputFileName}.." . file_exists($inputFileName));
  478. try
  479. {
  480. $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
  481. $objReader = PHPExcel_IOFactory::createReader($inputFileType);
  482. $objPHPExcel = $objReader->load($inputFileName);
  483. } catch(Exception $e)
  484. {
  485. die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
  486. }
  487. $objPHPExcel->setActiveSheetIndex(0);
  488. $col_A0_mapping = array('col_name' => 'A', 'col_title' => '', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  489. $col_A_mapping = array('col_name' => 'B', 'col_title' => '車號', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  490. $col_B_mapping = array('col_name' => 'C', 'col_title' => '進場時間(A)', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  491. $col_C_mapping = array('col_name' => 'E', 'col_title' => '出場時間(B)', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  492. $col_D_mapping = array('col_name' => 'G', 'col_title' => '停車時間', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  493. $col_E0_mapping = array('col_name' => 'I', 'col_title' => '', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  494. $col_E_mapping = array('col_name' => 'J', 'col_title' => '車號', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  495. $col_F_mapping = array('col_name' => 'K', 'col_title' => '進場時間(A)', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  496. $col_G_mapping = array('col_name' => 'M', 'col_title' => '出場時間(B)', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  497. $col_H_mapping = array('col_name' => 'O', 'col_title' => '停車時間', 'col_type' => PHPExcel_Cell_DataType::TYPE_STRING);
  498. $raw_index = 10;
  499. $sheet = $objPHPExcel->getActiveSheet();
  500. $sheet->setTitle('下載');
  501. $title_style = array(
  502. 'alignment' => array(
  503. 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  504. ),
  505. 'font' => array(
  506. 'bold' => true,
  507. //'color' => array('rgb' => 'FF0000'),
  508. 'size' => 20,
  509. 'name' => 'Verdana'
  510. )
  511. );
  512. $lpr_style = array(
  513. 'alignment' => array(
  514. 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
  515. )
  516. );
  517. $basic_style = array(
  518. 'alignment' => array(
  519. 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
  520. )
  521. );
  522. // 標題
  523. $sheet_data_title = $station_name . $query_year. '年'. $query_month .'月份停車場延時統計表';
  524. $sheet->setCellValueExplicit('A1', $sheet_data_title, PHPExcel_Cell_DataType::TYPE_STRING);
  525. $sheet->getStyle('A1')->applyFromArray($title_style);
  526. // 停車場名稱
  527. $sheet->setCellValueExplicit('D2', $station_name, PHPExcel_Cell_DataType::TYPE_STRING);
  528. $sheet->getStyle('D2')->applyFromArray($basic_style);
  529. // 地址
  530. $sheet->setCellValueExplicit('D3', $addr, PHPExcel_Cell_DataType::TYPE_STRING);
  531. $sheet->getStyle('D3')->applyFromArray($basic_style);
  532. // 電話
  533. $sheet->setCellValueExplicit('D4', $phone_no, PHPExcel_Cell_DataType::TYPE_STRING);
  534. $sheet->getStyle('D4')->applyFromArray($basic_style);
  535. $count = 0;
  536. foreach($results as $rows)
  537. {
  538. $member_name = $rows['member_name'];
  539. $plate_no = $rows['plate_no'];
  540. $in_time = $rows['in_time'];
  541. $out_time = empty($rows['out_time']) ? '無離場記錄或車辨失敗' : $rows['out_time'];
  542. $time_period = $rows['time_period'];
  543. if($count % 2 == 0)
  544. {
  545. $raw_index += 1;
  546. $sheet->setCellValueExplicit($col_A0_mapping['col_name'].$raw_index, $member_name, $col_A0_mapping['col_type']);
  547. $sheet->setCellValueExplicit($col_A_mapping['col_name'].$raw_index, $plate_no, $col_A_mapping['col_type']);
  548. $sheet->setCellValueExplicit($col_B_mapping['col_name'].$raw_index, $in_time, $col_B_mapping['col_type']);
  549. $sheet->setCellValueExplicit($col_C_mapping['col_name'].$raw_index, $out_time, $col_C_mapping['col_type']);
  550. $sheet->setCellValueExplicit($col_D_mapping['col_name'].$raw_index, $time_period, $col_D_mapping['col_type']);
  551. //$sheet->getStyle("A1:B1")->applyFromArray($style);
  552. $sheet->getStyle($col_A0_mapping['col_name'].$raw_index)->applyFromArray($basic_style);
  553. $sheet->getStyle($col_A_mapping['col_name'].$raw_index)->applyFromArray($lpr_style);
  554. $sheet->getStyle($col_B_mapping['col_name'].$raw_index)->applyFromArray($basic_style);
  555. $sheet->getStyle($col_C_mapping['col_name'].$raw_index)->applyFromArray($basic_style);
  556. $sheet->getStyle($col_D_mapping['col_name'].$raw_index)->applyFromArray($basic_style);
  557. }
  558. else
  559. {
  560. $sheet->setCellValueExplicit($col_E0_mapping['col_name'].$raw_index, $member_name, $col_E0_mapping['col_type']);
  561. $sheet->setCellValueExplicit($col_E_mapping['col_name'].$raw_index, $plate_no, $col_E_mapping['col_type']);
  562. $sheet->setCellValueExplicit($col_F_mapping['col_name'].$raw_index, $in_time, $col_F_mapping['col_type']);
  563. $sheet->setCellValueExplicit($col_G_mapping['col_name'].$raw_index, $out_time, $col_G_mapping['col_type']);
  564. $sheet->setCellValueExplicit($col_H_mapping['col_name'].$raw_index, $time_period, $col_H_mapping['col_type']);
  565. $sheet->getStyle($col_E0_mapping['col_name'].$raw_index)->applyFromArray($basic_style);
  566. $sheet->getStyle($col_E_mapping['col_name'].$raw_index)->applyFromArray($lpr_style);
  567. $sheet->getStyle($col_F_mapping['col_name'].$raw_index)->applyFromArray($basic_style);
  568. $sheet->getStyle($col_G_mapping['col_name'].$raw_index)->applyFromArray($basic_style);
  569. $sheet->getStyle($col_H_mapping['col_name'].$raw_index)->applyFromArray($basic_style);
  570. }
  571. $count++;
  572. }
  573. // 儲存檔案
  574. $filename_prefix = iconv('UTF-8', 'Big5', '車牌號碼進出記錄 - '. $station_name);
  575. $filename_postfix = iconv('UTF-8', 'Big5', $query_year . '年' .$query_month.'月份');
  576. $sheetCount = $objPHPExcel->getSheetCount();
  577. $objPHPExcel->removeSheetByIndex($sheetCount - 1);
  578. /*
  579. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  580. $objWriter->save(EXPORT_BASE.$filename_prefix.' - '.$filename_postfix.'.xlsx');
  581. trigger_error(EXPORT_LOG_TITLE . '..' . __FUNCTION__ . '..completed..' . "count: {$count}");
  582. return true;
  583. */
  584. // 網站下載
  585. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  586. header('Content-Type: application/vnd.ms-excel');
  587. header('Content-Disposition: attachment;filename="' . $filename_prefix. ' - ' . $filename_postfix . '.xlsx');
  588. header('Cache-Control: max-age=0');
  589. $objWriter->save('php://output');
  590. return true;
  591. }
  592. }