def sync_tracking_categories(self): """ Get Tracking Categories """ tenant_mapping = TenantMapping.objects.get( workspace_id=self.workspace_id) self.connection.set_tenant_id(tenant_mapping.tenant_id) tracking_categories = self.connection.tracking_categories.get_all( )['TrackingCategories'] for tracking_category in tracking_categories: tracking_category_attributes = [] for option in tracking_category['Options']: tracking_category_attributes.append({ 'attribute_type': tracking_category['Name'].upper().replace(' ', '_'), 'display_name': tracking_category['Name'], 'value': option['Name'], 'destination_id': option['TrackingOptionID'] }) DestinationAttribute.bulk_create_or_update_destination_attributes( tracking_category_attributes, tracking_category['Name'].upper().replace(' ', '_'), self.workspace_id, True) return []
def sync_vendors(self): """ Get vendors """ vendors = self.connection.vendors.get() vendor_attributes = [] for vendor in vendors: detail = { 'email': vendor['PrimaryEmailAddr']['Address'] if ('PrimaryEmailAddr' in vendor and vendor['PrimaryEmailAddr'] and 'Address' in vendor['PrimaryEmailAddr'] and vendor['PrimaryEmailAddr']['Address']) else None } vendor_attributes.append({ 'attribute_type': 'VENDOR', 'display_name': 'vendor', 'value': vendor['DisplayName'], 'destination_id': vendor['Id'], 'detail': detail }) DestinationAttribute.bulk_create_or_update_destination_attributes( vendor_attributes, 'VENDOR', self.workspace_id, True) return []
def sync_customers(self): """ Get customers """ customers_count = self.connection.customers.count() if customers_count < SYNC_UPPER_LIMIT['customers']: customers = self.connection.customers.get() customer_attributes = [] for customer in customers: if customer['Active']: customer_attributes.append({ 'attribute_type': 'CUSTOMER', 'display_name': 'customer', 'value': unidecode.unidecode(u'{0}'.format( customer['FullyQualifiedName'])), 'destination_id': customer['Id'], 'active': True }) DestinationAttribute.bulk_create_or_update_destination_attributes( customer_attributes, 'CUSTOMER', self.workspace_id, True) return []
def sync_employees(self): """ Get employees """ employees = self.connection.employees.get() employee_attributes = [] for employee in employees: detail = { 'email': employee['PrimaryEmailAddr']['Address'] if ('PrimaryEmailAddr' in employee and employee['PrimaryEmailAddr'] and 'Address' in employee['PrimaryEmailAddr'] and employee['PrimaryEmailAddr']['Address']) else None } employee_attributes.append({ 'attribute_type': 'EMPLOYEE', 'display_name': 'employee', 'value': employee['DisplayName'], 'destination_id': employee['Id'], 'detail': detail }) DestinationAttribute.bulk_create_or_update_destination_attributes( employee_attributes, 'EMPLOYEE', self.workspace_id, True) return []
def sync_contacts(self): """ Get contacts """ tenant_mapping = TenantMapping.objects.get( workspace_id=self.workspace_id) self.connection.set_tenant_id(tenant_mapping.tenant_id) contacts = self.connection.contacts.get_all()['Contacts'] contact_attributes = [] for contact in contacts: detail = { 'email': contact['EmailAddress'] if 'EmailAddress' in contact else None } contact_attributes.append({ 'attribute_type': 'CONTACT', 'display_name': 'Contact', 'value': contact['Name'], 'destination_id': contact['ContactID'], 'detail': detail }) DestinationAttribute.bulk_create_or_update_destination_attributes( contact_attributes, 'CONTACT', self.workspace_id, True) return []
def sync_classes(self): """ Get classes """ classes = self.connection.classes.get() class_attributes = [] for qbo_class in classes: class_attributes.append({ 'attribute_type': 'CLASS', 'display_name': 'class', 'value': qbo_class['Name'], 'destination_id': qbo_class['Id'] }) DestinationAttribute.bulk_create_or_update_destination_attributes( class_attributes, 'CLASS', self.workspace_id, True) return []
def sync_departments(self): """ Get departments """ departments = self.connection.departments.get() department_attributes = [] for department in departments: department_attributes.append({ 'attribute_type': 'DEPARTMENT', 'display_name': 'Department', 'value': department['Name'], 'destination_id': department['Id'] }) DestinationAttribute.bulk_create_or_update_destination_attributes( department_attributes, 'DEPARTMENT', self.workspace_id, True) return []
def sync_items(self): """ Get Items """ tenant_mapping = TenantMapping.objects.get( workspace_id=self.workspace_id) self.connection.set_tenant_id(tenant_mapping.tenant_id) items = self.connection.items.get_all()['Items'] item_attributes = [] for item in items: item_attributes.append({ 'attribute_type': 'ITEM', 'display_name': 'Item', 'value': item['Code'], 'destination_id': item['ItemID'] }) DestinationAttribute.bulk_create_or_update_destination_attributes( item_attributes, 'ITEM', self.workspace_id, True) return []
def create_contact_destination_attribute(self, contact): created_contact = DestinationAttribute.create_or_update_destination_attribute( { 'attribute_type': 'CONTACT', 'display_name': 'Contact', 'value': contact['Name'], 'destination_id': contact['ContactID'], 'detail': { 'email': contact['EmailAddress'] } }, self.workspace_id) return created_contact
def create_vendor_destionation_attribute(self, vendor): created_vendor = DestinationAttribute.create_or_update_destination_attribute( { 'attribute_type': 'VENDOR', 'display_name': 'vendor', 'value': vendor['DisplayName'], 'destination_id': vendor['Id'], 'detail': { 'email': vendor['PrimaryEmailAddr']['Address'] if 'PrimaryEmailAddr' in vendor else None } }, self.workspace_id) return created_vendor
def sync_vendors(self): """ Get vendors """ vendors = self.connection.vendors.get() vendor_attributes = [] for vendor in vendors: vendor_attributes.append({ 'attribute_type': 'VENDOR', 'display_name': 'vendor', 'value': vendor['DisplayName'], 'destination_id': vendor['Id'] }) account_attributes = DestinationAttribute.bulk_upsert_destination_attributes( vendor_attributes, self.workspace_id) return account_attributes
def sync_accounts(self): """ Get accounts """ accounts = self.connection.accounts.get_all() account_attributes = [] for account in accounts: account_attributes.append({ 'attribute_type': 'ACCOUNT', 'display_name': 'account', 'value': account['TITLE'], 'destination_id': account['ACCOUNTNO'] }) account_attributes = DestinationAttribute.bulk_upsert_destination_attributes( account_attributes, self.workspace_id) return account_attributes
def sync_customers(self): """ Get customers """ customers = self.connection.customers.get() customer_attributes = [] for customer in customers: customer_attributes.append({ 'attribute_type': 'CUSTOMER', 'display_name': 'customer', 'value': customer['DisplayName'], 'destination_id': customer['Id'] }) account_attributes = DestinationAttribute.bulk_upsert_destination_attributes( customer_attributes, self.workspace_id) return account_attributes
def sync_classes(self): """ Get classes """ classes = self.connection.classes.get() class_attributes = [] for qbo_class in classes: class_attributes.append({ 'attribute_type': 'CLASS', 'display_name': 'class', 'value': qbo_class['Name'], 'destination_id': qbo_class['Id'] }) account_attributes = DestinationAttribute.bulk_upsert_destination_attributes( class_attributes, self.workspace_id) return account_attributes
def sync_employees(self): """ Get employees """ employees = self.connection.employees.get() employee_attributes = [] for employee in employees: employee_attributes.append({ 'attribute_type': 'EMPLOYEE', 'display_name': 'employee', 'value': employee['DisplayName'], 'destination_id': employee['Id'] }) account_attributes = DestinationAttribute.bulk_upsert_destination_attributes( employee_attributes, self.workspace_id) return account_attributes
def sync_projects(self): """ Get projects """ projects = self.connection.projects.get_all() project_attributes = [] for project in projects: project_attributes.append({ 'attribute_type': 'PROJECT', 'display_name': 'project', 'value': project['NAME'], 'destination_id': project['PROJECTID'] }) account_attributes = DestinationAttribute.bulk_upsert_destination_attributes( project_attributes, self.workspace_id) return account_attributes
def sync_expense_types(self): """ Get expense types """ expense_types = self.connection.expense_types.get_all() expense_types_attributes = [] for expense_type in expense_types: expense_types_attributes.append({ 'attribute_type': 'EXPENSE_TYPE', 'display_name': 'Expense Types', 'value': expense_type['DESCRIPTION'], 'destination_id': expense_type['ACCOUNTLABEL'] }) account_attributes = DestinationAttribute.bulk_upsert_destination_attributes( expense_types_attributes, self.workspace_id) return account_attributes
def sync_departments(self): """ Get departments """ departments = self.connection.departments.get_all() department_attributes = [] for department in departments: department_attributes.append({ 'attribute_type': 'DEPARTMENT', 'display_name': 'department', 'value': department['TITLE'], 'destination_id': department['DEPARTMENTID'] }) account_attributes = DestinationAttribute.bulk_upsert_destination_attributes( department_attributes, self.workspace_id) return account_attributes
def sync_tenants(self): """ sync xero tenants """ tenants = self.connection.tenants.get_all() tenant_attributes = [] for tenant in tenants: tenant_attributes.append({ 'attribute_type': 'TENANT', 'display_name': 'Tenant', 'value': tenant['tenantName'], 'destination_id': tenant['tenantId'] }) tenant_attributes = DestinationAttribute.bulk_create_or_update_destination_attributes( tenant_attributes, 'TENANT', self.workspace_id, True) return tenant_attributes
def sync_locations(self): """ Get locations """ locations = self.connection.locations.get_all() location_attributes = [] for location in locations: location_attributes.append({ 'attribute_type': 'LOCATION', 'display_name': 'location', 'value': location['NAME'], 'destination_id': location['LOCATIONID'] }) account_attributes = DestinationAttribute.bulk_upsert_destination_attributes( location_attributes, self.workspace_id) return account_attributes
def sync_accounts(self, account_type: str): """ Get accounts """ accounts = self.connection.accounts.get() accounts = list( filter( lambda current_account: current_account['AccountType'] == account_type, accounts)) account_attributes = [] if account_type == 'Expense': attribute_type = 'ACCOUNT' display_name = 'Account' elif account_type == 'Credit Card': attribute_type = 'CREDIT_CARD_ACCOUNT' display_name = 'Credit Card Account' elif account_type == 'Bank': attribute_type = 'BANK_ACCOUNT' display_name = 'Bank Account' else: attribute_type = 'ACCOUNTS_PAYABLE' display_name = 'Accounts Payable' for account in accounts: account_attributes.append({ 'attribute_type': attribute_type, 'display_name': display_name, 'value': account['Name'], 'destination_id': account['Id'] }) account_attributes = DestinationAttribute.bulk_upsert_destination_attributes( account_attributes, self.workspace_id) return account_attributes
def sync_accounts(self): """ Get accounts """ accounts = self.connection.accounts.get() category_sync_version = 'v2' general_settings = WorkspaceGeneralSettings.objects.filter( workspace_id=self.workspace_id).first() if general_settings: category_sync_version = general_settings.category_sync_version account_attributes = { 'account': [], 'credit_card_account': [], 'bank_account': [], 'accounts_payable': [] } for account in accounts: if account['AccountType'] == 'Expense': account_attributes['account'].append({ 'attribute_type': 'ACCOUNT', 'display_name': 'Account', 'value': unidecode.unidecode(u'{0}'.format( account['Name'] if category_sync_version == 'v1' else account['FullyQualifiedName'])).replace( '/', '-'), 'destination_id': account['Id'], 'active': account['Active'], 'detail': { 'fully_qualified_name': account['FullyQualifiedName'] } }) elif account['AccountType'] == 'Credit Card': account_attributes['credit_card_account'].append({ 'attribute_type': 'CREDIT_CARD_ACCOUNT', 'display_name': 'Credit Card Account', 'value': unidecode.unidecode(u'{0}'.format( account['Name'] if category_sync_version == 'v1' else account['FullyQualifiedName'])), 'destination_id': account['Id'], 'active': account['Active'], 'detail': { 'fully_qualified_name': account['FullyQualifiedName'] } }) elif account['AccountType'] == 'Bank': account_attributes['bank_account'].append({ 'attribute_type': 'BANK_ACCOUNT', 'display_name': 'Bank Account', 'value': unidecode.unidecode(u'{0}'.format( account['Name'] if category_sync_version == 'v1' else account['FullyQualifiedName'])), 'destination_id': account['Id'], 'active': account['Active'], 'detail': { 'fully_qualified_name': account['FullyQualifiedName'] } }) else: account_attributes['accounts_payable'].append({ 'attribute_type': 'ACCOUNTS_PAYABLE', 'display_name': 'Accounts Payable', 'value': unidecode.unidecode(u'{0}'.format( account['Name'] if category_sync_version == 'v1' else account['FullyQualifiedName'])), 'destination_id': account['Id'], 'active': account['Active'], 'detail': { 'fully_qualified_name': account['FullyQualifiedName'] } }) for attribute_type, attribute in account_attributes.items(): if attribute: DestinationAttribute.bulk_create_or_update_destination_attributes( attribute, attribute_type.upper(), self.workspace_id, True) return []
def sync_accounts(self): """ Get accounts """ tenant_mapping = TenantMapping.objects.get( workspace_id=self.workspace_id) self.connection.set_tenant_id(tenant_mapping.tenant_id) accounts = self.connection.accounts.get_all()['Accounts'] account_attributes = {'bank_account': [], 'account': []} for account in accounts: detail = { 'account_name': account['Name'], 'account_type': account['Type'], } if account['Type'] == 'BANK': account_attributes['bank_account'].append({ 'attribute_type': 'BANK_ACCOUNT', 'display_name': 'Bank Account', 'value': unidecode.unidecode(u'{0}'.format( account['Name'])).replace('/', '-'), 'destination_id': account['AccountID'], 'active': True if account['Status'] == 'ACTIVE' else False, 'detail': detail }) elif account['Class'] == 'EXPENSE': account_attributes['account'].append({ 'attribute_type': 'ACCOUNT', 'display_name': 'Account', 'value': unidecode.unidecode(u'{0}'.format( account['Name'])).replace('/', '-'), 'destination_id': account['Code'], 'active': True if account['Status'] == 'ACTIVE' else False, 'detail': detail }) for attribute_type, account_attribute in account_attributes.items(): if account_attribute: DestinationAttribute.bulk_create_or_update_destination_attributes( account_attribute, attribute_type.upper(), self.workspace_id, True) return []