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 .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() 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, mark this step completed and go to next if request.method == 'POST': 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, }) @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, })