Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[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          = 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 = 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         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->baskets->count,
55     active         => $bookseller->active,
56     subscriptioncount   => $bookseller->subscriptions->count,
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   => $contract->{contractstartdate},
74             contractenddate     => $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     my $contractstart_dt = eval { dt_from_string( scalar $input->param('contractstartdate') ); };
96     my $contractend_dt = eval { dt_from_string( scalar $input->param('contractenddate') ); };
97     unless ( $contractstart_dt and $contractend_dt ) {
98         my $today = dt_from_string;
99         $contractstart_dt ||= $today;
100         $contractend_dt   ||= $today;
101     }
102
103     if ( $is_a_modif ) {
104         ModContract({
105             contractstartdate   => eval { output_pref({ dt => dt_from_string( $contractstart_dt ), dateformat => 'iso', dateonly => 1 } ); },
106             contractenddate     => eval { output_pref({ dt => dt_from_string( $contractend_dt ), dateformat => 'iso', dateonly => 1 } ); },
107             contractname        => scalar $input->param('contractname'),
108             contractdescription => scalar $input->param('contractdescription'),
109             booksellerid        => scalar $input->param('booksellerid'),
110             contractnumber      => scalar $input->param('contractnumber'),
111         });
112     } else {
113         AddContract({
114             contractname        => scalar $input->param('contractname'),
115             contractdescription => scalar $input->param('contractdescription'),
116             booksellerid        => scalar $input->param('booksellerid'),
117             contractstartdate   => eval { output_pref({ dt => dt_from_string( scalar $input->param('contractstartdate') ), dateformat => 'iso', dateonly => 1 } ); },
118             contractenddate     => eval { output_pref({ dt => dt_from_string( scalar $input->param('contractenddate') ), dateformat => 'iso', dateonly => 1 } ); },
119         });
120     }
121
122     print $input->redirect("/cgi-bin/koha/acqui/supplier.pl?booksellerid=$booksellerid");
123     exit;
124
125     # END $OP eq ADD_VALIDATE
126 }
127 #DELETE_CONFIRM: called by default form, used to confirm deletion of data in DB
128 elsif ( $op eq 'delete_confirm' ) {
129     $template->param( delete_confirm => 1 );
130
131     my $contract = GetContract( { contractnumber => $contractnumber } );
132
133     $template->param(
134         contractnumber      => $$contract{contractnumber},
135         contractname        => $$contract{contractname},
136         contractdescription => $$contract{contractdescription},
137         contractstartdate   => $$contract{contractstartdate},
138         contractenddate     => $$contract{contractenddate},
139     );
140
141     # END $OP eq DELETE_CONFIRM
142 }
143 #DELETE_CONFIRMED: called by delete_confirm, used to effectively confirm deletion of data in DB
144 elsif ( $op eq 'delete_confirmed' ) {
145     my $deleted = DelContract( { contractnumber => $contractnumber } );
146
147     if ( $deleted ) {
148         print $input->redirect("/cgi-bin/koha/acqui/supplier.pl?booksellerid=$booksellerid");
149         exit;
150     } else {
151         $template->param( error => 'not_deleted' );
152         $op = 'list';
153     }
154
155     # END $OP eq LIST
156 }
157 # DEFAULT: Builds a list of contracts and displays them
158 if ( $op eq 'list' ) {
159     $template->param(else => 1);
160
161     # get contracts
162     my @contracts = @{GetContracts( { booksellerid => $booksellerid } )};
163
164     # format dates
165     for my $contract ( @contracts ) {
166         $contract->{contractstartdate} =  output_pref({ dt => dt_from_string( $contract->{contractstartdate} ), dateonly => 1 });
167         $contract->{contractenddate}   =  output_pref({ dt => dt_from_string( $contract->{contractenddate} ), dateonly => 1 }),
168     }
169
170     $template->param(loop => \@contracts);
171
172     #---- END $OP eq DEFAULT
173 }
174
175 output_html_with_http_headers $input, $cookie, $template->output;
176