1- # -*- coding: utf-8 -*-
21# Copyright 2019 Camptocamp SA
32# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
43import json
54from operator import itemgetter
65
7- from odoo import api , fields , models , _
6+ from odoo import _ , api , fields , models
87
98
109class AccountInvoice (models .Model ):
@@ -14,20 +13,20 @@ class AccountInvoice(models.Model):
1413 # Allow changing payment mode in open state
1514 # TODO: Check if must be done in account_payment_partner instead
1615 payment_mode_id = fields .Many2one (
17- states = {' draft' : [(' readonly' , False )], ' open' : [(' readonly' , False )]}
16+ states = {" draft" : [(" readonly" , False )], " open" : [(" readonly" , False )]}
1817 )
1918 payment_mode_warning = fields .Char (
20- compute = ' _compute_payment_mode_warning' ,
19+ compute = " _compute_payment_mode_warning" ,
2120 )
2221 display_payment_mode_warning = fields .Boolean (
23- compute = ' _compute_payment_mode_warning' ,
22+ compute = " _compute_payment_mode_warning" ,
2423 )
2524
2625 @api .multi
2726 def invoice_validate (self ):
2827 res = super (AccountInvoice , self ).invoice_validate ()
2928 for invoice in self :
30- if invoice .type != ' out_invoice' :
29+ if invoice .type != " out_invoice" :
3130 continue
3231 if not invoice .payment_mode_id .auto_reconcile_outstanding_credits :
3332 continue
@@ -40,23 +39,18 @@ def invoice_validate(self):
4039 @api .multi
4140 def write (self , vals ):
4241 res = super (AccountInvoice , self ).write (vals )
43- if ' payment_mode_id' in vals :
42+ if " payment_mode_id" in vals :
4443 for invoice in self :
4544 # Do not auto reconcile anything else than open customer inv
46- if invoice .state != ' open' or invoice .type != ' out_invoice' :
45+ if invoice .state != " open" or invoice .type != " out_invoice" :
4746 continue
4847 payment_mode = invoice .payment_mode_id
4948 # Auto reconcile if payment mode sets it
50- if (
51- payment_mode
52- and payment_mode .auto_reconcile_outstanding_credits
53- ):
49+ if payment_mode and payment_mode .auto_reconcile_outstanding_credits :
5450 partial = payment_mode .auto_reconcile_allow_partial
5551 invoice .with_context (
5652 _payment_mode_auto_reconcile = True
57- ).auto_reconcile_credits (
58- partial_allowed = partial
59- )
53+ ).auto_reconcile_credits (partial_allowed = partial )
6054 # If the payment mode is not using auto reconcile we remove
6155 # the existing reconciliations
6256 elif invoice .payment_move_line_ids :
@@ -68,98 +62,90 @@ def auto_reconcile_credits(self, partial_allowed=True):
6862 for invoice in self :
6963 if not invoice .has_outstanding :
7064 continue
71- credits_info = json .loads (
72- invoice .outstanding_credits_debits_widget
73- )
65+ credits_info = json .loads (invoice .outstanding_credits_debits_widget )
7466 # Get outstanding credits in chronological order
7567 # (using reverse because aml is sorted by date desc as default)
76- credits_dict = credits_info .get (' content' )
68+ credits_dict = credits_info .get (" content" )
7769 if invoice .payment_mode_id .auto_reconcile_same_journal :
78- credits_dict = invoice ._filter_payment_same_journal (
79- credits_dict
80- )
70+ credits_dict = invoice ._filter_payment_same_journal (credits_dict )
8171 sorted_credits = self ._sort_credits_dict (credits_dict )
8272 for credit in sorted_credits :
83- if (
84- not partial_allowed
85- and credit .get ('amount' ) > invoice .residual
86- ):
73+ if not partial_allowed and credit .get ("amount" ) > invoice .residual :
8774 continue
88- invoice .assign_outstanding_credit (credit .get ('id' ))
75+ invoice .assign_outstanding_credit (credit .get ("id" ))
8976
9077 @api .model
9178 def _sort_credits_dict (self , credits_dict ):
9279 """Sort credits dict according to their id (oldest recs first)"""
93- return sorted (credits_dict , key = itemgetter ('id' ))
80+ return sorted (credits_dict , key = itemgetter ("id" ))
9481
9582 @api .multi
9683 def _filter_payment_same_journal (self , credits_dict ):
9784 """Keep only credits on the same journal than the invoice."""
9885 self .ensure_one ()
99- line_ids = [credit ['id' ] for credit in credits_dict ]
100- lines = self .env [' account.move.line' ].search ([
101- ( 'id' , 'in' , line_ids ), (' journal_id' , '=' , self .journal_id .id )
102- ] )
103- return [credit for credit in credits_dict if credit ['id' ] in lines .ids ]
86+ line_ids = [credit ["id" ] for credit in credits_dict ]
87+ lines = self .env [" account.move.line" ].search (
88+ [( "id" , "in" , line_ids ), (" journal_id" , "=" , self .journal_id .id )]
89+ )
90+ return [credit for credit in credits_dict if credit ["id" ] in lines .ids ]
10491
10592 @api .multi
10693 def auto_unreconcile_credits (self ):
10794 for invoice in self :
108- payments_info = json .loads (invoice .payments_widget or '{}' )
109- for payment in payments_info .get ('content' , []):
110- aml = self .env ['account.move.line' ].browse (
111- payment .get ('payment_id' )
112- )
95+ payments_info = json .loads (invoice .payments_widget or "{}" )
96+ for payment in payments_info .get ("content" , []):
97+ aml = self .env ["account.move.line" ].browse (payment .get ("payment_id" ))
11398 for apr in aml .matched_debit_ids :
114- if apr .amount != payment .get (' amount' ):
99+ if apr .amount != payment .get (" amount" ):
115100 continue
116101 if (
117102 apr .payment_mode_auto_reconcile
118103 and apr .debit_move_id .invoice_id == invoice
119104 ):
120- aml .with_context (
121- invoice_id = invoice .id
122- ).remove_move_reconcile ()
105+ aml .with_context (invoice_id = invoice .id ).remove_move_reconcile ()
123106
124107 @api .depends (
125- 'type' , 'payment_mode_id' , 'payment_move_line_ids' , 'state' ,
126- 'has_outstanding'
108+ "type" , "payment_mode_id" , "payment_move_line_ids" , "state" , "has_outstanding"
127109 )
128110 def _compute_payment_mode_warning (self ):
129111 # TODO Improve me but watch out
130112 for invoice in self :
131- if invoice .type != ' out_invoice' or invoice .state == ' paid' :
132- invoice .payment_mode_warning = ''
113+ if invoice .type != " out_invoice" or invoice .state == " paid" :
114+ invoice .payment_mode_warning = ""
133115 invoice .display_payment_mode_warning = False
134116 continue
135117 invoice .display_payment_mode_warning = True
136118 if (
137- invoice .state != 'open' and invoice .payment_mode_id and
138- invoice .payment_mode_id .auto_reconcile_outstanding_credits
119+ invoice .state != "open"
120+ and invoice .payment_mode_id
121+ and invoice .payment_mode_id .auto_reconcile_outstanding_credits
139122 ):
140123 invoice .payment_mode_warning = _ (
141- ' Validating invoices with this payment mode will reconcile'
142- ' any outstanding credits.'
124+ " Validating invoices with this payment mode will reconcile"
125+ " any outstanding credits."
143126 )
144127 elif (
145- invoice .state == 'open' and invoice .payment_move_line_ids and (
146- not invoice .payment_mode_id or not
147- invoice .payment_mode_id .auto_reconcile_outstanding_credits
128+ invoice .state == "open"
129+ and invoice .payment_move_line_ids
130+ and (
131+ not invoice .payment_mode_id
132+ or not invoice .payment_mode_id .auto_reconcile_outstanding_credits
148133 )
149134 ):
150135 invoice .payment_mode_warning = _ (
151- ' Changing payment mode will unreconcile existing auto '
152- ' reconciled payments.'
136+ " Changing payment mode will unreconcile existing auto "
137+ " reconciled payments."
153138 )
154139 elif (
155- invoice .state == 'open' and not invoice .payment_move_line_ids
140+ invoice .state == "open"
141+ and not invoice .payment_move_line_ids
156142 and invoice .payment_mode_id
157143 and invoice .payment_mode_id .auto_reconcile_outstanding_credits
158144 and invoice .has_outstanding
159145 ):
160146 invoice .payment_mode_warning = _ (
161- ' Changing payment mode will reconcile outstanding credits.'
147+ " Changing payment mode will reconcile outstanding credits."
162148 )
163149 else :
164- invoice .payment_mode_warning = ''
150+ invoice .payment_mode_warning = ""
165151 invoice .display_payment_mode_warning = False
0 commit comments