Bug 30477: Add new UNIMARC installer translation files
[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 qw( get_template_and_user );
27 use C4::Output qw( output_html_with_http_headers );
28 use C4::Contract qw(
29     AddContract
30     DelContract
31     GetContract
32     GetContracts
33     ModContract
34 );
35 use Koha::DateUtils qw( dt_from_string output_pref );
36
37 use Koha::Acquisition::Booksellers;
38
39 my $input          = CGI->new;
40 my $contractnumber = $input->param('contractnumber');
41 my $booksellerid   = $input->param('booksellerid');
42 my $op             = $input->param('op') || 'list';
43
44 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
45
46 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
47     {   template_name   => "admin/aqcontract.tt",
48         query           => $input,
49         type            => "intranet",
50         flagsrequired   => { acquisition => 'contracts_manage' },
51     }
52 );
53
54 $template->param(
55     contractnumber => $contractnumber,
56     booksellerid   => $booksellerid,
57     booksellername => $bookseller->name,
58     basketcount   => $bookseller->baskets->count,
59     active         => $bookseller->active,
60     subscriptioncount   => $bookseller->subscriptions->count,
61 );
62
63 #ADD_FORM: called if $op is 'add_form'. Used to create form to add or  modify a record
64 if ( $op eq 'add_form' ) {
65     $template->param( add_form => 1 );
66
67    # if contractnumber exists, it's a modify action, so read values to modify...
68     if ($contractnumber) {
69         my $contract = GetContract({
70             contractnumber => $contractnumber
71         });
72
73         $template->param(
74             contractnumber      => $contract->{contractnumber},
75             contractname        => $contract->{contractname},
76             contractdescription => $contract->{contractdescription},
77             contractstartdate   => $contract->{contractstartdate},
78             contractenddate     => $contract->{contractenddate},
79         );
80     } else {
81         $template->param(
82             contractnumber           => undef,
83             contractname             => undef,
84             contractdescription      => undef,
85             contractstartdate        => undef,
86             contractenddate          => undef,
87         );
88     }
89
90     # END $OP eq ADD_FORM
91 }
92 #ADD_VALIDATE: called by add_form, used to insert/modify data in DB
93 elsif ( $op eq 'add_validate' ) {
94 ## Please see file perltidy.ERR
95     $template->param( add_validate => 1 );
96
97     my $is_a_modif = $input->param("is_a_modif");
98
99     my $contractstart_dt = eval { dt_from_string( scalar $input->param('contractstartdate') ); };
100     my $contractend_dt = eval { dt_from_string( scalar $input->param('contractenddate') ); };
101     unless ( $contractstart_dt and $contractend_dt ) {
102         my $today = dt_from_string;
103         $contractstart_dt ||= $today;
104         $contractend_dt   ||= $today;
105     }
106
107     if ( $is_a_modif ) {
108         ModContract({
109             contractstartdate   => eval { output_pref({ dt => dt_from_string( $contractstart_dt ), dateformat => 'iso', dateonly => 1 } ); },
110             contractenddate     => eval { output_pref({ dt => dt_from_string( $contractend_dt ), dateformat => 'iso', dateonly => 1 } ); },
111             contractname        => scalar $input->param('contractname'),
112             contractdescription => scalar $input->param('contractdescription'),
113             booksellerid        => scalar $input->param('booksellerid'),
114             contractnumber      => scalar $input->param('contractnumber'),
115         });
116     } else {
117         AddContract({
118             contractname        => scalar $input->param('contractname'),
119             contractdescription => scalar $input->param('contractdescription'),
120             booksellerid        => scalar $input->param('booksellerid'),
121             contractstartdate   => eval { output_pref({ dt => dt_from_string( scalar $input->param('contractstartdate') ), dateformat => 'iso', dateonly => 1 } ); },
122             contractenddate     => eval { output_pref({ dt => dt_from_string( scalar $input->param('contractenddate') ), dateformat => 'iso', dateonly => 1 } ); },
123         });
124     }
125
126     print $input->redirect("/cgi-bin/koha/acqui/supplier.pl?booksellerid=$booksellerid");
127     exit;
128
129     # END $OP eq ADD_VALIDATE
130 }
131 #DELETE_CONFIRM: called by default form, used to confirm deletion of data in DB
132 elsif ( $op eq 'delete_confirm' ) {
133     $template->param( delete_confirm => 1 );
134
135     my $contract = GetContract( { contractnumber => $contractnumber } );
136
137     $template->param(
138         contractnumber      => $$contract{contractnumber},
139         contractname        => $$contract{contractname},
140         contractdescription => $$contract{contractdescription},
141         contractstartdate   => $$contract{contractstartdate},
142         contractenddate     => $$contract{contractenddate},
143     );
144
145     # END $OP eq DELETE_CONFIRM
146 }
147 #DELETE_CONFIRMED: called by delete_confirm, used to effectively confirm deletion of data in DB
148 elsif ( $op eq 'delete_confirmed' ) {
149     my $deleted = DelContract( { contractnumber => $contractnumber } );
150
151     if ( $deleted ) {
152         print $input->redirect("/cgi-bin/koha/acqui/supplier.pl?booksellerid=$booksellerid");
153         exit;
154     } else {
155         $template->param( error => 'not_deleted' );
156         $op = 'list';
157     }
158
159     # END $OP eq LIST
160 }
161 # DEFAULT: Builds a list of contracts and displays them
162 if ( $op eq 'list' ) {
163     $template->param(else => 1);
164
165     # get contracts
166     my @contracts = @{GetContracts( { booksellerid => $booksellerid } )};
167
168     # format dates
169     for my $contract ( @contracts ) {
170         $contract->{contractstartdate} =  output_pref({ dt => dt_from_string( $contract->{contractstartdate} ), dateonly => 1 });
171         $contract->{contractenddate}   =  output_pref({ dt => dt_from_string( $contract->{contractenddate} ), dateonly => 1 }),
172     }
173
174     $template->param(loop => \@contracts);
175
176     #---- END $OP eq DEFAULT
177 }
178
179 output_html_with_http_headers $input, $cookie, $template->output;
180