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

Shop_model.php 4.5KB

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