Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

my improvements on the migration of purchase_order_analytic_search #21

Open
wants to merge 5 commits into
base: 10.0-mig-purchase_order_analytic_search
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
[IMP] computed field implementation
improve the search method on the field to handle the != operator
improve the search method on the field to handle searching on account name
gurneyalex committed Mar 15, 2018
commit 68b5a4721dae96e618b50db60f5704d846a0a0a3
1 change: 1 addition & 0 deletions purchase_order_analytic_search/README.rst
Original file line number Diff line number Diff line change
@@ -50,6 +50,7 @@ Contributors

* Gisela Mora <[email protected]>
* Serpent Consulting Services Pvt. Ltd. <[email protected]>
* Alexandre Fayolle <[email protected]>

Maintainer
----------
2 changes: 1 addition & 1 deletion purchase_order_analytic_search/__manifest__.py
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
"version": "10.0.1.0.0",
"website": "https://odoo-community.org/",
"category": "Purchase Workflow",
"author": "Eficent, Odoo Community Association (OCA)",
"author": "Eficent, Camptocamp, Odoo Community Association (OCA)",
"license": "LGPL-3",
"installable": True,
"depends": [
39 changes: 26 additions & 13 deletions purchase_order_analytic_search/models/purchase_order.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright 2015 Eficent Business and IT Consulting Services S.L.
# Copyright 2018 Camptocamp
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from odoo import api, fields, models
@@ -12,24 +13,36 @@ class PurchaseOrder(models.Model):
@api.multi
@api.depends('order_line.account_analytic_id')
def _compute_analytic_accounts(self):
res = {}
for purchase in self:
res[purchase.id] = []
for po_line in purchase.order_line:
if po_line.account_analytic_id:
res[purchase.id].append(po_line.account_analytic_id.id)
return res
purchase.account_analytic_ids = purchase.mapped(
'order_line.account_analytic_id'
)

@api.model
def _search_analytic_accounts(self, operator, value):
po_line_obj = self.env['purchase.order.line']
res = []
po_lines = po_line_obj.search(
[('account_analytic_id', operator, value)])
order_ids = [po_line.order_id and po_line.order_id.id for po_line in
po_lines]
res.append(('id', 'in', order_ids))
return res
if not value:
return [('id', '=', False)]
if isinstance(value, (tuple, list)):
# we are searching on a list of ids
domain = [('order_id', '!=', False),
('account_analytic_id', 'in', value)]
else:
if isinstance(value, int):
# we are searching on the id of the analytic_account
domain = [('order_id', '!=', False),
('account_analytic_id', '=', value)]
else:
# assume we are searching on the analytic account name
domain = [('order_id', '!=', False),
('account_analytic_id.name', 'like', value),
]
po_lines = po_line_obj.search(domain)
orders = po_lines.mapped('order_id')
if operator in ('!=', 'not in'):
return [('id', 'not in', orders.ids)]
else:
return [('id', 'in', orders.ids)]

account_analytic_ids = fields.Many2many(
comodel_name='account.analytic.account',
166 changes: 123 additions & 43 deletions purchase_order_analytic_search/tests/test_analytic_search.py
Original file line number Diff line number Diff line change
@@ -1,78 +1,158 @@
# -*- coding: utf-8 -*-
# Copyright 2015-17 Eficent Business and IT Consulting Services S.L.
# Copyright 2018 Camptocamp
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from odoo.tests.common import TransactionCase
from odoo.tests.common import SavepointCase
from odoo.fields import Datetime


class TestAnalyticSearch(TransactionCase):
class TestAnalyticSearch(SavepointCase):

def setUp(self):
super(TestAnalyticSearch, self).setUp()
self.purchase_order_model = self.env['purchase.order']
purchase_order_line_model = self.env['purchase.order.line']
partner_model = self.env['res.partner']
prod_model = self.env['product.product']
analytic_account_model = self.env['account.analytic.account']
self.product_uom_model = self.env['product.uom']
@classmethod
def setUpClass(cls):
super(TestAnalyticSearch, cls).setUpClass()
cls.purchase_order_model = cls.env['purchase.order']
partner_model = cls.env['res.partner']
prod_model = cls.env['product.product']
analytic_account_model = cls.env['account.analytic.account']
cls.product_uom_model = cls.env['product.uom']

pa_dict = {
'name': 'Partner 1',
'supplier': True,
}
self.partner = partner_model.sudo().create(pa_dict)
po_dict = {
'partner_id': self.partner.id,
}
# PURCHASE ORDER NUM 1
self.purchase_order_1 = self.purchase_order_model.create(po_dict)
uom_id = self.product_uom_model.search([
cls.partner = partner_model.create(pa_dict)
uom_id = cls.product_uom_model.search([
('name', '=', 'Unit(s)')])[0].id
pr_dict = {
'name': 'Product Test',
'uom_id': uom_id,
}
self.product = prod_model.sudo().create(pr_dict)
cls.product = prod_model.create(pr_dict)
ac_dict = {
'name': 'analytic account 1',
'name': 'account 1',
}
self.analytic_account_1 = \
analytic_account_model.sudo().create(ac_dict)
pl_dict1 = {
'date_planned': Datetime.now(),
'name': 'PO01',
'order_id': self.purchase_order_1.id,
'product_id': self.product.id,
'product_uom': uom_id,
'price_unit': 1,
'product_qty': 5.0,
'account_analytic_id': self.analytic_account_1.id,
cls.analytic_account_1 = \
analytic_account_model.create(ac_dict)
ac_dict = {
'name': 'dummyname',
}
cls.analytic_account_2 = \
analytic_account_model.create(ac_dict)
# PURCHASE ORDER NUM 1 => account 1
po_dict = {
'partner_id': cls.partner.id,
'order_line': [
(0, 0, {
'date_planned': Datetime.now(),
'name': 'PO01',
'product_id': cls.product.id,
'product_uom': uom_id,
'price_unit': 1,
'product_qty': 5.0,
'account_analytic_id': cls.analytic_account_1.id,
}),
(0, 0, {
'date_planned': Datetime.now(),
'name': 'PO01',
'product_id': cls.product.id,
'product_uom': uom_id,
'price_unit': 1,
'product_qty': 5.0,
'account_analytic_id': cls.analytic_account_1.id,
})],
}
self.purchase_order_line_1 = \
purchase_order_line_model.sudo().create(pl_dict1)
cls.purchase_order_1 = cls.purchase_order_model.create(po_dict)

# PURCHASE ORDER NUM 2
# PURCHASE ORDER NUM 2 => account 1 and 2
pa_dict2 = {
'name': 'Partner 2',
'supplier': True,
}
self.partner2 = partner_model.sudo().create(pa_dict2)
cls.partner2 = partner_model.create(pa_dict2)
po_dict2 = {
'partner_id': self.partner2.id,
'partner_id': cls.partner2.id,
'order_line': [
(0, 0, {
'date_planned': Datetime.now(),
'name': 'PO01',
'product_id': cls.product.id,
'product_uom': uom_id,
'price_unit': 1,
'product_qty': 5.0,
'account_analytic_id': cls.analytic_account_1.id,
}),
(0, 0, {
'date_planned': Datetime.now(),
'name': 'PO01',
'product_id': cls.product.id,
'product_uom': uom_id,
'price_unit': 1,
'product_qty': 5.0,
'account_analytic_id': cls.analytic_account_2.id,
}),
]
}
self.purchase_order_2 = self.purchase_order_model.create(po_dict2)
cls.purchase_order_2 = cls.purchase_order_model.create(po_dict2)
# PURCHASE ORDER NUM 3 => account 2
po_dict3 = {
'partner_id': cls.partner2.id,
'order_line': [
(0, 0, {
'date_planned': Datetime.now(),
'name': 'PO01',
'product_id': cls.product.id,
'product_uom': uom_id,
'price_unit': 1,
'product_qty': 5.0,
'account_analytic_id': cls.analytic_account_2.id,
}),
(0, 0, {
'date_planned': Datetime.now(),
'name': 'PO01',
'product_id': cls.product.id,
'product_uom': uom_id,
'price_unit': 1,
'product_qty': 5.0,
'account_analytic_id': cls.analytic_account_2.id,
}),
]
}

cls.purchase_order_3 = cls.purchase_order_model.create(po_dict3)

def test_filter_analytic_accounts(self):
found = self.purchase_order_model.search([
('account_analytic_ids', '=', self.analytic_account_1.id)])
self.assertEqual(
found, self.purchase_order_1,
'Expected %s and got %s' % (self.purchase_order_1, found))
found,
self.purchase_order_1 + self.purchase_order_2
)

def test_compute_analytic_accounts(self):
self.assertDictContainsSubset(
{self.purchase_order_1.id: [self.analytic_account_1.id]},
self.purchase_order_1._compute_analytic_accounts(),
'Method \'_compute_analytic_accounts\' does not work as expected.'
def test_filter_analytic_accounts_by_name(self):
found = self.purchase_order_model.search([
('account_analytic_ids', '=', 'nt 1')])
self.assertEqual(
found,
self.purchase_order_1 + self.purchase_order_2
)

def test_filter_analytic_accounts_not_equal(self):
found = self.purchase_order_model.search([
('account_analytic_ids', '!=', self.analytic_account_1.id)])
self.assertTrue(
self.purchase_order_3 in found
)
self.assertTrue(
self.purchase_order_1 not in found
)
self.assertTrue(
self.purchase_order_2 not in found
)

def test_compute_analytic_accounts(self):
self.assertEqual(self.purchase_order_1.account_analytic_ids,
self.analytic_account_1)
self.assertEqual(self.purchase_order_2.account_analytic_ids,
self.analytic_account_1 + self.analytic_account_2)