Show
Ignore:
Timestamp:
05/16/08 17:01:04 (5 months ago)
Author:
erickson
Message:

the previous asset creation code naively ignored the lineitem_detail phase. new code takes volume/copy info from the lineitem_detail instead of directly from the client. code still needs testing

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/acq-experiment/Open-ILS/src/perlmods/OpenILS/Application/Acq/Picklist.pm

    r9628 r9630  
    330330            {desc => 'Authentication token', type => 'string'}, 
    331331            {desc => 'The lineitem id', type => 'number'}, 
    332             {desc => q/Options hash.  This contains an object that can be mapped into 
    333                 a set of volume and copy objects.  {"volumes":[{"label":"vol1", "owning_lib":4,"copies":[{"barcode":"123"},...]}, ...]} 
    334                 /} 
     332            {desc => q/Options hash./} 
    335333        ], 
    336334        return => {desc => 'ID of newly created bib record, Event on error'} 
     
    343341    return $e->die_event unless $e->checkauth; 
    344342 
    345     my $li = $e->retrieve_acq_lineitem([$li_id,  
     343    my $li = $e->retrieve_acq_lineitem([ 
     344        $li_id, 
    346345        {   flesh => 1, 
    347346            flesh_fields => {jub => ['purchase_order']} 
    348347        } 
    349     ]); 
     348    ]) or return $e->die_event; 
    350349 
    351350    return OpenILS::Event->new('BAD_PARAMS') # make this perm-based, not owner-based 
    352351        unless $li->purchase_order->owner == $e->requestor->id; 
    353352 
    354     my $record = $U->simplereq( 
    355         'open-ils.cat',  
    356         'open-ils.cat.biblio.record.xml.import', 
    357         $auth, $li->marc, $li->source_label); 
    358  
    359     return $record and $e->rollback if $U->event_code($record); 
    360     $logger->info("acq created new lineitem bib record ".$record->id); 
    361  
    362     return $record->id unless $$options{volumes}; 
    363  
    364     for my $vol (@{$$options{volumes}}) { 
    365         my $volume = Fieldmapper::asset::call_number->new; 
    366         $volume->isnew(1); 
    367         $volume->record($record->id); 
    368         $volume->$_($vol->{$_}) for keys %$vol; 
    369         $volume->copies([]); 
    370  
    371         if($vol->{copies}) { 
    372             for my $cp (@{$vol->{copies}}) { 
    373                 my $copy = Fieldmapper::asset::copy->new; 
    374                 $copy->isnew(1); 
    375                 $copy->$_($cp->{$_}) for keys %$cp; 
    376                 $copy->loan_duration(2) unless $copy->loan_duration; 
    377                 $copy->fine_level(2) unless $copy->fine_level; 
    378                 $copy->status(OILS_COPY_STATUS_ON_ORDER) unless $copy->status; 
    379                 push(@{$volume->copies}, $copy); 
    380             } 
    381         } 
     353    # ----------------------------------------------------------------- 
     354    # first, create the bib record if necessary 
     355    # ----------------------------------------------------------------- 
     356    unless($li->eg_bib_id) { 
     357        my $record = $U->simplereq( 
     358            'open-ils.cat',  
     359            'open-ils.cat.biblio.record.xml.import', 
     360            $auth, $li->marc, $li->source_label); 
     361 
     362        if($U->event_code($record)) { 
     363            $e->rollback; 
     364            return $record; 
     365        } 
     366 
     367        $li->eg_bib_id($record->id); 
     368        $e->update_acq_lineitem($li) or return $e->die_event; 
     369    } 
     370 
     371    my $li_details = $e->search_acq_lineitem_detail({lineitem => $li_id}, {idlist=>1}); 
     372 
     373    # ----------------------------------------------------------------- 
     374    # for each lineitem_detail, create the volume if necessary, create  
     375    # a copy, and link them all together. 
     376    # ----------------------------------------------------------------- 
     377    my %volcache; 
     378    for my $li_detail_id (@{$li_details}) { 
     379 
     380        my $li_detail = $e->retrieve_acq_lineitem_detail($li_detail_id) 
     381            or return $e->die_event; 
     382 
     383        my $volume = $volcache{$li_detail->cn_label}; 
     384        unless($volume and $volume->owning_lib == $li_detail->owning_lib) { 
     385            $volume = $U->simplereq( 
     386                'open-ils.cat', 
     387                'open-ils.cat.call_number.find_or_create', 
     388                $auth, $li_detail->cn_label, $li->eg_bib_id, $li_detail->owning_lib); 
     389        } 
     390 
     391        if($U->event_code($volume)) { 
     392            $e->rollback; 
     393            return $volume; 
     394        } 
     395 
     396        my $copy = Fieldmapper::asset::copy->new; 
     397        $copy->isnew(1); 
     398        $copy->loan_duration(2); 
     399        $copy->fine_level(2); 
     400        $copy->status(OILS_COPY_STATUS_ON_ORDER); 
     401        $copy->barcode($li_detail->barcode); 
     402        $copy->location($li_detail->location); 
    382403 
    383404        my $stat = $U->simplereq( 
    384405            'open-ils.cat', 
    385             'open-ils.cat.asset.volume.fleshed.batch.update', $auth, [$volume]); 
    386         return $stat and $e->rollback if $U->event_code($stat); 
    387         $logger->info("acq created new lineitem volume ".$volume->label); 
    388     } 
    389  
    390     $li->eg_bib_id($record->id); 
    391     $e->update_acq_lineitem($li) or return $e->die_event; 
     406            'open-ils.cat.asset.copy.fleshed.batch.update', $auth, [$copy]); 
     407 
     408        if($U->event_code($stat)) { 
     409            $e->rollback; 
     410            return $stat; 
     411        } 
     412 
     413        my $new_copy = $e->retrieve_asset_copy({deleted=>'f', barcode=>$copy->barcode}) 
     414            or return $e->die_event; 
     415 
     416        $li_detail->eg_copy_id($new_copy->id); 
     417        $e->update_acq_lineitem_detail($li_detail)  
     418            or return $e->die_event; 
     419    } 
     420 
    392421    $e->commit; 
    393  
    394422    return 1; 
    395423}