Bug 19804: Add a 'Fine charging interval' for suspension days
[koha.git] / acqui / neworderempty.pl
blobe9dcc2dda5d9563d1464d5742edd5569105b24d8
1 #!/usr/bin/perl
3 #script to show display basket of orders
4 #written by chris@katipo.co.nz 24/2/2000
6 # Copyright 2000-2002 Katipo Communications
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>.
24 =head1 NAME
26 neworderempty.pl
28 =head1 DESCRIPTION
30 this script allows to create a new record to order it. This record shouldn't exist
31 on database.
33 =head1 CGI PARAMETERS
35 =over 4
37 =item booksellerid
38 the bookseller the librarian has to buy a new book.
40 =item title
41 the title of this new record.
43 =item author
44 the author of this new record.
46 =item publication year
47 the publication year of this new record.
49 =item ordernumber
50 the number of this order.
52 =item biblio
54 =item basketno
55 the basket number for this new order.
57 =item suggestionid
58 if this order comes from a suggestion.
60 =item breedingid
61 the item's id in the breeding reservoir
63 =item close
65 =back
67 =cut
69 use Modern::Perl;
70 use CGI qw ( -utf8 );
71 use C4::Context;
73 use C4::Auth;
74 use C4::Budgets;
76 use C4::Acquisition;
77 use C4::Contract;
78 use C4::Suggestions; # GetSuggestion
79 use C4::Biblio; # GetBiblioData GetMarcPrice
80 use C4::Items; #PrepareItemRecord
81 use C4::Output;
82 use C4::Koha;
83 use C4::Members;
84 use C4::Search qw/FindDuplicate/;
86 #needed for z3950 import:
87 use C4::ImportBatch qw/GetImportRecordMarc SetImportRecordStatus/;
89 use Koha::Acquisition::Booksellers;
90 use Koha::Acquisition::Currencies;
91 use Koha::ItemTypes;
92 use Koha::Patrons;
94 our $input = new CGI;
95 my $booksellerid = $input->param('booksellerid'); # FIXME: else ERROR!
96 my $budget_id = $input->param('budget_id') || 0;
97 my $title = $input->param('title');
98 my $author = $input->param('author');
99 my $publicationyear = $input->param('publicationyear');
100 my $ordernumber = $input->param('ordernumber') || '';
101 our $biblionumber = $input->param('biblionumber');
102 our $basketno = $input->param('basketno');
103 my $suggestionid = $input->param('suggestionid');
104 my $close = $input->param('close');
105 my $uncertainprice = $input->param('uncertainprice');
106 my $import_batch_id = $input->param('import_batch_id'); # if this is filled, we come from a staged file, and we will return here after saving the order !
107 my $subscriptionid = $input->param('subscriptionid');
108 my $data;
109 my $new = 'no';
111 my $budget_name;
113 our ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
115 template_name => "acqui/neworderempty.tt",
116 query => $input,
117 type => "intranet",
118 authnotrequired => 0,
119 flagsrequired => { acquisition => 'order_manage' },
120 debug => 1,
124 our $marcflavour = C4::Context->preference('marcflavour');
126 if(!$basketno) {
127 my $order = GetOrder($ordernumber);
128 $basketno = $order->{'basketno'};
131 our $basket = GetBasket($basketno);
132 my $basketobj = Koha::Acquisition::Baskets->find( $basketno );
133 $booksellerid = $basket->{booksellerid} unless $booksellerid;
134 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
136 my $contract = GetContract({
137 contractnumber => $basket->{contractnumber}
140 #simple parameters reading (all in one :-)
141 our $params = $input->Vars;
142 my $listprice=0; # the price, that can be in MARC record if we have one
143 if ( $ordernumber eq '' and defined $params->{'breedingid'}){
144 #we want to import from the breeding reservoir (from a z3950 search)
145 my ($marcrecord, $encoding) = MARCfindbreeding($params->{'breedingid'});
146 die("Could not find the selected record in the reservoir, bailing") unless $marcrecord;
148 # Remove all the items (952) from the imported record
149 foreach my $item ($marcrecord->field('952')) {
150 $marcrecord->delete_field($item);
153 my $duplicatetitle;
154 #look for duplicates
155 ($biblionumber,$duplicatetitle) = FindDuplicate($marcrecord);
156 if($biblionumber && !$input->param('use_external_source')) {
157 #if duplicate record found and user did not decide yet, first warn user
158 #and let them choose between using a new record or an existing record
159 Load_Duplicate($duplicatetitle);
160 exit;
162 #from this point: add a new record
163 if (C4::Context->preference("BiblioAddsAuthorities")){
164 my $headings_linked=BiblioAutoLink($marcrecord, $params->{'frameworkcode'});
166 my $bibitemnum;
167 $params->{'frameworkcode'} or $params->{'frameworkcode'} = "";
168 ( $biblionumber, $bibitemnum ) = AddBiblio( $marcrecord, $params->{'frameworkcode'} );
169 # get the price if there is one.
170 $listprice = GetMarcPrice($marcrecord, $marcflavour);
171 SetImportRecordStatus($params->{'breedingid'}, 'imported');
176 my ( @order_user_ids, @order_users );
177 if ( $ordernumber eq '' ) { # create order
178 $new = 'yes';
180 # $ordernumber=newordernum;
181 if ( $biblionumber && !$suggestionid ) {
182 $data = GetBiblioData($biblionumber);
185 # get suggestion fields if applicable. If it's a subscription renewal, then the biblio already exists
186 # otherwise, retrieve suggestion information.
187 if ($suggestionid) {
188 $data = ($biblionumber) ? GetBiblioData($biblionumber) : GetSuggestion($suggestionid);
189 $budget_id ||= $data->{'budgetid'} // 0;
192 else { #modify order
193 $data = GetOrder($ordernumber);
194 $biblionumber = $data->{'biblionumber'};
195 $budget_id = $data->{'budget_id'};
197 $basket = GetBasket( $data->{'basketno'} );
198 $basketno = $basket->{'basketno'};
200 @order_user_ids = GetOrderUsers($ordernumber);
201 foreach my $order_user_id (@order_user_ids) {
202 # FIXME Could be improved with search -in
203 my $order_patron = Koha::Patrons->find( $order_user_id );
204 push @order_users, $order_patron if $order_patron;
208 my $suggestion;
209 $suggestion = GetSuggestionInfo($suggestionid) if $suggestionid;
211 my @currencies = Koha::Acquisition::Currencies->search;
212 my $active_currency = Koha::Acquisition::Currencies->get_active;
214 # build bookfund list
215 my $patron = Koha::Patrons->find( $loggedinuser )->unblessed;
217 my $budget = GetBudget($budget_id);
218 # build budget list
219 my $budget_loop = [];
220 my $budgets = GetBudgetHierarchy;
221 foreach my $r (@{$budgets}) {
222 next unless (CanUserUseBudget($patron, $r, $userflags));
223 if (!defined $r->{budget_amount} || $r->{budget_amount} <0) {
224 next;
226 push @{$budget_loop}, {
227 b_id => $r->{budget_id},
228 b_txt => $r->{budget_name},
229 b_sort1_authcat => $r->{'sort1_authcat'},
230 b_sort2_authcat => $r->{'sort2_authcat'},
231 b_active => $r->{budget_period_active},
232 b_sel => ( $r->{budget_id} == $budget_id ) ? 1 : 0,
233 b_level => $r->{budget_level},
237 if ($close) {
238 $budget_id = $data->{'budget_id'};
239 $budget_name = $budget->{'budget_name'};
243 $template->param( sort1 => $data->{'sort1'} );
244 $template->param( sort2 => $data->{'sort2'} );
246 if ($basketobj->effective_create_items eq 'ordering' && !$ordernumber) {
247 # Check if ACQ framework exists
248 my $marc = GetMarcStructure(1, 'ACQ', { unsafe => 1 } );
249 unless($marc) {
250 $template->param('NoACQframework' => 1);
252 $template->param(
253 AcqCreateItemOrdering => 1,
254 UniqueItemFields => C4::Context->preference('UniqueItemFields'),
257 # Get the item types list, but only if item_level_itype is YES. Otherwise, it will be in the item, no need to display it in the biblio
258 my @itemtypes;
259 @itemtypes = Koha::ItemTypes->search unless C4::Context->preference('item-level_itypes');
261 if ( defined $subscriptionid ) {
262 my $lastOrderReceived = GetLastOrderReceivedFromSubscriptionid $subscriptionid;
263 if ( defined $lastOrderReceived ) {
264 $budget_id = $lastOrderReceived->{budgetid};
265 $data->{listprice} = $lastOrderReceived->{listprice};
266 $data->{uncertainprice} = $lastOrderReceived->{uncertainprice};
267 $data->{tax_rate} = $lastOrderReceived->{tax_rate_on_ordering};
268 $data->{discount} = $lastOrderReceived->{discount};
269 $data->{rrp} = $lastOrderReceived->{rrp};
270 $data->{ecost} = $lastOrderReceived->{ecost};
271 $data->{quantity} = $lastOrderReceived->{quantity};
272 $data->{unitprice} = $lastOrderReceived->{unitprice};
273 $data->{order_internalnote} = $lastOrderReceived->{order_internalnote};
274 $data->{order_vendornote} = $lastOrderReceived->{order_vendornote};
275 $data->{sort1} = $lastOrderReceived->{sort1};
276 $data->{sort2} = $lastOrderReceived->{sort2};
278 $basket = GetBasket( $input->param('basketno') );
282 # Find the items.barcode subfield for barcode validations
283 my (undef, $barcode_subfield) = GetMarcFromKohaField('items.barcode', '');
285 # fill template
286 $template->param(
287 close => $close,
288 budget_id => $budget_id,
289 budget_name => $budget_name
290 ) if ($close);
292 # get option values for gist syspref
293 my @gst_values = map {
294 option => $_ + 0.0
295 }, split( '\|', C4::Context->preference("gist") );
297 my $quantity = $input->param('rr_quantity_to_order') ?
298 $input->param('rr_quantity_to_order') :
299 $data->{'quantity'};
300 $quantity //= 0;
302 $template->param(
303 existing => $biblionumber,
304 ordernumber => $ordernumber,
305 # basket informations
306 basketno => $basketno,
307 basket => $basket,
308 basketname => $basket->{'basketname'},
309 basketnote => $basket->{'note'},
310 booksellerid => $basket->{'booksellerid'},
311 basketbooksellernote => $basket->{booksellernote},
312 basketcontractno => $basket->{contractnumber},
313 basketcontractname => $contract->{contractname},
314 creationdate => $basket->{creationdate},
315 authorisedby => $basket->{'authorisedby'},
316 authorisedbyname => $basket->{'authorisedbyname'},
317 closedate => $basket->{'closedate'},
318 # order details
319 suggestionid => $suggestion->{suggestionid},
320 surnamesuggestedby => $suggestion->{surnamesuggestedby},
321 firstnamesuggestedby => $suggestion->{firstnamesuggestedby},
322 biblionumber => $biblionumber,
323 uncertainprice => $data->{'uncertainprice'},
324 discount_2dp => sprintf( "%.2f", $bookseller->discount ) , # for display
325 discount => $bookseller->discount,
326 orderdiscount_2dp => sprintf( "%.2f", $data->{'discount'} || 0 ),
327 orderdiscount => $data->{'discount'},
328 order_internalnote => $data->{'order_internalnote'},
329 order_vendornote => $data->{'order_vendornote'},
330 listincgst => $bookseller->listincgst,
331 invoiceincgst => $bookseller->invoiceincgst,
332 name => $bookseller->name,
333 cur_active_sym => $active_currency->symbol,
334 cur_active => $active_currency->currency,
335 currencies => \@currencies,
336 currency => $data->{currency},
337 vendor_currency => $bookseller->listprice,
338 orderexists => ( $new eq 'yes' ) ? 0 : 1,
339 title => $data->{'title'},
340 author => $data->{'author'},
341 publicationyear => $data->{'publicationyear'} ? $data->{'publicationyear'} : $data->{'copyrightdate'},
342 editionstatement => $data->{'editionstatement'},
343 budget_loop => $budget_loop,
344 isbn => $data->{'isbn'},
345 ean => $data->{'ean'},
346 seriestitle => $data->{'seriestitle'},
347 itemtypeloop => \@itemtypes,
348 quantity => $quantity,
349 quantityrec => $quantity,
350 rrp => $data->{'rrp'},
351 gst_values => \@gst_values,
352 tax_rate => $data->{tax_rate_on_ordering} ? $data->{tax_rate_on_ordering}+0.0 : $bookseller->tax_rate ? $bookseller->tax_rate+0.0 : 0,
353 listprice => sprintf( "%.2f", $data->{listprice} || $data->{price} || $listprice),
354 total => sprintf( "%.2f", ($data->{ecost} || 0) * ($data->{'quantity'} || 0) ),
355 ecost => sprintf( "%.2f", $data->{ecost} || 0),
356 unitprice => sprintf( "%.2f", $data->{unitprice} || 0),
357 publishercode => $data->{'publishercode'},
358 barcode_subfield => $barcode_subfield,
359 import_batch_id => $import_batch_id,
360 subscriptionid => $subscriptionid,
361 acqcreate => $basketobj->effective_create_items eq "ordering" ? 1 : "",
362 users_ids => join(':', @order_user_ids),
363 users => \@order_users,
364 (uc(C4::Context->preference("marcflavour"))) => 1
367 output_html_with_http_headers $input, $cookie, $template->output;
370 =head2 MARCfindbreeding
372 $record = MARCfindbreeding($breedingid);
374 Look up the import record repository for the record with
375 record with id $breedingid. If found, returns the decoded
376 MARC::Record; otherwise, -1 is returned (FIXME).
377 Returns as second parameter the character encoding.
379 =cut
381 sub MARCfindbreeding {
382 my ( $id ) = @_;
383 my ($marc, $encoding) = GetImportRecordMarc($id);
384 # remove the - in isbn, koha store isbn without any -
385 if ($marc) {
386 my $record = MARC::Record->new_from_usmarc($marc);
387 my ($isbnfield,$isbnsubfield) = GetMarcFromKohaField('biblioitems.isbn','');
388 if ( $record->field($isbnfield) ) {
389 foreach my $field ( $record->field($isbnfield) ) {
390 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
391 my $newisbn = $field->subfield($isbnsubfield);
392 $newisbn =~ s/-//g;
393 $field->update( $isbnsubfield => $newisbn );
397 # fix the unimarc 100 coded field (with unicode information)
398 if ($marcflavour eq 'UNIMARC' && $record->subfield(100,'a')) {
399 my $f100a=$record->subfield(100,'a');
400 my $f100 = $record->field(100);
401 my $f100temp = $f100->as_string;
402 $record->delete_field($f100);
403 if ( length($f100temp) > 28 ) {
404 substr( $f100temp, 26, 2, "50" );
405 $f100->update( 'a' => $f100temp );
406 my $f100 = MARC::Field->new( '100', '', '', 'a' => $f100temp );
407 $record->insert_fields_ordered($f100);
411 if ( !defined(ref($record)) ) {
412 return -1;
414 else {
415 # normalize author : probably UNIMARC specific...
416 if ( C4::Context->preference("z3950NormalizeAuthor")
417 and C4::Context->preference("z3950AuthorAuthFields") )
419 my ( $tag, $subfield ) = GetMarcFromKohaField("biblio.author", '');
421 my $auth_fields =
422 C4::Context->preference("z3950AuthorAuthFields");
423 my @auth_fields = split /,/, $auth_fields;
424 my $field;
426 if ( $record->field($tag) ) {
427 foreach my $tmpfield ( $record->field($tag)->subfields ) {
429 my $subfieldcode = shift @$tmpfield;
430 my $subfieldvalue = shift @$tmpfield;
431 if ($field) {
432 $field->add_subfields(
433 "$subfieldcode" => $subfieldvalue )
434 if ( $subfieldcode ne $subfield );
436 else {
437 $field =
438 MARC::Field->new( $tag, "", "",
439 $subfieldcode => $subfieldvalue )
440 if ( $subfieldcode ne $subfield );
444 $record->delete_field( $record->field($tag) );
445 foreach my $fieldtag (@auth_fields) {
446 next unless ( $record->field($fieldtag) );
447 my $lastname = $record->field($fieldtag)->subfield('a');
448 my $firstname = $record->field($fieldtag)->subfield('b');
449 my $title = $record->field($fieldtag)->subfield('c');
450 my $number = $record->field($fieldtag)->subfield('d');
451 if ($title) {
452 $field->add_subfields(
453 "$subfield" => ucfirst($title) . " "
454 . ucfirst($firstname) . " "
455 . $number );
457 else {
458 $field->add_subfields(
459 "$subfield" => ucfirst($firstname) . ", "
460 . ucfirst($lastname) );
463 $record->insert_fields_ordered($field);
465 return $record, $encoding;
468 return -1;
471 sub Load_Duplicate {
472 my ($duplicatetitle)= @_;
473 ($template, $loggedinuser, $cookie) = get_template_and_user(
475 template_name => "acqui/neworderempty_duplicate.tt",
476 query => $input,
477 type => "intranet",
478 authnotrequired => 0,
479 flagsrequired => { acquisition => 'order_manage' },
480 # debug => 1,
484 $template->param(
485 biblionumber => $biblionumber,
486 basketno => $basketno,
487 booksellerid => $basket->{'booksellerid'},
488 breedingid => $params->{'breedingid'},
489 duplicatetitle => $duplicatetitle,
490 (uc(C4::Context->preference("marcflavour"))) => 1
493 output_html_with_http_headers $input, $cookie, $template->output;