-
Notifications
You must be signed in to change notification settings - Fork 154
/
equipment.rb
378 lines (317 loc) · 12.2 KB
/
equipment.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# = Informations
#
# == License
#
# Ekylibre - Simple agricultural ERP
# Copyright (C) 2008-2009 Brice Texier, Thibaud Merigon
# Copyright (C) 2010-2012 Brice Texier
# Copyright (C) 2012-2017 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 http://www.gnu.org/licenses.
#
# == Table: products
#
# address_id :integer
# birth_date_completeness :string
# birth_farm_number :string
# born_at :datetime
# category_id :integer not null
# codes :jsonb
# country :string
# created_at :datetime not null
# creator_id :integer
# custom_fields :jsonb
# dead_at :datetime
# default_storage_id :integer
# derivative_of :string
# description :text
# end_of_life_reason :string
# father_country :string
# father_identification_number :string
# father_variety :string
# filiation_status :string
# first_calving_on :datetime
# fixed_asset_id :integer
# id :integer not null, primary key
# identification_number :string
# initial_born_at :datetime
# initial_container_id :integer
# initial_dead_at :datetime
# initial_enjoyer_id :integer
# initial_father_id :integer
# initial_geolocation :geometry({:srid=>4326, :type=>"st_point"})
# initial_mother_id :integer
# initial_movement_id :integer
# initial_owner_id :integer
# initial_population :decimal(19, 4) default(0.0)
# initial_shape :geometry({:srid=>4326, :type=>"multi_polygon"})
# lock_version :integer default(0), not null
# member_variant_id :integer
# mother_country :string
# mother_identification_number :string
# mother_variety :string
# name :string not null
# nature_id :integer not null
# number :string not null
# origin_country :string
# origin_identification_number :string
# originator_id :integer
# parent_id :integer
# person_id :integer
# picture_content_type :string
# picture_file_name :string
# picture_file_size :integer
# picture_updated_at :datetime
# team_id :integer
# tracking_id :integer
# type :string
# updated_at :datetime not null
# updater_id :integer
# uuid :uuid
# variant_id :integer not null
# variety :string not null
# work_number :string
#
class Equipment < Matter
include Attachable
has_many :components, class_name: 'ProductNatureVariantComponent', through: :variant
has_many :part_replacements, class_name: 'InterventionInput', foreign_key: :assembly_id
refers_to :variety, scope: :equipment
def best_activity_production(options = {})
at = options[:at] || Time.zone.now
ActivityProduction.where(support: groups_at(at)).at(at).first || super
end
def tractor?
variety == :tractor
end
def self_prepelled_equipment?
variety == :self_prepelled_equipment
end
##################################################
### Statuses #####################################
def status
return :stop if dead_at?
return :caution if issues.where(state: :opened).any?
:go
end
def wear_status(component = nil)
if component.nil?
# To take into account the tear&wear of the components.
comp_status = variant.components.map { |c| wear_status(c) }
# To take into account the tear&wear of the equipment itself.
progresses = [lifespan_progress, working_lifespan_progress]
worn_out = progresses.any? { |prog| prog >= 1 }
almost_worn_out = progresses.any? { |prog| prog >= 0.85 }
return :stop if comp_status.include?(:stop) || worn_out
return :caution if comp_status.include?(:caution) || almost_worn_out
return :go
end
# If we're only working on a single component.
life_progress = lifespan_progress_of(component)
work_progress = working_lifespan_progress_of(component)
progresses = [life_progress, work_progress]
return :stop if progresses.any? { |prog| prog >= 1 }
return :caution if progresses.any? { |prog| prog >= 0.85 }
:go
end
##################################################
### Total lifespans values fetched from variant ##
def total_lifespan
variant.lifespan if has_indicator?(:lifespan)
end
def total_working_lifespan
variant.working_lifespan if has_indicator?(:working_lifespan)
end
##################################################
### Used up lifespan #############################
def current_life
(Time.zone.today - born_at.to_date).in(:day)
end
def current_work_life
working_duration.in(:day)
end
##################################################
### Remaining lifespans (total - gone) ###########
def remaining_lifespan
return nil unless total_lifespan
total_lifespan - current_life
end
def remaining_working_lifespan
return nil unless total_working_lifespan
total_working_lifespan - current_work_life
end
##################################################
### Lifespan progress in [0-1] and percents ######
#### [0 - 1]
def lifespan_progress
return 0 unless current_life && total_lifespan
current_life / total_lifespan
end
def working_lifespan_progress
return 0 unless current_work_life && total_working_lifespan
current_work_life / total_working_lifespan
end
#### [0 - 100%]
def lifespan_progress_percent
lifespan_progress * 100
end
def working_lifespan_progress_percent
working_lifespan_progress * 100
end
##################################################
### Work duration ################################
### Return working duration using the appropriate calculation.
# Either calculates from daily_average_working_time or from
# the time spent in interventions.
def working_duration(since = nil)
start = since || born_at
work_duration = working_duration_from_average(since: start)
work_duration ||= working_duration_from_interventions(since: start)
work_duration.to_f.in_second
end
### Returns working duration as the sum of the time spent
# working in interventions.
def working_duration_from_interventions(options = {})
role = options[:as] || :tool
periods = InterventionWorkingPeriod.with_intervention_parameter(role, self)
periods = periods.where('started_at >= ?', options[:since]) if options[:since]
periods = periods.of_campaign(options[:campaign]) if options[:campaign]
periods.sum(:duration).in_second
end
### Returns working duration as a product of the average work
# time per day and the number of days worked.
def working_duration_from_average(options = {})
return nil unless has_indicator?(:daily_average_working_time)
duration = (Time.zone.today - options[:since].to_date)
average = variant.daily_average_working_time
(average * duration.in_day).in_second
end
#######################################################
### Components ########################################
##################################################
###### Replacements ##############################
# Returns the list of replacements
def replacements_of(component)
raise ArgumentError, 'Incompatible component' unless component.product_nature_variant == variant
part_replacements.where(component: component.self_and_parents)
end
def replaced_at(component, since = nil)
replacement = last_replacement(component)
return replacement.intervention.stopped_at if replacement
since
end
def last_replacement(component)
replacements_of(component).joins(:intervention).order('interventions.stopped_at DESC').first
end
##################################################
###### Total lifespans values ####################
def total_lifespan_of(component)
comp_variant = component.part_product_nature_variant
return nil unless comp_variant && comp_variant.has_indicator?(:lifespan)
comp_variant.lifespan
end
def total_working_lifespan_of(component)
comp_variant = component.part_product_nature_variant
return nil unless comp_variant && comp_variant.has_indicator?(:working_lifespan)
comp_variant.working_lifespan
end
##################################################
###### Used up lifespan ##########################
def current_life_of(component)
(Time.zone.now - replaced_at(component, born_at)).to_f.in_second
end
def current_work_life_of(component)
working_duration(replaced_at(component, born_at)).to_f.in_second
end
##################################################
######### Remaining lifespan values ##############
def remaining_lifespan_of(component)
return nil unless total_lifespan_of(component)
total_lifespan_of(component) - current_life_of(component)
end
def remaining_working_lifespan_life_of(component)
return nil unless total_working_lifespan_of(component)
total_working_life_of(component) - current_work_life_of(component)
end
##################################################
###### Lifespan progress in [0-1] and percents ###
####### [0 - 1]
def lifespan_progress_of(component)
return 0 unless current_life_of(component) && total_lifespan_of(component)
current_life_of(component) / total_lifespan_of(component)
end
def working_lifespan_progress_of(component)
return 0 unless current_work_life_of(component) && total_working_lifespan_of(component)
current_work_life_of(component) / total_working_lifespan_of(component)
end
####### [0 - 100%]
def lifespan_progress_percent
lifespan_progress_of(component) * 100
end
def working_lifespan_progress_percent
working_lifespan_progress_of(component) * 100
end
#######################################################
### Notifications #####################################
##################################################
###### Equipment #################################
def alert_life
User.notify_administrators(
:equipment_is_at_end_of_life,
interpolations(remaining_lifespan),
target: self,
level: :warning
)
end
def alert_work
User.notify_administrators(
:equipment_is_worn_out,
interpolations(remaining_working_lifespan),
target: self,
level: :warning
)
end
##################################################
###### Component #################################
def alert_component_life(component)
User.notify_administrators(
:equipment_component_is_at_end_of_life,
interpolations_for(component, remaining_lifespan_of(component)),
target: self,
level: :warning
)
end
def alert_component_work(component)
User.notify_administrators(
:equipment_component_is_worn_out,
interpolations_for(component, remaining_working_lifespan_of(component)),
target: self,
level: :warning
)
end
protected
def interpolations(lifespan)
{
name: name,
remaining_time: lifespan.in(:hour).round(2).l(precision: 0)
}
end
def interpolations_for(component, lifespan)
{
equipment_name: name,
component_name: component.name,
remaining_time: lifespan.in(:hour).round(2).l(precision: 0)
}
end
end