diff --git a/Gemfile b/Gemfile index 3b8f7ca8..bccc69a8 100644 --- a/Gemfile +++ b/Gemfile @@ -43,6 +43,8 @@ gem 'jbuilder', '~> 2.5' # Use ActiveModel has_secure_password gem 'bcrypt', '~> 3.1.7' +gem 'sidekiq' + # Use Capistrano for deployment # gem 'capistrano-rails', group: :development diff --git a/Gemfile.lock b/Gemfile.lock index 00b87ae7..c89fcd45 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -55,6 +55,7 @@ GEM execjs coffee-script-source (1.12.2) concurrent-ruby (1.0.5) + connection_pool (2.2.1) cups (0.0.7) database_cleaner (1.5.3) debug_inspector (0.0.2) @@ -108,6 +109,8 @@ GEM puma (3.8.2) rack (2.0.1) rack-cors (0.4.1) + rack-protection (1.5.3) + rack rack-test (0.6.3) rack (>= 1.0) rails (5.0.2) @@ -137,6 +140,7 @@ GEM rb-fsevent (0.9.8) rb-inotify (0.9.8) ffi (>= 0.5.0) + redis (3.3.3) rspec-core (3.5.4) rspec-support (~> 3.5.0) rspec-expectations (3.5.0) @@ -166,6 +170,11 @@ GEM activesupport (>= 3.2.1) shoulda-matchers (3.1.1) activesupport (>= 4.0.0) + sidekiq (4.2.10) + concurrent-ruby (~> 1.0) + connection_pool (~> 2.2, >= 2.2.0) + rack-protection (>= 1.5.0) + redis (~> 3.2, >= 3.2.1) simple_form (3.4.0) actionpack (> 4, < 5.1) activemodel (> 4, < 5.1) @@ -231,6 +240,7 @@ DEPENDENCIES sass-rails (~> 5.0) schema_to_scaffold shoulda-matchers (~> 3.1) + sidekiq simple_form spreadsheet spring diff --git a/app/controllers/api/orders_controller.rb b/app/controllers/api/orders_controller.rb index 46dd29b0..064348fa 100644 --- a/app/controllers/api/orders_controller.rb +++ b/app/controllers/api/orders_controller.rb @@ -1,4 +1,4 @@ -class Api::Restaurant::OrdersController < ActionController::API +class Api::OrdersController < ActionController::API #before :authenticate_token #Description @@ -19,13 +19,15 @@ class Api::Restaurant::OrdersController < ActionController::API # Output Params # Status [Success | Error | System Error] , order_id, error_message (*) def create + Rails.logger.debug "Order Source - " + params[:order_source] + Rails.logger.debug "Table ID - " + params[:table_id] + # begin @order = Order.new @order.source = params[:order_source] @order.order_type = params[:order_type] @order.customer_id = params[:customer_id] - json_hash = params[:order_items] - @order.items = json_hash + @order.items = params[:order_items] @order.guest = params[:guest_info] @order.table_id = params[:table_id] @order.new_booking = true @@ -38,7 +40,8 @@ class Api::Restaurant::OrdersController < ActionController::API @order.booking_id = params[:booking_id] end - @status = @order.generate + @status = @order.generate + # rescue Exception => error # @status = false # @error_messages = "Exception has occurs on System" @@ -59,5 +62,9 @@ class Api::Restaurant::OrdersController < ActionController::API end - + def order_params + params.permits(:order_source, :booking_id,:order_type, + :customer_id,:guest_info, :table_id, :room_id, + order_items: [:item_code, :qty, :option, :remark]) + end end diff --git a/app/jobs/order_queue_processor_job.rb b/app/jobs/order_queue_processor_job.rb index 7727260d..33380674 100644 --- a/app/jobs/order_queue_processor_job.rb +++ b/app/jobs/order_queue_processor_job.rb @@ -1,16 +1,16 @@ class OrderQueueProcessorJob < ApplicationJob queue_as :default - def perform(:order_id) + def perform(order_id) # Do something later #Order ID order = Order.find(order_id) - #Loop through the order stations and process the items #Execute orders and send to order stations if order oqs = OrderQueueStation.new oqs.process_order(order) end + end end diff --git a/app/models/assigned_order_item.rb b/app/models/assigned_order_item.rb index b4c4b242..c2d2e8b0 100644 --- a/app/models/assigned_order_item.rb +++ b/app/models/assigned_order_item.rb @@ -1,4 +1,5 @@ class AssignedOrderItem < ApplicationRecord + belongs_to :order belongs_to :order_queue_station def self.assigned_order_item (order, item_code, order_queue_station ) diff --git a/app/models/menu_item.rb b/app/models/menu_item.rb index 7b27388f..f2aab33c 100644 --- a/app/models/menu_item.rb +++ b/app/models/menu_item.rb @@ -6,4 +6,24 @@ class MenuItem < ApplicationRecord default_scope { order('item_code asc') } + def self.search_by_item_code(item_code) + menu_item_hash = Hash.new + mt_instance = MenuItemInstance.find_by_item_instance_code(item_code) + if (!mt_instance.nil?) + menu_item = MenuItem.find(mt_instance.menu_item_id) + menu_item_hash[:type] = menu_item.type + menu_item_hash[:item_code] = menu_item.item_code + menu_item_hash[:item_instance_code] = mt_instance.item_instance_code + menu_item_hash[:name] = menu_item.name.to_s + " - " + mt_instance.item_instance_name.to_s + menu_item_hash[:alt_name] = menu_item.alt_name.to_s + " - " + mt_instance.item_instance_name.to_s + menu_item_hash[:price] = mt_instance.price + menu_item_hash[:promotion_price] = mt_instance.promotion_price + menu_item_hash[:is_on_promotion] = mt_instance.is_on_promotion + menu_item_hash[:is_available] = mt_instance.is_available + + return menu_item_hash + end + + return nil + end end diff --git a/app/models/order.rb b/app/models/order.rb index 3a0d1cfb..101ba718 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -1,6 +1,5 @@ class Order < ApplicationRecord before_create :set_order_date - before_save :update_products_and_quantity_count belongs_to :customer has_many :order_items, autosave: true , inverse_of: :order @@ -37,13 +36,10 @@ class Order < ApplicationRecord self.adding_line_items #Add Order Table and Room relation afrer order creation - if booking.type = "TableBooking" - #add to table_booking_order - tbo = TableBookingOrder.create({:table_booking_id => booking.id, :order => self}) - elsif booking.type = "RoomBooking" - #add to room_booking_order - rbo = RoomBookingOrder.create({:room_booking_id => booking.id, :order => self}) - end + BookingOrder.create({:booking_id => booking.id, :order => self}) + + #Send order to queue one it done! + process_order_queue return true @@ -63,8 +59,25 @@ class Order < ApplicationRecord if self.items #loop to add all items to order self.items.each do |item| - self.order_items.add( OrderItem.processs_item(item, self.id, self.employee_name) ) + menu_item = MenuItem.search_by_item_code(item[:item_code]) + + set_order_items = nil + ##If menu Item set item - must add child items to order as well, where price is only take from menu_item + if (menu_item[:type] == "SetMenuItem") + set_order_items + end + + if (menu_item) + + OrderItem.processs_item(menu_item[:item_instance_code], menu_item[:name], + item[:qty], item[:options], set_order_items, self.id, + self.employee_name) + end end + + self.item_count = self.order_items.count + self.save! + return true else self.errors.add(:order_items, :blank, message: "Order items cannot be blank") @@ -73,6 +86,7 @@ class Order < ApplicationRecord end + def default_values self.customer = Customer.find(1) if self.customer_id.nil? self.source = "emenu" if self.source.nil? @@ -94,7 +108,7 @@ class Order < ApplicationRecord item_count = 0 quantity_count = 0 # Count number of different items - self.item_count = item_count + self.item_count = self.order_items.item_count self.quantity_count = quantity_count # Counter number of quantity end @@ -102,6 +116,6 @@ class Order < ApplicationRecord #Process order items and send to order queue def process_order_queue #Send to background job for processing - OrderQueueProcessorJob.perform_later(:order_id => self.id) + OrderQueueProcessorJob.perform_later(self.id) end end diff --git a/app/models/order_item.rb b/app/models/order_item.rb index 30d91466..14eb6287 100644 --- a/app/models/order_item.rb +++ b/app/models/order_item.rb @@ -5,7 +5,7 @@ class OrderItem < ApplicationRecord #Validation validates_presence_of :item_code, :item_name, :qty validates :qty, numericality: { :greater_than => 0 } - validates_associated :orders + validates_associated :order #This Method - handle how items is added into order # order_item : { @@ -15,17 +15,19 @@ class OrderItem < ApplicationRecord # option_values : [], # sub_order_items : [], # } - def self.processs_item (item, orde_id, item_order_by) + def self.processs_item (item_code, menu_name, qty, options, set_menu_items, order_id, item_order_by) + orderitem = OrderItem.create do |oitem| - oitem.item_code = item["item_code"] oitem.order_id = order_id - oitem.item_name = item["name"] - oitem.qty = item["qty"] - oitem.option = item["option"] - oitem.set_order_items = item["sub_order_items"] - oitem.item_order_by = item_order_by + oitem.item_code = item_code + oitem.item_name = menu_name + oitem.qty = qty + oitem.options = options + oitem.set_menu_items = set_menu_items + oitem.item_order_by = item_order_by #person who order this. * If emenu - it will be login user on the app end - logger.debug orderitem.to_yml + + #logger.debug orderitem.to_yml orderitem.save! diff --git a/app/views/api/orders/create.json.jbuilder b/app/views/api/orders/create.json.jbuilder index 837d6889..1c89b2f2 100644 --- a/app/views/api/orders/create.json.jbuilder +++ b/app/views/api/orders/create.json.jbuilder @@ -2,7 +2,7 @@ if @status == true json.status :success json.id @order.id json.order_items do - json.array! @order.order_items, :item_code, :item_name, :qty, :options, :variants, :remarks + json.array! @order.order_items, :item_code, :item_name, :qty, :options, :remark end else json.status :error diff --git a/config/application.rb b/config/application.rb index 37494d3d..9ca67743 100644 --- a/config/application.rb +++ b/config/application.rb @@ -12,6 +12,7 @@ module SXRestaurants # Application configuration should go into files in config/initializers # -- all .rb files in that directory are automatically loaded. config.active_record.time_zone_aware_types = [:datetime, :time] + config.active_job.queue_adapter = :sidekiq end end diff --git a/config/routes.rb b/config/routes.rb index 76b0bb16..391979b0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,8 @@ +require 'sidekiq/web' + Rails.application.routes.draw do root 'home#index' + mount Sidekiq::Web => '/kiq' #--------- SmartSales Installation ------------# get 'install' => 'install#index' @@ -19,12 +22,10 @@ Rails.application.routes.draw do namespace :restaurant do get 'zones' => "zones#index" resources :menu, only:[:index, :show] - resources :menu_categories, only: [:index] resources :menu_items, only: [:index, :show] resources :menu_item_attributes, only: [:index] resources :menu_item_options, only: [:index] - resources :menu_sold_out, only: [:index] end @@ -34,7 +35,7 @@ Rails.application.routes.draw do #Order Controller resources :orders, only: [:create, :show, :update] do - post "bill/:orer_id" => "bill#create" + post "bill" => "bill#create" end #Current active bookings resources :bookings, only: [:index, :create, :update]