class PriceLine < ApplicationRecord
  belongs_to :price_line_block
  belongs_to :p_product
  belongs_to :p_product_ref
  default_scope { order('position ASC') }
  
  validates :p_product_ref_id, :presence => true
  validates :qte,  :presence => true
  attr_accessor :forced_price
  
  QI_DYNAMICS = %w(price_calc weight_u weight_tot accounting_zone_id accounting_zone_name tva_account_id tva_account_value ref title description price_u_ht price_u_tva price_u_ttc tot_line_ht tot_line_tva tot_line_ttc tot_discount_ht tot_discount_tva tot_discount_ttc tot_amount_ht tot_amount_tva tot_amount_ttc discount_market_percent discount_qte_percent discount_delay_percent discount_enrobage_percent discount_ecole_percent discount_comptant_percent product_remise_enrobage_ok uv p_product_specific_customer_id)

  
  eval(QI_DYNAMICS_CORE)
  
  
  def personalised_archive
    
  end
  
  def personalised_unarchive
    
  end
  
  
  

  
  before_validation do
   
  end
  
  
  
  def ca_price_calc
    self.p_product_ref.p_product.price_calc == "Kg"
    
  end
  
  def ca_p_product_specific_customer_id
    if self.p_product_ref and self.p_product_ref.p_product
      self.p_product_ref.p_product.p_customer_id
    else
      nil
    end
  end
  
  def ca_weight_u
    if self.p_product_ref
      self.p_product_ref.weight
    else
      0.0
    end
  end
  
  def ca_weight_tot
    self.weight_u.to_f * self.qte.to_f
  end
  
  def ca_accounting_zone_id
    self.price_line_block.accounting_zone_id if self.price_line_block
  end

  def ca_accounting_zone_name
    self.price_line_block.accounting_zone_name
  end
  
  def ca_tva_account_id
    if self.p_product_ref and self.p_product_ref.p_product and tva_rate = self.p_product_ref.p_product.tva_rate(self.accounting_zone_id)
      return tva_rate.id
    end
  end

  def ca_tva_account_value
    if self.p_product_ref and self.p_product_ref.p_product and tva_rate = self.p_product_ref.p_product.tva_rate(self.accounting_zone_id)
      return (tva_rate.rate).round(2)
      
    else
      return 0.0
    end
  end

  def ca_ref
    if self.p_product_ref and self.p_product_ref.p_product
      self.p_product_ref.p_product.code
    else
      ""
    end

  end

  def ca_title
    if self.p_product_ref
      "#{self.p_product_ref.p_product.name} - #{self.p_product_ref.uv}"
    else
      ""
    end  
  end
  
  def ca_uv
    if self.p_product_ref
      self.p_product_ref.uv
    else
      ""
    end
  end

  def ca_description

  end
  
  def th_price_u_ht
    if self.p_product_ref   
      return self.p_product_ref.get_price(:p_customer_id => nil, :qte => self.qte)
    else
      return nil
    end
  end
  
  def ca_price_u_ht
    if self.ct_u_price_ht
      return self.ct_u_price_ht
    elsif self.p_product_ref
      
      return self.p_product_ref.get_price(:p_customer_id => nil, :qte => self.qte)
      
      
      
    else
      return 0.0
    end
  end
  
  def ca_price_u_kg_ht
    
    if self.price_calc == "Kg"
    
      if self.weight_u.to_f != 0.0
        self.price_u_ht.to_f / self.weight_u.to_f
      else
        0
      end
      
    else
      self.price_u_ht
      
    end
      
  end
  
  
  def weight_qte 
    if self.price_calc == "Kg"
      self.qte.to_f * self.weight_u.to_f
    else
      self.qte.to_f
    end

  end
  

  def ca_price_u_tva
    ((self.tva_account_value/100) * self.price_u_ht).round(2)
  end

  def ca_price_u_ttc
    (self.price_u_ht + self.price_u_tva).round(2)
  end
  
  

  def ca_tot_line_ht
    (self.price_u_ht * self.qte.to_f).round(2)
  end

  def ca_tot_line_tva
    (self.tot_line_ht * (self.tva_account_value/100)).round(2)
  end

  def ca_tot_line_ttc
    (self.tot_line_ht + self.tot_line_tva).round(2)
  end




  # REMISES
  
  # Remise marché annuel
  def ca_discount_market_percent 
    if !self.ct_u_price_ht and !self.p_product_specific_customer_id and self.discount_ecole_percent == 0 and self.p_customer and self.p_customer.market_discount
      self.p_customer.market_discount.percent
    else
      0.0
    end
    
  end

  def ca_discount_market_ht 
    ((1-(self.discount_market_percent.to_f / 100)) * self.ca_price_u_kg_ht)
  end


  # Remise quantité livrée (par commande)
  def ca_discount_qte_percent 
    if !self.ct_u_price_ht and !self.p_product_specific_customer_id and self.discount_ecole_percent == 0 and self.price_line_block and self.price_line_block.remise_qte_ok
      weight = self.price_line_block.weight_tot
      if weight > 500
        percent = 5
      elsif weight > 300
        percent = 4
      elsif weight > 220
        percent = 3
      elsif weight > 160
        percent = 2
      elsif weight > 75
        percent = 1
      else
        percent = 0
      end
        
        
      return percent
        
    else
      return 0.0
    end
  end
  
  

  def ca_discount_qte_ht
    ((1-(self.discount_qte_percent.to_f / 100) )* self.ca_discount_market_ht) 
  end

  def ca_discount_delay_percent 
    if !self.ct_u_price_ht and !self.p_product_specific_customer_id and self.discount_ecole_percent == 0 and self.price_line_block  and self.price_line_block.remise_pre_order_ok and self.price_line_block.wish_date and self.price_line_block.created_at and self.price_line_block.wish_date >= (self.price_line_block.created_at + 8.weeks)
      2
    else
      0
    end
    
  end

  def ca_discount_delay_ht
    ((1-(self.discount_delay_percent.to_f / 100)) * self.ca_discount_qte_ht)  
  end

  

  def ca_discount_comptant_percent
    if !self.p_product_specific_customer_id and self.discount_ecole_percent == 0 and self.p_customer and self.p_customer.discount_comptant
      2
    else
      0
    end 
  end

  def ca_discount_comptant_ht 
    ((1-(self.discount_comptant_percent.to_f / 100)) * self.ca_discount_delay_ht)
  end
  
  def ca_product_remise_enrobage_ok
    if !self.ct_u_price_ht and self.p_product_ref and self.p_product_ref.p_product and self.p_product_ref.p_product.discount_enrobage
      true
    else
      false
    end
  end
  def ca_discount_enrobage_percent 
    if !self.p_product_specific_customer_id and self.discount_ecole_percent == 0 and self.price_line_block and self.price_line_block.remise_enrobage_ok and self.product_remise_enrobage_ok
      5
    else
      0
    end
    
  end

  def ca_discount_enrobage_ht 
    ((1-(self.discount_enrobage_percent.to_f / 100) )* self.ca_discount_comptant_ht)
  end

  def ca_discount_ecole_percent 
    if !self.ct_u_price_ht and !self.p_product_specific_customer_id and self.price_line_block and self.price_line_block.remise_ecole_ok
      20
    else
      0
    end
  end

  def ca_discount_ecole_ht 
    ((1-(self.discount_ecole_percent.to_f / 100)) * self.ca_discount_enrobage_ht)
  end
  
  
  # FIN REMISES
  
  def ca_price_u_kg_net_ht
    self.ca_discount_ecole_ht
  end








  

  

  






  def ca_tot_amount_ht
    (self.ca_price_u_kg_net_ht * self.weight_qte).round(2)
  end

  def ca_tot_amount_tva
    (self.tot_amount_ht * (self.tva_account_value/100)).round(2)
  end

  def ca_tot_amount_ttc
    (self.tot_amount_ht + self.tot_amount_tva).round(2)
  end
  
  
  
  def ca_tot_discount_ht
    (self.ca_price_u_ht * self.qte.to_f).round(2) - self.ca_tot_amount_ht
  end
  
  
  
  def ca_tot_discount_tva
    (self.tot_discount_ht * (self.tva_account_value/100)).round(2)
  end

  def ca_tot_discount_ttc
    (self.tot_discount_ht+self.tot_discount_tva).round(2)
  end
  
  
  
  
  
  
  
  
  def p_customer
    if self.price_line_block
      self.price_line_block.p_customer
    end
  end
  
  
  
  
  
  
  
end
