campo-sirio/ha/ha2200.cpp

475 lines
16 KiB
C++
Raw Normal View History

#include <applicat.h>
#include <automask.h>
#include <progind.h>
#include <recarray.h>
#include <recset.h>
#include <reputils.h>
#include <textset.h>
#include <utility.h>
#include <nditte.h>
#include <anagr.h>
#include <comuni.h>
#include <clifo.h>
#include <doc.h>
#include <rdoc.h>
#include "../mg/codcorr.h"
#include "../cg/cglib01.h"
#include "../ve/velib.h"
#include "halib.h"
#include "ha2.h"
#include "ha2200a.h"
const char* const APPNAME = TR("Esselunga: generazione file Fatture");
///////////////////////////////////////////////////////////
// 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
/////////////////////////////////////////////////////////////
enum TFE_type { AN, NU };
class TFatture_recordset : public TAS400_recordset
{
void add_field(const char* name, TFE_type type, int len, int pos,
bool required = false, const char* def = NULL);
public:
TFatture_recordset();
};
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 nulla sul campo ", name);
TFieldtypes ft = _alfafld;
switch (type)
{
case NU:
ft = _longfld;
break;
default:
ft = _alfafld;
break;
}
bool ok = false;
if (def && *def)
ok = create_field(name, pos, len, ft, required, TVariant(def));
else
ok = create_field(name, pos, len, ft, required);
CHECKS(ok, "Impossibile creare il campo ", (const char*)name);
}
TFatture_recordset::TFatture_recordset()
: TAS400_recordset("AS400(610)")
{
// dati di configurazione
TConfig config(CONFIG_DITTA, "ha");
const TString80 piva_hardy = config.get("Esselunga_PIvaHardy");
const TString80 piva_esselunga = config.get("Esselunga_PIvaEsselunga");
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");
// 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", NU, 8, 151, true);
add_field("BGM.ORADOC", NU, 4, 159);
add_field("BGM.FILLER", AN, 6, 163);
TString80 str;
str << ditta.get(ANA_INDRES) << ", " << ditta.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));
add_field("NAS.NAZIOF", AN, 3, 217, true, anag.get(ANA_STATORES));
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", AN, 35, 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);
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, piva_esselunga); // verificare con Robbi perche' nell'esempio c'e' un altro numero
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));
add_field("NAI.NAZIOI", AN, 3, 217, true, cliente_esselunga.get(CLI_STATOCF));
add_field("NAI.PIVANAZI", AN, 35, 220, true, cliente_esselunga.get(CLI_PAIV));
add_field("NAI.FILLER", AN, 51, 255);
// 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);
// 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", NU, 8, 7);
add_field("PAT.RIFTERMP", AN, 3, 15, true);
add_field("PAT.RELTERMP", AN, 3, 18);
add_field("PAT.UNTEMP", AN, 3, 21);
add_field("PAT.NUNTEMP", NU, 3, 24);
add_field("PAT.IMPORTO", NU, 16, 27);
add_field("PAT.DIVISA", AN, 3, 43);
add_field("PAT.PERC", NU, 7, 46);
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);
// *** Sezione di dettaglio fattura ***
// Record DET: identificazione, quantita'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", NU, 6, 13);
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);
add_field("DET.QTACONS", NU, 16, 165);
add_field("DET.UDMQCONS", AN, 3, 181);
add_field("DET.QTAFATT", NU, 16, 184);
add_field("DET.UDMQFATT", AN, 3, 200);
add_field("DET.NRCUINTU", NU, 16, 203, true);
add_field("DET.UDMNRCUINTU", AN, 3, 219, true, "PCE");
add_field("DET.PRZUNI", NU, 16, 222, true);
add_field("DET.TIPORPZ", AN, 3, 238, true, "AAA"); // prezzo netto
add_field("DET.UDMPRZUN", AN, 3, 241, true);
add_field("DET.PRZUN2", NU, 16, 244, true);
add_field("DET.TIPOPRZ2", AN, 3, 260, true, "AAB"); // prezo lordo
add_field("DET.UDMPRZUN2", AN, 3, 263, true);
add_field("DET.IMPORTO", NU, 16, 266, true);
add_field("DET.DIVRIGA", AN, 3, 282);
add_field("DET.IMPORTO2", NU, 16, 285);
add_field("DET.DIVRIGA2", AN, 3, 301);
// Record DES: descrizione articolo
add_field("DES.TIPOREC", AN, 3, 1, true, "DES");
add_field("DES.DESCR", AN,175, 3, true);
// 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);
// 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", NU, 16, 52, true);
// Record ALD: sconti di riga
add_field("ALD.TIPOREC", AN, 3, 1, true, "ALD");
add_field("ALD.INDSCADD", AN, 3, 4);
add_field("ALD.DESCR", AN, 35, 7);
add_field("ALD.INDSEQ", NU, 3, 42);
add_field("ALD.TIPOSCADD", AN, 6, 45);
add_field("ALD.IMPORTO", NU, 16, 51);
add_field("ALD.PERC", NU, 7, 67, true);
add_field("ALD.FLGPRZUN", AN, 3, 74);
// Record NAD: identificazione punto di consegna
add_field("NAD.TIPOREC", AN, 3, 1, true, "NAD");
add_field("NAD.CODCONS", AN, 17, 4);
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", NU, 8, 255, true);
add_field("NAD.NUMORD", AN, 35, 263, true);
add_field("NAD.DATAORD", NU, 8, 298, true);
// *** 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);
// Record ALT: sconto di sommario
add_field("ALT.TIPOREC", AN, 3, 1, true, "ALT");
add_field("ALT.INDSCADD", AN, 3, 4, true);
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", NU, 16, 51, true);
add_field("ALT.PERC", NU, 7, 67, true);
// 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", NU, 16, 52, true);
add_field("IVA.SIMPORTO", NU, 16, 68);
// Record TMA: totali documento
add_field("TMA.TIPOREC", AN, 3, 1, true, "TMA");
add_field("TMA.TOTDOC1", NU, 16, 4, true);
add_field("TMA.IMPOSTA1", NU, 16, 20, true);
add_field("TMA.IMPONIB1", NU, 16, 36, true);
add_field("TMA.TOTRIGHE1", NU, 16, 52, true);
add_field("TMA.TOTANT1", NU, 16, 68);
add_field("TMA.TOTPAG1", NU, 16, 84);
add_field("TMA.DIVISA1", AN, 3, 100);
add_field("TMA.TOTDOC2", NU, 16, 103);
add_field("TMA.IMPOSTA2", NU, 16, 119);
add_field("TMA.IMPONIB2", NU, 16, 135);
add_field("TMA.TOTRIGHE2", NU, 16, 151);
add_field("TMA.TOTANT2", NU, 16, 167);
add_field("TMA.TOTPAG2", NU, 16, 183);
add_field("TMA.DIVISA2", AN, 3, 199);
}
///////////////////////////////////////
// TSkeleton_application
///////////////////////////////////////
class THardy_esselunga_fat : public TSkeleton_application
{
protected:
long genera_recordset(const TMask& mask, TISAM_recordset& recset);
void elabora(const TMask& mask);
void check_date(const TDate& datafine, TDate& dataini);
long check_cliente(const long codcf);
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 \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::elabora(const TMask& mask)
{
TLog_report log;
TConfig config(CONFIG_DITTA, "ha");
TFilename file_fatture = config.get("Esselunga_Path");
file_fatture.add("FATTURE");
file_fatture.ext("txt");
TISAM_recordset recset("");
const long items = genera_recordset(mask, recset);
if (items == 0)
log.log(1, "Non esistono documenti che soddisfano i parametri selezionati.");
// lettura dei documenti da recordset
TProgind pi(recset.items(), TR("Elaborazione documenti in corso..."), true, true);
TFatture_recordset fatture;
TCodArtEsselunga_cache cache_ca;
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 long codcf_esselunga = check_cliente(codcf);
if ( codcf_esselunga > 0)
{
TDocumento* doc = new TDocumento(recset.cursor()->curr());
FOR_EACH_PHYSICAL_RDOC(*doc, r, rigadoc)
{
const TString80& codart_esselunga = cache_ca.decode(rigadoc->get(RDOC_CODART));
fatture.new_rec("");
//fatture.set("CDC", TVariant(codcf_esselunga));
//fatture.set("CODART", codart_esselunga);
//fatture.set("NUMBOLLA", numdoc);
} //FOR_EACH...
} // if check_cliente...
} //for (bool ok = recset.move_first()...
fatture.save_as(file_fatture, fmt_text);
// memorizzo su campo virtuale che il doc. e' stato inviato
const bool definitivo = mask.get_bool(F_DEFINITIVO);
if (definitivo)
{
}
log.print_or_preview();
}
long THardy_esselunga_fat::check_cliente(const long codcf)
{
long codcf_hardy = -1;
TString key;
key.format("%ld", codcf);
const TRectype rec_cliente = cache().get("&CEL", key);
const bool invio = rec_cliente.get_bool("B0");
if (invio)
codcf_hardy = rec_cliente.get_long("I0");
return codcf_hardy;
}
void THardy_esselunga_fat::main_loop()
{
THardy_esselunga_fat_mask mask;
while (mask.run() == K_ENTER)
elabora(mask);
}
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, APPNAME);
return 0;
}