Class: Cart
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Cart
- Defined in:
- app/models/cart.rb
Instance Attribute Summary collapse
-
#free_shipping ⇒ Object
Returns the value of attribute free_shipping.
-
#special_offer_discount ⇒ Object
Returns the value of attribute special_offer_discount.
-
#special_offer_discount_price ⇒ Object
Returns the value of attribute special_offer_discount_price.
-
#voucher ⇒ Object
Returns the value of attribute voucher.
-
#voucher_discount ⇒ Object
Returns the value of attribute voucher_discount.
-
#voucher_discount_price ⇒ Object
Returns the value of attribute voucher_discount_price.
Instance Method Summary collapse
- #add_product(product, quantity = nil) ⇒ Object
- #add_product_id(product_id, quantity = 1) ⇒ Object
- #address_delivery ⇒ Object
- #address_from_colissimo ⇒ Object
- #address_invoice ⇒ Object
- #discount ⇒ Object
- #discount_cart(discount, percent = nil) ⇒ Object
-
#is_empty? ⇒ Boolean
Returns true if cart is empty, returns false else.
- #options ⇒ Object
- #packaging_price ⇒ Object
- #patronage_discount ⇒ Object
- #remove_product(product, quantity = nil) ⇒ Object
- #remove_product_id(product_id, quantity = 1) ⇒ Object
- #taxes ⇒ Object
-
#to_empty ⇒ Object
Empty this cart.
- #total(*args) ⇒ Object
- #total_items ⇒ Object
- #transporter ⇒ Object
- #transporter_id ⇒ Object
-
#weight(product = nil) ⇒ Object
Returns weight of this cart.
Instance Attribute Details
#free_shipping ⇒ Object
Returns the value of attribute free_shipping.
4 5 6 |
# File 'app/models/cart.rb', line 4 def free_shipping @free_shipping end |
#special_offer_discount ⇒ Object
Returns the value of attribute special_offer_discount.
3 4 5 |
# File 'app/models/cart.rb', line 3 def special_offer_discount @special_offer_discount end |
#special_offer_discount_price ⇒ Object
Returns the value of attribute special_offer_discount_price.
3 4 5 |
# File 'app/models/cart.rb', line 3 def special_offer_discount_price @special_offer_discount_price end |
#voucher ⇒ Object
Returns the value of attribute voucher.
2 3 4 |
# File 'app/models/cart.rb', line 2 def voucher @voucher end |
#voucher_discount ⇒ Object
Returns the value of attribute voucher_discount.
2 3 4 |
# File 'app/models/cart.rb', line 2 def voucher_discount @voucher_discount end |
#voucher_discount_price ⇒ Object
Returns the value of attribute voucher_discount_price.
2 3 4 |
# File 'app/models/cart.rb', line 2 def voucher_discount_price @voucher_discount_price end |
Instance Method Details
#add_product(product, quantity = nil) ⇒ Object
13 14 15 16 |
# File 'app/models/cart.rb', line 13 def add_product(product,quantity=nil) return false if product.nil? || product.new_record? add_product_id(product.id,quantity) end |
#add_product_id(product_id, quantity = 1) ⇒ Object
18 19 20 21 22 23 24 |
# File 'app/models/cart.rb', line 18 def add_product_id(product_id,quantity=1) if cart_item = cart_items.find_by_product_id(product_id) cart_item.increment(:quantity, quantity) else CartItem.create(:product_id => product_id, :quantity => quantity, :cart_id => self.id) end end |
#address_delivery ⇒ Object
141 142 143 |
# File 'app/models/cart.rb', line 141 def address_delivery AddressDelivery.find_by_id([:address_delivery_id]) end |
#address_from_colissimo ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'app/models/cart.rb', line 158 def address_from_colissimo if params = self.[:colissimo] case params[:DELIVERYMODE] when 'DOM', 'RDV', 'DOS' AddressDelivery.create( :designation => 'So colissimo', :civility => params['CECIVILITY'], :name => params['CENAME'], :firstname => params['CEFIRSTNAME'], :city => params['CETOWN'], :zip_code => params['CEZIPCODE'], :address => params['CEADRESS3'], :address_2 => params['CEADRESS4'], :country_id => Country.find_by_name('FRANCE').id, :form_attributes => { :other_phone => params['CEPHONENUMBER'], :phone => params['CEPHONENUMBER'],:email => params['CEEMAIL'] } ) else AddressDelivery.create( :designation => 'So colissimo', :civility => params['CECIVILITY'], :name => params['PRNAME'], :firstname => params['CEFIRSTNAME'], :city => params['PRTOWN'], :zip_code => params['PRZIPCODE'], :address => params['PRADRESS1'], :address_2 => params['PRADRESS2'], :country_id => Country.find_by_name('FRANCE').id, :form_attributes => { :colisssimo_point_id => params['PRID'], :other_phone => params['CEPHONENUMBER'], :phone => params['CEPHONENUMBER'], :email => params['CEEMAIL'] } ) end end end |
#address_invoice ⇒ Object
137 138 139 |
# File 'app/models/cart.rb', line 137 def address_invoice AddressInvoice.find_by_id([:address_invoice_id]) end |
#discount ⇒ Object
54 55 56 |
# File 'app/models/cart.rb', line 54 def discount total(:cart_voucher_discount => false, :cart_special_offer_discount => false, :product_voucher_discount => false) - total end |
#discount_cart(discount, percent = nil) ⇒ Object
133 134 135 |
# File 'app/models/cart.rb', line 133 def discount_cart(discount, percent=nil) percent.nil? ? self.update_attributes(:discount => discount) : self.update_attributes(:discount => discount, :percent => 1) end |
#is_empty? ⇒ Boolean
Returns true if cart is empty, returns false else
46 47 48 |
# File 'app/models/cart.rb', line 46 def is_empty? total_items == 0 end |
#options ⇒ Object
149 150 151 |
# File 'app/models/cart.rb', line 149 def read_attribute(:options) end |
#packaging_price ⇒ Object
94 95 96 97 |
# File 'app/models/cart.rb', line 94 def packaging_price #TODO get carts options from config return 0 end |
#patronage_discount ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'app/models/cart.rb', line 99 def patronage_discount return 0 unless self.user if self.user.has_nephew_discount? if Setting.first.nephew_discount_percent? self.user.patronage_discount * total(:packaging => false, :tax => false) / 100.0 else self.user.patronage_discount end elsif self.user.has_godfather_discount? if Setting.first.nephew_discount_percent? self.user.patronage_discount * total(:packaging => false, :tax => false) / 100.0 else self.user.patronage_discount end else 0 end end |
#remove_product(product, quantity = nil) ⇒ Object
26 27 28 |
# File 'app/models/cart.rb', line 26 def remove_product(product,quantity=nil) remove_product_id(product.id,quantity) end |
#remove_product_id(product_id, quantity = 1) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'app/models/cart.rb', line 30 def remove_product_id(product_id, quantity = 1) if cart_item = cart_items.find_all_by_product_id(product_id) cart_item.decrement(:quantity, quantity) cart_item.destroy if cart_item.quantity.zero? else false end end |
#taxes ⇒ Object
50 51 52 |
# File 'app/models/cart.rb', line 50 def taxes #total(true) - total(false) end |
#to_empty ⇒ Object
Empty this cart
41 42 43 |
# File 'app/models/cart.rb', line 41 def to_empty cart_items.destroy_all end |
#total(*args) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'app/models/cart.rb', line 58 def total(*args) = args. = { :tax => true, :cart_voucher_discount => true, :cart_special_offer_discount => true, :product_voucher_discount => true, :product_special_offer_discount => true, :patronage => true, :cart_packaging => true, :product_packaging=> true }.update(.symbolize_keys) total = 0.0 cart_items.each do |cart_product| product_price = cart_product.product.price( :tax => [:tax], :voucher_discount => [:product_voucher_discount], :special_offer_discount => [:product_special_offer_discount], :packaging => [:product_packaging] ) total += product_price * cart_product.quantity end total -= self.voucher_discount_price.to_f || 0.0 if [:cart_voucher_discount] total -= self.special_offer_discount_price.to_f || 0.0 if [:cart_special_offer_discount] total -= self.patronage_discount.to_f || 0.0 if [:patronage] total += self.packaging_price.to_f || 0.0 if [:cart_packaging] total = self.after_total(total, ) if self.respond_to?(:after_total) total = 0.0 if total < 0 total end |
#total_items ⇒ Object
129 130 131 |
# File 'app/models/cart.rb', line 129 def total_items return cart_items.size end |
#transporter ⇒ Object
145 146 147 |
# File 'app/models/cart.rb', line 145 def transporter TransporterRule.find_by_id([:transporter_rule_id]) end |
#transporter_id ⇒ Object
153 154 155 156 |
# File 'app/models/cart.rb', line 153 def transporter_id return 0 unless [:transporter_rule_id] [:transporter_rule_id].to_i end |
#weight(product = nil) ⇒ Object
Returns weight of this cart
119 120 121 122 123 124 125 126 127 |
# File 'app/models/cart.rb', line 119 def weight(product=nil) if product.nil? cart_items.map(&:weight).sum elsif cart_item = cart_items.find_by_product_id(product.id) cart_item.weight else 0 end end |