update number_formattable. update dashboard query.

This commit is contained in:
Thein Lin Kyaw
2019-11-28 14:13:00 +06:30
parent 47c12c6faa
commit 2010b6a4bb
8 changed files with 225 additions and 319 deletions

View File

@@ -29,10 +29,8 @@ 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
@@ -849,7 +847,7 @@ def self.daily_sales_list(from,to)
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
.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
@@ -1577,20 +1575,16 @@ end
return tax
end
def self.top_bottom_products(today,current_user,from,to,from_time,to_time,type)
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")
.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}'")
@@ -1606,17 +1600,12 @@ end
query.limit(20).sum('qty')
end
def self.hourly_sales(today,current_user,from,to,from_time,to_time)
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')
@@ -1624,62 +1613,59 @@ 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)
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.id)
end
payments_for_credits = SalePayment.joins(:sale_audit).to_sql
query = employee_sale(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)
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?) && (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')
if shift = ShiftSale.current_open_shift(current_user.id)
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)
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.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)
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.id)
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)
def self.credit_payment(current_user=nil,from=nil,to=nil)
payments_for_credits = SalePayment.joins(:sale_audit).to_sql
query = SalePayment.credits
@@ -1687,14 +1673,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 = ?", 'creditnote', 'completed')
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.id)
query = query.where("sales.shift_sale_id = ?", shift.id)
@@ -1704,46 +1686,43 @@ end
return query.sum("sale_payments.payment_amount - IFNULL(payments_for_credits.payment_amount, 0)")
end
def self.summary_sale_receipt(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('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)
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.id)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
query = query.first()
end
def self.total_payment_methods(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = Sale.select("distinct sp.payment_method")
.where('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.id)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
return query
end
def self.payment_sale(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")
@@ -1757,13 +1736,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')
@@ -1771,25 +1745,31 @@ end
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
query.first
end
def self.total_customer(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
total_dinein_takeaway = self.total_dinein_takeaway(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(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
@@ -1797,43 +1777,39 @@ end
return total_cus, dinein_cnt, takeaway_cnt, member_cnt
end
def self.total_dinein_takeaway(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.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)
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.id)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
query = query.first()
end
def self.total_membership(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.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)
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.id)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
query = query.first()
end
@@ -1883,17 +1859,13 @@ end
# query = query.first()
# end
def self.total_order(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)
.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')
@@ -1905,64 +1877,58 @@ end
query = query.first
end
def self.account_data(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.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)
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.id)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
query = query.first
end
def self.top_items(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("(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)
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.id)
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(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.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)
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.id)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
query = query.count()
end
@@ -2101,17 +2067,12 @@ def unique_tax_profiles(order_source, customer_id)
return tax_data
end
def self.employee_sale(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
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?

View File

@@ -4,6 +4,8 @@ class StockJournal < ApplicationRecord
ORDER_TRANS = "order"
STOCK_CHECK_TRANS = "stock_check"
scope :created_at_between, -> (from, to) { where(created_at: from..to)}
def self.add_to_journal(item_instance_code, qty, old_balance, stock_message, inventory_definition, trans_ref, trans_type) # item => saleObj | balance => Stock journal
balance = calculate_balance(old_balance, qty)
@@ -52,23 +54,13 @@ class StockJournal < ApplicationRecord
journal.save
end
def self.inventory_balances(today,from,to,from_time,to_time)
def self.inventory_balances(from,to)
query = StockJournal.select("mii.item_instance_name as item_instance_name,balance")
.joins("join menu_item_instances mii on mii.item_instance_code=stock_journals.item_code")
.group("mii.item_instance_name")
.order("mii.item_instance_name ASC")
if !from.nil? && !to.nil?
query = StockJournal.select("mii.item_instance_name as item_instance_name,balance")
.joins("join menu_item_instances mii on mii.item_instance_code=stock_journals.item_code")
if !from_time.nil? && !to_time.nil?
query = query.where("DATE_FORMAT(CONVERT_TZ(stock_journals.created_at,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'")
else
query = query.where("DATE_FORMAT(CONVERT_TZ(stock_journals.created_at,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and DATE_FORMAT(CONVERT_TZ(stock_journals.created_at,'+00:00','+06:30'),'%H:%M') between '#{from_time}' and '#{to_time}'")
end
query = query.group("mii.item_instance_name")
.order("mii.item_instance_name ASC")
else
query = StockJournal.select("mii.item_instance_name as item_instance_name,balance")
.joins("join menu_item_instances mii on mii.item_instance_code=stock_journals.item_code")
.where("DATE_FORMAT(stock_journals.created_at,'%Y-%m-%d') = '#{today}'")
.group("mii.item_instance_name")
.order("mii.item_instance_name ASC")
query = query.created_at_between(from, to)
end
end