# Copyright 2026 Ecocentral, Criptomart
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import api
from odoo import fields
from odoo import models


class AccountMove(models.Model):
    _inherit = "account.move"

    @api.model
    def _sort_grouped_lines(self, lines_dic):
        """Break ties on same-day pickings by name.

        ``account_invoice_report_grouped_by_picking`` (OCA) sorts groups by
        ``picking.date``/``date_done`` only: when several pickings share
        both (the common case for same-day deliveries), their relative
        order is otherwise undefined/insertion-order, which the picking
        name settles.
        """
        return sorted(
            lines_dic,
            key=lambda x: (
                x["picking"].date or fields.Datetime.now(),
                x["picking"].date_done or fields.Datetime.now(),
                x["picking"].name or "",
            ),
        )
