from netforce.model import get_model from netforce import migration from netforce.access import set_active_user, set_active_company class Migration(migration.Migration): _name="clinic.remove.dbl.contact" _version="2.12.3" def migrate(self): set_active_company(1) set_active_user(1) contacts={} context={ 'active_test': False, #find archive also } for partner in get_model("partner").search_browse([],context=context): contacts.setdefault(partner.name,{'name': partner.name,'lines': []}) contacts[partner.name]['lines'].append(partner.id) cnames={} for contact_name, vals in contacts.items(): lines=vals['lines'] if len(lines) > 1: for contact_id in lines: contact=get_model('partner').browse(contact_id) if contact.is_patient: res=get_model("clinic.patient").search([['partner_id','=',contact_id]]) if not res: try: print(contact_name,' not use, will delete') get_model('partner').delete([contact_id]) except Exception as e: print("Can not delete contact_id ", contact_id) print("ERROR ", e) else: res=get_model("account.invoice").search_read([['partner_id','=',contact_id]],['number']) if not res: print(contact_name,' not use, will delete') get_model('partner').delete([contact_id]) else: if contact_name not in cnames.keys(): cnames[contact_name]=contact_id else: for r in res: inv=get_model('account.invoice').browse(r['id']) inv.write({ 'partner_id': cnames[contact_name], }) get_model('partner').delete([contact_id]) print("del ", contact_name) print("#"*80) Migration.register()