<commit_before># == License
# Ekylibre - Simple agricultural ERP
# Copyright (C) 2014 <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/>.
#

module Backend
  class InterventionParticipationsController < Backend::BaseController
    manage_restfully only: %i[create update destroy]

    def index
      @worked_on = if params[:worked_on].blank?
                     first_participation = current_user.intervention_participations.unprompted.order(created_at: :desc).first
                     first_participation.present? ? first_participation.created_at : Time.zone.today
                   else
                     params[:worked_on].to_date
                   end
    end

    # Creates an intervention from intervention participation and redirects to an edit form for
    # the newly created intervention.
    def convert
      return unless intervention_participation = find_and_check
      begin
        if intervention = intervention_participation.convert!(params.slice(:procedure_name, :working_width))
          redirect_to edit_backend_intervention_path(intervention)
        elsif current_user.intervention_participations.unprompted.any?
          redirect_to backend_intervention_participations_path(worked_on: params[:worked_on])
        else
          redirect_to backend_interventions_path
        end
      rescue StandardError => e
        notify_error(e.message)
        redirect_to backend_intervention_participations_path(worked_on: params[:worked_on])
      end
    end

    def create
      participation = InterventionParticipation.find_or_initialize_by(
        product_id: permitted_params[:product_id],
        intervention_id: permitted_params[:intervention_id]
      )

      working_periods_ids = participation.working_periods.map(&:id)
      params_ids = permitted_params[:working_periods_attributes].to_h.map { |param| param.second['id'].to_i }

      working_periods_to_destroy = working_periods_ids - params_ids
      InterventionWorkingPeriod.where(id: working_periods_to_destroy).destroy_all

      permitted_params[:working_periods_attributes].values.each do |working_period_params|
        nature = working_period_params[:nature]
        started_at = Time.strptime(working_period_params[:started_at], t('time.formats.full'))
        stopped_at = Time.strptime(working_period_params[:stopped_at], t('time.formats.full'))

        next if nature.to_sym == :pause

        if !working_period_params[:id].nil?
          working_period = participation.working_periods.find(working_period_params[:id])
          working_period.started_at = started_at
          working_period.stopped_at = stopped_at
          working_period.save
        else
          participation.working_periods.create!(<commit_msg>Fix validation error create participation<commit_after># == License
# Ekylibre - Simple agricultural ERP
# Copyright (C) 2014 <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/>.
#

module Backend
  class InterventionParticipationsController < Backend::BaseController
    manage_restfully only: %i[create update destroy]

    def index
      @worked_on = if params[:worked_on].blank?
                     first_participation = current_user.intervention_participations.unprompted.order(created_at: :desc).first
                     first_participation.present? ? first_participation.created_at : Time.zone.today
                   else
                     params[:worked_on].to_date
                   end
    end

    # Creates an intervention from intervention participation and redirects to an edit form for
    # the newly created intervention.
    def convert
      return unless intervention_participation = find_and_check
      begin
        if intervention = intervention_participation.convert!(params.slice(:procedure_name, :working_width))
          redirect_to edit_backend_intervention_path(intervention)
        elsif current_user.intervention_participations.unprompted.any?
          redirect_to backend_intervention_participations_path(worked_on: params[:worked_on])
        else
          redirect_to backend_interventions_path
        end
      rescue StandardError => e
        notify_error(e.message)
        redirect_to backend_intervention_participations_path(worked_on: params[:worked_on])
      end
    end

    def create
      participation = InterventionParticipation.find_or_initialize_by(
        product_id: permitted_params[:product_id],
        intervention_id: permitted_params[:intervention_id]
      )

      if participation.new_record?
        participation.state = :done
        participation.save
      end

      working_periods_ids = participation.working_periods.map(&:id)
      params_ids = permitted_params[:working_periods_attributes].to_h.map { |param| param.second['id'].to_i }

      working_periods_to_destroy = working_periods_ids - params_ids
      InterventionWorkingPeriod.where(id: working_periods_to_destroy).destroy_all

      permitted_params[:working_periods_attributes].values.each do |working_period_params|
        nature = working_period_params[:nature]
        started_at = Time.strptime(working_period_params[:started_at], t('time.formats.full'))
        stopped_at = Time.strptime(working_period_params[:stopped_at], t('time.formats.full'))

        next if nature.to_sym == :pause

        if !working_period_params[:id].nil?
          working_period = participation.working_periods.find(working_period_params[:id])
          working_period.started_at = started_at
          working_period.stopped_at = stopped_at
          working_period.save
        else
          participation.working_periods.create!(
{'fraction_non_alphanumeric': 0.06904656970765388, 'fraction_numerical': 0.0014690759512266784, 'mean_word_length': 3.484848484848485, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '4153580', 'n_tokens_mistral': 1853, 'n_tokens_neox': 1759, 'n_words': 465}