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
80 use C4::Bookseller qw/ GetBookSellerFromId /;
82 use C4::Suggestions; # GetSuggestion
83 use C4::Biblio; # GetBiblioData
87 use C4::Branch; # GetBranches
89 use C4::Search qw/FindDuplicate BiblioAddAuthorities/;
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; # FIXME: else ERROR!
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 $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;
137 ($biblionumber,$duplicatetitle) = FindDuplicate($marcrecord);
138 if($biblionumber && !$input->param('use_external_source')) {
139 #if duplicate record found and user did not decide yet, first warn user
140 #and let him choose between using new record or existing record
141 Load_Duplicate($duplicatetitle);
144 #from this point: add a new record
145 if (C4::Context->preference("BiblioAddsAuthorities")){
146 my ($countlinked,$countcreated)=BiblioAddAuthorities($marcrecord, $params->{'frameworkcode'});
149 $params->{'frameworkcode'} or $params->{'frameworkcode'} = "";
150 ( $biblionumber, $bibitemnum ) = AddBiblio( $marcrecord, $params->{'frameworkcode'} );
151 # get the price if there is one.
152 # filter by storing only the 1st number
153 # we suppose the currency is correct, as we have no possibilities to get it.
154 if ($marcrecord->subfield("345","d")) {
155 $listprice = $marcrecord->subfield("345","d");
156 if ($listprice =~ /^([\d\.,]*)/) {
158 $listprice =~ s/,/\./;
163 elsif ($marcrecord->subfield("010","d")) {
164 $listprice = $marcrecord->subfield("010","d");
165 if ($listprice =~ /^([\d\.,]*)/) {
167 $listprice =~ s/,/\./;
172 SetImportRecordStatus($params->{'breedingid'}, 'imported');
176 my $cur = GetCurrency();
178 if ( $ordernumber eq '' ) { # create order
181 # $ordernumber=newordernum;
182 if ( $biblionumber && !$suggestionid ) {
183 $data = GetBiblioData($biblionumber);
186 # get suggestion fields if applicable. If it's a subscription renewal, then the biblio already exists
187 # otherwise, retrieve suggestion information.
189 $data = ($biblionumber) ? GetBiblioData($biblionumber) : GetSuggestion($suggestionid);
193 $data = GetOrder($ordernumber);
194 $biblionumber = $data->{'biblionumber'};
195 $budget_id = $data->{'budget_id'};
197 #get basketno and supplierno. too!
198 my $data2 = GetBasket( $data->{'basketno'} );
199 $basketno = $data2->{'basketno'};
200 $booksellerid = $data2->{'booksellerid'};
203 # get currencies (for change rates calcs if needed)
204 my @rates = GetCurrencies();
205 my $count = scalar @rates;
209 my @loop_currency = ();
210 for ( my $i = 0 ; $i < $count ; $i++ ) {
212 $line{currency} = $rates[$i]->{'currency'};
213 $line{rate} = $rates[$i]->{'rate'};
214 push @loop_currency, \%line;
217 # build branches list
218 my $onlymine=C4::Context->preference('IndependantBranches') &&
219 C4::Context->userenv &&
220 C4::Context->userenv->{flags}!=1 &&
221 C4::Context->userenv->{branch};
222 my $branches = GetBranches($onlymine);
224 foreach my $thisbranch ( sort {$branches->{$a}->{'branchname'} cmp $branches->{$b}->{'branchname'}} keys %$branches ) {
226 value => $thisbranch,
227 branchname => $branches->{$thisbranch}->{'branchname'},
229 $row{'selected'} = 1 if( $thisbranch eq $data->{branchcode}) ;
230 push @branchloop, \%row;
232 $template->param( branchloop => \@branchloop );
234 # build bookfund list
235 my $borrower= GetMember('borrowernumber' => $loggedinuser);
236 my ( $flags, $homebranch )= ($borrower->{'flags'},$borrower->{'branchcode'});
238 my $budget = GetBudget($budget_id);
240 my $budget_loop = [];
241 my $budgets = GetBudgetHierarchy(q{},$borrower->{branchcode},$borrower->{borrowernumber});
242 foreach my $r (@{$budgets}) {
243 if (!defined $r->{budget_amount} || $r->{budget_amount} == 0) {
246 push @{$budget_loop}, {
247 b_id => $r->{budget_id},
248 b_txt => $r->{budget_name},
249 b_sel => ( $r->{budget_id} == $budget_id ) ? 1 : 0,
255 $budget_id = $data->{'budget_id'};
256 $budget_name = $budget->{'budget_name'};
261 if ($budget) { # its a mod ..
262 if ( defined $budget->{'sort1_authcat'} ) { # with custom Asort* planning values
263 $CGIsort1 = GetAuthvalueDropbox( 'sort1', $budget->{'sort1_authcat'}, $data->{'sort1'} );
265 } elsif(scalar(@$budgets)){
266 $CGIsort1 = GetAuthvalueDropbox( 'sort1', @$budgets[0]->{'sort1_authcat'}, '' );
268 $CGIsort1 = GetAuthvalueDropbox( 'sort1','', '' );
271 # if CGIsort is successfully fetched, the use it
272 # else - failback to plain input-field
274 $template->param( CGIsort1 => $CGIsort1 );
276 $template->param( sort1 => $data->{'sort1'} );
281 if ( defined $budget->{'sort2_authcat'} ) {
282 $CGIsort2 = GetAuthvalueDropbox( 'sort2', $budget->{'sort2_authcat'}, $data->{'sort2'} );
284 } elsif(scalar(@$budgets)) {
285 $CGIsort2 = GetAuthvalueDropbox( 'sort2', @$budgets[0]->{sort2_authcat}, '' );
287 $CGIsort2 = GetAuthvalueDropbox( 'sort2','', '' );
291 $template->param( CGIsort2 => $CGIsort2 );
293 $template->param( sort2 => $data->{'sort2'} );
296 if (C4::Context->preference('AcqCreateItem') eq 'ordering' && !$ordernumber) {
297 # prepare empty item form
298 my $cell = PrepareItemrecordDisplay('','','','ACQ');
299 # warn "==> ".Data::Dumper::Dumper($cell);
301 $cell = PrepareItemrecordDisplay('','','','');
302 $template->param('NoACQframework' => 1);
305 push @itemloop,$cell;
307 $template->param(items => \@itemloop);
313 budget_id => $budget_id,
314 budget_name => $budget_name
318 existing => $biblionumber,
319 ordernumber => $ordernumber,
320 # basket informations
321 basketno => $basketno,
322 basketname => $basket->{'basketname'},
323 basketnote => $basket->{'note'},
324 booksellerid => $basket->{'booksellerid'},
325 basketbooksellernote => $basket->{booksellernote},
326 basketcontractno => $basket->{contractnumber},
327 basketcontractname => $contract->{contractname},
328 creationdate => C4::Dates->new($basket->{creationdate},'iso')->output,
329 authorisedby => $basket->{'authorisedby'},
330 authorisedbyname => $basket->{'authorisedbyname'},
331 closedate => C4::Dates->new($basket->{'closedate'},'iso')->output,
333 suggestionid => $suggestionid,
334 biblionumber => $biblionumber,
335 uncertainprice => $data->{'uncertainprice'},
336 authorisedbyname => $borrower->{'firstname'} . " " . $borrower->{'surname'},
337 biblioitemnumber => $data->{'biblioitemnumber'},
338 discount_2dp => sprintf( "%.2f", $bookseller->{'discount'}) , # for display
339 discount => $bookseller->{'discount'},
340 listincgst => $bookseller->{'listincgst'},
341 invoiceincgst => $bookseller->{'invoiceincgst'},
342 name => $bookseller->{'name'},
343 cur_active_sym => $cur->{'symbol'},
344 cur_active => $cur->{'currency'},
345 currency => $bookseller->{'listprice'} || $cur->{'currency'}, # eg: 'EUR'
346 loop_currencies => \@loop_currency,
347 orderexists => ( $new eq 'yes' ) ? 0 : 1,
348 title => $data->{'title'},
349 author => $data->{'author'},
350 publicationyear => $data->{'publicationyear'} ? $data->{'publicationyear'} : $data->{'copyrightdate'},
351 budget_loop => $budget_loop,
352 isbn => $data->{'isbn'},
353 seriestitle => $data->{'seriestitle'},
354 quantity => $data->{'quantity'},
355 quantityrec => $data->{'quantity'},
356 rrp => $data->{'rrp'},
357 listprice => sprintf("%.2f", $data->{'listprice'}||$listprice),
358 total => sprintf("%.2f", ($data->{'ecost'}||0)*($data->{'quantity'}||0) ),
359 ecost => $data->{'ecost'},
360 notes => $data->{'notes'},
361 publishercode => $data->{'publishercode'},
363 import_batch_id => $import_batch_id,
365 # CHECKME: gst-stuff needs verifing, mason.
366 gstrate => $bookseller->{'gstrate'} // C4::Context->preference("gist") // 0,
367 gstreg => $bookseller->{'gstreg'},
370 output_html_with_http_headers $input, $cookie, $template->output;
373 =head2 MARCfindbreeding
375 $record = MARCfindbreeding($breedingid);
377 Look up the import record repository for the record with
378 record with id $breedingid. If found, returns the decoded
379 MARC::Record; otherwise, -1 is returned (FIXME).
380 Returns as second parameter the character encoding.
384 sub MARCfindbreeding {
386 my ($marc, $encoding) = GetImportRecordMarc($id);
387 # remove the - in isbn, koha store isbn without any -
389 my $record = MARC::Record->new_from_usmarc($marc);
390 my ($isbnfield,$isbnsubfield) = GetMarcFromKohaField('biblioitems.isbn','');
391 if ( $record->field($isbnfield) ) {
392 foreach my $field ( $record->field($isbnfield) ) {
393 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
394 my $newisbn = $field->subfield($isbnsubfield);
396 $field->update( $isbnsubfield => $newisbn );
400 # fix the unimarc 100 coded field (with unicode information)
401 if (C4::Context->preference('marcflavour') eq 'UNIMARC' && $record->subfield(100,'a')) {
402 my $f100a=$record->subfield(100,'a');
403 my $f100 = $record->field(100);
404 my $f100temp = $f100->as_string;
405 $record->delete_field($f100);
406 if ( length($f100temp) > 28 ) {
407 substr( $f100temp, 26, 2, "50" );
408 $f100->update( 'a' => $f100temp );
409 my $f100 = MARC::Field->new( '100', '', '', 'a' => $f100temp );
410 $record->insert_fields_ordered($f100);
414 if ( !defined(ref($record)) ) {
418 # normalize author : probably UNIMARC specific...
419 if ( C4::Context->preference("z3950NormalizeAuthor")
420 and C4::Context->preference("z3950AuthorAuthFields") )
422 my ( $tag, $subfield ) = GetMarcFromKohaField("biblio.author");
424 # my $summary = C4::Context->preference("z3950authortemplate");
426 C4::Context->preference("z3950AuthorAuthFields");
427 my @auth_fields = split /,/, $auth_fields;
430 if ( $record->field($tag) ) {
431 foreach my $tmpfield ( $record->field($tag)->subfields ) {
433 # foreach my $subfieldcode ($tmpfield->subfields){
434 my $subfieldcode = shift @$tmpfield;
435 my $subfieldvalue = shift @$tmpfield;
437 $field->add_subfields(
438 "$subfieldcode" => $subfieldvalue )
439 if ( $subfieldcode ne $subfield );
443 MARC::Field->new( $tag, "", "",
444 $subfieldcode => $subfieldvalue )
445 if ( $subfieldcode ne $subfield );
449 $record->delete_field( $record->field($tag) );
450 foreach my $fieldtag (@auth_fields) {
451 next unless ( $record->field($fieldtag) );
452 my $lastname = $record->field($fieldtag)->subfield('a');
453 my $firstname = $record->field($fieldtag)->subfield('b');
454 my $title = $record->field($fieldtag)->subfield('c');
455 my $number = $record->field($fieldtag)->subfield('d');
458 # $field->add_subfields("$subfield"=>"[ ".ucfirst($title).ucfirst($firstname)." ".$number." ]");
459 $field->add_subfields(
460 "$subfield" => ucfirst($title) . " "
461 . ucfirst($firstname) . " "
466 # $field->add_subfields("$subfield"=>"[ ".ucfirst($firstname).", ".ucfirst($lastname)." ]");
467 $field->add_subfields(
468 "$subfield" => ucfirst($firstname) . ", "
469 . ucfirst($lastname) );
472 $record->insert_fields_ordered($field);
474 return $record, $encoding;
481 my ($duplicatetitle)= @_;
482 ($template, $loggedinuser, $cookie) = get_template_and_user(
484 template_name => "acqui/neworderempty_duplicate.tmpl",
487 authnotrequired => 0,
488 flagsrequired => { acquisition => 'order_manage' },
494 biblionumber => $biblionumber,
495 basketno => $basketno,
496 booksellerid => $basket->{'booksellerid'},
497 breedingid => $params->{'breedingid'},
498 duplicatetitle => $duplicatetitle,
501 output_html_with_http_headers $input, $cookie, $template->output;