Bug 11401: QA followup
[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 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;
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::Bookseller qw/GetBookSellerFromId/;
31 use C4::Contract;
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 = GetBookSellerFromId($booksellerid);
39
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
41     {   template_name   => "admin/aqcontract.tt",
42         query           => $input,
43         type            => "intranet",
44         authnotrequired => 0,
45         flagsrequired   => { acquisition => 'contracts_manage' },
46         debug           => 1,
47     }
48 );
49
50 $template->param(
51     contractnumber => $contractnumber,
52     booksellerid   => $booksellerid,
53     booksellername => $bookseller->{name},
54     basketcount   => $bookseller->{'basketcount'},
55     subscriptioncount   => $bookseller->{'subscriptioncount'},
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 => format_date( $contract->{contractstartdate} ),
73             contractenddate   => format_date( $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     if ( $is_a_modif ) {
95         ModContract({
96             contractstartdate   => format_date_in_iso( $input->param('contractstartdate') ),
97             contractenddate     => format_date_in_iso( $input->param('contractenddate') ),
98             contractname        => $input->param('contractname'),
99             contractdescription => $input->param('contractdescription'),
100             booksellerid        => $input->param('booksellerid'),
101             contractnumber      => $input->param('contractnumber'),
102         });
103     } else {
104         AddContract({
105             contractname        => $input->param('contractname'),
106             contractdescription => $input->param('contractdescription'),
107             booksellerid        => $input->param('booksellerid'),
108             contractstartdate   => format_date_in_iso( $input->param('contractstartdate') ),
109             contractenddate     => format_date_in_iso( $input->param('contractenddate') ),
110         });
111     }
112
113     print $input->redirect("/cgi-bin/koha/acqui/supplier.pl?booksellerid=$booksellerid");
114     exit;
115
116     # END $OP eq ADD_VALIDATE
117 }
118 #DELETE_CONFIRM: called by default form, used to confirm deletion of data in DB
119 elsif ( $op eq 'delete_confirm' ) {
120     $template->param( delete_confirm => 1 );
121
122     my $contract = GetContract( { contractnumber => $contractnumber } );
123
124     $template->param(
125         contractnumber      => $$contract{contractnumber},
126         contractname        => $$contract{contractname},
127         contractdescription => $$contract{contractdescription},
128         contractstartdate   => format_date( $$contract{contractstartdate} ),
129         contractenddate     => format_date( $$contract{contractenddate} ),
130     );
131
132     # END $OP eq DELETE_CONFIRM
133 }
134 #DELETE_CONFIRMED: called by delete_confirm, used to effectively confirm deletion of data in DB
135 elsif ( $op eq 'delete_confirmed' ) {
136     my $deleted = DelContract( { contractnumber => $contractnumber } );
137
138     if ( $deleted ) {
139         print $input->redirect("/cgi-bin/koha/acqui/supplier.pl?booksellerid=$booksellerid");
140         exit;
141     } else {
142         $template->param( error => 'not_deleted' );
143         $op = 'list';
144     }
145
146     # END $OP eq LIST
147 }
148 # DEFAULT: Builds a list of contracts and displays them
149 if ( $op eq 'list' ) {
150     $template->param(else => 1);
151
152     # get contracts
153     my @contracts = @{GetContracts( { booksellerid => $booksellerid } )};
154
155     # format dates
156     for ( @contracts ) {
157         $$_{contractstartdate} = format_date($$_{contractstartdate});
158         $$_{contractenddate}   = format_date($$_{contractenddate});
159     }
160
161     $template->param(loop => \@contracts);
162
163     #---- END $OP eq DEFAULT
164 }
165
166 output_html_with_http_headers $input, $cookie, $template->output;
167