class PriceLine < ApplicationRecord
  belongs_to :price_line_block
  belongs_to :p_product
  belongs_to :p_product_ref
  default_scope { order('position ASC') }
  
  
  #validates :qte,  :presence => true
  attr_accessor :forced_price
  
   
  acts_as_caching :fields => [:ship_price_ht, :caution, :credits, :permis_b, :stage_remorque, :permis_remorque, :carburant, :carburant_type, :carburant_price, :carburant_mention, :carburant_reservoir, :age_min, :cg, :accessoire, :sale_account, :p_product_cat_id, :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]
  
  
  
  def ca_caution
    if self.ct_caution?
      self.ct_caution
    elsif self.p_product_ref and  self.p_product_ref.p_product
      self.p_product_ref.p_product.caution
      
    end
      
  end
  
  def ca_ship_price_ht
    if self.ct_ship_price_ht?
      self.ct_ship_price_ht
    elsif self.p_product_ref and  self.p_product_ref.p_product
      #self.p_product.caution
      
    end
      
  end
  
  
  def personalised_archive
    
  end
  
  def personalised_unarchive
    
  end
  
  
  def ca_sale_account
  
    if self.ct_sale_account
      self.ct_sale_account
  
    elsif self.p_product_cat
      self.p_product_cat.sale_account
    end
    
  end
  
  

  
  before_validation do
   
  end
  
  def p_product_cat
    PProductCat.where(:id => self.p_product_cat_id).first
  end
  def ca_p_product_cat_id
    if true
      if self.ct_p_product_cat_id
        self.ct_p_product_cat_id
      else
        if self.p_product_ref and self.p_product_ref.p_product
          return self.p_product_ref.p_product.p_product_cat_id
        end
      end
    end
  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 true
      if self.ct_tva_account_id
        self.ct_tva_account_id
      else
        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
    end
  end

  def ca_tva_account_value
    if self.tva_account_id
      return TvaRate.find(self.tva_account_id).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.ref
    else
      ""
    end

  end

  def ca_title
    if self.ct_title?
      self.ct_title
    elsif self.p_product_ref
      "#{self.p_product_ref.p_product.s_brand.name} #{self.p_product_ref.name}"
    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.weight_u and self.weight_u != 0.0
      self.price_u_ht / self.weight_u
    else
      0
    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) + self.ship_price_ht.to_f
  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 
    0
    
  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 
    0
  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 
    0
    
  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
    0
  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
    false
  end
  def ca_discount_enrobage_percent 
    0
    
  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 
    0
  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_ht * self.qte.to_f).round(2)+ self.ship_price_ht.to_f
  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
