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 under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 this script allows to create a new record to order it. This record shouldn't exist
38 the bookseller the librarian has to buy a new book.
41 the title of this new record.
44 the author of this new record.
46 =item publication year
47 the publication year of this new record.
50 the number of this order.
55 the basket number for this new order.
58 if this order comes from a suggestion.
61 the item's id in the breeding reservoir
79 use C4::Bookseller qw/ GetBookSellerFromId /;
81 use C4::Suggestions; # GetSuggestion
82 use C4::Biblio; # GetBiblioData GetMarcPrice
83 use C4::Items; #PrepareItemRecord
87 use C4::Branch; # GetBranches
89 use C4::Search qw/FindDuplicate/;
91 #needed for z3950 import:
92 use C4::ImportBatch qw/GetImportRecordMarc SetImportRecordStatus/;
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 $bookseller = GetBookSellerFromId($booksellerid); # FIXME: else ERROR!
101 my $ordernumber = $input->param('ordernumber') || '';
102 my $biblionumber = $input->param('biblionumber');
103 my $basketno = $input->param('basketno');
104 my $suggestionid = $input->param('suggestionid');
105 my $close = $input->param('close');
106 my $uncertainprice = $input->param('uncertainprice');
107 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 !
113 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
115 template_name => "acqui/neworderempty.tmpl",
118 authnotrequired => 0,
119 flagsrequired => { acquisition => 'order_manage' },
124 my $marcflavour = C4::Context->preference('marcflavour');
127 my $order = GetOrder($ordernumber);
128 $basketno = $order->{'basketno'};
131 my $basket = GetBasket($basketno);
132 my $contract = &GetContract($basket->{contractnumber});
134 #simple parameters reading (all in one :-)
135 my $params = $input->Vars;
136 my $listprice=0; # the price, that can be in MARC record if we have one
137 if ( $ordernumber eq '' and defined $params->{'breedingid'}){
138 #we want to import from the breeding reservoir (from a z3950 search)
139 my ($marcrecord, $encoding) = MARCfindbreeding($params->{'breedingid'});
140 die("Could not find the selected record in the reservoir, bailing") unless $marcrecord;
142 # Remove all the items (952) from the imported record
143 foreach my $item ($marcrecord->field('952')) {
144 $marcrecord->delete_field($item);
149 ($biblionumber,$duplicatetitle) = FindDuplicate($marcrecord);
150 if($biblionumber && !$input->param('use_external_source')) {
151 #if duplicate record found and user did not decide yet, first warn user
152 #and let him choose between using new record or existing record
153 Load_Duplicate($duplicatetitle);
156 #from this point: add a new record
157 if (C4::Context->preference("BiblioAddsAuthorities")){
158 my $headings_linked=BiblioAutoLink($marcrecord, $params->{'frameworkcode'});
161 $params->{'frameworkcode'} or $params->{'frameworkcode'} = "";
162 ( $biblionumber, $bibitemnum ) = AddBiblio( $marcrecord, $params->{'frameworkcode'} );
163 # get the price if there is one.
164 $listprice = GetMarcPrice($marcrecord, $marcflavour);
165 SetImportRecordStatus($params->{'breedingid'}, 'imported');
170 if ( $ordernumber eq '' ) { # create order
173 # $ordernumber=newordernum;
174 if ( $biblionumber && !$suggestionid ) {
175 $data = GetBiblioData($biblionumber);
178 # get suggestion fields if applicable. If it's a subscription renewal, then the biblio already exists
179 # otherwise, retrieve suggestion information.
181 $data = ($biblionumber) ? GetBiblioData($biblionumber) : GetSuggestion($suggestionid);
185 $data = GetOrder($ordernumber);
186 $biblionumber = $data->{'biblionumber'};
187 $budget_id = $data->{'budget_id'};
189 #get basketno and supplierno. too!
190 my $data2 = GetBasket( $data->{'basketno'} );
191 $basketno = $data2->{'basketno'};
192 $booksellerid = $data2->{'booksellerid'};
196 $suggestion = GetSuggestionInfo($suggestionid) if $suggestionid;
198 # get currencies (for change rates calcs if needed)
199 my $active_currency = GetCurrency();
200 my $default_currency;
201 if (! $data->{currency} ) { # New order no currency set
202 if ( $bookseller->{listprice} ) {
203 $default_currency = $bookseller->{listprice};
206 $default_currency = $active_currency->{currency};
210 my @rates = GetCurrencies();
214 my @loop_currency = ();
215 for my $curr ( @rates ) {
217 if ($data->{currency} ) {
218 $selected = $curr->{currency} eq $data->{currency};
221 $selected = $curr->{currency} eq $default_currency;
223 push @loop_currency, {
224 currcode => $curr->{currency},
225 rate => $curr->{rate},
226 selected => $selected,
230 # build branches list
231 my $onlymine=C4::Context->preference('IndependantBranches') &&
232 C4::Context->userenv &&
233 C4::Context->userenv->{flags}!=1 &&
234 C4::Context->userenv->{branch};
235 my $branches = GetBranches($onlymine);
237 foreach my $thisbranch ( sort {$branches->{$a}->{'branchname'} cmp $branches->{$b}->{'branchname'}} keys %$branches ) {
239 value => $thisbranch,
240 branchname => $branches->{$thisbranch}->{'branchname'},
242 $row{'selected'} = 1 if( $thisbranch && $data->{branchcode} && $thisbranch eq $data->{branchcode}) ;
243 push @branchloop, \%row;
245 $template->param( branchloop => \@branchloop );
247 # build bookfund list
248 my $borrower= GetMember('borrowernumber' => $loggedinuser);
249 my ( $flags, $homebranch )= ($borrower->{'flags'},$borrower->{'branchcode'});
251 my $budget = GetBudget($budget_id);
253 my $budget_loop = [];
254 my $budgets = GetBudgetHierarchy(q{},$borrower->{branchcode},$borrower->{borrowernumber});
255 foreach my $r (@{$budgets}) {
256 if (!defined $r->{budget_amount} || $r->{budget_amount} == 0) {
259 push @{$budget_loop}, {
260 b_id => $r->{budget_id},
261 b_txt => $r->{budget_name},
262 b_active => $r->{budget_period_active},
263 b_sel => ( $r->{budget_id} == $budget_id ) ? 1 : 0,
268 sort { uc( $a->{b_txt}) cmp uc( $b->{b_txt}) } @{$budget_loop};
271 $budget_id = $data->{'budget_id'};
272 $budget_name = $budget->{'budget_name'};
277 if ($budget) { # its a mod ..
278 if ( defined $budget->{'sort1_authcat'} ) { # with custom Asort* planning values
279 $CGIsort1 = GetAuthvalueDropbox( $budget->{'sort1_authcat'}, $data->{'sort1'} );
281 } elsif(@{$budgets}){
282 $CGIsort1 = GetAuthvalueDropbox( @$budgets[0]->{'sort1_authcat'}, '' );
284 $CGIsort1 = GetAuthvalueDropbox( '', '' );
287 # if CGIsort is successfully fetched, the use it
288 # else - failback to plain input-field
290 $template->param( CGIsort1 => $CGIsort1 );
292 $template->param( sort1 => $data->{'sort1'} );
297 if ( defined $budget->{'sort2_authcat'} ) {
298 $CGIsort2 = GetAuthvalueDropbox( $budget->{'sort2_authcat'}, $data->{'sort2'} );
300 } elsif(@{$budgets}) {
301 $CGIsort2 = GetAuthvalueDropbox( @$budgets[0]->{sort2_authcat}, '' );
303 $CGIsort2 = GetAuthvalueDropbox( '', '' );
307 $template->param( CGIsort2 => $CGIsort2 );
309 $template->param( sort2 => $data->{'sort2'} );
312 if (C4::Context->preference('AcqCreateItem') eq 'ordering' && !$ordernumber) {
313 # Check if ACQ framework exists
314 my $marc = GetMarcStructure(1, 'ACQ');
316 $template->param('NoACQframework' => 1);
319 AcqCreateItemOrdering => 1,
320 UniqueItemFields => C4::Context->preference('UniqueItemFields'),
323 # 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
325 @itemtypes = C4::ItemType->all unless C4::Context->preference('item-level_itypes');
327 # Find the items.barcode subfield for barcode validations
328 my (undef, $barcode_subfield) = GetMarcFromKohaField('items.barcode', '');
333 budget_id => $budget_id,
334 budget_name => $budget_name
338 existing => $biblionumber,
339 ordernumber => $ordernumber,
340 # basket informations
341 basketno => $basketno,
342 basketname => $basket->{'basketname'},
343 basketnote => $basket->{'note'},
344 booksellerid => $basket->{'booksellerid'},
345 basketbooksellernote => $basket->{booksellernote},
346 basketcontractno => $basket->{contractnumber},
347 basketcontractname => $contract->{contractname},
348 creationdate => $basket->{creationdate},
349 authorisedby => $basket->{'authorisedby'},
350 authorisedbyname => $basket->{'authorisedbyname'},
351 closedate => $basket->{'closedate'},
353 suggestionid => $suggestion->{suggestionid},
354 surnamesuggestedby => $suggestion->{surnamesuggestedby},
355 firstnamesuggestedby => $suggestion->{firstnamesuggestedby},
356 biblionumber => $biblionumber,
357 uncertainprice => $data->{'uncertainprice'},
358 authorisedbyname => $borrower->{'firstname'} . " " . $borrower->{'surname'},
359 biblioitemnumber => $data->{'biblioitemnumber'},
360 discount_2dp => sprintf( "%.2f", $bookseller->{'discount'}) , # for display
361 discount => $bookseller->{'discount'},
362 listincgst => $bookseller->{'listincgst'},
363 invoiceincgst => $bookseller->{'invoiceincgst'},
364 name => $bookseller->{'name'},
365 cur_active_sym => $active_currency->{'symbol'},
366 cur_active => $active_currency->{'currency'},
367 loop_currencies => \@loop_currency,
368 orderexists => ( $new eq 'yes' ) ? 0 : 1,
369 title => $data->{'title'},
370 author => $data->{'author'},
371 publicationyear => $data->{'publicationyear'} ? $data->{'publicationyear'} : $data->{'copyrightdate'},
372 editionstatement => $data->{'editionstatement'},
373 budget_loop => $budget_loop,
374 isbn => $data->{'isbn'},
375 ean => $data->{'ean'},
376 seriestitle => $data->{'seriestitle'},
377 itemtypeloop => \@itemtypes,
378 quantity => $data->{'quantity'},
379 quantityrec => $data->{'quantity'},
380 rrp => $data->{'rrp'},
381 listprice => sprintf("%.2f", $data->{'listprice'}||$data->{'price'}||$listprice),
382 total => sprintf("%.2f", ($data->{'ecost'}||0)*($data->{'quantity'}||0) ),
383 ecost => $data->{'ecost'},
384 unitprice => sprintf("%.2f", $data->{'unitprice'}||0),
385 notes => $data->{'notes'},
386 publishercode => $data->{'publishercode'},
387 barcode_subfield => $barcode_subfield,
389 import_batch_id => $import_batch_id,
391 # CHECKME: gst-stuff needs verifing, mason.
392 gstrate => $bookseller->{'gstrate'} // C4::Context->preference("gist") // 0,
393 gstreg => $bookseller->{'gstreg'},
394 (uc(C4::Context->preference("marcflavour"))) => 1
397 output_html_with_http_headers $input, $cookie, $template->output;
400 =head2 MARCfindbreeding
402 $record = MARCfindbreeding($breedingid);
404 Look up the import record repository for the record with
405 record with id $breedingid. If found, returns the decoded
406 MARC::Record; otherwise, -1 is returned (FIXME).
407 Returns as second parameter the character encoding.
411 sub MARCfindbreeding {
413 my ($marc, $encoding) = GetImportRecordMarc($id);
414 # remove the - in isbn, koha store isbn without any -
416 my $record = MARC::Record->new_from_usmarc($marc);
417 my ($isbnfield,$isbnsubfield) = GetMarcFromKohaField('biblioitems.isbn','');
418 if ( $record->field($isbnfield) ) {
419 foreach my $field ( $record->field($isbnfield) ) {
420 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
421 my $newisbn = $field->subfield($isbnsubfield);
423 $field->update( $isbnsubfield => $newisbn );
427 # fix the unimarc 100 coded field (with unicode information)
428 if ($marcflavour eq 'UNIMARC' && $record->subfield(100,'a')) {
429 my $f100a=$record->subfield(100,'a');
430 my $f100 = $record->field(100);
431 my $f100temp = $f100->as_string;
432 $record->delete_field($f100);
433 if ( length($f100temp) > 28 ) {
434 substr( $f100temp, 26, 2, "50" );
435 $f100->update( 'a' => $f100temp );
436 my $f100 = MARC::Field->new( '100', '', '', 'a' => $f100temp );
437 $record->insert_fields_ordered($f100);
441 if ( !defined(ref($record)) ) {
445 # normalize author : probably UNIMARC specific...
446 if ( C4::Context->preference("z3950NormalizeAuthor")
447 and C4::Context->preference("z3950AuthorAuthFields") )
449 my ( $tag, $subfield ) = GetMarcFromKohaField("biblio.author");
451 # my $summary = C4::Context->preference("z3950authortemplate");
453 C4::Context->preference("z3950AuthorAuthFields");
454 my @auth_fields = split /,/, $auth_fields;
457 if ( $record->field($tag) ) {
458 foreach my $tmpfield ( $record->field($tag)->subfields ) {
460 # foreach my $subfieldcode ($tmpfield->subfields){
461 my $subfieldcode = shift @$tmpfield;
462 my $subfieldvalue = shift @$tmpfield;
464 $field->add_subfields(
465 "$subfieldcode" => $subfieldvalue )
466 if ( $subfieldcode ne $subfield );
470 MARC::Field->new( $tag, "", "",
471 $subfieldcode => $subfieldvalue )
472 if ( $subfieldcode ne $subfield );
476 $record->delete_field( $record->field($tag) );
477 foreach my $fieldtag (@auth_fields) {
478 next unless ( $record->field($fieldtag) );
479 my $lastname = $record->field($fieldtag)->subfield('a');
480 my $firstname = $record->field($fieldtag)->subfield('b');
481 my $title = $record->field($fieldtag)->subfield('c');
482 my $number = $record->field($fieldtag)->subfield('d');
485 # $field->add_subfields("$subfield"=>"[ ".ucfirst($title).ucfirst($firstname)." ".$number." ]");
486 $field->add_subfields(
487 "$subfield" => ucfirst($title) . " "
488 . ucfirst($firstname) . " "
493 # $field->add_subfields("$subfield"=>"[ ".ucfirst($firstname).", ".ucfirst($lastname)." ]");
494 $field->add_subfields(
495 "$subfield" => ucfirst($firstname) . ", "
496 . ucfirst($lastname) );
499 $record->insert_fields_ordered($field);
501 return $record, $encoding;
508 my ($duplicatetitle)= @_;
509 ($template, $loggedinuser, $cookie) = get_template_and_user(
511 template_name => "acqui/neworderempty_duplicate.tmpl",
514 authnotrequired => 0,
515 flagsrequired => { acquisition => 'order_manage' },
521 biblionumber => $biblionumber,
522 basketno => $basketno,
523 booksellerid => $basket->{'booksellerid'},
524 breedingid => $params->{'breedingid'},
525 duplicatetitle => $duplicatetitle,
526 (uc(C4::Context->preference("marcflavour"))) => 1
529 output_html_with_http_headers $input, $cookie, $template->output;