diff --git a/l10n_br_account_withholding/__init__.py b/l10n_br_account_withholding/__init__.py index 0650744f6bc6..9b4296142f47 100644 --- a/l10n_br_account_withholding/__init__.py +++ b/l10n_br_account_withholding/__init__.py @@ -1 +1,2 @@ from . import models +from . import wizard diff --git a/l10n_br_account_withholding/__manifest__.py b/l10n_br_account_withholding/__manifest__.py index a84e12d3e499..3d047aaa6ddd 100644 --- a/l10n_br_account_withholding/__manifest__.py +++ b/l10n_br_account_withholding/__manifest__.py @@ -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", diff --git a/l10n_br_account_withholding/models/__init__.py b/l10n_br_account_withholding/models/__init__.py index e958216983e8..d5d20ae7e74a 100644 --- a/l10n_br_account_withholding/models/__init__.py +++ b/l10n_br_account_withholding/models/__init__.py @@ -1,3 +1,4 @@ from . import account_move from . import l10n_br_fiscal_tax_group from . import account_move_line +from . import res_partner diff --git a/l10n_br_account_withholding/models/account_move.py b/l10n_br_account_withholding/models/account_move.py index 68918e32b4fa..e6800d92e0e7 100644 --- a/l10n_br_account_withholding/models/account_move.py +++ b/l10n_br_account_withholding/models/account_move.py @@ -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), diff --git a/l10n_br_account_withholding/models/res_partner.py b/l10n_br_account_withholding/models/res_partner.py new file mode 100644 index 000000000000..f0793f7a8bc7 --- /dev/null +++ b/l10n_br_account_withholding/models/res_partner.py @@ -0,0 +1,11 @@ +# Copyright 2024 - TODAY, Kaynnan Lemes +# 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) diff --git a/l10n_br_account_withholding/security/ir.model.access.csv b/l10n_br_account_withholding/security/ir.model.access.csv new file mode 100644 index 000000000000..4d30d068777f --- /dev/null +++ b/l10n_br_account_withholding/security/ir.model.access.csv @@ -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 diff --git a/l10n_br_account_withholding/views/res_partner.xml b/l10n_br_account_withholding/views/res_partner.xml new file mode 100644 index 000000000000..bfc4ea0ef541 --- /dev/null +++ b/l10n_br_account_withholding/views/res_partner.xml @@ -0,0 +1,36 @@ + + + + + + res.partner.form (in l10n_br_account_withholding) + res.partner + + + + + + + + + + + res.partner.filter (in l10n_br_account_withholding) + res.partner + + + + + + + + + + diff --git a/l10n_br_account_withholding/wizard/__init__.py b/l10n_br_account_withholding/wizard/__init__.py new file mode 100644 index 000000000000..67a0be7a76d0 --- /dev/null +++ b/l10n_br_account_withholding/wizard/__init__.py @@ -0,0 +1 @@ +from . import l10n_br_account_wh_wizard diff --git a/l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.py b/l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.py new file mode 100644 index 000000000000..769462ebb337 --- /dev/null +++ b/l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.py @@ -0,0 +1,51 @@ +# Copyright (C) 2024 - TODAY, Kaynnan Lemes +# 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", [])) + for partner in partners: + if self.wh_record_type == "cityhall": + self.action_convert_cityhall(partner) + else: + self.action_reset_convert(partner) + return {"type": "ir.actions.act_window_close"} + + def _prepare_wh_conversion(self, partner): + return {"partner_id": partner.id} + + 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( + [ + ("city_id", "=", partner.city_id.id), + ("wh_cityhall", "=", True), + ("id", "!=", partner.id), + ], + limit=1, + ) + + if existing_partner: + raise UserError( + _("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}) + + def action_reset_convert(self, partner): + partner.write({"wh_cityhall": False}) diff --git a/l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.xml b/l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.xml new file mode 100644 index 000000000000..33743a683555 --- /dev/null +++ b/l10n_br_account_withholding/wizard/l10n_br_account_wh_wizard.xml @@ -0,0 +1,38 @@ + + + + Convert to a Withholding Partner + l10n_br_account.wh.wizard + form + new + + form,list + + + + + Convert to WH + l10n_br_account.wh.wizard + +
+
+ Select whether you want to convert this record to WH. +
+ + + + + + +
+
+
+