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 @@ class AccountFiscalPositionRule(models.Model):
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 @@ class AccountFiscalPositionRule(models.Model):
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 @@ class AccountFiscalPositionRule(models.Model):
),
)

@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(
_('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 @@ def _onchange_to_invoice_country_group_id(self):
]
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

@api.onchange("to_shipping_country_group_id")
def _onchange_to_shipping_country_group_id(self):
self.ensure_one()
Expand All @@ -149,6 +184,11 @@ def _onchange_to_shipping_country_group_id(self):
]
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

@api.onchange("company_id")
def onchange_company(self):
self.from_country = self.company_id.country_id
Expand Down Expand Up @@ -198,6 +238,8 @@ def _map_domain(self, partner, addrs, company, **kwargs):
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 @@ def _map_domain(self, partner, addrs, company, **kwargs):
]
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 fiscal_position_map(self, **kwargs):

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()
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)
if key_zip_from in vals.keys()
else rec[key_zip_from]
)
zip_to = (
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[
"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
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