148 lines
4.1 KiB
Python
148 lines
4.1 KiB
Python
from django.db import transaction
|
|
from rest_framework import status, viewsets
|
|
from rest_framework.decorators import api_view, permission_classes
|
|
from rest_framework.response import Response
|
|
from rest_framework.views import APIView
|
|
|
|
from .models import (
|
|
Account,
|
|
CompanySettings,
|
|
Employee,
|
|
Invoice,
|
|
Party,
|
|
PartyAdjustment,
|
|
Payslip,
|
|
Product,
|
|
SalesType,
|
|
TreasuryTxn,
|
|
Voucher,
|
|
)
|
|
from .permissions import IsAdminAccount
|
|
from .seeding import seed_default_data
|
|
from .serializers import (
|
|
AccountSerializer,
|
|
CompanySettingsSerializer,
|
|
EmployeeSerializer,
|
|
InvoiceSerializer,
|
|
PartyAdjustmentSerializer,
|
|
PartySerializer,
|
|
PayslipSerializer,
|
|
ProductSerializer,
|
|
SalesTypeSerializer,
|
|
TreasuryTxnSerializer,
|
|
VoucherSerializer,
|
|
)
|
|
|
|
|
|
class _AdminModelViewSet(viewsets.ModelViewSet):
|
|
"""Base viewset restricting access to admin accounts."""
|
|
|
|
permission_classes = [IsAdminAccount]
|
|
|
|
|
|
class AccountViewSet(_AdminModelViewSet):
|
|
queryset = Account.objects.all()
|
|
serializer_class = AccountSerializer
|
|
pagination_class = None
|
|
|
|
|
|
class PartyViewSet(_AdminModelViewSet):
|
|
queryset = Party.objects.all()
|
|
serializer_class = PartySerializer
|
|
pagination_class = None
|
|
|
|
|
|
class ProductViewSet(_AdminModelViewSet):
|
|
queryset = Product.objects.all()
|
|
serializer_class = ProductSerializer
|
|
pagination_class = None
|
|
|
|
|
|
class VoucherViewSet(_AdminModelViewSet):
|
|
queryset = Voucher.objects.prefetch_related('lines').all()
|
|
serializer_class = VoucherSerializer
|
|
pagination_class = None
|
|
|
|
|
|
class InvoiceViewSet(_AdminModelViewSet):
|
|
queryset = Invoice.objects.prefetch_related('lines').all()
|
|
serializer_class = InvoiceSerializer
|
|
pagination_class = None
|
|
|
|
|
|
class SalesTypeViewSet(_AdminModelViewSet):
|
|
queryset = SalesType.objects.all()
|
|
serializer_class = SalesTypeSerializer
|
|
pagination_class = None
|
|
|
|
|
|
class PartyAdjustmentViewSet(_AdminModelViewSet):
|
|
queryset = PartyAdjustment.objects.all()
|
|
serializer_class = PartyAdjustmentSerializer
|
|
pagination_class = None
|
|
|
|
|
|
class TreasuryTxnViewSet(_AdminModelViewSet):
|
|
queryset = TreasuryTxn.objects.all()
|
|
serializer_class = TreasuryTxnSerializer
|
|
pagination_class = None
|
|
|
|
|
|
class EmployeeViewSet(_AdminModelViewSet):
|
|
queryset = Employee.objects.all()
|
|
serializer_class = EmployeeSerializer
|
|
pagination_class = None
|
|
|
|
|
|
class PayslipViewSet(_AdminModelViewSet):
|
|
queryset = Payslip.objects.all()
|
|
serializer_class = PayslipSerializer
|
|
pagination_class = None
|
|
|
|
|
|
class CompanySettingsView(APIView):
|
|
"""Read/update the singleton company settings."""
|
|
|
|
permission_classes = [IsAdminAccount]
|
|
|
|
def _get_instance(self):
|
|
instance, _created = CompanySettings.objects.get_or_create(id=1)
|
|
if instance.bank_account_id is None and Account.objects.exists():
|
|
# Backfill the mapping for an empty/legacy settings row.
|
|
seed_default_data(Account, CompanySettings)
|
|
instance.refresh_from_db()
|
|
return instance
|
|
|
|
def get(self, request):
|
|
serializer = CompanySettingsSerializer(self._get_instance())
|
|
return Response(serializer.data)
|
|
|
|
def patch(self, request):
|
|
serializer = CompanySettingsSerializer(
|
|
self._get_instance(), data=request.data, partial=True
|
|
)
|
|
serializer.is_valid(raise_exception=True)
|
|
serializer.save()
|
|
return Response(serializer.data)
|
|
|
|
|
|
@api_view(['POST'])
|
|
@permission_classes([IsAdminAccount])
|
|
@transaction.atomic
|
|
def reset_accounting_data(request):
|
|
"""Wipes all accounting data and recreates a clean, zeroed starting point."""
|
|
Voucher.objects.all().delete()
|
|
Invoice.objects.all().delete()
|
|
PartyAdjustment.objects.all().delete()
|
|
SalesType.objects.all().delete()
|
|
TreasuryTxn.objects.all().delete()
|
|
Payslip.objects.all().delete()
|
|
Employee.objects.all().delete()
|
|
Product.objects.all().delete()
|
|
Party.objects.all().delete()
|
|
CompanySettings.objects.all().delete()
|
|
Account.objects.all().delete()
|
|
|
|
seed_default_data(Account, CompanySettings)
|
|
return Response({'message': 'اطلاعات حسابداری بازنشانی شد.'}, status=status.HTTP_200_OK)
|