1
0
forked from erp-dev/erp
Files
erpnew/api_v1/views/business/balance/views.py
2025-11-30 23:04:21 +08:00

37 lines
1.2 KiB
Python

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),
}
)