33# Copyright 2020 Tecnativa - Pedro M. Baeza
44# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
55
6- from odoo import _
7- from odoo .exceptions import UserError , ValidationError
6+ from odoo .exceptions import UserError
87from odoo .tests import tagged
98from odoo .tests .common import Form , TransactionCase
109
@@ -16,37 +15,23 @@ def setUpClass(cls):
1615 super ().setUpClass ()
1716 cls .account_obj = cls .env ["account.account" ]
1817 cls .account_move_obj = cls .env ["account.move" ]
19- cls .chart = cls .env ["account.chart.template" ].search ([], limit = 1 )
20- if not cls .chart :
21- raise ValidationError (
22- # translation to avoid pylint warnings
23- _ ("No Chart of Account Template has been defined !" )
24- )
2518
2619 cls .company_a = cls .env ["res.company" ].create (
2720 {
2821 "name" : "Company A" ,
29- "currency_id" : cls .env .ref ("base.EUR" ).id ,
30- "country_id" : cls .env .ref ("base.fr" ).id ,
31- "parent_id" : cls .env .ref ("base.main_company" ).id ,
3222 "invoice_auto_validation" : True ,
3323 }
3424 )
35- cls .chart .try_loading (company = cls .company_a , install_demo = False )
3625 cls .partner_company_a = cls .env ["res.partner" ].create (
3726 {"name" : cls .company_a .name , "is_company" : True }
3827 )
3928 cls .company_a .partner_id = cls .partner_company_a
4029 cls .company_b = cls .env ["res.company" ].create (
4130 {
4231 "name" : "Company B" ,
43- "currency_id" : cls .env .ref ("base.EUR" ).id ,
44- "country_id" : cls .env .ref ("base.fr" ).id ,
45- "parent_id" : cls .env .ref ("base.main_company" ).id ,
4632 "invoice_auto_validation" : True ,
4733 }
4834 )
49- cls .chart .try_loading (company = cls .company_b , install_demo = False )
5035 cls .partner_company_b = cls .env ["res.partner" ].create (
5136 {"name" : cls .company_b .name , "is_company" : True }
5237 )
@@ -59,7 +44,6 @@ def setUpClass(cls):
5944 }
6045 )
6146 cls .company_b .partner_id = cls .partner_company_b
62- # cls.partner_company_b = cls.company_b.parent_id.partner_id
6347 cls .user_company_a = cls .env ["res.users" ].create (
6448 {
6549 "name" : "User A" ,
@@ -332,15 +316,6 @@ def setUpClass(cls):
332316 "company_id" : cls .company_a .id ,
333317 }
334318 )
335- cls .pcg_X58 = cls .env ["account.account.template" ].create (
336- {
337- "name" : "Internal Transfers" ,
338- "code" : "X58" ,
339- "account_type" : "asset_current" ,
340- "reconcile" : True ,
341- }
342- )
343-
344319 cls .a_recv_company_a = cls .account_obj .create (
345320 {
346321 "code" : "X11002.A" ,
@@ -363,8 +338,18 @@ def setUpClass(cls):
363338 cls .partner_company_a .property_account_receivable_id = cls .a_recv_company_a .id
364339 cls .partner_company_a .property_account_payable_id = cls .a_pay_company_a .id
365340
366- cls .partner_company_b .property_account_receivable_id = cls .a_recv_company_b .id
367- cls .partner_company_b .property_account_payable_id = cls .a_pay_company_b .id
341+ cls .partner_company_b .with_user (
342+ cls .user_company_a .id
343+ ).property_account_receivable_id = cls .a_recv_company_a .id
344+ cls .partner_company_b .with_user (
345+ cls .user_company_a .id
346+ ).property_account_payable_id = cls .a_pay_company_a .id
347+ cls .partner_company_b .with_user (
348+ cls .user_company_b .id
349+ ).property_account_receivable_id = cls .a_recv_company_b .id
350+ cls .partner_company_b .with_user (
351+ cls .user_company_b .id
352+ ).property_account_payable_id = cls .a_pay_company_b .id
368353
369354 cls .invoice_company_a = Form (
370355 cls .account_move_obj .with_user (cls .user_company_a .id ).with_context (
@@ -373,7 +358,6 @@ def setUpClass(cls):
373358 )
374359 cls .invoice_company_a .partner_id = cls .partner_company_b
375360 cls .invoice_company_a .journal_id = cls .sales_journal_company_a
376- cls .invoice_company_a .currency_id = cls .env .ref ("base.EUR" )
377361
378362 with cls .invoice_company_a .invoice_line_ids .new () as line_form :
379363 line_form .product_id = cls .product_consultant_multi_company
@@ -390,7 +374,7 @@ def setUpClass(cls):
390374 cls .product_a = cls .invoice_line_a .product_id
391375 cls .product_a .with_user (
392376 cls .user_company_b .id
393- ).property_account_expense_id = cls .a_expense_company_b .id
377+ ).sudo (). property_account_expense_id = cls .a_expense_company_b .id
394378
395379
396380class TestAccountInvoiceInterCompany (TestAccountInvoiceInterCompanyBase ):
@@ -415,7 +399,9 @@ def test03_confirm_invoice_and_cancel(self):
415399 # ensure the catalog is shared
416400 self .env .ref ("product.product_comp_rule" ).write ({"active" : False })
417401 # Make sure there are no taxes in target company for the used product
418- self .product_a .with_user (self .user_company_b .id ).supplier_taxes_id = False
402+ self .product_a .with_user (
403+ self .user_company_b .id
404+ ).sudo ().supplier_taxes_id = False
419405 # Confirm the invoice of company A
420406 self .invoice_company_a .with_user (self .user_company_a .id ).action_post ()
421407 # Check destination invoice created in company B
@@ -443,9 +429,9 @@ def test03_confirm_invoice_and_cancel(self):
443429 self .invoice_company_a .invoice_line_ids [0 ].product_id ,
444430 )
445431 # Cancel the invoice of company A
446- invoice_origin = ( "%s - Canceled Invoice: %s" ) % (
447- self .invoice_company_a .company_id .name ,
448- self .invoice_company_a .name ,
432+ invoice_origin = "{company_name} - Canceled Invoice: {invoice_name}" . format (
433+ company_name = self .invoice_company_a .company_id .name ,
434+ invoice_name = self .invoice_company_a .name ,
449435 )
450436 self .invoice_company_a .with_user (self .user_company_a .id ).button_cancel ()
451437 # Check invoices after to cancel invoice of company A
@@ -454,6 +440,7 @@ def test03_confirm_invoice_and_cancel(self):
454440 self .assertEqual (invoices [0 ].invoice_origin , invoice_origin )
455441 # Check if keep the invoice number
456442 invoice_number = self .invoice_company_a .name
443+ self .invoice_company_a .with_user (self .user_company_a .id ).button_draft ()
457444 self .invoice_company_a .with_user (self .user_company_a .id ).action_post ()
458445 self .assertEqual (self .invoice_company_a .name , invoice_number )
459446 # When the destination invoice is posted we can't modify the origin either
@@ -484,30 +471,17 @@ def test_confirm_invoice_with_child_partner(self):
484471 )
485472 self .assertEqual (len (invoices ), 1 )
486473
487- def test_confirm_invoice_with_product_and_shared_catalog (self ):
488- """With no security rule, child company have access to any product.
489- Then child invoice can share the same product
490- """
491- # ensure the catalog is shared even if product is in other company
492- self .env .ref ("product.product_comp_rule" ).write ({"active" : False })
493- # Product is set to a specific company
494- self .product_a .write ({"company_id" : self .company_a .id })
495- invoices = self ._confirm_invoice_with_product ()
496- self .assertNotEqual (
497- invoices .invoice_line_ids [0 ].product_id , self .env ["product.product" ]
498- )
499-
500474 def test_confirm_invoice_with_native_product_rule_and_shared_product (self ):
501475 """With native security rule, products with access in both companies
502476 must be present in parent and child invoices.
503477 """
504478 # ensure the catalog is shared even if product is in other company
505479 self .env .ref ("product.product_comp_rule" ).write ({"active" : True })
506480 # Product is set to a specific company
507- self .product_a .write ({"company_id" : False })
481+ self .product_a .sudo (). write ({"company_id" : False })
508482 # If product_multi_company is installed
509483 if "company_ids" in dir (self .product_a ):
510- self .product_a .write ({"company_ids" : [(5 , 0 , 0 )]})
484+ self .product_a .sudo (). write ({"company_ids" : [(5 , 0 , 0 )]})
511485 invoices = self ._confirm_invoice_with_product ()
512486 self .assertEqual (invoices .invoice_line_ids [0 ].product_id , self .product_a )
513487
@@ -518,10 +492,10 @@ def test_confirm_invoice_with_native_product_rule_and_unshared_product(self):
518492 # ensure the catalog is shared even if product is in other company
519493 self .env .ref ("product.product_comp_rule" ).write ({"active" : True })
520494 # Product is set to a specific company
521- self .product_a .write ({"company_id" : self .company_a .id })
495+ self .product_a .sudo (). write ({"company_id" : self .company_a .id })
522496 # If product_multi_company is installed
523497 if "company_ids" in dir (self .product_a ):
524- self .product_a .write ({"company_ids" : [(6 , 0 , [self .company_a .id ])]})
498+ self .product_a .sudo (). write ({"company_ids" : [(6 , 0 , [self .company_a .id ])]})
525499 with self .assertRaises (UserError ):
526500 self ._confirm_invoice_with_product ()
527501
0 commit comments