change query for credit notes payment

This commit is contained in:
phyusin
2018-11-14 17:47:31 +06:30
parent fe9b1ace90
commit 26d44c0a7d
4 changed files with 18 additions and 20 deletions

View File

@@ -874,6 +874,8 @@ class SalePayment < ApplicationRecord
customer = " and s.customer_id = '#{params["customer_id"]}'"
end
order_source_query = "(select orders.source FROM orders JOIN sale_orders so ON so.order_id=orders.order_id WHERE so.sale_id=s.sale_id GROUP BY so.sale_id)"
query = SalePayment.select("s.receipt_no, sale_payments.sale_payment_id,
sale_payments.payment_method,
SUM(sale_payments.payment_amount) as payment_amount,
@@ -882,15 +884,13 @@ 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")
.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")
if params[:type].nil?
query = query.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 s.payment_status='paid' #{receipt_no} #{customer}")
elsif params[:type] == "cashier"
query = query.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' AND o.source='#{params[:type]}' OR o.source='emenu' END) and s.sale_status = 'completed' and s.payment_status='paid' #{receipt_no} #{customer}")
query = query.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' AND #{order_source_query}='#{params[:type]}' OR #{order_source_query}='emenu' END) and s.sale_status = 'completed' and s.payment_status='paid' #{receipt_no} #{customer}")
else
query = query.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' AND o.source='#{params[:type]}' END) and s.sale_status = 'completed' and s.payment_status='paid' #{receipt_no} #{customer}")
query = query.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' AND #{order_source_query}='#{params[:type]}' END) and s.sale_status = 'completed' and s.payment_status='paid' #{receipt_no} #{customer}")
end
query = query.group("s.receipt_no")
.order("s.receipt_date ASC, s.receipt_no ASC")