a7cd098b64eaa1fba42573221fc89288c8cd154f
[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 qw( get_template_and_user );
52 use C4::Output qw( output_html_with_http_headers );
53 use C4::Acquisition qw( GetBasket ModBasket ModBasketHeader NewBasket );
54 use C4::Contract qw( GetContracts GetContract );
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     }
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 $template->param( available_additional_fields => [ Koha::AdditionalFields->search( { tablename => 'aqbasket' } ) ] );
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         my $contracts = GetContracts({
88             booksellerid => $booksellerid,
89             activeonly => 1,
90         });
91
92         @contractloop = @$contracts;
93         for (@contractloop) {
94             if ( $basket->{'contractnumber'} eq $_->{'contractnumber'} ) {
95                 $_->{'selected'} = 1;
96             }
97         }
98         $template->param( is_an_edit => 1);
99         $template->param(
100             additional_field_values => { map {
101                 $_->field->id => $_->value
102             } Koha::Acquisition::Baskets->find($basketno)->additional_field_values->as_list },
103         );
104     } else {
105     #new basket
106         my $basket;
107         my $contracts = GetContracts({
108             booksellerid => $booksellerid,
109             activeonly => 1,
110         });
111         push(@contractloop, @$contracts);
112     }
113     my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
114     my $count = scalar @contractloop;
115     if ( $count > 0) {
116         $template->param(contractloop => \@contractloop,
117                          basketcontractnumber => $basket->{'contractnumber'});
118     }
119     my @booksellers = Koha::Acquisition::Booksellers->search(
120                         undef,
121                         { order_by => { -asc => 'name' } } );
122
123     $template->param( add_form => 1,
124                     basketname => $basket->{'basketname'},
125                     basketnote => $basket->{'note'},
126                     basketbooksellernote => $basket->{'booksellernote'},
127                     booksellername => $bookseller->name,
128                     booksellerid => $booksellerid,
129                     basketno => $basketno,
130                     booksellers => \@booksellers,
131                     is_standing => $basket->{is_standing},
132                     create_items => $basket->{create_items},
133     );
134
135     my $billingplace = $basket->{'billingplace'} || C4::Context->userenv->{"branch"};
136     my $deliveryplace = $basket->{'deliveryplace'} || C4::Context->userenv->{"branch"};
137
138     $template->param( billingplace => $billingplace );
139     $template->param( deliveryplace => $deliveryplace );
140
141 #End Edit
142 } elsif ( $op eq 'add_validate' ) {
143 #we are confirming the changes, save the basket
144     if ( $is_an_edit ) {
145         ModBasketHeader(
146             $basketno,
147             scalar $input->param('basketname'),
148             scalar $input->param('basketnote'),
149             scalar $input->param('basketbooksellernote'),
150             scalar $input->param('basketcontractnumber') || undef,
151             scalar $input->param('basketbooksellerid'),
152             scalar $input->param('deliveryplace'),
153             scalar $input->param('billingplace'),
154             scalar $input->param('is_standing') ? 1 : undef,
155             scalar $input->param('create_items')
156         );
157     } else { #New basket
158         $basketno = NewBasket(
159             scalar $input->param('basketbooksellerid'),
160             $loggedinuser,
161             scalar $input->param('basketname'),
162             scalar $input->param('basketnote'),
163             scalar $input->param('basketbooksellernote'),
164             scalar $input->param('basketcontractnumber') || undef,
165             scalar $input->param('deliveryplace'),
166             scalar $input->param('billingplace'),
167             scalar $input->param('is_standing') ? 1 : undef,
168             scalar $input->param('create_items')
169         );
170     }
171
172     my @additional_fields;
173     my $basket_fields = Koha::AdditionalFields->search({ tablename => 'aqbasket' });
174     while ( my $field = $basket_fields->next ) {
175         my $value = $input->param('additional_field_' . $field->id);
176         push @additional_fields, {
177             id => $field->id,
178             value => $value,
179         };
180     }
181     Koha::Acquisition::Baskets->find($basketno)->set_additional_fields(\@additional_fields);
182
183     print $input->redirect('basket.pl?basketno='.$basketno);
184     exit 0;
185 }
186 output_html_with_http_headers $input, $cookie, $template->output;