Bug 28704: Library MARCOrgCode field needs maxlength attribute
[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 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/;
55
56 use Koha::Acquisition::Booksellers;
57 use Koha::Acquisition::Baskets;
58 use Koha::AdditionalFields;
59
60 my $input = CGI->new;
61 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
62     {
63         template_name   => "acqui/basketheader.tt",
64         query           => $input,
65         type            => "intranet",
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 $template->param( available_additional_fields => [ Koha::AdditionalFields->search( { tablename => 'aqbasket' } ) ] );
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         $template->param(
101             additional_field_values => { map {
102                 $_->field->id => $_->value
103             } Koha::Acquisition::Baskets->find($basketno)->additional_field_values->as_list },
104         );
105     } else {
106     #new basket
107         my $basket;
108         my $contracts = GetContracts({
109             booksellerid => $booksellerid,
110             activeonly => 1,
111         });
112         push(@contractloop, @$contracts);
113     }
114     my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
115     my $count = scalar @contractloop;
116     if ( $count > 0) {
117         $template->param(contractloop => \@contractloop,
118                          basketcontractnumber => $basket->{'contractnumber'});
119     }
120     my @booksellers = Koha::Acquisition::Booksellers->search(
121                         undef,
122                         { order_by => { -asc => 'name' } } );
123
124     $template->param( add_form => 1,
125                     basketname => $basket->{'basketname'},
126                     basketnote => $basket->{'note'},
127                     basketbooksellernote => $basket->{'booksellernote'},
128                     booksellername => $bookseller->name,
129                     booksellerid => $booksellerid,
130                     basketno => $basketno,
131                     booksellers => \@booksellers,
132                     is_standing => $basket->{is_standing},
133                     create_items => $basket->{create_items},
134     );
135
136     my $billingplace = $basket->{'billingplace'} || C4::Context->userenv->{"branch"};
137     my $deliveryplace = $basket->{'deliveryplace'} || C4::Context->userenv->{"branch"};
138
139     $template->param( billingplace => $billingplace );
140     $template->param( deliveryplace => $deliveryplace );
141
142 #End Edit
143 } elsif ( $op eq 'add_validate' ) {
144 #we are confirming the changes, save the basket
145     if ( $is_an_edit ) {
146         ModBasketHeader(
147             $basketno,
148             scalar $input->param('basketname'),
149             scalar $input->param('basketnote'),
150             scalar $input->param('basketbooksellernote'),
151             scalar $input->param('basketcontractnumber') || undef,
152             scalar $input->param('basketbooksellerid'),
153             scalar $input->param('deliveryplace'),
154             scalar $input->param('billingplace'),
155             scalar $input->param('is_standing') ? 1 : undef,
156             scalar $input->param('create_items')
157         );
158     } else { #New basket
159         $basketno = NewBasket(
160             scalar $input->param('basketbooksellerid'),
161             $loggedinuser,
162             scalar $input->param('basketname'),
163             scalar $input->param('basketnote'),
164             scalar $input->param('basketbooksellernote'),
165             scalar $input->param('basketcontractnumber') || undef,
166             scalar $input->param('deliveryplace'),
167             scalar $input->param('billingplace'),
168             scalar $input->param('is_standing') ? 1 : undef,
169             scalar $input->param('create_items')
170         );
171     }
172
173     my @additional_fields;
174     my $basket_fields = Koha::AdditionalFields->search({ tablename => 'aqbasket' });
175     while ( my $field = $basket_fields->next ) {
176         my $value = $input->param('additional_field_' . $field->id);
177         push @additional_fields, {
178             id => $field->id,
179             value => $value,
180         };
181     }
182     Koha::Acquisition::Baskets->find($basketno)->set_additional_fields(\@additional_fields);
183
184     print $input->redirect('basket.pl?basketno='.$basketno);
185     exit 0;
186 }
187 output_html_with_http_headers $input, $cookie, $template->output;