add product
This commit is contained in:
1 parent
423d4cf40e
commit
c8d45dd07c
58 files changed
+8309
-674
No files matched your search
+78
-52
@@ -10,7 +10,9 @@ from apps.cuser.models import CuserRole as CuserRoleModel
|
||||
from rest_framework import exceptions
|
||||
from utils.get_user_workspace import get_user_workspace, get_user_workspace_uuid
|
||||
from utils.get_cuser_role import get_cuser_role
|
||||
from django.db import transaction
|
||||
from django.db import transaction, IntegrityError
|
||||
from django.core.exceptions import ValidationError as DjangoValidationError
|
||||
from utils.validations.mobile import normalize_mobile
|
||||
from django.contrib.auth import get_user_model
|
||||
import uuid
|
||||
import random
|
||||
@@ -20,11 +22,20 @@ CUser = get_user_model()
|
||||
|
||||
@transaction.atomic
|
||||
def customer_panel_access(request, mobile: int, customer_id: int, status: bool) -> CuserRoleModel:
|
||||
|
||||
try:
|
||||
workspace = get_user_workspace(request)
|
||||
customer = CustomerModel.objects.get(id=customer_id, sys_ws=workspace)
|
||||
|
||||
mobile = normalize_mobile(mobile)
|
||||
if customer.mobile != mobile:
|
||||
raise exceptions.ValidationError({'mobile': 'شماره موبایل باید با شماره مشتری یکسان باشد.'})
|
||||
# Disabling access must not create an unrelated account or a new role.
|
||||
if not status:
|
||||
if customer.cuser_role_id:
|
||||
if customer.cuser_role.sys_ws_id != workspace.pk:
|
||||
raise exceptions.ValidationError('اتصال پنل متعلق به این فضای کاری نیست؛ ابتدا اتصال را اصلاح کنید.')
|
||||
customer.cuser_role.customer_panel = False
|
||||
customer.cuser_role.save(update_fields=['customer_panel'])
|
||||
return customer.cuser_role
|
||||
try:
|
||||
cuser_obj = CUser.objects.get(mobile=mobile)
|
||||
except CUser.DoesNotExist:
|
||||
@@ -36,29 +47,29 @@ def customer_panel_access(request, mobile: int, customer_id: int, status: bool)
|
||||
is_active=True,
|
||||
)
|
||||
|
||||
try:
|
||||
cuser_role_obj = CuserRoleModel.objects.get(cuser=cuser_obj, role_type_id=3)
|
||||
cuser_role_obj.customer_panel = status
|
||||
cuser_role_obj.save()
|
||||
except CuserRoleModel.DoesNotExist:
|
||||
cuser_role_obj = CuserRoleModel.objects.create(
|
||||
cuser=cuser_obj,
|
||||
role_type_id=3,
|
||||
name_family=customer.name_family,
|
||||
mobile=mobile,
|
||||
mobile_verify=True,
|
||||
is_active=True,
|
||||
customer_panel=status,
|
||||
sys_ws=workspace
|
||||
)
|
||||
|
||||
if cuser_role_obj:
|
||||
customer.cuser_role = cuser_role_obj
|
||||
customer.save()
|
||||
return cuser_role_obj
|
||||
else:
|
||||
raise Exception
|
||||
|
||||
from apps.cuser.models import RoleType
|
||||
if not cuser_obj.is_active:
|
||||
raise exceptions.PermissionDenied('حساب کاربری مربوط به این شماره غیرفعال است.')
|
||||
role_type = RoleType.objects.get(role_name_en='workspace_customer')
|
||||
cuser_role_obj, _ = CuserRoleModel.objects.get_or_create(
|
||||
cuser=cuser_obj, role_type=role_type, sys_ws=workspace,
|
||||
defaults={'name_family': customer.name_family, 'mobile': mobile,
|
||||
'mobile_verify': True, 'is_active': True, 'customer_panel': True},
|
||||
)
|
||||
if CustomerModel.objects.filter(cuser_role=cuser_role_obj, sys_ws=workspace).exclude(pk=customer.pk).exists():
|
||||
raise exceptions.ValidationError({'mobile': 'این شماره قبلاً به پنل مشتری دیگری متصل شده است.'})
|
||||
cuser_role_obj.customer_panel = True
|
||||
cuser_role_obj.save(update_fields=['customer_panel'])
|
||||
customer.cuser_role = cuser_role_obj
|
||||
customer.full_clean()
|
||||
customer.save()
|
||||
return cuser_role_obj
|
||||
except exceptions.APIException:
|
||||
raise
|
||||
except DjangoValidationError as exp:
|
||||
raise exceptions.ValidationError(exp.message_dict)
|
||||
except IntegrityError:
|
||||
raise exceptions.ValidationError({'mobile': 'این شماره موبایل قبلاً ثبت شده است.'})
|
||||
except Exception as exp:
|
||||
message = f'Error Create or Update Customer panel accsess: {exp}'
|
||||
raise exceptions.ValidationError({'error': message})
|
||||
@@ -104,6 +115,7 @@ def delete_shive_ashenaei(shive_id: int, workspace_uuid: str) -> ShiveAshenaeiMo
|
||||
raise exceptions.ValidationError({'error': message})
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def create_customer(name_family: str , mobile: int, phone: int, address: str, city: int, shive_ashenaei: int,
|
||||
responsible_group: int, cuser_role: int = None, workspace: WorkspaceModel = None) -> CustomerModel:
|
||||
try:
|
||||
@@ -122,39 +134,48 @@ def create_customer(name_family: str , mobile: int, phone: int, address: str, ci
|
||||
customer_obj.full_clean()
|
||||
customer_obj.save()
|
||||
return customer_obj
|
||||
except DjangoValidationError as exp:
|
||||
raise exceptions.ValidationError(exp.message_dict)
|
||||
except IntegrityError:
|
||||
raise exceptions.ValidationError({'mobile': 'مشتری با این شماره موبایل قبلاً ثبت شده است.'})
|
||||
except Exception as exp:
|
||||
message = f'Error Creating Customer: {exp}'
|
||||
raise exceptions.ValidationError({'error': message})
|
||||
|
||||
|
||||
def update_customer(customer_id: int, name_family: str = None , mobile: int = None, phone: int = None, address: str = None,
|
||||
def set_customer_mobile(customer, mobile):
|
||||
if mobile is None:
|
||||
return
|
||||
normalized = normalize_mobile(mobile)
|
||||
if normalized != customer.mobile and customer.cuser_role_id:
|
||||
role = customer.cuser_role
|
||||
if role.sys_ws_id == customer.sys_ws_id:
|
||||
role.customer_panel = False
|
||||
role.save(update_fields=['customer_panel'])
|
||||
customer.cuser_role = None
|
||||
customer.mobile = normalized
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def update_customer(customer_id: int, name_family: str = None, mobile: int = None, phone: int = None, address: str = None,
|
||||
city: int = None, shive_ashenaei: int = None, responsible_group: int = None, cuser_role: int = None, workspace_uuid: str = None) -> CustomerModel:
|
||||
try:
|
||||
customer_obj = CustomerModel.objects.get(id=customer_id, sys_ws__uuid=workspace_uuid)
|
||||
|
||||
if name_family is not None:
|
||||
customer_obj.name_family = name_family
|
||||
if mobile is not None:
|
||||
customer_obj.mobile = mobile
|
||||
if phone is not None:
|
||||
customer_obj.phone = phone
|
||||
if address is not None:
|
||||
customer_obj.address = address
|
||||
if city is not None:
|
||||
customer_obj.city = city
|
||||
if shive_ashenaei is not None:
|
||||
customer_obj.shive_ashenaei = shive_ashenaei
|
||||
if responsible_group is not None:
|
||||
customer_obj.responsible_group = responsible_group
|
||||
if cuser_role is not None:
|
||||
customer_obj.cuser_role = cuser_role
|
||||
|
||||
customer_obj.full_clean()
|
||||
customer_obj.save()
|
||||
return customer_obj
|
||||
except Exception as exp:
|
||||
message = f'Error Creating Customer: {exp}'
|
||||
raise exceptions.ValidationError({'error': message})
|
||||
customer = CustomerModel.objects.select_for_update().get(id=customer_id, sys_ws__uuid=workspace_uuid)
|
||||
set_customer_mobile(customer, mobile)
|
||||
fields = {'name_family': name_family, 'phone': phone, 'address': address, 'city': city,
|
||||
'shive_ashenaei': shive_ashenaei, 'responsible_group': responsible_group, 'cuser_role': cuser_role}
|
||||
for field, value in fields.items():
|
||||
if value is not None:
|
||||
setattr(customer, field, value)
|
||||
customer.full_clean()
|
||||
customer.save()
|
||||
return customer
|
||||
except DjangoValidationError as error:
|
||||
raise exceptions.ValidationError(error.message_dict)
|
||||
except IntegrityError:
|
||||
raise exceptions.ValidationError({'mobile': 'مشتری با این شماره موبایل قبلاً ثبت شده است.'})
|
||||
except CustomerModel.DoesNotExist:
|
||||
raise exceptions.NotFound('مشتری در این فضای کاری پیدا نشد.')
|
||||
|
||||
|
||||
def delete_customer(customer_id: int, workspace_uuid: str = None) -> CustomerModel:
|
||||
@@ -165,6 +186,7 @@ def delete_customer(customer_id: int, workspace_uuid: str = None) -> CustomerMod
|
||||
raise exceptions.ValidationError({'error': message})
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def create_customer_v2(request, name_family: str , mobile: int, phone: int, address: str, city: int, shive_ashenaei: int,
|
||||
responsible_group: int, cuser_role: int = None) -> CustomerModel:
|
||||
try:
|
||||
@@ -186,6 +208,10 @@ def create_customer_v2(request, name_family: str , mobile: int, phone: int, addr
|
||||
customer_obj.full_clean()
|
||||
customer_obj.save()
|
||||
return customer_obj
|
||||
except DjangoValidationError as exp:
|
||||
raise exceptions.ValidationError(exp.message_dict)
|
||||
except IntegrityError:
|
||||
raise exceptions.ValidationError({'mobile': 'مشتری با این شماره موبایل قبلاً ثبت شده است.'})
|
||||
except Exception as exp:
|
||||
message = f'Error Creating Customer: {exp}'
|
||||
raise exceptions.ValidationError({'error': message})
|
||||
|
||||
Reference in new issue
Block a user