<commit_before># Copyright (C) 2014 Forest and Biomass Romania
# Copyright (C) 2020 NextERP Romania
# Copyright (C) 2020 Terrabit
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import logging

from odoo import _, models
from odoo.tools.float_utils import float_is_zero

_logger = logging.getLogger(__name__)


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

    def _stock_account_prepare_anglo_saxon_in_lines_vals(self):
        # inainte de a genera liniile de diferenta de pret

        lines_vals_list = []
        for invoice in self:
            account_id = invoice.company_id.property_stock_picking_payable_account_id
            if invoice.move_type in ["in_invoice", "in_refund"]:
                # Add new account moves for difference between reception
                # with notice and invoice values. The price difference account
                # will be changes with the first one available in the reception
                # notice move lines
                if invoice.is_reception_notice():
                    lines_vals_list = super(
                        AccountMove, invoice
                    )._stock_account_prepare_anglo_saxon_in_lines_vals()
                    rec_account = invoice.get_reception_account()
                    if rec_account:
                        for line_vals in lines_vals_list:
                            if line_vals["account_id"] != account_id.id:
                                line_vals["account_id"] = rec_account.id
                for line in invoice.invoice_line_ids:
                    add_diff = False
                    if line.product_id.cost_method != "standard":
                        add_diff = not invoice.company_id.stock_acc_price_diff

                    # daca linia a fost receptionata pe baza de aviz se
                    # seteaza contul 408 pe nota contabile
                    # if account_id and line.account_id == account_id:
                    #     # trebuie sa adaug diferenta dintre receptie pe
                    #     # baza de aviz si receptia din factura
                    #     add_diff = True

                    if not add_diff:
                        # se reevalueaza stocul
                        price_diff = line.get_stock_valuation_difference()
                        if price_diff:
                            line.modify_stock_valuation(price_diff)

                if not invoice.is_reception_notice():
                    lines_vals_list = super(
                        AccountMove, invoice
                    )._stock_account_prepare_anglo_saxon_in_lines_vals()
        return lines_vals_list

    def _stock_account_prepare_anglo_saxon_out_lines_vals(self):
        # nu se mai face descarcarea de gestiune la facturare
        invoices = self
        for move in self:
            if self.company_id.romanian_accounting:
                invoices -= move
        return super(
            AccountMove, invoices
        )._stock_account_prepare_anglo_saxon_out_lines_vals()

    def post(self):
        res = super(AccountMove, self).post()
        for move in self:
            for line in move.line_ids:
                _logger.info(
                    "%s\t\t%s\t\t%s"
                    % (line.debit, line.credit, line.account_id.display_name)
                )
        return res

    def is_reception_notice(self):
        self.ensure_one()
        purchases = self.line_ids.mapped("purchase_line_id.order_id")
        picking_notice = self.env["stock.picking"].search(
            [
                ("id", "in", purchases.mapped("picking_ids").ids),
                ("state", "=", "done"),
                ("notice", "=", True),
            ]
        )
        if picking_notice:
            return True
        return False

    def get_reception_account(self):
        self.ensure_one()
        account = self.env["account.account"]
        acc_payable = self.company_id.property_stock_picking_payable_account_id
        valuation_stock_moves = self.env["stock.move"].search(
            [
                (
                    "purchase_line_id",
                    "in",
                    self.line_ids.mapped("purchase_line_id").ids,
                ),
                ("state", "=", "done"),
                ("picking_id.notice", "=", True),
                ("product_qty", "!=", 0.0),
            ]
        )
        if valuation_stock_moves:
            acc_moves = valuation_stock_moves.mapped("account_move_ids")
            lines = self.env["account.move.line"].search(
                [("move_id", "in", acc_moves.ids)]
            )
            lines_diff_acc = lines.mapped("account_id").filtered(
                lambda a: a != acc_payable
            )
            if lines_diff_acc:
                account = lines_diff_acc[0]
        return account


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

    def _get_computed_account(self):
        if (
            self.product_id.type == "product"
            and self.move_id.company_id.anglo_saxon_accounting
        ):
            if self.move_id.is_purchase_document():
                purchase = self.move_id.purchase_id
                if purchase and self.product_id.purchase_method == "receive":
                    # Control bills based on received quantities
                    if any([p.notice for p in purchase.picking_ids]):<commit_msg>determinare 408<commit_after># Copyright (C) 2014 Forest and Biomass Romania
# Copyright (C) 2020 NextERP Romania
# Copyright (C) 2020 Terrabit
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import logging

from odoo import _, models
from odoo.tools.float_utils import float_is_zero

_logger = logging.getLogger(__name__)


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

    def _stock_account_prepare_anglo_saxon_in_lines_vals(self):
        # inainte de a genera liniile de diferenta de pret

        lines_vals_list = []
        for invoice in self:
            account_id = invoice.company_id.property_stock_picking_payable_account_id
            if invoice.move_type in ["in_invoice", "in_refund"]:
                # Add new account moves for difference between reception
                # with notice and invoice values. The price difference account
                # will be changes with the first one available in the reception
                # notice move lines
                if invoice.is_reception_notice():
                    lines_vals_list = super(
                        AccountMove, invoice
                    )._stock_account_prepare_anglo_saxon_in_lines_vals()
                    rec_account = invoice.get_reception_account()
                    if rec_account:
                        for line_vals in lines_vals_list:
                            if line_vals["account_id"] != account_id.id:
                                line_vals["account_id"] = rec_account.id
                for line in invoice.invoice_line_ids:
                    add_diff = False
                    if line.product_id.cost_method != "standard":
                        add_diff = not invoice.company_id.stock_acc_price_diff

                    # daca linia a fost receptionata pe baza de aviz se
                    # seteaza contul 408 pe nota contabile
                    # if account_id and line.account_id == account_id:
                    #     # trebuie sa adaug diferenta dintre receptie pe
                    #     # baza de aviz si receptia din factura
                    #     add_diff = True

                    if not add_diff:
                        # se reevalueaza stocul
                        price_diff = line.get_stock_valuation_difference()
                        if price_diff:
                            line.modify_stock_valuation(price_diff)

                if not invoice.is_reception_notice():
                    lines_vals_list = super(
                        AccountMove, invoice
                    )._stock_account_prepare_anglo_saxon_in_lines_vals()
        return lines_vals_list

    def _stock_account_prepare_anglo_saxon_out_lines_vals(self):
        # nu se mai face descarcarea de gestiune la facturare
        invoices = self
        for move in self:
            if self.company_id.romanian_accounting:
                invoices -= move
        return super(
            AccountMove, invoices
        )._stock_account_prepare_anglo_saxon_out_lines_vals()

    def post(self):
        res = super(AccountMove, self).post()
        for move in self:
            for line in move.line_ids:
                _logger.info(
                    "%s\t\t%s\t\t%s"
                    % (line.debit, line.credit, line.account_id.display_name)
                )
        return res

    def is_reception_notice(self):
        self.ensure_one()
        purchases = self.line_ids.mapped("purchase_line_id.order_id")
        picking_notice = self.env["stock.picking"].search(
            [
                ("id", "in", purchases.mapped("picking_ids").ids),
                ("state", "=", "done"),
                ("notice", "=", True),
            ]
        )
        if picking_notice:
            return True
        return False

    def get_reception_account(self):
        self.ensure_one()
        account = self.env["account.account"]
        acc_payable = self.company_id.property_stock_picking_payable_account_id
        valuation_stock_moves = self.env["stock.move"].search(
            [
                (
                    "purchase_line_id",
                    "in",
                    self.line_ids.mapped("purchase_line_id").ids,
                ),
                ("state", "=", "done"),
                ("picking_id.notice", "=", True),
                ("product_qty", "!=", 0.0),
            ]
        )
        if valuation_stock_moves:
            acc_moves = valuation_stock_moves.mapped("account_move_ids")
            lines = self.env["account.move.line"].search(
                [("move_id", "in", acc_moves.ids)]
            )
            lines_diff_acc = lines.mapped("account_id").filtered(
                lambda a: a != acc_payable
            )
            if lines_diff_acc:
                account = lines_diff_acc[0]
        return account


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

    def _get_computed_account(self):
        if (
            self.product_id.type == "product"
            and self.move_id.company_id.anglo_saxon_accounting
        ):
            if self.move_id.is_purchase_document():
                purchase = self.purchase_order_id
                if purchase and self.product_id.purchase_method == "receive":
                    # Control bills based on received quantities
                    if any([p.notice for p in purchase.picking_ids]):
{'fraction_non_alphanumeric': 0.07951606944957518, 'fraction_numerical': 0.003971185814554858, 'mean_word_length': 1.635434412265758, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, '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': '3741046', 'n_tokens_mistral': 3126, 'n_tokens_neox': 2915, 'n_words': 638}