Bug 19049: Fix regression on stage-marc-import with to_marc plugin
[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::Output;
54 use C4::Acquisition qw/GetBasket NewBasket ModBasketHeader/;
55 use C4::Contract qw/GetContracts/;
56
57 use Koha::Acquisition::Booksellers;
58
59 my $input = new CGI;
60 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
61     {
62         template_name   => "acqui/basketheader.tt",
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 $basket;
75 my $op = $input ->param('op');
76 my $is_an_edit= $input ->param('is_an_edit');
77
78 if ( $op eq 'add_form' ) {
79     my @contractloop;
80     if ( $basketno ) {
81     #this is an edit
82         $basket = GetBasket($basketno);
83         if (! $booksellerid) {
84             $booksellerid=$basket->{'booksellerid'};
85         }
86         my $contracts = GetContracts({
87             booksellerid => $booksellerid,
88             activeonly => 1,
89         });
90
91         @contractloop = @$contracts;
92         for (@contractloop) {
93             if ( $basket->{'contractnumber'} eq $_->{'contractnumber'} ) {
94                 $_->{'selected'} = 1;
95             }
96         }
97         $template->param( is_an_edit => 1);
98     } else {
99     #new basket
100         my $basket;
101         my $contracts = GetContracts({
102             booksellerid => $booksellerid,
103             activeonly => 1,
104         });
105         push(@contractloop, @$contracts);
106     }
107     my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
108     my $count = scalar @contractloop;
109     if ( $count > 0) {
110         $template->param(contractloop => \@contractloop,
111                          basketcontractnumber => $basket->{'contractnumber'});
112     }
113     my @booksellers = Koha::Acquisition::Booksellers->search;
114     $template->param( add_form => 1,
115                     basketname => $basket->{'basketname'},
116                     basketnote => $basket->{'note'},
117                     basketbooksellernote => $basket->{'booksellernote'},
118                     booksellername => $bookseller->name,
119                     booksellerid => $booksellerid,
120                     basketno => $basketno,
121                     booksellers => \@booksellers,
122                     is_standing => $basket->{is_standing},
123     );
124
125     my $billingplace = $basket->{'billingplace'} || C4::Context->userenv->{"branch"};
126     my $deliveryplace = $basket->{'deliveryplace'} || C4::Context->userenv->{"branch"};
127
128     $template->param( billingplace => $billingplace );
129     $template->param( deliveryplace => $deliveryplace );
130
131 #End Edit
132 } elsif ( $op eq 'add_validate' ) {
133 #we are confirming the changes, save the basket
134     if ( $is_an_edit ) {
135         ModBasketHeader(
136             $basketno,
137             $input->param('basketname'),
138             $input->param('basketnote'),
139             $input->param('basketbooksellernote'),
140             $input->param('basketcontractnumber') || undef,
141             $input->param('basketbooksellerid'),
142             $input->param('deliveryplace'),
143             $input->param('billingplace'),
144             $input->param('is_standing') ? 1 : undef,
145         );
146     } else { #New basket
147         $basketno = NewBasket(
148             $booksellerid,
149             $loggedinuser,
150             $input->param('basketname'),
151             $input->param('basketnote'),
152             $input->param('basketbooksellernote'),
153             $input->param('basketcontractnumber') || undef,
154             $input->param('deliveryplace'),
155             $input->param('billingplace'),
156             $input->param('is_standing') ? 1 : undef,
157         );
158     }
159     print $input->redirect('basket.pl?basketno='.$basketno);
160     exit 0;
161 }
162 output_html_with_http_headers $input, $cookie, $template->output;