forked from ingadhoc/account-invoicing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_account_move.py
More file actions
67 lines (51 loc) · 2.98 KB
/
validate_account_move.py
File metadata and controls
67 lines (51 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import logging
from odoo import _, fields, models
from odoo.exceptions import UserError
_logger = logging.getLogger(__name__)
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")
batch_size = fields.Integer(compute="_compute_batch_size")
force_background = fields.Integer(compute="_compute_force_background")
def _compute_batch_size(self):
self.batch_size = self.env["ir.config_parameter"].sudo().get_param("account_background_post.batch_size", 20)
def _compute_force_background(self):
for rec in self:
rec.force_background = rec.count_inv > rec.batch_size
def default_get(self, fields):
res = super().default_get(fields)
if res:
res["count_inv"] = len(res["move_ids"][0][2])
return res
def action_background_post(self):
self.move_ids.background_post = True
self.env.ref("account_background_post.ir_cron_background_post_invoices")._trigger()
def validate_move(self):
"""Sobre escribimos este método para el caso de varias invoices para hacer:
1. Que en lugar de hacer un _post hacemos un _action_post. esto porque odoo hace cosas como lanzar acciones y correr validaciones solo cuando corremos el action_post. y nosotros queremos que esas se apliquen. eso incluye el envio de email cuando validamos la factura.
2. que se valida cada factura una a uno y no todas jutnas. esto para evitar problemas que puedan surgier
por errores, que no se puedan ejecutar los commits que tenemos para guardar el estado de factura electronica y tambien para asegurar que tras cada fatura se envie su email, asi si hay un error posterior las facturas que fueron validadas aseguremos que hayan sido totalmente procesadas.
3. Limitamos sui el usuario quiere validar mas facturas que el batch size definido directamente
le pedimos que las valide en background."""
if self.count_inv:
if self.count_inv > self.batch_size:
raise UserError(
_(
"You can only validate on batches of size < %s invoices. If you need to validate"
" more invoices please use the validate on background option",
self.batch_size,
)
)
for move in self.move_ids:
_logger.info("Validating invoice %s", move.id)
move.action_post()
move._cr.commit()
return {"type": "ir.actions.act_window_close"}
else:
return super().validate_move()
def validate_move_confirm(self):
"""Bridge method called from the view's Confirm button renamed to
avoid name collision. It delegates to `validate_move` to keep the
same behaviour."""
return self.validate_move()