Skip to content

Commit

Permalink
[REF] account_multicompany_ux: new wizard to change company on invoices
Browse files Browse the repository at this point in the history
Lo hacemos por estos motivos:
a) tenemos muchos errores en distintos casos (viniendo desde OC, cambiando partner y luego cia, etc). Tanto depends/onchange lo hace super complicado
b) además con esto nos evitamos necesitar el pr odoo/odoo#121725
  • Loading branch information
jjscarafia committed Oct 10, 2023
1 parent 35365e6 commit ce5004e
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 13 deletions.
1 change: 1 addition & 0 deletions account_multicompany_ux/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
# directory
##############################################################################
from . import models
from . import wizards
4 changes: 3 additions & 1 deletion account_multicompany_ux/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
##############################################################################
{
'name': 'Account Multicompany Usability',
'version': "16.0.1.0.0",
'version': "16.0.1.1.0",
'author': 'ADHOC SA',
'website': 'www.adhoc.com.ar',
'license': 'AGPL-3',
Expand All @@ -34,6 +34,8 @@
'views/product_template_views.xml',
'views/product_product_views.xml',
'views/account_journal_dashboard_views.xml',
'wizards/account_change_company_views.xml',
'views/account_move_views.xml',
'security/account_multicompany_ux_security.xml',
'security/ir.model.access.csv',
],
Expand Down
12 changes: 0 additions & 12 deletions account_multicompany_ux/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,3 @@ def check_company(self):
if move.company_id.consolidation_company:
raise ValidationError(_(
'You can not create entries on a consolidation company'))

@api.depends('company_id', 'invoice_filter_type_domain')
def _compute_suitable_journal_ids(self):
"""
We override this method to add filter by companies in the env instead of the company of the user
For this to work the pr is needed https://github.com/odoo/odoo/pull
"""
for m in self:
journal_type = m.invoice_filter_type_domain or 'general'
company_ids = self.env.companies.ids
domain = [('company_id', 'in', company_ids), ('type', '=', journal_type)]
m.suitable_journal_ids = self.env['account.journal'].search(domain)
1 change: 1 addition & 0 deletions account_multicompany_ux/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_res_company_property,access_res_company_property,model_res_company_property,base.group_user,1,1,1,1
account_multicompany_ux.access_account_change_company,access_account_change_company,account_multicompany_ux.model_account_change_company,base.group_user,1,1,1,0
19 changes: 19 additions & 0 deletions account_multicompany_ux/views/account_move_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<odoo>

<record id="view_move_form" model="ir.ui.view">
<field name="model">account.move</field>
<field name="name">account.move.form</field>
<field name="inherit_id" ref="account.view_move_form"/>
<field name="arch" type="xml">
<label for="journal_id" position="before">
<label for="company_id" readonly="True" groups="base.group_multi_company"/>
<div groups="base.group_multi_company">
<field name="company_id" readonly="True" class="oe_inline"/>
<button name="%(action_account_change_company)d" type="action" attrs="{'invisible':[('state','!=','draft')]}" icon="fa-pencil" class="btn-link" title="Change Company" />
</div>
</label>

</field>
</record>

</odoo>
5 changes: 5 additions & 0 deletions account_multicompany_ux/wizards/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
##############################################################################
# For copyright and license notices, see __manifest__.py file in module root
# directory
##############################################################################
from . import account_change_company
70 changes: 70 additions & 0 deletions account_multicompany_ux/wizards/account_change_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
##############################################################################
# For copyright and license notices, see __manifest__.py file in module root
# directory
##############################################################################
from odoo import fields, models, api, _


class AccountChangeCurrency(models.TransientModel):
_name = 'account.change.company'
_description = 'Change Company'

@api.model
def get_move(self):
move = self.env['account.move'].browse(
self._context.get('active_id', False))
return move

move_id = fields.Many2one('account.move', default=get_move)
company_ids = fields.Many2many('res.company', compute='_compute_company_ids')
company_id = fields.Many2one(
'res.company', required=True, ondelete='cascade',
compute='_compute_company', store=True, readonly=False)
journal_id = fields.Many2one(
'account.journal', required=True, ondelete='cascade', domain="[('id', 'in', suitable_journal_ids)]",
store=True, compute='_compute_journal', readonly=False)
suitable_journal_ids = fields.Many2many(
'account.journal',
compute='_compute_suitable_journal_ids',
)

@api.depends('move_id')
@api.depends_context('allowed_company_ids')
def _compute_company_ids(self):
self.company_ids = self.env.companies - self.move_id.company_id

@api.depends('company_ids')
def _compute_company(self):
for rec in self:
rec.company_id = self.company_ids[:1]

@api.depends('suitable_journal_ids')
def _compute_journal(self):
for rec in self:
rec.journal_id = rec.suitable_journal_ids[:1]

@api.depends('move_id', 'company_id')
def _compute_suitable_journal_ids(self):
"""
We override this method to add filter by companies in the env instead of the company of the user
For this to work the pr is needed https://github.com/odoo/odoo/pull
"""
for rec in self:
journal_type = rec.move_id.invoice_filter_type_domain or 'general'
domain = [('company_id', '=', rec.company_id._origin.id), ('type', '=', journal_type)]
rec.suitable_journal_ids = self.env['account.journal'].search(domain)

def change_company(self):
self.ensure_one()
self.move_id.write({
'company_id': self.company_id.id,
'journal_id': self.journal_id.id,
})
self.move_id._compute_partner_bank_id()
# si el payment term tiene compañía y es distinta a la que elegimos, forzamos recomputo
if self.move_id.invoice_payment_term_id.company_id and self.move_id.invoice_payment_term_id.company_id != self.company_id:
self.move_id._compute_invoice_payment_term_id()
self.move_id.line_ids.with_company(self.company_id.id)._compute_account_id()
self.move_id.invoice_line_ids.with_company(self.company_id.id)._compute_tax_ids()
for invoice_line in self.move_id.invoice_line_ids.filtered(lambda x: not x.product_id).with_company(self.company_id.id):
invoice_line.tax_ids = invoice_line._get_computed_taxes()
33 changes: 33 additions & 0 deletions account_multicompany_ux/wizards/account_change_company_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="view_account_change_company" model="ir.ui.view">
<field name="name">Change Company</field>
<field name="model">account.change.company</field>
<field name="arch" type="xml">
<form>
<group>
<field name="move_id" invisible="1"/>
<field name="suitable_journal_ids" invisible="1"/>
<field name="company_ids" invisible="1"/>
<field name="company_id" domain="[('id', 'in', company_ids)]"/>
<field name="journal_id"/>
</group>
<footer>
<button name="change_company" string="Change Company" type="object" class="oe_highlight"/>
or
<button string="Cancel" class="oe_link" special="cancel"/>
</footer>
</form>
</field>
</record>

<record id="action_account_change_company" model="ir.actions.act_window">
<field name="name">Change Company</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">account.change.company</field>
<field name="view_mode">form</field>
<field name="view_id" ref="view_account_change_company"/>
<field name="context">{}</field>
<field name="target">new</field>
</record>
</odoo>

0 comments on commit ce5004e

Please sign in to comment.