summaryrefslogtreecommitdiff
path: root/LedgerSMB
diff options
context:
space:
mode:
authoreinhverfr <einhverfr@4979c152-3d1c-0410-bac9-87ea11338e46>2007-07-18 19:46:07 +0000
committereinhverfr <einhverfr@4979c152-3d1c-0410-bac9-87ea11338e46>2007-07-18 19:46:07 +0000
commitaccf1eeac6040c6b47155532ac0a1bb096013591 (patch)
treea141731b7128a57f0bfdb2586499d6796e965e4e /LedgerSMB
parent0d48810d44ef1c0d37585d4b636847c5e825df26 (diff)
Applying Victor's patch for invoice reversals
git-svn-id: https://ledger-smb.svn.sourceforge.net/svnroot/ledger-smb/trunk@1423 4979c152-3d1c-0410-bac9-87ea11338e46
Diffstat (limited to 'LedgerSMB')
-rw-r--r--LedgerSMB/IR.pm27
-rw-r--r--LedgerSMB/IS.pm26
2 files changed, 47 insertions, 6 deletions
diff --git a/LedgerSMB/IR.pm b/LedgerSMB/IR.pm
index eec7985c..2d3c5776 100644
--- a/LedgerSMB/IR.pm
+++ b/LedgerSMB/IR.pm
@@ -318,6 +318,7 @@ sub post_invoice {
}
if ( $form->{"inventory_accno_id_$i"} ) {
+ my $totalqty = $form->{"qty_$i"};
if($form->{"qty_$i"}<0) {
# check for unallocated entries at the same price to match our entry
$query = qq|
@@ -340,7 +341,29 @@ sub post_invoice {
$allocated += $qty;
last if ( ( $totalqty -= $qty ) >= 0 );
}
- }
+ } else {
+ # start patch bug 1755355 ###############################################################################
+ # check for unallocated entries atthe 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);
+ 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 );
+ }
+ # stop patch bug 1755355 ###############################################################################
+ }
# add purchase to inventory
push @{ $form->{acc_trans}{lineitems} },
@@ -378,8 +401,6 @@ sub post_invoice {
$sth->execute( $form->{"id_$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);
diff --git a/LedgerSMB/IS.pm b/LedgerSMB/IS.pm
index 724dc4f2..d4cfd5be 100644
--- a/LedgerSMB/IS.pm
+++ b/LedgerSMB/IS.pm
@@ -1053,9 +1053,29 @@ sub post_invoice {
$form->{"qty_$i"} * -1
) unless $form->{shipped};
if($form->{"qty_$i"} > 0){
- $allocated =
- &cogs( $dbh, $form, $form->{"id_$i"}, $form->{"qty_$i"},
- $project_id );
+ #start patch bug 1755355
+ # 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 ar 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}|, (-1)*$qty);
+ $allocated += $qty;
+ last if ( ( $totalqty += $qty ) <= 0 );
+ }
+ $allocated += &cogs( $dbh, $form, $form->{"id_$i"}, $totalqty, $project_id );
+ #stop patch bug 1755355
}
else {
$query = qq|