Limit minimum quantity when creating manual purchase orders.

This commit is contained in:
Albert Cervera i Areny 2014-06-26 09:16:17 +02:00
parent 921dba255f
commit 02c7686f26
5 changed files with 104 additions and 12 deletions

6
README
View File

@ -1,6 +1,12 @@
This Module runs with the Tryton application platform.
This module is developed and tested over a Tryton server and core modules
patched with patches from `trytond-patches project`_ (take care with the
branches). Maybe these patches are required for the module to work.
.. _trytond-patches project: https://bitbucket.org/nantic/trytond-patches
Installing
----------

View File

@ -8,6 +8,7 @@ def register():
Pool.register(
ProductSupplier,
PurchaseRequest,
PurchaseLine,
module='stock_supply_minimum', type_='model')
Pool.register(
CreatePurchase,

View File

@ -1,7 +1,8 @@
Stock Supply Minimum Module
###########################
Stock Supply Minimum
####################
The Stock Supply Minimum module adds Minimum Quantity on the Product
Supplier form. This allows to define the minimum purchase quantity for the given
product and supplier so purchases created for this product and supplier from
purchase requests will honour this value.
The Stock Supply Minimum module adds Minimum Quantity on the Product's
Supplier field.
It allows to define the minimum purchase quantity for the given product and
supplier, so purchases created for this product and supplier (manually or from
purchase requests) will respect this condition.

View File

@ -1,9 +1,11 @@
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
# The COPYRIGHT file at the top level of this repository contains the full
# copyright notices and license terms.
from trytond.model import fields
from trytond.pool import PoolMeta
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Bool, Eval
__all__ = ['ProductSupplier', 'PurchaseRequest', 'CreatePurchase']
__all__ = ['ProductSupplier', 'PurchaseRequest', 'CreatePurchase',
'PurchaseLine']
__metaclass__ = PoolMeta
@ -17,13 +19,15 @@ class PurchaseRequest:
minimum_quantity = fields.Function(fields.Float('Minimum Quantity'),
'on_change_with_minimum_quantity')
@fields.depends('supplier', 'product')
@fields.depends('supplier', 'product', 'uom')
def on_change_with_minimum_quantity(self, name):
Uom = Pool().get('product.uom')
if not self.product:
return
for product_supplier in self.product.product_suppliers:
if product_supplier.party == self.party:
return product_supplier.minimum_quantity
return Uom.compute_qty(self.product.purchase_uom,
product_supplier.minimum_quantity, self.uom)
class CreatePurchase:
@ -34,3 +38,62 @@ class CreatePurchase:
line = super(CreatePurchase, cls).compute_purchase_line(request)
line.quantity = max(line.quantity, request.minimum_quantity)
return line
class PurchaseLine:
__name__ = 'purchase.line'
minimum_quantity = fields.Float('Minimum Quantity', readonly=True, states={
'invisible': ~Bool(Eval('minimum_quantity')),
},
help='The quantity must be greater or equal than minimum quantity')
@fields.depends('minimum_quantity')
def on_change_product(self):
minimum_quantity = self._get_minimum_quantity()
if minimum_quantity and (not self.quantity or
self.quantity < minimum_quantity):
self.quantity = minimum_quantity
else:
minimum_quantity = None
res = super(PurchaseLine, self).on_change_product()
if minimum_quantity:
res['minimum_quantity'] = minimum_quantity
res['quantity'] = minimum_quantity
else:
res['minimum_quantity'] = None
return res
@fields.depends('minimum_quantity')
def on_change_quantity(self):
minimum_quantity = self._get_minimum_quantity()
if (self.quantity and minimum_quantity and
self.quantity < minimum_quantity):
self.quantity = minimum_quantity
else:
minimum_quantity = None
res = super(PurchaseLine, self).on_change_quantity()
if minimum_quantity:
res['minimum_quantity'] = minimum_quantity
res['quantity'] = minimum_quantity
else:
res['minimum_quantity'] = None
return res
@fields.depends('product', '_parent_purchase.party', 'quantity', 'unit',
'minimum_quantity')
def _get_minimum_quantity(self):
Uom = Pool().get('product.uom')
if not self.product or not self.purchase.party:
return
for product_supplier in self.product.product_suppliers:
if product_supplier.party == self.purchase.party:
minimum_quantity = product_supplier.minimum_quantity
uom_category = self.product.purchase_uom.category
if (minimum_quantity and self.unit and
self.unit in uom_category.uoms):
return Uom.compute_qty(self.product.purchase_uom,
minimum_quantity, self.unit)
return minimum_quantity

View File

@ -9,15 +9,36 @@ this repository contains the full copyright notices and license terms. -->
<field name="name">product_supplier_form</field>
</record>
<!-- purchase.request -->
<record model="ir.ui.view" id="purchase_request_view_form">
<field name="model">purchase.request</field>
<field name="inherit" ref="stock_supply.purchase_request_view_form"/>
<field name="name">purchase_request_form</field>
</record>
<record model="ir.ui.view" id="purchase_request_view_tree">
<field name="model">purchase.request</field>
<field name="inherit" ref="stock_supply.purchase_request_view_tree"/>
<field name="name">purchase_request_tree</field>
</record>
<!-- purchase.line -->
<record model="ir.ui.view" id="purchase_line_view_form">
<field name="model">purchase.line</field>
<field name="inherit" ref="purchase.purchase_line_view_form"/>
<field name="name">purchase_line_form</field>
</record>
<record model="ir.ui.view" id="purchase_line_view_tree">
<field name="model">purchase_line</field>
<field name="inherit" ref="purchase.purchase_line_view_tree"/>
<field name="name">purchase_line_tree</field>
</record>
<record model="ir.ui.view" id="purchase_line_view_tree_sequence">
<field name="model">purchase_line</field>
<field name="inherit" ref="purchase.purchase_line_view_tree_sequence"/>
<field name="name">purchase_line_tree_sequence</field>
</record>
</data>
</tryton>