class Partition < ApplicationRecord
  belongs_to :admin

  has_many :partition_lines, :dependent => :destroy
  accepts_nested_attributes_for :partition_lines, allow_destroy: true

  acts_as_sorting :fields => {
    :id => {:name => "Id", :reorder => true},
    :created_at => {:name => "Date de création", :reorder => true},
    :date => {:name => "Date d'exécution", :reorder => true},
    :stock_ok => {:name => "Executé ?", :reorder => true, :as => :boolean},



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

  has_many :line_stocks, :as => :stockable
  
  
  def cancel_execution
    #Fonction Admin
    
    self.line_stocks.each do |ls|
      ls.destroy
    end    
    
    self.stock_ok = false
    self.save
  end
  


  def ecart_warning
    r = false
    self.partition_lines.each do |pl|
      r = true if pl.dif_weight.to_f != 0.0
    end

    return r
  end

  def stock_warning
    r = false
    self.partition_lines.each do |pl|
      r = true if !pl.stock_available?
    end

    return r
  end

  def ok_to_execute
    if !self.stock_ok and !self.ecart_warning and !self.stock_warning
      true
    else
      false

    end
  end

  def generate_stock
    if self.ok_to_execute

      self.partition_lines.each do |pl|
          new_line_stock_price = 0.0
          
          decr_line_stock = LineStock.create(:date => self.date, :p_product_ref => pl.p_product_ref_from, :qte => pl.qte_from*-1, :description => self.description, :price_ht => 0.0, :partition_line_id => pl.id, :stockable => self)
          puts "a"
          puts decr_line_stock.id
          puts "b"


          price = 0.0
          line_stock_usages = []

          qte_to_affect = pl.qte_from

          LineStock.where(:p_product_ref_id => pl.p_product_ref_from.id).where("qte_available > 0.0").each do |ls| #.where("date <= ?", self.date) à rajouter lorsqu'on bloquera les dates...
            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.new(:qte => qte_here, :line_stock => ls, :dest_line_stock_id => decr_line_stock.id)
            
            if lsu.save
              
            else
              raise
            end
            
            
            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
          
          
          new_line_stock_price += (decr_line_stock.price_ht * -1)
          
          LineStock.create(:date => self.date, :p_product_ref => pl.p_product_ref_to, :description => self.description, :qte => pl.qte_to, :price_ht => new_line_stock_price, :partition_line_id => pl.id, :stockable => self)
          
          
      end

      self.stock_ok = true
      self.save
      return true
    end
  end

end
