shafafiyat/contracts/views.py

109 lines
4.3 KiB
Python

from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required
from django.urls import reverse
from django.utils import timezone
from django.template import Template, Context
from processes.models import ProcessInstance, StepInstance
from common.consts import UserRoles
from .models import ContractTemplate, ContractInstance
from _helpers.utils import jalali_converter2
def build_contract_context(instance: ProcessInstance) -> dict:
representative = instance.representative
profile = getattr(representative, 'profile', None)
well = instance.well
return {
'customer_full_name': representative.get_full_name() if representative else '',
'national_code': profile.national_code if profile else '',
'address': profile.address if profile else '',
'phone': profile.phone_number_1 if profile else '',
'phone2': profile.phone_number_2 if profile else '',
'water_subscription_number': well.water_subscription_number if well else '',
'electricity_subscription_number': well.electricity_subscription_number if well else '',
'water_meter_serial_number': well.water_meter_serial_number if well else '',
'well_power': well.well_power if well else '',
'request_code': instance.code,
'today': jalali_converter2(timezone.now()),
}
@login_required
def contract_step(request, instance_id, step_id):
instance = get_object_or_404(ProcessInstance, id=instance_id)
# Resolve step navigation
step = get_object_or_404(instance.process.steps, id=step_id)
previous_step = instance.process.steps.filter(order__lt=step.order).last()
next_step = instance.process.steps.filter(order__gt=step.order).first()
# Access control:
# - INSTALLER: can open step but cannot view contract body (show inline message)
# - Others: can view
# - Only BROKER can submit/complete this step
profile = getattr(request.user, 'profile', None)
is_broker = False
can_view_contract_body = True
try:
is_broker = bool(profile and profile.has_role(UserRoles.BROKER))
if profile and profile.has_role(UserRoles.INSTALLER):
can_view_contract_body = False
except Exception:
pass
template_obj = ContractTemplate.objects.first()
if not template_obj:
return render(request, 'contracts/contract_missing.html', {'instance': instance})
ctx = build_contract_context(instance)
rendered = Template(template_obj.body).render(Context(ctx))
contract, _ = ContractInstance.objects.get_or_create(
process_instance=instance,
defaults={
'template': template_obj,
'rendered_body': rendered,
'created_by': request.user,
}
)
# keep latest rendering if template changed (optional)
contract.template = template_obj
contract.rendered_body = rendered
contract.save()
# If user submits to go next, only broker can complete and go to next
if request.method == 'POST':
if not is_broker:
from django.http import JsonResponse
return JsonResponse({'success': False, 'message': 'شما مجوز تایید این مرحله را ندارید'}, status=403)
StepInstance.objects.update_or_create(
process_instance=instance,
step=step,
defaults={'status': 'completed', 'completed_at': timezone.now()}
)
if next_step:
instance.current_step = next_step
instance.save()
return redirect('processes:step_detail', instance_id=instance.id, step_id=next_step.id)
return redirect('processes:request_list')
return render(request, 'contracts/contract_step.html', {
'instance': instance,
'step': step,
'contract': contract,
'template': template_obj,
'previous_step': previous_step,
'next_step': next_step,
'is_broker': is_broker,
'can_view_contract_body': can_view_contract_body,
})
@login_required
def contract_print(request, instance_id):
instance = get_object_or_404(ProcessInstance, id=instance_id)
contract = get_object_or_404(ContractInstance, process_instance=instance)
return render(request, 'contracts/contract_print.html', {
'instance': instance,
'contract': contract,
})