From edade3662b5c4cdb6b12c2c3eb2d904278a7ed80 Mon Sep 17 00:00:00 2001 From: Aung Myo Date: Fri, 9 Feb 2018 11:32:29 +0630 Subject: [PATCH] update quickservice --- app/assets/javascripts/addorder.js | 54 +++++--- .../origami/payments_controller.rb | 14 +- .../origami/quick_service_controller.rb | 125 ++++++++++++++++++ app/models/order.rb | 21 +-- app/models/sale.rb | 44 ++++++ app/views/layouts/_left_sidebar.html.erb | 2 +- app/views/origami/addorders/detail.html.erb | 48 +++---- app/views/origami/pending_order/show.html.erb | 4 +- config/routes.rb | 5 +- 9 files changed, 243 insertions(+), 74 deletions(-) diff --git a/app/assets/javascripts/addorder.js b/app/assets/javascripts/addorder.js index e330015a..9d5edf7e 100755 --- a/app/assets/javascripts/addorder.js +++ b/app/assets/javascripts/addorder.js @@ -1,4 +1,7 @@ $(function() { + + type = $('#type').val(); + modify_order = $('#modify_order').val(); //click menu sidebar menu category $(".product").on("click", function(){ var url = $(this).attr('data-ref'); @@ -85,8 +88,13 @@ $(function() { function show_menu_item_list(url_item){ var menu_list = $('.menu_items_list'); menu_list.empty(); - if ($('#type').val() == 'true') { - url_item = 'addorders/'+url_item + + if (modify_order=="true" && type =="true") { + url_item = '../../../addorders/'+url_item; + }if(modify_order=="false" && type =="true"){ + url_item = 'addorders/'+url_item + }else{ + url_item = url_item; } //Start Ajax @@ -763,18 +771,27 @@ $(function() { // Pay Discount for Payment $("#create_pay_order").on('click', function(e){ - $(this).attr('disabled', 'disabled'); e.preventDefault(); + type = "quick_service" $("#oqs_loading_wrapper").show(); - - type = $('#type').val(); - type = 'quick_service' - var table_type = $('#table_id').find("option:selected").data('type'); - var table_id = $('#table_id').val(); - var customer_id = $('#customer_id').val(); - var booking_id = $('#booking_id').text(); - var ajax_url = 'addorders/create'; + console.log(modify_order) + if (modify_order == "true") { + console.log($('#sale_id').text()) + var ajax_url = '../../../quick_service/update_modify_order'; + var table_type = $('#table_type').text(); + var table_id = $('#table_id').text(); + var customer_id = $('#customer_id').text(); + var booking_id = $('#booking_id').text(); + var sale_id = $('#sale_id').text(); + }else{ + var ajax_url = 'addorders/create'; + var table_type = $('#table_id').find("option:selected").data('type'); + var table_id = $('#table_id').val(); + var customer_id = $('#customer_id').val(); + var booking_id = $('#booking_id').text(); + var sale_id = "" + } if (!booking_id.length > 0) { @@ -788,7 +805,8 @@ $(function() { var params = {'order_source': type, 'order_type': "dine_in", 'customer_id': customer_id, 'guest_info': "", 'table_id': table_id, - 'order_items': order_items }; + 'order_items': order_items,'sale_id': sale_id }; + console.log(params) $.ajax({ type: "POST", url: ajax_url, @@ -799,7 +817,6 @@ $(function() { if (result.status) { window.location.href = "/origami/sale/"+result.data["sale_id"]+"/"+type+"/payment" } - // window.location.href = "/origami/quick_service" } }); }); @@ -1032,9 +1049,6 @@ $(function() { $(".sub_click").on("click", function(){ var menu_id = $(this).attr("data-id"); var url = "get_menu_sub_category/"+menu_id; - if ($('#type').val() == 'true') { - url = 'addorders/'+url - } sub_category = $(this).find('.sub_category_list'); show_sub_category_list(url,sub_category); }); @@ -1043,6 +1057,14 @@ $(function() { //show menu item list when click menu category function show_sub_category_list(url,sub_category){ + if (modify_order == "true" && type =="true") { + var url = "../../../addorders/"+url; + }if(modify_order=="false" && type =="true"){ + var url = 'addorders/'+url; + }else{ + var url = url; + } + var menu_list = $('.sub_category_list'); menu_list.empty(); //Start Ajax diff --git a/app/controllers/origami/payments_controller.rb b/app/controllers/origami/payments_controller.rb index d5e14bfc..4069edd0 100755 --- a/app/controllers/origami/payments_controller.rb +++ b/app/controllers/origami/payments_controller.rb @@ -69,13 +69,6 @@ class Origami::PaymentsController < BaseOrigamiController sale_id = params[:sale_id] member_info = nil - if params[:type] == "quick_service" - booking = Booking.find_by_sale_id(sale_id) - booking.booking_orders.each do |order| - Order.pay_process_order_queue(order.order_id,booking.dining_facility_id) - end - end - if(Sale.exists?(sale_id)) saleObj = Sale.find(sale_id) shop_details = Shop::ShopDetail @@ -147,6 +140,13 @@ class Origami::PaymentsController < BaseOrigamiController printer = Printer::ReceiptPrinter.new(print_settings) printer.print_receipt_bill(print_settings,cashier_terminal,saleObj.sale_items,saleObj,customer.name, item_price_by_accounts, discount_price_by_accounts, member_info,rebate_amount,shop_details, "Paid",current_balance,card_data) + + if params[:type] == "quick_service" + booking = Booking.find_by_sale_id(sale_id) + booking.booking_orders.each do |order| + Order.pay_process_order_queue(order.order_id,booking.dining_facility_id) + end + end end end end diff --git a/app/controllers/origami/quick_service_controller.rb b/app/controllers/origami/quick_service_controller.rb index 3b668a32..0c90530f 100644 --- a/app/controllers/origami/quick_service_controller.rb +++ b/app/controllers/origami/quick_service_controller.rb @@ -17,6 +17,7 @@ class Origami::QuickServiceController < ApplicationController def modify_order @menu = MenuCategory.all.active @table_id = params[:id] + @sale_id = params[:sale_id] @table = DiningFacility.find(@table_id) @booking = @table.get_booking if @booking @@ -39,6 +40,90 @@ class Origami::QuickServiceController < ApplicationController render "origami/addorders/detail" end + def update_modify_order + Rails.logger.debug "Order Source - " + params[:order_source].to_s + Rails.logger.debug "Table ID - " + params[:table_id].to_s + is_extra_time = false + extra_time = '' + + items_arr = [] + JSON.parse(params[:order_items]).each { |i| + i["item_instance_code"] = i["item_instance_code"].downcase.to_s + if i["item_instance_code"].include? "ext" + is_extra_time = true + arr_exts = i["item_instance_code"].split("_") + if arr_exts[1].match(/^(\d)+$/) + time = arr_exts[1].to_i*60*i["quantity"].to_i + extra_time = Time.at(time) + end + end + if i["parent_order_item_id"] + items = {"order_item_id": i["order_item_id"],"item_instance_code": i["item_instance_code"],"quantity": i["quantity"],"parent_order_item_id": i["parent_order_item_id"],"options": i["options"]} + else + items = {"order_item_id": i["order_item_id"],"item_instance_code": i["item_instance_code"],"quantity": i["quantity"],"options": i["options"]} + end + items_arr.push(items) + } + + # begin + @order = Order.new + @order.source = params[:order_source] + @order.order_type = params[:order_type] + @order.customer_id = params[:customer_id] == ""? "CUS-000000000001" : params[:customer_id] # for no customer id from mobile + @order.items = items_arr + @order.guest = params[:guest_info] + @order.table_id = params[:table_id] # this is dining facilities's id + @order.new_booking = true + @order.waiters = current_login_employee.name + @order.employee_name = current_login_employee.name + + @order.is_extra_time = is_extra_time + @order.extra_time = extra_time + + #Create Table Booking or Room Booking + if !params["booking_id"].nil? + # check booking id is already completed. + booking = Booking.find(params[:booking_id]) + + if booking + if booking.dining_facility_id.to_i == params[:table_id].to_i && booking.booking_status != 'moved' + if !booking.sale_id.nil? + sale_status = check_order_with_booking(booking) + if sale_status + return return_json_status_with_code(400, "bill requested") + end + else + @order.new_booking = false + @order.booking_id = params[:booking_id] + end + else + sale_status = check_order_with_table(params[:table_id]) + if sale_status + return return_json_status_with_code(400, "bill requested") + end + end + end #booking exists + else + sale_status = check_order_with_table(params[:table_id]) + if sale_status + # return false , @message = "bill requested" + return return_json_status_with_code(400, "bill requested") + end + end + + @status, @booking = @order.generate + + if @status && @booking && @order.source == 'quick_service' + if params[:sale_id] + @sale = Sale.find(params[:sale_id]) + update = Sale.add_to_existing_pending_invoice(@order.table_id,params[:sale_id]) + end + + result = {:status=> true, :data => @sale } + render :json => result.to_json + end + end + def get_menu_category () if (params[:id]) puts params[:id] @@ -66,4 +151,44 @@ class Origami::QuickServiceController < ApplicationController @product = Product.all end + # render json for http status code + def return_json_status_with_code(code, msg) + render status: code, json: { + message: msg, + booking_id: booking_id + }.to_json + end + + def check_order_with_table(table_id) + table = DiningFacility.find(table_id) + if table + booking = table.get_current_booking + # puts booking + if booking + if !booking.sale_id.nil? + if booking.sale.sale_status == "completed" || booking.sale.sale_status == "new" + @order.new_booking = true + return false + end + else + @order.new_booking = false + @order.booking_id = booking.booking_id + return false + end + end + end + end + + # this can always true + def check_order_with_booking(booking) + if booking.sale.sale_status == "completed" || booking.sale.sale_status == "new" + @order.new_booking = true + return false + else + @order.new_booking = false + @order.booking_id = params[:booking_id] + return false + end + end + end diff --git a/app/models/order.rb b/app/models/order.rb index c4326adb..0ca06316 100755 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -305,29 +305,10 @@ class Order < ApplicationRecord #Process order items and send to order queue def self.pay_process_order_queue(id,table_id) #Send to background job for processing - - cup_status = `#{"sudo service cups status"}` - print_status = check_cup_status(cup_status) - - if print_status OrderQueueProcessorJob.perform_later(id, table_id) - else - cup_start = `#{"sudo service cups start"}` - cup_status = `#{"sudo service cups status"}` - print_status = check_cup_status(cup_status) - - if print_status - OrderQueueProcessorJob.perform_later(id, table_id) - else - msg = ' Print Error ! Please contact to service' - ActionCable.server.broadcast "call_waiter_channel",table: msg,time:'print_error' - OrderQueueProcessorJob.perform_later(id, table_id) - end - end - end - def self.check_cup_status(status) + def check_cup_status(status) if status.include? "Active: active (running)" || "Active: active (exited)" #"Cup Server is already running" return true end diff --git a/app/models/sale.rb b/app/models/sale.rb index a5ffa8d9..6c6f5795 100755 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -1232,6 +1232,50 @@ end query = query.where("sales.sale_status = 'new' AND orders.status = 'billed' AND orders.source =? ","quick_service") end + def self.add_to_existing_pending_invoice(dining,sale_id) + puts sale_id + table = DiningFacility.find(dining) + existing_booking = Booking.find_by_sale_id(sale_id) + puts existing_booking.to_json + table.bookings.each do |booking| + # if !booking.checkout_at.nil? + # existing_booking.update_attributes(checkout_at: checkout_at) + # end + + if booking.sale_id.nil? + puts booking.to_json + booking.booking_orders.each do |booking_order| + + booking.booking_status = 'moved' + order = Order.find(booking_order.order_id) + order.status = 'billed' + order.order_items.each do |item| + item.order_item_status = 'billed' + end + # create sale item + saleobj = Sale.find(sale_id) + puts "saleobj" + puts saleobj.to_json + order.order_items.each do |orer_item| + saleobj.add_item (orer_item) + end + + # Re-compute for add + saleobj.compute + saleobj.save + order.save + booking.save + end + + booking_order = BookingOrder.where('booking_id=?',booking) + booking_order.each do |bo| + bo.booking_id = existing_booking.booking_id + bo.save + end + end + end + end + private def generate_custom_id diff --git a/app/views/layouts/_left_sidebar.html.erb b/app/views/layouts/_left_sidebar.html.erb index c6b1356a..3944f5a8 100755 --- a/app/views/layouts/_left_sidebar.html.erb +++ b/app/views/layouts/_left_sidebar.html.erb @@ -45,7 +45,7 @@ <% end %> <% if can? :index, :home %>
  • - + monetization_on <%= t :origami %> diff --git a/app/views/origami/addorders/detail.html.erb b/app/views/origami/addorders/detail.html.erb index 2c23b37b..8c6a5974 100755 --- a/app/views/origami/addorders/detail.html.erb +++ b/app/views/origami/addorders/detail.html.erb @@ -5,7 +5,9 @@ <% type = request.path_info.include?('quick_service')%> +<% modify_order = request.path_info.include?('modify_order')%> +
    <% else %>
    @@ -125,24 +132,13 @@ <% if type %> - - + + + <% end %> + <% if !modify_order %> - + <% end %>
    @@ -333,7 +329,7 @@ - <% if type %> + <% if type && !modify_order%>