'Insert the document in the database (as a new document).
This will check for user permissions and execute `before_insert`,
`validate`, `on_update`, `after_insert` methods if they are written.
:param ignore_permissions: Do not check permissions if True.'

def insert(self, ignore_permissions=None, ignore_if_duplicate=False, ignore_mandatory=None):

    if self.flags.in_print:
        return
    self.flags.email_alerts_executed = []
    if (ignore_permissions != None):
        self.flags.ignore_permissions = ignore_permissions
    if (ignore_mandatory != None):
        self.flags.ignore_mandatory = ignore_mandatory
    self.set(u'__islocal', True)
    self.check_permission(u'create')
    self._set_defaults()
    self.set_user_and_timestamp()
    self.set_docstatus()
    self.check_if_latest()
    self.run_method(u'before_insert')
    self.set_new_name()
    self.set_parent_in_children()
    self.validate_higher_perm_levels()
    self.flags.in_insert = True
    self.run_before_save_methods()
    self._validate()
    self.set_docstatus()
    self.flags.in_insert = False
    if getattr(self.meta, u'issingle', 0):
        self.update_single(self.get_valid_dict())
    else:
        try:
            self.db_insert()
        except frappe.DuplicateEntryError as e:
            if (not ignore_if_duplicate):
                raise e
    for d in self.get_all_children():
        d.db_insert()
    self.run_method(u'after_insert')
    self.flags.in_insert = True
    if self.get(u'amended_from'):
        self.copy_attachments_from_amended_from()
    self.run_post_save_methods()
    self.flags.in_insert = False
    if hasattr(self, u'__islocal'):
        delattr(self, u'__islocal')
    return self
