kelvinfix
kelvinfix

Reputation: 3075

Vb6 calculate update

How do I calculate and update the refundamt?

I have these three rows initially in database:

NO  |  TRANAMT  |  REFUNDAMT
1   |  100      |  0 
2   |  200      |  0
3   |  300      |  0

If refund is 350, the refundamt will be updated as follow, the refundamt cannot be more then the tranamt:

NO  |  TRANAMT  |  REFUNDAMT
1   |  100      |  100 
2   |  200      |  200
3   |  300      |  50

When refund again with 50, it will only update the last record, the refundamt will be updated as follow:

NO  |  TRANAMT  |  REFUNDAMT
1   |  100      |  100 
2   |  200      |  200
3   |  300      |  100

Upvotes: 0

Views: 105

Answers (1)

Emil Vikström
Emil Vikström

Reputation: 91953

Here's a simple algorithm (not considering multithreading and locks):

  1. Let RF be the total refund
  2. Find the first row where REFUNDAMT < TRANAMT and call it ROW
  3. Calculate the difference DIFF for ROW: DIFF = TRANAMT - REFUNDAMT
    • If DIFF >= RF, update REFUNDAMT in ROW to REFUNDAMT+RF and you are done.
    • If DIFF < RF, update REFUNDAMT in ROW to TRANAMT, let RF = RF-DIFF and go to step 2.

Upvotes: 1

Related Questions