summaryrefslogtreecommitdiff
path: root/LedgerSMB
diff options
context:
space:
mode:
authoreinhverfr <einhverfr@4979c152-3d1c-0410-bac9-87ea11338e46>2007-07-12 04:32:05 +0000
committereinhverfr <einhverfr@4979c152-3d1c-0410-bac9-87ea11338e46>2007-07-12 04:32:05 +0000
commit141c456419eb14d19a05c9fd9e1ba8f252f4103c (patch)
tree88012bfb9edf22713d0b329db5258a904f286ccd /LedgerSMB
parentd0d78206feeb963ae297d1b3d5decef6051b1164 (diff)
Committing Victor's reversal cogs patch
git-svn-id: https://ledger-smb.svn.sourceforge.net/svnroot/ledger-smb/trunk@1382 4979c152-3d1c-0410-bac9-87ea11338e46
Diffstat (limited to 'LedgerSMB')
-rw-r--r--LedgerSMB/IR.pm23
1 files changed, 23 insertions, 0 deletions
diff --git a/LedgerSMB/IR.pm b/LedgerSMB/IR.pm
index 612c45d8..7a26de61 100644
--- a/LedgerSMB/IR.pm
+++ b/LedgerSMB/IR.pm
@@ -315,6 +315,29 @@ sub post_invoice {
}
if ( $form->{"inventory_accno_id_$i"} ) {
+ if($form->{"qty_$i"}<0) {
+ # check for unallocated entries at the same price to match our entry
+ $query = qq|
+ SELECT i.id, i.qty, i.allocated, a.transdate
+ FROM invoice i
+ JOIN parts p ON (p.id = i.parts_id)
+ JOIN ap a ON (a.id = i.trans_id)
+ WHERE i.parts_id = ? AND (i.qty + i.allocated) < 0 AND i.sellprice = ?
+ ORDER BY transdate
+ |;
+ $sth = $dbh->prepare($query);
+ $sth->execute( $form->{"id_$i"}, $form->{"sellprice_$i"}) || $form->dberror($query);
+ my $totalqty = $form->{"qty_$i"};
+ while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
+ $form->db_parse_numeric(sth=>$sth, hashref => $ref);
+ my $qty = $ref->{qty} + $ref->{allocated};
+ if ( ( $qty - $totalqty ) < 0 ) { $qty = $totalqty; }
+ # update allocated for sold item
+ $form->update_balance( $dbh, "invoice", "allocated", qq|id = $ref->{id}|, $qty * -1 );
+ $allocated += $qty;
+ last if ( ( $totalqty -= $qty ) >= 0 );
+ }
+ }
# add purchase to inventory
push @{ $form->{acc_trans}{lineitems} },