Ignore variant stock when performing stocktake (#4752)

* Ignore variant stock when performing stocktake

- Including variant stock can cause duplicate counts of items
- Causes valuation numbers to be incorrect

* Fix unit tests

* Further unit test fixes
This commit is contained in:
Oliver 2023-05-02 23:57:40 +10:00 committed by GitHub
parent 6da108e031
commit baaa147fd0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 7 deletions

View File

@ -151,7 +151,9 @@ def perform_stocktake(target: part.models.Part, user: User, note: str = '', comm
"""
# Grab all "available" stock items for the Part
stock_entries = target.stock_entries(in_stock=True, include_variants=True)
# We do not include variant stock when performing a stocktake,
# otherwise the stocktake entries will be duplicated
stock_entries = target.stock_entries(in_stock=True, include_variants=False)
# Cache min/max pricing information for this Part
pricing = target.pricing

View File

@ -3079,22 +3079,21 @@ class PartStocktakeTest(InvenTreeAPITestCase):
from part.tasks import generate_stocktake_report
n_parts = Part.objects.count()
# Initially, no stocktake records are available
self.assertEqual(PartStocktake.objects.count(), 0)
# Generate stocktake data for all parts (default configuration)
generate_stocktake_report()
# There should now be 1 stocktake entry for each part
self.assertEqual(PartStocktake.objects.count(), n_parts)
# At least one report now created
n = PartStocktake.objects.count()
self.assertGreater(n, 0)
self.assignRole('stocktake.view')
response = self.get(reverse('api-part-stocktake-list'), expected_code=200)
self.assertEqual(len(response.data), n_parts)
self.assertEqual(len(response.data), n)
# Stocktake report should be available via the API, also
response = self.get(reverse('api-part-stocktake-report-list'), expected_code=200)
@ -3103,7 +3102,7 @@ class PartStocktakeTest(InvenTreeAPITestCase):
data = response.data[0]
self.assertEqual(data['part_count'], 14)
self.assertEqual(data['part_count'], 8)
self.assertEqual(data['user'], None)
self.assertTrue(data['report'].endswith('.csv'))