278 lines
9.5 KiB
Ruby
278 lines
9.5 KiB
Ruby
class SalePayment < ApplicationRecord
|
|
self.primary_key = "sale_payment_id"
|
|
|
|
#primary key - need to be unique generated for multiple shops
|
|
before_create :generate_custom_id
|
|
|
|
belongs_to :sale
|
|
|
|
attr_accessor :received_amount, :card_payment_reference, :voucher_no, :giftcard_no, :customer_id, :external_payment_status
|
|
|
|
def process_payment(invoice, action_by, cash_amount, payment_method)
|
|
|
|
self.sale = invoice
|
|
self.received_amount = cash_amount
|
|
amount_due = invoice.grand_total
|
|
|
|
#get all payment for this invoices
|
|
invoice.sale_payments.each do |payment|
|
|
if (payment.payment_status == "paid" )
|
|
amount_due = amount_due - payment.payment_amount
|
|
end
|
|
end
|
|
|
|
if (amount_due > 0)
|
|
payment_status = false
|
|
#route to payment type
|
|
case payment_method
|
|
when "cash"
|
|
payment_status = cash_payment
|
|
when "creditnote"
|
|
if !self.customer_id.nil?
|
|
payment_status = creditnote_payment(self.customer_id)
|
|
end
|
|
when "visa"
|
|
payment_status = external_terminal_card_payment(:visa)
|
|
when "master"
|
|
payment_status = external_terminal_card_payment(:master)
|
|
when "jcb"
|
|
payment_status = external_terminal_card_payment(:jcb)
|
|
when "mpu"
|
|
payment_status = external_terminal_card_payment(:mpu)
|
|
when "unionpay"
|
|
payment_status = external_terminal_card_payment(:unionpay)
|
|
when "vochure"
|
|
payment_status = vochure_payment
|
|
when "giftcard"
|
|
payment_status = giftcard_payment
|
|
when "paypar"
|
|
payment_status = paypar_payment
|
|
else
|
|
puts "it was something else"
|
|
end
|
|
|
|
#record an payment in sale-audit
|
|
remark = "Payment #{payment_method}- for Invoice #{invoice.receipt_no} Due [#{amount_due}]| pay amount -> #{cash_amount} | Payment Status ->#{payment_status}"
|
|
sale_audit = SaleAudit.record_payment(invoice.id, remark, action_by)
|
|
|
|
return true, self.sale
|
|
else
|
|
#record an payment in sale-audit
|
|
remark = "No outstanding Amount - Grand Total [#{invoice.grand_total}] | Due [#{amount_due}] | Paid [#{invoice.amount_received}]"
|
|
sale_audit = SaleAudit.record_payment(invoice.id, remark,action_by)
|
|
|
|
return false, "No outstanding Amount"
|
|
end
|
|
|
|
end
|
|
|
|
def self.get_paypar_account(url,token,membership_id,campaign_type_id,merchant_uid,auth_token)
|
|
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,
|
|
:headers => {
|
|
'Content-Type' => 'application/json',
|
|
'Accept' => 'application/json'
|
|
}
|
|
)
|
|
return response;
|
|
|
|
end
|
|
|
|
def self.redeem(paypar_url,token,membership_id,received_amount,sale_id)
|
|
membership_actions_data = MembershipAction.find_by_membership_type("redeem");
|
|
if !membership_actions_data.nil?
|
|
url = paypar_url.to_s + membership_actions_data.gateway_url.to_s
|
|
merchant_uid = membership_actions_data.merchant_account_id
|
|
auth_token = membership_actions_data.auth_token
|
|
campaign_type_id = membership_actions_data.additional_parameter["campaign_type_id"]
|
|
sale_data = Sale.find_by_sale_id(sale_id)
|
|
if sale_data
|
|
response = HTTParty.post(url,
|
|
:body => { generic_customer_id:membership_id,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'
|
|
}
|
|
)
|
|
else
|
|
response = false;
|
|
end
|
|
|
|
else
|
|
response =false;
|
|
end
|
|
return response;
|
|
|
|
end
|
|
|
|
private
|
|
def cash_payment
|
|
payment_status = false
|
|
self.payment_method = "cash"
|
|
self.payment_amount = self.received_amount
|
|
self.outstanding_amount = self.sale.grand_total.to_f - self.received_amount.to_f
|
|
self.payment_status = "paid"
|
|
payment_method = self.save!
|
|
sale_update_payment_status(self.received_amount)
|
|
|
|
return payment_status
|
|
end
|
|
|
|
def creditnote_payment(customer_id)
|
|
|
|
payment_status = false
|
|
|
|
self.payment_method = "creditnote"
|
|
self.payment_amount = self.received_amount
|
|
self.customer_id = self.customer_id
|
|
self.outstanding_amount = 0 - self.received_amount
|
|
self.payment_status = "outstanding"
|
|
payment_method = self.save!
|
|
|
|
sale_update_payment_status(self.received_amount)
|
|
|
|
return payment_status
|
|
end
|
|
|
|
def external_terminal_card_payment(method)
|
|
payment_status = false
|
|
|
|
self.payment_method = method
|
|
self.payment_amount = self.received_amount
|
|
self.payment_reference = self.card_payment_reference
|
|
self.outstanding_amount = self.sale.grand_total.to_f - self.received_amount.to_f
|
|
self.payment_status = "paid"
|
|
payment_method = self.save!
|
|
|
|
sale_update_payment_status(self.received_amount)
|
|
|
|
return payment_status
|
|
end
|
|
|
|
def voucher_payment
|
|
payment_status = false
|
|
|
|
#Next time - validate if the vochure number is valid - within
|
|
self.payment_method = "voucher"
|
|
self.payment_amount = self.received_amount
|
|
self.payment_reference = self.voucher_no
|
|
self.outstanding_amount = self.sale.grand_total- self.received_amount
|
|
self.payment_status = "paid"
|
|
payment_method = self.save!
|
|
|
|
sale_update_payment_status(self.received_amount)
|
|
|
|
return payment_status
|
|
|
|
end
|
|
|
|
def giftcard_payment
|
|
payment_status = false
|
|
|
|
#Next time - validate if the vochure number is valid - within
|
|
self.payment_method = "giftcard"
|
|
self.payment_amount = self.received_amount
|
|
self.payment_reference = self.giftcard_no
|
|
self.outstanding_amount = self.sale.grand_total- self.received_amount
|
|
self.payment_status = "paid"
|
|
payment_method = self.save!
|
|
|
|
sale_update_payment_status(self.received_amount)
|
|
|
|
return payment_status
|
|
|
|
end
|
|
|
|
def paypar_payment
|
|
payment_status = false
|
|
|
|
#Next time - validate if the vochure number is valid - within
|
|
self.payment_method = "paypar"
|
|
self.payment_amount = self.received_amount
|
|
self.payment_reference = self.voucher_no
|
|
self.outstanding_amount = self.sale.grand_total.to_f - self.received_amount.to_f
|
|
self.payment_status = "pending"
|
|
payment_method = self.save!
|
|
|
|
customer_data = Customer.find_by_customer_id(self.sale.customer_id)
|
|
membership_setting = MembershipSetting.find_by_membership_type("paypar_url")
|
|
membership_data = SalePayment.redeem(membership_setting.gateway_url,membership_setting.auth_token,customer_data.membership_id,self.received_amount,self.sale.sale_id)
|
|
if membership_data["status"]==true
|
|
SalePayment.where(:sale_payment_id => self.sale_payment_id).update_all(:payment_status => 'paid')
|
|
sale_update_payment_status(self.received_amount.to_f)
|
|
|
|
else
|
|
sale_update_payment_status(0)
|
|
end
|
|
|
|
return payment_status
|
|
|
|
end
|
|
|
|
def sale_update_payment_status(paid_amount)
|
|
#update amount_outstanding
|
|
self.sale.amount_received = self.sale.amount_received.to_f + paid_amount.to_f
|
|
self.sale.amount_changed = paid_amount.to_f - self.sale.amount_received.to_f
|
|
all_received_amount = 0.0
|
|
sObj = Sale.find(self.sale_id)
|
|
sObj.sale_payments.each do |spay|
|
|
all_received_amount += spay.payment_amount.to_f
|
|
end
|
|
if (self.sale.grand_total <= all_received_amount)
|
|
self.sale.payment_status = "paid"
|
|
self.sale.sale_status = "completed"
|
|
self.sale.save!
|
|
table_update_status(sObj)
|
|
rebat(sObj)
|
|
end
|
|
|
|
end
|
|
|
|
def table_update_status(sale_obj)
|
|
booking = Booking.find_by_sale_id(sale_obj.id)
|
|
if booking
|
|
table = DiningFacility.find(booking.dining_facility_id)
|
|
if table
|
|
table.status = "available"
|
|
table.save
|
|
end
|
|
end
|
|
end
|
|
|
|
def rebat(sObj)
|
|
food_prices, beverage_prices = SaleItem.calculate_food_beverage(sObj.sale_items)
|
|
generic_customer_id = sObj.customer.membership_id
|
|
if generic_customer_id != nil || generic_customer_id != "" || generic_customer_id != 0
|
|
paypar = sObj.sale_payments
|
|
payparcost = 0
|
|
paypar.each do |pp|
|
|
if pp.payment_method == "paypar"
|
|
payparcost = payparcost + pp.payment_amount
|
|
end
|
|
end
|
|
total_amount = food_prices - payparcost
|
|
if total_amount > 0
|
|
receipt_no = sObj.receipt_no
|
|
membership = MembershipSetting.find_by_membership_type("paypar_url")
|
|
memberaction = MembershipAction.find_by_membership_type("rebate")
|
|
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
|
|
response = HTTParty.post(url, :body => { generic_customer_id:generic_customer_id ,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'
|
|
})
|
|
|
|
puts response.to_json
|
|
end
|
|
end
|
|
end
|
|
|
|
private
|
|
def generate_custom_id
|
|
self.sale_payment_id = SeedGenerator.generate_id(self.class.name, "SPI")
|
|
end
|
|
end
|