'Remove any duplicate memberships and update affiliations'

@staticmethod
def org_branch_onaccept(form):

    _id = form.vars.id
    db = current.db
    s3db = current.s3db
    inherit = ['region_id', 'country']
    otable = s3db.org_organisation
    ltable = db.org_organisation_branch
    btable = db.org_organisation.with_alias('org_branch_organisation')
    ifields = ([otable[fn] for fn in inherit] + [btable[fn] for fn in inherit])
    left = [otable.on((ltable.organisation_id == otable.id)), btable.on((ltable.branch_id == btable.id))]
    record = db((ltable.id == _id)).select(otable.root_organisation, btable.root_organisation, ltable.branch_id, ltable.organisation_id, ltable.deleted, ltable.deleted_fk, left=left, limitby=(0, 1), *ifields).first()
    if record:
        organisation = record.org_organisation
        branch = record.org_branch_organisation
        link = record.org_organisation_branch
        branch_id = link.branch_id
        organisation_id = link.organisation_id
        if (branch_id and organisation_id and (not link.deleted)):
            query = ((((ltable.branch_id == branch_id) & (ltable.organisation_id == organisation_id)) & (ltable.id != _id)) & (ltable.deleted != True))
            deleted_fk = {'branch_id': branch_id, 'organisation_id': organisation_id}
            db(query).update(deleted=True, branch_id=None, organisation_id=None, deleted_fk=json.dumps(deleted_fk))
            update = dict(((field, organisation[field]) for field in inherit if ((not branch[field]) and organisation[field])))
            if update:
                db((otable.id == branch_id)).update(**update)
            record_ids = (organisation_id, branch_id)
            ltable = db.org_organisation_organisation_type
            rows = db(ltable.organisation_id.belongs(record_ids)).select(ltable.organisation_id, ltable.organisation_type_id)
            org_types = set()
            branch_types = set()
            for row in rows:
                if (row.organisation_id == organisation_id):
                    org_types.add(row.organisation_type_id)
                else:
                    branch_types.add(row.organisation_type_id)
            for t in (org_types - branch_types):
                link_id = ltable.insert(organisation_id=branch_id, organisation_type_id=t)
                form = Storage(vars=Storage(id=link_id))
                S3OrganisationModel.org_organisation_organisation_type_onaccept(link_id)
            ltable = s3db.org_sector_organisation
            rows = db(ltable.organisation_id.belongs(record_ids)).select(ltable.organisation_id, ltable.sector_id)
            org_sectors = set()
            branch_sectors = set()
            for row in rows:
                if (row.organisation_id == organisation_id):
                    org_sectors.add(row.sector_id)
                else:
                    branch_sectors.add(row.sector_id)
            for s in (org_sectors - branch_sectors):
                ltable.insert(organisation_id=branch_id, sector_id=s)
        org_update_affiliations('org_organisation_branch', link)
        if (link.deleted or (branch.root_organisation is None) or (branch.root_organisation != organisation.root_organisation)):
            org_update_root_organisation(branch_id)
