campo-sirio/cg/cg7200.cpp

491 lines
13 KiB
C++
Raw Normal View History

#include <applicat.h>
#include <assoc.h>
#include <automask.h>
#include <currency.h>
#include <filetext.h>
#include <msksheet.h>
#include <printer.h>
#include <recarray.h>
#include <relation.h>
#include <sort.h>
#include <utility.h>
#include "cg7.h"
#include "cg7200a.h"
#include <mov.h>
#include <rmov.h>
#include <rmoviva.h>
#include <pagsca.h>
#include <partite.h>
#define ALIAS_PCON1 100
#define ALIAS_PCON2 200
#define ALIAS_ABPCON1 300
#define ALIAS_ABPCON2 400
class TInvioP_file: public TFile_text
{
protected:
virtual void validate(TCursor& cur,TRecord_text &rec, TToken_string &val, TString& str);
public:
TInvioP_file(const TString& file_name, const TString& config_name);
virtual ~TInvioP_file() { }
};
TInvioP_file::TInvioP_file(const TString& file_name, const TString& config_name)
: TFile_text(file_name, config_name)
{
}
class TInvioP_mask : public TAutomask
{
protected:
bool on_field_event(TOperable_field& o, TField_event e, long jolly);
public:
TInvioP_mask();
virtual ~TInvioP_mask(){};
};
TInvioP_mask::TInvioP_mask() :TAutomask ("cg7200a")
{
}
bool TInvioP_mask::on_field_event(TOperable_field& o, TField_event e, long jolly)
{
switch (o.dlg())
{
case F_CODDITTA:
if (e==fe_init && o.empty())
{
set(F_CODDITTA, main_app().get_firm());
((TEdit_field&) o).check();
disable(F_CODDITTA);
}
break;
default:
break;
}
return TRUE;
}
class TInvioP : public TSkeleton_application
{
TCursor* _cur;
TInvioP_mask* _msk;
TInvioP_file* _trasfile;
TInvioP_file* _trasfilepag;
TDate _dataini, _datafin;
long _nregcosto, _nregpag;
real _importo;
protected:
virtual bool create(void);
virtual bool destroy(void);
virtual void main_loop() ;
void invio_proforma();
bool i_proforma_movimenti(const bool pagamenti);
bool i_proforma_righe();
bool i_proforma_pagamenti(const TCursor& cur);
bool i_proforma_clifor(char tipocf = 'C');
bool i_proforma_conti();
public:
const real get_importo() {return _importo;};
const long get_nregcosto() {return _nregcosto;};
const long get_nregpag() {return _nregpag;};
TInvioP() {};
virtual ~TInvioP() {};
};
// restituisce un riferimento all' applicazione
inline TInvioP& app() { return (TInvioP&) main_app();}
// gestione dei messaggi estesi nei campi
void TInvioP_file::validate(TCursor& cur,TRecord_text &rec, TToken_string &s, TString& str)
{
const TString code(s.get(0));
TString valore;
if (code == "_FISSO")
{
// gestione dei campi fissi per i record delle riba
// sintassi: _FISSO,!<valore>
// dove: <valore> <20> la stringa fissa da emettere
TString in(s.get());
CHECK(in[0]=='!',"Macro _FISSO senza carattere '!'");
in.ltrim(1);
in.trim();
valore = in;
}
else if (code == "_TELEFONO")
{
valore = str;
valore.trim();
str = cur.curr().get("PTEL");
valore << str;
valore.trim();
}
else if (code == "_CODCBL")
{
valore = str;
int i = valore.find("$");
valore = valore.left(i);
}
else if (code == "_RAGSOC")
{
valore = str;
valore = valore.strip_d_spaces();
}
else if (code == "_FLAG")
{
const char chiusa = str[0];
valore = (chiusa=='X') ? "S" : "A";
}
else if (code == "_NREGCOSTO")
{
valore = app().get_nregcosto();
}
else if (code == "_NREGPAG")
{
valore = app().get_nregpag();
}
else if (code == "_IMPORTO")
{
valore = app().get_importo().string();
}
else NFCHECK("Macro non definita: %s", (const char *)code);
str = valore;
}
bool TInvioP::create()
{
open_files(LF_CAUSALI, LF_CLIFO, LF_PCON, LF_MOV, LF_RMOV, LF_RMOVIVA, LF_ABPCON, LF_PARTITE, 0);
_msk = new TInvioP_mask();
_trasfile = NULL;
_trasfilepag = NULL;
return TSkeleton_application::create();
}
bool TInvioP::destroy()
{
if (_trasfile)
delete _trasfile;
if (_trasfilepag)
delete _trasfilepag;
delete _msk;
return TSkeleton_application::destroy();
}
void TInvioP::main_loop()
{
TFilename configname = "cg7200a.ini";
TConfig configfile(configname);
_msk->set(F_DATAINI, configfile.get("DATA","OPZIONI"));
_msk->set(F_DESTINAZIONE, configfile.get("PERCORSO","OPZIONI"));
while (_msk->run()!=K_QUIT)
{
configfile.set("DATA", _msk->get_date(F_DATAFIN),"OPZIONI");
configfile.set("PERCORSO", _msk->get(F_DESTINAZIONE),"OPZIONI");
_dataini = _msk->get_date(F_DATAINI);
_datafin = _msk->get_date(F_DATAFIN);
const char tipoinvio = _msk->get(F_TIPOINVIO)[0];
if (tipoinvio == 'P')
invio_proforma();
}
}
void TInvioP::invio_proforma()
{
if (_msk->get_bool(F_MOVIMENTI))
{
i_proforma_movimenti(_msk->get_bool(F_PAGAMENTI));
i_proforma_righe();
}
if (_msk->get_bool(F_CLIENTI))
i_proforma_clifor();
if (_msk->get_bool(F_FORNITORI))
i_proforma_clifor('F');
if (_msk->get_bool(F_CONTI))
i_proforma_conti();
}
bool TInvioP::i_proforma_conti()
{
TFilename configname = "proforma.ini";
TConfig configfile(configname);
TFilename filename = _msk->get(F_DESTINAZIONE);
filename.add("pianocon.txt");
if (fexist(filename))
remove(filename);
_trasfile = new TInvioP_file(filename, configname);
_trasfile->open(filename,'w');
_trasfile->force_record_separator();
TRelation rel(LF_PCON);
TCursor cur(&rel);
const long cur_items = cur.items();
if (cur_items != 0)
{
cur.freeze();
TRectype& cur_rec = cur.curr();
for (cur = 0; cur.pos() < cur_items; ++(cur))
{
TRecord_text rec;
rec.set_type("P");
_trasfile->autoload(rec, cur);
_trasfile->write(rec);
}
}
_trasfile->close();
delete _trasfile;
_trasfile = NULL;
return TRUE;
}
bool TInvioP::i_proforma_movimenti(const bool pagamenti)
{
TFilename configname = "proforma.ini";
TConfig configfile(configname);
TFilename filename = _msk->get(F_DESTINAZIONE);
filename.add("registra.txt");
if (fexist(filename))
remove(filename);
_trasfile = new TInvioP_file(filename, configname);
_trasfile->open(filename,'w');
_trasfile->force_record_separator();
if (pagamenti)
{
TFilename filenamepag = _msk->get(F_DESTINAZIONE);
filenamepag.add("pagament.txt");
if (fexist(filenamepag))
remove(filenamepag);
_trasfilepag = new TInvioP_file(filenamepag, configname);
_trasfilepag->open(filenamepag,'w');
_trasfilepag->force_record_separator();
}
TRectype da(LF_MOV);
TRectype a(LF_MOV);
da.put(MOV_DATAREG, _dataini);
a.put(MOV_DATAREG, _datafin);
TRelation rel(LF_MOV);
rel.add(LF_CAUSALI, "CODCAUS==CODCAUS", 1);
TCursor cur(&rel, "", 2, &da, &a);
const long cur_items = cur.items();
if (cur_items != 0)
{
cur.freeze();
TRectype& cur_rec = cur.curr();
for (cur = 0; cur.pos() < cur_items; ++(cur))
{
TRecord_text rec;
rec.set_type("T");
_trasfile->autoload(rec, cur);
_trasfile->write(rec);
if (pagamenti)
i_proforma_pagamenti(cur);
}
}
_trasfile->close();
delete _trasfile;
_trasfile = NULL;
if (pagamenti)
{
_trasfilepag->close();
delete _trasfilepag;
_trasfilepag = NULL;
}
return TRUE;
}
bool TInvioP::i_proforma_righe()
{
TFilename configname = "proforma.ini";
TConfig configfile(configname);
TFilename filename = _msk->get(F_DESTINAZIONE);
filename.add("righe.txt");
if (fexist(filename))
remove(filename);
_trasfile = new TInvioP_file(filename, configname);
_trasfile->open(filename,'w');
_trasfile->force_record_separator();
TRectype da(LF_MOV);
TRectype a(LF_MOV);
da.put(MOV_DATAREG, _dataini);
a.put(MOV_DATAREG, _datafin);
TRelation rel(LF_MOV);
rel.add(LF_RMOVIVA, "NUMREG==NUMREG", 1);
rel.add(LF_RMOV, "NUMREG==NUMREG", 1);
rel.add(LF_PCON, "GRUPPO==GRUPPO|CONTO==CONTO|SOTTOCONTO==SOTTOCONTO", 1, LF_RMOV, ALIAS_PCON1);
rel.add(LF_PCON, "GRUPPO==GRUPPO|CONTO==CONTO|SOTTOCONTO==SOTTOCONTO", 1, LF_RMOVIVA, ALIAS_PCON2);
rel.add(LF_ABPCON, "CODCBL==CODCBL", 1, -ALIAS_PCON1, ALIAS_ABPCON1);
rel.add(LF_ABPCON, "CODCBL==CODCBL", 1, -ALIAS_PCON2, ALIAS_ABPCON2);
TCursor cur(&rel, "", 2, &da, &a);
const long cur_items = cur.items();
if (cur_items != 0)
{
cur.freeze();
TRectype& cur_rec = cur.curr();
for (cur = 0; cur.pos() < cur_items; ++(cur))
{
const long numreg = cur.curr().get_long(MOV_NUMREG);
bool continua = TRUE;
while (continua)
{
const long numregrig = cur.curr(LF_RMOVIVA).get_long(RMI_NUMREG);
if (numreg == numregrig)
{
TRectype& cur_rec_righe = cur.curr(LF_RMOVIVA);
TRecord_text recrighe;
recrighe.set_type("I");
_trasfile->autoload(recrighe, cur);
_trasfile->write(recrighe);
}
continua = cur.next_match(LF_RMOVIVA, "NUMREG");
}
continua = TRUE;
while (continua)
{
const long numregrig = cur.curr(LF_RMOV).get_long(RMV_NUMREG);
if (numreg == numregrig)
{
TRectype& cur_rec_righe = cur.curr(LF_RMOV);
TRecord_text recrighe;
recrighe.set_type("R");
_trasfile->autoload(recrighe, cur);
_trasfile->write(recrighe);
}
continua = cur.next_match(LF_RMOV, "NUMREG");
}
}
}
_trasfile->close();
delete _trasfile;
_trasfile = NULL;
return TRUE;
}
bool TInvioP::i_proforma_pagamenti(const TCursor& cur)
{
const char tipomov = cur.curr().get(MOV_TIPOMOV)[0];
if (tipomov == '3' || tipomov == '2' || tipomov == '6')
{
TRelation relpart(LF_PARTITE);
TRectype da(LF_PARTITE);
da.put(PART_NREG, cur.curr().get(MOV_NUMREG));
TCursor curpart(&relpart, "", 2, &da, &da);
const long curpart_items = curpart.items();
if (curpart_items != 0)
{
curpart.freeze();
TRectype& curpart_rec = curpart.curr();
for (curpart = 0; curpart.pos() < curpart_items; ++(curpart))
{
TRelation relpag(LF_PAGSCA);
TRectype da(LF_PAGSCA);
da.put(PAGSCA_TIPOC, curpart_rec.get(PART_TIPOCF));
da.put(PAGSCA_GRUPPO, curpart_rec.get(PART_GRUPPO));
da.put(PAGSCA_CONTO, curpart_rec.get(PART_CONTO));
da.put(PAGSCA_SOTTOCONTO, curpart_rec.get(PART_SOTTOCONTO));
da.put(PAGSCA_ANNO, curpart_rec.get(PART_ANNO));
da.put(PAGSCA_NUMPART, curpart_rec.get(PART_NUMPART));
const int nrigapart = curpart_rec.get_int(PART_NRIGA);
TString80 filtro;
filtro.format("NRIGP == %d", nrigapart);
TCursor curpag(&relpart, filtro, 1, &da, &da);
const long curpag_items = curpag.items();
if (curpag_items != 0)
{
TAssoc_array pagame;
pagame.destroy();
curpag.freeze();
TRectype& curpag_rec = curpag.curr();
for (curpag = 0; curpag.pos() < curpag_items; ++(curpag))
{
TString80 indice = curpag_rec.get(PAGSCA_ANNO);
indice << '|' << curpag_rec.get(PAGSCA_NUMPART);
indice << '|' << curpag_rec.get(PAGSCA_NRIGA);
real importo = ZERO;
bool is_key = pagame.is_key(indice);
real& somma = is_key ? (real&) pagame[indice] : importo;
somma += curpag_rec.get_real(PAGSCA_IMPORTO);
if (!is_key)
pagame.add(indice, somma);
}
// scrivo i record risultanti
real* cp;
for (cp = (real*) pagame.first_item(); cp != NULL; cp = (real*) pagame.succ_item())
{
TToken_string keypart;
keypart << curpart_rec.get(PART_TIPOCF);
keypart << curpart_rec.get(PART_GRUPPO);
keypart << curpart_rec.get(PART_CONTO);
keypart << curpart_rec.get(PART_SOTTOCONTO);
keypart << pagame.get_hashobj()->key();
const TRectype& partita = cache().get(LF_PARTITE, keypart);
_nregpag = cur.curr().get_long(MOV_NUMREG);
_nregcosto = partita.get_long(PART_NREG);
_importo = *cp;
TRecord_text recpag;
recpag.set_type("G");
_trasfilepag->autoload(recpag, curpag);
_trasfilepag->write(recpag);
}
}
}
}
}
return TRUE;
}
bool TInvioP::i_proforma_clifor(char tipocf)
{
TFilename configname = "proforma.ini";
TConfig configfile(configname);
TFilename filename = _msk->get(F_DESTINAZIONE);
if (tipocf == 'C')
filename.add("clienti.txt");
else
filename.add("fornit.txt");
if (fexist(filename))
remove(filename);
_trasfile = new TInvioP_file(filename, configname);
_trasfile->open(filename,'w');
_trasfile->force_record_separator();
TString80 filtro = "";
filtro.format("TIPOCF == \"%c\"", tipocf);
TRelation rel(LF_CLIFO);
rel.add(LF_COMUNI, "STATO==STATOCF|COM==COMCF", 1);
TCursor cur(&rel, filtro);
const long cur_items = cur.items();
if (cur_items != 0)
{
cur.freeze();
TRectype& cur_rec = cur.curr();
for (cur = 0; cur.pos() < cur_items; ++(cur))
{
TRecord_text rec;
rec.set_type("C");
_trasfile->autoload(rec, cur);
_trasfile->write(rec);
}
}
_trasfile->close();
delete _trasfile;
_trasfile = NULL;
return TRUE;
}
int cg7200(int argc, char **argv)
{
TInvioP a;
a.run(argc, argv, "Invio dati contabilit<69>");
return 0;
}