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

PR 2: add support for cost codes and categories #59

Merged
merged 10 commits into from
Nov 2, 2023
2 changes: 1 addition & 1 deletion apps/sage300/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def sync_dimensions(sage300_credential: Sage300Credential, workspace_id: int) ->
sage300_connection = import_string('apps.sage300.utils.SageDesktopConnector')(sage300_credential, workspace_id)

# List of dimensions to sync
dimensions = ['accounts', 'vendors', 'commitments', 'jobs', 'categories', 'cost_codes']
dimensions = ['accounts', 'vendors', 'commitments', 'jobs', 'standard_categories', 'standard_cost_codes']

for dimension in dimensions:
try:
Expand Down
21 changes: 15 additions & 6 deletions apps/sage300/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,22 @@ def sync_jobs(self):
self._sync_data(jobs, 'JOB', 'job', self.workspace_id, field_names)
return []

def sync_cost_codes(self):
def sync_standard_cost_codes(self):
"""
Synchronize cost codes from Sage Desktop SDK to your application
Synchronize standard cost codes from Sage Desktop SDK to your application
"""
cost_codes = self.connection.jobs.get_all_costcodes()
field_names = ['code', 'version', 'is_standard', 'description']
self._sync_data(cost_codes, 'COST_CODE', 'cost_code', self.workspace_id, field_names)
self._sync_data(cost_codes, 'STANDARD_COST_CODE', 'standard_cost_code', self.workspace_id, field_names)
return []

def sync_categories(self):
def sync_standard_categories(self):
"""
Synchronize categories from Sage Desktop SDK to your application
Synchronize standard categories from Sage Desktop SDK to your application
"""
categories = self.connection.jobs.get_all_categories()
field_names = ['code', 'version', 'description', 'accumulation_name']
self._sync_data(categories, 'CATEGORY', 'category', self.workspace_id, field_names)
self._sync_data(categories, 'STANDARD_CATEGORY', 'standard_category', self.workspace_id, field_names)
return []

def sync_commitments(self):
Expand All @@ -131,3 +131,12 @@ def sync_commitments(self):
]
self._sync_data(commitments, 'COMMITMENT', 'commitment', self.workspace_id, field_names)
return []

def sync_cost_codes(self):
"""
Synchronize cost codes from Sage Desktop SDK to your application
"""
cost_codes = self.connection.jobs.get_all_costcodes()
field_names = ['code', 'version', 'job_id', 'standard_costcode_id']
self._sync_data(cost_codes, 'COST_CODE', 'cost_code', self.workspace_id, field_names)
return []
6 changes: 5 additions & 1 deletion sage_desktop_sdk/apis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
from .commitments import Commitments
from .documents import Documents
from .operation_status import OperationStatus
from .categories import Categories
from .cost_codes import CostCodes

__all__ = [
'Accounts',
'Vendors',
'Jobs',
'Commitments',
'Documents',
'OperationStatus'
'OperationStatus',
'Categories',
'CostCodes'
]
2 changes: 1 addition & 1 deletion sage_desktop_sdk/apis/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ def get_all(self):
Get all Attachables
:return: List of Dicts in Attachable Schema
"""
accounts = self._query_get_all(Accounts.GET_ACCOUNTS)
accounts = self._query_get_all(Accounts.GET_ACCOUNTS)
for account in accounts:
yield Account.from_dict(account)
21 changes: 21 additions & 0 deletions sage_desktop_sdk/apis/categories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Sage Desktop Categories
"""
from sage_desktop_sdk.core.client import Client
from sage_desktop_sdk.core.schema.read_only import Category


class Categories(Client):
"""Class for Jobs APIs."""

GET_CATEGORIES = '/JobCosting/Api/V1/JobCost.svc/jobs/categories'

def get_all_categories(self):
"""
Get all Jobs
:return: List of Dicts in Jobs Schema
"""
categories = self._query_get_all(Categories.GET_CATEGORIES)
for category in categories:
print('catefor', category)
yield Category.from_dict(category)
3 changes: 1 addition & 2 deletions sage_desktop_sdk/apis/commitments.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from sage_desktop_sdk.core.client import Client
from sage_desktop_sdk.core.schema.read_only import Commitment


class Commitments(Client):
"""Class for Documents APIs."""

GET_COMMITMENT_ITEMS = '/JobCosting/Api/V1/Commitment.svc/commitments/items/synchronize?commitment={}'
GET_COMMITMENTS = '/JobCosting/Api/V1/Commitment.svc/commitments'


def get_all(self):
"""
Get all Vendors
Expand All @@ -21,7 +21,6 @@ def get_all(self):
for commitment in commitments:
yield Commitment.from_dict(commitment)


def get_commitment_items(self, commitment_id: str):
"""
Get Commitment By Id
Expand Down
20 changes: 20 additions & 0 deletions sage_desktop_sdk/apis/cost_codes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
Sage Desktop Cost Codes
"""
from sage_desktop_sdk.core.client import Client
from sage_desktop_sdk.core.schema.read_only import CostCode


class CostCodes(Client):
"""Class for Cost Code APIs."""

GET_COST_CODE = '/JobCosting/Api/V1/JobCost.svc/jobs/costcodes'

def get_all_costcodes(self):
"""
Get all Cost Code
:return: List of Dicts in Cost Code Schema
"""
cost_codes = self._query_get_all(CostCodes.GET_COST_CODE)
for cost_code in cost_codes:
yield CostCode.from_dict(cost_code)
4 changes: 1 addition & 3 deletions sage_desktop_sdk/apis/direct_costs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
"""
import json
from sage_desktop_sdk.core.client import Client
from sage_desktop_sdk.core.schema.write_only import DocumentPostPayload


class DirectCost(Client):
"""Class for Direct Cost APIs."""

POST_DIRECT_COST= '/JobCosting/Api/V1/JobTransaction.svc/transactions/direct-costs'

POST_DIRECT_COST = '/JobCosting/Api/V1/JobTransaction.svc/transactions/direct-costs'

def post_document(self, data: dict):
"""
Expand Down
12 changes: 4 additions & 8 deletions sage_desktop_sdk/apis/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""
import json
from sage_desktop_sdk.core.client import Client
from sage_desktop_sdk.core.schema.write_only import DocumentPostPayload


class Documents(Client):
Expand All @@ -13,25 +12,22 @@ class Documents(Client):
POST_DOCUMENT = '/documentmanagement/Api/V1/Document.svc/document'
POST_DOCUMENT_EXPORT = '/DocumentManagement/Api/V1/Document.svc/document/actions/export?document={}'


def get(self, document_id: str):
"""
Get all Vendors
:return: List of Dicts in Vendors Schema
"""
return self._query_get_by_id(Documents.GET_DOCUMENTS.format(document_id))


def post_document(self, data: dict):
"""
Get Vendor Types
:return: List of Dicts in Vendor Types Schema
"""
return self._post_request(Documents.POST_DOCUMENT, data=json.dumps(data.__dict__))


def export_document(self, document_id: str):
"""
Export Document to Sage300
"""
return self._post_request(Documents.POST_DOCUMENT_EXPORT.format(document_id))
"""
Export Document to Sage300
"""
return self._post_request(Documents.POST_DOCUMENT_EXPORT.format(document_id))
25 changes: 11 additions & 14 deletions sage_desktop_sdk/apis/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Sage Desktop Jobs
"""
from sage_desktop_sdk.core.client import Client
from sage_desktop_sdk.core.schema.read_only import Job, CostCode, Category
from sage_desktop_sdk.core.schema.read_only import Job, StandardCategory, StandardCostCode


class Jobs(Client):
Expand All @@ -12,32 +12,29 @@ class Jobs(Client):
GET_COST_CODES = '/JobCosting/Api/V1/JobCost.svc/costcodes'
GET_CATEGORIES = '/JobCosting/Api/V1/JobCost.svc/categories'


def get_all_jobs(self):
"""
Get all Jobs
:return: List of Dicts in Jobs Schema
"""
jobs = self._query_get_all(Jobs.GET_JOBS)
jobs = self._query_get_all(Jobs.GET_JOBS)
for job in jobs:
yield Job.from_dict(job)


def get_all_costcodes(self):
def get_standard_costcodes(self):
"""
Get all Cost Codes
Get all standard Cost Codes
:return: List of Dicts in cost code Schema
"""
costcodes = self._query_get_all(Jobs.GET_COST_CODES)
costcodes = self._query_get_all(Jobs.GET_COST_CODES)
for costcode in costcodes:
yield CostCode.from_dict(costcode)

yield StandardCostCode.from_dict(costcode)

def get_all_categories(self):
def get_standard_categories(self):
"""
Get all Categories
:return: List of Dicts in Cstegories Schema
Get all standard Categories
:return: List of Dicts in Categories Schema
"""
categories = self._query_get_all(Jobs.GET_CATEGORIES)
categories = self._query_get_all(Jobs.GET_CATEGORIES)
for category in categories:
yield Category.from_dict(category)
yield StandardCategory.from_dict(category)
1 change: 1 addition & 0 deletions sage_desktop_sdk/apis/operation_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from sage_desktop_sdk.core.client import Client
from sage_desktop_sdk.core.schema.read_only import OperationStatusResponse


class OperationStatus(Client):
"""Class for Operation Status API."""

Expand Down
2 changes: 0 additions & 2 deletions sage_desktop_sdk/apis/vendors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class Vendors(Client):
GET_VENDORS = '/AccountsPayable/Api/V1/Vendor.svc/vendors'
GET_VENDOR_TYPES = '/AccountsPayable/Api/V1/Vendor.svc/vendors/types'


def get_all(self):
"""
Get all Vendors
Expand All @@ -21,7 +20,6 @@ def get_all(self):
for vendor in vendors:
yield Vendor.from_dict(vendor)


def get_vendor_types(self):
"""
Get Vendor Types
Expand Down
33 changes: 18 additions & 15 deletions sage_desktop_sdk/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@
import requests
import json
from typing import List, Dict
from sage_desktop_sdk.exceptions import *
from sage_desktop_sdk.exceptions import (
InvalidUserCredentials,
InvalidWebApiClientCredentials,
UserAccountLocked,
WebApiClientLocked,
WrongParamsError,
NotAcceptableClientError,
NotFoundItemError,
SageDesktopSDKError,
InternalServerError
)


class Client:
"""
This is base class for all API classes
"""

def __init__(self, url: str = None):

self.__api_url = None
self.__user_id = None
self.__user_password = None
self.__cookie = None


def set_user_id_and_password(self, user_id: str, user_password: str):
"""
Set the user_id and user_password for APIs
Expand All @@ -28,7 +36,6 @@ def set_user_id_and_password(self, user_id: str, user_password: str):
self.__user_id = user_id
self.__user_password = user_password


def set_api_url(self, indentifier: str):
"""
Set the api url and indentifier for APIs
Expand All @@ -37,7 +44,6 @@ def set_api_url(self, indentifier: str):
"""
self.__api_url = "https://{0}".format(indentifier)


def update_cookie(self, api_key: str, api_secret: str):
"""
Sets the cookies for APIs
Expand Down Expand Up @@ -70,37 +76,36 @@ def update_cookie(self, api_key: str, api_secret: str):

if response['Result'] == 1:
raise InvalidUserCredentials('Invalid User Credentials')

if response['Result'] == 2:
raise InvalidWebApiClientCredentials('Invalid Webapp Client')

if response['Result'] == 3:
raise UserAccountLocked('User Account Locked')

if response['Result'] == 4:
raise WebApiClientLocked('Web API client Locked')


def _query_get_all(self, url: str) -> List[Dict]:
"""
Gets all the objects of a particular type for query type GET calls
:param url: GET URL of object
:param object_type: type of object
:return: list of objects
"""

request_url = '{0}{1}'.format(self.__api_url, url)
api_headers = {
'Cookie': self.__cookie,
'Accept': 'application/json'
}

response = requests.get(url=request_url, headers=api_headers)

if response.status_code == 200:
data = json.loads(response.text)
return data

if response.status_code == 400:
raise WrongParamsError('Some of the parameters are wrong', response.text)

Expand All @@ -115,7 +120,6 @@ def _query_get_all(self, url: str) -> List[Dict]:

raise SageDesktopSDKError('Error: {0}'.format(response.status_code), response.text)


def _query_get_by_id(self, url: str) -> List[Dict]:
"""
Gets the object of a particular id
Expand Down Expand Up @@ -150,7 +154,6 @@ def _query_get_by_id(self, url: str) -> List[Dict]:

raise SageDesktopSDKError('Error: {0}'.format(response.status_code), response.text)


def _post_request(self, url: str, data=None) -> Dict:
"""
Gets all the objects of a particular type for query type GET calls
Expand Down
Loading
Loading