diff --git a/app/models/customer.rb b/app/models/customer.rb index 002bde6c..06f7f1d1 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -67,6 +67,9 @@ class Customer < ApplicationRecord }, :timeout => 10 ) + rescue HTTParty::Error + response = {status: false, message: "Can't open membership server " } + rescue Net::OpenTimeout response = { status: false , message: "Server Time out"} diff --git a/app/models/sale_item.rb b/app/models/sale_item.rb index a0bc1ae2..af2230de 100644 --- a/app/models/sale_item.rb +++ b/app/models/sale_item.rb @@ -66,26 +66,32 @@ class SaleItem < ApplicationRecord return discount_accounts end - # Calculate rebate_by_account + # Calculate rebate_by_account def self.calculate_rebate_by_account(sale_items) rebateacc = Account.where("rebate=?",true) - puts "Account that can rebate" - rebateacc.each do |i| - puts i.title - end - prices=0 - sale_items.each do |si| - price = self.get_rebate_price(si.sale_item_id,rebateacc) + price = 0 + rebate_arr = [] + rebateacc.each do |a| + account_price = {:type => a.title, :amount => 0} - prices = prices + price + # Check for actual sale items + sale_items.each do |si| + if si.account_id == a.id + account_price[:amount] = account_price[:amount] + si.price + price = price + si.price + end + + end + rebate_arr.push(account_price) end - return prices + + return price,rebate_arr end # get food price or beverage price for item def self.get_rebate_price(sale_item_id,rebateacc) - price=0 - + price = 0 + type = '' item=SaleItem.select("sale_items.price , menu_items.account_id") .joins("left join menu_items on menu_items.item_code = sale_items.product_code") .where("sale_items.sale_item_id=?", sale_item_id.to_s) @@ -93,12 +99,14 @@ class SaleItem < ApplicationRecord rebateacc.each do |i| if item[0].account_id == i.id price = item[0].price + type = i.title end end - return price + return price,type end + # def self.get_overall_discount(sale_id) # price = 0.0 # item=SaleItem.where("product_code=?", sale_id) diff --git a/app/models/sale_payment.rb b/app/models/sale_payment.rb index 8df6f1c0..e1047de8 100644 --- a/app/models/sale_payment.rb +++ b/app/models/sale_payment.rb @@ -77,7 +77,10 @@ class SalePayment < ApplicationRecord # Control for Paypar Cloud begin response = HTTParty.get(url, - :body => { app_token: token,membership_id:membership_id,campaign_type_id:campaign_type_id,merchant_uid:merchant_uid,auth_token:auth_token}.to_json, + :body => { app_token: token,membership_id:membership_id, + campaign_type_id:campaign_type_id,merchant_uid:merchant_uid, + auth_token:auth_token + }.to_json, :headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json' @@ -115,7 +118,14 @@ class SalePayment < ApplicationRecord # Control for Paypar Cloud begin response = HTTParty.post(url, - :body => { generic_customer_id:membership_id,total_amount: redeem_prices,total_sale_transaction_amount: sale_data.grand_total,redeem_amount:received_amount,receipt_no:sale_data.receipt_no,campaign_type_id:campaign_type_id,account_no:"",merchant_uid:merchant_uid,auth_token:auth_token}.to_json, + :body => { generic_customer_id:membership_id, + total_amount: redeem_prices, + total_sale_transaction_amount: sale_data.grand_total, + redeem_amount:received_amount, + receipt_no:sale_data.receipt_no, + campaign_type_id:campaign_type_id, + account_no:"",merchant_uid:merchant_uid, + auth_token:auth_token}.to_json, :headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json' @@ -261,7 +271,7 @@ class SalePayment < ApplicationRecord end self.sale.sale_status = "completed" - if MembershipSetting.find(1).rebate + if MembershipSetting.find_by_rebate(1) response = rebat(sObj) if !response.nil? @@ -280,12 +290,12 @@ class SalePayment < ApplicationRecord end end puts "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr" + puts response.to_json end self.sale.save! table_update_status(sObj) - if paid_amount != "0.0" update_shift end @@ -324,13 +334,14 @@ class SalePayment < ApplicationRecord end def rebat(sObj) - rebate_prices = SaleItem.calculate_rebate_by_account(sObj.sale_items) - + rebate_prices,campaign_method = SaleItem.calculate_rebate_by_account(sObj.sale_items) generic_customer_id = sObj.customer.membership_id if generic_customer_id.present? - paypar = sObj.sale_payments + + paypar = sObj.sale_payments payparcost = 0 - credit = 0 + credit = 0 + paypar.each do |pp| if pp.payment_method == "paypar" payparcost = payparcost + pp.payment_amount @@ -339,46 +350,113 @@ class SalePayment < ApplicationRecord end end # overall_dis = SaleItem.get_overall_discount(sObj.id) - overall_dis = sObj.total_discount - - total_amount = rebate_prices - payparcost - overall_dis - if credit == 1 - total_amount = 0 - end - if total_amount >= 0 - receipt_no = sObj.receipt_no - membership = MembershipSetting.find_by_membership_type("paypar_url") - memberaction = MembershipAction.find_by_membership_type("rebate") - merchant_uid = memberaction.merchant_account_id.to_s - campaign_type_id = memberaction.additional_parameter["campaign_type_id"] - auth_token = memberaction.auth_token.to_s - url = membership.gateway_url.to_s + memberaction.gateway_url.to_s + overall_dis = sObj.total_discount + + membership = MembershipSetting.find_by_membership_type("paypar_url") + memberaction = MembershipAction.find_by_membership_type("get_member_campaign") + merchant_uid = memberaction.merchant_account_id.to_s + campaign_type_id = memberaction.additional_parameter["campaign_type_id"] + auth_token = memberaction.auth_token.to_s + url = membership.gateway_url.to_s + memberaction.gateway_url.to_s # Control for Paypar Cloud begin - response = HTTParty.post(url, :body => { generic_customer_id:generic_customer_id ,total_sale_transaction_amount: sObj.grand_total,merchant_uid:merchant_uid,total_amount: total_amount,campaign_type_id: campaign_type_id, - receipt_no: receipt_no,auth_token:auth_token}.to_json, - :headers => { - 'Content-Type' => 'application/json', - 'Accept' => 'application/json' - }, :timeout => 10) + response = HTTParty.get(url, + :body => { + member_group_id:sObj.customer.membership_type, + merchant_uid:merchant_uid, + campaign_type_id: campaign_type_id, + auth_token:auth_token + }.to_json, + :headers => { + 'Content-Type' => 'application/json', + 'Accept' => 'application/json' + }, :timeout => 10) rescue Net::OpenTimeout response = { "status": false , "message": "Connect To" } - - rescue OpenURI::HTTPError response = { "status": false, "message": "Can't connect server"} rescue SocketError response = { "status": false, "message": "Can't connect server"} + end + + redeem_amount = payparcost + overall_dis + + total_percentage = 0 + + type_arr = [] + response["membership_campaign_data"].each do |a| + data = {:type => a["rules_type"], :percentage => a["change_unit"].to_i * a["base_unit"].to_i} + total_percentage = total_percentage + a["change_unit"].to_i * a["base_unit"].to_i + + type_arr.push(data) + end + + rebate_arr =[] + campaign_method.each do |a| + data = {:type => a[:type], :amount => a[:amount]} + puts data + type_arr.each do |si| + if si[:type] == a[:type] + puts "steeeeeeeeeeeee" + amount = (redeem_amount / total_percentage)*si[:percentage] + actual = a[:amount] - amount + data[:amount] = actual + end + + end + rebate_arr.push(data) + + end + puts "ssssssssss" + puts rebate_arr.to_json + total_amount = rebate_prices - payparcost - overall_dis + + if credit == 1 + total_amount = 0 + end + if total_amount >= 0 + receipt_no = sObj.receipt_no + membership = MembershipSetting.find_by_membership_type("paypar_url") + memberaction = MembershipAction.find_by_membership_type("rebate") + merchant_uid = memberaction.merchant_account_id.to_s + campaign_type_id = memberaction.additional_parameter["campaign_type_id"] + auth_token = memberaction.auth_token.to_s + url = membership.gateway_url.to_s + memberaction.gateway_url.to_s + + # Control for Paypar Cloud + begin + response = HTTParty.post(url, + :body => { + generic_customer_id:generic_customer_id , + total_sale_transaction_amount: sObj.grand_total, + merchant_uid:merchant_uid, + total_amount: total_amount, + campaign_type_id: campaign_type_id, + receipt_no: receipt_no, + campaign_method: rebate_arr.to_json, + auth_token:auth_token + }.to_json, + :headers => { + 'Content-Type' => 'application/json', + 'Accept' => 'application/json' + }, :timeout => 10) + rescue Net::OpenTimeout + response = { "status": false , "message": "Connect To" } + rescue OpenURI::HTTPError + response = { "status": false, "message": "Can't connect server"} + rescue SocketError + response = { "status": false, "message": "Can't connect server"} end return response + puts "latest respppppp" puts response.to_json - end + end else response = { "status": "no_member", "message": "Not membership"} - + end end diff --git a/db/seeds.rb b/db/seeds.rb index 08233594..317194c1 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -174,6 +174,7 @@ member_actions= MembershipAction.create([{membership_type:"get_account_balance", {membership_type:"get_all_member_account",gateway_url:"/api/generic_customer/get_membership_data",merchant_account_id:"vWSsseoZCzxd6xcNf_uS",auth_token:"code2lab"}, {membership_type:"get_member_transactions",gateway_url:"/api/generic_customer/get_membership_transactions",merchant_account_id:"vWSsseoZCzxd6xcNf_uS",auth_token:"code2lab"}, {membership_type:"member_discount",gateway_url:"/api/membership_campaigns/discount",additional_parameter:{campaign_type_id:6},merchant_account_id:"vWSsseoZCzxd6xcNf_uS",auth_token:"code2lab"}, + {membership_type:"get_member_campaign",gateway_url:"/api/membership_campaigns/get_member_campaign",additional_parameter:{campaign_type_id:6},merchant_account_id:"vWSsseoZCzxd6xcNf_uS",auth_token:"code2lab"}, ]) payment_methods = PaymentMethodSetting.create({payment_method:"MPU",gateway_url: "http://192.168.1.47:3006"})