diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index da8695d8..d2b0ad36 100755 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -86,64 +86,72 @@ class HomeController < ApplicationController today = DateTime.now.strftime('%Y-%m-%d') if !@from.nil? && !@to.nil? - @orders = Sale::where("payment_status='new' and sale_status='bill' and DATE_FORMAT(receipt_date,'%Y-%m-%d') between '#{@from}' and '#{@to}'").count() + if !@from_time.nil? && @to_time.nil? + @orders = Sale::where("payment_status='new' and sale_status='bill' and DATE_FORMAT(receipt_date,'%Y-%m-%d') between '#{@from}' and '#{@to}' and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%H:%m') between '#{@from_time}' and '#{@to_time}'").count() + else + @orders = Sale::where("payment_status='new' and sale_status='bill' and DATE_FORMAT(receipt_date,'%Y-%m-%d') between '#{@from}' and '#{@to}'").count() + end else @orders = Sale::where("payment_status='new' and sale_status='bill' and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'").count() end if !@from.nil? && !@to.nil? - @sales = Sale::where("payment_status='paid' and sale_status='completed' and DATE_FORMAT(receipt_date,'%Y-%m-%d') between '#{@from}' and '#{@to}'").count() + if !@from_time.nil? && @to_time.nil? + @sales = Sale::where("payment_status='paid' and sale_status='completed' and DATE_FORMAT(receipt_date,'%Y-%m-%d') between '#{@from}' and '#{@to}' and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%H:%m') between '#{@from_time}' and '#{@to_time}'").count() + else + @sales = Sale::where("payment_status='paid' and sale_status='completed' and DATE_FORMAT(receipt_date,'%Y-%m-%d') between '#{@from}' and '#{@to}'").count() + end else @sales = Sale::where("payment_status='paid' and sale_status='completed' and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'").count() end - @top_products = Sale.top_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) + @top_products = Sale.top_products(today,current_user,@from,@to,@from_time,@to_time).sum('i.qty') + @bottom_products = Sale.bottom_products(today,current_user,@from,@to,@from_time,@to_time).sum('i.qty') + @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') # .sum(:grand_total) - @employee_sales = Sale.employee_sales(today,@from,@to) + @employee_sales = Sale.employee_sales(today,current_user,@from,@to,@from_time,@to_time) .sum('(CASE WHEN sp.payment_method="cash" THEN (sp.payment_amount - sales.amount_changed) ELSE sp.payment_amount END)') - @inventories = StockJournal.inventory_balances(today,@from,@to).sum(:balance) + @inventories = StockJournal.inventory_balances(today,@from,@to,@from_time,@to_time).sum(:balance) - @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) + @total_sale = Sale.total_sale(today,current_user,@from,@to,@from_time,@to_time) + @total_count = Sale.total_count(today,current_user,@from,@to,@from_time,@to_time) + @total_card = Sale.total_card_sale(today,current_user,@from,@to,@from_time,@to_time) + @total_credit = Sale.credit_payment(today,current_user,@from,@to,@from_time,@to_time) @sale_data = Array.new - @total_payment_methods = Sale.total_payment_methods(today,current_user,@from,@to) + @total_payment_methods = Sale.total_payment_methods(today,current_user,@from,@to,@from_time,@to_time) if !@total_payment_methods.nil? @total_payment_methods.each do |payment| - if payment.payment_method == "mpu" || payment.payment_method == "visa" || payment.payment_method == "master" || payment.payment_method == "jcb" - pay = Sale.payment_sale('card', today, current_user,@from,@to) + if payment.payment_method == "mpu" || payment.payment_method == "visa" || payment.payment_method == "master" || payment.payment_method == "jcb" || payment.payment_method == "unionpay" + pay = Sale.payment_sale('card', today, current_user,@from,@to,@from_time,@to_time) @sale_data.push({'card' => pay.payment_amount}) else - pay = Sale.payment_sale(payment.payment_method, today, current_user,@from,@to) + pay = Sale.payment_sale(payment.payment_method, today, current_user,@from,@to,@from_time,@to_time) @sale_data.push({payment.payment_method => pay.payment_amount}) end end end - @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) + @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_dinein = Sale.total_dinein(today,current_user,@from,@to,@from_time,@to_time) + @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) - @total_accounts = Sale.total_account(today,current_user,@from,@to) + @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) @account_data = Array.new if !@total_accounts.nil? @total_accounts.each do |account| - acc = Sale.account_data(account.account_id, today,current_user,@from,@to) + acc = Sale.account_data(account.account_id, today,current_user,@from,@to,@from_time,@to_time) if !acc.nil? @account_data.push({account.title => acc.cnt_acc, account.title + '_amount' => acc.total_acc}) end end end - @top_items = Sale.top_items(today,current_user,@from,@to) - @total_foc_items = Sale.total_foc_items(today,current_user,@from,@to) + @top_items = Sale.top_items(today,current_user,@from,@to,@from_time,@to_time) + @total_foc_items = Sale.total_foc_items(today,current_user,@from,@to,@from_time,@to_time) # get printer info @print_settings = PrintSetting.get_precision_delimiter() @@ -202,12 +210,15 @@ class HomeController < ApplicationController to_time = params[:to_time] if from.present? && to.present? - f_date = DateTime.parse(from) - t_date = DateTime.parse(to) - f_time = Time.mktime(f_date.year,f_date.month,f_date.day,f_date.hour,f_date.min,f_date.sec) - t_time = Time.mktime(t_date.year,t_date.month,t_date.day,t_date.hour,t_date.min,t_date.sec) - from = f_time.beginning_of_day.utc.getlocal - to = t_time.end_of_day.utc.getlocal + # f_date = DateTime.parse(from) + # t_date = DateTime.parse(to) + # f_time = Time.mktime(f_date.year,f_date.month,f_date.day,f_date.hour,f_date.min,f_date.sec) + # t_time = Time.mktime(t_date.year,t_date.month,t_date.day,t_date.hour,t_date.min,t_date.sec) + # from = f_time.beginning_of_day.utc.getlocal + # to = t_time.end_of_day.utc.getlocal + + from = DateTime.parse(from).utc.getlocal.strftime('%Y-%m-%d') + to = DateTime.parse(to).utc.getlocal.strftime('%Y-%m-%d') end return from, to, from_time, to_time diff --git a/app/controllers/transactions/sales_controller.rb b/app/controllers/transactions/sales_controller.rb index bae9c353..be370371 100755 --- a/app/controllers/transactions/sales_controller.rb +++ b/app/controllers/transactions/sales_controller.rb @@ -8,14 +8,23 @@ class Transactions::SalesController < ApplicationController def index receipt_no = params[:receipt_no] - from = params[:from] - to = params[:to] + # from = params[:from] + # to = params[:to] + from, to = get_date_range_from_params + @shift = '' + if params[:shift_name].to_i != 0 + @shift = ShiftSale.find(params[:shift_name]) + end if receipt_no.nil? && from.nil? && to.nil? - @sales = Sale.where("NOT sale_status='new'").order("sale_id desc") + if @shift.blank? + @sales = Sale.where("NOT sale_status='new'").order("sale_id desc") + else + @sales = Sale.where("NOT sale_status='new' and shift_sale_id ='#{@shift.id}'").order("sale_id desc") + end @sales = Kaminari.paginate_array(@sales).page(params[:page]).per(20) else - sale = Sale.search(receipt_no,from,to) + sale = Sale.search(receipt_no,from,to,@shift) if sale.count > 0 @sales = sale @sales = Kaminari.paginate_array(@sales).page(params[:page]).per(20) @@ -26,6 +35,12 @@ class Transactions::SalesController < ApplicationController @receipt_no = receipt_no @from = from @to = to + + if @shift.present? + @shift_from = @shift.shift_started_at.nil? ? '-' : @shift.shift_started_at.utc.getlocal.strftime("%e %b %I:%M%p") + @shift_to = @shift.shift_closed_at.nil? ? '-' : @shift.shift_closed_at.utc.getlocal.strftime("%e %b %I:%M%p") + @shift_data = @shift + end # if receipt_no.nil? && search_date.nil? # @sales = Sale.where("NOT sale_status = 'void' " ).order("sale_id desc").limit(500) @@ -48,7 +63,7 @@ class Transactions::SalesController < ApplicationController @sale_audits = [] @sale_item_audits = [] - if !@sales.nil? + if @sales != 0 @sales.each do |sale| sale_audit = SaleAudit.where("(action = 'SALEPAYMENT' or action = 'SALEVOID') and sale_id = ? and remark IS NOT NULL",sale.sale_id) if !sale_audit.nil? @@ -148,6 +163,74 @@ class Transactions::SalesController < ApplicationController end end + # date range + PERIOD = { + "today" => 0, + "yesterday" => 1, + "this_week" => 2, + "last_week" => 3, + "last_7" => 4, + "this_month" => 5, + "last_month" => 6, + "last_30" => 7, + "this_year" => 8, + "last_year" => 9 + } + + def get_date_range_from_params + period_type = params[:period_type] + period = params[:period] + from = params[:from] + to = params[:to] + day_ref = Time.now.utc.getlocal + + 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 + else + case period.to_i + when PERIOD["today"] + from = day_ref.beginning_of_day.utc + to = day_ref.end_of_day.utc + + when PERIOD["yesterday"] + from = (day_ref - 1.day).beginning_of_day.utc + to = (day_ref - 1.day).end_of_day.utc + + when PERIOD["this_week"] + from = Time.now.beginning_of_week.utc + to = Time.now.utc + when PERIOD["last_week"] + from = (day_ref - 7.day).beginning_of_week.utc + to = (day_ref - 7.day).end_of_week.utc + when PERIOD["last_7"] + from = (day_ref - 7.day).utc + to = Time.now.utc + when PERIOD["this_month"] + from = Time.now.beginning_of_month.utc + to = Time.now.utc + when PERIOD["last_month"] + from = (day_ref - 1.month).beginning_of_month.utc + to = (day_ref - 1.month).end_of_month.utc + when PERIOD["last_30"] + from = (day_ref - 30.day).utc + to = Time.now.utc + when PERIOD["this_year"] + from = Time.now.beginning_of_year.utc + to = Time.now.utc + when PERIOD["last_year"] + from = (day_ref - 1.year).beginning_of_year.utc + to = (day_ref - 1.year).end_of_year.utc + end + end + + return from, to + end + def check_user if current_user.nil? redirect_to root_path diff --git a/app/models/sale.rb b/app/models/sale.rb index 5fc0c06f..a997e27f 100755 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -598,7 +598,7 @@ class Sale < ApplicationRecord end end - def self.search(filter,from,to) + def self.search(filter,from,to,shift) if filter.blank? keyword = '' else @@ -606,10 +606,18 @@ class Sale < ApplicationRecord end if from.present? && to.present? - sale = Sale.where("DATE_FORMAT(receipt_date,'%d-%m-%Y') >= ?" + " AND DATE_FORMAT(receipt_date,'%d-%m-%Y') <= ? and NOT sale_status = 'new' ", from,to) - query = sale.where(keyword) + if shift.blank? + sale = Sale.where("DATE_FORMAT(receipt_date,'%Y-%m-%d') >= ?" + " AND DATE_FORMAT(receipt_date,'%Y-%m-%d') <= ? and NOT sale_status = 'new' ", from,to) + else + sale = Sale.where("DATE_FORMAT(receipt_date,'%Y-%m-%d') >= ?" + " AND DATE_FORMAT(receipt_date,'%Y-%m-%d') <= ? and NOT sale_status = 'new' and shift_sale_id = '#{shift.id}'", from,to) + end + query = sale.where(keyword) else + if shift.blank? where("receipt_no LIKE ? OR cashier_name LIKE ? OR sale_status ='#{filter}'","%#{filter}%","%#{filter}%") + else + where("receipt_no LIKE ? OR cashier_name LIKE ? OR sale_status ='#{filter}' and shift_sale_id = ?","%#{filter}%","%#{filter}%",shift.id) + end end end @@ -1134,88 +1142,319 @@ end return tax end - def self.top_products(today,from,to) + def self.top_products(today,current_user,from,to,from_time,to_time) if !from.nil? && !to.nil? - query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + + 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:%M') 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) DESC").limit(20) + else + if current_user.role == 'administrator' || current_user.role == 'manager' + 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 sale_status= 'completed'") - .group('mi.name') - .order("SUM(i.qty) DESC").limit(20) + 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:%M') 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) DESC").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:%M') 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'),'%Y-%m-%d') between '#{from}' and '#{to}'"+ + " and sale_status= 'completed' and shift_sale_id='#{shift.id}'") + end + query = query.group('mi.name') + .order("SUM(i.qty) DESC").limit(20) + end + end + end else - query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + + 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'") + " and sale_status= 'completed'") .group('mi.name') .order("SUM(i.qty) DESC").limit(20) + else + if current_user.role == 'administrator' || current_user.role == 'manager' + 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) DESC").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) DESC").limit(20) + end + end + end end end - def self.bottom_products(today,from,to) + def self.bottom_products(today,current_user,from,to,from_time,to_time) 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 sale_status= 'completed'") - .group('mi.name') + 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:%M') 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' + 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:%M') 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:%M') 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 - query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + + 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' + 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 - def self.hourly_sales(today,from,to) + def self.hourly_sales(today,current_user,from,to,from_time,to_time) if !from.nil? && !to.nil? - query= Sale.select("grand_total") - .where('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') + if current_user.nil? + query = Sale.select("grand_total") + if !from_time.nil? && !to_time.nil? + query = query.where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%M") between ? and ?',from,to,from_time,to_time) + else + query = query.where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) + end + query = query.group("date_format(CONVERT_TZ(receipt_date,'+00:00', 'SYSTEM'), '%I %p')") + .order('receipt_date') + else + if current_user.role == 'administrator' || current_user.role == 'manager' + query = Sale.select("grand_total") + if !from_time.nil? && !to_time.nil? + query = query.where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%M") between ? and ?',from,to,from_time,to_time) + else + query = query.where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) + end + query = query.group("date_format(CONVERT_TZ(receipt_date,'+00:00', 'SYSTEM'), '%I %p')") + .order('receipt_date') + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("grand_total") + if !from_time.nil? && !to_time.nil? + query = query.where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%M") between ? and ? and shift_sale_id=?',from,to,from_time,to_time,shift.id) + else + query = query.where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and shift_sale_id=?',from,to,shift.id) + end + query = query.group("date_format(CONVERT_TZ(receipt_date,'+00:00', 'SYSTEM'), '%I %p')") + .order('receipt_date') + end + end + end else - query= Sale.select("grand_total") + if current_user.nil? + query = Sale.select("grand_total") .where('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') + else + if current_user.role == 'administrator' || current_user.role == 'manager' + query = Sale.select("grand_total") + .where('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') + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("grand_total") + .where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ? and shift_sale_id=?',today,shift.id) + .group("date_format(CONVERT_TZ(receipt_date,'+00:00', 'SYSTEM'), '%I %p')") + .order('receipt_date') + end + end + end end end - def self.employee_sales(today,from,to) + def self.employee_sales(today,current_user,from,to,from_time,to_time) if !from.nil? && !to.nil? - query = Sale.joins("JOIN employees as e on e.id=sales.cashier_id") + if current_user.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") + if !from_time.nil? && !to_time.nil? + query = query.where("sales.payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%M') between '#{from_time}' and '#{to_time}'") + else + query = query.where("sales.payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'") + end + query = query.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 + if current_user.role == 'administrator' || current_user.role == 'manager' + 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") + if !from_time.nil? && !to_time.nil? + query = query.where("sales.payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%M') between '#{from_time}' and '#{to_time}'") + else + query = query.where("sales.payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'") + end + query = query.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 + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.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") + if !from_time.nil? && !to_time.nil? + query = query.where("sales.payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%M') between '#{from_time}' and '#{to_time}' and shift_sale_id='#{shift.id}'") + else + query = query.where("sales.payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and shift_sale_id='#{shift.id}'") + end + query = query.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 + end else - query = Sale.joins("JOIN employees as e on e.id=sales.cashier_id") + if current_user.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 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,current_user=nil,from=nil,to=nil) - 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' || current_user.role == 'manager' - total = Sale.where('sale_status = "completed" and receipt_date between ? and ?',from,to).sum("grand_total") + 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") 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) + 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}' and shift_sale_id='#{shift.id}'") + .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 + end + end + end + + def self.total_sale(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? + total = Sale.where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%M") between ? and ?',from,to,from_time,to_time).sum("grand_total") + else + total = Sale.where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to).sum("grand_total") + end + else + if current_user.role == 'administrator' || current_user.role == 'manager' + if !from_time.nil? && !to_time.nil? + total = Sale.where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%M") between ? and ?',from,to,from_time,to_time).sum("grand_total") + else + total = Sale.where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to).sum("grand_total") + end + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + if !from_time.nil? && !to_time.nil? + total = Sale.where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%M") between ? and ? and shift_sale_id=?',from,to,from_time,to_time,shift.id) .sum("grand_total") + else + total = Sale.where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and shift_sale_id=?',from,to,shift.id) + .sum("grand_total") + end end end end @@ -1236,17 +1475,29 @@ end end end - def self.total_count(today,current_user=nil,from=nil,to=nil) + def self.total_count(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) if !from.nil? && !to.nil? if current_user.nil? - total = Sale.where('sale_status = "completed" and receipt_date between ? and ?',from,to).count + if !from_time.nil? && !to_time.nil? + total = Sale.where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date, "+00:00", "+06:30"),"%H:%M") between ? and ?',from,to,from_time,to_time).count + else + total = Sale.where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ?',from,to).count + end else if current_user.role == 'administrator' || current_user.role == 'manager' - total = Sale.where('sale_status = "completed" and receipt_date between ? and ?',from,to).count + if !from_time.nil? && !to_time.nil? + total = Sale.where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date, "+00:00", "+06:30"),"%H:%M") between ? and ?',from,to,from_time,to_time).count + else + total = Sale.where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ?',from,to).count + end 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 + if !from_time.nil? && !to_time.nil? + total = Sale.where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date, "+00:00", "+06:30"),"%H:%M") between ? and ? and shift_sale_id = ?',from,to,from_time,to_time,shift.id).count + else + total = Sale.where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and shift_sale_id = ?',from,to,shift.id).count + end end end end @@ -1266,23 +1517,41 @@ end end end - def self.total_card_sale(today,current_user=nil,from=nil,to=nil) + def self.total_card_sale(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) 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' || current_user.role == 'manager' - 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) + if !from_time.nil? && !to_time.nil? + query = Sale.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:%M") 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,from_time,to_time) .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) + query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") 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") + end + else + if current_user.role == 'administrator' || current_user.role == 'manager' + if !from_time.nil? && !to_time.nil? + query = Sale.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:%M") 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,from_time,to_time) + .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .sum("sp.payment_amount") + else + query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") 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") + end + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + if !from_time.nil? && !to_time.nil? + query = Sale.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:%M") 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,from_time,to_time,shift.id) + .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .sum("sp.payment_amount") + else + query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") 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 end @@ -1308,23 +1577,41 @@ end end end - def self.credit_payment(today,current_user=nil,from=nil,to=nil) + def self.credit_payment(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) 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' || current_user.role == 'manager' - query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and s.receipt_date between ? and ?',from,to) + if !from_time.nil? && !to_time.nil? + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%H:%M") between ? and ?',from,to,from_time,to_time) .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) + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") .sum("payment_amount") + end + else + if current_user.role == 'administrator' || current_user.role == 'manager' + if !from_time.nil? && !to_time.nil? + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%H:%M") between ? and ?',from,to,from_time,to_time) + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + .sum("payment_amount") + else + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + .sum("payment_amount") + end + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + if !from_time.nil? && !to_time.nil? + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%H:%M") between ? and ? and s.shift_sale_id=?',from,to,from_time,to_time,shift.id) + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + .sum("payment_amount") + else + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") 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 end @@ -1350,23 +1637,41 @@ end end end - def self.summary_sale_receipt(today,current_user=nil,from=nil,to=nil) + def self.summary_sale_receipt(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) 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) + if !from_time.nil? && !to_time.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(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%M") between ? and ?',from,to,from_time,to_time) .first() + else + 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(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) + .first() + end else if current_user.role == 'administrator' || current_user.role == 'manager' - 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() + if !from_time.nil? && !to_time.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(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%M") between ? and ?',from,to,from_time,to_time) + .first() + else + 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(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) + .first() + end 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() + if !from_time.nil? && !to_time.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(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%M") between ? and ? and shift_sale_id=?',from,to,from_time,to_time,shift.id) + .first() + else + 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(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and shift_sale_id=?',from,to,shift.id) + .first() + end end end end @@ -1392,23 +1697,41 @@ end end end - def self.total_payment_methods(today,current_user=nil,from=nil,to=nil) + def self.total_payment_methods(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) 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) + if !from_time.nil? && !to_time.nil? + query = Sale.select("distinct sp.payment_method") + .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:%M") between ? and ?',from,to,from_time,to_time) .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + else + query = Sale.select("distinct sp.payment_method") + .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ?',from,to) + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + end else if current_user.role == 'administrator' || current_user.role == 'manager' - query = Sale.select("distinct sp.payment_method") - .where('sales.sale_status = "completed" and sales.receipt_date between ? and ?',from,to) + if !from_time.nil? && !to_time.nil? + query = Sale.select("distinct sp.payment_method") + .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:%M") between ? and ?',from,to,from_time,to_time) .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + else + query = Sale.select("distinct sp.payment_method") + .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ?',from,to) + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + end 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") + if !from_time.nil? && !to_time.nil? + query = Sale.select("distinct sp.payment_method") + .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:%M") between ? and ? and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + else + query = Sale.select("distinct sp.payment_method") + .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") 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 end @@ -1434,35 +1757,69 @@ end end end - def self.payment_sale(payment_method, today, current_user=nil,from=nil,to=nil) + def self.payment_sale(payment_method, today, current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) + time_query = '' + if !from_time.nil? && !to_time.nil? + time_query = " and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%M') between '#{from_time}' and '#{to_time}'" + end 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' || current_user.role == 'manager' + if !from_time.nil? && !to_time.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) + 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:%M") 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,from_time,to_time) else - query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and sales.receipt_date between ? and ?",from,to) + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ? and and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%M') between ? and ?",from,to,from_time,to_time) 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(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',from,to) + else + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') 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() + end + else + if current_user.role == 'administrator' || current_user.role == 'manager' + if !from_time.nil? && !to_time.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) + 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:%M") 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,from_time,to_time) 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) + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' 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:%M') between ? and ?",from,to,from_time,to_time) 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 + 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(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',from,to) + else + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') 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() + end + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + if !from_time.nil? && !to_time.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(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:%M") 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,from_time,to_time,shift.id) + else + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' 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:%M') between ? and ? and sales.shift_sale_id=?",from,to,from_time,to_time,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() + else + 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(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") 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 DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') 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 end @@ -1500,10 +1857,10 @@ end end end - def self.total_customer(today,current_user=nil,from=nil,to=nil) - 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) + 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) + takeaway_cnt = self.total_takeaway(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 if !dinein_cnt.nil? || !takeaway_cnt.nil? || !membership_cnt.nil? @@ -1513,26 +1870,47 @@ end return total_cus end - def self.total_dinein(today,current_user=nil,from=nil,to=nil) + def self.total_dinein(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) if !from.nil? && !to.nil? if current_user.nil? - query = Sale.select("count(sales.customer_id) as total_dinein_cus") + if !from_time.nil? && !to_time.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) + .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:%M") between ? and ? and c.customer_type = "Dinein" and c.membership_id is null',from,to,from_time,to_time) .first() + else + 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(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) + .first() + end else if current_user.role == 'administrator' || current_user.role == 'manager' - query = Sale.select("count(sales.customer_id) as total_dinein_cus") + if !from_time.nil? && !to_time.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) + .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:%M") between ? and ? and c.customer_type = "Dinein" and c.membership_id is null',from,to,from_time,to_time) .first() + else + 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(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) + .first() + end else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? - query = Sale.select("count(sales.customer_id) as total_dinein_cus") + if !from_time.nil? && !to_time.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) + .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:%M") 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) .first() + else + 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(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) + .first() + end end end end @@ -1561,26 +1939,47 @@ end end end - def self.total_takeaway(today,current_user=nil,from=nil,to=nil) + 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? - 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' || current_user.role == 'manager' + 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 sales.receipt_date between ? and ? and c.customer_type = "Takeaway" 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 DATE_FORMAT(CONVERT_TZsales.receipt_date,"%H:%M") 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' + 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:%M") 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? - 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() + 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:%M") 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 @@ -1609,26 +2008,43 @@ end end end - def self.total_membership(today,current_user=nil,from=nil,to=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 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' || current_user.role == 'manager' + if !from_time.nil? && !to_time.nil? query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and 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) + .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:%M") 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,from_time,to_time) .first() + else + 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(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") 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() + end + else + if current_user.role == 'administrator' || current_user.role == 'manager' + if !from_time.nil? && !to_time.nil? + query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and 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:%M") 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,from_time,to_time) + .first() + else + 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(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") 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() + end 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() + 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:%M") 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,from_time,to_time,shift.id).first() + 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 = "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 end @@ -1657,26 +2073,41 @@ end end end - def self.total_other_customer(today,current_user=nil,from=nil,to=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 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() + 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:%M") between ? and ? and c.customer_type is null and c.membership_id is null',from,to,from_time,to_time) + .first() + 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) + .first() + end else if current_user.role == 'administrator' || current_user.role == 'manager' 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() + 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:%M") between ? and ? and c.customer_type is null and c.membership_id is null',from,to,from_time,to_time) + .first() + 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) + .first() + end 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() + 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:%M") 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() + 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) + .first() + end end end end @@ -1705,29 +2136,44 @@ end end end - def self.total_order(today,current_user=nil,from=nil,to=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 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() + if !from_time.nil? && !to_time.nil? + query = query.where('b.status = "billed" 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:%M") between ? and ?',from,to,from_time,to_time) + .first() + else + query = query.where('b.status = "billed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) + .first() + end else if current_user.role == 'administrator' || current_user.role == 'manager' 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() + if !from_time.nil? && !to_time.nil? + query = query.where('b.status = "billed" 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:%M") between ? and ?',from,to,from_time,to_time) + .first() + else + query = query.where('b.status = "billed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) + .first() + end 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() + if !from_time.nil? && !to_time.nil? + query = query.where('b.status = "billed" 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:%M") between ? and ? and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) + .first() + else + query = query.where('b.status = "billed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and sales.shift_sale_id=?',from,to,shift.id) + .first() + end end end end @@ -1759,26 +2205,38 @@ end end end - def self.total_account(today,current_user=nil,from=nil,to=nil) + def self.total_account(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) 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) + 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"),"%Y-%m-%d") between ? and ?',from,to,from_time,to_time) + 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 ?',from,to) + end else if current_user.role == 'administrator' || current_user.role == 'manager' 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) + 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:%M") between ? and ?',from,to,from_time,to_time) + 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 ?',from,to) + end 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) + 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:%M") between ? and ? and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) + 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 sales.shift_sale_id=?',from,to,shift.id) + end end end end @@ -1807,26 +2265,41 @@ end end end - def self.account_data(account_id, today, current_user=nil,from=nil,to=nil) + def self.account_data(account_id, today, current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) 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() + if !from_time.nil? && !to_time.nil? + query = query.where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' 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:%M') between ? and ?",from,to,from_time,to_time) + .first() + else + query = query.where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) + .first() + end else if current_user.role == 'administrator' || current_user.role == 'manager' 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() + if !from_time.nil? && !to_time.nil? + query = query.where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' 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:%M') between ? and ?",from,to,from_time,to_time) + .first() + else + query = query.where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) + .first() + end 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() + if !from_time.nil? && !to_time.nil? + query = query.where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' 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:%M') between ? and ? and sales.shift_sale_id=?",from,to,from_time,to_time,shift.id) + .first() + else + query = query.where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and sales.shift_sale_id=?",from,to,shift.id) + .first() + end end end end @@ -1855,32 +2328,44 @@ end end end - def self.top_items(today,current_user=nil,from=nil,to=nil) + def self.top_items(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) 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() + if !from_time.nil? && !to_time.nil? + query = query.where("(a.qty > 0 and a.price > 0) and payment_status='paid' and 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:%M') between ? and ?",from,to,from_time,to_time) + else + query = query.where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) + end + query = query.group("a.product_code") + .order("SUM(a.qty) DESC") + .first() else if current_user.role == 'administrator' || current_user.role == 'manager' 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() + if !from_time.nil? && !to_time.nil? + query = query.where("(a.qty > 0 and a.price > 0) and payment_status='paid' and 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:%M') between ? and ?",from,to,from_time,to_time) + else + query = query.where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) + end + query = query.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() + if !from_time.nil? && !to_time.nil? + query = query.where("(a.qty > 0 and a.price > 0) and payment_status='paid' and 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:%M') between ? and ? and sales.shift_sale_id=?",from,to,from_time,to_time,shift.id) + else + query = query.where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and sales.shift_sale_id=?",from,to,shift.id) + end + query = query.group("a.product_code") + .order("SUM(a.qty) DESC") + .first() end end end @@ -1915,23 +2400,38 @@ end end end - def self.total_foc_items(today,current_user=nil,from=nil,to=nil) + def self.total_foc_items(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) 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") + if !from_time.nil? && !to_time.nil? + query = query.where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' 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:%M') between ? and ?",from,to,from_time,to_time) + .sum("a.qty") + else + query = query.where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) + .sum("a.qty") + end else if current_user.role == 'administrator' || current_user.role == 'manager' 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") + if !from_time.nil? && !to_time.nil? + query = query.where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' 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:%M') between ? and ?",from,to,from_time,to_time) + .sum("a.qty") + else + query = query.where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) + .sum("a.qty") + end 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") + if !from_time.nil? && !to_time.nil? + query = query.where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' 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:%M') between ? and ? and sales.shift_sale_id=?",from,to,from_time,to_time,shift.id) + .sum("a.qty") + else + query = query.where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and sales.shift_sale_id=?",from,to,shift.id) + .sum("a.qty") + end end end end diff --git a/app/models/stock_journal.rb b/app/models/stock_journal.rb index c2a12f4a..f2904b13 100755 --- a/app/models/stock_journal.rb +++ b/app/models/stock_journal.rb @@ -36,12 +36,16 @@ class StockJournal < ApplicationRecord journal.save end - def self.inventory_balances(today,from,to) + def self.inventory_balances(today,from,to,from_time,to_time) if !from.nil? && !to.nil? query = StockJournal.select("mii.item_instance_name as item_instance_name,balance") .joins("join menu_item_instances mii on mii.item_instance_code=stock_journals.item_code") - .where("stock_journals.created_at between '#{from}' and '#{to}'") - .group("mii.item_instance_name") + if !from_time.nil? && !to_time.nil? + query = query.where("DATE_FORMAT(CONVERT_TZ(stock_journals.created_at,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'") + else + query = query.where("DATE_FORMAT(CONVERT_TZ(stock_journals.created_at,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and DATE_FORMAT(CONVERT_TZ(stock_journals.created_at,'+00:00','+06:30'),'%H:%M') between '#{from_time}' and '#{to_time}'") + end + query = query.group("mii.item_instance_name") .order("mii.item_instance_name ASC") else query = StockJournal.select("mii.item_instance_name as item_instance_name,balance") diff --git a/app/views/home/dashboard.html.erb b/app/views/home/dashboard.html.erb index 6ee68d97..865202b0 100755 --- a/app/views/home/dashboard.html.erb +++ b/app/views/home/dashboard.html.erb @@ -68,24 +68,46 @@
There is no data for search....
There is no data for search....