'Checks if `modified` timestamp provided by document being updated is same as the
`modified` timestamp in the database. If there is a different, the document has been
updated in the database after the current copy was read. Will throw an error if
timestamps don\'t match.
Will also validate document transitions (Save > Submit > Cancel) calling
`self.check_docstatus_transition`.'

def check_if_latest(self):

    conflict = False
    self._action = u'save'
    if (not self.get(u'__islocal')):
        if self.meta.issingle:
            modified = frappe.db.sql(u"select    value    from    tabSingles\n DCTB  DCTB  DCTB  DCTB  DCTB where    doctype=%s    and    field='modified'    for    update", self.doctype)
            modified = (modified and modified[0][0])
            if (modified and (modified != cstr(self._original_modified))):
                conflict = True
        else:
            tmp = frappe.db.sql(u'select    modified,    docstatus    from    `tab{0}`\n DCTB  DCTB  DCTB  DCTB  DCTB where    name    =    %s    for    update'.format(self.doctype), self.name, as_dict=True)
            if (not tmp):
                frappe.throw(_(u'Record    does    not    exist'))
            else:
                tmp = tmp[0]
            modified = cstr(tmp.modified)
            if (modified and (modified != cstr(self._original_modified))):
                conflict = True
            self.check_docstatus_transition(tmp.docstatus)
        if conflict:
            frappe.msgprint(((_(u'Error:    Document    has    been    modified    after    you    have    opened    it') + (u'    (%s,    %s).    ' % (modified, self.modified))) + _(u'Please    refresh    to    get    the    latest    document.')), raise_exception=frappe.TimestampMismatchError)
    else:
        self.check_docstatus_transition(0)
