2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-10 23:14:13 +00:00

Fix for returning stock from the customer to a specified location (#4208)

* Fix for returning stock from the customer to a specified location

When returning stock from the customer, the allocations of that stock
are freed. If the new stock location is the same as the parent stock,
merge the stock back into the parent.

* Added tests for allocateToCustomer and return_from_customer

* Added additional test that checks total stock

* Fixed typos
This commit is contained in:
bloemp
2023-01-24 23:34:15 +01:00
committed by GitHub
parent 6e234e4a14
commit 8316086c61
2 changed files with 89 additions and 2 deletions

View File

@ -974,7 +974,11 @@ class StockItem(InvenTreeBarcodeMixin, MetadataMixin, MPTTModel):
@transaction.atomic
def return_from_customer(self, location, user=None, **kwargs):
"""Return stock item from customer, back into the specified location."""
"""Return stock item from customer, back into the specified location.
If the selected location is the same as the parent, merge stock back into the parent.
Otherwise create the stock in the new location
"""
notes = kwargs.get('notes', '')
tracking_info = {}
@ -991,7 +995,10 @@ class StockItem(InvenTreeBarcodeMixin, MetadataMixin, MPTTModel):
location=location
)
self.clearAllocations()
self.customer = None
self.belongs_to = None
self.sales_order = None
self.location = location
trigger_event(
@ -999,7 +1006,16 @@ class StockItem(InvenTreeBarcodeMixin, MetadataMixin, MPTTModel):
id=self.id,
)
self.save()
"""If new location is the same as the parent location, merge this stock back in the parent"""
if self.parent and self.location == self.parent.location:
self.parent.merge_stock_items(
{self},
user=user,
location=location,
notes=notes
)
else:
self.save()
def is_allocated(self):
"""Return True if this StockItem is allocated to a SalesOrder or a Build."""