|
/* -------------------------------------------------------------------------------- |
* |
* Programma di esempio per la registrazione di una scrittura in partita doppia |
* cassa a banca utilizzando le funzioni della libreria SPPSERV.DLL. |
* Prima di utilizzare questo esempio è necessario modificare alcune informazioni |
* in modo da adattare il codice alla base dati utilizzata. |
* |
* In questo esempio l'esercizio di lavoro e' considerato corrispondente all'anno |
* solare in corso e le date impostate per default a quella attuale. |
* |
* Questo esempio e' compilato come applicazione Windows 32bit, in quanto utilizza |
* librerie, tra cui la SppServ.dll, che sono esplicitamente di tipo Win32. |
* |
* QUESTO SORGENTE E' FORNITO A TITOLO DI ESEMPIO COME DIMOSTRAZIONE DELL'UTILIZZO |
* DELLA LIBRERIA SPPSERV.DLL. NESSUNA GARANZIA, NE' IMPLICITA NE' ESPLICITA, E' |
* PREVISTA PER QUALSIASI UTILIZZO DIVERSO DA QUELLO INDICATO. |
* |
-------------------------------------------------------------------------------- */ |
using System; |
using System.IO; |
using System.Collections.Generic; |
using System.Text; |
using System.Runtime.InteropServices; |
|
namespace SppServTest2 |
{ |
class Program |
{ |
//costante booleana che, se settata a true, abilita l'uso di Start v.3 |
const bool STARTSUITE = false; |
const string MY_LIBRARY_NAME = "sppserv.dll"; |
const int SPPSRV_SUCCESS = 0; |
|
static bool IsSppServInit = false; |
|
[DllImport("user32.dll", SetLastError = false)] |
static extern IntPtr GetDesktopWindow(); |
|
// prototipi delle funzioni della sppserv.dll |
[DllImport(MY_LIBRARY_NAME, SetLastError = true)] |
private static extern int SPPSRVVersion(); |
[DllImport(MY_LIBRARY_NAME, SetLastError = true)] |
private static extern int SPPSRVInit(IntPtr mainwnd); |
[DllImport(MY_LIBRARY_NAME, SetLastError = true)] |
private static extern int SPPSRVExit(); |
[DllImport(MY_LIBRARY_NAME, SetLastError = true)] |
private static extern int SPPSRVComuniConnect([MarshalAs(UnmanagedType.LPStr)] |
StringBuilder comuniconnectstring); |
[DllImport(MY_LIBRARY_NAME, SetLastError = true)] |
private static extern int SPPSRVDittaConnect([MarshalAs(UnmanagedType.LPStr)] |
StringBuilder dittaconnectstring); |
[DllImport(MY_LIBRARY_NAME, SetLastError = true)] |
private static extern int SPPSRVSetEsercizio([MarshalAs(UnmanagedType.LPStr)] |
StringBuilder esercizio); |
[DllImport(MY_LIBRARY_NAME, SetLastError = true)] |
private static extern int SPPSRVSetEuro(bool IsEuro); |
[DllImport(MY_LIBRARY_NAME, SetLastError = true)] |
private static extern int SPPSRVSetUtente([MarshalAs(UnmanagedType.LPStr)] |
StringBuilder utente); |
[DllImport(MY_LIBRARY_NAME, SetLastError = true)] |
private static extern int SPPSRVSetTodayDate([MarshalAs(UnmanagedType.LPStr)] |
StringBuilder todaydate); |
[DllImport(MY_LIBRARY_NAME, SetLastError = true)] |
private static extern int SPPSRVActivateTransaction(); |
[DllImport(MY_LIBRARY_NAME, SetLastError = true)] |
private static extern int SPPSRVGetNum([MarshalAs(UnmanagedType.LPStr)] |
StringBuilder code); |
[DllImport(MY_LIBRARY_NAME, SetLastError = true)] |
private static extern int SPPSRVInitMovcoTransaction(); |
[DllImport(MY_LIBRARY_NAME, SetLastError = true)] |
private static extern int SPPSRVEndMovcoTransaction(); |
[DllImport(MY_LIBRARY_NAME, SetLastError = true)] |
private static extern int SPPSRVSetMovcoField([MarshalAs(UnmanagedType.LPStr)] |
StringBuilder fldname, [MarshalAs(UnmanagedType.LPStr)] StringBuilder value); |
[DllImport(MY_LIBRARY_NAME, SetLastError = true)] |
private static extern int SPPSRVAppendMovcoRecord(); |
[DllImport(MY_LIBRARY_NAME, SetLastError = true)] |
private static extern int SPPSRVCheckMovcoRecord(); |
[DllImport("sppserv.dll", SetLastError = true)] |
private static extern int SPPSRVGetMovcoField([MarshalAs(UnmanagedType.LPStr)] |
StringBuilder fldname, [MarshalAs(UnmanagedType.LPStr)] StringBuilder value, int valuelen); |
|
static void Main(string[] args) |
{ |
/* |
* §: ESEMPIO di registrazione di una scrittura in partita doppia |
*/ |
|
int rc = 0; |
StringBuilder MovcoNum = new StringBuilder(16); |
Console.Clear(); |
Console.WriteLine("Test inserimento scrittura cassa a banca..."); |
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); |
string msgfilepath = assembly.CodeBase; |
if (msgfilepath.IndexOf("file:///") >= 0) |
msgfilepath = msgfilepath.Substring(8, msgfilepath.Length - 8); |
msgfilepath = System.IO.Path.GetDirectoryName(msgfilepath); |
//Verifica della presenza della SppServ.dll e delle sue dipendenze |
if (!File.Exists(msgfilepath + "\\sppserv.dll")) |
Console.WriteLine("Libreria SppServ.dll non trovata"); |
else if (!File.Exists(msgfilepath + "\\xnmba423.dll")) |
Console.WriteLine("Libreria xnmba423.dll non trovata"); |
else if (!File.Exists(msgfilepath + "\\Sx32w.dll")) |
Console.WriteLine("Libreria Sx32w.dll non trovata"); |
else if (!File.Exists(msgfilepath + "\\hasp_windows_90347.dll")) |
Console.WriteLine("Libreria Hasp_Windows_90347.dll non trovata"); |
else if (!File.Exists(msgfilepath + "\\sppmsg.ita")) |
Console.WriteLine("File dei messaggi SppMsg.ita non trovato"); |
else |
{ |
// inizializzazione della DLL |
try |
{ |
if ((rc = SPPSRVInit(GetDesktopWindow())) != SPPSRV_SUCCESS) |
{ |
Console.WriteLine("Inizializzazione SppServ.dll fallita. SPPSRVInit ha tornato: " + |
rc.ToString()); |
goto FINE; |
} |
else |
{ |
IsSppServInit = true; |
// numero di versione della dll |
int sppver = SPPSRVVersion(); |
Console.WriteLine("Inizializzazione SppServ.dll eseguita con successo." + |
" Versione libreria (valore di ritorno di SPPSRVVersion): " + sppver.ToString()); |
} |
} |
catch (Exception ex) |
{ |
Console.WriteLine("Errore generico inizializzazione SppServ.dll: " + ex.Message); |
goto FINE; |
} |
// connessione al database dei dati comuni e dei dati ditta |
if (STARTSUITE) |
{ |
// versione per SIGLA StartSuite (archivi dui tipo DBF) |
rc = SPPSRVComuniConnect(new StringBuilder("C:\\SIGLAPP\\PROVGEN")); |
Console.WriteLine("SPPSRVComuniConnect ha tornato: " + rc.ToString()); |
if (rc != SPPSRV_SUCCESS) goto FINE; |
rc = SPPSRVDittaConnect(new StringBuilder("C:\\SIGLAPP\\PROVDIT")); |
Console.WriteLine("SPPSRVDittaConnect ha tornato: " + rc.ToString()); |
if (rc != SPPSRV_SUCCESS) goto FINE; |
} |
else |
{ |
// versione per SIGLA (accesso ai dati via ODBC) |
rc = SPPSRVComuniConnect(new StringBuilder("DSN=SPPGEN;UID=sigla;PWD=sigla")); |
Console.WriteLine("SPPSRVComuniConnect ha tornato: " + rc.ToString()); |
if (rc != SPPSRV_SUCCESS) goto FINE; |
rc = SPPSRVDittaConnect(new StringBuilder("DSN=SPPDIT;UID=sigla;PWD=sigla")); |
Console.WriteLine("SPPSRVDittaConnect ha tornato: " + rc.ToString()); |
if (rc != SPPSRV_SUCCESS) goto FINE; |
rc = SPPSRVActivateTransaction(); |
Console.WriteLine("SPPSRVActivateTransaction ha tornato: " + rc.ToString()); |
} |
// impostazione dell'esercizio di lavoro |
rc = SPPSRVSetEsercizio(new StringBuilder("2012")); |
Console.WriteLine("SPPSRVSetEsercizio ha tornato: " + rc.ToString()); |
// impostazione dell'utente |
rc = SPPSRVSetUtente(new StringBuilder("SIGLA ")); |
Console.WriteLine("SPPSRVSetUtente ha tornato: " + rc.ToString()); |
// impostazione della data |
rc = SPPSRVSetTodayDate(new StringBuilder("20120412")); |
Console.WriteLine("SPPSRVSetTodayDate ha tornato: " + rc.ToString()); |
// impostazione dell'euro come valuta di lavoro |
rc = SPPSRVSetEuro(true); |
Console.WriteLine("SPPSrvSetEuro ha tornato: " + rc.ToString()); |
// inizializzazione della transazione |
rc = SPPSRVInitMovcoTransaction(); |
Console.WriteLine("SPPSRVInitMovcoTransaction ha tornato: " + rc.ToString()); |
if (rc == SPPSRV_SUCCESS) |
{ |
// solo dopo l'inizializzazione può essere letto il valore del campo NUMERO |
// della tabella MOVCO (sarà automaticamente utilizzato anche per MOVIVA) |
rc = SPPSRVGetMovcoField(new StringBuilder("NUMERO"), MovcoNum, 16); |
Console.WriteLine("SPPSRVGetMovcoField NUMERO ha tornato: " + rc.ToString()); |
} |
// registrazione del primo record di MOVCO |
Console.WriteLine("Registrazione del record sulla cassa..."); |
SetMovcoField("DATAREG", "20120412"); |
SetMovcoField("ESEREGISTR", "2012"); |
SetMovcoField("DATACOMPET", "20120412"); |
SetMovcoField("DTCOMPCONT", "20120412"); |
SetMovcoField("ESECOMPET", "2012"); |
SetMovcoField("SOTTOCONTO", "CASSA "); |
SetMovcoField("CONTROPART", "1270108003"); |
SetMovcoField("C_F", "A"); |
SetMovcoField("SEGNO", "D"); |
// imposta la causale contabile |
SetMovcoField("CAUSALE", "PGF"); |
SetMovcoField("EIMPORTO", "100.00"); |
// imposta il campo CASO a "prima nota non iva" |
SetMovcoField("CASO", "4"); |
// controllo e inserimento del record |
rc = SPPSRVCheckMovcoRecord(); |
Console.WriteLine("SPPSRVCheckMovcoRecord ha tornato: " + rc.ToString()); |
rc = SPPSRVAppendMovcoRecord(); |
Console.WriteLine("SPPSRVAppendMovcoRecord ha tornato: " + rc.ToString()); |
// registrazione del secondo record di MOVCO |
// (i campi impostati non vengono resettati da SPPSRVAppendNovcoRecord) |
Console.WriteLine("Registrazione del record sulla banca..."); |
SetMovcoField("SOTTOCONTO", "1270108003"); |
SetMovcoField("CONTROPART", "CASSA "); |
SetMovcoField("SEGNO", "A"); |
SetMovcoField("EIMPORTO", "100.00"); |
// controllo e inserimento del record |
rc = SPPSRVCheckMovcoRecord(); |
Console.WriteLine("SPPSRVCheckMovcoRecord ha tornato: " + rc.ToString()); |
rc = SPPSRVAppendMovcoRecord(); |
Console.WriteLine("SPPSRVAppendMovcoRecord ha tornato: " + rc.ToString()); |
// termine della transazione |
rc = SPPSRVEndMovcoTransaction(); |
Console.WriteLine("SPPSRVEndMovcoTransaction ha tornato: " + rc.ToString()); |
Console.WriteLine("Registrazione eseguita MOVCO.NUMERO = '" + MovcoNum.ToString() + "'"); |
} |
FINE: |
// resetta la DLL |
if (IsSppServInit) |
{ |
if ((rc = SPPSRVExit()) == SPPSRV_SUCCESS) |
Console.WriteLine("Deinizializzazione SppServ.dll eseguita con successo"); |
Console.WriteLine("SPPSRVExit ha tornato: " + rc.ToString()); |
} |
Console.WriteLine("... premere <INVIO> per terminare..."); |
Console.ReadKey(); |
} |
|
static void SetMovcoField(string fldname, string value) |
{ |
int rc; |
|
rc = SPPSRVSetMovcoField(new StringBuilder(fldname), new StringBuilder(value)); |
if (rc != SPPSRV_SUCCESS) |
Console.WriteLine("SPPSRVSetMovcoField " + fldname + " ha tornato: " + rc.ToString()); |
} |
} |
} |