sub reverse_invoice {
    my ( $dbh2, $form ) = @_;
    my $dbh   = $form->{dbh};
    my $dbh2   = $form->{dbh};
    my $query = qq|
		SELECT id FROM ar
		WHERE id = ?|;

    my $sth;
    $sth = $dbh->prepare($query);
    $sth->execute( $form->{id} );
    my ($id) = $sth->fetchrow_array;

    return unless $id;

    # reverse inventory items
    my $query = qq|
		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 = ?|;
    my $sth = $dbh->prepare($query);
    $sth->execute( $form->{id} ) || $form->dberror($query);

    while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {

        if ( $ref->{inventory_accno_id} || $ref->{assembly} ) {

            # if the invoice item is not an assemblyitem
            # adjust parts onhand
            if ( !$ref->{assemblyitem} ) {

                # adjust onhand in parts table
                $form->update_balance( $dbh, "parts", "onhand",
                    qq|id = $ref->{parts_id}|,
                    $ref->{qty} );
            }

            # loop if it is an assembly
            next if ( $ref->{assembly} );

            # de-allocated purchases
            $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);
              
            #================================================================
            if ($sth2->rows==0){
            $query = qq|
				  SELECT id, trans_id, allocated
				    FROM invoice
				   WHERE parts_id = ?
				         AND ((qty + allocated) = 0) AND (qty <0)
				ORDER BY trans_id DESC|;
            $sth2 = $dbh2->prepare($query);
            $sth2->execute( $ref->{parts_id} )
              || $form->dberror($query);
            }
            #==================================================================
            while ( my $inhref = $sth2->fetchrow_hashref(NAME_lc) ) {
                $allocated = $ref->{allocated};

                # update invoice
                $form->update_balance( $dbh, "invoice", "allocated",
                    qq|id = $inhref->{id}|,
                    $allocated );

                last if ( ( $ref->{qty} -= $qty ) <= 0 );
            }
            $sth2->finish;
        }
    }

    $sth->finish;

    # delete acc_trans
    $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;

    $sth = $dbh->prepare($query);
    $sth->execute( $form->{id} ) || $form->dberror($query);

    # delete invoice entries
    $query = qq|DELETE FROM invoice WHERE trans_id = ?|;
    $sth   = $dbh->prepare($query);
    $sth->execute( $form->{id} ) || $form->dberror($query);

    $query = qq|DELETE FROM shipto WHERE trans_id = ?|;
    $sth   = $dbh->prepare($query);
    $sth->execute( $form->{id} ) || $form->dberror($query);

    $dbh->commit;

}
