Function items_fetch

This function retrieves the rows from the items table that match the value of order_num in the order currently displayed on the form. The description and unit values are retrieved from the stock table, using the column stock_num. The value for line_total is calculated and retrieved. After displaying the items on the form, the function order_total is called to calculate the total price of all the items for the current order.

Function items_fetch (orders.4gl):
01 FUNCTION items_fetch()
02   DEFINE item_cnt INTEGER,
03         item_rec RECORD
04            stock_num    LIKE items.stock_num,
05            description  LIKE stock.description,
06            quantity     LIKE items.quantity,
07            unit         LIKE stock.unit,
08            price        LIKE items.price,
09            line_total   DECIMAL(9,2)
10         END RECORD
11 
12   IF order_rec.order_num IS NULL THEN
13      RETURN
14   END IF
15 
16   DECLARE items_curs CURSOR FOR
17      SELECT items.stock_num, 
18             stock.description, 
19             items.quantity, 
20             stock.unit, 
21             items.price,
22             items.price * items.quantity line_total 
23         FROM items, stock 
24        WHERE items.order_num = order_rec.order_num 
25          AND items.stock_num = stock.stock_num 
26 
27   LET item_cnt = 0
28   CALL arr_items.clear()
29   FOREACH items_curs INTO item_rec.*
30       LET item_cnt = item_cnt + 1
31       LET arr_items[item_cnt].* = item_rec.*
32   END FOREACH
33   FREE items_curs 
34 
35   CALL items_show()
36   CALL order_total(item_cnt)
37 
38 END FUNCTION
Note:
  • Line 02 defines a variable item_cnt to hold the array count.
  • Line 12 returns from the function if the order number in the program record order_rec is NULL.
  • Lines 16 thru 25 declare a cursor for the SELECT statement to retrieve the rows from the items table that have the same order number as the value in the order_num field of the program record order_rec. The description and unit values are retrieved from the stock table, using the column stock_num. The value for line_total is calculated.
  • Lines 29 thru32 the FOREACH statement loads the dynamic array arr_items.
  • Line 33 releases the memory associated with the cursor items_curs, which is no longer needed.
  • Lines 35 calls the items_show function to display the order lines to the form.
  • Line 36 calls the function order_total to calculate the total price of the items on the order.