Class: InterventionWorkingPeriod

Inherits:
Ekylibre::Record::Base show all
Includes:
PeriodicCalculable
Defined in:
app/models/intervention_working_period.rb

Overview

Informations

License

Ekylibre - Simple agricultural ERP Copyright (C) 2008-2009 Brice Texier, Thibaud Merigon Copyright (C) 2010-2012 Brice Texier Copyright (C) 2012-2019 Brice Texier, David Joulin

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 www.gnu.org/licenses.

Table: intervention_working_periods

created_at                    :datetime         not null
creator_id                    :integer
duration                      :integer          not null
id                            :integer          not null, primary key
intervention_id               :integer
intervention_participation_id :integer
lock_version                  :integer          default(0), not null
nature                        :string
started_at                    :datetime         not null
stopped_at                    :datetime         not null
updated_at                    :datetime         not null
updater_id                    :integer

Constant Summary

Constants included from PeriodicCalculable

PeriodicCalculable::PARAMETERS

Instance Method Summary collapse

Methods inherited from Ekylibre::Record::Base

#already_updated?, #check_if_destroyable?, #check_if_updateable?, columns_definition, #customizable?, customizable?, #customized?, #destroyable?, #editable?, has_picture, #human_attribute_name, nomenclature_reflections, #old_record, #others, refers_to, #unsuppress, #updateable?

Methods included from Userstamp::Stampable

included

Methods included from Userstamp::Stamper

included

Instance Method Details

#duration_gapObject


174
175
176
# File 'app/models/intervention_working_period.rb', line 174

def duration_gap
  (stopped_at - started_at) / 3600
end

#first_activity_production_stopped_onObject


121
122
123
124
# File 'app/models/intervention_working_period.rb', line 121

def first_activity_production_stopped_on
  targets = intervention&.targets || []
  targets.map { |t| t.activity_production&.stopped_on }.compact.min
end

#gap_with_period?(working_period) ⇒ Boolean

Returns:

  • (Boolean)

170
171
172
# File 'app/models/intervention_working_period.rb', line 170

def gap_with_period?(working_period)
  stopped_at < working_period.started_at
end

#hide?Boolean

Returns:

  • (Boolean)

139
140
141
# File 'app/models/intervention_working_period.rb', line 139

def hide?
  started_at.to_i == stopped_at.to_i
end

#hours_gapObject


143
144
145
# File 'app/models/intervention_working_period.rb', line 143

def hours_gap
  (gap[:day] * 24) + gap[:hour]
end

#last_activity_production_started_onObject


116
117
118
119
# File 'app/models/intervention_working_period.rb', line 116

def last_activity_production_started_on
  targets = intervention&.targets || []
  targets.map { |t| t.activity_production&.started_on }.compact.max
end

#minutes_gapObject


147
148
149
# File 'app/models/intervention_working_period.rb', line 147

def minutes_gap
  gap[:minute]
end

#next_periodObject


158
159
160
161
162
163
# File 'app/models/intervention_working_period.rb', line 158

def next_period
  return if last?

  next_index = index + 1
  intervention_participation.working_periods.fetch(next_index)
end

#pause_next?Boolean

Returns:

  • (Boolean)

165
166
167
168
# File 'app/models/intervention_working_period.rb', line 165

def pause_next?
  return false if last?
  gap_with_period?(next_period)
end

#previous_periodObject


151
152
153
154
155
156
# File 'app/models/intervention_working_period.rb', line 151

def previous_period
  return if first?

  previous_index = index - 1
  intervention_participation.working_periods.fetch(previous_index)
end

#validate_started_stopped_atObject


126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/models/intervention_working_period.rb', line 126

def validate_started_stopped_at
  activity_started_on = last_activity_production_started_on
  activity_stopped_on = first_activity_production_stopped_on
  if activity_started_on.present?
    errors.add(:started_at, :posterior, to: activity_started_on) if started_at < activity_started_on
    errors.add(:stopped_at, :posterior, to: activity_started_on) if stopped_at < activity_started_on
  end
  if activity_stopped_on.present?
    errors.add(:started_at, :inferior, to: activity_started_on) if started_at > activity_stopped_on
    errors.add(:stopped_at, :inferior, to: activity_started_on) if stopped_at > activity_stopped_on
  end
end