Bug 11944: use CGI( -utf8 ) everywhere
[koha-equinox.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 under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
23 use strict;
24 use warnings;
25 use CGI qw ( -utf8 );
26 use C4::Context;
27 use C4::Auth;
28 use C4::Output;
29 use C4::Dates qw/format_date format_date_in_iso/;
30 use C4::Contract;
31
32 use Koha::Acquisition::Bookseller;
33
34 my $input          = new CGI;
35 my $contractnumber = $input->param('contractnumber');
36 my $booksellerid   = $input->param('booksellerid');
37 my $op             = $input->param('op') || 'list';
38
39 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
40
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42     {   template_name   => "admin/aqcontract.tt",
43         query           => $input,
44         type            => "intranet",
45         authnotrequired => 0,
46         flagsrequired   => { acquisition => 'contracts_manage' },
47         debug           => 1,
48     }
49 );
50
51 $template->param(
52     contractnumber => $contractnumber,
53     booksellerid   => $booksellerid,
54     booksellername => $bookseller->{name},
55     basketcount   => $bookseller->{'basketcount'},
56     subscriptioncount   => $bookseller->{'subscriptioncount'},
57 );
58
59 #ADD_FORM: called if $op is 'add_form'. Used to create form to add or  modify a record
60 if ( $op eq 'add_form' ) {
61     $template->param( add_form => 1 );
62
63    # if contractnumber exists, it's a modify action, so read values to modify...
64     if ($contractnumber) {
65         my $contract = GetContract({
66             contractnumber => $contractnumber
67         });
68
69         $template->param(
70             contractnumber      => $contract->{contractnumber},
71             contractname        => $contract->{contractname},
72             contractdescription => $contract->{contractdescription},
73             contractstartdate => format_date( $contract->{contractstartdate} ),
74             contractenddate   => format_date( $contract->{contractenddate} ),
75         );
76     } else {
77         $template->param(
78             contractnumber           => undef,
79             contractname             => undef,
80             contractdescription      => undef,
81             contractstartdate        => undef,
82             contractenddate          => undef,
83         );
84     }
85
86     # END $OP eq ADD_FORM
87 }
88 #ADD_VALIDATE: called by add_form, used to insert/modify data in DB
89 elsif ( $op eq 'add_validate' ) {
90 ## Please see file perltidy.ERR
91     $template->param( add_validate => 1 );
92
93     my $is_a_modif = $input->param("is_a_modif");
94
95     if ( $is_a_modif ) {
96         ModContract({
97             contractstartdate   => format_date_in_iso( $input->param('contractstartdate') ),
98             contractenddate     => format_date_in_iso( $input->param('contractenddate') ),
99             contractname        => $input->param('contractname'),
100             contractdescription => $input->param('contractdescription'),
101             booksellerid        => $input->param('booksellerid'),
102             contractnumber      => $input->param('contractnumber'),
103         });
104     } else {
105         AddContract({
106             contractname        => $input->param('contractname'),
107             contractdescription => $input->param('contractdescription'),
108             booksellerid        => $input->param('booksellerid'),
109             contractstartdate   => format_date_in_iso( $input->param('contractstartdate') ),
110             contractenddate     => format_date_in_iso( $input->param('contractenddate') ),
111         });
112     }
113
114     print $input->redirect("/cgi-bin/koha/acqui/supplier.pl?booksellerid=$booksellerid");
115     exit;
116
117     # END $OP eq ADD_VALIDATE
118 }
119 #DELETE_CONFIRM: called by default form, used to confirm deletion of data in DB
120 elsif ( $op eq 'delete_confirm' ) {
121     $template->param( delete_confirm => 1 );
122
123     my $contract = GetContract( { contractnumber => $contractnumber } );
124
125     $template->param(
126         contractnumber      => $$contract{contractnumber},
127         contractname        => $$contract{contractname},
128         contractdescription => $$contract{contractdescription},
129         contractstartdate   => format_date( $$contract{contractstartdate} ),
130         contractenddate     => format_date( $$contract{contractenddate} ),
131     );
132
133     # END $OP eq DELETE_CONFIRM
134 }
135 #DELETE_CONFIRMED: called by delete_confirm, used to effectively confirm deletion of data in DB
136 elsif ( $op eq 'delete_confirmed' ) {
137     my $deleted = DelContract( { contractnumber => $contractnumber } );
138
139     if ( $deleted ) {
140         print $input->redirect("/cgi-bin/koha/acqui/supplier.pl?booksellerid=$booksellerid");
141         exit;
142     } else {
143         $template->param( error => 'not_deleted' );
144         $op = 'list';
145     }
146
147     # END $OP eq LIST
148 }
149 # DEFAULT: Builds a list of contracts and displays them
150 if ( $op eq 'list' ) {
151     $template->param(else => 1);
152
153     # get contracts
154     my @contracts = @{GetContracts( { booksellerid => $booksellerid } )};
155
156     # format dates
157     for ( @contracts ) {
158         $$_{contractstartdate} = format_date($$_{contractstartdate});
159         $$_{contractenddate}   = format_date($$_{contractenddate});
160     }
161
162     $template->param(loop => \@contracts);
163
164     #---- END $OP eq DEFAULT
165 }
166
167 output_html_with_http_headers $input, $cookie, $template->output;
168