fixed daily sales query for empty data

This commit is contained in:
NyanLinHtut
2020-06-24 16:14:50 +06:30
parent ce1c2371f9
commit 2835797ec5

View File

@@ -751,6 +751,7 @@ class Sale < ApplicationRecord
def self.daily_sales_list(from,to)
payment_methods = SalePayment.where.not(payment_method: ['cash', 'creditnote', 'foc']).distinct.pluck(:payment_method)
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(', ')},
@@ -785,6 +786,42 @@ class Sale < ApplicationRecord
#{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
return daily_total
end