VM暫存
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.

201 line
5.5KB

  1. <?php
  2. /**
  3. * 結算: 收費回傳物件 key
  4. */
  5. abstract class BillResultKey
  6. {
  7. const days = "d"; // 結算: 日
  8. const hours = "h"; // 結算: 時
  9. const mins = "i"; // 結算: 分
  10. const price = "p"; // 結算: 費用
  11. const price_detail = "p_detail"; // 結算: 明細
  12. const price_plan = "p_plan"; // 結算: 費率
  13. const price_plan_date = "p_plan_d"; // 結算: 費率日期
  14. }
  15. /**
  16. * 收費規則 key
  17. */
  18. abstract class PricePlanKey
  19. {
  20. const price_plan = "price_plan"; // 費率
  21. }
  22. /**
  23. * 收費規則 value
  24. */
  25. abstract class PricePlanValue
  26. {
  27. const TYPE_0 = 0; // 臨停
  28. }
  29. /**
  30. * 日期規則 key
  31. */
  32. abstract class DatePlanKey
  33. {
  34. const p_type = "p_type"; // 收費類型 (0:平日, 1:假日, etc..)
  35. const p_date = "p_date"; // 日期
  36. }
  37. /**
  38. * 日期規則 value
  39. */
  40. abstract class DatePlanValue
  41. {
  42. const TYPE_0 = 0; // 平日
  43. const TYPE_1 = 1; // 假日
  44. }
  45. /**
  46. * Altob 金流抽象介面
  47. *
  48. * @version 1.0
  49. * @author mip
  50. */
  51. abstract class AltobPaymentAbstractClass
  52. {
  53. public $ServiceURL = "ServiceURL";
  54. protected $Price = "Price"; // 結算價錢
  55. protected $PriceDetail = "PriceDetail"; // 結算明細
  56. protected $PricePlan = "PricePlan"; // 費率設定
  57. protected $PricePlanDate = "PricePlanDate"; // 特殊日期
  58. abstract public function getBill($inTime, $balanceTime, $station); // 拿帳單 (實作這一段)
  59. /**
  60. * 產生帳單回傳結果 (最上層的資料結構)
  61. */
  62. protected function genBillResult($interval)
  63. {
  64. $data = array();
  65. $data[BillResultKey::days] = $interval->d;
  66. $data[BillResultKey::hours] = $interval->h;
  67. $data[BillResultKey::mins] = $interval->i;
  68. $data[BillResultKey::price] = $this->Price;
  69. $data[BillResultKey::price_detail] = $this->PriceDetail;
  70. $data[BillResultKey::price_plan] = $this->PricePlan;
  71. $data[BillResultKey::price_plan_date] = $this->PricePlanDate;
  72. return $data;
  73. }
  74. /**
  75. * 取得費率資訊
  76. */
  77. protected function getPricePlan($stationNo, $txType=PricePlanValue::TYPE_0)
  78. {
  79. $plan = array();
  80. // 從 DB 取得費率設定
  81. $result = $this->ServerPost("{$this->ServiceURL}/get_price_plan/{$stationNo}/{$txType}");
  82. $decode_result = json_decode($result, true);
  83. if(! empty($decode_result[0])){
  84. $plan = json_decode($decode_result[0][PricePlanKey::price_plan], true);
  85. }
  86. return $plan;
  87. }
  88. /**
  89. * 取得特殊日期陣列
  90. */
  91. protected function getPricePlanDate($inTime, $balanceTime)
  92. {
  93. $result = array();
  94. $date_plan = $this->getDatePlan($inTime, $balanceTime); // 取得特殊日期
  95. foreach ($date_plan as $val){
  96. $day = new DateTime($val[DatePlanKey::p_date]);
  97. $day_key = $this->genLv1Key($day);
  98. $result[$day_key] = $val;
  99. }
  100. return $result;
  101. }
  102. // 取得特殊日期
  103. private function getDatePlan($inTime, $balanceTime)
  104. {
  105. $result = array();
  106. $inTimestamp = strtotime($inTime);
  107. $balanceTimestamp = strtotime($balanceTime);
  108. // 算出週六日
  109. $weekdays = $this->get_weekdays($inTimestamp, $balanceTimestamp);
  110. foreach ($weekdays as $val){
  111. $weekday = array();
  112. $weekday[DatePlanKey::p_type] = DatePlanValue::TYPE_1;
  113. $weekday[DatePlanKey::p_date] = date('Y-m-d H:i:s', $val);
  114. array_push($result, $weekday);
  115. }
  116. // 從 DB 取得其它假日
  117. $db_result = $this->ServerPost("{$this->ServiceURL}/get_date_plan/{$inTimestamp}/{$balanceTimestamp}");
  118. $decode_db_result = json_decode($db_result, true);
  119. if(! empty($decode_db_result)){
  120. foreach ($decode_db_result as $val){
  121. $holiday = array();
  122. $holiday[DatePlanKey::p_type] = $val[DatePlanKey::p_type];
  123. $holiday[DatePlanKey::p_date] = $val[DatePlanKey::p_date]; //date('Y-m-d H:i:s', );
  124. array_push($result, $holiday);
  125. }
  126. }
  127. return $result;
  128. }
  129. // 取得指定期間週末 (timestamp)
  130. private function get_weekdays($from, $to=false)
  131. {
  132. if ($to == false)
  133. $to = $this->last_day_of_month($from);
  134. $days = array();
  135. for ($x = $from; $x < $to; $x+=86400 ) { // 60*60*24 一天的秒數
  136. //if (date('w', $x) > 0 && date('w', $x) < 6)
  137. if (date('w', $x) == 0 || date('w', $x) == 6)
  138. $days[] = $x;
  139. }
  140. return $days;
  141. }
  142. // 取得當月最後一天 (timestamp)
  143. private function last_day_of_month($ts=false)
  144. {
  145. $m = date('m', $ts);
  146. $y = date('y', $ts);
  147. return mktime(23, 59, 59, ($m+1), 0, $y);
  148. }
  149. // 呼叫其它服務
  150. private function ServerPost($url, $parameters=array())
  151. {
  152. $ch = curl_init();
  153. curl_setopt($ch, CURLOPT_URL, $url);
  154. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  155. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  156. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  157. curl_setopt($ch, CURLOPT_POST, TRUE);
  158. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parameters));
  159. $rs = curl_exec($ch);
  160. curl_close($ch);
  161. return $rs;
  162. }
  163. // 產生日期當 lv1 key
  164. protected function genLv1Key($date)
  165. {
  166. return $date->format('Y-m-d');
  167. }
  168. // 產生時間當 lv2 key
  169. protected function genLv2Key($date)
  170. {
  171. return $date->format('H:i:s');
  172. }
  173. }