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 66d0bcc
Show file tree
Hide file tree
Showing 8 changed files with 122 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
16 changes: 16 additions & 0 deletions account_post_in_background/__manifest__.py
@@ -0,0 +1,16 @@
{
'name': 'Account Post In Background',
'version': "16.0.1.0.0",
'author': 'ADHOC SA',
'depends': [
'account',
],
'data': [
'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
22 changes: 22 additions & 0 deletions account_post_in_background/models/account_move.py
@@ -0,0 +1,22 @@
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.")

@api.model
def _cron_background_post_invoices(self):
moves = self.search([('background_validate', '=', True)])
if moves:
try:
moves[0]._post()
moves[0]._cr.commit()
except Exception as exp:
moves[0].background_validate = False
moves[0].message_post(_('We tried to validate this invoice on the background but got this error: %s') % str(exp))
# 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()
1 change: 1 addition & 0 deletions account_post_in_background/wizards/__init__.py
@@ -0,0 +1 @@
from . import validate_account_move
35 changes: 35 additions & 0 deletions account_post_in_background/wizards/validate_account_move.py
@@ -0,0 +1,35 @@
from odoo import api, fields, models
from odoo.exceptions import UserError


class ValidateAccountMove(models.TransientModel):

_inherit = "validate.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['count_inv']: self.env['account.move'].search_count(domain)
return res

def action_background_validate(self):
# TODO KZ esto no me gusta que se haga varias veces, con que lo calculemos una sola vez deberia de ir
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."))

self.env['account.move'].search(domain).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="attrs">{'invisible': [('count_inv', '&gt;=', 20)]}</attribute>
</button>

</field>
</record>

</data>
</odoo>

0 comments on commit 66d0bcc

Please sign in to comment.