Skip to content

Commit

Permalink
Merge PR #659 into 10.0
Browse files Browse the repository at this point in the history
Signed-off-by jbeficent
  • Loading branch information
OCA-git-bot committed Nov 7, 2019
2 parents 67b4dde + 9dfc651 commit 4308c73
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 38 deletions.
2 changes: 1 addition & 1 deletion purchase_request/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"name": "Purchase Request",
"author": "Eficent Business and IT Consulting Services S.L., "
"Odoo Community Association (OCA)",
"version": "10.0.1.1.2",
"version": "10.0.1.2.0",
"summary": "Use this module to have notification of requirements of "
"materials and/or external services and keep track of such "
"requirements.",
Expand Down
4 changes: 4 additions & 0 deletions purchase_request/models/purchase_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ def _track_subtype(self, init_values):
compute='_compute_line_count',
readonly=True
)
product_id = fields.Many2one('product.product',
related='line_ids.product_id',
readonly=True,
string='Product')

@api.depends('line_ids')
def _compute_line_count(self):
Expand Down
6 changes: 4 additions & 2 deletions purchase_request/views/purchase_request_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
<search string="Search Purchase Request">
<field name="name" string="Purchase Request"/>
<separator/>
<field name="product_id"/>
<field name="state"/>
<filter name="unassigned" string="Unassigned"
domain="[('assigned_to','=', False)]"
Expand Down Expand Up @@ -247,7 +248,8 @@
<field name="model">purchase.request.line</field>
<field name="arch" type="xml">
<tree string="Purchase Request Lines" create="false"
decoration-muted="cancelled == True">
decoration-muted="cancelled == True"
decoration-info="request_state in ('draft', 'to_approve')">
<field name="request_id"/>
<field name="request_state"/>
<field name="requested_by"/>
Expand Down Expand Up @@ -310,6 +312,7 @@
</div>
<field name="analytic_account_id"
groups="analytic.group_analytic_accounting"
domain="[('type','not in',('view','template'))]"
attrs="{'readonly': [('is_editable','=', False)]}"/>
<field name="date_required"
attrs="{'readonly': [('is_editable','=', False)]}"/>
Expand Down Expand Up @@ -403,7 +406,6 @@
<field name="res_model">purchase.request.line</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="domain">[('request_state','!=','draft')]</field>
<field name="search_view_id" ref="purchase_request_line_search"/>
</record>

Expand Down
4 changes: 2 additions & 2 deletions purchase_request_procurement/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

{
'name': "Purchase Request Procurement",
'version': "10.0.1.0.1",
'version': "10.0.1.1.0",
'author': "Eficent Business and IT Consulting Services S.L., "
"Odoo Community Association (OCA)",
'website': "https://github.com/OCA/purchase-workflow",
Expand All @@ -16,8 +16,8 @@
'data': [
"views/product_template.xml",
"views/procurement_order.xml",
"views/purchase_request_view.xml",
],
'demo': [],
'license': 'LGPL-3',
'installable': True,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2018 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl-3.0).

import logging
logger = logging.getLogger(__name__)


def fill_purchase_request_group_id(cr):
logger.info('Migrating procurement group in purchase request.')

cr.execute("""
UPDATE purchase_request as pr
SET group_id = rule.group_id
FROM purchase_request_line as prl
INNER JOIN procurement_order as proc
ON proc.id = prl.procurement_id
INNER JOIN procurement_group as pg
ON proc.group_id = pg.id
INNER JOIN procurement_rule as rule
ON rule.id = proc.rule_id
WHERE prl.request_id = pr.id
AND rule.group_propagation_option = 'fixed'
""")

cr.execute("""
UPDATE purchase_request as pr
SET group_id = pg.id
FROM purchase_request_line as prl
INNER JOIN procurement_order as proc
ON proc.id = prl.procurement_id
INNER JOIN procurement_group as pg
ON proc.group_id = pg.id
INNER JOIN procurement_rule as rule
ON rule.id = proc.rule_id
WHERE prl.request_id = pr.id
AND rule.group_propagation_option = 'propagate'
""")


def migrate(cr, version):
if not version:
return
fill_purchase_request_group_id(cr)
64 changes: 46 additions & 18 deletions purchase_request_procurement/models/procurement_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ProcurementOrder(models.Model):
)

@api.multi
def _prepare_purchase_request_line(self):
def _prepare_purchase_request_line(self, pr):
self.ensure_one()
product = self.product_id
procurement_uom_po_qty = self.product_uom._compute_quantity(
Expand All @@ -27,35 +27,50 @@ def _prepare_purchase_request_line(self):
'date_required': self.date_planned,
'product_uom_id': product.uom_po_id.id,
'product_qty': procurement_uom_po_qty,
'request_id': self.request_id.id,
'request_id': pr.id,
'procurement_id': self.id
}

@api.multi
def _prepare_purchase_request(self):
self.ensure_one()

gpo = self.rule_id.group_propagation_option
group_id = (gpo == 'fixed' and self.group_id.id) or \
(gpo == 'propagate' and self.group_id.id) or False
return {
'origin': self.origin,
'company_id': self.company_id.id,
'picking_type_id': self.rule_id.picking_type_id.id,
'group_id': group_id,
}

@api.multi
def _search_existing_purchase_request(self):
def _make_pr_get_domain(self, values):
"""
This method is to be implemented by other modules that can
provide a criteria to select the appropriate purchase request to be
extended.
:return: False
"""
return False
domain = (
('state', '=', 'draft'),
('picking_type_id', '=', values['picking_type_id']),
('company_id', '=', values['company_id']),
)
gpo = self.rule_id.group_propagation_option
group_id = (gpo == 'fixed' and self.group_id.id) or \
(gpo == 'propagate' and self.group_id.id) or False
if group_id:
domain += (
('group_id', '=', group_id),
)
return domain

@api.multi
def _run(self):
self.ensure_one()
if self.is_create_purchase_request_allowed():
self.create_purchase_request()
self.request_id = self.create_purchase_request()
return True
return super(ProcurementOrder, self)._run()

Expand Down Expand Up @@ -83,19 +98,32 @@ def create_purchase_request(self):

purchase_request_model = self.env['purchase.request']
purchase_request_line_model = self.env['purchase.request.line']

# Search for an existing Purchase Request to be considered
# to be extended.
pr = self._search_existing_purchase_request()
origin = self.origin
cache = {}
pr = self.env['purchase.request']
request_data = self._prepare_purchase_request()
domain = self._make_pr_get_domain(request_data)
if domain in cache:
pr = cache[domain]
elif domain:
pr = self.env['purchase.request'].search([dom for dom in domain])
pr = pr[0] if pr else False
cache[domain] = pr
if not pr:
request_data = self._prepare_purchase_request()
req = purchase_request_model.create(request_data)
self.message_post(body=_("Purchase Request created"))
self.request_id = req
request_line_data = self._prepare_purchase_request_line()
purchase_request_line_model.create(request_line_data),
self.message_post(body=_("Purchase Request extended."))
return self.request_id
pr = purchase_request_model.create(request_data)
cache[domain] = pr
elif not pr.origin or origin not in pr.origin.split(', '):
if pr.origin:
if origin:
pr.write({'origin': pr.origin + ', ' + origin})
else:
pr.write({'origin': pr.origin})
else:
pr.write({'origin': origin})
# Create Line
request_line_data = self._prepare_purchase_request_line(pr)
purchase_request_line_model.create(request_line_data)
return pr

@api.multi
def propagate_cancels(self):
Expand Down
19 changes: 11 additions & 8 deletions purchase_request_procurement/models/purchase_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl-3.0).

from odoo import api, models, _
from odoo.exceptions import UserError
from odoo import api, fields, models, _
from odoo.exceptions import ValidationError


class PurchaseRequest(models.Model):

_inherit = 'purchase.request'

group_id = fields.Many2one(
'procurement.group', string="Procurement Group", copy=False)

@api.multi
def _check_rejected_allowed(self):
if any([s == 'done' for s in self.mapped(
'line_ids.procurement_id.state')]):
raise UserError(
_('You cannot reject a purchase request with lines related to '
'done procurement orders.'))
raise ValidationError(
_('You cannot reject a purchase request with lines related '
'to done procurement orders.'))

@api.multi
def button_rejected(self):
Expand All @@ -27,9 +30,9 @@ def button_rejected(self):
def _check_reset_allowed(self):
if any([s == 'done' for s in self.mapped(
'line_ids.procurement_id.state')]):
raise UserError(
_('You cannot set back to draft a purchase request with lines '
'related to done procurement orders.'))
raise ValidationError(
_('You cannot set back to draft a purchase request with '
'lines related to done procurement orders.'))

@api.multi
def button_draft(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from odoo.tests import common
from odoo import fields
from odoo.exceptions import UserError
from odoo.exceptions import ValidationError


class TestPurchaseRequestProcurement(common.SavepointCase):
Expand Down Expand Up @@ -76,13 +76,14 @@ def test_cancel_procurement(self):

def test_cancel_line_with_done_procurement(self):
"""Tests that it isn't allowed to cancel or reset a PR with lines
realted to done procurements."""
related to done procurements."""
proc = self.create_procurement_order('TEST/0003', self.product_1, 4)
proc.run()
request = proc.request_id
proc.write({'state': 'done'})
with self.assertRaises(UserError):
with self.assertRaises(ValidationError):
request.button_rejected()
with self.assertRaises(UserError):
with self.assertRaises(ValidationError):
request.button_draft()

def test_product_uom_po_id(self):
Expand Down
19 changes: 19 additions & 0 deletions purchase_request_procurement/views/purchase_request_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<!-- Copyright 2016 Eficent Business and IT Consulting Services S.L.
License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl-3.0) -->
<odoo>

<record model="ir.ui.view" id="view_purchase_request_form">
<field name="name">purchase.request.form</field>
<field name="model">purchase.request</field>
<field name="inherit_id"
ref="purchase_request.view_purchase_request_form"/>
<field name="arch" type="xml">
<field name="assigned_to" position="after">
<field name="group_id"
attrs="{'readonly': [('is_editable','=', False)]}"/>
</field>
</field>
</record>

</odoo>
2 changes: 1 addition & 1 deletion purchase_request_to_rfq/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"author": "Eficent, "
"Acsone SA/NV,"
"Odoo Community Association (OCA)",
"version": "10.0.1.0.3",
"version": "10.0.1.1.0",
"website": "https://github.com/OCA/purchase-workflow",
"category": "Purchase Management",
"depends": [
Expand Down
2 changes: 0 additions & 2 deletions purchase_request_to_rfq/views/purchase_order_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
position="inside">
<field name="purchase_request_lines" invisible="1"/>
<button string="Purchase Request lines"
attrs="{'invisible': [('purchase_request_lines', '=', [])]}"
name="action_openRequestLineTreeView"
type="object"
icon="fa-folder-open-o"/>
Expand Down Expand Up @@ -41,7 +40,6 @@
<xpath expr="//tree" position="inside">
<field name="purchase_request_lines" invisible="1"/>
<button string="Purchase Request lines"
attrs="{'invisible': [('purchase_request_lines', '=', [])]}"
name="action_openRequestLineTreeView"
type="object"
icon="fa-folder-open-o"/>
Expand Down

0 comments on commit 4308c73

Please sign in to comment.