Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Job, Dep Field code-prepending support #206

Merged
merged 13 commits into from
Jul 23, 2024
8 changes: 8 additions & 0 deletions apps/mappings/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

def format_attribute_name(use_code_in_naming: bool, attribute_name: str, attribute_code: str = None) -> str:
Hrishabh17 marked this conversation as resolved.
Show resolved Hide resolved
"""
Format the attribute name based on the use_code_in_naming
"""
if use_code_in_naming:
return "{} {}".format(attribute_code, attribute_name)
return attribute_name
7 changes: 3 additions & 4 deletions apps/mappings/imports/modules/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from apps.sage300.utils import SageDesktopConnector
from apps.mappings.exceptions import handle_import_exceptions
from apps.accounting_exports.models import Error

from apps.mappings.helpers import format_attribute_name

logger = logging.getLogger(__name__)
logger.level = logging.INFO
Expand Down Expand Up @@ -95,13 +95,12 @@ def remove_duplicate_attributes(self, destination_attributes: List[DestinationAt

for destination_attribute in destination_attributes:
attribute_value = destination_attribute.value
if self.use_code_in_naming:
attribute_value = '{} {}'.format(destination_attribute.code, destination_attribute.value)
attribute_value = format_attribute_name(self.use_code_in_naming, destination_attribute.value, destination_attribute.code)

if attribute_value.lower() not in attribute_values:
destination_attribute.value = attribute_value
unique_attributes.append(destination_attribute)
attribute_values.append(attribute_value.lower())
attribute_values.append(destination_attribute.value.lower())

return unique_attributes

Expand Down
20 changes: 12 additions & 8 deletions apps/sage300/dependent_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from apps.mappings.models import ImportLog
from apps.mappings.exceptions import handle_import_exceptions
from apps.workspaces.models import ImportSetting
from apps.mappings.helpers import format_attribute_name

logger = logging.getLogger(__name__)
logger.level = logging.INFO
Expand Down Expand Up @@ -78,7 +79,7 @@ def post_dependent_cost_code(import_log: ImportLog, dependent_field_setting: Dep
import_settings = ImportSetting.objects.filter(workspace_id=import_log.workspace.id).first()
use_job_code_in_naming = False
use_cost_code_in_naming = False

last_successful_run_at = datetime.now()
if 'JOB' in import_settings.import_code_fields:
use_job_code_in_naming = True
if 'COST_CODE' in import_settings.import_code_fields:
Expand Down Expand Up @@ -106,7 +107,7 @@ def post_dependent_cost_code(import_log: ImportLog, dependent_field_setting: Dep
is_errored = False

for project in projects:
project_name = "{} {}".format(project['job_code'], project['job_name']) if use_job_code_in_naming else project['job_name']
project_name = format_attribute_name(use_code_in_naming=use_job_code_in_naming, attribute_name=project['job_name'], attribute_code=project['job_code'])
projects_from_categories.append(project_name)

existing_projects_in_fyle = ExpenseAttribute.objects.filter(
Expand All @@ -122,11 +123,11 @@ def post_dependent_cost_code(import_log: ImportLog, dependent_field_setting: Dep
for project in projects:
payload = []
cost_code_names = []
project_name = "{} {}".format(project['job_code'], project['job_name']) if use_job_code_in_naming else project['job_name']
project_name = format_attribute_name(use_code_in_naming=use_job_code_in_naming, attribute_name=project['job_name'], attribute_code=project['job_code'])

for cost_code in project['cost_codes']:
if project_name in existing_projects_in_fyle:
cost_code_name = "{} {}".format(cost_code['cost_code_code'], cost_code['cost_code_name']) if use_cost_code_in_naming else cost_code['cost_code_name']
cost_code_name = format_attribute_name(use_code_in_naming=use_cost_code_in_naming, attribute_name=cost_code['cost_code_name'], attribute_code=cost_code['cost_code_code'])
payload.append({
'parent_expense_field_id': dependent_field_setting.project_field_id,
'parent_expense_field_value': project_name,
Expand All @@ -149,7 +150,8 @@ def post_dependent_cost_code(import_log: ImportLog, dependent_field_setting: Dep
import_log.status = 'PARTIALLY_FAILED' if is_errored else 'COMPLETE'
import_log.error_log = []
import_log.processed_batches_count = processed_batches
import_log.last_successful_run_at = datetime.now() if not is_errored else import_log.last_successful_run_at
if not is_errored:
import_log.last_successful_run_at = last_successful_run_at
import_log.save()

return posted_cost_codes, is_errored
Expand All @@ -160,6 +162,7 @@ def post_dependent_cost_type(import_log: ImportLog, dependent_field_setting: Dep
import_settings = ImportSetting.objects.filter(workspace_id=import_log.workspace.id).first()
use_cost_code_in_naming = False
use_category_code_in_naming = False
last_successful_run_at = datetime.now()

if 'COST_CODE' in import_settings.import_code_fields:
use_cost_code_in_naming = True
Expand Down Expand Up @@ -190,11 +193,11 @@ def post_dependent_cost_type(import_log: ImportLog, dependent_field_setting: Dep

for category in cost_categories:
if category['cost_code_name'] in posted_cost_codes:
cost_code_name = "{} {}".format(category['cost_code_code'], category['cost_code_name']) if use_cost_code_in_naming else category['cost_code_name']
cost_code_name = format_attribute_name(use_code_in_naming=use_cost_code_in_naming, attribute_name=category['cost_code_name'], attribute_code=category['cost_code_code'])
payload = []

for cost_type in category['cost_categories']:
cost_type_name = "{} {}".format(cost_type['cost_category_code'], cost_type['cost_category_name']) if use_category_code_in_naming else cost_type['cost_category_name']
cost_type_name = format_attribute_name(use_code_in_naming=use_category_code_in_naming, attribute_name=cost_type['cost_category_name'], attribute_code=cost_type['cost_category_code'])
payload.append({
'parent_expense_field_id': dependent_field_setting.cost_code_field_id,
'parent_expense_field_value': cost_code_name,
Expand All @@ -216,7 +219,8 @@ def post_dependent_cost_type(import_log: ImportLog, dependent_field_setting: Dep
import_log.status = 'PARTIALLY_FAILED' if is_errored else 'COMPLETE'
import_log.error_log = []
import_log.processed_batches_count = processed_batches
import_log.last_successful_run_at = datetime.now() if not is_errored else import_log.last_successful_run_at
if not is_errored:
import_log.last_successful_run_at = last_successful_run_at
import_log.save()

return is_errored
Expand Down
13 changes: 4 additions & 9 deletions apps/sage300/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from apps.fyle.models import DependentFieldSetting
from apps.sage300.dependent_fields import post_dependent_cost_code
from apps.mappings.models import ImportLog
from apps.mappings.helpers import format_attribute_name

logger = logging.getLogger(__name__)
logger.level = logging.INFO
Expand Down Expand Up @@ -102,9 +103,7 @@ def disable_projects(workspace_id: int, projects_to_disable: Dict):

project_values = []
for projects_map in projects_to_disable.values():
project_name = projects_map['value']
if use_code_in_naming:
project_name = "{} {}".format(projects_map['code'], project_name)
project_name = format_attribute_name(use_code_in_naming=use_code_in_naming, attribute_name=projects_map['value'], attribute_code=projects_map['code'])
project_values.append(project_name)

filters = {
Expand All @@ -117,9 +116,7 @@ def disable_projects(workspace_id: int, projects_to_disable: Dict):
# Expense attribute value map is as follows: {old_project_name: destination_id}
expense_attribute_value_map = {}
for k, v in projects_to_disable.items():
project_name = v['value']
if use_code_in_naming:
project_name = "{} {}".format(v['code'], project_name)
project_name = format_attribute_name(use_code_in_naming=use_code_in_naming, attribute_name=v['value'], attribute_code=v['code'])
expense_attribute_value_map[project_name] = k

expense_attributes = ExpenseAttribute.objects.filter(**filters)
Expand Down Expand Up @@ -173,9 +170,7 @@ def update_and_disable_cost_code(workspace_id: int, cost_codes_to_disable: Dict,
# here we are updating the CostCategory with the new project name
bulk_update_payload = []
for destination_id, value in cost_codes_to_disable.items():
updated_job_name = value['updated_value']
if use_code_in_naming:
updated_job_name = "{} {}".format(value['updated_code'], value['updated_value'])
updated_job_name = format_attribute_name(use_code_in_naming=use_code_in_naming, attribute_name=value['updated_value'], attribute_code=value['updated_code'])

cost_categories = CostCategory.objects.filter(
workspace_id=workspace_id,
Expand Down
2 changes: 1 addition & 1 deletion apps/sage300/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def _add_to_destination_attributes(self, item, attribute_type, display_name, fie
item.id,
item.is_active,
detail,
item.code
item.code if hasattr(item, 'code') else None
)

def _get_attribute_class(self, attribute_type: str):
Expand Down
Loading