diff --git a/apps/pos_device/services/services.py b/apps/pos_device/services/services.py index 8f5b20e..1f85fe3 100644 --- a/apps/pos_device/services/services.py +++ b/apps/pos_device/services/services.py @@ -22,51 +22,61 @@ def pos_organizations_sharing_information( 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 item.broker and not owner_org.type.key == '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 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 + }) + elif owner_org.type.key == 'AGC': + agc_stake_holder = owner_org.pos_stake_holders.filter( + holders_share_amount__quota_distribution=distribution + ).first() + stake_holders = agc_stake_holder.device.stake_holders.select_related( + 'broker', 'organization' + ).filter() + for item in stake_holders: + if item.broker: # 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 + }) - # if device owner is an agency organization print(sharing_information_list) + # if device owner is an agency organization if owner_org.type.key == 'AGC': sharing_information_list = agency_organization_pos_info( agency=owner_org, @@ -90,14 +100,15 @@ def agency_organization_pos_info( """ # get agency share amount - agc_share_amount = (agency.pos_stake_holders.filter( - device=device, - )) - # .holders_share_amount.filter( - # quota_distribution=distribution - # )) - print(agc_share_amount, device) - agc_share_amount = agc_share_amount.first().share_amount if agc_share_amount else None + agc_share_amount = agency.pos_stake_holders.filter( + holders_share_amount__quota_distribution=distribution + ) + + agc_share_amount = agc_share_amount.first().holders_share_amount.filter( + quota_distribution=distribution + ) if agc_share_amount.exists() else None + + agc_share_amount = agc_share_amount.first().share_amount if agc_share_amount.exists() else None # calculate agency parent share amount agc_parent_amount = next((item for item in pos_sharing_list if item.get('default_account')), None)