Class: Cart

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/cart.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#free_shippingObject

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_discountObject

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_priceObject

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

#voucherObject

Returns the value of attribute voucher.



2
3
4
# File 'app/models/cart.rb', line 2

def voucher
  @voucher
end

#voucher_discountObject

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_priceObject

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_deliveryObject



141
142
143
# File 'app/models/cart.rb', line 141

def address_delivery
  AddressDelivery.find_by_id(options[:address_delivery_id])
end

#address_from_colissimoObject



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.options[: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_invoiceObject



137
138
139
# File 'app/models/cart.rb', line 137

def address_invoice
  AddressInvoice.find_by_id(options[:address_invoice_id])
end

#discountObject



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

Returns:

  • (Boolean)


46
47
48
# File 'app/models/cart.rb', line 46

def is_empty?
  total_items == 0
end

#optionsObject



149
150
151
# File 'app/models/cart.rb', line 149

def options
  read_attribute(:options)
end

#packaging_priceObject



94
95
96
97
# File 'app/models/cart.rb', line 94

def packaging_price
   #TODO get carts options from config
  return 0
end

#patronage_discountObject



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

#taxesObject



50
51
52
# File 'app/models/cart.rb', line 50

def taxes
  #total(true) - total(false)
end

#to_emptyObject

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)
  passed_options = args.extract_options!
  options = {
    :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(passed_options.symbolize_keys)

  total = 0.0

  cart_items.each do |cart_product|
    product_price = cart_product.product.price(
      :tax => options[:tax],
      :voucher_discount => options[:product_voucher_discount],
      :special_offer_discount => options[:product_special_offer_discount],
      :packaging => options[:product_packaging]
    )
    total += product_price * cart_product.quantity
  end

  total -= self.voucher_discount_price.to_f || 0.0 if options[:cart_voucher_discount]
  total -= self.special_offer_discount_price.to_f || 0.0 if options[:cart_special_offer_discount]
  total -= self.patronage_discount.to_f || 0.0 if options[:patronage]
  total += self.packaging_price.to_f || 0.0 if options[:cart_packaging]

  total = self.after_total(total, options) if self.respond_to?(:after_total)

  total = 0.0 if total < 0
  total
end

#total_itemsObject



129
130
131
# File 'app/models/cart.rb', line 129

def total_items
  return cart_items.size
end

#transporterObject



145
146
147
# File 'app/models/cart.rb', line 145

def transporter
  TransporterRule.find_by_id(options[:transporter_rule_id])
end

#transporter_idObject



153
154
155
156
# File 'app/models/cart.rb', line 153

def transporter_id
  return 0 unless options[:transporter_rule_id]
  options[: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