Skip to content

Commit

Permalink
export item to sage intacct (#284)
Browse files Browse the repository at this point in the history
* export item to sage intacct
  • Loading branch information
DhaaraniCIT authored May 24, 2023
1 parent e6d0b6f commit ebd8fd6
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 12 deletions.
1 change: 0 additions & 1 deletion apps/fyle/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def create_expense_groups(workspace_id: int, fund_source: List[str], task_log: T
"""
try:
with transaction.atomic():

workspace = Workspace.objects.get(pk=workspace_id)

last_synced_at = workspace.last_synced_at
Expand Down
2 changes: 1 addition & 1 deletion apps/fyle/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class FyleFieldsView(generics.ListAPIView):
serializer_class = ExpenseFieldSerializer

def get(self, request, *args, **kwargs):
default_attributes = ['EMPLOYEE', 'CATEGORY', 'PROJECT', 'COST_CENTER', 'LOCATION_ENTITY', 'TAX_GROUP', 'CORPORATE_CARD']
default_attributes = ['EMPLOYEE', 'CATEGORY', 'PROJECT', 'COST_CENTER', 'LOCATION_ENTITY', 'TAX_GROUP', 'CORPORATE_CARD', 'MERCHANT']

attributes = ExpenseAttribute.objects.filter(
~Q(attribute_type__in=default_attributes),
Expand Down
30 changes: 23 additions & 7 deletions apps/sage_intacct/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,30 @@ def get_customer_id_or_none(expense_group: ExpenseGroup, lineitem: Expense, gene
def get_item_id_or_none(expense_group: ExpenseGroup, lineitem: Expense, general_mappings: GeneralMapping):
item_id = None

if lineitem.billable:
project_setting: MappingSetting = MappingSetting.objects.filter(
workspace_id=expense_group.workspace_id,
destination_field='PROJECT'
).first()
if project_setting:
item_id = general_mappings.default_item_id if general_mappings.default_item_id else None
item_setting: MappingSetting = MappingSetting.objects.filter(
workspace_id=expense_group.workspace_id,
destination_field='ITEM'
).first()

if item_setting:
if item_setting.source_field == 'PROJECT':
source_value = lineitem.project
elif item_setting.source_field == 'COST_CENTER':
source_value = lineitem.cost_center
else:
attribute = ExpenseAttribute.objects.filter(attribute_type=item_setting.source_field).first()
source_value = lineitem.custom_properties.get(attribute.display_name, None)

mapping: Mapping = Mapping.objects.filter(
source_type=item_setting.source_field,
destination_type='ITEM',
source__value=source_value,
workspace_id=expense_group.workspace_id
).first()
if mapping:
item_id = mapping.destination.destination_id
if item_id is None:
item_id = general_mappings.default_item_id if general_mappings.default_item_id else None
return item_id


Expand Down
3 changes: 1 addition & 2 deletions apps/sage_intacct/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ class TriggerExportsView(generics.GenericAPIView):
def post(self, request, *args, **kwargs):
expense_group_ids = request.data.get('expense_group_ids', [])
export_type = request.data.get('export_type')

if export_type == 'BILL':
schedule_bills_creation(kwargs['workspace_id'], expense_group_ids)
elif export_type == 'CHARGE_CARD_TRANSACTION':
Expand Down Expand Up @@ -128,7 +127,7 @@ def get_queryset(self):
attributes = DestinationAttribute.objects.filter(
~Q(attribute_type='EMPLOYEE') & ~Q(attribute_type='VENDOR') & ~Q(attribute_type='CHARGE_CARD_NUMBER') &
~Q(attribute_type='EXPENSE_TYPE') & ~Q(attribute_type='ACCOUNT') & ~Q(attribute_type='CCC_ACCOUNT'),
~Q(attribute_type='PAYMENT_ACCOUNT'), ~Q(attribute_type='EXPENSE_PAYMENT_TYPE'), ~Q(attribute_type='ITEM'),
~Q(attribute_type='PAYMENT_ACCOUNT'), ~Q(attribute_type='EXPENSE_PAYMENT_TYPE'),
~Q(attribute_type='LOCATION_ENTITY'), ~Q(attribute_type='TAX_DETAIL'),
workspace_id=self.kwargs['workspace_id']
).values('attribute_type', 'display_name').distinct()
Expand Down
125 changes: 124 additions & 1 deletion tests/test_sageintacct/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_create_bill(db):

for bill_lineitem in bill_lineitems:
assert bill_lineitem.amount == 21.0
assert bill_lineitem.billable == False
assert bill_lineitem.billable == None

assert bill.currency == 'USD'
assert bill.transaction_date.split('T')[0] == datetime.now().strftime('%Y-%m-%d')
Expand Down Expand Up @@ -608,6 +608,129 @@ def test_get_memo(db):

get_memo(expense_group, Bill, workspace_id)

def test_get_item_id_or_none(db, mocker):
workspace_id = 1

general_mappings = GeneralMapping.objects.get(workspace_id=workspace_id)

expense_group = ExpenseGroup.objects.get(id=1)

expense = expense_group.expenses.first()

general_mappings.default_item_id = None
general_mappings.save()

item_id = get_item_id_or_none(expense_group, expense, general_mappings)

assert item_id == None

general_mappings.default_item_id = '1234'
general_mappings.save()

item_id = get_item_id_or_none(expense_group, expense, general_mappings)

assert item_id == general_mappings.default_item_id

item_setting: MappingSetting = MappingSetting.objects.filter(workspace_id=workspace_id).first()
item_setting.source_field='PROJECT'
item_setting.destination_field='ITEM'
item_setting.save()

item_id = get_item_id_or_none(expense_group, expense, general_mappings)

assert item_id == general_mappings.default_item_id

expense_attribute = ExpenseAttribute.objects.filter(
attribute_type = 'PROJECT',
value = 'Aaron Abbott'
).first()

mapping = Mapping.objects.first()
mapping.destination_type = 'ITEM'
mapping.source_type = 'PROJECT'
mapping.source=expense_attribute
mapping.workspace_id=general_mappings.workspace
mapping.save()

source_value = expense.project

mapping: Mapping = Mapping.objects.filter(
source_type=item_setting.source_field,
destination_type='ITEM',
source__value=source_value,
workspace_id=expense_group.workspace_id
).first()

item_id = get_item_id_or_none(expense_group, expense, general_mappings)

assert item_id == mapping.destination.destination_id

item_setting: MappingSetting = MappingSetting.objects.filter(workspace_id=workspace_id).first()
item_setting.source_field='COST_CENTER'
item_setting.destination_field='ITEM'
item_setting.save()

expense_attribute = ExpenseAttribute.objects.filter(
attribute_type = 'COST_CENTER'
).first()

mapping = Mapping.objects.first()
mapping.destination_type = 'ITEM'
mapping.source_type = 'COST_CENTER'
mapping.source=expense_attribute
mapping.workspace_id=general_mappings.workspace
mapping.save()

expense.cost_center = expense_attribute.value
expense.save()

source_value = expense.cost_center

mapping: Mapping = Mapping.objects.filter(
source_type=item_setting.source_field,
destination_type='ITEM',
source__value=source_value,
workspace_id=expense_group.workspace_id
).first()

item_id = get_item_id_or_none(expense_group, expense, general_mappings)

assert item_id == mapping.destination.destination_id

item_setting: MappingSetting = MappingSetting.objects.filter(workspace_id=workspace_id).first()
item_setting.source_field='EMPLOYEE'
item_setting.destination_field='ITEM'
item_setting.save()

expense_attribute = ExpenseAttribute.objects.first()
expense_attribute.attribute_type = 'EMPLOYEE'
expense_attribute.display_name = 'Employee'
expense_attribute.value = 'Los'
expense_attribute.save()

mapping = Mapping.objects.first()
mapping.destination_type = 'ITEM'
mapping.source_type = 'EMPLOYEE'
mapping.source=expense_attribute
mapping.workspace_id=general_mappings.workspace
mapping.save()

expense.custom_properties[expense_attribute.display_name] = expense_attribute.value
expense.save()

source_value = expense.custom_properties.get(expense_attribute.display_name, None)

mapping: Mapping = Mapping.objects.filter(
source_type=item_setting.source_field,
destination_type='ITEM',
source__value=source_value,
workspace_id=expense_group.workspace_id
).first()

item_id = get_item_id_or_none(expense_group, expense, general_mappings)

assert item_id == mapping.destination.destination_id

def test_get_ccc_account_id(db, mocker):
workspace_id = 1

Expand Down

0 comments on commit ebd8fd6

Please sign in to comment.