home *** CD-ROM | disk | FTP | other *** search
- #include <sybfront.h>
- #include <sybdb.h>
- #include <sybfrs.h>
-
- #if MSDOS
- # include "aforms.h"
- #endif /* MSDOS */
-
- #define SEPARATOR " "
- #define SEP_LEN 2
- #define BUFSIZE 100
-
- /*
- ** DSCIN - Get the discount information and display it.
- **
- ** Retrieve all suitable discounts from the database and put
- ** them onto the form.
- ** This procedure is triggered by sdsc form pre-processing.
- */
-
- dscin(context, argarray)
- FRSCONTEXT *context;
- POINTER argarray[];
- {
-
- DBPROCESS *dbproc;
- FORM *form;
- FRSIO *io_cxt;
- OBJDSC objdsc;
- OBJDSC *curfld = &objdsc;
- OBJDSC grpdsc;
- OBJDSC *group = &grpdsc;
- int numrows;
- int datlen;
- int len;
- char *storeid;
- char *char_vol;
- char buf[BUFSIZE];
- char *bufptr;
-
- dbproc = (DBPROCESS *)argarray[0];
- form = fscurform(context);
-
- /* We want all store discounts, quantity discounts,
- ** and general-purpose discounts.
- */
-
- /* Get the number of books ordered and the storeid. Since the SQL
- ** command uses character strings rather than actual numbers, we
- ** can take the character string straight from the form.
- */
- char_vol = fsfreadval(form, "quantity", -1, NULL, NULL, NULL);
- storeid = fsfreadval(form, "storeid", -1, NULL, NULL, NULL);
- if ((char_vol == NULL) || (storeid == NULL))
- return; /* Nothing to process... */
-
- /* Place the SQL query in the command buffer. */
- dbfcmd(dbproc,
- "select discounttype, stor_id, lowqty, highqty, discount from");
- dbfcmd(dbproc, " discounts where stor_id = \"%s\" or", storeid);
- dbfcmd(dbproc, " %s >= lowqty and %s <= highqty or", char_vol,char_vol);
- dbfcmd(dbproc, " stor_id = NULL and lowqty = NULL and highqty = NULL");
-
- /* Send commands to SQL Server; start execution. */
- dbsqlexec(dbproc);
-
- /* Prepare to process results of the query. */
- dbresults(dbproc);
-
- /* Set up the group for loading. */
-
- if ( (fsggetgrp(form, "grpdiscount", -1, NULL, group) ) == NULL)
- fsabort("Unable to find the 'grpdiscount' group");
-
- io_cxt = fsioalloc(form);
- fsiosetfld(io_cxt, group, NULL);
-
- numrows = 0;
- fsgsetusedrows(form, group, numrows);
-
- /* Process each row returned from the SQL Server. Each form row
- ** has several fields, and each SQL Server row has several fields,
- ** but not the same fields--this code connects them.
- */
- while (dbnextrow(dbproc) != NO_MORE_ROWS)
- {
- /* The 'pick' field is on the form, but its value is not returned
- ** by the SQL Server. Initialize it to 'NO', which is dataid 2.
- */
- fsgsetusedrows(form, group, ++numrows);
- curfld = fsionxtfld(io_cxt, FALSE);
- fsfputval(form, curfld, 2, SSON, NULL, NULL, -1, FALSE);
-
-
- /* The discount description field on the form is composed of
- ** several database columns, plus some additional information.
- */
- curfld = fsionxtfld(io_cxt, FALSE);
-
- /* If there is a discount description in the database,
- ** copy it to the buffer.
- */
- bufptr = buf;
- datlen = dbdatlen(dbproc, 1);
- if (datlen > 0)
- {
- strncpy( bufptr, dbdata(dbproc, 1), datlen);
- bufptr += datlen;
- strncpy(bufptr, SEPARATOR, SEP_LEN);
- bufptr += SEP_LEN;
- }
-
- /* If this discount is specific to a store,
- ** copy the store name to the buffer.
- */
- datlen = dbdatlen(dbproc, 2);
- if (datlen > 0)
- {
- strncpy( bufptr, "Store Id:", 9);
- bufptr += 9;
- strncpy( bufptr, dbdata(dbproc, 2), datlen);
- bufptr += datlen;
- strncpy(bufptr, SEPARATOR, SEP_LEN);
- bufptr += SEP_LEN;
- }
-
- /* If this discount is volume-based, copy the low and
- ** high limits of the quantity to the buffer.
- */
- if (dbdatlen(dbproc, 3) > 0)
- {
- len = dbconvert(dbproc, dbcoltype(dbproc, 3),
- dbdata(dbproc, 3),
- dbdatlen(dbproc, 3),
- SYBCHAR, bufptr, -1);
- bufptr += len;
- strncpy( bufptr, " -> ", 4);
- bufptr += 4;
- len = dbconvert(dbproc, dbcoltype(dbproc, 4),
- dbdata(dbproc, 4),
- dbdatlen(dbproc, 4),
- SYBCHAR, bufptr, -1);
- bufptr += len;
- }
- *bufptr = '\0';
- fsfputval(form, curfld, -1, SSOFF, NULL, buf, -1, FALSE);
-
-
- /* The last field, percent, is in the database
- ** and on the form.
- */
- curfld = fsionxtfld(io_cxt, FALSE);
- fsfputval(form, curfld, -1, SSOFF,
- dbdata(dbproc, 5), buf, -1, FALSE);
-
- /* We have now looped through the entire form row and
- ** have read the entire SQL Server row.
- */
- }
-
- /* Set the initial additional discount to zero. */
-
- fsfwriteval(form, "adddisc", -1, NULL, -1, SSOFF,
- NULL, "0", 1, FALSE);
-
- /* Set the initial total discount to zero. */
-
- fsfwriteval(form, "totpercent", -1, NULL, -1, SSOFF,
- NULL, "0", 1, FALSE);
-
- /* Set the initial cost: (per-book cost * quantity) */
-
- clcst(form, "quantity", "totpercent", "price", "totalcost");
-
- /* All done! */
- fsiofree(io_cxt);
- return;
- }
-