Merge remote-tracking branch 'origin/new/bug_6858'
[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::Output;
54 use C4::Acquisition qw/GetBasket NewBasket GetContracts ModBasketHeader/;
55 use C4::Bookseller qw/GetBookSellerFromId GetBookSeller/;
56
57
58 my $input = new CGI;
59 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
60     {
61         template_name   => "acqui/basketheader.tmpl",
62         query           => $input,
63         type            => "intranet",
64         authnotrequired => 0,
65        flagsrequired   => { acquisition => 'order_manage' },
66         debug           => 1,
67     }
68 );
69
70 #parameters:
71 my $booksellerid = $input->param('booksellerid');
72 my $basketno = $input->param('basketno');
73 my $basket;
74 my $op = $input ->param('op');
75 my $is_an_edit= $input ->param('is_an_edit');
76
77 if ( $op eq 'add_form' ) {
78     my @contractloop;
79     if ( $basketno ) {
80     #this is an edit
81         $basket = GetBasket($basketno);
82         if (! $booksellerid) {
83             $booksellerid=$basket->{'booksellerid'};
84         }
85         @contractloop = &GetContracts($booksellerid, 1);
86         for (@contractloop) {
87             if ( $basket->{'contractnumber'} eq $_->{'contractnumber'} ) {
88                 $_->{'selected'} = 1;
89             }
90         }
91         $template->param( is_an_edit => 1);
92     } else {
93     #new basket
94         my $basket;
95         push(@contractloop, &GetContracts($booksellerid, 1));
96     }
97     my $bookseller = GetBookSellerFromId($booksellerid);
98     my $count = scalar @contractloop;
99     if ( $count > 0) {
100         $template->param(contractloop => \@contractloop,
101                          basketcontractnumber => $basket->{'contractnumber'});
102     }
103     my @booksellers = GetBookSeller();
104     $template->param( add_form => 1,
105                     basketname => $basket->{'basketname'},
106                     basketnote => $basket->{'note'},
107                     basketbooksellernote => $basket->{'booksellernote'},
108                     booksellername => $bookseller->{'name'},
109                     booksellerid => $booksellerid,
110                     basketno => $basketno,
111                     booksellers => \@booksellers,
112         );
113 #End Edit
114 } elsif ( $op eq 'add_validate' ) {
115 #we are confirming the changes, save the basket
116     my $basketno;
117     if ( $is_an_edit ) {
118         $basketno = $input->param('basketno');
119         ModBasketHeader( $input->param('basketno'), $input->param('basketname'), $input->param('basketnote'), $input->param('basketbooksellernote'), $input->param('basketcontractnumber') || undef, $input->param('basketbooksellerid') );
120     } else { #New basket
121         $basketno = NewBasket($booksellerid, $loggedinuser, $input->param('basketname'), $input->param('basketnote'), $input->param('basketbooksellernote'), $input->param('basketcontractnumber'));
122     }
123     print $input->redirect('basket.pl?basketno='.$basketno);
124     exit 0;
125 }
126 output_html_with_http_headers $input, $cookie, $template->output;