From bcdce092ccfa558e0cd8e59bc6633dda9267f55e Mon Sep 17 00:00:00 2001 From: Min Zeya Phyo Date: Tue, 4 Apr 2017 12:18:17 +0630 Subject: [PATCH] order api - WIP & db structure change --- .../javascripts/api/restaurant/zone.coffee | 3 - .../stylesheets/api/restaurant/zone.scss | 3 - app/controllers/api/customer_controller.rb | 10 --- .../api/restaurant/order_controller.rb | 36 --------- app/models/customer.rb | 4 +- app/models/dining_in.rb | 4 - app/models/order.rb | 74 +++++++++++++++++++ app/models/order_item.rb | 23 +++++- app/models/room_booking.rb | 3 +- app/models/room_order.rb | 4 - app/views/layouts/_header.html.erb | 6 +- config/routes.rb | 7 ++ .../20170403140820_create_order_items.rb | 3 +- .../20170403143526_create_dining_ins.rb | 11 --- .../20170403144732_create_room_bookings.rb | 15 ---- .../20170403145042_create_room_orders.rb | 11 --- db/schema.txt | 8 +- db/seeds.rb | 10 +-- spec/models/dining_in_spec.rb | 5 -- spec/models/room_booking_spec.rb | 5 -- spec/models/room_order_spec.rb | 5 -- 21 files changed, 120 insertions(+), 130 deletions(-) delete mode 100644 app/assets/javascripts/api/restaurant/zone.coffee delete mode 100644 app/assets/stylesheets/api/restaurant/zone.scss delete mode 100644 app/controllers/api/customer_controller.rb delete mode 100644 app/controllers/api/restaurant/order_controller.rb delete mode 100644 app/models/dining_in.rb delete mode 100644 app/models/room_order.rb delete mode 100644 db/migrate/20170403143526_create_dining_ins.rb delete mode 100644 db/migrate/20170403144732_create_room_bookings.rb delete mode 100644 db/migrate/20170403145042_create_room_orders.rb delete mode 100644 spec/models/dining_in_spec.rb delete mode 100644 spec/models/room_booking_spec.rb delete mode 100644 spec/models/room_order_spec.rb diff --git a/app/assets/javascripts/api/restaurant/zone.coffee b/app/assets/javascripts/api/restaurant/zone.coffee deleted file mode 100644 index 24f83d18..00000000 --- a/app/assets/javascripts/api/restaurant/zone.coffee +++ /dev/null @@ -1,3 +0,0 @@ -# Place all the behaviors and hooks related to the matching controller here. -# All this logic will automatically be available in application.js. -# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/api/restaurant/zone.scss b/app/assets/stylesheets/api/restaurant/zone.scss deleted file mode 100644 index a9c71ee3..00000000 --- a/app/assets/stylesheets/api/restaurant/zone.scss +++ /dev/null @@ -1,3 +0,0 @@ -// Place all the styles related to the api/restaurant/zone controller here. -// They will automatically be included in application.css. -// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/api/customer_controller.rb b/app/controllers/api/customer_controller.rb deleted file mode 100644 index 4accb5fd..00000000 --- a/app/controllers/api/customer_controller.rb +++ /dev/null @@ -1,10 +0,0 @@ -class Api::CustomersController < ActionController::API - - #List all active customers by name - def index - end - - #Show customer by ID - def show - end -end diff --git a/app/controllers/api/restaurant/order_controller.rb b/app/controllers/api/restaurant/order_controller.rb deleted file mode 100644 index 0fbc9f3f..00000000 --- a/app/controllers/api/restaurant/order_controller.rb +++ /dev/null @@ -1,36 +0,0 @@ -class Api::Restaurant::OrderController < ActionController::API - before :authenticate_token - - #Description - # This API show current order details - # Input Params - order_id - def show - order = Order.find(params[:order_id]) - order.order_items - end - - - # Description - # This API allow new order creation - # Input Params - # order_source [* default - emenu] | table_id (*require for Dine-In) | order_type [* Default - Dine-in] - # | guest_info (optional) | customer_id (optional) - # order_items {[item_code, item_instance_code , qty, option, variants]} - # Output Params - # Status [Success | Error | System Error] , order_id, error_message (*) - def create - - - end - - # Description - # This API - allow order to add new items to existing orders, does not allow you to remove confirm items - # Update customer info, Guest Info - # Input Params - # order_id , order_items {[item_code, item_instance_code , qty, option, variants]} - def update - - end - - -end diff --git a/app/models/customer.rb b/app/models/customer.rb index e785ef23..42247791 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -5,5 +5,7 @@ class Customer < ApplicationRecord validates_presence_of :name, :contact_no validates :contact_no, uniqueness: true - + def lastest_invoices + sales.where(:customer_id => self.id).order("created_at desc").limit(5) + end end diff --git a/app/models/dining_in.rb b/app/models/dining_in.rb deleted file mode 100644 index a7307eca..00000000 --- a/app/models/dining_in.rb +++ /dev/null @@ -1,4 +0,0 @@ -class DiningIn < ApplicationRecord - belongs_to :table - belongs_to :order -end diff --git a/app/models/order.rb b/app/models/order.rb index fb7ea2f6..6835e0b9 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -1,12 +1,86 @@ class Order < ApplicationRecord before_create :set_order_date + before_save :update_products_and_quantity_count + belongs_to :customer has_many :order_items, inverse_of: :order has_many :dining_ins + #internal references attributes for business logic control + attr_accessor :items, :guest, :table_id, :new_booking, :booking_type + + + #Main Controller method to create new order - validate all inputs and generate new order + # order_item : { + # order_item_code : "", + # item_instance_code : "", + # quantity : 0, + # option_values : [], + # sub_order_items : [], + # } + def generate + if self.new_booking + booking = Booking.create({:dining_facility_id => :table_id, }) + + self.adding_line_items + self.save! + else + + + booking = Booking.find(self.booking_id) + #Add Order Table and Room relation afrer order creation + if booking.type = "Table" + #add to table_booking_order + tbo = TableBookingOrder.create({:table_booking => booking, :order => self}) + elsif booking.type = "Room" + #add to room_booking_order + rbo = RoomBookingOrder.create({:table_booking => booking, :order => self}) + end + + end + + end + + #Main Method - to update order / add items + def modify + + end private + def validate_api_inputs + + end + + def adding_line_items + + if self.items + #loop to add all items to order + self.items.each do |item| + self.order_items.add( OrderItem.processs_item(item) ) + end + else + self.errors.add(:order_items, :blank, message: "Order items cannot be blank") + end + + end + def set_order_date self.date = Time.now.utc end + + #Update Items Count and Quantity changes whenever there is changes + def update_products_and_quantity_count + item_count = 0 + quantity_count = 0 + # Count number of different items + self.item_count = item_count + self.quantity_count = quantity_count + # Counter number of quantity + end + + #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) + end end diff --git a/app/models/order_item.rb b/app/models/order_item.rb index 4aae01a4..a728ca48 100644 --- a/app/models/order_item.rb +++ b/app/models/order_item.rb @@ -4,7 +4,28 @@ class OrderItem < ApplicationRecord #Validation validates_presence_of :item_code, :item_name, :qty - validates :qty, numericality: { :greater_than 0 } + validates :qty, numericality: { :greater_than => 0 } validates_associated :orders + #This Method - handle how items is added into order + # order_item : { + # order_item_code : "", + # item_instance_code : "", + # quantity : 0, + # option_values : [], + # sub_order_items : [], + # } + def processs_item (item, item_order_by) + OrderItem.new do |oitem| + oitem.item_code = item.item_code + oitem.item_name = item.name + oitem.qty = item.qty + oitem.option = item.option + oitem.variants = item.variants + oitem.set_order_items = item.sub_order_items + oitem.item_order_by = item_order_by + + end + + end end diff --git a/app/models/room_booking.rb b/app/models/room_booking.rb index aa2c31c9..fb7c0dac 100644 --- a/app/models/room_booking.rb +++ b/app/models/room_booking.rb @@ -1,2 +1,3 @@ -class RoomBooking < ApplicationRecord +class RoomBooking < Booking + end diff --git a/app/models/room_order.rb b/app/models/room_order.rb deleted file mode 100644 index 2e8dbf77..00000000 --- a/app/models/room_order.rb +++ /dev/null @@ -1,4 +0,0 @@ -class RoomOrder < ApplicationRecord - belongs_to :room - belongs_to :orders -end diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index 4878d5e6..912c5992 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -2,7 +2,7 @@ - SXRestaurant + H diff --git a/config/routes.rb b/config/routes.rb index 6d65834f..34b614f6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,8 +13,15 @@ Rails.application.routes.draw do namespace :restaurant do get 'zones' => "zones#index" get 'tables' => "#index" + + #Order Controller + resources :orders, only: [:create, :show, :update] + + end + resources :customers, only: [:index, :show, :create] + end diff --git a/db/migrate/20170403140820_create_order_items.rb b/db/migrate/20170403140820_create_order_items.rb index 43dc7bc5..939b8189 100644 --- a/db/migrate/20170403140820_create_order_items.rb +++ b/db/migrate/20170403140820_create_order_items.rb @@ -3,13 +3,12 @@ class CreateOrderItems < ActiveRecord::Migration[5.0] create_table :order_items do |t| t.references :order, foreign_key: true, :null => false t.string :order_item_status, :null => false, :default => "new" + t.string :item_order_by #person who order this t.string :item_code, :null => false t.string :item_name, :null => false t.decimal :qty, :precision => 10, :scale => 2, :null => false, :default => 0.00 - t.string :item_order_by #person who order this t.string :remark t.string :options - t.string :variants t.json :set_menu_items #this parameter is require to route the items correctly t.timestamps diff --git a/db/migrate/20170403143526_create_dining_ins.rb b/db/migrate/20170403143526_create_dining_ins.rb deleted file mode 100644 index 546e74b0..00000000 --- a/db/migrate/20170403143526_create_dining_ins.rb +++ /dev/null @@ -1,11 +0,0 @@ -class CreateDiningIns < ActiveRecord::Migration[5.0] - def change - create_table :dining_ins do |t| - t.references :table - t.references :order - t.boolean :currently_occupied, :default => false #there will only be 1 true - where table_id+order_id - - t.timestamps - end - end -end diff --git a/db/migrate/20170403144732_create_room_bookings.rb b/db/migrate/20170403144732_create_room_bookings.rb deleted file mode 100644 index bc339585..00000000 --- a/db/migrate/20170403144732_create_room_bookings.rb +++ /dev/null @@ -1,15 +0,0 @@ -class CreateRoomBookings < ActiveRecord::Migration[5.0] - def change - create_table :room_bookings do |t| - t.references :room, :null => false - t.datetime :checkin_at, :null => false - t.datetime :checkout_at - t.string :checkout_by - t.datetime :reserved_at - t.string :reserved_by - t.string :status, :null => false, :default => "new" - t.boolean :is_currently_checkin, :default => false, :null => true - t.timestamps - end - end -end diff --git a/db/migrate/20170403145042_create_room_orders.rb b/db/migrate/20170403145042_create_room_orders.rb deleted file mode 100644 index e8cfb4be..00000000 --- a/db/migrate/20170403145042_create_room_orders.rb +++ /dev/null @@ -1,11 +0,0 @@ -class CreateRoomOrders < ActiveRecord::Migration[5.0] - def change - create_table :room_orders do |t| - t.references :room - t.references :orders, foreign_key: true - t.string :order_by - - t.timestamps - end - end -end diff --git a/db/schema.txt b/db/schema.txt index 7925bb57..baa3cf48 100644 --- a/db/schema.txt +++ b/db/schema.txt @@ -35,10 +35,10 @@ menu_item_instance : { } order_item : { - order_item_code : "", item_instance_code : "", + item_instance_price: "", quantity : 0, - option_values : [], + option_values : [{key,value},], sub_order_items : [], } @@ -51,8 +51,8 @@ customer {name, company, contact, email, member_id, member_type, member_authenti order { id, date, order_ref, order_source [tablet, order_station, emenu, api], order_type [dine-in, takeaway, delivery],customer_id, item_count, quantity_count, status [new, processing, fulfilled], waiters[], guest_info: {adult_count, child_count, woman_count, man_count}} order_items { order_item_status, product_code, name, qty, price, remark, options , variants: [], set_menu_items :[]} order_delivery_info {name, address, contact_no, delivery-by [InHouse | YDoor2Door | Food2U], tracker-id, sale} -dine-in-table {table, order, status} -room-booking {room, check-in, check-out, reserved_by, status, orders} +booking {dining_facility_id, type, checkin_at, checkout_at, checkout_by, reserved_at, reserved_by, status, is_currently_checkin} +booking_order(booking, order) order_queue_station {name, is_active, processing_items [product_code], print_copy: boolean, printer_name, font_size, cut_per_item, use_alternate_name} order_queue_log {order_id, job_status [new, completed], print_status [], header: {table name, order_type, order_date}, items :{name, comment, qty}} diff --git a/db/seeds.rb b/db/seeds.rb index 0a8d85c3..ef60ce83 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -47,11 +47,6 @@ menu_item_type = Lookup.create([{lookup_type:'menu_item_type', name: 'SIMPLE', v {lookup_type:'menu_item_type', name: 'Set Menu', value: 'set_menu'}, {lookup_type:'menu_item_type', name: 'DIY', value: 'diy'}]) -#dining_facilities:[available| reserved | occupied| cleaning] -dining_facilities_status = Lookup.create([{lookup_type:'dining_facilities_status', name: 'Available', value: 'available'}, - {lookup_type:'dining_facilities_status', name: 'Reserved', value: 'reserved'}, - {lookup_type:'dining_facilities_status', name: 'Occupied', value: 'occupied'}, - {lookup_type:'dining_facilities_status', name: 'Cleaning', value: 'cleaning'}]) #Employee Roles employee_roles = Lookup.create([{lookup_type:'employee_roles', name: 'Cashier', value: 'cashier'}, @@ -60,6 +55,11 @@ employee_roles = Lookup.create([{lookup_type:'employee_roles', name: 'Cashier', {lookup_type:'employee_roles', name: 'Manager', value: 'manager'}, {lookup_type:'employee_roles', name: 'Administrator', value: 'Administrator'}]) +#booking_status +booking_status = Lookup.create([{lookup_type:'booking_status', name: 'Available', value: 'available'}, + {lookup_type:'booking_status', name: 'Reserved', value: 'reserved'}, + {lookup_type:'booking_status', name: 'Occupied', value: 'occupied'}, + {lookup_type:'booking_status', name: 'Cleaning', value: 'cleaning'}]) #WALK CUSTOMER - Default CUSTOMER (take key 1) customer = Customer.create({id:1, name:"WALK-IN", contact_no:"000000000"}) diff --git a/spec/models/dining_in_spec.rb b/spec/models/dining_in_spec.rb deleted file mode 100644 index e6429905..00000000 --- a/spec/models/dining_in_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'rails_helper' - -RSpec.describe DiningIn, type: :model do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/models/room_booking_spec.rb b/spec/models/room_booking_spec.rb deleted file mode 100644 index 7b2b3b79..00000000 --- a/spec/models/room_booking_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'rails_helper' - -RSpec.describe RoomBooking, type: :model do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/models/room_order_spec.rb b/spec/models/room_order_spec.rb deleted file mode 100644 index f148edd8..00000000 --- a/spec/models/room_order_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'rails_helper' - -RSpec.describe RoomOrder, type: :model do - pending "add some examples to (or delete) #{__FILE__}" -end