diff --git a/Gemfile b/Gemfile
index 67348308..7b2cdca5 100644
--- a/Gemfile
+++ b/Gemfile
@@ -31,6 +31,7 @@ gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
gem 'simple_form'
+gem 'nested_form'
gem 'bootstrap', '~> 4.0.0.alpha3'
gem 'tether-rails'
gem "font-awesome-rails"
diff --git a/Gemfile.lock b/Gemfile.lock
index 45b9b826..912f79ef 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -123,6 +123,7 @@ GEM
multi_json (1.12.1)
multi_xml (0.6.0)
mysql2 (0.4.6)
+ nested_form (0.3.2)
nio4r (2.1.0)
nokogiri (1.8.0)
mini_portile2 (~> 2.2.0)
@@ -269,6 +270,7 @@ DEPENDENCIES
kaminari (~> 1.0.1)
listen (~> 3.0.5)
mysql2 (>= 0.3.18, < 0.5)
+ nested_form
pg
prawn
prawn-table
diff --git a/app/controllers/settings/promotion_products_controller.rb b/app/controllers/settings/promotion_products_controller.rb
new file mode 100644
index 00000000..29248e1f
--- /dev/null
+++ b/app/controllers/settings/promotion_products_controller.rb
@@ -0,0 +1,77 @@
+class Settings::PromotionProductsController < ApplicationController
+ before_action :set_promotion, only: [:show, :edit, :update, :destroy,:new]
+ before_action :set_promotion_product, only: [:show, :edit, :update, :destroy]
+
+ # GET /promotion_products
+ # GET /promotion_products.json
+ def index
+ @promotion_products = PromotionProduct.all
+ end
+
+ # GET /promotion_products/1
+ # GET /promotion_products/1.json
+ def show
+ end
+
+ # GET /promotion_products/new
+ def new
+ @promotion_product = PromotionProduct.new
+ end
+
+ # GET /promotion_products/1/edit
+ def edit
+ end
+
+ # POST /promotion_products
+ # POST /promotion_products.json
+ def create
+ @promotion_product = PromotionProduct.new(promotion_params)
+ respond_to do |format|
+ if @promotion_product.save
+ format.html { redirect_to edit_settings_promotion_path(@promotion), notice: 'PromotionProduct was successfully created.' }
+ format.json { render :show, status: :created, location: @promotion_product }
+ else
+ format.html { render :new }
+ format.json { render json: @promotion_product.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # PATCH/PUT /promotion_products/1
+ # PATCH/PUT /promotion_products/1.json
+ def update
+ respond_to do |format|
+ if @promotion_product.update(promotion_params)
+ format.html { redirect_to edit_settings_promotion_path(@promotion), notice: 'PromotionProduct was successfully updated.' }
+ format.json { render :show, status: :ok, location: @promotion_product }
+ else
+ format.html { render :edit }
+ format.json { render json: @promotion_product.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /promotion_products/1
+ # DELETE /promotion_products/1.json
+ def destroy
+ @promotion_product.destroy
+ respond_to do |format|
+ format.html { redirect_to edit_settings_promotion_path(@promotion) , notice: 'PromotionProduct was successfully destroyed.' }
+ format.json { head :no_content }
+ end
+ end
+
+ private
+ # Use callbacks to share common setup or constraints between actions.
+ def set_promotion
+ @promotion = Promotion.find(params[:promotion_id])
+ end
+ def set_promotion_product
+ @promotion_product = PromotionProduct.find(params[:id])
+ end
+
+ # Never trust parameters from the scary internet, only allow the white list through.
+ def promotion_params
+ params.require(:promotion_product).permit(:promo_code, :promo_start_date, :promo_end_date, :promo_start_hour,:promo_end_hour ,:promo_day, :promo_type,:original_product ,:min_qty ,:created_by)
+ end
+end
diff --git a/app/controllers/settings/promotions_controller.rb b/app/controllers/settings/promotions_controller.rb
index 277cf62d..63773b01 100644
--- a/app/controllers/settings/promotions_controller.rb
+++ b/app/controllers/settings/promotions_controller.rb
@@ -71,6 +71,6 @@ class Settings::PromotionsController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through.
def promotion_params
- params.require(:promotion).permit(:promo_code, :promo_start_date, :promo_end_date, :promo_start_hour,:promo_end_hour ,:promo_day, :promo_type,:original_product ,:min_qty ,:created_by)
+ params.require(:promotion).permit(:promo_code, :promo_start_date, :promo_end_date, :promo_start_hour,:promo_end_hour ,:promo_day, :promo_type,:original_product ,:min_qty ,:created_by,:promotion_products_attributes => [:item_code, :min_qty, :net_off, :net_price, :percentage, :_destroy])
end
end
diff --git a/app/models/promotion.rb b/app/models/promotion.rb
index 9b5c4c12..b255ceec 100644
--- a/app/models/promotion.rb
+++ b/app/models/promotion.rb
@@ -1,2 +1,12 @@
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
+
+ has_many :promotion_products
+
+ accepts_nested_attributes_for :promotion_products , :allow_destroy => true
+
+ PROMO_TYPE1 = "Quantity"
+ PROMO_TYPE2 = "Net_off"
+ PROMO_TYPE3 = "Net_price"
+ PROMO_TYPE4 = "Percentage"
end
diff --git a/app/models/promotion_product.rb b/app/models/promotion_product.rb
new file mode 100644
index 00000000..c853ebdc
--- /dev/null
+++ b/app/models/promotion_product.rb
@@ -0,0 +1,3 @@
+class PromotionProduct < ApplicationRecord
+ belongs_to :promotion
+end
diff --git a/app/views/settings/promotion_products/_form.html.erb b/app/views/settings/promotion_products/_form.html.erb
new file mode 100644
index 00000000..38f894f2
--- /dev/null
+++ b/app/views/settings/promotion_products/_form.html.erb
@@ -0,0 +1,32 @@
+<%= simple_form_for([:settings,@promotion,@promotion_product]) do |f| %>
+ <%= f.error_notification %>
+
+
+
+
+ <%= f.button :submit %>
+
+<% end %>
diff --git a/app/views/settings/promotion_products/_promotion.json.jbuilder b/app/views/settings/promotion_products/_promotion.json.jbuilder
new file mode 100644
index 00000000..d9b08aef
--- /dev/null
+++ b/app/views/settings/promotion_products/_promotion.json.jbuilder
@@ -0,0 +1,2 @@
+json.extract! promotion, :id, :created_at, :updated_at
+json.url promotion_url(promotion, format: :json)
diff --git a/app/views/settings/promotion_products/edit.html.erb b/app/views/settings/promotion_products/edit.html.erb
new file mode 100644
index 00000000..b498fb74
--- /dev/null
+++ b/app/views/settings/promotion_products/edit.html.erb
@@ -0,0 +1,11 @@
+
+
+ <%= render 'form', promotion_products: @promotion_product %>
+
diff --git a/app/views/settings/promotion_products/index.html.erb b/app/views/settings/promotion_products/index.html.erb
new file mode 100644
index 00000000..eb8d8eed
--- /dev/null
+++ b/app/views/settings/promotion_products/index.html.erb
@@ -0,0 +1,47 @@
+<%= notice %>
+
+
+
+
+
+
+
+
+
+ | Promotion Code |
+ Item Code |
+ Minimum Quantity |
+ Net Off |
+ Net Price |
+ Percentage |
+ Created At |
+ |
+
+
+
+
+ <% @promotion_products.each do |pro| %>
+
+ | <%= link_to pro.promotion.promo_code, settings_promotion_promotion_path(pro.promotion) %> |
+ <%= pro.item_code %> |
+ <%= pro.min_qty %> |
+ <%= pro.net_off rescue "-" %> |
+ <%= pro.net_price rescue "-" %> |
+ <%= pro.percentage %> |
+ <%= pro.created_at.utc.getlocal.strftime("%Y-%m-%d/%I:%M %p") %> |
+ <%= link_to 'Edit', edit_settings_promotion_promotion_product_path(pro) %> |
+ <%= link_to 'Destroy', settings_promotion_promotion_product_path(pro), method: :delete, data: { confirm: 'Are you sure?' } %> |
+
+ <% end %>
+
+
+
diff --git a/app/views/settings/promotion_products/index.json.jbuilder b/app/views/settings/promotion_products/index.json.jbuilder
new file mode 100644
index 00000000..1c9c5f97
--- /dev/null
+++ b/app/views/settings/promotion_products/index.json.jbuilder
@@ -0,0 +1 @@
+json.array! @promotions, partial: 'promotions/promotion', as: :promotion
diff --git a/app/views/settings/promotion_products/new.html.erb b/app/views/settings/promotion_products/new.html.erb
new file mode 100644
index 00000000..32e700f5
--- /dev/null
+++ b/app/views/settings/promotion_products/new.html.erb
@@ -0,0 +1,15 @@
+
+
+
+ <%= render 'form', promotion_products: @promotion_product %>
+
+
diff --git a/app/views/settings/promotion_products/show.html.erb b/app/views/settings/promotion_products/show.html.erb
new file mode 100644
index 00000000..9f414b57
--- /dev/null
+++ b/app/views/settings/promotion_products/show.html.erb
@@ -0,0 +1,28 @@
+
+
+
+
+
+
+
Promotion Product
+
+
+
+ | Promotion Code | <%= @promotion_product.promo_code %> |
+ | Item Code | <%= @promotion_product.item_code %> |
+ | Min Qty | <%= @promotion_product.min_qty %> |
+ | Net off | <%= @promotion_product.net_off %> |
+ | Net Price | <%= @promotion_product.net_price %> |
+ | Percentage | <%= @promotion_product.percentage %> |
+ | <%= link_to 'Edit', edit_settings_promotion_product_path(@promotion_product) %> | <%= link_to 'Destroy', settings_promotion_product_path(@promotion_product), method: :delete, data: { confirm: 'Are you sure?' } %> |
+
+
+
+
+
diff --git a/app/views/settings/promotion_products/show.json.jbuilder b/app/views/settings/promotion_products/show.json.jbuilder
new file mode 100644
index 00000000..52507108
--- /dev/null
+++ b/app/views/settings/promotion_products/show.json.jbuilder
@@ -0,0 +1 @@
+json.partial! "promotions/promotion", promotion: @promotion
diff --git a/app/views/settings/promotions/_form.html.erb b/app/views/settings/promotions/_form.html.erb
index 98dbb7fe..dfdd40c2 100644
--- a/app/views/settings/promotions/_form.html.erb
+++ b/app/views/settings/promotions/_form.html.erb
@@ -1,4 +1,4 @@
-<%= simple_form_for([:settings,@promotion]) do |f| %>
+<%= simple_nested_form_for([:settings,@promotion]) do |f| %>
<%= f.error_notification %>
diff --git a/app/views/settings/promotions/show.html.erb b/app/views/settings/promotions/show.html.erb
index 4dd7e593..170ba4bf 100644
--- a/app/views/settings/promotions/show.html.erb
+++ b/app/views/settings/promotions/show.html.erb
@@ -1,4 +1,28 @@
-<%= notice %>
-<%= link_to 'Edit', edit_promotion_path(@promotion) %> |
-<%= link_to 'Back', promotions_path %>
+
+
+
+
+
+
Promotion
+
+
+
+
+ | Promotion code | <%= @promotion.promo_code %> |
+ | Promotion Start Date | <%= @promotion.promo_start_date %> |
+ | Promotion End Date | <%= @promotion.promo_end_date %> |
+ | Promotion Start Hour | <%= @promotion.promo_start_hour %> |
+ | Promotion Start Hour | <%= @promotion.promo_end_hour %> |
+ | <%= link_to 'Edit', edit_settings_promotion_path(@promotion) %> | <%= link_to 'Destroy', settings_promotion_path(@promotion), method: :delete, data: { confirm: 'Are you sure?' } %> |
+
+
+
+
+
+
diff --git a/config/routes.rb b/config/routes.rb
index 0b8c8115..7276e31f 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -281,7 +281,9 @@ Rails.application.routes.draw do
end
#promotion
- resources :promotions
+ resources :promotions do
+ resources :promotion_products
+ end
end