Lines 30-35
use C4::MarcModificationTemplates;
Link Here
|
30 |
use Koha::Items; |
30 |
use Koha::Items; |
31 |
use DateTime; |
31 |
use DateTime; |
32 |
use DateTime::Format::Strptime; |
32 |
use DateTime::Format::Strptime; |
|
|
33 |
use Koha::Database; |
33 |
use Koha::Plugins::Handler; |
34 |
use Koha::Plugins::Handler; |
34 |
use Koha::Logger; |
35 |
use Koha::Logger; |
35 |
|
36 |
|
Lines 50-55
BEGIN {
Link Here
|
50 |
AddItemsToImportBiblio |
51 |
AddItemsToImportBiblio |
51 |
ModAuthorityInBatch |
52 |
ModAuthorityInBatch |
52 |
ModBiblioInBatch |
53 |
ModBiblioInBatch |
|
|
54 |
CheckRecordControlNumberInBatch |
53 |
|
55 |
|
54 |
BatchStageMarcRecords |
56 |
BatchStageMarcRecords |
55 |
BatchFindDuplicates |
57 |
BatchFindDuplicates |
Lines 313-318
sub ModBiblioInBatch {
Link Here
|
313 |
|
315 |
|
314 |
} |
316 |
} |
315 |
|
317 |
|
|
|
318 |
=head2 CheckRecordControlNumberInBatch |
319 |
|
320 |
CheckRecordControlNumberInBatch( $marc_record, $batch_id[, $import_record_id] ); |
321 |
|
322 |
Check to see if the given record's control number is already in use in the given batch. If provided, |
323 |
will check only for conflicts in records besides the given C<$import_batch_id>. |
324 |
|
325 |
If a record with the given control number in the given batch exists, will return its C<import_record_id>. |
326 |
|
327 |
=cut |
328 |
|
329 |
sub CheckRecordControlNumberInBatch { |
330 |
my ( $marc_record, $batch_id, $import_record_id ) = @_; |
331 |
|
332 |
return unless ( $marc_record->field('001') ); |
333 |
|
334 |
my %extra_conditions; |
335 |
|
336 |
$extra_conditions{'import_record.import_record_id'} = { '!=', $import_record_id } if ( $import_record_id ); |
337 |
|
338 |
my $control_number = $marc_record->field('001')->data; |
339 |
|
340 |
my $schema = Koha::Database->new()->schema(); |
341 |
return $schema->resultset('ImportBiblio')->search( |
342 |
{ |
343 |
control_number => $control_number, |
344 |
'import_record.import_batch_id' => $batch_id, |
345 |
%extra_conditions |
346 |
}, |
347 |
{ |
348 |
join => 'import_record', |
349 |
} |
350 |
)->get_column('import_record.import_batch_id')->first(); |
351 |
} |
352 |
|
316 |
=head2 AddAuthToBatch |
353 |
=head2 AddAuthToBatch |
317 |
|
354 |
|
318 |
my $import_record_id = AddAuthToBatch($batch_id, $record_sequence, |
355 |
my $import_record_id = AddAuthToBatch($batch_id, $record_sequence, |
Lines 353-429
sub ModAuthInBatch {
Link Here
|
353 |
|
390 |
|
354 |
=head2 BatchStageMarcRecords |
391 |
=head2 BatchStageMarcRecords |
355 |
|
392 |
|
356 |
( $batch_id, $num_records, $num_items, @invalid_records ) = |
393 |
{ |
357 |
BatchStageMarcRecords( |
394 |
batch_id => $batch_id, |
358 |
$record_type, $encoding, |
395 |
num_records => $num_records, |
359 |
$marc_records, $file_name, |
396 |
num_items => $num_items, |
360 |
$marc_modification_template, $comments, |
397 |
invalid_records => \@invalid_records |
361 |
$branch_code, $parse_items, |
398 |
} = BatchStageMarcRecords( { |
362 |
$leave_as_staging, $progress_interval, |
399 |
record_type => $record_type, |
363 |
$progress_callback |
400 |
encoding => $encoding, |
364 |
); |
401 |
marc_records => $marc_records, |
|
|
402 |
file_name => $file_name, |
403 |
comments => $comments, |
404 |
[ control_number_handling => 'preserve' | 'overwrite' ], |
405 |
[ existing_batch_id => $existing_batch_id, ] |
406 |
[ leave_as_staging => 1 ] |
407 |
[ marc_modification_template => $marc_modification_template, ] |
408 |
[ parse_items => 1 ] |
409 |
[ progress_interval => $progress_interval, |
410 |
progress_callback => \&progress_callback, ] |
411 |
[ timestamp_update => 'now' ], |
412 |
[ to_marc_plugin => $to_marc_plugin, ] |
413 |
} ); |
414 |
|
415 |
Any optional parameters are assumed to be no / off if omitted. If C<progress_interval> is specified, C<progress_callback> must be as well. |
365 |
|
416 |
|
366 |
=cut |
417 |
=cut |
367 |
|
418 |
|
368 |
sub BatchStageMarcRecords { |
419 |
sub BatchStageMarcRecords { |
369 |
my $record_type = shift; |
420 |
my $params = shift; |
370 |
my $encoding = shift; |
|
|
371 |
my $marc_records = shift; |
372 |
my $file_name = shift; |
373 |
my $marc_modification_template = shift; |
374 |
my $comments = shift; |
375 |
my $branch_code = shift; |
376 |
my $parse_items = shift; |
377 |
my $leave_as_staging = shift; |
378 |
|
379 |
# optional callback to monitor status |
421 |
# optional callback to monitor status |
380 |
# of job |
422 |
# of job |
381 |
my $progress_interval = 0; |
423 |
my $progress_interval = 0; |
382 |
my $progress_callback = undef; |
424 |
my $progress_callback = undef; |
383 |
if ($#_ >= 1) { |
425 |
if ( $params->{progress_interval} ) { |
384 |
$progress_interval = shift; |
426 |
$progress_interval = $params->{progress_interval}; |
385 |
$progress_callback = shift; |
427 |
$progress_callback = $params->{progress_callback}; |
386 |
$progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0; |
428 |
$progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0; |
387 |
$progress_interval = 0 unless 'CODE' eq ref $progress_callback; |
429 |
$progress_interval = 0 unless 'CODE' eq ref $progress_callback; |
388 |
} |
430 |
} |
389 |
my $existing_batch_id = shift; |
|
|
390 |
|
431 |
|
391 |
my $batch_id; |
432 |
my $batch_id; |
392 |
|
433 |
|
393 |
if ( $existing_batch_id ) { |
434 |
if ( $params->{existing_batch_id} ) { |
394 |
$batch_id = $existing_batch_id; |
435 |
$batch_id = $params->{existing_batch_id}; |
395 |
} else { |
436 |
} else { |
396 |
$batch_id = AddImportBatch( { |
437 |
$batch_id = AddImportBatch( { |
397 |
overlay_action => 'create_new', |
438 |
overlay_action => 'create_new', |
398 |
import_status => 'staging', |
439 |
import_status => 'staging', |
399 |
batch_type => 'batch', |
440 |
batch_type => 'batch', |
400 |
file_name => $file_name, |
441 |
file_name => $params->{file_name}, |
401 |
comments => $comments, |
442 |
comments => $params->{comments}, |
402 |
record_type => $record_type, |
443 |
record_type => $params->{record_type}, |
403 |
} ); |
444 |
} ); |
404 |
} |
445 |
} |
405 |
|
446 |
|
406 |
if ($parse_items) { |
447 |
if ($params->{parse_items}) { |
407 |
SetImportBatchItemAction($batch_id, 'always_add'); |
448 |
SetImportBatchItemAction($batch_id, 'always_add'); |
408 |
} else { |
449 |
} else { |
409 |
SetImportBatchItemAction($batch_id, 'ignore'); |
450 |
SetImportBatchItemAction($batch_id, 'ignore'); |
410 |
} |
451 |
} |
411 |
|
452 |
|
412 |
|
|
|
413 |
my $marc_type = C4::Context->preference('marcflavour'); |
453 |
my $marc_type = C4::Context->preference('marcflavour'); |
414 |
$marc_type .= 'AUTH' if ($marc_type eq 'UNIMARC' && $record_type eq 'auth'); |
454 |
$marc_type .= 'AUTH' if ($marc_type eq 'UNIMARC' && $params->{record_type} eq 'auth'); |
415 |
my @invalid_records = (); |
455 |
my @invalid_records = (); |
416 |
my $num_valid = 0; |
456 |
my $num_valid = 0; |
417 |
my $num_items = 0; |
457 |
my $num_items = 0; |
|
|
458 |
my $num_matched_control_number = 0; |
418 |
# FIXME - for now, we're dealing only with bibs |
459 |
# FIXME - for now, we're dealing only with bibs |
419 |
my $rec_num = 0; |
460 |
my $rec_num = 0; |
420 |
foreach my $marc_record (@$marc_records) { |
461 |
foreach my $marc_record (@{$params->{marc_records}}) { |
|
|
462 |
next unless $marc_record; |
421 |
$rec_num++; |
463 |
$rec_num++; |
422 |
if ($progress_interval and (0 == ($rec_num % $progress_interval))) { |
464 |
if ($progress_interval and (0 == ($rec_num % $progress_interval))) { |
423 |
&$progress_callback($rec_num); |
465 |
&$progress_callback($rec_num); |
424 |
} |
466 |
} |
425 |
|
467 |
|
426 |
ModifyRecordWithTemplate( $marc_modification_template, $marc_record ) if ( $marc_modification_template ); |
468 |
ModifyRecordWithTemplate( $params->{marc_modification_template}, $marc_record ) if ( $params->{marc_modification_template} ); |
427 |
|
469 |
|
428 |
my $import_record_id; |
470 |
my $import_record_id; |
429 |
if (scalar($marc_record->fields()) == 0) { |
471 |
if (scalar($marc_record->fields()) == 0) { |
Lines 432-456
sub BatchStageMarcRecords {
Link Here
|
432 |
|
474 |
|
433 |
# Normalize the record so it doesn't have separated diacritics |
475 |
# Normalize the record so it doesn't have separated diacritics |
434 |
SetUTF8Flag($marc_record); |
476 |
SetUTF8Flag($marc_record); |
435 |
|
|
|
436 |
$num_valid++; |
477 |
$num_valid++; |
437 |
if ($record_type eq 'biblio') { |
478 |
|
438 |
$import_record_id = AddBiblioToBatch($batch_id, $rec_num, $marc_record, $encoding, int(rand(99999)), 0); |
479 |
C4::Biblio::UpdateMarcTimestamp( $marc_record ) if ( $params->{timestamp_update} eq 'now' ); |
439 |
if ($parse_items) { |
480 |
my $existing_import_record_id; |
|
|
481 |
|
482 |
if ( $params->{control_number_handling} ) { |
483 |
$existing_import_record_id = CheckRecordControlNumberInBatch( $marc_record, $batch_id ); |
484 |
|
485 |
if ( $existing_import_record_id ) { |
486 |
$num_matched_control_number++; |
487 |
|
488 |
next if ( $params->{control_number_handling} eq 'preserve' ); |
489 |
} |
490 |
} |
491 |
|
492 |
if ($params->{record_type} eq 'biblio') { |
493 |
if ( $existing_import_record_id ) { |
494 |
$import_record_id = $existing_import_record_id; |
495 |
ModBiblioInBatch( $import_record_id, $marc_record ); |
496 |
} else { |
497 |
$import_record_id = AddBiblioToBatch($batch_id, $rec_num, $marc_record, $params->{encoding}, int(rand(99999)), 0); |
498 |
} |
499 |
|
500 |
if ($params->{parse_items}) { |
440 |
my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 0); |
501 |
my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 0); |
441 |
$num_items += scalar(@import_items_ids); |
502 |
$num_items += scalar(@import_items_ids); |
442 |
} |
503 |
} |
443 |
} elsif ($record_type eq 'auth') { |
504 |
} elsif ($params->{record_type} eq 'auth') { |
444 |
$import_record_id = AddAuthToBatch($batch_id, $rec_num, $marc_record, $encoding, int(rand(99999)), 0, $marc_type); |
505 |
if ( $existing_import_record_id ) { |
|
|
506 |
$import_record_id = $existing_import_record_id; |
507 |
ModAuthInBatch( $import_record_id, $marc_record ); |
508 |
} else { |
509 |
$import_record_id = AddAuthToBatch($batch_id, $rec_num, $marc_record, $params->{encoding}, int(rand(99999)), 0, $marc_type); |
510 |
} |
445 |
} |
511 |
} |
446 |
} |
512 |
} |
447 |
} |
513 |
} |
448 |
unless ($leave_as_staging) { |
514 |
unless ($params->{leave_as_staging}) { |
449 |
SetImportBatchStatus($batch_id, 'staged'); |
515 |
SetImportBatchStatus($batch_id, 'staged'); |
450 |
} |
516 |
} |
451 |
# FIXME branch_code, number of bibs, number of items |
517 |
# FIXME branch_code, number of bibs, number of items |
452 |
_update_batch_record_counts($batch_id); |
518 |
_update_batch_record_counts($batch_id); |
453 |
return ($batch_id, $num_valid, $num_items, @invalid_records); |
519 |
return { |
|
|
520 |
batch_id => $batch_id, |
521 |
num_items => $num_items, |
522 |
num_valid => $num_valid, |
523 |
num_matched_control_number => $num_matched_control_number, |
524 |
invalid_records => \@invalid_records |
525 |
}; |
454 |
} |
526 |
} |
455 |
|
527 |
|
456 |
=head2 AddItemsToImportBiblio |
528 |
=head2 AddItemsToImportBiblio |
Lines 469-474
sub AddItemsToImportBiblio {
Link Here
|
469 |
my @import_items_ids = (); |
541 |
my @import_items_ids = (); |
470 |
|
542 |
|
471 |
my $dbh = C4::Context->dbh; |
543 |
my $dbh = C4::Context->dbh; |
|
|
544 |
$dbh->do( "DELETE FROM import_items WHERE import_record_id = ?", undef, $import_record_id ); |
472 |
my ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",''); |
545 |
my ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",''); |
473 |
foreach my $item_field ($marc_record->field($item_tag)) { |
546 |
foreach my $item_field ($marc_record->field($item_tag)) { |
474 |
my $item_marc = MARC::Record->new(); |
547 |
my $item_marc = MARC::Record->new(); |