fix garanty esh
This commit is contained in:
1 parent
bf78bc2520
commit
739104115a
6 files changed
+382
-78
No files matched your search
+50
-22
@@ -2,7 +2,7 @@ from django.db import models
|
||||
from django.db.models import Sum
|
||||
# from django.db.models.signals import post_save, post_delete
|
||||
from django.dispatch import receiver
|
||||
from django.db import transaction # اصلاح فیکس خطای تراکنش
|
||||
from django.db import transaction, connection # اصلاح فیکس خطای تراکنش
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
@@ -43,10 +43,18 @@ class Customer(models.Model):
|
||||
create_date = models.DateTimeField(auto_now_add=True)
|
||||
last_bazdid_dt = models.DateTimeField(null=True, blank=True)
|
||||
laghv_bazdid = models.BooleanField(null=True, blank=True, default=False)
|
||||
|
||||
# فیلد جدید برای حذف نرم (بدون تداخل با دادههای قبلی)
|
||||
is_active = models.BooleanField(default=True, null=True, blank=True, help_text="وضعیت فعالیت مشتری")
|
||||
|
||||
def __str__(self):
|
||||
return self.name_family
|
||||
|
||||
# بازنویسی متد حذف: جلوگیری از فاجعه در دیتابیس
|
||||
def delete(self, *args, **kwargs):
|
||||
# به جای حذف فیزیکی که انبار و فاکتورها را نابود میکند، مشتری را غیرفعال میکنیم
|
||||
self.is_active = False
|
||||
self.save()
|
||||
|
||||
class PhoneNumber(models.Model):
|
||||
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
|
||||
@@ -127,9 +135,27 @@ class Factor(models.Model):
|
||||
]
|
||||
PayType = models.CharField(max_length=30, choices=PayTypeList, default='')
|
||||
|
||||
# فیلد جدید برای حذف نرم
|
||||
is_active = models.BooleanField(default=True, null=True, blank=True)
|
||||
|
||||
# بازنویسی متد حذف: ابطال فاکتور به جای حذف فیزیکی
|
||||
def delete(self, *args, **kwargs):
|
||||
# فاکتور باطل میشود اما رکوردهای انبار دستنخورده باقی میمانند!
|
||||
self.is_active = False
|
||||
self.pay_status = 'cancell'
|
||||
self.save()
|
||||
|
||||
# --- مدل اصلاح شده انبار با کارایی فوقالعاده بالا ---
|
||||
class ProductInStore(models.Model):
|
||||
|
||||
class MovementType(models.TextChoices):
|
||||
SALE = 'SALE', 'فروش عادی'
|
||||
WARRANTY = 'WARRANTY', 'گارانتی'
|
||||
PROMOTION = 'PROMOTION', 'اشانتیون'
|
||||
TRANSFER = 'TRANSFER', 'انتقال'
|
||||
INITIAL = 'INITIAL', 'موجودی اولیه'
|
||||
DIRECT_IN = 'DIRECT_IN', 'ورود دستی'
|
||||
DIRECT_OUT = 'DIRECT_OUT', 'خروج دستی'
|
||||
|
||||
StoreRoom = models.ForeignKey(StoreRoom, on_delete=models.CASCADE)
|
||||
Product = models.ForeignKey(Product, on_delete=models.CASCADE)
|
||||
s_in = models.IntegerField(default=0)
|
||||
@@ -139,14 +165,24 @@ class ProductInStore(models.Model):
|
||||
year = models.IntegerField(default=1400)
|
||||
createdDT = models.DateTimeField(auto_now_add=True)
|
||||
changeDT = models.DateTimeField(auto_now=True)
|
||||
factor = models.ForeignKey(Factor, on_delete=models.CASCADE, null=True, blank=True)
|
||||
|
||||
# تغییر بسیار مهم استراتژیک: تبدیل CASCADE به PROTECT
|
||||
# هیچکس نمیتواند فاکتوری که کالا از انبار خارج کرده را از دیتابیس پاک کند!
|
||||
factor = models.ForeignKey(Factor, on_delete=models.PROTECT, null=True, blank=True)
|
||||
|
||||
movement_type = models.CharField(
|
||||
max_length=20,
|
||||
choices=MovementType.choices,
|
||||
db_index=True,
|
||||
null=True,
|
||||
blank=True
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.Product.product_name} - In: {self.s_in} Out: {self.s_out}"
|
||||
|
||||
# --- متد Save کاملاً فیکس شده و ایمن برای محیطهای چند کانتینری ---
|
||||
# متدهای save و delete دقیقاً همان کدهای بینقص و پرسرعتی هستند که در پیام قبل تایید کردیم
|
||||
def save(self, *args, **kwargs):
|
||||
# ۱. لایه دفاعی تبدیل امن دادهها جهت جلوگیری از TypeError
|
||||
try:
|
||||
self.s_in = int(self.s_in or 0)
|
||||
self.s_out = int(self.s_out or 0)
|
||||
@@ -156,11 +192,11 @@ class ProductInStore(models.Model):
|
||||
is_created = self.pk is None
|
||||
|
||||
with transaction.atomic():
|
||||
# ۲. فیکس خطای پنهان: استفاده از list() برای اجبار دیتابیس به اعمال قفل FOR UPDATE
|
||||
# این خط تضمین میکند کانتینرهای دیگر در Swarm منتظر اتمام این تراکنش میمانند
|
||||
list(ProductInStore.objects.filter(StoreRoom=self.StoreRoom, Product=self.Product).select_for_update())
|
||||
qs_lock = ProductInStore.objects.filter(StoreRoom=self.StoreRoom, Product=self.Product)
|
||||
if connection.features.has_select_for_update:
|
||||
qs_lock = qs_lock.select_for_update()
|
||||
list(qs_lock)
|
||||
|
||||
# ۳. محاسبه دقیق تغییرات فیزیکی انبار در صورت ویرایش رکورد قدیمی
|
||||
if not is_created:
|
||||
old_instance = ProductInStore.objects.get(pk=self.pk)
|
||||
old_balance = old_instance.s_in - old_instance.s_out
|
||||
@@ -171,10 +207,8 @@ class ProductInStore(models.Model):
|
||||
change_amount = self.s_in - self.s_out
|
||||
description = self.desc or "ثبت تراکنش جدید انبار"
|
||||
|
||||
# ۴. ذخیره ردیف جاری در دیتابیس
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
# ۵. محاسبه مجموع دقیق ورودی و خروجیها پس از ثبت تغییر جاری
|
||||
totals = ProductInStore.objects.filter(StoreRoom=self.StoreRoom, Product=self.Product).aggregate(
|
||||
total_in=Sum('s_in'),
|
||||
total_out=Sum('s_out')
|
||||
@@ -183,13 +217,9 @@ class ProductInStore(models.Model):
|
||||
total_out = totals['total_out'] or 0
|
||||
current_stock = total_in - total_out
|
||||
|
||||
# ۶. بهروزرسانی امن دیتابیس (بدون ایجاد لوپ بینهایت متد save)
|
||||
ProductInStore.objects.filter(pk=self.pk).update(current_stock_at_time=current_stock)
|
||||
|
||||
# ۷. فیکس خطای همگامسازی حافظه: آپدیت کردن آبجکت جاری در پایتون
|
||||
self.current_stock_at_time = current_stock
|
||||
|
||||
# ۸. ثبت نهایی لاگ در جدول تاریخچه کل
|
||||
ProductStockHistory.objects.create(
|
||||
store_room=self.StoreRoom,
|
||||
product=self.Product,
|
||||
@@ -198,28 +228,26 @@ class ProductInStore(models.Model):
|
||||
desc=description
|
||||
)
|
||||
|
||||
# --- متد Delete کاملاً فیکس شده و ایمن ---
|
||||
def delete(self, *args, **kwargs):
|
||||
store_room = self.StoreRoom
|
||||
product = self.Product
|
||||
change_amount = -(int(self.s_in or 0) - int(self.s_out or 0))
|
||||
description = f"حذف تراکنش قبلی انبار: {self.desc}"
|
||||
description = f"حذف دستی تراکنش انبار: {self.desc}"
|
||||
|
||||
with transaction.atomic():
|
||||
# اجبار دیتابیس به قفل کردن ردیفها قبل از حذف نهایی
|
||||
list(ProductInStore.objects.filter(StoreRoom=store_room, Product=product).select_for_update())
|
||||
qs_lock = ProductInStore.objects.filter(StoreRoom=store_room, Product=product)
|
||||
if connection.features.has_select_for_update:
|
||||
qs_lock = qs_lock.select_for_update()
|
||||
list(qs_lock)
|
||||
|
||||
# حذف فیزیکی رکورد تراکنش انبار
|
||||
super().delete(*args, **kwargs)
|
||||
|
||||
# محاسبه مجدد موجودی کلِ باقیمانده پس از حذف
|
||||
totals = ProductInStore.objects.filter(StoreRoom=store_room, Product=product).aggregate(
|
||||
total_in=Sum('s_in'),
|
||||
total_out=Sum('s_out')
|
||||
)
|
||||
current_stock = (totals['total_in'] or 0) - (totals['total_out'] or 0)
|
||||
|
||||
# ثبت ردیف معکوس در تاریخچه برای حسابرسی دقیق انبار
|
||||
ProductStockHistory.objects.create(
|
||||
store_room=store_room,
|
||||
product=product,
|
||||
|
||||
Reference in new issue
Block a user