Bug 32030: ERM - Users
[koha.git] / erm / agreements.pl
1 #! /usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19 use CGI qw ( -utf8 );
20 use C4::Context;
21 use C4::Auth qw( get_template_and_user );
22 use C4::Output qw( output_html_with_http_headers );
23
24 use Koha::DateUtils qw( dt_from_string );
25 use Koha::Acquisition::Booksellers;
26 use Koha::ERM::Agreements;
27
28 my $input        = CGI->new;
29 my $agreement_id = $input->param('agreement_id');
30 my $op           = $input->param('op') || 'list';
31 my @messages;
32
33 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
34     {
35         template_name => "erm/agreements.tt",
36         query         => $input,
37         type          => "intranet",
38         flagsrequired => { 'erm' => '1' },
39     }
40 );
41
42 my $dbh = C4::Context->dbh;
43 if ( $op eq 'add_form' ) {
44     my $agreement;
45     if ($agreement_id) {
46         $agreement = Koha::ERM::Agreements->find($agreement_id);
47     }
48
49     $template->param( agreement => $agreement, );
50 }
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');
60
61     my $schema = Koha::Database->new->schema;
62     $schema->txn_do(sub{
63         my ( $stored, $agreement );
64         if ($agreement_id) {
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);
74
75             eval { $agreement->store; };
76             if ($@) {
77                 push @messages, { type => 'error', code => 'error_on_update' };
78             }
79             else {
80                 $stored = 1;
81                 push @messages, { type => 'message', code => 'success_on_update' };
82             }
83         }
84         else {
85             $agreement = Koha::ERM::Agreement->new(
86                 {
87                     vendor_id        => $vendor_id,
88                     name             => $name,
89                     description      => $description,
90                     status           => $status,
91                     closure_reason   => $closure_reason,
92                     is_perpetual     => $is_perpetual,
93                     renewal_priority => $renewal_priority,
94                     license_info     => $license_info,
95                 }
96             );
97             eval { $agreement->store; };
98             if ($@) {
99                 push @messages, { type => 'error', code => 'error_on_insert' };
100             }
101             else {
102                 $stored = 1;
103                 push @messages, { type => 'message', code => 'success_on_insert' };
104             }
105         }
106
107         if ( $stored ) {
108             if ( $agreement_id ) {
109                 $agreement->periods->delete;
110                 $agreement->user_roles->delete;
111             }
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 );
118
119                 $started_on = dt_from_string($started_on);
120                 $ended_on &&= dt_from_string($ended_on);
121                 $cancellation_deadline &&= dt_from_string($cancellation_deadline);
122
123                 Koha::ERM::Agreement::Period->new(
124                     {
125                         agreement_id          => $agreement->agreement_id,
126                         started_on            => $started_on,
127                         ended_on              => $ended_on,
128                         cancellation_deadline => $cancellation_deadline,
129                         notes                 => $notes,
130                     }
131                 )->store;
132             }
133
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(
139                     {
140                         agreement_id => $agreement->agreement_id,
141                         user_id      => $user_id,
142                         role         => $role,
143                     }
144                 )->store;
145             }
146
147         }
148     });
149     $op = 'list';
150 }
151 elsif ( $op eq 'delete_confirm' ) {
152     my $agreement = Koha::ERM::Agreements->find($agreement_id);
153     $template->param( agreement => $agreement, );
154 }
155 elsif ( $op eq 'delete_confirmed' ) {
156     my $agreement = Koha::ERM::Agreements->find($agreement_id);
157     my $deleted   = eval { $agreement->delete; };
158
159     if ( $@ or not $deleted ) {
160         push @messages, { type => 'error', code => 'error_on_delete' };
161     }
162     else {
163         push @messages, { type => 'message', code => 'success_on_delete' };
164     }
165     $op = 'list';
166 }
167
168 if ( $op eq 'list' ) {
169     $template->param(
170         agreements_count => Koha::ERM::Agreements->search->count );
171 }
172
173 $template->param(
174     vendors      => Koha::Acquisition::Booksellers->search,
175     agreement_id => $agreement_id,
176     messages     => \@messages,
177     op           => $op,
178 );
179
180 output_html_with_http_headers $input, $cookie, $template->output;