2015-09-21 17:59:03 +02:00
|
|
|
class PieceJustificative < ActiveRecord::Base
|
2017-10-24 18:12:25 +02:00
|
|
|
belongs_to :dossier, touch: true
|
2015-09-21 17:59:03 +02:00
|
|
|
belongs_to :type_de_piece_justificative
|
2016-04-20 16:51:57 +02:00
|
|
|
has_one :commentaire
|
2016-01-18 16:20:51 +01:00
|
|
|
|
2016-03-17 17:33:38 +01:00
|
|
|
belongs_to :user
|
|
|
|
|
2016-06-08 16:45:18 +02:00
|
|
|
delegate :api_entreprise, :libelle, :order_place, to: :type_de_piece_justificative
|
2016-01-18 16:20:51 +01:00
|
|
|
|
2015-09-21 17:59:03 +02:00
|
|
|
alias_attribute :type, :type_de_piece_justificative_id
|
2016-01-18 16:20:51 +01:00
|
|
|
|
2015-09-21 17:59:03 +02:00
|
|
|
mount_uploader :content, PieceJustificativeUploader
|
2016-11-25 12:07:01 +01:00
|
|
|
validates :content, :file_size => {:maximum => 20.megabytes}
|
2016-04-20 16:51:57 +02:00
|
|
|
validates :content, presence: true, allow_blank: false, allow_nil: false
|
2015-09-21 17:59:03 +02:00
|
|
|
|
2016-12-26 18:37:27 +01:00
|
|
|
after_save :internal_notification, if: Proc.new { !dossier.nil? }
|
2016-12-26 11:42:02 +01:00
|
|
|
|
2017-10-05 16:10:00 +02:00
|
|
|
scope :updated_since?, -> (date) { where('pieces_justificatives.updated_at > ?', date) }
|
|
|
|
|
2015-09-21 17:59:03 +02:00
|
|
|
def empty?
|
|
|
|
content.blank?
|
|
|
|
end
|
2016-02-26 13:59:03 +01:00
|
|
|
|
2017-01-03 15:12:49 +01:00
|
|
|
def libelle
|
|
|
|
if type_de_piece_justificative.nil?
|
|
|
|
return content.to_s
|
|
|
|
else
|
|
|
|
type_de_piece_justificative.libelle
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-26 13:59:03 +01:00
|
|
|
def content_url
|
2016-05-18 11:43:32 +02:00
|
|
|
unless content.url.nil?
|
|
|
|
if Features.remote_storage
|
|
|
|
(RemoteDownloader.new content.filename).url
|
|
|
|
else
|
2016-05-24 18:35:25 +02:00
|
|
|
(LocalDownloader.new content.path,
|
2017-06-12 13:49:51 +02:00
|
|
|
(type_de_piece_justificative.nil? ? content.original_filename : type_de_piece_justificative.libelle)).url
|
2016-05-13 16:08:51 +02:00
|
|
|
end
|
2016-02-26 13:59:03 +01:00
|
|
|
end
|
|
|
|
end
|
2016-04-20 16:51:57 +02:00
|
|
|
|
|
|
|
def self.accept_format
|
|
|
|
" application/pdf,
|
|
|
|
application/msword,
|
|
|
|
application/vnd.openxmlformats-officedocument.wordprocessingml.document,
|
|
|
|
application/vnd.ms-excel,
|
|
|
|
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,
|
|
|
|
application/vnd.ms-powerpoint,
|
|
|
|
application/vnd.openxmlformats-officedocument.presentationml.presentation,
|
|
|
|
application/vnd.oasis.opendocument.text,
|
|
|
|
application/vnd.oasis.opendocument.presentation,
|
2016-06-06 16:38:14 +02:00
|
|
|
application/vnd.oasis.opendocument.spreadsheet,
|
|
|
|
image/png,
|
|
|
|
image/jpeg
|
2016-04-20 16:51:57 +02:00
|
|
|
"
|
|
|
|
end
|
2016-12-26 11:42:02 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def internal_notification
|
2017-12-04 16:17:15 +01:00
|
|
|
unless self.type_de_piece_justificative.nil? && dossier.state == 'brouillon'
|
2016-12-26 14:38:00 +01:00
|
|
|
NotificationService.new('piece_justificative', self.dossier.id, self.libelle).notify
|
|
|
|
end
|
2016-12-26 11:42:02 +01:00
|
|
|
end
|
2015-09-21 17:59:03 +02:00
|
|
|
end
|