<commit_before># Copyright 2020 Camptocamp SA (http://www.camptocamp.com)
# @author <NAME> <<EMAIL>>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from itertools import groupby

from odoo import _, api, exceptions, fields
from odoo.models import TransientModel
from odoo.tools import DotDict


class StockPickingMergeWizard(TransientModel):
    _name = "stock.picking.merge"
    _description = "Stock Picking Merge"

    selected_picking_ids = fields.Many2many(
        string="Selected Pickings",
        comodel_name="stock.picking",
        default=lambda self: self.env.context.get("active_ids"),
    )
    valid_picking_ids = fields.Many2many(
        string="Valid Pickings",
        comodel_name="stock.picking",
        compute="_compute_pickings",
    )
    discarded_picking_ids = fields.Many2many(
        string="Discarded Pickings",
        comodel_name="stock.picking",
        compute="_compute_pickings",
    )
    details = fields.Html(compute="_compute_info")
    nothing_todo = fields.Boolean(compute="_compute_info")
    show_discarded_detail = fields.Boolean(default=False)

    @api.depends("selected_picking_ids")
    def _compute_pickings(self):
        for rec in self:
            valid_pickings = rec._valid_pickings()
            discarded = rec.selected_picking_ids - valid_pickings
            rec.update(
                {
                    "discarded_picking_ids": discarded.ids,
                    "valid_picking_ids": valid_pickings.ids,
                }
            )

    def _valid_pickings(self):
        return self.selected_picking_ids.filtered(self._filter_picking).sorted(
            key=self._key_group_picking,
        )

    @staticmethod
    def _filter_picking(picking):
        return (
            picking.picking_type_id.group_pickings
            and picking.state not in ("cancel", "done")
            and not picking.printed
        )

    @staticmethod
    def _key_group_picking(picking):
        return (
            picking.partner_id.id or 0,
            picking.carrier_id.id or 0,
            picking.location_id.id,
            picking.location_dest_id.id,
            picking.picking_type_id.id,
        )

    def _grouped_pickings(self):
        return groupby(self.valid_picking_ids, key=self._key_group_picking)

    @api.depends("valid_picking_ids")
    def _compute_info(self):
        tmpl = self._get_info_template()
        for rec in self:
            info = rec._get_grouping_info()
            rec.details = tmpl.render(info)
            rec.nothing_todo = not info["something_todo"]

    def _get_grouping_info(self):
        grouping_forecast = []
        for key, _pickings in self._grouped_pickings():
            partner = self.env["res.partner"].browse()
            carrier = self.env["delivery.carrier"].browse()<commit_msg>[FIX] s_p_group_by_partner_by_carrier: fix merge wizard<commit_after># Copyright 2020 Camptocamp SA (http://www.camptocamp.com)
# @author <NAME> <<EMAIL>>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from itertools import groupby

from odoo import _, api, exceptions, fields
from odoo.models import TransientModel
from odoo.tools import DotDict


class StockPickingMergeWizard(TransientModel):
    _name = "stock.picking.merge"
    _description = "Stock Picking Merge"

    selected_picking_ids = fields.Many2many(
        string="Selected Pickings",
        comodel_name="stock.picking",
        default=lambda self: self.env.context.get("active_ids"),
    )
    valid_picking_ids = fields.Many2many(
        string="Valid Pickings",
        comodel_name="stock.picking",
        compute="_compute_pickings",
    )
    discarded_picking_ids = fields.Many2many(
        string="Discarded Pickings",
        comodel_name="stock.picking",
        compute="_compute_pickings",
    )
    details = fields.Html(compute="_compute_info")
    nothing_todo = fields.Boolean(compute="_compute_info")
    show_discarded_detail = fields.Boolean(default=False)

    @api.depends("selected_picking_ids")
    def _compute_pickings(self):
        for rec in self:
            valid_pickings = rec._valid_pickings()
            discarded = rec.selected_picking_ids - valid_pickings
            rec.update(
                {
                    "discarded_picking_ids": discarded.ids,
                    "valid_picking_ids": valid_pickings.ids,
                }
            )

    def _valid_pickings(self):
        return self.selected_picking_ids.filtered(self._filter_picking).sorted(
            key=self._key_group_picking,
        )

    @staticmethod
    def _filter_picking(picking):
        return (
            picking.picking_type_id.group_pickings
            and picking.state not in ("cancel", "done")
            and not picking.printed
        )

    @staticmethod
    def _key_group_picking(picking):
        return (
            picking.partner_id.id or 0,
            picking.carrier_id.id or 0,
            picking.location_id.id,
            picking.location_dest_id.id,
            picking.picking_type_id.id,
        )

    def _grouped_pickings(self):
        return groupby(self.valid_picking_ids, key=self._key_group_picking)

    @api.depends("valid_picking_ids")
    def _compute_info(self):
        tmpl = self._get_info_template()
        for rec in self:
            info = rec._get_grouping_info()
            rec.details = tmpl._render(info)
            rec.nothing_todo = not info["something_todo"]

    def _get_grouping_info(self):
        grouping_forecast = []
        for key, _pickings in self._grouped_pickings():
            partner = self.env["res.partner"].browse()
            carrier = self.env["delivery.carrier"].browse()
{'fraction_non_alphanumeric': 0.0976336546888694, 'fraction_numerical': 0.0038562664329535494, 'mean_word_length': 2.528756957328386, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6274710', 'n_tokens_mistral': 1797, 'n_tokens_neox': 1735, 'n_words': 299}