Function order_new

This function inserts a new row in the database table orders, using the values from the order_rec program record.

Function order_new (orders.4gl)
01 FUNCTION order_new()
02   SELECT MAX(order_num)+1 INTO order_rec.order_num 
03     FROM orders 
04   IF order_rec.order_num IS NULL
05    OR order_rec.order_num == 0 THEN
06     LET order_rec.order_num = 1
07   END IF
08  LET order_total = 0
09   -- We keep the same store...
10   LET order_rec.order_date = TODAY
11   LET order_rec.fac_code = "ASC"
12   LET order_rec.ship_instr = "FEDEX"
13   LET order_rec.promo = "N"
14
15   WHENEVER ERROR CONTINUE
16   INSERT INTO orders (
17      store_num,
18      order_num,
19      order_date,
20      fac_code,
21      ship_instr,
22      promo 
23   ) VALUES (
24      order_rec.store_num,
25      order_rec.order_num,
26      order_rec.order_date,
27      order_rec.fac_code,
28      order_rec.ship_instr,
29      order_rec.promo 
30   )
31   WHENEVER ERROR STOP
32   IF SQLCA.SQLCODE <> 0 THEN
33      CLEAR FORM
34      CALL __mbox_ok(title1,SQLERRMESSAGE,"stop")
35      RETURN FALSE
36   END IF
37   CALL arr_ordnums.insertElement(1)
38  LET arr_ordnums[1] = order_rec.order_num 
39   CALL arr_items.clear()
40   MESSAGE msg11
41   RETURN TRUE
42 END FUNCTION
Note:
  • Lines 02 thru 07 add the next unused order number to the order_num field of the order_rec program record, based on the existing order numbers in the orders database table.
  • Lines 08 thru 13 set the order total to zero, and add default values to some order_rec fields.
  • Lines 15 thru 31 execute the SQL statement to insert a new row in the orders database table using values from the order_rec program record.
  • Lines 32 thru 36 clear the form and display an error message if the insert into the database table failed, and return FALSE to the calling function.
  • Line 37 inserts a new empty element into the arr_ordnums array at the first position, after the successful insert into the orders table.
  • Line 38 sets the value of the new element to the order number of the order_rec program record. The arr_ordnums array keeps track of the order numbers of the orders that were retrieved from the database or newly inserted.
  • Line 39 clears the program array for items, preparing for the addition of items for the new order.
  • Line 40 displays a message indicating the insert of a new row in the orders database table was successful.
  • Line 42 returns TRUE to the calling function, indicating the insert into the orders database table was successful.