4
4
5
5
import logging
6
6
import re
7
+ import warnings
7
8
from datetime import datetime
8
9
10
+ import psycopg2
11
+
9
12
from odoo import api , fields , models
10
13
from odoo .exceptions import UserError
11
14
from odoo .fields import first
@@ -614,14 +617,9 @@ def get_line_product(self, line, partner):
614
617
return product
615
618
616
619
def adjust_accounting_data (self , product , line_vals ):
617
- account = self .get_credit_account (product )
618
- line_vals ["account_id" ] = account .id
619
-
620
620
new_tax = None
621
621
if len (product .product_tmpl_id .supplier_taxes_id ) == 1 :
622
622
new_tax = product .product_tmpl_id .supplier_taxes_id [0 ]
623
- elif len (account .tax_ids ) == 1 :
624
- new_tax = account .tax_ids [0 ]
625
623
line_tax = self .env ["account.tax" ]
626
624
if (
627
625
line_vals .get ("tax_ids" )
@@ -651,10 +649,16 @@ def adjust_accounting_data(self, product, line_vals):
651
649
# move_line.tax_ids
652
650
# move_line.name
653
651
# move_line.sequence
654
- # move_line.account_id
655
652
# move_line.price_unit
656
653
# move_line.quantity
657
654
def _prepareInvoiceLineAliquota (self , credit_account_id , line , nline ):
655
+ if credit_account_id :
656
+ warnings .warn (
657
+ "The `credit_account_id` argument is deprecated "
658
+ "because the account is automatically computed from the move line." ,
659
+ DeprecationWarning ,
660
+ stacklevel = 2 ,
661
+ )
658
662
retLine = {}
659
663
account_taxes = self .get_account_taxes (line .AliquotaIVA , line .Natura )
660
664
if account_taxes :
@@ -666,27 +670,31 @@ def _prepareInvoiceLineAliquota(self, credit_account_id, line, nline):
666
670
{
667
671
"name" : f"Riepilogo Aliquota { line .AliquotaIVA } " ,
668
672
"sequence" : nline ,
669
- "account_id" : credit_account_id ,
670
673
"price_unit" : float (abs (line .ImponibileImporto )),
671
674
}
672
675
)
673
676
return retLine
674
677
675
678
# move_line.name
676
679
# move_line.sequence
677
- # move_line.account_id
678
680
# move_line.price_unit
679
681
# move_line.quantity
680
682
# move_line.discount
681
683
# move_line.admin_ref
682
684
# move_line.invoice_line_tax_wt_ids
683
685
def _prepareInvoiceLine (self , credit_account_id , line , wt_founds = False ):
686
+ if credit_account_id :
687
+ warnings .warn (
688
+ "The `credit_account_id` argument is deprecated "
689
+ "because the account is automatically computed from the move line." ,
690
+ DeprecationWarning ,
691
+ stacklevel = 2 ,
692
+ )
684
693
retLine = self ._prepare_generic_line_data (line )
685
694
retLine .update (
686
695
{
687
696
"name" : line .Descrizione ,
688
697
"sequence" : int (line .NumeroLinea ),
689
- "account_id" : credit_account_id ,
690
698
"price_unit" : float (line .PrezzoUnitario ),
691
699
"display_type" : "product" ,
692
700
}
@@ -1029,52 +1037,13 @@ def create_e_invoice_line(self, line):
1029
1037
return einvoiceline
1030
1038
1031
1039
def get_credit_account (self , product = None ):
1032
- """
1033
- Try to get default credit account for invoice line looking in
1034
-
1035
- 1) product (if provided)
1036
- 2) journal
1037
- 3) company default.
1038
-
1039
- :param product: Product whose expense account will be used
1040
- :return: The account found
1041
- """
1042
- credit_account = self .env ["account.account" ].browse ()
1043
-
1044
- # If there is a product, get its default expense account
1045
- if product :
1046
- template = product .product_tmpl_id
1047
- accounts_dict = template .get_product_accounts ()
1048
- credit_account = accounts_dict ["expense" ]
1049
-
1050
- company = self .env .company
1051
- # Search in journal
1052
- journal = self .get_journal (company )
1053
- if not credit_account :
1054
- credit_account = journal .default_account_id
1055
-
1056
- # Search in company defaults
1057
- if not credit_account :
1058
- credit_account = (
1059
- self .env ["ir.property" ]
1060
- .with_company (company )
1061
- ._get ("property_account_expense_categ_id" , "product.category" )
1062
- )
1063
-
1064
- if not credit_account :
1065
- raise UserError (
1066
- _ (
1067
- "Please configure Default Credit Account "
1068
- "in Journal '{journal}' "
1069
- "or check default expense account "
1070
- "for company '{company}'."
1071
- ).format (
1072
- journal = journal .display_name ,
1073
- company = company .display_name ,
1074
- )
1075
- )
1076
-
1077
- return credit_account
1040
+ warnings .warn (
1041
+ "The `get_credit_account` method is deprecated "
1042
+ "because the account is automatically computed from the move line." ,
1043
+ DeprecationWarning ,
1044
+ stacklevel = 2 ,
1045
+ )
1046
+ return self .env ["account.account" ].browse ()
1078
1047
1079
1048
def _get_currency (self , FatturaBody ):
1080
1049
# currency 2.1.1.2
@@ -1207,14 +1176,13 @@ def invoiceCreate(self, fatt, fatturapa_attachment, FatturaBody, partner_id):
1207
1176
found_withholding_taxes = self .set_withholding_tax (FatturaBody , invoice_data )
1208
1177
1209
1178
invoice = self .env ["account.move" ].create (invoice_data )
1210
- credit_account = self .get_credit_account ()
1211
1179
1212
1180
invoice_lines = []
1213
1181
# 2.2.1
1214
1182
invoice_lines .extend (
1215
1183
self .set_invoice_line_ids (
1216
1184
FatturaBody ,
1217
- credit_account . id ,
1185
+ False ,
1218
1186
partner ,
1219
1187
found_withholding_taxes ,
1220
1188
invoice ,
@@ -1223,9 +1191,7 @@ def invoiceCreate(self, fatt, fatturapa_attachment, FatturaBody, partner_id):
1223
1191
1224
1192
# 2.1.1.7
1225
1193
invoice_lines .extend (
1226
- self .set_welfares_fund (
1227
- FatturaBody , credit_account .id , invoice , found_withholding_taxes
1228
- )
1194
+ self .set_welfares_fund (FatturaBody , False , invoice , found_withholding_taxes )
1229
1195
)
1230
1196
1231
1197
# 2.1.1.10
@@ -1600,6 +1566,13 @@ def set_withholding_tax(self, FatturaBody, invoice_data):
1600
1566
return found_withholding_taxes
1601
1567
1602
1568
def set_welfares_fund (self , FatturaBody , credit_account_id , invoice , wt_founds ):
1569
+ if credit_account_id :
1570
+ warnings .warn (
1571
+ "The `credit_account_id` argument is deprecated "
1572
+ "because the account is automatically computed from the move line." ,
1573
+ DeprecationWarning ,
1574
+ stacklevel = 2 ,
1575
+ )
1603
1576
invoice_line_model = self .env ["account.move.line" ]
1604
1577
invoice_line_ids = []
1605
1578
if self .e_invoice_detail_level == "2" :
@@ -1621,7 +1594,6 @@ def set_welfares_fund(self, FatturaBody, credit_account_id, invoice, wt_founds):
1621
1594
"name" : _ ("Welfare Fund: %s" ) % welfareLine .TipoCassa ,
1622
1595
"price_unit" : float (welfareLine .ImportoContributoCassa ),
1623
1596
"move_id" : invoice .id ,
1624
- "account_id" : credit_account_id ,
1625
1597
"quantity" : 1 ,
1626
1598
}
1627
1599
)
@@ -1735,6 +1707,13 @@ def _set_invoice_lines(
1735
1707
def set_invoice_line_ids (
1736
1708
self , FatturaBody , credit_account_id , partner , wt_founds , invoice
1737
1709
):
1710
+ if credit_account_id :
1711
+ warnings .warn (
1712
+ "The `credit_account_id` argument is deprecated "
1713
+ "because the account is automatically computed from the move line." ,
1714
+ DeprecationWarning ,
1715
+ stacklevel = 2 ,
1716
+ )
1738
1717
invoice_lines = []
1739
1718
invoice_line_model = self .env ["account.move.line" ]
1740
1719
if self .e_invoice_detail_level == "1" :
@@ -1779,11 +1758,29 @@ def check_invoice_amount(self, invoice, FatturaElettronicaBody):
1779
1758
)
1780
1759
1781
1760
def create_and_get_line_id (self , invoice_line_ids , invoice_line_model , upd_vals ):
1782
- invoice_line_id = (
1783
- invoice_line_model .with_context (check_move_validity = False )
1784
- .create (upd_vals )
1785
- .id
1786
- )
1761
+ try :
1762
+ with self .env .cr .savepoint ():
1763
+ invoice_line_id = (
1764
+ invoice_line_model .with_context (check_move_validity = False )
1765
+ .create (upd_vals )
1766
+ .id
1767
+ )
1768
+ except psycopg2 .errors .CheckViolation as cv :
1769
+ if (
1770
+ cv .diag .constraint_name
1771
+ == "account_move_line_check_accountable_required_fields"
1772
+ ):
1773
+ move = self .env ["account.move" ].browse (upd_vals ["move_id" ])
1774
+ journal = move .journal_id
1775
+ if journal :
1776
+ raise UserError (
1777
+ _ (
1778
+ "Please configure Default Credit Account "
1779
+ "in Journal %(journal)s'." ,
1780
+ journal = journal .display_name ,
1781
+ )
1782
+ ) from cv
1783
+ raise cv
1787
1784
invoice_line_ids .append (invoice_line_id )
1788
1785
1789
1786
def _set_decimal_precision (self , precision_name , field_name , attachments ):
0 commit comments