From 211db16311b376966311addb862afacf5912479b Mon Sep 17 00:00:00 2001 From: phyusin Date: Wed, 13 Dec 2017 15:58:44 +0630 Subject: [PATCH] add channel for checkin process --- app/assets/javascripts/channels/checkin.js | 25 ++++++++++++++++++++++ app/channels/checkin_channel.rb | 10 +++++++++ app/controllers/base_origami_controller.rb | 5 +++++ app/controllers/home_controller.rb | 2 +- app/jobs/checkin_job.rb | 10 +++++++++ app/models/dining_facility.rb | 22 +++++++++++++++++++ app/models/sale.rb | 6 +++--- spec/jobs/checkin_job_spec.rb | 5 +++++ 8 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 app/assets/javascripts/channels/checkin.js create mode 100644 app/channels/checkin_channel.rb create mode 100644 app/jobs/checkin_job.rb create mode 100644 spec/jobs/checkin_job_spec.rb diff --git a/app/assets/javascripts/channels/checkin.js b/app/assets/javascripts/channels/checkin.js new file mode 100644 index 00000000..859cb733 --- /dev/null +++ b/app/assets/javascripts/channels/checkin.js @@ -0,0 +1,25 @@ +App.order = App.cable.subscriptions.create('CheckinChannel', { +// App.messages = App.cable.subscriptions.create('MessagesChannel', { + + connected: function() {}, + + disconnected: function() {}, + + received: function(data) { + if((data.table != undefined) && (data.table.length > 0)){ + $.each(data.table,function(key,value){ + if($('.table_'+value.table_id).hasClass('blue')){ + $('.table_'+value.table_id).removeClass('blue'); + $('.table_'+value.table_id).addClass('orange'); + } + else if($('.table_'+value.table_id).hasClass('red')){ + $('.table_'+value.table_id).removeClass('red'); + $('.table_'+value.table_id).addClass('orange'); + } + $('.new_text_'+value.table_id).removeClass('hide'); + }); + + } + } +}); + diff --git a/app/channels/checkin_channel.rb b/app/channels/checkin_channel.rb new file mode 100644 index 00000000..c2e78132 --- /dev/null +++ b/app/channels/checkin_channel.rb @@ -0,0 +1,10 @@ +class CheckinChannel < ApplicationCable::Channel + def subscribed + stream_from "checkin_channel" + end + + def unsubscribed + stop_all_streams + # Any cleanup needed when channel is unsubscribed + end +end diff --git a/app/controllers/base_origami_controller.rb b/app/controllers/base_origami_controller.rb index 979fdf02..f9688235 100755 --- a/app/controllers/base_origami_controller.rb +++ b/app/controllers/base_origami_controller.rb @@ -2,6 +2,8 @@ class BaseOrigamiController < ActionController::Base include LoginVerification layout "origami" + before_action :checkin_process + #before_action :check_installation protect_from_forgery with: :exception rescue_from CanCan::AccessDenied do |exception| @@ -13,4 +15,7 @@ class BaseOrigamiController < ActionController::Base @current_user ||= Employee.find_by_token_session(session[:session_token]) if session[:session_token] end + def checkin_process + CheckinJob.set(wait: 30.seconds).perform_later() + end end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 35308c5f..64a6c587 100755 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -72,7 +72,7 @@ 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(:grand_total) + @employee_sales = Sale.employee_sales(today).sum('total_amount') @inventories = StockJournal.inventory_balances(today).sum(:balance) @total_sale = Sale.total_sale(today) diff --git a/app/jobs/checkin_job.rb b/app/jobs/checkin_job.rb new file mode 100644 index 00000000..3a0d329f --- /dev/null +++ b/app/jobs/checkin_job.rb @@ -0,0 +1,10 @@ +class CheckinJob < ApplicationJob + queue_as :default + + def perform() + table = DiningFacility.get_checkin_booking + ActionCable.server.broadcast "checkin_channel",table: table + end + + +end diff --git a/app/models/dining_facility.rb b/app/models/dining_facility.rb index 9948ec48..d3e05582 100755 --- a/app/models/dining_facility.rb +++ b/app/models/dining_facility.rb @@ -96,4 +96,26 @@ class DiningFacility < ApplicationRecord end end end + + def self.get_checkin_booking + bookings = Booking.where("booking_status ='assign' and checkin_at between '#{DateTime.now.utc - 5.hours}' and '#{DateTime.now.utc}' and reserved_by is not null and checkout_by is null") + arr_booking = Array.new + if bookings + bookings.each do |booking| + now = Time.now.utc.getlocal + hr = (now.strftime("%H").to_i).to_int + min = (now.strftime("%M").to_i).to_int + if !booking.checkout_at.nil? + checkout_at = booking.checkout_at.utc.getlocal + checkout_at_hr = (checkout_at.strftime("%H").to_i).to_int + checkout_at_min = (checkout_at.strftime("%M").to_i).to_int + checkout_at_min -= min + if (checkout_at_hr <= hr) && (checkout_at_min <= 15) + arr_booking.push({'table_id' => booking.dining_facility_id}) + end + end + end + end + return arr_booking + end end diff --git a/app/models/sale.rb b/app/models/sale.rb index fbd63076..ecebfe67 100755 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -979,11 +979,11 @@ end def self.hourly_sales(today) query= Sale.select("grand_total") .where('payment_status="paid" and sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today) - .group("date_format(receipt_date, '%I %p')") + .group("date_format(convert_tz(receipt_date,'+00:00','+06:30'), '%I:%p')") end def self.employee_sales(today) - query = Sale.select("e.name as employee_name,grand_total") + query = Sale.select("e.name as employee_name,(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE sp.payment_amount END) as total_amount") .where('sales.payment_status="paid" and sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) .joins("join employees e on e.id=sales.cashier_id") .joins("join sale_payments sp on sp.sale_id=sales.sale_id") @@ -1102,7 +1102,7 @@ end .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") .where("sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) .group("a.product_code") - .order("SUM(a.price) DESC") + .order("SUM(a.qty) DESC") .first() end diff --git a/spec/jobs/checkin_job_spec.rb b/spec/jobs/checkin_job_spec.rb new file mode 100644 index 00000000..80f4ac0e --- /dev/null +++ b/spec/jobs/checkin_job_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe CheckinJob, type: :job do + pending "add some examples to (or delete) #{__FILE__}" +end