From 2835797ec5ca8465075db5d2699439c72187259f Mon Sep 17 00:00:00 2001 From: NyanLinHtut Date: Wed, 24 Jun 2020 16:14:50 +0630 Subject: [PATCH] fixed daily sales query for empty data --- app/models/sale.rb | 103 ++++++++++++++++++++++++++++++--------------- 1 file changed, 70 insertions(+), 33 deletions(-) diff --git a/app/models/sale.rb b/app/models/sale.rb index 49ef027c..5c75263c 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -751,40 +751,77 @@ class Sale < ApplicationRecord def self.daily_sales_list(from,to) payment_methods = SalePayment.where.not(payment_method: ['cash', 'creditnote', 'foc']).distinct.pluck(:payment_method) - sales = select(" - sales.*, - #{payment_methods.map { |method| "SUM(case when (sale_payments.payment_method='#{method}') then sale_payments.payment_amount else 0 end) as #{method == 'paypar' ? 'redeem' : method}"}.join(', ')}, - SUM(case when (sale_payments.payment_method='cash') then sale_payments.payment_amount else 0 end) as cash_amount, - CASE WHEN SUM(case when sale_payments.payment_method not in('creditnote') - then sale_payments.payment_amount else 0 end) < sales.grand_total - THEN sales.grand_total - SUM(case when sale_payments.payment_method not in('creditnote') - then sale_payments.payment_amount else 0 end) - ELSE 0 END as credit_amount, - SUM(case when (sale_payments.payment_method='foc') then sale_payments.payment_amount else 0 end) as foc_amount") - .along_with_sale_payments_except_void_between(from, to) - .where("(sale_status = ? OR sale_status = ?) AND sales.receipt_date between ? AND ? ", 'completed', 'void', from, to) - .group("sale_id").to_sql + unless payment_methods.empty? + sales = select(" + sales.*, + #{payment_methods.map { |method| "SUM(case when (sale_payments.payment_method='#{method}') then sale_payments.payment_amount else 0 end) as #{method == 'paypar' ? 'redeem' : method}"}.join(', ')}, + SUM(case when (sale_payments.payment_method='cash') then sale_payments.payment_amount else 0 end) as cash_amount, + CASE WHEN SUM(case when sale_payments.payment_method not in('creditnote') + then sale_payments.payment_amount else 0 end) < sales.grand_total + THEN sales.grand_total - SUM(case when sale_payments.payment_method not in('creditnote') + then sale_payments.payment_amount else 0 end) + ELSE 0 END as credit_amount, + SUM(case when (sale_payments.payment_method='foc') then sale_payments.payment_amount else 0 end) as foc_amount") + .along_with_sale_payments_except_void_between(from, to) + .where("(sale_status = ? OR sale_status = ?) AND sales.receipt_date between ? AND ? ", 'completed', 'void', from, to) + .group("sale_id").to_sql + + daily_total = connection.select_all("SELECT + IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0) as grand_total, + IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0) as total_sale, + IFNULL(SUM(case when (sale_status='completed') then old_grand_total else 0 end),0) as old_grand_total, + IFNULL(SUM(case when (sale_status='completed') then total_discount else 0 end),0) as total_discount, + IFNULL(SUM(case when (sale_status='completed') then amount_changed else 0 end),0) as total_change_amount, + IFNULL(SUM(case when (sale_status='void') then grand_total else 0 end),0) as void_amount, + IFNULL(SUM(case when (sale_status='completed') then rounding_adjustment else 0 end),0) as rounding_adj, + IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0) / 21 as tax, + (IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0))-(IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0) / 21) as net_sale, + (IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0)) + (IFNULL(SUM(case when (sale_status='completed') then total_discount else 0 end),0)) as gross_sale, + CAST((CONVERT_TZ(receipt_date,'+00:00','+06:30')) AS DATE) as sale_date, + #{payment_methods.map { |method| pm = method == 'paypar' ? 'redeem' : method; "SUM(#{pm}) as #{pm}"}.join(', ')}, + SUM(cash_amount) as cash_amount, + SUM(credit_amount) as credit_amount, + SUM(foc_amount) as foc_amount + FROM ( + #{sales} + ) as s + GROUP BY DATE(CONVERT_TZ(receipt_date,'+00:00','+06:30'))").to_hash.map(&:symbolize_keys) + else + sales = select(" + sales.*, + SUM(case when (sale_payments.payment_method='cash') then sale_payments.payment_amount else 0 end) as cash_amount, + CASE WHEN SUM(case when sale_payments.payment_method not in('creditnote') + then sale_payments.payment_amount else 0 end) < sales.grand_total + THEN sales.grand_total - SUM(case when sale_payments.payment_method not in('creditnote') + then sale_payments.payment_amount else 0 end) + ELSE 0 END as credit_amount, + SUM(case when (sale_payments.payment_method='foc') then sale_payments.payment_amount else 0 end) as foc_amount") + .along_with_sale_payments_except_void_between(from, to) + .where("(sale_status = ? OR sale_status = ?) AND sales.receipt_date between ? AND ? ", 'completed', 'void', from, to) + .group("sale_id").to_sql + + daily_total = connection.select_all("SELECT + IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0) as grand_total, + IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0) as total_sale, + IFNULL(SUM(case when (sale_status='completed') then old_grand_total else 0 end),0) as old_grand_total, + IFNULL(SUM(case when (sale_status='completed') then total_discount else 0 end),0) as total_discount, + IFNULL(SUM(case when (sale_status='completed') then amount_changed else 0 end),0) as total_change_amount, + IFNULL(SUM(case when (sale_status='void') then grand_total else 0 end),0) as void_amount, + IFNULL(SUM(case when (sale_status='completed') then rounding_adjustment else 0 end),0) as rounding_adj, + IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0) / 21 as tax, + (IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0))-(IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0) / 21) as net_sale, + (IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0)) + (IFNULL(SUM(case when (sale_status='completed') then total_discount else 0 end),0)) as gross_sale, + CAST((CONVERT_TZ(receipt_date,'+00:00','+06:30')) AS DATE) as sale_date, + SUM(cash_amount) as cash_amount, + SUM(credit_amount) as credit_amount, + SUM(foc_amount) as foc_amount + FROM ( + #{sales} + ) as s + GROUP BY DATE(CONVERT_TZ(receipt_date,'+00:00','+06:30'))").to_hash.map(&:symbolize_keys) + end + - daily_total = connection.select_all("SELECT - IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0) as grand_total, - IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0) as total_sale, - IFNULL(SUM(case when (sale_status='completed') then old_grand_total else 0 end),0) as old_grand_total, - IFNULL(SUM(case when (sale_status='completed') then total_discount else 0 end),0) as total_discount, - IFNULL(SUM(case when (sale_status='completed') then amount_changed else 0 end),0) as total_change_amount, - IFNULL(SUM(case when (sale_status='void') then grand_total else 0 end),0) as void_amount, - IFNULL(SUM(case when (sale_status='completed') then rounding_adjustment else 0 end),0) as rounding_adj, - IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0) / 21 as tax, - (IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0))-(IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0) / 21) as net_sale, - (IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0)) + (IFNULL(SUM(case when (sale_status='completed') then total_discount else 0 end),0)) as gross_sale, - CAST((CONVERT_TZ(receipt_date,'+00:00','+06:30')) AS DATE) as sale_date, - #{payment_methods.map { |method| pm = method == 'paypar' ? 'redeem' : method; "SUM(#{pm}) as #{pm}"}.join(', ')}, - SUM(cash_amount) as cash_amount, - SUM(credit_amount) as credit_amount, - SUM(foc_amount) as foc_amount - FROM ( - #{sales} - ) as s - GROUP BY DATE(CONVERT_TZ(receipt_date,'+00:00','+06:30'))").to_hash.map(&:symbolize_keys) return daily_total end