use Modern::Perl;
+use MIME::Base64 qw( decode_base64 );
+use MIME::Types;
+
use Koha::Database;
+use Koha::DateUtils qw( dt_from_string );
use base qw(Koha::Object);
use Koha::ERM::Agreement::Periods;
use Koha::ERM::Agreement::UserRoles;
+use Koha::ERM::Agreement::Licenses;
+use Koha::ERM::Agreement::Relationships;
+use Koha::ERM::Agreement::Documents;
+use Koha::ERM::EHoldings::Package::Agreements;
=head1 NAME
return Koha::ERM::Agreement::UserRoles->_new_from_dbic($user_roles_rs);
}
+=head3 agreement_licenses
+
+Returns the agreement_licenses for this agreement
+
+=cut
+
+sub agreement_licenses {
+ my ( $self, $agreement_licenses ) = @_;
+
+ if ( $agreement_licenses ) {
+ my $schema = $self->_result->result_source->schema;
+ $schema->txn_do(
+ sub {
+ $self->agreement_licenses->delete;
+
+ for my $agreement_license (@$agreement_licenses) {
+ $self->_result->add_to_erm_agreement_licenses($agreement_license);
+ }
+ }
+ );
+ }
+ my $agreement_licenses_rs = $self->_result->erm_agreement_licenses;
+ return Koha::ERM::Agreement::Licenses->_new_from_dbic($agreement_licenses_rs);
+}
+
+=head3 agreement_relationships
+
+Returns the agreement relationships of this agreement
+
+=cut
+
+sub agreement_relationships {
+ my ( $self, $relationships ) = @_;
+
+ if ( $relationships ) {
+ my $schema = $self->_result->result_source->schema;
+ # FIXME naming - is "back link" ok?
+ my $back_links = {
+ 'supersedes' => 'is-superseded-by',
+ 'is-superseded-by' => 'supersedes',
+ 'provides_post-cancellation_access_for' => 'has-post-cancellation-access-in',
+ 'has-post-cancellation-access-in' => 'provides_post-cancellation_access_for',
+ 'tracks_demand-driven_acquisitions_for' => 'has-demand-driven-acquisitions-in',
+ 'has-demand-driven-acquisitions-in' => 'tracks_demand-driven_acquisitions_for',
+ 'has_backfile_in' => 'has_frontfile_in',
+ 'has_frontfile_in' => 'has_backfile_in',
+ 'related_to' => 'related_to',
+ };
+ $schema->txn_do(
+ sub {
+ $self->agreement_relationships->delete;
+ $self->agreement_back_relationships->delete;
+
+ for my $relationship (@$relationships) {
+ $self->_result->add_to_erm_agreement_relationships_agreements($relationship);
+ my $back_link = {
+ agreement_id => $relationship->{related_agreement_id},
+ related_agreement_id => $self->agreement_id,
+ relationship => $back_links->{$relationship->{relationship}},
+ notes => $relationship->{notes}, # FIXME Is it correct, do we keep the note here?
+ };
+ $self->_result->add_to_erm_agreement_relationships_related_agreements($back_link);
+ }
+ }
+ );
+ }
+ my $related_agreements_rs = $self->_result->erm_agreement_relationships_agreements;
+ return Koha::ERM::Agreement::Relationships->_new_from_dbic($related_agreements_rs);
+}
+
+=head3 agreement_back_relationships
+
+# FIXME Naming - how is it called?
+Returns the reverse relationship
+
+=cut
+
+sub agreement_back_relationships {
+ my ( $self ) = @_;
+ my $rs = $self->_result->erm_agreement_relationships_related_agreements;
+ return Koha::ERM::Agreement::Relationships->_new_from_dbic($rs);
+}
+
+=head3 documents
+
+Returns the documents for this agreement
+
+=cut
+
+sub documents {
+ my ( $self, $documents ) = @_;
+
+ if ($documents) {
+ my $schema = $self->_result->result_source->schema;
+ $schema->txn_do(
+ sub {
+ my $existing_documents = $self->documents;
+
+ # FIXME Here we are not deleting all the documents before recreating them, like we do for other related resources.
+ # As we do not want the content of the documents to transit over the network we need to use the document_id (and allow it in the API spec)
+ # to distinguish from each other
+ # Delete all the documents that are not part of the PUT request
+ my $modified_document_ids = [ map { $_->{document_id} || () } @$documents ];
+ $self->documents->search(
+ {
+ @$modified_document_ids
+ ? (
+ document_id => {
+ '-not_in' => $modified_document_ids
+ }
+ )
+ : ()
+ }
+ )->delete;
+
+ for my $document (@$documents) {
+ if ( $document->{document_id} ) {
+ # The document already exists in DB
+ $existing_documents->find( $document->{document_id} )
+ ->set(
+ {
+ file_description => $document->{file_description},
+ physical_location => $document->{physical_location},
+ uri => $document->{uri},
+ notes => $document->{notes},
+ }
+ )->store;
+ }
+ else {
+ # Creating a whole new document
+ my $file_content = decode_base64( $document->{file_content} );
+ my $mt = MIME::Types->new();
+ $document->{file_type} = $mt->mimeTypeOf( $document->{file_name} );
+ $document->{uploaded_on} //= dt_from_string;
+ $document->{file_content} = $file_content;
+ $self->_result->add_to_erm_agreement_documents( $document);
+ }
+ }
+ }
+ );
+ }
+ my $documents_rs = $self->_result->erm_agreement_documents;
+ return Koha::ERM::Agreement::Documents->_new_from_dbic($documents_rs);
+}
+
+=head3 agreement_packages
+
+Return the local packages for this agreement (and the other ones that have an entry locally)
+
+=cut
+
+sub agreement_packages {
+ my ( $self ) = @_;
+ my $packages_agreements_rs = $self->_result->erm_eholdings_packages_agreements;
+ return Koha::ERM::EHoldings::Package::Agreements->_new_from_dbic($packages_agreements_rs);
+}
+
=head2 Internal methods
=head3 _type