home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #2 / Image.iso / personal / gitarid.zip / SRC1 < prev    next >
Text File  |  1993-03-24  |  13KB  |  550 lines

  1.  
  2. #include "isam.h"
  3. #include "winboss.h"                    /* windows header file */
  4. #include "tools.cfg"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <conio.h>
  8.  
  9. char   *enlarge_buf (
  10.                      char    *buf,
  11.                      int    len);
  12. void   printrec (
  13.                      char    **fields,
  14.                      FILE    *output,char type );
  15. int    datecmp (
  16.         char *,
  17.         char *);
  18. void   cnvtdate (
  19.         char *,
  20.         struct dt *);
  21.  
  22.  
  23. struct dt {
  24.         int    mo;
  25.         int    dy;
  26.         int    yr;
  27.         };
  28.  
  29. static struct db {            /* record layout */
  30.   char    date[10];            /* date string */
  31.   int       month,day,year;         /* date numerics */
  32.   char    date1[10];            /* time string */
  33.   int       mnth1,day1,yr1;         /* time numerics */
  34.   char    invent[3];             /* inventory report */
  35.   char    purch[3];                /* purchases report */
  36.   char    sales[3];                /* sales report */
  37.   char    all[3];                /* all years */
  38.   char    current[3];            /* current year */
  39.   char    out_file[10];            /* file name if file */
  40.   char    age[6];             /* prior year for prior year report */
  41.   int    old;                /* age numeric */
  42. } rec;
  43.  
  44.  
  45.  
  46. WINDOWPTR wn;                /* window pointer */
  47. WIFORM    frm;                /* form pointer */
  48. char      lmonth[5];
  49. long      total;
  50. int       lines;
  51. char      type_sort;    /* sort types */
  52. int       patrib, uatrib;
  53.  
  54.  
  55. void  print_heading(FILE *,char);
  56. void  print_invent(void);
  57. void  print_db ( Db_Obj     *db,  Index_Obj  *index);
  58.  
  59.  
  60. void print_func (ch)
  61. int ch;
  62. {
  63. char   * emsg1,*emsg2,*emsg3;        /* error msg ptr */
  64. char     fchar;                /* fill character */
  65.  
  66.    switch (ch)
  67.       {
  68.       case 1:
  69.          rec.invent [0] = 'x';
  70.          break;
  71.  
  72.       case 2:
  73.          rec.purch [0] = 'x';
  74.          break;
  75.  
  76.       case 3:
  77.          rec.sales [0] = 'x';
  78.          break;
  79.  
  80.       case 4:
  81.          rec.current [0] = 'x';
  82.          break;
  83.  
  84.       case 5:
  85.          rec.all [0] = 'x';
  86.          break;
  87.  
  88.       case 99:
  89.          return;
  90.          break;
  91.  
  92.       }
  93.    
  94.    wn = wn_open(0,5,10,45,13, patrib, uatrib);
  95.    if(!(wn))
  96.       {                /* errors ?? */
  97.       printf("wn_open failed!!\n");       /* tell of woe... */
  98.       return;                /* and die */
  99.       }
  100.  
  101.  
  102.    wn_title(wn," Inventory Print Options ");
  103.  
  104.   frm = wn_frmopn (4);
  105.   if(!frm) {                /* errors ?? */
  106.     printf("wn_frmopn failed!!\n");     /* tell of woe... */
  107.     return;
  108.   }
  109.  
  110.  
  111.   emsg1 = "Invalid date, press any key to continue...";
  112.   emsg2 = "Invalid time, press any key to continue...";
  113.   emsg3 = "Prior year must be between 1900 and 2050...";
  114.  
  115.   fchar = '_';
  116.   wn_gdate (SET,frm,0, wn,4,1, "Period Beginning Date: ",patrib,fchar,&rec.month,&rec.day,&rec.year,rec.date,NSTR,emsg1);
  117.   wn_gdate (SET,frm,1, wn,5,1, "Period Ending Date:    ",patrib,fchar,&rec.mnth1,&rec.day1,&rec.yr1, rec.date1,NSTR,emsg1);
  118.   wn_gtext (SET,frm,2, wn,7,1, "Output file name: ",patrib,fchar,8,rec.out_file,NSTR,NSTR);
  119.  
  120.   if(!wn_frmget(frm)) {         /* get (read) form */
  121.     v_cls(7);                /* die if errors !! */
  122.     v_locate(0,0,0);
  123.     printf("Memory Corruption Error!\n");
  124.     return;
  125.   }
  126.  
  127.   wn_clr(wn);
  128.   wn_close(wn);             /* close window */
  129.   print_invent();
  130.  
  131.   wn_printf(wn,"\nPress any key to continue..");
  132.   v_getch();
  133.  
  134.   wn_frmcls(frm);            /* close for */
  135.   wn_close(wn);             /* close window */
  136.  
  137.   return;             
  138.  
  139. }
  140.  
  141. /*********************      print_invent      ********************/
  142.  
  143. void  print_invent(void)
  144.  
  145. {
  146.     int    c;
  147.     Db_Obj    *db;
  148.     Index_Obj *index;
  149.     FILE    *output;
  150.    WINDOWPTR wn;               /* window pointer */
  151.    int pagecnt = 1;
  152.    int linecnt = 0;
  153.  
  154.    wn = wn_open(0,5,10,45,13,patrib, uatrib);
  155.    wn_printf(wn,"Outfile is: %s\n", rec.out_file);
  156.     db = iopen_db ("\\gun\\db\\INVENT");
  157.     if (db == NULL)
  158.       {
  159.         iprterr ();
  160.       iclose_db (db);
  161.         return;
  162.       }
  163.  
  164.     index = ihandle (db, "PURCH");
  165.     if (index == NULL)
  166.       {
  167.         iprterr ();
  168.       iclose_db (db);
  169.         return;
  170.       }
  171.  
  172.     for (;;)
  173.       {
  174.       wn_printf (wn,"\r\nMake sure your Printer is online");
  175.        wn_printf (wn,"\r\nPress Return to print to Printer:");
  176.        wn_printf (wn,"\r\nSpace to print to stdout:");
  177.       wn_printf (wn,"\r\nEsc to exit:");
  178.       c = getch ();
  179.       if (c == 0x1b)
  180.          {
  181.          iclose_db (db);
  182. //       wn_close(wn);             /* close window */
  183.             return;
  184.          }
  185.       else if (c == '\r' || c == '\n')
  186.          {
  187.          if ((output = fopen ("prn", "a")) == NULL)
  188.             {
  189.             wn_printf (wn,"\r\nCan't open printer device\n");
  190.             iclose_db (db);
  191.                 return;
  192.             }
  193.  
  194.          wn_printf (wn,"\r\n\r\nPrinting");
  195.             break;
  196.          }
  197.       else if ((c == ' ') && (rec.out_file [0] != ' '))
  198.          {
  199.          if ((output = fopen(rec.out_file, "w+")) == NULL)
  200.             {
  201.             wn_printf(wn,"\r\nCan't open %s.prn",rec.out_file);
  202.             iclose_db (db);
  203.             return;
  204.             }
  205.  
  206.          wn_printf (wn,"\r\nPRINTING to file %s",rec.out_file);
  207.          break;
  208.          }
  209.       }
  210.  
  211.    print_db(db, output);
  212.  
  213.    iclose_db (db);
  214.  
  215.    fprintf (output, "\f");
  216.    fclose (output);
  217.     return;
  218.  
  219. /******************************************************************************
  220.    print_db - print database in index order
  221.  
  222.    Returns no value, but prints to output file
  223. ******************************************************************************/
  224.  
  225.  
  226.  
  227.  
  228. void   print_db (db, output)
  229. Db_Obj   *db;      /* print this database */
  230. FILE   *output;   /* to this output file */
  231. {
  232.    int   status;    /* status from isam functions */
  233.    char   **fields;   /* points to fields in record */
  234.    int   nfields;   /* number of fields per record */
  235.    int   buflen;    /* length of record buffer */
  236.    char   *buf;      /* record buffer */
  237.    char   *newbuf;   /* points to larger record buffer */
  238.    Rec_Len reclen;    /* length of record */
  239.    Index_Obj *index;   /* in order of this index */
  240.    int   use_sd, use_ed, pastend, datesub;        /* counter */
  241.    char    sdate[10];
  242.    char    edate[10];
  243.    char *key[2];
  244.    int linecnt = 0;
  245.    char    rdate[9];
  246.  
  247.    lmonth[0] = '\0';
  248.    nfields = igetfldcount (db);
  249.    fields = (char **) malloc (
  250.       (nfields + 1 /* for NULL */) * sizeof (char *));
  251.    if (fields == NULL) {
  252.       cprintf ("Can't allocate memory\n");
  253.       return;
  254.    }
  255.  
  256.    buflen = 0;
  257.    buf = NULL;
  258.  
  259.  
  260.    type_sort = ' ';
  261.  
  262.    if(rec.invent[0] > ' ')
  263.       {
  264.       index = ihandle (db, "dateIDX");
  265.       if (index == NULL)
  266.          {
  267.          iprterr ();
  268.          return;
  269.          }
  270.  
  271.       datesub = 7;
  272.       type_sort = 'I';
  273.       }
  274.  
  275.    if (rec.purch[0] > ' ')
  276.       {
  277.       index = ihandle (db, "dateIDX");
  278.       if (index == NULL)
  279.          {
  280.          iprterr ();
  281.          return;
  282.          }
  283.  
  284.       datesub = 7;
  285.       type_sort = 'P';
  286.       }
  287.  
  288.    if(rec.sales[0] > ' ')
  289.       {
  290.       index = ihandle (db, "sdateIDX");
  291.       if (index == NULL)
  292.          {
  293.          iprterr ();
  294.          return;
  295.          }
  296.  
  297.       datesub = 11;
  298.       type_sort = 'S';
  299.       }
  300.  
  301.    if(rec.current[0] > ' ')
  302.       {
  303.       index = ihandle (db, "dateIDX");
  304.       if (index == NULL)
  305.          {
  306.          iprterr ();
  307.          return;
  308.          }
  309.  
  310.       datesub = 7;
  311.       type_sort = 'C';
  312.       }
  313.  
  314.    if (rec.all[0] > ' ')
  315.       {
  316.       index = ihandle (db, "makrIDX");
  317.       if (index == NULL)
  318.          {
  319.          iprterr ();
  320.          return;
  321.          }
  322.  
  323.       datesub = 7;
  324.       type_sort = 'M';
  325.       }
  326.  
  327.    if (type_sort == ' ')
  328.       return;
  329.    else
  330.       print_heading(output,type_sort);
  331.  
  332.    if (type_sort != 'M' && rec.month && rec.day && rec.year)
  333.       {
  334.       sprintf(sdate, "%.02d/%.02d/%.02d", rec.year, rec.month, rec.day);
  335.       wn_printf(wn,"\r\n\r\nBegin date: %s", sdate);
  336.       key[0] = sdate;
  337.       key[1] = "";
  338.       ifindkey(db, index, key);
  339.       }
  340.    else
  341.       ifindhead(db, index);
  342.  
  343.    if (type_sort == 'M' && rec.month && rec.day && rec.year)
  344.       {
  345.       sprintf(sdate, "%.02d/%.02d/%.02d", rec.year, rec.month, rec.day);
  346.       use_sd = TRUE;
  347.       }
  348.    else
  349.       use_sd = FALSE;
  350.  
  351.    if (rec.mnth1 && rec.day1 && rec.yr1)
  352.       {
  353.       use_ed = TRUE;
  354.       sprintf(edate, "%.02d/%.02d/%.02d", rec.yr1, rec.mnth1, rec.day1);
  355.       wn_printf(wn,"\r\nEnd date:   %s\n", edate);
  356.       }
  357.  
  358.    else use_ed = FALSE;
  359.  
  360.  
  361.    buflen = 0;
  362.    buf = NULL;
  363.  
  364.    for (status = OK,pastend = FALSE; status == OK && !pastend; status = ifindnext (db, index))
  365.       {
  366.       if (igetreclen (db, index, &reclen) == OK)
  367.          {
  368.          if (reclen > buflen)
  369.             {
  370.             newbuf = enlarge_buf (buf, reclen);
  371.             if (newbuf == NULL)
  372.                continue;
  373.  
  374.             buf = newbuf;
  375.             buflen = reclen;
  376.             }
  377.          }
  378.       else
  379.          {
  380.          iprterr ();
  381.          return;
  382.          }
  383.  
  384.       if (igetrec(db,index,fields,buf,buflen) == OK)   /* exists */
  385.          {
  386.          if (use_sd)
  387.             if (strcmp(sdate, fields[datesub]) > 0)
  388.                continue;
  389.             
  390.          if (use_ed)
  391.             if (strcmp(edate, fields[datesub]) < 0)
  392.                {
  393.                if (type_sort != 'M')
  394.                   pastend = TRUE;
  395.                continue;
  396.                }
  397.  
  398.          // reverse the date from the database
  399.          if (fields[11][0] == '\0')
  400.             rdate [0] = '\0';
  401.          else
  402.             {
  403.             rdate[0] = fields[11][6];
  404.             rdate[1] = fields[11][7];
  405.             rdate[2] = fields[11][2];
  406.             rdate[3] = fields[11][3];
  407.             rdate[4] = fields[11][4];
  408.             rdate[5] = fields[11][5];
  409.             rdate[6] = fields[11][0];
  410.             rdate[7] = fields[11][1];
  411.             rdate[8] = 0;
  412.             }
  413.  
  414.          // print inventory only if sales date does not exist or is after 
  415.          // the ending date
  416.          if (rec.invent[0] != 'x')
  417.             printrec (fields, output, type_sort);
  418.          else if ((rdate[0] == 0) || (strcmp (edate, rdate) < 0))
  419.             printrec (fields, output, type_sort);
  420.  
  421.          } /* if get */
  422.       else
  423.          {
  424.          iprterr();
  425.          return;
  426.          }
  427.       }  /* end for each record in db -- for(status... */
  428.    
  429.    return;
  430.    
  431. } /* print_db */
  432.  
  433.  
  434. /*
  435.    enlarge_buf - realloc buffer to new size len
  436.  
  437.    Returns pointer to bigger buffer, or NULL if out of memory
  438. */
  439.  
  440. char   *enlarge_buf (buf, len)
  441. char   *buf;      /* enlarge this buffer */
  442. int   len;      /* to this size */
  443. {
  444.    if (buf == NULL)
  445.       buf = malloc (len);
  446.    else
  447.       buf = realloc (buf, len);
  448.  
  449.    if (buf == NULL) {
  450.       cprintf ("Can't allocate record buffer\n");
  451.       return (NULL);
  452.    }
  453.    return (buf);
  454. } /* enlarge_buf */
  455. /*
  456.    printrec - print record to output file
  457.  
  458.    Returns no value, but prints to output file
  459. */
  460.  
  461. void   printrec (fields, output,type)
  462. char   **fields;      /* record to print */
  463. FILE   *output;      /* to this output file */
  464. char type;
  465. {
  466.  
  467.    lines++;
  468.    if (lines > 50)
  469.       {
  470.       fprintf (output, "\f");
  471.       print_heading(output,type_sort);
  472.       lines = 1;
  473.       }
  474.       
  475.  
  476. if(lmonth[0] == '\0'&& type == 'C'){
  477.     memcpy(lmonth,fields[7],5);
  478.     total = 0l;
  479.    }
  480.  
  481.  if(lmonth[0] &&  type == 'C'){
  482.   if(memcmp(fields[7],lmonth,5))
  483.     {
  484.     fprintf(output,"       ----------------------------------------------------------------------------------------------------\n");
  485.     fprintf(output,"       TOTAL FOR %.5s %*c %8ld\n\n",lmonth,58,' ',total);
  486.     memcpy(lmonth,fields[7],5);
  487.     total =atol(fields[9]);
  488.      }
  489.    else
  490.     total +=atol(fields[9]);
  491.    }
  492.  fprintf(output,"  %*c%-5.5s    %-10.10s    %-20.20s    %-4.4s    %-8.8s    %-8.8s    %-7.7s    %-15.15s\n",
  493.      5,' ',
  494.       fields[0], 
  495.       fields[1], 
  496.       fields[2], 
  497.       fields[3], 
  498.       fields[4], 
  499.       fields[7], 
  500.       fields[9], 
  501.       fields[11]);
  502.    return;
  503. } /* printrec */
  504.  
  505. /* end prlabels.c */
  506.  
  507.  
  508.  
  509. void print_heading(outfile,h_type)
  510.  FILE        *outfile;
  511.  char        h_type;
  512.  {
  513.   char report_title[25];
  514.   switch(h_type){
  515.    case 'I':
  516.      strcpy(report_title,"    INVENTORY  ");
  517.      break;
  518.    case 'P':
  519.      strcpy(report_title,"    PURCHASES  ");
  520.      break;
  521.    case 'S':
  522.      strcpy(report_title,"    SALES  ");
  523.      break;
  524.     case 'C':
  525.      strcpy(report_title,"   MONTHLY SUMMARY ");
  526.      break;
  527.     case 'M':
  528.      strcpy(report_title,"    MANUFACTURER  ");
  529.      break;
  530.    default:
  531.      strcpy(report_title,"      ");
  532.      break;
  533.     }
  534. fprintf(outfile,"%c",0x0f);           /* compressed print*/
  535. fprintf(outfile,"\n%*c Period Beginning Date: %02d/%02d/%02d %*c\n",
  536.           5,' ',rec.month,rec.day,rec.year,100,' ');
  537. fprintf(outfile,"%*c Period Ending Date   : %02d/%02d/%02d \n",
  538.           5,' ',rec.mnth1,rec.day1,rec.yr1);
  539.  
  540.  
  541. // fprintf(outfile,"$FORM SPSLND;\n");   /*    landscape     */
  542.  fprintf(outfile,"%c",0x0f);           /* compressed print*/
  543.  fprintf(outfile,"\n%*c%c%c%c%s%c%c%c\n\n\n",
  544.           40,' ', 0x0e,0x1b,0x45,report_title,0x1b,0x46,0x14);
  545.  fprintf(outfile,"  %*c%-5.5s    %-10.10s    %-20.20s    %-4.4s    %-8.8s    %-8.8s    %-7.7s    %-15.15s\n",
  546.           5,' ',  "STK #", "MAKER","MODEL", "YEAR", "SERIAL #", "PUR DATE", "COST", "DATE","SALES PR");
  547.  fprintf(outfile,"\n\n");
  548.  }
  549.