reduce query in dashboard

This commit is contained in:
phyusin
2018-06-18 10:29:30 +06:30
parent 86b83125e6
commit b9a8912f01
5 changed files with 141 additions and 284 deletions

View File

@@ -110,8 +110,8 @@ class HomeController < ApplicationController
else else
@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()
end end
@top_products = Sale.top_products(today,current_user,@from,@to,@from_time,@to_time).sum('i.qty') @top_products = Sale.top_bottom_products(today,current_user,@from,@to,@from_time,@to_time,"top").sum('i.qty')
@bottom_products = Sale.bottom_products(today,current_user,@from,@to,@from_time,@to_time).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) @hourly_sales = Sale.hourly_sales(today,current_user,@from,@to,@from_time,@to_time).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)
@@ -137,12 +137,9 @@ class HomeController < ApplicationController
end end
end end
@summ_sale = Sale.summary_sale_receipt(today,current_user,@from,@to,@from_time,@to_time) @summ_sale = Sale.summary_sale_receipt(today,current_user,@from,@to,@from_time,@to_time)
@total_customer = Sale.total_customer(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)
@total_dinein = Sale.total_dinein(today,current_user,@from,@to,@from_time,@to_time) # @total_other_customer = Sale.total_other_customer(today,current_user)
@total_takeaway = Sale.total_takeaway(today,current_user,@from,@to,@from_time,@to_time)
@total_other_customer = Sale.total_other_customer(today,current_user,@from,@to,@from_time,@to_time)
@total_membership = Sale.total_membership(today,current_user,@from,@to,@from_time,@to_time)
@total_order = Sale.total_order(today,current_user,@from,@to,@from_time,@to_time) @total_order = Sale.total_order(today,current_user,@from,@to,@from_time,@to_time)
@total_accounts = Sale.total_account(today,current_user,@from,@to,@from_time,@to_time) @total_accounts = Sale.total_account(today,current_user,@from,@to,@from_time,@to_time)
@account_data = Array.new @account_data = Array.new

View File

@@ -3,26 +3,7 @@ class Origami::DashboardController < BaseOrigamiController
def index def index
@shop = Shop.first @shop = Shop.first
today = DateTime.now.strftime('%Y-%m-%d') today = DateTime.now.strftime('%Y-%m-%d')
# @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()
# @top_products = Sale.top_products(today).sum('i.qty')
# @bottom_products = Sale.bottom_products(today).sum('i.qty')
# @hourly_sales = Sale.hourly_sales(today).sum(:grand_total)
# .group_by_hour(:created_at, :time_zone => 'Asia/Rangoon',format: '%I:%p')
# .sum(:grand_total)
# @employee_sales = Sale.employee_sales(today)
# .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)
# @total_sale = Sale.total_sale(today,current_user)
# @total_count = Sale.total_count(today,current_user)
# @total_card = Sale.total_card_sale(today,current_user)
# @total_credit = Sale.credit_payment(today,current_user)
@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)
if !@total_payment_methods.nil? if !@total_payment_methods.nil?
@@ -39,11 +20,8 @@ class Origami::DashboardController < BaseOrigamiController
@sale_data = nil @sale_data = nil
end end
@summ_sale = Sale.summary_sale_receipt(today,current_user) @summ_sale = Sale.summary_sale_receipt(today,current_user)
@total_customer = Sale.total_customer(today,current_user) @total_customer, @total_dinein, @total_takeaway, @total_membership = Sale.total_customer(today,current_user,@from,@to,@from_time,@to_time)
@total_dinein = Sale.total_dinein(today,current_user) # @total_other_customer = Sale.total_other_customer(today,current_user)
@total_takeaway = Sale.total_takeaway(today,current_user)
@total_other_customer = Sale.total_other_customer(today,current_user)
@total_membership = Sale.total_membership(today,current_user)
@total_order = Sale.total_order(today,current_user) @total_order = Sale.total_order(today,current_user)
@total_accounts = Sale.total_account(today,current_user) @total_accounts = Sale.total_account(today,current_user)

View File

@@ -1256,7 +1256,7 @@ end
return tax return tax
end end
def self.top_products(today,current_user,from,to,from_time,to_time) def self.top_bottom_products(today,current_user,from,to,from_time,to_time,type)
if !from.nil? && !to.nil? if !from.nil? && !to.nil?
if current_user.nil? if current_user.nil?
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," +
@@ -1269,8 +1269,13 @@ end
query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+ query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+
" and sale_status= 'completed'") " and sale_status= 'completed'")
end end
query = query.group('mi.name') if type == "top"
query = query.group('mi.name')
.order("SUM(i.qty) DESC").limit(20) .order("SUM(i.qty) DESC").limit(20)
elsif type == "bottom"
query = query.group('mi.name')
.order("SUM(i.qty) ASC").limit(20)
end
else else
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account'
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," +
@@ -1283,8 +1288,13 @@ end
query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+ query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+
" and sale_status= 'completed'") " and sale_status= 'completed'")
end end
query = query.group('mi.name') if type == "top"
query = query.group('mi.name')
.order("SUM(i.qty) DESC").limit(20) .order("SUM(i.qty) DESC").limit(20)
elsif type == "bottom"
query = query.group('mi.name')
.order("SUM(i.qty) ASC").limit(20)
end
else else
shift = ShiftSale.current_open_shift(current_user.id) shift = ShiftSale.current_open_shift(current_user.id)
if !shift.nil? if !shift.nil?
@@ -1298,8 +1308,13 @@ end
query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+ query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+
" and sale_status= 'completed' and shift_sale_id='#{shift.id}'") " and sale_status= 'completed' and shift_sale_id='#{shift.id}'")
end end
query = query.group('mi.name') if type == "top"
query = query.group('mi.name')
.order("SUM(i.qty) DESC").limit(20) .order("SUM(i.qty) DESC").limit(20)
elsif type == "bottom"
query = query.group('mi.name')
.order("SUM(i.qty) ASC").limit(20)
end
end end
end end
end end
@@ -1311,7 +1326,11 @@ end
.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+ .where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+
" and sale_status= 'completed'") " and sale_status= 'completed'")
.group('mi.name') .group('mi.name')
.order("SUM(i.qty) DESC").limit(20) if type == "top"
query = query.order("SUM(i.qty) DESC").limit(20)
elsif type == "bottom"
query = query.order("SUM(i.qty) ASC").limit(20)
end
else else
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account'
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," +
@@ -1320,7 +1339,11 @@ end
.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+ .where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+
" and sale_status= 'completed'") " and sale_status= 'completed'")
.group('mi.name') .group('mi.name')
.order("SUM(i.qty) DESC").limit(20) if type == "top"
query = query.order("SUM(i.qty) DESC").limit(20)
elsif type == "bottom"
query = query.order("SUM(i.qty) ASC").limit(20)
end
else else
shift = ShiftSale.current_open_shift(current_user.id) shift = ShiftSale.current_open_shift(current_user.id)
if !shift.nil? if !shift.nil?
@@ -1330,88 +1353,11 @@ end
.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+ .where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+
" and sale_status= 'completed' and shift_sale_id='#{shift.id}'") " and sale_status= 'completed' and shift_sale_id='#{shift.id}'")
.group('mi.name') .group('mi.name')
.order("SUM(i.qty) DESC").limit(20) if type == "top"
end query = query.order("SUM(i.qty) DESC").limit(20)
end elsif type == "bottom"
end query = query.order("SUM(i.qty) ASC").limit(20)
end end
end
def self.bottom_products(today,current_user,from,to,from_time,to_time)
if !from.nil? && !to.nil?
if current_user.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")
if !from_time.nil? && !to_time.nil?
query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+
" and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}' and sale_status= 'completed'")
else
query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+
" and sale_status= 'completed'")
end
query = query.group('mi.name')
.order("SUM(i.qty) ASC").limit(20)
else
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account'
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")
if !from_time.nil? && !to_time.nil?
query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+
" and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}' and sale_status= 'completed'")
else
query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+
" and sale_status= 'completed'")
end
query = query.group('mi.name')
.order("SUM(i.qty) ASC").limit(20)
else
shift = ShiftSale.current_open_shift(current_user.id)
if !shift.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")
if !from_time.nil? && !to_time.nil?
query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+
" and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}' and sale_status= 'completed' and shift_sale_id='#{shift.id}'")
else
query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30') between '#{from}' and '#{to}'"+
" and sale_status= 'completed' and shift_sale_id='#{shift.id}'")
end
query = query.group('mi.name')
.order("SUM(i.qty) ASC").limit(20)
end
end
end
else
if current_user.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 DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+
"and sale_status= 'completed'")
.group('mi.name')
.order("SUM(i.qty) ASC").limit(20)
else
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account'
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 DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+
"and sale_status= 'completed'")
.group('mi.name')
.order("SUM(i.qty) ASC").limit(20)
else
shift = ShiftSale.current_open_shift(current_user.id)
if !shift.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 DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+
" and sale_status= 'completed' and shift_sale_id=#{shift.id}")
.group('mi.name')
.order("SUM(i.qty) ASC").limit(20)
end end
end end
end end
@@ -1867,57 +1813,62 @@ end
end end
def self.total_customer(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) def self.total_customer(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
dinein_cnt = self.total_dinein(today,current_user,from,to,from_time,to_time) total_dinein_takeaway = self.total_dinein_takeaway(today,current_user,from,to,from_time,to_time)
takeaway_cnt = self.total_takeaway(today,current_user,from,to,from_time,to_time) dinein_cnt = 0
takeaway_cnt = 0
if !total_dinein_takeaway[0].nil?
dinein_cnt = total_dinein_takeaway[0].total_dinein_cus
takeaway_cnt = total_dinein_takeaway[0].total_take_cus
end
membership_cnt = self.total_membership(today,current_user,from,to,from_time,to_time) membership_cnt = self.total_membership(today,current_user,from,to,from_time,to_time)
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?
total_cus = dinein_cnt.total_dinein_cus.to_int + takeaway_cnt.total_take_cus.to_int + membership_cnt.total_memb_cus.to_int total_cus = dinein_cnt.to_int + takeaway_cnt.to_int + membership_cnt.total_memb_cus.to_int
end end
return total_cus return total_cus, dinein_cnt, takeaway_cnt, membership_cnt.total_memb_cus
end end
def self.total_dinein(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) def self.total_dinein_takeaway(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
if !from.nil? && !to.nil? if !from.nil? && !to.nil?
if current_user.nil? if current_user.nil?
if !from_time.nil? && !to_time.nil? if !from_time.nil? && !to_time.nil?
query = Sale.select("count(sales.customer_id) as total_dinein_cus") 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") .joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and c.customer_type = "Dinein" and c.membership_id is null',from,to,from_time,to_time) .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and c.membership_id is null',from,to,from_time,to_time)
.first() .first()
else else
query = Sale.select("count(sales.customer_id) as total_dinein_cus") 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") .joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and c.customer_type = "Dinein" and c.membership_id is null',from,to) .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and c.membership_id is null',from,to)
.first() .first()
end end
else else
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account'
if !from_time.nil? && !to_time.nil? if !from_time.nil? && !to_time.nil?
query = Sale.select("count(sales.customer_id) as total_dinein_cus") 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") .joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and c.customer_type = "Dinein" and c.membership_id is null',from,to,from_time,to_time) .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and c.membership_id is null',from,to,from_time,to_time)
.first() .first()
else else
query = Sale.select("count(sales.customer_id) as total_dinein_cus") 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") .joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and c.customer_type = "Dinein" and c.membership_id is null',from,to) .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and c.membership_id is null',from,to)
.first() .first()
end end
else else
shift = ShiftSale.current_open_shift(current_user.id) shift = ShiftSale.current_open_shift(current_user.id)
if !shift.nil? if !shift.nil?
if !from_time.nil? && !to_time.nil? if !from_time.nil? && !to_time.nil?
query = Sale.select("count(sales.customer_id) as total_dinein_cus") 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") .joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and c.customer_type = "Dinein" and c.membership_id is null and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and c.membership_id is null and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id)
.first() .first()
else else
query = Sale.select("count(sales.customer_id) as total_dinein_cus") 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") .joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and c.customer_type = "Dinein" and c.membership_id is null and sales.shift_sale_id=?',from,to,shift.id) .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and c.membership_id is null and sales.shift_sale_id=?',from,to,shift.id)
.first() .first()
end end
end end
@@ -1925,22 +1876,22 @@ end
end end
else else
if current_user.nil? if current_user.nil?
query = Sale.select("count(sales.customer_id) as total_dinein_cus") 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") .joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null',today) .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and c.membership_id is null',today)
.first() .first()
else else
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account'
query = Sale.select("count(sales.customer_id) as total_dinein_cus") 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") .joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null',today) .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and c.membership_id is null',today)
.first() .first()
else else
shift = ShiftSale.current_open_shift(current_user.id) shift = ShiftSale.current_open_shift(current_user.id)
if !shift.nil? if !shift.nil?
query = Sale.select("count(sales.customer_id) as total_dinein_cus") 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") .joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null and sales.shift_sale_id=?',today,shift.id) .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and c.membership_id is null and sales.shift_sale_id=?',today,shift.id)
.first() .first()
end end
end end
@@ -1948,75 +1899,6 @@ end
end end
end end
def self.total_takeaway(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
if !from.nil? && !to.nil?
if current_user.nil?
if !from_time.nil? && !to_time.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 DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZsales.receipt_date,"%H:%i") between ? and ? and c.customer_type = "Takeaway" and c.membership_id is null',from,to,from_time,to_time)
.first()
else
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 DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and c.customer_type = "Takeaway" and c.membership_id is null',from,to)
.first()
end
else
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account'
if !from_time.nil? && !to_time.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 DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and c.customer_type = "Takeaway" and c.membership_id is null',from,to,from_time,to_time)
.first()
else
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 DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and c.customer_type = "Takeaway" and c.membership_id is null',from,to)
.first()
end
else
shift = ShiftSale.current_open_shift(current_user.id)
if !shift.nil?
if !from_time.nil? && !to_time.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 DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and c.customer_type = "Takeaway" and c.membership_id is null and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id)
.first()
else
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 DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") 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
end
else
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 DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null',today)
.first()
else
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account'
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 DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null',today)
.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 DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null and sales.shift_sale_id=?',today,shift.id)
.first()
end
end
end
end
end
def self.total_membership(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) def self.total_membership(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
if !from.nil? && !to.nil? if !from.nil? && !to.nil?
if current_user.nil? if current_user.nil?
@@ -2082,68 +1964,68 @@ end
end end
end end
def self.total_other_customer(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) # def self.total_other_customer(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
if !from.nil? && !to.nil? # if !from.nil? && !to.nil?
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")
if !from_time.nil? && !to_time.nil? # if !from_time.nil? && !to_time.nil?
query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and c.customer_type is null and c.membership_id is null',from,to,from_time,to_time) # query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and c.customer_type is null and c.membership_id is null',from,to,from_time,to_time)
.first() # .first()
else # else
query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and c.customer_type is null and c.membership_id is null',from,to) # query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and c.customer_type is null and c.membership_id is null',from,to)
.first() # .first()
end # end
else # else
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' # if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account'
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")
if !from_time.nil? && !to_time.nil? # if !from_time.nil? && !to_time.nil?
query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and c.customer_type is null and c.membership_id is null',from,to,from_time,to_time) # query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and c.customer_type is null and c.membership_id is null',from,to,from_time,to_time)
.first() # .first()
else # else
query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and c.customer_type is null and c.membership_id is null',from,to) # query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and c.customer_type is null and c.membership_id is null',from,to)
.first() # .first()
end # end
else # else
shift = ShiftSale.current_open_shift(current_user.id) # shift = ShiftSale.current_open_shift(current_user.id)
if !shift.nil? # if !shift.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")
if !from_time.nil? && !to_time.nil? # if !from_time.nil? && !to_time.nil?
query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and c.customer_type is null and c.membership_id is null and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) # query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and c.customer_type is null and c.membership_id is null and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id)
.first() # .first()
else # else
query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and c.customer_type is null and c.membership_id is null and sales.shift_sale_id=?',from,to,shift.id) # query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and c.customer_type is null and c.membership_id is null and sales.shift_sale_id=?',from,to,shift.id)
.first() # .first()
end # end
end # end
end # end
end # end
else # 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")
.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null',today) # .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null',today)
.first() # .first()
else # else
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' # if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account'
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")
.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null',today) # .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null',today)
.first() # .first()
else # else
shift = ShiftSale.current_open_shift(current_user.id) # shift = ShiftSale.current_open_shift(current_user.id)
if !shift.nil? # if !shift.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")
.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null and sales.shift_sale_id=?',today,shift.id) # .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null and sales.shift_sale_id=?',today,shift.id)
.first() # .first()
end # end
end # end
end # end
end # end
end # end
def self.total_order(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) def self.total_order(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
if !from.nil? && !to.nil? if !from.nil? && !to.nil?

View File

@@ -280,25 +280,25 @@
<% if !@total_dinein.nil? %> <% if !@total_dinein.nil? %>
<tr> <tr>
<td><%= t("views.right_panel.detail.dine_in") %> : </td> <td><%= t("views.right_panel.detail.dine_in") %> : </td>
<td align="right"><%= @total_dinein.total_dinein_cus %></td> <td align="right"><%= @total_dinein %></td>
</tr> </tr>
<% end %> <% end %>
<% if !@total_takeaway.nil? %> <% if !@total_takeaway.nil? %>
<tr> <tr>
<td><%= t("views.right_panel.detail.takeaway") %> : </td> <td><%= t("views.right_panel.detail.takeaway") %> : </td>
<td align="right"><%= @total_takeaway.total_take_cus %></td> <td align="right"><%= @total_takeaway %></td>
</tr> </tr>
<% end %> <% end %>
<% if !@total_other_customer.nil? %> <!-- <% if !@total_other_customer.nil? %>
<tr> <tr>
<td><%= t :customer %> : </td> <td><%= t :customer %> : </td>
<td align="right"><%= @total_other_customer.total_cus %></td> <td align="right"><%= @total_other_customer.total_cus %></td>
</tr> </tr>
<% end %> <% end %> -->
<% if !@total_membership.nil? %> <% if !@total_membership.nil? %>
<tr> <tr>
<td><%= t("views.right_panel.detail.membership") %> : </td> <td><%= t("views.right_panel.detail.membership") %> : </td>
<td align="right"><%= @total_membership.total_memb_cus %></td> <td align="right"><%= @total_membership %></td>
</tr> </tr>
<% end %> <% end %>
</table> </table>

View File

@@ -137,25 +137,25 @@
<% if !@total_dinein.nil? %> <% if !@total_dinein.nil? %>
<tr> <tr>
<td><%= t("views.right_panel.detail.dine_in") %> : </td> <td><%= t("views.right_panel.detail.dine_in") %> : </td>
<td align="right"><%= @total_dinein.total_dinein_cus %></td> <td align="right"><%= @total_dinein %></td>
</tr> </tr>
<% end %> <% end %>
<% if !@total_takeaway.nil? %> <% if !@total_takeaway.nil? %>
<tr> <tr>
<td><%= t("views.right_panel.detail.takeaway") %> : </td> <td><%= t("views.right_panel.detail.takeaway") %> : </td>
<td align="right"><%= @total_takeaway.total_take_cus %></td> <td align="right"><%= @total_takeaway %></td>
</tr> </tr>
<% end %> <% end %>
<% if !@total_other_customer.nil? %> <!-- <% if !@total_other_customer.nil? %>
<tr> <tr>
<td><%= t :customer %> : </td> <td><%= t :customer %> : </td>
<td align="right"><%= @total_other_customer.total_cus %></td> <td align="right"><%= @total_other_customer.total_cus %></td>
</tr> </tr>
<% end %> <% end %> -->
<% if !@total_membership.nil? %> <% if !@total_membership.nil? %>
<tr> <tr>
<td><%= t("views.right_panel.detail.membership") %> : </td> <td><%= t("views.right_panel.detail.membership") %> : </td>
<td align="right"><%= @total_membership.total_memb_cus %></td> <td align="right"><%= @total_membership %></td>
</tr> </tr>
<% end %> <% end %>
</table> </table>