huge fix
This commit is contained in:
parent
810c87e2e0
commit
b5bf3a5dbe
51 changed files with 2397 additions and 326 deletions
|
|
@ -2,7 +2,7 @@ from django import forms
|
|||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.forms import UserCreationForm
|
||||
from .models import Profile, Role
|
||||
from common.consts import UserRoles
|
||||
from common.consts import UserRoles, USER_TYPE_CHOICES
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
|
@ -28,10 +28,15 @@ class CustomerForm(forms.ModelForm):
|
|||
class Meta:
|
||||
model = Profile
|
||||
fields = [
|
||||
'phone_number_1', 'phone_number_2', 'national_code',
|
||||
'user_type', 'phone_number_1', 'phone_number_2', 'national_code',
|
||||
'company_name', 'company_national_id',
|
||||
'address', 'card_number', 'account_number', 'bank_name'
|
||||
]
|
||||
widgets = {
|
||||
'user_type': forms.Select(attrs={
|
||||
'class': 'form-control',
|
||||
'id': 'user-type-select'
|
||||
}),
|
||||
'phone_number_1': forms.TextInput(attrs={
|
||||
'class': 'form-control',
|
||||
'placeholder': '09123456789'
|
||||
|
|
@ -46,6 +51,15 @@ class CustomerForm(forms.ModelForm):
|
|||
'maxlength': '10',
|
||||
'required': 'required'
|
||||
}),
|
||||
'company_name': forms.TextInput(attrs={
|
||||
'class': 'form-control company-field',
|
||||
'placeholder': 'نام شرکت'
|
||||
}),
|
||||
'company_national_id': forms.TextInput(attrs={
|
||||
'class': 'form-control company-field',
|
||||
'placeholder': 'شناسه ملی شرکت',
|
||||
'maxlength': '11'
|
||||
}),
|
||||
'address': forms.Textarea(attrs={
|
||||
'class': 'form-control',
|
||||
'placeholder': 'آدرس کامل',
|
||||
|
|
@ -67,9 +81,12 @@ class CustomerForm(forms.ModelForm):
|
|||
}),
|
||||
}
|
||||
labels = {
|
||||
'user_type': 'نوع کاربر',
|
||||
'phone_number_1': 'تلفن ۱',
|
||||
'phone_number_2': 'تلفن ۲',
|
||||
'national_code': 'کد ملی',
|
||||
'company_name': 'نام شرکت',
|
||||
'company_national_id': 'شناسه ملی شرکت',
|
||||
'address': 'آدرس',
|
||||
'card_number': 'شماره کارت',
|
||||
'account_number': 'شماره حساب',
|
||||
|
|
@ -89,6 +106,21 @@ class CustomerForm(forms.ModelForm):
|
|||
raise forms.ValidationError('این کد ملی قبلاً استفاده شده است.')
|
||||
return national_code
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super().clean()
|
||||
user_type = cleaned_data.get('user_type')
|
||||
company_name = cleaned_data.get('company_name')
|
||||
company_national_id = cleaned_data.get('company_national_id')
|
||||
|
||||
# If user type is legal, company fields are required
|
||||
if user_type == 'legal':
|
||||
if not company_name:
|
||||
self.add_error('company_name', 'برای کاربران حقوقی نام شرکت الزامی است.')
|
||||
if not company_national_id:
|
||||
self.add_error('company_national_id', 'برای کاربران حقوقی شناسه ملی شرکت الزامی است.')
|
||||
|
||||
return cleaned_data
|
||||
|
||||
def save(self, commit=True):
|
||||
def _compute_completed(cleaned):
|
||||
try:
|
||||
|
|
@ -100,7 +132,15 @@ class CustomerForm(forms.ModelForm):
|
|||
bank_ok = bool(cleaned.get('bank_name'))
|
||||
card_ok = bool((cleaned.get('card_number') or '').strip())
|
||||
acc_ok = bool((cleaned.get('account_number') or '').strip())
|
||||
return all([first_ok, last_ok, nc_ok, phone_ok, addr_ok, bank_ok, card_ok, acc_ok])
|
||||
|
||||
# Check user type specific requirements
|
||||
user_type = cleaned.get('user_type', 'individual')
|
||||
if user_type == 'legal':
|
||||
company_name_ok = bool((cleaned.get('company_name') or '').strip())
|
||||
company_id_ok = bool((cleaned.get('company_national_id') or '').strip())
|
||||
return all([first_ok, last_ok, nc_ok, phone_ok, addr_ok, bank_ok, card_ok, acc_ok, company_name_ok, company_id_ok])
|
||||
else:
|
||||
return all([first_ok, last_ok, nc_ok, phone_ok, addr_ok, bank_ok, card_ok, acc_ok])
|
||||
except Exception:
|
||||
return False
|
||||
# Check if this is an update (instance exists)
|
||||
|
|
@ -125,9 +165,12 @@ class CustomerForm(forms.ModelForm):
|
|||
profile.is_completed = _compute_completed({
|
||||
'first_name': user.first_name,
|
||||
'last_name': user.last_name,
|
||||
'user_type': self.cleaned_data.get('user_type'),
|
||||
'national_code': self.cleaned_data.get('national_code'),
|
||||
'phone_number_1': self.cleaned_data.get('phone_number_1'),
|
||||
'phone_number_2': self.cleaned_data.get('phone_number_2'),
|
||||
'company_name': self.cleaned_data.get('company_name'),
|
||||
'company_national_id': self.cleaned_data.get('company_national_id'),
|
||||
'address': self.cleaned_data.get('address'),
|
||||
'bank_name': self.cleaned_data.get('bank_name'),
|
||||
'card_number': self.cleaned_data.get('card_number'),
|
||||
|
|
@ -171,9 +214,12 @@ class CustomerForm(forms.ModelForm):
|
|||
profile.is_completed = _compute_completed({
|
||||
'first_name': user.first_name,
|
||||
'last_name': user.last_name,
|
||||
'user_type': self.cleaned_data.get('user_type'),
|
||||
'national_code': self.cleaned_data.get('national_code'),
|
||||
'phone_number_1': self.cleaned_data.get('phone_number_1'),
|
||||
'phone_number_2': self.cleaned_data.get('phone_number_2'),
|
||||
'company_name': self.cleaned_data.get('company_name'),
|
||||
'company_national_id': self.cleaned_data.get('company_national_id'),
|
||||
'address': self.cleaned_data.get('address'),
|
||||
'bank_name': self.cleaned_data.get('bank_name'),
|
||||
'card_number': self.cleaned_data.get('card_number'),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue