Bug 28591: Don't pass debug to get_template_and_user
[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          = CGI->new;
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     }
46 );
47
48 $template->param(
49     contractnumber => $contractnumber,
50     booksellerid   => $booksellerid,
51     booksellername => $bookseller->name,
52     basketcount   => $bookseller->baskets->count,
53     active         => $bookseller->active,
54     subscriptioncount   => $bookseller->subscriptions->count,
55 );
56
57 #ADD_FORM: called if $op is 'add_form'. Used to create form to add or  modify a record
58 if ( $op eq 'add_form' ) {
59     $template->param( add_form => 1 );
60
61    # if contractnumber exists, it's a modify action, so read values to modify...
62     if ($contractnumber) {
63         my $contract = GetContract({
64             contractnumber => $contractnumber
65         });
66
67         $template->param(
68             contractnumber      => $contract->{contractnumber},
69             contractname        => $contract->{contractname},
70             contractdescription => $contract->{contractdescription},
71             contractstartdate   => $contract->{contractstartdate},
72             contractenddate     => $contract->{contractenddate},
73         );
74     } else {
75         $template->param(
76             contractnumber           => undef,
77             contractname             => undef,
78             contractdescription      => undef,
79             contractstartdate        => undef,
80             contractenddate          => undef,
81         );
82     }
83
84     # END $OP eq ADD_FORM
85 }
86 #ADD_VALIDATE: called by add_form, used to insert/modify data in DB
87 elsif ( $op eq 'add_validate' ) {
88 ## Please see file perltidy.ERR
89     $template->param( add_validate => 1 );
90
91     my $is_a_modif = $input->param("is_a_modif");
92
93     my $contractstart_dt = eval { dt_from_string( scalar $input->param('contractstartdate') ); };
94     my $contractend_dt = eval { dt_from_string( scalar $input->param('contractenddate') ); };
95     unless ( $contractstart_dt and $contractend_dt ) {
96         my $today = dt_from_string;
97         $contractstart_dt ||= $today;
98         $contractend_dt   ||= $today;
99     }
100
101     if ( $is_a_modif ) {
102         ModContract({
103             contractstartdate   => eval { output_pref({ dt => dt_from_string( $contractstart_dt ), dateformat => 'iso', dateonly => 1 } ); },
104             contractenddate     => eval { output_pref({ dt => dt_from_string( $contractend_dt ), dateformat => 'iso', dateonly => 1 } ); },
105             contractname        => scalar $input->param('contractname'),
106             contractdescription => scalar $input->param('contractdescription'),
107             booksellerid        => scalar $input->param('booksellerid'),
108             contractnumber      => scalar $input->param('contractnumber'),
109         });
110     } else {
111         AddContract({
112             contractname        => scalar $input->param('contractname'),
113             contractdescription => scalar $input->param('contractdescription'),
114             booksellerid        => scalar $input->param('booksellerid'),
115             contractstartdate   => eval { output_pref({ dt => dt_from_string( scalar $input->param('contractstartdate') ), dateformat => 'iso', dateonly => 1 } ); },
116             contractenddate     => eval { output_pref({ dt => dt_from_string( scalar $input->param('contractenddate') ), dateformat => 'iso', dateonly => 1 } ); },
117         });
118     }
119
120     print $input->redirect("/cgi-bin/koha/acqui/supplier.pl?booksellerid=$booksellerid");
121     exit;
122
123     # END $OP eq ADD_VALIDATE
124 }
125 #DELETE_CONFIRM: called by default form, used to confirm deletion of data in DB
126 elsif ( $op eq 'delete_confirm' ) {
127     $template->param( delete_confirm => 1 );
128
129     my $contract = GetContract( { contractnumber => $contractnumber } );
130
131     $template->param(
132         contractnumber      => $$contract{contractnumber},
133         contractname        => $$contract{contractname},
134         contractdescription => $$contract{contractdescription},
135         contractstartdate   => $$contract{contractstartdate},
136         contractenddate     => $$contract{contractenddate},
137     );
138
139     # END $OP eq DELETE_CONFIRM
140 }
141 #DELETE_CONFIRMED: called by delete_confirm, used to effectively confirm deletion of data in DB
142 elsif ( $op eq 'delete_confirmed' ) {
143     my $deleted = DelContract( { contractnumber => $contractnumber } );
144
145     if ( $deleted ) {
146         print $input->redirect("/cgi-bin/koha/acqui/supplier.pl?booksellerid=$booksellerid");
147         exit;
148     } else {
149         $template->param( error => 'not_deleted' );
150         $op = 'list';
151     }
152
153     # END $OP eq LIST
154 }
155 # DEFAULT: Builds a list of contracts and displays them
156 if ( $op eq 'list' ) {
157     $template->param(else => 1);
158
159     # get contracts
160     my @contracts = @{GetContracts( { booksellerid => $booksellerid } )};
161
162     # format dates
163     for my $contract ( @contracts ) {
164         $contract->{contractstartdate} =  output_pref({ dt => dt_from_string( $contract->{contractstartdate} ), dateonly => 1 });
165         $contract->{contractenddate}   =  output_pref({ dt => dt_from_string( $contract->{contractenddate} ), dateonly => 1 }),
166     }
167
168     $template->param(loop => \@contracts);
169
170     #---- END $OP eq DEFAULT
171 }
172
173 output_html_with_http_headers $input, $cookie, $template->output;
174