From ff33cc43bd66a1931370e603162aed9ebf13e06b Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 21 Jun 2021 17:28:41 +1000 Subject: [PATCH 1/2] Create initial migration file --- .../migrations/0064_auto_20210621_1724.py | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 InvenTree/stock/migrations/0064_auto_20210621_1724.py diff --git a/InvenTree/stock/migrations/0064_auto_20210621_1724.py b/InvenTree/stock/migrations/0064_auto_20210621_1724.py new file mode 100644 index 0000000000..bc40a7523b --- /dev/null +++ b/InvenTree/stock/migrations/0064_auto_20210621_1724.py @@ -0,0 +1,41 @@ +# Generated by Django 3.2.4 on 2021-06-21 07:24 + +from django.db import migrations + + +def extract_purchase_price(apps, schema_editor): + """ + Find instances of StockItem which do *not* have a purchase price set, + but which point to a PurchaseOrder where there *is* a purchase price set. + + Then, assign *that* purchase price to original StockItem. + + This is to address an issue where older versions of InvenTree + did not correctly copy purchase price information cross to the StockItem objects. + + Current InvenTree version (as of 2021-06-21) copy this information across correctly, + so this one-time data migration should suffice. + """ + + # Required database models + StockItem = apps.get_model('stock', 'stockitem') + PurchaseOrder = apps.get_model('order', 'purchaseorder') + Part = apps.get_model('part', 'part') + + +def reverse_operation(apps, schema_editor): + """ + DO NOTHING! + """ + pass + + +class Migration(migrations.Migration): + + dependencies = [ + ('stock', '0063_auto_20210511_2343'), + ] + + operations = [ + migrations.RunPython(extract_purchase_price, reverse_code=reverse_operation) + ] From 02540edd589c51cdd20bb330174fb45cc58d7cc9 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 21 Jun 2021 20:30:15 +1000 Subject: [PATCH 2/2] Copy purchase price information across in the data migration --- .../migrations/0064_auto_20210621_1724.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/InvenTree/stock/migrations/0064_auto_20210621_1724.py b/InvenTree/stock/migrations/0064_auto_20210621_1724.py index bc40a7523b..71314f366c 100644 --- a/InvenTree/stock/migrations/0064_auto_20210621_1724.py +++ b/InvenTree/stock/migrations/0064_auto_20210621_1724.py @@ -20,8 +20,43 @@ def extract_purchase_price(apps, schema_editor): # Required database models StockItem = apps.get_model('stock', 'stockitem') PurchaseOrder = apps.get_model('order', 'purchaseorder') + PurchaseOrderLineItem = apps.get_model('order', 'purchaseorderlineitem') Part = apps.get_model('part', 'part') + # Find all the StockItem objects without a purchase_price which point to a PurchaseOrder + items = StockItem.objects.filter(purchase_price=None).exclude(purchase_order=None) + + print(f"Found {items.count()} stock items with missing purchase price information") + + update_count = 0 + + for item in items: + + part_id = item.part + + po = item.purchase_order + + # Look for a matching PurchaseOrderLineItem (with a price) + lines = PurchaseOrderLineItem.objects.filter(part__part=part_id, order=po) + + if lines.exists(): + + for line in lines: + if line.purchase_price is not None: + + # Copy pricing information across + item.purchase_price = line.purchase_price + item.purchases_price_currency = line.purchase_price_currency + + print(f"- Updating supplier price for {item.part.name} - {item.purchase_price} {item.purchase_price_currency}") + + update_count += 1 + + item.save() + + break + + print(f"Updated pricing for {update_count} stock items") def reverse_operation(apps, schema_editor): """