clean up proccess and req_list app.
This commit is contained in:
parent
35799b7754
commit
6f3ce51ab9
26 changed files with 287 additions and 744 deletions
|
@ -1,7 +1,6 @@
|
|||
from django.shortcuts import render, get_object_or_404, redirect
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
import json
|
||||
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib import messages
|
||||
from django.http import JsonResponse
|
||||
|
@ -11,41 +10,32 @@ from django.contrib.auth import get_user_model
|
|||
from .models import Process, ProcessInstance, StepInstance
|
||||
from wells.models import Well
|
||||
from accounts.models import Profile
|
||||
from .forms import ProcessInstanceForm
|
||||
from accounts.forms import CustomerForm
|
||||
from wells.forms import WellForm
|
||||
from wells.models import WaterMeterManufacturer
|
||||
|
||||
|
||||
@login_required
|
||||
def process_list(request):
|
||||
"""نمایش لیست فرآیندهای فعال"""
|
||||
processes = Process.objects.filter(is_active=True)
|
||||
return render(request, 'processes/process_list.html', {
|
||||
'processes': processes
|
||||
})
|
||||
|
||||
@login_required
|
||||
def process_detail(request, process_id):
|
||||
"""نمایش جزئیات فرآیند"""
|
||||
process = get_object_or_404(Process, id=process_id, is_active=True)
|
||||
return render(request, 'processes/process_detail.html', {
|
||||
'process': process
|
||||
})
|
||||
|
||||
|
||||
@login_required
|
||||
def request_list(request):
|
||||
"""نمایش لیست درخواستها با جدول و مدال ایجاد"""
|
||||
instances = ProcessInstance.objects.select_related('well', 'representative', 'requester').filter(is_deleted=False).order_by('-created')
|
||||
processes = Process.objects.filter(is_active=True)
|
||||
manufacturers = WaterMeterManufacturer.objects.all().order_by('name')
|
||||
# Summary stats for header cards
|
||||
total_count = instances.count()
|
||||
completed_count = instances.filter(status='completed').count()
|
||||
in_progress_count = instances.filter(status='in_progress').count()
|
||||
pending_count = instances.filter(status='pending').count()
|
||||
return render(request, 'processes/request_list.html', {
|
||||
'instances': instances,
|
||||
'customer_form': CustomerForm(),
|
||||
'well_form': WellForm(),
|
||||
'processes': processes,
|
||||
'manufacturers': manufacturers
|
||||
'manufacturers': manufacturers,
|
||||
'total_count': total_count,
|
||||
'completed_count': completed_count,
|
||||
'in_progress_count': in_progress_count,
|
||||
'pending_count': pending_count,
|
||||
})
|
||||
|
||||
|
||||
|
@ -127,23 +117,12 @@ def create_request_with_entities(request):
|
|||
well_id = request.POST.get('well_id') # optional if existing
|
||||
# Representative fields
|
||||
representative_id = request.POST.get('representative_id')
|
||||
# Prefer plain CustomerForm keys; fallback to representative_* keys
|
||||
representative_national_code = request.POST.get('national_code') or request.POST.get('representative_national_code')
|
||||
representative_first_name = request.POST.get('first_name') or request.POST.get('representative_first_name')
|
||||
representative_last_name = request.POST.get('last_name') or request.POST.get('representative_last_name')
|
||||
representative_username = request.POST.get('username') or request.POST.get('representative_username')
|
||||
representative_phone_number_1 = request.POST.get('phone_number_1') or request.POST.get('representative_phone_number_1')
|
||||
representative_phone_number_2 = request.POST.get('phone_number_2') or request.POST.get('representative_phone_number_2')
|
||||
representative_card_number = request.POST.get('card_number') or request.POST.get('representative_card_number')
|
||||
representative_account_number = request.POST.get('account_number') or request.POST.get('representative_account_number')
|
||||
representative_bank_name = request.POST.get('bank_name') or request.POST.get('representative_bank_name')
|
||||
representative_address = request.POST.get('address') or request.POST.get('representative_address')
|
||||
|
||||
if not process_id:
|
||||
return JsonResponse({'ok': False, 'errors': {'request': {'process': ['فرآیند الزامی است']}}}, status=400)
|
||||
if not water_subscription_number:
|
||||
return JsonResponse({'ok': False, 'errors': {'well': {'water_subscription_number': ['شماره اشتراک آب الزامی است']}}}, status=400)
|
||||
if not representative_id and not representative_national_code:
|
||||
if not representative_id and not request.POST.get('national_code'):
|
||||
return JsonResponse({'ok': False, 'errors': {'customer': {'national_code': ['کد ملی نماینده را وارد کنید یا دکمه بررسی/افزودن نماینده را بزنید']}}}, status=400)
|
||||
|
||||
representative_user = None
|
||||
|
@ -152,52 +131,20 @@ def create_request_with_entities(request):
|
|||
representative_profile = Profile.objects.select_related('user').filter(user_id=representative_id).first()
|
||||
if not representative_profile:
|
||||
return JsonResponse({'ok': False, 'errors': {'customer': {'__all__': ['نماینده انتخابشده یافت نشد']}}}, status=400)
|
||||
# Use CustomerForm with request.POST data, merging with existing values
|
||||
customer_form = CustomerForm(request.POST, instance=representative_profile)
|
||||
customer_form.request = request
|
||||
if not customer_form.is_valid():
|
||||
return JsonResponse({'ok': False, 'errors': {'customer': customer_form.errors}}, status=400)
|
||||
representative_profile = customer_form.save()
|
||||
representative_user = representative_profile.user
|
||||
# Optionally update if fields provided
|
||||
changed = False
|
||||
if representative_first_name:
|
||||
representative_user.first_name = representative_first_name
|
||||
changed = True
|
||||
if representative_last_name:
|
||||
representative_user.last_name = representative_last_name
|
||||
changed = True
|
||||
if representative_username:
|
||||
representative_user.username = representative_username
|
||||
changed = True
|
||||
if changed:
|
||||
representative_user.save()
|
||||
if representative_national_code:
|
||||
representative_profile.national_code = representative_national_code
|
||||
if representative_phone_number_1 is not None:
|
||||
representative_profile.phone_number_1 = representative_phone_number_1
|
||||
if representative_phone_number_2 is not None:
|
||||
representative_profile.phone_number_2 = representative_phone_number_2
|
||||
if representative_card_number is not None:
|
||||
representative_profile.card_number = representative_card_number
|
||||
if representative_account_number is not None:
|
||||
representative_profile.account_number = representative_account_number
|
||||
if representative_bank_name is not None:
|
||||
representative_profile.bank_name = representative_bank_name
|
||||
if representative_address is not None:
|
||||
representative_profile.address = representative_address
|
||||
representative_profile.save()
|
||||
else:
|
||||
# Use CustomerForm to validate/create/update representative profile by national code
|
||||
profile_instance = None
|
||||
if representative_national_code:
|
||||
profile_instance = Profile.objects.filter(national_code=representative_national_code).first()
|
||||
customer_data = {
|
||||
'first_name': representative_first_name or '',
|
||||
'last_name': representative_last_name or '',
|
||||
'phone_number_1': representative_phone_number_1 or '',
|
||||
'phone_number_2': representative_phone_number_2 or '',
|
||||
'national_code': representative_national_code or '',
|
||||
'address': representative_address or '',
|
||||
'card_number': representative_card_number or '',
|
||||
'account_number': representative_account_number or '',
|
||||
'bank_name': representative_bank_name or '',
|
||||
}
|
||||
customer_form = CustomerForm(customer_data, instance=profile_instance)
|
||||
national_code = request.POST.get('national_code')
|
||||
if national_code:
|
||||
profile_instance = Profile.objects.filter(national_code=national_code).first()
|
||||
customer_form = CustomerForm(request.POST, instance=profile_instance)
|
||||
customer_form.request = request
|
||||
if not customer_form.is_valid():
|
||||
return JsonResponse({'ok': False, 'errors': {'customer': customer_form.errors}}, status=400)
|
||||
|
@ -292,62 +239,24 @@ def create_request_with_entities(request):
|
|||
redirect_url = reverse('processes:instance_steps', args=[instance.id])
|
||||
return JsonResponse({'ok': True, 'instance_id': instance.id, 'redirect': redirect_url})
|
||||
|
||||
|
||||
@require_POST
|
||||
@login_required
|
||||
def delete_request(request, instance_id):
|
||||
"""حذف درخواست"""
|
||||
instance = get_object_or_404(ProcessInstance, id=instance_id)
|
||||
code = instance.code
|
||||
if instance.status == 'completed':
|
||||
return JsonResponse({
|
||||
'success': False,
|
||||
'message': 'درخواست تکمیل شده نمیتواند حذف شود'
|
||||
})
|
||||
instance.delete()
|
||||
return JsonResponse({
|
||||
'success': True,
|
||||
'message': f'درخواست {code} با موفقیت حذف شد'
|
||||
})
|
||||
|
||||
@login_required
|
||||
def start_process(request, process_id):
|
||||
"""شروع فرآیند جدید"""
|
||||
process = get_object_or_404(Process, id=process_id, is_active=True)
|
||||
|
||||
if request.method == 'POST':
|
||||
form = ProcessInstanceForm(request.POST)
|
||||
if form.is_valid():
|
||||
instance = form.save(commit=False)
|
||||
instance.process = process
|
||||
instance.requester = request.user
|
||||
instance.save()
|
||||
|
||||
# ایجاد نمونههای مرحله
|
||||
for step in process.steps.all():
|
||||
StepInstance.objects.create(
|
||||
process_instance=instance,
|
||||
step=step
|
||||
)
|
||||
|
||||
# تنظیم مرحله اول به عنوان مرحله فعلی
|
||||
first_step = process.steps.first()
|
||||
if first_step:
|
||||
instance.current_step = first_step
|
||||
instance.status = 'in_progress'
|
||||
instance.save()
|
||||
|
||||
messages.success(request, f'فرآیند {process.name} با موفقیت شروع شد.')
|
||||
return redirect('processes:instance_detail', instance_id=instance.id)
|
||||
else:
|
||||
form = ProcessInstanceForm()
|
||||
|
||||
return render(request, 'processes/start_process.html', {
|
||||
'process': process,
|
||||
'form': form
|
||||
})
|
||||
|
||||
@login_required
|
||||
def instance_detail(request, instance_id):
|
||||
"""نمایش جزئیات نمونه فرآیند"""
|
||||
instance = get_object_or_404(ProcessInstance, id=instance_id)
|
||||
return render(request, 'processes/instance_detail.html', {
|
||||
'instance': instance
|
||||
})
|
||||
|
||||
@login_required
|
||||
def step_detail(request, instance_id, step_id):
|
||||
|
@ -409,6 +318,7 @@ def step_detail(request, instance_id, step_id):
|
|||
'next_step': next_step,
|
||||
})
|
||||
|
||||
|
||||
@login_required
|
||||
def instance_steps(request, instance_id):
|
||||
"""هدایت به مرحله فعلی instance"""
|
||||
|
@ -430,6 +340,7 @@ def instance_steps(request, instance_id):
|
|||
return redirect('processes:instance_summary', instance_id=instance.id)
|
||||
return redirect('processes:step_detail', instance_id=instance.id, step_id=instance.current_step.id)
|
||||
|
||||
|
||||
@login_required
|
||||
def instance_summary(request, instance_id):
|
||||
"""نمای خلاصهٔ فقطخواندنی برای درخواستهای تکمیلشده."""
|
||||
|
@ -461,15 +372,4 @@ def instance_summary(request, instance_id):
|
|||
'latest_report': latest_report,
|
||||
'certificate': certificate,
|
||||
})
|
||||
|
||||
@login_required
|
||||
def my_processes(request):
|
||||
"""نمایش فرآیندهای کاربر"""
|
||||
my_instances = ProcessInstance.objects.filter(requester=request.user)
|
||||
assigned_steps = StepInstance.objects.filter(assigned_to=request.user, status='in_progress')
|
||||
|
||||
return render(request, 'processes/my_processes.html', {
|
||||
'my_instances': my_instances,
|
||||
'assigned_steps': assigned_steps
|
||||
})
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue