Sales and purchasing

Traditionally, salespersons used to work with paper order forms. They would write down the customer name and address and the items or services required.

Sales and purchasing

Paper order form

In Microsoft Dynamics NAV, the paper document is replaced by a sales and purchase document using a header for the general order information and lines to register the items and services.

The posting process breaks down the information in the document into the journals and posts them, so the end user does not have to worry about this. The application reuses the same posting routines as we discussed in earlier chapters.

Let's look at how the documents and journals tie together by drawing the table and transaction scheme for this:

Sales and purchasing

The first step is creating the document. When we create this Sales Document (Sales Header and Sales Line), nothing is posted. We are only entering the information into the system that can be changed at any time.

When we start the Codeunit Sales-Post (80), the system will create all the journals for us and post them. When we sell an item, the system will create an Item Journal Line, and when we sell a resource, the system creates a Resource Journal Line, and so on.

The Invoice Posting buffer is used to create the entries in the General Journal Line. We already discussed this feature in Chapter 3, Financial Management.

Microsoft Dynamics NAV allows us to create four different kinds of posted sales documents: invoices, shipments, credit memos, and return receipts. We will discuss all these types later in this chapter.

The unique concept of sales and purchase in Microsoft Dynamics NAV is the mirroring of the transaction structure. Once we understand how the sales transactions fit together, it will be easy to understand the structure of a purchase.

Let's demonstrate this by comparing the first fields in Table 37 Sales Line and Table 39 Purchase Line, as shown in the following screenshot:

Transaction mirroring

The fields in both tables are equally numbered and serve the same process even though they use different terminology, for example, field 18, Qty. to Ship (sale) and Qty. To Receive (purchase).

Some fields are different because they don't make sense to be in both processes, for example, Unit Price (LCY) (field 31) in purchase and Customer Price Group (field 42) in sales.

The purchase process also uses the same posting methodology. The purchase header (38) and purchase line (39) tables are posted using the codeunit Purch.-Post (90) in the purchase receipt, invoice, credit memo, and return shipment documents.

Let's have a closer look at the sales process.

The sales process supports six document types that are normalized into two tables, sales header (36) and sales line (37).

Each process can have its own Number Series and has a special card and list page, but they all share the same business logic. Let's discuss the document types:

In order to understand the examples in this chapter, we will discuss the most important fields of the sales order. A sales document contains one header and multiple lines.

While the sales header table contains more static registration of information, the sales line has more real business logic, such as price calculation, inventory availability, and VAT. We will discuss how this business logic is normalized.

Creating a new sales order

The sales line table has a specific validation flow of functions that is important to understand before making changes to the table. This flow is based on the normal way an end user creates a sales line.

To create a sales line, only four fields are populated and the line is ready to use. After setting the type and choosing a no., the end user types in the Quantity field and if necessary, the Unit Price field.

Let's analyze the C/AL code in the OnValidate trigger of the three fields that can calculate the sales line.

The C/AL code in the OnValidate trigger starts by doing the initial testing, if the change is allowed. After this, the record is cleared and the old values for the No. field and Quantity fields are applied, as follows:

Then, the sales line inherits the values from the sales header, if required, and the date fields are calculated, as follows:

When this is done, we see a CASE statement where the master data is acquired. This would be the place where we would move newly added fields from master data to the sales line table.

When this is done, the quantities are calculated and the unit price is calculated.

The latter is very important for our analysis. After this function, other code is executed but this is not important for this example.

In Microsoft Dynamics NAV, a sales order can be shipped and invoiced directly from the document.

However, not all companies have a combined shipping and invoicing process. Some companies ship the goods first and send the invoice later, most of the time using combined invoicing.

Before we can ship the items we sold, we first need to purchase or produce them. We discussed the production process in the previous chapter, so let's focus on the purchasing process.

Technically, the sales and purchase process are mirrored transactions and the application design is similar. The purchase header table has the same document types: quote, order, invoice, credit memo, blanket order, and return order, and the same posting process.

So instead of going into the similarities, we will discuss the differences.

When selling items that are not in the inventory, it is possible to purchase the items from a vendor and have them directly shipped to the customer. This process is called drop shipments.

This process can be handled manually and using the requisition worksheet.

Within the sales and purchase document process, there is a workflow available for releasing and approving a document. This is taken care of by a single status field and two processes.

Before a document can be posted, it is mandatory to release it. This is done by the codeunits Release Sales Document (414) and Release Purchase Document (415). These codeunits are, as you may have guessed, almost identical.

The codeunit performs a number of tests before setting the status to Released. Let's discuss some of these checks:

During the life cycle of our application, many documents will be created. There might come a day when this exceeds the point where some maintenance is required.