diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb
index 25fb0d70..97ac4051 100755
--- a/app/controllers/home_controller.rb
+++ b/app/controllers/home_controller.rb
@@ -62,57 +62,59 @@ class HomeController < ApplicationController
end
def dashboard
+ @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' 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)
+ @top_products = Sale.top_products(today,@from,@to).sum('i.qty')
+ @bottom_products = Sale.bottom_products(today,@from,@to).sum('i.qty')
+ @hourly_sales = Sale.hourly_sales(today,@from,@to).sum(:grand_total)
# .group_by_hour(:created_at, :time_zone => 'Asia/Rangoon',format: '%I:%p')
# .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)')
- @inventories = StockJournal.inventory_balances(today).sum(:balance)
+ @inventories = StockJournal.inventory_balances(today,@from,@to).sum(:balance)
- @total_sale = Sale.total_sale(today)
- @total_count = Sale.total_count(today)
- @total_card = Sale.total_card_sale(today)
- @total_credit = Sale.credit_payment(today)
+ @total_sale = Sale.total_sale(today,current_user,@from,@to)
+ @total_count = Sale.total_count(today,current_user,@from,@to)
+ @total_card = Sale.total_card_sale(today,current_user,@from,@to)
+ @total_credit = Sale.credit_payment(today,current_user,@from,@to)
@sale_data = Array.new
- @total_payment_methods = Sale.total_payment_methods(today)
+ @total_payment_methods = Sale.total_payment_methods(today,current_user,@from,@to)
@total_payment_methods.each do |payment|
if payment.payment_method == "mpu" || payment.payment_method == "visa" || payment.payment_method == "master" || payment.payment_method == "jcb"
- pay = Sale.payment_sale('card', today)
+ pay = Sale.payment_sale('card', today, current_user,@from,@to)
@sale_data.push({'card' => pay.payment_amount})
else
- pay = Sale.payment_sale(payment.payment_method, today)
+ pay = Sale.payment_sale(payment.payment_method, today, current_user,@from,@to)
@sale_data.push({payment.payment_method => pay.payment_amount})
end
end
- @summ_sale = Sale.summary_sale_receipt(today)
- @total_customer = Sale.total_customer(today)
- @total_dinein = Sale.total_dinein(today)
- @total_takeaway = Sale.total_takeaway(today)
- @total_other_customer = Sale.total_other_customer(today)
- @total_membership = Sale.total_membership(today)
+ @summ_sale = Sale.summary_sale_receipt(today,current_user,@from,@to)
+ @total_customer = Sale.total_customer(today,current_user,@from,@to)
+ @total_dinein = Sale.total_dinein(today,current_user,@from,@to)
+ @total_takeaway = Sale.total_takeaway(today,current_user,@from,@to)
+ @total_other_customer = Sale.total_other_customer(today,current_user,@from,@to)
+ @total_membership = Sale.total_membership(today,current_user,@from,@to)
- @total_order = Sale.total_order(today)
- @total_accounts = Sale.total_account(today)
+ @total_order = Sale.total_order(today,current_user,@from,@to)
+ @total_accounts = Sale.total_account(today,current_user,@from,@to)
@account_data = Array.new
@total_accounts.each do |account|
- acc = Sale.account_data(account.account_id, today)
+ acc = Sale.account_data(account.account_id, today,current_user,@from,@to)
if !acc.nil?
@account_data.push({account.title => acc.cnt_acc, account.title + '_amount' => acc.total_acc})
end
end
- @top_items = Sale.top_items(today)
- @total_foc_items = Sale.total_foc_items(today)
+ @top_items = Sale.top_items(today,current_user,@from,@to)
+ @total_foc_items = Sale.total_foc_items(today,current_user,@from,@to)
# get printer info
@print_settings = PrintSetting.get_precision_delimiter()
@@ -162,4 +164,20 @@ class HomeController < ApplicationController
redirect_to reports_dailysale_index_path
end
end
+
+ def get_date_range_from_params
+ from = params[:from]
+ to = params[:to]
+
+ 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
+ end
+
+ return from, to
+ end
end
diff --git a/app/controllers/reports/product_sale_controller.rb b/app/controllers/reports/product_sale_controller.rb
new file mode 100755
index 00000000..748c8716
--- /dev/null
+++ b/app/controllers/reports/product_sale_controller.rb
@@ -0,0 +1,18 @@
+class Reports::ProductSaleController < BaseReportController
+ authorize_resource :class => false
+
+ def index
+ @order_by = params[:order_by]
+
+ @sale_data = Sale.get_menu_item_query(@order_by)
+
+ # get printer info
+ @print_settings = PrintSetting.get_precision_delimiter()
+
+ respond_to do |format|
+ format.html
+ format.json
+ format.xls
+ end
+ end
+end
diff --git a/app/models/sale.rb b/app/models/sale.rb
index f42b62a0..5196672c 100755
--- a/app/models/sale.rb
+++ b/app/models/sale.rb
@@ -78,14 +78,29 @@ class Sale < ApplicationRecord
self.tax_type = "exclusive"
# set cashier
- if cashier != nil
+ open_cashier = Employee.where("role = 'cashier' AND token_session <> ''")
+ current_shift = ShiftSale.current_shift
+ shift = ShiftSale.current_open_shift(cashier.id)
+
+ # set cashier
+ if shift != nil
self.cashier_id = cashier.id
self.cashier_name = cashier.name
+ self.shift_sale_id = shift.id
else
- self.cashier_id = requested_by.id
- self.cashier_name = requested_by.name
+ if open_cashier.count>0
+ self.cashier_id = open_cashier[0].id
+ self.cashier_name = open_cashier[0].name
+ shift_id = ShiftSale.current_open_shift(open_cashier[0].id)
+ self.shift_sale_id = current_shift.id
+ else
+ self.cashier_id = current_shift.employee_id
+ self.cashier_name = Employee.find(current_shift.employee_id).name
+ self.shift_sale_id = current_shift.id
+ end
end
+
# set waiter
self.requested_by = requested_by.name
@@ -212,7 +227,7 @@ class Sale < ApplicationRecord
sale_item.product_name = item.item_name
sale_item.product_alt_name = item.alt_name
sale_item.account_id = item.account_id
- sale_item.remark = item.remark
+ sale_item.status = item.remark
sale_item.qty = item.qty
sale_item.unit_price = item.price
@@ -240,7 +255,7 @@ class Sale < ApplicationRecord
sale_item.product_name = instance.item_instance_name
sale_item.product_alt_name = menu_item.alt_name
sale_item.account_id = menu_item.account_id
- sale_item.remark = nil
+ sale_item.status = nil
sale_item.qty = item["quantity"]
sale_item.unit_price = item["price"]
@@ -366,7 +381,7 @@ class Sale < ApplicationRecord
rounding_adjustment = 0
sales_items.each do |item|
- if item.remark != 'void' && item.remark != 'foc'
+ if item.status != 'void' && item.status != 'foc'
#compute each item and added to total
subtotal_price = subtotal_price + item.price
@@ -402,35 +417,36 @@ class Sale < ApplicationRecord
tax_profiles = TaxProfile.all.order("order_by asc")
customer = Customer.find(sale.customer_id)
# #Creat new tax records
- tax_profiles.each do |tax|
- customer.tax_profiles.each do |cus_tax|
- if cus_tax.to_i == tax.id
- sale_tax = SaleTax.new(:sale => sale)
- sale_tax.tax_name = tax.name
- sale_tax.tax_rate = tax.rate
+ if sale.payment_status != 'foc'
+ tax_profiles.each do |tax|
+ customer.tax_profiles.each do |cus_tax|
+ if cus_tax.to_i == tax.id
+ sale_tax = SaleTax.new(:sale => sale)
+ sale_tax.tax_name = tax.name
+ sale_tax.tax_rate = tax.rate
- # substract , to give after discount
- total_tax = total_taxable - total_discount
- #include or execulive
- if tax.inclusive
- rate = tax.rate
- divided_value = (100 + rate)/rate
- sale_tax.tax_payable_amount = total_tax / divided_value
- else
- sale_tax.tax_payable_amount = total_tax * tax.rate / 100
- total_tax_amount = total_tax_amount + sale_tax.tax_payable_amount
- end
- #new taxable amount is standard rule for step by step
- if shop.calc_tax_order
- total_taxable = total_taxable + sale_tax.tax_payable_amount
- end
+ # substract , to give after discount
+ total_tax = total_taxable - total_discount
+ #include or execulive
+ if tax.inclusive
+ rate = tax.rate
+ divided_value = (100 + rate)/rate
+ sale_tax.tax_payable_amount = total_tax / divided_value
+ else
+ sale_tax.tax_payable_amount = total_tax * tax.rate / 100
+ total_tax_amount = total_tax_amount + sale_tax.tax_payable_amount
+ end
+ #new taxable amount is standard rule for step by step
+ if shop.calc_tax_order
+ total_taxable = total_taxable + sale_tax.tax_payable_amount
+ end
- sale_tax.inclusive = tax.inclusive
- sale_tax.save
+ sale_tax.inclusive = tax.inclusive
+ sale_tax.save
+ end
end
end
end
-
sale.total_tax = total_tax_amount
end
@@ -742,8 +758,8 @@ end
def self.get_item_query()
query = Sale.select("acc.title as account_name,mi.account_id, i.item_instance_code as item_code,i.account_id as account_id, " +
"SUM(i.qty * i.unit_price) as grand_total,SUM(i.qty) as total_item,i.qty as qty," +
- "i.remark as status_type,"+
- " mii.price as unit_price,i.price as price,i.product_name as product_name, mc.name as" +
+ "i.status as status_type,"+
+ " i.unit_price,i.price as price,i.product_name as product_name, mc.name as" +
" menu_category_name,mc.id as menu_category_id ")
query = query.joins("JOIN sale_items i ON i.sale_id = sales.sale_id" +
@@ -754,14 +770,14 @@ def self.get_item_query()
# "JOIN employee_accesses ea ON ea.`employee_id` = sales.cashier_id ")
query = query.joins(" JOIN accounts acc ON acc.id = mi.account_id")
# query = query.where("i.item_instance_code IS NOT NULL")
- query = query.group("acc.title,mi.account_id,mi.menu_category_id,i.product_name")
+ query = query.group("acc.title,mi.account_id,mi.menu_category_id,i.product_name,i.unit_price")
.order("acc.title desc, mi.account_id desc, mi.menu_category_id desc, i.unit_price asc")
end
def self.get_other_charges()
query = Sale.select("i.account_id as account_id, " +
"SUM(i.qty * i.unit_price) as grand_total,SUM(i.qty) as total_item," +
- "i.remark as status_type,"+
+ "i.status as status_type,"+
" i.unit_price as unit_price,i.product_name as product_name")
query = query.joins("JOIN sale_items i ON i.sale_id = sales.sale_id")
query = query.where("i.item_instance_code IS NULL AND i.product_code = 'Other Charges'")
@@ -770,7 +786,6 @@ end
def self.get_by_shift_items(shift_sale_range, shift, from, to, status)
# date_type_selection = get_sql_function_for_report_type(report_type)
-
query = self.get_item_query()
discount_query = 0
@@ -846,6 +861,27 @@ def self.get_by_shift_items(shift_sale_range, shift, from, to, status)
return query,other_charges, discount_query , total_cash_amount , total_card_amount , total_credit_amount , total_foc_amount , total_grand_total , change_amount
end
+#product sale report query
+def self.get_menu_item_query(order_by)
+ query = MenuItem.unscoped.select("acc.id as account_id,
+ acc.title as account_name,
+ mii.item_instance_code as item_code, " +
+ "(CASE WHEN si.qty > 0 THEN SUM(si.qty) ELSE 0 END) as total_item," +
+ "(CASE WHEN si.unit_price > 0 THEN si.unit_price ELSE mii.price END) as unit_price," +
+ "(CASE WHEN (si.qty * si.unit_price) > 0 THEN (si.qty * si.unit_price) ELSE 0 END) as grand_total," +
+ "mii.price as unit_price, menu_items.name as product_name,
+ mc.name as" +
+ " menu_category_name,mc.id as menu_category_id, si.remark as status_type, si.price as price ")
+ .joins(" LEFT JOIN menu_item_instances mii ON menu_items.id = mii.menu_item_id" +
+ " LEFT JOIN menu_categories mc ON mc.id = menu_items.menu_category_id" +
+ " LEFT JOIN accounts acc ON acc.id = menu_items.account_id" +
+ " LEFT JOIN sale_items si ON si.item_instance_code = mii.item_instance_code" +
+ " LEFT JOIN sales s ON s.sale_id = si.sale_id")
+ .group("mc.id, menu_items.name, si.qty, (CASE WHEN si.unit_price > 0 THEN si.unit_price ELSE mii.price END)")
+ .order("si.qty #{order_by}, menu_items.menu_category_id #{order_by}")
+end
+#product sale report query
+
def self.get_shift_sales_by_receipt_no(shift_sale_range,shift,from,to,payment_type)
## => left join -> show all sales although no orders
if payment_type.blank?
@@ -1053,95 +1089,376 @@ end
return tax
end
- def self.top_products(today)
- query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," +
+ 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," +
" 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 payment_status='paid' and sale_status= 'completed'")
.group('mi.name')
- .order("SUM(i.qty) DESC").limit(10)
+ .order("SUM(i.qty) DESC").limit(20)
+ end
end
- def self.bottom_products(today)
- query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," +
+ 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," +
" 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 payment_status='paid' and sale_status= 'completed'")
.group('mi.name')
- .order("SUM(i.qty) ASC").limit(10)
+ .order("SUM(i.qty) ASC").limit(20)
+ end
end
- def self.hourly_sales(today)
- query= Sale.select("grand_total")
+ 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")
.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')")
.order('receipt_date')
+ end
end
- def self.employee_sales(today)
- query = Sale.joins("JOIN employees as e on e.id=sales.cashier_id")
+ 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")
.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}'")
.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")
+ end
end
- def self.total_sale(today)
- total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).sum("grand_total")
-
- end
-
- def self.total_count(today)
- total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).count
- end
-
- def self.total_card_sale(today)
- payment_type = " and payment_method = 'mpu' or payment_method = 'visa' or payment_method = 'master' or payment_method = 'jcb' or payment_method = 'unionpay' "
- # query = Sale.select("SUM(tax_payable_amount) AS st_amount,tax_name")
- # .where('sale_status = "completed" #{payment_type} and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today)
- # .joins("join sale_payments on sale_id = sales.sale_id")
- # .group("sales.sale_id")
-
- 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")
- .sum("sp.payment_amount")
-
- end
-
- def self.credit_payment(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")
- .sum("payment_amount")
- end
-
- def self.summary_sale_receipt(today)
- 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)
- .first()
- end
-
- def self.total_payment_methods(today)
- query = Sale.select("distinct sp.payment_method")
- .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today)
- .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
- end
-
- def self.payment_sale(payment_method, today)
- 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 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)
- else
- query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today)
+ 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
- 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
+ else
+ if current_user.nil?
+ total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).sum("grand_total")
+ else
+ if current_user.role == 'administrator'
+ total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).sum("grand_total")
+ else
+ shift = ShiftSale.current_open_shift(current_user.id)
+ if !shift.nil?
+ total = Sale.where('sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and shift_sale_id=?',today,shift.id)
+ .sum("grand_total")
+ end
+ end
+ end
+ end
end
- def self.total_customer(today)
- dinein_cnt = self.total_dinein(today)
- takeaway_cnt = self.total_takeaway(today)
- membership_cnt = self.total_membership(today)
+ 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?
+ total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).count
+ else
+ if current_user.role == 'administrator'
+ total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).count
+ else
+ shift = ShiftSale.current_open_shift(current_user.id)
+ if !shift.nil?
+ total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ? and shift_sale_id = ?',today,shift.id).count
+ end
+ end
+ end
+ end
+ end
+
+ 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?
+ 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")
+ .sum("sp.payment_amount")
+ else
+ if current_user.role == 'administrator'
+ 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")
+ .sum("sp.payment_amount")
+ else
+ shift = ShiftSale.current_open_shift(current_user.id)
+ if !shift.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") and shift_sale_id=?',today,shift.id)
+ .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id")
+ .sum("sp.payment_amount")
+ end
+ end
+ end
+ end
+ end
+
+ 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?
+ 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")
+ .sum("payment_amount")
+ else
+ if current_user.role == 'administrator'
+ 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")
+ .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 DATE_FORMAT(s.receipt_date,"%Y-%m-%d") = ? and s.shift_sale_id=?',today,shift.id)
+ .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id")
+ .sum("payment_amount")
+ end
+ end
+ end
+ end
+ end
+
+ 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?
+ 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)
+ .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 DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today)
+ .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 DATE_FORMAT(receipt_date,"%Y-%m-%d") = ? and shift_sale_id=?',today,shift.id)
+ .first()
+ end
+ end
+ end
+ end
+ end
+
+ 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?
+ query = Sale.select("distinct sp.payment_method")
+ .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today)
+ .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 DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today)
+ .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 DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id)
+ .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
+ end
+ end
+ end
+ end
+ end
+
+ 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 sales.receipt_date between ? and ?",from,to)
+ 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 sales.receipt_date between ? and ?",from,to)
+ 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?
+ 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 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)
+ 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 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)
+ 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 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") and sales.shift_sale_id=?',today,shift.id)
+ else
+ query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,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
+ end
+ end
+
+ def self.total_customer(today,current_user=nil,from,to)
+ dinein_cnt = self.total_dinein(today,current_user,from,to)
+ takeaway_cnt = self.total_takeaway(today,current_user,from,to)
+ membership_cnt = self.total_membership(today,current_user,from,to)
total_cus = 0
if !dinein_cnt.nil? || !takeaway_cnt.nil? || !membership_cnt.nil?
@@ -1151,69 +1468,448 @@ end
return total_cus
end
- def self.total_dinein(today)
- 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 DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null',today)
- .first()
+ 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?
+ 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 DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null',today)
+ .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 DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Dinein" 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_dinein_cus")
+ .joins("JOIN customers as c ON c.customer_id = sales.customer_id")
+ .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null and sales.shift_sale_id=?',today,shift.id)
+ .first()
+ end
+ end
+ end
+ end
end
- def self.total_takeaway(today)
- 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(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null',today)
- .first()
+ 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?
+ 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(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null',today)
+ .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 DATE_FORMAT(sales.receipt_date,"%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(sales.receipt_date,"%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)
- 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 DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',today)
- .first()
+ 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?
+ 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 DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',today)
+ .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 DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',today)
+ .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 DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? 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=?',today,shift.id)
+ .first()
+ end
+ end
+ end
+ end
end
- def self.total_other_customer(today)
- 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 DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null',today)
- .first()
+ 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?
+ 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 DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null',today)
+ .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 DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type is null 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_cus")
+ .joins("JOIN customers as c ON c.customer_id = sales.customer_id")
+ .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null and sales.shift_sale_id=?',today,shift.id)
+ .first()
+ end
+ end
+ end
+ end
end
- def self.total_order(today)
- 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 DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today)
- .first()
+ 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?
+ 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 DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today)
+ .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 DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today)
+ .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 DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id)
+ .first()
+ end
+ end
+ end
+ end
end
- def self.total_account(today)
- 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 DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today)
+ 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?
+ 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 DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today)
+ 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 DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today)
+ 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 DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id)
+ end
+ end
+ end
+ end
end
- def self.account_data(account_id, today)
- 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 DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today)
- .first()
+ 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?
+ 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 DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today)
+ .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 DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today)
+ .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 DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id)
+ .first()
+ end
+ end
+ end
+ end
end
- def self.top_items(today)
- 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 DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today)
- .group("a.product_code")
- .order("SUM(a.qty) DESC")
- .first()
+ 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?
+ 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 DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today)
+ .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 DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today)
+ .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 DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id)
+ .group("a.product_code")
+ .order("SUM(a.qty) DESC")
+ .first()
+ end
+ end
+ end
+ end
end
- def self.total_foc_items(today)
- 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)
- .sum("a.qty")
+ 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?
+ 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)
+ .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 DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today)
+ .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 DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id)
+ .sum("a.qty")
+ end
+ end
+ end
+ end
end
#card sale trans data
diff --git a/app/views/home/dashboard.html.erb b/app/views/home/dashboard.html.erb
index 104c4c67..91cce7e3 100755
--- a/app/views/home/dashboard.html.erb
+++ b/app/views/home/dashboard.html.erb
@@ -1,6 +1,7 @@
-
<%= t :dashboard %>
+
+
<%= t :date_time %> : <%= Time.zone.now.utc.getlocal.strftime("%Y-%m-%d %I:%M %p") %>
+ <% end %>
+ <% if current_user.role == 'administrator' || current_user.role == 'manager' %>
+ <% else %>
+
+ <% end %>
<%= t("views.right_panel.detail.order") %>
@@ -274,6 +315,41 @@
+ <% end %>
- <% end %>
-
\ No newline at end of file
+
+
+
\ No newline at end of file
diff --git a/app/views/reports/product_sale/index.html.erb b/app/views/reports/product_sale/index.html.erb
new file mode 100755
index 00000000..eb797122
--- /dev/null
+++ b/app/views/reports/product_sale/index.html.erb
@@ -0,0 +1,113 @@
+
+
+
\ No newline at end of file
diff --git a/app/views/reports/product_sale/index.xls.erb b/app/views/reports/product_sale/index.xls.erb
new file mode 100755
index 00000000..7123588c
--- /dev/null
+++ b/app/views/reports/product_sale/index.xls.erb
@@ -0,0 +1,64 @@
+