From f6ba7403c57154803d314c6216614a8937966d66 Mon Sep 17 00:00:00 2001 From: phyusin Date: Mon, 9 Apr 2018 13:55:54 +0630 Subject: [PATCH 1/4] add customer api for order reservation --- .../api/order_reservation_controller.rb | 34 +++++++++++++++++++ config/routes.rb | 5 ++- 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 app/controllers/api/order_reservation_controller.rb diff --git a/app/controllers/api/order_reservation_controller.rb b/app/controllers/api/order_reservation_controller.rb new file mode 100644 index 00000000..97c07669 --- /dev/null +++ b/app/controllers/api/order_reservation_controller.rb @@ -0,0 +1,34 @@ +class Api::OrderReservationController < Api::ApiController + skip_before_action :authenticate + + def check_customer + customer_name = params[:name] + customer_email = params[:email] + customer_contact_no = params[:contact_no] + customer_gender = params[:gender] + customer_addr = params[:address] + customer_membership_id = params[:membership_id] + customer_membership_type = params[:membership_type] + customer_date_of_birth = params[:date_of_birth] + customer_id = 0 + + check_customer = Customer.find_by_email_and_contact_no_and_membership_id_and_membership_type(customer_email,customer_contact_no,customer_membership_id,customer_membership_type) + if !check_customer.nil? + customer_id = check_customer.customer_id + else + customer = Customer.new + customer.name = customer_name + customer.email = customer_email + customer.contact_no = customer_contact_no + customer.gender = customer_gender + customer.address = customer_addr + customer.date_of_birth = Time.parse(customer_date_of_birth).strftime("%Y-%m-%d") + customer.membership_id = customer_membership_id + customer.membership_type = customer_membership_type + customer.save! + customer_id = customer.id + end + + render :json => { :status => true, :data => { customer_id : customer_id} } + end +end diff --git a/config/routes.rb b/config/routes.rb index 365d5a79..7c224dfa 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -82,7 +82,10 @@ scope "(:locale)", locale: /en|mm/ do post "survey/:id" => "survey#create" end - + #order and reservation api + namespace :order_reservation do + post "customer" => "order_reservation#check_customer" + end end #--------- Cashier ------------# From 4bc56a31b80c47a2835c40feb8044e70418395f6 Mon Sep 17 00:00:00 2001 From: phyusin Date: Mon, 9 Apr 2018 16:23:16 +0630 Subject: [PATCH 2/4] check time --- .../api/check_in_process_controller.rb | 2 +- .../order_reservation_controller.rb | 15 ++++++++------- app/models/customer.rb | 4 ++-- .../api/order_reservation/index.json.jbuilder | 0 4 files changed, 11 insertions(+), 10 deletions(-) rename app/controllers/api/{ => order_reservation}/order_reservation_controller.rb (70%) create mode 100644 app/views/api/order_reservation/index.json.jbuilder diff --git a/app/controllers/api/check_in_process_controller.rb b/app/controllers/api/check_in_process_controller.rb index 3d375eee..4bd9434d 100644 --- a/app/controllers/api/check_in_process_controller.rb +++ b/app/controllers/api/check_in_process_controller.rb @@ -49,7 +49,7 @@ class Api::CheckInProcessController < Api::ApiController arr_time = checkout_time[0].split("-") start_time = Time.parse(arr_time[0].strip).utc.getlocal.strftime("%H:%M%p") end_time = Time.parse(arr_time[1].strip).utc.getlocal.strftime("%H:%M%p") - if start_time <= checkout_at.strftime("%H:%M%p") && checkout_at.strftime("%H:%M%p") <= end_time + if start_time <= today.strftime("%H:%M%p") && today.strftime("%H:%M%p") <= end_time checkout_at = checkout_at + (checkout_time[1]).to_i.minutes end end diff --git a/app/controllers/api/order_reservation_controller.rb b/app/controllers/api/order_reservation/order_reservation_controller.rb similarity index 70% rename from app/controllers/api/order_reservation_controller.rb rename to app/controllers/api/order_reservation/order_reservation_controller.rb index 97c07669..aa01ec39 100644 --- a/app/controllers/api/order_reservation_controller.rb +++ b/app/controllers/api/order_reservation/order_reservation_controller.rb @@ -1,5 +1,5 @@ -class Api::OrderReservationController < Api::ApiController - skip_before_action :authenticate +class Api::OrderReservation::OrderReservationController < Api::ApiController + # skip_before_action :authenticate def check_customer customer_name = params[:name] @@ -8,11 +8,10 @@ class Api::OrderReservationController < Api::ApiController customer_gender = params[:gender] customer_addr = params[:address] customer_membership_id = params[:membership_id] - customer_membership_type = params[:membership_type] customer_date_of_birth = params[:date_of_birth] customer_id = 0 - check_customer = Customer.find_by_email_and_contact_no_and_membership_id_and_membership_type(customer_email,customer_contact_no,customer_membership_id,customer_membership_type) + check_customer = Customer.find_by_email_and_contact_no_and_membership_id(customer_email,customer_contact_no,customer_membership_id) if !check_customer.nil? customer_id = check_customer.customer_id else @@ -24,11 +23,13 @@ class Api::OrderReservationController < Api::ApiController customer.address = customer_addr customer.date_of_birth = Time.parse(customer_date_of_birth).strftime("%Y-%m-%d") customer.membership_id = customer_membership_id - customer.membership_type = customer_membership_type - customer.save! + customer.customer_type = "Doemal" + customer.tax_profiles = ["2"] + customer.save + customer_id = customer.id end - render :json => { :status => true, :data => { customer_id : customer_id} } + render :json => { :status => true, :data => { :customer_id => customer_id} } end end diff --git a/app/models/customer.rb b/app/models/customer.rb index 934d371f..b05caaf6 100755 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -9,10 +9,10 @@ class Customer < ApplicationRecord has_many :orders has_many :sales - validates_presence_of :name, :contact_no, :email,:card_no + validates_presence_of :name, :contact_no, :email #,:card_no validates :contact_no, uniqueness: true, numericality: true validates :email, uniqueness: true,format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, on: :create } - validates :card_no, uniqueness: true + # validates :card_no, uniqueness: true # validates :paypar_account_no, uniqueness: true paginates_per 50 diff --git a/app/views/api/order_reservation/index.json.jbuilder b/app/views/api/order_reservation/index.json.jbuilder new file mode 100644 index 00000000..e69de29b From 57be26be77aa555550a0c90b4b66cc8d0694dfe5 Mon Sep 17 00:00:00 2001 From: phyusin Date: Mon, 9 Apr 2018 16:59:59 +0630 Subject: [PATCH 3/4] check customer --- .../order_reservation_controller.rb | 22 ++----------------- app/models/customer.rb | 17 ++++++++++++++ 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/app/controllers/api/order_reservation/order_reservation_controller.rb b/app/controllers/api/order_reservation/order_reservation_controller.rb index aa01ec39..43b9c597 100644 --- a/app/controllers/api/order_reservation/order_reservation_controller.rb +++ b/app/controllers/api/order_reservation/order_reservation_controller.rb @@ -2,31 +2,13 @@ class Api::OrderReservation::OrderReservationController < Api::ApiController # skip_before_action :authenticate def check_customer - customer_name = params[:name] - customer_email = params[:email] - customer_contact_no = params[:contact_no] - customer_gender = params[:gender] - customer_addr = params[:address] - customer_membership_id = params[:membership_id] - customer_date_of_birth = params[:date_of_birth] customer_id = 0 - check_customer = Customer.find_by_email_and_contact_no_and_membership_id(customer_email,customer_contact_no,customer_membership_id) + check_customer = Customer.find_by_email_and_contact_no_and_membership_id(params[:email],params[:contact_no],params[:membership_id]) if !check_customer.nil? customer_id = check_customer.customer_id else - customer = Customer.new - customer.name = customer_name - customer.email = customer_email - customer.contact_no = customer_contact_no - customer.gender = customer_gender - customer.address = customer_addr - customer.date_of_birth = Time.parse(customer_date_of_birth).strftime("%Y-%m-%d") - customer.membership_id = customer_membership_id - customer.customer_type = "Doemal" - customer.tax_profiles = ["2"] - customer.save - + customer = Customer.addCustomer(params) customer_id = customer.id end diff --git a/app/models/customer.rb b/app/models/customer.rb index b05caaf6..2ece4330 100755 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -352,6 +352,23 @@ class Customer < ApplicationRecord end + #new customer for doemal + def self.addCustomer(params) + customer = Customer.new + customer.name = params[:name] + customer.email = params[:email] + customer.contact_no = params[:contact_no] + customer.gender = params[:gender] + customer.address = params[:address] + customer.date_of_birth = Time.parse(params[:date_of_birth]).strftime("%Y-%m-%d") + customer.membership_id = params[:membership_id] + customer.customer_type = "Takeaway" + customer.tax_profiles = ["2"] + customer.save + + return customer + end + WALKIN = "CUS-000000000001" TAKEAWAY = "CUS-000000000002" From c7e5ae8ab0ce7b8bd7bc23442c6f7eb5651f7346 Mon Sep 17 00:00:00 2001 From: phyusin Date: Tue, 10 Apr 2018 15:21:49 +0630 Subject: [PATCH 4/4] order reservation api --- .../order_reservation_controller.rb | 17 ------ .../order_reservation_controller.rb | 52 +++++++++++++++++++ app/models/delivery.rb | 16 ++++++ app/models/order_reservation.rb | 47 +++++++++++++++++ app/models/order_reservation_item.rb | 32 ++++++++++++ .../api/order_reservation/index.json.jbuilder | 0 config/routes.rb | 3 +- 7 files changed, 149 insertions(+), 18 deletions(-) delete mode 100644 app/controllers/api/order_reservation/order_reservation_controller.rb create mode 100644 app/controllers/api/order_reserve/order_reservation_controller.rb create mode 100644 app/models/delivery.rb create mode 100644 app/models/order_reservation.rb create mode 100644 app/models/order_reservation_item.rb delete mode 100644 app/views/api/order_reservation/index.json.jbuilder diff --git a/app/controllers/api/order_reservation/order_reservation_controller.rb b/app/controllers/api/order_reservation/order_reservation_controller.rb deleted file mode 100644 index 43b9c597..00000000 --- a/app/controllers/api/order_reservation/order_reservation_controller.rb +++ /dev/null @@ -1,17 +0,0 @@ -class Api::OrderReservation::OrderReservationController < Api::ApiController - # skip_before_action :authenticate - - def check_customer - customer_id = 0 - - check_customer = Customer.find_by_email_and_contact_no_and_membership_id(params[:email],params[:contact_no],params[:membership_id]) - if !check_customer.nil? - customer_id = check_customer.customer_id - else - customer = Customer.addCustomer(params) - customer_id = customer.id - end - - render :json => { :status => true, :data => { :customer_id => customer_id} } - end -end diff --git a/app/controllers/api/order_reserve/order_reservation_controller.rb b/app/controllers/api/order_reserve/order_reservation_controller.rb new file mode 100644 index 00000000..0093409c --- /dev/null +++ b/app/controllers/api/order_reserve/order_reservation_controller.rb @@ -0,0 +1,52 @@ +class Api::OrderReserve::OrderReservationController < Api::ApiController + # skip_before_action :authenticate + ORDER = "order" + RESERVATION = "reservation" + ORDER_RESERVATION = "order and reservation" + + def check_customer + customer_id = 0 + + check_customer = Customer.find_by_email_and_contact_no_and_membership_id(params[:email],params[:contact_no],params[:membership_id]) + if !check_customer.nil? + customer_id = check_customer.customer_id + else + customer = Customer.addCustomer(params) + customer_id = customer.id + end + + render :json => { :status => true, :data => { :customer_id => customer_id} } + end + + def create + status = false + if !params[:cus_info] + render :json => { :status => false, :message => "cus_info is required!" } + elsif !params[:requested_time] + render :json => { :status => false, :message => "requested_time is required!" } + elsif !params[:order_type] + render :json => { :status => false, :message => "order_type is required!" } + elsif params[:order_type] == ORDER || params[:order_type] == ORDER_RESERVATION + if !params[:order_info] + render :json => { :status => false, :message => "order_info is required!" } + else + status = true + end + elsif !params[:payment_info] + render :json => { :status => false, :message => "payment_info is required!" } + elsif !params[:delivery_info] + render :json => { :status => false, :message => "delivery_info is required!" } + elsif params[:order_type] == RESERVATION || params[:order_type] == ORDER_RESERVATION + if !params[:reservation_info] + render :json => { :status => false, :message => "reservation_info is required!" } + else + status = true + end + end + + if status == true + OrderReservation.addOrderReservationInfo(params) + render :json => { :status => true, :message => "Order reservation is successfully created!" } + end + end +end diff --git a/app/models/delivery.rb b/app/models/delivery.rb new file mode 100644 index 00000000..d2273d8d --- /dev/null +++ b/app/models/delivery.rb @@ -0,0 +1,16 @@ +class Delivery < ApplicationRecord + belongs_to :order_reservation + + def self.addDeliveryInfo(params,order_reservation_id) + delivery = Delivery.new + delivery.order_reservation_id = order_reservation_id + delivery.provider = params[:provider] + delivery.delivery_type = params[:delivery_type] + delivery.township = params[:township] + delivery.address = params[:address] + delivery.direction_address = params[:address_direction] + delivery.delivery_fee = params[:delivery_fee] + delivery.remark = params[:delivery_note] + delivery.save! + end +end diff --git a/app/models/order_reservation.rb b/app/models/order_reservation.rb new file mode 100644 index 00000000..0be5e4c8 --- /dev/null +++ b/app/models/order_reservation.rb @@ -0,0 +1,47 @@ +class OrderReservation < ApplicationRecord + self.primary_key = "order_reservation_id" + + #primary key - need to be unique generated for multiple shops + before_create :generate_custom_id + + has_many :order_reservation_items + belongs_to :delivery + + def self.addOrderReservationInfo(params) + order_reservation = OrderReservation.new + order_reservation.order_reservation_type = params[:order_type] + order_reservation.customer_id = params[:cus_info] + order_reservation.requested_time = Time.parse(params[:requested_time]).utc.strftime("%Y-%m-%d %H:%M:%S") + if params[:order_info] + order_reservation.item_count = params[:order_info][:items].count + order_reservation.payment_type = params[:payment_info][:payment_type] + order_reservation.payment_status = params[:payment_info][:payment_status] + order_reservation.payment_ref = params[:payment_info][:payment_ref] + order_reservation.total_amount = params[:payment_info][:sub_total] + order_reservation.total_tax = params[:payment_info][:total_tax] + order_reservation.discount_amount = params[:payment_info][:discount_amount] + order_reservation.grand_total = params[:payment_info][:grand_total] + order_reservation.order_remark = params[:payment_info][:order_remark] + end + if params[:reservation_info] + order_reservation.total_customer = params[:reservation_info][:total_user] + order_reservation.reservation_remark = params[:reservation_info][:reservation_note] + end + order_reservation.save! + if params[:order_info][:items] + params[:order_info][:items].each do |oritem| + OrderReservationItem.process_order_reservation_item(oritem[:product_code],oritem[:item_instance_code],oritem[:product_name],oritem[:product_alt_name], + oritem[:account_id],oritem[:qty],oritem[:price],oritem[:unit_price], + oritem[:options],nil,order_reservation.id) + end + end + if params[:delivery_info] + Delivery.addDeliveryInfo(params[:delivery_info],order_reservation.id) + end + end + + private + def generate_custom_id + self.order_reservation_id = SeedGenerator.generate_id(self.class.name, "ODRS") + end +end diff --git a/app/models/order_reservation_item.rb b/app/models/order_reservation_item.rb new file mode 100644 index 00000000..0d932f92 --- /dev/null +++ b/app/models/order_reservation_item.rb @@ -0,0 +1,32 @@ +class OrderReservationItem < ApplicationRecord + self.primary_key = "order_reservation_items_id" + + #primary key - need to be unique + before_create :generate_custom_id + + belongs_to :order_reservation, autosave: true + + def self.process_order_reservation_item (item_code, instance_code, menu_name, alt_name, account_id, qty, price, unit_price, options, set_menu_items, order_reservation_id) + orderreservationitem = OrderReservationItem.create do |oitem| + oitem.order_reservation_id = order_reservation_id + oitem.item_code = item_code + oitem.item_instance_code = instance_code + oitem.item_name = menu_name + oitem.alt_name = alt_name ? alt_name : "" + oitem.account_id = account_id + oitem.qty = qty + oitem.unit_price = unit_price + oitem.price = price + oitem.options = options + oitem.set_menu_items = set_menu_items + end + + #logger.debug orderitem.to_yml + orderreservationitem.save! + end + + private + def generate_custom_id + self.order_reservation_items_id = SeedGenerator.generate_id(self.class.name, "ODRSI") + end +end diff --git a/app/views/api/order_reservation/index.json.jbuilder b/app/views/api/order_reservation/index.json.jbuilder deleted file mode 100644 index e69de29b..00000000 diff --git a/config/routes.rb b/config/routes.rb index 7c224dfa..cec8057f 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -83,8 +83,9 @@ scope "(:locale)", locale: /en|mm/ do end #order and reservation api - namespace :order_reservation do + namespace :order_reserve do post "customer" => "order_reservation#check_customer" + post "order" => "order_reservation#create" end end