FOC fixed
This commit is contained in:
@@ -75,6 +75,8 @@ class SalePayment < ApplicationRecord
|
||||
#get all payment for this invoices
|
||||
if payment_for
|
||||
amount_due = SalePayment.get_credit_amount_due_left(self.sale_id).first.payment_amount
|
||||
elsif payment_method == 'foc'
|
||||
amount_due = invoice.total_amount
|
||||
else
|
||||
amount_due = invoice.sale_payments
|
||||
.map(&:payment_amount).reduce(invoice.grand_total, :-)
|
||||
@@ -412,6 +414,7 @@ class SalePayment < ApplicationRecord
|
||||
def foc_payment
|
||||
payment_status = false
|
||||
sale = self.sale
|
||||
sale.sale_payments.update_all(payment_status: "cancelled")
|
||||
# add to sale item with foc
|
||||
sale_items = sale.sale_items
|
||||
|
||||
@@ -428,7 +431,7 @@ class SalePayment < ApplicationRecord
|
||||
self.payment_status = "paid"
|
||||
payment_status = self.save!
|
||||
# sale_update_payment_status(self.received_amount)
|
||||
sale_update_payment_status(0)
|
||||
sale_update_payment_status(0, true)
|
||||
return payment_status
|
||||
end
|
||||
|
||||
@@ -638,21 +641,28 @@ class SalePayment < ApplicationRecord
|
||||
def sale_update_payment_status(paid_amount, check_foc = false)
|
||||
#update amount_outstanding
|
||||
sale = self.sale
|
||||
total_payment_amount = sale.sale_payments.reload.sum(&:payment_amount)
|
||||
sale_payments = sale.sale_payments.reload
|
||||
|
||||
is_credit = sale_payments.any? { |x| x.payment_method == "creditnote" }
|
||||
is_foc = sale_payments.any? { |x| x.payment_method == "foc" } || check_foc
|
||||
|
||||
if is_foc
|
||||
total_payment_amount = 0.0
|
||||
else
|
||||
total_payment_amount = sale_payments.sum(&:payment_amount)
|
||||
end
|
||||
|
||||
sale.amount_received = sale.amount_received.to_f + paid_amount.to_f
|
||||
sale.amount_changed = total_payment_amount - sale.grand_total.to_f
|
||||
|
||||
is_credit = sale.sale_payments.any? { |x| x.payment_method == "creditnote" }
|
||||
is_foc = sale.sale_payments.any? { |x| x.payment_method == "foc" }
|
||||
|
||||
if sale.grand_total <= total_payment_amount && sale.sale_status == "new"
|
||||
if is_foc
|
||||
sale.payment_status = 'foc'
|
||||
sale.sale_status = 'completed'
|
||||
elsif sale.grand_total <= total_payment_amount && sale.sale_status == "new"
|
||||
sale.payment_status = "paid"
|
||||
if is_credit
|
||||
sale.payment_status = "outstanding"
|
||||
end
|
||||
if is_foc
|
||||
sale.payment_status = "foc"
|
||||
end
|
||||
|
||||
sale.sale_status = "completed"
|
||||
|
||||
@@ -679,25 +689,24 @@ class SalePayment < ApplicationRecord
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
sale.save!
|
||||
sale.save!
|
||||
|
||||
if check_foc
|
||||
table_update_status(sale)
|
||||
update_shift
|
||||
elsif paid_amount.to_f > 0 #|| paid_amount != "0.0"
|
||||
table_update_status(sale)
|
||||
update_shift
|
||||
elsif paid_amount.to_f == 0 && !is_credit
|
||||
table_update_status(sale)
|
||||
update_shift
|
||||
end
|
||||
if check_foc
|
||||
table_update_status(sale)
|
||||
update_shift
|
||||
elsif paid_amount.to_f > 0 #|| paid_amount != "0.0"
|
||||
table_update_status(sale)
|
||||
update_shift
|
||||
elsif paid_amount.to_f == 0 && !is_credit
|
||||
table_update_status(sale)
|
||||
update_shift
|
||||
end
|
||||
end
|
||||
|
||||
# update for cashier shift
|
||||
def update_shift
|
||||
|
||||
shift = ShiftSale.current_open_shift(self.action_by.id)
|
||||
|
||||
if shift.nil?
|
||||
@@ -735,7 +744,6 @@ class SalePayment < ApplicationRecord
|
||||
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!
|
||||
|
||||
Reference in New Issue
Block a user