Hi Niyas Raphy, thanks for the reply!
This is a custom module, and here's the section I suspect the error might have occurred:
@api.multi
def act_production(self):
project_id = self.env.user.project_id
for a in self:
boms = {}
buildings = {}
for al in a.atlas_line_ids:
al.assembly_section = parse_str(al.assembly_section)
al.bom_no = parse_str(al.bom_no)
# if it doesn't have bom_no, then the data format is incorrect
if not al.bom_no or len(al.bom_no.strip()) == 0:
continue
# sub_bom create.
if al.assembly_categ.code in ['frame']: # not include 'others', 'regardless' category
group_key = "%s/%s" % (al.bom_no, al.assembly_categ.code)
sub_bom = self.env['mrp.bom'].sudo().search(
[('project_id', '=', project_id.id),
('product_id.default_code', '=', group_key),
('atlas_id', '=', al.atlas_id.edit_atlas_id.id)], limit=1)
# Create BOM when BOM does not exist
if not sub_bom:
bom_product = self.env['product.product'].sudo().search(
[('name', '=', group_key),
('default_code', '=', group_key),
('project_id', '=', project_id.id),
('categ_id', '=', al.categ_id.id)], limit=1)
if not bom_product:
bom_product = self.env['product.product'].sudo().create({
'project_id': project_id.id,
'categ_id': al.categ_id.id,
'name': group_key,
'default_code': group_key,
'product_id': bom_product.id,
'type': 'product',
# 'material': p.material_no
})
sub_bom = self.env['mrp.bom'].sudo().create ({
'project_id': project_id.id,
'product_tmpl_id': bom_product.product_tmpl_id.id,
'product_id': bom_product.id,
'atlas_id': al.atlas_id.edit_atlas_id.id,
# 'product_qty': p.qty
})
if al.categ_id.code == 'aluminum' or 'raw':
product = self.env['product.product'].sudo ().search (
[('project_id', '=', project_id.id),
('categ_id', '=', al.categ_id.id),
('default_code', '=', al.name),
('part_no', '=', al.code),
], limit=1)
else:
product = self.env['product.product'].sudo ().search (
[('project_id', '=', project_id.id),
('categ_id', '=', al.categ_id.id),
('default_code', '=', al.name),
], limit=1)
# if it already has bom_line, find it.
bom_line = sub_bom.bom_line_ids.filtered(lambda l: l.product_id.default_code == al.name)
# if it doesn't have bom_line, create it.
if not bom_line:
bom_line = self.env['mrp.bom.line'].sudo().create({
'product_id': product.id,
'product_qty': parse_float(al.unit_qty),
'bom_id': sub_bom.id
})
# then assign unit qty to bom_line.
bom_line.product_qty = parse_float(al.unit_qty)
for al in a.atlas_line_ids:
if not al.bom_no or len(al.bom_no.strip()) == 0:
continue
elif (not al.name or len(al.name.strip()) == 0) and (not al.unit_qty or len(al.unit_qty.strip()) == 0) \
and al.bom_no and (not al.assembly_categ.code) and (not al.building or len(al.building)):
# Search BOM
bom = self.env['mrp.bom'].sudo().search(
[('project_id', '=', project_id.id),
('product_id.default_code', '=', al.bom_no),
('atlas_id', '=', a.edit_atlas_id.id)], limit=1)
# Create BOM when BOM does not exist
if not bom:
bom_product = self.env['product.product'].sudo().search(
[('name', '=', al.bom_no),
('project_id', '=', project_id.id),
('categ_id', '=', al.categ_id.id)], limit=1)
if not bom_product:
bom_product = self.env['product.product'].sudo().create({
'project_id': project_id.id,
'categ_id': al.categ_id.id,
'name': al.bom_no,
'default_code': al.bom_no,
'type': 'product',
# 'material': p.material_no
})
bom = self.env['mrp.bom'].sudo().create({
'project_id': project_id.id,
'product_tmpl_id': bom_product.product_tmpl_id.id,
'product_id': bom_product.id,
'atlas_id': a.edit_atlas_id.id,
# 'product_qty': p.qty
})
boms['%s:%s' % (al.building, bom.id)] = {"bom": bom, "qty": parse_float (al.qty), "building": al.building}
# Add bom search in atlas_id.
for sub_categ in ['frame']:
sub_bom_name = '%s/%s' % (bom.product_id.name, sub_categ)
sub_bom_exist = bom.bom_line_ids.filtered(lambda x: x.product_id.default_code == sub_bom_name)
# if not sub_bom_exist:
sub_bom = self.env['mrp.bom'].sudo().search(
[('project_id', '=', project_id.id),
('product_id.default_code', '=', sub_bom_name),
('atlas_id', '=', al.atlas_id.edit_atlas_id.id)], limit=1)
if sub_bom and not sub_bom_exist:
bom.write({'bom_line_ids': [(0, 0, {'product_id': sub_bom.product_id.id})],})
boms['%s:%s' % (al.building, sub_bom.id)] = {"bom": sub_bom, "qty": parse_float(al.qty), "building": al.building}
# a.production_ids = [(6, 0, production_ids)]
elif al.assembly_categ.code in ['surface', 'others']:
if al.categ_id.code == 'aluminum' or 'raw':
product = self.env['product.product'].sudo ().search (
[('project_id', '=', project_id.id),
('categ_id', '=', al.categ_id.id),
('default_code', '=', al.name),
('part_no', '=', al.code),
], limit=1)
else:
product = self.env['product.product'].sudo().search (
[('project_id', '=', project_id.id),
('categ_id', '=', al.categ_id.id),
('default_code', '=', al.name),
], limit=1)
# if it already has bom_line, find it.
bom_line = bom.bom_line_ids.filtered(lambda l: l.product_id.default_code == al.name)
# if it doesn't have bom_line, create it.
if not bom_line:
bom_line = self.env['mrp.bom.line'].sudo().create ({
'product_id': product.id,
'product_qty': parse_float (al.unit_qty),
'bom_id': bom.id
})
# then assign unit qty to bom_line.
bom_line.product_qty = parse_float(al.unit_qty)
production_ids = []
# for al in a.atlas_line_ids:
categs = self.env['product.category'].sudo ().search ([('code', 'in', ['assembly', 'assembly_preparation'])])
panels = self.env['tokiku.panel'].sudo ().search (
[('project_id', '=', self.env.user.project_id.id),
('categ_id', 'in', [c.id for c in categs])])
for v in boms.itervalues():
# if i in buildings:
# for k in buildings.keys():
# if boms[i][0].id == buildings[k][0]:
production_id = self.env['mrp.production'].sudo().search(
[('contract_id', '=', a.contract_id.id),
('bom_id', '=', v['bom'].id),
('atlas_id', '=', a.edit_atlas_id.id),
('building', '=', v['building'])])
location_src_id = self.env['stock.location'].with_context(lang=None).search(
[('usage', '=', 'internal'),
('name', '=', 'Assembly')], limit=1)
location_dest_id = self.env['stock.location'].with_context (lang=None).search (
[('usage', '=', 'internal'),
('name', '=', 'Pending'),
('location_id', '=', location_src_id.id)], limit=1)
if not production_id:
prod_obj = {
'contract_id': al.atlas_id.contract_id.id,
'bom_id': v['bom'].id,
'product_id': v['bom'].product_id.id,
'product_uom_id': v['bom'].product_id.uom_id.id,
'product_qty': v['qty'],
'atlas_id': a.edit_atlas_id.id,
'location_src_id': location_src_id.id,
'building': v['building'],
'partner_id': a.partner_id.id
# 'panel_ids': panel.id,
}
default_code = v['bom'].product_id.default_code
if default_code and ('frame' in default_code):
prod_obj.update({'location_dest_id': location_dest_id.id})
production_id = self.env['mrp.production'].sudo().create(prod_obj)
for r in production_id:
production_ids.append(r.id)
a.production_ids = [(6, 0, production_ids)]
for p in panels:
p.assembly_line_ids = [(6, 0, production_ids)]
Hi Hoang Tran, thanks for the reply, though I'm not exactly sure how to do that.