def ensure_plans(dry_run, verbose, for_tests, apps):
    SoftwarePlan = apps.get_model('accounting', 'SoftwarePlan')
    SoftwarePlanVersion = apps.get_model('accounting', 'SoftwarePlanVersion')
    Role = apps.get_model('django_prbac', 'Role')

    edition_to_features = _ensure_features(dry_run=dry_run, verbose=verbose, apps=apps)
    advanced_role = Role.objects.get(slug='advanced_plan_v0')

    for product_type in PRODUCT_TYPES:
        for edition in EDITIONS:
            software_plan_version = SoftwarePlanVersion(role=advanced_role)

            product, product_rates = _ensure_product_and_rate(
                product_type, edition, dry_run=dry_run, verbose=verbose, apps=apps
            )
            feature_rates = _ensure_feature_rates(
                edition_to_features[edition], edition, dry_run=dry_run,
                verbose=verbose, for_tests=for_tests, apps=apps
            )

            software_plan = SoftwarePlan(
                name='%s Edition' % product.name, edition=edition,
                visibility=SoftwarePlanVisibility.INTERNAL
            )
            if dry_run:
                logger.info("[DRY RUN] Creating Software Plan: %s" % software_plan.name)
            else:
                try:
                    software_plan = SoftwarePlan.objects.get(name=software_plan.name)
                    if verbose:
                        logger.info("Plan '%s' already exists. Using existing plan to add version."
                                    % software_plan.name)
                except SoftwarePlan.DoesNotExist:
                    software_plan.save()
                    if verbose:
                        logger.info("Creating Software Plan: %s" % software_plan.name)

                    software_plan_version.plan = software_plan

                    # must save before assigning many-to-many relationship
                    if hasattr(SoftwarePlanVersion, 'product_rates'):
                        software_plan_version.save()

                    for product_rate in product_rates:
                        product_rate.save()
                        if hasattr(SoftwarePlanVersion, 'product_rates'):
                            software_plan_version.product_rates.add(product_rate)
                        elif hasattr(SoftwarePlanVersion, 'product_rate'):
                            assert len(product_rates) == 1
                            software_plan_version.product_rate = product_rate
                        else:
                            raise AccountingError(
                                'SoftwarePlanVersion does not have product_rate '
                                'or product_rates field'
                            )

                    # must save before assigning many-to-many relationship
                    if hasattr(SoftwarePlanVersion, 'product_rate'):
                        software_plan_version.save()

                    for feature_rate in feature_rates:
                        feature_rate.save()
                        software_plan_version.feature_rates.add(feature_rate)
                    software_plan_version.save()
dataset/ETHPy150Open dimagi/commcare-hq/corehq/apps/accounting/management/commands/cchq_accounting_bootstrap_new_editions_042016.py/ensure_plans/original