-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcost_centers.py
147 lines (125 loc) · 5.44 KB
/
cost_centers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import logging
from datetime import datetime
from fyle_integrations_platform_connector import PlatformConnector
from fyle_accounting_mappings.models import (
DestinationAttribute,
ExpenseAttribute,
MappingSetting
)
from apps.mappings.imports.modules.base import Base
from apps.workspaces.models import Configuration, FyleCredential
from apps.mappings.helpers import prepend_code_to_name
logger = logging.getLogger(__name__)
logger.level = logging.INFO
class CostCenter(Base):
"""
Class for CostCenter module
"""
def __init__(self, workspace_id: int, destination_field: str, sync_after: datetime, prepend_code_to_name: bool = False):
super().__init__(
workspace_id=workspace_id,
source_field='COST_CENTER',
destination_field=destination_field,
platform_class_name='cost_centers',
sync_after=sync_after,
prepend_code_to_name=prepend_code_to_name
)
def trigger_import(self) -> None:
"""
Trigger import for CostCenter module
"""
self.check_import_log_and_start_import()
# remove the is_auto_sync_status_allowed parameter
def construct_fyle_payload(
self,
paginated_destination_attributes: list[DestinationAttribute],
existing_fyle_attributes_map: object,
is_auto_sync_status_allowed: bool
) -> list:
"""
Construct Fyle payload for CostCenter module
:param paginated_destination_attributes: List of paginated destination attributes
:param existing_fyle_attributes_map: Existing Fyle attributes map
:param is_auto_sync_status_allowed: Is auto sync status allowed
:return: Fyle payload
"""
payload = []
for attribute in paginated_destination_attributes:
cost_center = {
'name': attribute.value,
'is_enabled': True if attribute.active is None else attribute.active,
'description': 'Cost Center - {0}, Id - {1}'.format(
attribute.value,
attribute.destination_id
)
}
# Create a new cost-center if it does not exist in Fyle
if attribute.value.lower() not in existing_fyle_attributes_map:
payload.append(cost_center)
return payload
def disable_cost_centers(
workspace_id: int,
cost_centers_to_disable: dict,
is_import_to_fyle_enabled: bool = False,
*args,
**kwargs
) -> list:
"""
cost_centers_to_disable object format:
{
'destination_id': {
'value': 'old_cost_center_name',
'updated_value': 'new_cost_center_name',
'code': 'old_code',
'updated_code': 'new_code' ---- if the code is updated else same as code
}
}
"""
if not is_import_to_fyle_enabled or len(cost_centers_to_disable) == 0:
logger.info("Skipping disabling cost centers in Fyle | WORKSPACE_ID: %s", workspace_id)
return
destination_type = MappingSetting.objects.get(workspace_id=workspace_id, source_field='COST_CENTER').destination_field
use_code_in_naming = Configuration.objects.filter(workspace_id=workspace_id, import_code_fields__contains=[destination_type]).first()
fyle_credentials = FyleCredential.objects.get(workspace_id=workspace_id)
platform = PlatformConnector(fyle_credentials=fyle_credentials)
cost_center_values = []
for cost_center_map in cost_centers_to_disable.values():
if not use_code_in_naming and cost_center_map['value'] == cost_center_map['updated_value']:
continue
elif use_code_in_naming and (cost_center_map['value'] == cost_center_map['updated_value'] and cost_center_map['code'] == cost_center_map['updated_code']):
continue
cost_center_name = prepend_code_to_name(prepend_code_in_name=use_code_in_naming, value=cost_center_map['value'], code=cost_center_map['code'])
cost_center_values.append(cost_center_name)
filters = {
'workspace_id': workspace_id,
'attribute_type': 'COST_CENTER',
'value__in': cost_center_values,
'active': True
}
expense_attribute_value_map = {}
for destination_id, v in cost_centers_to_disable.items():
cost_center_name = prepend_code_to_name(prepend_code_in_name=use_code_in_naming, value=v['value'], code=v['code'])
expense_attribute_value_map[cost_center_name] = destination_id
expense_attributes = ExpenseAttribute.objects.filter(**filters)
bulk_payload = []
for expense_attribute in expense_attributes:
code = expense_attribute_value_map.get(expense_attribute.value, None)
if code:
payload = {
'name': expense_attribute.value,
'is_enabled': False,
'id': expense_attribute.source_id,
'description': 'Cost Center - {0}, Id - {1}'.format(
expense_attribute.value,
code
)
}
bulk_payload.append(payload)
else:
logger.error(f"Cost Center with value {expense_attribute.value} not found | WORKSPACE_ID: {workspace_id}")
if bulk_payload:
logger.info(f"Disabling Cost Center in Fyle | WORKSPACE_ID: {workspace_id} | COUNT: {len(bulk_payload)}")
platform.cost_centers.post_bulk(bulk_payload)
else:
logger.info(f"No Cost Center to Disable in Fyle | WORKSPACE_ID: {workspace_id}")
return bulk_payload