mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-17 20:45:44 +00:00
Create "SalesOrderAllocation" object
- Links multiple StockItem objects to a single SalesOrderLineItem
This commit is contained in:
@ -335,7 +335,7 @@ class SOLineItemList(generics.ListCreateAPIView):
|
||||
return queryset.prefetch_related(
|
||||
'part',
|
||||
'part__stock_items',
|
||||
'stock_items',
|
||||
'allocations',
|
||||
'order',
|
||||
)
|
||||
|
||||
|
26
InvenTree/order/migrations/0024_salesorderallocation.py
Normal file
26
InvenTree/order/migrations/0024_salesorderallocation.py
Normal file
@ -0,0 +1,26 @@
|
||||
# Generated by Django 3.0.5 on 2020-04-22 02:09
|
||||
|
||||
import InvenTree.fields
|
||||
import django.core.validators
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('stock', '0030_auto_20200422_0015'),
|
||||
('order', '0023_auto_20200420_2309'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='SalesOrderAllocation',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('quantity', InvenTree.fields.RoundingDecimalField(decimal_places=5, default=1, max_digits=15, validators=[django.core.validators.MinValueValidator(0)])),
|
||||
('item', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='sales_order_allocation', to='stock.StockItem')),
|
||||
('line', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='allocations', to='order.SalesOrderLineItem')),
|
||||
],
|
||||
),
|
||||
]
|
@ -380,6 +380,26 @@ class SalesOrderLineItem(OrderLineItem):
|
||||
This is a summation of the quantity of each attached StockItem
|
||||
"""
|
||||
|
||||
query = self.stock_items.aggregate(allocated=Coalesce(Sum('quantity'), Decimal(0)))
|
||||
query = self.allocations.aggregate(allocated=Coalesce(Sum('quantity'), Decimal(0)))
|
||||
|
||||
return query['allocated']
|
||||
|
||||
|
||||
class SalesOrderAllocation(models.Model):
|
||||
"""
|
||||
This model is used to 'allocate' stock items to a SalesOrder.
|
||||
Items that are "allocated" to a SalesOrder are not yet "attached" to the order,
|
||||
but they will be once the order is fulfilled.
|
||||
|
||||
Attributes:
|
||||
line: SalesOrderLineItem reference
|
||||
item: StockItem reference
|
||||
quantity: Quantity to take from the StockItem
|
||||
|
||||
"""
|
||||
|
||||
line = models.ForeignKey(SalesOrderLineItem, on_delete=models.CASCADE, related_name='allocations')
|
||||
|
||||
item = models.OneToOneField('stock.StockItem', on_delete=models.CASCADE, related_name='sales_order_allocation')
|
||||
|
||||
quantity = RoundingDecimalField(max_digits=15, decimal_places=5, validators=[MinValueValidator(0)], default=1)
|
||||
|
@ -31,11 +31,11 @@ class OrderTest(TestCase):
|
||||
|
||||
self.assertEqual(order.get_absolute_url(), '/order/purchase-order/1/')
|
||||
|
||||
self.assertEqual(str(order), 'PO 1')
|
||||
self.assertEqual(str(order), 'PO 1 - ACME')
|
||||
|
||||
line = PurchaseOrderLineItem.objects.get(pk=1)
|
||||
|
||||
self.assertEqual(str(line), "100 x ACME0001 from ACME (for PO 1)")
|
||||
self.assertEqual(str(line), "100 x ACME0001 from ACME (for PO 1 - ACME)")
|
||||
|
||||
def test_on_order(self):
|
||||
""" There should be 3 separate items on order for the M2x4 LPHS part """
|
||||
|
Reference in New Issue
Block a user