home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a063 / 6.img / SAMPLE / APTFORMS / DSCIN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-24  |  5.0 KB  |  179 lines

  1. #include <sybfront.h>
  2. #include <sybdb.h>
  3. #include <sybfrs.h>
  4.  
  5. #if MSDOS
  6. #    include    "aforms.h"
  7. #endif /* MSDOS */
  8.  
  9. #define SEPARATOR "  "
  10. #define SEP_LEN 2
  11. #define BUFSIZE 100
  12.  
  13. /*
  14. **    DSCIN - Get the discount information and display it.
  15. **    
  16. **    Retrieve all suitable discounts from the database and put
  17. **    them onto the form.
  18. **    This procedure is triggered by sdsc form pre-processing.
  19. */
  20.  
  21. dscin(context, argarray)
  22. FRSCONTEXT           *context;
  23. POINTER              argarray[];
  24. {
  25.  
  26.     DBPROCESS       *dbproc;
  27.     FORM            *form;
  28.     FRSIO           *io_cxt;
  29.     OBJDSC          objdsc;
  30.     OBJDSC          *curfld = &objdsc;
  31.     OBJDSC          grpdsc;
  32.     OBJDSC          *group = &grpdsc;
  33.     int             numrows;
  34.     int             datlen;
  35.     int             len;
  36.     char            *storeid;
  37.     char            *char_vol;
  38.     char            buf[BUFSIZE];
  39.     char            *bufptr;
  40.  
  41.     dbproc = (DBPROCESS *)argarray[0];
  42.     form = fscurform(context);
  43.  
  44.     /* We want all store discounts, quantity discounts,
  45.     ** and general-purpose discounts.
  46.     */
  47.  
  48.     /* Get the number of books ordered and the storeid.  Since the SQL 
  49.     ** command uses character strings rather than actual numbers, we 
  50.     ** can take the character string straight from the form.
  51.     */
  52.     char_vol = fsfreadval(form, "quantity", -1, NULL, NULL, NULL);
  53.     storeid = fsfreadval(form, "storeid", -1, NULL, NULL, NULL);
  54.     if ((char_vol == NULL) || (storeid == NULL))
  55.         return;     /* Nothing to process... */
  56.  
  57.     /* Place the SQL query in the command buffer. */
  58.     dbfcmd(dbproc,
  59.         "select discounttype, stor_id, lowqty, highqty, discount from");
  60.     dbfcmd(dbproc, " discounts where stor_id = \"%s\" or", storeid);
  61.     dbfcmd(dbproc, " %s >= lowqty and %s <= highqty or", char_vol,char_vol);
  62.     dbfcmd(dbproc, " stor_id = NULL and lowqty = NULL and highqty = NULL");
  63.  
  64.     /* Send commands to SQL Server; start execution. */
  65.     dbsqlexec(dbproc);
  66.  
  67.     /* Prepare to process results of the query. */
  68.     dbresults(dbproc);
  69.  
  70.     /* Set up the group for loading. */
  71.  
  72.     if ( (fsggetgrp(form, "grpdiscount", -1, NULL, group) )  == NULL)
  73.         fsabort("Unable to find the 'grpdiscount' group");
  74.  
  75.     io_cxt = fsioalloc(form);
  76.     fsiosetfld(io_cxt, group, NULL);
  77.  
  78.     numrows = 0;
  79.     fsgsetusedrows(form, group, numrows);
  80.  
  81.     /* Process each row returned from the SQL Server.  Each form row
  82.     ** has several fields, and each SQL Server row has several fields,
  83.     ** but not the same fields--this code connects them.
  84.     */
  85.     while (dbnextrow(dbproc) != NO_MORE_ROWS)
  86.     {
  87.         /* The 'pick' field is on the form, but its value is not returned 
  88.         ** by the SQL Server.  Initialize it to 'NO', which is dataid 2.
  89.         */
  90.         fsgsetusedrows(form, group, ++numrows);
  91.         curfld = fsionxtfld(io_cxt, FALSE);
  92.         fsfputval(form, curfld, 2, SSON, NULL, NULL, -1, FALSE);
  93.  
  94.  
  95.         /* The discount description field on the form is composed of
  96.         ** several database columns, plus some additional information.
  97.         */
  98.         curfld = fsionxtfld(io_cxt, FALSE);
  99.  
  100.         /* If there is a discount description in the database,
  101.         ** copy it to the buffer.
  102.         */
  103.         bufptr = buf;
  104.         datlen = dbdatlen(dbproc, 1);
  105.         if (datlen > 0)
  106.         {
  107.             strncpy( bufptr, dbdata(dbproc, 1), datlen);
  108.             bufptr += datlen;
  109.             strncpy(bufptr, SEPARATOR, SEP_LEN);
  110.             bufptr += SEP_LEN;
  111.         }
  112.  
  113.         /* If this discount is specific to a store,
  114.         ** copy the store name to the buffer.
  115.         */
  116.         datlen = dbdatlen(dbproc, 2);
  117.         if (datlen > 0)
  118.         {
  119.             strncpy( bufptr, "Store Id:", 9);
  120.             bufptr += 9;
  121.             strncpy( bufptr, dbdata(dbproc, 2), datlen);
  122.             bufptr += datlen;
  123.             strncpy(bufptr, SEPARATOR, SEP_LEN);
  124.             bufptr += SEP_LEN;
  125.         }
  126.  
  127.         /* If this discount is volume-based, copy the low and
  128.         ** high limits of the quantity to the buffer.
  129.         */
  130.         if (dbdatlen(dbproc, 3) > 0)
  131.         {
  132.             len = dbconvert(dbproc, dbcoltype(dbproc, 3),
  133.                             dbdata(dbproc, 3),
  134.                             dbdatlen(dbproc, 3),
  135.                             SYBCHAR, bufptr, -1);
  136.             bufptr += len;
  137.             strncpy( bufptr, " -> ", 4);
  138.             bufptr += 4;
  139.             len = dbconvert(dbproc, dbcoltype(dbproc, 4),
  140.                             dbdata(dbproc, 4),
  141.                             dbdatlen(dbproc, 4),
  142.                             SYBCHAR, bufptr, -1);
  143.             bufptr += len;
  144.         }
  145.         *bufptr = '\0';
  146.         fsfputval(form, curfld, -1, SSOFF, NULL, buf, -1, FALSE);
  147.  
  148.  
  149.         /* The last field, percent, is in the database
  150.         ** and on the form.
  151.         */
  152.         curfld = fsionxtfld(io_cxt, FALSE);
  153.         fsfputval(form, curfld, -1, SSOFF, 
  154.             dbdata(dbproc, 5), buf, -1, FALSE);
  155.  
  156.         /* We have now looped through the entire form row and 
  157.         ** have read the entire SQL Server row.
  158.         */
  159.     }
  160.  
  161.     /* Set the initial additional discount to zero. */
  162.  
  163.     fsfwriteval(form, "adddisc", -1, NULL, -1, SSOFF,
  164.         NULL, "0", 1, FALSE);
  165.  
  166.     /* Set the initial total discount to zero. */
  167.  
  168.     fsfwriteval(form, "totpercent", -1, NULL, -1, SSOFF,
  169.         NULL, "0", 1, FALSE);
  170.  
  171.     /* Set the initial cost: (per-book cost * quantity) */
  172.  
  173.     clcst(form, "quantity", "totpercent", "price", "totalcost");
  174.     
  175.     /* All done! */
  176.     fsiofree(io_cxt);
  177.     return;
  178. }
  179.