Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions addons/sale_purchase/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def _compute_purchase_count(self):

@api.onchange('product_uom_qty')
def _onchange_service_product_uom_qty(self):
if self.state == 'sale' and self.product_id.type == 'service' and self.product_id.service_to_purchase:
if self.state == 'sale' and self.product_id.type == 'service' and self.product_id.with_company(self._purchase_service_get_company()).service_to_purchase:
if self.product_uom_qty < self._origin.product_uom_qty:
if self.product_uom_qty < self.qty_delivered:
return {}
Expand Down Expand Up @@ -55,8 +55,8 @@ def write(self, values):
decreased_values = {}
if 'product_uom_qty' in values:
precision = self.env['decimal.precision'].precision_get('Product Unit of Measure')
increased_lines = self.sudo().filtered(lambda r: r.product_id.service_to_purchase and r.purchase_line_count and float_compare(r.product_uom_qty, values['product_uom_qty'], precision_digits=precision) == -1)
decreased_lines = self.sudo().filtered(lambda r: r.product_id.service_to_purchase and r.purchase_line_count and float_compare(r.product_uom_qty, values['product_uom_qty'], precision_digits=precision) == 1)
increased_lines = self.sudo().filtered(lambda r: r.product_id.with_company(r._purchase_service_get_company()).service_to_purchase and r.purchase_line_count and float_compare(r.product_uom_qty, values['product_uom_qty'], precision_digits=precision) == -1)
decreased_lines = self.sudo().filtered(lambda r: r.product_id.with_company(r._purchase_service_get_company()).service_to_purchase and r.purchase_line_count and float_compare(r.product_uom_qty, values['product_uom_qty'], precision_digits=precision) == 1)
increased_values = {line.id: line.product_uom_qty for line in increased_lines}
decreased_values = {line.id: line.product_uom_qty for line in decreased_lines}

Expand Down
70 changes: 44 additions & 26 deletions addons/sale_purchase/tests/test_sale_purchase.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,37 +350,55 @@ def test_pol_analytic_distribution(self):
self.assertEqual(purchase_line1.analytic_distribution, {str(self.sale_order_1.analytic_account_id.id): 100}, "Analytic Distribution in PO should be same as Analytic Account set in SO")
self.assertEqual(purchase_line2.analytic_distribution, {str(self.test_analytic_account_2.id): 100}, "Analytic Distribution in PO should be same as Analytic Distribution set in SOL")

def test_sale_order_multi_comp(self):
''' Test that the purchase service is done on the right company (the one of the SO) '''
company_a, company_b = self.company_data["company"], self.company_data_2["company"]

service_purchase = self.env['product.product'].with_company(company_a).create({
'name': "service 1",
'purchase_ok': True,
'sale_ok': True,
'list_price': 50,
'type': 'service',
'service_to_purchase': True,
'seller_ids': [
(0, 0, {'partner_id': self.partner_b.id, 'price': 100, 'company_id': company_a.id}),
],
def test_service_to_purchase_multi_company(self):
"""Test the service to purchase in a multi-company environment

The `product.template.service_to_purchase` is a company_dependent field, whose
value depends on the company are in, which is not necessarily the order company

Granted that:
- The current company is company_1
- The product is configured as a service to be purchased on company_1
- The product is NOT configured as a service to be purchased on company_2
- We process an order on company_2, while being logged in company_1

The order must be processed without generating a PO, respecting the product
setting for this order's company. We also check that the opposite case holds
true as well (i.e. PO is generated when confirming with a company that isn't
configured for it, but the SO's company is)
"""
company_1 = self.env.company
company_2 = self.company_data_2['company']
self.env.user.company_ids += company_2
self.assertTrue(self.service_purchase_1.service_to_purchase)
self.assertFalse(self.service_purchase_1.with_company(company_2).service_to_purchase)
order = self.env['sale.order'].create({
'partner_id': self.partner_a.id,
'company_id': company_2.id,
'order_line': [
Command.create({
'product_id': self.service_purchase_1.id,
'product_uom_qty': 1,
})
]
})
self.assertFalse(service_purchase.with_company(company_b).service_to_purchase)
# FIXME: there is some sort of multi-company misconfiguration with the permissions that require a sudo here
# for this test to run. Issue doesn't occur when running test locally => probably some other module is messing
# with the permissions and/or there's an issue with the subsidiary setup
order.sudo().with_company(company_1).action_confirm()
self.assertFalse(order.purchase_order_count)

so = self.env['sale.order'].create({
order2 = self.env['sale.order'].create({
'partner_id': self.partner_a.id,
'company_id': company_a.id,
'company_id': company_1.id,
'order_line': [
(0, 0, {
'name': service_purchase.name,
'product_id': service_purchase.id,
Command.create({
'product_id': self.service_purchase_1.id,
'product_uom_qty': 1,
})
],
]
})

self.env.user.company_id = company_b
so.action_confirm()

po = self.env['purchase.order'].search([('partner_id', '=', self.partner_b.id)], limit=1)
self.assertTrue(po)
# FIXME: same sudo issue as above
order2.sudo().with_company(company_2).action_confirm()
self.assertTrue(order2.purchase_order_count)