diff --git a/app/assets/javascripts/addorder.js b/app/assets/javascripts/addorder.js new file mode 100644 index 00000000..b9219c52 --- /dev/null +++ b/app/assets/javascripts/addorder.js @@ -0,0 +1,441 @@ +$(function(){ + + //click menu sidebar menu category + $(".menu_category").on("click", function(){ + var menu_id = $(this).find(".menu-id").text(); + var url = $(this).attr('data-ref'); + show_menu_item_list(url); + }); + //End menu category Click + + $(".change_qty").change(function(){ + qty = $(this).val(); + price = $("#unit_price").text(); + $("#total_price").text(qty*price); + }); + + //show menu item list when click menu category + function show_menu_item_list(url_item){ + var menu_list = $('.menu_items_list'); + menu_list.empty(); + //Start Ajax + $.ajax({ + type: "GET", + url: url_item, + data: {}, + dataType: "json", + success: function(data) { + var menu_items_list = $('.menu_items_list'); + menu_items_list.empty(); + menu_items = data.menu_items; + + for(var field in menu_items) { + instances = menu_items[field].instances ; + $(instances).each(function(i){ + if (instances[i].is_default === true) { + code = instances[i].code; + name = instances[i].name; + price = parseFloat(instances[i].price).toFixed(2); + is_available = instances[i].is_available ; + is_on_promotion = instances[i].is_on_promotion; + item_attributes = instances[i].values; + promotion_price = instances[i].promotion_price; + code = instances[i].code; + } + }); + + qty = 1; + options = []; + + row = '
' + +'
' + +''+ menu_items[field].name +'' + +"
" + +'' + +'
' + +'
' + + +"'; + $('.menu_items_list').append(row); + } + } + }); + //end Ajax + } + //end show list function + + //click item row for add order + $(document).on('click', '.menu_item_box', function(event){ + $('.attributes-list').empty(); + $('.options-list').empty(); + $('.attr-alert').addClass('hide'); + $('.add_to_order').removeAttr('data-instance-code'); + $('.add_to_order').removeAttr('data-instance'); + $('.add_to_order').removeAttr('data-price'); + $('.add_to_order').removeAttr('data-promotion-price'); + $('.add_to_order').removeAttr('data-item-code'); + $('.add_to_order').removeAttr('data-qty'); + $('.add_to_order').removeAttr('data-name'); + $('.add_to_order').removeAttr('data-attributes'); + $('.add_to_order').removeAttr('data-options'); + + data = $(this).parent().children().children('.add_icon'); + attributes = $(this).data('id'); + selected_item = $(this).data('item'); + instances = $(this).data('instance'); + item_options = $(this).data('option'); + + instance_attributes = [] + for(var field in instances) { + value = instances[field].values; + $(value).each(function(i){ + options = value[i]; + instance_attributes.push(options); + }); + } + + for(var field in attributes) { + value = attributes[field]["values"]; + type = attributes[field]["type"] + row = "

"+attributes[field]["type"]+"

" + $(value).each(function(i){ + disabled = "" + status =""; + + if(parseInt(jQuery.inArray(value[i], selected_item))!== -1){ + status = "selected-attribute"; + } + if(parseInt(jQuery.inArray(value[i], instance_attributes)) == -1){ + disabled = "disabled"; + } + row +=""; + }); + $(".attributes-list").append(row); + } + + for(var field in item_options) { + value = item_options[field]["values"]; + type = item_options[field]["type"]; + row = "

"+type+"

" + $(value).each(function(i){ + row +=""; + }); + $(".options-list").append(row); + } + + $('#change_qty').val(1); + $('#item_instances').text(instances); + $('#title_name').text(data.attr('data-name')); + $('#item_code').text(data.attr('data-item-code')); + $('#total_price').text(data.attr('data-price')); + $('#unit_price').text(data.attr('data-price')); + }); + + // click select option icon for add + $(document).on('click', '.attribute_btn', function(event){ + value = $(this).data('value'); + type = $(this).data('type'); + instances = $(this).data('instances'); + if ($(".attribute_btn").attr("data-type")==type){ + $('.'+type).removeClass("selected-attribute"); + } + $(this).addClass('selected-attribute'); + + var selected_attr = get_selected_attributes('selected-attribute'); + + qty = $('#change_qty').val(); + + for(var field in instances) { + item_attr = instances[field].values; + + if(JSON.stringify(item_attr) == JSON.stringify(selected_attr)){ + unit_price = instances[field].price ; + total_price = qty * unit_price ; + $('#instance_name').text(instances[field].name); + $('#instance_code').text(instances[field].code); + $('#promotion_price').text(instances[field].promotion_price); + $('#total_price').text(total_price); + $('#unit_price').text(unit_price); + } + } + + }); //End selecct attribute buttom + + // click select option icon for add + $(document).on('click', '.option_btn', function(event){ + value = $(this).data('value'); + type = $(this).data('type'); + if ($(".option_btn").attr("data-type")==type){ + $('.'+type).removeClass("selected-option"); + } + $(this).addClass('selected-option'); + + }); //End selecct attribute buttom + + // click add order + $(document).on('click', '.add_to_order', function(event){ + total_price = $('#total_price').text(); + qty = parseInt($('#change_qty').val()); + + var item_row = $('.selected-attribute'); + var instances = jQuery.parseJSON(item_row.attr('data-instances')); + + attribute_arr = get_selected_attributes('selected-attribute'); + option_arr = get_selected_attributes('selected-option'); + + for(var field in instances) { + if (JSON.stringify(attribute_arr) === JSON.stringify(instances[field].values)) { + + $('.add_to_order').attr('data-instance-code',instances[field].code); + $('.add_to_order').attr('data-instance',instances[field].name); + $('.add_to_order').attr('data-price',instances[field].price); + $('.add_to_order').attr('data-promotion-price',instances[field].promotion_price); + } + } + $('.add_to_order').attr('data-item-code',$('#item_code').text()); + $('.add_to_order').attr('data-qty',qty); + $('.add_to_order').attr('data-name',$('#title_name').text()); + $('.add_to_order').attr('data-attributes',JSON.stringify(attribute_arr)); + $('.add_to_order').attr('data-options',JSON.stringify(option_arr)); + var item_data = $(this); + show_item_detail(item_data); + calculate_sub_total(); + + }); //End Add Icon Click + + // click plus icon for add + $(document).on('click', '.add_icon', function(event){ + var item_data = $(this); + show_item_detail(item_data); + calculate_sub_total(); + }); //End Add Icon Click + + function show_item_detail(data){ + qty = parseInt(data.attr('data-qty')); + append = 0; + price = parseFloat(data.attr('data-price')).toFixed(2); + instance_name = data.attr('data-instance'); + if (instance_name == "undefined"){ + instance = ''; + }else{ + instance = "("+data.attr('data-instance')+")"; + } + + var rowCount = $('.summary-items tbody tr').length+1; + var item_row = $('.summary-items tbody tr'); + + $(item_row).each(function(i){ + item_code = $(item_row[i]).attr('data-code'); + instance_code = $(item_row[i]).attr('data-instance-code'); + + if (item_code == data.attr('data-item-code') && instance_code == data.attr('data-instance-code')) { + if (qty > 1) { + qty = parseInt($(item_row[i]).children('#item_qty').text()) + qty; + }else{ + qty = parseInt($(item_row[i]).children('#item_qty').text()) + 1; + } + + $(item_row[i]).children('#item_qty').text(qty); + parseFloat($(item_row[i]).children('#item_price').text(price*qty)).toFixed(2); + append =1; + }else{ + if (qty > 1) { + qty = qty; + }else{ + qty = 1; + } + } + }); + if (append===0) { + row ="" + +''+rowCount+'' + +'' + data.attr('data-name')+ ' ' + instance +'' + +'' + qty + '' + +'' + + parseFloat(price).toFixed(2) + +'' + +''; + $(".summary-items tbody").append(row); + } + } + + // Pay Discount for Payment + $("#create_order").on('click', function(e){ + e.preventDefault(); + $( "#loading_wrapper" ).show(); + var table_id = $('#table_id').text(); + var booking_id = $('#booking_id').text(); + if (!booking_id.length > 0) { + console.log("hi") + var params = {'order_source': "cashier", 'order_type': "dine_in", + 'customer_id': "", 'guest_info': "",'booking_id':booking_id, + 'table_id': table_id, + 'order_items': order_items }; + } + var table_type = $('#table_type').text(); + var order_items = JSON.stringify(get_order_item_rows()); + + var ajax_url = '../addorders/create'; + console.log(ajax_url) + + var params = {'order_source': "cashier", 'order_type': "dine_in", + 'customer_id': "", 'guest_info': "", + 'table_id': table_id, + 'order_items': order_items }; + + $.ajax({ + type: "POST", + url: ajax_url, + data: params, + dataType: "json", + success:function(result){ + $( "#loading_wrapper" ).show(); + $.confirm({ + title: 'Infomation!', + content: "result.status", + buttons: { + confirm: { + text: 'Ok', + btnClass: 'btn-green', + action: function(){ + if(table_type == "Table"){ + window.location.href = "/origami/table/" + table_id + } + else { + window.location.href = "/origami/room/" + table_id + } + } + } + } + }); + } + }); + }); + + //click item row for update qty + $('.summary-items').on('click', '.item_box', function(){ + $(this).attr('data-active',true); + name = $(this).children('#item_name').text(); + qty = $(this).children('#item_qty').text(); + $('#modal-item-name').text(name); + $('#modal-qty').val(qty); + }); + + //click remove buttom in modal box + $('#sx_itemModal').on('click','#remove', function(){ + $('.summary-items tr').filter(function(){ + if ($(this).attr('data-active') == 'true'){ + $(this).remove(); + } + }); + calculate_sub_total(); + }); + + //click close + $('#sx_itemModal').on('click','#close', function(){ + $(".item_box").removeAttr("data-active"); + }); + + //click save buttom after change qty + $('#sx_itemModal').on('click','#save', function(){ + summary_items_filter(); + calculate_sub_total(); + }); + + //calculate subtotal + function calculate_sub_total(){ + var total_price = 0; + var taxable_amount = 0; + var item_row = $('.summary-items tbody tr'); + + $(item_row).each(function(i){ + var unit_price = parseFloat($(item_row[i]).attr('data-price')); + var qty = parseFloat($(item_row[i]).children('#item_qty').text()); + total_price += qty*unit_price; + }); + var fixed_total_price = parseFloat(total_price).toFixed(2); + var fixed_taxable_amount = parseFloat(taxable_amount).toFixed(2); + + $('#sub_total').empty(); + $('#sub_total').append(fixed_total_price); + + if (fixed_total_price > 0) { + $('.create').removeAttr("disabled", false); + }else{ + $('.create').attr("disabled", true); + } + } + + function summary_items_filter(){ + $('.summary-items tr').filter(function(){ + if ($(this).attr('data-active') == 'true'){ + qty = $('#modal-qty').val(); + price = parseFloat($(this).attr('data-price')); + total_price = parseFloat(price*qty).toFixed(2); + + $(this).find('#item_qty').text(qty); + $(this).find('.item-cell-price').text(total_price); + + $(this).removeAttr('data-active'); + } + }); + } + + /* Get discount Item rows */ + function get_order_item_rows(){ + var order_items = []; + var item_row = $('.summary-items tbody tr'); + $(item_row).each(function(i){ + var order_item = {}; + order_item.order_item_id = $(item_row[i]).attr('data-row'); + order_item.item_instance_code = $(item_row[i]).attr('data-instance-code'); + order_item.quantity = $(item_row[i]).children('#item_qty').text(); + order_item.options = $(item_row[i]).attr('data-options'); + order_items.push(order_item); + }); + return order_items; + } + + function get_selected_attributes(selected_class) { + var item_row = $('.'+selected_class); + var attribute_arr = []; + $(item_row).each(function(i){ + value = $(item_row[i]).attr('data-value'); + str = value; + attribute_arr.push(str); + }); + return attribute_arr; + } + +}); \ No newline at end of file diff --git a/app/assets/stylesheets/addorder.scss b/app/assets/stylesheets/addorder.scss new file mode 100644 index 00000000..01e01e15 --- /dev/null +++ b/app/assets/stylesheets/addorder.scss @@ -0,0 +1,102 @@ +@import "bootstrap"; +@import "font-awesome"; +@import "theme"; +@import "jquery-ui"; +@import "bootstrap-datepicker3"; + +.card-head{ + height: 40px; + background-color: #54A5AF; + padding:5px; + color :#fff; + } + .card-block { + padding: 0.3rem !important; + } + @media (min-width: 34em) { + .card-columns { + -webkit-column-count: 2; + -moz-column-count: 2; + column-count: 2; + } + } + + @media (min-width: 48em) { + .card-columns { + -webkit-column-count: 3; + -moz-column-count: 3; + column-count: 3; + } + } + + @media (min-width: 62em) { + .card-columns { + -webkit-column-count: 4; + -moz-column-count: 4; + column-count: 4; + } + } + + @media (min-width: 75em) { + .card-columns { + -webkit-column-count: 4; + -moz-column-count: 4; + column-count: 4; + } + } + +.nav-tabs .nav-item { + border-bottom: 1px solid #fff; + border-left: 1px solid #54A5AF; + border-right: 1px solid #54A5AF; +} +.nav > li > a{ + color:#54A5AF; +} +.nav-tabs { + background-color: #eeeeee; +} +.nav-tabs .nav-link.active, .nav-tabs .nav-item.show .nav-link { + background-color: #fff; + border-left: 6px solid #111; + color:#54A5AF; + font-weight: bold; + border-color: #fff #fff #fff #54A5AF; +} + +.modal-dialog { + left:auto; + right: auto; + width: 100%; + padding-top: 200px; + +} +.selected-attribute { + color: #fff !important; + background-color: green !important; +} +.selected-option { + color: #fff !important; + background-color: green !important; +} +.attribute_btn { + white-space: normal !important; + height: 40px; + /*width: 80px;*/ + margin-bottom: 5px; + margin-right: 5px; +} + +.option_btn { + white-space: normal !important; + height: 40px; + /*width: 80px;*/ + margin-bottom: 5px; + margin-right: 5px; +} +#logo{ + margin: auto; + width: 50%; + border: 1px solid #54A5AF; + padding: 10px; +} \ No newline at end of file diff --git a/app/controllers/api/orders_controller.rb b/app/controllers/api/orders_controller.rb index 8c7d7396..fe5d1d90 100644 --- a/app/controllers/api/orders_controller.rb +++ b/app/controllers/api/orders_controller.rb @@ -1,5 +1,5 @@ class Api::OrdersController < Api::ApiController - + skip_before_action :authenticate #Description # This API show current order details # Input Params - order_id diff --git a/app/controllers/origami/addorders_controller.rb b/app/controllers/origami/addorders_controller.rb index e65b24b7..23a7bc26 100644 --- a/app/controllers/origami/addorders_controller.rb +++ b/app/controllers/origami/addorders_controller.rb @@ -1,5 +1,5 @@ class Origami::AddordersController < BaseOrigamiController - before_action :set_dining, only: [:show] + # before_action :set_dining, only: [:detail] def index @tables = Table.all.active.order('zone_id asc').group("zone_id") @@ -8,15 +8,134 @@ class Origami::AddordersController < BaseOrigamiController @all_room = Room.all.active.order('status desc') end - def show + def detail @menu = MenuCategory.all @table_id = params[:id] + @table = DiningFacility.find(@table_id) + @booking = @table.get_booking + if @booking + @booking_id = @booking.booking_id + @order_items = Array.new + @booking.booking_orders.each do |booking_order| + order = Order.find(booking_order.order_id) + if (order.status == "new") + @obj_order = order + @customer = order.customer + @date = order.created_at + order.order_items.each do |item| + @order_items.push(item) + end + end + end + else + @booking = nil + end + + + end + + def create + Rails.logger.debug "Order Source - " + params[:order_source].to_s + Rails.logger.debug "Table ID - " + params[:table_id].to_s + items_arr = [] + JSON.parse(params[:order_items]).each { |i| + items = {"order_item_id": i["order_item_id"],"item_instance_code": i["item_instance_code"],"quantity": i["quantity"],"options": i["options"]} + 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.employee_name = current_login_employee.name + #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) + # puts "WWwwWWWWWWww" + # puts sale_status + 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]) + # puts "OOOOOOOOO" + # puts sale_status + 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]) + # puts "MMMMMMMM" + # puts sale_status + if sale_status + # return false , @message = "bill requested" + return return_json_status_with_code(400, "bill requested") + end + end + + @status, @booking = @order.generate + + 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 private - def set_dining - @dining = DiningFacility.find(params[:id]) - end + # def set_dining + # @dining = DiningFacility.find(params[:id]) + # end end diff --git a/app/models/order.rb b/app/models/order.rb index e26d1ee2..0551a1c9 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -134,6 +134,8 @@ class Order < ApplicationRecord def re_order_items(form_items) #reorder inputs items as parents and child parent_id = Array.new parents = Array.new + puts form_items + puts "hhhhh" parents_and_children_items = Array.new new_items_list = Array.new diff --git a/app/views/origami/addorders/create.json.jbuilder b/app/views/origami/addorders/create.json.jbuilder new file mode 100644 index 00000000..175e6e94 --- /dev/null +++ b/app/views/origami/addorders/create.json.jbuilder @@ -0,0 +1,15 @@ +if @status == true + json.status :success + json.id @order.id + json.booking_id @booking.id + json.order_items do + json.array! @order.order_items, :item_code, :item_name, :qty, :options, :remark + end +else + json.status :error + if @error_messages + json.error_messages @error_messages + else + json.error_messages @order.errors + end +end diff --git a/app/views/origami/addorders/detail.html.erb b/app/views/origami/addorders/detail.html.erb new file mode 100644 index 00000000..a8534f94 --- /dev/null +++ b/app/views/origami/addorders/detail.html.erb @@ -0,0 +1,147 @@ +<%= stylesheet_link_tag 'addorder', media: 'all', 'data-turbolinks-track': 'reload' %> +<%= javascript_include_tag 'addorder', 'data-turbolinks-track': 'reload' %> +
+
+ +
+
+ +
+ +
+
+
+ ORDER DETAILS | Table <%=@table_id%> + + + +
+
+
+
+ + + + + + + + + + + + +
#ItemsQTYPrice
+
+ +
+
+ + + + + + + + + diff --git a/app/views/origami/addorders/show.html.erb b/app/views/origami/addorders/show.html.erb deleted file mode 100644 index 8f9b1c08..00000000 --- a/app/views/origami/addorders/show.html.erb +++ /dev/null @@ -1,525 +0,0 @@ -
-
- -
-
- -
- -
-
-
- ORDER DETAILS | Table <%=@table_id%> -
-
-
-
- - - - - - - - - - - - -
#ItemsQTYPrice
-
- -
-
- - - - - - - - - - - - - - diff --git a/config/initializers/assets.rb b/config/initializers/assets.rb index adb297d1..bcb83367 100644 --- a/config/initializers/assets.rb +++ b/config/initializers/assets.rb @@ -32,3 +32,7 @@ Rails.application.config.assets.precompile += %w( jquery-confirm.js ) # --- Image Uploader ---- Rails.application.config.assets.precompile += %w( fileinput.min.js ) + +# --- Customer/ Customer - Crm ---- +Rails.application.config.assets.precompile += %w( addorder.css ) +Rails.application.config.assets.precompile += %w( addorder.js ) diff --git a/config/routes.rb b/config/routes.rb index 83e7c150..55d3b1f2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -181,7 +181,9 @@ Rails.application.routes.draw do post '/:sale_id/get_customer' => "customers#get_customer" - resources :addorders + get '/:addorders/:id' => "addorders#detail" + post '/:addorders/create' => "addorders#create", :defaults => { :format => 'json' } + resources :commissions resources :commissioners resources :in_juties