# -*- coding: utf-8 -*-
### Class that will automatically aggregate confidential Entry of the same Stoff
class AggregatingArray < GkCollectingArray
  attr_reader :has_aggregated

  def initialize
    @has_aggregated = false
    super
  end

  def push(entry)
#    entry['vfracht'] ||= 0.0 unless entry.geheimk.empty?
    prev = self.last
    if !entry.geheimk.empty? && prev && !prev.geheimk.empty? && entry['stoffgruppe'].eql?(prev['stoffgruppe'])
      @has_aggregated = true
#      puts "DBG prev before: #{prev.inspect}"
#      puts "DBG freis before: #{freis.inspect}"
      prev['jfracht'] += entry['jfracht']
      unless entry['vfracht'].nil?
        if prev['vfracht'].nil?
         prev['vfracht'] = 0.0
         prev.aggr_vfracht = true
         puts "aggr_vfracht"
        end
        prev['vfracht'] += entry['vfracht']
      end
      prev.geheimk |= entry.geheimk
#      puts "DBG prev after: #{prev.inspect}"

    else
      super(entry)
    end
  end
end