[IMP] product: new widget for UOMFactor

bzr revid: hmo@tinyerp.com-20121012102708-weeno36hl33ysd9a
This commit is contained in:
Harry (OpenERP) 2012-10-12 15:57:08 +05:30
parent bfba3deb93
commit bfff63118c
5 changed files with 72 additions and 31 deletions

View File

@ -64,6 +64,13 @@ Print product labels with barcode.
'test/product_uom.yml',
'test/product_pricelist.yml',
],
'js': [
'static/src/js/product.js',
],
'qweb' : [
"static/src/xml/product.xml",
],
'installable': True,
'auto_install': False,
'images': ['images/product_uom.jpeg','images/product_pricelists.jpeg','images/products_categories.jpeg', 'images/products_form.jpeg'],

View File

@ -107,16 +107,6 @@ class product_uom(osv.osv):
del(data['factor_inv'])
return super(product_uom, self).create(cr, uid, data, context)
def _reference_uom(self, cr, uid, ids, field, arg, context=None):
res = {}
for uom in self.browse(cr, uid, ids, context):
uom_ids = []
if uom.category_id and uom.category_id.id:
uom_ids = self.search(cr, uid, [('category_id', '=', uom.category_id.id), ('uom_type', '=', 'reference')], context=context)
if uom_ids:
res[uom.id] = uom_ids[0]
return res
_order = "name"
_columns = {
'name': fields.char('Unit of Measure', size=64, required=True, translate=True),
@ -137,7 +127,6 @@ class product_uom(osv.osv):
'uom_type': fields.selection([('bigger','Bigger than the reference Unit of Measure'),
('reference','Reference Unit of Measure for this category'),
('smaller','Smaller than the reference Unit of Measure')],'Type', required=1),
'reference_uom_id': fields.function(_reference_uom, type='many2one', relation="product.uom"),
}
_defaults = {
@ -150,14 +139,6 @@ class product_uom(osv.osv):
('factor_gt_zero', 'CHECK (factor!=0)', 'The conversion ratio for a unit of measure cannot be 0!')
]
def onchange_category_id(self, cr, uid, ids, category_id):
reference_uom = False
if category_id:
uom_ids = self.search(cr, uid, [('category_id', '=',category_id),('uom_type', '=', 'reference')])
if uom_ids:
reference_uom = uom_ids[0]
return {'value':{'reference_uom_id': reference_uom}}
def _compute_qty(self, cr, uid, from_uom_id, qty, to_uom_id=False):
if not from_uom_id or not qty or not to_uom_id:
return qty

View File

@ -424,22 +424,13 @@
<group>
<group>
<field name="name"/>
<field name="category_id" on_change="onchange_category_id(category_id)"/>
<field name="category_id"/>
<field name="active"/>
</group>
<group>
<field name="uom_type" on_change="onchange_type(uom_type)"/>
<label string="Computation" for="factor" attrs="{'invisible':[('uom_type','=','reference')]}" style="margin: 9px 0 9px 0;"/>
<group col="5" attrs="{'invisible':[('uom_type','=','reference')]}">
<div style="margin-left: -12px;">
<field name="factor_inv" class="oe_inline" attrs="{'invisible':[('uom_type','!=','bigger')]}" String="Computation" nolabel="1"/>
<label string="1" attrs="{'invisible':[('uom_type','!=','smaller')]}"/>
<field class="oe_inline" name="reference_uom_id" attrs="{'invisible':[('uom_type','=','reference')]}" nolabel="1"/>
<label string="="/>
<field class="oe_inline" name="factor" attrs="{'invisible':[('uom_type','!=','smaller')]}" String="Computation" nolabel="1"/>
<label string="1" attrs="{'invisible':[('uom_type','!=','bigger')]}"/>
</div>
</group>
<field name="factor" attrs="{'invisible':[('uom_type','!=','smaller')]}" widget="uom_factor" options="{'category_field': 'category_id', 'name_field': 'name'}"/>
- <field name="factor_inv" attrs="{'invisible':[('uom_type','!=','bigger')]}" widget="uom_factor" options="{'category_field': 'category_id', 'name_field': 'name'}"/>
<field name="rounding"/>
</group>
</group>

View File

@ -0,0 +1,46 @@
openerp.product = function (instance) {
instance.web.form.widgets.add('uom_factor', 'instance.web.form.FieldUOMFactor');
instance.web.form.FieldUOMFactor = instance.web.form.FieldFloat.extend({
template: "FieldUOMFactor",
init: function() {
this._super.apply(this, arguments);
this.set({"category": false, 'name': false});
if (this.options.category_field) {
this.field_manager.on("field_changed:" + this.options.category_field, this, function() {
this.set({"category": this.field_manager.get_field_value(this.options.category_field)});
});
}
if (this.options.name_field) {
this.field_manager.on("field_changed:" + this.options.name_field, this, function() {
this.set({"name": this.field_manager.get_field_value(this.options.name_field)});
});
}
this.on("change:category", this, this.get_uom_reference);
this.get_uom_reference();
this.ci_dm = new instance.web.DropMisordered();
},
start: function() {
var tmp = this._super();
this.on("change:uom_reference", this, this.reinitialize);
this.on("change:name", this, this.reinitialize);
return tmp;
},
get_uom_reference: function() {
var self = this;
if (this.get("category") === false) {
this.set({"uom_reference": null});
return;
}
return this.ci_dm.add(new instance.web.Model("product.uom").query(["name"])
.filter([["category_id", "=", self.get("category")],["uom_type", "=", "reference"]]).first()).pipe(function(res) {
self.set({"uom_reference": res});
});
},
parse_value: function(val, def) {
return instance.web.parse_value(val, {type: "float"}, def);
},
format_value: function(val, def) {
return instance.web.format_value(val, {type: "float"}, def);
},
});
}

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates id="template" xml:space="preserve">
<t t-name="FieldUOMFactor" t-extend="FieldChar">
<t t-jquery="t:first" t-operation="before">
<t t-if="widget.get('uom_reference')">
1 <t t-esc="widget.get('uom_reference').name"/> =
</t>
</t>
<t t-jquery="t:last" t-operation="after">
<t t-esc="widget.get('name')"/>
</t>
</t>
</templates>