stackin-python-sdk 0.6.0__py3-none-any.whl → 0.8.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.
stackin/__init__.py CHANGED
@@ -1,6 +1,6 @@
1
1
  """Invoice __init__ module."""
2
2
 
3
- __version__ = "0.6.0"
3
+ __version__ = "0.8.0"
4
4
  __description__ = (
5
5
  "Python SDK for issuing, consulting and cancelling electronic invoices."
6
6
  )
stackin/br/product.py CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
+ from decimal import Decimal
5
6
  from typing import Annotated, Any
6
7
 
7
8
  from pydantic import BaseModel, Field
@@ -36,11 +37,17 @@ _BR_FIELDS = {
36
37
  }
37
38
 
38
39
 
40
+ def _decimal_str(value: Decimal | None) -> str | None:
41
+ """JSON has no decimal; a float would lose the tenth place."""
42
+ return None if value is None else format(value, "f")
43
+
44
+
39
45
  class Product(BaseModel):
40
46
  """One product or service line item on an invoice."""
41
47
 
42
48
  description: str = Field(..., min_length=1)
43
- amount: float = Field(..., gt=0)
49
+ amount: Decimal | None = Field(default=None, gt=0)
50
+ unit_price: Decimal | None = Field(default=None, gt=0)
44
51
  unit: str = Field(default="UN")
45
52
  quantity: float = Field(default=1.0, gt=0)
46
53
  barcode: str | None = Field(default=None)
@@ -102,6 +109,7 @@ class Product(BaseModel):
102
109
  exclude={
103
110
  "description",
104
111
  "amount",
112
+ "unit_price",
105
113
  "tax",
106
114
  *_BR_FIELDS,
107
115
  *_NFSE_FIELDS,
@@ -116,7 +124,8 @@ class Product(BaseModel):
116
124
  data["br"] = br
117
125
  return {
118
126
  "description": self.description,
119
- "amount": self.amount,
127
+ "amount": _decimal_str(self.amount),
128
+ "unit_price": _decimal_str(self.unit_price),
120
129
  "product": data,
121
130
  "service_code": self.service_code,
122
131
  "discount": self.service_discount,
stackin/core/client.py CHANGED
@@ -141,15 +141,25 @@ class Invoice:
141
141
  *,
142
142
  document_type: DocumentType,
143
143
  reason: str,
144
+ idempotency_key: str | None = None,
144
145
  ) -> dict:
145
- """Cancels a fiscal document by its access key."""
146
+ """Cancels a fiscal document by its access key.
147
+
148
+ Repeating the same idempotency_key with the same body replays
149
+ the first answer instead of cancelling twice. Cancelling is
150
+ irreversible and has a legal window, so a blind retry on a
151
+ dropped connection is the case this exists for.
152
+ """
146
153
  payload = {
147
154
  "document_type": document_type.value,
148
155
  "reason": reason,
149
156
  }
150
157
 
151
158
  return self._request(
152
- "POST", f"/invoices/{access_key}/cancel", json=payload
159
+ "POST",
160
+ f"/invoices/{access_key}/cancel",
161
+ json=payload,
162
+ idempotency_key=idempotency_key,
153
163
  )
154
164
 
155
165
  def invalidate(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: stackin-python-sdk
3
- Version: 0.6.0
3
+ Version: 0.8.0
4
4
  Summary: Python SDK for issuing, consulting and cancelling electronic invoices.
5
5
  License-Expression: MIT
6
6
  License-File: LICENSE
@@ -47,7 +47,73 @@ Description-Content-Type: text/markdown
47
47
 
48
48
  Python SDK for fiscal document issuance — a handful of business fields, nothing about certificates, XML, XSD, signing or SOAP. The API resolves all of that from the issuer's own configuration, identified by `api_key`.
49
49
 
50
- **One class, `Invoice`** — `issue()`/`consult()`/`cancel()`/`reissue()`/`correct()`/`invalidate()`/`pdf()`/`received()`/`manifest()`, nothing else to instantiate. Each line item is a `Product` (`stackin.br`) — `description`/`amount` are universal, everything else (`ncm`/`cfop`/`cest`/tax groups...) is Brazil-specific and only required for NFE; NFSE ignores it.
50
+ **One class, `Invoice`** — `issue()`/`consult()`/`cancel()`/`reissue()`/`correct()`/`invalidate()`/`pdf()`/`received()`/`manifest()`, nothing else to instantiate. Each line item is a `Product` (`stackin.br`) — `description` plus either `unit_price` or `amount` are universal, everything else (`ncm`/`cfop`/`cest`/tax groups...) is Brazil-specific and only required for NFE; NFSE ignores it.
51
+
52
+ ## What a line item is worth
53
+
54
+ `unit_price` is the price of **one unit**. `amount` is the **gross total of
55
+ the line's products**, before discount, freight, insurance and other
56
+ expenses. Send either; sending both asserts that they agree.
57
+
58
+ ```python
59
+ from decimal import Decimal
60
+
61
+ # One unit
62
+ Product(description="Consultoria", quantity=Decimal("1"), unit_price=Decimal("1500.00"))
63
+
64
+ # More than one unit — amount is computed as 2 x 120.00 = 240.00
65
+ Product(description="Teclado", quantity=Decimal("2"), unit_price=Decimal("120.00"))
66
+
67
+ # A fractional quantity — 1.5 x 30.00 = 45.00
68
+ Product(description="Cabo", quantity=Decimal("1.5"), unit_price=Decimal("30.00"))
69
+
70
+ # Legacy: amount alone still means the line's gross total
71
+ Product(description="Servico", quantity=Decimal("3"), amount=Decimal("150.00"))
72
+
73
+ # Both, checked against each other before the document is transmitted
74
+ Product(
75
+ description="Teclado",
76
+ quantity=Decimal("2"),
77
+ unit_price=Decimal("120.00"),
78
+ amount=Decimal("240.00"),
79
+ )
80
+
81
+ # With charges: 5000.00 - 100.50 + 40.00 + 10.00 = 4949.50 on the note
82
+ Product(
83
+ description="Servidor",
84
+ quantity=Decimal("2"),
85
+ unit_price=Decimal("2500.00"),
86
+ discount=100.50,
87
+ freight=40.00,
88
+ insurance=10.00,
89
+ )
90
+ ```
91
+
92
+ Amounts that do not add up are refused before the authorizer sees them,
93
+ with a `422` naming the line and both numbers:
94
+
95
+ ```json
96
+ {
97
+ "detail": {
98
+ "code": "ITEM_TOTAL_MISMATCH",
99
+ "field": "items[0].amount",
100
+ "message": "amount is 50.00, but quantity x unit_price is 150.00.",
101
+ "expected": "150.00",
102
+ "received": "50.00"
103
+ }
104
+ }
105
+ ```
106
+
107
+ ### Migrating
108
+
109
+ ```text
110
+ Before: quantity = 3, amount = 150.00
111
+ After: quantity = 3, unit_price = 50.00
112
+ ```
113
+
114
+ Nothing has to migrate. `amount` keeps the meaning it always had and is
115
+ not deprecated in this release; a line sent with `amount` alone is built
116
+ exactly as it was before.
51
117
 
52
118
  ## Install
53
119
 
@@ -60,6 +126,8 @@ pip install stackin-python-sdk
60
126
  Get an `api_key` from the [stackin dashboard](https://app.stackin.io) — select the issuing company, then Settings → API key (context `sdk`). One key per issuing company, shown once at creation. The API resolves the issuer (CNPJ, state, address, certificate, environment) entirely from it; nothing about the issuer is ever passed on a call.
61
127
 
62
128
  ```python
129
+ from decimal import Decimal
130
+
63
131
  from stackin import Invoice, DocumentType, Address
64
132
  from stackin.br import Product # Brazil-specific line item — NCM/CFOP
65
133
 
@@ -90,7 +158,16 @@ client.issue(
90
158
  document_type=DocumentType.NFE,
91
159
  client_name="Buyer Company Ltd",
92
160
  tax_id="11111111111111",
93
- items=[Product(description="Test product", amount=100.00, ncm="84713012", cfop="5102")],
161
+ items=[
162
+ Product(
163
+ description="Teclado",
164
+ quantity=Decimal("2"),
165
+ unit_price=Decimal("120.00"),
166
+ unit="UN",
167
+ ncm="84716052",
168
+ cfop="5102",
169
+ )
170
+ ],
94
171
  recipient_address=Address(
95
172
  street="Avenida Atlantica",
96
173
  number="500",
@@ -1,13 +1,13 @@
1
- stackin/__init__.py,sha256=H9pBLgWFY-FCi4oOnoW-RM63GgAIJQfTICEfO61LPZw,531
1
+ stackin/__init__.py,sha256=d07OH2vuFftm39TnZQmE1pIEWq3kHeTjkuRWFChVVE8,531
2
2
  stackin/br/__init__.py,sha256=ktBhaGVPE81n8r5jdIloERq4x9B2AE1k9-1IlzqVhVM,1184
3
- stackin/br/product.py,sha256=877FGYgVZ8iBIJFecsDRbbnEiKmbMY5WBuVtHoEoia4,4044
3
+ stackin/br/product.py,sha256=DrQ0DGIfQlJh8ANN1xK74y8LXnMLZGtN8bV_zsOpn14,4431
4
4
  stackin/br/tax.py,sha256=UxcYX-kJw1HxUixB0hRABSgoHqT6CLoEjup4xIRFaYg,54747
5
5
  stackin/core/__init__.py,sha256=dxtOP1DopMSNFYGh3Hf12RVcs58R9xwLn-gqfKFo-HE,19
6
6
  stackin/core/address.py,sha256=-w5R3nzfU5T3VoxIeygu5Dq4DRb_lJHAGc0lmSMwEMU,616
7
- stackin/core/client.py,sha256=Jh5Qkg_AHXBlKZ_rIkSFXEMMcjiC_928EDvMVZewnko,11701
7
+ stackin/core/client.py,sha256=G80o4ORwNtazasIlmJBEhyrwG5VgTAsit9MTxK7FcEI,12086
8
8
  stackin/core/exceptions.py,sha256=ELn-uL8rwjAFsD1PgM8Zr-1if3Z5yOWU81chUs4k6tU,505
9
9
  stackin/core/types.py,sha256=NpfYVN58Ug9fEY7VYeQ2TC1eekoFGHZq_habvrqEeTY,604
10
- stackin_python_sdk-0.6.0.dist-info/METADATA,sha256=OBtsl_XghGyuIZlQVJMe1fjxsLJyR6gWWHRYu3DcbLc,11041
11
- stackin_python_sdk-0.6.0.dist-info/WHEEL,sha256=eY7nduwzv-ldUxpzbRlxwvC693Hg6PX8bWDjEHjZ_dk,88
12
- stackin_python_sdk-0.6.0.dist-info/licenses/LICENSE,sha256=D-XHDcKFFhzEo9C5Dp4QPvt00pb_u__hi4Bc08zpxyU,1072
13
- stackin_python_sdk-0.6.0.dist-info/RECORD,,
10
+ stackin_python_sdk-0.8.0.dist-info/METADATA,sha256=s_AljpFSzWMedZupxxXaCkcDbEf90TjdV4Zdb0Z8AsE,13101
11
+ stackin_python_sdk-0.8.0.dist-info/WHEEL,sha256=eY7nduwzv-ldUxpzbRlxwvC693Hg6PX8bWDjEHjZ_dk,88
12
+ stackin_python_sdk-0.8.0.dist-info/licenses/LICENSE,sha256=D-XHDcKFFhzEo9C5Dp4QPvt00pb_u__hi4Bc08zpxyU,1072
13
+ stackin_python_sdk-0.8.0.dist-info/RECORD,,