|
| 1 | +# Copyright 2020 Coop IT Easy SCRL fs |
| 2 | +# Robin Keunen <[email protected]> |
| 3 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). |
| 4 | + |
| 5 | +from odoo import api, models |
| 6 | +from odoo.tools.float_utils import float_compare |
| 7 | + |
| 8 | + |
| 9 | +class AccountInvoice(models.Model): |
| 10 | + _inherit = "account.invoice" |
| 11 | + |
| 12 | + @api.multi |
| 13 | + def _get_move_account_id(self, move): |
| 14 | + self.ensure_one() |
| 15 | + invoice_line_obj = self.env["account.invoice.line"] |
| 16 | + account = invoice_line_obj.get_invoice_line_account( |
| 17 | + "in_invoice", |
| 18 | + move.product_id, |
| 19 | + self.partner_id.property_account_position_id, |
| 20 | + self.env.user.company_id, |
| 21 | + ) |
| 22 | + if account: |
| 23 | + account_id = account.id |
| 24 | + else: |
| 25 | + account_id = invoice_line_obj.with_context( |
| 26 | + {"journal_id": self.journal_id.id, "type": "in_invoice"} |
| 27 | + )._default_account() |
| 28 | + return account_id |
| 29 | + |
| 30 | + @api.multi |
| 31 | + def _prepare_invoice_line_from_move(self, move): |
| 32 | + self.ensure_one() |
| 33 | + qty = move.quantity_done |
| 34 | + flt_comparison = float_compare( |
| 35 | + qty, 0.0, precision_rounding=move.product_uom.rounding |
| 36 | + ) |
| 37 | + if flt_comparison <= 0: |
| 38 | + qty = 0.0 |
| 39 | + |
| 40 | + taxes = move.product_id.supplier_taxes_id |
| 41 | + invoice_line_tax_ids = self.env["account.fiscal.position"].map_tax( |
| 42 | + taxes, move.product_id, self.partner_id |
| 43 | + ) # fixme not sure about this |
| 44 | + account_id = self._get_move_account_id(move) |
| 45 | + data = { |
| 46 | + "name": move.name, |
| 47 | + "origin": self.origin, |
| 48 | + "uom_id": move.product_uom.id, |
| 49 | + "product_id": move.product_id.id, |
| 50 | + "account_id": account_id, |
| 51 | + "price_unit": move.price_unit, |
| 52 | + "quantity": qty, |
| 53 | + "discount": 0.0, |
| 54 | + # fixme not sure what to do with this |
| 55 | + # "account_analytic_id": move.account_analytic_id.id, |
| 56 | + # "analytic_tag_ids": move.analytic_tag_ids.ids, |
| 57 | + "invoice_line_tax_ids": invoice_line_tax_ids.ids, |
| 58 | + } |
| 59 | + |
| 60 | + return data |
| 61 | + |
| 62 | + @api.onchange("purchase_id") |
| 63 | + def purchase_order_change(self): |
| 64 | + res = super(AccountInvoice, self).purchase_order_change() |
| 65 | + po = self.env["purchase.order"].search([("name", "=", self.origin)]) |
| 66 | + moves = po.picking_ids.filtered( |
| 67 | + lambda p: p.state == "done" |
| 68 | + ).move_ids_without_package |
| 69 | + moves_to_invoice = moves.filtered( |
| 70 | + lambda m: m.state == "done" and not m.purchase_line_id |
| 71 | + ) |
| 72 | + new_lines = self.env["account.invoice.line"] |
| 73 | + for move in moves_to_invoice: |
| 74 | + data = self._prepare_invoice_line_from_move(move) |
| 75 | + new_line = new_lines.new(data) |
| 76 | + new_line._set_additional_fields(self) |
| 77 | + new_lines += new_line |
| 78 | + |
| 79 | + self.invoice_line_ids += new_lines |
| 80 | + return res |
0 commit comments