2019-08-02 11:44:56 +00:00
|
|
|
"""
|
|
|
|
JSON serializers for the Order API
|
|
|
|
"""
|
|
|
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2020-04-19 23:41:21 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
|
from django.db.models import Count
|
|
|
|
|
2019-08-02 11:44:56 +00:00
|
|
|
from InvenTree.serializers import InvenTreeModelSerializer
|
2020-05-26 10:16:10 +00:00
|
|
|
from InvenTree.serializers import InvenTreeAttachmentSerializerField
|
|
|
|
|
2020-04-24 02:52:08 +00:00
|
|
|
from company.serializers import CompanyBriefSerializer, SupplierPartSerializer
|
2020-04-20 22:57:13 +00:00
|
|
|
from part.serializers import PartBriefSerializer
|
2019-08-02 11:44:56 +00:00
|
|
|
|
|
|
|
from .models import PurchaseOrder, PurchaseOrderLineItem
|
2020-05-11 13:41:57 +00:00
|
|
|
from .models import PurchaseOrderAttachment, SalesOrderAttachment
|
2020-04-20 10:11:21 +00:00
|
|
|
from .models import SalesOrder, SalesOrderLineItem
|
2020-04-22 10:10:23 +00:00
|
|
|
from .models import SalesOrderAllocation
|
2019-08-02 11:44:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class POSerializer(InvenTreeModelSerializer):
|
2020-04-20 10:11:21 +00:00
|
|
|
""" Serializer for a PurchaseOrder object """
|
2019-08-02 11:44:56 +00:00
|
|
|
|
2020-04-19 23:41:21 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
|
|
supplier_detail = kwargs.pop('supplier_detail', False)
|
|
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
if supplier_detail is not True:
|
|
|
|
self.fields.pop('supplier_detail')
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def annotate_queryset(queryset):
|
|
|
|
"""
|
|
|
|
Add extra information to the queryset
|
|
|
|
"""
|
|
|
|
|
|
|
|
return queryset.annotate(
|
|
|
|
line_items=Count('lines'),
|
|
|
|
)
|
|
|
|
|
|
|
|
supplier_detail = CompanyBriefSerializer(source='supplier', many=False, read_only=True)
|
|
|
|
|
|
|
|
line_items = serializers.IntegerField(read_only=True)
|
|
|
|
|
|
|
|
status_text = serializers.CharField(source='get_status_display', read_only=True)
|
|
|
|
|
2019-08-02 11:44:56 +00:00
|
|
|
class Meta:
|
|
|
|
model = PurchaseOrder
|
|
|
|
|
|
|
|
fields = [
|
|
|
|
'pk',
|
2020-04-19 23:48:33 +00:00
|
|
|
'issue_date',
|
|
|
|
'complete_date',
|
|
|
|
'creation_date',
|
2019-08-02 11:44:56 +00:00
|
|
|
'description',
|
2020-04-19 23:41:21 +00:00
|
|
|
'line_items',
|
2020-04-06 01:56:52 +00:00
|
|
|
'link',
|
2020-04-19 23:41:21 +00:00
|
|
|
'reference',
|
|
|
|
'supplier',
|
|
|
|
'supplier_detail',
|
|
|
|
'supplier_reference',
|
2019-08-02 11:44:56 +00:00
|
|
|
'status',
|
2020-04-19 23:41:21 +00:00
|
|
|
'status_text',
|
2019-08-02 11:44:56 +00:00
|
|
|
'notes',
|
|
|
|
]
|
|
|
|
|
|
|
|
read_only_fields = [
|
|
|
|
'reference',
|
|
|
|
'status'
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class POLineItemSerializer(InvenTreeModelSerializer):
|
|
|
|
|
2020-04-24 02:52:08 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
|
|
part_detail = kwargs.pop('part_detail', False)
|
|
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
if part_detail is not True:
|
|
|
|
self.fields.pop('part_detail')
|
|
|
|
self.fields.pop('supplier_part_detail')
|
|
|
|
|
|
|
|
quantity = serializers.FloatField()
|
|
|
|
received = serializers.FloatField()
|
|
|
|
|
|
|
|
part_detail = PartBriefSerializer(source='get_base_part', many=False, read_only=True)
|
|
|
|
supplier_part_detail = SupplierPartSerializer(source='part', many=False, read_only=True)
|
|
|
|
|
2019-08-02 11:44:56 +00:00
|
|
|
class Meta:
|
|
|
|
model = PurchaseOrderLineItem
|
|
|
|
|
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'quantity',
|
|
|
|
'reference',
|
|
|
|
'notes',
|
|
|
|
'order',
|
|
|
|
'part',
|
2020-04-24 02:52:08 +00:00
|
|
|
'part_detail',
|
|
|
|
'supplier_part_detail',
|
2019-08-02 11:44:56 +00:00
|
|
|
'received',
|
|
|
|
]
|
2020-04-20 10:11:21 +00:00
|
|
|
|
|
|
|
|
2020-05-11 13:41:57 +00:00
|
|
|
class POAttachmentSerializer(InvenTreeModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializers for the PurchaseOrderAttachment model
|
|
|
|
"""
|
|
|
|
|
2020-05-26 10:26:37 +00:00
|
|
|
attachment = InvenTreeAttachmentSerializerField(required=True)
|
2020-05-26 10:16:10 +00:00
|
|
|
|
2020-05-11 13:41:57 +00:00
|
|
|
class Meta:
|
|
|
|
model = PurchaseOrderAttachment
|
|
|
|
|
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'order',
|
|
|
|
'attachment',
|
|
|
|
'comment',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-04-20 22:57:13 +00:00
|
|
|
class SalesOrderSerializer(InvenTreeModelSerializer):
|
2020-04-20 10:11:21 +00:00
|
|
|
"""
|
|
|
|
Serializers for the SalesOrder object
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
|
|
customer_detail = kwargs.pop('customer_detail', False)
|
|
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
if customer_detail is not True:
|
|
|
|
self.fields.pop('customer_detail')
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def annotate_queryset(queryset):
|
|
|
|
"""
|
|
|
|
Add extra information to the queryset
|
|
|
|
"""
|
|
|
|
|
|
|
|
return queryset.annotate(
|
|
|
|
line_items=Count('lines'),
|
|
|
|
)
|
|
|
|
|
|
|
|
customer_detail = CompanyBriefSerializer(source='customer', many=False, read_only=True)
|
|
|
|
|
|
|
|
line_items = serializers.IntegerField(read_only=True)
|
|
|
|
|
|
|
|
status_text = serializers.CharField(source='get_status_display', read_only=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = SalesOrder
|
|
|
|
|
|
|
|
fields = [
|
|
|
|
'pk',
|
2020-04-23 10:42:59 +00:00
|
|
|
'shipment_date',
|
2020-04-20 10:11:21 +00:00
|
|
|
'creation_date',
|
2020-04-20 10:27:52 +00:00
|
|
|
'description',
|
2020-04-20 10:11:21 +00:00
|
|
|
'line_items',
|
|
|
|
'link',
|
|
|
|
'reference',
|
|
|
|
'customer',
|
|
|
|
'customer_detail',
|
|
|
|
'customer_reference',
|
|
|
|
'status',
|
|
|
|
'status_text',
|
2020-04-26 05:29:21 +00:00
|
|
|
'shipment_date',
|
2020-04-20 10:11:21 +00:00
|
|
|
'notes',
|
|
|
|
]
|
|
|
|
|
|
|
|
read_only_fields = [
|
|
|
|
'reference',
|
|
|
|
'status'
|
|
|
|
]
|
2020-04-20 11:11:59 +00:00
|
|
|
|
|
|
|
|
2020-04-22 10:10:23 +00:00
|
|
|
class SalesOrderAllocationSerializer(InvenTreeModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for the SalesOrderAllocation model.
|
|
|
|
This includes some fields from the related model objects.
|
|
|
|
"""
|
|
|
|
|
|
|
|
location_path = serializers.CharField(source='get_location_path')
|
|
|
|
location_id = serializers.IntegerField(source='get_location')
|
2020-04-27 01:32:20 +00:00
|
|
|
serial = serializers.CharField(source='get_serial')
|
|
|
|
quantity = serializers.FloatField()
|
2020-04-22 10:10:23 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = SalesOrderAllocation
|
|
|
|
|
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'line',
|
2020-04-27 01:32:20 +00:00
|
|
|
'serial',
|
|
|
|
'quantity',
|
2020-04-22 10:10:23 +00:00
|
|
|
'location_id',
|
|
|
|
'location_path',
|
|
|
|
'item',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-04-20 11:11:59 +00:00
|
|
|
class SOLineItemSerializer(InvenTreeModelSerializer):
|
|
|
|
""" Serializer for a SalesOrderLineItem object """
|
|
|
|
|
2020-04-20 22:57:13 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
|
|
part_detail = kwargs.pop('part_detail', False)
|
|
|
|
order_detail = kwargs.pop('order_detail', False)
|
2020-04-22 10:10:23 +00:00
|
|
|
allocations = kwargs.pop('allocations', False)
|
2020-04-20 22:57:13 +00:00
|
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
if part_detail is not True:
|
|
|
|
self.fields.pop('part_detail')
|
|
|
|
|
|
|
|
if order_detail is not True:
|
|
|
|
self.fields.pop('order_detail')
|
|
|
|
|
2020-04-22 10:10:23 +00:00
|
|
|
if allocations is not True:
|
|
|
|
self.fields.pop('allocations')
|
|
|
|
|
2020-04-20 22:57:13 +00:00
|
|
|
order_detail = SalesOrderSerializer(source='order', many=False, read_only=True)
|
|
|
|
part_detail = PartBriefSerializer(source='part', many=False, read_only=True)
|
2020-04-22 10:10:23 +00:00
|
|
|
allocations = SalesOrderAllocationSerializer(many=True, read_only=True)
|
2020-04-20 22:57:13 +00:00
|
|
|
|
2020-04-21 01:41:08 +00:00
|
|
|
quantity = serializers.FloatField()
|
|
|
|
allocated = serializers.FloatField(source='allocated_quantity', read_only=True)
|
2020-04-26 22:58:18 +00:00
|
|
|
fulfilled = serializers.FloatField(source='fulfilled_quantity', read_only=True)
|
2020-04-21 01:41:08 +00:00
|
|
|
|
2020-04-20 11:11:59 +00:00
|
|
|
class Meta:
|
|
|
|
model = SalesOrderLineItem
|
|
|
|
|
|
|
|
fields = [
|
|
|
|
'pk',
|
2020-04-21 01:41:08 +00:00
|
|
|
'allocated',
|
2020-04-22 10:10:23 +00:00
|
|
|
'allocations',
|
2020-04-20 11:11:59 +00:00
|
|
|
'quantity',
|
2020-04-26 22:58:18 +00:00
|
|
|
'fulfilled',
|
2020-04-20 11:11:59 +00:00
|
|
|
'reference',
|
|
|
|
'notes',
|
|
|
|
'order',
|
2020-04-20 22:57:13 +00:00
|
|
|
'order_detail',
|
|
|
|
'part',
|
|
|
|
'part_detail',
|
2020-04-20 11:11:59 +00:00
|
|
|
]
|
2020-05-11 13:41:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SOAttachmentSerializer(InvenTreeModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializers for the SalesOrderAttachment model
|
|
|
|
"""
|
|
|
|
|
2020-05-26 10:26:37 +00:00
|
|
|
attachment = InvenTreeAttachmentSerializerField(required=True)
|
2020-05-26 10:16:10 +00:00
|
|
|
|
2020-05-11 13:41:57 +00:00
|
|
|
class Meta:
|
|
|
|
model = SalesOrderAttachment
|
|
|
|
|
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'order',
|
|
|
|
'attachment',
|
|
|
|
'comment',
|
|
|
|
]
|