'check if last maintenance visit against same sales order/ Warranty Claim'

def check_if_last_visit(self):

    check_for_docname = None
    for d in self.get(u'purposes'):
        if d.prevdoc_docname:
            check_for_docname = d.prevdoc_docname
    if check_for_docname:
        check = frappe.db.sql(u'select    t1.name    from    `tabMaintenance    Visit`    t1,    `tabMaintenance    Visit    Purpose`    t2    where    t2.parent    =    t1.name    and    t1.name!=%s    and    t2.prevdoc_docname=%s    and    t1.docstatus    =    1    and    (t1.mntc_date    >    %s    or    (t1.mntc_date    =    %s    and    t1.mntc_time    >    %s))', (self.name, check_for_docname, self.mntc_date, self.mntc_date, self.mntc_time))
        if check:
            check_lst = [x[0] for x in check]
            check_lst = u','.join(check_lst)
            frappe.throw(_(u'Cancel    Material    Visits    {0}    before    cancelling    this    Maintenance    Visit').format(check_lst))
            raise Exception
        else:
            self.update_customer_issue(0)
