powerhouse has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to build an update manager, where we pay our commissions to our members. Some members have multiple accounts that earn commissions, but here in the USA, they have to pay taxes so our Check Processor, ADP, combines those checks for those members into one check, so the result may look something like this:
----------------------------------------------------- |MemberId|ADPID|PayoutAmount|status|statuschanged|t ----------------------------------------------------- |1 |101 |401.67 |Preissued|1206583466|2008-03-26 13:34:48 ----------------------------------------------------- |3 |101 |308.21 |Preissued|1206583466|2008-03-26 13:34:48 ----------------------------------------------------- |5 |101 |904.34 |Preissued|1206583466|2008-03-26 13:34:48 ----------------------------------------------------- |8 |101 |603.98 |Preissued|1206583466|2008-03-26 13:34:48 -----------------------------------------------------
So when ADP sends their checks to them and we download the check reconciliation, it would show this:
File #, Amount, Check Number, Pay Date 101, 2218.20, 10108, 03/26/2008
So, what we need to do is if they had checks spaning two pay out dates, the amount may not equal all the preissued checks it may be less so I need the automated programming to find those that equal the amount of the check they sent, for example if that adp file said this instead:
File #, Amount, Check Number, Pay Date 101, 1313.86, 10108, 03/26/2008
so it would not equal all the preissued checks, therefore, I need to find a way for Perl to find only those that have a sum of 1313.86, that are preissued and belong to that member only.

Here is how I handle it right now:
# Wrote right now, not a copy and paste, so not exact... open(FILE, "/path/to/apd/file.csv") or die "could not open file: $!"; my @_FILE = <FILE>; close(FILE); foreach my $_line (@_FILE) { $_line =~ s/\s+//g; my @_fileparts = split(/,/, $_line); my $sth = $dbh->prepare(qq{SELECT * FROM `payouts` WHERE `status` = +"Preissue" AND `adp_id` = ?}); $sth->execute($_fileparts[0]); while(my $_pay = $sth->fetchrow_hashref()) { # See this will not work because it may include the one record not +included in this file record that I split, so I cannot update this ch +eck ID } $sth->finish(); }
Anyhow, I hope I made it clear what I need to do. I need to update records that may not include all of their preissued checks but find the ones that are included in the amount of the check.

Do you know of a way I can do this? I tried doing this:
$sth = $dbh->prepare(qq{ select * from payouts where SUM(amount) = ?}) +; $sth->execute($_fileparts[2]);
But that gives me an error saying illegal grouping.

So I would appreiciate any advice you can offer, or any where you can point me.

thx,
Richard

Replies are listed 'Best First'.
Re: Using Perl to update multiple Database Records but not all
by perrin (Chancellor) on Mar 27, 2008 at 02:59 UTC
    When you repost a previous question, it's nice to mention that fact and tell us what you've tried since that last post 2 months ago.
Re: Using Perl to update multiple Database Records but not all
by pc88mxer (Vicar) on Mar 27, 2008 at 15:52 UTC
    I'd make the following recommendations:
    1. Put your csv files in your database. It's an extra table, but your data will become so much more usable and valuable.
    2. Create a "transaction register" table to reconcile the payments. The contents of your csv files would be entered into this table with positive amounts, and the data from ADP would be entered with negative amounts. Then you can use SQL to tell you if everything adds up.
    The structure of the transaction register table would look something like this:
    CREATE TABLE txn (txn_id INT PRIMARY KEY AUTO_INCREMENT, account VARCH +AR(64), amount INT); ALTER TABLE txn ADD INDEX i1 (account);
    Then the contents of the first csv file would be entered with statements like the following:
    INSERT INTO txn VALUES (NULL, '101', 40167); INSERT INTO txn VALUES (NULL, '101', 30821); ...
    and the contents of the second csv file would be entered as:
    INSERT INTO txn VALUES (NULL, '101', -131386);
    To determine which accounts are not reconciled, perform the following query:
    SELECT account, SUM(amount) as total FROM txn GROUP BY account HAVING +total <> 0;
    and this will list the accounts and the amounts by which they are off.
Re: Using Perl to update multiple Database Records but not all
by apl (Monsignor) on Mar 27, 2008 at 12:09 UTC
    Do a select for each ADP#. This will give you the domain for a single problem. Then you need to (combinatorially) add the Payout amounts until you hit the desired value.

    That's what I did manually to determine Member IDs 1, 3 and 8 total to the desired amount.