Jacqueline Maldonado 7 лет назад
Родитель
Сommit
ad200624af
3 измененных файлов с 6 добавлено и 5 удалено
  1. 3 2
      app/controllers/promotions_controller.rb
  2. 1 1
      app/models/product.rb
  3. 2 2
      app/models/promotion.rb

+ 3 - 2
app/controllers/promotions_controller.rb

@@ -28,9 +28,10 @@ class PromotionsController < ApplicationController
   # POST /promotions.json
   def create
     @promotion = Promotion.new(promotion_params)
+    @promotion.subcategory_id = nil if !@promotion.nil? && @promotion.subcategory_id.zero?
     source =
-      if @promotion.category_id.present? || @promotion.subcategory_id.to_i != 0
-        "a la #{@promotion.subcategory_id.to_i != 0 ? 'sublínea' : 'línea' } #{@promotion.subcategory_id.to_i != 0 ? Category.find(@promotion.subcategory_id).category : @promotion.category.category}"
+      if @promotion.category_id.present?
+        "a la #{@promotion.subcategory_id.present? ? 'sublínea' : 'línea' } #{@promotion.subcategory_id.present? ? Category.find(@promotion.subcategory_id).category : @promotion.category.category}"
       elsif @promotion.product.present?
         "al producto #{@promotion.product.name}"
       end

+ 1 - 1
app/models/product.rb

@@ -65,7 +65,7 @@ class Product < ActiveRecord::Base
   end
 
   def get_promotion
-    category_id = categories[0].parent.id
+    category_id = categories[0].parent.present? ? categories[0].parent.id : categories[0].id
     category_array = [category_id, categories.ids].flatten
     product_ids = [id, parent_id]
     promos = Promotion.where("product_id IN (?) OR category_id IN (?)", product_ids, category_array).order("percent, id DESC").vigentes.first

+ 2 - 2
app/models/promotion.rb

@@ -50,13 +50,13 @@ class Promotion < ActiveRecord::Base
     source =
       if category_id.present?
         "category_id = #{category_id}"
-      elsif subcategory_id.present?
+      elsif subcategory_id.present? && subcategory_id != "0"
         "subcategory_id = #{subcategory_id}"
       elsif product_id.present?
         "product_id = #{product_id}"
       end
     if source
-      exists_already = Promotion.where(source).where("start_date < ? and end_date > ?", end_date, start_date).any?
+      exists_already = Promotion.activas.where(source).where("start_date < ? AND end_date > ?", end_date, start_date).any?
       errors.add(:base, 'Ya existe una promoción activa con los parámetros seleccionados') if exists_already
     end
   end