158 lines
3.3 KiB
Ruby
158 lines
3.3 KiB
Ruby
class PCustomer < ActiveRecord::Base
|
|
has_many :particulars, :as => :owner, :dependent => :destroy
|
|
|
|
has_many :p_contacts, :as => :contactable
|
|
accepts_nested_attributes_for :p_contacts
|
|
|
|
has_many :p_customer_sheets
|
|
|
|
accepts_nested_attributes_for :particulars
|
|
|
|
belongs_to :particular
|
|
accepts_nested_attributes_for :particular
|
|
|
|
has_secure_password validations: false
|
|
|
|
|
|
validates_presence_of :password, :on => :create, :if => :public_validation?
|
|
|
|
validates :email, :presence => true, :uniqueness => true, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, :if => :public_validation?
|
|
|
|
|
|
|
|
belongs_to :p_price_cat
|
|
|
|
|
|
attr_accessor :valid_public
|
|
|
|
|
|
|
|
|
|
def public_validation?
|
|
true if self.valid_public
|
|
end
|
|
|
|
before_create do
|
|
self.generate_token
|
|
end
|
|
|
|
def generate_token
|
|
begin
|
|
self.token = SecureRandom.urlsafe_base64
|
|
end while PCustomer.exists?(:token => self.token)
|
|
end
|
|
|
|
|
|
|
|
before_validation do
|
|
|
|
if !self.p_price_cat
|
|
self.p_price_cat = PPriceCat.first
|
|
end
|
|
generate_mlm_token
|
|
|
|
|
|
|
|
if self.comptant
|
|
|
|
if self.acompte
|
|
errors.add(:acompte, "N'est pas compatible avec les paiements comptants")
|
|
|
|
end
|
|
|
|
if self.acompte_percent?
|
|
errors.add(:acompte_percent, "N'est pas compatible avec les paiements comptants")
|
|
|
|
end
|
|
|
|
if self.payment_delais?
|
|
errors.add(:payment_delais, "N'est pas compatible avec les paiements comptants")
|
|
|
|
end
|
|
|
|
if self.payment_fin_de_mois
|
|
errors.add(:payment_fin_de_mois, "N'est pas compatible avec les paiements comptants")
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
if self.acompte
|
|
if !self.acompte_percent?
|
|
errors.add(:acompte_percent, "Doit être remplis pour demander un acompte")
|
|
end
|
|
end
|
|
|
|
|
|
if !self.comptant
|
|
if !self.payment_delais?
|
|
errors.add(:payment_delais, "Délais de paiement nécessaire si pas de paiement comptant")
|
|
end
|
|
end
|
|
|
|
end
|
|
|
|
def self.for_search(search)
|
|
PCustomer.joins(:particular).where("code LIKE ? or particulars.organisation LIKE ? or particulars.name LIKE ? or particulars.firstname LIKE ?","%#{search}%", "%#{search}%", "%#{search}%", "%#{search}%")
|
|
end
|
|
|
|
def generate_mlm_token
|
|
if !self.mlm_token?
|
|
self.mlm_token = loop do
|
|
|
|
mlm_token = SecureRandom.hex(3).upcase
|
|
break mlm_token unless PCustomer.exists?(mlm_token: mlm_token)
|
|
end
|
|
|
|
end
|
|
end
|
|
|
|
|
|
def name
|
|
self.show_name
|
|
end
|
|
|
|
def show_name
|
|
if self.particular
|
|
n = ""
|
|
n += self.particular.organisation+" " if self.particular.organisation?
|
|
n += self.particular.name+" " if self.particular.name?
|
|
n += self.particular.firstname+" " if self.particular.firstname?
|
|
return n
|
|
end
|
|
end
|
|
|
|
after_create do
|
|
if !self.particular and self.particulars.count > 0
|
|
self.particular = self.particulars.first
|
|
self.save
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
def send_password_reset
|
|
|
|
|
|
begin
|
|
self[:reset_password_token] = SecureRandom.urlsafe_base64
|
|
end while PCustomer.exists?(:reset_password_token => self[:reset_password_token])
|
|
|
|
self.reset_password_sent_at = Time.now
|
|
|
|
|
|
|
|
|
|
self.save(:validate => false)
|
|
PCustomerMailer.password_reset(self).deliver
|
|
end
|
|
|
|
|
|
end
|