class MOdrPrime < ApplicationRecord
  belongs_to :p_customer
  belongs_to :m_odr_file_roulage

  has_one :particular, :through => :p_customer

  include ActionView::Helpers::NumberHelper
  include ActionView::Helpers::TranslationHelper
  include ActionView::Helpers::UrlHelper

  

  has_many :m_odr_prime_files #, :dependent => :destroy
  
  
  has_many :m_odr_files, :through => :m_odr_prime_files
  
  has_many :m_odr_places, :through => :m_odr_files
  
  has_many :m_odr_file_products, :through => :m_odr_files
  
  
  has_many :m_odr_products, :through => :m_odr_file_products
  

  has_many :mail_hists
  has_many :m_odr_virements
  
  

  acts_as_sorting :fields => {
    :id => {:name => "Id", :reorder => true},
    #:p_customer => {:name => "Client", :reorder => true, :sort_name => "p_customers.cc_show_name"},
    
    :p_customer_created_at => {:name => "Inscription", :reorder => true, :sort_name => "p_customers.created_at"},
    
    :particular_name => {:name => "Nom", :reorder => true, :sort_name => "p_customers.cc_particular_name"},
    :particular_firstname => {:name => "Prénom", :reorder => true, :sort_name => "p_customers.cc_particular_firstname"},
    :amount => {:name => "Montant", :as => :currency},
    
    :created_at => {:name => "Date"},
    
    
    :pneu_gamme => {:name => "Gamme"},
    
    #:pneu_tailles => {:name => "Tailles"},
    
    :m_odr_place_name => {:name => "Revendeur - Nom"},
    :m_odr_place_cp=> {:name => "Revendeur - CP"},
    :m_odr_place_city => {:name => "Revendeur - Ville"},
    
    :organisateur_name => {:name => "Organisateur"},
    
    
    
    :date_roulage => {:name => "Date de roulage", :reorder => true, :sort_name => "m_odr_file_roulages.date"},
    :state => {:name => "Statut", :reorder => true},
    :date_virement => {:name => "Date de virement"},
    
    :actions => {:name => "Actions", :reorder => true},

  }
  
  

  before_validation do

    self.nbr_pneus = 0

    self.amount = 0 

    self.m_odr_prime_files.each do |m|
      self.nbr_pneus = self.nbr_pneus.to_i + m.nbr_pneus
      self.amount = self.amount.to_f + m.amount
    end
  
  end

  before_destroy do
    self.m_odr_file_roulage.nbr_primes_useds = self.m_odr_file_roulage.nbr_primes_useds - self.nbr_pneus
    self.m_odr_file_roulage.save
    self.m_odr_file_roulage.m_odr_file.save


    puts self.m_odr_prime_files.count

    self.m_odr_prime_files.each do |m|
      
      m.m_odr_file.nbr_pneus_useds = m.m_odr_file.nbr_pneus_useds - m.nbr_pneus
      m.m_odr_file.save

      m.destroy
      
    end

   

  end

  def change_state(state)
    self.state = state
    self.save
  end

  def cancel

  end

  def self.reset_primes
    MOdrFile.order("buy_at DESC").where(:m_odr_file_type_id => 2).update_all(:nbr_pneus_useds => 0)

    MOdrFileRoulage.update_all(:nbr_primes_useds => 0)

    MOdrFile.all.each do |m|
      m.save
    end

    MOdrPrime.destroy_all

  end

  def send_init_mail
    if self.state == "A traiter"
      slug = "primes-traitement-en-cours"
    else
      slug = "primes-manque-rib"
    end

    #MailHist.auto_generate_mail(:fr, slug, self.p_customer.email, options = {:arguments => {:remise => number_to_currency(self.amount.to_f)}, :p_customer => self.p_customer, :element => self})
    
  end


  def send_mail_type(slug, element = nil)

    mail_hist = MailHist.auto_generate_mail(:fr, slug, self.p_customer.email, {:arguments => {:remise => number_to_currency(self.amount.to_f), :nbr => self.nbr_pneus }, :m_odr_prime => self, :p_customer => self.p_customer, :element => self})
    
  end
  
  
  
  
  
  
  def csv_particular_name
    self.particular.name if self.particular
  end
  
  
  def csv_particular_firstname
    self.particular.firstname if self.particular
  end
  
  def csv_p_customer_created_at
    self.p_customer.created_at if self.p_customer
  end
  
  def csv_state
    self.state
  end
      
  def csv_date_roulage
    self.m_odr_file_roulage.date if self.m_odr_file_roulage
  end  
   
  
  
  def csv_pneu_gamme
    self.m_odr_products.uniq.map{ |u| (u.name)}.join(" | ")
  end
  
  def csv_m_odr_place_name
    self.m_odr_places.uniq.map{ |u| u.name}.join(" | ")
  end
      
  def csv_m_odr_place_cp
    self.m_odr_places.uniq.map{ |u| u.cp}.join(" | ")
  end
      
  def csv_m_odr_place_city
    self.m_odr_places.uniq.map{ |u| u.city}.join(" | ")
  end  
   
  def csv_organisateur_name
    self.m_odr_file_roulage.m_event.organisateur.name if self.m_odr_file_roulage and  self.m_odr_file_roulage.m_event and self.m_odr_file_roulage.m_event.organisateur
  end
      
  def csv_date_virement
    virement = self.m_odr_virements.where(:refused => 0, :sended => 1).order('id DESC').first
    if virement and virement.m_odr_remise
     virement.m_odr_remise.created_at
   end
  end
      
   
  
  
  
  
  
  
  
  

end
