-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into xero_merchants
- Loading branch information
Showing
9 changed files
with
362 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from fyle_accounting_mappings.models import ExpenseAttribute | ||
from django.db.models import Q | ||
|
||
from apps.tasks.models import TaskLog | ||
from apps.workspaces.models import FyleCredential, WorkspaceGeneralSettings, Workspace | ||
from apps.workspaces.serializers import WorkspaceSerializer | ||
from datetime import datetime, timezone | ||
from .models import Expense, ExpenseGroup, ExpenseGroupSettings | ||
|
||
from fyle_integrations_platform_connector import PlatformConnector | ||
|
||
def get_expense_field(workspace_id): | ||
|
||
default_attributes = ['EMPLOYEE', 'CATEGORY', 'PROJECT', 'COST_CENTER', 'CORPORATE_CARD', 'TAX_GROUP'] | ||
attributes = ExpenseAttribute.objects.filter( | ||
~Q(attribute_type__in=default_attributes), | ||
workspace_id=workspace_id | ||
).values('attribute_type', 'display_name').distinct() | ||
|
||
expense_fields= [ | ||
{'attribute_type': 'COST_CENTER', 'display_name': 'Cost Center'}, | ||
{'attribute_type': 'PROJECT', 'display_name': 'Project'} | ||
] | ||
|
||
for attribute in attributes: | ||
expense_fields.append(attribute) | ||
|
||
return expense_fields | ||
|
||
|
||
def sync_fyle_dimension(workspace_id): | ||
|
||
workspace = Workspace.objects.get(id=workspace_id) | ||
if workspace.source_synced_at: | ||
time_interval = datetime.now(timezone.utc) - workspace.source_synced_at | ||
|
||
if workspace.source_synced_at is None or time_interval.days > 0: | ||
fyle_credentials = FyleCredential.objects.get(workspace_id=workspace_id) | ||
|
||
platform = PlatformConnector(fyle_credentials) | ||
platform.import_fyle_dimensions() | ||
|
||
workspace.source_synced_at = datetime.now() | ||
workspace.save(update_fields=['source_synced_at']) | ||
|
||
|
||
def refresh_fyle_dimension(workspace_id): | ||
|
||
fyle_credentials = FyleCredential.objects.get(workspace_id=workspace_id) | ||
|
||
platform = PlatformConnector(fyle_credentials) | ||
platform.import_fyle_dimensions() | ||
|
||
workspace = Workspace.objects.get(id=workspace_id) | ||
workspace.source_synced_at = datetime.now() | ||
workspace.save(update_fields=['source_synced_at']) | ||
|
||
|
||
def exportable_expense_group(workspace_id): | ||
|
||
configuration = WorkspaceGeneralSettings.objects.get(workspace_id=workspace_id) | ||
fund_source = [] | ||
|
||
if configuration.reimbursable_expenses_object: | ||
fund_source.append('PERSONAL') | ||
if configuration.corporate_credit_card_expenses_object: | ||
fund_source.append('CCC') | ||
|
||
expense_group_ids = ExpenseGroup.objects.filter( | ||
workspace_id=workspace_id, | ||
exported_at__isnull=True, | ||
fund_source__in=fund_source | ||
).values_list('id', flat=True) | ||
|
||
return expense_group_ids |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import logging | ||
from .serializers import TenantMappingSerializer, GeneralMappingSerializer | ||
from .models import TenantMapping, GeneralMapping | ||
from apps.workspaces.models import XeroCredentials | ||
from apps.xero.utils import XeroConnector | ||
from apps.workspaces.models import Workspace | ||
from ..workspaces.models import WorkspaceGeneralSettings | ||
|
||
from django_q.tasks import Chain | ||
|
||
from xerosdk.exceptions import UnsuccessfulAuthentication | ||
|
||
from .utils import MappingUtils | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
def tenant_mapping_view(workspace_id, tenant_mapping_payload): | ||
|
||
mapping_utils = MappingUtils(workspace_id) | ||
tenant_mapping_object = mapping_utils.create_or_update_tenant_mapping(tenant_mapping_payload) | ||
xero_credentials = XeroCredentials.objects.filter(workspace_id=workspace_id).first() | ||
workspace = Workspace.objects.filter(id=workspace_id).first() | ||
|
||
try: | ||
xero_connector = XeroConnector(xero_credentials, workspace_id=workspace_id) | ||
tenant_mapping = TenantMapping.objects.filter(workspace_id=workspace_id).first() | ||
company_info = xero_connector.get_organisations()[0] | ||
workspace.xero_currency = company_info['BaseCurrency'] | ||
workspace.save() | ||
xero_credentials.country = company_info['CountryCode'] | ||
xero_credentials.save() | ||
|
||
if tenant_mapping and not tenant_mapping.connection_id: | ||
connections = xero_connector.connection.connections.get_all() | ||
connection = list(filter(lambda connection: connection['tenantId'] == tenant_mapping.tenant_id, connections)) | ||
|
||
if connection: | ||
tenant_mapping.connection_id = connection[0]['id'] | ||
tenant_mapping.save() | ||
|
||
except UnsuccessfulAuthentication: | ||
logger.info('Xero refresh token is invalid for workspace_id - %s', workspace_id) | ||
|
||
except Exception: | ||
logger.info('Error while fetching company information') | ||
|
||
return tenant_mapping_object |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.