class MOdrRep < ApplicationRecord
  belongs_to :m_odr
  
  include ActionView::Helpers::NumberHelper
  include ActionView::Helpers::TranslationHelper
  include ActionView::Helpers::UrlHelper
  
  
  has_many :particulars, :dependent => :destroy
  accepts_nested_attributes_for :particulars, :allow_destroy => true
  
  has_many :mail_hists
  
  has_many :m_odr_virements
  
  has_many :m_odr_rep_ribs, :dependent => :destroy
  accepts_nested_attributes_for :m_odr_rep_ribs, :allow_destroy => true
  
  belongs_to :m_odr_product
  
  belongs_to :m_odr_place
  
  belongs_to :particular
  
  has_many :m_odr_files, :dependent => :destroy
  accepts_nested_attributes_for :m_odr_files, :allow_destroy => true
  
  validates :m_odr_product_cat_id, :presence => true, :if => :product_cat_needed?
  
  validates :qte, :presence => true
  validates :m_odr_product_id, :presence => true
  validates :m_odr_place_id, :presence => true
  
  validates :rgpd, :presence => true, :if => :rgpd_needed?
  
  validates :reduc_code, :presence => true, length: { in: 6..6 }, :if => :reduc_code_needed?
  
  belongs_to :m_odr_tracker
  
  belongs_to :m_odr_product_cat
  

  def reduc_code_needed?
    if self.m_odr.reduc_code_needed
      true
    else
      false
    end
  end
  
  def product_cat_needed?
    if self.m_odr.m_odr_product_cats.count > 0
      true
    else
      false
    end
  end
  
  def rgpd_needed?
    if self.m_odr and self.m_odr.confirm_case_needed
      true
      
    else
      false
    end
    
  end
    
  
  
  def send_mail_type(slug, element = nil)

    if self.m_odr and mail_type = self.m_odr.mail_types.where(:slug => slug).first and self.particulars.first and self.particulars.first.email?
      mail_hist = MailHist.generate_mail(:fr, mail_type, self.particulars.first.email, {:arguments => {:nom_odr => self.m_odr.name, :remise => number_to_currency(self.remise.to_f), :civilite => self.particular_civilite, :nom => self.particular_name, :prenom => self.particular_firstname  }, :m_odr => self.m_odr, :m_odr_rep => self, :element => element})
    end
  end
  
  
  QI_DYNAMICS = %w(admin_ok_to_send rib_admin_ok
file_admin_ok remise particular_civilite particular_name particular_firstname particular_address_2 particular_address_3 particular_cp particular_city particular_country particular_email particular_tel product_name product_brand tracker_name place_name)

  eval(QI_DYNAMICS_CORE)
  
  
  
  def ca_rib_admin_ok
    if self.m_odr_rep_ribs.where(:admin_ok => true).count > 0
      true
    else
      false
    end
  end
  
  def ca_file_admin_ok
    if self.m_odr_files.where(:admin_ok => true).count > 0
      true
    else
      false
    end
  end
    
  
  
  def change_state(state)
    self.state = state
    self.save
  end
  
  before_validation do
    
    if self.m_odr.buy_date_needed
      if !self.buy_at
        errors.add(:buy_at, "doit être remplie")

      else
        r = true

        if  self.m_odr_product and self.m_odr_product.m_odr_brand and self.m_odr_product.m_odr_brand.start_at
          start_at = self.m_odr_product.m_odr_brand.start_at

        else
          start_at = self.m_odr.start_at
        end

        if  self.m_odr_product and self.m_odr_product.m_odr_brand and self.m_odr_product.m_odr_brand.end_at
          end_at = self.m_odr_product.m_odr_brand.end_at

        else
          end_at = self.m_odr.end_at
        end

        if self.buy_at < start_at
          r = false
          errors.add(:buy_at, "est avant le début de l'opération")
        end

        if self.buy_at > end_at
          r = false
          errors.add(:buy_at, "est après la fin de l'opération")
        end


        if !r 
          errors.add(:buy_at, "n'est pas valide")


        else

        end



      end

    end


    if self.m_odr.reduc_code_needed and self.m_odr.m_odr_coupons.count > 0 and !self.m_odr.m_odr_coupons.where("name LIKE ?", self.reduc_code).first
      
      errors.add(:reduc_code, "n'existe pas")
      
      
    end
    
    
    self.particular = self.particulars.first
    
    if !self.state
      self.state = "Reçue"
    end
    
    if self.t.to_s != ""
      self.m_odr_tracker = MOdrTracker.where(:token => self.t).first
    end
    
  end
  
  
  
  
  def ca_particular_civilite
    self.particular.civilite if self.particular
  end
  
  def ca_particular_name
    self.particular.name if self.particular
  end
  def ca_particular_firstname
    self.particular.firstname if self.particular
  end
  def ca_particular_address_2
    self.particular.address_2 if self.particular
  end
  def ca_particular_address_3
    self.particular.address_3 if self.particular
  end
  def ca_particular_cp
    self.particular.cp if self.particular
  end
  def ca_particular_city
    self.particular.city if self.particular
  end
  def ca_particular_country
    self.particular.country if self.particular
  end
  def ca_particular_email
    self.particular.email if self.particular
  end
  
  def ca_particular_tel
    self.particular.tel if self.particular
  end
  
  def ca_product_name
    self.m_odr_product.name if self.m_odr_product
  end
  
  def ca_product_brand
    self.m_odr_product.m_odr_brand.name if self.m_odr_product and self.m_odr_product.m_odr_brand
  end
  
  def ca_tracker_name
    self.m_odr_tracker.name if self.m_odr_tracker
  end
  
  def ca_place_name
    self.m_odr_place.name if self.m_odr_place
  end
  
  
  attr_accessor :t
  
  
  
  
  
  
  
  
  
  before_create { generate_token() }
  
  def generate_token()
    begin
      self[:token] = SecureRandom.urlsafe_base64
    end while MOdrRep.exists?(:token => self[:token])
  end
  
  
  
  def self.qi_table_order
    {
      :m_odr => "ODR",
      :created_at => {:name => "Date", :reorder => true, :sort_name => "m_odr_reps.created_at"},
      :state => {:name => "Etat", :reorder => true},
      :cc_rib_admin_ok => {:name => "Rib valide ?", :reorder => true},
      :cc_file_admin_ok => {:name => "Fichiers valides ?", :reorder => true},
      :cc_admin_ok_to_send => {:name => "Virement à envoyer ?", :reorder => true},
      :cc_particular_civilite => {:name => "Civ.", :reorder => true},
      :cc_particular_name => {:name => "Nom", :reorder => true},
      :cc_particular_firstname => {:name => "Prénom", :reorder => true},
      :cc_particular_address_2 => {:name => "Adresse", :reorder => true},
      :cc_particular_address_3 => {:name => "Adresse (2)", :reorder => true},
      :cc_particular_cp => {:name => "Code postal", :reorder => true},
      :cc_particular_city => {:name => "Ville", :reorder => true},
      :cc_particular_country => {:name => "Pays", :reorder => true},
      :cc_particular_email => {:name => "Email", :reorder => true},
      :cc_particular_tel => {:name => "Tel", :reorder => true},
      
      :cc_particular_tel => {:name => "Tel", :reorder => true},
      :cc_particular_tel => {:name => "Tel", :reorder => true},
      
      
      :cc_product_brand => {:name => "Marque", :reorder => true},
      :cc_product_name => {:name => "Produit", :reorder => true},
      :qte => {:name => "Quantité", :reorder => true},
      :cc_remise => {:name => "Remise", :reorder => true},
      
      :cc_place_name => {:name => "Revendeur", :reorder => true},
      
      :reduc_code => {:name => "Code réduction", :reorder => true},

      
      
      
      :cc_tracker_name => {:name => "Trackeur", :reorder => true},

      :m_odr_product_cat => {:name => "Type"},
      
      
      
      :actions => "Actions",
      
      #:created_at => {:name => "Date", :reorder => true},
      
  
    }
    #, :sort_name => "code"
  end
  
  def ca_admin_ok_to_send
    if self.rib_admin_ok and self.file_admin_ok
      true
    else
      false
    end 
  end
  
  
  def ca_remise
    if self.qte and self.m_odr_product
      
      if self.m_odr_product and remise = self.m_odr_product.m_odr_product_remises.where(:qte => self.qte).first
        remise.amount
      else
        nil
        
      end
      
      
    end
  end

  def long_name
    self.particular.long_name if self.particular
  end
  
  def self.valid_sort
    r = []
    self.qi_table_order.each do |key, value|
      if value.instance_of? Hash
        if value[:reorder] and value[:reorder] == true
          if value[:sort_name] 
           
            r << value[:sort_name] if value[:sort_name]
          else
            r << key.to_s if value[:reorder]
          end
        end
      end
    end
    return r
    
    
  end
  
end
