|
| 1 | +# Copyright 2025 Ángel Rivas <angel.rivas@sygel.es> |
| 2 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 3 | + |
| 4 | +from odoo import api, fields, models |
| 5 | + |
| 6 | + |
| 7 | +class StockMoveLine(models.Model): |
| 8 | + _inherit = "stock.move.line" |
| 9 | + |
| 10 | + genci_amount_subtotal = fields.Monetary( |
| 11 | + compute="_compute_genci_amount", |
| 12 | + compute_sudo=True, |
| 13 | + currency_field="currency_id", |
| 14 | + ) |
| 15 | + genci_amount_tax = fields.Monetary( |
| 16 | + compute="_compute_genci_amount", |
| 17 | + compute_sudo=True, |
| 18 | + currency_field="currency_id", |
| 19 | + ) |
| 20 | + genci_amount_total = fields.Monetary( |
| 21 | + compute="_compute_genci_amount", |
| 22 | + compute_sudo=True, |
| 23 | + currency_field="currency_id", |
| 24 | + ) |
| 25 | + |
| 26 | + genci_rule_id = fields.Many2one( |
| 27 | + "genci.rule", |
| 28 | + compute="_compute_genci_amount", |
| 29 | + store=False, |
| 30 | + readonly=True, |
| 31 | + ) |
| 32 | + |
| 33 | + def _get_genci_product_taxes(self, genci_product): |
| 34 | + """Map GENCI product taxes through fiscal position (similar to SIGAUS).""" |
| 35 | + self.ensure_one() |
| 36 | + taxes = genci_product.taxes_id |
| 37 | + if not taxes: |
| 38 | + return False |
| 39 | + |
| 40 | + sale = self.sale_line.order_id if self.sale_line else False |
| 41 | + fiscal_position = sale.fiscal_position_id if sale else False |
| 42 | + |
| 43 | + return fiscal_position.map_tax(taxes) if fiscal_position else taxes |
| 44 | + |
| 45 | + def _get_genci_tax_base_line_dict(self, genci_product, price, qty): |
| 46 | + """Prepare tax base line dict for account.tax._compute_taxes().""" |
| 47 | + self.ensure_one() |
| 48 | + return self.env["account.tax"]._convert_to_tax_base_line_dict( |
| 49 | + self, |
| 50 | + partner=self.sale_line.order_partner_id, |
| 51 | + currency=self.currency_id, |
| 52 | + product=genci_product, |
| 53 | + taxes=self._get_genci_product_taxes(genci_product), |
| 54 | + price_unit=price, |
| 55 | + quantity=qty, |
| 56 | + ) |
| 57 | + |
| 58 | + @api.depends( |
| 59 | + "product_id", |
| 60 | + "qty_done", |
| 61 | + "reserved_qty", |
| 62 | + "sale_line", |
| 63 | + "sale_line.order_id.is_genci", |
| 64 | + "sale_line.order_id.date_order", |
| 65 | + "product_id.genci_has_amount", |
| 66 | + "product_id.genci_rule_id", |
| 67 | + ) |
| 68 | + def _compute_genci_amount(self): |
| 69 | + Tax = self.env["account.tax"] |
| 70 | + |
| 71 | + for line in self: |
| 72 | + # Reset default values |
| 73 | + line.genci_amount_subtotal = 0.0 |
| 74 | + line.genci_amount_tax = 0.0 |
| 75 | + line.genci_amount_total = 0.0 |
| 76 | + line.genci_rule_id = False |
| 77 | + |
| 78 | + # Minimum conditions to compute GENCI |
| 79 | + if ( |
| 80 | + not line.product_id |
| 81 | + or not line.sale_line |
| 82 | + or not line.sale_line.order_id.is_genci |
| 83 | + or not line.product_id.genci_has_amount |
| 84 | + or not line.product_id.genci_rule_id |
| 85 | + ): |
| 86 | + continue |
| 87 | + |
| 88 | + rule = line.product_id.genci_rule_id |
| 89 | + line.genci_rule_id = rule |
| 90 | + |
| 91 | + # 👉 Cantidad para GENCI: UNIDADES, no peso |
| 92 | + qty = line._get_report_valued_quantity() or line.qty_done or 0.0 |
| 93 | + |
| 94 | + if not qty: |
| 95 | + continue |
| 96 | + |
| 97 | + price_unit = rule.unit_price |
| 98 | + |
| 99 | + # GENCI service product (para impuestos / cuenta) |
| 100 | + genci_product = self.env.ref("l10n_es_genci_account.product_genci_service") |
| 101 | + |
| 102 | + # Línea base para impuestos |
| 103 | + tax_base_line = line._get_genci_tax_base_line_dict( |
| 104 | + genci_product=genci_product, |
| 105 | + price=price_unit, |
| 106 | + qty=qty, |
| 107 | + ) |
| 108 | + |
| 109 | + tax_res = Tax._compute_taxes([tax_base_line]) |
| 110 | + totals = list(tax_res["totals"].values())[0] |
| 111 | + |
| 112 | + line.genci_amount_subtotal = totals.get("amount_untaxed", 0.0) |
| 113 | + line.genci_amount_tax = totals.get("amount_tax", 0.0) |
| 114 | + line.genci_amount_total = line.genci_amount_subtotal + line.genci_amount_tax |
0 commit comments