<commit_before># = Informations
#
# == License
#
# Ekylibre - Simple agricultural ERP
# Copyright (C) 2008-2009 <NAME>, <NAME>
# Copyright (C) 2010-2012 <NAME>
# Copyright (C) 2012-2016 <NAME>, <NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see http://www.gnu.org/licenses.
#
# == Table: intervention_parameters
#
#  created_at              :datetime         not null
#  creator_id              :integer
#  event_participation_id  :integer
#  group_id                :integer
#  id                      :integer          not null, primary key
#  intervention_id         :integer          not null
#  lock_version            :integer          default(0), not null
#  new_container_id        :integer
#  new_group_id            :integer
#  new_name                :string
#  new_variant_id          :integer
#  outcoming_product_id    :integer
#  position                :integer          not null
#  product_id              :integer
#  quantity_handler        :string
#  quantity_indicator_name :string
#  quantity_population     :decimal(19, 4)
#  quantity_unit_name      :string
#  quantity_value          :decimal(19, 4)
#  reference_name          :string           not null
#  type                    :string
#  updated_at              :datetime         not null
#  updater_id              :integer
#  variant_id              :integer
#  working_zone            :geometry({:srid=>4326, :type=>"multi_polygon"})
#

# An intervention output represents a product which is produced by the
# intervention. The output generate a product with the given quantity.
class InterventionOutput < InterventionProductParameter
  belongs_to :intervention, inverse_of: :outputs
  belongs_to :product, dependent: :destroy
  has_one :product_movement, as: :originator
  validates :variant, :quantity_population, presence: true

  after_commit do
    output = product
    output ||= variant.products.new unless output
    output.type = variant.matching_model.name
    output.born_at = intervention.started_at
    output.initial_born_at = output.born_at
    output.name = new_name unless new_name.blank?
    # output.attributes = product_attributes
    reading = readings.find_by(indicator_name: :shape)
    output.initial_shape = reading.value if reading
    output.save!

    movement = product_movement
    movement = build_product_movement(product: output) unless movement
    movement.delta = quantity_population
    movement.started_at = intervention.started_at if intervention
    movement.started_at ||= Time.zone.now - 1.hour
    movement.stopped_at = intervention.stopped_at if intervention
    movement.stopped_at ||= movement.started_at + 1.hour
    movement.save!

    update_columns(product_id: output.id) # , movement_id: movement.id)
    true
  end

  def earn_amount_computation
    options = { quantity: quantity_population, unit_name: product.unit_name }
    if product<commit_msg>Checks that InterventionOutput isn't destroyed before execute after_commit. Fixes #550.<commit_after># = Informations
#
# == License
#
# Ekylibre - Simple agricultural ERP
# Copyright (C) 2008-2009 <NAME>, <NAME>
# Copyright (C) 2010-2012 <NAME>
# Copyright (C) 2012-2016 <NAME>, <NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see http://www.gnu.org/licenses.
#
# == Table: intervention_parameters
#
#  created_at              :datetime         not null
#  creator_id              :integer
#  event_participation_id  :integer
#  group_id                :integer
#  id                      :integer          not null, primary key
#  intervention_id         :integer          not null
#  lock_version            :integer          default(0), not null
#  new_container_id        :integer
#  new_group_id            :integer
#  new_name                :string
#  new_variant_id          :integer
#  outcoming_product_id    :integer
#  position                :integer          not null
#  product_id              :integer
#  quantity_handler        :string
#  quantity_indicator_name :string
#  quantity_population     :decimal(19, 4)
#  quantity_unit_name      :string
#  quantity_value          :decimal(19, 4)
#  reference_name          :string           not null
#  type                    :string
#  updated_at              :datetime         not null
#  updater_id              :integer
#  variant_id              :integer
#  working_zone            :geometry({:srid=>4326, :type=>"multi_polygon"})
#

# An intervention output represents a product which is produced by the
# intervention. The output generate a product with the given quantity.
class InterventionOutput < InterventionProductParameter
  belongs_to :intervention, inverse_of: :outputs
  belongs_to :product, dependent: :destroy
  has_one :product_movement, as: :originator, dependent: :destroy
  validates :variant, :quantity_population, presence: true

  after_commit do
    unless destroyed?
      output = product
      output ||= variant.products.new unless output
      output.type = variant.matching_model.name
      output.born_at = intervention.started_at
      output.initial_born_at = output.born_at
      output.name = new_name unless new_name.blank?
      # output.attributes = product_attributes
      reading = readings.find_by(indicator_name: :shape)
      output.initial_shape = reading.value if reading
      output.save!

      movement = product_movement
      movement = build_product_movement(product: output) unless movement
      movement.delta = quantity_population
      movement.started_at = intervention.started_at if intervention
      movement.started_at ||= Time.zone.now - 1.hour
      movement.stopped_at = intervention.stopped_at if intervention
      movement.stopped_at ||= movement.started_at + 1.hour
      movement.save!

      update_columns(product_id: output.id) # , movement_id: movement.id)
      true
    end
  end

  def earn_amount_computation
    options = { quantity: quantity_population, unit_name: product.unit_name }
    if product
{'fraction_non_alphanumeric': 0.07170185631288083, 'fraction_numerical': 0.011194558594303529, 'mean_word_length': 2.8547241944292736, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4019489', 'n_tokens_mistral': 1997, 'n_tokens_neox': 1907, 'n_words': 613}