#define XVT_INCL_NATIVE #include #include #include "../cg/cg2103.h" #include "../mg/anamag.h" #include #include #include #include "velib.h" class TIndustriosa_app : public TSkeleton_application { protected: const TString& conto_industriale(const TRiga_documento& row) const; void get_field(const TDocumento& doc, const TRectype& mov, const TRectype& rec, const char* field, TString& val) const; bool filerr(const char* name) const; bool get_mci_path(TFilename& shell) const; bool call_mci(TDocumento& doc, const TRectype& movimento) const; public: virtual void main_loop(); }; const TString& TIndustriosa_app::conto_industriale(const TRiga_documento& row) const { const TDocumento& doc = row.doc(); const char tipocf = doc.get_char(DOC_TIPOCF); if (row.is_merce()) { const TRectype& rec = cache().get(LF_ANAMAG, row.get(RDOC_CODARTMAG)); return rec.get(tipocf == 'F' ? ANAMAG_CONTOINDA : ANAMAG_CONTOINDV); } else if (row.is_prestazione()) { const TRectype& rec = cache().get("PRS", row.get(RDOC_CODART)); return rec.get(tipocf == 'F' ? "S1" : "S2"); } return EMPTY_STRING; } void TIndustriosa_app::get_field(const TDocumento& doc, const TRectype& mov, const TRectype& rec, const char* field, TString& val) const { if (field[0] == '"' || field[0] == '\'') { val = field; val.rtrim(1); val.ltrim(1); } else if (field[0] == '_') { val.cut(0); if (stricmp(field, "_CANTIERE") == 0) { val = rec.get(RDOC_CODCMS).left(4); val << rec.get(RDOC_FASCMS); } else if (stricmp(field, "_CONINDU") == 0) { val = conto_industriale((TRiga_documento&)rec); } else if (stricmp(field, "_IMPORTO") == 0) { TCurrency_documento imp(((TRiga_documento&)rec).importo(TRUE, FALSE), doc); imp.change_to_firm_val(); val = imp.get_num().string(); } else if (stricmp(field, "_IMPORTOVAL") == 0) { if (doc.in_valuta()) { const TCurrency_documento imp(((TRiga_documento&)rec).importo(TRUE, FALSE), doc); val = imp.get_num().string(); } } else if (stricmp(field, "_IMPOSTADOC") == 0) { real impval; for (int i = 1; i <= doc.physical_rows(); i++) { const TRiga_documento& rdoc = doc[i]; if (conto_industriale(rdoc).not_empty()) impval += rdoc.imposta(); } TCurrency_documento imposta(impval, doc); imposta.change_to_firm_val(); val = imposta.get_num().string(); } else if (stricmp(field, "_SEZIONE") == 0) { TCausale causale(mov.get(MOV_CODCAUS)); const char str[2] = { causale.sezione_clifo(), '\0' }; val = str; } else if (stricmp(field, "_TOTDOC") == 0) { real totval; for (int i = 1; i <= doc.physical_rows(); i++) { const TRiga_documento& rdoc = doc[i]; if (conto_industriale(rdoc).not_empty()) totval += rdoc.importo(TRUE, FALSE); } TCurrency_documento tot(totval, doc); tot.change_to_firm_val(); val = tot.get_num().string(); } } else { const TRectype* r = &rec; if (strncmp(field, "33->", 4) == 0) { r = &doc; field += 4; } else if (strncmp(field, "23->", 4) == 0) { r = &mov; field += 4; } const TFieldtypes ft = r->type(field); switch (ft) { case _nullfld: break; case _realfld: val = r->get_real(field).string(); break; default: val = r->get(field); break; } } } bool TIndustriosa_app::filerr(const char* name) const { return error_box("Impossibile accedere al file '%s'", name); } bool TIndustriosa_app::get_mci_path(TFilename& shell) const { char winpath[_MAX_PATH]; ::GetWindowsDirectory(winpath, sizeof(winpath)); shell = winpath; shell.add("mci4.ini"); bool ok = FALSE; if (shell.exist()) { TConfig mci4(shell, "Paths"); TString8 lcl = mci4.get("lcldrive"); shell = mci4.get("inilocation"); if (!lcl.empty()) { lcl.cut(1); lcl << ":"; shell.insert(lcl); } if (shell.exist()) { TConfig appl(shell, "Paths"); shell = appl.get("exelocation"); shell.insert(lcl); shell.add("mci.exe"); ok = shell.exist(); if (!ok) filerr(shell); } else filerr(shell); } else filerr(shell); return ok; } bool TIndustriosa_app::call_mci(TDocumento& doc, const TRectype& movimento) const { TConfig mci("ve6600.ini"); TString_array para; mci.list_paragraphs(para); bool ok = FALSE; TFilename ininame; ininame.temp(); if (para.items() >= 2) { TConfig ini(ininame); TString val; TAssoc_array& hvar = mci.list_variables(para.row(0)); ini.set_paragraph(para.row(0)); FOR_EACH_ASSOC_STRING(hvar, obj, key, field) { get_field(doc, movimento, movimento, field, val); ini.set(key, val); } TAssoc_array& rvar = mci.list_variables(para.row(1)); for (int r = 1; r <= doc.physical_rows(); r++) { TString16 pr; pr.format("%s_%d", (const char*)para.row(1), r); // Paragrafo riga contabile ini.set_paragraph(pr); FOR_EACH_ASSOC_STRING(rvar, obj, key, field) { get_field(doc, movimento, doc[r], field, val); ini.set(key, val); } } } else error_box("Il file 've6600.ini' non e' valido"); if (ininame.exist()) { TFilename shell; if (get_mci_path(shell)) { shell << ' ' << ininame; TExternal_app app(shell); ok = app.run() == 0; if (ok) { TConfig ini(ininame, para.row(0)); const TString16 oldnumreg = doc.get("NUMREGIND"); const TString16 newnumreg = ini.get("NumReg"); if (oldnumreg != newnumreg) { doc.put("NUMREGIND", newnumreg); doc.rewrite(); } } else error_box("Impossibile eseguire %s", (const char*)shell); } ::remove(ininame); } return ok; } void TIndustriosa_app::main_loop() { if (argc() > 2) { TFilename ininame; ininame = argv(2); ininame.ltrim(2); // toglie /i if (ininame.exist()) { open_files(LF_TAB, LF_TABCOM, LF_DOC, LF_RIGHEDOC, LF_CLIFO, LF_CFVEN, LF_MOV, LF_ANAMAG, 0); TString8 para; para.format("%d", LF_DOC); TConfig ini(ininame, para); const char provv = ini.get(DOC_PROVV)[0]; const int anno = ini.get_int(DOC_ANNO); const TString8 codnum = ini.get(DOC_CODNUM); const long ndoc = ini.get_long(DOC_NDOC); TDocumento doc(provv, anno, codnum, ndoc); para.format("%d", LF_MOV); ini.set_paragraph(para); const long numreg = ini.get_long(MOV_NUMREG); const TRectype& mov = cache().get(LF_MOV, numreg); call_mci(doc, mov); } else filerr(ininame); } else error_box("Specificare il file con la transazione da eseguire"); } ////// Esecuzione del programma /////////// int ve6600(int argc, char** argv) { TIndustriosa_app app; app.run(argc,argv, "Contabilizzazione industriale"); return 0; }