Lesson 2 the Invoices

In this article we will build on top of Lesson 1 the Products, with the folowing two perspectives:

As a business problem

Adding the ability keep track of purchases for the products defined in the Lesson 1 the Products.

As a technical problem

Adding support for catalogs and tab editors.

Contents

Design

Solving the business problem requires a few new entities. First we need to be able to register purchases, each purchase will have at least a supplier, a date, a number and a list of products with quantities.

A purchase can be either a purchase order or an purchase invoice. Here is an example of how the actual business document may look like:

.

Looking at the document we determine the we need the following entities:

  1. supplier with the properties: 
    1. name of type: string
    2. address of type: string
  2. purchase_invoice -
    1. with the properties:
      1. date of type: datetime
      2. number of type: string
      3. terms of type: string
    2. and relationships:
      1. purchase_invoice_supplier of type: many-to-one to supplier
      2. purchase_invoice_purchase_invoice_item of type: parent-child to purchase_invoice_item
  3. purchase_invoice_item -
    1. with the properties:
      1. quantity of type: decimal
      2. unit_price of type: decimal
      3. amount of type: decimal
    2. and relationships:
      1. purchase_invoice_item_product of type: many-to-one to product
      2. purchase_invoice_purchase_invoice_item of type: child-parent to purchase_invoice

Creating the entities

Click the Develop app, and then click on the Customization sets menu.

In page that opens click the Add button located above the list.

In the page that opens, under the Edit section, fill the Name of the customization set, in this case "purchase_invoices".

For properties use the same technique described in section Creating the entities in Lesson 1 the Products, and for relationships as follows.

Select the Relationships tab, click the Add button for each relationship you want to add, for each added property fill the Relationship name field, the Entity name field, the Type name fieldand the Foreign entity name field. In this case you will fill:

Relationship nameEntity nameType nameForeign entity name
purchase_invoice_supplier purchase_invoicen:1supplier
purchase_invoice_purchase_invoice_item purchase_invoicep:cpurchase_invoice_item
purchase_invoice_item_product purchase_invoice_itemn:1product 

Note

In ONE ERP when you create a relationship the reversed relationship is created automatically, so when you create the parent-child relationship between purchase_invoice and purchase_invoice_item the child-parent relationship (reversed) is automatically created.
DO NOT TRY TO CREATE THE RELATIONSHIP TWICE.

Publishing the entities

Publish the entities the same way as described in Lesson 1 the Products.

Creating the views

The search view and edit view for the supplier entity should be created in the same way as described for the product entity in Lesson 1 the Products and naming it "supplierSearch" and ".supplierEditor".

For the purchase_invoice entity the views will be a bit different because they include catalogs and a parent-child relationship.

In ONE ERP entities in the parent-child relationships are handled in the same life-cycle as the parent.

Creating the catalogs

Catalogs are the logical user interface unit that facilitate working with related entities. In search views catalogs are used as filters based on related entities (e.g., searching for products in a given product category) and in edit views are used as inputs helping the user select the related entity using a display name rather than its key (e.g., selecting the supplier for a invoice by name).

In this example we will build two catalogs:

  1. A suppliers catalog that will connect the supplier to the purchase invoice
  2. A products catalog that wil connect the product to the purchase invoice item

The supplierList catalog

Click the Develop app, and then click on the Catalog menu.

In page that opens click the Add button located above the list.

In the page that opens, under the Catalog section, fill the Name of the catalog, in this case "supplierList".

Select the Fetch tab, and enter the folowing fetch query:

FETCH supplier (key, name AS value)

Click the Execute button, located above, to test the query. The result should appear under the query and should contain only the column names and a empty result set.

Click the Save button.

The productList catalog

Follow the same steps as in the previous example only changing the name to "productList" and the fetch query to:

FETCH product (key, name AS value)

Purchase invoices search view

Click the Develop app, and then click on the Search views menu.

Click the Add button located above the list.

In page that opens, under the Search sectionfill the following fields with:

  1. Title (in ui) = Purchase invoices

  2. List name (in url) = purchaseInvoiceSearch

  3. Key name (in results) = key

Select the Filters tab, click the Add button for each row, and enter the following:

TitleArgument nameInput typeCatalogOperatorProperty name
Suppliersupplier_keyFixedListsupplierListIsEqualkey
Numberinvoice_numberString
Containsnumber
Date (from)invoice_date_fromDate
IsGreaterOrEqualdate
Date (to)invoice_date_toDate
IsLessOrEqualdate

Notice the Catalog column. It is used for inputs of type FixedList and DynamicList. In this example we are using the supplierList catalog defined earlier.

Select the Fetch tab, and enter the folowing fetch query:

FETCH purchase_invoice (key GROUP BY, number, date) {
	supplier TO id_supplier (name AS supplierName GROUP BY) FILTER AND (${supplier_key}),
	purchase_invoice_item TO id_purchase_invoice (SUM(amount) AS invoice_amount)
}
FILTER AND (${invoice_number},${invoice_date_from},${invoice_date_to})

Click the Execute button, located above, to test the query. The result should appear under the query and should contain only the column names and a empty result set.

Select the Columns tabclick the Add button for each row, and enter the folowing:

Display nameColumn nameView type
Invoice numbernumberString
Supplier namesupplierNameString
Invoice datedateDate
Amountinvoice_amountDecimal

Click the Save button.

Purchase invoice edit view

Click the Develop app, and then click on the Edit views menu.

Click the Add button located above the list.

In the page that opens, under the Editor section, fill the following fields with:

  1. Name (in url) = purchaseInvoiceEditor
  2. Entity name = purchase_invoice
  3. Title new = New purchase invoice
  4. Title edit = Purchase invoice ${number}
  5. Title edit section = Invoice

Select the Tabs tab, click the Add button, fill Title = Invoice, and click the navigate arrow next to the Input tab field. 

Under the Tab sectionfill the following fields with:

  1. Tab name = purchaseInvoiceHeaderTab

  2. Tab type (grid/columns/code) = columns

Click the Add button for each row, and enter the folowing:

TitleProperty nameNavigate edit viewColumn indexIs separatorInput typeCatalog
Purchase invoice

0Yes

Supplierid_supplier
0NoFixedListsupplierList
Numbernumber
0NoInteger
Datedate
0NoDate
Termsterms
0NoLargeText

Click the Save button.

Select the Tabs tab, click the Add button, fill:

  1. Title = Items
  2. Relationship name = purchase_invoice_purchase_invoice_item 
  3. Child entity name = purchase_invoice_item

and click the navigate arrow next to the Input tab field. 

Under the Tab sectionfill the following fields with:

  1. Tab name = purchaseInvoiceItemsTab

  2. Tab type (grid/columns/code) = grid

Click the Add button for each row, and enter the folowing:

TitleProperty nameNavigate edit viewInput typeCatalogRead only
Productid_product.productEditorFixedListproductListNo
Unit priceunit_price
Decimal
No
Quantityquantity
Decimal
No
Amountamount
Decimal
Yes

Click the Save button to save the edit view tab.

Click the Save button to save the edit view.

Connecting the edit view to the search view

Click the Develop app, and then click on the Search views menu.

In page that opens locate the search view with the title "Purchase invoices" and name "purchaseInvoiceSearch"check it in the and click the Edit button located above the list.

In the page that opens, under the Edit sectionfill the following fields with:

  1. Edit view = purchaseInvoiceEditor (should be selected from list)

Click the Save button.

Updating the App

Click the Develop app, and then click on the Menus menu.

In page that opens locate the menu with the name "Products"check it in the and click the Edit button located above the list.

Click the Add button located above the list and enter the following:

NameNode item key
Suppliers#search\$supplierSearch
Purchase invoices#search\$purchaseInvoiceSearch

Click the Save button.

Now you should be able to track of your purchases, suppliers and products.