some first models of pos & add required to attributes

This commit is contained in:
2025-07-21 09:39:31 +03:30
parent c87204c134
commit ebc79a7dbd
14 changed files with 446 additions and 17 deletions

View File

@@ -0,0 +1,32 @@
# Generated by Django 5.0 on 2025-07-20 08:59
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('authentication', '0026_organizationstats'),
]
operations = [
migrations.RemoveField(
model_name='organizationstats',
name='total_buyers',
),
migrations.AddField(
model_name='organizationstats',
name='active_quotas_weight',
field=models.PositiveBigIntegerField(default=0),
),
migrations.AddField(
model_name='organizationstats',
name='closed_quotas_weight',
field=models.PositiveBigIntegerField(default=0),
),
migrations.AddField(
model_name='organizationstats',
name='total_quotas_weight',
field=models.PositiveBigIntegerField(default=0),
),
]

View File

@@ -143,10 +143,12 @@ class OrganizationStats(BaseModel):
null=True
)
total_quota_received = models.PositiveBigIntegerField(default=0)
active_quotas_weight = models.PositiveBigIntegerField(default=0)
closed_quotas_weight = models.PositiveBigIntegerField(default=0)
total_quotas_weight = models.PositiveBigIntegerField(default=0)
total_distributed = models.PositiveBigIntegerField(default=0)
total_inventory_in = models.PositiveBigIntegerField(default=0)
total_sold = models.PositiveBigIntegerField(default=0)
total_buyers = models.PositiveBigIntegerField(default=0)
def __str__(self):
return f'Organization: {self.organization.name}'

View File

@@ -1,11 +1,43 @@
from django.db.models import Sum
from apps.product.models import QuotaDistribution
from apps.warehouse.models import InventoryQuotaSaleTransaction
from apps.authentication.models import Organization, OrganizationStats
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
def update_organization_stats(instance: Organization):
""" update all stats of organization from quota """
if hasattr(instance, 'stats'):
stat = instance.stats
else:
stat = OrganizationStats.objects.create(organization=instance)
# set organization stats from quotas, distributions transactions & etc
stat.total_quota_received = instance.assigned_quotas.count()
stat.active_quotas_weight = instance.assigned_quotas.filter(is_closed=False).count()
stat.closed_quotas_weight = instance.assigned_quotas.filter(is_closed=True).count()
stat.total_distributed = instance.distributions.count()
stat.total_inventory_in = instance.inventory.count()
stat.total_sold = instance.distributions.all().aggregate(
total_sold=Sum('been_sold')
)['total_sold'] or 0
stat.save(update_fields=[
"total_quota_received",
"active_quotas_weight",
"closed_quotas_weight",
"total_distributed",
"total_inventory_in",
"total_sold",
])
@receiver([post_save, post_delete], sender=QuotaDistribution)
@receiver([post_save, post_delete], sender=InventoryQuotaSaleTransaction)
def update_organization_stats(sender, instance, **kwargs):
pass
def organization_stats(sender, instance, **kwargs):
if sender == QuotaDistribution:
update_organization_stats(instance.assigned_organization)
elif sender == InventoryQuotaSaleTransaction:
update_organization_stats(instance.inventory_entry.organization)

View File

@@ -24,7 +24,42 @@ class MyConsumer(AsyncWebsocketConsumer):
unit = await get_unit(int(message))
print("Received:", message)
# پاسخ به کلاینت
await self.send(text_data=json.dumps({
"message": unit
"message": unit.unit
}))
class SendFromServerConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.group_name = 'mojtaba_group' # noqa
await self.channel_layer.group_add(
self.group_name,
self.channel_name
)
await self.accept()
await self.send(text_data=json.dumps({"message": "Connected!"}))
async def disconnect(self, code):
await self.channel_layer.group_discard(
self.group_name,
self.channel_name
)
async def receive(self, text_data=None, bytes_data=None):
data = json.loads(text_data)
msg = data.get("message", "")
await self.channel_layer.group_send(
self.group_name,
{
"type": "chat.message",
"message": msg
}
)
async def chat_message(self, event):
message = event['message']
await self.send(text_data=json.dumps({
"message": message
}))

View File

@@ -2,5 +2,5 @@ from django.urls import re_path
from apps.authentication.websocket import consumer
websocket_urlpatterns = [
re_path(r"ws/somepath/$", consumer.MyConsumer.as_asgi()),
re_path(r"ws/somepath/$", consumer.SendFromServerConsumer.as_asgi()),
]