Topic: Calculating Money Spent

Hello,

I'm trying to keep track of dollars that employees spend on clothes, and I'm having a hard time get my discount to work.  Basically, each employee gets $150 to buy clothes a year at full catalog price.  Once they spend their $150, the money comes out of their paycheck, but they get a 40% discount off the catalog price.

This is what I have, but after the person gets into the negative(spending their own money), my calculation doesn't work correctly. 

        @order.order_line_items = @items
	@total =  @order.order_line_items.inject(0) do |sum, element| sum + ((element.quantity * element.unit_price))
	end
	if (@person.balance.to_i - @total.to_i) > 0
	    @person.balance = (@person.balance.to_i - @total.to_i)
	else (@person.ending_balance.to_i - @total.to_i) < 0
            @person.balance = @person.balance.to_i - (((@total.to_i - @person.balance.to_i) * 0.60) + @person.balance.to_i)	
	end

Any help you can provide will be greatly appreciated.

Thanks,
Zack

Last edited by zreed20 (2007-01-22 12:07:07)

But they that wait upon the Lord shall renew their strength

Re: Calculating Money Spent

Nevermind, I think this did it. But, I'd really like to DRY this code, if you have any input, please help.

	@sub_total =  @order.order_line_items.inject(0) do |sum, element| sum + ((element.quantity * element.unit_price))
	end
	if (@person.balance.to_i - @sub_total.to_i) > 0
	    @person.balance = (@person.balance.to_i - @sub_total.to_i)
	elsif ((@person.balance.to_i - @sub_total.to_i) < 0) and (@person.balance.to_i > 0)
        @person.balance = @person.balance.to_i - (((@sub_total.to_i - @person.balance.to_i) * 0.60) + @person.balance.to_i)	
	else ((@person.balance.to_i - @sub_total.to_i) < 0) and (@person.balance.to_i < 0)
		@person.balance = @person.balance.to_i - ((@sub_total.to_i) * 0.60)	
	end
But they that wait upon the Lord shall renew their strength