Skip to content

Commit

Permalink
[9.0] rma_purchase: add purchase_policy
Browse files Browse the repository at this point in the history
  • Loading branch information
LoisRForgeFlow authored and AaronHForgeFlow committed May 25, 2018
1 parent a107228 commit 2100329
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 20 deletions.
2 changes: 1 addition & 1 deletion rma_purchase/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# © 2017 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from . import models
from . import wizards
13 changes: 8 additions & 5 deletions rma_purchase/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
# -*- coding: utf-8 -*-
# © 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2017-18 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
{
'name': 'RMA Purchase',
'version': '9.0.1.0.0',
'version': '9.0.2.0.0',
'category': 'RMA',
'summary': 'RMA from PO',
'license': 'LGPL-3',
'author': 'Eficent, Odoo Community Association (OCA)',
'website': 'http://www.github.com/OCA/rma',
'depends': ['rma_account', 'purchase'],
'data': ['views/rma_order_view.xml',
'views/rma_order_line_view.xml',
'wizards/rma_add_purchase.xml'],
'data': [
'views/rma_operation_view.xml',
'views/rma_order_view.xml',
'views/rma_order_line_view.xml',
'wizards/rma_add_purchase.xml',
],
'installable': True,
'auto_install': True,
}
2 changes: 2 additions & 0 deletions rma_purchase/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@

from . import rma_order
from . import rma_order_line
from . import purchase_order
from . import purchase_order_line
from . import rma_operation
29 changes: 29 additions & 0 deletions rma_purchase/models/purchase_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
# Copyright 2017-18 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from openerp import api, fields, models


class PurchaseOrder(models.Model):
_inherit = "purchase.order"

@api.model
def new(self, vals):
"""Allows to propose a line based on the RMA information."""
res = super(PurchaseOrder, self).new(vals)
rma_line_id = self.env.context.get('rma_line_id')
if rma_line_id:
rma_line = self.env['rma.order.line'].browse(rma_line_id)
line = self.env['purchase.order.line'].new({
'product_id': rma_line.product_id.id,
})
line.onchange_product_id()
line.update({
'product_qty': rma_line.qty_to_purchase,
'product_uom': rma_line.uom_id.id,
})
res.order_line = line
# TODO: maybe this line is not needed in v10:
res.date_planned = res._compute_date_planned()
return res
17 changes: 16 additions & 1 deletion rma_purchase/models/purchase_order_line.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2017-18 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from odoo import api, models
Expand All @@ -8,6 +8,14 @@
class PurchaseOrderLine(models.Model):
_inherit = "purchase.order.line"

# TODO: to be removed on migration to v10:
# This is needed because odoo misspelled `store` in v9 :facepalm:
state = fields.Selection(related='order_id.state', store=True)

rma_line_id = fields.Many2one(
comodel_name='rma.order.line', string='RMA',
)

@api.model
def name_search(self, name='', args=None, operator='ilike', limit=100):
"""Allows to search by PO reference."""
Expand Down Expand Up @@ -47,3 +55,10 @@ def name_get(self):
return res
else:
return super(PurchaseOrderLine, self).name_get()

@api.model
def create(self, vals):
rma_line_id = self.env.context.get('rma_line_id')
if rma_line_id:
vals['rma_line_id'] = rma_line_id
return super(PurchaseOrderLine, self).create(vals)
25 changes: 25 additions & 0 deletions rma_purchase/models/rma_operation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# Copyright 2018 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from openerp import api, fields, models, _
from openerp.exceptions import ValidationError


class RmaOperation(models.Model):
_inherit = 'rma.operation'

purchase_policy = fields.Selection(
selection=[('no', 'Not required'),
('ordered', 'Based on Ordered Quantities'),
('delivered', 'Based on Delivered Quantities')],
string="Purchase Policy", default='no',
)

@api.multi
@api.constrains('purchase_policy')
def _check_purchase_policy(self):
if self.filtered(
lambda r: r.purchase_policy != 'no' and r.type != 'supplier'):
raise ValidationError(_(
'Purchase Policy can only apply to supplier operations'))
64 changes: 51 additions & 13 deletions rma_purchase/models/rma_order_line.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# © 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2017-18 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo import api, fields, models, _
from odoo.exceptions import ValidationError
Expand All @@ -17,7 +17,9 @@ def _compute_purchase_count(self):
if procurement_id.purchase_id and \
procurement_id.purchase_id.id:
purchase_list.append(procurement_id.purchase_id.id)
rec.purchase_count = len(list(set(purchase_list)))
rec.purchase_count = (
len(list(set(purchase_list))) +
len(rec.manual_purchase_line_ids.mapped('order_id')))

@api.multi
@api.depends('procurement_ids.purchase_line_id')
Expand All @@ -31,10 +33,18 @@ def _compute_purchase_order_lines(self):
rec.purchase_order_line_ids = [(6, 0, purchase_list)]

@api.multi
@api.depends('procurement_ids.purchase_line_id')
def _compute_qty_purchased(self):
@api.depends('procurement_ids.purchase_line_id',
'manual_purchase_line_ids',
'manual_purchase_line_ids.state', 'qty_delivered')
def _compute_qty_purchase(self):
for rec in self:
rec.qty_purchased = rec._get_rma_purchased_qty()
if rec.purchase_policy == 'ordered':
rec.qty_to_purchase = rec.product_qty - rec.qty_purchased
elif rec.purchase_policy == 'delivered':
rec.qty_to_purchase = rec.qty_delivered - rec.qty_purchased
else:
rec.qty_to_purchase = 0.0

purchase_count = fields.Integer(
compute='_compute_purchase_count', string='# of Purchases',
Expand All @@ -55,12 +65,36 @@ def _compute_qty_purchased(self):
column1='rma_order_line_id', column2='purchase_order_line_id',
string='Purchase Order Lines', compute='_compute_purchase_order_lines',
)
purchase_policy = fields.Selection(
selection=[('no', 'Not required'),
('ordered', 'Based on Ordered Quantities'),
('delivered', 'Based on Delivered Quantities')],
string="Purchase Policy", default='no',
required=True,
)
manual_purchase_line_ids = fields.One2many(
comodel_name='purchase.order.line',
inverse_name='rma_line_id',
string='Manual Purchase Order Lines',
readonly=True, copy=False)
qty_to_purchase = fields.Float(
string='Qty To Purchase', copy=False,
digits=dp.get_precision('Product Unit of Measure'),
readonly=True, compute='_compute_qty_purchase', store=True,
)
qty_purchased = fields.Float(
string='Qty Purchased', copy=False,
digits=dp.get_precision('Product Unit of Measure'),
readonly=True, compute='_compute_qty_purchased', store=True,
readonly=True, compute='_compute_qty_purchase', store=True,
)

@api.onchange('operation_id')
def _onchange_operation_id(self):
res = super(RmaOrderLine, self)._onchange_operation_id()
if self.operation_id:
self.purchase_policy = self.operation_id.purchase_policy or 'no'
return res

@api.multi
def _prepare_rma_line_from_po_line(self, line):
self.ensure_one()
Expand Down Expand Up @@ -144,20 +178,24 @@ def _remove_other_data_origin(self, exception):
def action_view_purchase_order(self):
action = self.env.ref('purchase.purchase_rfq')
result = action.read()[0]
order_ids = []
for procurement_id in self.procurement_ids:
order_ids.append(procurement_id.purchase_id.id)
result['domain'] = [('id', 'in', order_ids)]
orders = self.mapped('procurement_ids.purchase_id')
orders += self.mapped('manual_purchase_line_ids.order_id')
result['domain'] = [('id', 'in', orders.ids)]
return result

@api.multi
def _get_rma_purchased_qty(self):
self.ensure_one()
qty = 0.0
if self.type == 'customer':
return qty
uom_obj = self.env['product.uom']
for procurement_id in self.procurement_ids:
purchase_line = procurement_id.purchase_line_id
if self.type == 'supplier':
qty += purchase_line.product_qty
else:
qty = 0.0
qty += purchase_line.product_qty

for line in self.manual_purchase_line_ids.filtered(
lambda p: p.state not in ('draft', 'sent', 'cancel')):
qty += uom_obj._compute_qty(
self.uom_id.id, line.product_qty, line.product_uom.id)
return qty
28 changes: 28 additions & 0 deletions rma_purchase/views/rma_operation_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2018 Eficent Business and IT Consulting Services S.L.
License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl-3.0) -->
<odoo>

<record id="rma_operation_tree" model="ir.ui.view">
<field name="name">rma.operation.tree - rma_purchase</field>
<field name="model">rma.operation</field>
<field name="inherit_id" ref="rma.rma_operation_tree"/>
<field name="arch" type="xml">
<field name="delivery_policy" position="after">
<field name="purchase_policy"/>
</field>
</field>
</record>

<record id="rma_operation_form" model="ir.ui.view">
<field name="name">rma.operation.form - rma_purchase</field>
<field name="model">rma.operation</field>
<field name="inherit_id" ref="rma.rma_operation_form"/>
<field name="arch" type="xml">
<field name="delivery_policy" position="after">
<field name="purchase_policy"/>
</field>
</field>
</record>

</odoo>
26 changes: 26 additions & 0 deletions rma_purchase/views/rma_order_line_view.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>

<record id="action_rma_line_purchase" model="ir.actions.act_window">
<field name="name">Purchase Order</field>
<field name="res_model">purchase.order</field>
<field name="view_type">form</field>
<field name="target">current</field>
<field name="view_mode">form,tree</field>
</record>

<record id="view_rma_line_supplier_button_sale_form" model="ir.ui.view">
<field name="name">rma.order.line.supplier.form</field>
<field name="model">rma.order.line</field>
<field name="inherit_id" ref="rma.view_rma_line_supplier_button_form"/>
<field name="arch" type="xml">
<header position="inside">
<button name="%(action_rma_line_purchase)d" states="approved"
string="Create Purchase Order" class="oe_highlight"
context="{'rma_line_id': active_id, 'default_partner_id': partner_id}"
type="action"/>
</header>
</field>
</record>

<record id="view_rma_line_form" model="ir.ui.view">
<field name="name">rma.order.line.supplier.form</field>
<field name="model">rma.order.line</field>
Expand All @@ -25,9 +47,13 @@
</group>
<group name="quantities" position="inside">
<group>
<field name="qty_to_purchase"/>
<field name="qty_purchased"/>
</group>
</group>
<field name="delivery_policy" position="after">
<field name="purchase_policy"/>
</field>
<field name="origin" position="after">
<field name="purchase_id"
attrs="{'invisible': [('purchase_order_line_id', '=', False)]}"/>
Expand Down

0 comments on commit 2100329

Please sign in to comment.