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

[13.0][IMP] account_fiscal_position_rule: add zip_range for invoice and shipping #359

Open
wants to merge 2 commits into
base: 13.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import time

from odoo import api, fields, models
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError


class AccountFiscalPositionRule(models.Model):
Expand Down Expand Up @@ -49,6 +50,10 @@
domain="[('country_id','=',to_invoice_country)]",
)

to_invoice_zip_from = fields.Char(string="Invoice Zip Range From")

to_invoice_zip_to = fields.Char(string="Invoice Zip Range To")

to_shipping_country_group_id = fields.Many2one(
string="Destination Country Group",
comodel_name="res.country.group",
Expand All @@ -65,6 +70,10 @@
domain="[('country_id','=',to_shipping_country)]",
)

to_shipping_zip_from = fields.Char(string="Destination Zip Range From")

to_shipping_zip_to = fields.Char(string="Destination Zip Range To")

company_id = fields.Many2one(
comodel_name="res.company", sting="Company", required=True, index=True
)
Expand Down Expand Up @@ -113,6 +122,27 @@
),
)

@api.constrains(
"to_invoice_zip_from",
"to_invoice_zip_to",
"to_shipping_zip_from",
"to_shipping_zip_to",
)
def _check_zip(self):
for rule in self:
if (
rule.to_invoice_zip_from
and rule.to_invoice_zip_to
and rule.to_invoice_zip_from > rule.to_invoice_zip_to
) or (
rule.to_shipping_zip_from
and rule.to_shipping_zip_to
and rule.to_shipping_zip_from > rule.to_shipping_zip_to
):
raise ValidationError(

Check warning on line 142 in account_fiscal_position_rule/models/account_fiscal_position_rule.py

View check run for this annotation

Codecov / codecov/patch

account_fiscal_position_rule/models/account_fiscal_position_rule.py#L142

Added line #L142 was not covered by tests
_('Invalid "Zip Range", please configure it properly.')
)

@api.onchange("from_country_group_id")
def _onchange_from_country_group_id(self):
self.ensure_one()
Expand All @@ -137,6 +167,11 @@
]
return {"domain": {"to_invoice_country": to_invoice_country_domain}}

@api.onchange("to_invoice_country")
def _onchange_to_invoice_country(self):
self.ensure_one()
self.to_invoice_zip_from = self.to_invoice_zip_to = False

Check warning on line 173 in account_fiscal_position_rule/models/account_fiscal_position_rule.py

View check run for this annotation

Codecov / codecov/patch

account_fiscal_position_rule/models/account_fiscal_position_rule.py#L172-L173

Added lines #L172 - L173 were not covered by tests

@api.onchange("to_shipping_country_group_id")
def _onchange_to_shipping_country_group_id(self):
self.ensure_one()
Expand All @@ -149,6 +184,11 @@
]
return {"domain": {"to_shipping_country": to_shipping_country_domain}}

@api.onchange("to_shipping_country")
def _onchange_to_shipping_country(self):
self.ensure_one()
self.to_shipping_zip_from = self.to_shipping_zip_to = False

Check warning on line 190 in account_fiscal_position_rule/models/account_fiscal_position_rule.py

View check run for this annotation

Codecov / codecov/patch

account_fiscal_position_rule/models/account_fiscal_position_rule.py#L189-L190

Added lines #L189 - L190 were not covered by tests

@api.onchange("company_id")
def onchange_company(self):
self.from_country = self.company_id.country_id
Expand Down Expand Up @@ -198,6 +238,8 @@
key_country_group = "to_%s_country_group_id" % address_type
key_country = "to_%s_country" % address_type
key_state = "to_%s_state" % address_type
key_zip_from = "to_%s_zip_from" % address_type
key_zip_to = "to_%s_zip_to" % address_type
to_country = address.country_id or self.env["res.country"].browse()
domain += [
"|",
Expand All @@ -211,6 +253,15 @@
]
to_state = address.state_id or self.env["res.country.state"].browse()
domain += ["|", (key_state, "=", to_state.id), (key_state, "=", False)]
to_zip = address.zip
domain += [
"|",
(key_zip_to, ">=", to_zip),
(key_zip_to, "=", False),
"|",
(key_zip_from, "<=", to_zip),
(key_zip_from, "=", False),
]
return domain

def fiscal_position_map(self, **kwargs):
Expand Down Expand Up @@ -255,3 +306,41 @@

def apply_fiscal_mapping(self, **kwargs):
return self.fiscal_position_map(**kwargs)

@api.model
def create(self, vals):
for address_type in ["invoice", "shipping"]:
key_zip_from = "to_%s_zip_from" % address_type
key_zip_to = "to_%s_zip_to" % address_type
zip_from = vals.get(key_zip_from)
zip_to = vals.get(key_zip_to)
if zip_from and zip_to:
vals[key_zip_from], vals[key_zip_to] = self.env[
"account.fiscal.position"
]._convert_zip_values(zip_from, zip_to)
return super().create(vals)

def write(self, vals):
for rec in self:
new_vals = vals.copy()

Check warning on line 325 in account_fiscal_position_rule/models/account_fiscal_position_rule.py

View check run for this annotation

Codecov / codecov/patch

account_fiscal_position_rule/models/account_fiscal_position_rule.py#L325

Added line #L325 was not covered by tests
for address_type in ["invoice", "shipping"]:
key_zip_from = "to_%s_zip_from" % address_type
key_zip_to = "to_%s_zip_to" % address_type
zip_from = (

Check warning on line 329 in account_fiscal_position_rule/models/account_fiscal_position_rule.py

View check run for this annotation

Codecov / codecov/patch

account_fiscal_position_rule/models/account_fiscal_position_rule.py#L327-L329

Added lines #L327 - L329 were not covered by tests
vals.get(key_zip_from)
if key_zip_from in vals.keys()
else rec[key_zip_from]
)
zip_to = (

Check warning on line 334 in account_fiscal_position_rule/models/account_fiscal_position_rule.py

View check run for this annotation

Codecov / codecov/patch

account_fiscal_position_rule/models/account_fiscal_position_rule.py#L334

Added line #L334 was not covered by tests
vals.get(key_zip_to)
if key_zip_to in vals.keys()
else rec[key_zip_to]
)
if zip_from and zip_to:
new_vals[key_zip_from], new_vals[key_zip_to] = self.env[

Check warning on line 340 in account_fiscal_position_rule/models/account_fiscal_position_rule.py

View check run for this annotation

Codecov / codecov/patch

account_fiscal_position_rule/models/account_fiscal_position_rule.py#L340

Added line #L340 was not covered by tests
"account.fiscal.position"
]._convert_zip_values(
zip_from or rec[key_zip_from], zip_to or rec[key_zip_to]
)
super(AccountFiscalPositionRule, rec).write(new_vals)
return True

Check warning on line 346 in account_fiscal_position_rule/models/account_fiscal_position_rule.py

View check run for this annotation

Codecov / codecov/patch

account_fiscal_position_rule/models/account_fiscal_position_rule.py#L345-L346

Added lines #L345 - L346 were not covered by tests
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def setUpClass(cls):
cls.partner_01.country_id = cls.country_us
cls.partner_02 = cls.env.ref("base.res_partner_2")
cls.partner_03 = cls.env.ref("base.res_partner_address_34")
cls.partner_04 = cls.env.ref("base.res_partner_3")
cls.partner_04.write({"zip": "12345"})
# Chart template
cls.chart_template_01 = cls.env.ref(
"l10n_generic_coa.configurable_chart_template"
Expand Down Expand Up @@ -77,6 +79,21 @@ def setUpClass(cls):
"use_sale": True,
}
)
cls.fp_rule_02 = cls.fiscal_position_rule_model.create(
{
"name": "Fake rule with ZIP",
"company_id": cls.company_main.id,
"fiscal_position_id": cls.fiscal_position_02.id,
"use_sale": True,
"to_invoice_country": cls.country_us.id,
"to_invoice_zip_from": "00000",
"to_invoice_zip_to": "20000",
"to_shipping_country": cls.country_us.id,
"to_shipping_zip_from": "00000",
"to_shipping_zip_to": "20000",
"sequence": 9,
}
)

def test_01(self):
"""
Expand Down Expand Up @@ -148,3 +165,20 @@ def test_04(self):
kw = {"company_id": self.company_main, "partner_id": self.partner_02}
res = self.fp_rule_01.fiscal_position_map(**kw)
self.assertEqual(res, self.fiscal_position_01)

def test_05(self):
"""
Data:
- /
Test case:
- Trigger the mapping of specific partner
Expected result:
- The right rule is returned
"""
kw = {
"company_id": self.company_main,
"partner_id": self.partner_04,
"partner_shipping_id": self.partner_04,
}
res = self.fiscal_position_rule_model.fiscal_position_map(**kw)
self.assertEqual(res, self.fiscal_position_02)
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,48 @@
<field name="to_invoice_country_group_id" />
<field name="to_invoice_country" />
<field name="to_invoice_state" />
<label
for="to_invoice_zip_from"
string="Zip Range"
attrs="{'invisible': [('to_invoice_country', '=', False)]}"
/>
<div
attrs="{'invisible': [('to_invoice_country', '=', False)]}"
>
<span> From </span>
<field
name="to_invoice_zip_from"
class="oe_inline"
/>
<div class="oe_edit_only" />
<span> To </span>
<field name="to_invoice_zip_to" class="oe_inline" />
</div>
</group>
<group string="Shipping" name="shipping">
<field name="to_shipping_country_group_id" />
<field name="to_shipping_country" />
<field name="to_shipping_state" />
<label
for="to_shipping_zip_from"
string="Zip Range"
attrs="{'invisible': [('to_shipping_country', '=', False)]}"
/>
<div
attrs="{'invisible': [('to_shipping_country', '=', False)]}"
>
<span> From </span>
<field
name="to_shipping_zip_from"
class="oe_inline"
/>
<div class="oe_edit_only" />
<span> To </span>
<field
name="to_shipping_zip_to"
class="oe_inline"
/>
</div>
</group>
</group>
<group string="Configuration" name="configuration" colspan="4">
Expand Down
Loading