campo-sirio/cg/cg3900.cpp
guy 2c139d8b39 Patch level : 4.0
Files correlati     : cg3.exe
Ricompilazione Demo : [ ]
Commento            :
Allineato alla 3.0


git-svn-id: svn://10.65.10.50/trunk@15002 c028cbd2-c16b-5b4b-a496-9718f37d4682
2007-03-07 11:02:06 +00:00

461 lines
12 KiB
C++
Executable File

#include "cg3.h"
#include "cg3900a.h"
#include "cglib01.h"
#include <applicat.h>
#include <automask.h>
#include <defmask.h>
#include <progind.h>
#include <recarray.h>
#include <reprint.h>
#include <textset.h>
#include <causali.h>
#include <clifo.h>
#include <comuni.h>
#include <mov.h>
#include <rmoviva.h>
///////////////////////////////////////////////////////////
// TAllegato_info
///////////////////////////////////////////////////////////
struct TAllegato_importi : public TObject
{
real _imp, _iva, _impNI, _impES, _impNA;
};
class TAllegato_info : public TObject
{
char _tipo;
long _codice;
TAllegato_importi _curr, _prec;
public:
char tipo() const { return _tipo; }
long codice() const { return _codice; }
const TRectype& clifo() const;
TAllegato_importi& importi(bool prec) { return prec ? _prec : _curr; }
const TAllegato_importi& importi(bool prec) const { return prec ? _prec : _curr; }
TAllegato_info(char tipo, long codcf) : _tipo(tipo), _codice(codcf) { }
TAllegato_info(const TRectype& rec);
};
///////////////////////////////////////////////////////////
// TAllegati_set
///////////////////////////////////////////////////////////
class TAllegati_set : public TAS400_recordset
{
protected:
const TString& comune(const TRectype& clifo, const char* field) const;
public:
void add(const TAllegato_info& info);
TAllegati_set();
};
const TString& TAllegati_set::comune(const TRectype& clifo, const char* field) const
{
TString8 key;
key << clifo.get(CLI_STATOCF) << '|' << clifo.get(CLI_COMCF);
const TString& s1 = cache().get(LF_COMUNI, key, field);
if (s1.full())
return s1;
key = " |";
key << cap2comune(clifo.get(CLI_CAPCF), clifo.get(CLI_LOCCF));
return cache().get(LF_COMUNI, key, field);
}
void TAllegati_set::add(const TAllegato_info& info)
{
new_rec();
const TRectype& clifo = info.clifo();
for (unsigned int i = 0; i < columns(); i++)
{
const TString& field = column_info(i)._name;
if (clifo.type(field) != _nullfld)
set(field, TVariant(clifo.get(field)));
else
{
if (field == "COMUNE")
{
const TVariant var = comune(clifo, COM_DENCOM);
set(field, var);
} else
if (field == "PROVINCIA")
{
const TVariant var = comune(clifo, COM_PROVCOM);
set(field, var);
}
else
{
if (field.starts_with("C_") || field.starts_with("P_"))
{
const TAllegato_importi& allimp = info.importi(field[0] == 'P');
const TString& impfield = field.mid(2);
if (impfield == "IMP")
set(field, allimp._imp); else
if (impfield == "IVA")
set(field, allimp._iva); else
if (impfield == "NI")
set(field, allimp._impNI); else
if (impfield == "ES")
set(field, allimp._impES); else
if (impfield == "NA")
set(field, allimp._impNA);
}
}
}
}
}
TAllegati_set::TAllegati_set() : TAS400_recordset("AS400(512)")
{
const char* const campi[] = { CLI_TIPOCF, CLI_CODCF, CLI_RAGSOC, CLI_COFI, CLI_PAIV,
CLI_INDCF, CLI_CIVCF, CLI_CAPCF, CLI_LOCCF, CLI_COMCF, NULL };
TRectype clifo(LF_CLIFO);
for (int i = 0; campi[i]; i++)
{
const char* f = campi[i];
create_field(f, -1, clifo.length(f), clifo.type(f));
}
create_field("COMUNE", -1, 50, _alfafld);
create_field("PROVINCIA", -1, 2, _alfafld);
for (int j = 0; j < 2; j++)
{
create_field(j ? "P_IMP" : "C_IMP", -1, 15, _realfld);
create_field(j ? "P_IVA" : "C_IVA", -1, 15, _realfld);
create_field(j ? "P_NI" : "C_NI", -1, 15, _realfld);
create_field(j ? "P_ES" : "C_ES", -1, 15, _realfld);
create_field(j ? "P_NA" : "C_NA", -1, 15, _realfld);
}
}
const TRectype& TAllegato_info::clifo() const
{
TString16 key; key.format("%c|%ld", tipo(), codice());
return cache().get(LF_CLIFO, key);
}
TAllegato_info::TAllegato_info(const TRectype& rec)
: _tipo(rec.get_char(CLI_TIPOCF)), _codice(rec.get_long(CLI_CODCF))
{ }
///////////////////////////////////////////////////////////
// TRegistri_cache
///////////////////////////////////////////////////////////
class TRegistri_cache : public TCache
{
protected:
virtual TObject* key2obj(const char* key);
public:
const TRegistro& registro(const char* codice, int anno);
};
// key = AAAA|REG
TObject* TRegistri_cache::key2obj(const char* key)
{
const int anno = atoi(key);
const char* reg = key+5;
return new TRegistro(reg, anno);
}
const TRegistro& TRegistri_cache::registro(const char* codice, int anno)
{
TString8 key;
key.format("%04d|%s", anno, codice);
return *(TRegistro*)objptr(key);
}
///////////////////////////////////////////////////////////
// TCodiva_cache
///////////////////////////////////////////////////////////
class TCodiva_cache : public TCache
{
protected:
virtual TObject* key2obj(const char* key);
public:
const TCodiceIVA& codiva(const char* codice);
};
TObject* TCodiva_cache::key2obj(const char* key)
{
return new TCodiceIVA(key);
}
const TCodiceIVA& TCodiva_cache::codiva(const char* codice)
{
return *(TCodiceIVA*)objptr(codice);
}
///////////////////////////////////////////////////////////
// TAlleg_report
///////////////////////////////////////////////////////////
class TAlleg_report : public TReport
{
int _anno;
protected:
virtual bool use_mask() { return false; }
virtual bool get_usr_val(const TString& name, TVariant& var) const;
public:
TAlleg_report(TRecordset* rs, int anno);
};
bool TAlleg_report::get_usr_val(const TString& name, TVariant& var) const
{
if (name == "ANNO")
{
var.set(_anno);
return true;
}
if (name == "PREC")
{
var.set(_anno-1);
return true;
}
return TReport::get_usr_val(name, var);
}
TAlleg_report::TAlleg_report(TRecordset* rs, int anno) : _anno(anno)
{
load("cg3900a");
set_recordset(rs);
}
///////////////////////////////////////////////////////////
// TAlleg_mask
///////////////////////////////////////////////////////////
class TAlleg_mask : public TAutomask
{
TRegistri_cache _registri;
TCodiva_cache _codiva;
TAssoc_array _clifi;
protected:
bool documento_corrispettivi(const TString& tipodoc) const;
void scan_iva_rows(const TRecordset& mov);
TRecordset* new_recordset();
public:
virtual bool on_field_event(TOperable_field& o, TField_event e, long jolly);
TAlleg_mask();
};
bool TAlleg_mask::documento_corrispettivi(const TString& tipodoc) const
{
const TRectype& tpd = cache().get("%TPD", tipodoc);
bool corrisp = tpd.get_bool ("B0");
if (corrisp)
{
const int natura_doc = tpd.get_int("I0");
corrisp = natura_doc == 1 || natura_doc == 9;
}
return corrisp;
}
void TAlleg_mask::scan_iva_rows(const TRecordset& mov)
{
TToken_string clifo_key;
clifo_key << mov.get(MOV_TIPO) << '|' << mov.get(MOV_CODCF);
const TRectype& clifo = cache().get(LF_CLIFO, clifo_key);
const long codalleg = clifo.get_long(CLI_CODALLEG);
if (codalleg > 0) // Aggiorno codice clifo con allegato
clifo_key.add(codalleg,1);
TAllegato_info* ai = (TAllegato_info*)_clifi.objptr(clifo_key);
if (ai == NULL)
{
ai = new TAllegato_info(clifo);
_clifi.add(clifo_key, ai);
}
const TDate datareg = mov.get(MOV_DATAREG).as_date();
const TDate datadoc = mov.get(MOV_DATADOC).as_date();
TAllegato_importi& allimp = ai->importi(datadoc.year() < datareg.year());
TISAM_recordset righe_iva("USE RMOVIVA\nFROM NUMREG=#NR\nTO NUMREG=#NR");
righe_iva.set_var("#NR", mov.get(MOV_NUMREG));
for (bool ok = righe_iva.move_first(); ok; ok = righe_iva.move_next())
{
const TCodiceIVA& codiva = _codiva.codiva(righe_iva.get(RMI_CODIVA).as_string());
const int allegato = codiva.allegato(clifo_key[0]);
const real imponibile = righe_iva.get(RMI_IMPONIBILE).as_real();
const real imposta = righe_iva.get(RMI_IMPOSTA).as_real();
switch (allegato)
{
case 1: allimp._imp += imponibile; allimp._iva += imposta; break;
case 2: allimp._impNI += imponibile; break;
case 3: allimp._impES += imponibile; break;
default: allimp._impNA += imponibile; break;
}
}
}
TRecordset* TAlleg_mask::new_recordset()
{
// Compito: tradurre in ISAM la seguente query:
// SELECT * FROM MOV
// WHERE TIPO=#TYPE AND ANNOIVA=#YEAR
// ORDER BY TIPO,CODCF;
TString query = "USE MOV KEY 3"; // La chiave 3 e' TIPO+CODCF+DATAREG+NUMREG
query << "\nSELECT ANNOIVA=#YEAR"; // Seleziona solo l'anno desiderato
query << "\nFROM TIPO=#TYPE CODCF=1"; // Salta tutti movimenti senza CODCF
query << "\nTO TIPO=#TYPE"; // Inutile dire CODCF=999999
TISAM_recordset mov(query);
_clifi.destroy();
const TString& tipo = get(F_TIPO);
const int anno = get_int(F_ANNO);
mov.set_var("#YEAR", TVariant(long(anno)));
mov.set_var("#TYPE", TVariant(tipo));
TProgind pi(mov.items(), "Scansione movimenti", true, true);
for (bool ok = mov.move_first(); ok; ok = mov.move_next())
{
if (!pi.addstatus(1))
break;
// Controllo se registro e' compatibile (anche se lo e' sempre!)
const TString& codreg = mov.get(MOV_REG).as_string();
const TRegistro& reg = _registri.registro(codreg, anno);
if (reg.corrispettivi())
continue;
const TString& codcaus = mov.get(MOV_CODCAUS).as_string();
const TRectype& caus = cache().get(LF_CAUSALI, codcaus);
if (caus.get_bool(CAU_ALLEG)) // Controllo il flag di esclusione dagli allegati
continue;
// Controllo inutilmente anche il tipo documento
const TString& tipodoc = caus.get(CAU_TIPODOC);
if (documento_corrispettivi(tipodoc))
continue;
const TipoIVA tipoiva = reg.iva();
bool keep = tipoiva == iva_vendite || tipoiva == iva_acquisti; // Voglio solo registri IVA
if (keep)
keep = (tipo == "C") ^ (tipoiva == iva_acquisti); // compatibile
if (!keep)
continue;
const TVariant& occas = mov.get(MOV_OCFPI);
if (!occas.is_empty()) // Ignoro i clienti occasionali
continue;
scan_iva_rows(mov);
}
TAllegati_set* hullygully = new TAllegati_set;
FOR_EACH_ASSOC_OBJECT(_clifi, h, k, o)
hullygully->add(*(TAllegato_info*)o);
hullygully->sort();
return hullygully;
}
bool TAlleg_mask::on_field_event(TOperable_field& o, TField_event e, long jolly)
{
switch (o.dlg())
{
case DLG_SELECT:
if (e == fe_button)
{
TRecordset* rs = new_recordset();
TRecordset_sheet sheet(*rs, TR("Elenco di controllo"), 0x8);
while (sheet.run() == K_ENTER)
{
const long sel = sheet.selected();
TToken_string& row = sheet.row(sel);
TRectype clifo(LF_CLIFO);
clifo.put(CLI_TIPOCF, row.get(0));
clifo.put(CLI_CODCF, row.get(1));
clifo.edit();
}
delete rs;
return false;
}
break;
case DLG_EDIT:
if (e == fe_button)
{
TRecordset* rs = new_recordset();
TFilename fname = get(F_NAME);
if (fname.blank())
{
fname.tempdir();
fname.add("clifo");
}
fname.ext("xls");
if (rs->save_as(fname, fmt_silk))
xvt_sys_goto_url(fname, "open");
delete rs;
return false;
}
break;
case DLG_PRINT:
if (e == fe_button)
{
TRecordset* rs = new_recordset();
TAlleg_report rep(rs, get_int(F_ANNO));
TReport_book book;
book.add(rep);
book.print_or_preview();
return false;
}
break;
default:
break;
}
return true;
}
TAlleg_mask::TAlleg_mask() : TAutomask("cg3900a")
{
const TDate oggi(TODAY);
set(F_ANNO, oggi.year()-1);
TFilename fname; fname.tempdir();
fname.add("clifo.xls");
set(F_NAME, fname);
}
///////////////////////////////////////////////////////////
// TClifo_alleg_app
///////////////////////////////////////////////////////////
class TClifo_alleg_app : public TSkeleton_application
{
public:
virtual void main_loop();
};
void TClifo_alleg_app::main_loop()
{
open_files(LF_CAUSALI, LF_CLIFO, LF_COMUNI, LF_MOV, LF_RMOVIVA, 0);
TAlleg_mask m;
m.run();
}
int cg3900(int argc, char* argv[])
{
TClifo_alleg_app app;
app.run(argc, argv, TR("Elenco clienti/fornitori in allegato"));
return 0;
}