Empress DocTypes to Creation and Management

Introduction

This guide aims to provide developers with an in-depth understanding of DocTypes in Empress, their creation, and management from a technical standpoint. We will delve into the details of backend functionality, code integration, and technical aspects that are crucial in software development and customization.

Part 1: Creating a New DocType - Library Membership

To begin, let’s create a new DocType: Library Membership. It will have the following fields:

  1. Library Member (Link, Mandatory)
  2. Full Name (Data, Read Only)
  3. From Date (Date)
  4. To Date (Date)
  5. Paid (Check)

This DocType will have Is Submittable enabled, and its Naming convention will be set as LMS.#####. This DocType will be restricted to the Librarian role. The Title Field should be set to full_name in the View Settings section.

The Library Member Link field functions similarly to a Foreign Key column in other frameworks. It allows you to link the value to a record in another DocType. In this case, it links to a record from the Library Member DocType.

The Full Name field, which is Read Only, will automatically fetch data from the full_name field from the linked Library Member record.

Part 2: Understanding Linked and Submittable DocTypes

Linked DocTypes are DocTypes that are linked in other DocTypes as Link fields. We can classify DocTypes into Master and Transactional based on the type of data they store.

When you enable Is Submittable in a DocType, it becomes a Submittable DocType. A Submittable DocType can have 3 states: Draft, Submitted, and Cancelled. An extra field, Amended From, is added to our Library Membership DocType to keep track of amendments in documents.

Part 3: Implementing Controller Validation for Membership

To ensure that no active membership exists for a given member when creating a Library Membership, we can implement the following code in library_membership.py:

import frappe
from frappe.model.document import Document
from frappe.model.docstatus import DocStatus

class LibraryMembership(Document):
    def before_submit(self):
        exists = frappe.db.exists(
            "Library Membership",
            {
                "library_member": self.library_member,
                "docstatus": DocStatus.submitted(),
                "to_date": (">", self.from_date),
            },
        )
        if exists:
            frappe.throw("There is an active membership for this member")

Part 4: Creating a Library Transaction DocType

Next, we will create another DocType to record an Issue or Return of an Article by a Library Member with an active membership. This DocType will be called Library Transaction.

Part 5: Implementing Validation for Transactions

To ensure that the Library Member has an active membership and that the Article is available for Issue, we can implement the following code in library_transaction.py:

import frappe
from frappe.model.document import Document
from frappe.model.docstatus import DocStatus

class LibraryTransaction(Document):
    def before_submit(self):
        if self.type == "Issue":
            self.validate_issue()
            article = frappe.get_doc("Article", self.article)
            article.status = "Issued"
            article.save()

        elif self.type == "Return":
            self.validate_return()
            article = frappe.get_doc("Article", self.article)
            article.status = "Available"
            article.save()

Part 6: Creating Library Settings DocType

Finally, let’s create our last DocType for the application: Library Settings. This DocType will store global settings and therefore will have Is Single enabled.

Conclusion

In this guide, we have focused on the creation and management of DocTypes in Empress, covering the different types of DocTypes, how to create them, and how to implement and manage their various functionalities. This knowledge is vital for developers seeking to customize and enhance business solutions using Empress. By understanding and effectively utilizing these features, developers can significantly enhance the functionality and usability of their applications.