Initial commit: standalone Django accounting API with admin JWT authorization
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
129
accounting/views.py
Normal file
129
accounting/views.py
Normal file
@@ -0,0 +1,129 @@
|
||||
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,
|
||||
Payslip,
|
||||
Product,
|
||||
TreasuryTxn,
|
||||
Voucher,
|
||||
)
|
||||
from .permissions import IsAdminAccount
|
||||
from .seeding import seed_default_data
|
||||
from .serializers import (
|
||||
AccountSerializer,
|
||||
CompanySettingsSerializer,
|
||||
EmployeeSerializer,
|
||||
InvoiceSerializer,
|
||||
PartySerializer,
|
||||
PayslipSerializer,
|
||||
ProductSerializer,
|
||||
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 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()
|
||||
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)
|
||||
Reference in New Issue
Block a user