mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Function to predict the next purchase order number
This commit is contained in:
parent
41eff97c7c
commit
cebfe9a30f
@ -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
|
||||
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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 """
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user