diff --git a/app/controllers/settings/promotions_controller.rb b/app/controllers/settings/promotions_controller.rb index 4ab33dba..8acc7764 100644 --- a/app/controllers/settings/promotions_controller.rb +++ b/app/controllers/settings/promotions_controller.rb @@ -90,6 +90,21 @@ class Settings::PromotionsController < ApplicationController end end + def find_parent_item + res = [] + item = MenuItemInstance.find_by_item_instance_code(params[:item_instance_code]) + if item.nil? + product = Product.where("item_code = ?",params[:item_instance_code]).pluck(:name,:item_code) + res.push(product.name) + res.push(product.item_code) + else + # menu_item = item.menu_item.pluck(:name,:item_code) + res.push(item.item_instance_name) + res.push(item.menu_item.item_code) + end + render json: res + end + private # Use callbacks to share common setup or constraints between actions. def set_promotion diff --git a/app/models/promotion.rb b/app/models/promotion.rb index 624818f1..41dfaec6 100644 --- a/app/models/promotion.rb +++ b/app/models/promotion.rb @@ -1,5 +1,5 @@ class Promotion < ApplicationRecord - validates_presence_of :promo_code,:promo_start_date,:promo_end_date,:promo_start_hour,:promo_end_hour,:promo_day,:promo_type,:original_product,:min_qty + validates_presence_of :promo_code,:promo_start_date,:promo_end_date,:promo_start_hour,:promo_end_hour,:promo_type,:original_product,:min_qty has_many :promotion_products diff --git a/app/views/settings/promotions/_form.html.erb b/app/views/settings/promotions/_form.html.erb index c832461d..477731a1 100644 --- a/app/views/settings/promotions/_form.html.erb +++ b/app/views/settings/promotions/_form.html.erb @@ -39,9 +39,10 @@ <% end %> - +
- <%= f.hidden_field :promo_day, :value => "", :class => "form-control col-md-1" %> +
+ <%= f.hidden_field :promo_day, :value => "", :class => "form-control" %>
Sun
Mon
Tue
@@ -50,7 +51,7 @@
Fri
Sat
- +
<%= f.input :promo_type,input_html: { class: "" }, @@ -73,11 +74,12 @@ <% code = menuiteminstance.menu_item.item_code %> <% end %> <% end %> + <% arr.each do |a| %> <% if a[1] == code %> <% else %> - + <% end %> <% end %> @@ -109,19 +111,23 @@
<%= f.fields_for :promotion_products do |pro| %>
+ <%= pro.hidden_field :item_code,:class => "promo_product" %> <% arr = MenuItem.active.order("name desc").pluck(:name,:item_code) %> <% Product.order("name desc").pluck(:name,:item_code).each do |p| %> <% arr.push(p) %> <% end %> -
+ - <% sample = [] %> -
<%= pro.input :item_code, :class => 'promoproduct', collection: sample,input_html: { selected: 2 }, label: false %>
+ + +
<%= pro.input :item_code, :class => 'promoproduct', collection:[],input_html: { selected: 2 }, label: false %>
+
<%= pro.input :min_qty , label: false%>
<%= pro.input :net_off , label: false %>
<%= pro.input :net_price , label: false %>
@@ -156,15 +162,12 @@ $(document).ready(function(){ $('#promotion_promo_start_hour').datetimepicker({ datepicker:false, format:'H:m' - }); + }); $('#promotion_promo_end_hour').datetimepicker({ datepicker:false, format:'H:m' }); - - - var form = document.getElementById("new_promotion"); var inputs = $("input"); var arr = []; @@ -197,7 +200,7 @@ $(".selectDay").click(function() { $("#promotion_original_product").select2(); $(".item_code_place").select2(); $(".item_code_place").on('change', function(event) { - var ajax_url = "<%= settings_find_item_instance_path %>"; + var ajax_url = "<%= settings_find_item_instance_path %>"; var item_code = this.value; $.ajax({ type: "GET", @@ -223,6 +226,28 @@ $(".selectDay").click(function() { callforpromoproduct(); }, 0); }); + + var promopdt = $(".promo_product"); + jQuery.each( promopdt, function( i, ppdt ) { + var ajax_url = "<%= settings_find_parent_item_path %>"; + var item_code = ppdt.value; + var select_id = ppdt.id; + $.ajax({ + type: "GET", + url: ajax_url, + data: 'item_instance_code=' + item_code, + success: function (result) { + if(result.length > 0){ + $("select#"+select_id).empty(); + $("select#"+select_id).append(""); + $("select#"+select_id).parent().parent().siblings("div.menu_item_choose").find("select").find("option[value='"+result[1]+"']").attr("selected","true") + $("select#"+select_id).parent().parent().siblings("div.menu_item_choose").find("select").select2(); + } + } + }); + }); + + // promotion_promotion_products_attributes_0_item_code function callforpromoproduct(){ $(".item_code_place1").select2(); $(".item_code_place1").on('change', function(event) { @@ -234,16 +259,17 @@ $(".selectDay").click(function() { url: ajax_url, data: 'item_code=' + item_code, success: function (result) { - $("#"+id).empty(); + $("select#"+id).empty(); var itemlist; for (var i = 0; i < result.length; i++) { itemlist += "" } - $("#"+id).append(itemlist); - $("#"+id).select2(); + $("select#"+id).append(itemlist); + $("select#"+id).select2(); } }); }); } + }); diff --git a/config/routes.rb b/config/routes.rb index 8dcdb956..18f3c0b5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -299,6 +299,7 @@ Rails.application.routes.draw do resources :promotion_products end get '/find_item_instance' => 'promotions#find_item_instance', as:'find_item_instance' + get '/find_parent_item' => 'promotions#find_parent_item', as:'find_parent_item' # commission resources :commissions