diff --git a/app/models/card_sale_tran.rb b/app/models/card_sale_tran.rb index ea76e45a..ba624d0e 100644 --- a/app/models/card_sale_tran.rb +++ b/app/models/card_sale_tran.rb @@ -67,7 +67,6 @@ class CardSaleTran < ApplicationRecord shift_closed_at As closing_date,") .order("shift_sales.id DESC") return query = query.where("shift_sales.shift_started_at >= ?" , from) - byebug end end \ No newline at end of file diff --git a/app/models/card_settle_tran.rb b/app/models/card_settle_tran.rb index 2f087ef3..dd526020 100644 --- a/app/models/card_settle_tran.rb +++ b/app/models/card_settle_tran.rb @@ -64,6 +64,5 @@ class CardSettleTran < ApplicationRecord shift_closed_at As closing_date,") .order("shift_sales.id DESC") return query = query.where("shift_sales.shift_started_at >= ?" , from) - byebug end end diff --git a/app/models/sale.rb b/app/models/sale.rb index ce7caf7c..52f7acd3 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -1421,33 +1421,32 @@ def grand_total_after_rounding end def get_cash_amount - cash = 0.0 - self.sale_payments.each do |pay| - if pay.payment_method == 'cash' - cash += pay.payment_amount - end - end - return cash - self.amount_changed + self.sale_payments.where(payment_method: 'cash', payment_status: 'paid') + .pluck(:payment_amount).reduce(:+) end def get_credit_amount - credit = 0.0 - self.sale_payments.each do |pay| - if pay.payment_method == 'creditnote' - credit += pay.payment_amount - end - end - return credit + self.sale_payments.where(payment_method: 'creditnote') + .pluck(:payment_status, :payment_amount) + .inject(0.0) { |sum, pay| + if pay[0] == 'outstanding' + sum += pay[1] + else + sum -= pay[1] + end + } end def get_other_amount - other = 0.0 - self.sale_payments.each do |pay| - if pay.payment_method != 'cash' && pay.payment_method != 'creditnote' - other += pay.payment_amount - end - end - return other + self.sale_payments.where.not(payment_method: ['cash', 'creditnote']) + .pluck(:payment_status, :payment_amount) + .inject(0.0) { |sum, pay| + if pay[0] == 'paid' + sum += pay[1] + else + sum -= pay[1] + end + } end def get_commerical_tax diff --git a/app/models/sale_payment.rb b/app/models/sale_payment.rb index 6240f634..498c32c9 100755 --- a/app/models/sale_payment.rb +++ b/app/models/sale_payment.rb @@ -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!