Bug 26268: (QA follow-up) Split DB update checks
[koha.git] / admin / aqcontract.pl
1 #!/usr/bin/perl
2
3 #script to administer the contract table
4 #written 02/09/2008 by john.soros@biblibre.com
5
6 # Copyright 2008-2009 BibLibre SARL
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22
23 use Modern::Perl;
24 use CGI qw ( -utf8 );
25 use C4::Context;
26 use C4::Auth;
27 use C4::Output;
28 use C4::Contract;
29 use Koha::DateUtils;
30
31 use Koha::Acquisition::Booksellers;
32
33 my $input          = new CGI;
34 my $contractnumber = $input->param('contractnumber');
35 my $booksellerid   = $input->param('booksellerid');
36 my $op             = $input->param('op') || 'list';
37
38 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
39
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
41     {   template_name   => "admin/aqcontract.tt",
42         query           => $input,
43         type            => "intranet",
44         flagsrequired   => { acquisition => 'contracts_manage' },
45         debug           => 1,
46     }
47 );
48
49 $template->param(
50     contractnumber => $contractnumber,
51     booksellerid   => $booksellerid,
52     booksellername => $bookseller->name,
53     basketcount   => $bookseller->baskets->count,
54     active         => $bookseller->active,
55     subscriptioncount   => $bookseller->subscriptions->count,
56 );
57
58 #ADD_FORM: called if $op is 'add_form'. Used to create form to add or  modify a record
59 if ( $op eq 'add_form' ) {
60     $template->param( add_form => 1 );
61
62    # if contractnumber exists, it's a modify action, so read values to modify...
63     if ($contractnumber) {
64         my $contract = GetContract({
65             contractnumber => $contractnumber
66         });
67
68         $template->param(
69             contractnumber      => $contract->{contractnumber},
70             contractname        => $contract->{contractname},
71             contractdescription => $contract->{contractdescription},
72             contractstartdate   => $contract->{contractstartdate},
73             contractenddate     => $contract->{contractenddate},
74         );
75     } else {
76         $template->param(
77             contractnumber           => undef,
78             contractname             => undef,
79             contractdescription      => undef,
80             contractstartdate        => undef,
81             contractenddate          => undef,
82         );
83     }
84
85     # END $OP eq ADD_FORM
86 }
87 #ADD_VALIDATE: called by add_form, used to insert/modify data in DB
88 elsif ( $op eq 'add_validate' ) {
89 ## Please see file perltidy.ERR
90     $template->param( add_validate => 1 );
91
92     my $is_a_modif = $input->param("is_a_modif");
93
94     my $contractstart_dt = eval { dt_from_string( scalar $input->param('contractstartdate') ); };
95     my $contractend_dt = eval { dt_from_string( scalar $input->param('contractenddate') ); };
96     unless ( $contractstart_dt and $contractend_dt ) {
97         my $today = dt_from_string;
98         $contractstart_dt ||= $today;
99         $contractend_dt   ||= $today;
100     }
101
102     if ( $is_a_modif ) {
103         ModContract({
104             contractstartdate   => eval { output_pref({ dt => dt_from_string( $contractstart_dt ), dateformat => 'iso', dateonly => 1 } ); },
105             contractenddate     => eval { output_pref({ dt => dt_from_string( $contractend_dt ), dateformat => 'iso', dateonly => 1 } ); },
106             contractname        => scalar $input->param('contractname'),
107             contractdescription => scalar $input->param('contractdescription'),
108             booksellerid        => scalar $input->param('booksellerid'),
109             contractnumber      => scalar $input->param('contractnumber'),
110         });
111     } else {
112         AddContract({
113             contractname        => scalar $input->param('contractname'),
114             contractdescription => scalar $input->param('contractdescription'),
115             booksellerid        => scalar $input->param('booksellerid'),
116             contractstartdate   => eval { output_pref({ dt => dt_from_string( scalar $input->param('contractstartdate') ), dateformat => 'iso', dateonly => 1 } ); },
117             contractenddate     => eval { output_pref({ dt => dt_from_string( scalar $input->param('contractenddate') ), dateformat => 'iso', dateonly => 1 } ); },
118         });
119     }
120
121     print $input->redirect("/cgi-bin/koha/acqui/supplier.pl?booksellerid=$booksellerid");
122     exit;
123
124     # END $OP eq ADD_VALIDATE
125 }
126 #DELETE_CONFIRM: called by default form, used to confirm deletion of data in DB
127 elsif ( $op eq 'delete_confirm' ) {
128     $template->param( delete_confirm => 1 );
129
130     my $contract = GetContract( { contractnumber => $contractnumber } );
131
132     $template->param(
133         contractnumber      => $$contract{contractnumber},
134         contractname        => $$contract{contractname},
135         contractdescription => $$contract{contractdescription},
136         contractstartdate   => $$contract{contractstartdate},
137         contractenddate     => $$contract{contractenddate},
138     );
139
140     # END $OP eq DELETE_CONFIRM
141 }
142 #DELETE_CONFIRMED: called by delete_confirm, used to effectively confirm deletion of data in DB
143 elsif ( $op eq 'delete_confirmed' ) {
144     my $deleted = DelContract( { contractnumber => $contractnumber } );
145
146     if ( $deleted ) {
147         print $input->redirect("/cgi-bin/koha/acqui/supplier.pl?booksellerid=$booksellerid");
148         exit;
149     } else {
150         $template->param( error => 'not_deleted' );
151         $op = 'list';
152     }
153
154     # END $OP eq LIST
155 }
156 # DEFAULT: Builds a list of contracts and displays them
157 if ( $op eq 'list' ) {
158     $template->param(else => 1);
159
160     # get contracts
161     my @contracts = @{GetContracts( { booksellerid => $booksellerid } )};
162
163     # format dates
164     for my $contract ( @contracts ) {
165         $contract->{contractstartdate} =  output_pref({ dt => dt_from_string( $contract->{contractstartdate} ), dateonly => 1 });
166         $contract->{contractenddate}   =  output_pref({ dt => dt_from_string( $contract->{contractenddate} ), dateonly => 1 }),
167     }
168
169     $template->param(loop => \@contracts);
170
171     #---- END $OP eq DEFAULT
172 }
173
174 output_html_with_http_headers $input, $cookie, $template->output;
175