|
1 | 1 | # Copyright 2024 Hunki Enterprises BV |
2 | 2 | # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) |
3 | 3 |
|
4 | | -from odoo import models |
| 4 | + |
| 5 | +from odoo import api, fields, models |
5 | 6 |
|
6 | 7 |
|
7 | 8 | class AccountBankStatementLine(models.Model): |
8 | 9 | _inherit = "account.bank.statement.line" |
9 | 10 |
|
10 | | - def process_reconciliation( |
11 | | - self, counterpart_aml_dicts=None, payment_aml_rec=None, new_aml_dicts=None |
| 11 | + add_sale_order_id = fields.Many2one( |
| 12 | + "sale.order", |
| 13 | + check_company=True, |
| 14 | + store=False, |
| 15 | + default=False, |
| 16 | + prefetch=False, |
| 17 | + ) |
| 18 | + |
| 19 | + @api.onchange("add_sale_order_id") |
| 20 | + def _onchange_add_sale_order_id(self): |
| 21 | + new_data = [] |
| 22 | + to_add = True |
| 23 | + |
| 24 | + for line in self.reconcile_data_info["data"]: |
| 25 | + if line.get("sale_order_id") == self.add_sale_order_id.id: |
| 26 | + to_add = False |
| 27 | + continue |
| 28 | + new_data.append(line) |
| 29 | + |
| 30 | + if to_add: |
| 31 | + new_data.append( |
| 32 | + self._get_reconcile_line_for_sale_order( |
| 33 | + self.add_sale_order_id, |
| 34 | + "other", |
| 35 | + self.reconcile_data_info["reconcile_auxiliary_id"], |
| 36 | + ) |
| 37 | + ) |
| 38 | + self.reconcile_data_info["reconcile_auxiliary_id"] += 1 |
| 39 | + |
| 40 | + self.reconcile_data_info = self._recompute_suspense_line( |
| 41 | + new_data, |
| 42 | + self.reconcile_data_info["reconcile_auxiliary_id"], |
| 43 | + self.manual_reference, |
| 44 | + ) |
| 45 | + self.can_reconcile = self.reconcile_data_info.get("can_reconcile", False) |
| 46 | + |
| 47 | + def _default_reconcile_data(self, from_unreconcile=False): |
| 48 | + self = self.with_context(account_reconcile_sale_order_inject_rule_type=True) |
| 49 | + return super()._default_reconcile_data(from_unreconcile=from_unreconcile) |
| 50 | + |
| 51 | + def _get_reconcile_line( |
| 52 | + self, |
| 53 | + line, |
| 54 | + kind, |
| 55 | + is_counterpart=False, |
| 56 | + max_amount=False, |
| 57 | + from_unreconcile=False, |
| 58 | + reconcile_auxiliary_id=False, |
| 59 | + move=False, |
| 60 | + is_reconciled=False, |
| 61 | + ): |
| 62 | + if isinstance(line, models.Model) and line._name == "sale.order": |
| 63 | + return reconcile_auxiliary_id + len(line), [ |
| 64 | + self._get_reconcile_line_for_sale_order( |
| 65 | + sale_order, kind, reconcile_auxiliary_id + i |
| 66 | + ) |
| 67 | + for i, sale_order in enumerate(line) |
| 68 | + ] |
| 69 | + |
| 70 | + return super()._get_reconcile_line( |
| 71 | + line, |
| 72 | + kind, |
| 73 | + is_counterpart=is_counterpart, |
| 74 | + max_amount=max_amount, |
| 75 | + from_unreconcile=from_unreconcile, |
| 76 | + reconcile_auxiliary_id=reconcile_auxiliary_id, |
| 77 | + move=move, |
| 78 | + is_reconciled=is_reconciled, |
| 79 | + ) |
| 80 | + |
| 81 | + def _get_reconcile_line_for_sale_order( |
| 82 | + self, sale_order, kind, reconcile_auxiliary_id |
12 | 83 | ): |
13 | 84 | """ |
14 | | - Invoice selected sale orders and use resulting move lines |
| 85 | + Return dict to be added to reconcile_data_info["data"] for sale order |
15 | 86 | """ |
16 | | - new_aml_dicts2 = [] |
17 | | - counterpart_aml_dicts = (counterpart_aml_dicts or [])[:] |
18 | | - for new_aml_dict in new_aml_dicts or []: |
19 | | - sale_order_id = new_aml_dict.get("sale_order_id") |
20 | | - if sale_order_id: |
21 | | - order = self.env["sale.order"].browse(sale_order_id) |
22 | | - self._process_reconciliation_sale_order_invoice(order) |
23 | | - counterpart_aml_dicts += ( |
24 | | - self._process_reconciliation_sale_order_counterparts(order) |
| 87 | + account = sale_order.partner_id.property_account_receivable_id |
| 88 | + return { |
| 89 | + "id": False, |
| 90 | + "reference": f"reconcile_auxiliary;{reconcile_auxiliary_id}", |
| 91 | + "account_id": (account.id, self.env._("Sale Order %s", sale_order.name)), |
| 92 | + "partner_id": ( |
| 93 | + sale_order.partner_id.id, |
| 94 | + sale_order.partner_id.display_name, |
| 95 | + ), |
| 96 | + "date": fields.Date.to_string(sale_order.date_order), |
| 97 | + "name": sale_order.name, |
| 98 | + "amount": -sale_order.amount_total, |
| 99 | + "credit": sale_order.amount_total, |
| 100 | + "debit": 0, |
| 101 | + "kind": kind, |
| 102 | + "currency_id": sale_order.currency_id.id, |
| 103 | + "currency_amount": -sale_order.amount_total, |
| 104 | + "line_currency_id": sale_order.currency_id.id, |
| 105 | + "sale_order_id": sale_order.id, |
| 106 | + } |
| 107 | + |
| 108 | + def _prepare_reconcile_line_data(self, lines): |
| 109 | + for line in lines: |
| 110 | + if line.get("sale_order_id"): |
| 111 | + sale_order = self.env["sale.order"].browse(line["sale_order_id"]) |
| 112 | + counterpart_lines = ( |
| 113 | + self._prepare_reconcile_line_data_sale_order_invoice(sale_order) |
25 | 114 | ) |
26 | | - else: |
27 | | - new_aml_dicts2.append(new_aml_dict) |
| 115 | + line["counterpart_line_ids"] = counterpart_lines.ids |
28 | 116 |
|
29 | | - return super().process_reconciliation( |
30 | | - counterpart_aml_dicts=counterpart_aml_dicts, |
31 | | - payment_aml_rec=payment_aml_rec, |
32 | | - new_aml_dicts=new_aml_dicts2, |
33 | | - ) |
| 117 | + return super()._prepare_reconcile_line_data(lines) |
34 | 118 |
|
35 | | - def _process_reconciliation_sale_order_invoice(self, order): |
| 119 | + def _prepare_reconcile_line_data_sale_order_invoice(self, order): |
36 | 120 | """ |
37 | 121 | Invoice selected sale orders and post the invoices |
38 | 122 | """ |
39 | | - clean_context = { |
40 | | - key: value |
41 | | - for key, value in self.env.context.items() |
42 | | - if key != "force_price_include" |
43 | | - } |
44 | | - order = order.with_context(clean_context) # pylint: disable=context-overridden |
45 | 123 | if order.state in ("draft", "sent"): |
46 | 124 | order.action_confirm() |
47 | 125 | invoices = order._create_invoices() |
48 | 126 | invoices.action_post() |
49 | | - |
50 | | - def _process_reconciliation_sale_order_counterparts(self, order): |
51 | | - """ |
52 | | - Return counterpart aml dicts for sale order |
53 | | - """ |
54 | | - return [ |
55 | | - { |
56 | | - "name": line.name, |
57 | | - "move_line": line, |
58 | | - "debit": line.credit, |
59 | | - "credit": line.debit, |
60 | | - "analytic_tag_ids": [(6, 0, line.analytic_tag_ids.ids)], |
61 | | - } |
62 | | - for line in order.mapped("invoice_ids.line_ids") |
63 | | - if line.account_id.user_type_id.type == "receivable" |
64 | | - ] |
| 127 | + return invoices.line_ids.filtered( |
| 128 | + lambda x: x.account_id.account_type == "asset_receivable" |
| 129 | + ) |
0 commit comments