The current creation of bg_id is wrong because children with eigenbedarf_gedeckt=True will always have a different bg_id than their parents, even if the parents have eigenbedarf_gedeckt=True as well.
Proposed solution
Parents should have the same bg_id as their children if all of them have eigenbedarf_gedeckt=True.