Skip to content

Commit

Permalink
[ADD] account_post_in_background: add new module
Browse files Browse the repository at this point in the history
  • Loading branch information
zaoral committed Sep 13, 2023
1 parent b51c69c commit a37dbc9
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 0 deletions.
2 changes: 2 additions & 0 deletions account_post_in_background/__init__.py
@@ -0,0 +1,2 @@
from . import models
from . import wizards
17 changes: 17 additions & 0 deletions account_post_in_background/__manifest__.py
@@ -0,0 +1,17 @@
{
'name': 'Account Post In Background',
'version': "16.0.1.0.0",
'author': 'ADHOC SA',
'depends': [
'account',
],
'data': [
'views/account_move_views.xml',
'wizards/validate_account_move_views.xml',
'data/ir_cron.xml',
],
'license': 'AGPL-3',
'installable': True,
'auto_install': False,
'application': False,
}
15 changes: 15 additions & 0 deletions account_post_in_background/data/ir_cron.xml
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>

<record forcecreate="True" id="ir_cron_background_post_invoices" model="ir.cron">
<field name="name">Background Post Invoices: cron</field>
<field name="model_id" ref="account.model_account_move"/>
<field name="state">code</field>
<field name="code">model._cron_background_post_invoices()</field>
<field eval="True" name="active" />
<field name="interval_number">1</field>
<field name="interval_type">hours</field>
<field name="numbercall">-1</field>
</record>

</odoo>
1 change: 1 addition & 0 deletions account_post_in_background/models/__init__.py
@@ -0,0 +1 @@
from . import account_move
31 changes: 31 additions & 0 deletions account_post_in_background/models/account_move.py
@@ -0,0 +1,31 @@
from odoo import _, api, fields, models


class AccountMove(models.Model):

_inherit = 'account.move'

background_validate = fields.Boolean(help="If True then this invoice will be validated in the background by cron.")

def get_internal_partners(self):
res = self.env['res.partner']
for partner in self.message_partner_ids:
if partner.user_ids and all(user._is_internal() for user in partner.user_ids):
res |= partner
# TODO WIP
return res

@api.model
def _cron_background_post_invoices(self):
moves = self.search([('background_validate', '=', True)])
if moves:
try:
moves[0]._post()
moves[0].background_validate = False
moves[0]._cr.commit()
except Exception as exp:
moves[0].background_validate = False
moves[0].message_post(body=_('We tried to validate this invoice on the background but got this error: %s') % str(exp), message_type='comment', subtype_xmlid='mail.mt_comment', partner_ids=moves[0].get_internal_partners().ids)
# TODO KZ revisar que solo se este enviando a followers internos del documento
if len(moves) > 1:
self.env.ref('account_post_in_background.ir_cron_background_post_invoices')._trigger()
28 changes: 28 additions & 0 deletions account_post_in_background/views/account_move_views.xml
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>

<record id="view_out_invoice_tree" model="ir.ui.view">
<field name="name">account.move.tree.inherit</field>
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_out_invoice_tree" />
<field name="arch" type="xml">
<field name="state" position="before">
<field name="background_validate" invisible="1"/>
</field>
</field>
</record>

<record id="view_account_invoice_filter" model="ir.ui.view">
<field name="name">account.invoice.select.inherit</field>
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_account_invoice_filter"/>
<field name="arch" type="xml">
<filter name="to_check" position="after">
<filter string="To validate in background" name="background_post" domain="[('background_validate', '=', True)]"/>
</filter>
</field>
</record>

</data>
</odoo>
1 change: 1 addition & 0 deletions account_post_in_background/wizards/__init__.py
@@ -0,0 +1 @@
from . import validate_account_move
28 changes: 28 additions & 0 deletions account_post_in_background/wizards/validate_account_move.py
@@ -0,0 +1,28 @@
from odoo import _, api, fields, models
from odoo.exceptions import UserError


class ValidateAccountMove(models.TransientModel):

_inherit = "validate.account.move"

move_ids = fields.Many2many('account.move')
count_inv = fields.Integer(help="Technical field to know the number of invoices selected from the wizard")

@api.model
def default_get(self, fields):
res = super().default_get(fields)

if self._context.get('active_model') == 'account.move':
domain = [('id', 'in', self._context.get('active_ids', [])), ('state', '=', 'draft')]
elif self._context.get('active_model') == 'account.journal':
domain = [('journal_id', '=', self._context.get('active_id')), ('state', '=', 'draft')]
else:
raise UserError(_("Missing 'active_model' in context."))

res['move_ids'] = self.env['account.move'].search(domain).filtered('line_ids').ids
res['count_inv'] = len(res['move_ids'])
return res

def action_background_validate(self):
self.move_ids.background_validate = True
30 changes: 30 additions & 0 deletions account_post_in_background/wizards/validate_account_move_views.xml
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>

<record id="validate_account_move_view" model="ir.ui.view">
<field name="name">Post Journal Entries</field>
<field name="model">validate.account.move</field>
<field name="inherit_id" ref="account.validate_account_move_view"/>
<field name="arch" type="xml">

<field name="force_post" position="after">
<field name="count_inv" invisible="1"/>
</field>

<button name="validate_move" position="before">
<button string="Post in Background" name="action_background_validate" type="object" default_focus="1" class="btn-primary" data-hotkey="a" help="With this, all the invoices selected to be validated will be marked and they will be validated one by one. If an error is found when validating any invoice, the automatic validation of the same will be unmarked and it will be notified via messaging"/>
</button>

<button name="validate_move" position="attributes">
<attribute name="default_focus"/>
<attribute name="class"/>
<attribute name="confirm">Only use this for just a few invoices</attribute>
<attribute name="attrs">{'invisible': [('count_inv', '&gt;=', 20)]}</attribute>
</button>

</field>
</record>

</data>
</odoo>

0 comments on commit a37dbc9

Please sign in to comment.