/************************************************
* dataBaseFile.c
*
*       This library loads structures
*	reguardless of the version number
*
************************************************/

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fcntl.h>

#include "amiCheck.h"
#include "dataBase.h"
#include "dataBaseFile.h"

/* local routines */
DataConvertOldEntry(UWORD, char *, entryNode *en);

/**********************************************************
*  read in the default tax, net worth variable information 
**********************************************************/
DataReadTaxVars(UWORD magicnum, int fd, struct netInfoVars *netInfo)
{
struct netInfoVarsAAA9 oldnet;
amountType amnt;
UBYTE decim;

 memset(netInfo,0,sizeof(struct netInfoVars));

 /* read tax vars */
 if (magicnum < MAGICNUMOLD3)
	return 0;

 if (magicnum <= MAGICNUMOLD9) {

	decim = usrAccount.decimal;
	usrAccount.decimal = 2;	
	read (fd, &oldnet,sizeof(struct netInfoVarsAAA9));

	DataInitBal(DEPOSITTYPE,(amountType *)&oldnet.currbal,&netInfo->currAmnt);
	DataInitBal(DEPOSITTYPE,(amountType *)&oldnet.statebal,&netInfo->stateAmnt);
	DataInitBal(DEPOSITTYPE,(amountType *)&oldnet.taxdeductstate,&netInfo->taxdeductstate);
	DataInitBal(DEPOSITTYPE,(amountType *)&oldnet.taxdeductcurr,&netInfo->taxdeductcurr);

	netInfo->decimal = 2;
	usrAccount.decimal = decim;
 }
 else 	read (fd, netInfo, sizeof(struct netInfoVars));

 return 0;
}

/****************************************************************
* read in the filter setting structure 
*****************************************************************/
DataReadFilterSetting(UWORD magicnum, int fd, filterSetting *fs)
{
 filterSettingAAA9 filtold;

 memset(fs,0,sizeof(filterSetting));

 if (magicnum <= MAGICNUMOLD9) {
	memset(&filtold,0,sizeof(filterSettingAAA9));

	if (magicnum == MAGICNUMOLD1)
		read(fd,&filtold,sizeof(filterSettingAAA1));
	else 	read (fd,&filtold,sizeof(filterSettingAAA9));

	/* now put in newer form */
	fs->checks = filtold.checks;
	fs->withdrawals = filtold.withdrawals;
 	fs->deposits = filtold.deposits;
	fs->andflag = filtold.andflag;
	fs->notflag = filtold.notflag;
	fs->checkrange = filtold.checkrange;
	fs->checkfrom = filtold.checkfrom;
	fs->checkto = filtold.checkto;
	fs->amntrange = filtold.amntrange;
	fs->amountFrom = filtold.dollarfrom * 100 + filtold.centfrom;
	fs->amountTo = filtold.dollarto * 100 + filtold.centto;
	fs->daterange = filtold.daterange;
	fs->dateFrom.day = filtold.dayfrom;
	fs->dateFrom.month = filtold.monthfrom;
	fs->dateFrom.year = filtold.yearfrom;
	fs->dateTo.day = filtold.dayto;
	fs->dateTo.month = filtold.monthto;
	fs->dateTo.year = filtold.yearto;
	fs->usecategory = filtold.usecategory;
	fs->printcode = filtold.printcode;
	fs->taxcode = filtold.taxcode;
	fs->voidcode = filtold.voidcode;
	fs->reconcilecode = filtold.reconcilecode;
	fs->clearcode = filtold.clearcode;
	fs->usepayee = filtold.usepayee;
	fs->usememo = filtold.usememo;
	strcpy(fs->payee,filtold.payee);
	strcpy(fs->memo,filtold.memo);
	strcpy(fs->payeetoken,filtold.payeetoken);
	strcpy(fs->memotoken,filtold.memotoken);

 }
 else read (fd, fs, sizeof(filterSetting));

 return 0;
}

/******************************************************
* read in the search control structure 
******************************************************/
DataReadSearchControl(UWORD magicnum, int fd, searchState *ss)
{
 searchStateAAA9 old;

 memset(ss,0,sizeof(searchState));

 if (magicnum <= MAGICNUMOLD9) {
	read (fd, &old, sizeof(searchStateAAA9));

	/* now copy it over */
	ss->check = old.check;
	ss->checknum = old.checknum;
	ss->memo = old.memo;
	strcpy(ss->memostr,old.memostr);
	strcpy(ss->memostrtoken,old.memostrtoken);
	ss->name = old.name;
	strcpy(ss->namestr,old.namestr);
	strcpy(ss->namestr,old.namestr);
	ss->amnt = old.amnt;
	ss->amount = old.dollar * 100 + old.cent;
	ss->date = old.date;
	ss->searchdate.day = old.day;
	ss->searchdate.month = old.month;
	ss->searchdate.year = old.year;
	ss->category = old.category;
	strcpy(ss->catstr,old.catstr);
	strcpy(ss->catstrtoken,old.catstrtoken);
 }
 else read (fd,ss,sizeof(searchState));

 return 0;
}

/*******************************************************
* read account information
*******************************************************/
DataReadAccountInfo(UWORD magicnum, int fd, accntState *as)
{
 accntStateAAA9 old;

 memset(as,0,sizeof(accntState));

 if (magicnum <= MAGICNUMOLD9) {
	read(fd,&old,sizeof(accntStateAAA9));
	
	/* copy stuff over */
	strcpy(as->accnt,old.accnt);
	strcpy(as->bank,old.bank);
	strcpy(as->holder,old.holder);

	as->negative = old.negative;
	as->beginAmount = old.begindollar * 100 + old.begincent;
	
	as->openDate.day = old.openday;
	as->openDate.month = old.openmonth;
	as->openDate.year = old.openyear;
	
	as->closed = old.closed;
	as->closeDate.day = old.closeday;
	as->closeDate.month = old.closemonth;
	as->closeDate.year = old.closeyear;

	as->checknumber = old.checknumber;

	as->decimal = 2;
 }
 else read(fd,as,sizeof(accntState));

 return 0;
}

/**********************************************************
* read in the template structures 
***********************************************************/
DataReadTemplate(UWORD magicnum, int fd, templateNode *tn)
{
 templateNodeAAA9 old;

 memset(tn,0,sizeof(templateNode));
 
 if (magicnum <= MAGICNUMOLD9) {
	read(fd,&old,sizeof(templateNodeAAA9));

	/* move over */
	strcpy(tn->tempname,old.tempname);
	DataConvertOldEntry(magicnum,(char *)&old.template,
				&tn->template);
 }
 else  {
	read (fd,tn->tempname,TEMPNAMESIZE);
	DataReadEntry(magicnum,fd,&tn->template);
 }

 return 0;
}

/**********************************************************
* read in entry item node
***********************************************************/
DataReadEntry(UWORD magicnum, int fd, entryNode *en)
{
 entryNodeAAA9 entry;
 struct packitAAA9 oldout;
 struct packit newout;
 UBYTE catsize;
 amountType budget;

 if (magicnum <= MAGICNUMOLD9) {
	if (magicnum <= MAGICNUMOLD4) {
		read(fd,&entry,sizeof(entryNodeAAA9));
	}
	else {
	 	/* read in basics */
 		read(fd,&oldout,sizeof(struct packitAAA9));
 		entry.type = oldout.type;
 		entry.check = oldout.check;
 		entry.flags = oldout.flags;
 		entry.amnt_dollar = oldout.dollar;
 		entry.amnt_cent = oldout.cent; 
 		entry.year = oldout.year;
 		entry.day = oldout.day;
 		entry.month = oldout.month;
 
 		/* read in strings */
 		memset(entry.name,0,STRINGSIZE);
 		memset(entry.memo,0,STRINGSIZE);
 		memset(entry.category,0,CATNAMESIZE);
 		read(fd,entry.name,oldout.namesize);
 		read(fd,entry.memo,oldout.memosize);
 		read(fd,entry.category,oldout.catsize);
	}

	DataConvertOldEntry(MAGICNUMOLD9,(char *)&entry,en);
 }
 else  {
	read(fd,&newout,sizeof(struct packit));

	/* fill in entry fields */
	en->type = newout.type;
	en->check = newout.check;
	en->flags = newout.flags;
	memcpy(&en->amount,&newout.amount,sizeof(amountType));
	memcpy(&en->date,&newout.date,sizeof(dateType));

	/* read in strings */
	memset(en->name,0,STRINGSIZE);
	memset(en->memo,0,STRINGSIZE);
	read(fd,en->name,newout.namesize);
	read(fd,en->memo,newout.memosize);

	/* now read in the category - always one category right now */
	memset(en->category,0,CATNAMESIZE);
	read(fd,&catsize,sizeof(UBYTE));
	read(fd,en->category,catsize);
	read(fd,&budget,sizeof(amountType));
 }

 return 0;
}

/*********************************************************
* read the state variables
***********************************************************/
DataReadStateVars(UWORD magicnum, int fd, struct stateVars *sv)
{
 struct stateVarsAAA9 old;
 amountType oldamnt;
 
 memset(sv,0,sizeof(struct stateVars));

 if (magicnum <= MAGICNUMOLD9) {
	read(fd,&old.currDollar,sizeof(old.currDollar));
	read(fd,&old.currCent,sizeof(old.currCent));
	read(fd,&old.currTemplate,sizeof(old.currTemplate));
	read(fd,&old.stateDollar,sizeof(old.stateDollar));
	read(fd,&old.stateCent,sizeof(old.stateCent));
	read(fd,&old.currCheckNumber,sizeof(old.currCheckNumber));

 	if (magicnum < MAGICNUMOLD3) {
		read (fd, &oldamnt, sizeof(amountType));
 		read (fd, &oldamnt, sizeof(amountType));
 	}

	/* now move over */
	usrAccount.decimal = 2; /* just make sure */
	oldamnt =  old.currDollar * 100 + old.currCent;
	DataInitBal(DEPOSITTYPE,&oldamnt,&sv->currAmnt);

	oldamnt =  old.stateDollar * 100 + old.stateCent;
	DataInitBal(DEPOSITTYPE,&oldamnt, &sv->stateAmnt);

	sv->currTemplate = old.currTemplate;
	sv->currCheckNumber = old.currCheckNumber;
	
 }
 else read (fd,sv,sizeof(struct stateVars));

 return 0;
}

/********************************************************
* convert an old entrynode to a current one
*********************************************************/
DataConvertOldEntry(UWORD magicnum, char *old, entryNode *en)
{
 entryNodeAAA9 *olde = (entryNodeAAA9 *)old;
 memset(en,0,sizeof(entryNode));

 /* currently only AAA9 is needed */
 en->type = olde->type;
 en->check = olde->check;
 strcpy(en->name,olde->name);
 strcpy(en->memo,olde->memo);
 en->date.day = olde->day;
 en->date.month = olde->month;
 en->date.year = olde->year;
 en->amount = olde->amnt_dollar * 100 + olde->amnt_cent;
 en->flags = olde->flags;
 strcpy(en->category,olde->category);

 return 0;
}
