-Code representing the error condition. Can be 'duplicate_barcode',
-'invalid_homebranch', or 'invalid_holdingbranch'.
-
-=item error_information
-
-Additional information appropriate to the error condition.
-
-=back
-
-=cut
-
-sub AddBiblioAndItems {
- my ( $record, $frameworkcode ) = @_;
- my ($biblionumber,$biblioitemnumber,$error);
- my @itemnumbers = ();
- my @errors = ();
- my $dbh = C4::Context->dbh;
-
- # transform the data into koha-table style data
- # FIXME - this paragraph copied from AddBiblio
- my $olddata = TransformMarcToKoha( $dbh, $record, $frameworkcode );
- ($biblionumber,$error) = _koha_add_biblio( $dbh, $olddata, $frameworkcode );
- $olddata->{'biblionumber'} = $biblionumber;
- ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $olddata );
-
- # FIXME - this paragraph copied from AddBiblio
- _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
-
- # now we loop through the item tags and start creating items
- my @bad_item_fields = ();
- my ($itemtag, $itemsubfield) = &GetMarcFromKohaField("items.itemnumber",'');
- my $item_sequence_num = 0;
- ITEMFIELD: foreach my $item_field ($record->field($itemtag)) {
- $item_sequence_num++;
- # we take the item field and stick it into a new
- # MARC record -- this is required so far because (FIXME)
- # TransformMarcToKoha requires a MARC::Record, not a MARC::Field
- # and there is no TransformMarcFieldToKoha
- my $temp_item_marc = MARC::Record->new();
- $temp_item_marc->append_fields($item_field);
-
- # add biblionumber and biblioitemnumber
- my $item = TransformMarcToKoha( $dbh, $temp_item_marc, $frameworkcode, 'items' );
- $item->{'biblionumber'} = $biblionumber;
- $item->{'biblioitemnumber'} = $biblioitemnumber;
-
- # check for duplicate barcode
- my %item_errors = CheckItemPreSave($item);
- if (%item_errors) {
- push @errors, _repack_item_errors($item_sequence_num, $item, \%item_errors);
- push @bad_item_fields, $item_field;
- next ITEMFIELD;
- }
- my $duplicate_barcode = exists($item->{'barcode'}) && GetItemnumberFromBarcode($item->{'barcode'});
- if ($duplicate_barcode) {
- warn "ERROR: cannot add item $item->{'barcode'} for biblio $biblionumber: duplicate barcode\n";
- }
-
- # Make sure item statuses are set to 0 if empty or NULL in both the item and the MARC
- for ('notforloan', 'damaged','itemlost','wthdrawn') {
- if (!$item->{$_} or $item->{$_} eq "") {
- $item->{$_} = 0;
- &MARCitemchange( $temp_item_marc, "items.$_", 0 );
- }
- }
-
- # FIXME - dateaccessioned stuff copied from AddItem
- if ( !$item->{'dateaccessioned'} || $item->{'dateaccessioned'} eq '' ) {
-
- # find today's date
- my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
- localtime(time);
- $year += 1900;
- $mon += 1;
- my $date =
- "$year-" . sprintf( "%0.2d", $mon ) . "-" . sprintf( "%0.2d", $mday );
- $item->{'dateaccessioned'} = $date;
- &MARCitemchange( $temp_item_marc, "items.dateaccessioned", $date );
- }
-
- my ( $itemnumber, $error ) = &_koha_new_items( $dbh, $item, $item->{barcode} );
- warn $error if $error;
- push @itemnumbers, $itemnumber; # FIXME not checking error
-
- # FIXME - not copied from AddItem
- # FIXME - AddItems equiv code about passing $sth to TransformKohaToMarcOneField is stupid
- &MARCitemchange( $temp_item_marc, "items.itemnumber", $itemnumber );
-
- &logaction(C4::Context->userenv->{'number'},"CATALOGUING","ADD",$itemnumber,"item")
- if C4::Context->preference("CataloguingLog");
-
- $item_field->replace_with($temp_item_marc->field($itemtag));
- }
-
- # remove any MARC item fields for rejected items
- foreach my $item_field (@bad_item_fields) {
- $record->delete_field($item_field);
- }
-
- # now add the record
- # FIXME - this paragraph copied from AddBiblio -- however, moved since
- # since we need to create the items row and plug in the itemnumbers in the
- # MARC
- $biblionumber = ModBiblioMarc( $record, $biblionumber, $frameworkcode );
-
- # FIXME - when using this API, do we log both bib and item add, or just
- # bib
- &logaction(C4::Context->userenv->{'number'},"CATALOGUING","ADD",$biblionumber,"biblio")
- if C4::Context->preference("CataloguingLog");
-
- return ( $biblionumber, $biblioitemnumber, \@itemnumbers, \@errors);
-
-}
-
-sub _repack_item_errors {
- my $item_sequence_num = shift;
- my $item_ref = shift;
- my $error_ref = shift;
-
- my @repacked_errors = ();
-
- foreach my $error_code (sort keys %{ $error_ref }) {
- my $repacked_error = {};
- $repacked_error->{'item_sequence'} = $item_sequence_num;
- $repacked_error->{'item_barcode'} = exists($item_ref->{'barcode'}) ? $item_ref->{'barcode'} : '';
- $repacked_error->{'error_code'} = $error_code;
- $repacked_error->{'error_information'} = $error_ref->{$error_code};
- push @repacked_errors, $repacked_error;
- }
-
- return @repacked_errors;
-}
-
-=head2 ModBiblio
-
- ModBiblio( $record,$biblionumber,$frameworkcode);
- Exported function (core API) to modify a biblio