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>.
21 use C4::Auth qw( get_template_and_user );
22 use C4::Output qw( output_html_with_http_headers );
24 use Koha::DateUtils qw( dt_from_string );
25 use Koha::Acquisition::Booksellers;
26 use Koha::ERM::Agreements;
29 my $agreement_id = $input->param('agreement_id');
30 my $op = $input->param('op') || 'list';
33 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
35 template_name => "erm/agreements.tt",
38 flagsrequired => { 'erm' => '1' },
42 my $dbh = C4::Context->dbh;
43 if ( $op eq 'add_form' ) {
46 $agreement = Koha::ERM::Agreements->find($agreement_id);
49 $template->param( agreement => $agreement, );
51 elsif ( $op eq 'add_validate' ) {
52 my $vendor_id = $input->param('vendor_id');
53 my $name = $input->param('name');
54 my $description = $input->param('description');
55 my $status = $input->param('status');
56 my $closure_reason = $input->param('closure_reason');
57 my $is_perpetual = $input->param('is_perpetual');
58 my $renewal_priority = $input->param('renewal_priority');
59 my $license_info = $input->param('license_info');
61 my $schema = Koha::Database->new->schema;
63 my ( $stored, $agreement );
65 $agreement = Koha::ERM::Agreements->find($agreement_id);
66 $agreement->vendor_id($vendor_id);
67 $agreement->name($name);
68 $agreement->description($description);
69 $agreement->status($status);
70 $agreement->closure_reason($closure_reason);
71 $agreement->is_perpetual($is_perpetual);
72 $agreement->renewal_priority($renewal_priority);
73 $agreement->license_info($license_info);
75 eval { $agreement->store; };
77 push @messages, { type => 'error', code => 'error_on_update' };
81 push @messages, { type => 'message', code => 'success_on_update' };
85 $agreement = Koha::ERM::Agreement->new(
87 vendor_id => $vendor_id,
89 description => $description,
91 closure_reason => $closure_reason,
92 is_perpetual => $is_perpetual,
93 renewal_priority => $renewal_priority,
94 license_info => $license_info,
97 eval { $agreement->store; };
99 push @messages, { type => 'error', code => 'error_on_insert' };
103 push @messages, { type => 'message', code => 'success_on_insert' };
108 if ( $agreement_id ) {
109 $agreement->periods->delete;
110 $agreement->user_roles->delete;
112 for my $unique_id ( $input->multi_param('period_unique_id') ) {
113 my $started_on = $input->param( 'started_on_' . $unique_id );
114 next unless $started_on;
115 my $ended_on = $input->param( 'ended_on_' . $unique_id );
116 my $cancellation_deadline = $input->param( 'cancellation_deadline_' . $unique_id );
117 my $notes = $input->param( 'notes_' . $unique_id );
119 $started_on = dt_from_string($started_on);
120 $ended_on &&= dt_from_string($ended_on);
121 $cancellation_deadline &&= dt_from_string($cancellation_deadline);
123 Koha::ERM::Agreement::Period->new(
125 agreement_id => $agreement->agreement_id,
126 started_on => $started_on,
127 ended_on => $ended_on,
128 cancellation_deadline => $cancellation_deadline,
134 for my $unique_id ( $input->multi_param('user_unique_id') ) {
135 my $user_id = $input->param('user_id_' . $unique_id);
136 next unless $user_id;
137 my $role = $input->param('user_role_' . $unique_id);
138 Koha::ERM::Agreement::UserRole->new(
140 agreement_id => $agreement->agreement_id,
151 elsif ( $op eq 'delete_confirm' ) {
152 my $agreement = Koha::ERM::Agreements->find($agreement_id);
153 $template->param( agreement => $agreement, );
155 elsif ( $op eq 'delete_confirmed' ) {
156 my $agreement = Koha::ERM::Agreements->find($agreement_id);
157 my $deleted = eval { $agreement->delete; };
159 if ( $@ or not $deleted ) {
160 push @messages, { type => 'error', code => 'error_on_delete' };
163 push @messages, { type => 'message', code => 'success_on_delete' };
168 if ( $op eq 'list' ) {
170 agreements_count => Koha::ERM::Agreements->search->count );
174 vendors => Koha::Acquisition::Booksellers->search,
175 agreement_id => $agreement_id,
176 messages => \@messages,
180 output_html_with_http_headers $input, $cookie, $template->output;