From 63898533a3eb5121b23c1129f626411fd073a625 Mon Sep 17 00:00:00 2001 From: Mojtaba-z Date: Tue, 29 Jul 2025 07:58:32 +0330 Subject: [PATCH] some changes in inventory serializer - creted date & distribution data --- ...evice_is_activated_deviceactivationcode.py | 43 +++++++++++++++++++ ...organization_alter_productstats_product.py | 25 +++++++++++ apps/product/models.py | 8 +++- apps/product/signals.py | 10 +++-- apps/warehouse/web/api/v1/serializers.py | 15 ++----- 5 files changed, 86 insertions(+), 15 deletions(-) create mode 100644 apps/pos_device/migrations/0009_device_is_activated_deviceactivationcode.py create mode 100644 apps/product/migrations/0056_productstats_organization_alter_productstats_product.py diff --git a/apps/pos_device/migrations/0009_device_is_activated_deviceactivationcode.py b/apps/pos_device/migrations/0009_device_is_activated_deviceactivationcode.py new file mode 100644 index 0000000..255bad9 --- /dev/null +++ b/apps/pos_device/migrations/0009_device_is_activated_deviceactivationcode.py @@ -0,0 +1,43 @@ +# Generated by Django 5.0 on 2025-07-28 13:45 + +import datetime +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('pos_device', '0008_device_device_identity_deviceassignment'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.AddField( + model_name='device', + name='is_activated', + field=models.BooleanField(default=False), + ), + migrations.CreateModel( + name='DeviceActivationCode', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('create_date', models.DateTimeField(auto_now_add=True)), + ('modify_date', models.DateTimeField(auto_now=True)), + ('creator_info', models.CharField(max_length=100, null=True)), + ('modifier_info', models.CharField(max_length=100, null=True)), + ('trash', models.BooleanField(default=False)), + ('code', models.CharField(max_length=10, null=True, unique=True)), + ('expires_at', models.DateTimeField(default=datetime.datetime(2025, 7, 28, 17, 15, 1, 601936))), + ('is_used', models.BooleanField(default=False)), + ('company', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='code', to='pos_device.providercompany')), + ('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_createddby', to=settings.AUTH_USER_MODEL)), + ('device', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='code', to='pos_device.device')), + ('modified_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_modifiedby', to=settings.AUTH_USER_MODEL)), + ], + options={ + 'abstract': False, + }, + ), + ] diff --git a/apps/product/migrations/0056_productstats_organization_alter_productstats_product.py b/apps/product/migrations/0056_productstats_organization_alter_productstats_product.py new file mode 100644 index 0000000..f654458 --- /dev/null +++ b/apps/product/migrations/0056_productstats_organization_alter_productstats_product.py @@ -0,0 +1,25 @@ +# Generated by Django 5.0 on 2025-07-28 13:45 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('authentication', '0027_remove_organizationstats_total_buyers_and_more'), + ('product', '0055_alter_quota_limit_by_organizations'), + ] + + operations = [ + migrations.AddField( + model_name='productstats', + name='organization', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='product_stats', to='authentication.organization'), + ), + migrations.AlterField( + model_name='productstats', + name='product', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='stats', to='product.product'), + ), + ] diff --git a/apps/product/models.py b/apps/product/models.py index 72c8f93..a50856c 100644 --- a/apps/product/models.py +++ b/apps/product/models.py @@ -135,12 +135,18 @@ class Product(BaseModel): class ProductStats(BaseModel): - product = models.OneToOneField( + product = models.ForeignKey( Product, on_delete=models.CASCADE, related_name='stats', null=True ) + organization = models.ForeignKey( + Organization, + on_delete=models.CASCADE, + related_name='product_stats', + null=True + ) quotas_number = models.PositiveBigIntegerField(default=0) active_quotas_weight = models.PositiveBigIntegerField(default=0) closed_quotas_weight = models.PositiveBigIntegerField(default=0) diff --git a/apps/product/signals.py b/apps/product/signals.py index bd824c3..901bb7d 100644 --- a/apps/product/signals.py +++ b/apps/product/signals.py @@ -1,5 +1,6 @@ from django.db.models import Sum from django.db.models.signals import post_save, post_delete +from common.helpers import get_organization_by_user from django.dispatch import receiver from .models import ( QuotaDistribution, @@ -13,6 +14,7 @@ from apps.warehouse.models import ( InventoryEntry ) from django.db import models +from crum import get_current_user def recalculate_remaining_amount(quota): @@ -34,10 +36,12 @@ def update_quota_remaining(sender, instance, **kwargs): def update_product_stats(instance: Product): """ update all stats of product """ - if hasattr(instance, 'stats'): - stat = instance.stats + organization = get_organization_by_user(get_current_user()) + + if ProductStats.objects.filter(organization=organization, product=instance): + stat = instance.stats.get(organization=organization, product=instance) else: - stat = ProductStats.objects.create(product=instance) + stat = ProductStats.objects.create(product=instance, organization=organization) # number of quotas quotas_count = instance.quotas.filter(is_closed=False).count() # noqa diff --git a/apps/warehouse/web/api/v1/serializers.py b/apps/warehouse/web/api/v1/serializers.py index 7154beb..d6dbe09 100644 --- a/apps/warehouse/web/api/v1/serializers.py +++ b/apps/warehouse/web/api/v1/serializers.py @@ -14,6 +14,8 @@ class InventoryEntrySerializer(serializers.ModelSerializer): model = warehouse_models.InventoryEntry fields = [ "id", + "create_date", + "modify_date", "distribution", "weight", "lading_number", @@ -22,17 +24,6 @@ class InventoryEntrySerializer(serializers.ModelSerializer): "notes", ] - def create(self, validated_data): - """ Custom create & set organization """ - - distribution = validated_data['distribution'] - organization = distribution.assigned_organization - - return warehouse_models.InventoryEntry.objects.create( - organization=organization, - **validated_data - ) - def validate(self, attrs): """ check if inventory entries weight is not more than @@ -58,6 +49,8 @@ class InventoryEntrySerializer(serializers.ModelSerializer): def to_representation(self, instance): representation = super().to_representation(instance) representation['document'] = instance.document + if instance.distribution: + representation['distribution'] = instance.distribution.distribution_id return representation