diff --git a/app/controllers/settings/promotions_controller.rb b/app/controllers/settings/promotions_controller.rb index af8bedc1..8acc7764 100644 --- a/app/controllers/settings/promotions_controller.rb +++ b/app/controllers/settings/promotions_controller.rb @@ -69,6 +69,9 @@ class Settings::PromotionsController < ApplicationController # DELETE /promotions/1 # DELETE /promotions/1.json def destroy + @promotion.promotion_products.each do |pp| + pp.destroy + end @promotion.destroy respond_to do |format| format.html { redirect_to settings_promotions_path, notice: 'Promotion was successfully destroyed.' } @@ -87,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..1760f894 100644 --- a/app/views/settings/promotions/_form.html.erb +++ b/app/views/settings/promotions/_form.html.erb @@ -39,18 +39,19 @@ <% end %> - +
- <%= f.hidden_field :promo_day, :value => "", :class => "form-control col-md-1" %> -
Sun
-
Mon
-
Tue
-
Wed
-
Thu
-
Fri
-
Sat
+
+ <%= f.hidden_field :promo_day, :class => "form-control" %> +
+
+
+
+
+
+
- +
<%= 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,14 +162,24 @@ $(document).ready(function(){ $('#promotion_promo_start_hour').datetimepicker({ datepicker:false, format:'H:m' - }); + }); $('#promotion_promo_end_hour').datetimepicker({ datepicker:false, format:'H:m' }); + $('#promotion_promo_start_hour').on('change', function(event) { + $('#promotion_promo_start_hour').val($('#promotion_promo_start_hour').val().split(":")[0]+":00"); + }); + $('#promotion_promo_end_hour').on('change', function(event) { + $('#promotion_promo_end_hour').val($('#promotion_promo_end_hour').val().split(":")[0]+":00"); + }); + var dayy = $("#promotion_promo_day").val().replace("[","").replace("]",""); + jQuery.each( dayy.split(","), function( i, d ) { + $("input.selectDay[value='"+d+"']").prop( "checked", true ); + }); var form = document.getElementById("new_promotion"); var inputs = $("input"); @@ -197,7 +213,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 +239,29 @@ $(".selectDay").click(function() { callforpromoproduct(); }, 0); }); + + // for promotion products data showing + 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 +273,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 e979ac5d..9f505e73 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