import typing from apps.pos_device.models import Device, StakeHolders from apps.product.models import ( Quota, QuotaDistribution, Organization ) def pos_organizations_sharing_information( device: Device, quota: Quota = None, distribution: QuotaDistribution = None, owner_org: Organization = None ) -> typing.Any: """ pos sharing organizations' information, device have multiple organizations (sub_accounts) for sharing money """ stake_holders = device.stake_holders.select_related('broker', 'organization').filter() sharing_information_list = [] for item in stake_holders: if item.broker and not owner_org.type.name == 'AGC': # if stakeholder is not an agency, it is a broker sharing_information_list.append({ "organization_name": item.organization.name, "bank_account": { "credit_card": item.organization.bank_information.first().card, "sheba": item.organization.bank_information.first().sheba, "account": item.organization.bank_information.first().account, } if item.organization.bank_information.exists() else {}, "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 we will need to get agencies share amount, we can use this bellow code # # # item.holders_share_amount.filter(quota_distribution=distribution).first().share_amount # # if item.holders_share_amount.filter(quota_distribution=distribution).exists() else None # """ "agency": False, "default_account": item.default }) else: sharing_information_list.append({ "organization_name": "تعاونی دامداران فامنین", "bank_account": { "credit_card": "2222222222222222", "sheba": "666666666666666666", "account": "4444444444444444444444", }, "broker": "تعاونی", "amount": 1600, # """ # if we will need to get agencies share amount, we can use this bellow code # # # item.holders_share_amount.filter(quota_distribution=distribution).first().share_amount # # if item.holders_share_amount.filter(quota_distribution=distribution).exists() else None # """ "agency": False, "default_account": True }) # if device owner is an agency organization print(sharing_information_list) if owner_org.type.key == 'AGC': sharing_information_list = agency_organization_pos_info( agency=owner_org, pos_sharing_list=sharing_information_list, device=device, distribution=distribution ) return sharing_information_list def agency_organization_pos_info( agency: Organization = None, pos_sharing_list: list = None, device: Device = None, distribution: QuotaDistribution = None ) -> typing.Any: """ if pos org owner is an agency, calculate share amount of agency and share amount of parent organization """ # get agency share amount agc_share_amount = agency.pos_stake_holders.filter( device=device, ).first().holders_share_amount.filter( quota_distribution=distribution ) agc_share_amount = agc_share_amount.first().share_amount if agc_share_amount else None # calculate agency parent share amount agc_parent_amount = next((item for item in pos_sharing_list if item.get('default_account')), None) if agc_parent_amount and agc_share_amount: agc_parent_amount['amount'] = agc_parent_amount['amount'] - agc_share_amount pos_sharing_list.append({ "organization_name": agency.name, "bank_account": { "credit_card": agency.bank_information.first().card, "sheba": agency.bank_information.first().sheba, "account": agency.bank_information.first().account, } if agency.bank_information.exists() else {}, "amount": agc_share_amount, "agency": True, "default_account": True }) return pos_sharing_list return None def get_organization_by_stake_holder(id: int = None, stake_holder: StakeHolders = None) -> typing.Any: """ get organization object by stake holder """ if id: stake_holder = StakeHolders.objects.get(id=id) organization = get_organization_by_stake_holder(stake_holder=stake_holder) return organization organization = stake_holder.assignment.client.organization return organization # sssss