class OrderHistLine < ApplicationRecord
  belongs_to :p_product_ref
  belongs_to :p_payment_type
  belongs_to :p_commercial
  belongs_to :p_customer
  
  belongs_to :order_hist
  
  acts_as_sorting :fields => {
    #:id => {:name => "Id", :reorder => true},
    :new_code_client => {:name => "Nouveau code client", :reorder => true},
    :p_customer_id => {:name => "Client", :reorder => true},
    #:past_code_client => {:name => "Code client archive", :reorder => true},
    :numerofacture => {:name => "Numéro facture", :reorder => true},
    #:numero_piece => {:name => "", :reorder => true},
    #:d_number => {:name => "", :reorder => true},
    #:position => {:name => "", :reorder => true},
    :code_article => {:name => "Code article", :reorder => true},
    :p_product_ref_id => {:name => "Article", :reorder => true},
    :qte => {:name => "Quantité", :reorder => true},
    :prix_u => {:name => "Prix U.", :reorder => true, :as => :currency},
    :montant_ht => {:name => "Montant HT", :reorder => true, :as => :currency},
    :montant_ht => {:name => "Montant HT remisé", :reorder => true, :as => :currency},
    
    #:prix_achat => {:name => "Prix achat", :reorder => true, :as => :currency},
    :taux_tva => {:name => "Taux TVA", :reorder => true},
    #:remise_1 => {:name => "Remise 1", :reorder => true},
    #:remise_2 => {:name => "Remise 2", :reorder => true},
    #:date_creation => {:name => "Date création", :reorder => true, :as => :date},
    #:date_modification => {:name => "", :reorder => true},
    :compte_vente => {:name => "Compte vente", :reorder => true},
    :date_piece => {:name => "Date pièce", :reorder => true, :as => :date},
    :date_facture => {:name => "Date facture", :reorder => true, :as => :date},
    #:colisage => {:name => "", :reorder => true},
    #:nbr_colis => {:name => "", :reorder => true},
    :taux_com_1 => {:name => "Taux com.", :reorder => true},
    #:colisage_achat => {:name => "", :reorder => true},
    #:prix_achat_brut => {:name => "", :reorder => true},
    #:quantite_origine => {:name => "", :reorder => true},
    #:pu_tarif_origine => {:name => "", :reorder => true},
    #:taux_tva_article_2 => {:name => "Taux TVA 2", :reorder => true},
    #:compte_ventil_vente => {:name => "", :reorder => true},
    #:num_ligne_avancement => {:name => "", :reorder => true},
    #:etat => {:name => "", :reorder => true},
    :reference_1 => {:name => "Référence 1", :reorder => true},
    :reference_2 => {:name => "Référence 2", :reorder => true},
    :reference_3 => {:name => "Référence 3", :reorder => true},
    :reference_4 => {:name => "Référence 4", :reorder => true},
    #:code_mode_reglement => {:name => "", :reorder => true},
    :p_payment_type_id => {:name => "Type de paiement", :reorder => true},
    #:echeance_nbr_jour => {:name => "Nbr jours paiement", :reorder => true},
    :date_echeance => {:name => "Date d'échéance", :reorder => true},
    #:code_commercial => {:name => "Code commercial", :reorder => true},
    :p_commercial_id => {:name => "Commercial", :reorder => true},
    :taux_escompte => {:name => "Escompte", :reorder => true},
    :taux_remise => {:name => "Remise pied de page", :reorder => true},
    #:fichier_archive => {:name => "", :reorder => true},
    #:interlocuteur => {:name => "", :reorder => true},
    
    
    #:actions => {:name => "Actions"}
    
  }
  
  acts_as_csv_import :fields => [:past_code_client, :numero_piece, :numerofacture, :d_number, :position, :code_article, :p_product_ref, :qte, :prix_u, :prix_achat, :taux_tva, :remise_1, :remise_2, :date_creation, :date_modification, :compte_vente, :date_piece, :date_facture, :colisage, :nbr_colis, :taux_com_1, :colisage_achat, :prix_achat_brut, :quantite_origine, :pu_tarif_origine, :taux_tva_article_2, :compte_ventil_vente, :num_ligne_avancement, :etat, :reference_1, :reference_2, :reference_3, :reference_4, :code_mode_reglement, :p_payment_type, :echeance_nbr_jour, :date_echeance, :code_commercial, :p_commercial, :taux_escompte, :taux_remise, :fichier_archive, :interlocuteur, :new_code_client, :p_customer, :montant_ht, :montant_ht_remise]
  
  
  acts_as_caching :fields => [:montant_ht_com, :cout_achat_ht, :marge_brut_hors_remise, :marge_brut_remise, :marge_net, :marge_brut_hors_remise_pa, :marge_brut_remise_pa, :marge_net_ht_pa]  
  

  
  def ca_montant_ht_com
    (self.montant_ht_remise * (self.taux_com_1.to_f / 100.0)).round(2)
  end
    
  def ca_cout_achat_ht
    self.qte.to_f * self.prix_achat.to_f
  end
  
  def ca_marge_brut_hors_remise
    self.montant_ht - self.cout_achat_ht
  end
  
  def ca_marge_brut_remise
    self.montant_ht_remise - self.cout_achat_ht
  end
  
  def ca_marge_net
    self.montant_ht_remise - self.cout_achat_ht - self.montant_ht_com
  end
  
  def ca_marge_brut_hors_remise_pa
    self.marge_brut_hors_remise / self.cout_achat_ht * 100.0 if self.cout_achat_ht != 0.0
  end
  
  def ca_marge_brut_remise_pa
    self.marge_brut_remise / self.cout_achat_ht * 100.0 if self.cout_achat_ht != 0.0
  end
  
  def ca_marge_net_ht_pa
    self.marge_net / self.cout_achat_ht * 100.0 if self.cout_achat_ht != 0.0
  end
  
  

  
  
  
  before_validation do
    self.p_customer = PCustomer.where(:code => self.new_code_client.to_s).first if !self.p_customer 
    
    self.p_commercial = PCommercial.where(:code => self.code_commercial.to_s).first if !self.p_commercial
    
    self.p_product_ref = PProduct.where(:code => self.code_article.to_s).first.p_product_refs.first if !self.p_product_ref and PProduct.where(:code => self.code_article.to_s).first
    
    self.p_payment_type = PPaymentType.where(:code => self.code_mode_reglement.to_s).first if !self.p_payment_type 
    
  end
  
  attr_accessor :skip_order_hist
  
  after_save do
    if !skip_order_hist
      if !self.order_hist
        if !order_hist = OrderHist.where(:numerofacture => self.numerofacture).first
          order_hist = OrderHist.new
        end
      else
        order_hist = self.order_hist
      
      end
    
  
  
      copy = [:past_code_client, :numero_piece, :numerofacture, :d_number, :date_creation, :date_modification, :date_piece, :date_facture, :p_payment_type_id, :p_commercial_id, :p_customer_id]
  
      copy.each do |c|
        eval "order_hist.#{c.to_s} = self.#{c.to_s}"
      end
      
      order_hist.save
      self.order_hist = order_hist
      self.skip_order_hist = true
      self.save
      
 
      
    
    end
    
    
     self.order_hist.save if self.order_hist
    
    
    
    
    
  end
  
 

end
