campo-sirio/cg/cg4800.cpp
luca aa0912819c Patch level :10.0 250
Files correlati     :
Ricompilazione Demo : [ ]
Commento            :corretta chiamata dei BAR_ITEM con BAR_ITEM_ID


git-svn-id: svn://10.65.10.50/trunk@18333 c028cbd2-c16b-5b4b-a496-9718f37d4682
2009-02-24 10:16:54 +00:00

693 lines
15 KiB
C++
Executable File

//Creazione versamenti per acconti
#include <applicat.h>
#include <currency.h>
#include <mask.h>
#include <tabutil.h>
#include <recarray.h>
#include <relation.h>
#include <sheet.h>
#include <urldefid.h>
#include <prefix.h>
#include <printer.h>
#include <progind.h>
#include <utility.h>
#include <config.h>
#include <righef24.h>
#include <modaut.h>
#include "cglib03.h"
#include "cg4800a.h"
#include "cg4800b.h"
class VersAcc_app : public TApplication
{
TRelation* _nditte;
TArray_sheet* _ditte;
TArray _nomiditte, _desc;
TBit_array _selected;
TInteressi_IVA_table* _ver;
TProgind* _prind;
int _year;
real _acconto, _credito, _diff, _impver, _acconto_minimo, _acconto_minimo_cr_ut;
bool _calcall, _da_stampare, _print;
TDate _dataver;
TString16 _abi, _cab, _con;
TString16 _abips, _cabps; //dai parametri di studio
TString4 _freqviva;
protected:
virtual bool create();
virtual bool destroy();
virtual bool menu(MENU_TAG);
virtual void print();
// handlers
static bool ch_year_handler(TMask_field& f, KEY key);
public:
static VersAcc_app& app() { return (VersAcc_app&)main_app(); }
void build_ditte_sheet();
void build_nomiditte();
void lettura_delega();
void lettura_anagrafica(bool*);
void crea_riga_stampa(bool);
void aggiorna_delega(TTable & del);
void crea_delega(TTable & del);
void vers_acc();
bool look_lia();
bool video_conferma(TTable & del);
bool check_acc();
VersAcc_app() : _ditte(NULL), _selected(10000), _nomiditte(100), _desc(100) {}
};
bool VersAcc_app::look_lia()
{
TString16 y;
y.format("%05ld%04d", _nditte->lfile().get_long("CODDITTA"), _year);
const TRectype & lia = cache().get("%LIA", y);
_acconto = lia.get_real("R4");
_credito = lia.get_real("R6");
_freqviva = lia.get("S7");
return !lia.empty();
}
bool VersAcc_app::create()
{
TApplication::create();
open_files(LF_TABCOM, LF_TAB, LF_NDITTE, LF_ANAG, LF_COMUNI, 0);
_nditte = new TRelation(LF_NDITTE);
_nditte->add(LF_ANAG, "TIPOA=TIPOA|CODANAGR=CODANAGR");
_nditte->add(LF_COMUNI, "COM=COMRF(COMRES)", 1, LF_ANAG);
_ditte = new TArray_sheet(-1, -1, -4, -4, TR("Selezione Ditte"),
HR("@1|Cod.@5R|Ragione Sociale@50|Vers."));
_ver = new TInteressi_IVA_table();
TConfig c (CONFIG_STUDIO, "cg");
_abips = c.get("CodABI");
_cabps = c.get("CodCAB");
TDate oggi(TODAY);
_year = oggi.year();
begin_wait();
build_nomiditte();
build_ditte_sheet();
end_wait();
dispatch_e_menu(BAR_ITEM_ID(1));
return TRUE;
}
bool VersAcc_app::destroy()
{
delete _ver;
delete _ditte;
delete _nditte;
return TApplication::destroy();
}
bool VersAcc_app::ch_year_handler(TMask_field& f, KEY key)
{
if (key == K_TAB && f.focusdirty())
{
TWait_cursor hourglass;
app()._year = atoi(f.get());
app().build_nomiditte();
app().build_ditte_sheet();
}
return TRUE;
}
void VersAcc_app::build_ditte_sheet()
{
// build sheet
_ditte->destroy();
for (int i = 0; i < _nomiditte.items(); i++)
{
TToken_string* d = new TToken_string(64);
*d = (TToken_string&)_nomiditte[i];
const char vers = d->get_char(2);
d->insert(" |", 0);
bool selectable = vers == '?';
const long pos = _ditte->add(d);
if (selectable) _ditte->disable_row(pos);
else _ditte->enable_row(pos);
}
}
void VersAcc_app::build_nomiditte()
{
_nomiditte.destroy();
// ricostruire _nomiditte e rifare build_ditte_sheet
TCursor cur(_nditte, "", 1);
TRectype & dt = _nditte->curr();
const TRecnotype items = cur.items();
for (cur = 0L; cur.pos() < items; ++cur)
{
// check no archivi
if (prefix().exist(dt.get_long("CODDITTA")))
{
// check no parametri liquidazione
TString key;
TToken_string d;
// add record
d.add(dt.get_long("CODDITTA"));
d.add(dt.get("RAGSOC"));
key.format("%05ld%d", dt.get_long("CODDITTA"), _year);
const TRectype & lia = cache().get("%LIA", key);
d.add(lia.empty() ? "??" : lia.get("S7"));
_nomiditte.add(d);
}
}
}
bool VersAcc_app::menu(MENU_TAG)
{
if (check_acc())
{
if (_calcall || _selected.ones() > 0l)
{
vers_acc();
if (_print)
{
enable_menu_item(M_FILE_PRINT);
print();
}
}
else warning_box(TR("Nessuna ditta selezionata!"));
}
return FALSE;
}
bool VersAcc_app::check_acc()
{
KEY k = 0;
TMask m("cg4800a");
m.set_handler(FLD_CGB_YEAR, ch_year_handler);
long j;
_calcall = FALSE;
for (;;)
{
if (k == K_ESC || k == K_ENTER || k == K_QUIT)
break;
k = m.run();
switch (k)
{
case DLG_SELECT:
_ditte->run();
for (j = 0l; j < _ditte->items(); j++)
if (_ditte->checked(j)) _selected.set(j);
break;
case BUT_CGB_ALL:
_ditte->check(-1);
for (j = 0l; j < _ditte->items(); j++)
if (_ditte->checked(j) && !_ditte->row_disabled(j))
_selected.set(j);
_calcall = TRUE;
k = K_ENTER;
break;
}
}
if (k == K_ENTER) _year = m.get_int(FLD_CGB_YEAR);
return k == K_ENTER;
}
void VersAcc_app::vers_acc()
{
char buf[256]; bool tipo;
_desc.destroy();
_da_stampare = _print = FALSE;
_prind = new TProgind(_calcall ? 0l : _selected.ones(),
TR(" Creazione versam. acconti \n"
" preparazione archivi \n"
" \n"),
TRUE, TRUE);
if (_ver->read(_year,12)!=NOERR)
warning_box(FR("Errore %d in lettura tabella versamenti ed interessi."),_ver->status());
_acconto_minimo = _ver->get(I_ACCONTOIVA);
for (int l = 0; l < _ditte->items(); l++)
{
if (_prind->iscancelled())
break;
if (!(_calcall || _selected[l]) || _ditte->row_disabled(l))
continue;
_abi = _abips;
_cab = _cabps;
_da_stampare = FALSE; //relativo a ciascuna ditta
const long ditta = _ditte->row(l).get_long(1);
TApplication::set_firm(ditta);
TIva_round ir; ir.set_default_iva_mode(_year, FALSE);
_nditte->curr().zero();
_nditte->curr().put("CODDITTA",ditta);
_nditte->read();
if (!look_lia()) continue;
sprintf (buf,FR("Creazione vers. acconti:\nditta %s\n "),
(const char*)_nditte->lfile().get("RAGSOC"));
_prind->set_text(buf);
lettura_anagrafica(&tipo);
lettura_delega();
if (_da_stampare)
{
crea_riga_stampa(tipo);
_print = TRUE; //relativo alla stampa finale
}
_prind->addstatus(1);
}
delete _prind;
}
void VersAcc_app::print()
{
printer().open();
TPrintrow row;
row.put(TR("Gestione iva"), 0);
row.put(FR("Data @>"), 106);
row.put(FR("Pag.@#"), 124);
printer().setheaderline(0,row);
row.reset();
TString256 t(FR("@bCREAZIONE VERSAMENTI PER ACCONTI DICEMBRE"));
t <<' '<<_year;
row.put(t,34);
printer().setheaderline(1,row);
t = "";
t.fill('-',132);
row.reset();
row.put(t,0);
printer().setheaderline(2,row);
printer().setheaderline(4,row);
row.reset();
row.put(TR("Ditta"),0);
row.put(TR("Denominazione"),7);
row.put(TR("Importo"),46);
row.put(TR("Banca"),55);
row.put(TR("Dipendenza"),62);
row.put(TR("Concessione"),74);
printer().setheaderline(3,row);
for (int i = 0; i < _desc.items(); i++)
{
TToken_string& tt = (TToken_string&)_desc[i];
TParagraph_string rs (tt.get(1),30);
row.reset();
row.put(tt.get(0),0);
const real imp = tt.get(2);
TCurrency cur(imp);
TString80 descr = cur.string(TRUE);
descr.right_just(15);
row.put(descr,38);
if (tt.get_int(7) == 0)
{
row.put(tt.get(3),55);
row.put(tt.get(4),63);
}
else row.put(tt.get(5),77);
descr = tt.get(6);
row.put(descr.left(45),86);
const char* r;
while ((r = rs.get()) != NULL)
{
row.put(r,7);
printer().print(row);
row.reset();
}
}
printer().close();
}
void round_mille_lire(real& d)
{
const int dec = TCurrency::get_firm_dec();
if (dec == 0) // lire
{
if (d % real(500.0) == ZERO) d -= 1.0;
d.round(-3);
}
else // Non si sa ancora come gestire l'euro
d.round(0);
}
void VersAcc_app::lettura_delega()
{
const bool has_f24 = main_app().has_module(GVAUT);
TString16 cod;
TString16 y; y.format("%05ld", _nditte->lfile().get_long("CODDITTA"));
const real minimo = _credito != ZERO ? _acconto_minimo_cr_ut : _acconto_minimo;
_diff = _acconto - _credito;
TIva_round ir;
ir.round(_diff);
cod << y << _year << 12 << 7;
TTable del("%DEL");
del.put("CODTAB", cod);
if (del.read() == NOERR)
{
if (_diff < minimo) //l'importo non e' dovuto
{
//cancello la delega
del.remove();
return;
}
else
{
// se sono qui e' perche' l'importo e' dovuto (_diff >= minimo)
bool stampata = del.get_bool("B0");
_impver = del.get_real("R0");
_dataver = del.get_date("D0");
if (stampata)
{
_abi = del.get("S7");
_cab = del.get("S8");
_con = del.get("S9");
if (_diff != _impver)
video_conferma(del);
}
else
{
aggiorna_delega(del);
_da_stampare = TRUE;
}
}
}
else //non esiste la delega
{
if (_diff >= minimo)
//creo record delega
{
crea_delega(del);
_da_stampare = TRUE;
}
}
if (has_f24)
{
TLocalisamfile f24(LF_RIGHEF24);
TTable trib("%TRB");
long progr = 0L;
TString descr("Acconto IVA");
const bool mensile = _freqviva == "M";
const int codtrib = mensile ? 6013 : 6035;
TDate scad(27, 12, _year);
const long codditta = _nditte->lfile().get_long("CODDITTA");
while (scad.wday() > 5)
++scad;
trib.put("CODTAB", codtrib);
if (trib.read() != NOERR)
{
int g = 0, c = 0;
long s = 0;
trib.put("CODTAB", 6001);
if (trib.read(_isgteq) == NOERR)
{
const int cod = trib.get_int("CODTAB");
if (cod > 6000 && cod < 6036)
{
g = trib.get_int("I0");
c = trib.get_int("I1");
s = trib.get_long("I2");
}
}
trib.zero();
trib.put("CODTAB", codtrib);
trib.put("S0", descr);
trib.put("S6", "E");
trib.put("I0", g);
trib.put("I1", c);
trib.put("I2", s);
trib.write();
}
f24.setkey(4);
f24.put(F24_TIPOINTEST, "D");
f24.put(F24_INTEST, codditta);
f24.put(F24_ANNO, _year);
f24.put(F24_TRIBUTO, codtrib);
if (f24.read() == NOERR)
{
if (f24.get(F24_DATAVERS).blank())
{
int err = NOERR;
if (_diff < minimo)
err = f24.remove(); // non devo versare
else
{
f24.put(F24_DATASCAD, scad);
f24.put(F24_IMPORTODEB, _diff);
err = f24.rewrite();
}
if (err != NOERR)
error_box(FR("Ditta %ld\n%s\nerrore %d"), codditta, (const char *) descr, err);
}
}
else
{
if (_diff >= minimo)
{
f24.setkey(1);
if (f24.last() == NOERR)
progr = f24.get_long(F24_PROGR);
progr++;
f24.zero();
f24.put(F24_PROGR, progr);
f24.put(F24_TRIBUTO, codtrib);
f24.put(F24_TIPOINTEST, "D");
f24.put(F24_INTEST, codditta);
f24.put(F24_DESCR, descr);
f24.put(F24_DATASCAD, scad);
f24.put(F24_ANNO, _year);
f24.put(F24_IMPORTODEB, _diff);
const int err = f24.write();
if (err != NOERR)
error_box(FR("Ditta %ld\n%s\nerrore %d"), codditta, (const char *) descr, err);
}
}
}
return;
}
void VersAcc_app::crea_delega(TTable & del)
{
TString16 y (format("%05ld", _nditte->lfile().get_long("CODDITTA")));
TString16 cod;
cod << y << _year << 12 << 7;
del.zero();
del.put("CODTAB", cod);
del.put("S7", _abi);
del.put("S8", _cab);
del.put("S9", _con);
del.put("R0", _diff);
del.write();
}
void VersAcc_app::aggiorna_delega(TTable & del)
{
del.put("S7", _abi);
del.put("S8", _cab);
del.put("S9", _con);
del.put("R0", _diff);
del.rewrite();
}
static real pezza_euro(real n)
{
if (is_euro_value(NULL))
{
n /= 1936.27;
n.round(2);
}
return n;
}
void VersAcc_app::lettura_anagrafica(bool* tipo)
{
TLocalisamfile& anag = _nditte->lfile(LF_ANAG);
TLocalisamfile& com = _nditte->lfile(LF_COMUNI); // Comune residenza fiscale
const bool cf = anag.get_bool("TITCF");
const int isdel = anag.get_int("TIPOSTDEL");
// Se deve stampare un bollettino delega l'importo minimo con credito utilizzato diventa 12500;
// se invece deve stampare una distinta l'importo minimo e' 13500
if (cf)
_acconto_minimo_cr_ut = pezza_euro(isdel == 2 ? 13500.0 : 12500.0);
else
// Nel caso non sia titolare conto fiscale l'importo minimo e' 500
_acconto_minimo_cr_ut = pezza_euro(500.0);
if (cf && (isdel == 1 || isdel == 2))
{
*tipo = TRUE;
const int con = com.get_int("UFFCONC");
_abi = "";
_cab = "";
_con.format("%03d",con);
}
else
{
*tipo = FALSE;
_con = "";
const long codabi = _nditte->lfile().get_long("ABIBAN");
const long codcab = _nditte->lfile().get_long("CABBAN");
if (codabi != 0 && codcab != 0)
{
_abi = format("%05ld", codabi);
_cab = format("%05ld", codcab);
}
}
}
bool VersAcc_app::video_conferma(TTable & del)
{
TMask m("cg4800b");
m.field(F_CODDITTA).set(_nditte->lfile().get("CODDITTA"));
m.field(F_RAGSOC).set(_nditte->lfile().get("RAGSOC"));
m.field(F_DATAVER).set(_dataver);
m.field(F_ABI).set(_abi);
m.field(F_CAB).set(_cab);
m.field(F_CON).set(_con);
m.field(F_IMPVER).set(_impver.string());
m.field(F_NEWIMP).set(_diff.string());
for (bool stop = FALSE; !stop;)
{
KEY k = m.run();
switch(k)
{
case K_SAVE:
{
KEY k = yesnocancel_box(TR("Delega gia' stampata. Si desidera ugualmente confermare l'aggiornamento?"));
if (k == K_YES)
{
_da_stampare = TRUE;
del.put("R0",_diff);
del.put("B0", "");
del.rewrite();
}
if (k == K_YES || k == K_NO)
stop = TRUE;
}
break;
case K_ESC:
case K_QUIT:
stop = TRUE;
break;
}
}
return TRUE;
}
void VersAcc_app::crea_riga_stampa(bool tipost)
{
TToken_string* tt = new TToken_string(150);
TString80 desc;
if (tipost) //prendo la descrizione dell'ufficio concessione
{
if (_con.not_empty())
desc = cache().get("%UCC", _con, "S0");
}
else //prendo la descrizione della banca
{
if (_abi.not_empty())
{
TString16 key;
key = _abi;
if (!_cab.empty())
key << _cab;
desc = cache().get("%BAN", key, "S0");
}
}
tt->add(_nditte->lfile().get("CODDITTA"));
tt->add(_nditte->lfile().get("RAGSOC"));
tt->add(_diff.string());
tt->add(_abi);
tt->add(_cab);
tt->add(_con);
tt->add(desc);
tt->add(tipost);
if (tt != NULL)
_desc.add(tt);
}
int cg4800(int argc, char* argv[])
{
VersAcc_app app;
app.run(argc, argv, TR("Creazione versam. acconti dicembre"));
return 0;
}