from app.models import * from rest_framework.decorators import api_view from rest_framework.response import Response from django.db import transaction from django.db.models import F decimal = 0 #========================================================================================== def getcode(code, Model): try: obj = Model.objects.latest('id') val = 1 if obj == None else obj.id + 1 except Exception as e: val = 1 length = len(str(val)) text = '' for i in range(0, 6-length): text += '0' return '{}{}{}'.format(code, text, val) #========================================================================================== def account_entry_api(code, amount, content, type, category, userid, ref=None, product=None, customer=None): try: user = User.objects.get(id=userid) entry_type = Entry_Type.objects.get(code=type) entry_category = Entry_Category.objects.get(id=category) row = System_Setting.objects.filter(category='system', classify='current', code='date').first() if not row: return {'error': 'Không tìm thấy cài đặt ngày hệ thống.'} system_date = row.detail with transaction.atomic(): account = Internal_Account.objects.select_for_update().get(code=code) start_balance = account.balance or 0 if entry_type.code == 'DR' and start_balance < amount: return {'error': 'Số dư không đủ để thực hiện giao dịch.'} if entry_type.code == 'CR': # account.balance = F('balance') + amount account.balance += amount else: account.balance -= amount account.save() account.refresh_from_db() new_balance = account.balance entry = Internal_Entry.objects.create( category=entry_category, content=content, amount=amount, inputer=user, approver=user, type=entry_type, balance_before=start_balance, balance_after=new_balance, account=account, date=system_date, ref=ref, product=None if product==None else Product.objects.get(id=product), customer=None if customer==None else Customer.objects.get(id=customer) ) text = 'id,account__currency__code,ref,balance_before,balance_after,code,account,account__code,account__branch__name,account__type__name,date,amount,content,inputer,inputer__fullname,approver,approver__fullname,create_time,update_time,type,type__code,type__name' fields = text.split(',') return Internal_Entry.objects.filter(id=entry.id).values(*fields).first() except User.DoesNotExist: return {'error': f"Người dùng với ID {userid} không tồn tại."} except Internal_Account.DoesNotExist: return {'error': f"Tài khoản nội bộ với mã '{code}' không tồn tại."} except Entry_Type.DoesNotExist: return {'error': f"Loại bút toán với mã '{type}' không tồn tại."} except Entry_Category.DoesNotExist: return {'error': f"Danh mục bút toán với ID '{category}' không tồn tại."} except Exception as e: return {'error': f"Đã xảy ra lỗi không mong muốn: {str(e)}"} #========================================================================================== @api_view(['POST']) def account_entry(request): ref = request.data.get('ref') data = account_entry_api( code=request.data['code'], amount=request.data['amount'], content=request.data['content'], type=request.data['type'], category=request.data['category'], userid=request.data['user'], ref=ref, product=request.data['product'], customer=request.data['customer'] ) if 'error' in data: return Response(data, status=400) return Response(data)