[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SF.net SVN: ledger-smb: [1412] trunk/LedgerSMB/IS.pm



Revision: 1412
          http://svn.sourceforge.net/ledger-smb/?rev=1412&view=rev
Author:   einhverfr
Date:     2007-07-16 19:23:14 -0700 (Mon, 16 Jul 2007)

Log Message:
-----------
Applying Victor's patch for ar/ap revere bugs (bug 1752439)

Modified Paths:
--------------
    trunk/LedgerSMB/IS.pm

Modified: trunk/LedgerSMB/IS.pm
===================================================================
--- trunk/LedgerSMB/IS.pm	2007-07-17 01:36:00 UTC (rev 1411)
+++ trunk/LedgerSMB/IS.pm	2007-07-17 02:23:14 UTC (rev 1412)
@@ -1058,33 +1058,45 @@
                         $project_id );
                     }
 		    else {
-			#search for entries on ap invoices, that have been allocated already for that product.
-                	$query = qq|
-		        	    SELECT i.id, i.qty, i.allocated, a.transdate, i.sellprice
-				      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.allocated > 0 
-                                 ORDER BY transdate,id DESC
-                                     |;
-			$sth = $dbh->prepare($query);
-			$sth->execute( $form->{"id_$i"}) || $form->dberror($query);
+ 			$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"};
 			my $total_inventory = 0;
 			while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
-			    #deallocate the necessary entries
-			    $form->db_parse_numeric(sth=>$sth, hashref => $ref);
-			    my $qty = $ref->{allocated};
-			    if($totalqty + $qty > 0) { $qty = -$totalqty; }
-			    $allocated+=$qty;
-			    $form->update_balance( $dbh, "invoice", "allocated", qq|id = $ref->{id}|, $qty * -1 );
-			    $total_inventory += $qty*$ref->{sellprice};
-			    last if ( ( $totalqty += $qty ) >= 0 );
-			}
-			# increase the inventory account for that product
-                        $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id) VALUES (?, ?, ?, ?, ?)|;
-			$sth = $dbh->prepare($query);
-    			$sth->execute($form->{id}, $form->{"expense_accno_id_$i"}, $total_inventory, $form->{transdate}, $form->{"project_id_$i"}) || $form->dberror($query);
+ 		    	    $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 );
+ 		        }
+ 			
+                         $query = qq|
+ 				INSERT INTO acc_trans 
+ 					(trans_id, chart_id, amount, 
+ 					transdate, project_id) 
+ 				VALUES (?, ?, ?, ?, ?)|;
+  			$sth = $dbh->prepare($query);
+     			$sth->execute(
+                                 $form->{id}, 
+                                 $form->{"expense_accno_id_$i"}, 
+                                 $total_inventory, $form->{transdate}, 
+                                 $form->{"project_id_$i"}
+                         ) || $form->dberror($query);
 
 			$total_inventory = (-1)*$total_inventory;
                         $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id) VALUES (?, ?, ?, ?, ?)|;
@@ -1560,8 +1572,9 @@
 		   SELECT i.id, i.trans_id, i.qty, i.allocated, i.sellprice,
 		          i.fxsellprice, p.inventory_accno_id, 
 		          p.expense_accno_id
-		     FROM invoice i, parts p
+		     FROM invoice i, parts p, ap a 
 		    WHERE i.parts_id = p.id
+		      AND i.trans_id = a.id
 		      AND i.parts_id = ?
 		      AND (i.qty + i.allocated) < 0
 		 ORDER BY trans_id|;


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.