class StatLine < ApplicationRecord
  belongs_to :p_product
  belongs_to :p_product_cat
  belongs_to :p_product_ref
  belongs_to :p_commercial
  belongs_to :p_customer
  belongs_to :price_document
  belongs_to :s_brand


  acts_as_sorting :fields => {
    :id => {:name => "ID", :reorder => true},
    :date => {:name => "Date", :reorder => true},
    :p_product_id => {:name => "ID Produit", :reorder => true},
    :product_name => {:name => "Produit - nom", :reorder => true},
    :product_code => {:name => "Produit - code", :reorder => true},
    :p_product_ref_id => {:name => "ID Ref produit", :reorder => true},
    :p_product_ref_name => {:name => "Ref produit - nom", :reorder => true},
    :tva_rate => {:name => "Taux TVA", :reorder => true},
    :tva_account_id => {:name => "Compte TVA", :reorder => true},
    :title => {:name => "Désignation", :reorder => true},
    :price_u_ht => {:name => "Prix U. HT", :reorder => true},
    :price_u_tva => {:name => "Prix U. TVA", :reorder => true},
    :price_u_ttc => {:name => "Prix U. TTV", :reorder => true},
    :qte => {:name => "Quantité", :reorder => true},
    :tot_line_ht => {:name => "Total ligne HT", :reorder => true},
    :tot_line_tva => {:name => "Total ligne TVA", :reorder => true},
    :tot_line_ttc => {:name => "Total ligne TTC", :reorder => true},
    :tot_discount_ht => {:name => "Total remise HT", :reorder => true},
    :tot_discount_tva => {:name => "Total remise TVA", :reorder => true},
    :tot_discount_ttc => {:name => "Total remise TTC", :reorder => true},
    :tot_amount_ht => {:name => "Total HT", :reorder => true},
    :tot_amount_tva => {:name => "Total TVA", :reorder => true},
    :tot_amount_ttc => {:name => "Total TTC", :reorder => true},
    :weight_u => {:name => "Poids U.", :reorder => true},
    :weight_tot => {:name => "Poids total", :reorder => true},
    :cost_ht => {:name => "Coût HT", :reorder => true},
    :cost_u_ht => {:name => "Coût U.", :reorder => true},
    :marge_ht => {:name => "Marge HT", :reorder => true},
    :marge_u_ht => {:name => "Marge U. HT", :reorder => true},
    :cost_u_w_ht => {:name => "Cout U./Kg HT", :reorder => true},
    :marge_u_w_ht => {:name => "Marge U./Kg HT", :reorder => true},
    :p_commercial_id => {:name => "ID - Commercial", :reorder => true},
    :p_commercial_name => {:name => "Commercial - nom", :reorder => true},
    :p_commercial_code => {:name => "Commercial - code", :reorder => true},
    :p_product_cat_id => {:name => "ID - Cat. produit", :reorder => true},
    :p_product_cat_name => {:name => "Cat. produit - nom", :reorder => true},
    :p_customer_id => {:name => "ID - Client", :reorder => true},
    :p_customer_name => {:name => "Client - nom", :reorder => true},
    :p_customer_code => {:name => "Client - code", :reorder => true},
    :price_document_id => {:name => "ID Price Document", :reorder => true},
    :price_document_number => {:name => "N° Document", :reorder => true},
    :price_line_block_id => {:name => "ID Price line block", :reorder => true},
    :price_line_id => {:name => "ID Price line", :reorder => true}



  }
  
  
  def self.update_all_stats
    StatLine.destroy_all
    PriceDocument.all.each do |pd|
      pd.generate_stat_lines
    end
  end
  
end
