From c2bb50d9f89f62ff3eaa4d22c24b10fbf41de04b Mon Sep 17 00:00:00 2001 From: Mojtaba-z Date: Wed, 3 Dec 2025 15:03:24 +0330 Subject: [PATCH] fix - pos device agencies on new system with quota stat --- ...9_stakeholdershareamount_org_quota_stat.py | 20 +++++++++++++ apps/pos_device/models.py | 8 ++++- apps/pos_device/services/services.py | 29 ++++++++++--------- apps/pos_device/web/api/v1/viewsets/device.py | 17 +++++++++-- 4 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 apps/pos_device/migrations/0079_stakeholdershareamount_org_quota_stat.py diff --git a/apps/pos_device/migrations/0079_stakeholdershareamount_org_quota_stat.py b/apps/pos_device/migrations/0079_stakeholdershareamount_org_quota_stat.py new file mode 100644 index 0000000..22a147b --- /dev/null +++ b/apps/pos_device/migrations/0079_stakeholdershareamount_org_quota_stat.py @@ -0,0 +1,20 @@ +# Generated by Django 5.0 on 2025-12-03 11:13 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('pos_device', '0078_deviceversion_checksum_deviceversion_url'), + ('product', '0101_quota_edited_pricing_features'), + ] + + operations = [ + migrations.AddField( + model_name='stakeholdershareamount', + name='org_quota_stat', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='holders_share_amount', to='product.organizationquotastats'), + ), + ] diff --git a/apps/pos_device/models.py b/apps/pos_device/models.py index 7cba736..0d62f7e 100644 --- a/apps/pos_device/models.py +++ b/apps/pos_device/models.py @@ -7,7 +7,7 @@ from django.db import models from apps.authentication.models import Organization, City, Province from apps.authorization.models import UserRelations from apps.core.models import BaseModel -from apps.product.models import Product, Broker, QuotaBrokerValue, QuotaDistribution +from apps.product.models import Product, Broker, QuotaBrokerValue, QuotaDistribution, OrganizationQuotaStats class ProviderCompany(BaseModel): @@ -292,6 +292,12 @@ class StakeHolderShareAmount(BaseModel): related_name='holders_share_amount', null=True ) + org_quota_stat = models.ForeignKey( + OrganizationQuotaStats, + on_delete=models.CASCADE, + related_name='holders_share_amount', + null=True + ) stakeholders = models.ForeignKey( StakeHolders, on_delete=models.CASCADE, diff --git a/apps/pos_device/services/services.py b/apps/pos_device/services/services.py index 459cf06..cf65a14 100644 --- a/apps/pos_device/services/services.py +++ b/apps/pos_device/services/services.py @@ -4,14 +4,14 @@ from apps.pos_device.models import Device, StakeHolders from apps.product.models import ( Quota, QuotaDistribution, - Organization + Organization, OrganizationQuotaStats ) def pos_organizations_sharing_information( device: Device, quota: Quota = None, - quota_stat=None, + quota_stat: OrganizationQuotaStats = None, distribution: QuotaDistribution = None, owner_org: Organization = None ) -> typing.Any: @@ -23,7 +23,7 @@ def pos_organizations_sharing_information( sharing_information_list = [] for item in stake_holders: - if item.broker and not owner_org.type.key == 'AGC': # if stakeholder is not an agency, it is a broker + if item.broker: # if stakeholder is not an agency, it is a broker sharing_information_list.append({ "organization_name": item.organization.name, "bank_account": { @@ -34,8 +34,9 @@ def pos_organizations_sharing_information( "broker": item.broker.name if item.broker else None, "amount": quota.broker_values.filter( broker=item.broker, - ).first().value if quota and item.broker else None, - + # if quota price features was edited for this organization + org_quota_stat=quota_stat if quota_stat.broker_values.exists() else None + ).first().value if quota.broker_values.filter(broker=item.broker).exists() else None, # """ # if we will need to get agencies share amount, we can use this bellow code # @@ -47,7 +48,7 @@ def pos_organizations_sharing_information( }) elif owner_org.type.key == 'AGC': agc_stake_holder = owner_org.pos_stake_holders.filter( - holders_share_amount__quota_distribution=distribution + holders_share_amount__org_quota_stat=quota_stat, ).first() stake_holders = agc_stake_holder.device.stake_holders.select_related( 'broker', 'organization' @@ -64,9 +65,9 @@ def pos_organizations_sharing_information( "broker": item.broker.name if item.broker else None, "amount": quota.broker_values.filter( broker=item.broker, - org_quota_stat=quota_stat - ).first().value if quota and item.broker else None, - + # if quota price features was edited for this organization + org_quota_stat=quota_stat if quota_stat.broker_values.exists() else None + ).first().value if quota.broker_values.filter(broker=item.broker).exists() else None, # """ # if we will need to get agencies share amount, we can use this bellow code # @@ -83,7 +84,8 @@ def pos_organizations_sharing_information( agency=owner_org, pos_sharing_list=sharing_information_list, device=device, - distribution=distribution + distribution=distribution, + quota_stat=quota_stat ) return sharing_information_list @@ -93,7 +95,8 @@ def agency_organization_pos_info( agency: Organization = None, pos_sharing_list: list = None, device: Device = None, - distribution: QuotaDistribution = None + distribution: QuotaDistribution = None, + quota_stat: OrganizationQuotaStats = None ) -> typing.Any: """ if pos org owner is an agency, calculate share amount of agency @@ -102,11 +105,11 @@ def agency_organization_pos_info( # get agency share amount agc_share_amount = agency.pos_stake_holders.filter( - holders_share_amount__quota_distribution=distribution + holders_share_amount__org_quota_stat=quota_stat ) agc_share_amount = agc_share_amount.first().holders_share_amount.filter( - quota_distribution=distribution + org_quota_stat=quota_stat ) if agc_share_amount.exists() else None agc_share_amount = agc_share_amount.first().share_amount if agc_share_amount.exists() else None diff --git a/apps/pos_device/web/api/v1/viewsets/device.py b/apps/pos_device/web/api/v1/viewsets/device.py index df4feda..7e01021 100644 --- a/apps/pos_device/web/api/v1/viewsets/device.py +++ b/apps/pos_device/web/api/v1/viewsets/device.py @@ -26,7 +26,7 @@ from apps.core.services.visibility_service import apply_visibility_filter_by_org from apps.pos_device import models as pos_models from apps.pos_device.web.api.v1.serilaizers import device as device_serializer from apps.pos_device.web.api.v1.viewsets.client import POSClientViewSet -from apps.product.models import Broker +from apps.product.models import Broker, OrganizationQuotaStats from apps.product.web.api.v1.viewsets.quota_distribution_api import QuotaDistributionViewSet from common.helpers import generate_code from common.helpers import get_organization_by_user @@ -389,7 +389,11 @@ class StakeHoldersViewSet(SoftDeleteMixin, viewsets.ModelViewSet, DynamicSearchM class StakeHolderShareAmountViewSet(viewsets.ModelViewSet, DynamicSearchMixin, SoftDeleteMixin): - queryset = pos_models.StakeHolderShareAmount.objects.select_related('quota_distribution', 'stakeholders') + queryset = pos_models.StakeHolderShareAmount.objects.select_related( + 'quota_distribution', + 'org_quota_stat', + 'stakeholders' + ) serializer_class = device_serializer.StakeHolderShareAmountSerializer @transaction.atomic @@ -413,7 +417,14 @@ class StakeHolderShareAmountViewSet(viewsets.ModelViewSet, DynamicSearchMixin, S view=QuotaDistributionViewSet(), data=data['distribution'] ) - data.update({'quota_distribution': distribution['id']}) + + # quota stat will create after distribution , we get that obj here + quota_stat = OrganizationQuotaStats.objects.get( + quota_id=data['distribution']['quota'], + organization_id=data['distribution']['assigned_organization'] + ) + + data.update({'quota_distribution': distribution['id'], 'org_quota_stat': quota_stat.id}) serializer = self.serializer_class(data=data) if serializer.is_valid(raise_exception=True):