|
/* -------------------------------------------------------------------------------- |
* |
* Programma di esempio per la registrazione di una fattura di vendita 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 SppServTest |
{ |
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(MY_LIBRARY_NAME, SetLastError = true)] |
private static extern int SPPSRVSetMovIvaField([MarshalAs(UnmanagedType.LPStr)] StringBuilder fldname, |
[MarshalAs(UnmanagedType.LPStr)] StringBuilder value); |
[DllImport(MY_LIBRARY_NAME, SetLastError = true)] |
private static extern int SPPSRVAppendMovIvaRecord(); |
[DllImport("sppserv.dll", SetLastError = true)] |
private static extern int SPPSRVCheckMovIvaRecord(); |
[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 fattura di vendita |
* Il codice che segue realizza la registrazione di una fattura di vendita. |
* Viene gestita anche l’acquisizione del numero di protocollo iva. |
*/ |
|
int rc = 0; |
StringBuilder MovcoNum = new StringBuilder(16); |
Console.Clear(); |
Console.WriteLine("Test inserimento fattura di vendita..."); |
|
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=pgSPPGEN;UID=sigla;PWD=sigla")); |
Console.WriteLine("SPPSRVComuniConnect ha tornato: " + rc.ToString()); |
if (rc != SPPSRV_SUCCESS) goto FINE; |
rc = SPPSRVDittaConnect(new StringBuilder("DSN=pgSPPDIT;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()); |
// acquisizione del protocollo iva dal numeratore V1 (associato alla causale |
// contabile utilizzata); per la ft. di vendita si utilizza sia come protocollo |
// che come numero documento (N.B.: SPPSRVGetNum esegue automaticamente una Commit) |
int nprot = SPPSRVGetNum(new StringBuilder("V1")); |
Console.WriteLine("SPPSRVGetNum ha tornato: " + nprot.ToString()); |
if (nprot < 0) goto FINE; |
// memorizzazione del numero acquistito in formato ASCII |
string numeroprot = string.Format("{0:D7}", nprot); |
string numerodoc = string.Format("{0:D10}", nprot); |
// 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 record sul cliente (1o record di MOVCO) |
Console.WriteLine("Registrazione del record sul cliente..."); |
SetMovcoField("DATAREG", "20120412"); |
SetMovcoField("ESEREGISTR", "2012"); |
SetMovcoField("DATACOMPET", "20120412"); |
SetMovcoField("DTCOMPCONT", "20120412"); |
SetMovcoField("ESECOMPET", "2012"); |
SetMovcoField("SOTTOCONTO", "DELTA "); |
SetMovcoField("C_F", "C"); |
SetMovcoField("SEGNO", "D"); |
// imposta la causale contabile |
SetMovcoField("CAUSALE", "COR"); |
SetMovcoField("EIMPORTO", "121.00"); |
SetMovcoField("EIMPFATTUR", "121.00"); |
// imposta data e numero del documento |
SetMovcoField("DATAPROTOC", "20120412"); |
SetMovcoField("DATADOCUM", "20120412"); |
SetMovcoField("NUMDOCUM", numerodoc); |
SetMovcoField("NUMPROTOC", numeroprot); |
// imposta il codice del registro IVA (dipende dalla causale contabile) |
SetMovcoField("REGIVA", "CO"); |
// imposta il tipo documento IVA (dipende dalla causale contabile) |
SetMovcoField("TIPODOCIVA", "CO"); |
// imposta il campo CASO a "prima nota iva" |
SetMovcoField("CASO", "0"); |
// 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 record sul ricavo (2o record di MOVCO) |
Console.WriteLine("Registrazione del record sul ricavo..."); |
SetMovcoField("SOTTOCONTO", "VENMAT "); |
SetMovcoField("CONTROPART", "DELTA "); |
SetMovcoField("C_F", "A"); |
SetMovcoField("SEGNO", "A"); |
SetMovcoField("EIMPORTO", "100.00"); |
// i campi non impostati mantengono il valore del record precedente |
// 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 record sull’iva (3o record di MOVCO) |
Console.WriteLine("Registrazione del record sull'iva..."); |
SetMovcoField("SOTTOCONTO", "2370101003"); |
SetMovcoField("SEGNO", "A"); |
SetMovcoField("EIMPORTO", "21.00"); |
SetMovcoField("EIMPORTOIV", "21.00"); |
SetMovcoField("MOV_IVA_SN", "S"); |
// controllo e inserimento del record |
rc = SPPSRVCheckMovcoRecord(); |
Console.WriteLine("SPPSRVCheckMovcoRecord ha tornato: " + rc.ToString()); |
rc = SPPSRVAppendMovcoRecord(); |
Console.WriteLine("SPPSRVAppendMovcoRecord ha tornato: " + rc.ToString()); |
// registra un record su MOVIVA |
Console.WriteLine("Registrazione del record su moviva..."); |
SetMovIvaField("SOTTOCONTO", "DELTA "); |
SetMovIvaField("C_F", "C"); |
SetMovIvaField("TIPODOCIVA", "CO"); |
SetMovIvaField("REGISTRO", "CO"); |
SetMovIvaField("NUMPROTOC", numeroprot); |
SetMovIvaField("NUMERODOC", numerodoc); |
SetMovIvaField("DATADOC", "20120412"); |
SetMovIvaField("DATAREG", "20120412"); |
SetMovIvaField("ETOTFATTUR", "121.00"); |
SetMovIvaField("EIMPONIBIL", "100.00"); |
SetMovIvaField("CODIVA", "21 "); |
SetMovIvaField("EIMPOSTA", "21.00"); |
// controllo e inserimento del record |
rc = SPPSRVCheckMovIvaRecord(); |
Console.WriteLine("SPPSRVCheckMovIvaRecord ha tornato: " + rc.ToString()); |
rc = SPPSRVAppendMovIvaRecord(); |
Console.WriteLine("SPPSRVAppendMovIvaRecord 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()); |
} |
|
static void SetMovIvaField(string fldname, string value) |
{ |
int rc; |
|
rc = SPPSRVSetMovIvaField(new StringBuilder(fldname), new StringBuilder(value)); |
if (rc != SPPSRV_SUCCESS) |
Console.WriteLine("SPPSRVSetMovIvaField " + fldname + " ha tornato: " + |
rc.ToString()); |
} |
} |
} |