2016-08-08 12:52:30 +02:00
|
|
|
class ChampsService
|
2017-03-29 13:37:07 +02:00
|
|
|
class << self
|
2017-03-30 16:12:01 +02:00
|
|
|
def save_champs(champs, params)
|
2017-03-29 13:37:07 +02:00
|
|
|
fill_champs(champs, params)
|
|
|
|
|
|
|
|
champs.select(&:changed?).each(&:save)
|
2017-03-30 16:12:01 +02:00
|
|
|
end
|
2017-03-29 13:37:07 +02:00
|
|
|
|
2017-03-30 16:12:01 +02:00
|
|
|
def build_error_messages(champs)
|
|
|
|
champs.select(&:mandatory_and_blank?)
|
2017-10-11 17:32:45 +02:00
|
|
|
.map { |c| "Le champ #{c.libelle.truncate(200)} doit être rempli." }
|
2017-03-29 13:37:07 +02:00
|
|
|
end
|
|
|
|
|
2018-01-30 19:17:16 +01:00
|
|
|
def check_piece_justificative_files(champs)
|
|
|
|
champs.select do |champ|
|
2018-08-28 16:22:34 +02:00
|
|
|
champ.type_champ == TypeDeChamp.type_champs.fetch(:piece_justificative)
|
2018-10-01 14:20:14 +02:00
|
|
|
end.map(&:piece_justificative_file_errors).flatten
|
2018-01-30 19:17:16 +01:00
|
|
|
end
|
|
|
|
|
2017-03-29 13:37:07 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def fill_champs(champs, h)
|
2018-08-28 16:22:34 +02:00
|
|
|
datetimes, not_datetimes = champs.partition { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:datetime) }
|
2017-03-29 13:37:07 +02:00
|
|
|
|
2018-01-30 19:17:16 +01:00
|
|
|
not_datetimes.each do |c|
|
2018-08-28 16:22:34 +02:00
|
|
|
if c.type_champ == TypeDeChamp.type_champs.fetch(:piece_justificative) && h["champs"]["'#{c.id}'"].present?
|
2018-01-30 19:17:16 +01:00
|
|
|
c.piece_justificative_file.attach(h["champs"]["'#{c.id}'"])
|
|
|
|
else
|
|
|
|
c.value = h[:champs]["'#{c.id}'"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-29 13:37:07 +02:00
|
|
|
datetimes.each { |c| c.value = parse_datetime(c.id, h) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_datetime(champ_id, h)
|
|
|
|
"#{h[:champs]["'#{champ_id}'"]} #{extract_hour(champ_id, h)}:#{extract_minute(champ_id, h)}"
|
2016-08-08 12:52:30 +02:00
|
|
|
end
|
|
|
|
|
2017-03-29 13:37:07 +02:00
|
|
|
def extract_hour(champ_id, h)
|
|
|
|
h[:time_hour]["'#{champ_id}'"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_minute(champ_id, h)
|
|
|
|
h[:time_minute]["'#{champ_id}'"]
|
|
|
|
end
|
2016-08-08 12:52:30 +02:00
|
|
|
end
|
2016-12-26 17:55:31 +01:00
|
|
|
end
|