VM暫存
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

147 wiersze
4.4KB

  1. <?php
  2. /*
  3. file: Shop_model.php 購物
  4. */
  5. class Shop_model extends CI_Model
  6. {
  7. var $vars = array();
  8. function __construct()
  9. {
  10. parent::__construct();
  11. $this->load->database();
  12. // product code
  13. define('PRODUCT_CODE_COFFEE_SHOP', 'coffee_shop'); // 咖啡產品包
  14. define('PRODUCT_CODE_COFFEE', 'coffee'); // 咖啡
  15. }
  16. public function init($vars)
  17. {
  18. $this->vars = $vars;
  19. }
  20. // 取得產品資訊
  21. public function q_product($product_id=0, $product_code='')
  22. {
  23. $now = date('Y/m/d H:i:s');
  24. $where_arr = array('start_time <= ' => $now, 'valid_time > ' => $now);
  25. $where_arr['product_id'] = $product_id; // 指定產品流水號
  26. $where_arr['product_code'] = $product_code; // 指定產品包
  27. $data = array();
  28. $result = $this->db->select('product_id, product_code, product_name, product_desc, amt, remarks, product_plan')
  29. ->from('products')
  30. ->where($where_arr)
  31. ->limit(1)
  32. ->get()
  33. ->row_array();
  34. return $result;
  35. }
  36. // 產生交易序號
  37. private function gen_trx_no()
  38. {
  39. return time().rand(10000,99999);
  40. }
  41. // 建立產品訂單
  42. public function create_product_bill($product_id, $product_code)
  43. {
  44. // 取得商品資訊
  45. $product_info = $this->q_product($product_id, $product_code);
  46. if(!isset($product_info['product_plan']))
  47. {
  48. return 'unknown_product'; // 中斷
  49. }
  50. $data = array();
  51. $data['order_no'] = $this->gen_trx_no();
  52. $data['product_id'] = $product_info["product_id"];
  53. $data['product_code'] = $product_info["product_code"];
  54. $data['product_plan'] = $product_info["product_plan"];
  55. $data['invoice_remark'] = $product_info["product_name"];
  56. $data['amt'] = $product_info["amt"];
  57. $data['valid_time'] = date('Y-m-d H:i:s', strtotime(" + 15 minutes")); // 15 min 內有效
  58. $this->db->insert('product_bill', $data);
  59. $affect_rows = $this->db->affected_rows();
  60. if ($affect_rows <= 0)
  61. return 'fail';
  62. trigger_error(__FUNCTION__ . '..' . print_r($data, true));
  63. return $data;
  64. }
  65. // 處理產品訂單
  66. public function proceed_product_bill($parms, $tx_type=0)
  67. {
  68. $order_no = $parms['order_no'];
  69. $invoice_receiver = $parms['invoice_receiver'];
  70. $company_no = $parms['company_no'];
  71. $email = $parms['email'];
  72. $mobile = $parms['mobile'];
  73. $product_info = $this->db->select('valid_time, product_plan')
  74. ->from('product_bill')
  75. ->where(array('order_no' => $order_no))
  76. ->limit(1)
  77. ->get()
  78. ->row_array();
  79. if(!isset($product_info['product_plan']))
  80. {
  81. trigger_error(__FUNCTION__ . "|{$order_no}|unknown_order");
  82. return 'unknown_order'; // 中斷
  83. }
  84. if(!isset($product_info['valid_time']))
  85. {
  86. trigger_error(__FUNCTION__ . "|{$order_no}|valid_time_not_found");
  87. return 'valid_time_not_found'; // 中斷
  88. }
  89. $data = array();
  90. $data['tx_type'] = $tx_type; // 交易種類: 0:未定義, 1:現金, 40:博辰人工模組, 41:博辰自動繳費機, 50:歐付寶轉址刷卡, 51:歐付寶APP, 52:歐付寶轉址WebATM, 60:中國信託刷卡轉址
  91. if(strlen($company_no) >= 8)
  92. {
  93. $data['company_no'] = $company_no; // 電子發票:公司統編
  94. $data['company_receiver'] = "公司名稱"; // 電子發票:公司名稱
  95. $data['company_address'] = "公司地址"; // 電子發票:公司地址
  96. }
  97. $data['invoice_receiver'] = (strlen($invoice_receiver) >= 7) ? $invoice_receiver : ''; // 電子發票:載具編號
  98. $data['email'] = (strlen($email) >= 5) ? $email : ''; // 電子發票:email
  99. $data['mobile'] = (strlen($mobile) >= 10) ? $mobile : ''; // 電子發票:手機
  100. // 交易時間
  101. $tx_time = time();
  102. $data['tx_time'] = date('Y/m/d H:i:s', $tx_time);
  103. if(strtotime($product_info['valid_time']) < $tx_time)
  104. {
  105. $data['status'] = 99; //狀態: 99:訂單逾期作廢
  106. $this->db->update('product_bill', $data, array('order_no' => $order_no));
  107. trigger_error(__FUNCTION__ . "|{$order_no}| 99 gg");
  108. return 'gg';
  109. }
  110. // 完成
  111. $data['status'] = 100; // 狀態: 100:交易進行中
  112. $this->db->update('product_bill', $data, array('order_no' => $order_no));
  113. $affect_rows = $this->db->affected_rows();
  114. if ($affect_rows <= 0)
  115. return 'fail';
  116. $data['order_no'] = $order_no;
  117. trigger_error(__FUNCTION__ . ".." . print_r($data, true));
  118. return $data;
  119. }
  120. }