Auto-increment sales order reference number

This commit is contained in:
Oliver Walters 2020-05-14 15:05:55 +10:00
parent 08903f357e
commit b619f26074
2 changed files with 40 additions and 40 deletions

View File

@ -49,6 +49,43 @@ class Order(models.Model):
ORDER_PREFIX = "" ORDER_PREFIX = ""
@classmethod
def getNextOrderNumber(cls):
"""
Try to predict the next order-number
"""
if cls.objects.count() == 0:
return None
# We will assume that the latest pk has the highest PO number
order = cls.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 cls.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):
el = [] el = []
@ -96,44 +133,6 @@ 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

@ -29,7 +29,7 @@ from . import forms as order_forms
from InvenTree.views import AjaxView, AjaxCreateView, AjaxUpdateView, AjaxDeleteView from InvenTree.views import AjaxView, AjaxCreateView, AjaxUpdateView, AjaxDeleteView
from InvenTree.helpers import DownloadFile, str2bool from InvenTree.helpers import DownloadFile, str2bool
from InvenTree.status_codes import PurchaseOrderStatus, StockStatus from InvenTree.status_codes import PurchaseOrderStatus, SalesOrderStatus, StockStatus
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -321,7 +321,8 @@ class SalesOrderCreate(AjaxCreateView):
def get_initial(self): def get_initial(self):
initials = super().get_initial().copy() initials = super().get_initial().copy()
initials['status'] = PurchaseOrderStatus.PENDING initials['reference'] = SalesOrder.getNextOrderNumber()
initials['status'] = SalesOrderStatus.PENDING
customer_id = self.request.GET.get('customer', None) customer_id = self.request.GET.get('customer', None)