Complete order process

An order can have two possible ending scenarios:

  1. The user decides the products he added to the order are necessary and finalizes it - the order passes into pending and becomes available for the site administrators. To implement this functionality, you will create the complete_order page, which computes the remaining stocks, forwards the order to the administrators  and sends additional e-mail messages (e.g. thank you, confirmation, or if no stocks are available - but this case will be treated later on).

  2. The user decides the order is no longer necessary and cancels it. At this point, all order details must be destroyed, as a new order may be created. Order details are stored in the order_ord and order_product_opr tables, as well as in the kt_order_id session variable. Therefore, clearing the order means deleting all records and variables. This is the clear_order page.

 

First you will implement the clear_order page that deletes all details of an existing order. To build this page, follow the next steps:

  1. Open the clear_order page in Dreamweaver. A restrict access to page server behavior should be already on the page, added in the User authentication section of the tutorial.

  2. Since the page must delete the order details, it will contain a delete transaction that will remove the order details from the order_ord table (this is where the ID, date, status and user are stored for each order).

  3. To remove a record, apply the Delete Record Wizard from the MX Kollection tab of the Insert bar. Configure the wizard as follows:

  4. At this point, when you click the clear order link, order details are removed from the order_ord table. However, this is not the only table storing information about an order: the order_product_opr table stores the list and quantity of products associated to an order. As shown in the database structure description, it contains pairs of identifiers from the product and order tables, as well as the quantity for each product.

  5. To completely remove an order, all records stored in the order_product_opr table which relate to the order deleted at step 3 must be removed. Since the order ID is stored as a foreign key in the table, it can be viewed as a detail table of order_ord. MX Kollection 3 provides a server behavior that allows removing orphaned records (detail records without a master element): the Delete Detail Records trigger.

  6. Apply the Delete Detail Records trigger from the Server Behaviors tab > + > MX Kollection > Form Validation. The trigger will automatically recognize the existing delete transaction and will register to it. Also, the master table and primary key are automatically filled in.

  7. Configure the remaining options as follows:

  8. Now, all order details stored in the database will be deleted when the clear order link is used. The only remaining proof of the order existence (which also raises a problem when creating a new order) is the ID saved in the kt_order_id session variable. The problem appears when the same user tries to initiate a new order, after clearing an existing one. Since the ID is still in the session variable, the order is considered as existing in the database, and adding products to the order will result into invalid records (an order ID that does not exist will be entered together with the product code in the order_product_opr table).

  9. To remove this variable you will have to write some code of your own. To enter the code, add a Custom trigger to the page. You can access this server behavior from the Server Behaviors tab > + > MX Kollection > Forms. By default, the trigger will execute after the delete transaction, with a priority of 50. These properties are OK, as the variable removal must take place after the records are deleted from the database table.

  10. In the Custom trigger dialog box's Basic tab enter the code that clears the session variable in the textarea. The code to enter is:

  11. The custom trigger dialog box should look like the following (the screen shot was taken on PHP_MySQL so code differences will appear):


     

  12. Apply the server behavior and add the code to the page by clicking OK.

The page that clears an order is now completed, as it removes all traces of the order, both from the database and from the session variables.

 

The page you will create next handles completing and confirming an order. The page that performs this action is complete_order. The list of actions to be performed by the complete order page should be:

  1. For each product added to the order, the requested quantity must be checked against the available quantity. This is done by retrieving the ordered quantity and the product available quantity. If the ordered quantity is higher than the available one, than there is not enough in stock.

  2. The order is then treated globally:

  3. The user is redirected to a thank you page, which contains a link to the product list page.

 

To implement this suite of actions, follow the next steps:

  1. Open the complete_order page in Dreamweaver.

  2. The quantity check and the e-mail sending are implemented using triggers. The order status update is implemented by using an update record transaction. A trigger can only be added to a page that already contains a transaction, so the update transaction will be the first one to implement.

  3. Since the update operation will take place behind the scenes, without displaying any fields (the status to update to is set by the trigger that checks the quantity - either pending or waiting for acknowledgement), there is no point in using the wizard. Instead, use the Update Record Transaction server behavior. It can be found in the Server Behaviors tab > + > MX Kollection > Forms > Advanced.

  4. The dialog box is divided into three tabs, each addressing some options that can be set for the transaction. The Basic tab is where you must define general information regarding the database, starting condition and redirect page:

  5. In the Fields tab, you can decide which table columns will be involved in the update transaction and where each of them will take their value from.

  6. Click OK to close the dialog box and add all elements to the page. A translator will be displayed in Dreamweaver, pointing to where possible error messages will be displayed.

  7. After the update transaction is added to the page, you can start creating the section that checks the stocks, updates available quantity and sets the order status. These operations cannot be done with MX Kollection 3 features, so once more, a little coding is needed. The code is entered by using a Custom Trigger. To add a custom trigger, go to the Server Behaviors tab >+>MX Kollection >Forms.

  8. Before writing the actual code, switch to the Advanced tab, and set the trigger type to BEFORE and the priority to 1. This is done because the quantity check must take place first on page.

  9. The code provided below executes the following actions:

  10. The code to use is as follows (use the one appropriate to your server model)

  11. The completed dialog box (for PHP) should look as the image below:


     

  12. To close the Custom trigger dialog box click OK.

 

The last step to take in order to complete the page is to add the trigger that sends an e-mail message if not enough products are available. This e-mail message will contain two links: one that allows cancelling the order - by deleting it from the database tables, and one that switches its status to acknowledged.

The pages are delete_order and acknowledge_order. These pages will delete and update a record, respectively, given the URL parameter passed from the mail message. A security issue arises, as if the primary key is passed directly, one can delete or acknowledge multiple orders, thus corrupting the database information. To ensure this does not happen, another unique identifier, that is much more difficult to guess, must be used.

One solution is to use a new table column to store a unique random key that is generated with the mail sending, and to delete it once the operation is finished. The other option at hand is to use the existing sesid_ord column, that already stores a unique and hard to guess value: the user's session identifier. The choice is up to you, both methods will work. In this tutorial the latter option was chosen as it does not involve altering the table.

The delete_order and acknowledge_order pages will be created later on. For now, the mail message will contain links to those pages, carrying the sesid_ord value as an URL parameter.

To add the trigger that sends the e-mail message, follow the instructions below:

  1. Open the complete_order page, if necessary.

  2. In order to use the sesid_ord column as dynamic data in the e-mail message, you must first retrieve it from the database. To do so, create a new recordset on the order_ord table, filtered by the kt_order_id session variable as shown below:


     

  3. Before adding e-mail sending capabilities to the page, you must first retrieve the user's e-mail address. Since the email address is used for authentication, it is already available, as a session variable: kt_login_user:


       
  4. To add the e-mail sending option, you will use the Send E-mail trigger. Apply it from Server Behaviors > + > MX Kollection > Send E-mail. Configure the user interface as shown below:
  5. In order for the links to be displayed correctly, you must also set the message type to HTML instead of plain text. To do this, switch to the Options tab and select the HTML text option.


                           
  6. The last step to perform when creating the e-mail is to make its execution conditional. Do not close the Send E-mail user interface just yet. Before starting to work with the Send E-mail trigger, it has been mentioned that it must be executed only when there is not enough stock to satisfy the order.
    The existence of enough stock is reflected by the order status, after the transaction completes (its status is updated to either 2 - waiting for acknowledgment or 1 - pending). The Send E-mail trigger is by default of the AFTER type, and is executed after the transaction completes, and the status is updated in the transaction field. This will be the condition to check before starting the e-mail trigger.
  7. To add a starting condition to the e-mail trigger switch to the Advanced tab of the user interface. In the lower part of the window, a text field labeled Condition and a Build condition button are displayed. Click on the Build condition button to open the Condition builder.
  8. In the dialog box that opens, you can select the two expressions to compare, as well as the operator between them. For each you can use static and dynamic data.
  9. For the first expression, click on the InterAKT Dynamic Data icon. In the new dialog box, select the idsta_ord transaction field (it is the one used in the update transaction, that now stores the new status):


                                     
  10. Click OK to close the window and place the generated mark-up code into the first expression's text field.
  11. Next comes the operator. The condition states that the order status must be equal to the value 2 (waiting) to send the e-mail. Therefore, select the equal sign (==) from the drop-down menu.
  12. For the second expression, you will use a static, entered value. Since the status must be waiting for acknowledgment, the status code must equal 2. Enter the value 2 in the text field.


                                 
  13. At this point, the trigger's starting condition is completely defined (both expressions and operator) so you can click OK to close the dialog box and return to the Send E-mail trigger user interface. The condition you defined will be also displayed in the trigger's Condition text field.

 
To apply the Send E-mail server behavior click on the OK button. This will close the dialog box and add a new server behavior to the page. The order can now be completed.
In the next section of the tutorial, you will create the pages that process the user's answer to the low stock e-mail message: pages that allow canceling or acknowledging the order.