#ifndef __CM_XX_MGA__
#define __CM_XX_MGA__

#ifndef MGAEXT_CARBON_LIB


#ifndef	__LOGLIB__
#include	<loglib.h>
#endif

#ifndef	__XNET__
#ifndef SYD_MAC
#include	<xnelib.h>
#endif
#endif

#else 


// compatibility section START

#define VAL_FIX_LEN		8
#define QTA_FIX_LEN		6
#define PRC_FIX_LEN		4

// stopwatch in milli & microsecondi
#ifdef SYD_WIN
typedef struct Sy2SimpleStopWatchData {
	Sy2UDWord maxtime;
	int active;
	int domicrosec;
	int elapsed;
	LARGE_INTEGER startcount;
	LARGE_INTEGER Frequency;
} Sy2SimpleStopWatchData;
#else
#ifndef SYD_OSX
#include <Timer.h>
#endif
typedef struct Sy2SimpleStopWatchData {
	Sy2UDWord maxtime; //= (60 * 2 * 1000);	// 2 minuti
	int active;
	int domicrosec;
	int elapsed;
	Sy2UDWord accumulated;
	TMTask stopwatch;
} Sy2SimpleStopWatchData;
#endif


typedef Sy2UDWord Sy2TRID;	// transaction ID

typedef struct QUEElem {
	struct QUEElem *qLink;
	short qType;
} QUEElem;

typedef	QUEElem *QUEElemPtr;

typedef struct QUEHeader {
	short qFlags;
	QUEElemPtr qHead;
	QUEElemPtr qTail;
	unsigned short numelem;
} QUEHeader;

#define	QUE_NUMELEM(qhp)	((qhp).numelem)

typedef struct Sy2AsyncHeader {
	QUEElem qelem;
	long result;
} Sy2AsyncHeader;


typedef struct XNETClientRecord {
	unsigned short ServerID;
	unsigned short ClientID;
	unsigned short ClientType;	// regular derivata, estensione, etc.
	unsigned short filler;
	int machineType;	// mac, windows; eventually lower bits indicate byte sex, etc
	int serviceNum;
	int ChanNum;
	long ChanID;
	long IPAddress;
	long portAssigned;
	Sy2DateAndTime start;
	Sy2DateAndTime last;
	Sy2UDWord requests;
	Sy2UDWord failed;
	int flags;
	char key[32];
	char MachineName[32];
	char freeData[32];
} XNETClientRecord;

typedef struct XNETClientEntry {
	short valid;
	union {
		XNETClientRecord rec;
		char buffer[96];
	} u;
} XNETClientEntry;

typedef struct GSRAnswerHeader {
	long size;
	long code;
} GSRAnswerHeader;

#define	GSR_LOGINRESULT_KEY_SIZE			32
#define	GSR_LOGINRESULT_MESSAGE_SIZE		256

typedef struct GSRLoginResult {
	GSRAnswerHeader ah;
	short ChanNum;
	short Port;
	long ChanID;		// Chan ID fornita
	long extraInfo;
	long programFlags;	// in Tibet comunica i moduli attivabili
	char key[GSR_LOGINRESULT_KEY_SIZE];
	char message[GSR_LOGINRESULT_MESSAGE_SIZE];
} GSRLoginResult;

struct clientdatastatus_header {
	int xnet_max_cd;
	int xnet_used_cd;
	int really_used;
};

#ifdef SYD_WIN

#define	FAFILENAMESIZE	64
#define	FADIRNAMESIZE	256
#define	FAFILTERSIZE	64
 /*@}*/

typedef struct FileSpec {
	short vol;
	char dir[FADIRNAMESIZE];
	char name[FAFILENAMESIZE];
} FileSpec;

#else 

typedef struct FileSpec {
	FSSpec fs;
} FileSpec;

#endif

typedef struct Sy2FileStat {
	unsigned long fcredate;
	unsigned long fmoddate;
	long fdatalen;
	long freslen;
	long ftype;
	long fcreator;
	short fflags;
} Sy2FileStat;


#define	GSR_LOCALNAME_QUERY_LEN	(32)
#define	GSR_LOCALNAME_REPLY_LEN	(32)

typedef struct XNETLocalNameServerInfo {
	long valid;
	long serviceID;			// id del servizio
	long IPaddress;			// Address del server
	long connPort;			// porta a cui collegarsi
	long regPort;			// porta a cui registrarsi
	long version;			// versione del server
	long power;				// what I can do flags
	long protocol;			// protocollo usato
	char serverName[32];	// nome del server
	char serverEntity[32];	// nome dell'oggetto servente (p. es. azienda in Tibet)
	unsigned short server_id;			// numero di postazione principale ( utilizzato per il cripting)
} XNETLocalNameServerInfo;

typedef struct XNETLocalNamesReplyData {
	char reply[GSR_LOCALNAME_REPLY_LEN];
	XNETLocalNameServerInfo serverData;
} XNETLocalNamesReplyData;


typedef struct XNETLocalNameServerInfoTable{
	XNETLocalNameServerInfo *p;	//pointer to the start table
	int num;
	int max;
} XNETLocalNameServerInfoTable;

typedef int (*XNETQuerySpinProc)(XNETLocalNameServerInfo *p, long par);

enum {
	BT_VAR_LEN =	255,
	BT_MAXKEY =		100,
	//BT_NUM_BUFS =	8,	// Must be a power of 2
	BT_NUM_BUFS =	64,	// Must be a power of 2
	BT_MAX_NUM_BUFS	=	512,
	//BT_MAX_LEVELS =	16,
	BT_MAX_LEVELS =	32,	// Why not, 071197
	BT_IXB_SIZE =	1024,
	BT_FINFO_SIZE =	128,
	BT_MAX_SUBINDEX =	24,
	BT_CHUNK	 =	10			// Chunk di blocchi per file open
};

typedef struct {
	long	sonptr;				/*  points to data record OR lower leaf */
	long	recptr;				/*  points to data record OR lower leaf */
	long	count;				/*  # of underlying leaf elements       */
} BTENTRYHEADER;

typedef struct  {				/*  entry structure in index        */
	BTENTRYHEADER eh;
	char	key[BT_MAXKEY];		/*  start of record key             */
} BTENTRY;

typedef struct DDSRef {
	HANDLE h;
	long size;
	long offset;
	short flags;
	unsigned short chunksize;
} DDSRef;

// compatibility section END

#endif	/* MGAEXT_CARBON_LIB */


#define SPECIAL_TAG_HAS_CHECKSUM	'cksm'
#define	DO_SYST_CHECKSUM	1


#define MGA_VERSION				0x01070600
#define MGA_VERSION_MASK		0xffffff00	// 0xAABBCCXX  XX puo' cambiare nel client e il server continua ad accettarlo

#ifdef SRVLIB


#define MGA_POOL_SIZE			srv_conf_pgm.hPOSZ
#define MGAMAXCLIENTS			srv_conf_pgm.hNRPD		// number of max pd

#else

#define MGA_POOL_SIZE			cli_conf_pgm.hPOSZ
#define MGAMAXCLIENTS			4		// non SERVE !!!! %T2% number of max pd

#endif

#define MAXVARIANT		5

#define	STD_CONN_FLAGS	(GSR_USE_SPEEDY_CONN + GSR_PUT_HEADER + GSR_DO_CRYPTING)
#define MACOS9_BASE_TCP_CONNECTING_PORT			1135
#define BASE_TCP_CONNECTING_PORT				1132
#define BASE_LOCALTALK_CONNECTING_PORT			65
#define MGA_TCP_OFFSET_MULTIPROGRAMS			50
#define MGA_LOCALTALK_OFFSET_MULTIPROGRAMS		8
#define MGA_TCP_MAX_MULTIPROGRAMS					12	// 1.5.2a
#define MGA_LOCALTALK_MAX_MULTIPROGRAMS					8	// 1.6.1

enum {	// client states
	MGA_CS_NORMAL				= 0,
	MGA_CS_PRINT_ONE_NOTE		= 1
};

#define MGA_AGGIORNA_PRZ_ACQUISTO	0x0001
#define MGA_AGGIORNA_PRZ_VENDITA	0x0002


#define MGA_LOTTI_OBBLIGO_IDENTIFIC	0x0001
#define MGA_LOTTI_OBBLIGO_DATA_SCAD	0x0002
#define MGA_LOTTI_OBBLIGO_DATA_PROD	0x0004


#define MGA_AUTO_EXP_FNAME		"100_auto_export.txt"
#define DBMV_AUTO_EXP_FNAME		"200_carscarico.txt"
#define DBDF_AUTO_EXP_FNAME		"300_docfiscali.txt"
#define DBOR_AUTO_EXP_FNAME		"400_ordclienti.txt"
#define DBAQ_AUTO_EXP_FNAME		"500_ordfornito.txt"
#define DBPN_AUTO_EXP_FNAME		"600_primanota.txt"
#define DBSN_AUTO_EXP_FNAME		"700_scontrini.txt"
#define DBLO_AUTO_EXP_FNAME		"800_lotti.txt"
#define SERVER_INMIX_FNAME		"in_mix.txt"
#define SERVER_INTX_FNAME		"in_tx.txt"

#define DB_IN_FIRST_AZI	90	// max num db in first azienda includes anagrafiche
#define DB_IN_NEXT_AZI	50	// max num db specific for every azienda

#define RECUP_CHECK_NONE	1
#define RECUP_CHECK_STD		2
#define RECUP_CHECK_ALL		3

#define MGA_MSG_DISABLE	0x0001

#define MGA_TRY_TO_USE_NUMINT	-1


enum {
	// MGA Actions
	MGA_DB_CHECK		= 1,
	MGA_DB_ADD			= 2,
	MGA_DB_READ			= 3,
	MGA_DB_WRITE		= 4,
	MGA_DB_DEL			= 5,
	MGA_ARCH_CHECKNEW	= 6,
	MGA_ARCH_CHECK		= 7,
	MGA_ARCH_ADD		= 8,
	MGA_ARCH_READ		= 9,
	MGA_ARCH_WRITE		= 10,
	MGA_ARCH_DEL		= 11,
	MGA_KEYEXISTS		= 12,
	MGA_GET_DBINFO		= 13,
	MGA_GET_NTH_KEY		= 14,
	MGA_GET_NEXT_KEY	= 15,
	MGA_GET_PREV_KEY	= 16,
	MGA_FIND_NEXT_KEY	= 17,
	MGA_GET_DBLIST		= 18,
	MGA_FIND_STARTS		= 19,
	MGA_FIND_CONTAINS	= 20,
	MGA_SCAN_OPEN		= 21,
	MGA_SCAN_ARCH_OPEN	= 22,
	MGA_SCAN_CLOSE		= 23,
	MGA_GET_SYSTATUS	= 24,
	MGA_COMPLETE_FIELDS	= 25,
	MGA_FIND_NEXT_REC	= 26,
	MGA_GET_NTH_REC		= 27,
	MGA_GET_CACHE		= 28,
	MGA_GET_LEFTOVER	= 29,
	MGA_PUT_IN_QUEUE	= 30,
	MGA_DB_ASK_DEL		= 31,
	MGA_ARCH_ASK_DEL	= 32,
	MGA_DATA2DISPLAY	= 33,
	MGA_DB_LOCK			= 34,
	MGA_ARCH_LOCK		= 35,
	MGA_DB_UNLOCK		= 36,
	MGA_ARCH_UNLOCK		= 37,
	MGA_LOGIN			= 38,
	MGA_LOGOUT			= 39,
	MGA_CLIENT_NUM		= 40,
	MGA_CLIENT_DATA		= 41,
	MGA_DB_KILL			= 42,
	MGA_ARCH_KILL		= 43,
	MGA_GET_FIRST_KEY	= 44,
	MGA_FIND_FROM_SD	= 45,
	MGA_LOG_INT_ERR		= 46,
	MGA_GET_CMMNDATA	= 47,
	MGA_AUTHENTICATE_USER	= 48,
	MGA_NEED_USERNM		= 49,
	MGA_ADD_LOG_ENTRY	= 50,
	MGA_CHNG_CMMNFLAG	= 51,
	MGA_ARCH_SOFT_KILL	= 52,
	MGA_KILL_DELETING	= 53,
	MGA_BY_SCAN_FIND	= 54,
	MGA_SEND_MSG		= 55,
	MGA_GET_RESPONSE	= 56,
	MGA_KEYLOCATE		= 57,
	MGA_DOC_FISC_DOAUTO = 58,
	MGA_PRIMANOTA_DOAUTO= 59,
	MGA_PN_KILL_PAROWS	= 60,
	MGA_PN_KILL_FDROWS	= 61,
	MGA_PN_AUTO_DELETE	= 62,
	MGA_DF_AUTO_DELETE	= 63,
	MGA_UPDATE_ULMG		= 64,
	MGA_DECLARE_STATE	= 65,
	MGA_GET_SERVER_STAT	= 66,
	MGA_GET_CLIENT_STAT	= 67,
	MGA_GET_KEYINFO		= 68,
	MGA_GET_DBSTATUS	= 69,
	MGA_KILL_LGAT		= 70,		// !!!! 1.2.2d per ora in attesa di poter fare diversamente
	MGA_CLQUERY			= 71,
	MGA_JOB_EXEC		= 72,
	MGA_UPDATE_ULCF		= 73,
	MGA_SET_AUTOEXP		= 74,
	MGA_REMOVE_CLIENT	= 75,
	MGA_SQL_QUERY_OPEN	= 76,
	MGA_SQL_QUERY_CLOSE	= 77,
	MGA_SQL_NUM_ROWS    = 78,
	MGA_SQL_FETCH_ROW	= 79,
	MGA_SET_JOURNALMODE	= 80,
	MGA_CONFIRM_DOC		= 81,
	MGA_EXTRA_CUSTOM	= 82,
	MGA_READ_NOTES		= 83,
	MGA_WRITE_NOTES		= 84,
	MGA_AUTHENTICATE_SERVER	= 85,
	MGA_READ_CONFIG			= 86,
	MGA_REFRESH_CONFIG		= 87,
	MGA_OPEN_DATAFILE    	= 88,
	MGA_CLOSE_DATAFILE		= 89,
	MGA_CHANGE_ADM_PASS   	= 90,
	MGA_GET_LOG         	= 91,
	MGA_GET_PROGRAM_FLAGS	= 92,
	MGA_CHANGE_LEVELLOG		= 93,
	MGA_DEL_ADMIN_JOUR		= 94,
	//MGA_ALM_NEW				= 95,
	MGA_SEND_ACTKEY		= 96,	// invia la chiave di attivazione al server
	MGA_REQ_ACTKEY    = 97,   // t2config richiede la chiave di attivazione
	MGA_SERVER_STATUS		= 98,	// torna una struttura che descrive lo "stato" del server
	MGA_TEST_PROGRESS		= 99,	// usata per collaudare il meccanismo di feedback per il server che tarda a rispondere
	MGA_DUMP_JOURNAL		= 100,
	MGA_READ_NOTES2		= 101,
	MGA_INTERNAL_CMD	= 102,
	MGA_GENERIC_ACTION	= 0x0100,
	MGA_RECUPIX_ACTION	= 0x0200,
	MGA_LOAD_COMPANY	= 0x0201,
	MGA_UNLOAD_COMPANY	= 0x0202,
	MGA_UPDATING_COMPANY= 0x0203,
	MGA_RESTORE_COMPANY	= 0x0204,
	// MGA_BACKUP_COMPANY	= 0x0205,
	MGA_RAW_MODE		= 0x8000,
	MGA_ACTION_MASK		= 0x0fff
};

#define MAXLEN_MGAPACKET	(MGA_NETBUF_SIZE - sizeof(MGALocalRequest))

enum {
	// various common
	MGA_NETBUF_SIZE		= (1024*8)
};

//#define MGA_DEBUG_MESSAGES	1

#define MGA_MAX_ACT_STRINGS	128
struct decodemgaact {
	char act_string[32];
};


enum {
	MGA_BY_RECID		= 0,
	MGA_BY_KEY			= 1,
	MGA_BY_SCAN_LOCATE	= 2,
	MGA_BY_SCAN_NEXT	= 3
};

enum {
	MGA_OK				= 0,
	MGA_JUSTAPART		= 1,
	MGA_TBPU_DISABLED	= 2,
	MGA_DELFLAG_ONLY	= 3,
	MGA_PLEASEWAIT		= 4,
	MGA_DBNOTFOUND	= -7001,
	MGA_KEYNOTFOUND = -7002,
	MGA_MEMFULL		= -7003,
	MGA_EXCEPTION	= -7004,
	MGA_COMPNOTOPEN	= -7005,
	MGA_QUEUE_EL_NF	= -7006,
	MGA_LOCK_FAILED = -7007,
	MGA_ILL_ACTION	= -7008,
	MGA_USER_ABORT	= -7009,
	MGA_KEY_IS_DEL	= -7010,
	MGA_TOO_MANY_DB	= -7011,	//  aumentare il numero massimo di aziende (conf. programma)
	MGA_NO_RECUPIX	= -7012,
	MGA_PANOTFOUND	= -7013,
	MGA_ERR_PAREAD	= -7014,
	MGA_PADISASTER	= -7015,
	MGA_M2CODEDIFF	= -7016,
	MGA_ERR_FDREAD	= -7017,
	MGA_FDNOTFOUND	= -7018,
	MGA_FDDISASTER	= -7019,
	MGA_BTCHID_NF	= -7020,
	MGA_CRC1_ERR	= -7021,
	MGA_LOGIN_DISABLED 		= -7022,
	MGA_REQ_NOT_FOUND 		= -7023,
	MGA_ILL_STATUS			= -7024,
	MGA_CLIENT_NOTFOUND 	= -7025,
	MGA_NOT_AUTHENTICATED 	= -7026,
	MGA_BUF_TOO_SMALL		= -7027,
	MGA_API_ARE_DISABLED	= -7028,
	MGA_NOEXT_AS_CLIENT		= -7029,
	MGA_NOMORE_EXT_AVAIL	= -7030,
	MGA_DETNOTFOUND			= -7031,
	MGA_JOBREF_NOT_FOUND	= -7032,
	MGA_CLIENT_VERS_DIFF	= -7033,
	MGA_WEB2B_IS_DISABLED	= -7034,
	MGA_FTP_STUFF_INTX_NF	= -7035,
	MGA_FTP_STUFF_INMIX		= -7036,
	MGA_FTP_FILE_EMPTY		= -7037,
	MGA_SQL_PARSE_ERROR		= -7038,
	MGA_SQL_ERR_UPDATE		= -7039,
	MGA_SQL_TOO_MANY_FIELDS	= -7040,
	MGA_SQL_ERR_DELETE		= -7041,
	MGA_SQL_ERR_INSERT		= -7042,
	MGA_SQL_ERR_SELECT  	= -7043,
	MGA_OUT_NUMOFROWS		= -7044,
	MGA_ERR_IN_CONF_LEN		= -7045,
	MGA_ERR_PAWRITE			= -7046,
	MGA_ERR_INCASSOAUTO1	= -7047,
	MGA_ERR_INCASSOAUTO2	= -7048,
	MGA_ERR_ACCESS_DENIED	= -7049,
	MGA_OS9_NOT_SUPPORTED	= -7050,
	MGA_M2CODE_NF			= -7051,	// m2 code not found
	MGA_DSLOCK_ERR			= -7052,
	MGA_ERR_GRANT_RES_DBGET		= -7053,	// errore nella lettura di un record di cui si vogliono controllare eventuali archivi collegati (per il lock)
	MGA_ERR_GRANT_RES_KEY2REC	= -7054,	// errore nella lettura di un record di cui si vuole fare il lock
	MGA_ERR_SERVER_EXPIRED		= -7055,
	MGA_ERR_MODULO_SERVER		= -7056,		// siamo un servizio o un demone e  non abbiamo il modulo server abilitato
	MGA_ERR_VERS_MGACLI			= -7057,
	MGA_ERR_VERS_MGASRV			= -7058,
	MGA_ERR_DATADIC_NOTFOUND	= -7059,
	MGA_ERR_REQ_ABORTED			= -7060,
	MGA_ERR_CLIENT_IS_SP		= -7061,
	MGA_ERR_DECL_ROWS_DIFF		= -7062,		// quando legge un numero di righe diverso da quelle dichiarate in testata
	MGA_ERR_CODE_MISSING		= -7063			// quando si incontra un codice vuoto che invece dovrebbe essere presente
};

enum {
	STS_WRITING_CMMN		= -20001,
	STS_NTGETNOTIF			= -20002,
	STS_PUTR_BTCH			= -20003,
	STS_SCAN_BTCH			= -20004,
	STS_DELR_BTCH			= -20005,
	STS_DOAUTO_DFCANF		= -20006,
	STS_PUTR_VARLIST		= -20007,
	STS_PUTR_VARCODI		= -20008,
	STS_RECUP_ADD_IX		= -20009,
	STS_PUTR_DISTPRES		= -20010,
	STS_PUTR_EF_INSO_PA		= -20011,
	STS_PUTR_PANUEPN		= -20012,
	STS_CONF_FATTDIFF		= -20013,
	STS_CREA_EFFETTI		= -20014,
	STS_PUTREC_RXDU			= -20015,
	STS_PROC_REQ_QUEUE		= -20016,
	STS_IDL1				= -20017,
	STS_IDL2				= -20018,
	STS_CRC_ERR_NOTIF		= -20019,
	STS_PUTR_PANUEMV		= -20020,
	STS_PUTR_PANUEDF		= -20021,
	STS_REQP_IN_NTREQ		= -20022,
	STS_WRITING_SYST		= -20023,
	STS_ELIM_DF_PREGR		= -20024,
	STS_ELIM_OR_TOTEVAS		= -20025,
	STS_ELIM_EF_PREGR		= -20026,
	STS_IDL3				= -20027,
	STS_GEN_ORD_ACQ			= -20028,
	STS_LEN_ERR_NOTIF		= -20029,
	STS_AGG_SIT_ORD1		= -20030,
	STS_AGG_SIT_ORD2		= -20031,
	STS_PNDOAUTO1			= -20032,
	STS_PNDOAUTO2			= -20033,
	STS_ANSWNOTIF1			= -20034,
	STS_ANSWNOTIF2			= -20035,
	STS_IDL4				= -20036,
	STS_MGAPROCREQ1			= -20037,
	STS_MGADESTROY			= -20038,
	STS_MGACREATEREQ		= -20039,
	STS_MGABACKUPAUTO		= -20040,
	STS_DOCFIDOAUTO1		= -20041,
	STS_DOCFIDOAUTO2		= -20042,
	STS_RIPG_GETMG			= -20043,
	STS_RIPG_PUTMG			= -20044,
	STS_RIPG_KILLINGMV		= -20045,
	STS_CREA_TBDP			= -20046,
	STS_CREA_TBLQ			= -20047,
	STS_DOCFIDOAUTO3		= -20048,
	STS_DOCFIDOAUTO4		= -20049,
	STS_INCASSOAUTO1		= -20050,
	STS_INCASSOAUTO2		= -20051,
	STS_KILLPAROWS_FROM_PM	= -20052,
	STS_KILLFDROWS_FROM_PM	= -20053,
	STS_PN_AUTO_DEL1		= -20054,
	STS_PN_AUTO_DEL2		= -20055,
	STS_PN_AUTO_DEL3		= -20056,
	STS_PN_AUTO_DEL4		= -20057,
	STS_PN_AUTO_DEL5		= -20058,
	STS_PNDOAUTO3			= -20059,
	STS_PNDOAUTO4			= -20060,
	STS_PNDOAUTO5			= -20061,
	STS_PNDOAUTO6			= -20062,
	STS_PNDOAUTO7			= -20063,
	STS_PNDOAUTO8			= -20064,
	STS_PNDOAUTO9			= -20065,
	STS_GRANT_PA4PN_1		= -20066,
	STS_GRANT_PA4PN_2		= -20067,
	STS_GRANT_PA4PN_3		= -20068,
	STS_GRANT_CF4DF			= -20069,
	STS_GRANT_MG4DF_1		= -20070,
	STS_GRANT_MG4DF_2		= -20071,
	STS_GRANT_PA4PN_4		= -20072,
	STS_GRANT_PA4PN_5		= -20073,
	STS_GRANT_PA4PN_6		= -20074,
	STS_GRANT_PA4PN_7		= -20075,
	STS_GRANT_PA4PN_8		= -20076,
	STS_GRANT_FD4PN_1		= -20077,
	STS_GRANT_FD4PN_2		= -20078,
	STS_GRANT_FD4PN_3		= -20079,
	STS_GRANT_FD4PN_4		= -20080,
	STS_GRANT_PA			= -20081,
	STS_GRANT_EF			= -20082,
	STS_GRANT_ORAQMG4MV_1	= -20083,
	STS_GRANT_ORAQMG4MV_2	= -20084,
	STS_GRANT_DF			= -20085,
	STS_GRANT_OR_1			= -20086,
	STS_GRANT_OR_2			= -20087,
	STS_GRANT_MG4OR_1		= -20088,
	STS_GRANT_MG4OR_2		= -20089,
	STS_GRANT_AQ_1			= -20090,
	STS_GRANT_AQ_2			= -20091,
	STS_GRANT_MG4AQ_1		= -20092,
	STS_GRANT_MG4AQ_2		= -20093,
	STS_GRANT_AC_1			= -20094,
	STS_GRANT_AC_2			= -20095,
	STS_GRANT_MG4AC_1		= -20096,
	STS_GRANT_MG4AC_2		= -20097,
	STS_CONFIRMDOC1			= -20098,
	STS_CONFIRMDOC2			= -20099,
	STS_FIND_PARTIAL_KEYS	= -20100,
	STS_FIND_FROM_SD		= -20101,
	STS_CONFIRMDOC3			= -20102,
	STS_CREA_EFFETTI2		= -20103,
	STS_PUTR_FIFOLIST		= -20104,
	STS_GETR_FIFOLIST		= -20105,
	STS_PNDOAUTO10			= -20106,
	STS_PUTR_AGG_INFI		= -20107,
	STS_PUTR_AGG_DPAG		= -20108,
	STS_MGA_BTCH_CLEAR_FLAG = -20109,
	STS_RIPSALDI			= -20110,
	STS_PUT_SHOOTING_STAR	= -20111,
	STS_LOCK_SHOOTING_STAR	= -20112
};

enum {
	MGA_REQ_IN_PROCESS	= 0,
	MGA_REQ_PROCESSED	= 1
};

enum {
	MGA_CLIENT			= 0,
	MGA_SERVER			= 1,
	MGA_NONETWORK		= 2,
	MGA_STANDALONE		= 3
};

// journal mode 1.6.2
enum {
	MGA_JOURNAL_DISABLE		= 0,
	MGA_JOURNAL_ENABLE		= 1
};

// mga runtime flags
enum {
	MGARTFL_NEWAZ_MODE		= 0x0001,
	MGARTFL_API_MODE		= 0x0002,
	MGARTFL_ZEROSAREOK		= 0x0004,
	MGARTFL_DISABLEJOURN	= 0x0008,	// 1.6.2
	MGARTFL_CLIENT_MODE		= 0x0020,
	MGARTFL_SA_MODE			= 0x0040,	//stand alone
	MGARTFL_INIT_IN_RPC		= 0x0080	// per evitare che su mac os 9 il client usi GSRLIB
};

enum {		// status flags (SIDX)
	FLAG_RIPSNOK	= 0x00000001,
	FLAG_KLPNNOK	= 0x00000002,
	FLAG_RIPGNOK	= 0x00000004,
	FLAG_KLMVNOK	= 0x00000008
};

enum {
	PANUEFL			= 0x0001,		// doppio esercizio attivato
	FLAG_OK_CONTAB	= 0x0002,		// CONTABFL is OK
	CONTABFL		= 0x0004,		// integrata con contabilita'
	NEED_VESA = 0x00000800,	// need ver saldi
	NEED_VEGI = 0x00000400,	// need ver giac
	NEED_VEOR = 0x00000200	// need ver ordini
};


typedef struct RecSpec {
	long rec_id;
	long count;
} RecSpec;

#define	Flip_RecSpec(p)	FlipDWordInd(&(p).rec_id);\
							FlipDWordInd(&(p).count)

#define	Flip_MGAResponse(p) FlipRecord(&(p),\
					- membersize(MGAResponse, sy2ah),\
					membersize(MGAResponse, code),\
					membersize(MGAResponse, ref),\
					membersize(MGAResponse, resp_len),\
					membersize(MGAResponse, extradata),\
					membersize(MGAResponse, sequence),\
					membersize(MGAResponse, crc32),\
					membersize(MGAResponse, completion_progress), FLIP_END)

#define	Flip_MGALocalRequest(p) FlipRecord(&(p),\
			- membersize(MGALocalRequest, resp),\
			membersize(MGALocalRequest, reqid),\
			membersize(MGALocalRequest, chan_num),\
			membersize(MGALocalRequest, user_id),\
			membersize(MGALocalRequest, name),\
			membersize(MGALocalRequest, azienda),\
			membersize(MGALocalRequest, ref),\
			membersize(MGALocalRequest, action),\
			membersize(MGALocalRequest, mode),\
			membersize(MGALocalRequest, variant),\
			membersize(MGALocalRequest, rec_id),\
			membersize(MGALocalRequest, keyname),\
			membersize(MGALocalRequest, count),\
			membersize(MGALocalRequest, nummasters),\
			membersize(MGALocalRequest, numdetails),\
			membersize(MGALocalRequest, rawdatalen),\
			membersize(MGALocalRequest, mastercardlen),\
			membersize(MGALocalRequest, master2len),\
			membersize(MGALocalRequest, detailcardlen),\
			- (membersize(MGALocalRequest, dbip) + membersize(MGALocalRequest, dbip2) + membersize(MGALocalRequest, dbidetp)),\
			membersize(MGALocalRequest, extramode),\
			membersize(MGALocalRequest, flip2client),\
			- membersize(MGALocalRequest, max_len),\
			membersize(MGALocalRequest, total_len), \
			- membersize(MGALocalRequest, sts), \
			- membersize(MGALocalRequest, tsd), \
			membersize(MGALocalRequest, encoding2client),\
			FLIP_END)

#define	FlipDBKeyLayout(p) FlipRecord(&(p),\
				membersize(DBKeyLayout, nsegs),\
				membersize(DBKeyLayout, flags),\
				membersize(DBKeyLayout, type),\
				membersize(DBKeyLayout, name),\
				membersize(DBKeyLayout, keylen), 0)
				
#define	FlipDBKeySegment(p) FlipRecord(&(p),\
				membersize(DBKeySegment, offset),\
				membersize(DBKeySegment, len),\
				membersize(DBKeySegment, type), 0)


struct greq_item {
	int lockfid;
	long ref;
};

struct grant_req_rec {
	long numreq;
	long lastdbname;
	int lastlockfid;
	int lastazienda;
	int process;
	DDSRef greqh;
};

typedef struct grant_req_rec * MGAGrantedREF;

struct db_buf_per_thread {
	void *bufp;
	void *buf2p;
	void *bufdetp;
	void *mga_clicrd_dp;	// buffer abbastanza grande da contenere una card del client (MGA_CARD_MAXSIZE)
};

typedef struct MGATSData {	// MGA ThreadSafe Data
	DDSRef userdatah;
	DDSRef *saved_details_dhp;
	struct db_buf_per_thread *dbtp;
} MGATSData;

typedef struct MGAReqStatus {
	long queued_at_ticks;		// ticks when received
	long last_answ_at_ticks;		// ticks when last PLEASEWAIT
	int next_status;
	int result;
	//Sy2TRID caller_trid;		// != 0 se chiamata all'interno di una transazione
	struct MGALocalRequest *callerhp;		// punta al MGALocalRequest del caller, caller_trid diventa callerhp->local_trid
	Sy2TRID local_trid;
	unsigned long flags;
	long last_masterref;
	short last_detail;
	MGAGrantedREF grref;
	long internal_result;
	struct MGALocalRequest *internal_hp;		// punta al MGALocalRequest del callee se 
	DDSRef userdatah;
	long foobar;				// a disposizione
	//struct GSRNotification *ntnp;		// se diverso da nil il client sta ancora aspettando la NTAnswer
	DDSRef *saved_details_dhp;	// !!!!! 1.5.9
	int reqtype;
	long reqref;	// puo' contenere un GSRNotification o un XNETNotification
} MGAReqStatus;


typedef struct MGAResponse {
	Sy2AsyncHeader sy2ah;
	int code;
	long ref;
	long resp_len;
	long extradata;
	Sy2UDWord sequence;
	Sy2UDWord crc32;
	long completion_progress;
} MGAResponse;

typedef struct MGALocalRequest {
	MGAResponse resp;
	long reqid;		// request id
	long chan_num;
	long user_id;
	long name;		// dbname
	short azienda;	// azienda attiva
	long ref;		// puo' essere il magazzino attivo
	unsigned short action;
	short mode;
	short variant;
	long rec_id;
	long keyname;
	long count;
	short nummasters;
	short numdetails;
	long rawdatalen;
	long mastercardlen;
	long master2len;
	long detailcardlen;
	struct db_info *dbip;
	struct db_info *dbip2;
	struct db_info *dbidetp;
	short extramode;
	short flip2client;
	long max_len;
	long total_len;
	MGAReqStatus sts;
	MGATSData tsd;
	short encoding2client;
} MGALocalRequest;


typedef struct MGAInfo {
	char name[32];
	short message_len;
} MGAInfo;

struct db_status {
	long dbname;
	long numinfile;
	int timelastmod;
	short is_common;	// giusto per doppio controllo
};

#define	Flip_db_status(p) FlipRecord(&(p),\
					membersize(struct db_status, dbname),\
					membersize(struct db_status, numinfile),\
					membersize(struct db_status, timelastmod),\
					membersize(struct db_status, is_common), FLIP_END)


// header fisso per tutti i record di mga
struct mgarech {
	long hNUMI;		// numero interno
	Sy2Date hDTCR;	// data di creazione
	Sy2Date hDTUM;	// data ultima modifica
	short hWHOM;	// chi ha modificato
	short hUSFL;	// userflags
// ---- i seguenti aggiunti nella versione 1.7.1  -- 06-04-2006
	Sy2Time hORCR;	// ora creazione
	Sy2Time hORUM;	// ora ult. modifica
	short hWHOC;	// chi ha creato
	long hNRWS;		// numero di righe eventualmente abbinate alla testata
};

// header fisso per tutti i record di tipo detail di mga
struct mgadetailrech {
	long hNUMI;		// numero interno header
	short hXXRN;	// row number
};


// time frame is STATSTIMEFRAME

typedef struct MGAStatData {
	unsigned long num_reqs;			// total num of client_req
	unsigned long num_reqs_ok;		// num of client_req that went ok
	unsigned long num_reqs_fail;	// num of client_req that failed
	unsigned long req_min_size;		// min size of client_req
	unsigned long req_max_size;		// max size of client_req
	unsigned long req_prog_size;	// total size of client_reqs
	
	unsigned long proc_min_time;	// min time spent processing last client_req
	unsigned long proc_max_time;	// max time spent processing last client_req
	unsigned long proc_prog_time;	// total time spent processing client_req
	unsigned long last_proc_time;	// time spent processing last client_req
	
	unsigned long curr_tf_numreqs;	// numero client_req in current time frame
	unsigned long curr_tf_numidle;	// numero idle in current time frame
	unsigned long last_tf_numreqs;	// numero client_req in last time frame
	unsigned long last_tf_numidle;	// numero idle       in last time frame
	unsigned long max_reqs_in_tf;	// max    client_req in one  time frame
	
	// unsigned long answ_min_size;	// min size of answer to a client_req 
	// QUESTA e' NOTA : sizeof(MGAResponse)
	unsigned long answ_max_size;	// max size of answer to a client_req
	unsigned long answ_prog_size;	// total size of answers to a client_req
	unsigned long answ_min_time;	// min time spent answering last client_req
	unsigned long answ_max_time;	// max time spent answering last client_req
	unsigned long answ_prog_time;	// total time spent answering client_req
	
	unsigned long curr_tf_net_time;		// tempo dedicato al nettask in current time frame
	unsigned long curr_tf_busy_time;	// tempo dedicato a servire i client in current time frame
	unsigned long last_tf_net_time;		// tempo dedicato al nettask in last time frame
	unsigned long last_tf_busy_time;	// tempo dedicato a servire i client in last time frame
	
	Sy2DateAndTime start;
} MGAServerStats;

typedef struct MGAClientData {
	long current_state;
} MGAClientData;

#define	Flip_MGAClientData(p)	FlipDWordInd(&(p).current_state)

typedef struct MGAClientStats {
	XNETClientEntry clientrec;
	MGAClientData mgadata;
} MGAClientStats;



typedef struct MGASqlRow {
	long numfield;
	DDSRef row;
} MGASqlRow;

struct MGAServerStatus {
	int numclients;
	int lma_cs_status;
	struct clientdatastatus_header cdsh;
	int numcdsdet;

	// seguono numclients XNETClientEntry
	// seguono numcdsdet struct clientdatastatus_detail
};

#define	Flip_MGAServerStatus(p) FlipRecord(&(p),\
					membersize(struct MGAServerStatus, numclients),\
					membersize(struct MGAServerStatus, lma_cs_status),\
					membersize(struct MGAServerStatus, cdsh.xnet_max_cd),\
					membersize(struct MGAServerStatus, cdsh.xnet_used_cd),\
					membersize(struct MGAServerStatus, cdsh.really_used),\
					membersize(struct MGAServerStatus, numcdsdet),\
					FLIP_END)

enum {
	MGACB_WAIT_OPEN				= 0,
	MGACB_WAIT_ADJUST			= 1,
	MGACB_WAIT_CLOSE			= 2,
	MGACB_WAIT_RESET			= 3,
	MGACB_WAIT_SET_MSG_STR		= 4,
	MGACB_GET_STRING			= 5,
	MGACB_ALERT					= 6,
	MGACB_GET_BK_PATH			= 7,
	MGACB_BEFORE_ABORT			= 8,
	MGACB_START_SERVERDELAY		= 9,
	MGACB_STOP_SERVERDELAY		= 10,
	MGACB_LISTEN_SERVERDELAY	= 11,
	MGACB_WAIT_SET_MSG_ID		= 12,
	MGACB_INTERNAL_EXCEPTION	= 13,
	MGACB_CLIENT_STATE_OPEN		= 14,
	MGACB_CLIENT_STATE_ADJUST	= 15,
	MGACB_CLIENT_STATE_CLOSE	= 16,
	MGACB_IDLE					= 17,
	MGACB_CLIENT_EVENT			= 18,
	MGACB_WAIT_STATUS			= 19,
	MGACB_DO_SLEEP				= 20,
	MGACB_CLOSE_CONNECTION		= 21,
	MGACB_RECUP_IX				= 22,
	MGACB_SAVE_CFG				= 23,
	MGACB_GET_PROG_DICTPATH		= 24,
	MGACB_GET_PROG_FILESPEC		= 25,
	MGACB_SET_STRING			= 26,
	MGACB_WRITE_ACT_STUFF		= 27,
	MGACB_GET_BUNDLE_FILESPEC	= 28
};

// id di stringa riservate nella MGACB_GET_STRING
#define	ACT_KEY_STRING_ID	'#act'
#define	USR_KEY_STRING_ID	'#usr'
#define	SN_KEY_STRING_ID	'#snr'

#define		PGM_ACT_KEY_LEN		30
#define		USR_ACT_KEY_LEN		40
#define		SN_ACT_KEY_LEN		16

#define SERVER_ID_PROVVISORIO	39302
#define CLIENT_ID_PROVVISORIO	39303


typedef int (*MGACallBackProc)(int cmd, int mgaaction, long par1, long par2, int wpar);

// dovrebbe esserci un xx_mgajm.h --- start

typedef struct MJMRequest {
	int job_token;
	int async;
	int flags;
	long par1;
	long par2;
	long par3;
	long par4;
	DDSRef answdh;
	FileSpec answfs;
} MJMRequest;

enum {	// Job command tokens
	MJM_CLQUERY			= 1,
	MJM_TOKENQUERY		= 2,
	MJM_PRINTTAB		= 3
};

#define MJMFL_DDSREF_PRESENT	1

// dovrebbe esserci un xx_mgajm.h --- end


enum {	// conf pgm
	// MGAMAXCLIENTS
	MGA_NRPD_MINVAL		= 1,
	MGA_NRPD_MAXVAL		= 128,
	MGA_NRPD_STDVAL		= 16,
	// FLUSHVOLTIME
	MGA_TMFV_MINVAL		= 40,		// in tenths of second
	MGA_TMFV_MAXVAL		= 30000,	// in tenths of second
	MGA_TMFV_STDVAL		= 200,		// in tenths of second
	// BTCHDELTIME
	MGA_TMBD_MINVAL		= 10,		// in tenths of second
	MGA_TMBD_MAXVAL		= 30000,	// in tenths of second
	MGA_TMBD_STDVAL		= 40,		// in tenths of second
	// STATSTIMEFRAME
	MGA_TMST_MINVAL		= 40,		// in tenths of second
	MGA_TMST_MAXVAL		= 30000,	// in tenths of second
	MGA_TMST_STDVAL		= 10,		// in tenths of second
	// BTCHDELNUMCHK
	MGA_NRCK_MINVAL		= 1,		// in tenths of second
	MGA_NRCK_MAXVAL		= 30000,	// in tenths of second
	MGA_NRCK_STDVAL		= 5,
	
	MGA_MIN_FREE_RAM	= (100*1024),
	
	MGA_MXSE_MINVAL		= 4,
	MGA_MXSE_MAXVAL		= 1024,
	MGA_MXSE_STDVAL		= 32,
	MGA_MXCO_MINVAL		= 1,
	MGA_MXCO_MAXVAL		= 512,
	MGA_MXCO_STDVAL		= 3,
	MGA_MCHC_MINVAL		= 4,
	MGA_MCHC_MAXVAL		= 8192,
	MGA_MCHC_STDVAL		= 12,
	MGA_TMUD_MINVAL		= 1,		// in seconds
	MGA_TMUD_MAXVAL		= 30000,	// in seconds
	MGA_TMUD_STDVAL		= 4,		// in seconds

	MGA_TOCN_MINVAL		= 1,		// in seconds
	MGA_TOCN_MAXVAL		= 30000,	// in seconds
	MGA_TOCN_STDVAL		= 19,		// in seconds
	MGA_RTCN_MINVAL		= 1,
	MGA_RTCN_MAXVAL		= 30000,
	MGA_RTCN_STDVAL		= 8,
	MGA_TODA_MINVAL		= 1,		// in seconds
	MGA_TODA_MAXVAL		= 30000,	// in seconds
	MGA_TODA_STDVAL		= 10,		// in seconds

	MGA_QUEUED_REQ_TIME_MINVAL		= 1,		// in 1/60 of second
	MGA_QUEUED_REQ_TIME_MAXVAL		= 30000,	// in 1/60 of second
	MGA_QUEUED_REQ_TIME_STDVAL		= 5,		// in 1/60 of second

	MGA_POOL_SIZE_MINVAL		= (1024*30),
	MGA_POOL_SIZE_MAXVAL		= (10*1024*1024),
	MGA_POOL_SIZE_STDVAL		= (1024*300),
	
	MGA_NUM_POOL_MINVAL		= 8,
	MGA_NUM_POOL_MAXVAL		= 30000,
	MGA_NUM_POOL_STDVAL		= 8,
	
	MGA_CARD_MAXSIZE_MINVAL	= (1024*4),
	MGA_CARD_MAXSIZE_MAXVAL	= (1024*100),
	MGA_CARD_MAXSIZE_STDVAL	= (1024*4),
	
	MGA_MAXLOCK_MINVAL		= 400,
	MGA_MAXLOCK_MAXVAL		= 100000,
	MGA_MAXLOCK_STDVAL		= 1000,
	
	MGA_MAXLOG_MINVAL			= 100,
	MGA_MAXLOG_MAXVAL			= 1024*1024,
	MGA_MAXLOG_STDVAL			= 2000,
	
	MGA_PDREQ_MAXDELAY_MINVAL	= 1,			// in seconds
	MGA_PDREQ_MAXDELAY_MAXVAL	= 30000,			// in seconds
	MGA_PDREQ_MAXDELAY_STDVAL	= 20,			// in seconds
	
	MGA_REQ_WAIT_TIME_MINVAL	= 1,			// in 1/60 of second
	MGA_REQ_WAIT_TIME_MAXVAL	= 30000,		// in 1/60 of second
	MGA_REQ_WAIT_TIME_STDVAL	= 3*60,			// in 1/60 of second
	
	MGA_MIN_FREE_DISK_INKB_MINVAL	= 10,		// minimo spazio libero necessario su disco in Kb
	MGA_MIN_FREE_DISK_INKB_MAXVAL	= 102400,		// minimo spazio libero necessario su disco in Kb
	MGA_MIN_FREE_DISK_INKB_STDVAL	= 100,		// minimo spazio libero necessario su disco in Kb

	MGA_NUMDETAILS_IN_TASK_MINVAL	= 1,
	MGA_NUMDETAILS_IN_TASK_MAXVAL	= 1000,
	MGA_NUMDETAILS_IN_TASK_STDVAL	= 10,

	MGA_SRVR_TASK_TIME_MINVAL		= 0,		// in msecs
	MGA_SRVR_TASK_TIME_MAXVAL		= 30000,	// in msecs
	MGA_SRVR_TASK_TIME_STDVAL		= 25,		// in msecs
	MGA_SRVR_TASK_TIME_MAXUSERVAL	= 500,		// in msecs
	MGA_SRVR_RW_TIMEOUT				= 2000,		// in millisecondi. Timeout per le read sotto osx	
	MGA_DISCONN_TIMEOUT				= 20,		// secondi che il server attende prima di chiudersi
	MGA_DEFAULT_LOG					= 0			// log di default 0 = NORMAL REPORT	1 = DEBUG REPORT	2 = EXTREME REPORT

};

enum {
	// mga flags in conf_pgm
	MGA_FLAG_DOSQZ			= 0x0001,	// 1
	MGA_FLAG_DOLOG			= 0x0002,	// 2
	MGA_FLAG_LOG4DEBUG		= 0x0004,	// 4
	MGA_FLAG_DOLOG1			= 0x0008,	// 8	flag per log di un solo utente

	MGA_FLAG_RESTORE_SYST	= 0x0020,	// 32
	MGA_FLAG_SAMUELE		= 0x0040,	// 64
	MGA_FLAG_NOPREFRECTS	= 0x0080,	// 128 se impostato non usa le preferenze per la pos. finestre
	MGA_FLAG_EASYBYTE		= 0x0100,	// 256
	MGA_FLAG_SUPERVISOR		= 0x0200,	// 512
	MGA_FLAG_DONOTCRIPT		= 0x0400,	// 1024
	MGA_FLAG_LOGTYPE02		= 0x0800,	// 2048
	MGA_FLAG_LOGTYPE03		= 0x1000,	// 4096
	MGA_FLAG_CHECK_EXTEND	= 0x2000,	// 8192
	MGA_FLAG_FORCE_BACKUP	= 0x4000	// 16384
};

enum {	// mga status flags
	STSFL_NEEDS_SPECIAL_DISPOSE	= 0x00000001,
	STSFL_TASK_IS_SUSPENDED		= 0x00000002,
	STSFL_EXTERN_DISPOSE		= 0x00000004,
	STSFL_CLIENTREQ_WAITING		= 0x00000008
};

struct note_key {
	short hNOAZ;
	long hNODB;
	long hNOID;
	short hNONR;
};

struct note_key2 {
	short hNOAZ;
	long hNODB;
	char hNOVC[16];
	short hNONR;
};

//-------------------- backup

#define		MGASQZBUFLEN	(50*1024)

typedef struct StdBackupHeader {
	Sy2UDWord id;					// mettere BKUP
	Sy2UWord  vers;					// mettere zero
	Sy2UWord  fnum;					// mettere 1
} StdBackupHeader;

// seguono fnum files ciascuno preceduto dal suo headerino:

typedef struct StdBFHeader {
	Sy2UDWord lenOfThisHeader;	// per estensioni
	Sy2UDWord typeID;			// tipo di file (0 se unused)
	Sy2UDWord uniqueID;			// unique id (generata random)
	Sy2UDWord compLen;			// lunghezza compattato
	Sy2UDWord realLen;			// lunghezza scompattato
	Sy2DateAndTime when;		// quando e' stato backuppato
	char name[64];				// nome
	Sy2FileStat fstat;			// original file info
	// altro se necessario
} StdBFHeader;


#define	Flip_StdBackupHeader(p) FlipRecord(&(p),\
					membersize(StdBackupHeader, id),\
					membersize(StdBackupHeader, vers),\
					membersize(StdBackupHeader, fnum), FLIP_END)

typedef int (*SQZProgressProc)(long par, long value, long max);





typedef struct CARD_SRCP {
	long hVERS;

	short hMODU;
	short hNWST;
	short hPROT;
	short hTPGM;
	short hBKAU;
	short hNRPD;
	short hNRCK;
	short hTMFV;
	short hTMBD;
	short hTMST;
	short hMXSE;
	short hMXCO;
	short hMCHC;
	short hDUM1;
	short hBJMD;
	short hFLGS;
	long hFXIP;
	short hTMUD;
	short hTOCN;
	short hRTCN;
	short hTODA;
	short hFINT;
	short hSRVR;
	
	long hNTSK;
	long hPOSZ;
	short hNRPO;
	long hCRSZ;
	long hMXLK;
	long hMXLG;
	short hRQDE;
	short hRQWT;
	long hMNKB;
	short hNRDT;
	short hRQTF;

	short hWWWS;
	long hBKHM;
	long hTCPP;
	short hCTMO;
	long hPADE;
	
	
	char hBKPT[512];
	char hDBPT[512];
	char hPUUT[40];
	char hPUKW[16];
	short hAPAU;
	
	int hRWTM;
	int hDSSV;
	int hTYLG;
	int hAGAR;
	short hSOS9;
	
} CARD_SRCP;

#define Flip_CARD_SRCP(p) FlipRecord(&(p),\
	membersize(CARD_SRCP, hVERS),\
	membersize(CARD_SRCP, hMODU),\
	membersize(CARD_SRCP, hNWST),\
	membersize(CARD_SRCP, hPROT),\
	membersize(CARD_SRCP, hTPGM),\
	membersize(CARD_SRCP, hBKAU),\
	membersize(CARD_SRCP, hNRPD),\
	membersize(CARD_SRCP, hNRCK),\
	membersize(CARD_SRCP, hTMFV),\
	membersize(CARD_SRCP, hTMBD),\
	membersize(CARD_SRCP, hTMST),\
	membersize(CARD_SRCP, hMXSE),\
	membersize(CARD_SRCP, hMXCO),\
	membersize(CARD_SRCP, hMCHC),\
	membersize(CARD_SRCP, hDUM1),\
	membersize(CARD_SRCP, hBJMD),\
	membersize(CARD_SRCP, hFLGS),\
	membersize(CARD_SRCP, hFXIP),\
	membersize(CARD_SRCP, hTMUD),\
	membersize(CARD_SRCP, hTOCN),\
	membersize(CARD_SRCP, hRTCN),\
	membersize(CARD_SRCP, hTODA),\
	membersize(CARD_SRCP, hFINT),\
	membersize(CARD_SRCP, hSRVR),\
	membersize(CARD_SRCP, hNTSK),\
	membersize(CARD_SRCP, hPOSZ),\
	membersize(CARD_SRCP, hNRPO),\
	membersize(CARD_SRCP, hCRSZ),\
	membersize(CARD_SRCP, hMXLK),\
	membersize(CARD_SRCP, hMXLG),\
	membersize(CARD_SRCP, hRQDE),\
	membersize(CARD_SRCP, hRQWT),\
	membersize(CARD_SRCP, hMNKB),\
	membersize(CARD_SRCP, hNRDT),\
	membersize(CARD_SRCP, hRQTF),\
	membersize(CARD_SRCP, hWWWS),\
	membersize(CARD_SRCP, hBKHM),\
	membersize(CARD_SRCP, hTCPP),\
	membersize(CARD_SRCP, hCTMO),\
	membersize(CARD_SRCP, hPADE),\
	membersize(CARD_SRCP, hRWTM),\
	membersize(CARD_SRCP, hDSSV),\
	membersize(CARD_SRCP, hTYLG),\
	membersize(CARD_SRCP, hAGAR),\
	-membersize(CARD_SRCP, hBKPT),\
	-membersize(CARD_SRCP, hDBPT),\
	-membersize(CARD_SRCP, hPUUT),\
	-membersize(CARD_SRCP, hPUKW),\
	membersize(CARD_SRCP, hAPAU),\
	membersize(CARD_SRCP, hRWTM),\
	membersize(CARD_SRCP, hDSSV),\
	membersize(CARD_SRCP, hTYLG),\
	membersize(CARD_SRCP, hAGAR),\
	membersize(CARD_SRCP, hSOS9) , FLIP_END);	
	



typedef struct CARD_CLCP {
	long hVERS;
	short hPROT;
	short hFLGS;
	short hTOCN;
	short hRTCN;
	short hTODA;
	long hNTSK;
	long hPOSZ;
	short hNRPO;
	short hRQDE;
	short hRQWT;
	long hTCPP;
	long hPADE;
} CARD_CLCP;


typedef struct CARD_CPGM {
	long hVERS;
	struct CARD__RNT {
		short hTYPE;
		short hPORT;
		short hHMON;
		short hWSTP;
		long hDRVR;
	}	hPRNT[4];
	short hVPRN;
	short hASKB;
	short hBKDE;
	short hMODU;
	short hNWST;
	short hHMOP;
	short hPARS;
	short hSCOR;
	short hIVAI;
	short hFBOP;
	short hOBLI;
	short hSTPE;
	short hPORE;
	short hMPLU;
	short hTIMV;
	short hUNIF;
	short hPROT;
	short hAUPS;
	short hTPGM;
	short hBKAU;
	short hNRPD;
	short hNRCK;
	short hTMFV;
	short hTMBD;
	short hTMST;
	short hMXSE;
	short hMXCO;
	short hMCHC;
	short hDUM1;
	short hBJMD;
	short hFLGS;
	long hFXIP;
	short hTMUD;
	short hTOCN;
	short hRTCN;
	short hTODA;
	short hFINT;
	short hSRVR;
	long hNTSK;
	long hPOSZ;
	short hNRPO;
	long hCRSZ;
	long hMXLK;
	long hMXLG;
	short hRQDE;
	short hRQWT;
	long hMNKB;
	short hNRDT;
	short hRQTF;
	short hCOLC;
	short hCPIN;
	short hCMOD;
	short hCFRE;
	short hCPRT;
	short hCFLG;
	short hCACM;
	short hWWWS;
	long hBKHM;
	long hTCPP;
	short hCTMO;
	short hCOAL;
	char hMFCD[4];
	long hPADE;
	short hDRMF;
	short hFIWI;
	char hCACS[120];
	long hCAFL;
	char hBKPT[512];
	char hDBPT[512];
	char hPUUT[40];
	char hPUKW[16];
	short hAPAU;
	long hRWTM;
	long hDSSV;
	long hTYLG;
	long hAGAR;
	short hSOS9;
	long hFLG2;
} CARD_CPGM;


enum {
	// various
	MGA_BASENUMSCAN		= 4,
	MGA_SERVER_ID		= 0x1000,
	MGA_INTERNAL_PID	= 0x1001,
	MGA_FLAG_PU_DISABLED = 0x0001,
	MGA_FLAG_SIX2_GENERIC	= 0x0002,		// usato nella 1.0.2 per forzare il recup ix settato in tutte le aziende
	MGA_FLAG_PAST104D 		= 0x0004,		// usato nella 1.0.5 per forzare il check delle reg. CEE
	MGA_FLAG_PAST104F 		= 0x0008,		// usato nella 1.0.5 per aggiornare il tipo gestione IVA nella TBCC
	MGA_FLAG_JUST_UPD 		= 0x0010,		// usato per sapere se e' una azienda appena aggiornata
	MGA_FLAG_PAST109b5 		= 0x0020,		// usato nella 1.0.9b6 per aggiornare il segno delle paperte da NC
	MGA_FLAG_EUROREADY 		= 0x0040,		// ex past119: usato nella 1.2 per aggiornare i valori in #EURO
	MGA_FLAG_PAST122F 		= 0x0080,		// usato nella 1.2.4 per aggiornare i valori in #EURO FASE 3
	MGA_FLAG_PAST124B 		= 0x0100,		// usato nella 1.2.4c per aggiornare NUMI in DBCF
	MGA_FLAG_PAST140D 		= 0x0200,		// usato nella 1.5.0 per aggiornare da TBGS a DBAC
	MGA_FLAG_PAST152B 		= 0x0400,		// usato nella 1.6.0 per aggiornare campi temporanei in SYST
	MGA_FLAG_PAST166 		= 0x0800,		// usato nella 1.6.7 per aggiornare lo "stato" dei docfisc
	MGA_FLAG_PAST168 		= 0x1000,		// usato nella 1.7.0 per aggiornare le note
	MGA_FLAG_PAST170 		= 0x2000,		// usato nella 1.7.1 per aggiornare varie cose (ad esempio i depositi)
	MGA_FLAG_PAST172 		= 0x4000,		// usato nella 1.7.4 per aggiornare le banche con IT come codice paese
	MGA_FLAG_PAST175 		= 0x8000,		// usato nella 1.7.6 per richiedere l'eventuale import raggr. cee
	MGA_FLAG_PAST176 		= 0x00010000,	// usato nella 1.7.7 per aggiornare varie cose (ad esempio i clienti)
	MGA_STDFL_CMMN_CHANGED	= 0x0001
};

#define MGA_NUM_IS_OFFSET	0
#define MGA_NUM_IS_CHANNUM	1

#define MGA_FLAG_SAVE_BUFTHREAD		0x0001

void mga_set_bundle_filespec(FileSpec *fsp);
void mga_set_pool_size(long sz);

MGALocalRequest *mga_new_req(register long hsize, struct db_buf_per_thread *buflistp);
void mga_init_req(MGALocalRequest *reqp, MGALocalRequest *mgahp);
MGALocalRequest *mga_init_new_req(MGALocalRequest *mgahp, struct db_buf_per_thread *buflistp);
void mga_copy_header(MGALocalRequest *reqp, MGALocalRequest *mgahp, int flags);
int mga_copy_req(MGALocalRequest *dreqp, MGALocalRequest *sreqp, int flags);
void mga_dump_core_data(void *datap, int datalen, char *commentstr);
int mga_dispose_req(MGALocalRequest *mgahp, int status);
void mga_reset_req(MGALocalRequest *reqp, MGALocalRequest *mgahp);

char *mga_getRAWDataPtr(MGALocalRequest *hp);
char *mga_getMasterPtr(MGALocalRequest *hp);
char *mga_getDetailPtr(MGALocalRequest *hp);

MGALocalRequest *mga_appendRAWData2Req(MGALocalRequest *hp, void *bufp, long bufsize);
MGALocalRequest *mga_appendData2Req(MGALocalRequest *hp, void *bufp, long bufsize);

int mga_get_current_protocol(void);

#endif	/* __CM_XX_MGA__ */
