From 7881ae4dfa02f331dae29edbff83bd63b3e4bc86 Mon Sep 17 00:00:00 2001 From: Aung Myo Date: Sun, 18 Jun 2017 16:38:46 +0630 Subject: [PATCH] daily sale report --- app/controllers/base_report_controller.rb | 146 ++++++++---- .../reports/daily_sale_controller.rb | 22 +- app/models/sale.rb | 53 +++++ app/models/sale_tax.rb | 4 + app/views/layouts/_header.html.erb | 2 +- .../_shift_sale_report_filter.html.erb | 212 ++++++++++++++++++ app/views/reports/daily_sale/index.html.erb | 190 ++++++++++++++++ app/views/reports/daily_sale/index.xls.erb | 116 ++++++++++ app/views/reports/daily_sale_report.html.erb | 20 ++ config/routes.rb | 1 + 10 files changed, 720 insertions(+), 46 deletions(-) create mode 100644 app/views/reports/daily_sale/_shift_sale_report_filter.html.erb create mode 100644 app/views/reports/daily_sale/index.html.erb create mode 100644 app/views/reports/daily_sale/index.xls.erb diff --git a/app/controllers/base_report_controller.rb b/app/controllers/base_report_controller.rb index c1ae1a01..0ea51cf5 100644 --- a/app/controllers/base_report_controller.rb +++ b/app/controllers/base_report_controller.rb @@ -23,55 +23,117 @@ class BaseReportController < ActionController::Base period = params[:period] from = params[:from] to = params[:to] - day_ref = Time.now - if period_type.to_i == 1 - if params[:from] && params[:to] - if params[:from] != "" && params[:to] !="" - from = DateTime.strptime(params[:from], "%m/%d/%Y") - to = DateTime.strptime(params[:to], "%m/%d/%Y") - else + day_ref = Time.now.utc.getlocal + + if params[:report_type] == "daily_sale" + + if from != "" && to != "" + + f_date = DateTime.parse(params[:from]) + t_date = DateTime.parse(params[:to]) + f_time = Time.mktime(f_date.year,f_date.month,f_date.day,f_date.hour,f_date.min,f_date.sec) + t_time = Time.mktime(t_date.year,t_date.month,t_date.day,t_date.hour,t_date.min,t_date.sec) + from = f_time.beginning_of_day.utc.getlocal + to = t_time.end_of_day.utc.getlocal + + else + + case period.to_i + when PERIOD["today"] + from = day_ref.beginning_of_day.utc to = day_ref.end_of_day.utc - end - end - else - case period.to_i - when PERIOD["today"] - from = day_ref.beginning_of_day.utc - to = day_ref.end_of_day.utc + when PERIOD["yesterday"] + from = (day_ref - 1.day).beginning_of_day.utc + to = (day_ref - 1.day).end_of_day.utc - when PERIOD["yesterday"] - from = (day_ref - 1.day).beginning_of_day.utc - to = (day_ref - 1.day).end_of_day.utc + when PERIOD["this_week"] + from = Time.now.beginning_of_week.utc + to = Time.now.utc + when PERIOD["last_week"] + from = (day_ref - 7.day).beginning_of_week.utc + to = (day_ref - 7.day).end_of_week.utc + when PERIOD["last_7"] + from = (day_ref - 7.day).utc + to = Time.now.utc + when PERIOD["this_month"] + from = Time.now.beginning_of_month.utc + to = Time.now.utc + when PERIOD["last_month"] + from = (day_ref - 1.month).beginning_of_month.utc + to = (day_ref - 1.month).end_of_month.utc + when PERIOD["last_30"] + from = (day_ref - 30.day).utc + to = Time.now.utc + when PERIOD["this_year"] + from = Time.now.beginning_of_year.utc + to = Time.now.utc + when PERIOD["last_year"] + from = (day_ref - 1.year).beginning_of_year.utc + to = (day_ref - 1.year).end_of_year.utc + end + end + else # end daily sale report + if period_type.to_i == 1 + + if params[:from] && params[:to] - when PERIOD["this_week"] - from = Time.now.beginning_of_week.utc - to = Time.now.utc - when PERIOD["last_week"] - from = (day_ref - 7.day).beginning_of_week.utc - to = (day_ref - 7.day).end_of_week.utc - when PERIOD["last_7"] - from = (day_ref - 7.day).utc - to = Time.now.utc - when PERIOD["this_month"] - from = Time.now.beginning_of_month.utc - to = Time.now.utc - when PERIOD["last_month"] - from = (day_ref - 1.month).beginning_of_month.utc - to = (day_ref - 1.month).end_of_month.utc - when PERIOD["last_30"] - from = (day_ref - 30.day).utc - to = Time.now.utc - when PERIOD["this_year"] - from = Time.now.beginning_of_year.utc - to = Time.now.utc - when PERIOD["last_year"] - from = (day_ref - 1.year).beginning_of_year.utc - to = (day_ref - 1.year).end_of_year.utc - end + if params[:from] != "" && params[:to] !="" + + f_date = DateTime.parse(params[:from]) + t_date = DateTime.parse(params[:to]) + f_time = Time.mktime(f_date.year,f_date.month,f_date.day,f_date.hour,f_date.min,f_date.sec) + t_time = Time.mktime(t_date.year,t_date.month,t_date.day,t_date.hour,t_date.min,t_date.sec) + from = f_time.beginning_of_day.utc.getlocal + to = t_time.end_of_day.utc.getlocal + else + from = day_ref.beginning_of_day.utc + to = day_ref.end_of_day.utc + end + end + else + case period.to_i + when PERIOD["today"] + + from = day_ref.beginning_of_day.utc + to = day_ref.end_of_day.utc + + when PERIOD["yesterday"] + from = (day_ref - 1.day).beginning_of_day.utc + to = (day_ref - 1.day).end_of_day.utc + + when PERIOD["this_week"] + from = Time.now.beginning_of_week.utc + to = Time.now.utc + when PERIOD["last_week"] + from = (day_ref - 7.day).beginning_of_week.utc + to = (day_ref - 7.day).end_of_week.utc + when PERIOD["last_7"] + from = (day_ref - 7.day).utc + to = Time.now.utc + when PERIOD["this_month"] + from = Time.now.beginning_of_month.utc + to = Time.now.utc + when PERIOD["last_month"] + from = (day_ref - 1.month).beginning_of_month.utc + to = (day_ref - 1.month).end_of_month.utc + when PERIOD["last_30"] + from = (day_ref - 30.day).utc + to = Time.now.utc + when PERIOD["this_year"] + from = Time.now.beginning_of_year.utc + to = Time.now.utc + when PERIOD["last_year"] + from = (day_ref - 1.year).beginning_of_year.utc + to = (day_ref - 1.year).end_of_year.utc + end + end end + return from, to + + end end diff --git a/app/controllers/reports/daily_sale_controller.rb b/app/controllers/reports/daily_sale_controller.rb index b487c269..86e60702 100644 --- a/app/controllers/reports/daily_sale_controller.rb +++ b/app/controllers/reports/daily_sale_controller.rb @@ -1,9 +1,25 @@ class Reports::DailySaleController < BaseReportController def index - from, to = get_date_range_from_params - @sale_data = Sale.get_receipt_no_list(from,to) - @sale_data = Kaminari.paginate_array(@sale_data).page(params[:page]).per(50) + from, to = get_date_range_from_params + @sale_data = Sale.daily_sales_list(from,to) + @tax = SaleTax.get_tax(from,to) + end + + # @locations = Location.all + + # branch,from, to, report_type = get_date_range_from_params + + # @location = Location.find_by_id(current_location) + # @sale_data = Sale.daily_sales_report(current_location,from,to) + # @tax = SaleT.get_tax(current_location,from,to) + + # if @sale_data.blank? && @tax.blank? && request.post? + # flash.now[:notice] = "No data available for selected filters" + # end + + def show + end end \ No newline at end of file diff --git a/app/models/sale.rb b/app/models/sale.rb index 9683c2b2..53a128ce 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -273,6 +273,59 @@ class Sale < ApplicationRecord end end + def self.daily_sales_list(from,to) + 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='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='cash') then sale_payments.payment_amount else 0 end) as cash_amount, + SUM(case when (sale_payments.payment_method='credit') then sale_payments.payment_amount 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") + .joins("join (select * from sale_payments group by sale_payments.sale_id, sale_payments.payment_method) sale_payments on sale_payments.sale_id = sales.sale_id") + .where("sale_status = ? AND sales.receipt_date between ? and ? AND total_amount != 0", 'completed', from, to) + .group("DATE_FORMAT((CONVERT_TZ(sales.receipt_date,'+00:00','+06:30')),'%Y-%m-%d')") + + daily_total = Array.new + + 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 total_discount else 0 end),0) as total_discount, + 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 + total_discount = sale.total_discount + void_amount = sale.void_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, + :cash_amount => pay.cash_amount, + :credit_amount => pay.credit_amount, + :foc_amount => pay.foc_amount, + :total_discount => total_discount, + :grand_total => grand_total, + :void_amount => void_amount, + :rounding_adj => sale.rounding_adj} + daily_total.push(total) + end + + end + return daily_total + end + private def generate_custom_id diff --git a/app/models/sale_tax.rb b/app/models/sale_tax.rb index 79de9134..7a42905a 100644 --- a/app/models/sale_tax.rb +++ b/app/models/sale_tax.rb @@ -5,6 +5,10 @@ class SaleTax < ApplicationRecord before_create :generate_custom_id belongs_to :sale + def self.get_tax(from,to) + query = SaleTax.select("sale_taxes.tax_name,SUM(sale_taxes.tax_payable_amount) as tax_amount").joins("join sales on sales.sale_id = sale_taxes.sale_id").where("sale_status = ? AND sales.receipt_date between ? and ? AND total_amount != 0", 'completed', from, to).group("sale_taxes.tax_name") + end + private def generate_custom_id self.sale_tax_id = SeedGenerator.generate_id(self.class.name, "STI") diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index 58ecd9b5..aaaa9c69 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -35,7 +35,7 @@