Skip to content

Commit

Permalink
[IMP] l10n_br_account_withholding: handling of partner_id assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
kaynnan committed Jul 17, 2024
1 parent 0ea78da commit 97e8748
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 1 deletion.
1 change: 1 addition & 0 deletions l10n_br_account_withholding/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import models
from . import wizard
3 changes: 3 additions & 0 deletions l10n_br_account_withholding/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"data": [
"views/l10n_br_fiscal_tax_group.xml",
"views/account_move.xml",
"views/res_partner.xml",
"wizard/l10n_br_account_wh_wizard.xml",
"security/ir.model.access.csv",
],
"demo": [
"demo/ir.property.xml",
Expand Down
1 change: 1 addition & 0 deletions l10n_br_account_withholding/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from . import account_move
from . import l10n_br_fiscal_tax_group
from . import account_move_line
from . import res_partner
13 changes: 12 additions & 1 deletion l10n_br_account_withholding/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,19 @@ def _prepare_wh_invoice(self, move_line, fiscal_group):
"""
wh_date_invoice = move_line.move_id.date
wh_due_invoice = wh_date_invoice.replace(day=fiscal_group.wh_due_day)

city_id = (
self.invoice_line_ids[0].issqn_fg_city_id
if self.invoice_line_ids[0].issqn_fg_city_id
else self.partner_id.city_id
)
partner_wh = self.env["res.partner"].search(
[("city_id", "=", city_id.id), ("wh_cityhall", "=", True)], limit=1
)
partner_id = partner_wh if partner_wh else fiscal_group.partner_id

values = {
"partner_id": fiscal_group.partner_id.id,
"partner_id": partner_id,
"date": wh_date_invoice,
"invoice_date": wh_date_invoice,
"invoice_date_due": wh_due_invoice + relativedelta(months=1),
Expand Down
11 changes: 11 additions & 0 deletions l10n_br_account_withholding/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright 2024 - TODAY, Kaynnan Lemes <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import fields, models


class ResPartner(models.Model):

_inherit = "res.partner"

wh_cityhall = fields.Boolean("Is a WH City Hall", readonly=True)
2 changes: 2 additions & 0 deletions l10n_br_account_withholding/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_l10n_br_account_wh_wizard,access_l10n_br_account_wh_wizard,model_l10n_br_account_wh_wizard,account.group_account_invoice,1,1,1,0
36 changes: 36 additions & 0 deletions l10n_br_account_withholding/views/res_partner.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2024 - TODAY, Kaynnan Lemes <[email protected]>
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
<odoo>

<record model="ir.ui.view" id="res_partner_form_view">
<field name="name">res.partner.form (in l10n_br_account_withholding)</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form" />
<field name="arch" type="xml">
<xpath expr="//notebook" position="inside">
<page string="Withhoulding" name="partner_wh">
<label for="wh_cityhall" />
<field name="wh_cityhall" />
</page>
</xpath>
</field>
</record>

<record id="res_partner_filter_view" model="ir.ui.view">
<field name="name">res.partner.filter (in l10n_br_account_withholding)</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_res_partner_filter" />
<field name="arch" type="xml">
<xpath expr="//search" position="inside">
<separator />
<filter
string="WH City Hall"
name="wh_cityhall"
domain="[('wh_cityhall', '=', True)]"
/>
</xpath>
</field>
</record>

</odoo>
1 change: 1 addition & 0 deletions l10n_br_account_withholding/wizard/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import l10n_br_account_wh_wizard
51 changes: 51 additions & 0 deletions l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright (C) 2024 - TODAY, Kaynnan Lemes <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, fields, models
from odoo.exceptions import UserError


class AccountWHWizard(models.TransientModel):
"""
A wizard to convert a res.partner record to a Withholding Partner
"""

_name = "l10n_br_account.wh.wizard"
_description = "Partner WH Record Conversion"

wh_record_type = fields.Selection([("cityhall", "City Hall")], "Record Type")

def action_convert(self):
partners = self.env["res.partner"].browse(self._context.get("active_ids", []))

Check warning on line 19 in l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.py#L19

Added line #L19 was not covered by tests
for partner in partners:
if self.wh_record_type == "cityhall":
self.action_convert_cityhall(partner)

Check warning on line 22 in l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.py#L22

Added line #L22 was not covered by tests
else:
self.action_reset_convert(partner)
return {"type": "ir.actions.act_window_close"}

Check warning on line 25 in l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.py#L24-L25

Added lines #L24 - L25 were not covered by tests

def _prepare_wh_conversion(self, partner):
return {"partner_id": partner.id}

Check warning on line 28 in l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.py#L28

Added line #L28 was not covered by tests

def action_convert_cityhall(self, partner):
# Verify if there is already a partner with wh_cityhall=True in the same city
existing_partner = self.env["res.partner"].search(

Check warning on line 32 in l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.py#L32

Added line #L32 was not covered by tests
[
("city_id", "=", partner.city_id.id),
("wh_cityhall", "=", True),
("id", "!=", partner.id),
],
limit=1,
)

if existing_partner:
raise UserError(

Check warning on line 42 in l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.py#L42

Added line #L42 was not covered by tests
_("It is allowed to have only one City Hall partner per city.")
)

# If no such partner exists, proceed with the conversion
self._prepare_wh_conversion(partner)
partner.write({"wh_cityhall": True})

Check warning on line 48 in l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.py#L47-L48

Added lines #L47 - L48 were not covered by tests

def action_reset_convert(self, partner):
partner.write({"wh_cityhall": False})

Check warning on line 51 in l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.py#L51

Added line #L51 was not covered by tests
38 changes: 38 additions & 0 deletions l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<odoo>
<!-- Res Partner Action -->
<record id="partner_wh_action" model="ir.actions.act_window">
<field name="name">Convert to a Withholding Partner</field>
<field name="res_model">l10n_br_account.wh.wizard</field>
<field name="view_mode">form</field>
<field name="target">new</field>
<field name="binding_model_id" ref="base.model_res_partner" />
<field name="binding_view_types">form,list</field>
<field name="groups_id" eval="[(4, ref('account.group_account_invoice'))]" />
</record>
<!-- wizard view -->
<record id="partner_wizard_view" model="ir.ui.view">
<field name="name">Convert to WH</field>
<field name="model">l10n_br_account.wh.wizard</field>
<field name="arch" type="xml">
<form string="Convert to a Partner Withholding">
<div>
Select whether you want to convert this record to WH.
</div>
<group>
<group>
<field name="wh_record_type" widget="selection" />
</group>
</group>
<footer>
<button
string="Convert"
name="action_convert"
type="object"
class="btn-primary"
/>
<button string="Cancel" class="btn-default" special="cancel" />
</footer>
</form>
</field>
</record>
</odoo>

0 comments on commit 97e8748

Please sign in to comment.