Bug 15258: Fix Perl scripts declaring unused variables
[koha.git] / acqui / basketheader.pl
1 #!/usr/bin/perl
2
3 #script to add basket and edit header options (name, notes and contractnumber)
4 #written by john.soros@biblibre.com 15/09/2008
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 =head1 NAME
24
25 basketheader.pl
26
27 =head1 DESCRIPTION
28
29 This script is used to edit the basket's "header", or add a new basket, the header contains the supplier ID,
30 notes to the supplier, local notes, and the contractnumber, which identifies the basket to a specific contract.
31
32 =head1 CGI PARAMETERS
33
34 =over 4
35
36 =item booksellerid
37
38 C<$booksellerid> is the id of the supplier we add the basket to.
39
40 =item basketid
41
42 If it exists, C<$basketno> is the basket we edit
43
44 =back
45
46 =cut
47
48 use strict;
49 use warnings;
50 use CGI qw ( -utf8 );
51 use C4::Context;
52 use C4::Auth;
53 use C4::Branch;
54 use C4::Output;
55 use C4::Acquisition qw/GetBasket NewBasket ModBasketHeader/;
56 use C4::Contract qw/GetContracts/;
57
58 use Koha::Acquisition::Bookseller;
59
60 my $input = new CGI;
61 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
62     {
63         template_name   => "acqui/basketheader.tt",
64         query           => $input,
65         type            => "intranet",
66         authnotrequired => 0,
67        flagsrequired   => { acquisition => 'order_manage' },
68         debug           => 1,
69     }
70 );
71
72 #parameters:
73 my $booksellerid = $input->param('booksellerid');
74 my $basketno = $input->param('basketno');
75 my $branches = GetBranches;
76 my $basket;
77 my $op = $input ->param('op');
78 my $is_an_edit= $input ->param('is_an_edit');
79
80 if ( $op eq 'add_form' ) {
81     my @contractloop;
82     if ( $basketno ) {
83     #this is an edit
84         $basket = GetBasket($basketno);
85         if (! $booksellerid) {
86             $booksellerid=$basket->{'booksellerid'};
87         }
88         my $contracts = GetContracts({
89             booksellerid => $booksellerid,
90             activeonly => 1,
91         });
92
93         @contractloop = @$contracts;
94         for (@contractloop) {
95             if ( $basket->{'contractnumber'} eq $_->{'contractnumber'} ) {
96                 $_->{'selected'} = 1;
97             }
98         }
99         $template->param( is_an_edit => 1);
100     } else {
101     #new basket
102         my $basket;
103         my $contracts = GetContracts({
104             booksellerid => $booksellerid,
105             activeonly => 1,
106         });
107         push(@contractloop, @$contracts);
108     }
109     my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
110     my $count = scalar @contractloop;
111     if ( $count > 0) {
112         $template->param(contractloop => \@contractloop,
113                          basketcontractnumber => $basket->{'contractnumber'});
114     }
115     my @booksellers = Koha::Acquisition::Bookseller->search;
116     $template->param( add_form => 1,
117                     basketname => $basket->{'basketname'},
118                     basketnote => $basket->{'note'},
119                     basketbooksellernote => $basket->{'booksellernote'},
120                     booksellername => $bookseller->{'name'},
121                     booksellerid => $booksellerid,
122                     basketno => $basketno,
123                     booksellers => \@booksellers,
124                     deliveryplace => $basket->{deliveryplace},
125                     billingplace => $basket->{billingplace},
126     );
127
128     my $billingplace = $basket->{'billingplace'} || C4::Context->userenv->{"branch"};
129     my $deliveryplace = $basket->{'deliveryplace'} || C4::Context->userenv->{"branch"};
130
131     # Build the combobox to select the billing place
132
133     my $branches = C4::Branch::GetBranchesLoop( $billingplace );
134     $template->param( billingplaceloop => $branches );
135     $branches = C4::Branch::GetBranchesLoop( $deliveryplace );
136     $template->param( deliveryplaceloop => $branches );
137
138 #End Edit
139 } elsif ( $op eq 'add_validate' ) {
140 #we are confirming the changes, save the basket
141     if ( $is_an_edit ) {
142         ModBasketHeader(
143             $basketno,
144             $input->param('basketname'),
145             $input->param('basketnote'),
146             $input->param('basketbooksellernote'),
147             $input->param('basketcontractnumber') || undef,
148             $input->param('basketbooksellerid'),
149             $input->param('deliveryplace'),
150             $input->param('billingplace'),
151         );
152     } else { #New basket
153         $basketno = NewBasket(
154             $booksellerid,
155             $loggedinuser,
156             $input->param('basketname'),
157             $input->param('basketnote'),
158             $input->param('basketbooksellernote'),
159             $input->param('basketcontractnumber') || undef,
160             $input->param('deliveryplace'),
161             $input->param('billingplace'),
162         );
163     }
164     print $input->redirect('basket.pl?basketno='.$basketno);
165     exit 0;
166 }
167 output_html_with_http_headers $input, $cookie, $template->output;