add filter for dashboard

This commit is contained in:
phyusin
2018-02-21 09:15:41 +06:30
parent 521b4d897b
commit 84c0a60c08
5 changed files with 703 additions and 256 deletions

View File

@@ -89,8 +89,6 @@ class ApplicationController < ActionController::Base
end end
def current_user def current_user
puts "session"
puts session[:session_token]
@current_user ||= Employee.find_by_token_session(session[:session_token]) if session[:session_token] @current_user ||= Employee.find_by_token_session(session[:session_token]) if session[:session_token]
end end

View File

@@ -62,8 +62,7 @@ class HomeController < ApplicationController
end end
def dashboard def dashboard
# puts "date range =>" @from, @to = get_date_range_from_params
# puts get_date_range_from_params
@shop = Shop.first @shop = Shop.first
@@ -71,51 +70,51 @@ class HomeController < ApplicationController
@orders = Sale::where("payment_status='new' and sale_status='bill' and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'").count() @orders = Sale::where("payment_status='new' and sale_status='bill' and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'").count()
@sales = Sale::where("payment_status='paid' and sale_status='completed' and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'").count() @sales = Sale::where("payment_status='paid' and sale_status='completed' and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'").count()
@top_products = Sale.top_products(today).sum('i.qty') @top_products = Sale.top_products(today,@from,@to).sum('i.qty')
@bottom_products = Sale.bottom_products(today).sum('i.qty') @bottom_products = Sale.bottom_products(today,@from,@to).sum('i.qty')
@hourly_sales = Sale.hourly_sales(today).sum(:grand_total) @hourly_sales = Sale.hourly_sales(today,@from,@to).sum(:grand_total)
# .group_by_hour(:created_at, :time_zone => 'Asia/Rangoon',format: '%I:%p') # .group_by_hour(:created_at, :time_zone => 'Asia/Rangoon',format: '%I:%p')
# .sum(:grand_total) # .sum(:grand_total)
@employee_sales = Sale.employee_sales(today) @employee_sales = Sale.employee_sales(today,@from,@to)
.sum('(CASE WHEN sp.payment_method="cash" THEN (sp.payment_amount - sales.amount_changed) ELSE sp.payment_amount END)') .sum('(CASE WHEN sp.payment_method="cash" THEN (sp.payment_amount - sales.amount_changed) ELSE sp.payment_amount END)')
@inventories = StockJournal.inventory_balances(today).sum(:balance) @inventories = StockJournal.inventory_balances(today,@from,@to).sum(:balance)
@total_sale = Sale.total_sale(today,current_user) @total_sale = Sale.total_sale(today,current_user,@from,@to)
@total_count = Sale.total_count(today,current_user) @total_count = Sale.total_count(today,current_user,@from,@to)
@total_card = Sale.total_card_sale(today,current_user) @total_card = Sale.total_card_sale(today,current_user,@from,@to)
@total_credit = Sale.credit_payment(today,current_user) @total_credit = Sale.credit_payment(today,current_user,@from,@to)
@sale_data = Array.new @sale_data = Array.new
@total_payment_methods = Sale.total_payment_methods(today,current_user) @total_payment_methods = Sale.total_payment_methods(today,current_user,@from,@to)
@total_payment_methods.each do |payment| @total_payment_methods.each do |payment|
if payment.payment_method == "mpu" || payment.payment_method == "visa" || payment.payment_method == "master" || payment.payment_method == "jcb" if payment.payment_method == "mpu" || payment.payment_method == "visa" || payment.payment_method == "master" || payment.payment_method == "jcb"
pay = Sale.payment_sale('card', today, current_user) pay = Sale.payment_sale('card', today, current_user,@from,@to)
@sale_data.push({'card' => pay.payment_amount}) @sale_data.push({'card' => pay.payment_amount})
else else
pay = Sale.payment_sale(payment.payment_method, today, current_user) pay = Sale.payment_sale(payment.payment_method, today, current_user,@from,@to)
@sale_data.push({payment.payment_method => pay.payment_amount}) @sale_data.push({payment.payment_method => pay.payment_amount})
end end
end end
@summ_sale = Sale.summary_sale_receipt(today,current_user) @summ_sale = Sale.summary_sale_receipt(today,current_user,@from,@to)
@total_customer = Sale.total_customer(today,current_user) @total_customer = Sale.total_customer(today,current_user,@from,@to)
@total_dinein = Sale.total_dinein(today,current_user) @total_dinein = Sale.total_dinein(today,current_user,@from,@to)
@total_takeaway = Sale.total_takeaway(today,current_user) @total_takeaway = Sale.total_takeaway(today,current_user,@from,@to)
@total_other_customer = Sale.total_other_customer(today,current_user) @total_other_customer = Sale.total_other_customer(today,current_user,@from,@to)
@total_membership = Sale.total_membership(today,current_user) @total_membership = Sale.total_membership(today,current_user,@from,@to)
@total_order = Sale.total_order(today,current_user) @total_order = Sale.total_order(today,current_user,@from,@to)
@total_accounts = Sale.total_account(today,current_user) @total_accounts = Sale.total_account(today,current_user,@from,@to)
@account_data = Array.new @account_data = Array.new
@total_accounts.each do |account| @total_accounts.each do |account|
acc = Sale.account_data(account.account_id, today,current_user) acc = Sale.account_data(account.account_id, today,current_user,@from,@to)
if !acc.nil? if !acc.nil?
@account_data.push({account.title => acc.cnt_acc, account.title + '_amount' => acc.total_acc}) @account_data.push({account.title => acc.cnt_acc, account.title + '_amount' => acc.total_acc})
end end
end end
@top_items = Sale.top_items(today,current_user) @top_items = Sale.top_items(today,current_user,@from,@to)
@total_foc_items = Sale.total_foc_items(today,current_user) @total_foc_items = Sale.total_foc_items(today,current_user,@from,@to)
# get printer info # get printer info
@print_settings = PrintSetting.get_precision_delimiter() @print_settings = PrintSetting.get_precision_delimiter()

View File

@@ -1053,7 +1053,16 @@ end
return tax return tax
end end
def self.top_products(today) def self.top_products(today,from,to)
if !from.nil? && !to.nil?
query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," +
" i.price as unit_price,mi.name as product_name")
.joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code")
.where("(i.qty > 0 and i.price > 0) and receipt_date between '#{from}' and '#{to}'"+
"and payment_status='paid' and sale_status= 'completed'")
.group('mi.name')
.order("SUM(i.qty) DESC").limit(20)
else
query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," +
" i.price as unit_price,mi.name as product_name") " i.price as unit_price,mi.name as product_name")
.joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code") .joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code")
@@ -1062,8 +1071,18 @@ end
.group('mi.name') .group('mi.name')
.order("SUM(i.qty) DESC").limit(20) .order("SUM(i.qty) DESC").limit(20)
end end
end
def self.bottom_products(today) def self.bottom_products(today,from,to)
if !from.nil? && !to.nil?
query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," +
" i.price as unit_price,mi.name as product_name")
.joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code")
.where("(i.qty > 0 and i.price > 0) and receipt_date between '#{from}' and '#{to}'"+
"and payment_status='paid' and sale_status= 'completed'")
.group('mi.name')
.order("SUM(i.qty) ASC").limit(20)
else
query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," +
" i.price as unit_price,mi.name as product_name") " i.price as unit_price,mi.name as product_name")
.joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code") .joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code")
@@ -1072,23 +1091,54 @@ end
.group('mi.name') .group('mi.name')
.order("SUM(i.qty) ASC").limit(20) .order("SUM(i.qty) ASC").limit(20)
end end
end
def self.hourly_sales(today) def self.hourly_sales(today,from,to)
if !from.nil? && !to.nil?
query= Sale.select("grand_total")
.where('payment_status="paid" and sale_status = "completed" and receipt_date between ? and ?',from,to)
.group("date_format(CONVERT_TZ(receipt_date,'+00:00', 'SYSTEM'), '%I %p')")
.order('receipt_date')
else
query= Sale.select("grand_total") query= Sale.select("grand_total")
.where('payment_status="paid" and sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today) .where('payment_status="paid" and sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today)
.group("date_format(CONVERT_TZ(receipt_date,'+00:00', 'SYSTEM'), '%I %p')") .group("date_format(CONVERT_TZ(receipt_date,'+00:00', 'SYSTEM'), '%I %p')")
.order('receipt_date') .order('receipt_date')
end end
end
def self.employee_sales(today) def self.employee_sales(today,from,to)
if !from.nil? && !to.nil?
query = Sale.joins("JOIN employees as e on e.id=sales.cashier_id")
.joins("JOIN sale_payments as sp on sp.sale_id=sales.sale_id")
.where("sales.payment_status='paid' and sales.sale_status = 'completed' and sales.receipt_date between '#{from}' and '#{to}'")
.group("(CASE WHEN (sp.payment_method='mpu' or sp.payment_method='visa' or sp.payment_method='master' or sp.payment_method='jcb' or sp.payment_method='unionpay') THEN 'card' ELSE sp.payment_method END)","e.name")
.order("e.name")
else
query = Sale.joins("JOIN employees as e on e.id=sales.cashier_id") query = Sale.joins("JOIN employees as e on e.id=sales.cashier_id")
.joins("JOIN sale_payments as sp on sp.sale_id=sales.sale_id") .joins("JOIN sale_payments as sp on sp.sale_id=sales.sale_id")
.where("sales.payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = '#{today}'") .where("sales.payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = '#{today}'")
.group("(CASE WHEN (sp.payment_method='mpu' or sp.payment_method='visa' or sp.payment_method='master' or sp.payment_method='jcb' or sp.payment_method='unionpay') THEN 'card' ELSE sp.payment_method END)","e.name") .group("(CASE WHEN (sp.payment_method='mpu' or sp.payment_method='visa' or sp.payment_method='master' or sp.payment_method='jcb' or sp.payment_method='unionpay') THEN 'card' ELSE sp.payment_method END)","e.name")
.order("e.name") .order("e.name")
end end
end
def self.total_sale(today,current_user=nil) def self.total_sale(today,current_user=nil,from,to)
if !from.nil? && !to.nil?
if current_user.nil?
total = Sale.where('sale_status = "completed" and receipt_date between ? and ?',from,to).sum("grand_total")
else
if current_user.role == 'administrator'
total = Sale.where('sale_status = "completed" and receipt_date between ? and ?',from,to).sum("grand_total")
else
shift = ShiftSale.current_open_shift(current_user.id)
if !shift.nil?
total = Sale.where('sale_status = "completed" and sales.receipt_date between ? and ? and shift_sale_id=?',from,to,shift.id)
.sum("grand_total")
end
end
end
else
if current_user.nil? if current_user.nil?
total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).sum("grand_total") total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).sum("grand_total")
else else
@@ -1103,8 +1153,23 @@ end
end end
end end
end end
end
def self.total_count(today,current_user=nil) def self.total_count(today,current_user=nil,from,to)
if !from.nil? && !to.nil?
if current_user.nil?
total = Sale.where('sale_status = "completed" and receipt_date between ? and ?',from,to).count
else
if current_user.role == 'administrator'
total = Sale.where('sale_status = "completed" and receipt_date between ? and ?',from,to).count
else
shift = ShiftSale.current_open_shift(current_user.id)
if !shift.nil?
total = Sale.where('sale_status = "completed" and receipt_date between ? and ? and shift_sale_id = ?',from,to,shift.id).count
end
end
end
else
if current_user.nil? if current_user.nil?
total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).count total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).count
else else
@@ -1118,8 +1183,29 @@ end
end end
end end
end end
end
def self.total_card_sale(today,current_user=nil) def self.total_card_sale(today,current_user=nil,from,to)
if !from.nil? && !to.nil?
if current_user.nil?
query = Sale.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? 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")',from,to)
.joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id")
.sum("sp.payment_amount")
else
if current_user.role == 'administrator'
query = Sale.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? 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")',from,to)
.joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id")
.sum("sp.payment_amount")
else
shift = ShiftSale.current_open_shift(current_user.id)
if !shift.nil?
query = Sale.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? 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") and shift_sale_id=?',from,to,shift.id)
.joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id")
.sum("sp.payment_amount")
end
end
end
else
if current_user.nil? if current_user.nil?
query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? 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")',today) query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? 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")',today)
.joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id")
@@ -1139,8 +1225,29 @@ end
end end
end end
end end
end
def self.credit_payment(today,current_user=nil) def self.credit_payment(today,current_user=nil,from,to)
if !from.nil? && !to.nil?
if current_user.nil?
query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and s.receipt_date between ? and ?',from,to)
.joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id")
.sum("payment_amount")
else
if current_user.role == 'administrator'
query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and s.receipt_date between ? and ?',from,to)
.joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id")
.sum("payment_amount")
else
shift = ShiftSale.current_open_shift(current_user.id)
if !shift.nil?
query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and s.receipt_date between ? and ? and s.shift_sale_id=?',from,to,shift.id)
.joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id")
.sum("payment_amount")
end
end
end
else
if current_user.nil? if current_user.nil?
query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(s.receipt_date,"%Y-%m-%d") = ?',today) query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(s.receipt_date,"%Y-%m-%d") = ?',today)
.joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id")
@@ -1160,8 +1267,29 @@ end
end end
end end
end end
end
def self.summary_sale_receipt(today,current_user=nil) def self.summary_sale_receipt(today,current_user=nil,from,to)
if !from.nil? && !to.nil?
if current_user.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" and receipt_date between ? and ?',from,to)
.first()
else
if current_user.role == 'administrator'
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" and receipt_date between ? and ?',from,to)
.first()
else
shift = ShiftSale.current_open_shift(current_user.id)
if !shift.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" and receipt_date between ? and ? and shift_sale_id=?',from,to,shift.id)
.first()
end
end
end
else
if current_user.nil? if current_user.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') 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" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today) .where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today)
@@ -1181,8 +1309,29 @@ end
end end
end end
end end
end
def self.total_payment_methods(today,current_user=nil) def self.total_payment_methods(today,current_user=nil,from,to)
if !from.nil? && !to.nil?
if current_user.nil?
query = Sale.select("distinct sp.payment_method")
.where('sales.sale_status = "completed" and sales.receipt_date between ? and ?',from,to)
.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
else
if current_user.role == 'administrator'
query = Sale.select("distinct sp.payment_method")
.where('sales.sale_status = "completed" and sales.receipt_date between ? and ?',from,to)
.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
else
shift = ShiftSale.current_open_shift(current_user.id)
if !shift.nil?
query = Sale.select("distinct sp.payment_method")
.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and sales.shift_sale_id=?',from,to,shift.id)
.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
end
end
end
else
if current_user.nil? if current_user.nil?
query = Sale.select("distinct sp.payment_method") query = Sale.select("distinct sp.payment_method")
.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today)
@@ -1202,8 +1351,41 @@ end
end end
end end
end end
end
def self.payment_sale(payment_method, today, current_user=nil) def self.payment_sale(payment_method, today, current_user=nil,from,to)
if !from.nil? && !to.nil?
if current_user.nil?
query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
if payment_method == 'card'
query = query.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? 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")',from,to)
else
query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today)
end
query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first()
else
if current_user.role == 'administrator'
query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
if payment_method == 'card'
query = query.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? 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")',from,to)
else
query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today)
end
query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first()
else
shift = ShiftSale.current_open_shift(current_user.id)
if !shift.nil?
query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
if payment_method == 'card'
query = query.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? 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") and sales.shift_sale_id=?',from,to,shift.id)
else
query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and sales.receipt_date between ? and ? and sales.shift_sale_id=?",from,to,shift.id)
end
query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first()
end
end
end
else
if current_user.nil? if current_user.nil?
query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
if payment_method == 'card' if payment_method == 'card'
@@ -1235,11 +1417,12 @@ end
end end
end end
end end
end
def self.total_customer(today,current_user=nil) def self.total_customer(today,current_user=nil,from,to)
dinein_cnt = self.total_dinein(today,current_user) dinein_cnt = self.total_dinein(today,current_user,from,to)
takeaway_cnt = self.total_takeaway(today,current_user) takeaway_cnt = self.total_takeaway(today,current_user,from,to)
membership_cnt = self.total_membership(today,current_user) membership_cnt = self.total_membership(today,current_user,from,to)
total_cus = 0 total_cus = 0
if !dinein_cnt.nil? || !takeaway_cnt.nil? || !membership_cnt.nil? if !dinein_cnt.nil? || !takeaway_cnt.nil? || !membership_cnt.nil?
@@ -1249,7 +1432,30 @@ end
return total_cus return total_cus
end end
def self.total_dinein(today,current_user=nil) def self.total_dinein(today,current_user=nil,from,to)
if !from.nil? && !to.nil?
if current_user.nil?
query = Sale.select("count(sales.customer_id) as total_dinein_cus")
.joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Dinein" and c.membership_id is null',from,to)
.first()
else
if current_user.role == 'administrator'
query = Sale.select("count(sales.customer_id) as total_dinein_cus")
.joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Dinein" and c.membership_id is null',from,to)
.first()
else
shift = ShiftSale.current_open_shift(current_user.id)
if !shift.nil?
query = Sale.select("count(sales.customer_id) as total_dinein_cus")
.joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Dinein" and c.membership_id is null and sales.shift_sale_id=?',from,to,shift.id)
.first()
end
end
end
else
if current_user.nil? if current_user.nil?
query = Sale.select("count(sales.customer_id) as total_dinein_cus") query = Sale.select("count(sales.customer_id) as total_dinein_cus")
.joins("JOIN customers as c ON c.customer_id = sales.customer_id") .joins("JOIN customers as c ON c.customer_id = sales.customer_id")
@@ -1272,8 +1478,32 @@ end
end end
end end
end end
end
def self.total_takeaway(today,current_user=nil) def self.total_takeaway(today,current_user=nil,from,to)
if !from.nil? && !to.nil?
if current_user.nil?
query = Sale.select("count(sales.customer_id) as total_take_cus")
.joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Takeaway" and c.membership_id is null',from,to)
.first()
else
if current_user.role == 'administrator'
query = Sale.select("count(sales.customer_id) as total_take_cus")
.joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Takeaway" and c.membership_id is null',from,to)
.first()
else
shift = ShiftSale.current_open_shift(current_user.id)
if !shift.nil?
query = Sale.select("count(sales.customer_id) as total_take_cus")
.joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Takeaway" and c.membership_id is null and sales.shift_sale_id=?',from,to,shift.id)
.first()
end
end
end
else
if current_user.nil? if current_user.nil?
query = Sale.select("count(sales.customer_id) as total_take_cus") query = Sale.select("count(sales.customer_id) as total_take_cus")
.joins("JOIN customers as c ON c.customer_id = sales.customer_id") .joins("JOIN customers as c ON c.customer_id = sales.customer_id")
@@ -1296,8 +1526,32 @@ end
end end
end end
end end
end
def self.total_membership(today,current_user=nil) def self.total_membership(today,current_user=nil,from,to)
if !from.nil? && !to.nil?
if current_user.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 sales.receipt_date between ? and ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',from,to)
.first()
else
if current_user.role == 'administrator'
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 sales.receipt_date between ? and ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',from,to)
.first()
else
shift = ShiftSale.current_open_shift(current_user.id)
if !shift.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 sales.receipt_date between ? and ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null)) and sales.shift_sale_id=?',from,to,shift.id)
.first()
end
end
end
else
if current_user.nil? if current_user.nil?
query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") query = Sale.select("count(distinct sales.customer_id) as total_memb_cus")
.joins("JOIN customers as c ON c.customer_id = sales.customer_id") .joins("JOIN customers as c ON c.customer_id = sales.customer_id")
@@ -1320,8 +1574,32 @@ end
end end
end end
end end
end
def self.total_other_customer(today,current_user=nil) def self.total_other_customer(today,current_user=nil,from,to)
if !from.nil? && !to.nil?
if current_user.nil?
query = Sale.select("count(sales.customer_id) as total_cus")
.joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type is null and c.membership_id is null',from,to)
.first()
else
if current_user.role == 'administrator'
query = Sale.select("count(sales.customer_id) as total_cus")
.joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type is null and c.membership_id is null',from,to)
.first()
else
shift = ShiftSale.current_open_shift(current_user.id)
if !shift.nil?
query = Sale.select("count(sales.customer_id) as total_cus")
.joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type is null and c.membership_id is null and sales.shift_sale_id=?',from,to,shift.id)
.first()
end
end
end
else
if current_user.nil? if current_user.nil?
query = Sale.select("count(sales.customer_id) as total_cus") query = Sale.select("count(sales.customer_id) as total_cus")
.joins("JOIN customers as c ON c.customer_id = sales.customer_id") .joins("JOIN customers as c ON c.customer_id = sales.customer_id")
@@ -1344,8 +1622,35 @@ end
end end
end end
end end
end
def self.total_order(today,current_user=nil) def self.total_order(today,current_user=nil,from,to)
if !from.nil? && !to.nil?
if current_user.nil?
query = Sale.select("count(distinct a.order_id) as total_order")
.joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id")
.joins("JOIN orders as b ON b.order_id = a.order_id")
.where('b.status = "billed" and sales.receipt_date between ? and ?',from,to)
.first()
else
if current_user.role == 'administrator'
query = Sale.select("count(distinct a.order_id) as total_order")
.joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id")
.joins("JOIN orders as b ON b.order_id = a.order_id")
.where('b.status = "billed" and sales.receipt_date between ? and ?',from,to)
.first()
else
shift = ShiftSale.current_open_shift(current_user.id)
if !shift.nil?
query = Sale.select("count(distinct a.order_id) as total_order")
.joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id")
.joins("JOIN orders as b ON b.order_id = a.order_id")
.where('b.status = "billed" and sales.receipt_date between ? and ? and sales.shift_sale_id=?',from,to,shift.id)
.first()
end
end
end
else
if current_user.nil? if current_user.nil?
query = Sale.select("count(distinct a.order_id) as total_order") query = Sale.select("count(distinct a.order_id) as total_order")
.joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id")
@@ -1371,8 +1676,32 @@ end
end end
end end
end end
end
def self.total_account(today,current_user=nil) def self.total_account(today,current_user=nil,from,to)
if !from.nil? && !to.nil?
if current_user.nil?
query = Sale.select("distinct b.id as account_id, b.title as title")
.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
.joins("JOIN accounts as b ON b.id = a.account_id")
.where('sales.sale_status = "completed" and sales.receipt_date between ? and ?',from,to)
else
if current_user.role == 'administrator'
query = Sale.select("distinct b.id as account_id, b.title as title")
.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
.joins("JOIN accounts as b ON b.id = a.account_id")
.where('sales.sale_status = "completed" and sales.receipt_date between ? and ?',from,to)
else
shift = ShiftSale.current_open_shift(current_user.id)
if !shift.nil?
query = Sale.select("distinct b.id as account_id, b.title as title")
.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
.joins("JOIN accounts as b ON b.id = a.account_id")
.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and sales.shift_sale_id=?',from,to,shift.id)
end
end
end
else
if current_user.nil? if current_user.nil?
query = Sale.select("distinct b.id as account_id, b.title as title") query = Sale.select("distinct b.id as account_id, b.title as title")
.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
@@ -1395,8 +1724,32 @@ end
end end
end end
end end
end
def self.account_data(account_id, today, current_user=nil) def self.account_data(account_id, today, current_user=nil,from,to)
if !from.nil? && !to.nil?
if current_user.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}' and sales.receipt_date between ? and ?",from,to)
.first()
else
if current_user.role == 'administrator'
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}' and sales.receipt_date between ? and ?",from,to)
.first()
else
shift = ShiftSale.current_open_shift(current_user.id)
if !shift.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}' and sales.receipt_date between ? and ? and sales.shift_sale_id=?",from,to,shift.id)
.first()
end
end
end
else
if current_user.nil? if current_user.nil?
query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") 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") .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
@@ -1419,8 +1772,38 @@ end
end end
end end
end end
end
def self.top_items(today,current_user=nil) def self.top_items(today,current_user=nil,from,to)
if !from.nil? && !to.nil?
if current_user.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' and sales.receipt_date between ? and ?",from,to)
.group("a.product_code")
.order("SUM(a.qty) DESC")
.first()
else
if current_user.role == 'administrator'
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' and sales.receipt_date between ? and ?",from,to)
.group("a.product_code")
.order("SUM(a.qty) DESC")
.first()
else
shift = ShiftSale.current_open_shift(current_user.id)
if !shift.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' and sales.receipt_date between ? and ? and sales.shift_sale_id=?",from,to,shift.id)
.group("a.product_code")
.order("SUM(a.qty) DESC")
.first()
end
end
end
else
if current_user.nil? if current_user.nil?
query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") 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") .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
@@ -1449,8 +1832,29 @@ end
end end
end end
end end
end
def self.total_foc_items(today,current_user=nil) def self.total_foc_items(today,current_user=nil,from,to)
if !from.nil? && !to.nil?
if current_user.nil?
query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
.where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and sales.receipt_date between ? and ?",from,to)
.sum("a.qty")
else
if current_user.role == 'administrator'
query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
.where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and sales.receipt_date between ? and ?",from,to)
.sum("a.qty")
else
shift = ShiftSale.current_open_shift(current_user.id)
if !shift.nil?
query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
.where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and sales.receipt_date between ? and ? and sales.shift_sale_id=?",from,to,shift.id)
.sum("a.qty")
end
end
end
else
if current_user.nil? if current_user.nil?
query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
.where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today)
@@ -1470,6 +1874,7 @@ end
end end
end end
end end
end
#card sale trans data #card sale trans data
def self.getCardSaleTrans(sale_id) def self.getCardSaleTrans(sale_id)

View File

@@ -36,12 +36,20 @@ class StockJournal < ApplicationRecord
journal.save journal.save
end end
def self.inventory_balances(today) def self.inventory_balances(today,from,to)
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")
.where("stock_journals.created_at between '#{from}' and '#{to}'")
.group("mii.item_instance_name")
.order("mii.item_instance_name ASC")
else
query = StockJournal.select("mii.item_instance_name as item_instance_name,balance") 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") .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}'") .where("DATE_FORMAT(stock_journals.created_at,'%Y-%m-%d') = '#{today}'")
.group("mii.item_instance_name") .group("mii.item_instance_name")
.order("mii.item_instance_name ASC") .order("mii.item_instance_name ASC")
end end
end
end end

View File

@@ -67,21 +67,23 @@
<!-- CPU Usage --> <!-- CPU Usage -->
<!-- Date range for dashboard --> <!-- Date range for dashboard -->
<!-- <div class="row clearfix"> <div class="row clearfix">
<div class="col-lg-4 col-md-4 col-sm-4"> <div class="col-lg-4 col-md-4 col-sm-4">
<label class="font-14"><%= t("views.right_panel.detail.from") %></label> <label class="font-14"><%= t("views.right_panel.detail.from") %></label>
<input data-behaviour='datepicker' class="form-control datepicker" name="from" id="from" type="text" placeholder="From date" style="height: 35px;"> <input data-behaviour='datepicker' class="form-control datepicker" name="from" id="from" type="text" value="<%= Time.now.utc.strftime('%d-%m-%Y') %>" placeholder="From date" style="height: 35px;">
<span id="fromErr" style="color:red;"></span>
</div> </div>
<div class="col-lg-4 col-md-4 col-sm-4"> <div class="col-lg-4 col-md-4 col-sm-4">
<label class="font-14"><%= t("views.right_panel.detail.to") %></label> <label class="font-14"><%= t("views.right_panel.detail.to") %></label>
<input data-behaviour='datepicker' class="form-control datepicker" name="to" id="to" type="text" placeholder="To date" style="height: 35px;"> <input data-behaviour='datepicker' class="form-control datepicker" name="to" id="to" type="text" value="<%= Time.now.utc.strftime('%d-%m-%Y') %>" placeholder="To date" style="height: 35px;">
<span id="toErr" style="color:red;"></span>
</div> </div>
<div class="col-lg-2 col-md-2 col-sm-2"> <div class="col-lg-2 col-md-2 col-sm-2">
<label></label><br> <label></label><br>
<input type="button" value="Generate" class='btn btn-primary btn_generate'> <input type="button" value="Generate" class='btn btn-primary btn_generate'>
</div> </div>
</div> </div>
<br> --> <br>
<!-- Date range for dashboard --> <!-- Date range for dashboard -->
<div class="row"> <div class="row">
@@ -316,3 +318,38 @@
<% end %> <% end %>
</div> </div>
</div> </div>
<script type="text/javascript">
$(document).ready(function(){
var from = '<%= @from.strftime('%d-%m-%Y') rescue '' %>';
var to = '<%= @to.strftime('%d-%m-%Y') rescue '' %>';
if((from!=undefined) && (from != null) && (from != '')){
$('#from').val(from);
}
if((to!=undefined) && (to != null) && (to != '')){
$('#to').val(to);
}
});
$('.btn_generate').on('click',function(){
var from = $("#from").val();
var to = $("#to").val();
if((from=='') && (to=='')){
$('#fromErr').html("can't be blank");
$('#toErr').html("can't be blank");
}else if((from!='') && (to=='')){
$('#fromErr').html("");
$('#toErr').html("can't be blank");
}else if((from=='') && (to!='')){
$('#fromErr').html("can't be blank");
$('#toErr').html("");
}else{
$('#fromErr').html("");
$('#toErr').html("");
}
if((from!='') && (to!='')){
window.location.href = '/dashboard?from='+from+'&to='+to;
}
});
</script>