#!/usr/bin/perl
#######################################################################################
#
# import cc transactions from iModules to the 'documents' structure on the hard disk.
# unlike BNY and later CDS transactions, and unlike Verisign CC transactions,
# you don't have to have any images with these transactions.
#
# Usage::  stage1 <configfile> <desgfile> <reconfile> 
#  stage1 config.yml des.csv res.csv
#
#######################################################################################

use MIME::Lite;
use YAML;
use File::Copy;
use Text::CSV;
use Scalar::Util qw(reftype);
use Locale::Country qw(country_code2code);
use Data::Dumper;
use DateTime;
use JSON;

# $base = "data";  ## temporary for development


### setup environment variables so we can run Oracle subprocesses

 $ENV{"ORACLE_HOME"} = "/infra/software/instantclient/instantclient-11.2";
 $ENV{"LD_LIBRARY_PATH"} = "/infra/software/instantclient/instantclient-11.2";
 $ENV{"PATH"}            = "/users/lamp/perl5/bin:$PATH";
 $ENV{"PERL5LIB"}        = "/users/lamp/perl5/lib/perl5:/docs/lib/perl5lib";
 $ENV{"PERL_LOCAL_LIB_ROOT"} = "/users/lamp/perl5";
 $ENV{"PERL_MB_OPT"}     = "--install_base /users/lamp/perl5";
 $ENV{"PERL_MM_OPT"}     = "INSTALL_BASE=/users/lamp/perl5";
 $ENV{"TESSDATA_PREFIX"} = "/docs/bin/tessdata";


### Setup country code translation
my $countrycode = {};
my $countrycodefile = "countrycodes.csv";
my $countrycsv = Text::CSV->new ( { binary => 1 } )
  or die "Cannot use CSV: " . Text::CSV->error_diag();
open (my $ccfile, $countrycodefile) or die "Can't open $reconciliationfile: $!";
my $cdata = $countrycsv->getline($ccfile);  ## skip titles
while ( $cdata = $countrycsv->getline($ccfile) ) {
  $a = $cdata->[1];
  $b = $cdata->[2];
  # print  "country >$a< >$b<\n";
  $countrycode->{$cdata->[1]} = $cdata->[2]; 
}

sub gift_or_nongift_test {
  $designation = shift;
  $gift_type = "gift";
  # 449901 from scenario 2
  # 604102 from scenario 6
  # 0005324 0007005 from scenario 10
  my @designation_fund_codes = ('0001930','0002537','0003681','0003711','0003907','0004056','0004086',
  '0004721','0005049','0006466','0010503', '0449901', '0604102',
  '0005324', '0007005', '0006889', '0000807', '0830102', '0917216', '0020905', '0000128', '0000102',
  '0000306');
  # pad it on the left to 7 characters
  $designation = substr('0000000' . $designation, -7);
  if (grep {$_ eq $designation} @designation_fund_codes) {
    $gift_type = 'nongift';
  }
  return $gift_type;
}

sub gift_or_nongift {
  $designation = shift;
  $gift_type = "gift";
  my $d = `/usr/bin/perl designation.pl $designation`;
  if (0) { # debugging
    print "designation: $d\n";
  }
  if ($d !~ /^\{/) { return "gift";}  ## could not find designation
  my $dh = decode_json $d;
  my $descr = $dh->{"DESCR254"};
  if ($descr =~ /nongift/i)   {$gift_type = "nongift";}
  if ($descr =~ /non\-gift/i) {$gift_type = "nongift";}
  if ($descr =~ /non\ gift/i) {$gift_type = "nongift";}
  return $gift_type;
}


my $csv = Text::CSV->new();

##### FLAGS
##### Used for debugging

my $oracle_present = "0";

use DBI;

if ($oracle_present) {
  eval "use DBD::Oracle"; die $@ if $@;
}


#$ENV{"ORACLE_HOME"}    ="/infra/software/instantclient/instantclient-11.2";
#$ENV{"LD_LIBRARY_PATH"}="/infra/software/instantclient/instantclient-11.2";

die "\n\nUsage: $0 <config file> <designations file> <reconciliation file>\n\nex: $0 cagingcfg.yml designations.csv reconciliation.csv\n\n" if $#ARGV < 2;

my ($configfile, $designationsfile, $reconciliationfile) = @ARGV;

die "\nConfiguration file $configfile does not exist\n" unless -e $configfile;
die "\nDesignations input file $designationsfile does not exist\n" unless -e $designationsfile;
die "\nReconciliation input file $reconciliationfile does not exist\n" unless -e $reconciliationfile;

my $error_messages = "";
my $debug_level = "";

my $config = YAML::LoadFile($configfile);

my $gpqueue = $config->{gpqueue};

my $docsdriver = $config->{docsdriver};
my $docsdb = $config->{docsdb};
my $docsuser = $config->{docsuser};
my $docspw = $config->{docspw};
my $dsn = "DBI:$docsdriver:database=$docsdb;host=localhost;port=3306";
my $dbh = DBI->connect($dsn, $docsuser, $docspw);
my $drh = DBI->install_driver("mysql");
my $dsth = $dbh->prepare("delete from handles where authority=? and document=?");
my $hsth = $dbh->prepare("insert into handles set authority=?, document=?,path=?");

# search for '$imodules' to uncomment this. There is another
# copy of the query initialization which is no longer used.
#$dsn = "dbi:mysql:imodulesdev:localhost:3306";
#$imodules = DBI->connect($dsn, "imodulesdev", "imodulesdev");
#my $diquery = "INSERT INTO designations (transaction, creationdate, data) values (?,?,?)";
#my $dinsert = $imodules->prepare($diquery);
#my $dsquery = "SELECT * FROM designations where transaction = ?";
#my $dselect = $imodules->prepare($dsquery);
#my $ddquery = "DELETE from designations where transaction = ?";
#my $ddelete = $imodules->prepare($ddquery);


#
# Establish the connection to the Oracle database and the prepare statement
#

# -- jee  my $odbh = DBI->connect("dbi:Oracle:$boracle_db", $boracle_user, $boracle_password, {RaiseError=>1});

# -- jee  die "\nCould not connect to Oracle DB with user \n" if (!defined($odbh));

#
#if (!defined($odbh)) {
#    print "Could not connect to database.\n";
#    exit;
#}

# -- jee my $getdescr   = $odbh->prepare("select descr254 from psprod.giftingest_account_tbl where designation = ?");

# The customer said that the input file columns could change several times during
# development, so they wanted us to go by the first line of the two files (which
# contain column names). That's what these next two hash tables are for.

my $column_map   = YAML::LoadFile($config->{column_map});
my $table_map = YAML::LoadFile($config->{table_map});

open AADLOG, '>>', "aadlog.txt" or die "Cannot open aadlog.txt\n";
print AADLOG "*******************************************\n";
my $datestring = localtime();
print AADLOG "  Begin run at: $datestring\n";
print AADLOG "*******************************************\n";

## NEED TO DOCUMENT::

my $outbase;
my $directory;

my $struct   = {};
my $metadata = {};

### $outbasetop
# this is where the persondata file goes
# this is also where the ccgYYYYMM directories go
my $outbasetop = $config->{outbasetop};  

my $gpqueue    = $config->{gpqueue};


#$config = "";


### get eNote
open ENOTETEXT, "</docs/configuration/config-docsdev/eNote.txt";
my @enote = <ENOTETEXT>;
close ENOTETEXT;




# Make the PersonData filename and open it

my ($nowday, $nowmonth, $nowyear) = (localtime)[3,4,5];
$nowyear += 1900;
$nowmonth += 1;
my $psdfn = sprintf("IM%04u%02u%02u.txt",$nowyear,$nowmonth,$nowday);

open PDATA, ">$outbasetop/$psdfn";




open (my $reconcil, $reconciliationfile) or die "Can't open $reconciliationfile: $!";


my $csv = Text::CSV->new ( { binary => 1 } )
	or die "Cannot use CSV: " . Text::CSV->error_diag();

# first get the column names:

my $rnames = $csv->getline($reconcil);  # get vendor column names
my $colcount = scalar @{$rnames}; # number of columns
# replace vendor column names with standard names from column_map
for (my $col = 0; $col < $colcount; $col++) { 
  $rnames->[$col] = $column_map->{$rnames->[$col]};
}

if (0) {
  print "$colcount columns in reconciliation data.\n";
}

# ------------------------------------------------------------------------------------
# Read in the reconciliation file.   
# Create a hash for each row keyed on the column name. 
# Create a hash of rows keyed on the "Transaction ID"
# ------------------------------------------------------------------------------------
#
my $rowcount = 0;
my $reconciliation = {};

while (my $thisrecord = $csv->getline($reconcil)) {

  my $row = {}; # hash


  # PersonData are fields that do not go on to the YAML output; they are only for
  # the separate data-about-a-particular-person-or-organization output file.

  my %persondata;

  # default value; this field NOT in input files
  $source{source_exception} = '?';

  for (my $col = 0; $col < $colcount; $col++) {
    $thisval = $thisrecord->[$col];
    $thisval =~ s/^\s+//;  # trim whitespace
    $thisval =~ s/\s+$//;
    $row->{$rnames->[$col]} = $thisval;
  }

  my $tid = $row->{"vendor_transaction_id"};
  $reconciliation->{$tid} = $row;
  $rowcount++;

if (0) {  # debugging
     print "==== Reconciliation for key $tid\n";
    foreach my $k (keys %$row) {
      print "key: $k => $row->{$k}\n"
    }
}

  # Write out the PersonData, which is based solely on the Reconciliation.
  # First, turn country code from 2 letter to 3 letter

  if (exists($row->{"billing_country"}) and 0 < length($row->{"billing_country"})) {
    $tbc = $row->{"billing_country"};
    if ($tbc =~ /^[A-Z]{2}$/) {
      # two upper-case letters are ISO 3166 alpha-2
      ##my $tbd = country_code2code( $tbc, LOCALE_CODE_ALPHA_2, LOCALE_CODE_ALPHA_3);
      $row->{"billing_country"} = $countrycode->{$tbc} if defined($countrycode->{$tbc});
    }
  }
  $tbd = $row->{"billing_country"};
  # print "COUNTRY CODE old: $tbc new $tbd\n" if ($tbc ne "");

  my $pdrec;
  my $tab = "\t";
  $pdrec .= $row->{"vendor_transaction_id"};
  $pdrec .= $tab;
  $pdrec .= $row->{"source"};
  $pdrec .= $tab;
  $pdrec .= $tab;
  $pdrec .= $row->{"donor_last_name"};
  $pdrec .= $tab;
  $pdrec .= $row->{"donor_first_name"};
  $pdrec .= $tab;
  $pdrec .= $tab;
  $pdrec .= $tab;
  $pdrec .= $tab;
  $pdrec .= $tab;
  $pdrec .= "Donor";
  $pdrec .= $tab;
  $pdrec .= $row->{"billing_email"};
  $pdrec .= $tab;
  $pdrec .= $tab;
  $pdrec .= $row->{"donor_last_name"};
  $pdrec .= $tab;
  $pdrec .= $row->{"donor_first_name"};
  $pdrec .= $tab;
  $pdrec .= $tab;
  $pdrec .= $tab;
  $pdrec .= $row->{"billing_phone"};
  $pdrec .= $tab;
  $pdrec .= $tab;
  $pdrec .= $row->{"source_donorid"};
  $pdrec .= $tab;
  $pdrec .= "P"; #  person ID type. Temporarily hardcoded to 'P' for person.
  $pdrec .= $tab;
  $pdrec .= $row->{"billing_country"};
  $pdrec .= $tab;
  $pdrec .= $row->{"billing_street_1"};
  $pdrec .= $tab;
  $pdrec .= $row->{"billing_street_2"};
  $pdrec .= $tab;
  $pdrec .= $row->{"billing_city"};
  $pdrec .= $tab;
  $pdrec .= $row->{"billing_state"};
  $pdrec .= $tab;
  $pdrec .= $tab;
  $pdrec .= $row->{"billing_zip"};
  $pdrec .= $tab;
  $pdrec .= "\t" x 100; # a total of 127 tab characters to separate 128 fields
  $pdrec .= "\n"; # and a linefeed to end it

  print PDATA $pdrec;

}

print "$rowcount rows were read from the reconciliation file\n";


close $reconcil;

close PDATA;


## Open the designations file.
open (my $desfile, $designationsfile) or die "Can't open $designationsfile: $!";

## Read the designation column headers
my $desnames = $csv->getline($desfile); # the columns
my $descount = scalar @{$desnames};  # number of columns

print AADLOG "\n$descount columns in reconciliation data.\n\n";

# Use standard names for designation column headers
for (my $descol = 0; $descol < $descount; $descol++) {
  $desnames->[$descol] = $column_map->{$desnames->[$descol]};

}

my $designation = {}; 


# Read all of the designations.

while (my $line = $csv->getline($desfile)) {

  my $row = {};
  
  for (my $coll = 0; $coll < $descount; $coll++) {
     my $value = $line->[$coll];
     $value =~ s/^\s+//;
     $value =~ s/\s+$//;
     $row->{$desnames->[$coll]} = $value;
  } # end columns

  print AADLOG "    Reading designation for Transaction_id: $row->{vendor_transaction_id}\n";

  # gather designation rows with like transaction ids and gift types in an array

  my $tid = $row->{vendor_transaction_id};

  unless (exists($designation->{$tid}))
  {
    $designation->{$tid} = {"gift"=>[], "nongift"=>[]};
  }

  # check if this designation is a gift or nongift:
  $gift_type = gift_or_nongift($row->{"designation_fund_code"});

  # is_recur will be true or false 1/0
  $designation->{$tid}->{"is_recur"} = ($reconciliation->{$tid}->{"original_transaction_id"} ne "");


  ## check for the existence of a reconciliation record

  $r = $reconciliation->{$tid};
  if ($r->{"vendor_transaction_id"} ne $tid) {
      print AADLOG "NO RECONCILIATION for transaction $tid\n";
      my $msg = MIME::Lite->new
          (
           Subject => "iModules Stage 1 Error",
           From    => 'rjm2@cornell.edu',
           To      => 'rjm2@cornell.edu',
           Type    => 'text/html',
           Data    => "There was no reconciliation record for transaction id $tid."
          );
      $msg->send();
  } else {

    push @{$designation->{$tid}->{$gift_type}}, $row;

  }

}

## having read in all of the designations, let's go around and see if there are
## reconciliations for which there are no designations.  These would be the 
## non-first recurring transactions, or if not-recurring, they are errors.

foreach my $key (keys %$reconciliation) {

  if (ref($designation->{$key}) ne "HASH") {
    $designation->{$key} = {"gift"=>[], "nongift"=>[]};  # no designations
    $designation->{$key}->{"is_recur"} = ($reconciliation->{$key}->{"original_transaction_id"} ne "");
    if (! ($designation->{$key}->{"is_recur"}) ) {  ## scenario 17
       print AADLOG "NO DESIGNATION for transaction $tid\n";
    }
  }

}

##########################
if (1) {   # debug switch. This shows that the designations are being read in.
  foreach my $key (keys %$designation) {
    my $rstring = "";
    if ($designation->{$key}->{"is_recur"}) {$rstring = "RECURRING";}
    print "Designations for transaction id $key $rstring\n";
    foreach my $r (@{$designation->{$key}->{"gift"}}) {

      print "  GIFT: Amount: $r->{'amount'}, Fund Code: $r->{'designation_fund_code'}\n";
    }
    foreach my $r (@{$designation->{$key}->{"nongift"}}) {
      print "  NONGFT: Amount: $r->{'amount'}, Fund Code: $r->{'designation_fund_code'}\n";
    }
  }
}
#########################

### Before running through the data, let's set up the data we need for batches 
### and transactions and dates.

### Batches will be start with letter(s) and a 3 digit number. 
### A: Non-recurring gifts with some designations.  One gift detail  e.g. A001
### B: Non-recurring non-gifts with some designations. One gift detail
### C: Non-recurring mixed (gifts and non-gifts. Two gift details
### D: Recurring gifts with some designations. 
### E: Recurring non-gifts with some designations  One gift detail, set exception
### F: Recurring mixed (gifts and non-gifts) Two gift details, set exception.

### There will be a transation number starting with  01-20

$batchA = [1,1];
$batchB = [1,1];
$batchC = [1,1];
$batchD = [1,1];
$batchE = [1,1];
$batchF = [1,1];
sub makenext {
  my $letter = shift;
  my $batch = shift;
  my $b = sprintf("%03d", $batch->[0]);
  my $t = sprintf("%02d", $batch->[1]);
  $x = $batch->[1]++;
  if ($x >= 20) {
    $batch->[1] = 1;
    $batch->[0]++;
  }
  #print "IN MAKENEXT: letter $letter batch $b trans $t\n";
  return [$letter, $b, $t];
}
#  each of these returns [letter, batchnumber,transactionnumber]
sub nextA { return makenext("A",$batchA);}
sub nextB { return makenext("B",$batchB);}
sub nextC { return makenext("C",$batchC);}
sub nextD { return makenext("D",$batchD);}
sub nextE { return makenext("E",$batchE);}
sub nextF { return makenext("F",$batchF);}

sub lookup_idtype {
  my $emplid = shift;
  my $type = "P";
  my $t = `/usr/bin/perl personorg.pl $emplid`;
  if (0) { # debugging
    print "cr_constituent_dim: $t\n";
  }
  if ($t !~ /^\{/) { return $type;}  ## could not find person
  my $dh = decode_json $t;
  $type = $dh->{"EMP_ORG_TYPE"};
  if (($type ne "P") && ($type ne "O")) { $type = "P";}
  # print "returning $type\n";
  return $type;
}

# decide what to do with each set of designations. 

foreach my $key (keys %$designation) {

  my $transaction = $designation->{$key};
  my $gift_designations = $designation->{$key}->{"gift"};  ## array
  my $nongift_designations = $designation->{$key}->{"nongift"};

  $count_gift_ds = @{$gift_designations};
  $count_nongift_ds = @{$nongift_designations};

# '$imodules'
  if (0) {
#  if ($transaction->{"is_recur"}) {
    if ($count_gift_ds + $count_nongift_ds == 0) {
      ### look for designations in the database
      ### we need to look for the original transaction_id, from the reconciliation
      $orig_key = $reconciliation->{$key}->{"original_transaction_id"};
      print "Key $key, original key $orig_key\n";
# '$imodules'
#      $dselect->execute($orig_key);
      my $row = $dselect->fetchrow_hashref();
      if ( $row->{"transaction"} ne "$orig_key") {
          print AADLOG "could not find designations for $key (original transaction $orig_key) in the database\n";
          print  "could not find designations for $key (original transaction $orig_key) in the database\n";
      } else {
        my $data = decode_json $row->{"data"};
        $gift_designations = $data->{"gift"};
        $nongift_designations = $data->{"nongift"};
        $count_gift_ds = @{$gift_designations};
        $count_nongift_ds = @{$nongift_designations};
      }
    } else {
      ### store the designations in the database
      ### '$imodules' fix this
#      $ddelete->execute($key);
      my $today = DateTime->now(time_zone=>'local');
      $data = {'gift'=>$gift_designations, 'nongift'=>$nongift_designations};
      ### '$imodules' fix this
#      $dinsert->execute($key, $today->ymd("-"), to_json $data);
    }
  }

#my $diquery = "INSERT INTO designations (id, transaction, creationdate, data) values (?,?,?,?)";
#my $dinsert = $imodules->prepare($diquery);
#my $dsquery = "SELECT FROM designations where transaction = ?";
#my $dselect = $imodules->prepare($dsquery);
#my $ddquery = "DELETE from designations where transaction = ?";
#my $ddelete = $imodules->prepare($ddquery);



if (0) {   # debug switch. This lists the designations
  print "==== Gift designations for key $key\n";
  foreach my $d (@$gift_designations) {
    foreach my $k (keys %$d) {
      print "key: $k => $d->{$k}\n"
    }
  }
}

if (0) {   # debug switch. This lists the related reconciliation

    my $recon = $reconciliation->{$key};
    print "==== Reconciliation for key $key\n";
    foreach my $k (keys %$recon) {
      print "key: $k => $recon->{$k}\n"
    }
}

# these three variables need to survive the if/elsif
my $legacy = {};

my $directory = "";
my $directorypath = "";

  if ($transaction->{"is_recur"}) {

      ## Get the reconciliation
      $r = $reconciliation->{$key};


     ### RECURRING

      ## since this is a recurring gift, we need to know
      ## if this is the first gift, a middle gift, or the 
      ## last gift. 

      ##'Scheduled Payments Active': scheduled_payments_active
      ##'Payment Frequency': payment_frequency
      ##'Total Scheduled Amount': total_scheduled_amount
      ##'Next Payment Amount': next_payment_amount
      ##'Next Payment Date': next_payment_date
      ##'Payments Remaining': payments_remaining
      ##'Payment Type': payment_type
      ##'Amount To Date': amount_to_date
      ##'First Payment Date': first_payment_date
      ##'Original Transaction Id': original_transaction_id

      if ($r->{"original_transaction_id"} eq $r->{"vendor_transaction_id"}) {

        # NEED TO WRITE THE DESIGNATIONS INTO THE DATABASE

      } else {

        # NEED TO FETCH THE DESIGNATIONS FROM THE DATABASE

      }
  

        ## Get $month, $day, $year for directory
        my $tempdate = $r->{"purchase_date"};
        #print "tempdate $tempdate\n";
        my ($month, $day, $year) = ( $tempdate =~ /^(\d+)\/(\d+)\/(\d+)\s/ );
        $month = sprintf("%02d", $month);
        $day   = sprintf("%02d", $day);
        if ($year < 100) {$year = $year + 2000;}
        $year  = sprintf("%04d", $year);

        $outbase = "$outbasetop/ccg$year$month";

        ## if necessary, create the monthly directory
        mkdir $outbase if (-d $outbase);

        ## increment the batch number and transaction number
        ## this is type D

        my $barray = nextA();
        my $type = $barray->[0];
        my $batch = $barray->[1];
        my $transaction = $barray->[2];

        #print "date: $year $month $day, batch: $type - $batch - $transaction\n";

        ## make a directory
        my $vt = sprintf("%07d", $r->{"vendor_transaction_id"});

        # $directory and $directorypath need to survive the if/elsif
        # so they are declared before it

        $directory = "ccg${year}${month}${day}${vt}";
        #print "filename $filename\n";
        $directorypath = "$outbase/$directory";
        mkdir $directorypath;

        mkdir "$directorypath/tiff";
        mkdir "$directorypath/ocr";

        $struct = {};
        $struct->{pages} = [];
        $struct->{sections} = {};
        $struct->{sections}->{1} = {};
        $struct->{sections}->{1}->{type} = "volume";
        $struct->{sections}->{2} = {};
        $struct->{sections}->{2}->{type} = "misc";
        $struct->{sections}->{2}->{creator} = "";
        $struct->{sections}->{2}->{title} = "$directory $donorname";
        $struct->{sections}->{2}->{date} = "$year-$month-$day";
        $struct->{sections}->{2}->{insection} = 1;
        $struct->{sections}->{2}->{subject} = [];
        @{$struct->{pages}} = ();

        YAML::DumpFile("$directorypath/structure.yml", $struct);

        my $alldesignations = [];
        foreach my $d (@$gift_designations) { push @$alldesignations, $d;}
        foreach my $d (@$nongift_designations) { push @$alldesignations, $d;}

        ## now make the metadata
        ## first the "legacy";

        # $legacy needs to survive the if/elsif so is declared before it

        $legacy = {};
        $legacy->{"batch_number"} = "OG$batch"; ## removed $type
        $legacy->{"transaction_number"} = $r->{"vendor_transaction_id"};  ## was $transaction
        $legacy->{"subcollection"} = "lbinbox";
        $legacy->{"processing_date"} = "$year$month$day";
            # format the reconciliation date stamp
            my $ds_date = $r->{"date_stamp"};
            my ($ds_month, $ds_day, $ds_year) = ( $ds_date =~ /^(\d+)\/(\d+)\/(\d+)\s/ );
            $ds_month = sprintf("%02d", $month);
            $ds_day   = sprintf("%02d", $day);
            $ds_year  = sprintf("%04d", $year);
            if ($ds_year < 100) {$ds_year = $ds_year + 2000;};
            $ds_date = "$ds_year$ds_month$ds_day";
        $legacy->{"deposit_date"} = $ds_date;
        $legacy->{"donor"} = $r->{"donor_first_name"}." ".$r->{"donor_last_name"};
        $legacy->{"emplid"} = $r->{"source_donorid"}; 
        $legacy->{"gift_processor"} = "-unassigned-";
        $legacy->{"amount"} = $r->{"order_total"};
        $legacy->{"gift_amount"} = sprintf("%.2f", $amount);
        $legacy->{"total_amount"} = sprintf("%.2f", $amount);

        ## next the "second" metadata stanza

        my $second = {};
        $second->{"batchid"} = $legacy->{"batch_number"};
        $second->{"transactionid"} = $legacy->{"transaction_number"};
        # original transaction id not present in non recurring gifts
        $second->{"depositdate"} = $ds_date; # formatted above
        $second->{"source_donorname"} = $r->{"donor_first_name"}." ".$r->{"donor_last_name"};
        $second->{"source_donorid"} = $r->{"source_donorid"};
        $second->{"source_idtype"} = lookup_idtype( $second->{"source_donorid"} );  # P or O or "";
        $second->{"source_amount"} = $legacy->{"amount"};
        $second->{"subcollection"} = "lbinbox";
        $second->{"source_giftdate"} = $legacy->{"deposit_date"};
        $second->{"gift_processor"} = $legacy->{"gift_processor"};
        $second->{"processing_date"} = $legacy->{"deposit_date"};
        $second->{"vendor_transaction_id"} = $r->{"vendor_transaction_id"};
        $second->{"source_exception"} = "?";  # no exception default
        if ($count_nongift_ds > 0) {
          $second->{"source_exception"} = "Y"; # set exception if non gifts present
        } else {
          $second->{"source_exception"} = "?";  # no exception if all gifts
        }

        if ($r->{"designation_instructions"} ne "")  # scenario 21
          {
            $second->{"source_exception"} = "Y";
            $second->{"source_comments"} .= " $r->{'designation_instructions'}";
          }

        # now the "third" metadata stanza, the gift details

        my $third = [];  # array of gift details.  There is just one for recur

        if (scalar(@$alldesignations) > 0) {
          my $detail = {};
          $detail->{"deposit_date"} = $ds_date;
          $detail->{"batchid"} = $legacy->{"batch_number"};
          $detail->{"transactionid"} = $legacy->{"transaction_number"};
          $detail->{"giftdetailnum"} = 1;
          $detail->{"detail_gifttype"} = "RECUR";
          $detail->{"detail_amount"} = $legacy->{"amount"};
          $detail->{"detail_idtype"} = $second->{"source_idtype"};
          $detail->{"detail_donorid"} = $legacy->{"emplid"};
          $detail->{"detail_tendertype"} = "OGVSN";
          $detail->{"detail_description"} = "[NOR]";
          if ($count_nongift_ds > 0) {
            $detail->{"detail_description"} = "[NOR][QPQ]";
          }
          $detail->{"designations"} = [];
          $detail->{"tributes"} = [];
          $detail->{"recognitions"} = [];

          # assemble the designations
          foreach my $d (@$alldesignations) {
            my $des = {};
            $des->{"depositdate"} = $legacy->{"deposit_date"};
            $des->{"batchid"} = $legacy->{"batch_number"};
            $des->{"transactionid"} = $legacy->{"transaction_number"};
            $des->{"giftdetailnum"} = 1;
            $des->{"designation_designationnum"} = $d->{"designation_fund_code"};
            $des->{"designation_cic"} = $d->{"designation_cic"};
              # look for comma separated appeal codes.  scenario 23
              my $appeal_code = $r->{"appeal_code"};
              if ($appeal_code =~ /,/) 
              {
                my @ac = split ",",$appeal_code;
                $appeal_code = @ac[0];
              }
              if ($appeal_code eq "") {$appeal_code = "CW0000000";} # scenario 22
            $des->{"designation_appeal"} = $appeal_code;
            # need to calculate the designation amount: amount * designation_percent
            $des->{"designation_amount"} = sprintf("%.2f", $d->{"amount"} * $d->{"designation_percent"}/100);
            push @{$detail->{"designations"}}, $des;
          }

          # assemble a recognition for the hard credit

          my $rec = {};
          $rec->{"depositdate"} = $legacy->{"deposit_date"};
          $rec->{"batchid"} = $legacy->{"batch_number"};
          $rec->{"transactionid"} = $legacy->{"transaction_number"};
          $rec->{"giftdetailnum"} = 1;
          $rec->{"recognition_idtype"} = $second->{"source_idtype"};
          $rec->{"recognition_id"} = $legacy->{"emplid"};
          $rec->{"recognition_type"} = "H";
          $rec->{"recognition_amount"} = $detail->{"detail_amount"};
          push @{$detail->{"recognitions"}}, $rec;
          
          
          # assemble any tributes, look for 2
          # first tribute

          if (($r->{"tribute_first_name_1"} ne "") ||
              ($r->{"tribute_last_name_1"} ne "") ||
              ($r->{"tribute_species_1"} ne "") ||
              ($r->{"tribute_type_1"} ne "") ||
              ($r->{"tribute_hm_1"} ne "")
             )
          {
            my $tribute = {};
            $tribute->{"depositdate"} = $legacy->{"deposit_date"};
            $tribute->{"batchid"} = $legacy->{"batch_number"};
            $tribute->{"transactionid"} = $legacy->{"transaction_number"};
            $tribute->{"giftdetailnum"} = 1;
              my $type = "";
              if ($r->{"tribute_hm_1"} eq "In honor of") { $type = "H";}
              if ($r->{"tribute_hm_1"} eq "In memory of") { $type = "M";}
            $tribute->{"tribute_type"} = $type;
            if ($r->{"tribute_species_1"} eq "") {
              my $fn = $r->{"tribute_first_name_1"};
              my $ln = $r->{"tribute_last_name_1"};
              $tribute->{"tribute_name"} = "$ln $fn";
            } else {
              $tribute->{"tribute_name"} = "PET";
            }
            $tribute->{"tribute_amount"} = $detail->{"detail_amount"};
            $tribute->{"tribute_emplid"} = "";
            $tribute->{"tribute_occasion"} = "";
            push @{$detail->{"tributes"}}, $tribute;
          }


          # second tribute

          if (($r->{"tribute_first_name_2"} ne "") ||
              ($r->{"tribute_last_name_2"} ne "") ||
              ($r->{"tribute_species_2"} ne "") ||
              ($r->{"tribute_type_2"} ne "") ||
              ($r->{"tribute_hm_2"} ne "")
             )
          {
            my $tribute = {};
            $tribute->{"depositdate"} = $legacy->{"deposit_date"};
            $tribute->{"batchid"} = $legacy->{"batch_number"};
            $tribute->{"transactionid"} = $legacy->{"transaction_number"};
            $tribute->{"giftdetailnum"} = 1;
              my $type = "";
              if ($r->{"tribute_hm_2"} eq "In honor of") { $type = "H";}
              if ($r->{"tribute_hm_2"} eq "In memory of") { $type = "M";}
            $tribute->{"tribute_type"} = $type;
            if ($r->{"tribute_species_2"} eq "") {
              my $fn = $r->{"tribute_first_name_2"};
              my $ln = $r->{"tribute_last_name_2"};
              $tribute->{"tribute_name"} = "$ln $fn";
            } else {
              $tribute->{"tribute_name"} = "PET";
            }
            $tribute->{"tribute_amount"} = $detail->{"detail_amount"};
            $tribute->{"tribute_emplid"} = "";
            $tribute->{"tribute_occasion"} = "";
            push @{$detail->{"tributes"}}, $tribute;
          }
          push @$third, $detail;

        }

        # removed code for non gift section

        ## clean up any rounding in the designations
        $dtotal = 0;
        $lastdesignation; # keep track of the last designation
        foreach my $giftdetail (@$third) {
          foreach my $designation (@{$giftdetail->{designations}}) {
            $lastdesignation = $designation;  #  keep track of last designation
            $dtotal += $designation->{"designation_amount"};
          }
        }
        # fix up total in last designation
        $ddiff = $r->{"order_total"} - $dtotal;
        $lastdesignation->{"designation_amount"} = sprintf("%.2f", $ddiff + $lastdesignation->{"designation_amount"});
        # end of clean up rounding in the designations


        YAML::DumpFile("$directorypath/metadata.yml", $legacy, $second, $third, $r);
        print "dumping $directorypath/metadata.yml\n";


  }  



      else 





  {  ## THIS IS NOT A RECURRING GIFT




# any number of gifts and non gifts

      ## Get the reconciliation
      $r = $reconciliation->{$key};

      if ($r->{"vendor_transaction_id"} ne $key) {
        print "NO RECONCILIATION for $key\n";
        my $msg = MIME::Lite->new
            (
             Subject => "iModules Stage 1 Error",
             From    => 'rjm2@cornell.edu',
             To      => 'rjm2@cornell.edu',
             Type    => 'text/html',
             Data    => "There was no reconciliation record for transaction id $key."
            );
            $msg->send();
        next;  ## should go to the next transaction
      }



      ## Get $month, $day, $year for directory
      my $tempdate = $r->{"purchase_date"};
      #print "tempdate $tempdate\n";
      my ($month, $day, $year) = ( $tempdate =~ /^(\d+)\/(\d+)\/(\d+)\s/ );
      $month = sprintf("%02d", $month);
      $day   = sprintf("%02d", $day);
      if ($year < 100) {$year = $year + 2000;}
      $year  = sprintf("%04d", $year);

      $outbase = "$outbasetop/ccg$year$month";
      ## if necessary, create the monthly directory
      mkdir $outbase if (!-d $outbase); 

      ## increment the batch number and transaction number
      ## this is type A

      my $barray = nextA();
      my $type = $barray->[0];
      my $batch = $barray->[1];
      my $transaction = $barray->[2];

      #print "date: $year $month $day, batch: $type - $batch - $transaction\n";

      ## make a directory
        my $vt = sprintf("%07d", $r->{"vendor_transaction_id"});

      # $directory and $directorypath need to survive the if/elsif
      # so they are declared before it

      $directory = "ccg${year}${month}${day}${vt}";
      #print "filename $filename\n";
      $directorypath = "$outbase/$directory";
      mkdir $directorypath;

      mkdir "$directorypath/tiff";
      mkdir "$directorypath/ocr";

      $struct = {};
      $struct->{pages} = [];
      $struct->{sections} = {};
      $struct->{sections}->{1} = {};
      $struct->{sections}->{1}->{type} = "volume";
      $struct->{sections}->{2} = {};
      $struct->{sections}->{2}->{type} = "misc";
      $struct->{sections}->{2}->{creator} = "";
      $struct->{sections}->{2}->{title} = "$directory $donorname";
      $struct->{sections}->{2}->{date} = "$year-$month-$day";
      $struct->{sections}->{2}->{insection} = 1;
      $struct->{sections}->{2}->{subject} = [];
      @{$struct->{pages}} = ();

      YAML::DumpFile("$directorypath/structure.yml", $struct);

      my $alldesignations = [];
      foreach my $d (@$gift_designations) { push @$alldesignations, $d;}
      foreach my $d (@$nongift_designations) { push @$alldesignations, $d;}

      ## now make the metadata
      ## first the "legacy";

      # $legacy needs to survive the if/elsif so it is declared before it

      $legacy = {};
      $legacy->{"batch_number"} = "OG$batch";
      $legacy->{"transaction_number"} = $r->{"vendor_transaction_id"};
      $legacy->{"subcollection"} = "lbinbox";
      $legacy->{"processing_date"} = "$year$month$day";
          # format the reconciliation date stamp
          my $ds_date = $r->{"date_stamp"};
          my ($ds_month, $ds_day, $ds_year) = ( $ds_date =~ /^(\d+)\/(\d+)\/(\d+)\s/ );
          $ds_month = sprintf("%02d", $month);
          $ds_day   = sprintf("%02d", $day);
          $ds_year  = sprintf("%04d", $year);
          if ($ds_year < 100) {$ds_year = $ds_year + 2000;};
          $ds_date = "$ds_year$ds_month$ds_day";
      $legacy->{"deposit_date"} = $ds_date;
      $legacy->{"donor"} = $r->{"donor_first_name"}." ".$r->{"donor_last_name"};
      $legacy->{"emplid"} = $r->{"source_donorid"}; 
      $legacy->{"gift_processor"} = "-unassigned-";
          # sum and format designation amounts
          my $amount = 0;
          foreach my $d (@$alldesignations) {
            $amount = $amount + $d->{"designation_amount"};
          }
      $legacy->{"amount"} = sprintf("%.2f", $amount);
      $legacy->{"gift_amount"} = sprintf("%.2f", $amount);
      $legacy->{"total_amount"} = sprintf("%.2f", $amount);

      ## next the "second" metadata stanza

      my $second = {};
      $second->{"batchid"} = $legacy->{"batch_number"};
      $second->{"transactionid"} = $legacy->{"transaction_number"};
      # original transaction id not present in non recurring gifts
      $second->{"depositdate"} = $ds_date; # formatted above
      $second->{"source_donorname"} = $r->{"donor_first_name"}." ".$r->{"donor_last_name"};
      $second->{"source_donorid"} = $r->{"source_donorid"};
      $second->{"source_idtype"} = lookup_idtype( $second->{"source_donorid"} );  # P or O or "";
      $second->{"source_amount"} = $legacy->{"amount"};
      $second->{"subcollection"} = "lbinbox";
      $second->{"source_giftdate"} = $legacy->{"deposit_date"};
      $second->{"gift_processor"} = $legacy->{"gift_processor"};
      $second->{"processing_date"} = $legacy->{"deposit_date"};
      $second->{"vendor_transaction_id"} = $r->{"vendor_transaction_id"};
      $second->{"source_exception"} = "?";  # no exception in this simple case
      if ($r->{"designation_instructions"} ne "")  # scenario 21
        {
          $second->{"source_exception"} = "Y";
          $second->{"source_comments"} .= " $r->{'designation_instructions'}";
        }
      # now the "third" metadata stanza, the gift details

      my $third = [];  # array of gift details.  

      if ($count_gift_ds > 0) {


        my $detail = {};
        $detail->{"deposit_date"} = $ds_date;
        $detail->{"batchid"} = $legacy->{"batch_number"};
        $detail->{"transactionid"} = $legacy->{"transaction_number"};
        $detail->{"giftdetailnum"} = 1;
        $detail->{"detail_gifttype"} = "GIFT";
           # sum and format designation amounts
          my $amount = 0;
          foreach my $d (@$gift_designations) {
            $amount = $amount + $d->{"designation_amount"};
          }
        $detail->{"detail_amount"} = sprintf("%.2f", $amount);       
        $detail->{"detail_idtype"} = $second->{"source_idtype"};
        $detail->{"detail_donorid"} = $legacy->{"emplid"};
        $detail->{"detail_tendertype"} = "OGVSN";
        $detail->{"detail_description"} = "[NOR]";
        $detail->{"designations"} = [];
        $detail->{"tributes"} = [];
        $detail->{"recognitions"} = [];

        # assemble the designations

        foreach my $d (@$gift_designations) {
          my $des = {};
          $des->{"depositdate"} = $legacy->{"deposit_date"};
          $des->{"batchid"} = $legacy->{"batch_number"};
          $des->{"transactionid"} = $legacy->{"transaction_number"};
          $des->{"giftdetailnum"} = 1;
          $des->{"designation_designationnum"} = $d->{"designation_fund_code"};
          $des->{"designation_cic"} = $d->{"designation_cic"};
            # look for comma separated appeal codes.  scenario 23
            my $appeal_code = $r->{"appeal_code"};
            if ($appeal_code =~ /,/) 
            {
              my @ac = split ",",$appeal_code;
              $appeal_code = @ac[0];
            }
            if ($appeal_code eq "") {$appeal_code = "CW0000000";} # scenario 22
          $des->{"designation_appeal"} = $appeal_code;
          $des->{"designation_amount"} = sprintf("%.2f", $d->{"designation_amount"});
          push @{$detail->{"designations"}}, $des;
        }

        # assemble a recognition for the hard credit

        my $rec = {};
        $rec->{"depositdate"} = $legacy->{"deposit_date"};
        $rec->{"batchid"} = $legacy->{"batch_number"};
        $rec->{"transactionid"} = $legacy->{"transaction_number"};
        $rec->{"giftdetailnum"} = 1;
        $rec->{"recognition_idtype"} = $second->{"source_idtype"};
        $rec->{"recognition_id"} = $legacy->{"emplid"};
        $rec->{"recognition_type"} = "H";
        $rec->{"recognition_amount"} = $detail->{"detail_amount"};
        push @{$detail->{"recognitions"}}, $rec;
        
        
        # assemble any tributes, look for 2
        # first tribute

        if (($r->{"tribute_first_name_1"} ne "") ||
            ($r->{"tribute_last_name_1"} ne "") ||
            ($r->{"tribute_species_1"} ne "") ||
            ($r->{"tribute_type_1"} ne "") ||
            ($r->{"tribute_hm_1"} ne "")
           )
        {
          my $tribute = {};
          $tribute->{"depositdate"} = $legacy->{"deposit_date"};
          $tribute->{"batchid"} = $legacy->{"batch_number"};
          $tribute->{"transactionid"} = $legacy->{"transaction_number"};
          $tribute->{"giftdetailnum"} = 1;
            my $type = "";
            if ($r->{"tribute_hm_1"} eq "In honor of") { $type = "H";}
            if ($r->{"tribute_hm_1"} eq "In memory of") { $type = "M";}
          $tribute->{"tribute_type"} = $type;
          if ($r->{"tribute_species_1"} eq "") {
            my $fn = $r->{"tribute_first_name_1"};
            my $ln = $r->{"tribute_last_name_1"};
            $tribute->{"tribute_name"} = "$ln $fn";
          } else {
            $tribute->{"tribute_name"} = "PET";
          }
          $tribute->{"tribute_amount"} = $detail->{"detail_amount"};
          $tribute->{"tribute_emplid"} = "";
          $tribute->{"tribute_occasion"} = "";
          push @{$detail->{"tributes"}}, $tribute;
        }


        # second tribute

        if (($r->{"tribute_first_name_2"} ne "") ||
            ($r->{"tribute_last_name_2"} ne "") ||
            ($r->{"tribute_species_2"} ne "") ||
            ($r->{"tribute_type_2"} ne "") ||
            ($r->{"tribute_hm_2"} ne "")
           )
        {
          my $tribute = {};
          $tribute->{"depositdate"} = $legacy->{"deposit_date"};
          $tribute->{"batchid"} = $legacy->{"batch_number"};
          $tribute->{"transactionid"} = $legacy->{"transaction_number"};
          $tribute->{"giftdetailnum"} = 1;
            my $type = "";
            if ($r->{"tribute_hm_2"} eq "In honor of") { $type = "H";}
            if ($r->{"tribute_hm_2"} eq "In memory of") { $type = "M";}
          $tribute->{"tribute_type"} = $type;
          if ($r->{"tribute_species_2"} eq "") {
            my $fn = $r->{"tribute_first_name_2"};
            my $ln = $r->{"tribute_last_name_2"};
            $tribute->{"tribute_name"} = "$ln $fn";
          } else {
            $tribute->{"tribute_name"} = "PET";
          }
          $tribute->{"tribute_amount"} = $detail->{"detail_amount"};
          $tribute->{"tribute_emplid"} = "";
          $tribute->{"tribute_occasion"} = "";
          push @{$detail->{"tributes"}}, $tribute;
        }
        push @$third, $detail;
      }

      if ($count_nongift_ds > 0) {

        my $detail = {};
        $detail->{"deposit_date"} = $ds_date;
        $detail->{"batchid"} = $legacy->{"batch_number"};
        $detail->{"transactionid"} = $legacy->{"transaction_number"};
        $detail->{"giftdetailnum"} = 1;
        $detail->{"detail_gifttype"} = "NONGFT";
                   # sum and format designation amounts
          my $amount = 0;
          foreach my $d (@$nongift_designations) {
            $amount = $amount + $d->{"designation_amount"};
          }
        $detail->{"detail_amount"} = sprintf("%.2f", $amount); 
        $detail->{"detail_idtype"} = $second->{"source_idtype"};
        $detail->{"detail_donorid"} = $legacy->{"emplid"};
        $detail->{"detail_tendertype"} = "OGVSN";
        $detail->{"detail_description"} = "[NOR][QPQ]";
        $detail->{"designations"} = [];
        $detail->{"tributes"} = [];
        $detail->{"recognitions"} = [];

        # assemble the designations

        foreach my $d (@$nongift_designations) {

          $second->{"source_exception"} = "Y";  # set the exception flag


          my $des = {};
          $des->{"depositdate"} = $legacy->{"deposit_date"};
          $des->{"batchid"} = $legacy->{"batch_number"};
          $des->{"transactionid"} = $legacy->{"transaction_number"};
          $des->{"giftdetailnum"} = 1;
          $des->{"designation_designationnum"} = $d->{"designation_fund_code"};
          $des->{"designation_cic"} = $d->{"designation_cic"};
            # look for comma separated appeal codes.  scenario 23
            my $appeal_code = $r->{"appeal_code"};
            if ($appeal_code =~ /,/) 
            {
              my @ac = split ",",$appeal_code;
              $appeal_code = @ac[0];
            }
            if ($appeal_code eq "") {$appeal_code = "CW0000000";} # scenario 22
          $des->{"designation_appeal"} = $appeal_code;
          $des->{"designation_amount"} = sprintf("%.2f", $d->{"designation_amount"});
          push @{$detail->{"designations"}}, $des;
        }

        # assemble a recognition for the hard credit

        my $rec = {};
        $rec->{"depositdate"} = $legacy->{"deposit_date"};
        $rec->{"batchid"} = $legacy->{"batch_number"};
        $rec->{"transactionid"} = $legacy->{"transaction_number"};
        $rec->{"giftdetailnum"} = 1;
        $rec->{"recognition_idtype"} = $second->{"source_idtype"};
        $rec->{"recognition_id"} = $legacy->{"emplid"};
        $rec->{"recognition_type"} = "H";
        $rec->{"recognition_amount"} = $detail->{"detail_amount"};
        ### (not correct:) NO RECOGNITION RECORD ADDED FOR NON-GIFT, comment out next line
        ### See: AADIMODOG-9
        push @{$detail->{"recognitions"}}, $rec;
        
        
        # assemble any tributes, look for 2
        # first tribute

        if (($r->{"tribute_first_name_1"} ne "") ||
            ($r->{"tribute_last_name_1"} ne "") ||
            ($r->{"tribute_species_1"} ne "") ||
            ($r->{"tribute_type_1"} ne "") ||
            ($r->{"tribute_hm_1"} ne "")
           )
        {
          my $tribute = {};
          $tribute->{"depositdate"} = $legacy->{"deposit_date"};
          $tribute->{"batchid"} = $legacy->{"batch_number"};
          $tribute->{"transactionid"} = $legacy->{"transaction_number"};
          $tribute->{"giftdetailnum"} = 1;
            my $type = "";
            if ($r->{"tribute_hm_1"} eq "In honor of") { $type = "H";}
            if ($r->{"tribute_hm_1"} eq "In memory of") { $type = "M";}
          $tribute->{"tribute_type"} = $type;
          if ($r->{"tribute_species_1"} eq "") {
            my $fn = $r->{"tribute_first_name_1"};
            my $ln = $r->{"tribute_last_name_1"};
            $tribute->{"tribute_name"} = "$ln $fn";
          } else {
            $tribute->{"tribute_name"} = "PET";
          }
          $tribute->{"tribute_amount"} = $detail->{"detail_amount"};
          $tribute->{"tribute_emplid"} = "";
          $tribute->{"tribute_occasion"} = "";
          push @{$detail->{"tributes"}}, $tribute;
        }


        # second tribute

        if (($r->{"tribute_first_name_2"} ne "") ||
            ($r->{"tribute_last_name_2"} ne "") ||
            ($r->{"tribute_species_2"} ne "") ||
            ($r->{"tribute_type_2"} ne "") ||
            ($r->{"tribute_hm_2"} ne "")
           )
        {
          my $tribute = {};
          $tribute->{"depositdate"} = $legacy->{"deposit_date"};
          $tribute->{"batchid"} = $legacy->{"batch_number"};
          $tribute->{"transactionid"} = $legacy->{"transaction_number"};
          $tribute->{"giftdetailnum"} = 1;
            my $type = "";
            if ($r->{"tribute_hm_2"} eq "In honor of") { $type = "H";}
            if ($r->{"tribute_hm_2"} eq "In memory of") { $type = "M";}
          $tribute->{"tribute_type"} = $type;
          if ($r->{"tribute_species_2"} eq "") {
            my $fn = $r->{"tribute_first_name_2"};
            my $ln = $r->{"tribute_last_name_2"};
            $tribute->{"tribute_name"} = "$ln $fn";
          } else {
            $tribute->{"tribute_name"} = "PET";
          }
          $tribute->{"tribute_amount"} = $detail->{"detail_amount"};
          $tribute->{"tribute_emplid"} = "";
          $tribute->{"tribute_occasion"} = "";
          push @{$detail->{"tributes"}}, $tribute;
        }
        push @$third, $detail;

      }

      YAML::DumpFile("$directorypath/metadata.yml", $legacy, $second, $third, $r);
      print "dumping $directorypath/metadata.yml\n";

    } # end of if/else on recurring or not.

	# Now make handle in database, lockbox_status, and put in queue.

    # NEW DOCUMENT NEEDS A HANDLE

# TEST change to testlockbox
     $dsth->execute("hein.lockbox", $directory);

# TEST change to testlockbox
     $hsth->execute("hein.lockbox", $directory, $directorypath);

     # UPDATE THE DATABASE TABLE
     my $table = "lockbox_status";
     my $query = "insert $table set
               status = ?,
               donor = ?,
               emplid = ?,
               amount = ?,
               batch_number = ?,
               transaction_number = ?,
               cornellbatchnumber = ?,
               processing_date = ?,
               subcollection = ?,
               authority = ?,
               document = ?";
     my $sth = $dbh->prepare($query);
# TEST comment execute out
      $sth->execute($legacy->{gift_processor},
                    $legacy->{donor},
                    $legacy->{emplid},
                    $legacy->{amount},
                    $legacy->{batch_number},
                    $legacy->{transaction_number},
                    "", # no cornell_batch_number, but it's expected; zero length string is not null.
                    $legacy->{processing_date},
                    $legacy->{subcollection},
                    "hein.lockbox",
                    $directory);

      # PUT IN QUEUE FOR STAGE TWO

#     $> = "lamp";
# code for gift processing
      print "trying to write into $gpqueue/$directory.txt: $docdir\n";
      open QUEUEDOC, ">$gpqueue/$directory.txt";
      print QUEUEDOC "$directorypath\n";
      close QUEUEDOC;

#     $> = "root";
					

  }  # foreach key in the designations hash

 




__END__ 






      # --------------------------------
      # one of the two difficult ones
      # which of the five honorees is it
      # --------------------------------
      my $whicht = 1;
      if ($originalnames->[$col] =~ /(.+?)(\d)/) {
        $whicht = $2;
        if (!exists($tribute{$whicht})) {
          my %newt = ();
          $tribute{$whicht} = %newt;
        }
        $tribute{$whicht}{$thisrealn} = $thisval;
        if ($debug_level >= 2) {
          print "$thisrealn is number $whicht\n";
        }
      }
    } elsif ("notification" eq $thiswhich and length($thisval) > 0) {
      # one of the two difficult ones
      # which of the five notifications is it
      my $whichnote = '1';
      if ($originalnames->[$col] =~ /(.+?)(\d)/) {
        $whichnote = $2;
        # exactly one notification line has a digit that does NOT 
        # mean WHICH notification it is, so change it back.
        if ($originalnames->[$col] =~ /Notification Address 2/) {
          $whichnote = '1';
        }
      }
      if (!exists($notify{$whichnote})) {
        my %newn = ();
        $notify{$whichnote} = %newn;
      }
      $notify{$whichnote}{$thisrealn} = $thisval;
      if ($debug_level >= 2) {
        print "$thisrealn $originalnames->[$col] is number $whichnote: value $thisval\n";
      }
      my $ncount = scalar keys %notify;
      if ($debug_level >= 2) {
        print "$ncount is size of percent notify\n";
      }
    }

  } # end for $col




  # join all the names together for source and tribute
  $source{source_donorname} = $source{donor_first_name} . " " . $source{donor_last_name};
  # delete $source{donor_last_name}; # keep these after all (for PersonData file)
  # delete $source{donor_first_name}; # keep

  #  my $myskey = scalar keys %source;

  print AADLOG "\nSource:\n";

  foreach $skey (keys %source) {
    print AADLOG  "    $skey : $source{$skey}\n";
  };

  print AADLOG "\n    Designation: $designation{'designation_appeal'}\n";
  print AADLOG "    Gift: order total: $gift{'order_total'}\n";

  if (exists($recur{'payment_type'}) && ("OT" ne $recur{'payment_type'})) {
    print AADLOG "    Recur:\n";

    foreach $rkey (keys %recur) {
      print AADLOG "        $rkey : $recur{$rkey}\n";
    }
  }


  for (my $i = 1; $i < 6; $i++) {
    if (exists($tribute{$i})) {
      $tribute{$i}{'tribute_name'} = $tribute{$i}{'tribute_first_name_TEMP'} . " " . $tribute{$i}{'$tribute_last_name_TEMP'};
      delete $tribute{$i}{'tribute_first_name_TEMP'};
      delete $tribute{$i}{'tribute_last_name_TEMP'};
      next if (1 == length($tribute{$i}{'tribute_name'}));
      print AADLOG "    Tribute: $tribute{$i}{'tribute_hm'} $tribute{$i}{'tribute_name'} $tribute{$i}{'tribute_type'} $tribute{$i}{'tribute_species'}\n";
      if ($debug_level >= 3) {
        print Dumper(%tribute);
      }
      1;

    } 
  };


  # condense the notification data

  for (my $ji = 1; $ji < 6; $ji++) {
    $j = "" . $ji;
    if (exists($notify{$j})) {
      my $noter = $notify{$j}{'notification_first_name_TEMP'} . " " . $notify{$j}{'notification_last_name_TEMP'};
      next if (1 == length($noter)); # in other words, no name.
      if (exists($notify{$j}{'notification_address_1'})) {
        $noter = $noter . ", " . $notify{$j}{'notification_address_1'};
      };
      if (exists($notify{$j}{'notification_address_2'})) {
        $noter = $noter . ", " . $notify{$j}{'notification_address_2'};
      };
      if (exists($notify{$j}{'notification_city'})) {
        $noter = $noter . ", " . $notify{$j}{'notification_city'};
      };
      if (exists($notify{$j}{'notification_state'})) { 
        $noter = $noter . ", " . $notify{$j}{'notification_state'};
      };
      if (exists($notify{$j}{'notification_postal_code'})) {
        $noter = $noter . " " . $notify{$j}{'notification_postal_code'};
      };
      if (exists($notify{$j}{'notification_country'})) {
        $noter = $noter . " " . $notify{$j}{'notification_country'};
      };
      if (exists($notify{$j}{'notification_comments'})) {
        $noter = $noter . " -- " . $notify{$j}{'notification_comments'};
      };

      print AADLOG "    Notify: $noter\n";
      
    }
  }
  
  my $tid = $source{vendor_transaction_id};
  if (exists $allrecons{$tid}) {
    error_message("**** Warning: More than one reconciliation for transaction id $tid: last one found will be used.");
  }

  #
  # Now that we have the transaction id in hand load up a hash with original names and values
  # to be used later in tributes
  #
  my $i=0;
  foreach (@plain_originalnames) {
      $recon_map{$tid}{$plain_originalnames[$i]} = $thisrecord->[$i];
      $i++;
  } # end foreach plain_originalnames

  # I did this two ways because I was having trouble putting them to YAML later.

  $allrecons{$tid} = {};
  $allrecons{$tid}->{source}      = \%source;
  $allrecons{$tid}->{designation} = \%designation;
  $allrecons{$tid}->{recur}       = \%recur;
  $allrecons{$tid}->{gift}        = \%gift;
  $allrecons{$tid}->{notify}      = \%notify;
  $allrecons{$tid}->{tribute}     = \%tribute;

  if (exists($source{designation_instructions}) and (0 < length($source{designation_instructions}))) {
    $source{exception} = '!';
  };


  # ??? put out the PersonData line here. That's based solely on the Reconciliation.
  # First order of business, turn country code from 2 letter to 3 letter
  if (exists($persondata{billing_country}) and 0 < length($persondata{billing_country})) {
    $tbc = $persondata{billing_country};
    if ($tbc =~ /^[A-Z]{2}$/) {
      # two upper-case letters are ISO 3166 alpha-2
      my $tbd = country_code2code( $tbc, LOCALE_CODE_ALPHA_2, LOCALE_CODE_ALPHA_3);
      $persondata{billing_country} = $tbd if defined($tbd);
    }
  }

  my $pdrec = "$source{vendor_transaction_id}\t$source{source_donorid}\t\t$source{donor_last_name}\t";
  $pdrec .= "$source{donor_first_name}\t\t\t\t\tDonor\t$persondata{billing_email}\t\t$source{donor_last_name}\t";
  $pdrec .= "$source{donor_first_name}\t\t\t$persondata{billing_phone}\t\t$source{source_donorid}\tP\t";

  # just before that last tab would be the person ID type. Temporarily hardcoded to 'P' for person.

  $pdrec .= "$persondata{billing_country}\t$persondata{billing_street_1}\t$persondata{billing_street_2}\t";
  $pdrec .= "$persondata{billing_city}\t$persondata{billing_state}\t\t$persondata{$billing_zip}\t";
  $pdrec .= "\t" x 100; # a total of 127 tab characters to separate 128 fields
  $pdrec .= "\n"; # and a linefeed to end it

  print PDATA $pdrec;
  
} # end while

close $reconcil;

close PDATA;

open (my $desfile, $designationsfile) or die "Can't open $designationsfile: $!";

my @realndes = ();
my @which_hash_des = ();

my $originaldes = $csv->getline($desfile);

my $descount = scalar @{$originaldes}; # number of columns

# print AADLOG "\n$descount columns in reconciliation data.\n\n";

for (my $descol = 0; $descol < $descount; $descol++) {
  $realndes[$descol] = $realname->{$originaldes->[$descol]};
  $which_hash_des[$descol] = $which_hash->{$originaldes->[$descol]};
}

my %desbighash; # contains all the designations as a hash of hashes of hashes
	# top level is indexed by for example "2735_0" which is the first designation
	# for transaction id 2735. each such entry hashes the individual fields of that
        # designation. 
my %gtypes; # a quick drippy way to get the gift types of all designations of a particular transaction id
        # the key is the transaction id and the value is an array of the gift type strings for designation
        # 0, designation 1, and so on.

while (my $thisdes = $csv->getline($desfile)) {

  my %design;
  
  for (my $coll = 0; $coll < $descount; $coll++) {
     $thisv = $thisdes->[$coll];
     $thisv =~ s/^\s+//;
     $thisv =~ s/\s+$//;
     next if (length($thisv) == 0);
     $thisrdes = $realndes[$coll];
     $thiswdes = $which_hash_des[$coll];

     next if ("ignore" eq $thiswdes); 

     if (!exists $design{$thisrdes}) {
     }

     $design{$thisrdes} = $thisv;
     print AADLOG "    Transaction_id $thisv\n" if ($thisrdes eq 'vendor_transaction_id');

  } # end for

  $desid = $design{'vendor_transaction_id'};

  print AADLOG "\n    Designation:\n";

  foreach $dkey (keys %design) {
    print AADLOG "        $dkey : $design{$dkey}\n";
    # print "        $dkey : $design{$dkey}\n";
  } # end foreach

  if (length($desid) == 0) {
    error_message("**** Designation ID $design{'designation_id'} with unique_id $design{'unique_id'} lacks a transaction ID. Cannot be imported.");
    next; # sorry have to go on to the next one
  }

  my $deskey = -1;

  do {
    $deskey++;
    $sdeskey = $desid . "_" . $deskey;
  } until (!exists $desbighash{$sdeskey}); # end do

  if (!exists $allrecons{$desid}) {
    error_message("**** Designation $design{'designation_id'} is associated with transaction id $desid which does not exist in the reconciliations. Cannot be imported.");
    next;
  }

  # preliminary gift type; may be overriden by later code
  # it's put in two places in memory for easier access.

  # Here looks to be the rules for determining the gift type
  #

  my $tgift_type = 'GIFT'; # default type to gift


  #
  # This is where we determine if it is a non gift from a 
  # database lookup
  #
  # but as the database connection isn't there yet, we'll do it this temporary way

  my $designation_fund_codes = ('0001930','0002537','0003681','0003711','0003907','0004056','0004086',
	'0004721','0005049','0006466','0010503');

  my $tdfc = $design{'designation_fund_code'};
  # pad it on the left to 7 characters
  $tdfc = substr('0000000' . $tdfc, -7);
 
  if (grep {$_ eq $tdfc} @designation_fund_codes) {
    $tgift_type = 'NONGFT';
  }

  # if ($design{'designation_description'} =~ /non[\-\ ]?gift/i) {
  #    $tgift_type = 'NONGFT';
  # }

  # now decide if it's a RECURring gift, by whether original_transaction_id is present
  # (that's the official way, nothing temporary about this one)

  # get the original_transaction_id, trim it, and keep it handy 
  my $toti = $allrecons{$desid}->{source}->{original_transaction_id};
  # remove leading and trailing blanks
  $toti =~ s/^\s+//;
  $toti =~ s/\s+$//;

  # if there IS an original_transaction_id, then this is a RECUR -- but then the previously-determined
  # gift type MUST be GIFT not NONGFT
  if (length($toti) > 0) {
   if ($tgift_type eq 'GIFT') {
     $tgift_type = 'RECUR';
   } else {
     $allrecons{$desid}->{source}->{source_exception} = '!';
     $sources{$desid}->{source_exception} = '!';
   }
  }

  $design{'gift_type'} = $tgift_type;
  push (@{$gtypes{$desid}}, $tgift_type);
  print AADLOG "    Put into gtypes\{$desid\}: $tgift_type\n";

  foreach $movekey (keys %design) {
    $desbighash{$sdeskey}{$movekey} = $design{$movekey};
  } # end for

  1;

} # end while


# dump the %gtypes arrays.
foreach $gtkey (sort keys %gtypes) {
  @tarr = @{$gtypes{$gtkey}};
  print AADLOG $gtkey . " ";
  foreach (@tarr) {
    print AADLOG $_ . " ";
  }
  print AADLOG "\n";
}


# now do an interim sanity check: is there a vendor_transaction_id for each one?

my $batchidnum = 1;
my $transinbatch = 0;

# -----------------------------------------------------------------------------------
# Process designation entries one at a time.
# -----------------------------------------------------------------------------------

my $curr_trid;
my $prev_trid;
my @giftdetails;
my $first_time = 0;
my $trid;
my $batchnumber;

# -----------------------------------------------------------------------------------
# Go through transaction id at a time
# -----------------------------------------------------------------------------------
#

foreach my $trid (keys %recon_map) {

        # ---------------------------------------------------------------------------------
        # Go through each transaction in the reconciliation list
        # ---------------------------------------------------------------------------------

	@giftdetails = ();

        #
        # ---------------------------------------------------------------------------------
        # If we made it here this is the first designation record for this transaction id
        # ---------------------------------------------------------------------------------
        # it's the first designation for this transaction id;
        # Make the batch number

        if (20 < ++$transinbatch) {
          $transinbatch = 1;
          $batchidnum++;
        } # end if

        # evaluate which scenario from the requirements document this is.
        my $gtype = 0; # GIFT type
        my $ntype = 0; # NONGFT type
        my $rtype = 0; # RECUR type
        my $gtotal = 0; # total dollar amount
        my $allsame = "yes"; # are all designations for this giftsource the same?

        my $howmdes = scalar @{$gtypes{$trid}}; # how many total designations

        foreach (@{$gtypes{$trid}}) {
          $gtype++ if ($_ eq 'GIFT');
          $ntype++ if ($_ eq 'NONGFT');
          $rtype++ if ($_ eq 'RECUR');
        } # end for

        print AADLOG "howmdes $howmdes gtype $gtype ntype $ntype rtype $rtype \n";
        if ($gtype == $howmdes || $ntype == $howmdes || $rtype == $howmdes) {
          # either there's only one, OR there's more than one and they're all the same
          print AADLOG "    All designations, whether one or more than one, of the same type\n";
          $allsame = "yes";
        } else {
          # we have more than one and they are not the same
          print AADLOG "    More than one designation and not all the same type\n";
          $allsame = "no";
        } # end if

        # ------------------------------------------------------------------------------
        # add all of the GIFT distributions together and see if they add up to the total

        my $addcash = 0;
        my $dbotherkey = $dbkey;
        my $dwhicha = 0;
        my $ttotalamt = $desbighash{$dbotherkey}{amount};

        #
        # The loop below is commented out for future reading and can likely be removed when
        # approved.  The error check has been moved to below the designations loop 
        #

#        do {
#          if ($ttotalamt != $desbighash{$dbotherkey}{amount}) {
#            error_message("    Designation $dbotherkey: total amount not consistent among these designations.\n");
#            $allrecons{$trid}->{source}->{source_exception} = '!'; 
#            $sources{$trid}->{source_exception} = '!';
#          }
#          print AADLOG "    type: $desbighash{$dbotherkey}{gift_type} amount: $desbighash{$dbotherkey}{amount}  designation_amount: $desbighash{$dbotherkey}{designation_amount}\n";
#          $addcash = $addcash + $desbighash{$dbotherkey}{designation_amount} if ('GIFT' eq $desbighash{$dbotherkey}{gift_type});
#          $dbotherkey = $trid . "_" . ++$dwhicha;
#        } until (!exists $desbighash{$dbotherkey}); # end do
#
#        if ($ttotalamt != $addcash) {
#          error_message("    Designation $dbotherkey: total amount $ttotalamt and sum of designations $addcash not equal.");
#          $allrecons{$trid}->{source}->{source_exception} = '!';
#          $sources{$trid}->{source_exception} = '!';
#        } # end if

        # ------------------------------------------------------------------------------

        # Needs a batch number

        $batchnumber = "OG$batchidnum";
  
        # my $date = $desbighash{$dbkey}{deposit_date};
        my $date = $recon_map{$trid}{'Purchase Date'};
        my ($month,$day,$year) = ($date =~ /^(\d{1,2})\/(\d{1,2})\/(\d{4})/);
        $month = sprintf("%02i",$month);
        $day = sprintf("%02i",$day);
        print AADLOG "Purchase date $date Deposit date $desbighash{$dbkey}{deposit_date} Year $year month $month day $day\n";
      


        $metadata = {};
        $metadata->{gift_processor} = '?';
        $metadata->{donor} = $allrecons{$trid}->{source}->{source_donorname};
        my $tdonorid = $allrecons{$trid}->{source}->{source_donorid};
        $metadata->{emplid} = $tdonorid if (defined($tdonorid) and ($tdonorid ne ""));
        #
        # 
        #
        # $metadata->{amount} = $desbighash{$dbkey}{amount};
        $metadata->{amount} = $recon_map{$trid}{'Order Total'};
        $metadata->{gift_amount} = $metadata->{amount};
        $metadata->{total_amount} = $metadata->{amount};
        $metadata->{batch_number} = $batchnumber;
        $metadata->{transaction_number} = $transinbatch;
        $metadata->{processing_date} = $date;
        $metadata->{subcollection} = 'lbinbox';



        $outbase = "$outbasetop/ccg$year$month";
        if (!-d $outbase) {mkdir $outbase;};

        my $batthree = sprintf("%03d", $batchidnum);
        my $txthree = sprintf("%02d", $transinbatch);
        $directory = "ccg$year$month$day$batthree$txthree";

        if (-d "$outbase/$directory") {
          ### BAIL OUT IF THIS DIRECTORY ALREADY EXISTS
          my $existerr = "ERROR: $outbase/$directory already exists\n";
          print $existerr;
          print AADLOG $existerr;
          next;
        } # end if

        print AADLOG "donor is $metadata->{donor}, emplid is $metadata->{emplid}\n";
        print AADLOG "    trying to make $outbase/$directory\n";

        mkdir "$outbase/$directory";
        mkdir "$outbase/$directory/tiff";
        mkdir "$outbase/$directory/ocr";

        $struct = {};
        $struct->{pages} = [];
        $struct->{sections} = {};
        $struct->{sections}->{1} = {};
        $struct->{sections}->{1}->{type} = "volume";
        $struct->{sections}->{2} = {};
        $struct->{sections}->{2}->{type} = "misc";
        $struct->{sections}->{2}->{creator} = [$donorname];
        $struct->{sections}->{2}->{title} = "$batchidnum-$transinbatch $donorname";
        $struct->{sections}->{2}->{date} = "$year-$month-$day";
        $struct->{sections}->{2}->{insection} = 1;
        $struct->{sections}->{2}->{subject} = [];
        @{$struct->{pages}} = ();

        # email -------------------------------------------------------------------------
        if (0) { # ??? don't actually send emails yet!
#      if ($emplid > 0) {
          $osth->execute($emplid);
          my $h = $osth->fetchrow_hashref;
          if ($h->{"COMMON_ID"} eq $emplid) {
            my $name = "$h->{PREFERRED_FIRST_NAME} $h->{PREFERRED_MIDDLE_NAME} $h->{PREFERRED_LAST_NAME}";
            if ($metadata->{donor} eq "") {
              $metadata->{donor} = $name;
              $struct->{sections}->{2}->{creator} = [$name];
              $struct->{sections}->{2}->{title} = "$batchidnum-$transinbatch $name";
            }
          } # end if

          my $ok = $h->{OK_TO_EMAIL};
          if ($batchid =~ /^COR\-/) {$ok = "N";}  # suppress correspondence only
          my $where = $h->{PREFERRED_EMAIL};
          print AADLOG "eNote: $emplid $name ok is $ok  where is $where\n";
          if ($ok eq "Y" && (length($where) > 0) && ($where = /\@/)) {
            # create a new MIME Lite based email
            my $data;
            $data = join "", @enote;
            my $msg = MIME::Lite->new
            (
             Subject => "Thank you from Cornell University",
             From    => 'enote@cornell.edu',
             To      => $where,
             Type    => 'text/html',
             Data    => $data
            );
            $msg->send();
            $enotedata .= "$directory,$emplid,$name,$where\n";
          }
        } # end if (0) - turn on/off email
        # email -------------------------------------------------------------------------

        my $outsource = \$allrecons{$trid}->{source};
        print AADLOG "base type of outsource: " . reftype($outsource) . "\n";
     
        my %t2hash = {};

        foreach (keys %{$allrecons{$trid}->{source}}) {
          print AADLOG "    $_ : $allrecons{$trid}->{source}->{$_}\n";
          $t2hash->{$_} = $allrecons{$trid}->{source}->{$_};
        } # end for

#      print YAML::Dump($outsource) . "\n";
      
        my $outyaml = YAML::Dump($metadata) . YAML::Dump($sources{$trid});

        if ($debug_level >=2) {
          print $outyaml;
        }
     
        my $logonetrans = "Transaction $sources{trid}->{vendor_transaction_id}\n";

        if ($debug_level >= 1) {
          print $logonetrans;
        }

        print AADLOG "\n$logonetrans\n";
        print AADLOG $outyaml;

        $prev_trid = $curr_trid;

#	push(@giftdetails, {'Designations' => undef,
#                            'Tributes' => undef,
#                            'Recognitions' => undef});

       # -------------------------------------------------------------------------
       # Note that the gift type can be used to index the array. Right now we will
       # put in index 0 till non gift is figured out
       # -------------------------------------------------------------------------

       # Put in Recognition
 
       push(@{$giftdetails[0]{'recognitions'}}, {map {$_ => $allrecons{$trid}->{'source'}->{$_}} keys %{$allrecons{$trid}->{'source'}}});

       # Tributes are tricky due to the one-many relationship embedded in the recognition record.

       # Test for existance before pushing onto the array

       if ($recon_map{$trid}{'HonorMemory'}) {
           my $trib_type;
	   if ($recon_map{$trid}{'HonorMemory'} =~ /In honor of/i) {
	       $trib_type = 'H';
           }
	   if ($recon_map{$trid}{'HonorMemory'} =~ /In memory of/i) {
	       $trib_type = 'M';
           }
           push(@{$giftdetails[0]{'tributes'}}, {'tribute_name'     => $recon_map{$trid}{'Honoree Last Name'} . " " . $recon_map{$trid}{'Make Your Gift to Cornell - Honoree First Name'},
                                                 'tribute_type'     => $trib_type,
                                                 'tribute_amount'   => $recon_map{$trid}{'Order Total'}});
       } # end if

       if ($recon_map{$trid}{'HonorMemory 2'}) {
           my $trib_type;
	   if ($recon_map{$trid}{'HonorMemory 2'} =~ /In honor of/i) {
	       $trib_type = 'H';
           }
	   if ($recon_map{$trid}{'HonorMemory 2'} =~ /In memory of/i) {
	       $trib_type = 'M';
           }
           push(@{$giftdetails[0]{'tributes'}}, {'tribute_name'     => $recon_map{$trid}{'Honoree Last Name'} . " " . $recon_map{$trid}{'Make Your Gift to Cornell - Honoree First Name'},
                                                 'tribute_type'     => $trib_type,
                                                 'tribute_amount'   => $recon_map{$trid}{'Order Total'}});
       } # end if

       # Now put in the other data that goes in the gift detail
       $giftdetails[0]{'batchid'} = $batchnumber;


  # -- Now see if there are designations --

  my @my_des_ids = grep /^$trid\_/, keys %desbighash;

  my $des_total = 0;
  foreach $my_des_id (@my_des_ids) {

    # Test for non gift here

#    if (is_non_gift($my_des_id, $getdescr) {
#    } else {

      push(@{$giftdetails[0]{'designations'}}, {map {$_ => $desbighash{$my_des_id}{$_}} keys %{$desbighash{$my_des_id}}});
#    }

    # Add up the totals for the designations to use later in a sanity check
    #
    foreach (keys %{$desbighash{$my_des_id}}) {
	$des_total += $desbighash{$my_des_id}{$_};
    } # end foreach

    1;

  } # end foreach $my_des_id

  # Check for amount exceptions
  
  # error_message("    Designation $dbotherkey: total amount $ttotalamt and sum of designations $addcash not equal.");
  
  # $sources{$trid}->{source_exception} = '!' if $des_total != $metadata{amount} || $des_total > 0;

  # Output to the YAML file

  YAML::DumpFile("$outbase/$directory/structure.yml",$struct);
  YAML::DumpFile("$outbase/$directory/metadata.yml",$metadata, $sources{$trid},@giftdetails); 

  1;

} # foreach $trid





#######################################################################################
# error_message
#######################################################################################

sub error_message {
  my $errstr = @_[0] . "\n";

  print AADLOG $errstr;

  $error_messages = $error_messages . $errstr; 

} # end error_message

sub is_non_gift {

#    my ($des_id, $query) = @_;
#    my $rc;

    # Using the Oracle data

#    if ($query->execute($des_id)) {
#        my $row = $query->fetchrow_hashref();
#        return 1 if ($row->{'DESCR254'};
#    }

#    return(0);
    
} # end is_non_gift

__END__


