campo-sirio/ve/ve6600.cpp
luca 9365854f44 Patch level :
Files correlati     :
Ricompilazione Demo : [ ]
Commento            :


git-svn-id: svn://10.65.10.50/trunk@17519 c028cbd2-c16b-5b4b-a496-9718f37d4682
2008-10-31 10:33:32 +00:00

391 lines
9.7 KiB
C++
Executable File

#define XVT_INCL_NATIVE
#include <applicat.h>
#include <execp.h>
#include "../cg/cg2103.h"
#include "../mg/anamag.h"
#include <mov.h>
#include <doc.h>
#include <rdoc.h>
#include "velib.h"
class TIndustriosa_app : public TSkeleton_application
{
int _len_commessa;
int _len_fase;
protected:
const TString& conto_industriale(const TRiga_documento& row, TString& conto) const;
void trim_and_cut(TString& val, int len) const;
void get_field(const TDocumento& doc, const TRectype& mov, TCausale& causale,
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);
public:
virtual void main_loop();
};
const TString& TIndustriosa_app::conto_industriale(const TRiga_documento& row, TString& conto) const
{
const TDocumento& doc = row.doc();
const bool acquisti = doc.get_char(DOC_TIPOCF) == 'F';
conto.cut(0);
if (row.is_merce())
{
const TRectype& rec = cache().get(LF_ANAMAG, row.get(RDOC_CODARTMAG));
conto = rec.get(acquisti ? ANAMAG_CONTOINDA : ANAMAG_CONTOINDV);
} else
if (row.is_prestazione())
{
const TRectype& rec = cache().get("PRS", row.get(RDOC_CODART));
conto = rec.get("S2");
if (acquisti)
conto = conto.mid(20,12);
else
conto = conto.left(12);
conto.trim();
}
if (!conto.empty())
{
TString16 cnt;
cnt << conto.mid(1,2);
cnt << conto.mid(4,2);
cnt << conto.mid(6);
conto = cnt;
}
return conto;
}
void TIndustriosa_app::trim_and_cut(TString& val, int len) const
{
if (len > 0)
{
int zeroes = 0;
for (zeroes = 0; val[zeroes] == '0'; zeroes++);
if (zeroes > 0)
val.ltrim(zeroes);
val.cut(len);
}
else
val.cut(0);
}
void TIndustriosa_app::get_field(const TDocumento& doc, const TRectype& mov, TCausale& causale,
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 (xvt_str_compare_ignoring_case(field, "_CANTIERE") == 0)
{
TString cms, fas;
cms = rec.get(RDOC_CODCMS);
if (cms.blank())
{
cms = doc.get(DOC_CODCMS);
fas = doc.get(DOC_FASCMS);
}
else
fas = rec.get(RDOC_FASCMS);
trim_and_cut(cms, _len_commessa);
trim_and_cut(fas, _len_fase);
val << cms << fas;
} else
if (xvt_str_compare_ignoring_case(field, "_CODCAUS") == 0)
{
val = causale.codice();
} else
if (xvt_str_compare_ignoring_case(field, "_CONINDU") == 0)
{
conto_industriale((TRiga_documento&)rec, val);
} else
if (xvt_str_compare_ignoring_case(field, "_IMPORTO") == 0)
{
TCurrency_documento imp(((TRiga_documento&)rec).importo(TRUE, FALSE), doc);
imp.change_to_firm_val();
val = imp.get_num().stringa();
} else
if (xvt_str_compare_ignoring_case(field, "_IMPORTOVAL") == 0)
{
if (doc.in_valuta())
{
const TCurrency_documento imp(((TRiga_documento&)rec).importo(TRUE, FALSE), doc);
val = imp.get_num().stringa();
}
} else
if (xvt_str_compare_ignoring_case(field, "_IMPOSTADOC") == 0)
{
TString16 conto;
real impval;
for (int i = 1; i <= doc.physical_rows(); i++)
{
const TRiga_documento& rdoc = doc[i];
conto_industriale(rdoc, conto);
if (conto.not_empty())
impval += rdoc.imposta();
}
TCurrency_documento imposta(impval, doc);
imposta.change_to_firm_val();
val = imposta.get_num().stringa();
} else
if (xvt_str_compare_ignoring_case(field, "_SEZIONE") == 0)
{
const char str[2] = { causale.sezione(2), '\0' };
val = str;
} else
if (xvt_str_compare_ignoring_case(field, "_TOTDOC") == 0)
{
TString16 conto;
real totval;
for (int i = 1; i <= doc.physical_rows(); i++)
{
const TRiga_documento& rdoc = doc[i];
conto_industriale(rdoc, conto);
if (conto.not_empty())
totval += rdoc.importo(TRUE, FALSE);
}
TCurrency_documento tot(totval, doc);
tot.change_to_firm_val();
val = tot.get_num().stringa();
if (val[0] == '-')
val.ltrim(1);
}
}
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 _alfafld:
val = r->get(field);
if (r->num() == LF_DOC && val.not_empty() && real::is_real(val)) // Campo virtuale numerico
{
const real n(val);
val = n.stringa();
}
break;
case _nullfld:
val.cut(0);
break;
case _realfld:
{
const real n(r->get(field));
if (!n.is_zero())
{
val = n.stringa();
if (val[0] == '-' && r->num() == LF_MOV && xvt_str_compare_ignoring_case(field, MOV_TOTDOC) == 0)
val.ltrim(1);
}
else
val.cut(0);
}
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);
}
shell.add("appl.ini");
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)
{
TConfig mci("ve6600.ini");
TString_array para;
mci.list_paragraphs(para);
_len_commessa = mci.get_int("LCommessa", "General", -1, 20);
_len_fase = mci.get_int("LFase", "General", -1, 0);
int pos =para.find("General");
if (pos >= 0)
para.destroy(pos, TRUE);
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));
const TTipo_documento& td = doc.tipo();
const TString16 codcaus = td.causale();
TCausale causale(codcaus);
FOR_EACH_ASSOC_STRING(hvar, obj, key, field)
{
get_field(doc, movimento, causale, movimento, field, val);
ini.set(key, val);
}
TAssoc_array& rvar = mci.list_variables(para.row(1));
int i = 1;
for (int r = 1; r <= doc.physical_rows(); r++)
{
const TRiga_documento& rdoc = doc[r];
TString16 conto;
conto_industriale(rdoc, conto);
if (conto.not_empty())
{
TString16 pr; pr.format("%s_%d", (const char*)para.row(1), i++); // Paragrafo riga contabile
ini.set_paragraph(pr);
FOR_EACH_ASSOC_STRING(rvar, obj, key, field)
{
get_field(doc, movimento, causale, doc[r], field, val);
ini.set(key, val);
}
}
}
mci.set_paragraph(para.row(0)); // Forza scrittura ultima riga
}
else
error_box("Il file ve6600.ini non e' valido");
if (ininame.exist())
{
TFilename shell;
if (get_mci_path(shell))
{
shell << " -c " << 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_CAUSALI, LF_RCAUSALI, 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");
}
////// Esecuzione del programma ///////////
int ve6600(int argc, char** argv)
{
TIndustriosa_app app;
app.run(argc,argv, "Contabilizzazione industriale");
return 0;
}