--- /usr/pub/backups/IS.pm	2008-08-18 12:15:08.000000000 -0600
+++ ./IS.pm	2008-10-23 13:36:01.000000000 -0600
@@ -36,7 +36,9 @@ use LedgerSMB::Tax;
 use LedgerSMB::PriceMatrix;
 use LedgerSMB::Sysconfig;
 
+
 sub reverse_invoice {
     my ( $dbh2, $form ) = @_;
     my $dbh   = $form->{dbh};
+    #SHC--23Oct-2008 I added it to read another select statment because I need to read some attributes but there are some confilict with the previous select statment which use the same object
+    my $dbh2   = $form->{dbh};
     my $query = qq|
 		SELECT id FROM ar
 		WHERE id = ?|;
@@ -1731,8 +1767,8 @@ sub reverse_invoice {
 
     # reverse inventory items
     my $query = qq|
-		SELECT i.id, i.parts_id, i.qty, i.assemblyitem, p.assembly,
-		       p.inventory_accno_id
+		SELECT i.id, i.parts_id, i.qty, i.allocated,i.assemblyitem,i.istariff ,p.assembly,
+		       p.inventory_accno_id , i.istariff
 		  FROM invoice i
 		  JOIN parts p ON (i.parts_id = p.id)
 		 WHERE i.trans_id = ?|;
@@ -1757,30 +1793,48 @@ sub reverse_invoice {
             next if ( $ref->{assembly} );
 
             # de-allocated purchases
+            #SHC--23-10-2008 I search about the product where qty - allocated will be >0 to get the last recient vendor invoice to process the allocated of it
+            #This is the first case , when I have a stock and I need to reverse item to it
+            $query = qq|
+				  SELECT id, trans_id, allocated FROM invoice
+				   WHERE parts_id = ?
+				         AND ((ABS(qty) - ABS(allocated)) > 0)
+				ORDER BY trans_id DESC|;
+            my $sth2 = $dbh2->prepare($query);
+            $sth2->execute( $ref->{parts_id} )
+              || $form->dberror($query);
+              
+            #================================================================
+            #SHC--23-10-2008, If I don't have record this mean , I don't have stock and I sold all what I have
+            if ($sth2->rows==0){
+            #SHC--23-10-2008 but in case I sold everything I will couldn't determine the recent vendor invoice because the qty always = allocated in all this product record
+            #so, I will add this condition , (qty + allocated) = 0) AND (qty <0) Desc
+
             $query = qq|
 				  SELECT id, trans_id, allocated
 				    FROM invoice
 				   WHERE parts_id = ?
-				         AND allocated > 0
+				         AND ((qty + allocated) = 0) AND (qty <0)
 				ORDER BY trans_id DESC|;
-            my $sth = $dbh->prepare($query);
-            $sth->execute( $ref->{parts_id} )
+            $sth2 = $dbh2->prepare($query);
+            $sth2->execute( $ref->{parts_id} )
               || $form->dberror($query);
-
-            while ( my $inhref = $sth->fetchrow_hashref(NAME_lc) ) {
-                $qty = $ref->{qty};
-                if ( ( $ref->{qty} - $inhref->{allocated} ) > 0 ) {
-                    $qty = $inhref->{allocated};
-                }
-
+            }
+            #==================================================================
+            #Idon't know why you add while, I work with one record not more
+            while ( my $inhref = $sth2->fetchrow_hashref(NAME_lc) ) {
+                #SHC--23-10-2008 I will bring the current allocated for the current item in the invoice and send it to the update_balance function at Form.pm
+                $allocated = $ref->{allocated};
+                $qty =  $ref->{qty};
+ 
                 # update invoice
                 $form->update_balance( $dbh, "invoice", "allocated",
                     qq|id = $inhref->{id}|,
-                    $qty * -1 );
+                    $allocated );
 
                 last if ( ( $ref->{qty} -= $qty ) <= 0 );
             }
-            $sth->finish;
+            $sth2->finish;
         }
     }
 
@@ -1806,6 +1860,7 @@ sub reverse_invoice {
 }
 
