Bug 7038 Contextual help is always in English
[koha.git] / acqui / neworderempty.pl
blob6361125eeeda706c0505b133fad802b2f05bfd0a
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 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
13 # version.
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.
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 warnings;
70 use strict;
71 use CGI;
72 use C4::Context;
73 use C4::Input;
75 use C4::Auth;
76 use C4::Budgets;
77 use C4::Input;
78 use C4::Dates;
80 use C4::Bookseller qw/ GetBookSellerFromId /;
81 use C4::Acquisition;
82 use C4::Suggestions; # GetSuggestion
83 use C4::Biblio; # GetBiblioData
84 use C4::Output;
85 use C4::Input;
86 use C4::Koha;
87 use C4::Branch; # GetBranches
88 use C4::Members;
89 use C4::Search qw/FindDuplicate BiblioAddAuthorities/;
91 #needed for z3950 import:
92 use C4::ImportBatch qw/GetImportRecordMarc SetImportRecordStatus/;
94 my $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 $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 !
108 my $data;
109 my $new = 'no';
111 my $budget_name;
113 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
115 template_name => "acqui/neworderempty.tmpl",
116 query => $input,
117 type => "intranet",
118 authnotrequired => 0,
119 flagsrequired => { acquisition => 'order_manage' },
120 debug => 1,
124 my $basket = GetBasket($basketno);
125 my $contract = &GetContract($basket->{contractnumber});
127 #simple parameters reading (all in one :-)
128 my $params = $input->Vars;
129 my $listprice=0; # the price, that can be in MARC record if we have one
130 if ( $ordernumber eq '' and defined $params->{'breedingid'}){
131 #we want to import from the breeding reservoir (from a z3950 search)
132 my ($marcrecord, $encoding) = MARCfindbreeding($params->{'breedingid'});
133 die("Could not find the selected record in the reservoir, bailing") unless $marcrecord;
135 # Remove all the items (952) from the imported record
136 foreach my $item ($marcrecord->field('952')) {
137 $marcrecord->delete_field($item);
140 my $duplicatetitle;
141 #look for duplicates
142 ($biblionumber,$duplicatetitle) = FindDuplicate($marcrecord);
143 if($biblionumber && !$input->param('use_external_source')) {
144 #if duplicate record found and user did not decide yet, first warn user
145 #and let him choose between using new record or existing record
146 Load_Duplicate($duplicatetitle);
147 exit;
149 #from this point: add a new record
150 if (C4::Context->preference("BiblioAddsAuthorities")){
151 my ($countlinked,$countcreated)=BiblioAddAuthorities($marcrecord, $params->{'frameworkcode'});
153 my $bibitemnum;
154 $params->{'frameworkcode'} or $params->{'frameworkcode'} = "";
155 ( $biblionumber, $bibitemnum ) = AddBiblio( $marcrecord, $params->{'frameworkcode'} );
156 # get the price if there is one.
157 # filter by storing only the 1st number
158 # we suppose the currency is correct, as we have no possibilities to get it.
159 if ($marcrecord->subfield("345","d")) {
160 $listprice = $marcrecord->subfield("345","d");
161 if ($listprice =~ /^([\d\.,]*)/) {
162 $listprice = $1;
163 $listprice =~ s/,/\./;
164 } else {
165 $listprice = 0;
168 elsif ($marcrecord->subfield("010","d")) {
169 $listprice = $marcrecord->subfield("010","d");
170 if ($listprice =~ /^([\d\.,]*)/) {
171 $listprice = $1;
172 $listprice =~ s/,/\./;
173 } else {
174 $listprice = 0;
177 SetImportRecordStatus($params->{'breedingid'}, 'imported');
182 if ( $ordernumber eq '' ) { # create order
183 $new = 'yes';
185 # $ordernumber=newordernum;
186 if ( $biblionumber && !$suggestionid ) {
187 $data = GetBiblioData($biblionumber);
190 # get suggestion fields if applicable. If it's a subscription renewal, then the biblio already exists
191 # otherwise, retrieve suggestion information.
192 if ($suggestionid) {
193 $data = ($biblionumber) ? GetBiblioData($biblionumber) : GetSuggestion($suggestionid);
196 else { #modify order
197 $data = GetOrder($ordernumber);
198 $biblionumber = $data->{'biblionumber'};
199 $budget_id = $data->{'budget_id'};
201 #get basketno and supplierno. too!
202 my $data2 = GetBasket( $data->{'basketno'} );
203 $basketno = $data2->{'basketno'};
204 $booksellerid = $data2->{'booksellerid'};
207 # get currencies (for change rates calcs if needed)
208 my $active_currency = GetCurrency();
209 my $default_currency;
210 if (! $data->{currency} ) { # New order no currency set
211 if ( $bookseller->{listprice} ) {
212 $default_currency = $bookseller->{listprice};
214 else {
215 $default_currency = $active_currency->{currency};
219 my @rates = GetCurrencies();
221 # ## @rates
223 my @loop_currency = ();
224 for my $curr ( @rates ) {
225 my $selected;
226 if ($data->{currency} ) {
227 $selected = $curr->{currency} eq $data->{currency};
229 else {
230 $selected = $curr->{currency} eq $default_currency;
232 push @loop_currency, {
233 currcode => $curr->{currency},
234 rate => $curr->{rate},
235 selected => $selected,
239 # build branches list
240 my $onlymine=C4::Context->preference('IndependantBranches') &&
241 C4::Context->userenv &&
242 C4::Context->userenv->{flags}!=1 &&
243 C4::Context->userenv->{branch};
244 my $branches = GetBranches($onlymine);
245 my @branchloop;
246 foreach my $thisbranch ( sort {$branches->{$a}->{'branchname'} cmp $branches->{$b}->{'branchname'}} keys %$branches ) {
247 my %row = (
248 value => $thisbranch,
249 branchname => $branches->{$thisbranch}->{'branchname'},
251 $row{'selected'} = 1 if( $thisbranch && $data->{branchcode} && $thisbranch eq $data->{branchcode}) ;
252 push @branchloop, \%row;
254 $template->param( branchloop => \@branchloop );
256 # build bookfund list
257 my $borrower= GetMember('borrowernumber' => $loggedinuser);
258 my ( $flags, $homebranch )= ($borrower->{'flags'},$borrower->{'branchcode'});
260 my $budget = GetBudget($budget_id);
261 # build budget list
262 my $budget_loop = [];
263 my $budgets = GetBudgetHierarchy(q{},$borrower->{branchcode},$borrower->{borrowernumber});
264 foreach my $r (@{$budgets}) {
265 if (!defined $r->{budget_amount} || $r->{budget_amount} == 0) {
266 next;
268 push @{$budget_loop}, {
269 b_id => $r->{budget_id},
270 b_txt => $r->{budget_name},
271 b_active => $r->{budget_period_active},
272 b_sel => ( $r->{budget_id} == $budget_id ) ? 1 : 0,
277 if ($close) {
278 $budget_id = $data->{'budget_id'};
279 $budget_name = $budget->{'budget_name'};
283 my $CGIsort1;
284 if ($budget) { # its a mod ..
285 if ( defined $budget->{'sort1_authcat'} ) { # with custom Asort* planning values
286 $CGIsort1 = GetAuthvalueDropbox( $budget->{'sort1_authcat'}, $data->{'sort1'} );
288 } elsif(@{$budgets}){
289 $CGIsort1 = GetAuthvalueDropbox( @$budgets[0]->{'sort1_authcat'}, '' );
290 }else{
291 $CGIsort1 = GetAuthvalueDropbox( '', '' );
294 # if CGIsort is successfully fetched, the use it
295 # else - failback to plain input-field
296 if ($CGIsort1) {
297 $template->param( CGIsort1 => $CGIsort1 );
298 } else {
299 $template->param( sort1 => $data->{'sort1'} );
302 my $CGIsort2;
303 if ($budget) {
304 if ( defined $budget->{'sort2_authcat'} ) {
305 $CGIsort2 = GetAuthvalueDropbox( $budget->{'sort2_authcat'}, $data->{'sort2'} );
307 } elsif(@{$budgets}) {
308 $CGIsort2 = GetAuthvalueDropbox( @$budgets[0]->{sort2_authcat}, '' );
309 }else{
310 $CGIsort2 = GetAuthvalueDropbox( '', '' );
313 if ($CGIsort2) {
314 $template->param( CGIsort2 => $CGIsort2 );
315 } else {
316 $template->param( sort2 => $data->{'sort2'} );
319 if (C4::Context->preference('AcqCreateItem') eq 'ordering' && !$ordernumber) {
320 # prepare empty item form
321 my $cell = PrepareItemrecordDisplay('','','','ACQ');
322 # warn "==> ".Data::Dumper::Dumper($cell);
323 unless ($cell) {
324 $cell = PrepareItemrecordDisplay('','','','');
325 $template->param('NoACQframework' => 1);
327 my @itemloop;
328 push @itemloop,$cell;
330 $template->param(items => \@itemloop);
332 # 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
333 my @itemtypes;
334 @itemtypes = C4::ItemType->all unless C4::Context->preference('item-level_itypes');
336 # fill template
337 $template->param(
338 close => $close,
339 budget_id => $budget_id,
340 budget_name => $budget_name
341 ) if ($close);
343 $template->param(
344 existing => $biblionumber,
345 ordernumber => $ordernumber,
346 # basket informations
347 basketno => $basketno,
348 basketname => $basket->{'basketname'},
349 basketnote => $basket->{'note'},
350 booksellerid => $basket->{'booksellerid'},
351 basketbooksellernote => $basket->{booksellernote},
352 basketcontractno => $basket->{contractnumber},
353 basketcontractname => $contract->{contractname},
354 creationdate => C4::Dates->new($basket->{creationdate},'iso')->output,
355 authorisedby => $basket->{'authorisedby'},
356 authorisedbyname => $basket->{'authorisedbyname'},
357 closedate => C4::Dates->new($basket->{'closedate'},'iso')->output,
358 # order details
359 suggestionid => $suggestionid,
360 biblionumber => $biblionumber,
361 uncertainprice => $data->{'uncertainprice'},
362 authorisedbyname => $borrower->{'firstname'} . " " . $borrower->{'surname'},
363 biblioitemnumber => $data->{'biblioitemnumber'},
364 discount_2dp => sprintf( "%.2f", $bookseller->{'discount'}) , # for display
365 discount => $bookseller->{'discount'},
366 listincgst => $bookseller->{'listincgst'},
367 invoiceincgst => $bookseller->{'invoiceincgst'},
368 name => $bookseller->{'name'},
369 cur_active_sym => $active_currency->{'symbol'},
370 cur_active => $active_currency->{'currency'},
371 loop_currencies => \@loop_currency,
372 orderexists => ( $new eq 'yes' ) ? 0 : 1,
373 title => $data->{'title'},
374 author => $data->{'author'},
375 publicationyear => $data->{'publicationyear'} ? $data->{'publicationyear'} : $data->{'copyrightdate'},
376 budget_loop => $budget_loop,
377 isbn => $data->{'isbn'},
378 seriestitle => $data->{'seriestitle'},
379 itemtypeloop => \@itemtypes,
380 quantity => $data->{'quantity'},
381 quantityrec => $data->{'quantity'},
382 rrp => $data->{'rrp'},
383 listprice => sprintf("%.2f", $data->{'listprice'}||$data->{'price'}||$listprice),
384 total => sprintf("%.2f", ($data->{'ecost'}||0)*($data->{'quantity'}||0) ),
385 ecost => $data->{'ecost'},
386 notes => $data->{'notes'},
387 publishercode => $data->{'publishercode'},
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'},
396 output_html_with_http_headers $input, $cookie, $template->output;
399 =head2 MARCfindbreeding
401 $record = MARCfindbreeding($breedingid);
403 Look up the import record repository for the record with
404 record with id $breedingid. If found, returns the decoded
405 MARC::Record; otherwise, -1 is returned (FIXME).
406 Returns as second parameter the character encoding.
408 =cut
410 sub MARCfindbreeding {
411 my ( $id ) = @_;
412 my ($marc, $encoding) = GetImportRecordMarc($id);
413 # remove the - in isbn, koha store isbn without any -
414 if ($marc) {
415 my $record = MARC::Record->new_from_usmarc($marc);
416 my ($isbnfield,$isbnsubfield) = GetMarcFromKohaField('biblioitems.isbn','');
417 if ( $record->field($isbnfield) ) {
418 foreach my $field ( $record->field($isbnfield) ) {
419 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
420 my $newisbn = $field->subfield($isbnsubfield);
421 $newisbn =~ s/-//g;
422 $field->update( $isbnsubfield => $newisbn );
426 # fix the unimarc 100 coded field (with unicode information)
427 if (C4::Context->preference('marcflavour') eq 'UNIMARC' && $record->subfield(100,'a')) {
428 my $f100a=$record->subfield(100,'a');
429 my $f100 = $record->field(100);
430 my $f100temp = $f100->as_string;
431 $record->delete_field($f100);
432 if ( length($f100temp) > 28 ) {
433 substr( $f100temp, 26, 2, "50" );
434 $f100->update( 'a' => $f100temp );
435 my $f100 = MARC::Field->new( '100', '', '', 'a' => $f100temp );
436 $record->insert_fields_ordered($f100);
440 if ( !defined(ref($record)) ) {
441 return -1;
443 else {
444 # normalize author : probably UNIMARC specific...
445 if ( C4::Context->preference("z3950NormalizeAuthor")
446 and C4::Context->preference("z3950AuthorAuthFields") )
448 my ( $tag, $subfield ) = GetMarcFromKohaField("biblio.author");
450 # my $summary = C4::Context->preference("z3950authortemplate");
451 my $auth_fields =
452 C4::Context->preference("z3950AuthorAuthFields");
453 my @auth_fields = split /,/, $auth_fields;
454 my $field;
456 if ( $record->field($tag) ) {
457 foreach my $tmpfield ( $record->field($tag)->subfields ) {
459 # foreach my $subfieldcode ($tmpfield->subfields){
460 my $subfieldcode = shift @$tmpfield;
461 my $subfieldvalue = shift @$tmpfield;
462 if ($field) {
463 $field->add_subfields(
464 "$subfieldcode" => $subfieldvalue )
465 if ( $subfieldcode ne $subfield );
467 else {
468 $field =
469 MARC::Field->new( $tag, "", "",
470 $subfieldcode => $subfieldvalue )
471 if ( $subfieldcode ne $subfield );
475 $record->delete_field( $record->field($tag) );
476 foreach my $fieldtag (@auth_fields) {
477 next unless ( $record->field($fieldtag) );
478 my $lastname = $record->field($fieldtag)->subfield('a');
479 my $firstname = $record->field($fieldtag)->subfield('b');
480 my $title = $record->field($fieldtag)->subfield('c');
481 my $number = $record->field($fieldtag)->subfield('d');
482 if ($title) {
484 # $field->add_subfields("$subfield"=>"[ ".ucfirst($title).ucfirst($firstname)." ".$number." ]");
485 $field->add_subfields(
486 "$subfield" => ucfirst($title) . " "
487 . ucfirst($firstname) . " "
488 . $number );
490 else {
492 # $field->add_subfields("$subfield"=>"[ ".ucfirst($firstname).", ".ucfirst($lastname)." ]");
493 $field->add_subfields(
494 "$subfield" => ucfirst($firstname) . ", "
495 . ucfirst($lastname) );
498 $record->insert_fields_ordered($field);
500 return $record, $encoding;
503 return -1;
506 sub Load_Duplicate {
507 my ($duplicatetitle)= @_;
508 ($template, $loggedinuser, $cookie) = get_template_and_user(
510 template_name => "acqui/neworderempty_duplicate.tmpl",
511 query => $input,
512 type => "intranet",
513 authnotrequired => 0,
514 flagsrequired => { acquisition => 'order_manage' },
515 # debug => 1,
519 $template->param(
520 biblionumber => $biblionumber,
521 basketno => $basketno,
522 booksellerid => $basket->{'booksellerid'},
523 breedingid => $params->{'breedingid'},
524 duplicatetitle => $duplicatetitle,
527 output_html_with_http_headers $input, $cookie, $template->output;