#!/usr/local/bin/perl
{
use DBI();
eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;

# process any FOO=bar switches
$DEBUG = 0;
$USER= "zimuser";
$PASS= "";
$DB="zimdb";
$UDDS="A3200";
$BLDG="0521";
$ACCOUNT="account";
$ZIM_OPCODE = "100";
$DELIVER_TO = "Geology & Geophysics, UW-Madison ";
$REQ_FILE = "reqs.hs76";
$LEDG_FILE = "ledgers.small";


require ("retsys.pl");

#open (LEDGER, $LEDG_FILE) or die ;
#while (<LEDGER>) {
#chomp;
#my ($rdate, $fy, $ledgcode, $subcode, $reqnum, $act, $class, $vendor, $reqamt, $enc, $pd, $pdamt, $descript) = split(/\t/,$_);
## trim whitespace front and back.
#foreach ($rdate, $fy, $ledgcode, $subcode, $reqnum, $act, $class, $vendor, $reqamt, $enc, $pd, $pdamt, $descript) {

open (REQ, $REQ_FILE) or die ;
while (<REQ>) {
chomp;
my ($reqnum, $rdate, $descript, $act, $class, $ledgcode, $reqamt, $enc, $pd, 
    $pdamt, $comment, $subcode, $fy, 
    $call, $dup, $vcode, $vendor, $vadd1, $vadd2, $vadd3, $whofor, 
    $recadd1, $recadd2, $recadd3, $bofdate, $botdate, $prevpo, $fob, $terms, $delivery, 
    $reference, $bonum, $waiver, $bulletin, $rtype) = split(/\t/,$_);


#
## trim whitespace front and back.
foreach ($reqnum, $rdate, $descript, $act, $class, $ledgcode, $reqamt, $enc, $pd, 
         $pdamt, $comment, $subcode, $fy, $call, $dup, $vcode, $vendor, $vadd1, 
         $vadd2, $vadd3, $whofor, $recadd1, $recadd2, $recadd3, $bofdate, $botdate, 
         $prevpo, $fob, $terms, $delivery, $reference, $bonum, $waiver, $bulletin, $rtype ) { 
    s/^\s+//;
    s/\s+$//;
    print $_, ":" if $DEBUG ;
    }  
print "\n" if ($DEBUG);


$fy = &fy_correct($rdate, $fy);

$enc_date = &pd_date_cor($enc, $fy);
$paid_date = &pd_date_cor($pd, $fy);
$account_seq = &if_ledg_acct_exists($dbh, $ledgcode, $subcode);
if ($account_seq != 0) {
   print "Account: $account_seq  \n";
   }
$trans_seq = &get_trans_seq($dbh, $reqnum);
   print "Trans_seq: $trans_seq  \n";
$vnum = &get_vendor_num($dbh, $vcode, $vendor, $vadd1, $vadd2, $vadd3);
    print "Vendor_num: $vnum  \n";

&insert_ledger_req($dhb, $account_seq, $trans_seq, $rdate, $fy, $reqnum, $act, $class, $vendor, 
		   $reqamt, $enc_date, $paid_date, $pdamt, $descript);

&insert_requisition( $dhb, $reqnum, $rdate, $trans_seq, $rtype, $fy, $whofor, $DELIVER_TO, 
		     $recadd1, $recadd2, $recadd3, $call, $bofdate, $botdate, $fob, 
		     $terms, $delivery, $reference, $bonum, $waiver, $bulletin, 
		     $prevpo, $waiver);
#&insert_account_req_rel($dbh);


#&insert_ledger_account($dbh, $act_h, $ledg_h, $ledgcd_ih, $ledgcd_sh, 
#               $rdate, $fy, $ledgcode, $subcode, $reqnum, $act, $class, 
#               $vendor, $reqamt, $enc, $pd, $pdamt, $descript);

}
}
sub fy_correct
{
my ($rdate, $fy) = @_ ;
my $xdate = $rdate/1000 ;
if ($xdate >= 2000) { $fy = $fy + 2000 };
if ((1990 <= $xdate) and ($xdate <= 1999 )) { $fy = $fy + 1990 };
if ((1980 <= $xdate) and ($xdate <= 1989 )) { $fy = $fy + 1980 };
print "fy_correct: $fy \n" if $DEBUG ;
($fy);
}

sub get_trans_seq
{
my ($dbh, $reqnum) = @_ ;
my ($DEBUG) = 1;
$trans_seq_sh->execute($reqnum);
my ($trans_seq) = $trans_seq_sh->fetchrow_array ;
print ("if get_trans_seq Rows:  $trans_seq \n") if ($DEBUG) ;
$trans_seq ne '' ? $trans_seq : &new_trans_seq($dbh) ;
}

sub get_vendor_num
{
my ($dbh, $vcode, $vendor, $vadd1, $vadd2, $vadd3) = @_ ;
print ("get_vendor_num:", $vcode,":", $vendor, ":", $vadd1,":",  $vadd2,":",  $vadd3, "\n") ;

# Prepare vendor select statement
(my $xvend_sh) = $dbh->prepare("SELECT vendor_num from vendors
		               WHERE  vendor = ? 
			       ORDER BY mod_date DESC ");


$vend_sh->execute($vcode, $vendor, $vadd1, $vadd2, $vadd3) ;
(my $vnum) = $vend_sh->fetchrow_array ;
print "get_vendor_num: $vnum \n";
$vnum ne '' ? $vnum : &new_vendor($vendor, $vadd1, $vadd2, $vadd3);
}


sub new_vendor 
{
my ($vcode, $vendor, $vadd1, $vadd2, $vadd3) = @_ ;
vend_ih->execute($vcode, $vendor, $vadd1, $vadd2, $vadd3);
my $vnum = $dbh->{'mysql_insertid'};
$vnum ;
}


sub insert_ledger_req 
{
my ($dhb, $account_seq, $trans_seq, $rdate, $fy, $reqnum, $act, $class, $vendor, 
    $reqamt, $enc_date, $paid_date, $pdamt, $descript) = @_ ;
	    $e_trans_code = $GEN_REQ_ENC;
	    $p_trans_code = $REQ_PAY;
	    $p_trans_code = $BO_PAY if $descript =~m/^blanket|^BO/i ;

	    $descript = join(':', "VDR",$vendor, $descript) ;
# Encumbrance
            $ledg_enc_h->execute($fy, $account_seq, "Requisition", $account_seq, $trans_seq, 
				 $rdate, $act, $descript, $class, $e_trans_code, 
				 $recamt, $enc_date, $invoice_num, $voucher, $reconcile,
			         $ZIM_OPCODE, $datecode) ;
# Payment
            $ledg_pay_h->execute($fy, $account_seq, "Requisition", $account_seq, $trans_seq, 
				 $rdate, $act, $descript, $class, $p_trans_code, 
				 $pdamt, $paid_date, $invoice_num, $voucher, $reconcile,
			         $ZIM_OPCODE, $datecode) ;
}

sub insert_requisition
{
my ($dhb, $reqnum, $rdate, $t_seq, $rtype, $fy, $whofor, $recadd1, $recadd2, $recadd3, $call,
		     $bofdate, $botdate, $fob, $terms, $delivery, $reference, $bonum, $waiver, $bulletin, 
		     $prevpo, $waiver) = @_ ;

$rt = "E" if $rtype  =~m/^EXT/i ;
$rt = "I" if $rtype  =~m/^INT/i ;

if ($req_seq = &get_req_seq ($dbh, $reqnum) == 0) {
    $req_ih ->execute($reqnum, $rdate, $t_seq, $rt, $fy, $whofor, $DELIVER_TO,
		      $recadd1, $recadd2, $recadd3, $call, $bofdate, $botdate, $fob, 
		      $terms, $delivery, $reference, $bonum, $waiver, $bulletin, 
		      $prevpo, $waiver, $ZIM_OPCODE, $datecode );
 
 $req_seq = $dbh->{'mysql_insertid'};
 print ("Req Inserted: $req_seq  \n");

}
}

sub get_req_seq
{
my ($dbh, $reqnum) = @_ ;
my ($DEBUG) = 1;
$req_sh->execute($reqnum);
my ($req_seq) = $req_sh->fetchrow_array ;
print ("if get_req_seq Rows:  $req_seq \n") if ($DEBUG) ;
$req_seq ne '' ? $req_seq : 0  ;
}
