Skip to content

Commit e694364

Browse files
Merge pull request #73 from mpb-com/refactor-line-number-internals
Base the line number off of the lines list length
2 parents 7775f4d + 37bc77c commit e694364

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

src/transaction_builder_methods.py

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@
3131
class Mixin:
3232
"""Mixin containing methods attached to TransactionBuilder Class."""
3333

34+
@property
35+
def line_num(self):
36+
return len(self.create_model.get('lines', []))
37+
38+
def _get_line_number(self, line_number=None):
39+
if line_number is None:
40+
line_number = self.line_num
41+
return str(line_number)
42+
3443
def with_commit(self):
3544
"""
3645
Set the commit flag of the transaction.
@@ -127,7 +136,14 @@ def with_latlong(self, address_type, lat, long_):
127136
'longitude': float(long_)}
128137
return self
129138

130-
def with_line(self, amount, quantity, item_code, tax_code, line_number=None):
139+
def with_line(
140+
self,
141+
amount,
142+
quantity,
143+
item_code,
144+
tax_code,
145+
line_number=None,
146+
):
131147
r"""
132148
Add a line to the transaction.
133149
@@ -136,42 +152,45 @@ def with_line(self, amount, quantity, item_code, tax_code, line_number=None):
136152
:param string item_code: Code of the item.
137153
:param string tax_code: Tax Code of the item. If left blank, \
138154
the default item (P0000000) is assumed.
139-
:param [int] line_number: Value of the line number.
155+
:param str line_number: Value of the line number.
140156
:return: TransactionBuilder
141157
"""
142-
if line_number is not None:
143-
self.line_num = line_number;
144-
158+
145159
temp = {
146-
'number': str(self.line_num),
160+
'number': self._get_line_number(line_number),
147161
'amount': amount,
148162
'quantity': quantity,
149163
'itemCode': str(item_code),
150164
'taxCode': str(tax_code)
151165
}
152166
self.create_model['lines'].append(temp)
153-
self.line_num += 1
154167
return self
155168

156-
def with_exempt_line(self, amount, item_code, exemption_code):
169+
def with_exempt_line(
170+
self,
171+
amount,
172+
item_code,
173+
exemption_code,
174+
line_number=None,
175+
):
157176
"""
158177
Add a line with an exemption to this transaction.
159178
160179
:param float amount: The amount of this line item
161180
:param string item_code: The code for the item
162181
:param string exemption_code: The exemption code for this line item
182+
:param str line_number: Value of the line number.
163183
:return: TransactionBuilder
164184
"""
165185

166186
temp = {
167-
'number': str(self.line_num),
187+
'number': self._get_line_number(line_number),
168188
'quantity': 1,
169189
'amount': amount,
170190
'exemptionCode': str(exemption_code),
171191
'itemCode': str(item_code)
172192
}
173193
self.create_model['lines'].append(temp)
174-
self.line_num += 1
175194
return self
176195

177196
def with_diagnostics(self):
@@ -297,7 +316,13 @@ def with_tax_override(self, type_, reason, tax_amount, tax_date):
297316
}
298317
return self
299318

300-
def with_separate_address_line(self, amount, type_, address):
319+
def with_separate_address_line(
320+
self,
321+
amount,
322+
type_,
323+
address,
324+
line_number=None,
325+
):
301326
r"""
302327
Add a line to this transaction.
303328
@@ -315,10 +340,11 @@ def with_separate_address_line(self, amount, type_, address):
315340
region State or Region of the location.
316341
postal_code Postal/zip code of the location.
317342
country The two-letter country code of the location.
343+
:param str line_number: Value of the line number.
318344
:return: TransactionBuilder
319345
"""
320346
temp = {
321-
'number': self.line_num,
347+
'number': self._get_line_number(line_number),
322348
'quantity': 1,
323349
'amount': amount,
324350
'addresses': {
@@ -327,7 +353,6 @@ def with_separate_address_line(self, amount, type_, address):
327353
}
328354

329355
self.create_model['lines'].append(temp)
330-
self.line_num += 1
331356
return self
332357

333358
def create_adjustment_request(self, desc, reason):

0 commit comments

Comments
 (0)