Merge remote-tracking branch 'origin/new/bug_7986'
[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 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 =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;
51 use C4::Context;
52 use C4::Auth;
53 use C4::Branch;
54 use C4::Output;
55 use C4::Acquisition qw/GetBasket NewBasket GetContracts ModBasketHeader/;
56 use C4::Bookseller qw/GetBookSellerFromId GetBookSeller/;
57
58
59 my $input = new CGI;
60 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
61     {
62         template_name   => "acqui/basketheader.tmpl",
63         query           => $input,
64         type            => "intranet",
65         authnotrequired => 0,
66        flagsrequired   => { acquisition => 'order_manage' },
67         debug           => 1,
68     }
69 );
70
71 #parameters:
72 my $booksellerid = $input->param('booksellerid');
73 my $basketno = $input->param('basketno');
74 my $branches = GetBranches;
75 my $basket;
76 my $op = $input ->param('op');
77 my $is_an_edit= $input ->param('is_an_edit');
78
79 if ( $op eq 'add_form' ) {
80     my @contractloop;
81     if ( $basketno ) {
82     #this is an edit
83         $basket = GetBasket($basketno);
84         if (! $booksellerid) {
85             $booksellerid=$basket->{'booksellerid'};
86         }
87         @contractloop = &GetContracts($booksellerid, 1);
88         for (@contractloop) {
89             if ( $basket->{'contractnumber'} eq $_->{'contractnumber'} ) {
90                 $_->{'selected'} = 1;
91             }
92         }
93         $template->param( is_an_edit => 1);
94     } else {
95     #new basket
96         my $basket;
97         push(@contractloop, &GetContracts($booksellerid, 1));
98     }
99     my $bookseller = GetBookSellerFromId($booksellerid);
100     my $count = scalar @contractloop;
101     if ( $count > 0) {
102         $template->param(contractloop => \@contractloop,
103                          basketcontractnumber => $basket->{'contractnumber'});
104     }
105     my @booksellers = C4::Bookseller::GetBookSeller();
106     $template->param( add_form => 1,
107                     basketname => $basket->{'basketname'},
108                     basketnote => $basket->{'note'},
109                     basketbooksellernote => $basket->{'booksellernote'},
110                     booksellername => $bookseller->{'name'},
111                     booksellerid => $booksellerid,
112                     basketno => $basketno,
113                     booksellers => \@booksellers,
114                     deliveryplace => $basket->{deliveryplace},
115                     billingplace => $basket->{billingplace},
116     );
117
118     my $billingplace = $basket->{'billingplace'} || C4::Context->userenv->{"branch"};
119     my $deliveryplace = $basket->{'deliveryplace'} || C4::Context->userenv->{"branch"};
120
121     # Build the combobox to select the billing place
122     my @billingplaceloop;
123
124     my $branches = C4::Branch::GetBranchesLoop( $billingplace );
125     $template->param( billingplaceloop => $branches );
126     $branches = C4::Branch::GetBranchesLoop( $deliveryplace );
127     $template->param( deliveryplaceloop => $branches );
128
129 #End Edit
130 } elsif ( $op eq 'add_validate' ) {
131 #we are confirming the changes, save the basket
132     if ( $is_an_edit ) {
133         ModBasketHeader(
134             $basketno,
135             $input->param('basketname'),
136             $input->param('basketnote'),
137             $input->param('basketbooksellernote'),
138             $input->param('basketcontractnumber') || undef,
139             $input->param('basketbooksellerid'),
140             $input->param('deliveryplace'),
141             $input->param('billingplace'),
142         );
143     } else { #New basket
144         $basketno = NewBasket(
145             $booksellerid,
146             $loggedinuser,
147             $input->param('basketname'),
148             $input->param('basketnote'),
149             $input->param('basketbooksellernote'),
150             $input->param('basketcontractnumber') || undef,
151             undef,
152             $input->param('deliveryplace'),
153             $input->param('billingplace'),
154         );
155     }
156     print $input->redirect('basket.pl?basketno='.$basketno);
157     exit 0;
158 }
159 output_html_with_http_headers $input, $cookie, $template->output;