Skip to content

Commit

Permalink
Merge pull request #23 from yvaucher/8.0-framework-agreement-sourcing…
Browse files Browse the repository at this point in the history
…-fix-onchanges

port onchanges to API 8.0 to fix #22
  • Loading branch information
lepistone committed Nov 10, 2014
2 parents 48915fe + e350d69 commit 9fd6a2a
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 104 deletions.
177 changes: 97 additions & 80 deletions framework_agreement_sourcing/model/logistic_requisition_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
##############################################################################
#
# Author: Nicolas Bessi
# Copyright 2013 Camptocamp SA
# Copyright 2013-2014 Camptocamp SA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand All @@ -20,9 +20,9 @@
##############################################################################
from itertools import chain

from openerp.osv import orm, fields
from openerp import fields, api, osv
from openerp.osv import orm
from openerp.tools.translate import _
from openerp.addons.framework_agreement.utils import id_boilerplate


class logistic_requisition_source(orm.Model):
Expand All @@ -32,13 +32,15 @@ class logistic_requisition_source(orm.Model):
_inherit = "logistic.requisition.source"

_columns = {
'framework_agreement_id': fields.many2one('framework.agreement',
'Agreement'),
'framework_agreement_po_id': fields.many2one('purchase.order',
'Agreement Purchase'),
'supplier_id': fields.related('framework_agreement_id', 'supplier_id',
type='many2one', relation='res.partner',
string='Agreement Supplier')}
'framework_agreement_id': osv.fields.many2one('framework.agreement',
'Agreement'),
'framework_agreement_po_id': osv.fields.many2one(
'purchase.order',
'Agreement Purchase'),
'supplier_id': osv.fields.related(
'framework_agreement_id', 'supplier_id',
type='many2one', relation='res.partner',
string='Agreement Supplier')}

def _get_procur_method_hook(self, cr, uid, context=None):
"""Adds framework agreement as a procurement method in selection
Expand Down Expand Up @@ -260,27 +262,79 @@ def _is_sourced_fw_agreement(self, cr, uid, source, context=None):
# predicate
return bool(sources_ids)

# ---------------OpenERP tedious onchange management ----------------------
# ---------------Odoo onchange management ----------------------

def _get_date(self, cr, uid, requision_line_id, context=None):
@api.model
def _get_date(self):
"""helper to retrive date to be used by framework agreement
when in source line context
:param source_id: requisition.line.source id that should
:param requisition_line_id: requisition.line id that should
provide date
:returns: date/datetime string
"""
req_obj = self.pool['logistic.requisition.line']
current = req_obj.browse(cr, uid, requision_line_id, context=context)
now = fields.datetime.now()
return current.requisition_id.date or now
return self.requisition_id.date or now

@id_boilerplate
def onchange_sourcing_method(self, cr, uid, ids, method, req_line_id,
proposed_product_id, proposed_qty=0,
context=None):
@api.multi
def _check_enought_qty(self, agreement):
""" Raise a warning if quantity is not enough
to fullfil completely the sourcing
:returns: dict with warning message
"""
if self.proposed_qty > agreement.available_quantity:
msg = _("You have asked for a quantity of %s \n"
" but there is only %s available"
" for current agreement") % (self.proposed_qty,
agreement.available_quantity)

return {'warning': {'message': msg}}

@api.multi
def _get_best_agreement(self):
""" Search for an agreement with enough quantity
and if not find, search for an agreement without
specifying qty
:returns: agreement record
"""
agreement_obj = self.env['framework.agreement']
date = self._get_date()
agreement, __ = agreement_obj.get_cheapest_agreement_for_qty(
self.proposed_product_id.id, date, qty=self.proposed_qty,
currency=self.currency_id)
if not agreement:
agreement, __ = agreement_obj.get_cheapest_agreement_for_qty(
self.proposed_product_id.id, date, qty=None,
currency=self.currency_id)
return agreement

@api.multi
def _onchange_base_agreement_method(self):
""" When agreement method basics change,
we want to find a proper agreement,
update price and supplier based on this agreement.
"""
agreement = self._get_best_agreement()
if not agreement:
self.framework_agreement_id = False
return
price = agreement.get_price(self.proposed_qty,
currency=self.currency_id)
self.framework_agreement_id = agreement.id
self.unit_cost = price
self.total_cost = price * self.proposed_qty
self.supplier_id = agreement.supplier_id.id
return self._check_enought_qty(agreement)

@api.onchange('procurement_method')
def onchange_sourcing_method(self):
"""
Called when source method is set on a source line.
Expand All @@ -289,71 +343,34 @@ def onchange_sourcing_method(self, cr, uid, ids, method, req_line_id,
and raise quantity warning.
"""
line_source = self.browse(cr, uid, ids, context=context)
res = {'value': {'framework_agreement_id': False}}
if (method != 'fw_agreement' or not proposed_product_id):
return res
currency = line_source.currency_id
agreement_obj = self.pool['framework.agreement']
date = self._get_date(cr, uid, req_line_id, context=context)
agreement, enough_qty = agreement_obj.get_cheapest_agreement_for_qty(
cr, uid, proposed_product_id, date, proposed_qty,
currency=currency, context=context)
if not agreement:
return res
price = agreement.get_price(proposed_qty, currency=currency)
res['value'] = {'framework_agreement_id': agreement.id,
'unit_cost': price,
'total_cost': price * proposed_qty,
'supplier_id': agreement.supplier_id.id}
if not enough_qty:
msg = _("You have ask for a quantity of %s \n"
" but there is only %s available"
" for current agreement") % (proposed_qty,
agreement.available_quantity)
res['warning'] = msg
return res

@id_boilerplate
def onchange_quantity(self, cr, uid, ids, method, req_line_id, qty,
proposed_product_id, context=None):
if (self.procurement_method != 'fw_agreement'
or not self.proposed_product_id):
self.framework_agreement_id = False
return
return self._onchange_base_agreement_method()

@api.onchange('proposed_qty')
def onchange_quantity(self):
"""Raise a warning if agreed qty is not sufficient"""
line_source = self.browse(cr, uid, ids, context=context)
if (method != 'fw_agreement' or not proposed_product_id):
return {}
currency = line_source.currency_id
date = self._get_date(cr, uid, req_line_id, context=context)
return self.onchange_quantity_obs(cr, uid, ids, qty, date,
proposed_product_id,
currency=currency,
price_field='dummy',
context=context)

@id_boilerplate
def onchange_product_id(self, cr, uid, ids, method, req_line_id,
proposed_product_id, proposed_qty,
context=None):
if (self.procurement_method != 'fw_agreement'
or not self.proposed_product_id):
return
agreement = self._get_best_agreement()
self.framework_agreement_id = agreement.id
if agreement:
return self._check_enought_qty(agreement)

@api.onchange('proposed_product_id')
def onchange_product_id(self):
"""Call when product is set on a source line.
If sourcing method is framework agreement
it will set price, agreement and supplier if possible
and raise quantity warning.
"""
if method != 'fw_agreement':
if proposed_product_id:
value = {'proposed_uom_id': ''}
if proposed_product_id:
prod_obj = self.pool.get('product.product')
prod = prod_obj.browse(
cr, uid, proposed_product_id, context=context)
value = {
'proposed_uom_id': prod.uom_id.id,
}
return {'value': value}
return {}

return self.onchange_sourcing_method(cr, uid, ids, method, req_line_id,
proposed_product_id,
proposed_qty=proposed_qty,
context=context)
if self.procurement_method != 'fw_agreement':
if self.proposed_product_id:
self.proposed_uom_id = self.proposed_product_id.uom_id or False
return
return self._onchange_base_agreement_method()
24 changes: 0 additions & 24 deletions framework_agreement_sourcing/view/requisition_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,5 @@
</field>
</record>

<record id="addd_agreement_source_line_onchange" model="ir.ui.view">
<field name="name">ad agreement source line onchange</field>
<field name="model">logistic.requisition.source</field>
<field name="inherit_id" ref="logistic_requisition.view_logistic_requisition_source_form"/>
<field name="arch" type="xml">
<data>
<field name="procurement_method"
position="attributes">
<attribute name="on_change">onchange_sourcing_method(procurement_method, requisition_line_id, proposed_product_id, proposed_qty)</attribute>
</field>

<field name="proposed_qty"
position="attributes">
<attribute name="on_change">onchange_quantity(procurement_method, requisition_line_id, proposed_qty, proposed_product_id)</attribute>
</field>
<field name="proposed_product_id"
position="attributes">
<attribute name="on_change">onchange_product_id(procurement_method, requisition_line_id, proposed_product_id, proposed_qty)</attribute>
</field>

</data>
</field>
</record>

</data>
</openerp>

0 comments on commit 9fd6a2a

Please sign in to comment.