improve receipt/details reports and implement number formatting

1) eager load reports for receipt/details
2) introduce number_format lookups to replace print_settings for number formatting
3) implement NumberFormattable concern, reference number_format lookups or print_settings if not exist, to get number format settings and number formatting
4) replace rails NumberHelper.number_with_precision with NumberFormattable.number_format hopefully to reduce overhead, formatting numbers for huge lists of data
This commit is contained in:
Thein Lin Kyaw
2019-11-25 23:17:53 +06:30
parent a36e170d94
commit 3c1cc737b5
71 changed files with 1338 additions and 1898 deletions

View File

@@ -1,6 +1,6 @@
class ApplicationController < ActionController::Base
include LoginVerification
#before_action :check_installation
protect_from_forgery with: :exception
@@ -17,7 +17,5 @@ class ApplicationController < ActionController::Base
flash[:warning] = exception.message
redirect_to root_path
end
end

View File

@@ -5,7 +5,7 @@ class BaseOrigamiController < ActionController::Base
before_action :check_user
#before_action :check_installation
protect_from_forgery with: :exception
protect_from_forgery with: :exception
helper_method :current_token
@@ -15,16 +15,16 @@ class BaseOrigamiController < ActionController::Base
redirect_to origami_dashboard_path
end
def check_user
if check_mobile
def check_user
if check_mobile
if current_user.nil?
return render status: 401, json: {
message: "User using other device!"
message: "User using other device!"
}.to_json
end
else
else
if current_user.nil?
redirect_to root_path
redirect_to root_path
end
end
end
@@ -41,13 +41,13 @@ class BaseOrigamiController < ActionController::Base
#check webview
def check_mobile
status = false
authenticate_with_http_token do |token, options|
authenticate_with_http_token do |token, options|
if token
session[:webview] = true
session[:session_token] = token
end
end
end
if session[:webview] && request.user_agent =~ /android|blackberry|iphone|ipad|ipod|iemobile|mobile|webos/i
status = true
end

View File

@@ -1,11 +1,11 @@
class BaseReportController < ActionController::Base
include LoginVerification
layout "application"
layout "application"
before_action :check_user
before_action :check_user
#before_action :check_installation
protect_from_forgery with: :exception
#before_action :check_installation
protect_from_forgery with: :exception
rescue_from CanCan::AccessDenied do |exception|
flash[:warning] = exception.message
@@ -29,7 +29,7 @@ class BaseReportController < ActionController::Base
period_type = params[:period_type]
period = params[:period]
from = params[:from]
to = params[:to]
to = params[:to]
day_ref = Time.now.utc.getlocal
if from.present? && to.present?
@@ -39,10 +39,10 @@ class BaseReportController < ActionController::Base
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
to = t_time.end_of_day.utc.getlocal
else
else
case period.to_i
when PERIOD["today"]
@@ -77,10 +77,10 @@ class BaseReportController < ActionController::Base
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
return from, to
end
def check_user

View File

@@ -0,0 +1,57 @@
module NumberFormattable
extend ActiveSupport::Concern
def precision
@precision ||= Lookup.number_formats.find { |f| f.name.parameterize.underscore == 'precision'}
if @precision
@precision.value.to_i
else
@print_settings ||= PrintSetting.get_precision_delimiter
if @print_settings
@print_settings.precision.to_i
end
end || 2
end
def delimiter
@delimiter ||= Lookup.number_formats.find { |f| f.name.parameterize.underscore == 'delimiter'}
if @delimiter
@delimiter_value ||= @delimiter.value.to_s.gsub(/\\u(\h{4})/) { |m| [$1].pack("H*").unpack("n*").pack("U*") }
else
@print_settings ||= PrintSetting.get_precision_delimiter
if @print_settings
"," if @print_settings.delimiter
end
end || ""
end
def strip_insignificant_zeros
@strip_insignificant_zeros ||= Lookup.number_formats.find { |f| f.name.parameterize.underscore == 'strip_insignificant_zeros'}
if @strip_insignificant_zeros
['1', 't', 'true', 'on', 'y', 'yes'].include? @strip_insignificant_zeros.value.to_s
end || false
end
def number_format(number, options = {})
options[:precision] = options[:precision] || precision
# options[:delimiter] = options[:delimiter] || delimiter
options[:strip_insignificant_zeros] = options[:strip_insignificant_zeros] || strip_insignificant_zeros
if options[:precision] > 0
if options[:strip_insignificant_zeros]
formatted = "%.12g" % number.round(options[:precision])
else
formatted = "%.#{options[:precision]}f" % number.round(options[:precision])
end
else
formatted = number.to_i.to_s
end
if options[:delimiter] &&
formatted = formatted.gsub(/(\d)(?=\d{3}+(\.\d*)?$)/, "\\1#{options[:delimiter]}")
end
return formatted
end
end

View File

@@ -94,27 +94,24 @@ class HomeController < ApplicationController
@shop = Shop.first
today = DateTime.now.strftime('%Y-%m-%d')
@orders = Sale.where("payment_status = 'new' and sale_status = 'bill'")
@sales = Sale.completed
if !@from.nil? && !@to.nil?
@orders = @orders.date_between(@from, @to)
@sales = @sales.date_between(@from, @to)
if !@from_time.nil? && @to_time.nil?
@orders = Sale::where("payment_status='new' and sale_status='bill' and DATE_FORMAT(receipt_date,'%Y-%m-%d') between '#{@from}' and '#{@to}' and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%H:%m') between '#{@from_time}' and '#{@to_time}'").count()
else
@orders = Sale::where("payment_status='new' and sale_status='bill' and DATE_FORMAT(receipt_date,'%Y-%m-%d') between '#{@from}' and '#{@to}'").count()
@orders = @orders.time_between(@from_time, @to_time)
@sales = @sales.time_between(@from_time, @to_time)
end
else
@orders = Sale::where("payment_status='new' and sale_status='bill' and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'").count()
@orders = @orders.date_on(today)
@sales = @sales.date_on(today)
end
if !@from.nil? && !@to.nil?
if !@from_time.nil? && @to_time.nil?
@sales = Sale::where("payment_status='paid' and sale_status='completed' and DATE_FORMAT(receipt_date,'%Y-%m-%d') between '#{@from}' and '#{@to}' and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%H:%m') between '#{@from_time}' and '#{@to_time}'").count()
else
@sales = Sale::where("payment_status='paid' and sale_status='completed' and DATE_FORMAT(receipt_date,'%Y-%m-%d') between '#{@from}' and '#{@to}'").count()
end
else
@sales = Sale::where("payment_status='paid' and sale_status='completed' and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'").count()
end
@top_products = Sale.top_bottom_products(today,current_user,@from,@to,@from_time,@to_time,"top").sum('i.qty')
@bottom_products = Sale.top_bottom_products(today,current_user,@from,@to,@from_time,@to_time,"bottom").sum('i.qty')
@hourly_sales = Sale.hourly_sales(today,current_user,@from,@to,@from_time,@to_time).sum(:grand_total)
@top_products = Sale.top_bottom_products(today,current_user,@from,@to,@from_time,@to_time,"top")
@bottom_products = Sale.top_bottom_products(today,current_user,@from,@to,@from_time,@to_time,"bottom")
@hourly_sales = Sale.hourly_sales(today,current_user,@from,@to,@from_time,@to_time)
# .group_by_hour(:created_at, :time_zone => 'Asia/Rangoon',format: '%I:%p')
# .sum(:grand_total)
logger.debug 'hourly_sales<>><><><<<<<<>><<<><><><><><><><><><<>><'

View File

@@ -1,4 +1,4 @@
class Reports::ReceiptNoDetailController < BaseReportController
class Reports::ReceiptNoDetailController < BaseReportController
authorize_resource :class => false
def index
@payments = [["All Payment",''], ["Cash Payment","cash"], ["Credit Payment","creditnote"], ["FOC Payment","foc"]]
@@ -9,11 +9,10 @@ authorize_resource :class => false
@shift = ''
if params[:shift_name].to_i != 0
@shift_sale_range = Sale.get_by_shift_sale_by_item(from,to,Sale::SALE_STATUS_COMPLETED)
@shift_sale = ShiftSale.find(params[:shift_name])
if to.blank?
if to.blank?
@shift = ShiftSale.where('shift_started_at = ? and shift_closed_at is NULL ',@shift_sale.shift_started_at)
else
if @shift_sale.shift_closed_at.blank?
@@ -25,7 +24,7 @@ authorize_resource :class => false
end
payment_type = params[:payment_type]
@sale_data = Sale.get_shift_sales_by_receipt_no_detail(@shift_sale_range,@shift,from,to,payment_type)
@sale_data = Sale.get_shift_sales_by_receipt_no_detail(@shift_sale_range,@shift,from,to,payment_type)
@from = from
@to = to
@@ -62,10 +61,10 @@ authorize_resource :class => false
end
out = {:status => 'ok', :message => date_arr}
respond_to do |format|
format.json { render json: out }
end
end
end
end