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 FUNCTIONNote: 
- Line 
02defines a variableitem_cntto hold the array count. - Line 12 returns from the function if the order number in the program record
                        
order_recisNULL. - Lines 
16thru25declare a cursor for theSELECTstatement to retrieve the rows from theitemstable that have the same order number as the value in theorder_numfield of the program recordorder_rec. Thedescriptionandunitvalues are retrieved from thestocktable, using the columnstock_num. The value forline_totalis calculated. - Lines 
29thru32theFOREACHstatement loads the dynamic arrayarr_items. - Line 
33releases the memory associated with the cursoritems_curs, which is no longer needed. - Lines 
35calls theitems_showfunction to display the order lines to the form. - Line 
36calls the functionorder_totalto calculate the total price of the items on the order.