Repository: inventree/InvenTree

Issue:
Question: Importing Data with Creation Dates
I have a relatively simple question, but one that is really important to my use case. I've spent some time writing import scripts to bring data over from a legacy system into InvenTree. I'm doing so via Python using InvenTree's API to create objects.

However, when importing Purchase Order information, I noticed there is no documented way to create a Purchase Order with a custom creation date. It obviously would be ideal if I could preserve dates during my import, both for POs and other types of objects. Is there a recommended way of doing so?

I'm using the internal API documentation at `/api-doc/#tag/order/operation/order_po_.*_create`

Thank you in advance


Discussion:
By strictly looking on the code all orders (including the purchase orders) has a creation_date field.
In the [save logic it looks like there is care taken](https://github.com/inventree/InvenTree/blob/master/InvenTree/order/models.py#L194) to use the passed values, so I would recommend to set that field before the API call.
How could I set it _before_ the API call? I'm using [the python module documented here](https://docs.inventree.org/en/latest/api/python/python/)

I did try passing a field called `creation_date` with the API call with no luck. Something like

```python
from inventree.api import InvenTreeAPI
from inventree.purchase_order import PurchaseOrder

INVENTREE_API_URL = "http://localhost:8000"
INVENTREE_API_USERNAME = ""
INVENTREE_API_PASSWORD = ""
API = InvenTreeAPI(INVENTREE_API_URL, username=INVENTREE_API_USERNAME, password=INVENTREE_API_PASSWORD)

po_data = {
    "reference": "PO-12345"
    "supplier": "1276",
    "target_date": "2024-01-21", # this works!
    "creation_date": "2022-10-28" # this doesn't :(
}

PurchaseOrder.create(API, data)
```

Resolving patch:
diff --git a/InvenTree/order/models.py b/InvenTree/order/models.py
--- a/InvenTree/order/models.py
+++ b/InvenTree/order/models.py
@@ -211,7 +211,6 @@ def save(self, *args, **kwargs):
         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
--- 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)


Test patch:
diff --git a/InvenTree/order/test_api.py b/InvenTree/order/test_api.py
--- a/InvenTree/order/test_api.py
+++ b/InvenTree/order/test_api.py
@@ -357,6 +357,39 @@ def test_po_create(self):
             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')