Debian Codegraph @

addons/pos_sale/models/account_move_line.py

raw symbols SCIP
1 from odoo import models
2 from odoo.tools import float_compare
3
4
5 class AccountMoveLine(models.Model):
6 _inherit = 'account.move.line'
7
8 def _get_downpayment_lines(self):
9 # EXTENDS sale
10 downpayment_lines = self.env["account.move.line"]
11 if not self.env['pos.order.line'].has_access('read'):
12 return super()._get_downpayment_lines()
13
14 for record in self:
15 rounding = record.currency_id.rounding
16 if related_sol := record.sale_line_ids:
17 # if order is not settled through POS
18 # We're assuming that if an order is settled through POS it will have sale_line_ids empty.
19 # We're also assuming that the downpayment line will have the same price_subtotal & tax_ids as the record.
20 pos_downpayment_moves = related_sol.filtered("is_downpayment").pos_order_line_ids.order_id.account_move
21 downpayment_lines |= pos_downpayment_moves.invoice_line_ids.filtered(
22 lambda r: float_compare(r.price_subtotal, -record.price_subtotal, precision_rounding=rounding) == 0
23 and r.tax_ids == record.tax_ids,
24 )
25
26 elif related_posl := record.move_id.pos_order_ids.lines:
27 # if order is settled through POS
28 # We get the downpayment lines through:
29 # final invoice -> final POS order -> origin sale order -> downpayment pos order -> downpayment invoice
30 sale_orders = related_posl.sale_order_origin_id
31 candidate_moves = sale_orders.pos_order_line_ids.order_id.account_move.filtered(lambda r: r._is_downpayment())
32 applicable_lines = candidate_moves.invoice_line_ids.filtered(
33 lambda line: float_compare(line.price_subtotal, -record.price_subtotal, precision_rounding=rounding) == 0
34 and line.tax_ids == record.tax_ids,
35 )
36
37 if len(applicable_lines) > 1:
38 # In the case there are multiple downpayment lines with the same tax & total we'll
39 # pair them up as per their ids and get the downpayment line paired with the record.
40 move_lines = record.move_id.invoice_line_ids
41 lines_dict = dict(zip(move_lines.sorted('id'), applicable_lines.sorted('id')))
42 downpayment_lines |= lines_dict.get(record)
43 else:
44 downpayment_lines |= applicable_lines
45
46 return downpayment_lines | super()._get_downpayment_lines()