vendredi 1 juillet 2016

Querying an object in OpenErp 7


I have two objects,'article' and 'mvt', the logic behind the module am trying to create is that an article is defined by a name and a price, but the quantity is calculated through a set of transactions or 'mouvements', each mouvement has a realtion of many2one with an article, a date and a qte, the last field can be either positive or negative (positive means we added new articles, negative the opposite), so here's the code i used to do this, i am kind of new to openErp.

class article(osv.osv):

_name = 'cm.article'

def _get_qte(self, cr, uid, ids, fld_name, arg, context=None):
    result = {}
    mvtObject=self.pool.get('cm.mvt')
    mvtids=mvtObject.search(cr,uid,[])
    sum = 0
    for id in mvtids:
        mvt_line=mvtObject.browse(cr,uid,id,context)
        if mvt_line.article.id == ids[0]:
            sum = sum + mvt_line.qte
    result[sum] = sum
    return result
_columns = {
        'name': fields.char(size=32, string='Nom', required=True),
        'pu':fields.float(required="True",string='Prix Unitaire'),
        'qte': fields.function(_get_qte,type='integer',obj="cm.article",method=True,string='Quantity'),
}
article()


class mvt(osv.osv):
    _name = 'cm.mvt'
    _columns = {
        'article' : fields.many2one('cm.article', 'name'),
        'date' : fields.datetime(string="Date Mouvement"),
        'qte':fields.integer(String="Quantity")
    }
    _defaults = {'date' : fields.date.context_today}
mvt()

Thank you in advance.


Aucun commentaire:

Enregistrer un commentaire