update sale_payment database
This commit is contained in:
@@ -1,17 +1,21 @@
|
||||
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
|
||||
attr_accessor :received_amount, :card_payment_reference, :voucher_no, :giftcard_no, :customer_id, :external_payment_status
|
||||
|
||||
def process_payment(invoice, action_by, cash_amount)
|
||||
|
||||
def process_payment(invoice, action_by)
|
||||
self.sale = invoice
|
||||
|
||||
self.received_amount = cash_amount
|
||||
|
||||
payment_method = "cash"
|
||||
amount_due = invoice.grand_total
|
||||
|
||||
#get all payment for this invoices
|
||||
invoice.sale_payments.each do |payment|
|
||||
if (payment.payment_status == "paid" )
|
||||
@@ -22,32 +26,33 @@ class SalePayment < ApplicationRecord
|
||||
if (amount_due > 0)
|
||||
payment_status = false
|
||||
#route to payment type
|
||||
switch (payment_method)
|
||||
case "cash"
|
||||
case payment_method
|
||||
when "cash"
|
||||
payment_status = cash_payment
|
||||
case "creditnote"
|
||||
payment_status = creditnote_payment
|
||||
case "visa"
|
||||
payment_status = external_terminal_card_payment(:visa)
|
||||
case "master"
|
||||
payment_status = external_terminal_card_payment(:master)
|
||||
case "jcb"
|
||||
payment_status = external_terminal_card_payment(:jcb)
|
||||
case "mpu"
|
||||
payment_status = external_terminal_card_payment(:mpu)
|
||||
case "unionpay"
|
||||
payment_status = external_terminal_card_payment(:unionpay)
|
||||
case "vochure"
|
||||
payment_status = vochure_payment
|
||||
case "giftcard"
|
||||
payment_status = giftcard_payment
|
||||
case "paypar"
|
||||
#TODO: implement paypar implementation
|
||||
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 -> #{amount} | Payment Status ->#{payment_status}"
|
||||
sale_audit = SaleAudit.record_payment(invoice.id, remark, action_by)
|
||||
@@ -66,10 +71,9 @@ class SalePayment < ApplicationRecord
|
||||
private
|
||||
def cash_payment
|
||||
payment_status = false
|
||||
|
||||
self.payment_method = "cash"
|
||||
self.payment_amount = self.received_amount
|
||||
self.outstanding_amount = self.sale.grand_total - received_amount
|
||||
self.outstanding_amount = self.sale.grand_total - self.received_amount
|
||||
self.payment_status = "paid"
|
||||
payment_method = self.save!
|
||||
|
||||
@@ -78,7 +82,8 @@ class SalePayment < ApplicationRecord
|
||||
return payment_status
|
||||
end
|
||||
|
||||
def creditnote_payment(self.customer_id)
|
||||
def creditnote_payment(customer_id)
|
||||
|
||||
payment_status = false
|
||||
|
||||
self.payment_method = "creditnote"
|
||||
@@ -147,11 +152,12 @@ class SalePayment < ApplicationRecord
|
||||
end
|
||||
|
||||
def sale_update_payment_status(paid_amount)
|
||||
|
||||
puts "paid_amount"
|
||||
puts paid_amount
|
||||
#update amount_outstanding
|
||||
self.sale.amount_received = self.sale.amount_received + paid_amount
|
||||
self.sale.amount_changed = amount - self.sale.amount_received
|
||||
if (self.sale.grand_total <= self.sale.amount_received && self.sale.amount_changed > 0)
|
||||
self.sale.amount_changed = amount - self.sale.amount_received
|
||||
if (self.sale.grand_total <= self.sale.amount_received && self.sale.amount_changed > 0)
|
||||
self.sale.payment_status = "paid"
|
||||
self.sale.sale_status = "completed"
|
||||
self.sale.save!
|
||||
|
||||
Reference in New Issue
Block a user