Bug 9108: Followup: send the dateformat value from C4::Auth
[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') || '';
37
38 my $bookseller = GetBookSellerFromId($booksellerid);
39
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
41     {   template_name   => "admin/aqcontract.tmpl",
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 );
55
56 #ADD_FORM: called if $op is 'add_form'. Used to create form to add or  modify a record
57 if ( $op eq 'add_form' ) {
58     $template->param( add_form => 1 );
59
60    # if contractnumber exists, it's a modify action, so read values to modify...
61     if ($contractnumber) {
62         my $contract =
63           @{ GetContract( { contractnumber => $contractnumber } ) }[0];
64
65         $template->param(
66             contractnumber      => $contract->{contractnumber},
67             contractname        => $contract->{contractname},
68             contractdescription => $contract->{contractdescription},
69             contractstartdate => format_date( $contract->{contractstartdate} ),
70             contractenddate   => format_date( $contract->{contractenddate} ),
71         );
72     } else {
73         $template->param(
74             contractnumber           => undef,
75             contractname             => undef,
76             contractdescription      => undef,
77             contractstartdate        => undef,
78             contractenddate          => undef,
79         );
80     }
81
82     # END $OP eq ADD_FORM
83 }
84 #ADD_VALIDATE: called by add_form, used to insert/modify data in DB
85 elsif ( $op eq 'add_validate' ) {
86 ## Please see file perltidy.ERR
87     $template->param( add_validate => 1 );
88
89     my $is_a_modif = $input->param("is_a_modif");
90
91     if ( $is_a_modif ) {
92         ModContract({
93             contractstartdate   => format_date_in_iso( $input->param('contractstartdate') ),
94             contractenddate     => format_date_in_iso( $input->param('contractenddate') ),
95             contractname        => $input->param('contractname'),
96             contractdescription => $input->param('contractdescription'),
97             booksellerid        => $input->param('booksellerid'),
98             contractnumber      => $input->param('contractnumber'),
99         });
100     } else {
101         AddContract({
102             contractname        => $input->param('contractname'),
103             contractdescription => $input->param('contractdescription'),
104             booksellerid        => $input->param('booksellerid'),
105             contractstartdate   => format_date_in_iso( $input->param('contractstartdate') ),
106             contractenddate     => format_date_in_iso( $input->param('contractenddate') ),
107         });
108     }
109
110     print $input->redirect("/cgi-bin/koha/acqui/supplier.pl?booksellerid=$booksellerid");
111     exit;
112
113     # END $OP eq ADD_VALIDATE
114 }
115 #DELETE_CONFIRM: called by default form, used to confirm deletion of data in DB
116 elsif ( $op eq 'delete_confirm' ) {
117     $template->param( delete_confirm => 1 );
118
119     my $contract = @{GetContract( { contractnumber => $contractnumber } )}[0];
120
121     $template->param(
122         contractnumber      => $$contract{contractnumber},
123         contractname        => $$contract{contractname},
124         contractdescription => $$contract{contractdescription},
125         contractstartdate   => format_date( $$contract{contractstartdate} ),
126         contractenddate     => format_date( $$contract{contractenddate} ),
127     );
128
129     # END $OP eq DELETE_CONFIRM
130 }
131 #DELETE_CONFIRMED: called by delete_confirm, used to effectively confirm deletion of data in DB
132 elsif ( $op eq 'delete_confirmed' ) {
133     $template->param( delete_confirmed => 1 );
134
135     DelContract( { contractnumber => $contractnumber } );
136
137     print $input->redirect("/cgi-bin/koha/acqui/supplier.pl?booksellerid=$booksellerid");
138     exit;
139
140     # END $OP eq DELETE_CONFIRMED
141 }
142 # DEFAULT: Builds a list of contracts and displays them
143 else {
144     $template->param(else => 1);
145
146     # get contracts
147     my @contracts = @{GetContract( { booksellerid => $booksellerid } )};
148
149     # format dates
150     for ( @contracts ) {
151         $$_{contractstartdate} = format_date($$_{contractstartdate});
152         $$_{contractenddate}   = format_date($$_{contractenddate});
153     }
154
155     $template->param(loop => \@contracts);
156
157     #---- END $OP eq DEFAULT
158 }
159
160 output_html_with_http_headers $input, $cookie, $template->output;
161