1 package Koha::ERM::Agreement;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use MIME::Base64 qw( decode_base64 );
24 use Koha::DateUtils qw( dt_from_string );
26 use Koha::Acquisition::Bookseller;
28 use base qw(Koha::Object);
30 use Koha::ERM::Agreement::Periods;
31 use Koha::ERM::Agreement::Licenses;
32 use Koha::ERM::Agreement::Relationships;
33 use Koha::ERM::UserRoles;
34 use Koha::ERM::Documents;
35 use Koha::ERM::EHoldings::Package::Agreements;
39 Koha::ERM::Agreement - Koha ErmAgreement Object class
49 Returns the periods for this agreement
54 my ( $self, $periods ) = @_;
57 my $schema = $self->_result->result_source->schema;
60 $self->periods->delete;
62 for my $period (@$periods) {
63 $self->_result->add_to_erm_agreement_periods($period);
69 my $periods_rs = $self->_result->erm_agreement_periods;
70 return Koha::ERM::Agreement::Periods->_new_from_dbic($periods_rs);
75 Returns the user roles for this agreement
80 my ( $self, $user_roles ) = @_;
83 my $schema = $self->_result->result_source->schema;
86 $self->user_roles->delete;
88 for my $user_role (@$user_roles) {
89 $self->_result->add_to_erm_user_roles($user_role);
94 my $user_roles_rs = $self->_result->erm_user_roles;
95 return Koha::ERM::UserRoles->_new_from_dbic($user_roles_rs);
98 =head3 agreement_licenses
100 Returns the agreement_licenses for this agreement
104 sub agreement_licenses {
105 my ( $self, $agreement_licenses ) = @_;
107 if ( $agreement_licenses ) {
108 my $controlling = grep { $_->{status} eq 'controlling' } @$agreement_licenses;
109 if ( $controlling > 1 ) {
110 Koha::Exceptions::DuplicateObject->throw(
111 "Only one controlling license can exist for a given agreement");
114 my $schema = $self->_result->result_source->schema;
117 $self->agreement_licenses->delete;
119 for my $agreement_license (@$agreement_licenses) {
120 $self->_result->add_to_erm_agreement_licenses($agreement_license);
125 my $agreement_licenses_rs = $self->_result->erm_agreement_licenses;
126 return Koha::ERM::Agreement::Licenses->_new_from_dbic($agreement_licenses_rs);
129 =head3 agreement_relationships
131 Returns the agreement relationships of this agreement
135 sub agreement_relationships {
136 my ( $self, $relationships ) = @_;
138 if ( $relationships ) {
139 my $schema = $self->_result->result_source->schema;
140 # FIXME naming - is "back link" ok?
142 'supersedes' => 'is-superseded-by',
143 'is-superseded-by' => 'supersedes',
144 'provides_post-cancellation_access_for' => 'has-post-cancellation-access-in',
145 'has-post-cancellation-access-in' => 'provides_post-cancellation_access_for',
146 'tracks_demand-driven_acquisitions_for' => 'has-demand-driven-acquisitions-in',
147 'has-demand-driven-acquisitions-in' => 'tracks_demand-driven_acquisitions_for',
148 'has_backfile_in' => 'has_frontfile_in',
149 'has_frontfile_in' => 'has_backfile_in',
150 'related_to' => 'related_to',
154 $self->agreement_relationships->delete;
155 $self->agreement_back_relationships->delete;
157 for my $relationship (@$relationships) {
158 $self->_result->add_to_erm_agreement_relationships_agreements($relationship);
160 agreement_id => $relationship->{related_agreement_id},
161 related_agreement_id => $self->agreement_id,
162 relationship => $back_links->{$relationship->{relationship}},
163 notes => $relationship->{notes}, # FIXME Is it correct, do we keep the note here?
165 $self->_result->add_to_erm_agreement_relationships_related_agreements($back_link);
170 my $related_agreements_rs = $self->_result->erm_agreement_relationships_agreements;
171 return Koha::ERM::Agreement::Relationships->_new_from_dbic($related_agreements_rs);
174 =head3 agreement_back_relationships
176 # FIXME Naming - how is it called?
177 Returns the reverse relationship
181 sub agreement_back_relationships {
183 my $rs = $self->_result->erm_agreement_relationships_related_agreements;
184 return Koha::ERM::Agreement::Relationships->_new_from_dbic($rs);
189 Returns the documents for this agreement
195 Returns or updates the documents for this agreement
200 my ( $self, $documents ) = @_;
202 $self->documents->replace_with($documents, $self);
204 my $documents_rs = $self->_result->erm_documents;
205 return Koha::ERM::Documents->_new_from_dbic($documents_rs);
208 =head3 agreement_packages
210 Return the local packages for this agreement (and the other ones that have an entry locally)
214 sub agreement_packages {
216 my $packages_agreements_rs = $self->_result->erm_eholdings_packages_agreements;
217 return Koha::ERM::EHoldings::Package::Agreements->_new_from_dbic($packages_agreements_rs);
222 Return the vendor for this agreement
228 my $vendor_rs = $self->_result->vendor;
229 return unless $vendor_rs;
230 return Koha::Acquisition::Bookseller->_new_from_dbic($vendor_rs);
233 =head2 Internal methods
240 return 'ErmAgreement';