/*
** Konfiguration des Rechnungsprogrammes
**
** Copyright (C) 1996 by Stefan Scherer.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for non-commerical use and without fee is hereby granted,
** provided that the above copyright notice appear in all copies and that
** both that copyright notice and this permission notice appear in
** supporting documentation.  This software is provided "as is" without
** express or implied warranty.
*/

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

#include "Config.h"

#define FILENAME_CONFIG "Konfiguration.dat"


/* intern verwaltete Konfigurations-Strings */

static char Firma[MAX_CONF_LEN + 1];
static char Inhaber[MAX_CONF_LEN + 1];
static char Strasse[MAX_CONF_LEN + 1];
static char Ort[MAX_CONF_LEN + 1];
static char Zeichen[MAX_CONF_LEN + 1];
static char BankName[MAX_CONF_LEN + 1];
static char BLZ[MAX_CONF_LEN + 1];
static char Kontonr[MAX_CONF_LEN + 1];
static char PhoneVorwahl[MAX_CONF_LEN + 1];
static char Phone[MAX_CONF_LEN + 1];
static char FaxVorwahl[MAX_CONF_LEN + 1];
static char Fax[MAX_CONF_LEN + 1];

static double MwSt1 = 15.0;
static double MwSt2 = 7.0;

/* 10.4.96, ss: Dies war ein Versuch, MUI-Rechnung auf Bruttoberechnung
** umzuschreiben. Jedoch wurde bisher nichts daraus.
*/

static double MwSt1New = 15.0;	/* MwSt, wenn exakte Bruttobeträge verlangt */
static double MwSt2New = 7.0;

/* die nächsten Modi sind für die Cyclegadgets, besser allgemein:
** Die Darstellung in Netto (=0) oder Brutto (=1).
*/

static LONG orderMwStMode = 1, invoiceMwStMode = 1, articleMwStMode = 1;

/* die nächsten Modi sind für die Cyclegadgets, besser allgemein: Welcher
** Filter wird für die Darstellung der Listen verwendet (0..max-1)
*/

static LONG orderMode = 0, invoiceMode = 0, letterMode = 0;
static LONG bigarticles = 0, moreinvoice = 0, isNetto = 0, LabelLandscape = 0;


static char DVIprinter[MAX_CONF_LEN + 1];
static LONG DVIHoffset;		/* in Millimetern ! */
static LONG DVIVoffset;

static char Kopf1[MAX_CONF_LEN + 1];	/* zwei Zeilen */
static char Kopf2[MAX_CONF_LEN + 1];	/* zwei Zeilen */
static char Fuss1[MAX_CONF_LEN + 1];	/* vier Fußzeilen */
static char Fuss2[MAX_CONF_LEN + 1];	/* vier Fußzeilen */
static char Fuss3[MAX_CONF_LEN + 1];	/* vier Fußzeilen */
static char Fuss4[MAX_CONF_LEN + 1];	/* vier Fußzeilen */

/* 9.5.95: TeX configuration */
static char formatname[24] = "plain";
static char texname[256] = "TeX:bin/VirTeX";
static char previewname[256] = "SYS:rexxc/rx ShowDVI.rexx";
static char printname[256] = "SYS:rexxc/rx DVIprint.rexx";


/* Fehlercode und Fehlerstrings */

static int error;
static const STRPTR errormsg[] =
{
  "",				//   ERR_CONF_OK
   "Fehler beim laden",		//   ERR_CONF_LOAD
   "Fehler beim speichern",	//   ERR_CONF_SAVE
};

/* lade die Konfiguration ein */

int 
CO_LoadConfig (void)
{
  FILE *file;
  int result = ERR_CONF_LOAD;
  STRPTR p;
  char buffer[MAX_CONF_LEN + 1];
  BOOL ok = TRUE;

  if (file = fopen (FILENAME_CONFIG, "r"))
    {
      if (!fgets (Firma, MAX_CONF_LEN, file))
	ok = FALSE;
      if (p = strchr (Firma, '\n'))
	*p = 0;

      if (ok && !fgets (Inhaber, MAX_CONF_LEN, file))
	ok = FALSE;
      if (p = strchr (Inhaber, '\n'))
	*p = 0;

      if (ok && !fgets (Strasse, MAX_CONF_LEN, file))
	ok = FALSE;
      if (p = strchr (Strasse, '\n'))
	*p = 0;

      if (ok && !fgets (Ort, MAX_CONF_LEN, file))
	ok = FALSE;
      if (p = strchr (Ort, '\n'))
	*p = 0;

      if (ok && !fgets (Zeichen, MAX_CONF_LEN, file))
	ok = FALSE;
      if (p = strchr (Zeichen, '\n'))
	*p = 0;

      if (ok && !fgets (BankName, MAX_CONF_LEN, file))
	ok = FALSE;
      if (p = strchr (BankName, '\n'))
	*p = 0;

      if (ok && !fgets (BLZ, MAX_CONF_LEN, file))
	ok = FALSE;
      if (p = strchr (BLZ, '\n'))
	*p = 0;

      if (ok && !fgets (Kontonr, MAX_CONF_LEN, file))
	ok = FALSE;
      if (p = strchr (Kontonr, '\n'))
	*p = 0;

      if (ok && !fgets (PhoneVorwahl, MAX_CONF_LEN, file))
	ok = FALSE;
      if (p = strchr (PhoneVorwahl, '\n'))
	*p = 0;

      if (ok && !fgets (Phone, MAX_CONF_LEN, file))
	ok = FALSE;
      if (p = strchr (Phone, '\n'))
	*p = 0;

      if (ok && !fgets (FaxVorwahl, MAX_CONF_LEN, file))
	ok = FALSE;
      if (p = strchr (FaxVorwahl, '\n'))
	*p = 0;

      if (ok && !fgets (Fax, MAX_CONF_LEN, file))
	ok = FALSE;
      if (p = strchr (Fax, '\n'))
	*p = 0;

      if (ok && !fgets (buffer, MAX_CONF_LEN, file))
	ok = FALSE;
      sscanf (buffer, "%lf", &MwSt1);
      MwSt1New = MwSt1;

      if (ok && !fgets (buffer, MAX_CONF_LEN, file))
	ok = FALSE;
      sscanf (buffer, "%lf", &MwSt2);
      MwSt2New = MwSt2;

      if (ok && !fgets (DVIprinter, MAX_CONF_LEN, file))
	ok = FALSE;
      if (p = strchr (DVIprinter, '\n'))
	*p = 0;

      if (ok && !fgets (buffer, MAX_CONF_LEN, file))
	ok = FALSE;
      DVIHoffset = atol (buffer);

      if (ok && !fgets (buffer, MAX_CONF_LEN, file))
	ok = FALSE;
      DVIVoffset = atol (buffer);

      if (ok && !fgets (Kopf1, MAX_CONF_LEN, file))
	ok = FALSE;
      if (p = strchr (Kopf1, '\n'))
	*p = 0;
      if (ok && !fgets (Kopf2, MAX_CONF_LEN, file))
	ok = FALSE;
      if (p = strchr (Kopf2, '\n'))
	*p = 0;

      if (ok && !fgets (Fuss1, MAX_CONF_LEN, file))
	ok = FALSE;
      if (p = strchr (Fuss1, '\n'))
	*p = 0;
      if (ok && !fgets (Fuss2, MAX_CONF_LEN, file))
	ok = FALSE;
      if (p = strchr (Fuss2, '\n'))
	*p = 0;
      if (ok && !fgets (Fuss3, MAX_CONF_LEN, file))
	ok = FALSE;
      if (p = strchr (Fuss3, '\n'))
	*p = 0;
      if (ok && !fgets (Fuss4, MAX_CONF_LEN, file))
	ok = FALSE;
      if (p = strchr (Fuss4, '\n'))
	*p = 0;

      if (ok && !fgets (buffer, MAX_CONF_LEN, file))
	ok = FALSE;
      if (ok)
	orderMode = atol (buffer);

      if (ok && !fgets (buffer, MAX_CONF_LEN, file))
	ok = FALSE;
      if (ok)
	invoiceMode = atol (buffer);

      if (ok && !fgets (buffer, MAX_CONF_LEN, file))
	ok = FALSE;
      if (ok)
	orderMwStMode = atol (buffer);

      if (ok && !fgets (buffer, MAX_CONF_LEN, file))
	ok = FALSE;
      if (ok)
	invoiceMwStMode = atol (buffer);

      if (ok && !fgets (buffer, MAX_CONF_LEN, file))
	ok = FALSE;
      if (ok)
	articleMwStMode = atol (buffer);

      if (ok && !fgets (buffer, MAX_CONF_LEN, file))
	ok = FALSE;
      if (ok)
	letterMode = atol (buffer);

      if (ok && !fgets (buffer, MAX_CONF_LEN, file))
	ok = FALSE;
      if (ok)
	bigarticles = atol (buffer);

      if (ok && !fgets (buffer, MAX_CONF_LEN, file))
	ok = FALSE;
      if (ok)
	moreinvoice = atol (buffer);

      if (ok && !fgets (buffer, MAX_CONF_LEN, file))
	ok = FALSE;
      if (ok)
	isNetto = atol (buffer);

// isNetto = 0;  /* vorerst doch keine exakten Bruttobeträge */

      /* 16.4.95: set some cycles to "brutto" if exact "brutto" is needed */
      if (isNetto)
	{
	  articleMwStMode = 1;
	  invoiceMwStMode = 1;
	  orderMwStMode = 1;
	  /* bei exakten Bruttobeträgen "alte" Funktionen ausschalten */
	  MwSt1 = 0.0;
	  MwSt2 = 0.0;
	}

      if (ok)
	result = ERR_CONF_OK;

      /* 9.5.95: configuration for TeX calls */
      if (ok && !fgets (buffer, MAX_CONF_LEN, file))
	ok = FALSE;
      if (p = strchr (buffer, '\n'))
	*p = 0;
      if (ok)
	strcpy (formatname, buffer);

      if (ok && !fgets (buffer, MAX_CONF_LEN, file))
	ok = FALSE;
      if (p = strchr (buffer, '\n'))
	*p = 0;
      if (ok)
	strcpy (texname, buffer);

      if (ok && !fgets (buffer, MAX_CONF_LEN, file))
	ok = FALSE;
      if (p = strchr (buffer, '\n'))
	*p = 0;
      if (ok)
	strcpy (previewname, buffer);

      if (ok && !fgets (buffer, MAX_CONF_LEN, file))
	ok = FALSE;
      if (p = strchr (buffer, '\n'))
	*p = 0;
      if (ok)
	strcpy (printname, buffer);

      if (ok && !fgets (buffer, MAX_CONF_LEN, file))
	ok = FALSE;
      if (ok)
	LabelLandscape = atol (buffer);

      if (ok)
	result = ERR_CONF_OK;

      fclose (file);
    }

  return (error = result);
}


/* speichere die Konfiguration im File */

int 
CO_SaveConfig (void)
{
  FILE *file;
  int result = ERR_CONF_SAVE;
  BOOL ok = TRUE;

  if (file = fopen (FILENAME_CONFIG, "w"))
    {
      if (fprintf (file, "%s\n%s\n%s\n%s\n%s\n", Firma, Inhaber, Strasse,
		   Ort, Zeichen) < 0)
	ok = FALSE;

      if (ok && fprintf (file, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n", BankName, BLZ,
			 Kontonr, PhoneVorwahl, Phone, FaxVorwahl, Fax) < 0)
	ok = FALSE;

      if (isNetto)
	{
	  if (ok && fprintf (file, "%.2lf\n%.2lf\n%s\n%ld\n%ld\n", MwSt1New,
	                     MwSt2New, DVIprinter, DVIHoffset, DVIVoffset) < 0)
	    ok = FALSE;
	}
      else
	{
	  if (ok && fprintf (file, "%.2lf\n%.2lf\n%s\n%ld\n%ld\n", MwSt1,
	                     MwSt2, DVIprinter, DVIHoffset, DVIVoffset) < 0)
	    ok = FALSE;
	}

      if (ok && fprintf (file, "%s\n%s\n%s\n%s\n%s\n%s\n", Kopf1, Kopf2,
			 Fuss1, Fuss2, Fuss3, Fuss4) < 0)
	ok = FALSE;

      if (ok && fprintf (file, "%ld  ; order cycle\n%ld  ; invoice cycle\n",
			 orderMode, invoiceMode) < 0)
	ok = FALSE;

      if (ok && fprintf (file, "%ld  ; order mwst\n%ld  ; invoice mwst\n"
                         "%ld  ; article mwst\n", orderMwStMode,
                         invoiceMwStMode, articleMwStMode) < 0)
	ok = FALSE;

      if (ok && fprintf (file, "%ld  ; letter cycle\n",
			 letterMode) < 0)
	ok = FALSE;

      if (ok && fprintf (file, "%ld  ; big articles\n",
			 bigarticles) < 0)
	ok = FALSE;

      if (ok && fprintf (file, "%ld  ; more invoice data\n",
			 moreinvoice) < 0)
	ok = FALSE;

      if (ok && fprintf (file, "%ld  ; without vat\n",
			 isNetto) < 0)
	ok = FALSE;

      /* 9.5.95: configuration for TeX calls */

      if (fprintf (file, "%s\n%s\n%s\n%s\n", formatname, texname,
		   previewname, printname) < 0)
	ok = FALSE;

      if (ok && fprintf (file, "%ld  ; print label in landscape mode\n",
			 LabelLandscape) < 0)
	ok = FALSE;

      if (ok)
	result = ERR_CONF_OK;

      fclose (file);
    }

  return (error = result);
}


/* o-functions */

void 
CO_SetBankName (STRPTR s)
{
  strcpy (BankName, s);
}

void 
CO_SetBLZ (STRPTR s)
{
  strcpy (BLZ, s);
}

void 
CO_SetKontonr (STRPTR s)
{
  strcpy (Kontonr, s);
}

void 
CO_SetPhoneVorwahl (STRPTR s)
{
  strcpy (PhoneVorwahl, s);
}

void 
CO_SetPhone (STRPTR s)
{
  strcpy (Phone, s);
}

void 
CO_SetFaxVorwahl (STRPTR s)
{
  strcpy (FaxVorwahl, s);
}

void 
CO_SetFax (STRPTR s)
{
  strcpy (Fax, s);
}

void 
CO_SetZeichen (STRPTR s)
{
  strcpy (Zeichen, s);
}

void 
CO_SetFirma (STRPTR s)
{
  strcpy (Firma, s);
}

void 
CO_SetInhaber (STRPTR s)
{
  strcpy (Inhaber, s);
}

void 
CO_SetStrasse (STRPTR s)
{
  strcpy (Strasse, s);
}

void 
CO_SetOrt (STRPTR s)
{
  strcpy (Ort, s);
}

void 
CO_SetMwSt1 (double s)
{
  MwSt1 = s;
}

void 
CO_SetMwSt2 (double s)
{
  MwSt2 = s;
}

void 
CO_SetMwSt1New (double s)
{
  MwSt1New = s;
}

void 
CO_SetMwSt2New (double s)
{
  MwSt2New = s;
}

void 
CO_SetOrderMode (LONG s)
{
  orderMode = s;
}

void 
CO_SetInvoiceMode (LONG s)
{
  invoiceMode = s;
}

void 
CO_SetLetterMode (LONG s)
{
  letterMode = s;
}


void 
CO_SetArticleMwStMode (LONG s)
{
  articleMwStMode = s;
}

void 
CO_SetOrderMwStMode (LONG s)
{
  orderMwStMode = s;
}

void 
CO_SetInvoiceMwStMode (LONG s)
{
  invoiceMwStMode = s;
}


void 
CO_SetDVIprinter (STRPTR s)
{
  strcpy (DVIprinter, s);
}

void 
CO_SetDVIHoffset (LONG s)
{
  DVIHoffset = s;
}

void 
CO_SetDVIVoffset (LONG s)
{
  DVIVoffset = s;
}


void 
CO_SetKopf1 (STRPTR s)
{
  strcpy (Kopf1, s);
}				/* zwei Zeilen */

void 
CO_SetKopf2 (STRPTR s)
{
  strcpy (Kopf2, s);
}				/* zwei Zeilen */

void 
CO_SetFuss1 (STRPTR s)
{
  strcpy (Fuss1, s);
}				/* vier Fußzeilen */

void 
CO_SetFuss2 (STRPTR s)
{
  strcpy (Fuss2, s);
}				/* vier Fußzeilen */

void 
CO_SetFuss3 (STRPTR s)
{
  strcpy (Fuss3, s);
}				/* vier Fußzeilen */

void 
CO_SetFuss4 (STRPTR s)
{
  strcpy (Fuss4, s);
}				/* vier Fußzeilen */



/* v-functions */

STRPTR 
CO_GetBankName (void)
{
  return (BankName);
}

STRPTR 
CO_GetBLZ (void)
{
  return (BLZ);
}

STRPTR 
CO_GetKontonr (void)
{
  return (Kontonr);
}

STRPTR 
CO_GetPhoneVorwahl (void)
{
  return (PhoneVorwahl);
}

STRPTR 
CO_GetPhone (void)
{
  return (Phone);
}

STRPTR 
CO_GetFaxVorwahl (void)
{
  return (FaxVorwahl);
}

STRPTR 
CO_GetFax (void)
{
  return (Fax);
}

STRPTR 
CO_GetZeichen (void)
{
  return (Zeichen);
}

STRPTR 
CO_GetFirma (void)
{
  return (Firma);
}

STRPTR 
CO_GetInhaber (void)
{
  return (Inhaber);
}

STRPTR 
CO_GetStrasse (void)
{
  return (Strasse);
}

STRPTR 
CO_GetOrt (void)
{
  return (Ort);
}

double 
CO_GetMwSt1 (void)
{
  return (MwSt1);
}

double 
CO_GetMwSt2 (void)
{
  return (MwSt2);
}

double 
CO_GetMwSt1New (void)
{
  return (MwSt1New);
}

double 
CO_GetMwSt2New (void)
{
  return (MwSt2New);
}

double 
CO_GetMwSt (UWORD preistab)
{
  if (preistab == 1)
    return (MwSt1);
  else
    return (MwSt2);
}

LONG 
CO_GetOrderMode (void)
{
  return (orderMode);
}

LONG 
CO_GetInvoiceMode (void)
{
  return (invoiceMode);
}

LONG 
CO_GetLetterMode (void)
{
  return (letterMode);
}

LONG 
CO_GetArticleMwStMode (void)
{
  return (articleMwStMode);
}

LONG 
CO_GetOrderMwStMode (void)
{
  return (orderMwStMode);
}

LONG 
CO_GetInvoiceMwStMode (void)
{
  return (invoiceMwStMode);
}

LONG 
CO_GetBigArticles (void)
{
  return (bigarticles);
}

LONG 
CO_GetMoreInvoice (void)
{
  return (moreinvoice);
}

LONG 
CO_GetIsNetto (void)
{
  return (isNetto);
}

STRPTR 
CO_GetDVIprinter (void)
{
  return (DVIprinter);
}

LONG 
CO_GetDVIHoffset (void)
{
  return (DVIHoffset);
}

LONG 
CO_GetDVIVoffset (void)
{
  return (DVIVoffset);
}

STRPTR 
CO_GetKopf1 (void)
{
  return (Kopf1);
}				/* zwei Zeilen */

STRPTR 
CO_GetKopf2 (void)
{
  return (Kopf2);
}				/* zwei Zeilen */

STRPTR 
CO_GetFuss1 (void)
{
  return (Fuss1);
}				/* vier Fußzeilen */

STRPTR 
CO_GetFuss2 (void)
{
  return (Fuss2);
}				/* vier Fußzeilen */

STRPTR 
CO_GetFuss3 (void)
{
  return (Fuss3);
}				/* vier Fußzeilen */

STRPTR 
CO_GetFuss4 (void)
{
  return (Fuss4);
}				/* vier Fußzeilen */

/* 2.12.94: Anzahl der Etiketten in den Spalten/Zeilen ermitteln */

ULONG 
CO_GetLabelX (void)
{
  return (3);
}

ULONG 
CO_GetLabelY (void)
{
  if (KeyConfig.LabelFlags & LF_BIGLABEL)
    return (8);
  else
    return (7);
}


/*
** Hole den Fehlerstring
*/

STRPTR 
CO_GetErrorMsg (void)
{
  return (errormsg[error]);
}


/*
** lies das Keyfile ein
**
** anhand dessen können später noch feste Konfigurationen voreingestellt
** werden.
*/

void 
ReadKeyfile (STRPTR buffer)
{
  FILE *in;
  if (in = fopen ("MUI-Rechnung.user", "r"))
    {
      fread (buffer, 80, 4, in);
      fclose (in);
    }
}



/*
** Hole Informationen für die TeX-Ausgabe
*/

STRPTR 
CO_GetFormatName (void)
{
  return (formatname);
}

STRPTR 
CO_GetTeXName (void)
{
  return (texname);
}

STRPTR 
CO_GetPreviewName (void)
{
  return (previewname);
}

STRPTR 
CO_GetPrintName (void)
{
  return (printname);
}

LONG 
CO_LabelLandscape (void)
{
  return (LabelLandscape);
}
