order api - WIP & db structure change

This commit is contained in:
Min Zeya Phyo
2017-04-04 12:18:17 +06:30
parent aba3e52d0b
commit bcdce092cc
21 changed files with 120 additions and 130 deletions

View File

@@ -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/

View File

@@ -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/

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -1,4 +0,0 @@
class DiningIn < ApplicationRecord
belongs_to :table
belongs_to :order
end

View File

@@ -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

View File

@@ -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

View File

@@ -1,2 +1,3 @@
class RoomBooking < ApplicationRecord
class RoomBooking < Booking
end

View File

@@ -1,4 +0,0 @@
class RoomOrder < ApplicationRecord
belongs_to :room
belongs_to :orders
end

View File

@@ -2,7 +2,7 @@
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">SXRestaurant</a>
<a class="navbar-brand" href="#">H</a>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
@@ -11,9 +11,7 @@
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
</div>