credit payment for quick service
This commit is contained in:
@@ -16,7 +16,7 @@ class SalePayment < ApplicationRecord
|
||||
#get all payment for this invoices
|
||||
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
|
||||
amount_due = SalePayment.get_credit_total_left(self.sale_id)[0] ? SalePayment.get_credit_total_left(self.sale_id)[0].payment_amount.to_f : 0
|
||||
else
|
||||
invoice_sale_payments = invoice.sale_payments
|
||||
amount_due = invoice.grand_total
|
||||
@@ -314,7 +314,7 @@ class SalePayment < ApplicationRecord
|
||||
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
|
||||
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"
|
||||
@@ -370,7 +370,7 @@ class SalePayment < ApplicationRecord
|
||||
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
|
||||
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"
|
||||
@@ -860,18 +860,20 @@ class SalePayment < ApplicationRecord
|
||||
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}")
|
||||
.joins("INNER JOIN sale_orders so ON so.sale_id = s.sale_id")
|
||||
.joins("INNER JOIN orders o ON o.order_id = so.order_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' and o.source='#{params[:type]}' #{receipt_no} #{customer}")
|
||||
.group("s.receipt_no")
|
||||
.order("s.receipt_date ASC, s.receipt_no ASC")
|
||||
return query
|
||||
end
|
||||
|
||||
def get_credit_total_left
|
||||
def self.get_credit_total_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 = '#{self.sale_id}'")
|
||||
.where("sale_payments.payment_method = 'creditnote' AND sale_payments.sale_id = '#{sale_id}'")
|
||||
.group("sale_payments.sale_id")
|
||||
return query
|
||||
end
|
||||
@@ -880,9 +882,8 @@ class SalePayment < ApplicationRecord
|
||||
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}'
|
||||
")
|
||||
") #AND sale_payments.payment_method!='cash'
|
||||
.group("sale_payments.sale_payment_id")
|
||||
return query
|
||||
end
|
||||
@@ -899,6 +900,25 @@ class SalePayment < ApplicationRecord
|
||||
return query
|
||||
end
|
||||
|
||||
def self.get_sale_payments(sale_data)
|
||||
query = sale_data.sale_payments
|
||||
.where("CASE WHEN ((SELECT SUM(payment_amount)
|
||||
FROM sale_payments where payment_method='creditnote'
|
||||
and sale_id='#{sale_data.sale_id}') -
|
||||
(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}')
|
||||
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
|
||||
self.sale_payment_id = SeedGenerator.generate_id(self.class.name, "SPI")
|
||||
|
||||
Reference in New Issue
Block a user