diff --git a/InvenTree/order/models.py b/InvenTree/order/models.py index e224053f7f..2eb3aecf0f 100644 --- a/InvenTree/order/models.py +++ b/InvenTree/order/models.py @@ -211,7 +211,6 @@ class Order( Ensures that the reference field is rebuilt whenever the instance is saved. """ self.reference_int = self.rebuild_reference_field(self.reference) - if not self.creation_date: self.creation_date = datetime.now().date() diff --git a/InvenTree/order/serializers.py b/InvenTree/order/serializers.py index c6e777c70a..ec5ba4d200 100644 --- a/InvenTree/order/serializers.py +++ b/InvenTree/order/serializers.py @@ -97,6 +97,8 @@ class AbstractOrderSerializer(serializers.Serializer): barcode_hash = serializers.CharField(read_only=True) + creation_date = serializers.DateField(required=False, allow_null=True) + def validate_reference(self, reference): """Custom validation for the reference field.""" self.Meta.model.validate_reference_field(reference) diff --git a/InvenTree/order/test_api.py b/InvenTree/order/test_api.py index 889f6477fc..a15bebce48 100644 --- a/InvenTree/order/test_api.py +++ b/InvenTree/order/test_api.py @@ -357,6 +357,39 @@ class PurchaseOrderTest(OrderTest): expected_code=201, ) + def test_po_creation_date(self): + """Test that we can create set the creation_date field of PurchaseOrder via the API.""" + self.assignRole('purchase_order.add') + + response = self.post( + reverse('api-po-list'), + { + 'reference': 'PO-19881110', + 'supplier': 1, + 'description': 'PO created on 1988-11-10', + 'creation_date': '1988-11-10', + }, + expected_code=201, + ) + + po = models.PurchaseOrder.objects.get(pk=response.data['pk']) + self.assertEqual(po.creation_date, datetime(1988, 11, 10).date()) + + """Ensure if we do not pass the creation_date field than the current date will be saved""" + creation_date = datetime.now().date() + response = self.post( + reverse('api-po-list'), + { + 'reference': 'PO-11111111', + 'supplier': 1, + 'description': 'Check that the creation date is today', + }, + expected_code=201, + ) + + po = models.PurchaseOrder.objects.get(pk=response.data['pk']) + self.assertEqual(po.creation_date, creation_date) + def test_po_duplicate(self): """Test that we can duplicate a PurchaseOrder via the API.""" self.assignRole('purchase_order.add')