|
| 1 | +# Copyright 2025 ForgeFlow S.L. (https://www.forgeflow.com) |
| 2 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
| 3 | +# account_liquidity_forecast_purchase_stock/report/liquidity_forecast.py |
| 4 | +from odoo import _, models |
| 5 | + |
| 6 | + |
| 7 | +class LiquidityForecastReport(models.AbstractModel): |
| 8 | + _inherit = "report.account_liquidity_forecast.liquidity_forecast" |
| 9 | + |
| 10 | + def _prepare_liquidity_forecast_lines_period( |
| 11 | + self, data, liquidity_forecast_lines, period, periods |
| 12 | + ): |
| 13 | + res = super()._prepare_liquidity_forecast_lines_period( |
| 14 | + data, liquidity_forecast_lines, period, periods |
| 15 | + ) |
| 16 | + self._prepare_cash_flow_lines_po_moves( |
| 17 | + data, liquidity_forecast_lines, period, periods |
| 18 | + ) |
| 19 | + return res |
| 20 | + |
| 21 | + def _prepare_cash_flow_lines_po_moves( |
| 22 | + self, data, liquidity_forecast_lines, period, periods |
| 23 | + ): |
| 24 | + """Add stock moves to the liquidity forecast""" |
| 25 | + domain_move_states = ["confirmed", "waiting", "assigned"] |
| 26 | + |
| 27 | + stock_moves = self.env["stock.move"].search( |
| 28 | + [ |
| 29 | + ("state", "in", domain_move_states), |
| 30 | + ("purchase_line_id", "!=", False), |
| 31 | + ("company_id", "=", data["company_id"]), |
| 32 | + ] |
| 33 | + ) |
| 34 | + |
| 35 | + code = "cash_flow_line_out_purchase_moves" |
| 36 | + lines = [line for line in liquidity_forecast_lines if line["code"] == code] |
| 37 | + if lines: |
| 38 | + flow_line = lines[0] |
| 39 | + # RESET amounts before recomputing |
| 40 | + for p_seq in flow_line["periods"]: |
| 41 | + flow_line["periods"][p_seq]["amount"] = 0.0 |
| 42 | + flow_line["periods"][p_seq]["domain"] = "" |
| 43 | + else: |
| 44 | + flow_line = { |
| 45 | + "code": code, |
| 46 | + "type": "amount", |
| 47 | + "level": "detail", |
| 48 | + "model": "stock.move", |
| 49 | + "title": _("Expected PO Deliveries"), |
| 50 | + "sequence": 3550, |
| 51 | + "periods": { |
| 52 | + p["sequence"]: {"amount": 0.0, "domain": ""} for p in periods |
| 53 | + }, |
| 54 | + } |
| 55 | + liquidity_forecast_lines.append(flow_line) |
| 56 | + |
| 57 | + move_ids_by_period = {p["sequence"]: [] for p in periods} |
| 58 | + |
| 59 | + for move in stock_moves: |
| 60 | + if not move.date: |
| 61 | + continue |
| 62 | + move_date = move.date.date() if hasattr(move.date, "date") else move.date |
| 63 | + |
| 64 | + for p in periods: |
| 65 | + if p["date_from"] <= move_date <= p["date_to"]: |
| 66 | + po_line = move.purchase_line_id |
| 67 | + price = po_line.price_unit |
| 68 | + qty = move.product_qty |
| 69 | + flow_line["periods"][p["sequence"]]["amount"] += -qty * price |
| 70 | + move_ids_by_period[p["sequence"]].append(move.id) |
| 71 | + break |
| 72 | + |
| 73 | + for seq, move_ids in move_ids_by_period.items(): |
| 74 | + flow_line["periods"][seq]["domain"] = [("id", "in", move_ids)] |
| 75 | + |
| 76 | + def _prepare_cash_flow_lines_po_lines( |
| 77 | + self, data, liquidity_forecast_lines, period, periods |
| 78 | + ): |
| 79 | + """Override the purchase module's PO lines method to exclude lines |
| 80 | + covered by stock moves""" |
| 81 | + # Get PO lines using the filtering logic |
| 82 | + po_lines = self.get_po_lines(data, period, periods) |
| 83 | + |
| 84 | + if not po_lines: |
| 85 | + return |
| 86 | + |
| 87 | + code = "cash_flow_line_out_purchase" |
| 88 | + lines = [line for line in liquidity_forecast_lines if line["code"] == code] |
| 89 | + if lines: |
| 90 | + flow_line = lines[0] |
| 91 | + # RESET amounts before recomputing |
| 92 | + for p_seq in flow_line["periods"]: |
| 93 | + flow_line["periods"][p_seq]["amount"] = 0.0 |
| 94 | + flow_line["periods"][p_seq]["domain"] = "" |
| 95 | + else: |
| 96 | + flow_line = { |
| 97 | + "code": code, |
| 98 | + "type": "amount", |
| 99 | + "level": "detail", |
| 100 | + "model": "purchase.order.line", |
| 101 | + "title": _("Purchase Orders"), |
| 102 | + "sequence": 3500, |
| 103 | + "periods": { |
| 104 | + p["sequence"]: {"amount": 0.0, "domain": ""} for p in periods |
| 105 | + }, |
| 106 | + } |
| 107 | + liquidity_forecast_lines.append(flow_line) |
| 108 | + |
| 109 | + po_line_ids_by_period = {p["sequence"]: [] for p in periods} |
| 110 | + |
| 111 | + for po_line in po_lines: |
| 112 | + if not po_line.date_planned: |
| 113 | + continue |
| 114 | + |
| 115 | + planned_date = ( |
| 116 | + po_line.date_planned.date() |
| 117 | + if hasattr(po_line.date_planned, "date") |
| 118 | + else po_line.date_planned |
| 119 | + ) |
| 120 | + |
| 121 | + for p in periods: |
| 122 | + if p["date_from"] <= planned_date <= p["date_to"]: |
| 123 | + # Calculate uninvoiced amount |
| 124 | + valid_invoice_lines = po_line.invoice_lines.filtered( |
| 125 | + lambda inv_line: inv_line.move_id.state |
| 126 | + not in ("draft", "cancel") |
| 127 | + ) |
| 128 | + invoiced_qty = sum(valid_invoice_lines.mapped("quantity")) |
| 129 | + uninvoiced_qty = po_line.product_qty - invoiced_qty |
| 130 | + |
| 131 | + if uninvoiced_qty > 0: |
| 132 | + amount = -uninvoiced_qty * po_line.price_unit |
| 133 | + flow_line["periods"][p["sequence"]]["amount"] += amount |
| 134 | + po_line_ids_by_period[p["sequence"]].append(po_line.id) |
| 135 | + break |
| 136 | + |
| 137 | + for seq, po_line_ids in po_line_ids_by_period.items(): |
| 138 | + flow_line["periods"][seq]["domain"] = [("id", "in", po_line_ids)] |
| 139 | + |
| 140 | + def get_po_lines(self, data, period, periods): |
| 141 | + """Filter PO lines to exclude those already covered by stock moves""" |
| 142 | + states = ["sent", "to approve", "purchase", "done"] |
| 143 | + if data.get("include_po_draft", False): |
| 144 | + states.append("draft") |
| 145 | + |
| 146 | + domain = [ |
| 147 | + ("state", "in", states), |
| 148 | + ("date_planned", "<=", period["date_to"]), |
| 149 | + ] |
| 150 | + if period["sequence"] > 0: |
| 151 | + domain += [("date_planned", ">=", period["date_from"])] |
| 152 | + |
| 153 | + po_lines = self.env["purchase.order.line"].search(domain) |
| 154 | + |
| 155 | + filtered_po_lines = [] |
| 156 | + |
| 157 | + for line in po_lines: |
| 158 | + # Check if PO line is already covered by moves |
| 159 | + covered_by_move = False |
| 160 | + for move in line.move_ids.filtered( |
| 161 | + lambda m: m.state not in ("done", "cancel") |
| 162 | + ): |
| 163 | + if not move.date: |
| 164 | + continue |
| 165 | + move_date = ( |
| 166 | + move.date.date() if hasattr(move.date, "date") else move.date |
| 167 | + ) |
| 168 | + for p in periods: |
| 169 | + if p["date_from"] <= move_date <= p["date_to"]: |
| 170 | + covered_by_move = True |
| 171 | + break |
| 172 | + if covered_by_move: |
| 173 | + break |
| 174 | + |
| 175 | + if covered_by_move: |
| 176 | + continue # already handled in _prepare_cash_flow_lines_po_moves |
| 177 | + |
| 178 | + # Check if there's uninvoiced quantity |
| 179 | + valid_invoice_lines = line.invoice_lines.filtered( |
| 180 | + lambda inv_line: inv_line.move_id.state not in ("draft", "cancel") |
| 181 | + ) |
| 182 | + invoiced_qty = sum(valid_invoice_lines.mapped("quantity")) |
| 183 | + |
| 184 | + if line.product_qty > invoiced_qty: |
| 185 | + filtered_po_lines.append(line) |
| 186 | + |
| 187 | + return self.env["purchase.order.line"].browse( |
| 188 | + [line.id for line in filtered_po_lines] |
| 189 | + ) |
0 commit comments