branddrive-python-sdk 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- branddrive/__init__.py +33 -0
- branddrive/client.py +134 -0
- branddrive/config.py +15 -0
- branddrive/dataclasses/__init__.py +118 -0
- branddrive/dataclasses/_utils.py +8 -0
- branddrive/dataclasses/customers.py +66 -0
- branddrive/dataclasses/expenses.py +155 -0
- branddrive/dataclasses/invoices.py +131 -0
- branddrive/dataclasses/payment_links.py +134 -0
- branddrive/dataclasses/pos.py +141 -0
- branddrive/dataclasses/products.py +166 -0
- branddrive/dataclasses/purchase_orders.py +141 -0
- branddrive/dataclasses/services.py +76 -0
- branddrive/dataclasses/vendors.py +48 -0
- branddrive/dataclasses/wallets.py +72 -0
- branddrive/exceptions.py +35 -0
- branddrive/protocols/__init__.py +43 -0
- branddrive/protocols/analytics.py +61 -0
- branddrive/protocols/customers.py +42 -0
- branddrive/protocols/expenses.py +69 -0
- branddrive/protocols/invoices.py +81 -0
- branddrive/protocols/payment_links.py +49 -0
- branddrive/protocols/pos.py +88 -0
- branddrive/protocols/products.py +150 -0
- branddrive/protocols/purchase_orders.py +68 -0
- branddrive/protocols/services.py +56 -0
- branddrive/protocols/vendors.py +39 -0
- branddrive/protocols/wallets.py +52 -0
- branddrive/resources/__init__.py +125 -0
- branddrive/resources/_utils.py +10 -0
- branddrive/resources/analytics.py +198 -0
- branddrive/resources/customers.py +194 -0
- branddrive/resources/expenses.py +209 -0
- branddrive/resources/invoices.py +268 -0
- branddrive/resources/payment_links.py +265 -0
- branddrive/resources/pos.py +353 -0
- branddrive/resources/products.py +612 -0
- branddrive/resources/purchase_orders.py +235 -0
- branddrive/resources/services.py +184 -0
- branddrive/resources/vendors.py +152 -0
- branddrive/resources/wallets.py +204 -0
- branddrive_python_sdk-0.1.0.dist-info/METADATA +132 -0
- branddrive_python_sdk-0.1.0.dist-info/RECORD +45 -0
- branddrive_python_sdk-0.1.0.dist-info/WHEEL +5 -0
- branddrive_python_sdk-0.1.0.dist-info/top_level.txt +1 -0
branddrive/__init__.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from typing import TYPE_CHECKING, Any
|
|
2
|
+
|
|
3
|
+
version = '0.1.0'
|
|
4
|
+
if TYPE_CHECKING:
|
|
5
|
+
from .client import BrandDriveClient
|
|
6
|
+
from .exceptions import APIException, InvalidAPIKeyException, InvalidBaseURLException
|
|
7
|
+
|
|
8
|
+
def __getattr__(name: str) -> Any:
|
|
9
|
+
if name == "BrandDriveClient":
|
|
10
|
+
from .client import BrandDriveClient
|
|
11
|
+
|
|
12
|
+
return BrandDriveClient
|
|
13
|
+
if name in {
|
|
14
|
+
"APIException",
|
|
15
|
+
"InvalidAPIKeyException",
|
|
16
|
+
"InvalidBaseURLException",
|
|
17
|
+
}:
|
|
18
|
+
from .exceptions import (
|
|
19
|
+
APIException,
|
|
20
|
+
InvalidAPIKeyException,
|
|
21
|
+
InvalidBaseURLException,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
return locals()[name]
|
|
25
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
__all__ = [
|
|
29
|
+
"BrandDriveClient",
|
|
30
|
+
"APIException",
|
|
31
|
+
"InvalidAPIKeyException",
|
|
32
|
+
"InvalidBaseURLException"
|
|
33
|
+
]
|
branddrive/client.py
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Optional, Any
|
|
4
|
+
|
|
5
|
+
from .config import settings
|
|
6
|
+
from .exceptions import handle_api_exception, InvalidAPIKeyException, InvalidBaseURLException
|
|
7
|
+
from .resources import (
|
|
8
|
+
Customer,
|
|
9
|
+
Expense,
|
|
10
|
+
Invoice,
|
|
11
|
+
Product,
|
|
12
|
+
POS,
|
|
13
|
+
PurchaseOrder,
|
|
14
|
+
Service,
|
|
15
|
+
Vendor,
|
|
16
|
+
Wallet,
|
|
17
|
+
PaymentLink,
|
|
18
|
+
Analytics,
|
|
19
|
+
)
|
|
20
|
+
from .protocols.customers import _CustomerAPI
|
|
21
|
+
from .protocols.invoices import _InvoiceAPI
|
|
22
|
+
from .protocols.products import _ProductAPI
|
|
23
|
+
from .protocols.pos import _POSAPI
|
|
24
|
+
from .protocols.services import _ServiceAPI
|
|
25
|
+
from .protocols.vendors import _VendorAPI
|
|
26
|
+
from .protocols.purchase_orders import _PurchaseOrderAPI
|
|
27
|
+
from .protocols.expenses import _ExpenseAPI
|
|
28
|
+
from .protocols.wallets import _WalletAPI
|
|
29
|
+
from .protocols.payment_links import _PaymentLinkAPI
|
|
30
|
+
from .protocols.analytics import _AnalyticsAPI
|
|
31
|
+
|
|
32
|
+
import httpx
|
|
33
|
+
import time
|
|
34
|
+
import random
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class Client:
|
|
38
|
+
def __init__(
|
|
39
|
+
self, api_key: Optional[str],
|
|
40
|
+
base_url: Optional[str],
|
|
41
|
+
timeout: Optional[int] = 30
|
|
42
|
+
) -> None:
|
|
43
|
+
self.api_key = api_key
|
|
44
|
+
self.base_url = base_url
|
|
45
|
+
self.timeout = timeout
|
|
46
|
+
|
|
47
|
+
if not self.api_key:
|
|
48
|
+
raise InvalidAPIKeyException("API key is required. Please set the BRANDDRIVE_API_KEY environment variable or provide it as an argument.")
|
|
49
|
+
|
|
50
|
+
if not self.base_url:
|
|
51
|
+
raise InvalidBaseURLException("Base URL is required. Please set the BRANDDRIVE_BASE_URL environment variable or provide it as an argument.")
|
|
52
|
+
|
|
53
|
+
def _client(self) -> httpx.Client:
|
|
54
|
+
|
|
55
|
+
"""Create an HTTP client with the specified settings."""
|
|
56
|
+
|
|
57
|
+
return httpx.Client(
|
|
58
|
+
base_url=self.base_url,
|
|
59
|
+
headers={
|
|
60
|
+
"Authorization": f"Bearer {self.api_key}",
|
|
61
|
+
"Accept": "application/json",
|
|
62
|
+
},
|
|
63
|
+
timeout=self.timeout,
|
|
64
|
+
limits=httpx.Limits(max_keepalive_connections=5, max_connections=10),
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
def __exit__(self, exc_type, exc, tb):
|
|
68
|
+
|
|
69
|
+
"""Close the HTTP client when exiting the context."""
|
|
70
|
+
self._client().close()
|
|
71
|
+
|
|
72
|
+
def close(self) -> None:
|
|
73
|
+
"""Close the HTTP client."""
|
|
74
|
+
self._client().close()
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class BrandDriveClient:
|
|
78
|
+
"""BrandDrive API client."""
|
|
79
|
+
|
|
80
|
+
def __init__(
|
|
81
|
+
self, api_key: str = settings.api_key,
|
|
82
|
+
base_url: str = settings.base_url,
|
|
83
|
+
timeout: int = 10,
|
|
84
|
+
max_retries: int = 3,
|
|
85
|
+
) -> None:
|
|
86
|
+
client = Client(api_key, base_url, timeout)
|
|
87
|
+
|
|
88
|
+
self._httpx = client._client()
|
|
89
|
+
self._max_retries = max_retries
|
|
90
|
+
self.customers: _CustomerAPI = Customer(self._httpx)
|
|
91
|
+
self.invoices: _InvoiceAPI = Invoice(self._httpx)
|
|
92
|
+
self.products: _ProductAPI = Product(self._httpx)
|
|
93
|
+
self.pos: _POSAPI = POS(self._httpx)
|
|
94
|
+
self.services: _ServiceAPI = Service(self._httpx)
|
|
95
|
+
self.vendors: _VendorAPI = Vendor(self._httpx)
|
|
96
|
+
self.purchase_orders: _PurchaseOrderAPI = PurchaseOrder(self._httpx)
|
|
97
|
+
self.expenses: _ExpenseAPI = Expense(self._httpx)
|
|
98
|
+
self.wallets: _WalletAPI = Wallet(self._httpx)
|
|
99
|
+
self.payment_links: _PaymentLinkAPI = PaymentLink(self._httpx)
|
|
100
|
+
self.analytics: _AnalyticsAPI = Analytics(self._httpx)
|
|
101
|
+
|
|
102
|
+
def __enter__(self) -> "BrandDriveClient":
|
|
103
|
+
return self
|
|
104
|
+
|
|
105
|
+
def __exit__(self, exc_type, exc, tb) -> None:
|
|
106
|
+
self.close()
|
|
107
|
+
|
|
108
|
+
def close(self) -> None:
|
|
109
|
+
"""Close the underlying HTTP connection pool."""
|
|
110
|
+
self._httpx.close()
|
|
111
|
+
|
|
112
|
+
def request(self, method: str, path: str, **kwargs) -> Any:
|
|
113
|
+
"""Send a request to the BrandDrive API with retry logic."""
|
|
114
|
+
|
|
115
|
+
attempt = 0
|
|
116
|
+
while True:
|
|
117
|
+
delay = 0.5 * 2**attempt + random.uniform(0, 0.25) # Exponential backoff delay
|
|
118
|
+
try:
|
|
119
|
+
response = self._httpx.request(method, path, **kwargs)
|
|
120
|
+
except httpx.TransportError:
|
|
121
|
+
if attempt >= self._max_retries:
|
|
122
|
+
raise
|
|
123
|
+
else:
|
|
124
|
+
status_code = response.status_code
|
|
125
|
+
if response.is_success:
|
|
126
|
+
return response.json()
|
|
127
|
+
|
|
128
|
+
retryable = status_code == 500 # Retry server error, return client error back to the user
|
|
129
|
+
if not retryable or attempt >= self._max_retries:
|
|
130
|
+
return handle_api_exception(response)
|
|
131
|
+
|
|
132
|
+
time.sleep(delay)
|
|
133
|
+
attempt += 1
|
|
134
|
+
|
branddrive/config.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
from dotenv import load_dotenv
|
|
5
|
+
|
|
6
|
+
load_dotenv()
|
|
7
|
+
|
|
8
|
+
class Settings:
|
|
9
|
+
"""Settings for BrandDrive SDK."""
|
|
10
|
+
|
|
11
|
+
def __init__(self) -> None:
|
|
12
|
+
self.api_key = os.environ.get("BRANDDRIVE_API_KEY", "")
|
|
13
|
+
self.base_url = os.environ.get("BRANDDRIVE_BASE_URL", "https://api.branddrive.com")
|
|
14
|
+
|
|
15
|
+
settings = Settings()
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
from .customers import CustomerData, UpdateCustomerData
|
|
2
|
+
from .pos import (
|
|
3
|
+
POSCreate,
|
|
4
|
+
POSPaymentProcurement,
|
|
5
|
+
POSProcurement,
|
|
6
|
+
POSProcurementUpdate,
|
|
7
|
+
POSProductProcurement,
|
|
8
|
+
POSVoidData,
|
|
9
|
+
PosUpdate,
|
|
10
|
+
)
|
|
11
|
+
from .invoices import (
|
|
12
|
+
InvoiceCreate,
|
|
13
|
+
InvoiceCreateSend,
|
|
14
|
+
InvoiceUpdate,
|
|
15
|
+
RecurringScheduleCreate,
|
|
16
|
+
RecurringScheduleUpdate,
|
|
17
|
+
)
|
|
18
|
+
from .products import (
|
|
19
|
+
ProcurementData,
|
|
20
|
+
ProductDistributionCreate,
|
|
21
|
+
ProductDistributionUpdate,
|
|
22
|
+
SingleProductCreate,
|
|
23
|
+
UpdateProductData,
|
|
24
|
+
UpdateProcurementData,
|
|
25
|
+
ProductVariationCreate
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
from .services import (
|
|
29
|
+
ServiceCategoryCreate,
|
|
30
|
+
ServiceCategoryUpdate,
|
|
31
|
+
ServiceCreate,
|
|
32
|
+
UpdateServiceData,
|
|
33
|
+
)
|
|
34
|
+
from .vendors import VendorData, UpdateVendorData
|
|
35
|
+
from .purchase_orders import (
|
|
36
|
+
PurchaseOrderCreate,
|
|
37
|
+
PurchaseOrderPaymentCreate,
|
|
38
|
+
PurchaseOrderPaymentMethod,
|
|
39
|
+
PurchaseOrderProcurementCreate,
|
|
40
|
+
PurchaseOrderProduct,
|
|
41
|
+
PurchaseOrderUpdate,
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
from .expenses import (
|
|
45
|
+
ExpenseCreate,
|
|
46
|
+
ExpenseLineItem,
|
|
47
|
+
ExpensePaymentCreate,
|
|
48
|
+
ExpensePaymentMethod,
|
|
49
|
+
ExpenseProcurementCreate,
|
|
50
|
+
ExpenseProcurementUpdate,
|
|
51
|
+
ExpenseUpdate,
|
|
52
|
+
)
|
|
53
|
+
from .wallets import (
|
|
54
|
+
WalletBusinessTransferData,
|
|
55
|
+
WalletExternalTransferData,
|
|
56
|
+
WalletFundData,
|
|
57
|
+
WalletPersonalTransferData,
|
|
58
|
+
)
|
|
59
|
+
from .payment_links import (
|
|
60
|
+
PaymentLinkCategoryCreate,
|
|
61
|
+
PaymentLinkCategoryData,
|
|
62
|
+
PaymentLinkCreate,
|
|
63
|
+
PaymentLinkPayData,
|
|
64
|
+
PaymentLinkSubscriptionDuration,
|
|
65
|
+
PaymentLinkUpdate,
|
|
66
|
+
)
|
|
67
|
+
__all__ = [
|
|
68
|
+
"CustomerData",
|
|
69
|
+
"UpdateCustomerData",
|
|
70
|
+
"POSCreate",
|
|
71
|
+
"POSPaymentProcurement",
|
|
72
|
+
"POSProcurement",
|
|
73
|
+
"POSProcurementUpdate",
|
|
74
|
+
"POSProductProcurement",
|
|
75
|
+
"POSVoidData",
|
|
76
|
+
"PosUpdate",
|
|
77
|
+
"InvoiceCreate",
|
|
78
|
+
"InvoiceCreateSend",
|
|
79
|
+
"InvoiceUpdate",
|
|
80
|
+
"RecurringScheduleCreate",
|
|
81
|
+
"RecurringScheduleUpdate",
|
|
82
|
+
"ProcurementData",
|
|
83
|
+
"UpdateProcurementData",
|
|
84
|
+
"ProductDistributionCreate",
|
|
85
|
+
"ProductDistributionUpdate",
|
|
86
|
+
"SingleProductCreate",
|
|
87
|
+
"UpdateProductData",
|
|
88
|
+
'ProductVariationCreate',
|
|
89
|
+
"ServiceCategoryCreate",
|
|
90
|
+
"ServiceCategoryUpdate",
|
|
91
|
+
"ServiceCreate",
|
|
92
|
+
"UpdateServiceData",
|
|
93
|
+
"VendorData",
|
|
94
|
+
"UpdateVendorData",
|
|
95
|
+
"PurchaseOrderCreate",
|
|
96
|
+
"PurchaseOrderUpdate",
|
|
97
|
+
"PurchaseOrderProduct",
|
|
98
|
+
"PurchaseOrderProcurementCreate",
|
|
99
|
+
"PurchaseOrderPaymentCreate",
|
|
100
|
+
"PurchaseOrderPaymentMethod",
|
|
101
|
+
"ExpenseProcurementCreate",
|
|
102
|
+
"ExpenseProcurementUpdate",
|
|
103
|
+
"ExpenseCreate",
|
|
104
|
+
"ExpenseUpdate",
|
|
105
|
+
"ExpenseLineItem",
|
|
106
|
+
"ExpensePaymentCreate",
|
|
107
|
+
"ExpensePaymentMethod",
|
|
108
|
+
"WalletFundData",
|
|
109
|
+
"WalletBusinessTransferData",
|
|
110
|
+
"WalletPersonalTransferData",
|
|
111
|
+
"WalletExternalTransferData",
|
|
112
|
+
"PaymentLinkCategoryCreate",
|
|
113
|
+
"PaymentLinkCategoryData",
|
|
114
|
+
"PaymentLinkCreate",
|
|
115
|
+
"PaymentLinkPayData",
|
|
116
|
+
"PaymentLinkSubscriptionDuration",
|
|
117
|
+
"PaymentLinkUpdate",
|
|
118
|
+
]
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def omit_none(payload: dict[str, Any]) -> dict[str, Any]:
|
|
7
|
+
"""Return a payload without fields whose value is ``None``."""
|
|
8
|
+
return {key: value for key, value in payload.items() if value is not None}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import Any, Optional
|
|
5
|
+
|
|
6
|
+
from ._utils import omit_none
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass
|
|
10
|
+
class CustomerData:
|
|
11
|
+
"""Fields used to create a customer."""
|
|
12
|
+
|
|
13
|
+
first_name: str
|
|
14
|
+
branch_id: int
|
|
15
|
+
birth_day: int
|
|
16
|
+
birth_month: int
|
|
17
|
+
last_name: Optional[str] = None
|
|
18
|
+
company_name: Optional[str] = None
|
|
19
|
+
email: Optional[str] = None
|
|
20
|
+
phone: Optional[str] = None
|
|
21
|
+
address: Optional[str] = None
|
|
22
|
+
country_code: Optional[str] = None
|
|
23
|
+
state_code: Optional[str] = None
|
|
24
|
+
city_id: Optional[int] = None
|
|
25
|
+
|
|
26
|
+
def to_dict(self) -> dict[str, Any]:
|
|
27
|
+
return omit_none({
|
|
28
|
+
"firstname": self.first_name,
|
|
29
|
+
"lastname": self.last_name,
|
|
30
|
+
"company": self.company_name,
|
|
31
|
+
"email": self.email,
|
|
32
|
+
"phone": self.phone,
|
|
33
|
+
"address": self.address,
|
|
34
|
+
"countryCode": self.country_code,
|
|
35
|
+
"stateCode": self.state_code,
|
|
36
|
+
"cityId": self.city_id,
|
|
37
|
+
"branchId": self.branch_id,
|
|
38
|
+
"birthDay": self.birth_day,
|
|
39
|
+
"birthMonth": self.birth_month,
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@dataclass
|
|
44
|
+
class UpdateCustomerData:
|
|
45
|
+
"""Fields used to update a customer's record."""
|
|
46
|
+
|
|
47
|
+
firstname: Optional[str] = None
|
|
48
|
+
lastname: Optional[str] = None
|
|
49
|
+
birth_day: Optional[int] = None
|
|
50
|
+
birth_month: Optional[int] = None
|
|
51
|
+
company_name: Optional[str] = None
|
|
52
|
+
email: Optional[str] = None
|
|
53
|
+
phone: Optional[str] = None
|
|
54
|
+
address: Optional[str] = None
|
|
55
|
+
|
|
56
|
+
def to_dict(self) -> dict[str, Any]:
|
|
57
|
+
return omit_none({
|
|
58
|
+
"firstname": self.firstname,
|
|
59
|
+
"lastname": self.lastname,
|
|
60
|
+
"birthDay": self.birth_day,
|
|
61
|
+
"birthMonth": self.birth_month,
|
|
62
|
+
"email": self.email,
|
|
63
|
+
"company": self.company_name,
|
|
64
|
+
"phoneNumber": self.phone,
|
|
65
|
+
"address": self.address,
|
|
66
|
+
})
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from datetime import date, datetime
|
|
5
|
+
from typing import Any, Literal, Optional
|
|
6
|
+
|
|
7
|
+
from ._utils import omit_none
|
|
8
|
+
|
|
9
|
+
ExpensePaymentMethod = Literal[
|
|
10
|
+
"BANK TRANSFER",
|
|
11
|
+
"CARD PAYMENT",
|
|
12
|
+
"CHEQUE",
|
|
13
|
+
"USSD",
|
|
14
|
+
"CASH PAYMENT",
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _iso_date(value: Optional[date]) -> Optional[str]:
|
|
19
|
+
if isinstance(value, datetime):
|
|
20
|
+
raise TypeError("date fields must use datetime.date, not datetime.datetime")
|
|
21
|
+
if value is not None and not isinstance(value, date):
|
|
22
|
+
raise TypeError("date fields must be datetime.date instances")
|
|
23
|
+
return value.isoformat() if value is not None else None
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass
|
|
27
|
+
class ExpenseLineItem:
|
|
28
|
+
"""A procurement line included when creating or updating an expense."""
|
|
29
|
+
|
|
30
|
+
amount: int
|
|
31
|
+
description: str
|
|
32
|
+
category_id: int
|
|
33
|
+
|
|
34
|
+
def to_dict(self) -> dict[str, Any]:
|
|
35
|
+
return omit_none({
|
|
36
|
+
"amount": self.amount,
|
|
37
|
+
"description": self.description,
|
|
38
|
+
"categoryId": self.category_id,
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@dataclass
|
|
43
|
+
class ExpenseCreate:
|
|
44
|
+
"""Fields used to create an expense."""
|
|
45
|
+
|
|
46
|
+
branch_id: int
|
|
47
|
+
vendor_id: int
|
|
48
|
+
note: Optional[str] = None
|
|
49
|
+
procurements: Optional[list[ExpenseLineItem]] = None
|
|
50
|
+
create_date: Optional[date] = None
|
|
51
|
+
payment_method: Optional[ExpensePaymentMethod] = None
|
|
52
|
+
paid_from: Optional[str] = None
|
|
53
|
+
due_date: Optional[date] = None
|
|
54
|
+
|
|
55
|
+
def to_dict(self) -> dict[str, Any]:
|
|
56
|
+
procurements = (
|
|
57
|
+
[procurement.to_dict() for procurement in self.procurements]
|
|
58
|
+
if self.procurements is not None
|
|
59
|
+
else None
|
|
60
|
+
)
|
|
61
|
+
return omit_none({
|
|
62
|
+
"branchId": self.branch_id,
|
|
63
|
+
"vendorId": self.vendor_id,
|
|
64
|
+
"note": self.note,
|
|
65
|
+
"procurements": procurements,
|
|
66
|
+
"createDate": _iso_date(self.create_date),
|
|
67
|
+
"paymentMethod": self.payment_method,
|
|
68
|
+
"paidFrom": self.paid_from,
|
|
69
|
+
"dueDate": _iso_date(self.due_date),
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@dataclass
|
|
74
|
+
class ExpenseUpdate:
|
|
75
|
+
"""Fields accepted when updating an expense."""
|
|
76
|
+
|
|
77
|
+
branch_id: int
|
|
78
|
+
note: Optional[str] = None
|
|
79
|
+
procurements: Optional[list[ExpenseLineItem]] = None
|
|
80
|
+
vendor_id: Optional[int] = None
|
|
81
|
+
payment_method: Optional[ExpensePaymentMethod] = None
|
|
82
|
+
due_date: Optional[date] = None
|
|
83
|
+
|
|
84
|
+
def to_dict(self) -> dict[str, Any]:
|
|
85
|
+
procurements = (
|
|
86
|
+
[procurement.to_dict() for procurement in self.procurements]
|
|
87
|
+
if self.procurements is not None
|
|
88
|
+
else None
|
|
89
|
+
)
|
|
90
|
+
return omit_none({
|
|
91
|
+
"branchId": self.branch_id,
|
|
92
|
+
"note": self.note,
|
|
93
|
+
"procurements": procurements,
|
|
94
|
+
"vendorId": self.vendor_id,
|
|
95
|
+
"paymentMethod": self.payment_method,
|
|
96
|
+
"dueDate": _iso_date(self.due_date),
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
@dataclass
|
|
101
|
+
class ExpenseProcurementCreate:
|
|
102
|
+
"""A procurement added to an existing expense."""
|
|
103
|
+
|
|
104
|
+
expense_id: int
|
|
105
|
+
amount: int
|
|
106
|
+
description: str
|
|
107
|
+
category_id: int
|
|
108
|
+
|
|
109
|
+
def to_dict(self) -> dict[str, Any]:
|
|
110
|
+
return omit_none({
|
|
111
|
+
"expenseId": self.expense_id,
|
|
112
|
+
"amount": self.amount,
|
|
113
|
+
"description": self.description,
|
|
114
|
+
"categoryId": self.category_id,
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
@dataclass
|
|
119
|
+
class ExpenseProcurementUpdate:
|
|
120
|
+
"""Optional fields accepted when updating an expense procurement."""
|
|
121
|
+
|
|
122
|
+
amount: Optional[int] = None
|
|
123
|
+
description: Optional[str] = None
|
|
124
|
+
category_id: Optional[int] = None
|
|
125
|
+
name: Optional[str] = None
|
|
126
|
+
|
|
127
|
+
def to_dict(self) -> dict[str, Any]:
|
|
128
|
+
return omit_none({
|
|
129
|
+
"amount": self.amount,
|
|
130
|
+
"description": self.description,
|
|
131
|
+
"categoryId": self.category_id,
|
|
132
|
+
"name": self.name,
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
@dataclass
|
|
137
|
+
class ExpensePaymentCreate:
|
|
138
|
+
"""A payment recorded against an expense."""
|
|
139
|
+
|
|
140
|
+
expense_id: int
|
|
141
|
+
amount: int
|
|
142
|
+
payment_date: date
|
|
143
|
+
paid_from: str
|
|
144
|
+
payment_method: ExpensePaymentMethod
|
|
145
|
+
note: Optional[str] = None
|
|
146
|
+
|
|
147
|
+
def to_dict(self) -> dict[str, Any]:
|
|
148
|
+
return omit_none({
|
|
149
|
+
"expenseId": self.expense_id,
|
|
150
|
+
"amount": self.amount,
|
|
151
|
+
"paymentDate": _iso_date(self.payment_date),
|
|
152
|
+
"paidFrom": self.paid_from,
|
|
153
|
+
"paymentMethod": self.payment_method,
|
|
154
|
+
"note": self.note,
|
|
155
|
+
})
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from datetime import date
|
|
5
|
+
from typing import Any, Optional
|
|
6
|
+
|
|
7
|
+
from .products import ProcurementData
|
|
8
|
+
from .pos import _serialize_procurements
|
|
9
|
+
from ._utils import omit_none
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _iso_date(value: Optional[date]) -> Optional[str]:
|
|
13
|
+
return value.isoformat() if value is not None else None
|
|
14
|
+
|
|
15
|
+
@dataclass
|
|
16
|
+
class InvoiceCreate:
|
|
17
|
+
"""Fields used to create a new invoice."""
|
|
18
|
+
|
|
19
|
+
branch_id: int
|
|
20
|
+
issued_at: date
|
|
21
|
+
due_at: date
|
|
22
|
+
currency: str
|
|
23
|
+
customer_id: int
|
|
24
|
+
invoice_name: str
|
|
25
|
+
procurements: list[ProcurementData]
|
|
26
|
+
description: Optional[str] = None
|
|
27
|
+
SO_number: Optional[int] = None
|
|
28
|
+
payment_methods: Optional[list[str]] = None
|
|
29
|
+
|
|
30
|
+
def to_dict(self) -> dict[str, Any]:
|
|
31
|
+
payload: dict[str, Any] = {
|
|
32
|
+
"branchId": self.branch_id,
|
|
33
|
+
"issuedAt": _iso_date(self.issued_at),
|
|
34
|
+
"dueAt": _iso_date(self.due_at),
|
|
35
|
+
"customerId": self.customer_id,
|
|
36
|
+
"currency": self.currency,
|
|
37
|
+
"invoiceName": self.invoice_name,
|
|
38
|
+
"procurements": _serialize_procurements(self.procurements),
|
|
39
|
+
"description": self.description,
|
|
40
|
+
"SONumber": self.SO_number,
|
|
41
|
+
"paymentMethods": self.payment_methods,
|
|
42
|
+
}
|
|
43
|
+
return omit_none(payload)
|
|
44
|
+
|
|
45
|
+
@dataclass
|
|
46
|
+
class InvoiceCreateSend:
|
|
47
|
+
"""Fields used to create an invoice and optionally send it."""
|
|
48
|
+
|
|
49
|
+
branch_id: int
|
|
50
|
+
send: bool = False
|
|
51
|
+
issued_at: Optional[date] = None
|
|
52
|
+
due_at: Optional[date] = None
|
|
53
|
+
customer_id: Optional[int] = None
|
|
54
|
+
SO_number: Optional[int] = None
|
|
55
|
+
currency: Optional[str] = None
|
|
56
|
+
note: Optional[str] = None
|
|
57
|
+
invoice_name: Optional[str] = None
|
|
58
|
+
procurements: Optional[list[ProcurementData]] = None
|
|
59
|
+
payment_methods: Optional[list[str]] = None
|
|
60
|
+
discount: Optional[int] = None
|
|
61
|
+
total_amount_due: Optional[int] = None
|
|
62
|
+
|
|
63
|
+
def to_dict(self) -> dict[str, Any]:
|
|
64
|
+
return omit_none({
|
|
65
|
+
"branchId": self.branch_id,
|
|
66
|
+
"send": self.send,
|
|
67
|
+
"issuedAt": _iso_date(self.issued_at),
|
|
68
|
+
"dueAt": _iso_date(self.due_at),
|
|
69
|
+
"customerId": self.customer_id,
|
|
70
|
+
"SONumber": self.SO_number,
|
|
71
|
+
"currency": self.currency,
|
|
72
|
+
"note": self.note,
|
|
73
|
+
"invoiceName": self.invoice_name,
|
|
74
|
+
"procurements": _serialize_procurements(self.procurements),
|
|
75
|
+
"paymentMethods": self.payment_methods,
|
|
76
|
+
"discount": self.discount,
|
|
77
|
+
"totalAmountDue": self.total_amount_due,
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@dataclass
|
|
82
|
+
class InvoiceUpdate(InvoiceCreateSend):
|
|
83
|
+
"""Fields used to update an invoice."""
|
|
84
|
+
|
|
85
|
+
status: Optional[str] = None
|
|
86
|
+
invoice_number: Optional[int] = None
|
|
87
|
+
|
|
88
|
+
def to_dict(self) -> dict[str, Any]:
|
|
89
|
+
payload = super().to_dict()
|
|
90
|
+
payload.update(omit_none({
|
|
91
|
+
"status": self.status,
|
|
92
|
+
"invoiceNumber": self.invoice_number,
|
|
93
|
+
}))
|
|
94
|
+
return payload
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
@dataclass
|
|
98
|
+
class RecurringScheduleCreate:
|
|
99
|
+
"""Fields used to create an invoice recurring schedule."""
|
|
100
|
+
|
|
101
|
+
start_date: date
|
|
102
|
+
end_date: date
|
|
103
|
+
frequency: str
|
|
104
|
+
specifically_on: Optional[date] = None
|
|
105
|
+
|
|
106
|
+
def to_dict(self) -> dict[str, Any]:
|
|
107
|
+
payload: dict[str, Any] = {
|
|
108
|
+
"startDate": _iso_date(self.start_date),
|
|
109
|
+
"endDate": _iso_date(self.end_date),
|
|
110
|
+
"frequency": self.frequency,
|
|
111
|
+
"specificallyOn": _iso_date(self.specifically_on),
|
|
112
|
+
}
|
|
113
|
+
return omit_none(payload)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
@dataclass
|
|
117
|
+
class RecurringScheduleUpdate:
|
|
118
|
+
"""Fields used to update an invoice recurring schedule."""
|
|
119
|
+
|
|
120
|
+
start_date: date
|
|
121
|
+
end_date: date
|
|
122
|
+
frequency: str
|
|
123
|
+
specifically_on: date
|
|
124
|
+
|
|
125
|
+
def to_dict(self) -> dict[str, Any]:
|
|
126
|
+
return omit_none({
|
|
127
|
+
"startDate": _iso_date(self.start_date),
|
|
128
|
+
"endDate": _iso_date(self.end_date),
|
|
129
|
+
"frequency": self.frequency,
|
|
130
|
+
"specificallyOn": _iso_date(self.specifically_on),
|
|
131
|
+
})
|