From 84d483cad23dd2295754bd6e9606330ce68a11f4 Mon Sep 17 00:00:00 2001 From: Aung Myo Date: Wed, 30 Aug 2017 18:41:54 +0630 Subject: [PATCH] update addorder --- app/assets/javascripts/addorder.js | 169 ++++++++++++++---- app/assets/stylesheets/addorder.scss | 41 ++++- .../menu_item_instances_controller.rb | 9 + .../origami/addorders_controller.rb | 1 + .../menu_item_instances/show.json.jbuilder | 39 ++++ app/views/origami/addorders/detail.html.erb | 30 ++-- .../get_instance.json.jbuilder | 26 +++ config/routes.rb | 3 +- 8 files changed, 265 insertions(+), 53 deletions(-) create mode 100644 app/controllers/api/restaurant/menu_item_instances_controller.rb create mode 100644 app/views/api/restaurant/menu_item_instances/show.json.jbuilder diff --git a/app/assets/javascripts/addorder.js b/app/assets/javascripts/addorder.js index 54b1c7b6..158246d4 100644 --- a/app/assets/javascripts/addorder.js +++ b/app/assets/javascripts/addorder.js @@ -12,6 +12,12 @@ $(function(){ qty = $(this).val(); price = $("#unit_price").text(); $("#total_price").text(qty*price); + }); + + $("#set_change_qty").change(function(){ + qty = $(this).val(); + price = $("#set_total_price").text(); + $("#set_total_price").text(qty*price); }); //show menu item list when click menu category @@ -98,23 +104,13 @@ $(function(){ //end Ajax } //end show list function - //click item row for add order + //click item row for item set $(document).on('click', '.set_item_box', function(event){ $(".instance-list").empty(); + $(".options-list").empty(); + data = $(this).parent().children().children('.add_icon'); - item_options = $(this).data('option'); - for(var field in item_options) { - value = item_options[field]["values"]; - type = item_options[field]["type"]; - row = "

"+type+"

" - $(value).each(function(i){ - row +=""; - }); - $(".opt-list").append(row); - } - instances = $(this).data('instance'); item_sets = $(this).data('item-sets'); @@ -123,21 +119,25 @@ $(function(){ $(value).each(function(i){ $.ajax({ type: "GET", - url: '../../settings/menu_item_instances/get_instance/'+value[i]["id"], + url: '../../api/restaurant/menu_item_instances/'+value[i]["id"], data: {id:value[i]}, - success:function(result){ - // var image_path = "" - console.log(result) - row = '
' - +'
' + success:function(result){ + + row = '
' + +"
" +'
' +''+result["name"]+'' +'
' +'
' - +'' + +'' +'
' +'' +'
' +'
' @@ -146,16 +146,97 @@ $(function(){ } }); }); - } - $('#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')); + $('#set_change_qty').val(1); + $('#set_item_instances').text(instances); + $('#set_name').text(data.attr('data-name')); + $('#set_item_code').text(data.attr('data-item-code')); + $('#set_total_price').text(data.attr('data-price')); + $('#set_unit_price').text(data.attr('data-price')); }); + // click instance for add item set + $(document).on('click', '.instance_box', function(event){ + + $(".options-list").empty(); + + item_options = $(this).data('option'); + code = $(this).data('code'); + + 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); + } + + if($(this).hasClass('selected-instance') == true){ + sub_total = $('#set_total_price').text(); + name = $(this).data('name'); + price = $(this).data('price'); + qty = $('#set_change_qty').val(); + total = qty*price; + var total_price = +sub_total - +total; + $(this).removeClass('selected-instance'); + $(".options-list").empty(); + $(this).removeAttr('data-options'); + $('#instance_option').text('') + }else { + sub_total = $('#set_total_price').text(); + name = $(this).data('name'); + price = $(this).data('price'); + qty = $('#set_change_qty').val(); + total = qty*price; + var total_price = +sub_total + +total; + $(this).addClass('selected-instance'); + } + $('#set_total_price').text(total_price); + }); //End selecct attribute buttom + + // click add order + $(document).on('click', '.set_order', function(event){ + total_price = $('#set_total_price').text(); + qty = parseInt($('#set_change_qty').val()); + item_code = $('#set_item_code').text(); + item_name = $('#set_name').text(); + + var items = $('.selected-instance'); + + attribute_arr = [] + option_arr = [] + var rowCount = $('.summary-items tbody tr').length+1; + $(items).each(function(i){ + code = $(items[i]).attr('data-code'); + name = $(items[i]).attr('data-name'); + price = $(items[i]).attr('data-price'); + option = $(items[i]).attr('data-options'); + total = qty * price ; + option_arr.push(option); + row ="" + +''+rowCount+'' + +'' + item_name+ ' ' + name +'' + +'' + qty + '' + +'' + + parseFloat(total).toFixed(2) + +'' + +''; + $(".summary-items tbody").append(row); + rowCount = rowCount + 1; + }); + calculate_sub_total(); + + }); //End add order Click + //click item row for add order $(document).on('click', '.menu_item_box', function(event){ $('.attributes-list').empty(); @@ -214,8 +295,8 @@ $(function(){ type = item_options[field]["type"]; row = "

"+type+"

" $(value).each(function(i){ - row +=""; + row +=""; }); $(".options-list").append(row); } @@ -271,13 +352,27 @@ $(function(){ $(document).on('click', '.option_btn', function(event){ value = $(this).data('value'); type = $(this).data('type'); - options = $(".option_btn"); + group = $(this).data('group'); + options = $(".option_btn"); $(options).each(function(i){ if ($(options[i]).attr('data-type')==type){ $('.'+type).removeClass("selected-option"); } }); $(this).addClass('selected-option'); + + if(group == "set_menu"){ + code = $(this).data('code'); + value = $(this).data('value'); + + instance = $(".selected-instance"); + $(instance).each(function(i){ + if ($(instance[i]).attr('data-code')==code){ + $(instance[i]).attr('data-options',value); + $(instance[i]).children().children('#instance_option').text(value); + } + }); + } }); //End selecct attribute buttom @@ -310,7 +405,7 @@ $(function(){ show_item_detail(item_data); calculate_sub_total(); - }); //End Add Icon Click + }); //End add order Click // click plus icon for add $(document).on('click', '.add_icon', function(event){ @@ -320,6 +415,7 @@ $(function(){ }); //End Add Icon Click function show_item_detail(data){ + console.log(data) qty = parseInt(data.attr('data-qty')); append = 0; price = parseFloat(data.attr('data-price')).toFixed(2); @@ -376,11 +472,11 @@ $(function(){ // Pay Discount for Payment $("#create_order").on('click', function(e){ e.preventDefault(); - $( "#loading_wrapper" ).show(); + $("#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, @@ -390,7 +486,7 @@ $(function(){ 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': "", @@ -403,10 +499,10 @@ $(function(){ data: params, dataType: "json", success:function(result){ - $( "#loading_wrapper" ).show(); + $("#loading_wrapper").hide(); $.confirm({ title: 'Infomation!', - content: "result.status", + content: "Order has been successfully created", buttons: { confirm: { text: 'Ok', @@ -501,6 +597,7 @@ $(function(){ var item_row = $('.summary-items tbody tr'); $(item_row).each(function(i){ var order_item = {}; + console.log($(item_row[i]).attr('data-options')); 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(); diff --git a/app/assets/stylesheets/addorder.scss b/app/assets/stylesheets/addorder.scss index 29ca7156..b819b47d 100644 --- a/app/assets/stylesheets/addorder.scss +++ b/app/assets/stylesheets/addorder.scss @@ -87,9 +87,16 @@ element.style { color: #fff !important; background-color: green !important; } +.card { + border: 2px solid rgba(0, 0, 0, 0.125) !important; + } +.selected-instance { + border: 2px solid #7a62d3 !important; +} + .attribute_btn { white-space: normal !important; - height: 40px; + /*width: 80px;*/ margin-bottom: 5px; margin-right: 5px; @@ -97,7 +104,7 @@ element.style { .option_btn { white-space: normal !important; - height: 40px; + /*width: 80px;*/ margin-bottom: 5px; margin-right: 5px; @@ -115,4 +122,34 @@ element.style { height: 80px; border: 1px solid #54A5AF; padding: 10px; +} + +.card-footer{ + padding:0.35rem 1.25rem !important; +} + +/*Loading gif for payment*/ + +#loading_wrapper{ + + position: fixed; + background-color: #C8C8C8 ; + height: 100%; + width: 100%; + left: 0; + opacity: 0.6; + top: 0; + z-index: 9999999; +} +#loading{ + position: relative; + height: 100%; + width: 100%; + background-image: url('../../../image/loading-ajax.gif'); + background-position: center center; + background-repeat: no-repeat; + opacity: 1; + filter: alpha(opacity=100); /* ie */ + -moz-opacity: 1; /* mozilla */ + } \ No newline at end of file diff --git a/app/controllers/api/restaurant/menu_item_instances_controller.rb b/app/controllers/api/restaurant/menu_item_instances_controller.rb new file mode 100644 index 00000000..be38f782 --- /dev/null +++ b/app/controllers/api/restaurant/menu_item_instances_controller.rb @@ -0,0 +1,9 @@ +class Api::Restaurant::MenuItemInstancesController < Api::ApiController + skip_before_action :authenticate + #Description + # Pull the default menu details and also other available (active) menus + # Input Params - order_id + def show + @id = MenuItemInstance.find(params[:id]) + end +end diff --git a/app/controllers/origami/addorders_controller.rb b/app/controllers/origami/addorders_controller.rb index 23a7bc26..1f9d70f3 100644 --- a/app/controllers/origami/addorders_controller.rb +++ b/app/controllers/origami/addorders_controller.rb @@ -37,6 +37,7 @@ class Origami::AddordersController < BaseOrigamiController def create Rails.logger.debug "Order Source - " + params[:order_source].to_s Rails.logger.debug "Table ID - " + params[:table_id].to_s + puts params[:order_items] 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"]} diff --git a/app/views/api/restaurant/menu_item_instances/show.json.jbuilder b/app/views/api/restaurant/menu_item_instances/show.json.jbuilder new file mode 100644 index 00000000..69302b8e --- /dev/null +++ b/app/views/api/restaurant/menu_item_instances/show.json.jbuilder @@ -0,0 +1,39 @@ +if(@id) + menu_item = MenuItem.find(@id.menu_item_id) + # Format for option json + opt_format = [] + # Format for attributes json + menu_item.item_options.each do|opt| + menu_opt = MenuItemOption.find(opt) + if opt_format.count == 0 + opt_format.push({ type: menu_opt.option_type, values: [menu_opt.name] }) + next + end + + opt_format.each do |of| + if menu_opt.option_type.in? opt_format.map {|k| k[:type]} + if menu_opt.option_type == of[:type] + of[:values].push(menu_opt.name) + end + else + new_opt = {type: menu_opt.option_type, values: [ menu_opt.name ] } + opt_format.push(new_opt) + break + end + end + end + + json.success true + json.id @id.id + json.name @id.item_instance_name + json.code @id.item_instance_code + + json.item_id @id.menu_item_id + json.attributes @id.item_attributes + json.price @id.price + json.is_default @id.is_default + + json.options opt_format +else + json.success false +end diff --git a/app/views/origami/addorders/detail.html.erb b/app/views/origami/addorders/detail.html.erb index b7f0cf1d..a1bd22dc 100644 --- a/app/views/origami/addorders/detail.html.erb +++ b/app/views/origami/addorders/detail.html.erb @@ -147,39 +147,41 @@