From 7a60ab91c51dbc5f227f4c190a32973df49f3d3d Mon Sep 17 00:00:00 2001 From: phyusin Date: Thu, 27 Dec 2018 15:36:30 +0630 Subject: [PATCH] change duplicate query --- .../origami/other_charges_controller.rb | 1 + app/models/sale.rb | 426 ++++++++---------- app/views/home/dashboard.html.erb | 16 +- 3 files changed, 195 insertions(+), 248 deletions(-) diff --git a/app/controllers/origami/other_charges_controller.rb b/app/controllers/origami/other_charges_controller.rb index 63ffa937..b88c0f80 100755 --- a/app/controllers/origami/other_charges_controller.rb +++ b/app/controllers/origami/other_charges_controller.rb @@ -46,6 +46,7 @@ class Origami::OtherChargesController < BaseOrigamiController sale_item = SaleItem.new sale_item.sale_id = sale_id + sale_item.menu_category_name = "Other Charges" sale_item.product_code = "Other Charges" sale_item.product_name = "*" + di["name"] sale_item.product_alt_name = "" diff --git a/app/models/sale.rb b/app/models/sale.rb index ce1e822f..1362f5ce 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -255,8 +255,12 @@ class Sale < ApplicationRecord sale_item = SaleItem.new #pull - sale_item.menu_category_code = menu_category.code ? menu_category.code : nil - sale_item.menu_category_name = menu_category.name + if !menu_category.nil? + sale_item.menu_category_code = menu_category.code + sale_item.menu_category_name = menu_category.name + else + sale_item.menu_category_name = "Product" + end sale_item.product_code = item.item_code sale_item.item_instance_code = item.item_instance_code sale_item.product_name = item.item_name @@ -911,16 +915,14 @@ def self.get_by_range_by_saleitems(from,to,status,report_type) mi.item_code as code,(SUM(i.qty) * i.unit_price) as grand_total, SUM(i.qty) as total_item," + " i.unit_price as unit_price, - mi.name as product_name, - mc.name as menu_category_name, - mc.id as menu_category_id ") - .group('mi.id') - .order("mi.menu_category_id") + i.product_name, + sale_items.menu_category_name, + sale_items.menu_category_code") + .group('i.product_code') + .order("sale_items.menu_category_code") - query = query.joins("JOIN sale_items i ON i.sale_id = sales.sale_id - JOIN menu_items mi ON i.product_code = mi.item_code" + - " JOIN menu_categories mc ON mc.id = mi.menu_category_id - JOIN employees ea ON ea.id = sales.cashier_id") + query = query.joins("JOIN sale_items i ON i.sale_id = sales.sale_id + JOIN employees ea ON ea.id = sales.cashier_id") query = query.where("(receipt_date between ? and ? and sale_status=?) AND i.unit_price <> 0",from,to,status) @@ -982,20 +984,21 @@ def self.get_by_shift_sale_by_item(from,to,status) end def self.get_item_query(type) + check_product = "i.menu_category_name != 'product'" if type == "revenue" sale_type = "i.qty > 0 and status IS NULL" elsif type == "all" || type.nil? - sale_type = "" + sale_type = "#{check_product}" elsif type == "discount" - sale_type = "i.status = 'Discount'" + sale_type = "#{check_product} and i.status = 'Discount'" elsif type == "foc" - sale_type = "i.status = 'foc' and i.item_instance_code IS NOT NULL and i.qty > 0" + sale_type = "#{check_product} and i.status = 'foc' and i.item_instance_code IS NOT NULL and i.qty > 0" elsif type == "void" - sale_type = "i.status = 'void' and i.item_instance_code IS NOT NULL and i.qty > 0" + sale_type = "#{check_product} and i.status = 'void' and i.item_instance_code IS NOT NULL and i.qty > 0" elsif type == "other" - sale_type = "i.item_instance_code IS NULL" + sale_type = "#{check_product} and i.item_instance_code IS NULL" elsif type == "promotion" - sale_type = "i.status = 'promotion'" + sale_type = "#{check_product} and i.status = 'promotion'" end query = Sale.select("acc.title as account_name, i.item_instance_code as item_code,i.account_id as account_id, " + @@ -1005,18 +1008,12 @@ def self.get_item_query(type) "i.unit_price,i.price as price,i.product_name as product_name, " + "i.menu_category_name,i.menu_category_code as menu_category_id ") - # query = query.joins("JOIN sale_items i ON i.sale_id = sales.sale_id" + - # " JOIN menu_item_instances mii ON i.item_instance_code = mii.item_instance_code" + - # " JOIN menu_items mi ON mi.id = mii.menu_item_id" + - # " JOIN shift_sales sh ON sh.`id` = sales.shift_sale_id" + - # " JOIN menu_categories mc ON mc.id = mi.menu_category_id ") - query = query.joins("JOIN sale_items i ON i.sale_id = sales.sale_id" + " JOIN shift_sales sh ON sh.`id` = sales.shift_sale_id") # "JOIN employee_accesses ea ON ea.`employee_id` = sales.cashier_id ") query = query.joins(" JOIN accounts acc ON acc.id = i.account_id") query = query.where("#{sale_type}") - query = query.group("acc.title,i.account_id,i.menu_category_code,i.menu_category_name,i.product_name,i.unit_price") + query = query.group("acc.title,i.account_id,i.menu_category_code,i.item_instance_code,i.product_name,i.unit_price") .order("acc.title desc, i.account_id desc, i.menu_category_code desc, i.unit_price asc") end @@ -1133,7 +1130,8 @@ def self.get_product_sale() "i.status as status_type,"+ " i.unit_price as unit_price,i.product_name as product_name,i.product_code as product_code") query = query.joins("JOIN sale_items i ON i.sale_id = sales.sale_id") - query = query.joins("JOIN products p ON p.`item_code` = i.product_code") + .where("LOWER(i.menu_category_name) = 'product'") + # query = query.joins("JOIN products p ON p.`item_code` = i.product_code") query = query.group("i.product_code") end @@ -1168,28 +1166,18 @@ def self.get_shift_sales_by_receipt_no(shift_sale_range,shift,from,to,payment_ty payment_type = " and sale_payments.payment_method = '#{payment_type}'" end - query = Sale.all + query = Sale.all.select("sales.*,sale_payments.*,df.name,df.type") + .where("#{payment_type} and sale_status= 'completed' and sale_payments.payment_amount != 0") + .joins("join sale_payments on sale_payments.sale_id = sales.sale_id") + .joins("join bookings on bookings.sale_id = sales.sale_id") + .joins("left join dining_facilities df on df.id = bookings.dining_facility_id") + .group("sales.sale_id") if shift.present? - query = query.select("sales.*,sale_payments.*,df.name,df.type") - .where("sales.shift_sale_id in (?) #{payment_type} and sale_status= 'completed' and sale_payments.payment_amount != 0", shift.to_a) - .joins("join sale_payments on sale_payments.sale_id = sales.sale_id") - .joins("join bookings on bookings.sale_id = sales.sale_id") - .joins("left join dining_facilities df on df.id = bookings.dining_facility_id") - .group("sales.sale_id") + query = query.where("sales.shift_sale_id in (?)", shift.to_a) elsif shift_sale_range.present? - query = query.select("sales.*,sale_payments.*,df.name,df.type") - .where("sale_status='completed' #{payment_type} and sale_payments.payment_amount != 0 and sales.shift_sale_id in (?)",shift_sale_range.to_a) - .joins("join sale_payments on sale_payments.sale_id = sales.sale_id") - .joins("join bookings on bookings.sale_id = sales.sale_id") - .joins("left join dining_facilities df on df.id = bookings.dining_facility_id") - .group("sales.sale_id") + query = query.where("sales.shift_sale_id in (?)",shift_sale_range.to_a) else - query = query.select("sales.*,sale_payments.*,df.name,df.type") - .where("sale_status='completed' and sales.receipt_date between ? and ? #{payment_type} and sale_payments.payment_amount != 0",from,to) - .joins("join sale_payments on sale_payments.sale_id = sales.sale_id") - .joins("join bookings on bookings.sale_id = sales.sale_id") - .joins("left join dining_facilities df on df.id = bookings.dining_facility_id") - .group("sales.sale_id") + query = query.where("sales.receipt_date between ? and ?",from,to) end return query end @@ -1203,21 +1191,16 @@ def self.get_shift_sales_by_receipt_no_detail(shift_sale_range,shift,from,to,pay end query = Sale.select("sales.*,bookings.dining_facility_id as table_id") + .where("#{payment_type} and sale_status= 'completed' and sale_payments.payment_amount != 0") + .joins("join sale_payments on sale_payments.sale_id = sales.sale_id") + .joins("join bookings on bookings.sale_id = sales.sale_id") + .group("sales.sale_id") if shift.present? - query = query.where("sales.shift_sale_id in (?) #{payment_type} and sale_status= 'completed' and sale_payments.payment_amount != 0", shift.to_a) - .joins("join sale_payments on sale_payments.sale_id = sales.sale_id") - .joins("join bookings on bookings.sale_id = sales.sale_id") - .group("sales.sale_id") + query = query.where("sales.shift_sale_id in (?)", shift.to_a) elsif shift_sale_range.present? - query = query.where("sale_status='completed' #{payment_type} and sale_payments.payment_amount != 0 and sales.shift_sale_id in (?)",shift_sale_range.to_a) - .joins("join sale_payments on sale_payments.sale_id = sales.sale_id") - .joins("join bookings on bookings.sale_id = sales.sale_id") - .group("sales.sale_id") + query = query.where("sales.shift_sale_id in (?)",shift_sale_range.to_a) else - query = query.where("sale_status='completed' and sales.receipt_date between ? and ? #{payment_type} and sale_payments.payment_amount != 0",from,to) - .joins("join sale_payments on sale_payments.sale_id = sales.sale_id") - .joins("join bookings on bookings.sale_id = sales.sale_id") - .group("sales.sale_id") + query = query.where("sales.receipt_date between ? and ?",from,to) end return query end @@ -1326,19 +1309,14 @@ def self.get_separate_tax(shift_sale_range=nil,shift,from,to,payment_type) payment_type = " and sale_payments.payment_method = '#{payment_type}'" end - if payment_type == '' - query = SaleTax.select("SUM(tax_payable_amount) AS st_amount,tax_name") + query = SaleTax.select("SUM(tax_payable_amount) AS st_amount,tax_name") .where("sale_status= 'completed'") .joins("LEFT JOIN sales ON sales.sale_id = sale_taxes.sale_id") .group("sale_taxes.tax_name") .order("sale_taxes.tax_name desc") - else - query = SaleTax.select("SUM(tax_payable_amount) AS st_amount,tax_name") - .joins("LEFT JOIN sales ON sales.sale_id = sale_taxes.sale_id") - .joins("JOIN sale_payments ON sale_taxes.sale_id = sale_payments.sale_id") - .where("sale_status= 'completed'#{payment_type} and sale_payments.payment_amount!=0") - .group("sale_taxes.tax_name") - .order("sale_taxes.tax_name desc") + + if payment_type != '' + query = query.where("#{payment_type} and sale_payments.payment_amount!=0") end if shift.present? @@ -1446,14 +1424,11 @@ def self.get_wastes_and_spoilages(from,to,status) else type = "and sales.sale_status = 'waste'" end - query = Sale.select("sales.sale_id,sales.receipt_no,sales.created_at,sales.total_amount,sales.grand_total,sales.rounding_adjustment,sales.shift_sale_id,sale_items.product_name,sale_items.product_code,sale_items.item_instance_code,sale_items.qty,sale_items.price,sale_items.unit_price,menu_categories.name") - .joins("JOIN sale_items ON sales.sale_id = sale_items.sale_id" + - " JOIN menu_item_instances ON sale_items.item_instance_code = menu_item_instances.item_instance_code" + - " JOIN menu_items ON menu_item_instances.menu_item_id = menu_items.id" + - " JOIN menu_categories ON menu_items.menu_category_id = menu_categories.id") + query = Sale.select("sales.sale_id,sales.receipt_no,sales.created_at,sales.total_amount,sales.grand_total,sales.rounding_adjustment,sales.shift_sale_id,sale_items.product_name,sale_items.product_code,sale_items.item_instance_code,sale_items.qty,sale_items.price,sale_items.unit_price,sale_items.menu_category_name as name") + .joins("JOIN sale_items ON sales.sale_id = sale_items.sale_id" ) .where("sales.receipt_date between ? and ? #{type}",from,to) - .group("sales.receipt_no,menu_categories.id,sale_items.item_instance_code") - .order("sales.sale_id,menu_categories.name,sale_items.product_name") + .group("sales.receipt_no,sale_items.menu_category_code,sale_items.item_instance_code") + .order("sales.sale_id,sale_items.menu_category_name,sale_items.product_name") end # def self.get_separate_tax(from,to,payment_method=nil) @@ -1510,62 +1485,36 @@ end end def self.top_bottom_products(today,current_user,from,to,from_time,to_time,type) - if !from.nil? && !to.nil? + if (!from.nil? && !to.nil?) && (from != "" && to!="") if current_user.nil? - query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + - " i.price as unit_price,mi.name as product_name") - .joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code") - if !from_time.nil? && !to_time.nil? - query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+ - " and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}' and sale_status= 'completed'") - else - query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+ - " and sale_status= 'completed'") - end + query = Sale.top_bottom(today,nil,from,to,from_time,to_time) + if type == "top" - query = query.group('mi.name') + query = query.group('i.product_name') .order("SUM(i.qty) DESC").limit(20) elsif type == "bottom" - query = query.group('mi.name') + query = query.group('i.product_name') .order("SUM(i.qty) ASC").limit(20) end else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + - " i.price as unit_price,mi.name as product_name") - .joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code") - if !from_time.nil? && !to_time.nil? - query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+ - " and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}' and sale_status= 'completed'") - else - query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+ - " and sale_status= 'completed'") - end + query = Sale.top_bottom(today,nil,from,to,from_time,to_time) if type == "top" - query = query.group('mi.name') + query = query.group('i.product_name') .order("SUM(i.qty) DESC").limit(20) elsif type == "bottom" - query = query.group('mi.name') + query = query.group('i.product_name') .order("SUM(i.qty) ASC").limit(20) end else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? - query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + - " i.price as unit_price,mi.name as product_name") - .joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code") - if !from_time.nil? && !to_time.nil? - query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+ - " and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}' and sale_status= 'completed' and shift_sale_id='#{shift.id}'") - else - query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+ - " and sale_status= 'completed' and shift_sale_id='#{shift.id}'") - end + query = Sale.top_bottom(today,shift,from,to,from_time,to_time) if type == "top" - query = query.group('mi.name') + query = query.group('i.product_name') .order("SUM(i.qty) DESC").limit(20) elsif type == "bottom" - query = query.group('mi.name') + query = query.group('i.product_name') .order("SUM(i.qty) ASC").limit(20) end end @@ -1573,12 +1522,8 @@ end end else if current_user.nil? - query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + - " i.price as unit_price,mi.name as product_name") - .joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code") - .where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+ - " and sale_status= 'completed'") - .group('mi.name') + query = Sale.top_bottom(today).group('i.product_name') + if type == "top" query = query.order("SUM(i.qty) DESC").limit(20) elsif type == "bottom" @@ -1586,12 +1531,7 @@ end end else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - 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') + query = Sale.top_bottom(today).group('i.product_name') if type == "top" query = query.order("SUM(i.qty) DESC").limit(20) elsif type == "bottom" @@ -1600,12 +1540,7 @@ end 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') + query = Sale.top_bottom(today,shift).group('i.product_name') if type == "top" query = query.order("SUM(i.qty) DESC").limit(20) elsif type == "bottom" @@ -1618,59 +1553,29 @@ end end def self.hourly_sales(today,current_user,from,to,from_time,to_time) - if !from.nil? && !to.nil? + if (!from.nil? && !to.nil?) && (from != "" && to!="") 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', '+06:30'), '%I %p')") - .order('receipt_date') + query = Sale.hourly_sale_data(today,nil,from,to,from_time,to_time) else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - 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', '+06:30'), '%I %p')") - .order('receipt_date') + query = Sale.hourly_sale_data(today,nil,from,to,from_time,to_time) 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', '+06:30'), '%I %p')") - .order('receipt_date') + query = Sale.hourly_sale_data(today,shift,from,to,from_time,to_time) end end end else 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', '+06:30'), '%I %p')") - .order('receipt_date') + query = Sale.hourly_sale_data(today) else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - 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', '+06:30'), '%I %p')") - .order('receipt_date') + query = Sale.hourly_sale_data(today) 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', '+06:30'), '%I %p')") - .order('receipt_date') + query = Sale.hourly_sale_data(today,shift) end end end @@ -1685,7 +1590,7 @@ end JOIN sales ON sale_audits.sale_id = sales.sale_id WHERE sale_payments.outstanding_amount LIKE '%-%' AND sales.sale_status='completed'" - if !from.nil? && !to.nil? + if (!from.nil? && !to.nil?) && (from != "" && to!="") if !from_time.nil? && !to_time.nil? outstanding_query += " 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:%i') between '#{from_time}' and '#{to_time}'" else @@ -1701,7 +1606,7 @@ end INNER JOIN sales ON sale_audits.sale_id = sales.sale_id WHERE sales.sale_status='completed'" - if !from.nil? && !to.nil? + if (!from.nil? && !to.nil?) && (from != "" && to!="") if !from_time.nil? && !to_time.nil? sub_query += " 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:%i') between '#{from_time}' and '#{to_time}'" else @@ -1716,7 +1621,7 @@ end FROM `sales` s WHERE s.sale_status='completed'" - if !from.nil? && !to.nil? + if (!from.nil? && !to.nil?) && (from != "" && to!="") if !from_time.nil? && !to_time.nil? changed_query += " AND DATE_FORMAT(CONVERT_TZ(s.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}' AND DATE_FORMAT(CONVERT_TZ(s.receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'" else @@ -1726,117 +1631,78 @@ end changed_query += " AND DATE_FORMAT(CONVERT_TZ(s.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'" end - if !from.nil? && !to.nil? + if (!from.nil? && !to.nil?) && (from != "" && to!="") 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:%i') 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.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (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' or sp.payment_method='alipay' or sp.payment_method='paymal' or sp.payment_method='dinga' or sp.payment_method='JunctionPay' or sp.payment_method='giftvoucher') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") - .group("(CASE WHEN (sp.payment_method='mpu' or sp.payment_method='visa' or sp.payment_method='master' or sp.payment_method='jcb' or sp.payment_method='unionpay' or sp.payment_method='alipay' or sp.payment_method='paymal' or sp.payment_method='dinga' or sp.payment_method='JunctionPay' or sp.payment_method='giftvoucher') THEN 'card' ELSE sp.payment_method END)","e.name") - .order("e.name") + query = Sale.employee_sale(today,nil,from,to,from_time,to_time) else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' changed_query += " AND s.shift_sale_id=sales.shift_sale_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") - 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:%i') 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.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (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' or sp.payment_method='alipay' or sp.payment_method='paymal' or sp.payment_method='dinga' or sp.payment_method='JunctionPay' or sp.payment_method='giftvoucher') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") - .group("(CASE WHEN (sp.payment_method='mpu' or sp.payment_method='visa' or sp.payment_method='master' or sp.payment_method='jcb' or sp.payment_method='unionpay' or sp.payment_method='alipay' or sp.payment_method='paymal' or sp.payment_method='dinga' or sp.payment_method='JunctionPay' or sp.payment_method='giftvoucher') THEN 'card' ELSE sp.payment_method END)","e.name") - .order("e.name") + query = Sale.employee_sale(today,nil,from,to,from_time,to_time) else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? changed_query += " AND s.shift_sale_id=#{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") - 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:%i') 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.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (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' or sp.payment_method='alipay' or sp.payment_method='paymal' or sp.payment_method='dinga' or sp.payment_method='JunctionPay' or sp.payment_method='giftvoucher') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") - .group("(CASE WHEN (sp.payment_method='mpu' or sp.payment_method='visa' or sp.payment_method='master' or sp.payment_method='jcb' or sp.payment_method='unionpay' or sp.payment_method='alipay' or sp.payment_method='paymal' or sp.payment_method='dinga' or sp.payment_method='JunctionPay' or sp.payment_method='giftvoucher') THEN 'card' ELSE sp.payment_method END)","e.name") - .order("e.name") + query = Sale.employee_sale(today,shift,from,to,from_time,to_time) end end end else 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' or sp.payment_method='alipay' or sp.payment_method='paymal' or sp.payment_method='dinga' or sp.payment_method='JunctionPay' or sp.payment_method='giftvoucher') THEN 'card' ELSE sp.payment_method END)","e.name") - .order("e.name") - .select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (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' or sp.payment_method='alipay' or sp.payment_method='paymal' or sp.payment_method='dinga' or sp.payment_method='JunctionPay' or sp.payment_method='giftvoucher') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") + query = Sale.employee_sale(today) else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' changed_query += " AND s.shift_sale_id=sales.shift_sale_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}'") - .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' or sp.payment_method='alipay' or sp.payment_method='paymal' or sp.payment_method='dinga' or sp.payment_method='JunctionPay' or sp.payment_method='giftvoucher') THEN 'card' ELSE sp.payment_method END)","e.name") - .order("e.name") - .select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (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' or sp.payment_method='alipay' or sp.payment_method='paymal' or sp.payment_method='dinga' or sp.payment_method='JunctionPay' or sp.payment_method='giftvoucher') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") + query = Sale.employee_sale(today) else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? changed_query += " AND s.shift_sale_id=#{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' or sp.payment_method='alipay' or sp.payment_method='paymal' or sp.payment_method='dinga' or sp.payment_method='JunctionPay' or sp.payment_method='giftvoucher') THEN 'card' ELSE sp.payment_method END)","e.name") - .order("e.name") - .select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (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' or sp.payment_method='alipay' or sp.payment_method='paymal' or sp.payment_method='dinga' or sp.payment_method='JunctionPay' or sp.payment_method='giftvoucher') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") + query = Sale.employee_sale(today,shift) end end end end + query = query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (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' or sp.payment_method='alipay' or sp.payment_method='paymal' or sp.payment_method='dinga' or sp.payment_method='JunctionPay' or sp.payment_method='giftvoucher') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") end def self.total_trans(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) - if !from.nil? && !to.nil? + query = Sale.select("SUM(grand_total) as total_sale, COUNT(sales.sale_id) as total_count") + .where('sale_status = "completed"') + if (!from.nil? && !to.nil?) && (from != "" && to!="") if current_user.nil? if !from_time.nil? && !to_time.nil? - total = Sale.select("SUM(grand_total) as total_sale, COUNT(sales.sale_id) as total_count").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:%i") between ? and ?',from,to,from_time,to_time) + total = query.where('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:%i") between ? and ?',from,to,from_time,to_time) else - total = Sale.select("SUM(grand_total) as total_sale, COUNT(sales.sale_id) as total_count").where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) + total = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) end else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' if !from_time.nil? && !to_time.nil? - total = Sale.select("SUM(grand_total) as total_sale, COUNT(sales.sale_id) as total_count").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:%i") between ? and ?',from,to,from_time,to_time) + total = query.where('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:%i") between ? and ?',from,to,from_time,to_time) else - total = Sale.select("SUM(grand_total) as total_sale, COUNT(sales.sale_id) as total_count").where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) + total = query.where('DATE_FORMAT(CONVERT_TZ(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? if !from_time.nil? && !to_time.nil? - total = Sale.select("SUM(grand_total) as total_sale, COUNT(sales.sale_id) as total_count").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:%i") between ? and ? and shift_sale_id=?',from,to,from_time,to_time,shift.id) + total = query.where('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:%i") between ? and ? and shift_sale_id=?',from,to,from_time,to_time,shift.id) else - total = Sale.select("SUM(grand_total) as total_sale, COUNT(sales.sale_id) as total_count").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) + total = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and shift_sale_id=?',from,to,shift.id) end end end end else if current_user.nil? - total = Sale.select("SUM(grand_total) as total_sale, COUNT(sales.sale_id) as total_count").where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',today,today) + total = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',today,today) else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - total = Sale.select("SUM(grand_total) as total_sale, COUNT(sales.sale_id) as total_count").where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',today,today) + total = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',today,today) else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? - total = Sale.select("SUM(grand_total) as total_sale, COUNT(sales.sale_id) as total_count").where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and shift_sale_id=?',today,today,shift.id) + total = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and shift_sale_id=?',today,today,shift.id) end end end @@ -1844,7 +1710,7 @@ end end 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 (!from.nil? && !to.nil?) && (from != "" && to!="") if current_user.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:%i") 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" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay")',from,to,from_time,to_time) @@ -1910,7 +1776,7 @@ end JOIN sales ON sale_audits.sale_id = sales.sale_id WHERE sale_payments.outstanding_amount LIKE '%-%' AND sales.sale_status='completed'" - if !from.nil? && !to.nil? + if (!from.nil? && !to.nil?) && (from != "" && to!="") if !from_time.nil? && !to_time.nil? outstanding_query += " 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:%i') between '#{from_time}' and '#{to_time}'" else @@ -2024,7 +1890,7 @@ end end 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 (!from.nil? && !to.nil?) && (from != "" && to!="") if current_user.nil? 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') @@ -2084,7 +1950,7 @@ end end 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 (!from.nil? && !to.nil?) && (from != "" && to!="") if current_user.nil? if !from_time.nil? && !to_time.nil? query = Sale.select("distinct sp.payment_method") @@ -2155,7 +2021,7 @@ end JOIN sales ON sale_audits.sale_id = sales.sale_id WHERE sale_payments.outstanding_amount LIKE '%-%' AND sales.sale_status='completed'" - if !from.nil? && !to.nil? + if (!from.nil? && !to.nil?) && (from != "" && to!="") if !from_time.nil? && !to_time.nil? outstanding_query += " 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:%i') between '#{from_time}' and '#{to_time}'" else @@ -2171,7 +2037,7 @@ end INNER JOIN sales ON sale_audits.sale_id = sales.sale_id WHERE sales.sale_status='completed'" - if !from.nil? && !to.nil? + if (!from.nil? && !to.nil?) && (from != "" && to!="") if !from_time.nil? && !to_time.nil? sub_query += " 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:%i') between '#{from_time}' and '#{to_time}'" else @@ -2185,7 +2051,7 @@ end FROM `sales` s WHERE s.sale_status='completed'" - if !from.nil? && !to.nil? + if (!from.nil? && !to.nil?) && (from != "" && to!="") if !from_time.nil? && !to_time.nil? changed_query += " AND DATE_FORMAT(CONVERT_TZ(s.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}' AND DATE_FORMAT(CONVERT_TZ(s.receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'" else @@ -2195,7 +2061,7 @@ end changed_query += " AND DATE_FORMAT(CONVERT_TZ(s.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'" end - if !from.nil? && !to.nil? + if (!from.nil? && !to.nil?) && (from != "" && to!="") if current_user.nil? if !from_time.nil? && !to_time.nil? query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") @@ -2316,7 +2182,7 @@ end end def self.total_dinein_takeaway(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) - if !from.nil? && !to.nil? + if (!from.nil? && !to.nil?) && (from != "" && to!="") if current_user.nil? if !from_time.nil? && !to_time.nil? query = Sale.select("(CASE WHEN c.customer_type='Dinein' THEN count(sales.customer_id) ELSE 0 END) as total_dinein_cus, (CASE WHEN c.customer_type='Takeaway' THEN count(sales.customer_id) ELSE 0 END) as total_take_cus") @@ -2385,7 +2251,7 @@ end end def self.total_membership(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) - if !from.nil? && !to.nil? + if (!from.nil? && !to.nil?) && (from != "" && to!="") if current_user.nil? if !from_time.nil? && !to_time.nil? query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") @@ -2513,7 +2379,7 @@ end # end def self.total_order(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) - if !from.nil? && !to.nil? + if (!from.nil? && !to.nil?) && (from != "" && to!="") 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") @@ -2582,7 +2448,7 @@ end end def self.total_account(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) - if !from.nil? && !to.nil? + if (!from.nil? && !to.nil?) && (from != "" && to!="") 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") @@ -2642,7 +2508,7 @@ end end 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 (!from.nil? && !to.nil?) && (from != "" && to!="") 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") @@ -2705,7 +2571,7 @@ end end def self.top_items(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) - if !from.nil? && !to.nil? + if (!from.nil? && !to.nil?) && (from != "" && to!="") 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") @@ -2777,7 +2643,7 @@ end end 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 (!from.nil? && !to.nil?) && (from != "" && to!="") if current_user.nil? query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") if !from_time.nil? && !to_time.nil? @@ -3011,6 +2877,84 @@ def unique_tax_profiles(order_source, customer_id) end end +def self.top_bottom(today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil) + 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,i.product_name") + .joins("JOIN sale_items i ON i.sale_id = sales.sale_id") + if !from_time.nil? && !to_time.nil? + query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+ + " and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}' and sale_status= 'completed'") + else + query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+ + " and sale_status= 'completed'") + end + if !shift.nil? + query = query.where("shift_sale_id='#{shift.id}'") + end + else + query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + + " i.price as unit_price,i.product_name") + .joins("JOIN sale_items i ON i.sale_id = sales.sale_id") + .where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+ + " and sale_status= 'completed'") + if !shift.nil? + query = query.where("shift_sale_id='#{shift.id}'") + end + end + return query +end + +def self.hourly_sale_data(today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil) + if !from.nil? && !to.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 + if !shift.nil? + query = query.where("shift_sale_id='#{shift.id}'") + end + query = query.group("date_format(CONVERT_TZ(receipt_date,'+00:00', '+06:30'), '%I %p')") + .order('receipt_date') + else + query = Sale.select("grand_total") + .where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today) + if !shift.nil? + query = query.where("shift_sale_id='#{shift.id}'") + end + query = query.group("date_format(CONVERT_TZ(receipt_date,'+00:00', '+06:30'), '%I %p')") + .order('receipt_date') + end + + return query +end + +def self.employee_sale(today,shift=nil,from=nil,to=nil,from_time=nil,to_time=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' ") + if !from.nil? && !to.nil? + if !from_time.nil? && !to_time.nil? + query = query.where("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:%i') between '#{from_time}' and '#{to_time}'") + else + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'") + end + else + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') = '#{today}'") + end + + if !shift.nil? + query = query.where("sales.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' or sp.payment_method='alipay' or sp.payment_method='paymal' or sp.payment_method='dinga' or sp.payment_method='JunctionPay' or sp.payment_method='giftvoucher') THEN 'card' ELSE sp.payment_method END)","e.name") + .order("e.name") + + return query +end + private def generate_custom_id diff --git a/app/views/home/dashboard.html.erb b/app/views/home/dashboard.html.erb index e63241be..a65ac631 100755 --- a/app/views/home/dashboard.html.erb +++ b/app/views/home/dashboard.html.erb @@ -78,12 +78,12 @@ <% if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' %>
<% - if !@from.nil? + if !@from.nil? && @from != "" from_date = DateTime.parse(@from).utc.getlocal.strftime("%d-%m-%Y") else from_date = Time.now.utc.getlocal.strftime('%d-%m-%Y') end - if !@to.nil? + if !@to.nil? && @to != "" to_date = DateTime.parse(@to).utc.getlocal.strftime("%d-%m-%Y") else to_date = Time.now.utc.getlocal.strftime('%d-%m-%Y') @@ -385,7 +385,6 @@ var to = $("#to").val(); var from_time = $("#from_time").val(); var to_time = $("#to_time").val(); - if(check(from,to,from_time,to_time)){ var params = '?'; if((from!='' && to!='') && (from_time!='' && to_time!='')){ @@ -398,7 +397,9 @@ }); function check(from,to,from_time,to_time){ - var status = true; + var status = false; + var t_status = false; + if((from=='') && (to=='')){ status = false; $('#fromErr').html("can't be blank"); @@ -418,18 +419,19 @@ } if(((from_time=='') && (to_time=='')) || ((from_time!='') && (to_time!=''))){ - status = true; + t_status = true; $('#from_timeErr').html(""); $('#to_timeErr').html(""); }else if((from_time!='') && (to_time=='')){ - status = false; + t_status = false; $('#from_timeErr').html(""); $('#to_timeErr').html("can't be blank"); }else if((from_time=='') && (to_time!='')){ - status = false; + t_status = false; $('#from_timeErr').html("can't be blank"); $('#to_timeErr').html(""); } + if(status && t_status) status = true; return status; } \ No newline at end of file