inventree / inventree-python

Python library for communication with inventree via API
https://docs.inventree.org/en/latest/api/python/python/
MIT License
26 stars 34 forks source link

Receive line items with 0 quantity remaining #200

Closed Elitearcanist closed 11 months ago

Elitearcanist commented 11 months ago

I have some code that receives line items one by one on a purchase order.

line_items = purcahse_order.getLineItems()
for line_item in line_items:
     line_item.receive(location=loc, batch_code=batch)

(This is very simplified but shows the point)

I noticed however that if it tried to receive a line item that had already been fully received it would create an API error. requests.exceptions.HTTPError: {'detail': 'Error occurred during API request', 'method': 'POST', 'status_code': 400, 'body': '{"items":[{"quantity":["Quantity must be greater than zero"]}]}', 'params': {'format': 'json'}, 'data': {'items': [{'line_item': 8828, 'supplier_part': 4147, 'quantity': 0.0, 'status': 10, 'location': 6, 'batch_code': '99999999', 'serial_numbers': ''}], 'location': 6}} (removed url and token for security)

The API wont accept receiving 0 stock but the python library does. It would make sense to have the library skip over these receive requests. I also noticed that po.recieveAll() does check that a line item has more than 0 to receive

Steps to Recreate:

  1. Use line_item.receive() to receive a PO line item
  2. Use line_item.receive() again on the same line item
Elitearcanist commented 11 months ago

I am not using po.receiveAll() because it doesn't quite work for my use case.

SchrodingersGat commented 11 months ago

@Elitearcanist would you be willing to submit a PR to address the issue?

miggland commented 11 months ago

Consider this first:

There are many cases where the API does not do any error checking before submitting data to the server. I think this applies to most commands.

Functions like "Receive All" have basic logic, because they should be straight forward to use and shouldn't break down in simple cases.

I think it makes sense to leave the logic and input checking mostly to the server. Otherwise, the logic will have to be implemented twice - which is a mess to keep track of.

In your case, you could check the amount remaining first in your script, or catch and handle the error.

On 9 August 2023 01:51:00 CEST, Oliver @.***> wrote:

@Elitearcanist would you be willing to submit a PR to address the issue?

-- Reply to this email directly or view it on GitHub: https://github.com/inventree/inventree-python/issues/200#issuecomment-1670457986 You are receiving this because you are subscribed to this thread.

Message ID: @.***>

SchrodingersGat commented 11 months ago

@miggland makes a good point here. The error message received here does contain all the information you need! I agree that wherever possible we should lean on the validation performed by the server

Elitearcanist commented 11 months ago

Fair enough closing...