From e9ba56826c8726d2c219a5de9e8dacfe0ba44cff Mon Sep 17 00:00:00 2001 From: Phyo Date: Mon, 21 Aug 2017 17:38:07 +0630 Subject: [PATCH 1/2] Promotion Product Add --- Gemfile | 1 + Gemfile.lock | 2 + .../settings/promotion_products_controller.rb | 77 +++++++++++++++++++ .../settings/promotions_controller.rb | 2 +- app/models/promotion_product.rb | 3 + .../promotion_products/_form.html.erb | 32 ++++++++ .../_promotion.json.jbuilder | 2 + .../settings/promotion_products/edit.html.erb | 11 +++ .../promotion_products/index.html.erb | 47 +++++++++++ .../promotion_products/index.json.jbuilder | 1 + .../settings/promotion_products/new.html.erb | 15 ++++ .../settings/promotion_products/show.html.erb | 28 +++++++ .../promotion_products/show.json.jbuilder | 1 + app/views/settings/promotions/_form.html.erb | 53 ++++++++++++- app/views/settings/promotions/show.html.erb | 30 +++++++- config/routes.rb | 4 +- 16 files changed, 302 insertions(+), 7 deletions(-) create mode 100644 app/controllers/settings/promotion_products_controller.rb create mode 100644 app/models/promotion_product.rb create mode 100644 app/views/settings/promotion_products/_form.html.erb create mode 100644 app/views/settings/promotion_products/_promotion.json.jbuilder create mode 100644 app/views/settings/promotion_products/edit.html.erb create mode 100644 app/views/settings/promotion_products/index.html.erb create mode 100644 app/views/settings/promotion_products/index.json.jbuilder create mode 100644 app/views/settings/promotion_products/new.html.erb create mode 100644 app/views/settings/promotion_products/show.html.erb create mode 100644 app/views/settings/promotion_products/show.json.jbuilder diff --git a/Gemfile b/Gemfile index fa2990ad..901262c4 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 ba8f6a0c..55a65889 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -119,6 +119,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) @@ -264,6 +265,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_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.input :item_code %>
+
+ <% if @promotion.promo_type == Promotion::PROMO_TYPE1 %> +
+
<%= f.input :min_qty %>
+
+ <% elsif @promotion.promo_type == Promotion::PROMO_TYPE2 %> +
+
<%= f.input :net_off %>
+
+ <% elsif @promotion.promo_type == Promotion::PROMO_TYPE3 %> +
+
<%= f.input :net_price %>
+
+ <% elsif @promotion.promo_type == Promotion::PROMO_TYPE4 %> +
+
<%= f.input :percentage %>
+
+ <% end %> +
+
+ +
+ <%= 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_products.each do |pro| %> + + + + + + + + + + + + <% end %> + +
Promotion CodeItem CodeMinimum QuantityNet OffNet PricePercentageCreated At
<%= 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?' } %>
+
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 %>
@@ -32,13 +32,62 @@
<%= f.input :promo_type,input_html: { class: "" }, - collection: %w{Quantity Net_off Net_price Percentage},:class => 'form-control' ,:label => "" %> + collection: %w{Quantity Net_off Net_price Percentage},:class => 'form-control' ,:label => "Promotion Type" %>
<%= f.input :original_product,collection: MenuItem.order("name desc"),input_html: { selected: 2 } %>
<%= f.input :min_qty %>
+ + +
+ + + + + + + + + + + + <% @promotion.promotion_products.each do |pp| %> + + + + + + + + + + + + + <% end %> + <%= f.fields_for :promotion_products, PromotionProduct.new do |pro| %> + + + + + + + + + + + <% end %> + +
Item CodeMin QtyNet offNet PricePercentage<%= f.link_to_add "Add Product", :promotion_products, :class => 'btn btn-primary' %>
<%= pp.item_code %><%= pp.min_qty %><%= pp.net_off %><%= pp.net_price %><%= pp.percentage %><%= link_to 'Edit', edit_settings_promotion_promotion_product_path(@promotion,pp) %><%= link_to 'Destroy', settings_promotion_promotion_product_path(@promotion,pp), method: :delete, data: { confirm: 'Are you sure?' } %>
<%= pro.input :item_code , :class => "form-control" %><%= pro.input :min_qty , :class => "form-control"%><%= pro.input :net_off , :class => "form-control" %><%= pro.input :net_price , :class => "form-control" %><%= pro.input :percentage , :class => "form-control" %><%= pro.link_to_remove "X" %>
+
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 93a7985a..e7955acb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -268,7 +268,9 @@ Rails.application.routes.draw do end #promotion - resources :promotions + resources :promotions do + resources :promotion_products + end end From 5a1c1617711c1252cdcd87eff6c18b9d489d24ff Mon Sep 17 00:00:00 2001 From: Phyo Date: Mon, 21 Aug 2017 17:46:16 +0630 Subject: [PATCH 2/2] Promotion Relation with PromotionProduct --- app/models/promotion.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) 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