|
|
|
|
@@ -1088,60 +1088,158 @@ end
|
|
|
|
|
.order("e.name")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.total_sale(today)
|
|
|
|
|
total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).sum("grand_total")
|
|
|
|
|
|
|
|
|
|
def self.total_sale(today,current_user=nil)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
def self.total_count(today)
|
|
|
|
|
total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).count
|
|
|
|
|
def self.total_count(today,current_user=nil)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
def self.total_card_sale(today,current_user=nil)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
def self.credit_payment(today,current_user=nil)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
def self.summary_sale_receipt(today,current_user=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 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
|
|
|
|
|
|
|
|
|
|
def self.total_payment_methods(today)
|
|
|
|
|
query = Sale.select("distinct sp.payment_method")
|
|
|
|
|
def self.total_payment_methods(today,current_user=nil)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
def self.payment_sale(payment_method, today)
|
|
|
|
|
query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
|
|
|
|
|
def self.payment_sale(payment_method, today, current_user=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 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
|
|
|
|
|
|
|
|
|
|
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_customer(today,current_user=nil)
|
|
|
|
|
dinein_cnt = self.total_dinein(today,current_user)
|
|
|
|
|
takeaway_cnt = self.total_takeaway(today,current_user)
|
|
|
|
|
membership_cnt = self.total_membership(today,current_user)
|
|
|
|
|
|
|
|
|
|
total_cus = 0
|
|
|
|
|
if !dinein_cnt.nil? || !takeaway_cnt.nil? || !membership_cnt.nil?
|
|
|
|
|
@@ -1151,69 +1249,226 @@ end
|
|
|
|
|
return total_cus
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.total_dinein(today)
|
|
|
|
|
query = Sale.select("count(sales.customer_id) as total_dinein_cus")
|
|
|
|
|
def self.total_dinein(today,current_user=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 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
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
def self.total_membership(today)
|
|
|
|
|
query = Sale.select("count(distinct sales.customer_id) as total_memb_cus")
|
|
|
|
|
def self.total_membership(today,current_user=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 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
|
|
|
|
|
|
|
|
|
|
def self.total_other_customer(today)
|
|
|
|
|
query = Sale.select("count(sales.customer_id) as total_cus")
|
|
|
|
|
def self.total_other_customer(today,current_user=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 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
|
|
|
|
|
|
|
|
|
|
def self.total_order(today)
|
|
|
|
|
query = Sale.select("count(distinct a.order_id) as total_order")
|
|
|
|
|
def self.total_order(today,current_user=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 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
|
|
|
|
|
|
|
|
|
|
def self.total_account(today)
|
|
|
|
|
query = Sale.select("distinct b.id as account_id, b.title as title")
|
|
|
|
|
def self.total_account(today,current_user=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 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
|
|
|
|
|
|
|
|
|
|
def self.account_data(account_id, today)
|
|
|
|
|
query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc")
|
|
|
|
|
def self.account_data(account_id, today, current_user=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 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
|
|
|
|
|
|
|
|
|
|
def self.top_items(today)
|
|
|
|
|
query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price")
|
|
|
|
|
def self.top_items(today,current_user=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 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
|
|
|
|
|
|
|
|
|
|
def self.total_foc_items(today)
|
|
|
|
|
query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
|
|
|
|
|
def self.total_foc_items(today,current_user=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 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
|
|
|
|
|
|
|
|
|
|
#card sale trans data
|
|
|
|
|
|