Bug 26569: Use gender neutral pronouns in system prefernece explanations
[koha.git] / acqui / addorder.pl
1 #!/usr/bin/perl
2
3 #script to add an order into the system
4 #written 29/2/00 by chris@katipo.co.nz
5
6 # Copyright 2000-2002 Katipo Communications
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
24 =head1 NAME
25
26 addorder.pl
27
28 =head1 DESCRIPTION
29
30 this script allows to add an order.
31 It is called by :
32
33 =over
34
35 =item neworderbiblio.pl to add an order from nothing.
36
37 =item neworderempty.pl to add an order from an existing biblio.
38
39 =item newordersuggestion.pl to add an order from an existing suggestion.
40
41 =back
42
43 =head1 CGI PARAMETERS
44
45 All of the cgi parameters below are related to the new order.
46
47 =over
48
49 =item C<ordernumber>
50 the number of this new order.
51
52 =item C<basketno>
53 the number of this new basket
54
55 =item C<booksellerid>
56 the bookseller the librarian has to pay.
57
58 =item C<existing>
59
60 =item C<title>
61 the title of the record ordered.
62
63 =item C<author>
64 the author of the record ordered.
65
66 =item C<copyrightdate>
67 the copyrightdate of the record ordered.
68
69 =item C<ISBN>
70 the ISBN of the record ordered.
71
72 =item C<format>
73
74 =item C<quantity>
75 the quantity to order.
76
77 =item C<list_price>
78 the price of this order.
79
80 =item C<uncertainprice>
81 uncertain price, can't close basket until prices of all orders are known.
82
83 =item C<branch>
84 the branch where this order will be received.
85
86 =item C<series>
87
88 =item C<notes>
89 Notes on this basket.
90
91 =item C<budget_id>
92 budget_id used to pay this order.
93
94 =item C<sort1> & C<sort2>
95
96 =item C<rrp>
97
98 =item C<ecost>
99
100 =item C<GST>
101
102 =item C<budget>
103
104 =item C<cost>
105
106 =item C<sub>
107
108 =item C<invoice>
109 the number of the invoice for this order.
110
111 =item C<publishercode>
112
113 =item C<suggestionid>
114 if it is an order from an existing suggestion : the id of this suggestion.
115
116 =item C<donation>
117
118 =back
119
120 =cut
121
122 use Modern::Perl;
123 use CGI qw ( -utf8 );
124 use C4::Auth;           # get_template_and_user
125 use C4::Acquisition;    # ModOrder
126 use C4::Suggestions;    # ModStatus
127 use C4::Biblio;         # AddBiblio TransformKohaToMarc
128 use C4::Budgets;
129 use C4::Items;
130 use C4::Output;
131 use Koha::Acquisition::Currencies;
132 use Koha::Acquisition::Orders;
133 use C4::Barcodes;
134
135 ### "-------------------- addorder.pl ----------"
136
137 # FIXME: This needs to do actual error checking and possibly return user to the same form,
138 # not just blindly call C4 functions and print a redirect.  
139
140 my $input = new CGI;
141 my $use_ACQ_framework = $input->param('use_ACQ_framework');
142
143 # Check if order total amount exceed allowed budget
144 my $confirm_budget_exceeding = $input->param('confirm_budget_exceeding');
145 unless($confirm_budget_exceeding) {
146     my $budget_id = $input->param('budget_id');
147     my $total = $input->param('total');
148     my $budget = GetBudget($budget_id);
149     my $budget_spent = GetBudgetSpent($budget_id);
150     my $budget_ordered = GetBudgetOrdered($budget_id);
151     my $budget_used = $budget_spent + $budget_ordered;
152     my $budget_remaining = $budget->{budget_amount} - $budget_used;
153     my $budget_encumbrance = $budget->{budget_amount} * $budget->{budget_encumb} / 100;
154     my $budget_expenditure = $budget->{budget_expend};
155
156     if ( $total > $budget_remaining
157       || ( ($budget_encumbrance+0) && ($budget_used + $total) > $budget_encumbrance)
158       || ( ($budget_expenditure+0) && ($budget_used + $total) > $budget_expenditure) )
159     {
160         my ($template, $loggedinuser, $cookie) = get_template_and_user({
161             template_name   => "acqui/addorder.tt",
162             query           => $input,
163             type            => "intranet",
164             flagsrequired   => {acquisition => 'order_manage'},
165         });
166
167         my $url = $input->referer();
168         unless ( defined $url ) {
169             my $basketno = $input->param('basketno');
170             $url = "/cgi-bin/koha/acqui/basket.pl?basketno=$basketno";
171         }
172
173         my $vars = $input->Vars;
174         my @vars_loop;
175         foreach (keys %$vars) {
176             push @vars_loop, {
177                 name => $_,
178                 values => [$input->param($_)],
179             };
180         }
181
182         if( ($budget_encumbrance+0) && ($budget_used + $total) > $budget_encumbrance
183           && $total <= $budget_remaining)
184         {
185             $template->param(
186                 encumbrance_exceeded => 1,
187                 encumbrance => sprintf("%.2f", $budget->{'budget_encumb'}),
188             );
189         }
190         if( ($budget_expenditure+0) && ($budget_used + $total) > $budget_expenditure
191           && $total <= $budget_remaining )
192         {
193             my $currency = Koha::Acquisition::Currencies->get_active;
194             $template->param(
195                 expenditure_exceeded => 1,
196                 expenditure => sprintf("%.2f", $budget_expenditure),
197                 currency => ($currency) ? $currency->symbol : '',
198             );
199         }
200         if($total > $budget_remaining){
201             $template->param(budget_exceeded => 1);
202         }
203
204         $template->param(
205             not_enough_budget => 1,
206             referer => $url,
207             vars_loop => \@vars_loop,
208         );
209         output_html_with_http_headers $input, $cookie, $template->output;
210         exit;
211     }
212 }
213
214 # get_template_and_user used only to check auth & get user id
215 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
216     {
217         template_name   => "acqui/booksellers.tt",
218         query           => $input,
219         type            => "intranet",
220         flagsrequired   => { acquisition => 'order_manage' },
221         debug           => 1,
222     }
223 );
224
225 # get CGI parameters
226 my $orderinfo = $input->Vars;
227 $orderinfo->{'list_price'}    ||=  0;
228 $orderinfo->{'uncertainprice'} ||= 0;
229 $orderinfo->{subscriptionid} ||= undef;
230
231 my $user     = $input->remote_user;
232 my $basketno = $$orderinfo{basketno};
233 my $basket   = Koha::Acquisition::Baskets->find($basketno);
234
235 # create, modify or delete biblio
236 # create if $quantity>0 and $existing='no'
237 # modify if $quantity>0 and $existing='yes'
238 if ( $basket->{is_standing} || $orderinfo->{quantity} ne '0' ) {
239     #TODO:check to see if biblio exists
240     unless ( $$orderinfo{biblionumber} ) {
241
242         my $record;
243         if ( $use_ACQ_framework ) {
244             my @tags         = $input->multi_param('bib_tag');
245             my @subfields    = $input->multi_param('bib_subfield');
246             my @field_values = $input->multi_param('bib_field_value');
247             my $xml = TransformHtmlToXml( \@tags, \@subfields, \@field_values );
248             $record=MARC::Record::new_from_xml($xml, 'UTF-8');
249         } else {
250             #if it doesn't create it
251             $record = TransformKohaToMarc(
252                 {
253                     "biblio.title"                => "$$orderinfo{title}",
254                     "biblio.author"               => $$orderinfo{author}          ? $$orderinfo{author}        : "",
255                     "biblio.seriestitle"          => $$orderinfo{series}          ? $$orderinfo{series}        : "",
256                     "biblioitems.isbn"            => $$orderinfo{isbn}            ? $$orderinfo{isbn}          : "",
257                     "biblioitems.ean"             => $$orderinfo{ean}             ? $$orderinfo{ean}           : "",
258                     "biblioitems.publishercode"   => $$orderinfo{publishercode}   ? $$orderinfo{publishercode} : "",
259                     "biblioitems.publicationyear" => $$orderinfo{publicationyear} ? $$orderinfo{publicationyear}: "",
260                     "biblio.copyrightdate"        => $$orderinfo{publicationyear} ? $$orderinfo{publicationyear}: "",
261                     "biblioitems.itemtype"        => $$orderinfo{itemtype} ? $$orderinfo{itemtype} : "",
262                     "biblioitems.editionstatement"=> $$orderinfo{editionstatement} ? $$orderinfo{editionstatement} : "",
263                 });
264
265         }
266         C4::Acquisition::FillWithDefaultValues( $record );
267
268         # create the record in catalogue, with framework ''
269         my ($biblionumber,$bibitemnum) = AddBiblio($record,'');
270         # change suggestion status if applicable
271         if ($$orderinfo{suggestionid}) {
272             ModSuggestion( {suggestionid=>$$orderinfo{suggestionid}, STATUS=>'ORDERED', biblionumber=>$biblionumber} );
273         }
274         $orderinfo->{biblionumber}=$biblionumber;
275     }
276
277     $orderinfo->{unitprice} = $orderinfo->{ecost} if not defined $orderinfo->{unitprice} or $orderinfo->{unitprice} eq '';
278
279     $orderinfo = C4::Acquisition::populate_order_with_prices(
280         {
281             order        => $orderinfo,
282             booksellerid => $orderinfo->{booksellerid},
283             ordering     => 1,
284         }
285     );
286
287     # if we already have $ordernumber, then it's an ordermodif
288     my $order = Koha::Acquisition::Order->new($orderinfo);
289     if ( $orderinfo->{ordernumber} ) {
290         ModOrder($orderinfo);
291     }
292     else { # else, it's a new line
293         $order->store;
294     }
295     my $order_users_ids = $input->param('users_ids');
296     my @order_users = split( /:/, $order_users_ids );
297     ModOrderUsers( $order->ordernumber, @order_users );
298
299     # now, add items if applicable
300     if ($basket->effective_create_items eq 'ordering') {
301
302         my @tags         = $input->multi_param('tag');
303         my @subfields    = $input->multi_param('subfield');
304         my @field_values = $input->multi_param('field_value');
305         my @serials      = $input->multi_param('serial');
306         my @itemid       = $input->multi_param('itemid');
307         my @ind_tag      = $input->multi_param('ind_tag');
308         my @indicator    = $input->multi_param('indicator');
309         #Rebuilding ALL the data for items into a hash
310         # parting them on $itemid.
311
312         my %itemhash;
313         my $countdistinct;
314         my $range=scalar(@itemid);
315         for (my $i=0; $i<$range; $i++){
316             unless ($itemhash{$itemid[$i]}){
317             $countdistinct++;
318             }
319         push @{$itemhash{$itemid[$i]}->{'tags'}},$tags[$i];
320         push @{$itemhash{$itemid[$i]}->{'subfields'}},$subfields[$i];
321             push @{$itemhash{$itemid[$i]}->{'field_values'}},$field_values[$i];
322             push @{$itemhash{$itemid[$i]}->{'ind_tag'}},$ind_tag[$i];
323             push @{$itemhash{$itemid[$i]}->{'indicator'}},$indicator[$i];
324         }
325         foreach my $item (keys %itemhash){
326             my $xml = TransformHtmlToXml( $itemhash{$item}->{'tags'},
327                                     $itemhash{$item}->{'subfields'},
328                                     $itemhash{$item}->{'field_values'},
329                                     $itemhash{$item}->{'indicator'},
330                                     $itemhash{$item}->{'ind_tag'},
331                                     'ITEM');
332             my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
333             my ($barcodefield,$barcodesubfield) = GetMarcFromKohaField('items.barcode');
334             next unless ( defined $barcodefield && defined $barcodesubfield );
335             my $barcode = $record->subfield($barcodefield,$barcodesubfield) || '';
336             my $aBpref = C4::Context->preference('autoBarcode');
337             if( $barcode eq '' && $aBpref ne 'OFF'){
338                 my $barcodeobj;
339                 if ( $aBpref eq 'hbyymmincr'){
340                     my ($homebranchfield,$homebranchsubfield) = GetMarcFromKohaField('items.homebranch');
341                     my $homebranch = $record->subfield($homebranchfield,$homebranchsubfield);
342                     $barcodeobj = C4::Barcodes->new($aBpref, $homebranch);
343                 } else {
344                     $barcodeobj = C4::Barcodes->new($aBpref);
345                 }
346                 $barcode = $barcodeobj->value();
347                 $record->field($barcodefield)->delete_subfield( code => $barcodesubfield);
348                 $record->field($barcodefield)->add_subfields($barcodesubfield => $barcode);
349             }
350             my ($biblionumber,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$$orderinfo{biblionumber});
351             $order->add_item($itemnumber);
352         }
353     }
354
355 }
356
357 my $booksellerid=$$orderinfo{booksellerid};
358 if (my $import_batch_id=$$orderinfo{import_batch_id}) {
359     print $input->redirect("/cgi-bin/koha/acqui/addorderiso2709.pl?import_batch_id=$import_batch_id&basketno=$basketno&booksellerid=$booksellerid");
360 } elsif ( defined $orderinfo->{invoiceid} ) {
361     print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=" . $orderinfo->{invoiceid});
362 } else {
363     print $input->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=$basketno");
364 }