home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 6 / 06.iso / a / a610 / 6.ddi / DEMO / FGL / WIND1.4GL < prev    next >
Encoding:
Text File  |  1989-12-08  |  9.9 KB  |  484 lines

  1.  
  2. DATABASE stores
  3.  
  4. #    The GLOBALS statement defines two program records
  5. #    and a program array used throughout the program.
  6.  
  7. GLOBALS
  8.  
  9.     DEFINE 
  10.  
  11.     p_customer     RECORD 
  12.              customer_num LIKE customer.customer_num,
  13.              fname LIKE customer.fname,
  14.              lname LIKE customer.lname,
  15.              address1 LIKE customer.address1,
  16.              address2 LIKE customer.address2,
  17.              city LIKE customer.city,
  18.              state LIKE customer.state,
  19.              zipcode LIKE customer.zipcode,
  20.              phone LIKE customer.phone
  21.  
  22.              END RECORD,
  23.  
  24.     p_orders     RECORD 
  25.  
  26.              order_num LIKE orders.order_num,
  27.              order_date LIKE orders.order_date,
  28.              ship_date LIKE orders.ship_date
  29.  
  30.              END RECORD,
  31.  
  32.     p_items         ARRAY[10] OF RECORD
  33.     
  34.              item_num LIKE items.item_num,
  35.              stock_num LIKE items.stock_num,
  36.              manu_code LIKE items.manu_code,
  37.              description LIKE stock.description,
  38.              quantity LIKE items.quantity,
  39.              unit_price LIKE stock.unit_price,
  40.              total_price LIKE items.total_price
  41.  
  42.              END RECORD
  43.  
  44. END GLOBALS
  45.  
  46.  
  47. #    The MAIN statement opens the order form and displays
  48. #    the form on the screen.  It calls the enter_order function
  49. #    that allows the user to enter information about a 
  50. #    customer order into the database.
  51.  
  52. MAIN
  53.  
  54.     OPEN FORM orderform FROM "order"
  55.  
  56.     DISPLAY FORM orderform
  57.  
  58.     CALL enter_order()
  59.  
  60.     CALL mess("End program.")
  61.  
  62.     CLEAR SCREEN
  63.  
  64. END MAIN
  65.  
  66. #    The enter_order function allows the user to enter an
  67. #    order for a customer on the order form, inserts the
  68. #    order into the stores database, and displays the
  69. #    order number for the new order.
  70.  
  71. FUNCTION enter_order()
  72.  
  73.     DEFINE pa_curr,sc_curr, stock     SMALLINT,
  74.         m_code CHAR(3)
  75.  
  76.     CALL get_cust()
  77.  
  78.     CALL mess("Enter an order date and a ship date.")
  79.  
  80.     INPUT p_orders.order_date, p_orders.ship_date
  81.         FROM order_date, ship_date
  82.  
  83.     CALL mess("Enter up to ten items.")
  84.  
  85.     INPUT ARRAY p_items FROM s_items.*
  86.  
  87.         BEFORE FIELD stock_num
  88.         MESSAGE "Enter a stock number or press CTRL-B for help."
  89.  
  90.         BEFORE FIELD manu_code
  91.             MESSAGE "Enter the manufacturing code or ",
  92.             "press CTRL-B for help."
  93.  
  94.         BEFORE FIELD quantity
  95.         MESSAGE "Enter a quantity."
  96.  
  97.         ON KEY (CONTROL-B)
  98.             IF infield(stock_num) OR infield(manu_code) THEN
  99.                 CALL stock_help()
  100.                 NEXT FIELD quantity
  101.             END IF
  102.  
  103.         AFTER FIELD stock_num, manu_code
  104.             MESSAGE ""
  105.  
  106.             LET pa_curr = arr_curr()
  107.  
  108.             IF     p_items[pa_curr].stock_num IS NOT NULL
  109.                 AND p_items[pa_curr].manu_code IS NOT NULL 
  110.             THEN
  111.                 CALL get_item()
  112.  
  113.                 IF p_items[pa_curr].quantity IS NOT NULL THEN
  114.  
  115.                     CALL get_total()
  116.  
  117.                 END IF
  118.  
  119.             END IF
  120.  
  121.         AFTER FIELD quantity
  122.             MESSAGE ""
  123.  
  124.             LET pa_curr = arr_curr()
  125.  
  126.             IF p_items[pa_curr].stock_num IS NOT NULL
  127.                 AND p_items[pa_curr].manu_code IS NOT NULL 
  128.                 AND p_items[pa_curr].quantity IS NOT NULL
  129.             THEN
  130.                 CALL get_total()
  131.  
  132.             END IF
  133.  
  134.  
  135.         BEFORE INSERT
  136.  
  137.             CALL get_item_num()
  138.  
  139.         AFTER INSERT
  140.  
  141.             CALL renum_items()
  142.  
  143.         AFTER DELETE     
  144.  
  145.             CALL renum_items()
  146.  
  147.     END INPUT 
  148.  
  149.     INSERT INTO orders (order_num, order_date, customer_num, ship_date)
  150.         VALUES (0,
  151.             p_orders.order_date, 
  152.             p_customer.customer_num,
  153.             p_orders.ship_date)
  154.  
  155.     LET p_orders.order_num = SQLCA.SQLERRD[2]
  156.  
  157.     DISPLAY p_orders.order_num TO order_num
  158.  
  159.     CALL insert_items()
  160.  
  161.     CALL mess("Order added.")
  162.  
  163. END FUNCTION
  164.  
  165. #    The get_cust function retrieves customer information from the
  166. #    stores database and displays it on the screen form.
  167.  
  168. FUNCTION get_cust()
  169.  
  170.     WHILE TRUE
  171.  
  172.         PROMPT "Enter a customer number or press CTRL-B for help:  "
  173.             FOR p_customer.customer_num
  174.             ON KEY (CONTROL-B)
  175.                 LET p_customer.customer_num = show_cust()
  176.         END PROMPT
  177.  
  178.         SELECT fname, lname, address1, address2,
  179.             city, state, zipcode, phone
  180.             INTO p_customer.fname THRU p_customer.phone
  181.             FROM customer 
  182.             WHERE customer_num = p_customer.customer_num
  183.  
  184.         IF status = 0 THEN
  185.  
  186.             EXIT WHILE
  187.         ELSE
  188.             CALL mess("No customer with that customer number.")
  189.  
  190.         END IF
  191.  
  192.     END WHILE
  193.  
  194.     DISPLAY p_customer.* TO customer.*
  195.  
  196. END FUNCTION
  197.  
  198. #    The show_cust function opens a window, allows the user to
  199. #    select a customer, and returns the customer number to the
  200. #    get_cust function.
  201.  
  202. FUNCTION show_cust()
  203.  
  204.     DEFINE p_cust ARRAY[25] OF RECORD
  205.         fname LIKE customer.fname,
  206.         lname LIKE customer.lname,
  207.         company LIKE customer.company
  208.         END RECORD,
  209.         p_custnum ARRAY[25] OF integer,
  210.         counter SMALLINT
  211.  
  212.     OPEN WINDOW cwindo AT 10,15
  213.         WITH FORM "cust"
  214.         ATTRIBUTE(BORDER, MESSAGE LINE FIRST)
  215.  
  216.     DECLARE cust_list CURSOR FOR
  217.         SELECT fname, lname, company, customer_num FROM customer
  218.  
  219.     LET counter = 1
  220.  
  221.     FOREACH cust_list INTO p_cust[counter].*, p_custnum[counter]
  222.         LET counter = counter + 1
  223.         IF counter > 25 THEN
  224.             EXIT FOREACH
  225.         END IF
  226.     END FOREACH
  227.  
  228.     CALL set_count(counter -1)
  229.  
  230.     MESSAGE "Highlight a customer name and press ESC"
  231.  
  232.     DISPLAY ARRAY p_cust TO s_cust.*
  233.  
  234.     LET counter = arr_curr()
  235.  
  236.     CLOSE WINDOW cwindo
  237.  
  238.     RETURN p_custnum[counter]
  239.     
  240. END FUNCTION
  241.  
  242. #    The stock_help function is called when the cursor is in the
  243. #    stock_num or manu_code field and the user presses CONTROL-B.
  244.  
  245. FUNCTION stock_help()
  246.     DEFINE pa_curr, sc_curr SMALLINT
  247.     LET sc_curr = scr_line()
  248.     LET pa_curr = arr_curr()
  249.     CALL get_stock() RETURNING 
  250.         p_items[pa_curr].stock_num,
  251.         p_items[pa_curr].manu_code,
  252.         p_items[pa_curr].description,
  253.         p_items[pa_curr].unit_price
  254.     DISPLAY p_items[pa_curr].stock_num, 
  255.         p_items[pa_curr].manu_code, 
  256.         p_items[pa_curr].description,  
  257.         p_items[pa_curr].unit_price 
  258.     TO s_items[sc_curr].stock_num, 
  259.         s_items[sc_curr].manu_code,
  260.         s_items[sc_curr].description, 
  261.         s_items[sc_curr].unit_price
  262.  
  263. END FUNCTION
  264.  
  265. #    The get_stock function opens a window, allows the user to
  266. #    query the database for information about stock items, and
  267. #    returns information about a selected item to the stock_help function.
  268.  
  269. FUNCTION get_stock()
  270.  
  271.     DEFINE p_stock ARRAY[25] OF RECORD
  272.         manu_name LIKE manufact.manu_name,
  273.         description LIKE stock.description,
  274.         unit LIKE stock.unit,
  275.         unit_price LIKE stock.unit_price
  276.         END RECORD,
  277.  
  278.         p_sn_mc ARRAY[25] OF RECORD
  279.         stock_num LIKE stock.stock_num,
  280.         manu_code LIKE stock.manu_code
  281.         END RECORD,
  282.  
  283.         counter SMALLINT,
  284.         query_1, sel_stmt CHAR(500)
  285.  
  286.     OPEN WINDOW w AT 10,8
  287.         WITH FORM "stock1"
  288.         ATTRIBUTE(BORDER, MESSAGE LINE FIRST)
  289.  
  290.  
  291.     WHILE TRUE
  292.  
  293.         MESSAGE "Enter search criteria for one or more items."
  294.  
  295.         CONSTRUCT query_1 ON 
  296.         manu_name, description, unit, unit_price
  297.         FROM s_stock[1].*
  298.  
  299.         LET sel_stmt = 
  300.         "SELECT manu_name, description, unit, unit_price, ",
  301.         "stock_num, stock.manu_code ",
  302.         "FROM stock, manufact ",
  303.         "WHERE stock.manu_code = manufact.manu_code AND ",
  304.         query_1 CLIPPED
  305.  
  306.         PREPARE s1 FROM sel_stmt
  307.  
  308.         DECLARE stock_list CURSOR FOR s1
  309.  
  310.         LET counter = 1
  311.  
  312.         FOREACH stock_list INTO  p_stock[counter].*, p_sn_mc[counter].*
  313.             LET counter = counter + 1
  314.             IF counter > 25 THEN
  315.                 EXIT FOREACH
  316.             END IF
  317.         END FOREACH
  318.  
  319.         IF counter = 1 THEN
  320.  
  321.             MESSAGE "No items satisfy the search criteria."
  322.             SLEEP 3
  323.         ELSE
  324.             EXIT WHILE
  325.  
  326.         END IF
  327.  
  328.     END WHILE
  329.  
  330.     CALL set_count(counter -1)
  331.  
  332.     MESSAGE "Highlight a stock item and press ESC"
  333.  
  334.     DISPLAY ARRAY p_stock TO s_stock.*
  335.  
  336.     LET counter = arr_curr()
  337.  
  338.     CLOSE WINDOW w
  339.  
  340.     RETURN p_sn_mc[counter].stock_num, 
  341.         p_sn_mc[counter].manu_code,
  342.         p_stock[counter].description, 
  343.         p_stock[counter].unit_price
  344.  
  345. END FUNCTION
  346.  
  347.  
  348. #    The get_item function selects the description and unit price
  349. #    for an item from the database, enters this information into
  350. #    the program array, and displays the information on the 
  351. #    screen form.
  352.  
  353. FUNCTION get_item()
  354.  
  355.     DEFINE pa_curr, sc_curr SMALLINT
  356.  
  357.     LET pa_curr = arr_curr()
  358.     LET sc_curr = scr_line()
  359.  
  360.     SELECT description, unit_price
  361.         INTO    p_items[pa_curr].description,
  362.             p_items[pa_curr].unit_price
  363.         FROM    stock
  364.         WHERE    stock.stock_num = p_items[pa_curr].stock_num
  365.             AND stock.manu_code = p_items[pa_curr].manu_code
  366.  
  367.     DISPLAY p_items[pa_curr].description, 
  368.             p_items[pa_curr].unit_price
  369.         TO s_items[sc_curr].description,
  370.             s_items[sc_curr].unit_price
  371.  
  372. END FUNCTION
  373.  
  374. #    The get_total function computes the total price for an item
  375. #    and displays this value on the form.
  376.  
  377. FUNCTION get_total()
  378.  
  379.     DEFINE pa_curr, sc_curr SMALLINT
  380.  
  381.     LET pa_curr = arr_curr()
  382.  
  383.     LET sc_curr = scr_line()
  384.  
  385.     LET p_items[pa_curr].total_price =
  386.         p_items[pa_curr].quantity * p_items[pa_curr].unit_price
  387.  
  388.     DISPLAY p_items[pa_curr].total_price
  389.         TO s_items[sc_curr].total_price
  390.  
  391. END FUNCTION
  392.  
  393.  
  394. #    The get_item_num function displays the item number of the
  395. #    current program array row before the user begins entering
  396. #    information.
  397.  
  398. FUNCTION get_item_num()
  399.  
  400.     DEFINE pa_curr, sc_curr SMALLINT
  401.  
  402.     LET pa_curr = arr_curr()
  403.  
  404.     LET sc_curr = scr_line()
  405.  
  406.     LET p_items[pa_curr].item_num = pa_curr
  407.  
  408.     DISPLAY pa_curr TO s_items[sc_curr].item_num
  409.  
  410. END FUNCTION
  411.  
  412.  
  413. FUNCTION renum_items()
  414.  
  415.     DEFINE pa_curr,
  416.         pa_total,
  417.         sc_curr,
  418.         sc_total,
  419.         k    SMALLINT
  420.  
  421.     LET pa_curr = arr_curr()
  422.  
  423.     LET pa_total = arr_count()
  424.  
  425.     LET sc_curr = scr_line()
  426.  
  427.     LET sc_total = 4
  428.  
  429.     FOR k = pa_curr TO pa_total
  430.  
  431.         LET p_items[k].item_num = k
  432.  
  433.         IF sc_curr <= sc_total THEN
  434.  
  435.             DISPLAY k TO s_items[sc_curr].item_num
  436.  
  437.             LET sc_curr = sc_curr + 1
  438.  
  439.         END IF
  440.  
  441.     END FOR
  442.  
  443. END FUNCTION
  444.  
  445.  
  446. #    The insert_items function inserts the items in the program array
  447. #    into the database.
  448.  
  449. FUNCTION insert_items()
  450.  
  451.     DEFINE counter SMALLINT
  452.  
  453.     FOR counter = 1 TO arr_count()
  454.  
  455.         INSERT INTO items 
  456.             VALUES (p_items[counter].item_num,
  457.                 p_orders.order_num,
  458.                 p_items[counter].stock_num,
  459.                 p_items[counter].manu_code,
  460.                 p_items[counter].quantity,
  461.                 p_items[counter].total_price)
  462.  
  463.     END FOR
  464.  
  465. END FUNCTION
  466.  
  467. #    The mess function displays a message on line 23 of the screen.
  468. #    Other functions in the program pass character strings 
  469. #    to the mess function.
  470.  
  471. FUNCTION mess(str)
  472.  
  473.     DEFINE str CHAR(50)
  474.  
  475.     DISPLAY str CLIPPED AT 23,1
  476.  
  477.     SLEEP 3
  478.  
  479.     DISPLAY "" AT 23, 1
  480.  
  481. END FUNCTION
  482.  
  483.  
  484.