Function to predict the next purchase order number

This commit is contained in:
Oliver Walters 2020-05-14 15:00:00 +10:00
parent 41eff97c7c
commit cebfe9a30f
3 changed files with 47 additions and 3 deletions

View File

@ -4,7 +4,7 @@
- model: order.purchaseorder - model: order.purchaseorder
pk: 1 pk: 1
fields: fields:
reference: 0001 reference: '0001'
description: "Ordering some screws" description: "Ordering some screws"
supplier: 1 supplier: 1
@ -12,7 +12,7 @@
- model: order.purchaseorder - model: order.purchaseorder
pk: 2 pk: 2
fields: fields:
reference: 0002 reference: '0002'
description: "Ordering some more screws" description: "Ordering some more screws"
supplier: 3 supplier: 3

View File

@ -24,7 +24,7 @@ from stock import models as stock_models
from company.models import Company, SupplierPart from company.models import Company, SupplierPart
from InvenTree.fields import RoundingDecimalField 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.status_codes import PurchaseOrderStatus, SalesOrderStatus, StockStatus
from InvenTree.models import InvenTreeAttachment from InvenTree.models import InvenTreeAttachment
@ -96,6 +96,44 @@ class PurchaseOrder(Order):
ORDER_PREFIX = "PO" 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): def __str__(self):
return "PO {ref} - {company}".format(ref=self.reference, company=self.supplier.name) return "PO {ref} - {company}".format(ref=self.reference, company=self.supplier.name)

View File

@ -37,6 +37,12 @@ class OrderTest(TestCase):
self.assertEqual(str(line), "100 x ACME0001 from ACME (for PO 1 - ACME)") 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): def test_on_order(self):
""" There should be 3 separate items on order for the M2x4 LPHS part """ """ There should be 3 separate items on order for the M2x4 LPHS part """