class PriceLineBlock < ApplicationRecord
  belongs_to :price_lineable, :polymorphic => true
  validates :wish_date, :presence => true
  has_many :price_lines
  accepts_nested_attributes_for :price_lines, allow_destroy: true
  
  
  validates :p_customer_id, :presence => true
  validates :particular_bill_id, :presence => true
  validates :particular_send_id, :presence => true
  
  belongs_to :particular_bill, :class_name => "Particular"#, :dependent => :destroy
  
  accepts_nested_attributes_for :particular_bill
  
  belongs_to :particular_send, :class_name => "Particular"#, :dependent => :destroy
  
  accepts_nested_attributes_for :particular_send
  
  
  belongs_to :p_customer
  
  belongs_to :p_payment_type
  
   
  
  QI_DYNAMICS = %w(fdp_tva_rate weight_tot accounting_zone_id accounting_zone_name tot_lines_ht tot_lines_tva tot_lines_ttc tot_fdp_ht tot_fdp_tva tot_fdp_ttc tot_discount_ht tot_discount_tva tot_discount_ttc tot_amount_af_discount_ht tot_amount_af_discount_tva tot_amount_af_discount_ttc gen_discount_percent tot_gen_discount_ht tot_gen_discount_tva tot_gen_discount_ttc tot_amount_ht tot_amount_tva tot_amount_ttc remise_enrobage_ok remise_ecole_ok remise_pre_order_ok remise_qte_ok payment_comptant payment_delais payment_month_end payment_end_at acompte acompte_percent payment_days nbr_ship creation_date p_customer_cat_id)

  
  eval(QI_DYNAMICS_CORE)
  
  def personalised_archive
    self.price_lines.each do |pl|
      pl.archive_now
    end
  end
  
  def personalised_unarchive
    self.price_lines.each do |pl|
      pl.unarchive_now
    end
  end
  
  
  
  before_validation do
    if !self.id and self.p_customer
      self.p_commercial_id = self.p_customer.p_commercial_id
      
      
      self.p_payment_type_id = self.p_customer.p_payment_type_id
      self.ct_payment_comptant = self.p_customer.comptant
      self.ct_acompte = self.p_customer.acompte
      self.ct_acompte_percent = self.p_customer.acompte_percent
      self.ct_payment_delais = self.p_customer.payment_delais
      self.ct_payment_month_end = self.p_customer.payment_fin_de_mois
      
      
    end
    
    if !self.p_customer or !self.particular_bill or !self.particular_bill.owner or self.particular_bill.owner != self.p_customer
      errors.add(:particular_bill_id, 'doit être une adresse du client') 
    end
    
    if !self.p_customer or !self.particular_send or !self.particular_send.owner or self.particular_send.owner != self.p_customer
      errors.add(:particular_send_id, 'doit être une adresse du client') 
    end
    
  end
  
  
  def ca_remise_pre_order_ok
    
    if self.p_customer and self.p_customer_cat_id == 1
      true
    else
      false
    end
  end
  
  
  def ca_remise_qte_ok
    
    if self.p_customer and self.p_customer_cat_id == 1
      true
    else
      false
    end
  end

  def ca_remise_enrobage_ok
    
    if !self.remise_ecole_ok and self.p_customer and self.p_customer.discount_enrobage
      true
    else
      false
    end
  end
  
  def ca_remise_ecole_ok
    if self.p_customer and self.p_customer_cat_id == 3
      true
    else
      false
    end
  end


  
  
  
  after_save do
    self.price_lines.each do |pl|
      pl.save
    end
  end
  
  
  
  def ca_weight_tot
    r = 0.0
    self.price_lines.each do |pl|
      r+= pl.weight_tot
    end
    return r
  end
  
  def ca_accounting_zone_id
    self.price_lineable.accounting_zone_id
  end

  def ca_accounting_zone_name
    self.price_lineable.accounting_zone_name
  end

  def ca_tot_lines_ht
    r = 0.0
    self.price_lines.each do |pl|
      r+= pl.tot_amount_ht
    end
    return r
    
  end

  def ca_tot_lines_tva
    r = 0.0
    self.price_lines.each do |pl|
      r+= pl.tot_amount_tva
    end
    return r
  end

  def ca_tot_lines_ttc
    r = 0.0
    self.price_lines.each do |pl|
      r+= pl.tot_amount_ttc
    end
    return r
  end
  
  def ca_tot_fdp_ht 
    if self.ct_tot_fdp_ht
      self.ct_tot_fdp_ht
    else
      if self.p_customer_cat_id == 2
        if (self.tot_lines_ht + self.tot_discount_ht) > 290.0
          0.0
        elsif (self.tot_lines_ht + self.tot_discount_ht) >= 150.0
          15.0
        else
          30.0
        end
      else
        
        if ca_weight_tot > 45
          0.0
        elsif ca_weight_tot > 30
          25.0
        else
          40.0
        end
        
        
      end
      
    end
      
  end
  
  def ca_fdp_tva_rate
    if self.accounting_zone_id == 1
      20.0
    else
      0.0
    end
  end
  
  def ca_tot_fdp_tva  
    self.tot_fdp_ht * (self.fdp_tva_rate/100)
  end
  
  def ca_tot_fdp_ttc 
    self.tot_fdp_ht + self.tot_fdp_tva
  end

  def ca_tot_discount_ht
    if self.ct_tot_discount_percent
      (self.ct_tot_discount_percent / -100.0) * self.ca_tot_lines_ht
    else
      0.0
    end
  end

  def ca_tot_discount_tva
    if self.ct_tot_discount_percent
      (self.ct_tot_discount_percent / -100.0) * self.ca_tot_lines_tva
    else
      0.0
    end
    
   
  end

  def ca_tot_discount_ttc
    self.tot_discount_ht + self.tot_discount_tva
  end

  def ca_tot_amount_af_discount_ht
    self.tot_lines_ht + self.tot_discount_ht + self.tot_fdp_ht
  end

  def ca_tot_amount_af_discount_tva
    self.tot_lines_tva + self.tot_discount_tva + self.tot_fdp_tva
  end

  def ca_tot_amount_af_discount_ttc
    self.tot_lines_ttc + self.tot_discount_ttc + self.tot_fdp_ttc
  end

  def ca_gen_discount_percent
    0.0
  end

  def ca_tot_gen_discount_ht
    0.0
  end

  def ca_tot_gen_discount_tva
    0.0
  end

  def ca_tot_gen_discount_ttc
    0.0
  end

  def ca_tot_amount_ht
    self.tot_amount_af_discount_ht + self.tot_gen_discount_ht
  end

  def ca_tot_amount_tva
    self.tot_amount_af_discount_tva + self.tot_gen_discount_tva
  end

  def ca_tot_amount_ttc
    self.tot_amount_af_discount_ttc + self.tot_gen_discount_ttc
  end
  
  
  def ca_payment_comptant
    if self.ct_payment_comptant
      self.ct_payment_comptant
    else
      if self.p_customer
        self.p_customer.comptant
      else
        nil
      end   
    end 
  end
  
  def ca_payment_delais
    if self.ct_payment_delais
      self.ct_payment_delais
    else
      if self.p_customer
        self.p_customer.payment_delais
      else
        nil
      end
    end
  end
  
  def ca_payment_month_end
    if self.ct_payment_month_end
      self.ct_payment_month_end
    else
      if self.p_customer
        self.p_customer.payment_fin_de_mois
      else
        nil
      end
    end
  end
  
  def ca_payment_end_at
    
    if self.ct_payment_comptant
      self.creation_date
    else
      self.creation_date + self.ct_payment_delais.to_i.days
    end
    
  end
  
  def ca_acompte
    if self.p_customer
      self.p_customer.acompte
    else
      nil
    end
  end
  
  def ca_acompte_percent
    if self.p_customer
      self.p_customer.acompte_percent
    else
      nil
    end
  end
  
  def ca_payment_days
  
  end
  
  def ca_nbr_ship
    if self.ct_nbr_ship
      self.ct_nbr_ship
    else
      nil
    end
      
  end
  
  def ca_creation_date
    if self.ct_creation_date
      self.ct_creation_date
    elsif self.id
      self.created_at
    else
      Date.today
    end
  end
  
  def ca_p_customer_cat_id
    if self.p_customer
      self.p_customer.p_customer_cat_id
    else
      nil
    end
  end
  
  def cumul_discount_ht
    r = 0.0
    self.price_lines.each do |pl|
      r += pl.ca_tot_discount_ht
      
    end
    return r
      
  end
  
  
  
  
end
