<commit_before># Copyright (C) 2022 NextERP Romania
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)


from odoo import _, api, fields, models
from odoo.exceptions import UserError, ValidationError


class AccountInvoiceDVI(models.Model):
    _name = "l10n.ro.account.dvi"
    _description = "Create DVI for invoices"

    name = fields.Char(required=True)
    date = fields.Date("Date", required=True)

    state = fields.Selection(
        string="Status",
        selection=[("draft", "Draft"), ("posted", "Posted"), ("reversed", "Reversed")],
        copy=False,
        index=True,
        readonly=True,
        tracking=True,
        default="draft",
    )
    company_id = fields.Many2one(
        "res.company",
        required=True,
        readonly=True,
        default=lambda self: self.env.company,
    )

    journal_id = fields.Many2one(
        "account.journal",
        string="Journal",
        required=True,
        readonly=True,
        domain="[('type', '=', 'general')]",
        states={"draft": [("readonly", False)]},
    )
    currency_id = fields.Many2one(
        related="company_id.currency_id",
        readonly=True,
        store=True,
        help="Utility field to express amount currency",
    )
    tax_id = fields.Many2one(
        "account.tax",
        required=True,
        domain="[('type_tax_use', '=', 'purchase')]",
        help="Is the vat that is paid in custom for products."
        " default is taken from custom duty tax"
        ". will put this vat tag in journal entry to find it in reports",
    )
    line_ids = fields.One2many(
        "l10n.ro.account.dvi.line",
        "dvi_id",
        string="DVI Lines",
        copy=False,
        readonly=False,
        states={"done": [("readonly", True)]},
    )
    total_base_tax_value = fields.Monetary(
        compute="_compute_total_tax_value",
        readonly=1,
        help="Is readonly sum of product tax and custom tax."
        "This must be the tax value that you have on dvi",
    )
    total_tax_value = fields.Monetary(
        compute="_compute_total_tax_value",
        readonly=1,
        help="Is readonly sum of product tax and custom tax."
        "This must be the tax value that you have on dvi",
    )

    customs_duty_product_id = fields.Many2one(
        "product.product",
        required=True,
        help="A product type service with l10n_ro_custom_duty checked"
        " (purchase tab).  Journal entry for duty will be with this product &"
        " default vat for custom duty and invoice - to find it in declaration based on tags",
    )
    customs_duty_value = fields.Monetary(help="This is a value from received dvi")
    customs_duty_tax_value = fields.Monetary(
        readonly=1,
        compute="_compute_total_tax_value",
        help="readonly computed tax from custom_duty_value",
    )

    customs_commision_product_id = fields.Many2one(
        "product.product",
        required=True,
        help="Product type service with l10n_ro_custom_commision checed "
        "(in purchase tab). Journal entry for commision will be with this product",
    )
    customs_commission_value = fields.Monetary(help="taken from dvi if exists")

    invoice_ids = fields.Many2many(
        "account.move",
        string="Invoices",
        required=True,
        domain=[("move_type", "in", ["in_invoice", "in_refund"])],
    )
    invoice_base_value = fields.Monetary(
        compute="_compute_amount", help="Invoices value without taxes"
    )
    invoice_tax_value = fields.Monetary(
        compute="_compute_amount",
        help="default is computed as tax_id from product;"
        " is not recomputed based on selected vat",
    )

    landed_cost_ids = fields.One2many(
        "stock.landed.cost", "l10n_ro_account_dvi_id", readonly=True
    )

    @api.model
    def default_get(self, fields_list):
        defaults = super(AccountInvoiceDVI, self).default_get(fields_list)
        defaults["date"] = fields.Date.today()
        if "company_id" not in defaults:
            defaults["company_id"] = self.env.company
        domain = [
            ("type", "=", "general"),
            ("company_id", "=", self.env.company.id),
        ]
        defaults["journal_id"] = self.env["account.journal"].search(domain, limit=1)
        customs_duty_product = (
            self.env.company._l10n_ro_get_or_create_custom_duty_product()
        )
        customs_commission_product = (
            self.env.company._l10n_ro_get_or_create_customs_commission_product()
        )
        defaults["customs_duty_product_id"] = customs_duty_product.id
        defaults["customs_commision_product_id"] = customs_commission_product.id
        return defaults

    def action_view_landed_costs(self):
        self.ensure_one()
        if not self.landed_cost_ids:
            raise ValidationError(
                _("You do not have created landed costs for this DVI")
            )
        action = self.env.ref("stock_landed_costs.action_stock_landed_cost")
        action = action.sudo().read()[0]
        action["domain"] = [("id", "in", self.landed_cost_ids.ids)]
        return action

    @api.depends(
        "invoice_base_value", "invoice_tax_value", "tax_id", "customs_duty_value"
    )
    def _compute_total_tax_value(self):
        for rec in self:
            if rec.customs_duty_value and rec.tax_id:
                tax_values = rec.tax_id.compute_all(rec.customs_duty_value)
                rec.customs_duty_tax_value = (
                    tax_values["total_included"] - tax_values["total_excluded"]
                )
            else:
                rec.customs_duty_tax_value = 0
            rec.total_base_tax_value = rec.invoice_base_value + rec.customs_duty_value
            rec.total_tax_value = rec.invoice_tax_value + rec.customs_duty_tax_value

    @api.depends("line_ids.base_amount", "line_ids.vat_amount")
    def _compute_amount(self):
        for dvi in self:
            dvi.invoice_base_value = sum(line.base_amount for line in dvi.line_ids)
            dvi.invoice_tax_value = sum(line.vat_amount for line in dvi.line_ids)

    def write(self, vals):
        res = super().write(vals)
        if vals.get("invoice_ids"):
            for dvi in self:
                new_lines = []
                if dvi.line_ids:
                    dvi.line_ids.unlink()
                for invoice in dvi.invoice_ids:
                    invoice_lines = invoice.invoice_line_ids.filtered(
                        lambda line: not line.display_type
                        and line.product_id.type == "product"
                    )
                    for inv_line in invoice_lines:
                        new_lines.append(
                            (
                                0,
                                0,
                                {
                                    "dvi_id": dvi.id,<commit_msg>[FIX] l10n_ro_dvi to include transport<commit_after># Copyright (C) 2022 NextERP Romania
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)


from odoo import _, api, fields, models
from odoo.exceptions import UserError, ValidationError


class AccountInvoiceDVI(models.Model):
    _name = "l10n.ro.account.dvi"
    _description = "Create DVI for invoices"

    name = fields.Char(required=True)
    date = fields.Date("Date", required=True)

    state = fields.Selection(
        string="Status",
        selection=[("draft", "Draft"), ("posted", "Posted"), ("reversed", "Reversed")],
        copy=False,
        index=True,
        readonly=True,
        tracking=True,
        default="draft",
    )
    company_id = fields.Many2one(
        "res.company",
        required=True,
        readonly=True,
        default=lambda self: self.env.company,
    )

    journal_id = fields.Many2one(
        "account.journal",
        string="Journal",
        required=True,
        readonly=True,
        domain="[('type', '=', 'general')]",
        states={"draft": [("readonly", False)]},
    )
    currency_id = fields.Many2one(
        related="company_id.currency_id",
        readonly=True,
        store=True,
        help="Utility field to express amount currency",
    )
    tax_id = fields.Many2one(
        "account.tax",
        required=True,
        domain="[('type_tax_use', '=', 'purchase')]",
        help="Is the vat that is paid in custom for products."
        " default is taken from custom duty tax"
        ". will put this vat tag in journal entry to find it in reports",
    )
    line_ids = fields.One2many(
        "l10n.ro.account.dvi.line",
        "dvi_id",
        string="DVI Lines",
        copy=False,
        readonly=False,
        states={"done": [("readonly", True)]},
    )
    total_base_tax_value = fields.Monetary(
        compute="_compute_total_tax_value",
        readonly=1,
        help="Is readonly sum of product tax and custom tax."
        "This must be the tax value that you have on dvi",
    )
    total_tax_value = fields.Monetary(
        compute="_compute_total_tax_value",
        readonly=1,
        help="Is readonly sum of product tax and custom tax."
        "This must be the tax value that you have on dvi",
    )

    customs_duty_product_id = fields.Many2one(
        "product.product",
        required=True,
        help="A product type service with l10n_ro_custom_duty checked"
        " (purchase tab).  Journal entry for duty will be with this product &"
        " default vat for custom duty and invoice - to find it in declaration based on tags",
    )
    customs_duty_value = fields.Monetary(help="This is a value from received dvi")
    customs_duty_tax_value = fields.Monetary(
        readonly=1,
        compute="_compute_total_tax_value",
        help="readonly computed tax from custom_duty_value",
    )

    customs_commision_product_id = fields.Many2one(
        "product.product",
        required=True,
        help="Product type service with l10n_ro_custom_commision checed "
        "(in purchase tab). Journal entry for commision will be with this product",
    )
    customs_commission_value = fields.Monetary(help="taken from dvi if exists")

    invoice_ids = fields.Many2many(
        "account.move",
        string="Invoices",
        required=True,
        domain=[("move_type", "in", ["in_invoice", "in_refund"])],
    )
    invoice_base_value = fields.Monetary(
        compute="_compute_amount", help="Invoices value without taxes"
    )
    invoice_tax_value = fields.Monetary(
        compute="_compute_amount",
        help="default is computed as tax_id from product;"
        " is not recomputed based on selected vat",
    )

    landed_cost_ids = fields.One2many(
        "stock.landed.cost", "l10n_ro_account_dvi_id", readonly=True
    )

    @api.model
    def default_get(self, fields_list):
        defaults = super(AccountInvoiceDVI, self).default_get(fields_list)
        defaults["date"] = fields.Date.today()
        if "company_id" not in defaults:
            defaults["company_id"] = self.env.company
        domain = [
            ("type", "=", "general"),
            ("company_id", "=", self.env.company.id),
        ]
        defaults["journal_id"] = self.env["account.journal"].search(domain, limit=1)
        customs_duty_product = (
            self.env.company._l10n_ro_get_or_create_custom_duty_product()
        )
        customs_commission_product = (
            self.env.company._l10n_ro_get_or_create_customs_commission_product()
        )
        defaults["customs_duty_product_id"] = customs_duty_product.id
        defaults["customs_commision_product_id"] = customs_commission_product.id
        return defaults

    def action_view_landed_costs(self):
        self.ensure_one()
        if not self.landed_cost_ids:
            raise ValidationError(
                _("You do not have created landed costs for this DVI")
            )
        action = self.env.ref("stock_landed_costs.action_stock_landed_cost")
        action = action.sudo().read()[0]
        action["domain"] = [("id", "in", self.landed_cost_ids.ids)]
        return action

    @api.depends(
        "invoice_base_value", "invoice_tax_value", "tax_id", "customs_duty_value"
    )
    def _compute_total_tax_value(self):
        for rec in self:
            if rec.customs_duty_value and rec.tax_id:
                tax_values = rec.tax_id.compute_all(rec.customs_duty_value)
                rec.customs_duty_tax_value = (
                    tax_values["total_included"] - tax_values["total_excluded"]
                )
            else:
                rec.customs_duty_tax_value = 0
            rec.total_base_tax_value = rec.invoice_base_value + rec.customs_duty_value
            rec.total_tax_value = rec.invoice_tax_value + rec.customs_duty_tax_value

    @api.depends("line_ids.base_amount", "line_ids.vat_amount")
    def _compute_amount(self):
        for dvi in self:
            dvi.invoice_base_value = sum(line.base_amount for line in dvi.line_ids)
            dvi.invoice_tax_value = sum(line.vat_amount for line in dvi.line_ids)

    def write(self, vals):
        res = super().write(vals)
        if vals.get("invoice_ids"):
            for dvi in self:
                new_lines = []
                if dvi.line_ids:
                    dvi.line_ids.unlink()
                for invoice in dvi.invoice_ids:
                    invoice_lines = invoice.invoice_line_ids.filtered(
                        lambda line: not line.display_type
                        and (
                            line.product_id.type == "product"
                            or line.is_landed_costs_line is True
                        )
                    )
                    for inv_line in invoice_lines:
                        new_lines.append(
                            (
                                0,
                                0,
                                {
                                    "dvi_id": dvi.id,
{'fraction_non_alphanumeric': 0.10244845360824742, 'fraction_numerical': 0.005441008018327606, 'mean_word_length': 2.1575497287522603, 'pattern_counts': {'":': 6, '<': 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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2637680', 'n_tokens_mistral': 4264, 'n_tokens_neox': 4114, 'n_words': 893}