diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index c0775008..d3666a60 100755 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -72,7 +72,8 @@ class HomeController < ApplicationController @hourly_sales = Sale.hourly_sales(today).sum(:grand_total) # .group_by_hour(:created_at, :time_zone => 'Asia/Rangoon',format: '%I:%p') # .sum(:grand_total) - @employee_sales = Sale.employee_sales(today).sum('payment_amount') + @employee_sales = Sale.employee_sales(today) + .sum('(CASE WHEN sp.payment_method="cash" THEN (sp.payment_amount - sales.amount_changed) ELSE sp.payment_amount END)') @inventories = StockJournal.inventory_balances(today).sum(:balance) @total_sale = Sale.total_sale(today) diff --git a/app/models/dining_facility.rb b/app/models/dining_facility.rb index 89f5796e..595f0e0f 100755 --- a/app/models/dining_facility.rb +++ b/app/models/dining_facility.rb @@ -79,7 +79,6 @@ class DiningFacility < ApplicationRecord booking = self.get_current_checkout_booking if booking now = Time.now.utc - puts now hr = (now.strftime("%H").to_i).to_int min = (now.strftime("%M").to_i).to_int if !booking.checkout_at.nil? diff --git a/app/models/sale.rb b/app/models/sale.rb index e92b1bb5..5816dc96 100755 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -984,12 +984,11 @@ end end def self.employee_sales(today) - query = Sale.select('(CASE WHEN (sp.payment_method="mpu" or sp.payment_method="visa" or sp.payment_method="master" or sp.payment_method="jcb") THEN "card" ELSE sp.payment_method END) as payment_name, (CASE WHEN sp.payment_method="cash" THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount') - .joins("join employees e on e.id=sales.cashier_id") - .joins("join sale_payments 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('payment_method','e.name') - .order('e.name') + 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') THEN 'card' ELSE sp.payment_method END)","e.name") + .order("e.name") end def self.total_sale(today) @@ -1043,11 +1042,16 @@ end end def self.total_customer(today) - query = Sale.select("(CASE WHEN (c.customer_type='Dinein' or c.customer_type='Takeaway') THEN count(sales.customer_id) ELSE count(distinct sales.customer_id) END) 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") = ?',today) - .order('sales.sale_id ASC') - .first() + dinein_cnt = self.total_dinein(today) + takeaway_cnt = self.total_takeaway(today) + membership_cnt = self.total_membership(today) + + total_cus = 0 + if !dinein_cnt.nil? || !takeaway_cnt.nil? || !membership_cnt.nil? + total_cus = dinein_cnt.total_dinein_cus.to_int + takeaway_cnt.total_take_cus.to_int + membership_cnt.total_memb_cus.to_int + end + + return total_cus end def self.total_dinein(today) @@ -1067,7 +1071,7 @@ 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 = "Takeaway" and c.membership_id is not null',today) + .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() end @@ -1111,8 +1115,8 @@ 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 DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - .count() + .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") end private diff --git a/app/views/home/dashboard.html.erb b/app/views/home/dashboard.html.erb index a42bce49..f7508f7d 100755 --- a/app/views/home/dashboard.html.erb +++ b/app/views/home/dashboard.html.erb @@ -56,7 +56,7 @@ <% if current_user.role == 'administrator' || current_user.role == 'manager' %>
-
+
@@ -111,7 +111,7 @@
-
+
<% if !@summ_sale.nil? %>
@@ -185,7 +185,7 @@ <% if !@total_customer.nil? %> <%= t("views.right_panel.detail.total") %> <%= t :customer %> : - <%= @total_customer.total_cus %> + <%= @total_customer %> <% end %> <% if !@total_dinein.nil? %>