2021-06-14 19:45:36 +00:00
|
|
|
# Copyright (C) 2018-2021 by BDE ENS Paris-Saclay
|
2020-03-22 15:04:09 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2021-09-05 22:47:11 +00:00
|
|
|
from django.db import transaction
|
2020-03-22 15:04:09 +00:00
|
|
|
from rest_framework import serializers
|
2020-03-24 16:06:50 +00:00
|
|
|
from note.api.serializers import SpecialTransactionSerializer
|
2020-03-22 15:04:09 +00:00
|
|
|
|
2020-04-22 01:26:45 +00:00
|
|
|
from ..models import Invoice, Product, RemittanceType, Remittance, SogeCredit
|
2020-03-22 15:04:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProductSerializer(serializers.ModelSerializer):
|
|
|
|
"""
|
|
|
|
REST API Serializer for Product types.
|
|
|
|
The djangorestframework plugin will analyse the model `Product` and parse all fields in the API.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Product
|
|
|
|
fields = '__all__'
|
|
|
|
|
|
|
|
|
|
|
|
class InvoiceSerializer(serializers.ModelSerializer):
|
|
|
|
"""
|
|
|
|
REST API Serializer for Invoice types.
|
|
|
|
The djangorestframework plugin will analyse the model `Invoice` and parse all fields in the API.
|
|
|
|
"""
|
|
|
|
class Meta:
|
|
|
|
model = Invoice
|
|
|
|
fields = '__all__'
|
|
|
|
read_only_fields = ('bde',)
|
|
|
|
|
|
|
|
products = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
def get_products(self, obj):
|
|
|
|
return serializers.ListSerializer(child=ProductSerializer())\
|
|
|
|
.to_representation(Product.objects.filter(invoice=obj).all())
|
2020-03-24 16:06:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RemittanceTypeSerializer(serializers.ModelSerializer):
|
|
|
|
"""
|
|
|
|
REST API Serializer for RemittanceType types.
|
|
|
|
The djangorestframework plugin will analyse the model `RemittanceType` and parse all fields in the API.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = RemittanceType
|
|
|
|
fields = '__all__'
|
|
|
|
|
|
|
|
|
|
|
|
class RemittanceSerializer(serializers.ModelSerializer):
|
|
|
|
"""
|
|
|
|
REST API Serializer for Remittance types.
|
|
|
|
The djangorestframework plugin will analyse the model `Remittance` and parse all fields in the API.
|
|
|
|
"""
|
|
|
|
|
|
|
|
transactions = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Remittance
|
|
|
|
fields = '__all__'
|
|
|
|
|
|
|
|
def get_transactions(self, obj):
|
|
|
|
return serializers.ListSerializer(child=SpecialTransactionSerializer()).to_representation(obj.transactions)
|
2020-04-22 01:26:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SogeCreditSerializer(serializers.ModelSerializer):
|
|
|
|
"""
|
|
|
|
REST API Serializer for SogeCredit types.
|
|
|
|
The djangorestframework plugin will analyse the model `SogeCredit` and parse all fields in the API.
|
|
|
|
"""
|
|
|
|
|
2021-09-05 22:47:11 +00:00
|
|
|
@transaction.atomic
|
|
|
|
def save(self, **kwargs):
|
|
|
|
# Update soge transactions after creating a credit
|
|
|
|
instance = super().save(**kwargs)
|
|
|
|
instance.update_transactions()
|
|
|
|
instance.save()
|
|
|
|
return instance
|
|
|
|
|
2020-04-22 01:26:45 +00:00
|
|
|
class Meta:
|
|
|
|
model = SogeCredit
|
|
|
|
fields = '__all__'
|