diff --git a/controllers/Shop.php b/controllers/Shop.php index 490da28..7c32941 100644 --- a/controllers/Shop.php +++ b/controllers/Shop.php @@ -18,13 +18,33 @@ class Shop extends CC_Controller // 付款流程頁面 (返回) public function client_back() { + trigger_error(__FUNCTION__ . '..'. print_r($_POST, true)); $this->show_page('main_page'); } - // 付款流程頁面 (返回) + // 付款流程頁面 (完成, 返回) public function order_result() { - $this->show_page('main_page'); + trigger_error(__FUNCTION__ . '..'. print_r($_POST, true)); + $order_no = $this->input->post('order_no', true); + $product_plan = $this->input->post('product_plan', true); + $invoice_no = $this->input->post('invoice_no', true); + $ck = $this->input->post('ck', true); + + // 建立頁面資料 + $data = array(); + $data['invoice_no'] = $invoice_no; + + // 更新產品訂單 + if($ck = md5($order_no.'alt'.$product_plan.'ob'.$invoice_no)) + { + $this->app_model()->reload_product_bill($order_no, $invoice_no, $product_plan); + + // 取得發票待兌換訂單 + //$data['invoice_ready_bill'] = $this->app_model()->q_invoice_ready_bill($invoice_no); + } + + $this->show_page('main_page', $data); } // 咖啡包預覽頁 @@ -107,6 +127,7 @@ class Shop extends CC_Controller echo $result; } + /* // 買 public function i_do() { @@ -119,4 +140,13 @@ class Shop extends CC_Controller echo 'ok'; } + public function test_product_plan() + { + $order_no = '151246880713876'; + + $this->app_model()->redeem_product_bill($order_no); + echo 'ok'; + } + */ + } diff --git a/models/Shop_model.php b/models/Shop_model.php index c97cb97..4f35ec0 100644 --- a/models/Shop_model.php +++ b/models/Shop_model.php @@ -22,18 +22,38 @@ class Shop_model extends CI_Model $this->vars = $vars; } + // 取得發票待兌換訂單 + public function q_invoice_ready_bill($invoice_no) + { + $where_arr['invoice_no'] = $invoice_no; + $where_arr['status'] = 1; + + $data = array(); + $result = $this->db->select('product_id, product_code, product_name, invoice_remark, product_plan') + ->from('product_bill') + ->where($where_arr)->order_by("create_time", "desc") + ->get() + ->row_array(); + return $result; + } + // 取得產品資訊 public function q_product($product_id=0, $product_code=PRODUCT_CODE_COFFEE_SHOP) { $now = date('Y/m/d H:i:s'); $where_arr = array('start_time <= ' => $now, 'valid_time > ' => $now); - $where_arr['product_id'] = $product_id; // 指定產品流水號 - $where_arr['product_code'] = $product_code; // 指定產品包 + + // 指定產品流水號 + if($product_id != 0) + $where_arr['product_id'] = $product_id; + + // 指定產品包 + $where_arr['product_code'] = $product_code; $data = array(); $result = $this->db->select('product_id, product_code, product_name, product_desc, amt, remarks, product_plan') ->from('products') - ->where($where_arr) + ->where($where_arr)->order_by("create_time", "desc") ->limit(1) ->get() ->row_array(); @@ -46,7 +66,7 @@ class Shop_model extends CI_Model return time().rand(10000,99999); } - // 建立產品訂單 + // S.1. 建立產品訂單 public function create_product_bill($product_id, $product_code) { // 取得商品資訊 @@ -76,7 +96,7 @@ class Shop_model extends CI_Model return $data; } - // 處理產品訂單 + // S.2. 處理產品訂單 public function proceed_product_bill($parms, $tx_type=0) { $order_no = $parms['order_no']; @@ -143,4 +163,110 @@ class Shop_model extends CI_Model trigger_error(__FUNCTION__ . ".." . print_r($data, true)); return $data; } + + // S.3. 更新產品訂單 + public function reload_product_bill($order_no, $invoice_no, $product_plan) + { + // 更新為已結帳 + $this->db->update('product_bill', + array('status' => 1, 'invoice_no' => $invoice_no, 'product_plan' => $product_plan), + array('status' => 100, 'order_no' => $order_no) + ); + + $affect_rows = $this->db->affected_rows(); + + if ($affect_rows <= 0) + return 'fail'; + + // 兌換 + return $this->redeem_product_bill($order_no); + } + + // S.4. 兌換訂單商品 + public function redeem_product_bill($order_no) + { + $product_info = $this->db->select('invoice_no, product_plan') + ->from('product_bill') + ->where(array('order_no' => $order_no)) + ->limit(1) + ->get() + ->row_array(); + + $invoice_no = $product_info['invoice_no']; + $product_plan = $product_info['product_plan']; + + // 取得訂單內容 + $data = json_decode($product_plan, true); + + if(!isset($data['product_code'])) + { + trigger_error(__FUNCTION__ . "|$order_no|not_found"); + return 'not_found'; + } + + $product_code = $data['product_code']; + + // 咖啡 + if($product_code == PRODUCT_CODE_COFFEE) + { + // 取得產品包資訊 + $station_no = $data['station_no']; + $amount = $data['amount']; + $memo = $data['memo']; + $product = $this->q_product(0, $product_code); + + if(!isset($product['product_id'])) + { + trigger_error(__FUNCTION__ . "|$order_no|$station_no, $product_code, $amount, $memo|product_not_found"); + return 'product_contain_not_found'; + } + + trigger_error(__FUNCTION__ . "|$order_no|$station_no, $product_code, $amount, $memo|兌換|" . print_r($product, true)); + + // 兌換產品 + return $this->redeem_product($order_no, $invoice_no, $product, $amount); + } + + trigger_error(__FUNCTION__ . "|$order_no|undefined_product|" . print_r($data, true)); + return 'undefined_product'; + } + + // 兌換產品 + function redeem_product($order_no, $invoice_no, $product, $amount=1) + { + // [A.開始] + $this->db->trans_start(); + + // 兌換時間 + $tx_time = date('Y/m/d H:i:s'); + + for($i = 0; $i < $amount; $i++) + { + $item = array(); + $item['order_no'] = $this->gen_trx_no(); + $item['tx_time'] = $tx_time; + $item['invoice_no'] = $invoice_no; + $item['product_id'] = $product["product_id"]; + $item['product_code'] = $product["product_code"]; + $item['product_plan'] = $product["product_plan"]; + $item['invoice_remark'] = $product["product_name"]; + $item['amt'] = $product["amt"]; + $item['status'] = 1; + $this->db->insert('product_bill', $item); + } + + // 更新為已領取 + $this->db->update('product_bill', array('status' => 111), array('status' => 1, 'order_no' => $order_no)); + + // [C.完成] + $this->db->trans_complete(); + if ($this->db->trans_status() === FALSE) + { + trigger_error(__FUNCTION__ . "..$order_no, $invoice_no..trans error..". '| last_query: ' . $this->db->last_query()); + return 'fail'; // 中斷 + } + + return 'ok'; + } + } diff --git a/views/shop/main_page.php b/views/shop/main_page.php index c3a2f70..1060b23 100644 --- a/views/shop/main_page.php +++ b/views/shop/main_page.php @@ -175,6 +175,7 @@ PRODUCT_RESULT.product_name = ''; PRODUCT_RESULT.product_desc = ''; PRODUCT_RESULT.amt = ''; PRODUCT_RESULT.remarks = ''; +PRODUCT_RESULT.invoice_no = ''; // 暫存區 var current_page_tags; // 目前所在頁面