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