Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finalise stocktake making invalid stock adjustments (into negative on ledger) #629

Closed
Chris-Petty opened this issue Mar 14, 2018 · 5 comments
Labels
Bug: production Bug was found or believed to be in a live release Priority: high

Comments

@Chris-Petty
Copy link
Contributor

Chris-Petty commented Mar 14, 2018

Build Number: 2.1.0-rc8

Description:
Through a fun sequence of editing you can make stocktakes that show a negative ledger total for an item but show's "fine" on mobile. Both are wrong. Fortunately it's not totally easy to achieve.

Reproducible: Yes

Reproduction Steps:

  1. Make a new stocktake with an item
  2. Make sure it has some quantity for snapshot (say 100)
  3. Make a customer invoice for the same item and issue some amount (say 100)
  4. Go back to the stocktake and set the actual quantity to 0. You cannot finalise because it'd make negative stock.
  5. Set actual quantity >= 100
  6. Edit actual quantity to 0 AND hit finalise before anything else
  7. Confirm and viola, you have messed the ledger.

In mSupply mobile, it'll say there is 0 stock. In mSupply Desktop for that store it'll say 0 stock too, but by the ledger it should have -100 stock.

(I've done it a couple times here, so -250)
image

Comments: 🙈

@Chris-Petty Chris-Petty added Priority: high Bug: production Bug was found or believed to be in a live release labels Mar 14, 2018
@anildahalsussol
Copy link
Contributor

Yah I also faced this problem in medical action myanmar with negative stock which is creating ledger problem. Sorry i didnot made the issue but i informed to andrei about it.

@andreievg
Copy link
Contributor

andreievg commented Mar 19, 2018

@Chris-Petty
I think the really issue is in the callback nature of database.write(()=>{}).. We can also finalise CustomInvoice/Stocktake with 0 quantities, i.e. set quantity to something other then zero, then back to zero then immediately finalise...

I like your fix: #634 , I think it can be extended, after checking 'checkForFinaliseError' and before record.finalise.. call checkForFinalisationError again.

@andreievg
Copy link
Contributor

I had a further look into this issue, first assumption was incorrect, it's nothing to do with realm.. It seems that onEndEditing is called after the button click, it's called just after the modal is shown. If you create a simple react native app with just a button and a text input, when button is pressed the text is still in focus. We can call Keyboard.dismiss() to un-focus the text input, and only after that try to open the finalisation modal. I've tested the code below, it works, but the timeout of 1 second might not be sufficient in all cases:

// mSupplyMobileApp
  renderFinaliseButton = () => (
    <FinaliseButton
      isFinalised={this.props.finaliseItem.record.isFinalised}
      onPress={() => {
        Keyboard.dismiss();
        setTimeout(() => this.setState({ confirmFinalise: true }), 1000);
      }}
    />
  )

@Chris-Petty
Copy link
Contributor Author

Yea I also tested keyboard.dismiss to try and get onEndEditing to run before the modal did it's thing, but it wasn't quick enough. The setTimeout is a bit of a hack/dirty I thought and potentially flawed as you mentioned.

I did try to think if there was a way to use database.isInTransaction and async/promise to do it a bit smarter but was in a bit of a hurry so made a PR of what I had done.

andreievg added a commit that referenced this issue Mar 26, 2018
…djustments

Looks good to me, I've tested both reducing below minimum in stocktake and finalising 0 quantity in customer invoice. Both times a valid message appears after pressing confirm.
@Chris-Petty
Copy link
Contributor Author

Fixed by #634

Further discussions at #638

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug: production Bug was found or believed to be in a live release Priority: high
Projects
None yet
Development

No branches or pull requests

3 participants