|
|
|
|
@@ -29,11 +29,10 @@ class Sale < ApplicationRecord
|
|
|
|
|
scope :complete_sale, -> { where("sale_status = 'completed' and receipt_date BETWEEN '#{DateTime.now.utc.beginning_of_day}' AND '#{DateTime.now.utc.end_of_day}'") }
|
|
|
|
|
scope :paid, -> { where(payment_status: 'paid')}
|
|
|
|
|
scope :completed, -> { where(sale_status: 'completed') }
|
|
|
|
|
scope :date_on, -> (date) { where("DATE(CONVERT_TZ(receipt_date, '+00:00', ?)) = ?", Time.zone.formatted_offset, date) }
|
|
|
|
|
scope :date_between, -> (from, to) { where("DATE(CONVERT_TZ(receipt_date, '+00:00', ?)) BETWEEN ? AND ?", Time.zone.formatted_offset, from, to) }
|
|
|
|
|
scope :time_between, -> (from, to) { where("TIME(CONVERT_TZ(receipt_date, '+00:00', ?)) BETWEEN ? AND ?", Time.zone.formatted_offset, from, to) }
|
|
|
|
|
|
|
|
|
|
scope :along_with_sale_payments_except_void, -> { joins("LEFT JOIN sale_payments on sales.sale_status != 'void' AND sale_payments.sale_id = sales.sale_id AND DATE(CONVERT_TZ(sale_payments.created_at,'+00:00', '#{Time.zone.formatted_offset}')) = DATE(CONVERT_TZ(sales.receipt_date,'+00:00', '#{Time.zone.formatted_offset}'))") }
|
|
|
|
|
scope :receipt_date_between, -> (from, to) { where(receipt_date: from..to) }
|
|
|
|
|
scope :along_with_sale_payments_except_void_between, -> (from, to) { joins(sanitize_sql_array(["LEFT JOIN sale_payments on sales.sale_status != 'void' AND sale_payments.sale_id = sales.sale_id AND sale_payments.created_at BETWEEN ? and ?", from, to])) }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def qty_of(item_instance_code)
|
|
|
|
|
order_items.select(:order_items_id, :item_instance_code, 'SUM(qty) as qty').where(item_instance_code: item_instance_code).group(:item_instance_code).first
|
|
|
|
|
@@ -848,8 +847,9 @@ def self.daily_sales_list(from,to,shop_code)
|
|
|
|
|
ELSE 0 END as credit_amount,
|
|
|
|
|
SUM(case when (sale_payments.payment_method='giftvoucher') then sale_payments.payment_amount else 0 end) as giftvoucher_amount,
|
|
|
|
|
SUM(case when (sale_payments.payment_method='foc') then sale_payments.payment_amount else 0 end) as foc_amount")
|
|
|
|
|
.along_with_sale_payments_except_void
|
|
|
|
|
.where("shop_code='#{shop_code}' and (sale_status = ? OR sale_status = ?) AND sales.receipt_date between ? AND ? ", 'completed', 'void', from, to)
|
|
|
|
|
|
|
|
|
|
.along_with_sale_payments_except_void_between(from, to)
|
|
|
|
|
.where("(sale_status = ? OR sale_status = ?) AND sales.receipt_date between ? AND ? ", 'completed', 'void', from, to)
|
|
|
|
|
.group("sale_id").to_sql
|
|
|
|
|
|
|
|
|
|
daily_total = connection.select_all("SELECT
|
|
|
|
|
@@ -1579,20 +1579,17 @@ end
|
|
|
|
|
return tax
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.top_bottom_products(today,current_user,from,to,from_time,to_time,type, shop)
|
|
|
|
|
|
|
|
|
|
def self.top_bottom_products(current_user,from,to,type)
|
|
|
|
|
query = Sale.joins("JOIN sale_items ON sale_items.sale_id = sales.sale_id")
|
|
|
|
|
.completed
|
|
|
|
|
.where("qty > 0 AND price > 0 AND shop_code='#{shop.shop_code}'")
|
|
|
|
|
.group("SUBSTRING_INDEX(product_name, ' - ', 1)")
|
|
|
|
|
|
|
|
|
|
if !from.nil? && !to.nil?
|
|
|
|
|
query = query.date_between(from, to)
|
|
|
|
|
if !from_time.nil? && !to_time.nil?
|
|
|
|
|
query = query.time_between(from_time, to_time)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
query = query.date_on(today)
|
|
|
|
|
query = query.receipt_date_between(from, to)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
|
|
|
|
|
if shift = ShiftSale.current_open_shift(current_user.id)
|
|
|
|
|
query = query.where("shift_sale_id='#{shift.id}'")
|
|
|
|
|
@@ -1609,17 +1606,14 @@ end
|
|
|
|
|
query.limit(20).sum('qty')
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.hourly_sales(today,current_user,from,to,from_time,to_time, shop)
|
|
|
|
|
query = Sale.group("date_format(CONVERT_TZ(receipt_date,'+00:00', '+06:30'), '%I %p')")
|
|
|
|
|
|
|
|
|
|
def self.hourly_sales(current_user,from,to)
|
|
|
|
|
query = Sale.group("date_format(CONVERT_TZ(receipt_date,'+00:00', '#{Time.zone.formatted_offset}'), '%I %p')")
|
|
|
|
|
|
|
|
|
|
.order('receipt_date').completed
|
|
|
|
|
|
|
|
|
|
if !from.nil? && !to.nil?
|
|
|
|
|
query = query.date_between(from, to)
|
|
|
|
|
if !from_time.nil? && !to_time.nil?
|
|
|
|
|
query = query.time_between(from_time, to_time)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
query = query.date_on(today)
|
|
|
|
|
query = query.receipt_date_between(from, to)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
|
|
|
|
|
@@ -1627,63 +1621,69 @@ end
|
|
|
|
|
query = query.where("shift_sale_id='#{shift.id}'")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
query.sum(:grand_total)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.employee_sales(today,current_user,from,to,from_time,to_time,shop)
|
|
|
|
|
|
|
|
|
|
def self.employee_sales(current_user,from,to)
|
|
|
|
|
|
|
|
|
|
shift = if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
|
|
|
|
|
ShiftSale.current_open_shift(current_user)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
payments_for_credits = SalePayment.joins(:sale_audit).to_sql
|
|
|
|
|
|
|
|
|
|
query = employee_sale(shop,today, shift, from, to, from_time, to_time)
|
|
|
|
|
|
|
|
|
|
query = employee_sale(shift, from, to)
|
|
|
|
|
|
|
|
|
|
.joins("LEFT JOIN (#{payments_for_credits}) payments_for_credits ON payments_for_credits.sale_id = sales.sale_id")
|
|
|
|
|
.select("SUM(sale_payments.payment_amount) - CASE WHEN sale_payments.payment_method = 'creditnote' THEN IFNULL(SUM(payments_for_credits.payment_amount), 0) ELSE ABS(SUM(CASE WHEN sale_payments.outstanding_amount < 0 THEN sale_payments.outstanding_amount ELSE 0 END)) END AS payment_amount,
|
|
|
|
|
CASE WHEN sale_payments.payment_method IN ('mpu', 'visa', 'master', 'jcb', 'unionpay', 'alipay', 'paymal', 'dinga', 'JunctionPay', 'giftvoucher') THEN 'card'
|
|
|
|
|
ELSE sale_payments.payment_method END AS payment_method, employees.name AS e_name")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.total_trans(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil,shop)
|
|
|
|
|
query = Sale.select("SUM(grand_total) as total_sale, COUNT(sales.sale_id) as total_count")
|
|
|
|
|
.where("sale_status = 'completed' AND shop_code='#{shop.shop_code}'")
|
|
|
|
|
if (!from.nil? && !to.nil?) && (from != "" && to!="")
|
|
|
|
|
|
|
|
|
|
query = query.date_between(from, to)
|
|
|
|
|
if !from_time.nil? && !to_time.nil?
|
|
|
|
|
query = query.time_between(from_time, to_time)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
query = query.date_on(today)
|
|
|
|
|
def self.total_trans(current_user=nil,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?)
|
|
|
|
|
query = query.receipt_date_between(from, to)
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
|
|
|
|
|
if shift = ShiftSale.current_open_shift(current_user)
|
|
|
|
|
query = query.where("sales.shift_sale_id = ?", shift.id)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return query
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.total_card_sale(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil,shop)
|
|
|
|
|
|
|
|
|
|
def self.total_card_sale(current_user=nil,from=nil,to=nil)
|
|
|
|
|
query = Sale.joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id")
|
|
|
|
|
.where("sales.shop_code='#{shop.shop_code}' and sales.sale_status = 'completed' 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')")
|
|
|
|
|
if (!from.nil? && !to.nil?) && (from != "" && to!="")
|
|
|
|
|
query = query.date_between(from, to)
|
|
|
|
|
if !from_time.nil? && !to_time.nil?
|
|
|
|
|
query = query.time_between(from_time, to_time)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
query = query.date_on(today)
|
|
|
|
|
.where('sales.sale_status = "completed" 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")')
|
|
|
|
|
|
|
|
|
|
if (!from.nil? && !to.nil?)
|
|
|
|
|
query = query.receipt_date_between(from, to)
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
|
|
|
|
|
if shift = ShiftSale.current_open_shift(current_user)
|
|
|
|
|
query = query.where("sales.shift_sale_id = ?", shift.id)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
query = query.sum("sp.payment_amount")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.credit_payment(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil,shop)
|
|
|
|
|
|
|
|
|
|
def self.credit_payment(current_user=nil,from=nil,to=nil)
|
|
|
|
|
|
|
|
|
|
payments_for_credits = SalePayment.joins(:sale_audit).to_sql
|
|
|
|
|
|
|
|
|
|
query = SalePayment.credits
|
|
|
|
|
@@ -1691,14 +1691,10 @@ end
|
|
|
|
|
.joins("LEFT JOIN (#{payments_for_credits}) payments_for_credits ON payments_for_credits.sale_id = sale_payments.sale_id")
|
|
|
|
|
.where("sale_payments.payment_method= ? AND sales.sale_status = ? AND sales.shop_code=?", 'creditnote', 'completed',shop.shop_code)
|
|
|
|
|
|
|
|
|
|
if (!from.nil? && !to.nil?) && (from != "" && to!="")
|
|
|
|
|
query = query.merge(Sale.date_between(from, to))
|
|
|
|
|
if !from_time.nil? && !to_time.nil?
|
|
|
|
|
query = query.merge(Sale.time_between(from_time, to_time))
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
query = query.merge(Sale.date_on(today))
|
|
|
|
|
if (!from.nil? && !to.nil?)
|
|
|
|
|
query = query.merge(Sale.receipt_date_between(from, to))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
|
|
|
|
|
if shift = ShiftSale.current_open_shift(current_user)
|
|
|
|
|
query = query.where("sales.shift_sale_id = ?", shift.id)
|
|
|
|
|
@@ -1708,46 +1704,49 @@ end
|
|
|
|
|
return query.sum("sale_payments.payment_amount - IFNULL(payments_for_credits.payment_amount, 0)")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.summary_sale_receipt(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
|
|
|
|
|
|
|
|
|
|
def self.summary_sale_receipt(current_user=nil,from=nil,to=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("shop_code='#{shop.shop_code}' and sale_status = 'completed'")
|
|
|
|
|
if (!from.nil? && !to.nil?) && (from != "" && to!="")
|
|
|
|
|
query = query.date_between(from, to)
|
|
|
|
|
if !from_time.nil? && !to_time.nil?
|
|
|
|
|
query = query.time_between(from_time, to_time)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
query = query.date_on(today)
|
|
|
|
|
.where('sale_status = "completed"')
|
|
|
|
|
|
|
|
|
|
if (!from.nil? && !to.nil?)
|
|
|
|
|
query = query.receipt_date_between(from, to)
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
|
|
|
|
|
if shift = ShiftSale.current_open_shift(current_user)
|
|
|
|
|
query = query.where("sales.shift_sale_id = ?", shift.id)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
query = query.first()
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.total_payment_methods(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
|
|
|
|
|
query = Sale.select("distinct sp.payment_method")
|
|
|
|
|
.where("sales.shop_code='#{shop.shop_code}' and sales.sale_status = 'completed'")
|
|
|
|
|
|
|
|
|
|
def self.total_payment_methods(current_user=nil,from=nil,to=nil)
|
|
|
|
|
query = Sale.select("CASE WHEN sp.payment_method IN ('mpu', 'visa', 'master', 'jcb', 'unionpay', 'alipay', 'paymal', 'dinga', 'JunctionPay', 'giftvoucher') THEN 'card' ELSE sp.payment_method END as payment_method")
|
|
|
|
|
.where("sales.sale_status = 'completed'")
|
|
|
|
|
|
|
|
|
|
.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
|
|
|
|
|
if (!from.nil? && !to.nil?) && (from != "" && to!="")
|
|
|
|
|
query = query.date_between(from, to)
|
|
|
|
|
if !from_time.nil? && !to_time.nil?
|
|
|
|
|
query = query.time_between(from_time, to_time)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
query = query.date_on(today)
|
|
|
|
|
.group("CASE WHEN sp.payment_method IN ('mpu', 'visa', 'master', 'jcb', 'unionpay', 'alipay', 'paymal', 'dinga', 'JunctionPay', 'giftvoucher') THEN 'card' ELSE sp.payment_method END")
|
|
|
|
|
|
|
|
|
|
if (!from.nil? && !to.nil?)
|
|
|
|
|
query = query.receipt_date_between(from, to)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
|
|
|
|
|
if shift = ShiftSale.current_open_shift(current_user)
|
|
|
|
|
query = query.where("sales.shift_sale_id = ?", shift.id)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return query
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.payment_sale(shop,payment_method, today, current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
|
|
|
|
|
|
|
|
|
|
def self.payment_sale(payment_method, current_user=nil,from=nil,to=nil)
|
|
|
|
|
|
|
|
|
|
payments_for_credits = SalePayment.joins(:sale_audit).to_sql
|
|
|
|
|
|
|
|
|
|
query = Sale.select("SUM(sale_payments.payment_amount) - CASE WHEN sale_payments.payment_method = 'creditnote' THEN IFNULL(SUM(payments_for_credits.payment_amount), 0) ELSE ABS(SUM(CASE WHEN sale_payments.outstanding_amount < 0 THEN sale_payments.outstanding_amount ELSE 0 END)) END AS payment_amount")
|
|
|
|
|
@@ -1762,13 +1761,8 @@ end
|
|
|
|
|
query = query.where("sale_payments.payment_method = ?", payment_method)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if (!from.nil? && !to.nil?) && (from != "" && to!="")
|
|
|
|
|
query = query.date_between(from, to)
|
|
|
|
|
if !from_time.nil? && !to_time.nil?
|
|
|
|
|
query = query.time_between(from_time,to_time)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
query = query.date_on(today)
|
|
|
|
|
if (!from.nil? && !to.nil?)
|
|
|
|
|
query = query.receipt_date_between(from, to)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
|
|
|
|
|
@@ -1776,25 +1770,35 @@ end
|
|
|
|
|
query = query.where("sales.shift_sale_id = ?", shift.id)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
query.first
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.total_customer(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
|
|
|
|
|
total_dinein_takeaway = self.total_dinein_takeaway(shop,today,current_user,from,to,from_time,to_time)
|
|
|
|
|
|
|
|
|
|
def self.total_customer(current_user=nil,from=nil,to=nil)
|
|
|
|
|
total_dinein_takeaway = self.total_dinein_takeaway(current_user,from,to)
|
|
|
|
|
|
|
|
|
|
dinein_cnt = 0
|
|
|
|
|
takeaway_cnt = 0
|
|
|
|
|
|
|
|
|
|
if !total_dinein_takeaway.nil?
|
|
|
|
|
if total_dinein_takeaway[0]
|
|
|
|
|
dinein_cnt = total_dinein_takeaway[0].total_dinein_cus
|
|
|
|
|
takeaway_cnt = total_dinein_takeaway[0].total_take_cus
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
membership_cnt = self.total_membership(shop,today,current_user,from,to,from_time,to_time)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
membership_cnt = self.total_membership(current_user,from,to)
|
|
|
|
|
|
|
|
|
|
member_cnt = 0
|
|
|
|
|
|
|
|
|
|
if !membership_cnt.nil?
|
|
|
|
|
member_cnt = membership_cnt.total_memb_cus
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
total_cus = 0
|
|
|
|
|
|
|
|
|
|
if dinein_cnt > dinein_cnt || takeaway_cnt > 0 || !membership_cnt.nil?
|
|
|
|
|
total_cus = dinein_cnt.to_int + takeaway_cnt.to_int + member_cnt.to_int
|
|
|
|
|
end
|
|
|
|
|
@@ -1802,43 +1806,43 @@ end
|
|
|
|
|
return total_cus, dinein_cnt, takeaway_cnt, member_cnt
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.total_dinein_takeaway(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
|
|
|
|
|
|
|
|
|
|
def self.total_dinein_takeaway(current_user=nil,from=nil,to=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")
|
|
|
|
|
.joins("JOIN customers as c ON c.customer_id = sales.customer_id")
|
|
|
|
|
.where("sales.shop_code='#{shop.shop_code}' and sales.sale_status = 'completed' and c.membership_id is null")
|
|
|
|
|
if (!from.nil? && !to.nil?) && (from != "" && to!="")
|
|
|
|
|
query = query.date_between(from, to)
|
|
|
|
|
if !from_time.nil? && !to_time.nil?
|
|
|
|
|
query = query.time_between(from_time, to_time)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
query = query.date_on(today)
|
|
|
|
|
.where('sales.sale_status = "completed" and c.membership_id is null')
|
|
|
|
|
|
|
|
|
|
if (!from.nil? && !to.nil?)
|
|
|
|
|
query = query.receipt_date_between(from, to)
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
|
|
|
|
|
if shift = ShiftSale.current_open_shift(current_user)
|
|
|
|
|
query = query.where("sales.shift_sale_id = ?", shift.id)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
query = query.first()
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.total_membership(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
|
|
|
|
|
|
|
|
|
|
def self.total_membership(current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
|
|
|
|
|
query = Sale.select("count(distinct sales.customer_id) as total_memb_cus")
|
|
|
|
|
.joins("JOIN customers as c ON c.customer_id = sales.customer_id")
|
|
|
|
|
.where("sales.shop_code='#{shop.shop_code}' and sales.sale_status = 'completed' and ((c.customer_type = 'Dinein' and c.membership_id is not null) or (c.customer_type = 'Takeaway' and c.membership_id is not null))")
|
|
|
|
|
if (!from.nil? && !to.nil?) && (from != "" && to!="")
|
|
|
|
|
query = query.date_between(from, to)
|
|
|
|
|
if !from_time.nil? && !to_time.nil?
|
|
|
|
|
query = query.time_between(from_time, to_time)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
query = query.date_on(today)
|
|
|
|
|
.where('sales.sale_status = "completed" and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))')
|
|
|
|
|
|
|
|
|
|
if (!from.nil? && !to.nil?)
|
|
|
|
|
query = query.receipt_date_between(from, to)
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
|
|
|
|
|
if shift = ShiftSale.current_open_shift(current_user)
|
|
|
|
|
query = query.where("sales.shift_sale_id = ?", shift.id)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
query = query.first()
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@@ -1888,18 +1892,16 @@ end
|
|
|
|
|
# query = query.first()
|
|
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
def self.total_order(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
|
|
|
|
|
|
|
|
|
|
def self.total_order(current_user=nil,from=nil,to=nil)
|
|
|
|
|
|
|
|
|
|
query = Sale.select("count(distinct sale_orders.order_id) as total_order")
|
|
|
|
|
.joins(:sale_orders)
|
|
|
|
|
.where("shop_code='#{shop.shop_code}'")
|
|
|
|
|
.completed
|
|
|
|
|
if (!from.nil? && !to.nil?) && (from != "" && to!="")
|
|
|
|
|
query = query.date_between(from, to)
|
|
|
|
|
if !from_time.nil? && !to_time.nil?
|
|
|
|
|
query = query.time_between(from_time, to_time)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
query = query.date_on(today)
|
|
|
|
|
|
|
|
|
|
if (!from.nil? && !to.nil?)
|
|
|
|
|
query = query.receipt_date_between(from, to)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
|
|
|
|
|
@@ -1911,64 +1913,64 @@ end
|
|
|
|
|
query = query.first
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.account_data(shop, account_id, today, current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
|
|
|
|
|
|
|
|
|
|
def self.account_data(account_id, current_user=nil,from=nil,to=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.shop_code='#{shop.shop_code}' and sales.sale_status = 'completed' and a.account_id ='#{account_id}'")
|
|
|
|
|
if (!from.nil? && !to.nil?) && (from != "" && to!="")
|
|
|
|
|
query = query.date_between(from, to)
|
|
|
|
|
if !from_time.nil? && !to_time.nil?
|
|
|
|
|
query = query.time_between(from_time, to_time)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
query = query.date_on(today)
|
|
|
|
|
.where("sales.sale_status = 'completed' and a.account_id ='#{account_id}'")
|
|
|
|
|
|
|
|
|
|
if (!from.nil? && !to.nil?)
|
|
|
|
|
query = query.receipt_date_between(from, to)
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
|
|
|
|
|
if shift = ShiftSale.current_open_shift(current_user)
|
|
|
|
|
query = query.where("sales.shift_sale_id = ?", shift.id)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
query = query.first
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.top_items(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
|
|
|
|
|
|
|
|
|
|
def self.top_items(current_user=nil,from=nil,to=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("sales.shop_code='#{shop.shop_code}' and (a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed'")
|
|
|
|
|
if (!from.nil? && !to.nil?) && (from != "" && to!="")
|
|
|
|
|
query = query.date_between(from, to)
|
|
|
|
|
if !from_time.nil? && !to_time.nil?
|
|
|
|
|
query = query.time_between(from_time, to_time)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
query = query.date_on(today)
|
|
|
|
|
.where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed'")
|
|
|
|
|
|
|
|
|
|
if (!from.nil? && !to.nil?)
|
|
|
|
|
query = query.receipt_date_between(from, to)
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
|
|
|
|
|
if shift = ShiftSale.current_open_shift(current_user)
|
|
|
|
|
query = query.where("sales.shift_sale_id = ?", shift.id)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
query = query.group("a.product_code")
|
|
|
|
|
.order("SUM(a.qty) DESC")
|
|
|
|
|
.first()
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.total_foc_items(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
|
|
|
|
|
|
|
|
|
|
def self.total_foc_items(current_user=nil,from=nil,to=nil)
|
|
|
|
|
query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
|
|
|
|
|
.where("sales.shop_code='#{shop.shop_code}' and sales.sale_status = 'completed' and a.status='foc' and a.product_name like '%FOC%'")
|
|
|
|
|
if (!from.nil? && !to.nil?) && (from != "" && to!="")
|
|
|
|
|
query = query.date_between(from, to)
|
|
|
|
|
if !from_time.nil? && !to_time.nil?
|
|
|
|
|
query = query.time_between(from_time, to_time)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
query = query.date_on(today)
|
|
|
|
|
.where("sales.sale_status = 'completed' and a.status='foc' and a.product_name like '%FOC%'")
|
|
|
|
|
|
|
|
|
|
if (!from.nil? && !to.nil?)
|
|
|
|
|
query = query.receipt_date_between(from, to)
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
|
|
|
|
|
if shift = ShiftSale.current_open_shift(current_user)
|
|
|
|
|
query = query.where("sales.shift_sale_id = ?", shift.id)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
query = query.count()
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@@ -2107,17 +2109,13 @@ def unique_tax_profiles(order_source, customer_id)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def self.employee_sale(shop, today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil)
|
|
|
|
|
def self.employee_sale(shift=nil,from=nil,to=nil,from_time=nil,to_time=nil)
|
|
|
|
|
|
|
|
|
|
query = Sale.joins(:cashier)
|
|
|
|
|
.joins(:sale_payments)
|
|
|
|
|
.paid.completed.where("sales.shop_code='#{shop.shop_code}'")
|
|
|
|
|
if !from.nil? && !to.nil?
|
|
|
|
|
query = query.date_between(from, to)
|
|
|
|
|
if !from_time.nil? && !to_time.nil?
|
|
|
|
|
query = query.time_between(from_time, to_time)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
query = query.date_on(today)
|
|
|
|
|
query = query.receipt_date_between(from, to)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if !shift.nil?
|
|
|
|
|
|