Class: Order
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Order
- Includes:
- AASM
- Defined in:
- app/models/order.rb
Constant Summary collapse
- PAYPAL_CERT_PEM =
File.exist?("#{Rails.root}/certs/paypal_cert.pem") ? File.read("#{Rails.root}/certs/paypal_cert.pem") : ''
- APP_CERT_PEM =
File.exist?("#{Rails.root}/certs/app_cert.pem") ? File.read("#{Rails.root}/certs/app_cert.pem") : ''
- APP_KEY_PEM =
File.exist?("#{Rails.root}/certs/app_key.pem") ? File.read("#{Rails.root}/certs/app_key.pem") : ''
Class Method Summary collapse
Instance Method Summary collapse
- #aasm_current_state_with_event_firing=(state) ⇒ Object
- #cmc_cic_encrypted ⇒ Object
- #cyberplus_encrypted ⇒ Object
- #discount ⇒ Object
- #elysnet_encrypted ⇒ Object
- #encrypt_for_paypal(values) ⇒ Object
- #packaging_price ⇒ Object
- #patronage_discount ⇒ Object
-
#paypal_encrypted ⇒ Object
GENERATE ENCRYPTED FORM FOR CHECKOUT.
- #product_names ⇒ Object
- #special_offer_discount_products ⇒ Object
-
#taxes ⇒ Object
def total(with_tax=false, with_currency=true,with_shipping=true,with_special_offer=true, with_voucher=true, with_patronage=true) amount = 0 .each do |order_detail| price = order_detail.price(with_tax, with_currency,with_special_offer,with_voucher) amount = price if price end amount = order_shipping.price if with_shipping && order_shipping && order_shipping.price amount -= self.special_offer_discount if with_special_offer && self.special_offer_discount amount -= self.voucher_discount if with_voucher && self.voucher_discount amount -= self.patronage_discount if with_patronage return (“%01.2f” % amount).to_f end.
- #to_colissimo_params ⇒ Object
-
#total(options = {}) ⇒ Object
Returns order’s amount.
- #total_items ⇒ Object
- #update_attributes_from_colissimo(params) ⇒ Object
- #valid_for_payment? ⇒ Boolean
- #valid_shipment? ⇒ Boolean
- #voucher_discount_products ⇒ Object
- #weight ⇒ Object
Class Method Details
.from_cart(cart) ⇒ Object
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'app/models/order.rb', line 310 def self.from_cart(cart) if cart and cart.user_id order_details_attributes = cart.cart_items.collect do |cart_product| OrderDetail.from_cart_product(cart_product).attributes end + Product.find_all_by_id(cart.[:free_product_ids]).collect do |product| OrderDetail.from_free_product(product).attributes end order = self.find_or_initialize_by_reference(cart.id) order.order_details.destroy_all order.attributes = { :user_id => cart.user_id, :voucher_discount => cart.voucher_discount_price, :special_offer_discount => cart.special_offer_discount_price, :order_details_attributes => order_details_attributes, :reference => cart.id } order.build_address_invoice(cart.address_invoice.attributes.update(:person_id => nil)) order.build_order_shipping(OrderShipping.from_cart(cart).attributes) if cart.[:colissimo].nil? order.build_address_delivery(cart.address_delivery.attributes.update(:person_id => nil)) else order.build_address_delivery(cart.address_from_colissimo.attributes) end self.after_from_cart(order,cart) if self.respond_to?(:after_from_cart) return order end end |
Instance Method Details
#aasm_current_state_with_event_firing=(state) ⇒ Object
66 67 68 69 70 71 |
# File 'app/models/order.rb', line 66 def aasm_current_state_with_event_firing=(state) aasm_events_for_current_state.each do |event_name| event = self.class.aasm_events[event_name] aasm_fire_event(event_name,false) if event && event.all_transitions.any?{ |t| t.to == state || t.to == state.to_sym } end end |
#cmc_cic_encrypted ⇒ Object
155 156 157 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 190 191 192 |
# File 'app/models/order.rb', line 155 def cmc_cic_encrypted cmc_cic = Setting.currency.payment_method_settings_with_env(:cmc_cic) sReference = "#{rand(1000)}A#{reference}" # Reference: unique, alphaNum (A-Z a-z 0-9), 12 characters max sMontant = '%.2f' % total # Amount : format "xxxxx.yy" (no spaces) sDevise = "EUR" # Currency : ISO 4217 compliant sTexteLibre = "" sDate = DateTime.now().strftime("%d/%m/%Y:%H:%M:%S") # transaction date : format dd/mm/YYYY:HH:mm:ss sLangue = "FR" # Language of the company code sUrlOk = CMCIC_URLOK sUrlKo = CMCIC_URLKO sUrlReturn = CMCIC_URL_RETURN sEmail = user.email # customer email sOptions = "" oTpe = CMCIC_Tpe.new(sLangue) oMac = CMCIC_Hmac.new(oTpe) sNbrEch = sDateEcheance1 = sMontantEcheance1 = sDateEcheance2 = sMontantEcheance2 = sDateEcheance3 = sMontantEcheance3 = sDateEcheance4 = sMontantEcheance4 = nil sChaineDebug = "V1.04.sha1.rb--[CtlHmac" + oTpe.sVersion + oTpe.sNumero + "]-" + oMac.computeHMACSHA1("CtlHmac" + oTpe.sVersion + oTpe.sNumero) # Control String for support sChaineMAC = [oTpe.sNumero, sDate, "#{sMontant}#{sDevise}", sReference, sTexteLibre, oTpe.sVersion, sLangue, oTpe.sCodeSociete, sEmail, sNbrEch, sDateEcheance1, sMontantEcheance1, sDateEcheance2, sMontantEcheance2, sDateEcheance3, sMontantEcheance3, sDateEcheance4, sMontantEcheance4, sOptions].join("*") # Data to certify payment = { :version => oTpe.sVersion, :TPE => oTpe.sNumero, :date => sDate, :montant => "#{sMontant}#{sDevise}", :reference => sReference, :MAC => oMac.computeHMACSHA1(sChaineMAC), :url_retour => sUrlReturn, :url_retour_ok => sUrlOk, :url_retour_err => sUrlKo, :lgue => sLangue, :societe => oTpe.sCodeSociete, :debug => sChaineDebug, :mail => sEmail, :bouton => oTpe.sNumero, :url_payment => oTpe.sUrlPaiement } return payment end |
#cyberplus_encrypted ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'app/models/order.rb', line 114 def cyberplus_encrypted ts = Time.now cyberplus = Setting.current.payment_method_settings_with_env(:cyberplus) payment_config = if cyberplus[:payment_config] =~ /^MULTI:.*count=(\d+)/i and self.payment_plans count = $1.to_i cyberplus[:payment_config].sub('#PRICE',"#{(total*100).to_i / count}") else 'SINGLE' end payment = { :version => cyberplus[:version], :site_id => cyberplus[:site_id], :ctx_mode => Setting.current.payment_method_for_test?(:cyberplus) ? 'TEST' : 'PRODUCTION', :trans_id => ts.strftime('%H%M%S'), :trans_date => ts.strftime('%Y%m%d%H%M%S'), :validation_mode => '', :capture_delay => '', :payment_config => payment_config, :payment_cards => cyberplus[:payment_cards], :amount => (total*100).to_i, :currency => cyberplus[:currency], :key => cyberplus[:key], :url_cancel => cyberplus[:url_ko], :url_success => cyberplus[:url_ok], :url_refused => cyberplus[:url_ko], :url_return => cyberplus[:url_return], :url_referral => cyberplus[:url_referral], :order_id => id } sign = [:version, :site_id, :ctx_mode, :trans_id, :trans_date, :validation_mode, :capture_delay, :payment_config, :payment_cards, :amount, :currency, :key].map{ |key| payment[key] } payment[:signature] = SHA1.new(sign.join('+')) return payment end |
#discount ⇒ Object
244 245 246 |
# File 'app/models/order.rb', line 244 def discount self.total(:cart_voucher_discount => false, :cart_special_offer_discount => false, :product_voucher_discount => false) - self.total end |
#elysnet_encrypted ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'app/models/order.rb', line 194 def elysnet_encrypted setting = Setting.first elysnet = Setting.current.payment_settings_with_env(:elysnet) parm = "merchant_id=#{elysnet[:merchant_id]}" parm += " merchant_country=fr" parm += " amount=#{(total*100).to_i}" parm += " currency_code=978" parm += " pathfile=" + $pathfile parm += " normal_return_url=#{elysnet[:url_ok]}" parm += " cancel_return_url=#{elysnet[:url_ko]}" parm += " automatic_response_url=#{elysnet[:auto_response]}" parm += " language=fr" parm += " payment_means=CB,1,VISA,1,MASTERCARD,1" parm += " header_flag=no" parm += " customer_email=#{self.user.email}" parm += " order_id=#{self.id}" result = `./lib/elysnet/bin/request #{parm}` #execution of request script tab = result.split("!") payment = tab[3] return payment end |
#encrypt_for_paypal(values) ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'app/models/order.rb', line 104 def encrypt_for_paypal(values) begin signed = OpenSSL::PKCS7::sign(OpenSSL::X509::Certificate.new(APP_CERT_PEM), OpenSSL::PKey::RSA.new(APP_KEY_PEM, ''), values.map { |k, v| "#{k}=#{v}" }.join("\n"), [], OpenSSL::PKCS7::BINARY) OpenSSL::PKCS7::encrypt([OpenSSL::X509::Certificate.new(PAYPAL_CERT_PEM)], signed.to_der, OpenSSL::Cipher::Cipher::new("DES3"), OpenSSL::PKCS7::BINARY).to_s.gsub("\n", "") rescue '' end end |
#packaging_price ⇒ Object
352 353 354 355 |
# File 'app/models/order.rb', line 352 def packaging_price #TODO get config from cart config 0 end |
#patronage_discount ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'app/models/order.rb', line 290 def patronage_discount return read_attribute(:patronage_discount_price) if self.patronage_discount_price return 0 unless self.user if self.user.has_nephew_discount? if Setting.first.nephew_discount_percent? self.user.patronage_discount * total(false,false,true,false,false,false) / 100 else self.user.patronage_discount end elsif self.user.has_godfather_discount? if Setting.first.nephew_discount_percent? self.user.patronage_discount * total(false,false,true,false,false,false) / 100 else self.user.patronage_discount end else 0 end end |
#paypal_encrypted ⇒ Object
GENERATE ENCRYPTED FORM FOR CHECKOUT
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/models/order.rb', line 80 def paypal_encrypted paypal = Setting.current.payment_method_settings_with_env(:paypal) values = { :business => paypal[:email], :cmd => '_cart', :upload => 1, :return => paypal[:url_ok], :invoice => id, :notify_url => paypal[:auto_response], :cert_id => paypal[:cert_id], :currency_code => paypal[:currency], :handling_cart => self.order_shipping.price.nil? ? 0 : self.order_shipping.price.round(2) } order_details.all( :group => 'product_id').each_with_index do |item, index| values.merge!({ "amount_#{index+1}" => (item.price).round(2), "item_name_#{index+1}" => item.product.name, "item_number_#{index+1}" => item.id, "quantity_#{index+1}" => item.quantity }) end encrypt_for_paypal(values) end |
#product_names ⇒ Object
269 270 271 272 273 274 275 276 |
# File 'app/models/order.rb', line 269 def product_names count = self.order_details.count if count == 1 return self.order_details.first.name else return "#{count} #{I18n.t('product', :count => 2)}" end end |
#special_offer_discount_products ⇒ Object
282 283 284 |
# File 'app/models/order.rb', line 282 def special_offer_discount_products return order_details.all(:conditions => ['special_offer_discount_price IS NOT NULL']) end |
#taxes ⇒ Object
def total(with_tax=false, with_currency=true,with_shipping=true,with_special_offer=true, with_voucher=true, with_patronage=true)
amount = 0
.each do |order_detail|
price = order_detail.price(with_tax, with_currency,with_special_offer,with_voucher)
amount += price if price
end
amount += order_shipping.price if with_shipping && order_shipping && order_shipping.price
amount -= self.special_offer_discount if with_special_offer && self.special_offer_discount
amount -= self.voucher_discount if with_voucher && self.voucher_discount
amount -= self.patronage_discount if with_patronage
return ("%01.2f" % amount).to_f
end
261 262 263 |
# File 'app/models/order.rb', line 261 def taxes ("%01.2f" % (total - total(:tax => false))).to_f end |
#to_colissimo_params ⇒ Object
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'app/models/order.rb', line 358 def to_colissimo_params setting = Setting.first colissimo = setting.colissimo_methods require "digest/sha1" order_id = "#{rand(1000)}m#{self.reference}" cart = Cart.find_by_id(self.reference) if transporter = TransporterRule.find_by_id(cart.[:transporter_rule_id]) price = transporter.variables else price = colissimo[:forwarding_charges] end signature_tmp = "#{colissimo[:fo]}#{self.user.lastname.to_s.parameterize(' ').upcase}#{colissimo[:preparation_time]}#{price}#{self.user_id}#{self.reference}#{order_id}#{colissimo[:sha]}" signature = Digest::SHA1.hexdigest(signature_tmp) unless user.civility.nil? civ = I18n.t("civility.label.#{self.user.civility}").upcase else civ = 'MR' end infos = { :ceAdress3 => cart.address_delivery.address.to_s.parameterize(' ').upcase, :ceAdress4 => cart.address_delivery.address_2.to_s.parameterize(' ').upcase, :ceZipCode => cart.address_delivery.zip_code, :ceTown => cart.address_delivery.city.to_s.parameterize(' ').upcase, #:cePhoneNumber => self.address_delivery.phone, :ceCivility => civ, :ceName => self.user.lastname.to_s.parameterize(' ').upcase, :ceFirstName => self.user.firstname.to_s.parameterize(' ').upcase, :ceEmail => self.user.email, :trClientNumber => self.user_id, :dyForwardingCharges => price, :dyPreparationTime => colissimo[:preparation_time], :trOrderNumber => self.reference, :orderId => order_id, :signature => signature, :pudoFOId => colissimo[:fo] } infos end |
#total(options = {}) ⇒ Object
Returns order’s amount
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'app/models/order.rb', line 218 def total( = {}) = {: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, :with_shipping => true}.update(.symbolize_keys) total = 0.0 order_details.each do |order_detail| product_price = order_detail.price({:tax => [:tax], :voucher_discount => [:product_voucher_discount], :special_offer_discount => [:product_special_offer_discount], :packaging => [:product_packaging] }) total += product_price * order_detail.quantity end total += order_shipping.price if [:with_shipping] && order_shipping && order_shipping.price total -= self.voucher_discount.to_f || 0.0 if [:cart_voucher_discount] && self.voucher_discount total -= self.special_offer_discount.to_f || 0.0 if [:cart_special_offer_discount] && self.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 return total end |
#total_items ⇒ Object
265 266 267 |
# File 'app/models/order.rb', line 265 def total_items return order_details.length end |
#update_attributes_from_colissimo(params) ⇒ Object
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
# File 'app/models/order.rb', line 401 def update_attributes_from_colissimo(params) case params[:DELIVERYMODE] when 'DOM', 'RDV', 'DOS' self.update_attributes( :order_shipping_attributes => { :name => 'So Colissimo', :price => params[:DYFORWARDINGCHARGES], :colissimo_type => params[:DELIVERYMODE]}, :address_delivery_attributes =>{ :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 self.update_attributes( :order_shipping_attributes => { :name => 'So Colissimo', :price => params[:DYFORWARDINGCHARGES], :colissimo_type => params[:DELIVERYMODE]}, :address_delivery_attributes => { :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 |
#valid_for_payment? ⇒ Boolean
348 349 350 |
# File 'app/models/order.rb', line 348 def valid_for_payment? valid_shipment? end |
#valid_shipment? ⇒ Boolean
343 344 345 346 |
# File 'app/models/order.rb', line 343 def valid_shipment? # AddressInvoice and AddressDelivery is obligatory for valid an order (address_invoice and address_invoice.valid?) and (address_delivery and address_delivery.valid?) end |
#voucher_discount_products ⇒ Object
286 287 288 |
# File 'app/models/order.rb', line 286 def voucher_discount_products return order_details.all(:conditions => ['voucher_discount_price IS NOT NULL']) end |
#weight ⇒ Object
278 279 280 |
# File 'app/models/order.rb', line 278 def weight order_details.map(&:weight).sum end |