class PSheetLine < ActiveRecord::Base
  belongs_to :p_customer_sheet
  belongs_to :p_product
  belongs_to :p_tank
  
  has_many :p_sheet_line_stocks, :dependent => :destroy
  
  validates :qte, :presence => true
  validates :price, :presence => true
  
  validates :ok_qte, :presence => true, :if => :validate_ok?
  validates :ok_price, :presence => true, :if => :validate_ok?
  validates :ok_at, :presence => true, :if => :validate_ok?
  
  validates :p_product, :presence => true
  #validates :p_tank, :presence => true, :if => :validate_tank?
  
  
	scope :oks, -> {where(:shiped => true).order("ok_at DESC")}
  scope :not_oks, -> {where(:shiped => false).order("ok_at DESC")}
	
	scope :between, lambda { |start, stop|
	        after(start).before(stop)
	    }
	
	scope :after, lambda { |date|
	        where("(ok_at >= ?)", date )
	    }
	scope :before, lambda { |date|
	        where("(ok_at <= ?)", date )
	    }
     
  after_save do
    self.p_customer_sheet.save if self.p_customer_sheet
  end 
      
  before_save do 
    self.a_total_cost_ok = self.total_cost_ok
    
    
    if self.bl and self.ok
      self.shiped = true
      self.lock = true
    end
  end
  
  def cost_u_ok
    self.total_cost_ok / self.ok_qte.to_f
  end
  
  def marge_u_ok
    self.marge_ok / self.ok_qte.to_f
  end
  
  def marge_ok
    self.ok_price_tot - self.total_cost_ok
  end
      
  def total_cost_ok
    var = 0.0
    self.p_sheet_line_stocks.each do |p_sheet_line_stock|
      var += p_sheet_line_stock.price_tot_ok
      
    end
    return var
  end
  
  
  def stock_is_ok
    if !self.stock_done
      if self.brut_product_needs_ok.size == self.p_sheet_line_stocks.group(:p_brut_product_id).length
      
        self.stock_done = true
        self.save
      
      end
    end
  end
  
  def update_stock
    if !self.externe and !self.stock_is_ok and self.shiped
      self.brut_product_needs_ok.each do |brut_index, brut_qte|
        p_brut_product = PBrutProduct.find(brut_index)
        qte_restante = brut_qte.to_f
      
        p_product_brut_stocks = PProductBrutStock.where(:p_brut_product_id => p_brut_product.id).where("qte_restant > 0.0").order("ok_at ASC").limit(100)
      
        if p_product_brut_stocks.sum(:qte_restant) >= qte_restante and self.p_sheet_line_stocks.where(:p_brut_product_id => p_brut_product.id).count == 0
      
          i = 0
          while qte_restante > 0.0
            p_product_brut_stock = p_product_brut_stocks[i]
        
            if qte_restante <= p_product_brut_stock.qte_restant
              qte_to_affect = qte_restante
          
            else
              qte_to_affect = p_product_brut_stock.qte_restant
            end
        
            a = self.p_sheet_line_stocks.build(:ok_at => self.ok_at,:ok => true, :p_brut_product_id => p_brut_product.id, :qte_ok => qte_to_affect, :price_ok => p_product_brut_stock.ok_price, :p_product_brut_stock_id => p_product_brut_stock.id)
    
          
            p_product_brut_stock.qte_restant = p_product_brut_stock.qte_restant.to_f - qte_to_affect
            p_product_brut_stock.save
            qte_restante = qte_restante - qte_to_affect
            i += 1
        
          end
      
        end
      
      end
    
      self.save
      self.stock_is_ok
    end
  end
  
  def brut_product_needs
    var = {}
    self.p_product.p_product_brut_products.each do |p_product_brut_product|
      var[p_product_brut_product.p_brut_product_id] = p_product_brut_product.qte.to_f * self.qte.to_f
    end
    return var
  end
  
  def brut_product_needs_ok
    var = {}
    self.p_product.p_product_brut_products.each do |p_product_brut_product|
      var[p_product_brut_product.p_brut_product_id] = p_product_brut_product.qte.to_f * self.ok_qte.to_f
    end
    return var
  end
  
  def generate_bl
    self.p_customer_sheet.generate_bl(self.id)
  end
  
  def validate_tank?
    if self.externe
      false
      self.p_tank = nil
    else
      true
    end
  end
  
  def validate_ok?
    if self.ok
      true
    else
      false
    end
  end
  
  def price_tot
    self.qte.to_f * self.price.to_f
  end 
  
  def tva
    0.2
  end
  
  def price_tot_ttc
    self.price_tot * (self.tva + 1.0)
  end
  
  
  def ok_price_tot
    self.ok_qte.to_f * self.ok_price.to_f
  end 
  
  def ok_price_tot_ttc
    self.ok_price_tot * (self.tva + 1.0)
  end
    
  
  def self.totals(sheet_lines)
    
    total = 0.0
    total_ttc = 0.0
    ok_total = 0.0
    ok_total_ttc = 0.0
    
    sheet_lines.each do |sheet_line|
      
      total += sheet_line.price_tot
      total_ttc += sheet_line.price_tot_ttc
      ok_total += sheet_line.ok_price_tot
      ok_total_ttc += sheet_line.ok_price_tot_ttc
    end
    
    return {
      :total => total,
      :total_ttc => total_ttc,
      :ok_total => ok_total,
      :ok_total_ttc => ok_total_ttc
    }
  end
  
end
