From 29eb5bcd9254cac313bcc89cd6f8255f50e7f3f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Lodeiros?= Date: Wed, 17 Apr 2024 10:49:49 +0200 Subject: [PATCH] [IMP]pms:improve reconcile move line and sync with folio sale lines --- pms/models/account_move.py | 41 -------------------------- pms/models/account_move_line.py | 52 +++++++++++++++++++++++++++++++-- pms/models/folio_sale_line.py | 29 ++++++++++++++---- 3 files changed, 72 insertions(+), 50 deletions(-) diff --git a/pms/models/account_move.py b/pms/models/account_move.py index 752aa6444..ffab6a17d 100644 --- a/pms/models/account_move.py +++ b/pms/models/account_move.py @@ -276,12 +276,6 @@ class AccountMove(models.Model): .line_ids.filtered( lambda line: line.account_id == pay_term_lines.account_id and line.folio_ids in move.folio_ids - and ( - line.move_id.partner_id == move.partner_id - or not line.move_id.partner_id - or move.partner_id - == self.env.ref("pms.various_pms_partner") - ) ) ) to_reconcile = self.match_pays_by_amount( @@ -302,41 +296,6 @@ class AccountMove(models.Model): self._autoreconcile_folio_payments() return res - def reconcile(self): - """ - Reconcile the account move - """ - res = super(AccountMove, self).reconcile() - # Update partner in payments and statement lines - for record in self: - if record.payment_id: - old_payment_partner = record.payment_id.partner_id - if old_payment_partner != record.partner_id: - record.payment_id.partner_id = record.partner_id - if old_payment_partner: - record.payment_id.message_post( - body=_( - f""" - Partner modify automatically from invoice {record.name}: - {old_payment_partner.name} to {record.partner_id.name} - """ - ) - ) - if record.statement_line_id: - old_statement_partner = record.statement_line_id.partner_id - if old_statement_partner != record.partner_id: - record.statement_line_id.partner_id = record.partner_id - if old_statement_partner: - record.statement_line_id.message_post( - body=_( - f""" - Partner modify automatically from invoice {record.name}: - {old_statement_partner.name} to {record.partner_id.name} - """ - ) - ) - return res - def match_pays_by_amount(self, payments, invoice): """ Match payments by amount diff --git a/pms/models/account_move_line.py b/pms/models/account_move_line.py index f24cc824d..3b9f287a3 100644 --- a/pms/models/account_move_line.py +++ b/pms/models/account_move_line.py @@ -1,7 +1,7 @@ # Copyright 2017 Alexandre Díaz # Copyright 2017 Dario Lodeiros # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) -from odoo import api, fields, models +from odoo import _, api, fields, models class AccountMoveLine(models.Model): @@ -63,8 +63,7 @@ class AccountMoveLine(models.Model): def _compute_name(self): res = super()._compute_name() for record in self: - if record.folio_line_ids and not record.name_changed_by_user: - record.name_changed_by_user = False + if record.folio_line_ids and not record.name: record.name = self.env["folio.sale.line"].generate_folio_sale_name( record.folio_line_ids.reservation_id, record.product_id, @@ -129,3 +128,50 @@ class AccountMoveLine(models.Model): move.pms_property_id.id or move.move_id.pms_property_id.id ) return result + + def reconcile(self): + """ + Reconcile the account move + """ + res = super(AccountMoveLine, self).reconcile() + # Update partner in payments and statement lines + for record in self: + if record.payment_id: + old_payment_partner = record.payment_id.partner_id + new_payment_partner = record.payment_id.mapped( + "reconciled_invoice_ids.partner_id" + ) + if ( + old_payment_partner != new_payment_partner + and len(new_payment_partner) == 1 + ): + record.payment_id.partner_id = new_payment_partner + if old_payment_partner: + record.payment_id.message_post( + body=_( + f""" + Partner modify automatically from invoice: + {old_payment_partner.name} to {new_payment_partner.name} + """ + ) + ) + if record.statement_line_id: + old_statement_partner = record.statement_line_id.partner_id + new_payment_partner = record.payment_id.mapped( + "reconciled_invoice_ids.partner_id" + ) + if ( + old_statement_partner != new_payment_partner + and len(new_payment_partner) == 1 + ): + record.statement_line_id.partner_id = new_payment_partner + if old_statement_partner: + record.statement_line_id.message_post( + body=_( + f""" + Partner modify automatically from invoice: + {old_statement_partner.name} to {new_payment_partner.name} + """ + ) + ) + return res diff --git a/pms/models/folio_sale_line.py b/pms/models/folio_sale_line.py index e2fa9fc17..c16f8f7c3 100644 --- a/pms/models/folio_sale_line.py +++ b/pms/models/folio_sale_line.py @@ -1006,6 +1006,7 @@ class FolioSaleLine(models.Model): ) # If has draft invoices, we need to update the invoice lines if "draft" in self.mapped("invoice_lines.move_id.state"): + draft_moves = self.env["account.move"] if "product_uom_qty" in values: for line in self: if line.qty_invoiced > values["product_uom_qty"]: @@ -1015,7 +1016,10 @@ class FolioSaleLine(models.Model): " You must reduce the invoiced quantity first." ) ) - for line in self.filtered(lambda l: not l.display_type): + for line in self.filtered( + lambda l: not l.display_type and l.move_id.state == "draft" + ): + draft_moves |= line.invoice_lines.move_id mapped_fields = self._get_mapped_move_line_fields() move_line_vals = [ ( @@ -1036,6 +1040,9 @@ class FolioSaleLine(models.Model): ) if "product_uom_qty" in values: line._mens_update_line_quantity(values) + # avoid draft invoice naming compute + if draft_moves: + draft_moves.name = "/" result = super(FolioSaleLine, self).write(values) return result @@ -1084,12 +1091,22 @@ class FolioSaleLine(models.Model): def unlink(self): for record in self: if record.qty_invoiced > 0: - raise UserError( - _( - "You cannot delete a sale order line once a " - "invoice has been created from it." + # If the invoice line is in draft, unlink it, else raise an error + if record.invoice_lines.filtered(lambda l: l.move_id.state == "draft"): + moves = record.invoice_lines.mapped("move_id") + record.invoice_lines.with_context( + check_move_validity=False + ).filtered(lambda l: l.move_id.state == "draft").unlink() + moves._recompute_dynamic_lines( + recompute_all_taxes=True, recompute_tax_base_amount=True + ) + else: + raise UserError( + _( + "You cannot delete a sale order line once a " + "invoice has been created from it." + ) ) - ) return super(FolioSaleLine, self).unlink() def _get_real_price_currency(self, product, rule_id, qty, uom, pricelist_id):