From da73f28cefc61ac9feafc3c1be58e14f9a90ea2c Mon Sep 17 00:00:00 2001 From: Maximiliano Mezzavilla Date: Wed, 7 Aug 2024 17:16:26 -0300 Subject: [PATCH] [FIX] l10n_ar_sale_order: Fix para que se setee correctamente la secuencia de la orden de venta --- l10n_ar_sale/models/sale_order.py | 43 +++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/l10n_ar_sale/models/sale_order.py b/l10n_ar_sale/models/sale_order.py index 227024ba..7e74f6de 100644 --- a/l10n_ar_sale/models/sale_order.py +++ b/l10n_ar_sale/models/sale_order.py @@ -41,8 +41,8 @@ def _compute_vat_discriminated(self): @api.depends('company_id') def _compute_sale_checkbook(self): - for rec in self: - if self.env.user.has_group('l10n_ar_sale.use_sale_checkbook') and rec.company_id: + for rec in self.filtered(lambda x: not x.sale_checkbook_id): + if self.env.user.has_group('l10n_ar_sale.use_sale_checkbook') and rec.company_id and not rec.name: rec.sale_checkbook_id = rec._get_sale_checkbook() else: rec.sale_checkbook_id = False @@ -57,16 +57,43 @@ def _get_sale_checkbook(self): @api.model_create_multi def create(self, vals): + """" + En caso de la creacion de una OV totalmente nueva consumimos el numero de la secuencia + """ for val in vals: if self.env.user.has_group('l10n_ar_sale.use_sale_checkbook') and \ - val.get('name', _('New')) == _('New') and \ - val.get('sale_checkbook_id'): - sale_checkbook = self.env['sale.checkbook'].browse( - val.get('sale_checkbook_id')) - val['name'] = sale_checkbook.sequence_id and\ - sale_checkbook.sequence_id._next() or _('New') + val.get('sale_checkbook_id'): + sale_checkbook = self.env['sale.checkbook'].browse(val.get('sale_checkbook_id')) + if val.get('name', _('New')) == _('New'): + val['name'] = sale_checkbook.sequence_id._next() + else: + number_next = sale_checkbook.sequence_id.number_next_actual + val['name'] = sale_checkbook.sequence_id.get_next_char(number_next) return super(SaleOrder, self).create(vals) + def write(self, vals): + """A sale checkbook could have a different order sequence, so we could + need to change it accordingly""" + if self.env.user.has_group('l10n_ar_sale.use_sale_checkbook') and vals.get('sale_checkbook_id'): + sale_checkbook = self.env['sale.checkbook'].browse(vals['sale_checkbook_id']) + if sale_checkbook.sequence_id: + for record in self: + if record.sale_checkbook_id != sale_checkbook and record.state in {"draft", "sent"}: + if not record.name or record.name == _('Nuevo'): + record.name = sale_checkbook.sequence_id._next() + else: + number_next = sale_checkbook.sequence_id.number_next_actual + vals['name'] = sale_checkbook.sequence_id.get_next_char(number_next) + return super(SaleOrder, record).write(vals) + return super().write(vals) + + def copy(self, default=None): + """Necesitamos que cuando duplicamos la OV mantenga el sale_checkbook_id""" + default = dict(default or {}) + if self.sale_checkbook_id: + default['sale_checkbook_id'] = self.sale_checkbook_id.id + return super().copy(default) + def _compute_tax_totals(self): """ Mandamos en contexto el invoice_date para calculo de impuesto con partner aliquot ver módulo l10n_ar_account_withholding. """