From 36b61a9d515944c841a1aa54605c542a0c47d707 Mon Sep 17 00:00:00 2001 From: Thein Lin Kyaw Date: Wed, 13 Nov 2019 15:43:56 +0630 Subject: [PATCH] optmize daily sales report --- app/models/sale.rb | 124 ++++++++++++++++++--------------------------- 1 file changed, 49 insertions(+), 75 deletions(-) diff --git a/app/models/sale.rb b/app/models/sale.rb index d8c73ec3..cfc573b2 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -751,84 +751,58 @@ class Sale < ApplicationRecord end def self.daily_sales_list(from,to) - sub_query = "SELECT (CASE WHEN SUM(sale_payments.payment_amount) > 0 THEN - (SUM(sale_payments.payment_amount) + SUM(sale_payments.outstanding_amount)) ELSE 0 END) - FROM sale_payments - INNER JOIN sale_audits sa ON SUBSTRING_INDEX(sa.remark,'||',1)=sale_payments.sale_payment_id - INNER JOIN sales s ON s.sale_id=sa.sale_id - WHERE s.sale_status='completed' - AND DATE_FORMAT(CONVERT_TZ(s.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') = DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d')" - payments_total = Sale.select("CAST((CONVERT_TZ(sales.receipt_date,'+00:00','+06:30')) AS DATE) as sale_date, - SUM(case when (sale_payments.payment_method='KBZPay') then sale_payments.payment_amount else 0 end) as kbzpay_amount, - SUM(case when (sale_payments.payment_method='mpu') then sale_payments.payment_amount else 0 end) as mpu_amount, - SUM(case when (sale_payments.payment_method='master') then sale_payments.payment_amount else 0 end) as master_amount, - SUM(case when (sale_payments.payment_method='visa') then sale_payments.payment_amount else 0 end) as visa_amount, - SUM(case when (sale_payments.payment_method='jcb') then sale_payments.payment_amount else 0 end) as jcb_amount, - SUM(case when (sale_payments.payment_method='paypar') then sale_payments.payment_amount else 0 end) as paypar_amount, - SUM(case when (sale_payments.payment_method='unionpay') then sale_payments.payment_amount else 0 end) as unionpay_amount, - SUM(case when (sale_payments.payment_method='alipay') then sale_payments.payment_amount else 0 end) as alipay_amount, - SUM(case when (sale_payments.payment_method='paymal') then sale_payments.payment_amount else 0 end) as paymal_amount, - SUM(case when (sale_payments.payment_method='dinga') then sale_payments.payment_amount else 0 end) as dinga_amount, - SUM(case when (sale_payments.payment_method='JunctionPay') then sale_payments.payment_amount else 0 end) as junctionpay_amount, - 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='creditnote') then sale_payments.payment_amount else 0 end)) > 0 THEN (SUM(case when (sale_payments.payment_method='creditnote') then sale_payments.payment_amount else 0 end) - (#{sub_query})) ELSE 0 END) as credit_amount, - SUM(case when (sale_payments.payment_method='giftvoucher') then sale_payments.payment_amount else 0 end) as giftvoucher_amount, - SUM(case when (sale_payments.payment_method='foc') then sale_payments.payment_amount else 0 end) as foc_amount") - .joins(" join sale_payments on sale_payments.sale_id = sales.sale_id") - .where("sale_status = ? AND sales.receipt_date between ? and ? ", 'completed', from, to) - .group("DATE_FORMAT((CONVERT_TZ(sales.receipt_date,'+00:00','+06:30')),'%Y-%m-%d')") + sales = select(" + sales.*, + SUM(case when (sale_payments.payment_method='KBZPay') then sale_payments.payment_amount else 0 end) as kbzpay_amount, + SUM(case when (sale_payments.payment_method='mpu') then sale_payments.payment_amount else 0 end) as mpu_amount, + SUM(case when (sale_payments.payment_method='master') then sale_payments.payment_amount else 0 end) as master_amount, + SUM(case when (sale_payments.payment_method='visa') then sale_payments.payment_amount else 0 end) as visa_amount, + SUM(case when (sale_payments.payment_method='jcb') then sale_payments.payment_amount else 0 end) as jcb_amount, + SUM(case when (sale_payments.payment_method='paypar') then sale_payments.payment_amount else 0 end) as paypar_amount, + SUM(case when (sale_payments.payment_method='unionpay') then sale_payments.payment_amount else 0 end) as unionpay_amount, + SUM(case when (sale_payments.payment_method='alipay') then sale_payments.payment_amount else 0 end) as alipay_amount, + SUM(case when (sale_payments.payment_method='paymal') then sale_payments.payment_amount else 0 end) as paymal_amount, + SUM(case when (sale_payments.payment_method='dinga') then sale_payments.payment_amount else 0 end) as dinga_amount, + SUM(case when (sale_payments.payment_method='JunctionPay') then sale_payments.payment_amount else 0 end) as junctionpay_amount, + 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 SUM(case when sale_payments.payment_method = 'creditnote' then sale_payments.payment_amount else 0 end) ELSE 0 END as credit_amount, + SUM(case when (sale_payments.payment_method='giftvoucher') then sale_payments.payment_amount else 0 end) as giftvoucher_amount, + SUM(case when (sale_payments.payment_method='foc') then sale_payments.payment_amount else 0 end) as foc_amount") + .joins("LEFT JOIN sale_payments on sales.sale_status != 'void' AND sale_payments.sale_id = sales.sale_id AND DATE(CONVERT_TZ(sale_payments.created_at,'+00:00','+06:30')) = DATE(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'))") + .where("(sale_status = ? OR sale_status = ?) AND sales.receipt_date between ? AND ? ", 'completed', 'void', from, to) + .group("sale_id").to_sql - daily_total = Array.new + 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 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, + CAST((CONVERT_TZ(receipt_date,'+00:00','+06:30')) AS DATE) as sale_date, + SUM(kbzpay_amount) as kbzpay_amount, + SUM(mpu_amount) as mpu_amount, + SUM(master_amount) as master_amount, + SUM(visa_amount) as visa_amount, + SUM(jcb_amount) as jcb_amount, + SUM(paypar_amount) as paypar_amount, + SUM(unionpay_amount) as unionpay_amount, + SUM(alipay_amount) as alipay_amount, + SUM(paymal_amount) as paymal_amount, + SUM(dinga_amount) as dinga_amount, + SUM(junctionpay_amount) as junctionpay_amount, + SUM(cash_amount) as cash_amount, + SUM(credit_amount) as credit_amount, + SUM(giftvoucher_amount) as giftvoucher_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) - payments_total.each do |pay| - sale_date = pay.sale_date - diff_time = payments_total.first.sale_date.beginning_of_day.utc - from - diff = diff_time % 86400 - from_date = sale_date.beginning_of_day.utc - diff - to_date = sale_date.end_of_day.utc - diff - - total_sale = Sale.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 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") - .where("(sale_status = ? OR sale_status = ?) AND receipt_date between ? and ? AND total_amount != 0", 'completed', 'void', from_date, to_date) - - total_sale.each do |sale| - grand_total = sale.grand_total - old_grand_total = sale.old_grand_total - total_discount = sale.total_discount - void_amount = sale.void_amount - total_change_amount = sale.total_change_amount - total = {:sale_date => pay.sale_date, - :mpu_amount => pay.mpu_amount, - :master_amount => pay.master_amount, - :visa_amount => pay.visa_amount, - :jcb_amount => pay.jcb_amount, - :paypar_amount => pay.paypar_amount, - :unionpay_amount => pay.unionpay_amount, - :alipay_amount => pay.alipay_amount, - :kbzpay_amount => pay.kbzpay_amount, - :paymal_amount => pay.paymal_amount, - :dinga_amount => pay.dinga_amount, - :junctionpay_amount => pay.junctionpay_amount, - :cash_amount => pay.cash_amount, - :credit_amount => pay.credit_amount, - :foc_amount => pay.foc_amount, - :giftvoucher_amount => pay.giftvoucher_amount, - :total_discount => total_discount, - :total_change_amount => total_change_amount, - :grand_total => grand_total, - :old_grand_total => old_grand_total, - :void_amount => void_amount, - :rounding_adj => sale.rounding_adj} - daily_total.push(total) - end - - end - return daily_total + return daily_total end def self.get_by_range_by_saleitems(from,to,status,report_type)