From 1b79fdc1913597b2b2def55f8dcea3d62c368079 Mon Sep 17 00:00:00 2001 From: "watcha.h" Date: Fri, 12 Feb 2016 11:33:30 +0700 Subject: [PATCH] prevent duplicate contact name --- netforce_clinic/migrations/__init__.py | 1 + .../migrations/remove_dbl_contact.py | 53 +++++++++++++++++++ netforce_clinic/models/partner.py | 6 +++ 3 files changed, 60 insertions(+) create mode 100644 netforce_clinic/migrations/remove_dbl_contact.py diff --git a/netforce_clinic/migrations/__init__.py b/netforce_clinic/migrations/__init__.py index 3619ae7..a5ef4b2 100644 --- a/netforce_clinic/migrations/__init__.py +++ b/netforce_clinic/migrations/__init__.py @@ -7,3 +7,4 @@ #from . import restore_picking #from . import rename_dbl_hdcase_number #from . import reset_hdcase_number +from . import remove_dbl_contact diff --git a/netforce_clinic/migrations/remove_dbl_contact.py b/netforce_clinic/migrations/remove_dbl_contact.py new file mode 100644 index 0000000..774851b --- /dev/null +++ b/netforce_clinic/migrations/remove_dbl_contact.py @@ -0,0 +1,53 @@ +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() diff --git a/netforce_clinic/models/partner.py b/netforce_clinic/models/partner.py index 5c7b9f8..4d8cf32 100644 --- a/netforce_clinic/models/partner.py +++ b/netforce_clinic/models/partner.py @@ -2,6 +2,8 @@ from netforce.model import Model, fields class Partner(Model): _inherit="partner" + #_key=["code","name"] + _key=["name"] _fields={ 'walkin_cust': fields.Boolean("Walkin Customer"), @@ -9,6 +11,10 @@ class Partner(Model): 'is_staff': fields.Boolean("Is Staff"), } + _sql_constraints=[ + ("contact_uniq","unique (name)","The name of contact must be unique!"), + ] + def name_get(self,ids,context={}): vals=[] for obj in self.browse(ids):