This commit is contained in:
javad committed 2024-01-22 13:01:42 +03:30
commit 89a5d70512
284 files changed
+8364

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
from .product_category import *
from .product import *
from .order import *
from .order_item import *
from .cart import *
+36
View File
@@ -0,0 +1,36 @@
from rest_framework import exceptions
from decimal import Decimal
from django.db import transaction
from apps.shop.models import Cart as CartModel
def add_to_cart(product_id: int, quantity: int, workspace, cuser_role) -> CartModel:
try:
try:
cart_obj = CartModel.objects.get(product_id=product_id, sys_ws=workspace, cuser_role=cuser_role)
except Exception as exp:
cart_obj = CartModel()
cart_obj.product_id = product_id
cart_obj.quantity = quantity
cart_obj.sys_ws = workspace
cart_obj.cuser_role = cuser_role
cart_obj.full_clean()
cart_obj.save()
return cart_obj
except Exception as exp:
message = f'Error Add to Cart: {exp}'
exceptions.ValidationError({'error': message})
def delete_cart_iteam(cart_id: int, cuser_role_uuid, workspace_uuid: str) -> CartModel:
try:
cart_obj = CartModel.objects.get(id=cart_id, cuser_role__uuid=cuser_role_uuid, sys_ws__uuid=workspace_uuid).delete()
return cart_obj
except Exception as exp:
message = f'Error Deleting Cart iteam: {exp}'
raise exceptions.ValidationError({'error': message})
+111
View File
@@ -0,0 +1,111 @@
import json
from apps.shop.models import (
Order as OrderModel,
OrderItem as OrderItemModel,
Product as ProductModel,
)
from apps.customer.models import Customer as CustomerModel
from rest_framework import exceptions
from decimal import Decimal
from django.db import transaction
def create_order(responsible_group_id: int, customer_id: int,
total_amount: Decimal, factor_type: int, factor_status: int, workspace) -> OrderModel:
try:
order_obj = OrderModel()
order_obj.responsible_group_id = responsible_group_id
order_obj.customer_id = customer_id
order_obj.total_amount = total_amount
order_obj.factor_type = factor_type
order_obj.factor_status = factor_status
order_obj.sys_ws = workspace
order_obj.full_clean()
order_obj.save()
return order_obj
except Exception as exp:
message = f'Error Create Order: {exp}'
exceptions.ValidationError({'error': message})
def create_order_and_items(items: str, customer: CustomerModel, workspace) -> OrderModel:
with transaction.atomic():
try:
sum_price = 0
order_obj = OrderModel()
order_obj.responsible_group = customer.responsible_group
order_obj.customer = customer
order_obj.total_price = 0
order_obj.factor_type = 1
order_obj.factor_status = 1
order_obj.sys_ws = workspace
order_obj.full_clean()
order_obj.save()
json_items = json.loads(items)
for item in json_items:
product_obj = ProductModel.objects.get(id=int(item['product_id']))
product_price = product_obj.price
product_name = product_obj.name
order_item_obj = OrderItemModel()
order_item_obj.order = order_obj
order_item_obj.product_name = product_name
order_item_obj.price = product_price
order_item_obj.amount = int(item['amount'])
order_item_obj.product_id = int(item['product_id'])
order_item_obj.full_clean()
order_item_obj.save()
sum_price += product_price
order_obj.total_price = sum_price
order_obj.full_clean()
order_obj.save()
return order_obj
except Exception as exp:
transaction.set_rollback(True)
message = f'Error Create Order: {exp}'
raise exceptions.ValidationError({'error': message})
def update_order(order_id: int, responsible_group_id: int = None, customer_id: int = None,
total_amount: Decimal = None, factor_type: int = None, factor_status: int = None, workspace_uuid = None) -> OrderModel:
try:
order_obj = OrderModel.objects.get(id=order_id, sys_ws__uuid=workspace_uuid)
if responsible_group_id is not None:
order_obj.responsible_group_id = responsible_group_id
if customer_id is not None:
order_obj.customer_id = customer_id
if total_amount is not None:
order_obj.total_amount = total_amount
if factor_type is not None:
order_obj.factor_type = factor_type
if factor_status is not None:
order_obj.factor_status = factor_status
order_obj.full_clean()
order_obj.save()
return order_obj
except Exception as exp:
message = f'Error Updating Order: {exp}'
raise exceptions.ValidationError({'error': message})
def delete_order(order_id: int, workspace_uuid: str) -> OrderModel:
try:
order_obj = OrderModel.objects.get(id=order_id, sys_ws__uuid=workspace_uuid).delete()
return order_obj
except Exception as exp:
message = f'Error Deleting Order: {exp}'
raise exceptions.ValidationError({'error': message})
+18
View File
@@ -0,0 +1,18 @@
import json
from decimal import Decimal
from apps.shop.models import OrderItem as OrderItemModel
def create_order_items(items: str, order_id: int):
json_items = json.loads(items)
for item in json_items:
order_item_obj = OrderItemModel()
order_item_obj.order_id = order_id
order_item_obj.price = Decimal(item.price)
order_item_obj.amount = int(item.amount)
order_item_obj.product_id = int(item.product)
order_item_obj.full_clean()
order_item_obj.save()
+55
View File
@@ -0,0 +1,55 @@
from apps.shop.models import Product as ProductModel
from decimal import Decimal
from rest_framework import exceptions
def create_product(name: str, price: Decimal, category: int, acc_code: str = None, attr: str = None, workspace=None) -> ProductModel:
try:
product_obj = ProductModel()
product_obj.name = name
product_obj.price = price
product_obj.category_id = category
if acc_code is not None:
product_obj.acc_code = acc_code
if attr is not None:
product_obj.attr = attr
product_obj.sys_ws = workspace
product_obj.full_clean()
product_obj.save()
return product_obj
except Exception as exp:
message = f'Error Create Product: {exp}'
exceptions.ValidationError({'error': message})
def update_product(product_id: int, name: str, price: Decimal, category: int, acc_code: str = None, attr: str = None, workspace_uuid: str = None) -> ProductModel:
try:
product_obj = ProductModel.objects.get(id=product_id, sys_ws__uuid=workspace_uuid)
product_obj.name = name
product_obj.price = price
product_obj.category_id = category
if acc_code is not None:
product_obj.acc_code = acc_code
if attr is not None:
product_obj.attr = attr
product_obj.full_clean()
product_obj.save()
return product_obj
except Exception as exp:
message = f'Error Updating Product: {exp}'
raise exceptions.ValidationError({'error': message})
def delete_product(product_id: int, workspace_uuid: str) -> ProductModel:
try:
product_obj = ProductModel.objects.get(id=product_id, sys_ws__uuid=workspace_uuid).delete()
return product_obj
except Exception as exp:
message = f'Error Deleting Product: {exp}'
raise exceptions.ValidationError({'error': message})
+40
View File
@@ -0,0 +1,40 @@
from apps.shop.models import ProductCategory as ProductCategoryModel
from rest_framework import exceptions
def create_product_category(name: str, parent: int = None, workspace=None) -> ProductCategoryModel:
try:
product_category_obj = ProductCategoryModel(name=name, sys_ws=workspace)
if parent is not None:
product_category_obj.parent_id = parent
product_category_obj.full_clean()
product_category_obj.save()
return product_category_obj
except Exception as exp:
message = f'Error Create ProductCategory: {exp}'
exceptions.ValidationError({'error': message})
def update_product_category(category_id: int, name: str, workspace_uuid: str) -> ProductCategoryModel:
try:
product_category_obj = ProductCategoryModel.objects.get(id=category_id, sys_ws__uuid=workspace_uuid)
product_category_obj.name = name
product_category_obj.full_clean()
product_category_obj.save()
return product_category_obj
except Exception as exp:
message = f'Error Updating ProductCategory: {exp}'
raise exceptions.ValidationError({'error': message})
def delete_product_category(category_id: int, workspace_uuid: str) -> ProductCategoryModel:
try:
product_category_obj = ProductCategoryModel.objects.get(id=category_id, sys_ws__uuid=workspace_uuid).delete()
return product_category_obj
except Exception as exp:
message = f'Error Deleting ProductCategory: {exp}'
raise exceptions.ValidationError({'error': message})