if ( $#ARGV < 0 ) {
   die "Usage: tdbFormatBulkassets.pl date file_to_be_parsed loan mloan";
}

$input_file1  = "$ARGV[0]";
$input_file2  = "$ARGV[1]";
$input_file3  = "$ARGV[2]";
$input_file4  = "$ARGV[3]";
$output_file1 = "$ARGV[4]";
$output_file2 = "$ARGV[5]";
$output_file3 = "$ARGV[6]";
$output_file4 = "$ARGV[7]";
$output_file5 = "$ARGV[8]";

$count = 0;
$hedge_entity = 70;
$hedge_request_id = "F";
$hedge_commit_stat = 0;
$hedge_commit_nbr = 0;
$rate_option = 101;
$sale_typ =  "C";
$hedge_conform_flag = "C";
$note_typ = "F";
$inv_typ = 2;
$arm_valid = "F";
$status_cd =  "A";
$inv_cd = 104;
$close_cd = 4;
$bank_cd    = "M";
$aot = 0; 
$commit_mgmt_cd = " ";
$phase_cd = 3;
$sale_stg_cd = 101;
$refieqty_cd = 0;
$sub_typ = 0;
$ref_company = "CHE";
$loan_typ = 9; 


# Map state abbreviation to state unit
%state_units = ("AL" => "01", "AK" => "02", "AZ" => "03", "AR" => "04", "CA" => "05", "CO" => "06",
                "CT" => "07", "DE" => "08", "DC" => "09", "FL" => "10", "GA" => "11", "HI" => "12",
                "ID" => "13", "IL" => "14", "IN" => "15", "IA" => "16", "KS" => "17", "KY" => "18",
                "LA" => "19", "ME" => "20", "MD" => "21", "MA" => "22", "MI" => "23", "MN" => "24",
                "MS" => "25", "MO" => "26", "MT" => "27", "NE" => "28", "NV" => "29", "NH" => "30",
                "NJ" => "31", "NM" => "32", "NY" => "33", "NC" => "34", "ND" => "35", "OH" => "36",
                "OK" => "37", "OR" => "38", "PA" => "39", "RI" => "40", "SC" => "41", "SD" => "42",
                "TN" => "43", "TX" => "44", "UT" => "45", "VT" => "46", "VA" => "47", "WA" => "48",
                "WV" => "49", "WI" => "50", "WY" => "51", "GU" => "61", "PR" => "64", "VI" => "65",
                "CZ" => "66", "EA" => "68");



# Open input text file and three output files for different reports
print "Opening files........\n";
open( CHETXT,      "$input_file1" )   or die "Can't open $input_file1: $!";
open( FEES,        "$input_file2" )   or die "Can't open $input_file2: $!";
open( COSTS,       "$input_file3" )   or die "Can't open $input_file3: $!";
open( CO_COSTS,    "$input_file4" )   or die "Can't open $input_file4: $!";
open( LOAN,        ">$output_file1" ) or die "Can't open $output_file1: $!";
open( MLOAN,       ">$output_file2" ) or die "Can't open $output_file2: $!";
open( MLOAN2,      ">$output_file3" ) or die "Can't open $output_file3: $!";
open( FICO_SCORES, ">$output_file4" ) or die "Can't open $output_file4: $!";
open( MCUST,       ">$output_file5" ) or die "Can't open $output_file5: $!";

$currentFeeLine = 0;
while ( $line = <FEES> ) {
( $dummy, $feeHighAmount[$currentFeeLine], $feeAmount[$currentFeeLine] ) = split( /,/, $line );
        chomp $feeAmount[$currentFeeLine];
        if ( $feeAmount[$currentFeeLine] eq "" ) {
                $feeAmount[$currentFeeLine] = 0;
        }
        $currentFeeLine++;
}
        
$currentCostLine = 0;
while ( $line = <COSTS> ) {
        ( $costState, $dummy, $costHighAmount, $costAmount ) = split( /,/, $line );
        chomp $costAmount;
        if ( $costHighAmount eq "" ) {
                $costHighAmount = 0;
        }
        if ( exists($costHash{ $costState }) ) {
                push @{ $costHash{ $costState } }, [ $costHighAmount, $costAmount ];
        }
        else {
                $costHash{ $costState } = [$costHighAmount, $costAmount];
        }
        $currentCostLine++;
}

$currentCostLine = 0;
while ( $line = <CO_COSTS> ) {
        ( $co_costState, $dummy, $co_costHighAmount, $co_costAmount ) = split( /,/, $line );
        chomp $co_costAmount;
        if ( $co_costHighAmount eq "" ) {
                $co_costHighAmount = 0;
        }
        if ( exists($co_costHash{ $co_costState }) ) {
                push @{ $co_costHash{ $co_costState } }, [ $co_costHighAmount, $co_costAmount ];
        }
        else {
                $co_costHash{ $co_costState } = [$co_costHighAmount, $co_costAmount];
        }
        $currentCostLine++;
}


# Read in file line by line
print "Parsing $input_file........\n";
while ( $line = <CHETXT> ) {
  
   # split line into variables 
   ( $product, $amount, $term, $interest, $bookno, $tdsr, $mthatres, $ltv, $mortgage, $propsite, $proptype, $fico_score,
     $channel, $entity, $aprvcode, $original, $mob, $balloon, $pricingdate, $commitmentdate, $maturitydate, 
     $approvaldate, $acaps_number, $appraisal_type, $registrationdate, $occupancytype ) = split( /,/, $line );

   if ( $product eq "" || $product eq "PRODUCT") {
     next;
   }

   $count++;
   
   #strip extra whitespace from end of last variable in line
   $occupancytype =~ s/\s//g;

   #correct problem with entity
   if ( $channel eq "Broker" ) {
     if ( $entity eq 820 || $entity eq 822 || $entity eq 824 || $entity eq 826 ) {
       $entity = $entity + 1;  
     }
   }

   #determine if combo or not
   $whem_ind = "F";
   if ( $product eq "XFFRH" ) {
     $whem_ind = "C";
   }


   # product
   $product_cd = 800;

   #prin bal
   $prin_bal = $amount;
   $orig_bal = $amount;

   # product term determination
   $product_term = 30;
   $year = $term/12.0;
   if ( $year >= 0.0 && $year <= 7.4) {
     $product_term = 5;
   }
   if ( $year >= 7.5 && $year <= 12.4) {
     $product_term = 10;
   }
   if ( $year >= 12.5 && $year <= 17.4) {
     $product_term = 15;
   }
   if ( $year >= 17.5 && $year <= 22.4) {
     $product_term = 20;
   }
   if ( $year >= 22.5 && $year <= 27.4) {
     $product_term = 25;
   }

   $term_remg = $term;

   #note rate
   $note_rate = $interest;

   #account nbr
   $account_nbr = substr($bookno, 8,10);

   #loan to value ratio
   $ltv_ratio = $ltv;

   $state_unit = $state_units{$propsite};

   #fico scores
   $original_fico = substr( "0000" . $fico_score, -4, 4 );
   $current_fico  = substr( "0000" . $fico_score, -4, 4 );

   #pricing date
   $pricing_dt = $approvaldate;

   # program category 
   $program_cat = 1;
   $origin_channel = "RETL";
   if ( $entity eq 821 || $entity eq 823 || $entity eq 825 || $entity eq 827 ) {
     $program_cat = 15;
     $origin_channel = "BROK";
   }

   # set commitment expiration date
   $cmt_exp_dt = $commitmentdate;

   # set maturity date
   $maturity_dt = $maturitydate;


   $fees = 0;
   $costs = 0;
   if ( $program_cat eq 15 ) {
    #look up fees for broker loan from fees table based upon prin_bal
        for ( $i = 0; $i < $currentFeeLine; $i++ ) {
                if ( $prin_bal <= $feeHighAmount[ $i ] ) {
                        $fees = $feeAmount[ $i ];
                        last;
                }
        }
        if ( $i == $currentFeeLine ) {
                $fees = $feeAmount[ $i - 1 ];
        }
   }

   #look up costs from costs table based upon $propsite and prin_bal
   if ($proptype eq "CP") {
     @currentCostArray = @{ $co_costHash{ "$propsite" } };
   }
   else {
        if ( exists $costHash{ "$propsite" } ){
                @currentCostArray = @{ $costHash{ "$propsite" } };
        }
        else {
                @currentCostArray = @{ $costHash{ "OTHER" } };
        }   
   }
   for ( $i = 0; $i < scalar @currentCostArray; $i++ ) {
        if ( $prin_bal <= $currentCostArray[$i][0] ) {
                $costs = $currentCostArray[$i][1];  
                last;
        }
   }
   if ( $i == scalar @currentCostArray ) {
        $costs = $currentCostArray[$i - 1][1];
   }

   # total origination fee
   $tot_orig_fee = $fees;

   # unearned origination cost
   $une_orig_cst = $costs;

   #sales center
   $sales_ctr = $entity;

   #approval code
   $approval_cd = $aprvcode;

   #obligation ratio
   $obl_ratio = $tdsr;

   # write the output to two text files 
   print LOAN   ( "$note_typ|$inv_typ|$account_nbr|$sale_typ|$bank_cd|0||$state_unit|$status_cd|0|$whem_ind|0|1|1|$program_cat|$product_term|$sale_stg_cd||0||$hedge_entity|$hedge_product||0|$coupon||$hedge_request_id|$hedge_conform_flag|$hedge_commit_stat|$hedge_commit_nbr|0|0|0|0|0|0|0|0|||||||||||$commit_mgmt_cd|$aot|$arm_valid|N|N|0|\n" );
   print MLOAN  ( "$note_typ|$inv_typ|$account_nbr|$hedge_commit_nbr|$note_rate|$prin_bal|$term|$rate_option|$cmt_exp_dt||$maturity_dt||||||$pricing_dt||0|||||||||||$orig_bal|$term_remg|$tot_orig_fee|0|0|$une_orig_cst|0|0|0|0|0|0|0|0|0|||||$ref_company|||||0||||$refieqty_cd|$inv_cd|0|$product_cd|$sales_ctr|0|$phase_cd|$close_cd|$approval_cd|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|$ltv_ratio|$obl_ratio|0|0|0|0|0||0|0|0|0|0|0|0|0||$loan_typ|$sub_typ|0|0|0|0|0|\n" );
   print MLOAN2 ( "$account_nbr|0|$origin_channel|||0|0|0|0|0\n" );
   print FICO_SCORES ( "$account_nbr|$original_fico|$current_fico\n" );
   print MCUST ("$account_nbr||||||$state_unit\n");
}
print "Closing files..............\n";
close CHETXT;
close FEES;
close COSTS;
close CO_COSTS;
close LOAN;
close MLOAN;
close MLOAN2;
close FICO_SCORES;
close MCUST;

print "$count lines read...........finished\n";
Salem commented: For not using [code][/code] tags, especially for so much code -2

It takes 9 command line arguments (which are filenames), reads the content of the first four, doing varying operations on the data within, and writes results to the last five. It seems to have to do with loan balances, fees, and, seemingly, the determination of FICO scores.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.