Ask A Question

Notifications

You’re not receiving notifications from this thread.

If-clause not working

Sascha M. asked in General

I've created a little tool which pulls a price from another table according to what was sold. This works perfectly fine.
However I need to include a discount logic into that. There should be 3 ways to give a discount:

  1. Enter discount manually (type in the value)
  2. Waive 1, 2 or 3 times the monthly price
  3. Discount up to 25% of the monthly price

All of the above can also waive the one-time Setup Fee (SuF).

Unfortunately the discount logic does not work and I can't find the reason why. Everything (price, setup fee etc.) gets added correctly to the record but not the discount. Also the values for discountquantity, discount_percentage, waive_suf are getting saved to the record as well. So the conditions to enter the respective if-clauses should be there.. What am I missing here? My guess is that I'm not returning the value correctly back to save it. I get no errors. The value for discount just does not get saved to the db.

Please find the explenations in the code below:

sale.rb

class Sale < ActiveRecord::Base
      ...
      before_save :discount
      ...

      def discount=(discount)

        #checks if manually typed discount is present and the discount value is not greater than the value defined in the user model (maxdiscount)
        if discount.present? && current_user.maxdiscount >= discount.to_d
          discount.to_s.gsub(",", ".") #need to transform comma into point
          discount = discount.to_d * quantity
          discount
        end

        #checks if discountquantity (1,2,3 months for free) is present
        if discountquantity.present? #discountquantity is the value (1,2,3) months for free
          discount = discountquantity * price
          discount
        end

        #checks if discount is 10%
        if discount_percentage == 10 #discount_percentage can be selected from the user in a dropdown
          discount = price * quantity * 0.90
          discount
        end

        #checks if discount is 25%
        if discount_percentage == 25
          discount = price * quantity * 0.75
          discount
        end

        #checks if SuF should be waived
        if waive_suf == true #can be selected from the user

            #if available the Setup fee gets pulled from another table
          if Warehouse.where(:product => self.product).where(:brand => self.order.brand).pluck(:suf).present? 
            suf = Warehouse.where(:product => self.product).where(:brand => self.order.brand).pluck(:suf).sum
            suf = suf.to_d * quantity
            suf
          end

          if setup.present? #can be entered manually
            setup = setup.gsub(",", ".")
            setup = setup.to_d * quantity
            setup
          end

          setup_fee = suf + setup #sums that up

        end

        self[:discount] = discount + setup_fee

      end #def discount=(discount)

    end

Sales Schema (simplified)

t.integer  "quantity",            default: 1
t.text     "comment"
t.decimal  "discount"
t.decimal  "price"
t.decimal  "setup"
t.integer  "discountquantity"
t.boolean  "waive_suf",           default: false
t.integer  "discount_percentage"

Many many thanks in advance!

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.