From 2010b6a4bbc12057605e34b45e66244574ac5974 Mon Sep 17 00:00:00 2001 From: Thein Lin Kyaw Date: Thu, 28 Nov 2019 14:13:00 +0630 Subject: [PATCH] update number_formattable. update dashboard query. --- app/controllers/base_report_controller.rb | 96 ++++---- .../concerns/number_formattable.rb | 39 ++-- app/controllers/home_controller.rb | 88 +++---- .../origami/dashboard_controller.rb | 25 +- app/models/sale.rb | 221 ++++++++---------- app/models/stock_journal.rb | 24 +- app/views/home/dashboard.html.erb | 49 ++-- .../origami/dashboard/_menu.json.jbuilder | 2 +- 8 files changed, 225 insertions(+), 319 deletions(-) diff --git a/app/controllers/base_report_controller.rb b/app/controllers/base_report_controller.rb index 8cb3d00d..691ece34 100755 --- a/app/controllers/base_report_controller.rb +++ b/app/controllers/base_report_controller.rb @@ -26,61 +26,51 @@ class BaseReportController < ActionController::Base } def get_date_range_from_params - period_type = params[:period_type] - period = params[:period] - from = params[:from] - to = params[:to] - day_ref = Time.now.utc.getlocal + period_type = params[:period_type] + period = params[:period] - if from.present? && to.present? + if params[:from].present? && params[:to].present? + from = Time.parse(params[:from]) + to = Time.parse(params[:to]) + else + case period.to_i + when PERIOD["today"] + from = Time.now + to = Time.now + when PERIOD["yesterday"] + from = 1.day.ago + to = 1.day.ago + when PERIOD["this_week"] + from = Time.now.beginning_of_week + to = Time.now + when PERIOD["last_week"] + from = 1.week.ago.beginning_of_week + to = 1.week.ago.end_of_week + when PERIOD["last_7"] + from = 7.day.ago + to = Time.now + when PERIOD["this_month"] + from = Time.now.beginning_of_month + to = Time.now + when PERIOD["last_month"] + from = 1.month.ago.beginning_of_month + to = 1.month.ago.end_of_month + when PERIOD["last_30"] + from = 30.day.ago + to = Time.now + when PERIOD["this_year"] + from = Time.now.beginning_of_year + to = Time.now + when PERIOD["last_year"] + from = 1.year.ago.beginning_of_year + to = 1.year.ago.end_of_year + end + end - f_date = DateTime.parse(from) - t_date = DateTime.parse(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 - - 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 - - return from, to + from = from.beginning_of_day + to = to.end_of_day + + return from, to end def check_user diff --git a/app/controllers/concerns/number_formattable.rb b/app/controllers/concerns/number_formattable.rb index a27c551c..a7b97732 100644 --- a/app/controllers/concerns/number_formattable.rb +++ b/app/controllers/concerns/number_formattable.rb @@ -3,40 +3,43 @@ module NumberFormattable def precision @precision ||= Lookup.number_formats.find { |f| f.name.parameterize.underscore == 'precision'} - if @precision - @precision.value.to_i - else + if @precision.nil? @print_settings ||= PrintSetting.get_precision_delimiter if @print_settings - @print_settings.precision.to_i + @precision = OpenStruct.new(value: @print_settings.precision.to_i) + else + @precision = OpenStruct.new(value: 2) end - end || 2 + end + @precison_value ||= @precision.value.to_i 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 + if @delimiter.nil? @print_settings ||= PrintSetting.get_precision_delimiter - if @print_settings - "," if @print_settings.delimiter + if @print_settings && @print_settings.delimiter + @delimiter = OpenStruct.new(value: ",") + else + @delimiter = OpenStruct.new(value: "") end - end || "" + end + @delimiter_value ||= @delimiter.value.to_s.gsub(/\\u(\h{4})/) { |m| [$1].pack("H*").unpack("n*").pack("U*") } 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 + if @strip_insignificant_zeros.nil? + @strip_insignificant_zeros = OpenStruct.new(value: false) + end + @strip_insignificant_zeros_value ||= ['1', 't', 'true', 'on', 'y', 'yes'].include? @strip_insignificant_zeros.value.to_s 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]) @@ -44,10 +47,10 @@ module NumberFormattable formatted = "%.#{options[:precision]}f" % number.round(options[:precision]) end else - formatted = number.to_i.to_s + formatted = number.round(options[:precision]).to_i.to_s end - - if options[:delimiter] && + + if options[:delimiter] && !options[:delimiter].empty? formatted = formatted.gsub(/(\d)(?=\d{3}+(\.\d*)?$)/, "\\1#{options[:delimiter]}") end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index fd45ed1d..a82a6483 100755 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -89,25 +89,12 @@ class HomeController < ApplicationController end def dashboard - @from, @to, @from_time, @to_time = get_date_range_from_params + @from, @to = get_date_range_from_params @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 = @orders.time_between(@from_time, @to_time) - @sales = @sales.time_between(@from_time, @to_time) - end - else - @orders = @orders.date_on(today) - @sales = @sales.date_on(today) - end + @orders = Sale.receipt_date_between(@from, @to).where("payment_status = 'new' and sale_status = 'bill'") + @sales = Sale.receipt_date_between(@from, @to).completed if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') if shift = ShiftSale.current_open_shift(current_user.id) @@ -116,14 +103,14 @@ class HomeController < ApplicationController end end - @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) + @top_products = Sale.top_bottom_products(current_user,@from,@to,"top") + @bottom_products = Sale.top_bottom_products(current_user,@from,@to,"bottom") + @hourly_sales = Sale.hourly_sales(current_user,@from,@to) # .group_by_hour(:created_at, :time_zone => 'Asia/Rangoon',format: '%I:%p') # .sum(:grand_total) logger.debug 'hourly_sales<>><><><<<<<<>><<<><><><><><><><><><<>><' logger.debug @hourly_sales.to_json - employee_sales = Sale.employee_sales(today,current_user,@from,@to,@from_time,@to_time) + employee_sales = Sale.employee_sales(current_user,@from,@to) # .sum("(CASE WHEN sp.payment_method='cash' THEN ((sp.payment_amount) - (sales.amount_changed)) ELSE (sp.payment_amount) END)") @employee_sales = [] if !employee_sales.nil? @@ -136,36 +123,32 @@ class HomeController < ApplicationController end end end - @inventories = StockJournal.inventory_balances(today,@from,@to,@from_time,@to_time).sum(:balance) + @inventories = StockJournal.inventory_balances(@from,@to).sum(:balance) - @total_trans = Sale.total_trans(today,current_user,@from,@to,@from_time,@to_time) - @total_card = Sale.total_card_sale(today,current_user,@from,@to,@from_time,@to_time) - @total_credit = Sale.credit_payment(today,current_user,@from,@to,@from_time,@to_time) + @total_trans = Sale.total_trans(current_user,@from,@to) + @total_card = Sale.total_card_sale(current_user,@from,@to) + @total_credit = Sale.credit_payment(current_user,@from,@to) @sale_data = Array.new - @total_payment_methods = Sale.total_payment_methods(today,current_user,@from,@to,@from_time,@to_time) + @total_payment_methods = Sale.total_payment_methods(current_user,@from,@to) if !@total_payment_methods.nil? @total_payment_methods.each do |payment| - if payment.payment_method == "mpu" || payment.payment_method == "visa" || payment.payment_method == "master" || payment.payment_method == "jcb" || payment.payment_method == "unionpay" || payment.payment_method == "alipay" - pay = Sale.payment_sale('card', today, current_user,@from,@to,@from_time,@to_time) - @sale_data.push({'card' => pay.payment_amount}) - else - pay = Sale.payment_sale(payment.payment_method, today, current_user,@from,@to,@from_time,@to_time) - @sale_data.push({payment.payment_method => pay.payment_amount}) - end + pay = Sale.payment_sale(payment.payment_method, current_user,@from,@to) + @sale_data.push({payment.payment_method => pay.payment_amount}) end end - @summ_sale = Sale.summary_sale_receipt(today,current_user,@from,@to,@from_time,@to_time) - @total_customer, @total_dinein, @total_takeaway, @total_membership = Sale.total_customer(today,current_user,@from,@to,@from_time,@to_time) + + @summ_sale = Sale.summary_sale_receipt(current_user,@from,@to) + @total_customer, @total_dinein, @total_takeaway, @total_membership = Sale.total_customer(current_user,@from,@to) # @total_other_customer = Sale.total_other_customer(today,current_user) - @total_order = Sale.total_order(today,current_user,@from,@to,@from_time,@to_time) + @total_order = Sale.total_order(current_user,@from,@to) @total_accounts = Account.select("accounts.id as account_id, accounts.title as title") @account_data = Array.new if !@total_accounts.nil? @total_accounts.each do |account| - acc = Sale.account_data(account.account_id, today,current_user,@from,@to,@from_time,@to_time) + acc = Sale.account_data(account.account_id,current_user,@from,@to) if !acc.nil? && acc.cnt_acc > 0 @account_data.push({account.title => acc.cnt_acc, account.title + '_amount' => acc.total_acc}) end @@ -173,8 +156,8 @@ class HomeController < ApplicationController @total_accounts = @total_accounts.reject.with_index { |x, i| @account_data[i].nil? } end - @top_items = Sale.top_items(today,current_user,@from,@to,@from_time,@to_time) - @total_foc_items = Sale.total_foc_items(today,current_user,@from,@to,@from_time,@to_time) + @top_items = Sale.top_items(current_user,@from,@to) + @total_foc_items = Sale.total_foc_items(current_user,@from,@to) # get printer info @print_settings = PrintSetting.get_precision_delimiter() @@ -236,23 +219,18 @@ class HomeController < ApplicationController end def get_date_range_from_params - from = params[:from] - to = params[:to] - from_time = params[:from_time] - to_time = params[:to_time] - - if from.present? && to.present? - # f_date = DateTime.parse(from) - # t_date = DateTime.parse(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 - - from = DateTime.parse(from).utc.getlocal.strftime('%Y-%m-%d') - to = DateTime.parse(to).utc.getlocal.strftime('%Y-%m-%d') + if params[:from].present? && params[:to].present? + if params[:from_time].present? && params[:to_time].present? + from = Time.parse("#{params[:from]} #{params[:from_time]}") + to = Time.parse("#{params[:to]} #{params[:to_time]}") + else + from = Time.parse(params[:from]) + to = Time.parse(params[:to]).end_of_day end - - return from, to, from_time, to_time + else + from = Time.now.beginning_of_day + to = Time.now.end_of_day + end + return from, to end end diff --git a/app/controllers/origami/dashboard_controller.rb b/app/controllers/origami/dashboard_controller.rb index a1aa2064..00766d51 100644 --- a/app/controllers/origami/dashboard_controller.rb +++ b/app/controllers/origami/dashboard_controller.rb @@ -8,30 +8,25 @@ class Origami::DashboardController < BaseOrigamiController @display_type = Lookup.find_by_lookup_type("display_type") @sale_data = Array.new - @total_payment_methods = Sale.total_payment_methods(today,current_user) + @total_payment_methods = Sale.total_payment_methods(current_user) if !@total_payment_methods.nil? @total_payment_methods.each do |payment| - if payment.payment_method == "mpu" || payment.payment_method == "visa" || payment.payment_method == "master" || payment.payment_method == "jcb" || payment.payment_method == "unionpay" || payment.payment_method == "alipay" - pay = Sale.payment_sale('card', today, current_user) - @sale_data.push({'card' => pay.payment_amount}) - else - pay = Sale.payment_sale(payment.payment_method, today, current_user) - @sale_data.push({payment.payment_method => pay.payment_amount}) - end + pay = Sale.payment_sale(payment.payment_method, current_user) + @sale_data.push({payment.payment_method => pay.payment_amount}) end else @sale_data = nil end - @summ_sale = Sale.summary_sale_receipt(today,current_user) - @total_customer, @total_dinein, @total_takeaway, @total_membership = Sale.total_customer(today,current_user,@from,@to,@from_time,@to_time) + @summ_sale = Sale.summary_sale_receipt(current_user) + @total_customer, @total_dinein, @total_takeaway, @total_membership = Sale.total_customer(current_user,@from,@to) # @total_other_customer = Sale.total_other_customer(today,current_user) - @total_order = Sale.total_order(today,current_user) + @total_order = Sale.total_order(current_user) @total_accounts = Account.select("accounts.id as account_id, accounts.title as title") @account_data = Array.new if !@total_accounts.nil? @total_accounts.each do |account| - acc = Sale.account_data(account.account_id, today,current_user) + acc = Sale.account_data(account.account_id, current_user) if !acc.nil? @account_data.push({account.title => acc.cnt_acc, account.title + '_amount' => acc.total_acc}) end @@ -41,8 +36,8 @@ class Origami::DashboardController < BaseOrigamiController @account_data = nil end - @top_items = Sale.top_items(today,current_user) - @total_foc_items = Sale.total_foc_items(today,current_user) + @top_items = Sale.top_items(current_user) + @total_foc_items = Sale.total_foc_items(current_user) # get printer info @print_settings = PrintSetting.get_precision_delimiter() @@ -105,7 +100,7 @@ class Origami::DashboardController < BaseOrigamiController end def get_all_menu - @menus = Menu.includes(:menu_categories => {:menu_items => :menu_item_instances}).includes(:menu_categories => {:menu_items => :item_sets }).active.all + @menus = Menu.includes(:menu_categories => {:menu_items => :menu_item_instances}).includes(:menu_categories => {:menu_items => :item_sets }).includes(:menu_categories => {:menu_items => {:item_sets => :menu_item_instances}}).active.all @item_attributes = MenuItemAttribute.all.load @item_options = MenuItemOption.all.load end diff --git a/app/models/sale.rb b/app/models/sale.rb index 5507d550..18be54a9 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -29,10 +29,8 @@ class Sale < ApplicationRecord scope :complete_sale, -> { where("sale_status = 'completed' and receipt_date BETWEEN '#{DateTime.now.utc.beginning_of_day}' AND '#{DateTime.now.utc.end_of_day}'") } scope :paid, -> { where(payment_status: 'paid')} scope :completed, -> { where(sale_status: 'completed') } - scope :date_on, -> (date) { where("DATE(CONVERT_TZ(receipt_date, '+00:00', ?)) = ?", Time.zone.formatted_offset, date) } - scope :date_between, -> (from, to) { where("DATE(CONVERT_TZ(receipt_date, '+00:00', ?)) BETWEEN ? AND ?", Time.zone.formatted_offset, from, to) } - scope :time_between, -> (from, to) { where("TIME(CONVERT_TZ(receipt_date, '+00:00', ?)) BETWEEN ? AND ?", Time.zone.formatted_offset, from, to) } - scope :along_with_sale_payments_except_void, -> { 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', '#{Time.zone.formatted_offset}')) = DATE(CONVERT_TZ(sales.receipt_date,'+00:00', '#{Time.zone.formatted_offset}'))") } + scope :receipt_date_between, -> (from, to) { where(receipt_date: from..to) } + scope :along_with_sale_payments_except_void_between, -> (from, to) { joins(sanitize_sql_array(["LEFT JOIN sale_payments on sales.sale_status != 'void' AND sale_payments.sale_id = sales.sale_id AND sale_payments.created_at BETWEEN ? and ?", from, to])) } def qty_of(item_instance_code) order_items.select(:order_items_id, :item_instance_code, 'SUM(qty) as qty').where(item_instance_code: item_instance_code).group(:item_instance_code).first @@ -849,7 +847,7 @@ def self.daily_sales_list(from,to) 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") - .along_with_sale_payments_except_void + .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 @@ -1577,20 +1575,16 @@ end return tax end - def self.top_bottom_products(today,current_user,from,to,from_time,to_time,type) + def self.top_bottom_products(current_user,from,to,type) query = Sale.joins("JOIN sale_items ON sale_items.sale_id = sales.sale_id") .completed .where("qty > 0 AND price > 0") .group("SUBSTRING_INDEX(product_name, ' - ', 1)") if !from.nil? && !to.nil? - query = query.date_between(from, to) - if !from_time.nil? && !to_time.nil? - query = query.time_between(from_time, to_time) - end - else - query = query.date_on(today) + query = query.receipt_date_between(from, to) end + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') if shift = ShiftSale.current_open_shift(current_user.id) query = query.where("shift_sale_id='#{shift.id}'") @@ -1606,17 +1600,12 @@ end query.limit(20).sum('qty') end - def self.hourly_sales(today,current_user,from,to,from_time,to_time) - query = Sale.group("date_format(CONVERT_TZ(receipt_date,'+00:00', '+06:30'), '%I %p')") + def self.hourly_sales(current_user,from,to) + query = Sale.group("date_format(CONVERT_TZ(receipt_date,'+00:00', '#{Time.zone.formatted_offset}'), '%I %p')") .order('receipt_date').completed if !from.nil? && !to.nil? - query = query.date_between(from, to) - if !from_time.nil? && !to_time.nil? - query = query.time_between(from_time, to_time) - end - else - query = query.date_on(today) + query = query.receipt_date_between(from, to) end if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') @@ -1624,62 +1613,59 @@ end query = query.where("shift_sale_id='#{shift.id}'") end end + query.sum(:grand_total) end - def self.employee_sales(today,current_user,from,to,from_time,to_time) + def self.employee_sales(current_user,from,to) shift = if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') ShiftSale.current_open_shift(current_user.id) end payments_for_credits = SalePayment.joins(:sale_audit).to_sql - query = employee_sale(today, shift, from, to, from_time, to_time) + query = employee_sale(shift, from, to) .joins("LEFT JOIN (#{payments_for_credits}) payments_for_credits ON payments_for_credits.sale_id = sales.sale_id") .select("SUM(sale_payments.payment_amount) - CASE WHEN sale_payments.payment_method = 'creditnote' THEN IFNULL(SUM(payments_for_credits.payment_amount), 0) ELSE ABS(SUM(CASE WHEN sale_payments.outstanding_amount < 0 THEN sale_payments.outstanding_amount ELSE 0 END)) END AS payment_amount, CASE WHEN sale_payments.payment_method IN ('mpu', 'visa', 'master', 'jcb', 'unionpay', 'alipay', 'paymal', 'dinga', 'JunctionPay', 'giftvoucher') THEN 'card' ELSE sale_payments.payment_method END AS payment_method, employees.name AS e_name") end - def self.total_trans(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) + def self.total_trans(current_user=nil,from=nil,to=nil) query = Sale.select("SUM(grand_total) as total_sale, COUNT(sales.sale_id) as total_count") .where('sale_status = "completed"') - if (!from.nil? && !to.nil?) && (from != "" && to!="") - query = query.date_between(from, to) - if !from_time.nil? && !to_time.nil? - query = query.time_between(from_time, to_time) - end - else - query = query.date_on(today) + + if (!from.nil? && !to.nil?) + query = query.receipt_date_between(from, to) end + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') if shift = ShiftSale.current_open_shift(current_user.id) query = query.where("sales.shift_sale_id = ?", shift.id) end end + return query end - def self.total_card_sale(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) + def self.total_card_sale(current_user=nil,from=nil,to=nil) query = Sale.joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") .where('sales.sale_status = "completed" and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay")') - if (!from.nil? && !to.nil?) && (from != "" && to!="") - query = query.date_between(from, to) - if !from_time.nil? && !to_time.nil? - query = query.time_between(from_time, to_time) - end - else - query = query.date_on(today) + + if (!from.nil? && !to.nil?) + query = query.receipt_date_between(from, to) end + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') if shift = ShiftSale.current_open_shift(current_user.id) query = query.where("sales.shift_sale_id = ?", shift.id) end end + query = query.sum("sp.payment_amount") end - def self.credit_payment(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) + def self.credit_payment(current_user=nil,from=nil,to=nil) payments_for_credits = SalePayment.joins(:sale_audit).to_sql query = SalePayment.credits @@ -1687,14 +1673,10 @@ end .joins("LEFT JOIN (#{payments_for_credits}) payments_for_credits ON payments_for_credits.sale_id = sale_payments.sale_id") .where("sale_payments.payment_method= ? AND sales.sale_status = ?", 'creditnote', 'completed') - if (!from.nil? && !to.nil?) && (from != "" && to!="") - query = query.merge(Sale.date_between(from, to)) - if !from_time.nil? && !to_time.nil? - query = query.merge(Sale.time_between(from_time, to_time)) - end - else - query = query.merge(Sale.date_on(today)) + if (!from.nil? && !to.nil?) + query = query.merge(Sale.receipt_date_between(from, to)) end + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') if shift = ShiftSale.current_open_shift(current_user.id) query = query.where("sales.shift_sale_id = ?", shift.id) @@ -1704,46 +1686,43 @@ end return query.sum("sale_payments.payment_amount - IFNULL(payments_for_credits.payment_amount, 0)") end - def self.summary_sale_receipt(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) + def self.summary_sale_receipt(current_user=nil,from=nil,to=nil) query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') .where('sale_status = "completed"') - if (!from.nil? && !to.nil?) && (from != "" && to!="") - query = query.date_between(from, to) - if !from_time.nil? && !to_time.nil? - query = query.time_between(from_time, to_time) - end - else - query = query.date_on(today) + + if (!from.nil? && !to.nil?) + query = query.receipt_date_between(from, to) end + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') if shift = ShiftSale.current_open_shift(current_user.id) query = query.where("sales.shift_sale_id = ?", shift.id) end end + query = query.first() end - def self.total_payment_methods(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) - query = Sale.select("distinct sp.payment_method") - .where('sales.sale_status = "completed"') + def self.total_payment_methods(current_user=nil,from=nil,to=nil) + query = Sale.select("CASE WHEN sp.payment_method IN ('mpu', 'visa', 'master', 'jcb', 'unionpay', 'alipay', 'paymal', 'dinga', 'JunctionPay', 'giftvoucher') THEN 'card' ELSE sp.payment_method END as payment_method") + .where("sales.sale_status = 'completed'") .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") - if (!from.nil? && !to.nil?) && (from != "" && to!="") - query = query.date_between(from, to) - if !from_time.nil? && !to_time.nil? - query = query.time_between(from_time, to_time) - end - else - query = query.date_on(today) + .group("CASE WHEN sp.payment_method IN ('mpu', 'visa', 'master', 'jcb', 'unionpay', 'alipay', 'paymal', 'dinga', 'JunctionPay', 'giftvoucher') THEN 'card' ELSE sp.payment_method END") + + if (!from.nil? && !to.nil?) + query = query.receipt_date_between(from, to) end + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') if shift = ShiftSale.current_open_shift(current_user.id) query = query.where("sales.shift_sale_id = ?", shift.id) end end + return query end - def self.payment_sale(payment_method, today, current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) + def self.payment_sale(payment_method, current_user=nil,from=nil,to=nil) payments_for_credits = SalePayment.joins(:sale_audit).to_sql query = Sale.select("SUM(sale_payments.payment_amount) - CASE WHEN sale_payments.payment_method = 'creditnote' THEN IFNULL(SUM(payments_for_credits.payment_amount), 0) ELSE ABS(SUM(CASE WHEN sale_payments.outstanding_amount < 0 THEN sale_payments.outstanding_amount ELSE 0 END)) END AS payment_amount") @@ -1757,13 +1736,8 @@ end query = query.where("sale_payments.payment_method = ?", payment_method) end - if (!from.nil? && !to.nil?) && (from != "" && to!="") - query = query.date_between(from, to) - if !from_time.nil? && !to_time.nil? - query = query.time_between(from_time,to_time) - end - else - query = query.date_on(today) + if (!from.nil? && !to.nil?) + query = query.receipt_date_between(from, to) end if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') @@ -1771,25 +1745,31 @@ end query = query.where("sales.shift_sale_id = ?", shift.id) end end + query.first end - def self.total_customer(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) - total_dinein_takeaway = self.total_dinein_takeaway(today,current_user,from,to,from_time,to_time) + def self.total_customer(current_user=nil,from=nil,to=nil) + total_dinein_takeaway = self.total_dinein_takeaway(current_user,from,to) dinein_cnt = 0 takeaway_cnt = 0 + if !total_dinein_takeaway.nil? if total_dinein_takeaway[0] dinein_cnt = total_dinein_takeaway[0].total_dinein_cus takeaway_cnt = total_dinein_takeaway[0].total_take_cus end end - membership_cnt = self.total_membership(today,current_user,from,to,from_time,to_time) + + membership_cnt = self.total_membership(current_user,from,to) member_cnt = 0 + if !membership_cnt.nil? member_cnt = membership_cnt.total_memb_cus end + total_cus = 0 + if dinein_cnt > dinein_cnt || takeaway_cnt > 0 || !membership_cnt.nil? total_cus = dinein_cnt.to_int + takeaway_cnt.to_int + member_cnt.to_int end @@ -1797,43 +1777,39 @@ end return total_cus, dinein_cnt, takeaway_cnt, member_cnt end - def self.total_dinein_takeaway(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) + def self.total_dinein_takeaway(current_user=nil,from=nil,to=nil) query = Sale.select("(CASE WHEN c.customer_type='Dinein' THEN count(sales.customer_id) ELSE 0 END) as total_dinein_cus, (CASE WHEN c.customer_type='Takeaway' THEN count(sales.customer_id) ELSE 0 END) as total_take_cus") .joins("JOIN customers as c ON c.customer_id = sales.customer_id") .where('sales.sale_status = "completed" and c.membership_id is null') - if (!from.nil? && !to.nil?) && (from != "" && to!="") - query = query.date_between(from, to) - if !from_time.nil? && !to_time.nil? - query = query.time_between(from_time, to_time) - end - else - query = query.date_on(today) + + if (!from.nil? && !to.nil?) + query = query.receipt_date_between(from, to) end + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') if shift = ShiftSale.current_open_shift(current_user.id) query = query.where("sales.shift_sale_id = ?", shift.id) end end + query = query.first() end - def self.total_membership(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) + def self.total_membership(current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") .joins("JOIN customers as c ON c.customer_id = sales.customer_id") .where('sales.sale_status = "completed" and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))') - if (!from.nil? && !to.nil?) && (from != "" && to!="") - query = query.date_between(from, to) - if !from_time.nil? && !to_time.nil? - query = query.time_between(from_time, to_time) - end - else - query = query.date_on(today) + + if (!from.nil? && !to.nil?) + query = query.receipt_date_between(from, to) end + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') if shift = ShiftSale.current_open_shift(current_user.id) query = query.where("sales.shift_sale_id = ?", shift.id) end end + query = query.first() end @@ -1883,17 +1859,13 @@ end # query = query.first() # end - def self.total_order(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) + def self.total_order(current_user=nil,from=nil,to=nil) query = Sale.select("count(distinct sale_orders.order_id) as total_order") .joins(:sale_orders) .completed - if (!from.nil? && !to.nil?) && (from != "" && to!="") - query = query.date_between(from, to) - if !from_time.nil? && !to_time.nil? - query = query.time_between(from_time, to_time) - end - else - query = query.date_on(today) + + if (!from.nil? && !to.nil?) + query = query.receipt_date_between(from, to) end if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') @@ -1905,64 +1877,58 @@ end query = query.first end - def self.account_data(account_id, today, current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) + def self.account_data(account_id, current_user=nil,from=nil,to=nil) query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}'") - if (!from.nil? && !to.nil?) && (from != "" && to!="") - query = query.date_between(from, to) - if !from_time.nil? && !to_time.nil? - query = query.time_between(from_time, to_time) - end - else - query = query.date_on(today) + + if (!from.nil? && !to.nil?) + query = query.receipt_date_between(from, to) end + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') if shift = ShiftSale.current_open_shift(current_user.id) query = query.where("sales.shift_sale_id = ?", shift.id) end end + query = query.first end - def self.top_items(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) + def self.top_items(current_user=nil,from=nil,to=nil) query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed'") - if (!from.nil? && !to.nil?) && (from != "" && to!="") - query = query.date_between(from, to) - if !from_time.nil? && !to_time.nil? - query = query.time_between(from_time, to_time) - end - else - query = query.date_on(today) + + if (!from.nil? && !to.nil?) + query = query.receipt_date_between(from, to) end + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') if shift = ShiftSale.current_open_shift(current_user.id) query = query.where("sales.shift_sale_id = ?", shift.id) end end + query = query.group("a.product_code") .order("SUM(a.qty) DESC") .first() end - def self.total_foc_items(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) + def self.total_foc_items(current_user=nil,from=nil,to=nil) query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") .where("sales.sale_status = 'completed' and a.status='foc' and a.product_name like '%FOC%'") - if (!from.nil? && !to.nil?) && (from != "" && to!="") - query = query.date_between(from, to) - if !from_time.nil? && !to_time.nil? - query = query.time_between(from_time, to_time) - end - else - query = query.date_on(today) + + if (!from.nil? && !to.nil?) + query = query.receipt_date_between(from, to) end + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') if shift = ShiftSale.current_open_shift(current_user.id) query = query.where("sales.shift_sale_id = ?", shift.id) end end + query = query.count() end @@ -2101,17 +2067,12 @@ def unique_tax_profiles(order_source, customer_id) return tax_data end -def self.employee_sale(today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil) +def self.employee_sale(shift=nil,from=nil,to=nil,from_time=nil,to_time=nil) query = Sale.joins(:cashier) .joins(:sale_payments) .paid.completed if !from.nil? && !to.nil? - query = query.date_between(from, to) - if !from_time.nil? && !to_time.nil? - query = query.time_between(from_time, to_time) - end - else - query = query.date_on(today) + query = query.receipt_date_between(from, to) end if !shift.nil? diff --git a/app/models/stock_journal.rb b/app/models/stock_journal.rb index 6e72d94a..94cb71d6 100755 --- a/app/models/stock_journal.rb +++ b/app/models/stock_journal.rb @@ -4,6 +4,8 @@ class StockJournal < ApplicationRecord ORDER_TRANS = "order" STOCK_CHECK_TRANS = "stock_check" + scope :created_at_between, -> (from, to) { where(created_at: from..to)} + def self.add_to_journal(item_instance_code, qty, old_balance, stock_message, inventory_definition, trans_ref, trans_type) # item => saleObj | balance => Stock journal balance = calculate_balance(old_balance, qty) @@ -52,23 +54,13 @@ class StockJournal < ApplicationRecord journal.save end - def self.inventory_balances(today,from,to,from_time,to_time) + def self.inventory_balances(from,to) + query = StockJournal.select("mii.item_instance_name as item_instance_name,balance") + .joins("join menu_item_instances mii on mii.item_instance_code=stock_journals.item_code") + .group("mii.item_instance_name") + .order("mii.item_instance_name ASC") if !from.nil? && !to.nil? - query = StockJournal.select("mii.item_instance_name as item_instance_name,balance") - .joins("join menu_item_instances mii on mii.item_instance_code=stock_journals.item_code") - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(stock_journals.created_at,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'") - else - query = query.where("DATE_FORMAT(CONVERT_TZ(stock_journals.created_at,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and DATE_FORMAT(CONVERT_TZ(stock_journals.created_at,'+00:00','+06:30'),'%H:%M') between '#{from_time}' and '#{to_time}'") - end - query = query.group("mii.item_instance_name") - .order("mii.item_instance_name ASC") - else - query = StockJournal.select("mii.item_instance_name as item_instance_name,balance") - .joins("join menu_item_instances mii on mii.item_instance_code=stock_journals.item_code") - .where("DATE_FORMAT(stock_journals.created_at,'%Y-%m-%d') = '#{today}'") - .group("mii.item_instance_name") - .order("mii.item_instance_name ASC") + query = query.created_at_between(from, to) end end diff --git a/app/views/home/dashboard.html.erb b/app/views/home/dashboard.html.erb index 5c2eeb8d..6cd52f4e 100755 --- a/app/views/home/dashboard.html.erb +++ b/app/views/home/dashboard.html.erb @@ -67,24 +67,16 @@
<% if !@from.nil? && @from != "" - from_date = DateTime.parse(@from).utc.getlocal.strftime("%d-%m-%Y") + from_date = @from.strftime("%d-%m-%Y") + from_time = @from.strftime("%H:%M") if @from.strftime("%H:%M") != "00:00" else - from_date = Time.now.utc.getlocal.strftime('%d-%m-%Y') + from_date = Time.now.strftime('%d-%m-%Y') end if !@to.nil? && @to != "" - to_date = DateTime.parse(@to).utc.getlocal.strftime("%d-%m-%Y") + to_date = @to.strftime("%d-%m-%Y") + to_time = @to.strftime("%H:%M") if @to.strftime("%H:%M") != "23:59" else - to_date = Time.now.utc.getlocal.strftime('%d-%m-%Y') - end - if !@from_time.nil? - from_time = @from_time - else - from_time = '' - end - if !@to_time.nil? - to_time = @to_time - else - to_time = '' + to_date = Time.now.strftime('%d-%m-%Y') end %>
@@ -217,26 +209,21 @@ <% if !(@total_payment_methods.nil?) %> <% @total_payment_methods.each do |payment| %> - <% if !@sale_data[0].empty? %> - <% if payment.payment_method != 'mpu' && payment.payment_method != 'visa' && payment.payment_method != 'master' && payment.payment_method != 'jcb' && payment.payment_method != 'unionpay' %> - - <% if payment.payment_method == 'paypar' %> - - <% else %> - - <% end %> - - - <% end %> + <% if payment.payment_method != 'card' %> + + <% if payment.payment_method == 'paypar' %> + + <% else %> + + <% end %> + + <% end %> <% end %> - <% total_card = @sale_data.select { |hash| hash["card"]!=nil }.first %> + <% total_card = @sale_data.find {|x| x.key?("card") } %> <% if !total_card.nil? %> diff --git a/app/views/origami/dashboard/_menu.json.jbuilder b/app/views/origami/dashboard/_menu.json.jbuilder index 9c9f1963..dcbff54f 100644 --- a/app/views/origami/dashboard/_menu.json.jbuilder +++ b/app/views/origami/dashboard/_menu.json.jbuilder @@ -80,7 +80,7 @@ if (menu.menu_categories) alt_name: its.alt_name, min_selectable_qty: its.min_selectable_qty, max_selectable_qty: its.max_selectable_qty, - instances: its.menu_item_instances.pluck(:id).map { |id| {id: id}} + instances: its.menu_item_instances.map { |id| {id: id}} } }
Redeem Sale : <%= payment.payment_method.to_s.capitalize %> Sale : - <% @sale_data.each do |data| %> - <% pay_mth = payment.payment_method %> - <%= number_format(data[""+pay_mth+""], precision: precision.to_i ,delimiter: delimiter) rescue number_format(0, precision: precision.to_i ,delimiter: delimiter) %> - <% end %> -
Redeem Sale : <%= payment.payment_method.to_s.capitalize %> Sale : + <%= number_format(@sale_data.find {|x| x.key?(payment.payment_method) }[payment.payment_method], precision: precision.to_i ,delimiter: delimiter) rescue number_format(0, precision: precision.to_i ,delimiter: delimiter) %> +
<%= t("views.right_panel.detail.other_payment") %> :