bemade_multiple_billing_contacts: Added multiple unit tests and fixed a bug they found in the billing contacts / invoice functionality.

This commit is contained in:
Marc Durepos 2023-06-22 15:33:54 -04:00
parent 02624877d6
commit e374c53e63
3 changed files with 73 additions and 6 deletions

View file

@ -18,7 +18,7 @@
# #
{ {
'name': 'bemade_multiple_billing_contacts', 'name': 'bemade_multiple_billing_contacts',
'version': '15.0.1.0.0', 'version': '15.0.1.0.1',
'summary': 'Send invoices to multiple contacts by default.', 'summary': 'Send invoices to multiple contacts by default.',
'description': """By default, newly created invoices add all invoice addresses for the given partner as 'description': """By default, newly created invoices add all invoice addresses for the given partner as
followers on the invoice. If billing contacts are set manually on the sales order, those billing followers on the invoice. If billing contacts are set manually on the sales order, those billing

View file

@ -25,8 +25,8 @@ class AccountMove(models.Model):
def _post(self, soft=True): def _post(self, soft=True):
# Override the original method to subscribe the partner's billing contacts instead of self.partner_id # Override the original method to subscribe the partner's billing contacts instead of self.partner_id
initial_subscribers = self.message_partner_ids.ids initial_subscribers = self.message_partner_ids.ids
final_subscribers = initial_subscribers + self.partner_id.billing_contacts.ids final_subscribers = initial_subscribers + self.billing_contacts.ids
posted = super()._post() posted = super()._post()
self.message_unsubscribe(self.message_partner_ids - initial_subscribers) self.message_unsubscribe([s.id for s in self.message_partner_ids if s not in initial_subscribers])
self.message_subscribe(final_subscribers) self.message_subscribe(final_subscribers)
return posted return posted

View file

@ -1,6 +1,9 @@
from odoo.tests import TransactionCase, tagged from odoo.tests import TransactionCase, tagged
from odoo import Command
import datetime
@tagged('-at_install', 'post_install')
class TestBillingContacts(TransactionCase): class TestBillingContacts(TransactionCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
@ -31,10 +34,74 @@ class TestBillingContacts(TransactionCase):
'parent_id': cls.parent_co.id, 'parent_id': cls.parent_co.id,
'type': 'contact', 'type': 'contact',
}) })
cls.product = cls.env['product.product'].with_company(cls.parent_co.company_id).create({
'name': 'Product',
'categ_id': cls.env['product.category'].create({'name': 'Product Category'}).id,
'list_price': 100.0,
'type': 'service',
'uom_id': cls.env.ref('uom.product_uom_unit').id,
'uom_po_id': cls.env.ref('uom.product_uom_unit').id,
'default_code': 'PRODUCT-X',
'invoice_policy': 'order',
'expense_policy': 'no',
'taxes_id': [(6, 0, [])],
'supplier_taxes_id': [(6, 0, [])],
})
@tagged('-at_install', 'post_install') cls.sale_order = cls.env['sale.order'].create({
def test_sale_order_defaults(self): 'partner_id': cls.parent_co.id,
billing_contacts = self.parent_co.billing_contacts 'client_order_ref': 'abc123',
})
cls.env['sale.order.line'].create({
'product_id': cls.product.id,
'name': cls.product.name,
'product_uom_qty': 2,
'product_uom': cls.product.uom_id.id,
'price_unit': cls.product.list_price,
'order_id': cls.sale_order.id,
'tax_id': False,
'qty_delivered_manual': 2,
})
def test_billing_contacts_structure(self):
self.assertTrue(self.billing_contact1 in self.parent_co.billing_contacts) self.assertTrue(self.billing_contact1 in self.parent_co.billing_contacts)
self.assertTrue(self.billing_contact2 in self.parent_co.billing_contacts) self.assertTrue(self.billing_contact2 in self.parent_co.billing_contacts)
self.assertTrue(self.non_billing_contact not in self.parent_co.billing_contacts) self.assertTrue(self.non_billing_contact not in self.parent_co.billing_contacts)
def test_sale_order_default_billing_contacts(self):
self.assertTrue(self.sale_order.billing_contacts == self.parent_co.billing_contacts)
def test_sale_order_change_contacts(self):
# Test that changing the billing contacts on an SO doesn't change them on the partner
# Validate that changing them manually on the SO transfers to the invoice
self.sale_order.write({'billing_contacts': [Command.link(self.non_billing_contact.id)]})
self.assertTrue(all([c in self.sale_order.billing_contacts for c in self.parent_co.billing_contacts]))
self.assertTrue(self.non_billing_contact not in self.parent_co.billing_contacts)
self.assertTrue(self.non_billing_contact in self.sale_order.billing_contacts)
def test_sale_order_to_invoice_contacts(self):
# Test that the invoices created from sales orders take the billing contacts configured on the SO
self.sale_order.write({'billing_contacts': [Command.link(self.non_billing_contact.id)]})
self.sale_order.action_confirm()
wiz = self.env['sale.advance.payment.inv'].create({})
invoice = wiz._create_invoice(self.sale_order,self.sale_order.order_line[0],self.sale_order.order_line.price_total)
self.assertTrue(invoice)
self.assertTrue(invoice.billing_contacts == self.sale_order.billing_contacts)
def test_direct_invoice_contacts(self):
invoice = self.env['account.move'].create({
'move_type': 'out_invoice',
'partner_id': self.parent_co,
})
self.assertTrue(self.parent_co.billing_contacts == invoice.billing_contacts)
def test_invoice_followers_on_validate(self):
self.sale_order.action_confirm()
wiz = self.env['sale.advance.payment.inv'].create({})
invoice = wiz._create_invoice(self.sale_order,self.sale_order.order_line[0],self.sale_order.order_line.price_total)
invoice.write({'date': datetime.date.today()})
invoice.action_post()
self.assertTrue(all([r in invoice.message_partner_ids for r in self.parent_co.billing_contacts]))