Show data in Edit

This commit is contained in:
Phyo
2017-09-06 17:01:33 +06:30
parent f56b7ab26e
commit 4ca3b708c4
4 changed files with 59 additions and 17 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -39,9 +39,10 @@
<% end %>
</div>
</div>
<br>
<div class="row checkboxes">
<%= f.hidden_field :promo_day, :value => "", :class => "form-control col-md-1" %>
<div class="col-md-2"><label class="control-label"><abbr title="required">*</abbr> Promotion Day</label></div>
<%= f.hidden_field :promo_day, :value => "", :class => "form-control" %>
<div class="col-md-1"><input class="selectDay" type="checkbox" name="Sunday" value="0" id="0"> Sun</div>
<div class="col-md-1"><input class="selectDay" type="checkbox" name="Monday" value="1" id="1">Mon</div>
<div class="col-md-1"><input class="selectDay" type="checkbox" name="Tuesday" value="2" id="2"> Tue</div>
@@ -50,7 +51,7 @@
<div class="col-md-1"><input class="selectDay" type="checkbox" name="Friday" value="5" id="5">Fri</div>
<div class="col-md-1"><input class="selectDay" type="checkbox" name="Saturday" value="6" id="6">Sat</div>
</div>
<br>
<div class="row">
<div class="col-md-6">
<%= f.input :promo_type,input_html: { class: "" },
@@ -73,11 +74,12 @@
<% code = menuiteminstance.menu_item.item_code %>
<% end %>
<% end %>
<option value="" selected>Choose Something</option>
<% arr.each do |a| %>
<% if a[1] == code %>
<option value="<%= a[1]%>" selected><%= a[0] %></option>
<% else %>
<option value="<%= a[1]%>"><%= a[0] %></option>
<option value="<%= a[1]%>"><%= a[0] %></option>
<% end %>
<% end %>
</select>
@@ -109,19 +111,23 @@
<div class="row"></div >
<%= f.fields_for :promotion_products do |pro| %>
<div class="row">
<%= 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 %>
<div class="col-md-2" style="text-align:left">
<div class="col-md-2 menu_item_choose" style="text-align:left">
<select class="form-control item_code_place1">
<option value="">Choose Something</option>
<% arr.each do |a| %>
<option value="<%= a[1 ]%>"><%= a[0] %></option>
<option value="<%= a[1 ]%>"><%= a[0] %></option>
<% end %>
</select>
</div>
<% sample = [] %>
<div class="col-md-2" style="text-align:left"><%= pro.input :item_code, :class => 'promoproduct', collection: sample,input_html: { selected: 2 }, label: false %></div>
<!-- <% sample = [] %> -->
<div class="col-md-2" style="text-align:left"><%= pro.input :item_code, :class => 'promoproduct', collection:[],input_html: { selected: 2 }, label: false %></div>
<div class="col-md-2"><%= pro.input :min_qty , label: false%></div>
<div class="col-md-2"><%= pro.input :net_off , label: false %></div>
<div class="col-md-2"><%= pro.input :net_price , label: false %></div>
@@ -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("<option value='"+item_code+"'>"+result[0]+"</option>");
$("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 += "<option value ="+result[i][1]+">"+result[i][0]+"</option>"
}
$("#"+id).append(itemlist);
$("#"+id).select2();
$("select#"+id).append(itemlist);
$("select#"+id).select2();
}
});
});
}
});
</script>

View File

@@ -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