Skip to content
Snippets Groups Projects
Commit 5adbd4f1 authored by Hassan Syyid's avatar Hassan Syyid
Browse files

Merge branch 'feature/hgi-7264' into 'main'

HGI-7264 Add customFields to invoices line items

See merge request !25
parents b6a12ca0 bcd6ef7d
No related branches found
No related tags found
1 merge request!25HGI-7264 Add customFields to invoices line items
......@@ -36,32 +36,77 @@ class UnifiedMapping:
return {key: payload}
return payload
def prepare_trackings_using_custom_fields(self, customFields):
"""
Prepares Xero-compatible Tracking Categories from customFields.
Args:
customFields (list): A list of custom fields to be mapped to Xero tracking categories.
Returns:
list: A list of tracking categories formatted for Xero.
"""
# Ensure customFields is a list, return empty list if not
if not isinstance(customFields, list):
return []
# Using list comprehension for cleaner and more Pythonic mapping
return [
{"Name": field.get("name"), "Option": field.get("value")}
for field in customFields
if field.get("name") and field.get("value")
]
def map_xero_list(
self, addresses_rows, address_mapping, payload, type="addresses", target="xero"
self, rows, mapping, payload, type="addresses", target="xero"
):
if isinstance(addresses_rows, str):
addresses_rows = json.loads(addresses_rows)
if isinstance(addresses_rows, list):
address_list = []
for addresses in addresses_rows:
address = {}
for key in address_mapping.keys():
if key in addresses:
address[address_mapping[key]] = addresses[key]
address = self.filter_ignore_keys(address)
address_list.append(address)
payload[type] = address_list
else:
address = {}
for key in address_mapping.keys():
if key in addresses_rows:
address[address_mapping[key]] = addresses_rows[key]
"""
Maps a list of records to Xero-compatible structures.
Args:
rows (list or str): Input data rows to be mapped.
mapping (dict): Mapping configuration for the target type.
payload (dict): The output payload to be populated.
type (str): The type of items being mapped (e.g., 'addresses', 'LineItems').
target (str): The target system, default is 'xero'.
Returns:
dict: The updated payload with mapped items.
"""
# Convert string to JSON if necessary
if isinstance(rows, str):
rows = json.loads(rows)
# If rows is a list, process each item in the list
if isinstance(rows, list):
items = []
for row in rows:
record = {}
for key in mapping.keys():
if key not in row:
continue
if type == 'LineItems' and key == 'customFields':
record['Tracking'] = self.prepare_trackings_using_custom_fields(row[key])
else:
record[mapping[key]] = row[key]
record = self.filter_ignore_keys(record)
items.append(record)
payload[type] = items
# If rows is a single dictionary, process it accordingly
elif isinstance(rows, dict):
record = {
mapping[key]: rows[key]
for key in mapping
if key in rows
}
if type == "Addresses":
address = self.filter_ignore_keys(address)
payload[type] = [address]
payload[type] = [self.filter_ignore_keys(record)]
else:
payload[type] = address
payload[type] = self.filter_ignore_keys(record)
return payload
# Modify this function and use recursion to support nested mapping
......@@ -151,11 +196,5 @@ class UnifiedMapping:
return payload
def filter_ignore_keys(self, payload, target="xero"):
payload_return = {}
mapping = self.read_json_file(f"mapping_{target}.json")
ignore = mapping["ignore"]
# filter ignored keys
for key in payload.keys():
if key not in ignore:
payload_return[key] = payload[key]
return payload_return
ignore_keys = self.read_json_file(f"mapping_{target}.json").get("ignore", [])
return {key: value for key, value in payload.items() if key not in ignore_keys}
......@@ -54,7 +54,11 @@
"discount": "DiscountRate",
"discountAmount": "DiscountAmount",
"classId": "classId",
"className": "className"
"className": "className",
"customFields": {
"name": "TrackingCategoryName",
"value": "TrackingCategoryValue"
}
},
"address": {
"line1": "AddressLine1",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment