From 9daf5c92ef7b0498dc4d2c9a9be7ce786e835b7d Mon Sep 17 00:00:00 2001 From: phyusin Date: Tue, 27 Feb 2018 18:07:15 +0630 Subject: [PATCH] check sale data --- app/models/sale.rb | 876 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 768 insertions(+), 108 deletions(-) diff --git a/app/models/sale.rb b/app/models/sale.rb index d8a91daf..7aa18282 100755 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -1074,95 +1074,376 @@ end return tax end - def self.top_products(today) - query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + + def self.top_products(today,from,to) + if !from.nil? && !to.nil? + query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + + " i.price as unit_price,mi.name as product_name") + .joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code") + .where("(i.qty > 0 and i.price > 0) and receipt_date between '#{from}' and '#{to}'"+ + "and payment_status='paid' and sale_status= 'completed'") + .group('mi.name') + .order("SUM(i.qty) DESC").limit(20) + else + query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + " i.price as unit_price,mi.name as product_name") .joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code") .where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+ "and payment_status='paid' and sale_status= 'completed'") .group('mi.name') - .order("SUM(i.qty) DESC").limit(10) + .order("SUM(i.qty) DESC").limit(20) + end end - def self.bottom_products(today) - query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + + def self.bottom_products(today,from,to) + if !from.nil? && !to.nil? + query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + + " i.price as unit_price,mi.name as product_name") + .joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code") + .where("(i.qty > 0 and i.price > 0) and receipt_date between '#{from}' and '#{to}'"+ + "and payment_status='paid' and sale_status= 'completed'") + .group('mi.name') + .order("SUM(i.qty) ASC").limit(20) + else + query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + " i.price as unit_price,mi.name as product_name") .joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code") .where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+ "and payment_status='paid' and sale_status= 'completed'") .group('mi.name') - .order("SUM(i.qty) ASC").limit(10) + .order("SUM(i.qty) ASC").limit(20) + end end - def self.hourly_sales(today) - query= Sale.select("grand_total") + def self.hourly_sales(today,from,to) + if !from.nil? && !to.nil? + query= Sale.select("grand_total") + .where('payment_status="paid" and sale_status = "completed" and receipt_date between ? and ?',from,to) + .group("date_format(CONVERT_TZ(receipt_date,'+00:00', 'SYSTEM'), '%I %p')") + .order('receipt_date') + else + query= Sale.select("grand_total") .where('payment_status="paid" and sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today) .group("date_format(CONVERT_TZ(receipt_date,'+00:00', 'SYSTEM'), '%I %p')") .order('receipt_date') + end end - def self.employee_sales(today) - query = Sale.joins("JOIN employees as e on e.id=sales.cashier_id") + def self.employee_sales(today,from,to) + if !from.nil? && !to.nil? + query = Sale.joins("JOIN employees as e on e.id=sales.cashier_id") + .joins("JOIN sale_payments as sp on sp.sale_id=sales.sale_id") + .where("sales.payment_status='paid' and sales.sale_status = 'completed' and sales.receipt_date between '#{from}' and '#{to}'") + .group("(CASE WHEN (sp.payment_method='mpu' or sp.payment_method='visa' or sp.payment_method='master' or sp.payment_method='jcb' or sp.payment_method='unionpay') THEN 'card' ELSE sp.payment_method END)","e.name") + .order("e.name") + else + query = Sale.joins("JOIN employees as e on e.id=sales.cashier_id") .joins("JOIN sale_payments as sp on sp.sale_id=sales.sale_id") .where("sales.payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = '#{today}'") .group("(CASE WHEN (sp.payment_method='mpu' or sp.payment_method='visa' or sp.payment_method='master' or sp.payment_method='jcb' or sp.payment_method='unionpay') THEN 'card' ELSE sp.payment_method END)","e.name") .order("e.name") + end end - def self.total_sale(today) - total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).sum("grand_total") - - end - - def self.total_count(today) - total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).count - end - - def self.total_card_sale(today) - payment_type = " and payment_method = 'mpu' or payment_method = 'visa' or payment_method = 'master' or payment_method = 'jcb' or payment_method = 'unionpay' " - # query = Sale.select("SUM(tax_payable_amount) AS st_amount,tax_name") - # .where('sale_status = "completed" #{payment_type} and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today) - # .joins("join sale_payments on sale_id = sales.sale_id") - # .group("sales.sale_id") - - query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) - .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") - .sum("sp.payment_amount") - - end - - def self.credit_payment(today) - query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(s.receipt_date,"%Y-%m-%d") = ?',today) - .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") - .sum("payment_amount") - end - - def self.summary_sale_receipt(today) - query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') - .where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today) - .first() - end - - def self.total_payment_methods(today) - query = Sale.select("distinct sp.payment_method") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) - .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") - end - - def self.payment_sale(payment_method, today) - query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") - if payment_method == 'card' - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) - else - query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + def self.total_sale(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + total = Sale.where('sale_status = "completed" and receipt_date between ? and ?',from,to).sum("grand_total") + else + if current_user.role == 'administrator' + total = Sale.where('sale_status = "completed" and receipt_date between ? and ?',from,to).sum("grand_total") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + total = Sale.where('sale_status = "completed" and sales.receipt_date between ? and ? and shift_sale_id=?',from,to,shift.id) + .sum("grand_total") end - query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + end + end + else + if current_user.nil? + total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).sum("grand_total") + else + if current_user.role == 'administrator' + total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).sum("grand_total") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + total = Sale.where('sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and shift_sale_id=?',today,shift.id) + .sum("grand_total") + end + end + end + end end - def self.total_customer(today) - dinein_cnt = self.total_dinein(today) - takeaway_cnt = self.total_takeaway(today) - membership_cnt = self.total_membership(today) + def self.total_count(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + total = Sale.where('sale_status = "completed" and receipt_date between ? and ?',from,to).count + else + if current_user.role == 'administrator' + total = Sale.where('sale_status = "completed" and receipt_date between ? and ?',from,to).count + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + total = Sale.where('sale_status = "completed" and receipt_date between ? and ? and shift_sale_id = ?',from,to,shift.id).count + end + end + end + else + if current_user.nil? + total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).count + else + if current_user.role == 'administrator' + total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).count + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ? and shift_sale_id = ?',today,shift.id).count + end + end + end + end + end + + def self.total_card_sale(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',from,to) + .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .sum("sp.payment_amount") + else + if current_user.role == 'administrator' + query = Sale.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',from,to) + .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .sum("sp.payment_amount") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay") and shift_sale_id=?',from,to,shift.id) + .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .sum("sp.payment_amount") + end + end + end + else + if current_user.nil? + query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) + .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .sum("sp.payment_amount") + else + if current_user.role == 'administrator' + query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) + .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .sum("sp.payment_amount") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay") and shift_sale_id=?',today,shift.id) + .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .sum("sp.payment_amount") + end + end + end + end + end + + def self.credit_payment(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and s.receipt_date between ? and ?',from,to) + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + .sum("payment_amount") + else + if current_user.role == 'administrator' + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and s.receipt_date between ? and ?',from,to) + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + .sum("payment_amount") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and s.receipt_date between ? and ? and s.shift_sale_id=?',from,to,shift.id) + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + .sum("payment_amount") + end + end + end + else + if current_user.nil? + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(s.receipt_date,"%Y-%m-%d") = ?',today) + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + .sum("payment_amount") + else + if current_user.role == 'administrator' + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(s.receipt_date,"%Y-%m-%d") = ?',today) + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + .sum("payment_amount") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(s.receipt_date,"%Y-%m-%d") = ? and s.shift_sale_id=?',today,shift.id) + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + .sum("payment_amount") + end + end + end + end + end + + def self.summary_sale_receipt(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') + .where('sale_status = "completed" and receipt_date between ? and ?',from,to) + .first() + else + if current_user.role == 'administrator' + query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') + .where('sale_status = "completed" and receipt_date between ? and ?',from,to) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') + .where('sale_status = "completed" and receipt_date between ? and ? and shift_sale_id=?',from,to,shift.id) + .first() + end + end + end + else + if current_user.nil? + query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') + .where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today) + .first() + else + if current_user.role == 'administrator' + query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') + .where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') + .where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ? and shift_sale_id=?',today,shift.id) + .first() + end + end + end + end + end + + def self.total_payment_methods(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("distinct sp.payment_method") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ?',from,to) + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + else + if current_user.role == 'administrator' + query = Sale.select("distinct sp.payment_method") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ?',from,to) + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("distinct sp.payment_method") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and sales.shift_sale_id=?',from,to,shift.id) + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + end + end + end + else + if current_user.nil? + query = Sale.select("distinct sp.payment_method") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + else + if current_user.role == 'administrator' + query = Sale.select("distinct sp.payment_method") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("distinct sp.payment_method") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + end + end + end + end + end + + def self.payment_sale(payment_method, today, current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + if payment_method == 'card' + query = query.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',from,to) + else + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and sales.receipt_date between ? and ?",from,to) + end + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + else + if current_user.role == 'administrator' + query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + if payment_method == 'card' + query = query.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',from,to) + else + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and sales.receipt_date between ? and ?",from,to) + end + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + if payment_method == 'card' + query = query.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay") and sales.shift_sale_id=?',from,to,shift.id) + else + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and sales.receipt_date between ? and ? and sales.shift_sale_id=?",from,to,shift.id) + end + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + end + end + end + else + if current_user.nil? + query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + if payment_method == 'card' + query = query.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) + else + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + end + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + else + if current_user.role == 'administrator' + query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + if payment_method == 'card' + query = query.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) + else + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + end + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + if payment_method == 'card' + query = query.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay") and sales.shift_sale_id=?',today,shift.id) + else + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) + end + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + end + end + end + end + end + + def self.total_customer(today,current_user=nil,from,to) + dinein_cnt = self.total_dinein(today,current_user,from,to) + takeaway_cnt = self.total_takeaway(today,current_user,from,to) + membership_cnt = self.total_membership(today,current_user,from,to) total_cus = 0 if !dinein_cnt.nil? || !takeaway_cnt.nil? || !membership_cnt.nil? @@ -1172,69 +1453,448 @@ end return total_cus end - def self.total_dinein(today) - query = Sale.select("count(sales.customer_id) as total_dinein_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null',today) - .first() + def self.total_dinein(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("count(sales.customer_id) as total_dinein_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Dinein" and c.membership_id is null',from,to) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(sales.customer_id) as total_dinein_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Dinein" and c.membership_id is null',from,to) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(sales.customer_id) as total_dinein_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Dinein" and c.membership_id is null and sales.shift_sale_id=?',from,to,shift.id) + .first() + end + end + end + else + if current_user.nil? + query = Sale.select("count(sales.customer_id) as total_dinein_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null',today) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(sales.customer_id) as total_dinein_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null',today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(sales.customer_id) as total_dinein_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null and sales.shift_sale_id=?',today,shift.id) + .first() + end + end + end + end end - def self.total_takeaway(today) - query = Sale.select("count(sales.customer_id) as total_take_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null',today) - .first() + def self.total_takeaway(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("count(sales.customer_id) as total_take_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Takeaway" and c.membership_id is null',from,to) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(sales.customer_id) as total_take_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Takeaway" and c.membership_id is null',from,to) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(sales.customer_id) as total_take_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Takeaway" and c.membership_id is null and sales.shift_sale_id=?',from,to,shift.id) + .first() + end + end + end + else + if current_user.nil? + query = Sale.select("count(sales.customer_id) as total_take_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null',today) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(sales.customer_id) as total_take_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null',today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(sales.customer_id) as total_take_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null and sales.shift_sale_id=?',today,shift.id) + .first() + end + end + end + end end - def self.total_membership(today) - query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',today) - .first() + def self.total_membership(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',from,to) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',from,to) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null)) and sales.shift_sale_id=?',from,to,shift.id) + .first() + end + end + end + else + if current_user.nil? + query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',today) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null)) and sales.shift_sale_id=?',today,shift.id) + .first() + end + end + end + end end - def self.total_other_customer(today) - query = Sale.select("count(sales.customer_id) as total_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null',today) - .first() + def self.total_other_customer(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("count(sales.customer_id) as total_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type is null and c.membership_id is null',from,to) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(sales.customer_id) as total_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type is null and c.membership_id is null',from,to) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(sales.customer_id) as total_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type is null and c.membership_id is null and sales.shift_sale_id=?',from,to,shift.id) + .first() + end + end + end + else + if current_user.nil? + query = Sale.select("count(sales.customer_id) as total_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null',today) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(sales.customer_id) as total_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null',today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(sales.customer_id) as total_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null and sales.shift_sale_id=?',today,shift.id) + .first() + end + end + end + end end - def self.total_order(today) - query = Sale.select("count(distinct a.order_id) as total_order") - .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") - .joins("JOIN orders as b ON b.order_id = a.order_id") - .where('b.status = "billed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) - .first() + def self.total_order(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("count(distinct a.order_id) as total_order") + .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") + .joins("JOIN orders as b ON b.order_id = a.order_id") + .where('b.status = "billed" and sales.receipt_date between ? and ?',from,to) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(distinct a.order_id) as total_order") + .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") + .joins("JOIN orders as b ON b.order_id = a.order_id") + .where('b.status = "billed" and sales.receipt_date between ? and ?',from,to) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(distinct a.order_id) as total_order") + .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") + .joins("JOIN orders as b ON b.order_id = a.order_id") + .where('b.status = "billed" and sales.receipt_date between ? and ? and sales.shift_sale_id=?',from,to,shift.id) + .first() + end + end + end + else + if current_user.nil? + query = Sale.select("count(distinct a.order_id) as total_order") + .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") + .joins("JOIN orders as b ON b.order_id = a.order_id") + .where('b.status = "billed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(distinct a.order_id) as total_order") + .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") + .joins("JOIN orders as b ON b.order_id = a.order_id") + .where('b.status = "billed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(distinct a.order_id) as total_order") + .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") + .joins("JOIN orders as b ON b.order_id = a.order_id") + .where('b.status = "billed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) + .first() + end + end + end + end end - def self.total_account(today) - query = Sale.select("distinct b.id as account_id, b.title as title") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .joins("JOIN accounts as b ON b.id = a.account_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + def self.total_account(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("distinct b.id as account_id, b.title as title") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .joins("JOIN accounts as b ON b.id = a.account_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ?',from,to) + else + if current_user.role == 'administrator' + query = Sale.select("distinct b.id as account_id, b.title as title") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .joins("JOIN accounts as b ON b.id = a.account_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ?',from,to) + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("distinct b.id as account_id, b.title as title") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .joins("JOIN accounts as b ON b.id = a.account_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and sales.shift_sale_id=?',from,to,shift.id) + end + end + end + else + if current_user.nil? + query = Sale.select("distinct b.id as account_id, b.title as title") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .joins("JOIN accounts as b ON b.id = a.account_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + else + if current_user.role == 'administrator' + query = Sale.select("distinct b.id as account_id, b.title as title") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .joins("JOIN accounts as b ON b.id = a.account_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("distinct b.id as account_id, b.title as title") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .joins("JOIN accounts as b ON b.id = a.account_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) + end + end + end + end end - def self.account_data(account_id, today) - query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - .first() + def self.account_data(account_id, today, current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and sales.receipt_date between ? and ?",from,to) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and sales.receipt_date between ? and ?",from,to) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and sales.receipt_date between ? and ? and sales.shift_sale_id=?",from,to,shift.id) + .first() + end + end + end + else + if current_user.nil? + query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) + .first() + end + end + end + end end - def self.top_items(today) - query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - .group("a.product_code") - .order("SUM(a.qty) DESC") - .first() + def self.top_items(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and sales.receipt_date between ? and ?",from,to) + .group("a.product_code") + .order("SUM(a.qty) DESC") + .first() + else + if current_user.role == 'administrator' + query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and sales.receipt_date between ? and ?",from,to) + .group("a.product_code") + .order("SUM(a.qty) DESC") + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and sales.receipt_date between ? and ? and sales.shift_sale_id=?",from,to,shift.id) + .group("a.product_code") + .order("SUM(a.qty) DESC") + .first() + end + end + end + else + if current_user.nil? + query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + .group("a.product_code") + .order("SUM(a.qty) DESC") + .first() + else + if current_user.role == 'administrator' + query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + .group("a.product_code") + .order("SUM(a.qty) DESC") + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) + .group("a.product_code") + .order("SUM(a.qty) DESC") + .first() + end + end + end + end end - def self.total_foc_items(today) - query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - .sum("a.qty") + def self.total_foc_items(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and sales.receipt_date between ? and ?",from,to) + .sum("a.qty") + else + if current_user.role == 'administrator' + query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and sales.receipt_date between ? and ?",from,to) + .sum("a.qty") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and sales.receipt_date between ? and ? and sales.shift_sale_id=?",from,to,shift.id) + .sum("a.qty") + end + end + end + else + if current_user.nil? + query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + .sum("a.qty") + else + if current_user.role == 'administrator' + query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + .sum("a.qty") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) + .sum("a.qty") + end + end + end + end end #card sale trans data