Skip to content

Commit

Permalink
Second draft of boundary/tax code/tax rule importing
Browse files Browse the repository at this point in the history
- support for boundary data with tax codes (only)
- dynamic resolution of tax rule and tax code for sale and invoice
- reporting code stored on tax line as text
- cascade deletes onto tax code lines
- Added jurasdiction names for Utah
  • Loading branch information
cdchapman committed Aug 19, 2024
1 parent 55ca525 commit 3551a0e
Show file tree
Hide file tree
Showing 9 changed files with 708 additions and 93 deletions.
3 changes: 3 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ def register():
tax.Tax,
tax.TaxBoundary,
tax.TaxCode,
tax.TaxCodeLine,
tax.TaxLine,
tax.TaxRule,
module='account_us_sstp', type_='model')
Pool.register(
account.InvoiceLine,
account.InvoiceTax,
module='account_us_sstp', type_='model',
depends=['account_invoice'])
Pool.register(
Expand Down
70 changes: 64 additions & 6 deletions account.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ class InvoiceLine(metaclass=PoolMeta):
__name__ = 'account.invoice.line'

@fields.depends(
'_parent_invoice.party', 'party', 'invoice', 'tax_date',
'_parent_invoice.party', 'party', 'invoice',
'_parent_invoice.accounting_date', '_parent_invoice.invoice_date',
'_parent_invoice.invoice_address')
def on_change_product(self):
pool = Pool()
Date = pool.get('ir.date')
Boundary = pool.get('account.tax.boundary')

if self.invoice and self.invoice.tax_date:
tax_date = self.invoice.tax_date
elif self.tax_date:
if self.tax_date:
tax_date = self.tax_date
elif self.taxes_date:
tax_date = self.taxes_date
elif self.invoice and self.invoice.tax_date:
tax_date = self.invoice.tax_date
else:
tax_date = Date.today()

if self.invoice and self.invoice.invoice_address:
a = self.invoice.invoice_address
Expand Down Expand Up @@ -67,3 +67,61 @@ def on_change_product(self):
party.customer_tax_rule = boundary.rule

return super().on_change_product()

class InvoiceTax(metaclass=PoolMeta):
__name__ = 'account.invoice.tax'

def get_move_lines(self):
lines = super().get_move_lines()

pool = Pool()
Date = pool.get('ir.date')
Boundary = pool.get('account.tax.boundary')

if self.invoice and self.invoice.tax_date:
tax_date = self.invoice.tax_date
else:
tax_date = Date.today()

if self.invoice and self.invoice.invoice_address:
a = self.invoice.invoice_address

pattern = r'(\d{5})-?(\d{4})?$'
match = re.match(pattern, a.postal_code)
if match:
zipcode, zipext = match.groups()

try:
boundary, = Boundary.search([
('start_date', '<=', tax_date),
['OR', [
('end_date', '>=', tax_date)
], [
('end_date', '=', None)
],
],
('authority.country', '=', a.country),
('authority.subdivision', '=', a.subdivision),
['OR', [
('type', '=', '4'),
('zipcode_low', '<=', zipcode),
('zipcode_high', '>=', zipcode),
('zipext_low', '<=', zipext),
('zipext_high', '>=', zipext),
], [
('type', '=', 'Z'),
('zipcode_low', '<=', zipcode),
('zipcode_high', '>=', zipcode),
],
]
], limit=1, order=[('type', 'DESC')])
except ValueError:
boundary = None

if boundary and boundary.code:
for line in lines:
for tax_line in line.tax_lines:
if tax_line.type == 'tax':
tax_line.code = boundary.code.code

return lines
2 changes: 0 additions & 2 deletions sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ def compute_taxes(self, party):
]
], limit=1, order=[('type', 'DESC')])
except ValueError:
print("Could not find a matching tax rule for %s" % party)
boundary = None

if boundary and boundary.rule:
print('using boundary rule %s' % boundary.rule.rec_name)
if party and not party.customer_tax_rule:
party.customer_tax_rule = boundary.rule

Expand Down
Loading

0 comments on commit 3551a0e

Please sign in to comment.