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

597 行
23KB

  1. <?php
  2. /*
  3. file: carpark_model.php 停車管理系統
  4. */
  5. class Carpark_model extends CI_Model
  6. {
  7. var $vars = array();
  8. function __construct()
  9. {
  10. parent::__construct();
  11. $this->load->database();
  12. }
  13. public function init($vars)
  14. {
  15. $this->vars = $vars;
  16. }
  17. // 月租會員加入
  18. public function member_add($data)
  19. {
  20. // 會員車輛基本資料檔
  21. // $data['start_date'] = "{$data['start_date']} 00:00:00";
  22. // $data['end_date'] = "{$data['end_date']} 23:59:59";
  23. $old_lpr = $data['old_lpr'];
  24. unset($data['old_lpr']);
  25. $data_car = array
  26. (
  27. 'lpr' => $data['lpr'],
  28. 'lpr_correct' => $data['lpr'],
  29. 'etag' => $data['etag'],
  30. 'station_no' => $data['station_no'],
  31. 'start_time' => $data['start_date'],
  32. 'end_time' => $data['end_date']
  33. );
  34. $check_member_no = $data['member_no'];
  35. unset($data['member_no']);
  36. trigger_error("members:".print_r($data, true)."car:".print_r($data_car, true));
  37. if ($check_member_no == 0) // 新增一筆會員資料
  38. {
  39. $this->db->insert('members', $data);
  40. $data_car['member_no'] = $this->db->insert_id();
  41. $this->db->insert('member_car', $data_car);
  42. }
  43. else // update會員資料
  44. {
  45. $this->db->update('members', $data, array('member_no' => $check_member_no));
  46. if ($old_lpr == $data['lpr']) // 沒有異動到車牌, 使用update, 否則重建一筆
  47. {
  48. unset($data_car['lpr']);
  49. unset($data_car['lpr_correct']);
  50. $this->db->update('member_car', $data_car, array('member_no' => $check_member_no));
  51. }
  52. else
  53. {
  54. $this->db->delete('member_car', array('member_no' => $check_member_no));
  55. $data_car['member_no'] = $check_member_no;
  56. $this->db->insert('member_car', $data_car);
  57. }
  58. }
  59. return true;
  60. }
  61. // 查詢車牌是否重複
  62. public function check_lpr($lpr)
  63. {
  64. $rows = $this->db->select('count(*) as counts')
  65. ->from('members')
  66. ->where(array('lpr' => $lpr))
  67. ->get()
  68. ->row_array();
  69. return $rows['counts'];
  70. }
  71. // 月租會員查詢
  72. public function member_query()
  73. {
  74. $results = $this->db->select('member_no, lpr, etag, member_name, mobile_no, start_date, end_date, contract_no, amt, member_id, tel_h, tel_o, addr, valid_time, station_no')
  75. ->from('members')
  76. ->order_by('station_no, lpr', 'asc')
  77. ->get()
  78. ->result_array();
  79. return $results;
  80. }
  81. // 刪除月租會員
  82. public function member_delete($member_no)
  83. {
  84. $this->db->delete('members', array('member_no' => $member_no));
  85. $this->db->delete('member_car', array('member_no' => $member_no));
  86. return true;
  87. }
  88. // 進出場現況表
  89. public function cario_list()
  90. {
  91. /*
  92. $data_cario = $this->db
  93. ->select('c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner')
  94. ->from('cario c')
  95. ->join('members m', 'c.member_no = m.member_no', 'left')
  96. ->where(array('c.err' => 0))
  97. ->order_by('c.update_time', 'desc')
  98. ->limit(10)
  99. ->get()
  100. ->result_array();
  101. */
  102. $sql = '(select c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner, c.in_time as time_order
  103. from cario c left join members m on c.obj_id = m.lpr
  104. where c.err = 0 and c.out_time is null)
  105. union
  106. (select c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner, c.out_time as time_order
  107. from cario c left join members m on c.obj_id = m.lpr
  108. where c.err = 0 and c.out_time is not null)
  109. order by time_order desc limit 10;';
  110. $data_cario = $this->db->query($sql)->result_array();
  111. // $lane_arr = array(0 => '入1', 1 => '入2', 3 => '出3', 3 => '出4');
  112. $idx = 0;
  113. foreach($data_cario as $rows)
  114. {
  115. ++$rows['in_lane'];
  116. ++$rows['out_lane'];
  117. //$lane_no = $rows['in_out'] == 'CI' ? "入{$rows['in_lane']}" : "入{$rows['in_lane']} -> 出{$rows['out_lane']}";
  118. $lane_no = empty($rows['out_time']) ? "入{$rows['in_lane']}" : "入{$rows['in_lane']} -> 出{$rows['out_lane']}"; // 2016/08/22 有離場時間就顯示
  119. $pic_name = str_replace('.jpg', '', empty($rows['out_pic_name']) ? $rows['in_pic_name'] : $rows['out_pic_name']);
  120. $arr = explode('-', $pic_name);
  121. $pic_path = APP_URL.'pics/'.substr($arr[7], 0, 8).'/'.$pic_name;
  122. $data[$idx] = array
  123. (
  124. // 'io_name' => $io_name[$rows['in_out']],
  125. 'io_name' => $lane_no,
  126. 'lpr' => $rows['lpr'],
  127. // 'etag' => $rows['etag'],
  128. 'etag' => $rows['etag'],
  129. 'owner' => $rows['owner'],
  130. 'io_time' => empty($rows['out_time']) ? $rows['in_time'] : "{$rows['in_time']}(入)<br>{$rows['out_time']}(出)<br>{$rows['minutes']}分(停留時間)",
  131. 'pic_name' => $pic_path
  132. );
  133. ++$idx;
  134. }
  135. return $data;
  136. }
  137. // 模糊比對
  138. function getLevenshteinSQLStatement($word, $target)
  139. {
  140. $words = array();
  141. if(strlen($word) >= 5)
  142. {
  143. for ($i = 0; $i < strlen($word); $i++) {
  144. // insertions
  145. $words[] = substr($word, 0, $i) . '_' . substr($word, $i);
  146. // deletions
  147. $words[] = substr($word, 0, $i) . substr($word, $i + 1);
  148. // substitutions
  149. //$words[] = substr($word, 0, $i) . '_' . substr($word, $i + 1);
  150. }
  151. }
  152. else
  153. {
  154. for ($i = 0; $i < strlen($word); $i++) {
  155. // insertions
  156. $words[] = substr($word, 0, $i) . '_' . substr($word, $i);
  157. }
  158. }
  159. // last insertion
  160. $words[] = $word . '_';
  161. //return $words;
  162. $fuzzy_statement = ' (';
  163. foreach ($words as $idx => $word)
  164. {
  165. $fuzzy_statement .= " {$target} LIKE '%{$word}%' OR ";
  166. }
  167. $last_or_pos = strrpos($fuzzy_statement, 'OR');
  168. if($last_or_pos !== false)
  169. {
  170. $fuzzy_statement = substr_replace($fuzzy_statement, ')', $last_or_pos, strlen('OR'));
  171. }
  172. return $fuzzy_statement;
  173. }
  174. // 車號入場查詢
  175. public function carin_lpr_query($word)
  176. {
  177. // updated 2016/09/09 fuzzy search
  178. if(empty($word) || strlen($word) < 4 || strlen($word) > 10)
  179. {
  180. return array();
  181. }
  182. $fuzzy_statement = $this->getLevenshteinSQLStatement($word, 'c.obj_id');
  183. trigger_error("模糊比對 {$word} where: {$fuzzy_statement}");
  184. $sql = "SELECT c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner
  185. FROM cario c
  186. LEFT JOIN members m ON c.obj_id = m.lpr
  187. WHERE {$fuzzy_statement} AND c.err = 0 AND c.obj_type = 1
  188. ORDER BY c.update_time DESC
  189. LIMIT 50";
  190. $data_cario = $this->db->query($sql)->result_array();
  191. /*
  192. $data_cario = $this->db
  193. ->select('c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner')
  194. ->from('cario c')
  195. ->join('members m', 'c.member_no = m.member_no', 'left')
  196. ->where(array('c.obj_type' => 1, 'obj_id' => $lpr))
  197. ->order_by('c.update_time', 'desc')
  198. ->get()
  199. ->result_array();
  200. */
  201. $data = array();
  202. $idx = 0;
  203. foreach($data_cario as $rows)
  204. {
  205. ++$rows['in_lane'];
  206. ++$rows['out_lane'];
  207. $lane_no = empty($rows['out_time']) ? "入{$rows['in_lane']}" : "入{$rows['in_lane']} -> 出{$rows['out_lane']}";
  208. $io_time = empty($rows['out_time']) ? $rows['in_time'] : "{$rows['in_time']}(入)<br>{$rows['out_time']}(出)<br>{$rows['minutes']}分(停留時間)";
  209. $pic_name = str_replace('.jpg', '', empty($rows['out_pic_name']) ? $rows['in_pic_name'] : $rows['out_pic_name']);
  210. $arr = explode('-', $pic_name);
  211. $pic_path = APP_URL.'pics/'.substr($arr[7], 0, 8).'/'.$pic_name;
  212. $data[$idx++] = array
  213. (
  214. 'io_name' => $lane_no,
  215. 'lpr' => $rows['lpr'],
  216. 'etag' => $rows['etag'],
  217. 'owner' => empty($rows['owner']) ? '' : $rows['owner'],
  218. 'io_time' => $io_time,
  219. 'pic_name' => $pic_path
  220. );
  221. }
  222. return $data;
  223. }
  224. // 車號入場查詢
  225. public function carin_time_query($time_query, $minutes_range)
  226. {
  227. $curr_time = date('Y-m-d H:i:s');
  228. $start_time = date('Y-m-d H:i:s', strtotime("{$time_query} - {$minutes_range} minutes"));
  229. $end_time = date('Y-m-d H:i:s', strtotime("{$time_query} + {$minutes_range} minutes"));
  230. $data_cario = $this->db
  231. ->select('c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner')
  232. ->from('cario c')
  233. ->join('members m', 'c.obj_id = m.lpr', 'left')
  234. ->where(array('c.obj_type' => 1, 'c.in_time >=' => $start_time, 'c.in_time <=' => $end_time))
  235. ->order_by('c.update_time', 'desc')
  236. ->get()
  237. ->result_array();
  238. $data = array();
  239. $idx = 0;
  240. foreach($data_cario as $rows)
  241. {
  242. ++$rows['in_lane'];
  243. ++$rows['out_lane'];
  244. $lane_no = empty($rows['out_time']) ? "入{$rows['in_lane']}" : "入{$rows['in_lane']} -> 出{$rows['out_lane']}";
  245. $io_time = empty($rows['out_time']) ? $rows['in_time'] : "{$rows['in_time']}(入)<br>{$rows['out_time']}(出)<br>{$rows['minutes']}分(停留時間)";
  246. $pic_name = str_replace('.jpg', '', empty($rows['out_pic_name']) ? $rows['in_pic_name'] : $rows['out_pic_name']);
  247. $arr = explode('-', $pic_name);
  248. $pic_path = APP_URL.'pics/'.substr($arr[7], 0, 8).'/'.$pic_name;
  249. $data[$idx++] = array
  250. (
  251. 'io_name' => $lane_no,
  252. 'lpr' => $rows['lpr'],
  253. 'etag' => $rows['etag'],
  254. 'owner' => empty($rows['owner']) ? '' : $rows['owner'],
  255. 'io_time' => $io_time,
  256. 'pic_name' => $pic_path
  257. );
  258. }
  259. return $data;
  260. }
  261. // 時間長度轉成日時分秒
  262. public function time2str($d1, $d2)
  263. {
  264. $time = strtotime($d2) - strtotime($d1);
  265. $day_str = floor($time/3600/24);
  266. $day_str = $day_str ? $day_str .= '天 ' : '';
  267. $hour_str = floor($time%(24*3600)/3600);
  268. $hour_str = $hour_str ? $hour_str .= '小時 ' : '';
  269. $minute_str = floor($time%3600/60);
  270. $minute_str = $minute_str ? $minute_str .= '分' : '';
  271. /*
  272. $second_str = $time%3600%60;
  273. $second_str = $second_str ? $second_str .= ' seconds ' : '';
  274. $n_time = floor($time/3600/24)."days".floor($time%(24*3600)/3600)."Hour".floor($time%3600/60)."Minute".($time%3600%60)."Second";
  275. */
  276. $n_time = $day_str . $hour_str . $minute_str;
  277. return $n_time;
  278. }
  279. // 在席車位檢查未有入場資料清單
  280. public function pks_check_list($max_rows)
  281. {
  282. $data = array();
  283. $data = $this->db
  284. ->select('pksno, lpr, in_time, pic_name')
  285. ->from('pks')
  286. ->where(array('status' => 'LR', 'cario_no' => 0, 'confirms' => 0, 'station_no' => STATION_NO))
  287. ->order_by('in_time', 'desc')
  288. ->limit($max_rows)
  289. ->get()
  290. ->result_array();
  291. return $data;
  292. }
  293. // 重設在席查核
  294. public function reset_pks_check()
  295. {
  296. // 讀出未查核過的資料
  297. $data_pks = $this->db
  298. ->select('pksno, lpr, in_time')
  299. ->from('pks')
  300. ->where(array('status' => 'LR', 'cario_no' => 0, 'station_no' => STATION_NO))
  301. ->get()
  302. ->result_array();
  303. // $tot = $this->db->num_rows(); // 總筆數
  304. $tot = count($data_pks); // 總筆數
  305. $num_cario = 0; // 入場資料筆數
  306. foreach($data_pks as $rows)
  307. {
  308. $lpr = $rows['lpr'];
  309. if ($lpr == 'NONE') continue; // 車辨失敗者不處理
  310. $pksno = $rows['pksno'];
  311. $pks_in_time = $rows['in_time'];
  312. // 讀取進場時間, 如讀不到資料, 以目前時間取代(add by TZUSS 2016-02-23)
  313. $rows_cario = $this->db
  314. ->select('cario_no, in_time')
  315. ->from('cario')
  316. ->where(array('in_out' => 'CI', 'obj_id' => $lpr, 'finished' => 0, 'err' => 0, 'station_no' => STATION_NO))
  317. ->order_by('cario_no', 'desc')
  318. ->limit(1)
  319. ->get()
  320. ->row_array();
  321. // if ($this->db->num_rows() == 1) // 有入場資料
  322. if (!empty($rows_cario['cario_no'])) // 有入cario_no場資料
  323. {
  324. $cario_no = $rows_cario['cario_no']; // 入場序號
  325. $in_time = $rows_cario['in_time'];
  326. // 在席與入場資料相符, 分別在cario與pks記錄之
  327. $data = array
  328. (
  329. 'pksno' => $pksno,
  330. 'pks_time' => $pks_in_time
  331. );
  332. $this->db->update('cario', $data, array('cario_no' => $cario_no, 'station_no' => STATION_NO));
  333. $data = array
  334. (
  335. 'cario_no' => $cario_no,
  336. 'in_time' => $in_time
  337. );
  338. // 車號及照片檔名填入資料庫內
  339. $this->db->update('pks', $data, array('pksno' => $pksno, 'station_no' => STATION_NO));
  340. ++$num_cario;
  341. }
  342. }
  343. return array('tot' => $tot, 'tot_correct' => $num_cario);
  344. }
  345. // 更正在席車號
  346. public function correct_pks_lpr($pksno, $lpr)
  347. {
  348. // 讀取進場時間, 如讀不到資料, 以目前時間取代(add by TZUSS 2016-02-23)
  349. $rows_cario = $this->db
  350. ->select('cario_no, in_time')
  351. ->from('cario')
  352. ->where(array('in_out' => 'CI', 'obj_id' => $lpr, 'finished' => 0, 'err' => 0, 'station_no' => STATION_NO))
  353. ->order_by('cario_no', 'desc')
  354. ->limit(1)
  355. ->get()
  356. ->row_array();
  357. if (!empty($rows_cario['cario_no'])) // 有cario_no入場資料
  358. {
  359. $cario_no = $rows_cario['cario_no']; // 入場序號
  360. $in_time = $rows_cario['in_time'];
  361. // 在席與入場資料相符, 分別在cario與pks記錄之
  362. $data = array
  363. (
  364. 'pksno' => $pksno,
  365. 'pks_time' => $in_time
  366. );
  367. $this->db->update('cario', $data, array('cario_no' => $cario_no, 'station_no' => STATION_NO));
  368. $data = array
  369. (
  370. 'confirms' => 1,
  371. 'cario_no' => $cario_no,
  372. 'lpr' => $lpr,
  373. 'in_time' => $in_time
  374. );
  375. // 車號及照片檔名填入資料庫內
  376. $this->db->update('pks', $data, array('pksno' => $pksno, 'station_no' => STATION_NO));
  377. $results = array
  378. (
  379. 'err' => 0,
  380. 'cario_no' => $cario_no
  381. );
  382. }
  383. else // 無入場資料
  384. {
  385. $data = array
  386. (
  387. 'confirms' => 1,
  388. 'lpr' => $lpr
  389. );
  390. $this->db->update('pks', $data, array('pksno' => $pksno, 'station_no' => STATION_NO));
  391. $results = array
  392. (
  393. 'err' => 0,
  394. 'cario_no' => 0 // 車號查無入場資料
  395. );
  396. }
  397. return $results;
  398. }
  399. // 入場車號查核在席無資料清單
  400. public function carin_check_list($max_rows)
  401. {
  402. $data = array();
  403. $rows_cario = $this->db
  404. ->select('cario_no, obj_id as lpr, in_time, member_no, in_pic_name')
  405. ->from('cario')
  406. ->where('in_out', 'CI')
  407. ->where(array('pksno' => 0, 'finished' => 0, 'err' => 0, 'confirms' => 0, 'station_no' => STATION_NO, 'in_time <=' => 'date_sub(now(), interval 20 minute)'), null, false)
  408. ->order_by('cario_no', 'desc')
  409. ->limit($max_rows)
  410. ->get()
  411. ->result_array();
  412. $idx = 0;
  413. foreach($rows_cario as $rows)
  414. {
  415. $data[$idx++] = array
  416. (
  417. 'cario_no' => $rows['cario_no'],
  418. 'lpr' => $rows['lpr'],
  419. 'in_time' => $rows['in_time'],
  420. 'type' => $rows['member_no'] == 0 ? '' : '月租',
  421. 'pic_name' => str_replace('-', '', substr($rows['in_time'], 0, 10)) . '/' . $rows['in_pic_name']
  422. );
  423. }
  424. return $data;
  425. }
  426. // 更正入場車號
  427. public function correct_carin_lpr($cario_no, $lpr, $in_time)
  428. {
  429. $rows = $this->db
  430. ->select('pksno, cario_no, in_time')
  431. ->from('pks')
  432. ->where(array('status' => 'LR', 'lpr' => $lpr, 'confirms' => 0, 'station_no' => STATION_NO))
  433. ->limit(1)
  434. ->get()
  435. ->row_array();
  436. // 如果在席資料相符
  437. if (!empty($rows['pksno']))
  438. {
  439. $pksno = $rows['pksno'];
  440. $data = array
  441. (
  442. 'cario_no' => $cario_no,
  443. 'in_time' => $in_time
  444. );
  445. $this->db->update('pks', $data, array('pksno' => $pksno, 'station_no' => STATION_NO));
  446. $data_cario = array
  447. (
  448. 'obj_id' => $lpr,
  449. 'pksno' => $pksno,
  450. 'pks_time' => $in_time,
  451. 'confirms' => 1
  452. );
  453. }
  454. else // 無在席資料
  455. {
  456. $data_cario = array
  457. (
  458. 'obj_id' => $lpr,
  459. 'pksno' => 0,
  460. 'confirms' => 1
  461. );
  462. }
  463. $this->db->update('cario', $data_cario, array('cario_no' => $cario_no, 'station_no' => STATION_NO));
  464. return(array('pksno' => $data_cario['pksno']));
  465. }
  466. // 查詢行動支付記錄
  467. public function tx_bill_query()
  468. {
  469. $sql = "SELECT
  470. tx_bill.order_no as order_no, tx_bill.lpr as lpr, tx_bill.invoice_no as invoice_no, tx_bill.in_time as in_time, tx_bill.balance_time as balance_time, tx_bill.company_no as company_no, tx_bill.email as email, tx_bill.mobile as mobile, tx_bill.amt as amt, tx_bill.tx_time as tx_time,
  471. allpay_feedback_log.rtn_msg as rtn_msg, allpay_feedback_log.payment_type as payment_type,
  472. cario.out_before_time as out_before_time
  473. FROM tx_bill
  474. LEFT JOIN cario ON tx_bill.cario_no = cario.cario_no
  475. LEFT JOIN allpay_feedback_log ON tx_bill.order_no = allpay_feedback_log.merchant_trade_no
  476. WHERE tx_bill.status = 111
  477. ORDER BY tx_bill.tx_time DESC";
  478. return $this->db->query($sql)->result_array();
  479. }
  480. // 查詢月租繳款機記錄
  481. public function tx_bill_ats_query()
  482. {
  483. $sql = "SELECT
  484. tx_bill_ats.order_no as order_no, tx_bill_ats.lpr as lpr, tx_bill_ats.invoice_no as invoice_no,
  485. tx_bill_ats.end_time as end_time, tx_bill_ats.next_start_time as next_start_time, tx_bill_ats.next_end_time as next_end_time,
  486. tx_bill_ats.company_no as company_no, tx_bill_ats.email as email, tx_bill_ats.mobile as mobile, tx_bill_ats.amt as amt,
  487. tx_bill_ats.remarks as remarks, tx_bill_ats.member_name as member_name, tx_bill_ats.tx_time as tx_time,
  488. allpay_feedback_log.rtn_msg as rtn_msg, allpay_feedback_log.payment_type as payment_type
  489. FROM tx_bill_ats
  490. LEFT JOIN allpay_feedback_log ON tx_bill_ats.order_no = allpay_feedback_log.merchant_trade_no
  491. WHERE tx_bill_ats.status = 111
  492. ORDER BY tx_bill_ats.tx_time DESC";
  493. return $this->db->query($sql)->result_array();
  494. }
  495. // 查詢樓層在席群組
  496. public function pks_group_query()
  497. {
  498. $sql = "SELECT
  499. pks_groups.station_no, pks_groups.group_name as group_name, pks_groups.tot as tot, pks_groups.parked as parked, pks_groups.availables as availables, pks_groups.group_id as group_id, pks_groups.renum as renum
  500. FROM pks_groups
  501. ORDER BY pks_groups.group_id DESC";
  502. return $this->db->query($sql)->result_array();
  503. }
  504. // 送出至message queue(目前用mqtt)
  505. public function mq_send($topic, $msg)
  506. {
  507. $this->vars['mqtt']->publish($topic, $msg, 0);
  508. trigger_error("mqtt:{$topic}|{$msg}");
  509. }
  510. }