class LineStock < ApplicationRecord
  belongs_to :p_product_ref

  belongs_to :stockable, :polymorphic => true
  belongs_to :price_line
  belongs_to :price_line_block
  belongs_to :stock_movement_line
  belongs_to :partition_lines
  acts_as_sorting :fields => {
    :id => {:name => "ID", :reorder => false},
    :date => {:name => "Date", :reorder => true, :as => :date},
    :stockable => {:name => "Origine", :reorder => false},
    :p_product_ref => {:name => "Produit", :reorder => false},
    :description => {:name => "Description", :reorder => true, :sort_name => "p_customer_sheets.created_at"},
    :dluo => {:name => "DLUO", :reorder => true},

    :qte => {:name => "Quantité", :reorder => true},
    :qte_available => {:name => "Quantité restante", :reorder => true},
    :qte_used => {:name => "Quantité utilisée", :reorder => true},
    :price_ht => {:name => "Valeur totale d'origine", :reorder => true, :as => :currency},

    :price_ht_available => {:name => "Valeur disponnible", :reorder => true, :as => :currency},

    :price_ht_used => {:name => "Valeur utilisée", :reorder => true, :as => :currency},

    :price_u_ht => {:name => "Prix Unitaire", :reorder => true, :as => :currency},

    :price_u_kg_ht => {:name => "Prix U [Kg]", :reorder => true, :as => :currency},

    :qte_kg_per_unit => {:name => "Poids par unité", :reorder => true},

    :qte_kg => {:name => "Poids total", :reorder => true},



    :actions => {:name => "Actions", :reorder => false},
  }

  has_many :line_stock_usages, :dependent => :destroy

  acts_as_caching :fields => [:line_type, :price_ht_available, :price_ht_used, :price_u_ht, :price_u_kg_ht, :qte_kg_per_unit, :qte_kg, :qte_kg_available, :qte_kg_used ]

  def price_u_ht
    self.price_ht.to_f / self.qte.to_f
  end


  before_destroy do
    LineStockUsage.where(:dest_line_stock_id => self.id).all.each do |lsu|
      
      lsu.destroy
      
    end
  end


  before_validation do
    if self.qte >= 0
      self.qte_used = self.line_stock_usages.sum(:qte)
      self.qte_available = self.qte - self.qte_used
    else
      self.qte_used = nil
      self.qte_available = nil
    end
  end

  def ca_line_type
    if self.stockable and self.stockable_type == "PriceDocument" and self.stockable.price_document_type_id == 6
      "Facture d'achat"
    elsif self.stockable and self.stockable_type == "PriceDocument" and self.stockable.price_document_type_id == 4
      "Facture vente"

    else
      "Entrée manuelle"
    end
  end

  def ca_price_ht_available

    if self.qte >= 0
      self.qte_available.to_f * self.price_u_ht.to_f

    end

  end

  def ca_price_ht_used

    if self.qte >= 0
      self.qte_used.to_f * self.price_u_ht

    end

  end

  def ca_price_u_ht
    (self.price_ht.to_f / self.qte.to_f).round(2)

  end

  def ca_price_u_kg_ht
    if self.qte_kg_per_unit.to_f != 0.0
      (self.price_u_ht.to_f / self.qte_kg_per_unit).round(2)
    else
      0.0
    end
  end

  def ca_qte_kg_per_unit
    if self.p_product_ref
      self.p_product_ref.weight
    else
      0.0
    end

  end

  def ca_qte_kg
    self.qte.to_f * self.qte_kg_per_unit.to_f
  end


  def ca_qte_kg_available
    self.qte_available.to_f * self.qte_kg_per_unit.to_f
  end

  def ca_qte_kg_used
    self.qte_used.to_f * self.qte_kg_per_unit.to_f
  end


end
