git-svn-id: svn://10.65.10.50/branches/R_10_00@22920 c028cbd2-c16b-5b4b-a496-9718f37d4682
		
			
				
	
	
		
			778 lines
		
	
	
		
			26 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			778 lines
		
	
	
		
			26 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
| #include <applicat.h>
 | ||
| #include <automask.h>
 | ||
| #include <progind.h>
 | ||
| #include <recarray.h>
 | ||
| #include <reputils.h>
 | ||
| #include <textset.h>
 | ||
| 
 | ||
| #include <nditte.h>
 | ||
| #include <anagr.h>
 | ||
| #include <comuni.h>
 | ||
| 
 | ||
| #include "../mg/codcorr.h"
 | ||
| #include "../cg/cglib01.h"
 | ||
| #include "../ve/velib.h"
 | ||
| 
 | ||
| #include "halib.h"
 | ||
| 
 | ||
| #include "ha2.h"
 | ||
| #include "ha2200a.h"
 | ||
| 
 | ||
| ///////////////////////////////////////////////////////////
 | ||
| // TAutomask
 | ||
| ///////////////////////////////////////////////////////////
 | ||
| class THardy_esselunga_fat_mask : public TAutomask
 | ||
| {
 | ||
| protected:
 | ||
|   virtual bool on_field_event(TOperable_field& o, TField_event e, long jolly);
 | ||
| 
 | ||
| public:
 | ||
|   THardy_esselunga_fat_mask();
 | ||
| 	virtual ~THardy_esselunga_fat_mask() {};
 | ||
| };
 | ||
| 
 | ||
| THardy_esselunga_fat_mask::THardy_esselunga_fat_mask() : TAutomask ("ha2200a")
 | ||
| {
 | ||
| }
 | ||
| 
 | ||
| bool THardy_esselunga_fat_mask::on_field_event(TOperable_field& o, TField_event e, long jolly)
 | ||
| { 
 | ||
|   return true;
 | ||
| }
 | ||
| 
 | ||
| /////////////////////////////////////////////////////////////
 | ||
| //	Recordset per file fatture
 | ||
| /////////////////////////////////////////////////////////////
 | ||
| 
 | ||
| // AN = alfanumerico
 | ||
| // NU = numerico zero filled
 | ||
| // NS = numerico zerofilled con segno
 | ||
| // DA = data
 | ||
| enum TFE_type { AN, NU, NS, NN, DA };
 | ||
| 
 | ||
| 
 | ||
| class TFatture_recordset : public TAS400_recordset
 | ||
| {
 | ||
| protected:
 | ||
| 	void add_field(const char* name, TFE_type type, int len, int pos,
 | ||
| 		             bool required = false, const char* def = NULL);
 | ||
| public:
 | ||
|   bool set(const char* name, const TVariant& value);
 | ||
|   virtual TRecnotype new_rec(const char* trc);
 | ||
| 	TFatture_recordset(bool nuovo);
 | ||
| };
 | ||
| 
 | ||
| TRecnotype TFatture_recordset::new_rec(const char* trc)
 | ||
| {
 | ||
| 	TRecnotype rec = -1;
 | ||
|   if (trc && *trc > ' ')
 | ||
| 	{
 | ||
|     rec = TAS400_recordset::new_rec(trc);
 | ||
| 		TToken_string& line = row(rec);
 | ||
| 		const int acapo = line.rfind('\n');
 | ||
| 		if (acapo>0)
 | ||
| 			line.cut(acapo+1);
 | ||
| 	}
 | ||
|   return rec;
 | ||
| }
 | ||
| 
 | ||
| bool TFatture_recordset::set(const char* name, const TVariant& value)
 | ||
| {
 | ||
| 	bool ok = false;
 | ||
| 	if (!value.is_empty())
 | ||
| 	{
 | ||
| 		if (value.is_date()) 
 | ||
| 		{
 | ||
| 			TString8 str; str.format("%08ld", value.as_int());
 | ||
| 			ok = TAS400_recordset::set(name, str);
 | ||
| 		}
 | ||
| 		else 
 | ||
|     {
 | ||
| 			if (value.is_real())
 | ||
| 			{
 | ||
| 				const real r = value.as_real()*1000;
 | ||
| 				TString16 str = r.string(16,0,'0');
 | ||
| 				if (r>ZERO)
 | ||
| 					str.overwrite("+",0,1);
 | ||
| 				else
 | ||
|         {
 | ||
| 				  if (r<ZERO)
 | ||
| 					  str.overwrite("-",0,1);
 | ||
| 				  else
 | ||
| 					  str.overwrite(" ",0,1);
 | ||
|         }
 | ||
| 	      ok = TAS400_recordset::set(name, str);
 | ||
| 			}
 | ||
| 			else
 | ||
| 				ok = TAS400_recordset::set(name, value);
 | ||
|     }
 | ||
| 	}
 | ||
|   return ok;
 | ||
| }
 | ||
| 
 | ||
| void TFatture_recordset::add_field(const char* name, TFE_type type, int len, int pos,
 | ||
| 									                 bool required, const char* def)
 | ||
| {	
 | ||
|   CHECKS(len > 0, "Lunghezza nulla sul campo ", name);
 | ||
|   CHECKS(pos >= 0, "Posizione non valida sul campo ", name);
 | ||
|   
 | ||
|   TFieldtypes ft = _alfafld;
 | ||
|   switch (type)
 | ||
|   {
 | ||
|   case NU:
 | ||
|     ft = _longzerofld;
 | ||
|     break;
 | ||
|   case DA:
 | ||
|     CHECKS(len == 8, "Lunghezza incoerente data ", name);
 | ||
|     ft = _longzerofld;
 | ||
|     break;
 | ||
|   default:
 | ||
|     ft = _alfafld; 
 | ||
|     break;
 | ||
|   }
 | ||
|   bool ok = false;
 | ||
|   if (def && *def)
 | ||
|     ok = create_field(name, pos-1, len, ft, required, TVariant(def));
 | ||
|   else
 | ||
|     ok = create_field(name, pos-1, len, ft, required);
 | ||
|   CHECKS(ok, "Impossibile creare il campo ", (const char*)name);
 | ||
| }
 | ||
| 
 | ||
| TFatture_recordset::TFatture_recordset(bool nuovo)
 | ||
| : TAS400_recordset("AS400(612,3,0)")
 | ||
| {
 | ||
| 	// dati di configurazione
 | ||
| 	TConfig config(CONFIG_DITTA, "ha");
 | ||
| 	const TString80 piva_hardy = config.get("Esselunga_PIvaHardy");
 | ||
| 	const TString8 tipo_emissione = config.get("Esselunga_TipoEmissione");
 | ||
| 	const long codcli_esselunga = config.get_long("Esselunga_CodEsselunga");
 | ||
| 	const TString80 cod_hardy = config.get("Esselunga_CodHardy");
 | ||
| 	TString16 piva_esselunga = config.get("Esselunga_PIvaEsselunga");
 | ||
| 
 | ||
|   if (nuovo)
 | ||
|     piva_esselunga << "N";  // 27-09-2012
 | ||
| 
 | ||
| 	// record cliente Esselunga
 | ||
|   TToken_string key;
 | ||
| 	key.add("C"); key.add(codcli_esselunga);
 | ||
| 	const TRectype& cliente_esselunga = cache().get(LF_CLIFO, key);
 | ||
| 	TString4 stato = cliente_esselunga.get(CLI_STATOCF);
 | ||
|   TString4 comune = cliente_esselunga.get(CLI_COMCF);
 | ||
| 	key.cut(0);
 | ||
| 	key.add(stato); key.add(comune);
 | ||
|   const TRectype& com_esselunga = cache().get(LF_COMUNI, key);
 | ||
| 
 | ||
| 	// ditta
 | ||
| 	const TFirm& ditta = prefix().firm();
 | ||
| 	// record anag
 | ||
|   const char tipoa = ditta.get(NDT_TIPOA)[0];
 | ||
|   const long codanagr = ditta.get_long(NDT_CODANAGR);
 | ||
| 	key.cut(0);
 | ||
|   key.add(tipoa); key.add(codanagr);
 | ||
|   const TRectype& anag = cache().get(LF_ANAG, key);
 | ||
|   
 | ||
| 	// record anagiu
 | ||
| 	key.cut(0);
 | ||
|   key.add(codanagr);
 | ||
|   const TRectype& anagiu = cache().get(LF_ANAGGIU, key);
 | ||
| 
 | ||
| 	// record comuni per hardy
 | ||
| 	stato = anag.get(ANA_STATORES);
 | ||
|   comune = anag.get(ANA_COMRES);
 | ||
| 	key.add(stato, 0); key.add(comune, 1);
 | ||
|   const TRectype& comres = cache().get(LF_COMUNI, key);
 | ||
| 
 | ||
| 	// *** Sezione di intestazione fattura ***
 | ||
| 
 | ||
| 	// Record BGM: nome documento e numero fattura
 | ||
| 	add_field("BGM.TIPOREC",				AN,  3,   1, true, "BGM");
 | ||
| 	add_field("BGM.ID-EDI-MITT-1",	AN, 35,   4, true, piva_hardy);
 | ||
| 	add_field("BGM.ID-EDI-MITT-2",	AN,  4,  39, true, tipo_emissione);
 | ||
| 	add_field("BGM.ID-EDI-MITT-3",	AN, 14,  43);
 | ||
| 	add_field("BGM.ID-EDI-DEST-1",	AN, 35,  57, true, piva_esselunga);
 | ||
| 	add_field("BGM.ID-EDI-DEST-2",  AN,  4,  92, true, tipo_emissione);
 | ||
| 	add_field("BGM.ID-EDI-DEST-3",	AN, 14,  96);
 | ||
| 	add_field("BGM.TIPODOC",				AN,  6, 110, true);
 | ||
| 	add_field("BGM.NUMDOC",					AN, 35, 116, true);
 | ||
| 	add_field("BGM.DATADOC",				DA,  8, 151, true);
 | ||
| 	add_field("BGM.ORADOC",					AN,  4, 159);
 | ||
| 	add_field("BGM.FILLER",					AN,  6, 163);
 | ||
| 	add_field("BGM.FINERECORD",			AN,  2,   0, true, "\r\n");
 | ||
| 
 | ||
| 	TString80 str;
 | ||
| 	str << anag.get(ANA_INDRES) << ", " << anag.get(ANA_CIVRES);
 | ||
| 
 | ||
| 	// Record NAS: identificazione mittente
 | ||
| 	add_field("NAS.TIPOREC",				AN,  3,   1, true, "NAS");
 | ||
| 	add_field("NAS.CODFORN",				AN,  17,  4, true, cod_hardy);
 | ||
| 	add_field("NAS.QCODFORN",				AN,  3,  21, true, "92");
 | ||
| 	add_field("NAS.RAGSOCF",				AN, 70,  24, true, ditta.ragione_sociale());
 | ||
| 	add_field("NAS.INDIRF",					AN, 70,  94, true, str);
 | ||
| 	add_field("NAS.CITTAF",					AN, 35, 164, true, comres.get(COM_DENCOM));
 | ||
| 	add_field("NAS.PROVF",					AN,  9, 199, true, comres.get(COM_PROVCOM));
 | ||
| 	add_field("NAS.CAPF",						AN,  9, 208, true, anag.get(ANA_CAPRES));
 | ||
| 	stato = anag.get(ANA_STATORES);
 | ||
| 	if (stato.empty())
 | ||
| 		stato = "IT";
 | ||
| 	add_field("NAS.NAZIOF",					AN,  3, 217, true, stato);
 | ||
| 	add_field("NAS.PIVANAZF",				AN, 35, 220, true, anag.get(ANA_PAIV));
 | ||
| 	add_field("NAS.TRIBUNALE",			AN, 35, 255, true, "MILANO");
 | ||
| 	add_field("NAS.LICIMPEXP",			AN, 35, 290);
 | ||
| 	add_field("NAS.CCIAA",					AN, 35, 325, true, ditta.get(NDT_NISCRAA));
 | ||
| 	add_field("NAS.CAPSOC",					AN, 35, 360, true, anagiu.get("CAPSOC"));
 | ||
| 	add_field("NAS.CODFISC",				NU, 16, 395, true, anag.get(ANA_COFI));
 | ||
| 	add_field("NAS.PIVAINT",				AN, 35, 430);
 | ||
| 	add_field("NAS.TELEFONO",				AN, 25, 465);
 | ||
| 	add_field("NAS.TELEFAX",				AN, 25, 490);
 | ||
| 	add_field("NAS.TELEX",					AN, 25, 515);
 | ||
| 	add_field("NAS.EMAIL",					AN, 70, 540);
 | ||
| 	add_field("NAS.FINERECORD",			AN,  2,   0, true, "\r\n");
 | ||
| 
 | ||
| 	str.cut(0);
 | ||
| 	str << cliente_esselunga.get(CLI_INDCF) << ", " << cliente_esselunga.get(CLI_CIVCF);
 | ||
| 
 | ||
| 	// Record NAI: identificazione destinatario
 | ||
| 	add_field("NAI.TIPOREC",				AN,  3,   1, true, "NAI");
 | ||
| 	add_field("NAI.CODFATT",				AN, 17,   4, true, cliente_esselunga.get(CLI_PAIV)); 
 | ||
| 	add_field("NAI.QCODFATT",				AN,  3,  21, true, "VA");
 | ||
| 	add_field("NAI.RAGSOCI",				AN, 70,  24, true, cliente_esselunga.get(CLI_RAGSOC));
 | ||
| 	add_field("NAI.INDIRI",					AN, 70,  94, true, str);
 | ||
| 	add_field("NAI.CITTAI",					AN, 35, 164, true, com_esselunga.get(COM_DENCOM));
 | ||
| 	add_field("NAI.PROVI",					AN,  9, 199, true, com_esselunga.get(COM_PROVCOM));
 | ||
| 	add_field("NAI.CAPI",						AN,  9, 208, true, cliente_esselunga.get(CLI_CAPCF));
 | ||
| 	stato = cliente_esselunga.get(CLI_STATOCF);
 | ||
| 	if (stato.empty())
 | ||
| 		stato = "IT";
 | ||
| 	add_field("NAI.NAZIOI",					AN,  3, 217, true, stato);
 | ||
| 	add_field("NAI.PIVANAZI",				AN, 35, 220, true, cliente_esselunga.get(CLI_PAIV));
 | ||
| 	add_field("NAI.FILLER",					AN, 51, 255);
 | ||
| 	add_field("NAI.FINERECORD",			AN,  2,   0, true, "\r\n");
 | ||
| 
 | ||
| 	// Record FTX: divisa di pagamento
 | ||
| 	add_field("FTX.TIPOREC",				AN,  3,   1, true, "FTX");
 | ||
| 	add_field("FTX.DIVISA",				  AN,  3,   4, true, "EUR");
 | ||
| 	add_field("FTX.NOTE",				    AN,350,   7);
 | ||
| 	add_field("FTX.FINERECORD",			AN,  2,   0, true, "\r\n");
 | ||
| 
 | ||
| 	// Record PAT: condizioni di pagamento standard
 | ||
| 	add_field("PAT.TIPOREC",				AN,  3,   1, true, "PAT");
 | ||
| 	add_field("PAT.TIPOCONDC",			AN,  3,   4, true, "10E");
 | ||
| 	add_field("PAT.DATASCAD",				DA,  8,   7);
 | ||
| 	add_field("PAT.RIFTERMP",				AN,  3,  15, true, "5");
 | ||
| 	add_field("PAT.RELTERMP",				AN,  3,  18);
 | ||
| 	add_field("PAT.UNTEMP",				  AN,  3,  21, true, "D");
 | ||
| 	add_field("PAT.NUNTEMP",				AN,  3,  24); // alfanumerico per non zerofillarlo
 | ||
| 	add_field("PAT.IMPORTO",				NS, 16,  27);
 | ||
| 	add_field("PAT.DIVISA",			  	AN,  3,  43);
 | ||
| 	add_field("PAT.PERC",						AN,  7,  46); // alfa per non zerofilled
 | ||
| 	add_field("PAT.DESCRIZ",				AN, 35,  53, true);
 | ||
| 	add_field("PAT.BANCACOD",				AN, 35,  88);
 | ||
| 	add_field("PAT.BANCADESC",			AN, 35, 123);
 | ||
| 	add_field("PAT.FACTOR",					AN, 35, 158);
 | ||
| 	add_field("PAT.CODPAG",					AN,  3, 193);
 | ||
| 	add_field("PAT.MEZZOPAG",				AN,  3, 196);
 | ||
| 	add_field("PAT.FINERECORD",			AN,  2,   0, true, "\r\n");
 | ||
| 
 | ||
| 	// *** Sezione di dettaglio fattura ***
 | ||
| 
 | ||
| 	// Record DET: identificazione, quantit<69> e prezzi articolo
 | ||
| 	add_field("DET.TIPOREC",				AN,  3,   1, true, "DET");
 | ||
| 	add_field("DET.NUMRIGA",				NU,  6,   4, true);
 | ||
| 	add_field("DET.IDSOTTOR",				AN,  3,  10);
 | ||
| 	add_field("DET.NUMSRIGA",				AN,  6,  13); // AN perche' e' un campo che non c'e' mai e non deve essere zerofilled
 | ||
| 	add_field("DET.CODEANCU",				AN, 35,  19, true);
 | ||
| 	add_field("DET.TIPCODCU",				AN,  3,  54, true, "EN");
 | ||
| 	add_field("DET.CODEANTU",				AN, 35,  57);
 | ||
| 	add_field("DET.CODFORTU",				AN, 35,  92);
 | ||
| 	add_field("DET.CODDISTU",				AN, 35, 127);
 | ||
| 	add_field("DET.TIPQUANT",				AN,  3, 162); // L01=Vendita L02=Reso
 | ||
| 	add_field("DET.QTACONS",				NS, 16, 165);
 | ||
| 	add_field("DET.UDMQCONS",				AN,  3, 181, true, "PCE");
 | ||
| 	add_field("DET.QTAFATT",				NS, 16, 184);
 | ||
| 	add_field("DET.UDMQFATT",				AN,  3, 200, true, "PCE");
 | ||
| 	add_field("DET.NRCUINTU",				NS, 16, 203, true);
 | ||
| 	add_field("DET.UDMNRCUINTU",		AN,  3, 219, true, "PCE");
 | ||
| 	add_field("DET.PRZUNI",					NS, 16, 222, true);
 | ||
| 	add_field("DET.TIPORPZ",				AN,  3, 238, true, "AAB"); // prezzo lordo
 | ||
| 	add_field("DET.UDMPRZUN",				AN,  3, 241, true, "PCE");
 | ||
| 	add_field("DET.PRZUN2",					NS, 16, 244, true);
 | ||
| 	add_field("DET.TIPOPRZ2",				AN,  3, 260, true, "AAA"); // prezzo netto
 | ||
| 	add_field("DET.UDMPRZUN2",			AN,  3, 263, true, "PCE");
 | ||
| 	add_field("DET.IMPORTO",				NS, 16, 266, true);
 | ||
| 	add_field("DET.DIVRIGA",				AN,  3, 282);
 | ||
| 	add_field("DET.IMPORTO2",				NS, 16, 285);
 | ||
| 	add_field("DET.DIVRIGA2",				AN,  3, 301);
 | ||
| 	add_field("DET.FINERECORD",			AN,  2,   0, true, "\r\n");
 | ||
| 
 | ||
| 	// Record DES: descrizione articolo
 | ||
| 	add_field("DES.TIPOREC",				AN,  3,   1, true, "DES");
 | ||
| 	add_field("DES.DESCR",					AN,175,   4, true);
 | ||
| 	add_field("DES.FINERECORD",			AN,  2,   0, true, "\r\n");
 | ||
| 
 | ||
| 	// Record RFN: riferimenti fatture (obbligatorio per note di credito e debito)
 | ||
| 	add_field("RFN.TIPOREC",				AN,  3,   1, true, "RFN");
 | ||
| 	add_field("RFN.TIPORIF",				AN,  3,   4, true, "IV");
 | ||
| 	add_field("RFN.RIFACCADD",			AN, 35,   7, true);
 | ||
| 	add_field("RFN.DATARIF",				AN,  8,  42, true);
 | ||
| 	add_field("RFN.FINERECORD",			AN,  2,		0, true, "\r\n");
 | ||
| 
 | ||
| 	// Record TAX: informazioni assoggettamento iva
 | ||
| 	add_field("TAX.TIPOREC",				AN,  3,   1, true, "TAX");
 | ||
| 	add_field("TAX.TIPOTASS",				AN,  3,   4, true, "VAT");
 | ||
| 	add_field("TAX.DESCRIZ",				AN, 35,   7);
 | ||
| 	add_field("TAX.CATIMP",					AN,  3,  42, true);
 | ||
| 	add_field("TAX.ALIQIVA",				NU,  7,  45, true);
 | ||
| 	add_field("TAX.IMPORTO",				NS, 16,  52, true);
 | ||
| 	add_field("TAX.FINERECORD",			AN,  2,   0, true, "\r\n");
 | ||
| 
 | ||
| 	// Record ALD: sconti di riga
 | ||
| 	add_field("ALD.TIPOREC",				AN,  3,   1, true, "ALD");
 | ||
| 	add_field("ALD.INDSCADD",				AN,  3,   4, true, "A");
 | ||
| 	add_field("ALD.DESCR",					AN, 35,   7);
 | ||
| 	add_field("ALD.INDSEQ",					AN,  3,  42);
 | ||
| 	add_field("ALD.TIPOSCADD",			AN,  6,  45, true, "TD");
 | ||
| 	add_field("ALD.IMPORTO",				NS, 16,  51);
 | ||
| 	add_field("ALD.PERC",						AN,  7,  67, true);
 | ||
| 	add_field("ALD.FLGPRZUN",				AN,  3,  74);
 | ||
| 	add_field("ALD.FINERECORD",			AN,  2,   0, true, "\r\n");
 | ||
| 
 | ||
| 	// Record NAD: identificazione punto di consegna
 | ||
| 	add_field("NAD.TIPOREC",				AN,  3,   1, true, "NAD");
 | ||
| 	add_field("NAD.CODCONS1",				NU,  2,   4, true, "00");  
 | ||
| 	add_field("NAD.CODCONS2",				AN,  4,   6);  
 | ||
| 	add_field("NAD.QCODCONS",				AN,  3,  21, true, "92");
 | ||
| 	add_field("NAD.RAGSOCD",				AN, 70,  24, true);
 | ||
| 	add_field("NAD.INDIRD",					AN, 70,  94, true);
 | ||
| 	add_field("NAD.CITTAD",					AN, 35, 164, true);
 | ||
| 	add_field("NAD.PROVD",					AN,  9, 199, true);
 | ||
| 	add_field("NAD.CAPD",						AN,  9, 208, true);
 | ||
| 	add_field("NAD.NAZIOD",					AN,  3, 217, true);
 | ||
| 	add_field("NAD.NUMBOLLA",				AN, 35, 220, true);
 | ||
| 	add_field("NAD.DATABOLL",				DA,  8, 255, true);
 | ||
| 	add_field("NAD.NUMORD",					AN, 35, 263, true);
 | ||
| 	add_field("NAD.DATAORD",				DA,  8, 298, true);
 | ||
| 	add_field("NAD.FINERECORD",			AN,  2,   0, true, "\r\n");
 | ||
| 
 | ||
| 	// *** Sezione di sommario fattura
 | ||
| 
 | ||
| 	// Record FTT: note di sommario
 | ||
| 	add_field("FTT.TIPOREC",				AN,  3,   1, true, "FTT");
 | ||
| 	add_field("FTT.TIPONOTA",				AN,  3,   4);
 | ||
| 	add_field("FTT.NOTE",						AN,350,   7);
 | ||
| 	add_field("FTT.FINERECORD",			AN,  2,   0, true, "\r\n");
 | ||
| 	
 | ||
| 	// Record ALT: sconto di sommario
 | ||
| 	add_field("ALT.TIPOREC",				AN,  3,   1, true, "ALT");
 | ||
| 	add_field("ALT.INDSCADD",				AN,  3,   4, true, "A");
 | ||
| 	add_field("ALT.DESCR",					AN, 35,   7);
 | ||
| 	add_field("ALT.INDSEQ",					NU,  3,  42);
 | ||
| 	add_field("ALT.TIPOSCADD",			AN,  6,  45);
 | ||
| 	add_field("ALT.IMPORTO",				NS, 16,  51, true);
 | ||
| 	add_field("ALT.PERC",						NU,  7,  67, true);
 | ||
| 	add_field("ALT.FINERECORD",			AN,  2,   0, true, "\r\n");
 | ||
| 
 | ||
| 	// Record IVA: subtotali imposte
 | ||
| 	add_field("IVA.TIPOREC",				AN,  3,   1, true, "IVA");
 | ||
| 	add_field("IVA.TIPOTASS",				AN,  3,   4, true, "VAT");
 | ||
| 	add_field("IVA.DESCRIZ",				AN, 35,   7);
 | ||
| 	add_field("IVA.CATIMP",					AN,  3,  42, true);
 | ||
| 	add_field("IVA.ALIQIVA",				NU,  7,  45);
 | ||
| 	add_field("IVA.SIMPONIB",				NS, 16,  52, true);
 | ||
| 	add_field("IVA.SIMPORTO",				NS, 16,  68);
 | ||
| 	add_field("IVA.FINERECORD",			AN,  2,   0, true, "\r\n");
 | ||
| 
 | ||
| 	// Record TMA: totali documento
 | ||
| 	add_field("TMA.TIPOREC",				AN,  3,   1, true, "TMA");
 | ||
| 	add_field("TMA.TOTDOC1",				NS, 16,   4, true);
 | ||
| 	add_field("TMA.IMPOSTA1",				NS, 16,  20, true);
 | ||
| 	add_field("TMA.IMPONIB1",				NS, 16,  36, true);
 | ||
| 	add_field("TMA.TOTRIGHE1",			NS, 16,  52, true);
 | ||
| 	add_field("TMA.TOTANT1",				NS, 16,  68);
 | ||
| 	add_field("TMA.TOTPAG1",				NS, 16,  84);
 | ||
| 	add_field("TMA.DIVISA1",				AN,  3, 100);
 | ||
| 	add_field("TMA.TOTDOC2",				NS, 16, 103);
 | ||
| 	add_field("TMA.IMPOSTA2",				NS, 16, 119);
 | ||
| 	add_field("TMA.IMPONIB2",				NS, 16, 135);
 | ||
| 	add_field("TMA.TOTRIGHE2",			NS, 16, 151);
 | ||
| 	add_field("TMA.TOTANT2",				NS, 16, 167);
 | ||
| 	add_field("TMA.TOTPAG2",				NS, 16, 183);
 | ||
| 	add_field("TMA.DIVISA2",				AN,  3, 199);
 | ||
| 	add_field("TMA.FINERECORD",			AN,  2,   0, true, "\r\n");
 | ||
| 
 | ||
| }
 | ||
| ///////////////////////////////////////
 | ||
| // TSkeleton_application
 | ||
| ///////////////////////////////////////
 | ||
| class THardy_esselunga_fat : public TSkeleton_application
 | ||
| {
 | ||
| 	TCodArtEsselunga_cache _cache_ca;
 | ||
|   TCodArtEsselungaUM_cache _cache_um;
 | ||
|   
 | ||
| protected:
 | ||
|   long genera_recordset(const TMask& mask, TISAM_recordset& recset);
 | ||
|   void elabora(const TMask& mask, int mode);
 | ||
|   void check_date(const TDate& datafine, TDate& dataini);
 | ||
|   TString4 check_cliente(const long codcf);
 | ||
| 
 | ||
|   void segnala_documento(const TDocumento& doc, TLog_report& log, const char* msg) const;
 | ||
|   bool esistono_righe_esselunga(const TDocumento& doc) const;
 | ||
| 	void add_rec_testata(TFatture_recordset& fat, TDocumento& doc);
 | ||
| 	void add_rec_sommario(TFatture_recordset& fat, TDocumento& doc);
 | ||
| 	void add_recs_dettaglio(TFatture_recordset& fat, TDocumento& doc);
 | ||
| 
 | ||
| public:
 | ||
|   virtual void main_loop();
 | ||
|   virtual bool create();
 | ||
| };
 | ||
| 
 | ||
| //metodo per ricavare la data iniziale di elaborazione qualora l'utonto non la metta e l'esercizio da usare
 | ||
| void THardy_esselunga_fat::check_date(const TDate& datafine, TDate& dataini)
 | ||
| {
 | ||
|   TEsercizi_contabili esc;
 | ||
|   TDate datafine_tmp = datafine;
 | ||
|   const int esercizio = esc.date2esc(datafine);
 | ||
|   esc.code2range(esercizio, dataini, datafine_tmp);
 | ||
| }
 | ||
| 
 | ||
| //metodo che filtra tutti i documenti in base ai parametri della maschera
 | ||
| long THardy_esselunga_fat::genera_recordset(const TMask& mask, TISAM_recordset& recset)
 | ||
| {
 | ||
|   TString query;
 | ||
| 	query << "USE DOC KEY 3\n";
 | ||
| 
 | ||
| 	TString filt_expr;
 | ||
| 	// aggiungo alla query le condizioni sulle numerazioni selezionare da maschera
 | ||
| 	TSheet_field& sheet = mask.sfield(F_SHEETDOC);
 | ||
|   const long items = sheet.items();
 | ||
|   if (items > 0)
 | ||
|   {
 | ||
| 	  TString16 codnum;
 | ||
| 	  filt_expr << " SELECT";
 | ||
| 	  FOR_EACH_SHEET_ROW(sheet, r, row)
 | ||
| 	  {
 | ||
| 	    codnum = row->get(0);
 | ||
| 	    if (codnum.not_empty())
 | ||
| 	    {
 | ||
| 	    	filt_expr << " (CODNUM=\"";
 | ||
| 	    	filt_expr << codnum << "\") ||";
 | ||
| 	    }
 | ||
| 	  }
 | ||
| 	  filt_expr.rtrim(2);  
 | ||
| 		query << filt_expr;
 | ||
| 	}
 | ||
|   query << "\nFROM DATADOC=#DATAINI PROVV='D' ANNO=#ANNO";
 | ||
|   query << "\nTO DATADOC=#DATAFIN PROVV='D' ANNO=#ANNO";
 | ||
| 
 | ||
|   recset.set(query);
 | ||
| 
 | ||
|   //settaggio delle variabili
 | ||
|   const TDate datafin = mask.get_date(F_DATAFIN);
 | ||
|   TDate dataini = mask.get_date(F_DATAINI);
 | ||
| 
 | ||
| 	//se la data iniziale <20> vuota deve coincidere con l'inizio dell'esercizio della data finale (obbligatoria!)
 | ||
|   int esc = datafin.year();
 | ||
|   if (!dataini.ok())
 | ||
|     check_date(datafin, dataini);
 | ||
| 
 | ||
|   recset.set_var("#ANNO", long(esc));
 | ||
|   recset.set_var("#DATAINI", dataini);
 | ||
|   recset.set_var("#DATAFIN", datafin);
 | ||
|   return recset.items();
 | ||
| }
 | ||
| 
 | ||
| void THardy_esselunga_fat::add_rec_testata(TFatture_recordset& fat, TDocumento& doc)
 | ||
| {
 | ||
| 	fat.new_rec("BGM");
 | ||
| 	TString16 numdoc;
 | ||
| 	const TString& num = doc.numerazione();
 | ||
| 	numdoc.format("%012d",doc.numero());
 | ||
| 	numdoc << cache().get("%NUM", num, "S7");
 | ||
| 	numdoc.strip("/");
 | ||
| 	numdoc = numdoc.right(12);
 | ||
| 
 | ||
| 	if (doc.tipo().nota_credito())
 | ||
| 		fat.set("BGM.TIPODOC", "NOTACC"); // nota di credito
 | ||
| 	else
 | ||
| 		fat.set("BGM.TIPODOC", "INVOIC"); // fattura
 | ||
| 	fat.set("BGM.NUMDOC", numdoc);
 | ||
| 	fat.set("BGM.DATADOC", TVariant(doc.data()));
 | ||
| 
 | ||
| 	fat.new_rec("NAS");
 | ||
| 	fat.new_rec("NAI");
 | ||
| 	fat.new_rec("FTX");
 | ||
| 
 | ||
| 	fat.new_rec("PAT");
 | ||
|   const TPagamento& pag = doc.pagamento();
 | ||
| 	fat.set("PAT.DESCRIZ", pag.name());
 | ||
| 	fat.set("PAT.DATASCAD", TVariant(pag.data_rata(0)));
 | ||
| 	fat.set("PAT.IMPORTO", TVariant(doc.totale_doc()));
 | ||
| 	TString8 tp = pag.tipo_prima_rata();
 | ||
| 	real imp = pag.importo_rata(0);
 | ||
| }
 | ||
| 
 | ||
| void THardy_esselunga_fat::add_rec_sommario(TFatture_recordset& fat, TDocumento& doc)
 | ||
| {
 | ||
| 	TAssoc_array& tabella_iva = doc.tabella_iva();
 | ||
| 	TRiepilogo_iva* ri;
 | ||
| 	for (ri = (TRiepilogo_iva*) tabella_iva.first_item(); ri != NULL; ri = (TRiepilogo_iva*) tabella_iva.succ_item())
 | ||
| 	{
 | ||
| 		fat.new_rec("IVA");
 | ||
| 		fat.set("IVA.DESCRIZ", ri->cod_iva().descrizione());
 | ||
| 		const TString4& tipo = ri->cod_iva().tipo();
 | ||
| 		TString4 tipo_hardy = "S";
 | ||
| 		if (tipo == "ES")
 | ||
| 			tipo_hardy = "X";
 | ||
| 		else 
 | ||
| 			if (tipo == "NI")
 | ||
| 				tipo_hardy = "G";
 | ||
| 			else 
 | ||
| 				if (tipo == "NS")
 | ||
| 					tipo_hardy = "E";
 | ||
| 		fat.set("IVA.CATIMP", tipo_hardy);
 | ||
| 
 | ||
|     const TString8 aliquota = real(ri->cod_iva().percentuale()*10000).string(7,0,0);
 | ||
| 		fat.set("IVA.ALIQIVA", aliquota);
 | ||
| 		fat.set("IVA.SIMPONIB", ri->imponibile());
 | ||
| 		fat.set("IVA.SIMPORTO", ri->imposta());
 | ||
| 	}
 | ||
| 	fat.new_rec("TMA");
 | ||
| 	fat.set("TMA.TOTDOC1", doc.totale_doc());
 | ||
| 	fat.set("TMA.IMPOSTA1", doc.imposta());
 | ||
| 	fat.set("TMA.IMPONIB1", doc.imponibile());
 | ||
| 	fat.set("TMA.TOTRIGHE1", doc.get_real("TOTMER"));
 | ||
| 	fat.set("TMA.TOTANT1", "");
 | ||
| }
 | ||
| 
 | ||
| bool THardy_esselunga_fat::esistono_righe_esselunga(const TDocumento& doc) const
 | ||
| {
 | ||
| 	FOR_EACH_PHYSICAL_RDOC((TDocumento&)doc, r, rigadoc)
 | ||
| 	{		
 | ||
|     const TString& codart = rigadoc->get(RDOC_CODARTMAG);
 | ||
|     if (codart.full())
 | ||
|     {
 | ||
| 		  const TString& codart_esselunga = ((TCodArtEsselunga_cache&)_cache_ca).decode(codart);
 | ||
|       if (codart_esselunga.full())
 | ||
|         return true;
 | ||
|     }
 | ||
|   }
 | ||
|   return false;
 | ||
| }
 | ||
| 
 | ||
| void THardy_esselunga_fat::add_recs_dettaglio(TFatture_recordset& fat, TDocumento& doc)
 | ||
| {
 | ||
|   TString codart;
 | ||
| 	FOR_EACH_PHYSICAL_RDOC(doc, r, rigadoc)
 | ||
| 	{		
 | ||
|     codart = rigadoc->get(RDOC_CODARTMAG);
 | ||
|     if (codart.blank())
 | ||
|       continue;
 | ||
| 		const TString& codart_esselunga = _cache_ca.decode(codart);
 | ||
|     if (codart_esselunga.blank())
 | ||
|       continue;
 | ||
| 		const TRectype& rec_anamag = cache().get(LF_ANAMAG, codart);
 | ||
| 		fat.new_rec("DET");
 | ||
| 		fat.set("DET.NUMRIGA",  long(r));
 | ||
| 		fat.set("DET.CODEANCU", codart_esselunga);
 | ||
| 		fat.set("DET.CODDISTU", codart_esselunga);
 | ||
| 		fat.set("DET.TIPQUANT", "L01"); // vendita ;   L02 reso
 | ||
| 		fat.set("DET.QTACONS",  rigadoc->quantita());
 | ||
| 		fat.set("DET.QTAFATT",  rigadoc->quantita());
 | ||
| 		fat.set("DET.NRCUINTU", rec_anamag.get_real(ANAMAG_PPCONF));
 | ||
| 		fat.set("DET.PRZUNI",   rigadoc->prezzo(false, false));
 | ||
| 		fat.set("DET.PRZUN2",   rigadoc->prezzo(true, false));
 | ||
| 		fat.set("DET.IMPORTO",  rigadoc->importo(true, false));
 | ||
| 
 | ||
|     const TString& tu = _cache_um.decode(codart);
 | ||
|     const TString& cu = rigadoc->get(RDOC_UMQTA);
 | ||
|     if (tu.full() && tu != cu)
 | ||
|     {
 | ||
|       fat.set("DET.UDMQCONS", "TU");
 | ||
|       fat.set("DET.UDMQFATT", "TU");
 | ||
|     }
 | ||
| 
 | ||
| 		fat.new_rec("DES");
 | ||
| 		fat.set("DES.DESCR", rigadoc->get(RDOC_DESCR));
 | ||
| 		
 | ||
| 		//fat.new_rec("RFN"); // solo per note di debito e di credito (chiedere a Robbi)
 | ||
| 
 | ||
| 		fat.new_rec("TAX"); 
 | ||
|     const TString8 aliquota = real(rigadoc->iva().percentuale()*10000).string(7,0,'0');
 | ||
| 		fat.set("TAX.ALIQIVA", aliquota);
 | ||
| 		fat.set("TAX.DESCRIZ", rigadoc->iva().descrizione());
 | ||
| 		fat.set("TAX.IMPORTO", rigadoc->imposta());
 | ||
| 		const TString4& tipo = rigadoc->iva().tipo();
 | ||
| 		TString4 tipo_hardy = "S";
 | ||
| 		if (tipo == "ES")
 | ||
| 			tipo_hardy = "X";
 | ||
| 		else 
 | ||
| 			if (tipo == "NI")
 | ||
| 				tipo_hardy = "G";
 | ||
| 			else 
 | ||
| 				if (tipo == "NS")
 | ||
| 					tipo_hardy = "E";
 | ||
| 		fat.set("TAX.CATIMP", tipo_hardy);
 | ||
| 		// gestione sconto riga
 | ||
| 		const real sconto = rigadoc->sconto();
 | ||
| 		if (!sconto.is_zero())
 | ||
| 		{
 | ||
| 			fat.new_rec("ALD");
 | ||
|       fat.set("ALD.IMPORTO", sconto);
 | ||
|       
 | ||
|       TString80 str;
 | ||
|       real quota; scontoexpr2perc(rigadoc->get(RDOC_SCONTO), false, str, quota);
 | ||
|       const real perc = (UNO - quota)*1000000;
 | ||
| 			str = perc.string(7,0,'0');
 | ||
| 			fat.set("ALD.PERC", str);
 | ||
| 		}
 | ||
| 		fat.new_rec("NAD");
 | ||
| 		const long codcf = doc.codcf();
 | ||
| 		const TString4 codcf_esselunga = check_cliente(codcf);
 | ||
| 	  TToken_string key;
 | ||
| 		key.add("C"); key.add(codcf);
 | ||
| 		const TRectype& rec_cli = cache().get(LF_CLIFO, key);
 | ||
| 		TString4 stato = rec_cli.get(CLI_STATOCF);
 | ||
| 		TString4 comune = rec_cli.get(CLI_COMCF);
 | ||
| 		key.cut(0);
 | ||
| 		key.add(stato); key.add(comune);
 | ||
| 		const TRectype& com = cache().get(LF_COMUNI, key);
 | ||
| 		fat.set("NAD.CODCONS2", codcf_esselunga);
 | ||
| 		fat.set("NAD.RAGSOCD", rec_cli.get(CLI_RAGSOC));	
 | ||
| 		TString80 str;
 | ||
| 		str.cut(0);
 | ||
| 		str << rec_cli.get(CLI_INDCF) << ", " << rec_cli.get(CLI_CIVCF);
 | ||
| 		fat.set("NAD.INDIRD", str);
 | ||
| 		fat.set("NAD.CITTAD", com.get(COM_DENCOM));
 | ||
| 		fat.set("NAD.PROVD", com.get(COM_DENCOM));
 | ||
| 		fat.set("NAD.CAPD", rec_cli.get(CLI_CAPCF));
 | ||
| 		if (stato.empty())
 | ||
| 			stato = "IT";
 | ||
| 		fat.set("NAD.NAZIOD", stato);
 | ||
| 		TString16 numdoc;
 | ||
| 		const TString& num = doc.numerazione();
 | ||
| 		numdoc.format("%012d",doc.numero());
 | ||
| 		numdoc << cache().get("%NUM", num, "S7");
 | ||
| 		numdoc.strip("/");
 | ||
| 		numdoc = numdoc.right(12);
 | ||
| 		fat.set("NAD.NUMDOC", numdoc);
 | ||
| 		fat.set("NAD.DATADOC", TVariant(doc.data()));
 | ||
| 		fat.set("NAD.NUMBOLLA", numdoc);
 | ||
| 		fat.set("NAD.DATABOLL", TVariant(doc.data()));
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| void THardy_esselunga_fat::segnala_documento(const TDocumento& doc, TLog_report& log, const char* msg) const
 | ||
| {
 | ||
|   TString s; 
 | ||
|   s << TR("Doc. ") << doc.get(DOC_CODNUM) << ' ' 
 | ||
|     << doc.get(DOC_ANNO) << '/' << doc.get(DOC_NDOC) << " : ";
 | ||
| 
 | ||
|   if (msg && *msg)
 | ||
|   {
 | ||
|     s << msg;
 | ||
|     log.log(1, s);
 | ||
|   }
 | ||
|   else
 | ||
|   {
 | ||
|     s << TR("Elaborato con successo");
 | ||
|     log.log(0, s);
 | ||
|   }
 | ||
| 
 | ||
| }
 | ||
| 
 | ||
| void THardy_esselunga_fat::elabora(const TMask& mask, int mode)
 | ||
| {
 | ||
| 	TLog_report log;
 | ||
| 	TConfig config(CONFIG_DITTA, "ha");
 | ||
| 	TFilename file_fatture = config.get("Esselunga_Path");
 | ||
| 	file_fatture.add("FATTURE");
 | ||
|   if ((mode & 2) != 0)
 | ||
|     file_fatture << "_N";
 | ||
| 	file_fatture.ext("txt");
 | ||
|   TISAM_recordset recset("");
 | ||
|   const long items = genera_recordset(mask, recset);
 | ||
|   if (items == 0)
 | ||
|     log.log(1, "Non esistono documenti che soddisfino i parametri selezionati.");
 | ||
| 	// lettura dei documenti da recordset
 | ||
| 	TProgind pi(recset.items(), TR("Elaborazione documenti in corso..."));
 | ||
| 	TFatture_recordset fatture((mode & 1) != 0);
 | ||
| 	TCodArtEsselunga_cache cache_ca;
 | ||
| 	const bool definitivo = (mode & 1) && mask.get_bool(F_DEFINITIVO);
 | ||
|   const bool forzatura = mask.get_bool(F_FORZATURA);
 | ||
|   for (bool ok = recset.move_first(); ok; ok = recset.move_next())
 | ||
| 	{  
 | ||
|     if (!pi.addstatus(1))
 | ||
|       break;
 | ||
| 
 | ||
|     const long codcf = recset.get(DOC_CODCF).as_int();
 | ||
| 		// verificare se il cliente ha tutti i parametri per poter essere inviato (invio e codice cliente per Hardy)
 | ||
| 		const TString4 codcf_esselunga = check_cliente(codcf);
 | ||
| 		if (codcf_esselunga.full())
 | ||
| 		{
 | ||
| 			TDocumento doc(recset.cursor()->curr());
 | ||
|       if (!forzatura && doc.get_bool("HA_FATTURE"))
 | ||
|         continue;
 | ||
| 
 | ||
|       if (esistono_righe_esselunga(doc))
 | ||
|       {
 | ||
| 			  add_rec_testata(fatture, doc);
 | ||
| 			  add_recs_dettaglio(fatture, doc);			
 | ||
| 			  add_rec_sommario(fatture, doc);
 | ||
| 			  if (definitivo)
 | ||
|         {
 | ||
| 				  doc.put("HA_FATTURE", "X");
 | ||
|           doc.rewrite();
 | ||
|         }
 | ||
|         segnala_documento(doc, log, NULL);
 | ||
|       }
 | ||
|       else
 | ||
|         segnala_documento(doc, log, "Non contiene righe valide per Esselunga");
 | ||
| 		} // if check_cliente...
 | ||
|   } //for (bool ok = recset.move_first()...
 | ||
| 
 | ||
| 	fatture.save_as(file_fatture, fmt_text);
 | ||
|   log.preview();
 | ||
| }
 | ||
| 
 | ||
| TString4 THardy_esselunga_fat::check_cliente(const long codcf)
 | ||
| {
 | ||
| 	TString4 codcf_hardy;
 | ||
| 	TString8 key; key << codcf;
 | ||
| 	const TRectype& rec_cliente = cache().get("&CEL", key);
 | ||
| 	const bool invio = rec_cliente.get_bool("B0");
 | ||
| 	if (invio)
 | ||
|   {
 | ||
|   	const char dipendenza = rec_cliente.get("S1")[0];
 | ||
|     const long clifo = rec_cliente.get_long("I0");
 | ||
| 		codcf_hardy.format("%c%03d", dipendenza, clifo);
 | ||
|   }
 | ||
| 	return codcf_hardy;
 | ||
| }
 | ||
| 
 | ||
| void THardy_esselunga_fat::main_loop()
 | ||
| {
 | ||
|   THardy_esselunga_fat_mask mask;
 | ||
|   while (mask.run() == K_ENTER)
 | ||
|   {
 | ||
|     const bool enne = mask.get_bool(F_ENNE);
 | ||
|     if (enne)
 | ||
|     {
 | ||
|       elabora(mask, 0);
 | ||
|       elabora(mask, 3);
 | ||
|     }
 | ||
|     else
 | ||
|       elabora(mask, 1);
 | ||
|   }
 | ||
| }
 | ||
| 
 | ||
| bool THardy_esselunga_fat::create()
 | ||
| {
 | ||
|   open_files(LF_DOC, LF_RIGHEDOC, 0);
 | ||
| 	return TSkeleton_application::create();
 | ||
| }
 | ||
| 
 | ||
| int ha2200 (int argc, char* argv[])
 | ||
| {
 | ||
|   THardy_esselunga_fat elabapp;
 | ||
| 	elabapp.run(argc, argv, TR("Esselunga: generazione file Fatture"));
 | ||
|   return 0;  
 | ||
| } |