'Ensures valid `docstatus` transition.
Valid transitions are (number in brackets is `docstatus`):
- Save (0) > Save (0)
- Save (0) > Submit (1)
- Submit (1) > Submit (1)
- Submit (1) > Cancel (2)'

def check_docstatus_transition(self, docstatus):

    if (not self.docstatus):
        self.docstatus = 0
    if (docstatus == 0):
        if (self.docstatus == 0):
            self._action = u'save'
        elif (self.docstatus == 1):
            self._action = u'submit'
            self.check_permission(u'submit')
        else:
            raise frappe.DocstatusTransitionError(_(u'Cannot    change    docstatus    from    0    to    2'))
    elif (docstatus == 1):
        if (self.docstatus == 1):
            self._action = u'update_after_submit'
            self.check_permission(u'submit')
        elif (self.docstatus == 2):
            self._action = u'cancel'
            self.check_permission(u'cancel')
        else:
            raise frappe.DocstatusTransitionError(_(u'Cannot    change    docstatus    from    1    to    0'))
    elif (docstatus == 2):
        raise frappe.ValidationError(_(u'Cannot    edit    cancelled    document'))
