fix final invoice and certificatre

This commit is contained in:
aminhashemi92 2025-10-04 12:01:37 +03:30
parent 0cfa86bde3
commit dd37ac3720
11 changed files with 120 additions and 66 deletions

View file

@ -942,8 +942,8 @@ def final_settlement_step(request, instance_id, step_id):
# Build approver statuses for template (include reason to display in UI)
reqs = list(step.approver_requirements.select_related('role').all())
approvals = list(step_instance.approvals.select_related('role').all())
rejections = list(step_instance.rejections.select_related('role').all())
approvals = list(step_instance.approvals.select_related('role', 'approved_by').filter(is_deleted=False))
rejections = list(step_instance.rejections.select_related('role', 'rejected_by').filter(is_deleted=False))
approvals_by_role = {a.role_id: a for a in approvals}
rejections_by_role = {r.role_id: r for r in rejections}
approver_statuses = []
@ -978,8 +978,8 @@ def final_settlement_step(request, instance_id, step_id):
# Compute whether current user has already decided (approved/rejected)
current_user_has_decided = False
try:
user_has_approval = step_instance.approvals.filter(approved_by=request.user).exists()
user_has_rejection = step_instance.rejections.filter(rejected_by=request.user).exists()
user_has_approval = step_instance.approvals.filter(approved_by=request.user, is_deleted=False).exists()
user_has_rejection = step_instance.rejections.filter(rejected_by=request.user, is_deleted=False).exists()
current_user_has_decided = bool(user_has_approval or user_has_rejection)
except Exception:
current_user_has_decided = False
@ -997,8 +997,8 @@ def final_settlement_step(request, instance_id, step_id):
if action == 'approve':
# enforce zero remaining
invoice.calculate_totals()
if invoice.remaining_amount != 0:
messages.error(request, f"تا زمانی که مانده فاکتور صفر نشده امکان تایید نیست (مانده فعلی: {invoice.remaining_amount})")
if invoice.get_remaining_amount() != 0:
messages.error(request, f"تا زمانی که مانده فاکتور صفر نشده امکان تایید نیست (مانده فعلی: {invoice.get_remaining_amount()})")
return redirect('invoices:final_settlement_step', instance_id=instance.id, step_id=step.id)
StepApproval.objects.create(
step_instance=step_instance,
@ -1203,8 +1203,8 @@ def add_final_payment(request, instance_id, step_id):
'redirect': reverse('invoices:final_settlement_step', args=[instance.id, step_id]),
'totals': {
'final_amount': str(invoice.final_amount),
'paid_amount': str(invoice.paid_amount),
'remaining_amount': str(invoice.remaining_amount),
'paid_amount': str(invoice.get_paid_amount()),
'remaining_amount': str(invoice.get_remaining_amount()),
}
})
@ -1216,14 +1216,17 @@ def delete_final_payment(request, instance_id, step_id, payment_id):
step = get_object_or_404(instance.process.steps, id=step_id)
invoice = get_object_or_404(Invoice, process_instance=instance)
payment = get_object_or_404(Payment, id=payment_id, invoice=invoice)
# Only BROKER can delete final settlement payments
try:
if not (hasattr(request.user, 'profile') and request.user.profile.has_role(UserRoles.BROKER)):
return JsonResponse({'success': False, 'message': 'شما مجوز حذف تراکنش تسویه را ندارید'}, status=403)
except Exception:
return JsonResponse({'success': False, 'message': 'شما مجوز حذف تراکنش تسویه را ندارید'}, status=403)
# Delete payment and recalculate invoice totals
payment.hard_delete()
invoice.refresh_from_db()
invoice.calculate_totals() # This is what was missing!
# On delete, return to awaiting approval
try:
@ -1231,16 +1234,11 @@ def delete_final_payment(request, instance_id, step_id, payment_id):
si.status = 'in_progress'
si.completed_at = None
si.save()
try:
for appr in list(si.approvals.all()):
appr.delete()
except Exception:
pass
try:
for rej in list(si.rejections.all()):
rej.delete()
except Exception:
pass
# Clear approvals and rejections (like in quote_payment)
for appr in list(si.approvals.all()):
appr.delete()
for rej in list(si.rejections.all()):
rej.delete()
except Exception:
pass
@ -1274,7 +1272,7 @@ def delete_final_payment(request, instance_id, step_id, payment_id):
return JsonResponse({'success': True, 'redirect': reverse('invoices:final_settlement_step', args=[instance.id, step_id]), 'totals': {
'final_amount': str(invoice.final_amount),
'paid_amount': str(invoice.paid_amount),
'remaining_amount': str(invoice.remaining_amount),
'paid_amount': str(invoice.get_paid_amount()),
'remaining_amount': str(invoice.get_remaining_amount()),
}})