Bug 24813: Prevent api/v1/holds.t to fail randomly
[koha.git] / acqui / basketheader.pl
blob0eb479ff203a946a0e550c2407169afaaca07bc6
1 #!/usr/bin/perl
3 #script to add basket and edit header options (name, notes and contractnumber)
4 #written by john.soros@biblibre.com 15/09/2008
6 # Copyright 2008 - 2009 BibLibre SARL
8 # This file is part of Koha.
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.
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.
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>.
23 =head1 NAME
25 basketheader.pl
27 =head1 DESCRIPTION
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.
32 =head1 CGI PARAMETERS
34 =over 4
36 =item booksellerid
38 C<$booksellerid> is the id of the supplier we add the basket to.
40 =item basketid
42 If it exists, C<$basketno> is the basket we edit
44 =back
46 =cut
48 use Modern::Perl;
49 use CGI qw ( -utf8 );
50 use C4::Context;
51 use C4::Auth;
52 use C4::Output;
53 use C4::Acquisition qw/GetBasket NewBasket ModBasketHeader/;
54 use C4::Contract qw/GetContracts/;
56 use Koha::Acquisition::Booksellers;
57 use Koha::Acquisition::Baskets;
58 use Koha::AdditionalFields;
60 my $input = new CGI;
61 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
63 template_name => "acqui/basketheader.tt",
64 query => $input,
65 type => "intranet",
66 authnotrequired => 0,
67 flagsrequired => { acquisition => 'order_manage' },
68 debug => 1,
72 #parameters:
73 my $booksellerid = $input->param('booksellerid');
74 my $basketno = $input->param('basketno');
75 my $basket;
76 my $op = $input->param('op');
77 my $is_an_edit = $input->param('is_an_edit');
79 $template->param( available_additional_fields => [ Koha::AdditionalFields->search( { tablename => 'aqbasket' } ) ] );
81 if ( $op eq 'add_form' ) {
82 my @contractloop;
83 if ( $basketno ) {
84 #this is an edit
85 $basket = GetBasket($basketno);
86 if (! $booksellerid) {
87 $booksellerid=$basket->{'booksellerid'};
89 my $contracts = GetContracts({
90 booksellerid => $booksellerid,
91 activeonly => 1,
92 });
94 @contractloop = @$contracts;
95 for (@contractloop) {
96 if ( $basket->{'contractnumber'} eq $_->{'contractnumber'} ) {
97 $_->{'selected'} = 1;
100 $template->param( is_an_edit => 1);
101 $template->param(
102 additional_field_values => { map {
103 $_->field->id => $_->value
104 } Koha::Acquisition::Baskets->find($basketno)->additional_field_values->as_list },
106 } else {
107 #new basket
108 my $basket;
109 my $contracts = GetContracts({
110 booksellerid => $booksellerid,
111 activeonly => 1,
113 push(@contractloop, @$contracts);
115 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
116 my $count = scalar @contractloop;
117 if ( $count > 0) {
118 $template->param(contractloop => \@contractloop,
119 basketcontractnumber => $basket->{'contractnumber'});
121 my @booksellers = Koha::Acquisition::Booksellers->search(
122 undef,
123 { order_by => { -asc => 'name' } } );
125 $template->param( add_form => 1,
126 basketname => $basket->{'basketname'},
127 basketnote => $basket->{'note'},
128 basketbooksellernote => $basket->{'booksellernote'},
129 booksellername => $bookseller->name,
130 booksellerid => $booksellerid,
131 basketno => $basketno,
132 booksellers => \@booksellers,
133 is_standing => $basket->{is_standing},
134 create_items => $basket->{create_items},
137 my $billingplace = $basket->{'billingplace'} || C4::Context->userenv->{"branch"};
138 my $deliveryplace = $basket->{'deliveryplace'} || C4::Context->userenv->{"branch"};
140 $template->param( billingplace => $billingplace );
141 $template->param( deliveryplace => $deliveryplace );
143 #End Edit
144 } elsif ( $op eq 'add_validate' ) {
145 #we are confirming the changes, save the basket
146 if ( $is_an_edit ) {
147 ModBasketHeader(
148 $basketno,
149 scalar $input->param('basketname'),
150 scalar $input->param('basketnote'),
151 scalar $input->param('basketbooksellernote'),
152 scalar $input->param('basketcontractnumber') || undef,
153 scalar $input->param('basketbooksellerid'),
154 scalar $input->param('deliveryplace'),
155 scalar $input->param('billingplace'),
156 scalar $input->param('is_standing') ? 1 : undef,
157 scalar $input->param('create_items')
159 } else { #New basket
160 $basketno = NewBasket(
161 $booksellerid,
162 $loggedinuser,
163 scalar $input->param('basketname'),
164 scalar $input->param('basketnote'),
165 scalar $input->param('basketbooksellernote'),
166 scalar $input->param('basketcontractnumber') || undef,
167 scalar $input->param('deliveryplace'),
168 scalar $input->param('billingplace'),
169 scalar $input->param('is_standing') ? 1 : undef,
170 scalar $input->param('create_items')
174 my @additional_fields;
175 my $basket_fields = Koha::AdditionalFields->search({ tablename => 'aqbasket' });
176 while ( my $field = $basket_fields->next ) {
177 my $value = $input->param('additional_field_' . $field->id);
178 push @additional_fields, {
179 id => $field->id,
180 value => $value,
183 Koha::Acquisition::Baskets->find($basketno)->set_additional_fields(\@additional_fields);
185 print $input->redirect('basket.pl?basketno='.$basketno);
186 exit 0;
188 output_html_with_http_headers $input, $cookie, $template->output;