#include #include #include #include #include #include #include #include #include #include #include #include #include #include #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 { protected: void add_field(const char* name, TFE_type type, int len, int pos, bool required = false, const char* def = NULL); public: virtual TRecnotype new_rec(const char* trc); TFatture_recordset(); }; 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; } 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 = _longfld; 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() : TAS400_recordset("AS400(612,3,0)") { // 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, "INVOIC"); 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); add_field("BGM.FINERECORD", AN, 2, 0, true, "\r\n"); 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); 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, 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); 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", NU, 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); 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); add_field("PAT.FINERECORD", AN, 2, 0, true, "\r\n"); // *** 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"); // prezzo 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); 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, 3, 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", NU, 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); 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); 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.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); 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", NU, 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", NU, 16, 52, true); add_field("IVA.SIMPORTO", NU, 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", 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); add_field("TMA.FINERECORD", AN, 2, 0, true, "\r\n"); } /////////////////////////////////////// // 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); void add_rec_testata(TFatture_recordset& fat, TDocumento& doc); void add_rec_sommario(TFatture_recordset& fat, TDocumento& doc); void add_rec_dettaglio(TFatture_recordset& fat, const TDocumento& doc, const TRiga_documento& rigadoc); 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 è 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"); fat.set("BGM.NUMDOC", doc.numero()); fat.set("BGM.DATAOC", doc.data().string()); fat.new_rec("NAS"); fat.new_rec("NAI"); fat.new_rec("FTX"); fat.new_rec("PAT"); fat.set("PAT.DESCRIZ", doc.pagamento().name()); } void THardy_esselunga_fat::add_rec_sommario(TFatture_recordset& fat, TDocumento& doc) { //fat.new_rec("FTT"); non ci sono note nel documento! // se c'e' sconto scrivo il record corrispondente real r; TString80 s; bool sconto = scontoexpr2perc(doc.get(DOC_SCONTOPERC), FALSE, s, r); if ( sconto && r != ZERO) { fat.new_rec("ALT"); fat.set("ALT.IMPORTO", r); fat.set("ALT.PERC", s); } 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()); fat.set("IVA.ALIQIVA", ri->cod_iva().percentuale()); 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.totale_netto()); fat.set("TMA.TOTANT1", ""); } void THardy_esselunga_fat::add_rec_dettaglio(TFatture_recordset& fat, const TDocumento& doc, const TRiga_documento& rigadoc) { fat.new_rec("DET"); fat.new_rec("DES"); fat.new_rec("RFN"); fat.new_rec("TAX"); fat.new_rec("ALD"); fat.new_rec("NAD"); } 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; const bool definitivo = mask.get_bool(F_DEFINITIVO); 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()); add_rec_testata(fatture, *doc); FOR_EACH_PHYSICAL_RDOC(*doc, r, rigadoc) add_rec_dettaglio(fatture, *doc, *rigadoc); add_rec_sommario(fatture, *doc); if (definitivo) doc->put("HA_FATTURE", "X"); } // if check_cliente... } //for (bool ok = recset.move_first()... fatture.save_as(file_fatture, fmt_text); 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; }