تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
1 الرد
5995 أدوات العرض


I have this function it filters in all selected MO's raw material lines, and then creates a report that is displayed in tree view.

But I have one problem, there can be allot lines with the same product. So my goal is to group all these lines and display them then just in one line with summed quantity.

Can someone help me with this?

class RawMaterialReport(models.Model):
    _name = 'raw.material.report'
    _description = 'Raw Material Report'

    product_id = fields.Many2one('product.product', string='Product', required=False)
    code = fields.Char(string='Code', required=True, readonly=True)
    total_qty = fields.Float(string='Total Qty', digits=(6, 2), readonly=True)
    virtual_qty = fields.Float(string='Forcasted Qty', digits=(6, 2), readonly=True)
    origin = fields.Char(string='Origin', required=True, readonly=True)
    production_id = fields.Many2one('mrp.production')


@api.multi
def open_raw_materials(self):
    self.search([]).unlink()
    mrp_productions = self._context.get('active_ids')
    mrp_production = self.env['mrp.production'].browse(mrp_productions)
    products_without_default_code = mrp_production.mapped('move_raw_ids').filtered(
        lambda x: not x.product_id.default_code
    )

    raws = mrp_production.mapped('move_raw_ids').sorted(
        key=lambda r: r.product_id.default_code
    )

    for r in raws:
        vals = {
            'product_id': r.product_id.id,
            'code': r.product_id.default_code,
            'total_qty': r.product_id.qty_available,
            'virtual_qty': r.product_id.virtual_available,
            'origin': r.reference,
            'production_id': r.raw_material_production_id.id,
        }
        self.create(vals)


الصورة الرمزية
إهمال
أفضل إجابة

Hello,

You can try tis


class RawMaterialReport(models.Model):
_name = 'raw.material.report'
_description = 'Raw Material Report'

product_id = fields.Many2one('product.product', string='Product', required=False)
code = fields.Char(string='Code', required=True, readonly=True)
total_qty = fields.Float(string='Total Qty', digits=(6, 2), readonly=True)
virtual_qty = fields.Float(string='Forcasted Qty', digits=(6, 2), readonly=True)
origin = fields.Char(string='Origin', required=True, readonly=True)
production_id = fields.Many2one('mrp.production')


@api.multi
def open_raw_materials(self):
data = {}
self.search([]).unlink()
mrp_productions = self._context.get('active_ids')
mrp_production = self.env['mrp.production'].browse(mrp_productions)
products_without_default_code = mrp_production.mapped('move_raw_ids').filtered(
lambda x: not x.product_id.default_code
)

raws = mrp_production.mapped('move_raw_ids').sorted(
key=lambda r: r.product_id.default_code
)

for r in raws:
key = (r.product_id.id, r.raw_material_production_id.id)
if key not in data:
data[key] = {
'product_id': r.product_id.id,
'code': r.product_id.default_code,
'total_qty': r.product_id.qty_available,
'virtual_qty': r.product_id.virtual_available,
'origin': r.reference,
'production_id': r.raw_material_production_id.id,
}
else:
data[key].update({
'total_qty' : data[key].get('total_qty') + r.product_id.qty_available,
})
for vals in data:
self.create(data[vals])
الصورة الرمزية
إهمال
المنشورات ذات الصلة الردود أدوات العرض النشاط
0
فبراير 22
4893
0
أبريل 24
1938
1
مارس 22
2318
0
ديسمبر 19
4631
5
ديسمبر 19
3735