class ProductCustomer < ActiveRecord::Base acts_as_tree has_secure_password validates :email, :presence => true, :uniqueness => true, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i validates_presence_of :password, :on => :create has_many :product_orders has_many :commissions has_many :account_ribs belongs_to :particular_bill, :class_name => "Particular" accepts_nested_attributes_for :particular_bill belongs_to :particular_send, :class_name => "Particular" accepts_nested_attributes_for :particular_send before_create do self.generate_token self.generate_mlm_token self.particular_bill = Particular.create(:particular_type => "facturation", :skip_validation => true) self.particular_idem = true self.particular_send = Particular.create(:particular_type => "expedition", :skip_validation => true) end def pseudo self.particular_bill.firstname.to_s+" "+self.particular_bill.name.to_s end def send_password_reset begin self[:reset_password_token] = SecureRandom.urlsafe_base64 end while ProductCustomer.exists?(:reset_password_token => self[:reset_password_token]) self.reset_password_sent_at = Time.now self.save(:validate => false) ProductCustomerMailer.password_reset(self).deliver end def generate_token begin self.token = SecureRandom.urlsafe_base64 puts self.token end while ProductCustomer.exists?(:token => self.token) end def generate_mlm_token self.mlm_token = loop do mlm_token = SecureRandom.hex(3).upcase break mlm_token unless ProductCustomer.exists?(mlm_token: mlm_token) end end def self.find_parrain(token) if token.to_s != "" ProductCustomer.find_by_mlm_token(token.upcase) else nil end end before_validation do if !self.id if self.parent_code? if mlm_parent = ProductCustomer.find_parrain(self.parent_code) self.parent_id = mlm_parent.id self.parent_at_create = true else errors.add(:parent_code, "Ce code de recommandation n'est pas valide, merci de vérifier son exactitude. ") end end end end def mlm_detail_ids i = 0 parents_id = [self.id] mlm_children_ids = [] mlm_children_id_by_levels = [] while i != 14 do i += 1 parents_id = ProductCustomer.where(:parent_id => parents_id).select(:id).map{ |v| v.id} mlm_children_id_by_levels[i] = parents_id mlm_children_ids.concat parents_id end { :mlm_children_id_by_levels => mlm_children_id_by_levels, :mlm_children_ids => mlm_children_ids } end def mlm_children_id_by_levels self.mlm_detail_ids[:mlm_children_id_by_levels] end def mlm_children_ids self.mlm_detail_ids[:mlm_children_ids] end def solde_commissions(devise_id=1) self.commissions.where(:devise_id => devise_id).sum(:amount) end def max_virement(devise_id=1) self.solde_commissions(1).floor - 5.0 end def mlm_small_percents [0,10,6,4,0,0,0,0,0,0,0,0] end def callif_now {:percents => self.mlm_small_percents} end def mlm_nbr_niveaux if self.callif_now 3 else 0 end end COUNTRY = [ "Suisse", "France", "Allemagne", "Autriche", "Belgique", "Espagne", "Estonie", "Finlande", "Grèce", "Irlande", "Italie", "Luxembourg", "Malte", "Pays-Bas", "Portugal", "Slovaquie", "Slovénie", "Croatie", "Bulgarie", "Danemark", "Hongrie", "Lettonie", "Lituanie", "Pologne", "République Tchèque", "Roumanie", "Royaume Uni", "Suède", "Islande", "Norvège", "Liechtenstein", "Monaco", "Guadeloupe", "Guyane", "Martinique", "La Réunion", "Mayotte", "Polynésie française", "Saint-Barthélemy", #"Saint-Martin", #"Saint-Pierre-et-Miquelon", "Wallis-et-Futuna", "Nouvelle-Calédonie" ] end