... we then have to go update the database to change the record from preissued to issued, the check number and timestamp... if the employee had 3 or more checks (they are combined into one check by the payroll processor)...
So, if you select "amount" from all records for a given "_emp_id" (the actual table column name for this is "apdid"), where "status='preissued'", the sum of the returned amounts should add up to the amount of the single check, right?
Given that the "_emp_id/apdid" and "status" fields identify the set of rows to be updated, you don't need to include the amounts as part of the query, do you? I presume it would be a good idea to test and report whether the sum of amounts in the database add up to the amount of a given check, but it would be easier to do this part in perl rather than in sql -- something like this maybe (untested):
(update; removed a presumably unnecessary "GROUP BY apdid" clause in the SELECT query)my $get_amt = $dbh->prepare( qq{SELECT SUM(amount) FROM `pot` WHERE `apdid`=? AND `status`='preissue'} ); my $upd_pay = $dbh->prepare( qq{UPDATE `pot` SET `status`='issued', `checkno`=?,statuschanged=? WHERE `apdid`=? AND `status`='preissue' +} ); my @file_fields = qw/checkNo checkAmt empID payDate/; while ( my $_line = <IN> ) { # assuming lines are coming from a file my %lineData = (); @lineData{@file_fields} = split /,/, $_line; $get_amt->execute( $lineData{empID} ); my $rows = $get_amt->fetchall_arrayref; my $sum = 0; for my $row ( @$rows ) { my ( $amt ) = @$row; $sum += $amt; } if ( $sum != $lineData{checkAmt} ) { warn sprintf( "empID %s: mismatch: sum of preissue rows= %s, c +heckAmt= %s\n", $lineData{empID}, $sum, $lineData{checkAmt} ); next; } $upd_pay->execute( $lineData{checkNo}, $lineData{payDate}, $lineDa +ta{empID} ); }
You might want more error checks on the DBI calls (unless you have RaiseError set to die on any failure). I would also recommend more error checks on reading the input data (correct number of fields, fields are non-empty, etc).
Note that the update statement will assign the current checkNo and payDate values (as well as resetting the status to "issued") for all the "preissue" rows for a given empID. This is a normal way of doing multi-row updates.
Also note that the statements are prepared once, outside the processing loop. This will save some overhead on the DB server.
You'll need to work out what really needs to be done when the amount of the check does not match the sum of "preissue" rows for a given employee, but I think the rest of it is basically what you want. (Or it could be that I don't get what you're doing at all.)
In reply to Re: payroll, mysql and perl question
by graff
in thread payroll, mysql and perl question
by powerhouse
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |