credit payment function
This commit is contained in:
@@ -8,14 +8,21 @@ class SalePayment < ApplicationRecord
|
||||
|
||||
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,remark=nil)
|
||||
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
|
||||
amount_due = invoice.grand_total
|
||||
|
||||
#get all payment for this invoices
|
||||
invoice.sale_payments.each do |payment|
|
||||
if payment_for
|
||||
invoice_sale_payments = SalePayment.get_sale_payment_for_credit(invoice)
|
||||
amount_due = get_credit_total_left[0] ? get_credit_total_left[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
|
||||
@@ -27,23 +34,23 @@ class SalePayment < ApplicationRecord
|
||||
#route to payment type
|
||||
case payment_method
|
||||
when "cash"
|
||||
payment_status = cash_payment
|
||||
payment_status = 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_status = external_terminal_card_payment(:visa, payment_for)
|
||||
when "master"
|
||||
payment_status = external_terminal_card_payment(:master)
|
||||
payment_status = external_terminal_card_payment(:master, payment_for)
|
||||
when "jcb"
|
||||
payment_status = external_terminal_card_payment(:jcb)
|
||||
payment_status = external_terminal_card_payment(:jcb, payment_for)
|
||||
when "mpu"
|
||||
payment_status = external_terminal_card_payment(:mpu)
|
||||
payment_status = external_terminal_card_payment(:mpu, payment_for)
|
||||
when "unionpay"
|
||||
payment_status = external_terminal_card_payment(:unionpay)
|
||||
payment_status = external_terminal_card_payment(:unionpay, payment_for)
|
||||
when "alipay"
|
||||
payment_status = external_terminal_card_payment(:alipay)
|
||||
payment_status = external_terminal_card_payment(:alipay, payment_for)
|
||||
when "vochure"
|
||||
payment_status = vochure_payment
|
||||
when "giftcard"
|
||||
@@ -65,8 +72,22 @@ class SalePayment < ApplicationRecord
|
||||
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)
|
||||
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)
|
||||
else
|
||||
sale_audit = SaleAudit.record_payment(invoice.id, remark, action_by)
|
||||
end
|
||||
|
||||
# update complete order items in oqs
|
||||
booking = Booking.find_by_sale_id(sale_id)
|
||||
@@ -82,7 +103,7 @@ class SalePayment < ApplicationRecord
|
||||
return true, self.save,membership_data
|
||||
else
|
||||
#record an payment in sale-audit
|
||||
# remark = "No outstanding Amount - Grand Total [#{invoice.grand_total}] | Due [#{amount_due}] | Paid [#{invoice.amount_received}]"
|
||||
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"
|
||||
@@ -280,21 +301,29 @@ class SalePayment < ApplicationRecord
|
||||
end
|
||||
|
||||
private
|
||||
def cash_payment
|
||||
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
|
||||
self.outstanding_amount = self.sale.grand_total.to_f - self.received_amount.to_f
|
||||
if !payment_for
|
||||
self.outstanding_amount = self.sale.grand_total.to_f - self.received_amount.to_f
|
||||
else
|
||||
credit_sale_payment = get_credit_total_left[0] ? get_credit_total_left[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_method = self.save!
|
||||
|
||||
sale_update_payment_status(self.received_amount,status)
|
||||
if payment_for
|
||||
update_shift_for_credit_payment
|
||||
else
|
||||
sale_update_payment_status(self.received_amount,status)
|
||||
end
|
||||
return payment_status
|
||||
end
|
||||
|
||||
@@ -333,15 +362,24 @@ class SalePayment < ApplicationRecord
|
||||
return payment_status
|
||||
end
|
||||
|
||||
def external_terminal_card_payment(method)
|
||||
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
|
||||
self.outstanding_amount = self.sale.grand_total.to_f - self.received_amount.to_f
|
||||
if !payment_for
|
||||
self.outstanding_amount = self.sale.grand_total.to_f - self.received_amount.to_f
|
||||
else
|
||||
credit_payment = get_credit_total_left ###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_method = self.save!
|
||||
sale_update_payment_status(self.received_amount)
|
||||
if payment_for
|
||||
update_shift_for_credit_payment
|
||||
else
|
||||
sale_update_payment_status(self.received_amount)
|
||||
end
|
||||
return payment_status
|
||||
end
|
||||
|
||||
@@ -594,6 +632,33 @@ class SalePayment < ApplicationRecord
|
||||
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
|
||||
shift.cash_sales = shift.cash_sales.to_f + credit_payment_left
|
||||
extra_changed_amount = self.received_amount.to_f - credit_payment_left
|
||||
self.sale.amount_received = self.sale.amount_received.to_f + extra_changed_amount.to_f
|
||||
self.sale.amount_changed = self.sale.amount_changed.to_f + extra_changed_amount.to_f
|
||||
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.cash_sales = shift.credit_sales.to_f - credit_payment_left
|
||||
end
|
||||
shift.save
|
||||
end
|
||||
end
|
||||
|
||||
def table_update_status(sale_obj)
|
||||
status = true
|
||||
sale_count = 0
|
||||
@@ -775,6 +840,65 @@ class SalePayment < ApplicationRecord
|
||||
|
||||
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
|
||||
|
||||
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")
|
||||
.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' #{receipt_no} #{customer}")
|
||||
.group("s.receipt_no")
|
||||
.order("s.receipt_date ASC, s.receipt_no ASC")
|
||||
return query
|
||||
end
|
||||
|
||||
def get_credit_total_left
|
||||
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 = '#{self.sale_id}'")
|
||||
.group("sale_payments.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 sale_payments.payment_method!='cash'
|
||||
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}'
|
||||
")
|
||||
.group("sale_payments.sale_payment_id")
|
||||
return query
|
||||
end
|
||||
|
||||
def get_credit_payment_left
|
||||
query = SalePayment.select("(CASE WHEN (SUM(payment_amount) - (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}')) > 0 THEN (SUM(payment_amount) - (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}')) ELSE 0 END) as payment_amount")
|
||||
.where("sale_payments.payment_method = 'creditnote' AND sale_payments.sale_id = '#{self.sale_id}'")
|
||||
return query
|
||||
end
|
||||
|
||||
private
|
||||
def generate_custom_id
|
||||
self.sale_payment_id = SeedGenerator.generate_id(self.class.name, "SPI")
|
||||
|
||||
Reference in New Issue
Block a user