class StockMovement < ApplicationRecord
  
  validates :date, :presence => true
  validates :description, :presence => true
  
  acts_as_sorting :fields =>{
    :id => {:name => "Id"},
    :description => {:name => "Description"},
    :date => {:name => "Date"},
    :movement_type => {:name => "Type de mouvement"},
    :stock_done => {:name => "Stock ok ?", :as => :boolean},
    :actions => {:name => "Actions", :reorder => false}
    
    
  }
  
  has_many :stock_movement_lines, :dependent => :destroy
  
  has_many :line_stocks, :as => :stockable
  
  accepts_nested_attributes_for :stock_movement_lines, allow_destroy: true
  
  acts_as_caching :fields => [:tot_amount_ht]
  
  
  def generate_stock
    
    if self.movement_type == "cred"
      self.stock_movement_lines.each do |pl|
        if pl.p_product_ref
          LineStock.create(:dluo => pl.dluo, :date => self.date, :p_product_ref => pl.p_product_ref, :description => "Entrée en stock par mouvement de stock manuel", :qte => pl.qte, :price_ht => pl.tot_amount_ht, :stock_movement_line => pl, :stockable => self)
      
        end
      
      
      end
    
      self.stock_done = true
      self.save
      return true
  
    else
    
      if self.test_if_stock_ok?
      
        self.stock_movement_lines.each do |pl|
          if pl.p_product_ref
            decr_line_stock = LineStock.create(:date => self.date, :p_product_ref => pl.p_product_ref, :qte => pl.qte*-1, :description => "Attribution à une sortie de stock manuelle", :price_ht => 0.0, :stock_movement_line => pl, :stockable => self)
            puts "a"
            puts decr_line_stock.id
            puts "b"
      
          
            price = 0.0
            line_stock_usages = []
          
            qte_to_affect = pl.qte
          
            LineStock.where(:p_product_ref_id => pl.p_product_ref_id).where("date <= ?", self.date).where("qte_available > 0.0").each do |ls|
              if qte_to_affect <= ls.qte_available
                qte_here = qte_to_affect
              else
                qte_here = ls.qte_available
              
              end
            
              qte_to_affect = qte_to_affect - qte_here
            
              lsu = LineStockUsage.create(:qte => qte_here, :line_stock => ls, :dest_line_stock_id => decr_line_stock.id)
              line_stock_usages << lsu 
            
              price += lsu.price_ht
              ls.save
            
              break if qte_to_affect == 0.0
            
              #price_u_ht
            
            end
            decr_line_stock.price_ht = price * -1
            decr_line_stock.save
          
          
         
      
          end
    
        end
        self.stock_done = true
        self.save
        return true
  
      
      end
    end
  
  end
  
  def test_if_stock_ok?
    to_r = true
    self.stock_movement_lines.each do |pl|
      if pl.p_product_ref
        if LineStock.where(:p_product_ref_id => pl.p_product_ref_id).sum(:qte_available) >= pl.qte
          
        else
          to_r = false
        end

        
      end
      
    end
    
    return to_r
  end
  
end
