from decimal import Decimal from rest_framework import views from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from basic_info import models as basic_models from business import services as business_services from api_v1.views.stock_change_views.mixins import StockChangeViewMixin class CustomerBalanceView(StockChangeViewMixin, views.APIView): permission_classes = [IsAuthenticated] def get(self, request, customer_id: int): if not self.check_employee_permission(request): return self.permission_error_response('无权限访问') merchant = request.user.employee.merchant try: customer = basic_models.Customer.objects.get(id=customer_id, merchant=merchant) except basic_models.Customer.DoesNotExist: return self.not_found_response('客户不存在') balance: Decimal = business_services.BalanceService.get_customer_balance( merchant=merchant, customer=customer, ) return Response( { 'customer': customer.id, 'customer_name': customer.name, 'balance': str(balance), } ) class SupplierBalanceView(StockChangeViewMixin, views.APIView): permission_classes = [IsAuthenticated] def get(self, request, supplier_id: int): if not self.check_employee_permission(request): return self.permission_error_response('无权限访问') merchant = request.user.employee.merchant try: supplier = basic_models.Supplier.objects.get(id=supplier_id, merchant=merchant) except basic_models.Supplier.DoesNotExist: return self.not_found_response('供应商不存在') balance: Decimal = business_services.BalanceService.get_supplier_balance( merchant=merchant, supplier=supplier, ) return Response( { 'supplier': supplier.id, 'supplier_name': supplier.name, 'balance': str(balance), } )