<commit_before># Copyright 2022 Tecnativa - <NAME>
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
from odoo import models
from odoo.tools import float_compare, float_is_zero


class PurchaseOrderLine(models.Model):
    _inherit = "purchase.order.line"

    def get_stock_moves_link_invoice(self):
        moves_linked = self.env["stock.move"]
        for stock_move in self.move_ids:
            if (
                stock_move.state != "done"
                or stock_move.scrapped
                or (
                    stock_move.location_dest_id.usage != "internal"
                    and (
                        stock_move.location_id.usage != "internal"
                        or not stock_move.to_refund
                    )
                )
            ):
                continue
            invoice_lines = stock_move.invoice_line_ids.filtered(
                lambda invl: invl.move_id.state != "cancel"
            )
            invoiced_qty = 0
            for inv_line in invoice_lines:
                if inv_line.move_id.move_type == "in_refund":
                    invoiced_qty -= inv_line.quantity
                else:
                    invoiced_qty += inv_line.quantity
            if float_is_zero(
                invoiced_qty, precision_rounding=self.product_uom.rounding
            ):
                moves_linked += stock_move
        return moves_linked

    def _prepare_account_move_line(self, move=False):
        vals = super()._prepare_account_move_line(move=move)
        stock_moves = self.get_stock_moves_link_invoice()
        # Invoice returned moves marked as to_refund
        if (
            float_compare(
                self.product_qty - self.qty_invoiced,
                0.0,
                precision_rounding=self.currency_id.rounding,
            )
            < 0
        ):
            stock_moves = stock_moves.filtered("to_refund")
        vals["move_line_ids"] = [(4, m.id) for m in stock_moves]
        return vals<commit_msg>[FIX] purchase_stock_picking_invoice_link: Consider DS pickings<commit_after># Copyright 2022 Tecnativa - <NAME>
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
from odoo import models
from odoo.tools import float_compare, float_is_zero


class PurchaseOrderLine(models.Model):
    _inherit = "purchase.order.line"

    def get_stock_moves_link_invoice(self):
        moves_linked = self.env["stock.move"]
        for stock_move in self.move_ids:
            if (
                stock_move.state != "done"
                or stock_move.scrapped
                or (
                    stock_move.location_id.usage != "supplier"
                    and (
                        stock_move.location_dest_id.usage != "supplier"
                        or not stock_move.to_refund
                    )
                )
            ):
                continue
            invoice_lines = stock_move.invoice_line_ids.filtered(
                lambda invl: invl.move_id.state != "cancel"
                and invl.move_id.move_type in {"in_invoice", "in_refund"}
            )
            invoiced_qty = 0
            for inv_line in invoice_lines:
                if inv_line.move_id.move_type == "in_refund":
                    invoiced_qty -= inv_line.quantity
                else:
                    invoiced_qty += inv_line.quantity
            if float_is_zero(
                invoiced_qty, precision_rounding=self.product_uom.rounding
            ):
                moves_linked += stock_move
        return moves_linked

    def _prepare_account_move_line(self, move=False):
        vals = super()._prepare_account_move_line(move=move)
        stock_moves = self.get_stock_moves_link_invoice()
        # Invoice returned moves marked as to_refund
        if (
            float_compare(
                self.product_qty - self.qty_invoiced,
                0.0,
                precision_rounding=self.currency_id.rounding,
            )
            < 0
        ):
            stock_moves = stock_moves.filtered("to_refund")
        vals["move_line_ids"] = [(4, m.id) for m in stock_moves]
        return vals
{'fraction_non_alphanumeric': 0.07814761215629522, 'fraction_numerical': 0.005788712011577424, 'mean_word_length': 1.6147540983606556, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '676242', 'n_tokens_mistral': 1266, 'n_tokens_neox': 1193, 'n_words': 216}