1064 lines
41 KiB
Ruby
Executable File
1064 lines
41 KiB
Ruby
Executable File
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
|
|
|
|
has_one :sale_audit, -> { where "SUBSTRING_INDEX(sale_audits.remark,'||',1) = sale_payments.sale_payment_id" }, foreign_key: "sale_id", primary_key: "sale_id"
|
|
|
|
attr_accessor :received_amount, :card_payment_reference, :voucher_no, :giftcard_no, :customer_id, :external_payment_status,:action_by
|
|
|
|
scope :credits, -> { where(payment_method: 'creditnote') }
|
|
scope :cards, -> { where(payment_method: ['mpu', 'visa', 'master', 'jcb', 'unionpay', 'alipay', 'paymal', 'dinga', 'JunctionPay', 'giftvoucher']) }
|
|
|
|
def self.sync_sale_payment_records(sale_payments)
|
|
if !sale_payments.nil?
|
|
sale_payments.each do |sp|
|
|
payment = SalePayment.find_by_sale_payment_id(sp['sale_payment_id'])
|
|
# unless SalePayment.exists?(sp['sale_payment_id'])
|
|
if payment.nil?
|
|
payment = SalePayment.new
|
|
end
|
|
payment.sale_payment_id = sp['sale_payment_id']
|
|
payment.sale_id = sp['sale_id']
|
|
payment.payment_method = sp['payment_method']
|
|
payment.payment_amount = sp['payment_amount']
|
|
payment.outstanding_amount = sp['outstanding_amount']
|
|
payment.payment_reference = sp['payment_reference']
|
|
payment.payment_status = sp['payment_status']
|
|
payment.save
|
|
end
|
|
Rails.logger.debug '....... Sale Payment sync completed ......'
|
|
end
|
|
end
|
|
|
|
def self.get_kbz_pay_amount(sale_id, current_user)
|
|
amount = 0
|
|
kbz_pay_method = PaymentMethodSetting.where(:payment_method => KbzPay::KBZ_PAY).last
|
|
sale_payment = SalePayment.where('sale_id=? and payment_method=? and payment_status!=?', sale_id, KbzPay::KBZ_PAY, 'dead').last
|
|
if !sale_payment.nil? and !kbz_pay_method.nil?
|
|
if sale_payment.payment_status == 'pending'
|
|
amount = KbzPay.query(sale_payment.id, current_user, kbz_pay_method.gateway_url, kbz_pay_method.auth_token, kbz_pay_method.merchant_account_id, kbz_pay_method.additional_parameters)
|
|
elsif sale_payment.payment_status == 'paid'
|
|
amount = sale_payment.payment_amount
|
|
end
|
|
end
|
|
|
|
return amount
|
|
end
|
|
|
|
def process_kbz_payment(sale_id, grand_total, pay_amount, status)
|
|
if status == 'pending'
|
|
SalePayment.where("sale_id=? and payment_method=? and payment_status=?", sale_id, KbzPay::KBZ_PAY, 'pending').update_all(:payment_status => 'dead')
|
|
end
|
|
|
|
self.sale_id = sale_id
|
|
self.payment_method = KbzPay::KBZ_PAY
|
|
self.payment_amount = self.payment_amount.to_i + pay_amount.to_i
|
|
if grand_total > 0
|
|
self.outstanding_amount = grand_total.to_i - pay_amount.to_i
|
|
else
|
|
self.outstanding_amount = self.outstanding_amount.to_i - pay_amount.to_i
|
|
end
|
|
self.payment_status = status
|
|
return self.save
|
|
end
|
|
|
|
def process_payment(invoice, action_by, cash_amount, payment_method,remark=nil,payment_for=false)
|
|
self.sale = invoice
|
|
self.received_amount = cash_amount
|
|
self.payment_reference = remark
|
|
# puts action_by
|
|
# puts "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
|
self.action_by = action_by
|
|
#get all payment for this invoices
|
|
if payment_for
|
|
invoice_sale_payments = SalePayment.get_sale_payment_for_credit(invoice)
|
|
amount_due = SalePayment.get_credit_amount_due_left(self.sale_id)[0] ? SalePayment.get_credit_amount_due_left(self.sale_id)[0].payment_amount.to_f : 0
|
|
else
|
|
invoice_sale_payments = invoice.sale_payments
|
|
amount_due = invoice.grand_total
|
|
end
|
|
|
|
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
|
|
membership_data = nil
|
|
#route to payment type
|
|
case payment_method
|
|
when "cash"
|
|
payment_status ,outstanding_amount ,balance_amount = cash_payment(payment_for)
|
|
when "creditnote"
|
|
if !self.sale.customer_id.nil?
|
|
payment_status = creditnote_payment(self.customer_id)
|
|
end
|
|
when "visa"
|
|
payment_status = external_terminal_card_payment(:visa, payment_for)
|
|
when "master"
|
|
payment_status = external_terminal_card_payment(:master, payment_for)
|
|
when "jcb"
|
|
payment_status = external_terminal_card_payment(:jcb, payment_for)
|
|
when "mpu"
|
|
payment_status = external_terminal_card_payment(:mpu, payment_for)
|
|
when "unionpay"
|
|
payment_status = external_terminal_card_payment(:unionpay, payment_for)
|
|
when "alipay"
|
|
payment_status = external_terminal_card_payment(:alipay, payment_for)
|
|
when "vochure"
|
|
payment_status = vochure_payment
|
|
when "giftcard"
|
|
payment_status = giftcard_payment
|
|
when "paypar"
|
|
payment_status = paypar_payment
|
|
when "foc"
|
|
payment_status = foc_payment
|
|
when "paymal"
|
|
payment_status,membership_data = paymal_payment
|
|
when "JunctionPay"
|
|
payment_status = junction_pay_payment
|
|
when "dinga"
|
|
payment_status,membership_data = dinga_payment
|
|
when "GiftVoucher"
|
|
payment_status = giftvoucher_payment
|
|
when KbzPay::KBZ_PAY
|
|
payment_status = kbz_payment
|
|
else
|
|
puts "it was something else"
|
|
end
|
|
|
|
if payment_status
|
|
#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}"
|
|
if payment_for
|
|
shift = ShiftSale.current_open_shift(self.sale.cashier_id)
|
|
if !shift.nil?
|
|
shift_sale_id = shift.id
|
|
else
|
|
shift = ShiftSale.current_shift
|
|
shift_sale_id = shift.id
|
|
end
|
|
|
|
remark = "#{self.sale_payment_id}||#{shift_sale_id} -> #{remark}"
|
|
|
|
sale_audit = SaleAudit.record_payment(invoice.id, remark, action_by.name)
|
|
if payment_method == "cash"
|
|
update_shift_for_credit_payment
|
|
end
|
|
else
|
|
sale_audit = SaleAudit.record_payment(invoice.id, remark, action_by.name)
|
|
end
|
|
|
|
# update complete order items in oqs
|
|
booking = Booking.find_by_sale_id(sale_id)
|
|
booking.booking_orders.each do |sodr|
|
|
assigned =AssignedOrderItem.where("order_id = '#{ sodr.order_id }'").pluck(:assigned_order_item_id)
|
|
AssignedOrderItem.where('assigned_order_item_id IN (?)', assigned).update_all(delivery_status: true)
|
|
# AssignedOrderItem.where("order_id = '#{ sodr.order_id }'").find_each do |aoi|
|
|
# aoi.delivery_status = 1
|
|
# aoi.save
|
|
# end
|
|
end
|
|
|
|
return true, self.save,membership_data, outstanding_amount ,balance_amount
|
|
else
|
|
#record an payment in sale-audit
|
|
remark = "Payment failed - Grand Total [#{invoice.grand_total}] | Due [#{amount_due}] | Paid [#{invoice.amount_received}]"
|
|
sale_audit = SaleAudit.record_payment(invoice.id, remark,action_by.name)
|
|
return false, "Payment failed"
|
|
end
|
|
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.name)
|
|
|
|
return false, "No outstanding Amount"
|
|
end
|
|
|
|
end
|
|
|
|
def kbz_edit_sale_payment(amt, action_by)
|
|
self.action_by = action_by
|
|
sale_update_payment_status(amt)
|
|
end
|
|
|
|
def self.get_paypar_account(url,token,membership_id,campaign_type_id,merchant_uid,auth_token)
|
|
# 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,
|
|
:headers => {
|
|
'Content-Type' => 'application/json',
|
|
'Accept' => 'application/json; version=3'
|
|
}, :timeout => 10
|
|
)
|
|
rescue Net::OpenTimeout
|
|
response = { status: false }
|
|
|
|
rescue OpenURI::HTTPError
|
|
response = { status: false}
|
|
|
|
rescue SocketError
|
|
response = { status: false}
|
|
end
|
|
Rails.logger.debug "Get Paypar Account "
|
|
Rails.logger.debug response.to_json
|
|
return response
|
|
end
|
|
|
|
def self.get_paypar_account_data(url,token,merchant_uid,auth_token,account_no,amount,receipt_no)
|
|
# Control for Paypar Cloud
|
|
begin
|
|
response = HTTParty.get(url,
|
|
:body => { merchant_uid:merchant_uid,auth_token:auth_token,receipt_no: receipt_no,
|
|
account_no: account_no, amount: amount}.to_json,
|
|
:headers => {
|
|
'Content-Type' => 'application/json',
|
|
'Accept' => 'application/json; version=3'
|
|
}, :timeout => 10
|
|
)
|
|
rescue Net::OpenTimeout
|
|
response = { status: false }
|
|
|
|
rescue OpenURI::HTTPError
|
|
response = { status: false}
|
|
|
|
rescue SocketError
|
|
response = { status: false}
|
|
end
|
|
Rails.logger.debug "Get Paypar Account "
|
|
Rails.logger.debug response.to_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");
|
|
membership_actions_data = PaymentMethodSetting.find_by_payment_method("Redeem")
|
|
puts "This is membership_actions_data"
|
|
puts membership_actions_data.to_json
|
|
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 = JSON.parse(membership_actions_data.additional_parameters)["campaign_type_id"]
|
|
sale_data = Sale.find_by_sale_id(sale_id)
|
|
|
|
if sale_data
|
|
others = 0
|
|
sale_data.sale_payments.each do |sale_payment|
|
|
others = others + sale_payment.payment_amount
|
|
end
|
|
redeem_prices = sale_data.grand_total - others
|
|
# Control for Paypar Cloud
|
|
begin
|
|
response = HTTParty.post(url,
|
|
:body => { membership_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; version=3'
|
|
},
|
|
:timeout => 10
|
|
)
|
|
rescue Net::OpenTimeout
|
|
response = false
|
|
|
|
rescue OpenURI::HTTPError
|
|
response = { status: false}
|
|
|
|
rescue SocketError
|
|
response = { status: false}
|
|
end
|
|
else
|
|
response = false;
|
|
end
|
|
|
|
else
|
|
response =false;
|
|
end
|
|
|
|
Rails.logger.debug "Redeem response"
|
|
Rails.logger.debug response.to_json
|
|
|
|
return response;
|
|
|
|
end
|
|
|
|
def self.create_payment(paypar_url,payment_type,membership_id,received_amount,sale_id)
|
|
# membership_actions_data = MembershipAction.find_by_membership_type("create_payment");
|
|
membership_actions_data = PaymentMethodSetting.find_by_payment_method(payment_type)
|
|
sale_data = Sale.find_by_sale_id(sale_id)
|
|
customer_data = Customer.find_by_customer_id(sale_data.customer_id)
|
|
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
|
|
|
|
if sale_data
|
|
others = 0
|
|
sale_data.sale_payments.each do |sale_payment|
|
|
others = others + sale_payment.payment_amount
|
|
end
|
|
payment_prices = sale_data.grand_total - others
|
|
|
|
if membership_id.to_i <= 0
|
|
membership_id = customer_data.membership_id
|
|
params = { membership_id:membership_id,
|
|
amount:received_amount,
|
|
receipt_no:sale_data.receipt_no,
|
|
merchant_uid:merchant_uid,
|
|
auth_token:auth_token}.to_json
|
|
else
|
|
params = { account_no:membership_id,
|
|
amount:received_amount,
|
|
receipt_no:sale_data.receipt_no,
|
|
merchant_uid:merchant_uid,
|
|
auth_token:auth_token}.to_json
|
|
end
|
|
|
|
puts params
|
|
# Control for Paypar Cloud
|
|
begin
|
|
response = HTTParty.post(url,
|
|
:body => params,
|
|
:headers => {
|
|
'Content-Type' => 'application/json',
|
|
'Accept' => 'application/json; version=3'
|
|
},
|
|
:timeout => 10
|
|
)
|
|
rescue Net::OpenTimeout
|
|
response = false
|
|
|
|
rescue OpenURI::HTTPError
|
|
response = { status: false}
|
|
|
|
rescue SocketError
|
|
response = { status: false}
|
|
end
|
|
else
|
|
response = false;
|
|
end
|
|
|
|
else
|
|
response =false;
|
|
end
|
|
|
|
Rails.logger.debug "Create Payment response"
|
|
Rails.logger.debug response.to_json
|
|
|
|
return response;
|
|
|
|
end
|
|
|
|
# Check for Card Payment
|
|
def self.get_sale_payments_by_card(sale_payments)
|
|
# Check for Card Payment
|
|
sale_payments.each do |sp|
|
|
if sp.payment_method == "jcb" || sp.payment_method == "mpu" || sp.payment_method == "visa" || sp.payment_method == "master" || sp.payment_method == "unionpay" || sp.payment_method == "alipay"
|
|
return true;
|
|
end
|
|
end
|
|
end
|
|
|
|
private
|
|
def cash_payment(payment_for=false)
|
|
status = false
|
|
sale_payments_data = SalePayment.find_by_sale_id(self.sale_id)
|
|
if sale_payments_data.nil?
|
|
status = true
|
|
end
|
|
|
|
payment_status = false
|
|
|
|
self.payment_method = "cash"
|
|
self.payment_amount = self.received_amount
|
|
if !payment_for
|
|
self.outstanding_amount = self.sale.grand_total.to_f - self.received_amount.to_f
|
|
else
|
|
credit_sale_payment = SalePayment.get_credit_total_left(self.sale_id)[0] ? SalePayment.get_credit_total_left(self.sale_id)[0].payment_amount.to_f : 0 ###need to calculate creditnote total in here
|
|
self.outstanding_amount = credit_sale_payment - self.received_amount.to_f
|
|
end
|
|
|
|
self.payment_status = "paid"
|
|
payment_status = self.save!
|
|
if !payment_for
|
|
sale_update_payment_status(self.received_amount,status)
|
|
end
|
|
balance_amount =0.0
|
|
outstanding_amount =0.0
|
|
if self.sale.grand_total.to_f > self.received_amount.to_f
|
|
balance_amount = self.sale.grand_total.to_f - self.received_amount.to_f
|
|
outstanding_amount = 0.0
|
|
else
|
|
balance_amount = 0.0
|
|
outstanding_amount = self.received_amount.to_f - self.sale.grand_total.to_f
|
|
end
|
|
return payment_status , outstanding_amount.to_i, balance_amount.to_i
|
|
end
|
|
|
|
def foc_payment
|
|
payment_status = false
|
|
|
|
# add to sale item with foc
|
|
sale_items = SaleItem.where("sale_id='#{ self.sale.sale_id }'")
|
|
|
|
sale_items.each do|item|
|
|
SaleItem.update_existing_item(item.qty, item, self.sale.sale_id, "foc", item.unit_price, item.price)
|
|
end
|
|
sale = Sale.find(self.sale.sale_id)
|
|
sale.compute_by_sale_items(sale.total_discount,'','','',"foc")
|
|
|
|
self.payment_method = "foc"
|
|
self.payment_amount = self.received_amount
|
|
# self.outstanding_amount = self.sale.grand_total.to_f - self.received_amount.to_f
|
|
self.outstanding_amount = 0.00
|
|
self.payment_status = "paid"
|
|
payment_status = self.save!
|
|
# sale_update_payment_status(self.received_amount)
|
|
sale_update_payment_status(0)
|
|
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.to_f
|
|
self.payment_status = "outstanding"
|
|
payment_status = self.save!
|
|
|
|
sale_update_payment_status(self.received_amount)
|
|
return payment_status
|
|
end
|
|
|
|
def external_terminal_card_payment(method, payment_for=false)
|
|
payment_status = false
|
|
self.payment_method = method
|
|
self.payment_amount = self.received_amount
|
|
# self.payment_reference = self.card_payment_reference
|
|
if !payment_for
|
|
self.outstanding_amount = self.sale.grand_total.to_f - self.received_amount.to_f
|
|
else
|
|
credit_payment = SalePayment.get_credit_total_left(self.sale_id) ###need to calculate creditnote total in here
|
|
self.outstanding_amount = credit_payment[0].payment_amount.to_f - self.received_amount.to_f
|
|
end
|
|
self.payment_status = "paid"
|
|
payment_status = self.save!
|
|
if payment_for
|
|
update_shift_for_credit_payment
|
|
else
|
|
sale_update_payment_status(self.received_amount)
|
|
end
|
|
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_status = 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_status = 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
|
|
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)
|
|
|
|
#record an payment in sale-audit
|
|
remark = "#{membership_data} Redeem- for Customer #{self.sale.customer_id} Sale Id [#{self.sale.sale_id}]| pay amount -> #{self.received_amount} "
|
|
sale_audit = SaleAudit.record_paymal(self.sale.sale_id, remark, 1)
|
|
|
|
if membership_data["status"]==true
|
|
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 = "paid"
|
|
payment_status = self.save!
|
|
# 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 paymal_payment
|
|
payment_status = false
|
|
|
|
#Next time - validate if the vochure number is valid - within
|
|
# customer_data = Customer.find_by_customer_id(self.sale.customer_id)
|
|
account_no = self.payment_reference
|
|
# if account_no.to_i <= 0
|
|
# account_no = customer_data.membership_id
|
|
# end
|
|
membership_setting = MembershipSetting.find_by_membership_type("paypar_url")
|
|
membership_data = SalePayment.create_payment(membership_setting.gateway_url,"PAYMAL",account_no,self.received_amount,self.sale.sale_id)
|
|
|
|
#record an payment in sale-audit
|
|
remark = "#{membership_data} PayMal Payment- for Customer #{self.sale.customer_id} Sale Id [#{self.sale.sale_id}]| pay amount -> #{self.received_amount} "
|
|
sale_audit = SaleAudit.record_paymal(self.sale.sale_id, remark, 1)
|
|
|
|
if membership_data["status"]==true
|
|
self.payment_method = "paymal"
|
|
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 = "paid"
|
|
payment_status = self.save!
|
|
# 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,membership_data
|
|
end
|
|
|
|
def junction_pay_payment
|
|
payment_status = false
|
|
|
|
#Next time - validate if the vochure number is valid - within
|
|
self.payment_method = "JunctionPay"
|
|
self.payment_amount = self.received_amount
|
|
# self.payment_reference = self.payment_reference
|
|
self.outstanding_amount = self.sale.grand_total- self.received_amount
|
|
self.payment_status = "paid"
|
|
payment_status = self.save!
|
|
sale_update_payment_status(self.received_amount)
|
|
return payment_status
|
|
end
|
|
|
|
def dinga_payment
|
|
payment_status = false
|
|
|
|
#Next time - validate if the vochure number is valid - within
|
|
|
|
# customer_data = Customer.find_by_customer_id(self.sale.customer_id)
|
|
|
|
account_no = self.payment_reference
|
|
# if account_no == 0
|
|
# account_no = customer_data.customer_id
|
|
# end
|
|
|
|
membership_setting = MembershipSetting.find_by_membership_type("paypar_url")
|
|
membership_data = SalePayment.create_payment(membership_setting.gateway_url,"DINGA",account_no,self.received_amount,self.sale.sale_id)
|
|
|
|
#record an payment in sale-audit
|
|
remark = "#{membership_data} Dinga Payment- for Customer #{self.sale.customer_id} Sale Id [#{self.sale.sale_id}]| pay amount -> #{self.received_amount} "
|
|
sale_audit = SaleAudit.record_paymal(self.sale.sale_id, remark, 1)
|
|
|
|
if membership_data["status"]==true
|
|
self.payment_method = "dinga"
|
|
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 = "paid"
|
|
payment_status = self.save!
|
|
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,membership_data
|
|
|
|
end
|
|
|
|
def giftvoucher_payment
|
|
payment_status = false
|
|
self.payment_method = "giftvoucher"
|
|
self.payment_amount = self.received_amount
|
|
self.payment_reference = self.payment_reference
|
|
self.outstanding_amount = self.sale.grand_total.to_f - self.received_amount.to_f
|
|
self.payment_status = "paid"
|
|
payment_status = self.save!
|
|
sale_update_payment_status(self.received_amount)
|
|
return payment_status
|
|
end
|
|
|
|
def kbz_payment
|
|
payment_status = false
|
|
self.payment_amount = self.received_amount
|
|
self.payment_reference = self.payment_reference
|
|
self.outstanding_amount = self.sale.grand_total.to_f - self.received_amount.to_f
|
|
self.payment_status = "paid"
|
|
payment_status = self.save!
|
|
# sale_update_payment_status(self.received_amount)
|
|
return payment_status
|
|
end
|
|
|
|
def sale_update_payment_status(paid_amount,check_foc = false)
|
|
#update amount_outstanding
|
|
self.sale.amount_received = self.sale.amount_received.to_f + paid_amount.to_f
|
|
self.sale.save!
|
|
self.sale.amount_changed = self.sale.amount_received.to_f - self.sale.grand_total.to_f
|
|
all_received_amount = 0.0
|
|
sObj = Sale.find(self.sale_id)
|
|
is_credit = 0
|
|
is_foc = 0
|
|
is_kbz_pay = 0
|
|
method_status = false
|
|
sObj.sale_payments.each do |spay|
|
|
all_received_amount += spay.payment_amount.to_f
|
|
if spay.payment_method == "creditnote"
|
|
is_credit = 1
|
|
end
|
|
if spay.payment_method == "foc"
|
|
is_foc = 1
|
|
end
|
|
if spay.payment_method == KbzPay::KBZ_PAY
|
|
is_kbz_pay = 1
|
|
end
|
|
if spay.payment_method == "cash" || spay.payment_method == "foc" || spay.payment_method == "creditnote" || spay.payment_method == KbzPay::KBZ_PAY
|
|
method_status = true
|
|
end
|
|
end
|
|
if (self.sale.grand_total <= all_received_amount) && method_status
|
|
if is_credit == 0
|
|
self.sale.payment_status = "paid"
|
|
else
|
|
self.sale.payment_status = "outstanding"
|
|
end
|
|
|
|
if is_foc == 0
|
|
self.sale.payment_status = "paid"
|
|
else
|
|
self.sale.payment_status = "foc"
|
|
end
|
|
|
|
if is_kbz_pay == 1
|
|
self.sale.payment_status = 'paid'
|
|
end
|
|
|
|
self.sale.sale_status = "completed"
|
|
|
|
if MembershipSetting.find_by_rebate(1) && is_foc == 0 && is_credit == 0
|
|
response = rebat(sObj)
|
|
|
|
#record an payment in sale-audit
|
|
remark = "#{response} Rebate- for Customer #{self.sale.customer_id} | Sale Id [#{self.sale.sale_id}]| pay amount -> #{self.received_amount} "
|
|
sale_audit = SaleAudit.record_paymal(sObj.sale_id, remark, 1)
|
|
|
|
if !response.nil?
|
|
if response["status"] == true
|
|
self.sale.rebate_status = 'true'
|
|
end
|
|
if response["status"] == false
|
|
self.sale.rebate_status = 'false'
|
|
end
|
|
|
|
if response[:status] == false
|
|
self.sale.rebate_status = 'false'
|
|
end
|
|
if response[:status] == "no_member"
|
|
self.sale.rebate_status = nil
|
|
end
|
|
end
|
|
end
|
|
|
|
self.sale.save!
|
|
|
|
if check_foc
|
|
table_update_status(sObj)
|
|
update_shift
|
|
elsif paid_amount.to_f > 0 #|| paid_amount != "0.0"
|
|
table_update_status(sObj)
|
|
update_shift
|
|
elsif method_status && paid_amount.to_f == 0 && is_credit == 0
|
|
table_update_status(sObj)
|
|
update_shift
|
|
end
|
|
end
|
|
end
|
|
|
|
# update for cashier shift
|
|
def update_shift
|
|
|
|
shift = ShiftSale.current_open_shift(self.action_by.id)
|
|
|
|
if shift.nil?
|
|
shift = ShiftSale.current_open_shift(self.sale.cashier_id)
|
|
end
|
|
|
|
if !shift.nil?
|
|
shift.update(self.sale)
|
|
self.sale.shift_sale_id = shift.id
|
|
self.sale.cashier_id = shift.employee_id
|
|
self.sale.cashier_name = Employee.find(shift.employee_id).name
|
|
self.sale.save
|
|
else
|
|
shift = ShiftSale.current_shift
|
|
shift.update(self.sale)
|
|
self.sale.shift_sale_id = shift.id
|
|
self.sale.cashier_id = shift.employee_id
|
|
self.sale.cashier_name = Employee.find(shift.employee_id).name
|
|
self.sale.save
|
|
end
|
|
end
|
|
|
|
# update for shift with credit payment
|
|
def update_shift_for_credit_payment
|
|
shift = ShiftSale.find_by_id(self.sale.shift_sale_id)
|
|
if !shift.nil?
|
|
credit_payment_left = get_credit_payment_left[0].payment_amount.to_f
|
|
if self.payment_method == "cash"
|
|
if credit_payment_left == 0 || credit_payment_left >= self.received_amount.to_f
|
|
shift.cash_sales = shift.cash_sales.to_f + self.received_amount.to_f
|
|
else
|
|
# extra_changed_amount = self.received_amount.to_f + credit_payment_left
|
|
shift.cash_sales = shift.cash_sales.to_f + (self.received_amount.to_f + credit_payment_left)
|
|
|
|
self.sale.amount_received = self.sale.amount_received.to_f - credit_payment_left
|
|
self.sale.amount_changed = self.sale.amount_changed.to_f - credit_payment_left
|
|
self.sale.save!
|
|
end
|
|
else
|
|
shift.other_sales = shift.other_sales.to_f + self.received_amount.to_f
|
|
end
|
|
if credit_payment_left == 0 || credit_payment_left >= self.received_amount.to_f
|
|
shift.credit_sales = shift.credit_sales.to_f - self.received_amount.to_f
|
|
else
|
|
shift.credit_sales = shift.credit_sales.to_f - (self.received_amount.to_f + credit_payment_left)
|
|
end
|
|
shift.save
|
|
end
|
|
end
|
|
|
|
def table_update_status(sale_obj)
|
|
status = true
|
|
sale_count = 0
|
|
if booking = sale_obj.bookings[0]
|
|
if booking.dining_facility_id.to_i > 0
|
|
table = booking.dining_facility
|
|
if Booking.left_joins(:sale).where(dining_facility_id: booking.dining_facility_id).where.not(booking_status: 'moved').where("sales.sale_status NOT IN ('completed', 'void', 'spoile', 'waste') OR sales.sale_status IS NULL").exists?
|
|
status = false
|
|
end
|
|
|
|
if status
|
|
table.update_attributes(status: "available")
|
|
# table.status = "available"
|
|
# table.save
|
|
end
|
|
|
|
# type = 'payment'
|
|
#Send to background job for processing
|
|
# OrderBroadcastJob.perform_later(table,type)
|
|
#if ENV["SERVER_MODE"] != 'cloud'
|
|
# if ENV["SERVER_MODE"] == 'cloud'
|
|
# from = request.subdomain + "." + request.domain
|
|
# else
|
|
# from = ""
|
|
# end
|
|
# ActionCable.server.broadcast "order_channel",table: table,type:type,from:from
|
|
#end
|
|
end
|
|
end
|
|
end
|
|
|
|
def rebat(sObj)
|
|
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
|
|
payparcost = 0
|
|
credit = 0
|
|
|
|
paypar.each do |pp|
|
|
if pp.payment_method == "paypar"
|
|
payparcost = payparcost + pp.payment_amount
|
|
end
|
|
if pp.payment_method == "creditnote"
|
|
credit = 1
|
|
end
|
|
end
|
|
# overall_dis = SaleItem.get_overall_discount(sObj.id)
|
|
overall_dis = sObj.total_discount
|
|
|
|
if credit != 1
|
|
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.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; version=3'
|
|
}, :timeout => 10)
|
|
rescue Net::OpenTimeout
|
|
response = { "status": false , "message": " Connection timeout" }
|
|
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 = []
|
|
Rails.logger.debug "-------------Get Member Campaign--------------"
|
|
Rails.logger.debug response.to_json
|
|
# Check for present response fields
|
|
if response["membership_campaign_data"].present?
|
|
response["membership_campaign_data"].each do |a|
|
|
campaign_method.each do |cm|
|
|
if cm[:type].downcase.strip == a["rules_type"].downcase.strip
|
|
if cm[:amount] > 0
|
|
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
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
Rails.logger.debug "--------------Get Member Campaign Data----------"
|
|
Rails.logger.debug type_arr.to_json
|
|
|
|
rebate_arr =[]
|
|
|
|
campaign_method.each do |a|
|
|
data = {:type => a[:type], :amount => a[:amount]}
|
|
type_arr.each do |si|
|
|
if (si[:type] == a[:type]) && (a[:amount] > 0)
|
|
if credit == 1
|
|
data[:amount] = 0
|
|
else
|
|
amount = (redeem_amount / total_percentage).to_f * si[:percentage]
|
|
actual = a[:amount].to_f - amount.to_f
|
|
data[:amount] = actual
|
|
end
|
|
|
|
end
|
|
end
|
|
rebate_arr.push(data)
|
|
end
|
|
Rails.logger.debug "---------------Rebate Response----------------"
|
|
Rails.logger.debug rebate_arr.to_json
|
|
|
|
total_amount = rebate_prices - payparcost - overall_dis
|
|
Rails.logger.debug total_amount
|
|
|
|
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; version=3'
|
|
}, :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
|
|
Rails.logger.debug "Rebage Response"
|
|
Rails.logger.debug response.to_json
|
|
return response
|
|
end
|
|
end
|
|
else
|
|
response = { "status": "no_member", "message": "Not membership"}
|
|
end
|
|
|
|
end
|
|
|
|
#credit payment query
|
|
def self.get_credit_sales(params)
|
|
receipt_no = ""
|
|
customer = ""
|
|
if !params["receipt_no"].blank?
|
|
receipt_no = " and s.receipt_no LIKE '%#{params["receipt_no"]}%'"
|
|
end
|
|
|
|
if !params["customer_id"].blank?
|
|
customer = " and s.customer_id = '#{params["customer_id"]}'"
|
|
end
|
|
|
|
order_source_query = "(select orders.source FROM orders JOIN sale_orders so ON so.order_id=orders.order_id WHERE so.sale_id=s.sale_id GROUP BY so.sale_id)"
|
|
|
|
query = SalePayment.select("s.receipt_no, sale_payments.sale_payment_id,
|
|
sale_payments.payment_method,
|
|
SUM(sale_payments.payment_amount) as payment_amount,
|
|
s.receipt_date as sale_date,
|
|
s.sale_id,
|
|
s.cashier_name as cashier_name, c.name as customer_name")
|
|
.joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id")
|
|
.joins("INNER JOIN customers c ON c.customer_id = s.customer_id")
|
|
|
|
if params[:type].nil?
|
|
query = query.where("(CASE WHEN (s.grand_total + s.amount_changed)=(select SUM(payment_amount) FROM sale_payments WHERE sale_id=s.sale_id AND payment_method!='creditnote') THEN NULL ELSE payment_method='creditnote' END) and s.sale_status = 'completed' and s.payment_status='paid' #{receipt_no} #{customer}")
|
|
elsif params[:type] == "cashier"
|
|
query = query.where("(CASE WHEN (s.grand_total + s.amount_changed)=(select SUM(payment_amount) FROM sale_payments WHERE sale_id=s.sale_id AND payment_method!='creditnote') THEN NULL ELSE payment_method='creditnote' AND #{order_source_query}='#{params[:type]}' OR #{order_source_query}='emenu' END) and s.sale_status = 'completed' and s.payment_status='paid' #{receipt_no} #{customer}")
|
|
else
|
|
query = query.where("(CASE WHEN (s.grand_total + s.amount_changed)=(select SUM(payment_amount) FROM sale_payments WHERE sale_id=s.sale_id AND payment_method!='creditnote') THEN NULL ELSE payment_method='creditnote' AND #{order_source_query}='#{params[:type]}' END) and s.sale_status = 'completed' and s.payment_status='paid' #{receipt_no} #{customer}")
|
|
end
|
|
query = query.group("s.receipt_no")
|
|
.order("s.receipt_date ASC, s.receipt_no ASC")
|
|
return query
|
|
end
|
|
|
|
def self.get_credit_amount_due_left(sale_id)
|
|
query = SalePayment.select("(SUM(sale_payments.payment_amount) -
|
|
(CASE WHEN SUBSTRING_INDEX(sa.remark,'||',1)=sale_payments.sale_payment_id
|
|
THEN SUM(sale_payments.payment_amount) ELSE 0 END)) as payment_amount")
|
|
.joins(" LEFT JOIN sale_audits sa on SUBSTRING_INDEX(sa.remark,'||',1)=sale_payments.sale_payment_id")
|
|
.where("sale_payments.payment_method = 'creditnote' AND sale_payments.sale_id = '#{sale_id}'")
|
|
return query
|
|
end
|
|
|
|
def self.get_credit_total_left(sale_id)
|
|
query = SalePayment.select("(SUM(sale_payments.payment_amount) -
|
|
(SELECT (CASE WHEN SUM(sale_payments.payment_amount) > 0 THEN SUM(sale_payments.payment_amount) ELSE 0 END) AS payment_amount
|
|
FROM sale_payments
|
|
INNER JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id
|
|
WHERE sale_payments.sale_id = '#{sale_id}')) as payment_amount")
|
|
.where("sale_payments.payment_method = 'creditnote' AND sale_payments.sale_id = '#{sale_id}'")
|
|
return query
|
|
end
|
|
|
|
def self.get_sale_payment_for_credit(sale_data)
|
|
query = sale_data.sale_payments
|
|
.joins(" JOIN sale_audits sa on SUBSTRING_INDEX(sa.remark,'||',1)=sale_payments.sale_payment_id")
|
|
.where("sa.action='SALEPAYMENT' AND sa.remark IS NOT NULL
|
|
AND DATE_FORMAT(sale_payments.created_at,'%Y-%m-%d') = '#{DateTime.now.strftime('%Y-%m-%d')}' OR DATE_FORMAT(sale_payments.created_at,'%Y-%m-%d') = '#{Date.today.prev_day}'
|
|
") #AND sale_payments.payment_method!='cash'
|
|
.group("sale_payments.sale_payment_id")
|
|
return query
|
|
end
|
|
|
|
def get_credit_payment_left
|
|
sql = "SELECT SUM(payment_amount)
|
|
from sale_payments
|
|
join sale_audits on SUBSTRING_INDEX(remark,'||',1)=sale_payment_id
|
|
where sale_payments.sale_id = '#{self.sale_id}'"
|
|
|
|
query = SalePayment.select("(SUM(payment_amount) - (#{sql})) as payment_amount")
|
|
.where("sale_payments.payment_method = 'creditnote' AND sale_payments.sale_id = '#{self.sale_id}'")
|
|
return query
|
|
end
|
|
|
|
def self.get_sale_payments(sale_data)
|
|
sql = "SELECT SUM(payment_amount)
|
|
FROM sale_payments where payment_method='creditnote'
|
|
and sale_id='#{sale_data.sale_id}'"
|
|
sql1 = "SELECT CASE WHEN s.amount_changed > 0 and (s.amount_received - s.amount_changed) = s.grand_total THEN ( SELECT SUM(payment_amount)
|
|
FROM sale_payments where payment_method='creditnote'
|
|
and sale_id='#{sale_data.sale_id}'"
|
|
|
|
query = sale_data.sale_payments
|
|
.where("CASE WHEN ((#{sql}) - (#{sql1})
|
|
ELSE SUM(payment_amount) END
|
|
FROM sale_payments
|
|
JOIN sales s ON s.sale_id=sale_payments.sale_id
|
|
JOIN sale_audits sa
|
|
ON SUBSTRING_INDEX(sa.remark,'||',1)=sale_payment_id
|
|
where sa.sale_id='#{sale_data.sale_id}')) = 0
|
|
THEN payment_method!='creditnote' ELSE 1 END")
|
|
.group("sale_payments.sale_payment_id")
|
|
return query
|
|
end
|
|
|
|
private
|
|
def generate_custom_id
|
|
if self.sale_payment_id.nil?
|
|
self.sale_payment_id = SeedGenerator.generate_id(self.class.name, "SPI")
|
|
end
|
|
end
|
|
end
|