## TitleSource 3 to Symphony 3.2 fund code and
## holding code translations.
## by Migell Acosta, County of Los Angeles Public Library

use MARC::Batch;

my $batch = MARC::Batch->new('USMARC','test.mrc');

open(OUT,'>result.mrc') or die $!;

my %location_codes = ('ADULT_SRV' ,	        'AD',
                      'AIRC',	                'AI',
                      'APRC',	                'AP',
                      'AUDIOVISUL',	        'AV',
                      'BRC',	                'BR',
                      'CRC',	                'CR',
                      'EB_ENG',	                'EE',
                      'EB_SPAN',	        'ES',
                      'EXEC_OFF',	        'EO',
                      'FACILITIES',	        'FA',
                      'FIC_ENG',	        'FI',
                      'FIC_LARGE',              'FL',
                      'FIC_SPAN',	        'FS',
                      'FIC_TEEN',	        'FT',
                      'FISCAL_SRV',	        'FS',
                      'HRD',	                'HR',
                      'J_PAPERBCK',	        'JP',
                      'J_REF',	                'JR',
                      'JFIC_ENG',	        'JE',
                      'JFIC_SPAN',	        'JS',
                      'JNF_ENG',	        'KE',
                      'JNF_REF',	        'KR',
                      'JNF_SPAN',	        'KS',
                      'LITERACY',	        'LI',
                      'MARKETING',	        'MA',
                      'NF_ENG',	                'NE',
                      'NF_LARGE',	        'NL',
                      'NF_SPAN',	        'NS',
                      'NF_TEEN',	        'NT',
                      'OFFICE',	                'OF',
                      'PAPERBACKS',	        'PB',
                      'PROF_COLL',	        'PR',
                      'PSA',	                'PS',
                      'REFERENCE',      	'RE',
                      'STAFF_SRV',	        'SS',
                      'TEC_SRV',	        'TE',
                      'YOUTH_SRV',	        'YO',
                      'NETWORK',	        'NE',
                      'MAGAZINES',	        'MA');

my %itype_codes = ('AUDIO_CD',      'AC',
                   'AUDIO_LIT',     'AL',
                   'AUDIO_TAPE',    'AT',
                   'BOOK',          'BK',
                   'BOOK_EB',       'BE',
                   'BOOKONCD',      'BC',
                   'BOOKONTAPE',    'BT',
                   'BOOK_LIT',      'BL',
                   'BOOK_REF',      'BR',
                   'CD-ROM',        'CR',
                   'ENCYCLPDIA',    'EN',
                   'LARGE_PRNT',    'LP',
                   'PAPERBACK',     'PB',
                   'SPOKEN_TPE',    'ST',
                   'VID_DVD',       'VD',
                   'VID_LIT',       'VL',
                   'VID_TAPE',      'VT',
                   'DOWNLOAD',      'DN',
                   'MAGAZINE',      'MA',
                   'ONLINE',        'ON');


my %lang_codes = ('ENG',        'EN',
                  'SPA',        'SP');

my %audience_codes = ('A',       'ADULT',
                      'G',     'GENERAL',
                      'J',    'JUVENILE',
                      'T',        'TEEN');



## This while loop goes through each MARC record in the batch

while (my $record = $batch->next()) {


  ## get the language code from the 008 field
  
  my $leader = $record->field('008');
  my $lang = uc(substr($leader->as_string, 35, 3));



  ## get all the 949 fields - there are more than one in each record

  my @f949s = $record->field('949');


    ## examine each 949 field and rename the various subfields
    foreach my $field (@f949s) {

    my $cost_code = $field->subfield('a');
    my $itype     = $field->subfield('b');
    my $location  = $field->subfield('c');
    my @flevels   = split(/-/,$field->subfield('h'));
    my $flevel2   = $flevels[0];
    my $flevel3   = $flevels[1];
    my $flevel4   = $flevels[2];
    my $price     = $field->subfield('p');
    my $qty       = $field->subfield('e');


    ## gather these all up and make the holding and funding codes
    
    my $holding_code = $cost_code . $itype_codes{$itype} . $location_codes{$location} . $lang_codes{$lang} . $flevel3;
    my $funding_code = $cost_code . $flevel2 . $audience_codes{$flevel3} . $flevel4;
  
    

    ## create a new 949 field.

    my $new_field = MARC::Field->new(
    '949','','', a => $funding_code,
                 m => $holding_code,
                 p => $price,
                 b => $qty);



    ## replace existing 949 with our new one.
    $field->replace_with($new_field);

    }

  ## get all the 951 fields - there are more than one in each record

  my @f951s = $record->field('951');


    ## examine each 951 field and rename the various subfields
    foreach my $field (@f951s) {

    my $discount_code = $field->subfield('a');
    my $line_notes    = $field->subfield('b');
    my $phys_format   = $field->subfield('c');
    my $pub_status    = $field->subfield('d');
    my $list_price    = $field->subfield('e');


    ## examine these fields and determine the vendor ID based on the comparisons below
    
    if($phys_format->as_string() =~ /Paperback/ ){
    
    my $vendorID = "L128820"}

    ## create a new 951 field.

    my $new_field = MARC::Field->new(
    '951','','', a => $vendorID);
                 



    ## replace existing 951 with our new one.
    $field->replace_with($new_field);

    }

## output modified record.
    print OUT $record->as_usmarc();

}