diff --git a/InvenTree/order/fixtures/order.yaml b/InvenTree/order/fixtures/order.yaml index 0ba4bdbeb5..ed4eae84eb 100644 --- a/InvenTree/order/fixtures/order.yaml +++ b/InvenTree/order/fixtures/order.yaml @@ -4,7 +4,7 @@ - model: order.purchaseorder pk: 1 fields: - reference: 0001 + reference: '0001' description: "Ordering some screws" supplier: 1 @@ -12,7 +12,7 @@ - model: order.purchaseorder pk: 2 fields: - reference: 0002 + reference: '0002' description: "Ordering some more screws" supplier: 3 diff --git a/InvenTree/order/models.py b/InvenTree/order/models.py index 56da6c783d..5e6c5e521d 100644 --- a/InvenTree/order/models.py +++ b/InvenTree/order/models.py @@ -24,7 +24,7 @@ from stock import models as stock_models from company.models import Company, SupplierPart from InvenTree.fields import RoundingDecimalField -from InvenTree.helpers import decimal2string +from InvenTree.helpers import decimal2string, increment from InvenTree.status_codes import PurchaseOrderStatus, SalesOrderStatus, StockStatus from InvenTree.models import InvenTreeAttachment @@ -96,6 +96,44 @@ class PurchaseOrder(Order): ORDER_PREFIX = "PO" + @classmethod + def getNextOrderNumber(cls): + """ + Try to predict the next order-number + """ + + if PurchaseOrder.objects.count() == 0: + return None + + # We will assume that the latest pk has the highest PO number + order = PurchaseOrder.objects.last() + ref = order.reference + + if not ref: + return None + + tries = set() + + tries.add(ref) + + while 1: + new_ref = increment(ref) + + if new_ref in tries: + # We are in a looping situation - simply return the original one + return ref + + # Check that the new ref does not exist in the database + if PurchaseOrder.objects.filter(reference=new_ref).exists(): + tries.add(new_ref) + new_ref = increment(new_ref) + + else: + break + + return new_ref + + def __str__(self): return "PO {ref} - {company}".format(ref=self.reference, company=self.supplier.name) diff --git a/InvenTree/order/tests.py b/InvenTree/order/tests.py index ca24b9586d..89c39db8f5 100644 --- a/InvenTree/order/tests.py +++ b/InvenTree/order/tests.py @@ -37,6 +37,12 @@ class OrderTest(TestCase): self.assertEqual(str(line), "100 x ACME0001 from ACME (for PO 1 - ACME)") + def test_increment(self): + + next_ref = PurchaseOrder.getNextOrderNumber() + + self.assertEqual(next_ref, '0003') + def test_on_order(self): """ There should be 3 separate items on order for the M2x4 LPHS part """