mirror of
https://github.com/inventree/InvenTree.git
synced 2025-07-02 19:50:59 +00:00
Add flag to API which allows using pack size (#4741)
* Add flag to API which allows using pack size when adding stock items manually * Check for use_pack_size before pop * Add test data and tests * Improve data handling * Add form field for use_pack_size when adding stock * Add description of pack size to docs * Don't check for supplier part if it is None * Move form field to after supplier part, for better logic * Fix wrong function * Fix tests * Adjust purchase price when using pack size * Adjust help text for purchase price * Adjust help text for purchase price some more * Fix tests for purchase price of added stock * Update api_version.py
This commit is contained in:
@ -10,6 +10,7 @@ from django.core.exceptions import ValidationError
|
||||
from django.urls import reverse
|
||||
|
||||
import tablib
|
||||
from djmoney.money import Money
|
||||
from rest_framework import status
|
||||
|
||||
import company.models
|
||||
@ -664,6 +665,113 @@ class StockItemTest(StockAPITestCase):
|
||||
expected_code=201
|
||||
)
|
||||
|
||||
def test_stock_item_create_withsupplierpart(self):
|
||||
"""Test creation of a StockItem via the API, including SupplierPart data."""
|
||||
|
||||
# POST with non-existent supplier part
|
||||
response = self.post(
|
||||
self.list_url,
|
||||
data={
|
||||
'part': 1,
|
||||
'location': 1,
|
||||
'quantity': 4,
|
||||
'supplier_part': 1000991
|
||||
},
|
||||
expected_code=400
|
||||
)
|
||||
|
||||
self.assertIn('The given supplier part does not exist', str(response.data))
|
||||
|
||||
# POST with valid supplier part, no pack size defined
|
||||
# Get current count of number of parts
|
||||
part_4 = part.models.Part.objects.get(pk=4)
|
||||
current_count = part_4.available_stock
|
||||
response = self.post(
|
||||
self.list_url,
|
||||
data={
|
||||
'part': 4,
|
||||
'location': 1,
|
||||
'quantity': 3,
|
||||
'supplier_part': 5,
|
||||
'purchase_price': 123.45,
|
||||
'purchase_price_currency': 'USD',
|
||||
},
|
||||
expected_code=201
|
||||
)
|
||||
# Reload part, count stock again
|
||||
part_4 = part.models.Part.objects.get(pk=4)
|
||||
self.assertEqual(part_4.available_stock, current_count + 3)
|
||||
stock_4 = StockItem.objects.get(pk=response.data['pk'])
|
||||
self.assertEqual(stock_4.purchase_price, Money('123.450000', 'USD'))
|
||||
|
||||
# POST with valid supplier part, no pack size defined
|
||||
# Send use_pack_size along, make sure this doesn't break stuff
|
||||
# Get current count of number of parts
|
||||
part_4 = part.models.Part.objects.get(pk=4)
|
||||
current_count = part_4.available_stock
|
||||
response = self.post(
|
||||
self.list_url,
|
||||
data={
|
||||
'part': 4,
|
||||
'location': 1,
|
||||
'quantity': 12,
|
||||
'supplier_part': 5,
|
||||
'use_pack_size': True,
|
||||
'purchase_price': 123.45,
|
||||
'purchase_price_currency': 'USD',
|
||||
},
|
||||
expected_code=201
|
||||
)
|
||||
# Reload part, count stock again
|
||||
part_4 = part.models.Part.objects.get(pk=4)
|
||||
self.assertEqual(part_4.available_stock, current_count + 12)
|
||||
stock_4 = StockItem.objects.get(pk=response.data['pk'])
|
||||
self.assertEqual(stock_4.purchase_price, Money('123.450000', 'USD'))
|
||||
|
||||
# POST with valid supplier part, WITH pack size defined - but ignore
|
||||
# Supplier part 6 is a 100-pack, otherwise same as SP 5
|
||||
current_count = part_4.available_stock
|
||||
response = self.post(
|
||||
self.list_url,
|
||||
data={
|
||||
'part': 4,
|
||||
'location': 1,
|
||||
'quantity': 3,
|
||||
'supplier_part': 6,
|
||||
'use_pack_size': False,
|
||||
'purchase_price': 123.45,
|
||||
'purchase_price_currency': 'USD',
|
||||
},
|
||||
expected_code=201
|
||||
)
|
||||
# Reload part, count stock again
|
||||
part_4 = part.models.Part.objects.get(pk=4)
|
||||
self.assertEqual(part_4.available_stock, current_count + 3)
|
||||
stock_4 = StockItem.objects.get(pk=response.data['pk'])
|
||||
self.assertEqual(stock_4.purchase_price, Money('123.450000', 'USD'))
|
||||
|
||||
# POST with valid supplier part, WITH pack size defined and used
|
||||
# Supplier part 6 is a 100-pack, otherwise same as SP 5
|
||||
current_count = part_4.available_stock
|
||||
response = self.post(
|
||||
self.list_url,
|
||||
data={
|
||||
'part': 4,
|
||||
'location': 1,
|
||||
'quantity': 3,
|
||||
'supplier_part': 6,
|
||||
'use_pack_size': True,
|
||||
'purchase_price': 123.45,
|
||||
'purchase_price_currency': 'USD',
|
||||
},
|
||||
expected_code=201
|
||||
)
|
||||
# Reload part, count stock again
|
||||
part_4 = part.models.Part.objects.get(pk=4)
|
||||
self.assertEqual(part_4.available_stock, current_count + 3 * 100)
|
||||
stock_4 = StockItem.objects.get(pk=response.data['pk'])
|
||||
self.assertEqual(stock_4.purchase_price, Money('1.234500', 'USD'))
|
||||
|
||||
def test_creation_with_serials(self):
|
||||
"""Test that serialized stock items can be created via the API."""
|
||||
trackable_part = part.models.Part.objects.create(
|
||||
|
Reference in New Issue
Block a user