Class: OrderDetail
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- OrderDetail
- Defined in:
- app/models/order_detail.rb
Class Method Summary collapse
Instance Method Summary collapse
- #discount(*args) ⇒ Object
- #increment_product_sold_counter ⇒ Object
- #old_price(with_tax = false, with_currency = true, with_special_offer = false, with_voucher = false) ⇒ Object
-
#price(*args) ⇒ Object
Returns price’s string with currency symbol.
- #rate_tax ⇒ Object
-
#tax(with_currency = true) ⇒ Object
Returns total product’s tax.
-
#total ⇒ Object
Returns price * quantity.
-
#total_tax(with_currency = true) ⇒ Object
Returns tax * quantity.
Class Method Details
.from_cart_product(cart_product) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'app/models/order_detail.rb', line 87 def self.from_cart_product(cart_product) object = self.new( :name => cart_product.product.name, :description => cart_product.product.description, :price => cart_product.product.price( :voucher_discount => false, :special_offer_discount => false ), :rate_tax => cart_product.product.rate_tax, :sku => cart_product.product.sku, :product_id => cart_product.product.id, :voucher_discount => cart_product.product.voucher_discount, :voucher_discount_price => cart_product.product.voucher_discount_price, :special_offer_discount => cart_product.product.special_offer_discount, :special_offer_discount_price => cart_product.product.special_offer_discount_price, :quantity => cart_product.quantity ) self.after_from_cart_product(object,cart_product) if self.respond_to?(:after_from_cart_product) return object end |
.from_free_product(gift) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'app/models/order_detail.rb', line 108 def self.from_free_product(gift) self.new( :name => gift.name, :description => gift.description, :price => 0, :rate_tax => 0, :sku => gift.sku, :product_id => gift.id, :special_offer_discount => I18n.t(:free_product), :special_offer_discount_price => 0 ) end |
Instance Method Details
#discount(*args) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'app/models/order_detail.rb', line 38 def discount(*args) = args. = { :voucher_discount => true, :special_offer_discount => true, }.update(.symbolize_keys) discount = 0.0 discount += self.special_offer_discount_price.to_f || 0.0 if [:special_offer_discount] discount += self.voucher_discount_price.to_f || 0.0 if [:voucher_discount] discount end |
#increment_product_sold_counter ⇒ Object
122 123 124 125 126 127 128 |
# File 'app/models/order_detail.rb', line 122 def increment_product_sold_counter if product quantity.times do product.sold_counters.new.increment_counter end end end |
#old_price(with_tax = false, with_currency = true, with_special_offer = false, with_voucher = false) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'app/models/order_detail.rb', line 51 def old_price(with_tax=false, with_currency=true, with_special_offer=false, with_voucher=false) ActiveSupport::Deprecation.warn('use price instead of old_price') price({ :tax => with_tax, :special_offer_discount => with_special_offer, :voucher_discount => with_voucher, }) end |
#price(*args) ⇒ Object
Returns price’s string with currency symbol
This method is an overload of price attribute.
Parameters
-
:with_tax
- false by defaults. Returns price with tax if true -
:with_currency
- true by defaults. The currency of user is considered if true
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/models/order_detail.rb', line 21 def price(*args) = args. = { :tax => true, :voucher_discount => true, :special_offer_discount => true, :packaging => false, }.update(.symbolize_keys) price = read_attribute(:price) || 0.0 price *= rate_tax if [:tax] price -= self.special_offer_discount_price || 0.0 if [:special_offer_discount] price -= self.voucher_discount_price || 0.0 if [:voucher_discount] price += self.packaging_price.to_f || 0.0 if [:packaging] price end |
#rate_tax ⇒ Object
9 10 11 12 |
# File 'app/models/order_detail.rb', line 9 def rate_tax value = read_attribute(:rate_tax) (not value or value < 1.0) ? 1.0 : value end |
#tax(with_currency = true) ⇒ Object
Returns total product’s tax
Parameters
-
:with_currency
- true by defaults. The currency of user is considered if true
This method use price : price(false, with_currency)
66 67 68 |
# File 'app/models/order_detail.rb', line 66 def tax(with_currency=true) price(:tax => true, :with_currency => with_currency) / rate_tax end |
#total ⇒ Object
Returns price * quantity
Parameters
-
:with_tax
- false by defaults. Returns price with tax if true -
:with_currency
- true by defaults. The currency of user is considered if true
83 84 85 |
# File 'app/models/order_detail.rb', line 83 def total price.to_f * quantity.to_f end |
#total_tax(with_currency = true) ⇒ Object
Returns tax * quantity
Parameters
-
:with_currency
- true by defaults. The currency of user is considered if true
74 75 76 |
# File 'app/models/order_detail.rb', line 74 def total_tax(with_currency=true) tax(with_currency) end |