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.

340 wiersze
9.7KB

  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_uuid_ready_bill($uuid)
  22. {
  23. $where_arr['uuid'] = $uuid;
  24. $where_arr['status'] = 1;
  25. $result = $this->db->select('order_no, invoice_remark, product_plan, tx_time')
  26. ->from('product_bill')
  27. ->where($where_arr)->order_by("tx_time", "desc")
  28. ->get()
  29. ->result_array();
  30. return $result;
  31. }
  32. // 取得待兌換訂單 (發票)
  33. public function q_invoice_ready_bill($invoice_no)
  34. {
  35. $where_arr['invoice_no'] = $invoice_no;
  36. $where_arr['status'] = 1;
  37. $result = $this->db->select('order_no, invoice_remark, product_plan, tx_time')
  38. ->from('product_bill')
  39. ->where($where_arr)->order_by("tx_time", "desc")
  40. ->get()
  41. ->row_array();
  42. return $result;
  43. }
  44. // 取得產品資訊
  45. public function q_product($product_id=0, $product_code=PRODUCT_CODE_COFFEE_SHOP)
  46. {
  47. $now = date('Y/m/d H:i:s');
  48. $where_arr = array('start_time <= ' => $now, 'valid_time > ' => $now);
  49. // 指定產品流水號
  50. if($product_id != 0)
  51. $where_arr['product_id'] = $product_id;
  52. // 指定產品包
  53. $where_arr['product_code'] = $product_code;
  54. $data = array();
  55. $result = $this->db->select('product_id, product_code, product_name, product_desc, amt, remarks, product_plan')
  56. ->from('products')
  57. ->where($where_arr)->order_by("create_time", "desc")
  58. ->limit(1)
  59. ->get()
  60. ->row_array();
  61. return $result;
  62. }
  63. // 產生交易序號
  64. private function gen_trx_no()
  65. {
  66. return time().rand(10000,99999);
  67. }
  68. // S.1. 建立產品訂單
  69. public function create_product_bill($product_id, $product_code)
  70. {
  71. // 取得商品資訊
  72. $product_info = $this->q_product($product_id, $product_code);
  73. if(!isset($product_info['product_plan']))
  74. {
  75. return 'unknown_product'; // 中斷
  76. }
  77. $data = array();
  78. $data['order_no'] = $this->gen_trx_no();
  79. $data['product_id'] = $product_info["product_id"];
  80. $data['product_code'] = $product_info["product_code"];
  81. $data['product_plan'] = $product_info["product_plan"];
  82. $data['invoice_remark'] = $product_info["product_name"];
  83. $data['amt'] = $product_info["amt"];
  84. $data['valid_time'] = date('Y-m-d H:i:s', strtotime(" + 15 minutes")); // 15 min 內有效
  85. $this->db->insert('product_bill', $data);
  86. $affect_rows = $this->db->affected_rows();
  87. if ($affect_rows <= 0)
  88. return 'fail';
  89. trigger_error(__FUNCTION__ . '..' . print_r($data, true));
  90. return $data;
  91. }
  92. // S.2. 處理產品訂單
  93. public function proceed_product_bill($parms, $tx_type=0)
  94. {
  95. $order_no = $parms['order_no'];
  96. $invoice_receiver = $parms['invoice_receiver'];
  97. $company_no = $parms['company_no'];
  98. $email = $parms['email'];
  99. $mobile = $parms['mobile'];
  100. $uuid = $parms['uuid'];
  101. $product_info = $this->db->select('valid_time, product_plan')
  102. ->from('product_bill')
  103. ->where(array('order_no' => $order_no))
  104. ->limit(1)
  105. ->get()
  106. ->row_array();
  107. if(!isset($product_info['product_plan']))
  108. {
  109. trigger_error(__FUNCTION__ . "|{$order_no}|unknown_order");
  110. return 'unknown_order'; // 中斷
  111. }
  112. if(!isset($product_info['valid_time']))
  113. {
  114. trigger_error(__FUNCTION__ . "|{$order_no}|valid_time_not_found");
  115. return 'valid_time_not_found'; // 中斷
  116. }
  117. $data = array();
  118. $data['tx_type'] = $tx_type; // 交易種類: 0:未定義, 1:現金, 40:博辰人工模組, 41:博辰自動繳費機, 50:歐付寶轉址刷卡, 51:歐付寶APP, 52:歐付寶轉址WebATM, 60:中國信託刷卡轉址
  119. $data['uuid'] = $uuid; // 客戶代號
  120. if(strlen($company_no) >= 8)
  121. {
  122. $data['company_no'] = $company_no; // 電子發票:公司統編
  123. $data['company_receiver'] = "公司名稱"; // 電子發票:公司名稱
  124. $data['company_address'] = "公司地址"; // 電子發票:公司地址
  125. }
  126. $data['invoice_receiver'] = (strlen($invoice_receiver) >= 7) ? $invoice_receiver : ''; // 電子發票:載具編號
  127. $data['email'] = (strlen($email) >= 5) ? $email : ''; // 電子發票:email
  128. $data['mobile'] = (strlen($mobile) >= 10) ? $mobile : ''; // 電子發票:手機
  129. // 交易時間
  130. $tx_time = time();
  131. $data['tx_time'] = date('Y/m/d H:i:s', $tx_time);
  132. if(strtotime($product_info['valid_time']) < $tx_time)
  133. {
  134. $data['status'] = 99; //狀態: 99:訂單逾期作廢
  135. $this->db->update('product_bill', $data, array('order_no' => $order_no));
  136. trigger_error(__FUNCTION__ . "|{$order_no}| 99 gg");
  137. return 'gg';
  138. }
  139. // 完成
  140. $data['status'] = 100; // 狀態: 100:交易進行中
  141. $this->db->update('product_bill', $data, array('order_no' => $order_no));
  142. $affect_rows = $this->db->affected_rows();
  143. if ($affect_rows <= 0)
  144. return 'fail';
  145. $data['order_no'] = $order_no;
  146. trigger_error(__FUNCTION__ . ".." . print_r($data, true));
  147. return $data;
  148. }
  149. // S.3. 更新產品訂單
  150. public function reload_product_bill($order_no, $invoice_no, $product_plan)
  151. {
  152. // 更新為已結帳
  153. $this->db->update('product_bill',
  154. array('status' => 1, 'invoice_no' => $invoice_no, 'product_plan' => $product_plan),
  155. array('status' => 100, 'order_no' => $order_no)
  156. );
  157. $affect_rows = $this->db->affected_rows();
  158. if ($affect_rows <= 0)
  159. return 'fail';
  160. // 兌換
  161. return $this->redeem_product_bill($order_no);
  162. }
  163. // S.4. 兌換訂單商品
  164. public function redeem_product_bill($order_no)
  165. {
  166. $product_info = $this->db->select('invoice_no, product_plan, uuid')
  167. ->from('product_bill')
  168. ->where(array('order_no' => $order_no, 'status' => 1))
  169. ->limit(1)
  170. ->get()
  171. ->row_array();
  172. $invoice_no = $product_info['invoice_no'];
  173. $product_plan = $product_info['product_plan'];
  174. $uuid = $product_info['uuid'];
  175. // 取得訂單內容
  176. $data = json_decode($product_plan, true);
  177. if(!isset($data['product_code']))
  178. {
  179. trigger_error(__FUNCTION__ . "|$order_no|not_found");
  180. return 'not_found';
  181. }
  182. $product_code = $data['product_code'];
  183. // 咖啡
  184. if($product_code == PRODUCT_CODE_COFFEE)
  185. {
  186. // 取得產品包資訊
  187. $station_no = $data['station_no'];
  188. $amount = $data['amount'];
  189. $memo = $data['memo'];
  190. $product = $this->q_product(0, $product_code);
  191. if(!isset($product['product_id']))
  192. {
  193. trigger_error(__FUNCTION__ . "|$order_no|$station_no, $product_code, $amount, $memo|product_not_found");
  194. return 'product_contain_not_found';
  195. }
  196. trigger_error(__FUNCTION__ . "|$order_no|$station_no, $product_code, $amount, $memo|兌換|" . print_r($product, true));
  197. // 兌換產品
  198. return $this->redeem_product($order_no, $invoice_no, $product, $uuid, $amount);
  199. }
  200. trigger_error(__FUNCTION__ . "|$order_no|undefined_product|" . print_r($data, true));
  201. return 'undefined_product';
  202. }
  203. // 兌換產品
  204. private function redeem_product($order_no, $invoice_no, $product, $uuid, $amount=1)
  205. {
  206. // [A.開始]
  207. $this->db->trans_start();
  208. // 兌換時間
  209. $tx_time = date('Y/m/d H:i:s');
  210. for($i = 0; $i < $amount; $i++)
  211. {
  212. $item = array();
  213. $item['uuid'] = $uuid; // 用戶代號
  214. $item['order_no'] = $this->gen_trx_no();
  215. $item['tx_time'] = $tx_time;
  216. $item['invoice_no'] = $invoice_no;
  217. $item['product_id'] = $product["product_id"];
  218. $item['product_code'] = $product["product_code"];
  219. $item['product_plan'] = $product["product_plan"];
  220. $item['invoice_remark'] = $product["product_name"];
  221. $item['amt'] = $product["amt"];
  222. $item['status'] = 1;
  223. $this->db->insert('product_bill', $item);
  224. }
  225. // 更新為已領取
  226. $this->db->update('product_bill', array('status' => 111), array('status' => 1, 'order_no' => $order_no));
  227. // [C.完成]
  228. $this->db->trans_complete();
  229. if ($this->db->trans_status() === FALSE)
  230. {
  231. trigger_error(__FUNCTION__ . "..$order_no, $invoice_no..trans error..". '| last_query: ' . $this->db->last_query());
  232. return 'fail'; // 中斷
  233. }
  234. return 'ok';
  235. }
  236. // S.5. 兌換 (兌換 商品包 或 商品)
  237. public function redeem_order($order_no)
  238. {
  239. $product_info = $this->db->select('order_no, product_code, product_plan')
  240. ->from('product_bill')
  241. ->where(array('order_no' => $order_no, 'status' => 1))
  242. ->limit(1)
  243. ->get()
  244. ->row_array();
  245. if(!isset($product_info['product_code']))
  246. {
  247. trigger_error(__FUNCTION__ . "|$order_no|not_found");
  248. return 'not_found';
  249. }
  250. $bill_product_code = $product_info['product_code'];
  251. // 兌換產品包
  252. if($bill_product_code == PRODUCT_CODE_COFFEE_SHOP)
  253. {
  254. return $this->redeem_product_bill($order_no);
  255. }
  256. // 兌換咖啡
  257. else if($bill_product_code == PRODUCT_CODE_COFFEE)
  258. {
  259. // [A.開始]
  260. $this->db->trans_start();
  261. // 兌換時間
  262. $tx_time = date('Y/m/d H:i:s');
  263. // 更新為已領取
  264. $this->db->update('product_bill', array('status' => 111, 'tx_time' => $tx_time), array('status' => 1, 'order_no' => $order_no));
  265. // [C.完成]
  266. $this->db->trans_complete();
  267. if ($this->db->trans_status() === FALSE)
  268. {
  269. trigger_error(__FUNCTION__ . "..$order_no..trans error..". '| last_query: ' . $this->db->last_query());
  270. return 'fail'; // 中斷
  271. }
  272. trigger_error(__FUNCTION__ . "|$order_no|已領取|" . print_r($product_info, true));
  273. return 'ok';
  274. }
  275. return 'gg';
  276. }
  277. }