optimize for dashboards
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
class ShiftSale < ApplicationRecord
|
||||
belongs_to :cashier_terminal
|
||||
belongs_to :employee, :foreign_key => 'employee_id'
|
||||
has_many :sales
|
||||
|
||||
def self.current_shift
|
||||
# today_date = DateTime.now.strftime("%Y-%m-%d")
|
||||
@@ -25,7 +26,7 @@ class ShiftSale < ApplicationRecord
|
||||
def self.current_open_shift(current_user)
|
||||
#if current_user
|
||||
#find open shift where is open today and is not closed and login by current cashier
|
||||
#DATE(shift_started_at)=? and
|
||||
#DATE(shift_started_at)=? and
|
||||
today_date = DateTime.now.strftime("%Y-%m-%d")
|
||||
shift = ShiftSale.where("shift_started_at is not null and shift_closed_at is null and employee_id = #{current_user}").take
|
||||
return shift
|
||||
@@ -43,7 +44,7 @@ class ShiftSale < ApplicationRecord
|
||||
# status = 'updated'
|
||||
if shift_sale.nil?
|
||||
shift_sale = ShiftSale.new
|
||||
# status = 'created'
|
||||
# status = 'created'
|
||||
end
|
||||
|
||||
shift_sale.id = ss['id']
|
||||
@@ -118,7 +119,7 @@ class ShiftSale < ApplicationRecord
|
||||
credit = saleobj.get_credit_amount
|
||||
other_sales = saleobj.get_other_amount
|
||||
tax = saleobj.get_commerical_tax
|
||||
|
||||
|
||||
if type == "void"
|
||||
self.total_revenue = self.total_revenue.to_f - saleobj.total_amount.to_f
|
||||
self.total_discounts = self.total_discounts - saleobj.total_discount
|
||||
@@ -137,7 +138,7 @@ class ShiftSale < ApplicationRecord
|
||||
self.takeaway_count = self.takeaway_count - 1
|
||||
end
|
||||
|
||||
|
||||
|
||||
self.save
|
||||
end
|
||||
end
|
||||
@@ -149,11 +150,11 @@ class ShiftSale < ApplicationRecord
|
||||
end
|
||||
|
||||
def self.get_by_shift_other_payment(shift)
|
||||
|
||||
|
||||
other_payment = Sale.select("sale_payments.payment_method as name,
|
||||
SUM(case when (sale_payments.payment_method='mpu') then (sale_payments.payment_amount) else 0 end) as mpu_amount,
|
||||
SUM(case when (sale_payments.payment_method='visa') then (sale_payments.payment_amount) else 0 end) as visa_amount,
|
||||
SUM(case when (sale_payments.payment_method='master') then (sale_payments.payment_amount) else 0 end) as master_amount,
|
||||
SUM(case when (sale_payments.payment_method='mpu') then (sale_payments.payment_amount) else 0 end) as mpu_amount,
|
||||
SUM(case when (sale_payments.payment_method='visa') then (sale_payments.payment_amount) else 0 end) as visa_amount,
|
||||
SUM(case when (sale_payments.payment_method='master') then (sale_payments.payment_amount) else 0 end) as master_amount,
|
||||
SUM(case when (sale_payments.payment_method='jcb') then (sale_payments.payment_amount) else 0 end) as jcb_amount,
|
||||
SUM(case when (sale_payments.payment_method='unionpay') then (sale_payments.payment_amount) else 0 end) as unionpay_amount,
|
||||
SUM(case when (sale_payments.payment_method='alipay') then (sale_payments.payment_amount) else 0 end) as alipay_amount,
|
||||
@@ -161,19 +162,19 @@ class ShiftSale < ApplicationRecord
|
||||
SUM(case when (sale_payments.payment_method='dinga') then (sale_payments.payment_amount) else 0 end) as dinga_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='JunctionPay') then (sale_payments.payment_amount) else 0 end) as junctionpay_amount,
|
||||
SUM(case when (sale_payments.payment_method='foc') then (sale_payments.payment_amount) else 0 end) as foc_amount,
|
||||
SUM(case when (sale_payments.payment_method='paymal') then (sale_payments.payment_amount) else 0 end) as paymal_amount,
|
||||
SUM(case when (sale_payments.payment_method='foc') then (sale_payments.payment_amount) else 0 end) as foc_amount,
|
||||
SUM(case when (sale_payments.payment_method='paymal') then (sale_payments.payment_amount) else 0 end) as paymal_amount,
|
||||
SUM(case when (sale_payments.payment_method='paypar') then (sale_payments.payment_amount) else 0 end) as paypar_amount")
|
||||
.joins("join sale_payments on sale_payments.sale_id = sales.sale_id")
|
||||
.where("sales.shift_sale_id =? and sale_status = 'completed' and sale_payments.payment_amount != 0 ", shift.id)
|
||||
end
|
||||
|
||||
def self.calculate_total_price_by_accounts(shift,type)
|
||||
query = Sale.select("acc.title as account_name," +
|
||||
query = Sale.select("acc.title as account_name," +
|
||||
"SUM(case when (acc.id=i.account_id) then (i.price) else 0 end) as total_price")
|
||||
|
||||
|
||||
query = query.joins("JOIN sale_items i ON i.sale_id = sales.sale_id "+
|
||||
"JOIN accounts acc ON acc.id = i.account_id" +
|
||||
"JOIN accounts acc ON acc.id = i.account_id" +
|
||||
" JOIN shift_sales sh ON sh.`id` = sales.shift_sale_id")
|
||||
if type == 'discount'
|
||||
query = query.where("sales.shift_sale_id =? and sale_status = 'completed' and i.remark = 'Discount'", shift.id)
|
||||
@@ -187,17 +188,17 @@ class ShiftSale < ApplicationRecord
|
||||
def self.get_total_member_discount(shift)
|
||||
query = Sale.select("SUM(sales.total_discount) as member_discount")
|
||||
.where("shift_sale_id =? and sale_status = 'completed' and discount_type = 'member_discount'", shift.id)
|
||||
|
||||
end
|
||||
|
||||
def self.get_total_dinein(shift)
|
||||
end
|
||||
|
||||
def self.get_total_dinein(shift)
|
||||
query = Sale.select("sum(sales.grand_total) as total_dinein_amount")
|
||||
.joins("JOIN customers as c ON c.customer_id = sales.customer_id")
|
||||
.where('shift_sale_id =? and sales.sale_status = "completed" and c.customer_type = "Dinein" and c.membership_id is null',shift.id)
|
||||
.first()
|
||||
end
|
||||
end
|
||||
|
||||
def self.get_total_takeway(shift)
|
||||
def self.get_total_takeway(shift)
|
||||
query = Sale.select("sum(sales.grand_total) as total_takeway_amount")
|
||||
.joins("JOIN customers as c ON c.customer_id = sales.customer_id")
|
||||
.where('shift_sale_id =? and sales.sale_status = "completed" and c.customer_type = "Takeaway" and c.membership_id is null',shift.id)
|
||||
@@ -211,12 +212,12 @@ class ShiftSale < ApplicationRecord
|
||||
# .where('s.sale_status = "completed" and sale_items.product_code = "Other Charges" and sale_items.item_instance_code is null and s.receipt_date between ? and ?',from, to)
|
||||
# end
|
||||
|
||||
def self.get_total_other_charges(shift)
|
||||
def self.get_total_other_charges(shift)
|
||||
query = SaleItem.select("sum(sale_items.qty * sale_items.unit_price) as total_other_charges_amount")
|
||||
.joins("JOIN sales as s ON s.sale_id = sale_items.sale_id")
|
||||
.where('shift_sale_id =? and s.sale_status = "completed" and sale_items.product_code = "Other Charges" and sale_items.item_instance_code is null',shift.id)
|
||||
.first()
|
||||
end
|
||||
end
|
||||
|
||||
def self.search(filter,from,to)
|
||||
if filter.blank?
|
||||
|
||||
Reference in New Issue
Block a user