campo-sirio/cg/cg2104.cpp
guy d28f09cc52 Fantastica gestione del saldaconto
git-svn-id: svn://10.65.10.50/trunk@1850 c028cbd2-c16b-5b4b-a496-9718f37d4682
1995-09-19 15:43:43 +00:00

440 lines
13 KiB
C++
Executable File

// --------------------------------------------------------------------------
// Scadenzario
// fv 24/8/94
// --------------------------------------------------------------------------
#include <sheet.h>
#include <utility.h>
#include "pagament.h"
#include "saldacon.h"
#include "cg2102.h"
#include "cg2100.h"
#include "cg21sld.h"
#include <clifo.h>
#include <partite.h>
#include <scadenze.h>
bool TPrimanota_application::pag_notify(TSheet_field&, int r, KEY k)
{
TPagamento& pag = *(app().pagamento());
TSheet_field& ps = app().pags();
TMask* msk = &(app().curr_mask());
TString_array& rws = app().pag_rows();
int rdiff = atoi(msk->get(FS_RDIFFER));
// ts contiene la vecchia riga, ns la nuova
TToken_string ts(36), ns(36);
bool doit = TRUE, m_imp = FALSE, m_perc = FALSE, m_pag = FALSE;
bool m_scad = FALSE, m_tipo = FALSE, mod = FALSE;
word ahiahi = P_OK;
TString news(15), newi(15), newp(15), newt(2);
bool recalc = msk->get_bool(FS_RECALC);
bool mcomm = msk->get_bool(FS_MCOMM);
switch (k)
{
case K_SPACE:
break;
case K_ENTER:
ns = ps.row(r);
ts = rws.row(r);
news = ns.get(0);
newp = ns.get(2);
newi = ns.get(1);
newt = ns.get(3);
// qui viene il bello, si fa per dire
if (news != ts.get(0)) // modificata data scadenza
{
mod = m_scad = TRUE;
}
if (newp != ts.get(2)) // modificata percentuale
{
mod = m_perc = TRUE;
}
if (newi != ts.get(1)) // modificato importo
{
// se si modifica la percentuale l'importo non viene cagato
if ((recalc && !m_perc) || (!recalc))
{
mod = m_imp = TRUE;
}
}
if (newt != ts.get(3)) // modificato tipo pagamento
{
mod = m_tipo = TRUE;
}
break;
case K_DEL:
case K_INS:
doit = FALSE;
break;
default:
break;
}
// settato da recalc_rate se occorre ridefinire lo sheet
// aggiungendo o togliendo righe
bool need_recalc = FALSE;
if (k == K_ENTER)
{
if (mod && recalc)
{
// ricalcola sheet come sai fare tu
ahiahi = pag.recalc_rate(r, m_perc,
((m_perc || m_imp) ?
(m_perc ? (const char*)newp : (const char*)newi) :
NULL),
(m_scad ? (const char*)news : NULL),
(m_tipo ? (const char*)newt : NULL),
rdiff, mcomm, need_recalc);
// see if rdiff changed
msk->set(FS_RDIFFER, pag.rate_differenziate() ? "1" : "2");
msk->set(FS_NRATE, format("%d", pag.n_rate()));
}
if (!recalc)
{
ahiahi = P_OK;
// put data as they are
TToken_string& trw = pag.rata(r);
TToken_string srw = trw;
if (m_scad) trw.add(news,3);
if (m_perc) trw.add(newp,1);
if (m_imp) trw.add(newi,4);
if (m_tipo) trw.add(newt,2);
// validate the payment
if ((ahiahi = pag.validate()) != P_OK)
pag.rata(r) = srw;
}
if (ahiahi) // any error? Rimetti le righe com'erano prima
{
beep();
// se gli errori sono voluti, spiegali
if (!recalc)
{
TString256 s;
pag.strerr(ahiahi,s);
warning_box(s);
}
ps.row(r) = rws.row(r);
ps.force_update(r);
}
else if (recalc && mod && need_recalc)
{
// ridefinisci lo sheet sulla base delle nuove rate
pag.set_sheet(ps);
ps.force_update();
rws = ps.rows_array();
}
}
return doit;
}
bool TPrimanota_application::codpag_handler(TMask_field& f, KEY key)
{
TMask& m = f.mask();
if (key == K_TAB && f.focusdirty() && m.is_running() && app().is_saldaconto())
{
const TString16 dt(m.get(F_DATADOC));
app().set_pagamento(f.get(), dt);
app().set_scadenze(m);
}
return TRUE;
}
bool TPrimanota_application::reset_handler(TMask_field& f, KEY key)
{
TMask& m = f.mask();
if (key == K_SPACE && m.is_running() && app().is_saldaconto())
{
app().reset_pagamento();
app().set_scadenze(m);
}
return TRUE;
}
bool TPrimanota_application::nrate_handler(TMask_field& f, KEY key)
{
TMask& m = f.mask();
TSheet_field& ps = (TSheet_field&)m.field(FS_RATESHEET);
if (key == K_TAB && m.is_running() && app().is_saldaconto())
{
if (app().pagamento() != NULL)
{
app().pagamento()->set_numero_rate(atoi(f.get()), /*TBI*/-1);
if (app().pagamento()->dirty())
app().pagamento()->set_sheet(ps);
}
else f.set("");
}
return TRUE;
}
void TPrimanota_application::reset_pagamento()
{
if (_pag != NULL)
{
_pag->read();
_pag->set_rate_auto();
}
}
void TPrimanota_application::set_pagamento(const char* c, const char* d)
{
if (_pag != NULL) delete _pag;
_pag = new TPagamento(c,d);
}
void TPrimanota_application::set_scadenze(TMask& m)
{
const real imponibile(m.get(F_IMPONIBILI));
const real imposta(m.get(F_IMPOSTE));
const real spese(0.0);
TPagamento& pag = *pagamento();
m.set(FS_RDIFFER, pag.rate_differenziate() ? "1" : "2");
m.set(FS_NAMEPAG, pag.name());
m.set(FS_TIPOPR, pag.desc_tpr());
m.set(FS_MCOMM, pag.mese_commerciale() ? "X" : "");
m.set(FS_RECALC, "X", TRUE);
pag.set_total(imponibile, imposta, spese);
pag.set_rate_auto();
bool new_part = TRUE;
TSheet_field& ps = (TSheet_field&)m.field(FS_RATESHEET);
// prepara lo sheet
pag.set_sheet(ps);
_pag_rows = ps.rows_array();
m.set(FS_NRATE, pag.n_rate());
// se la prima rata e' fissa non si tocca
if (pag.tipo_prima_rata() > 3)
{
ps.disable_cell(0,0);
ps.disable_cell(0,1);
ps.disable_cell(0,2);
ps.disable_cell(0,3);
}
}
bool TPrimanota_application::read_scadenze(TMask& m)
{
// should never happen but it doesn't hurt
if (_rel->cg(0).empty()) return FALSE;
const real imponibile(m.get(F_IMPONIBILI));
const real imposta(m.get(F_IMPOSTE));
const real spese(0.0);
TSheet_field& ps = (TSheet_field&)m.field(FS_RATESHEET);
m.set(FS_RECALC, "X");
TPagamento& pag = *pagamento();
pag.set_total(imponibile, imposta, spese);
const TRectype& testa = _rel->lfile().curr();
const long nreg = testa.get_long("NUMREG");
const TString16 ndoc(testa.get("NUMDOC"));
const int tmov = testa.get_int("TIPOMOV");
const TDate ddoc(testa.get_date("DATADOC"));
const TDate dreg(testa.get_date("DATAREG"));
const TString16 reg (testa.get("REG"));
const long protiva = testa.get_long("PROTIVA");
const TString16 codcaus(testa.get("CODCAUS"));
const TString16 codval (testa.get("CODVAL"));
const real cambio(testa.get("CAMBIO"));
const TRectype& prima = _rel->cg(0);
const char sezione = prima.get_char("SEZIONE"); // Dare/Avere
const int nriga = 1;
const int numrig = 1;
TBill clifo; clifo.get(prima);
const int anno = m.get_int(F_ANNORIF);
const TString16 numpart(m.get(F_NUMRIF));
partite().destroy();
const TPartita& part = partite().partita(clifo, anno, numpart);
int npart = part.prima_fattura(nreg);
bool fromscratch = FALSE;
if (npart != -1) // la gh'e'
{
const TRiga_partite& partita = part.riga(npart);
const TRiga_scadenze& primarata = partita.rata(1);
// se si e' specificato un codice pagamento diverso si rifa' da capo
// la follia e' che e' sulle SCADENZE, non sulle partite, naturalmente
// uguale per tutte
if (pag.code() != primarata.get(SCAD_CODPAG))
fromscratch = TRUE;
// se invece la maschera contiene importi diversi da quelli
// registrati si passa la palla al culano/a
// TBC controllo se imponibile deve contenere le spese
else if ((imponibile + imposta) != partita.get_real(PART_IMPORTO) ||
imposta != partita.get_real(PART_IMPOSTA) ||
spese != partita.get_real(PART_SPESE))
{
fromscratch = !yesno_box("Gli importi totali delle scadenze sono stati modificati rispetto"
" all'ultima registrazione. Si desidera mantenere"
" comunque la rateazione precedente");
// nel qual caso, of course, si disabilitano gli automatismi
if(!fromscratch) m.field(FS_RECALC).set("");
}
if (fromscratch)
pag.set_rate_auto();
else
{
pag.zap_rate();
for (int i = 1; i <= partita.rate(); i++)
{
const TRiga_scadenze& scadenza = partita.rata(i);
real importo = scadenza.get(SCAD_IMPORTO);
const TDate scad = scadenza.get(SCAD_DATASCAD);
const int tipop = scadenza.get_int(SCAD_TIPOPAG);
const bool paid = scadenza.get_bool(SCAD_PAGATA);
const TString16 ulc = scadenza.get(SCAD_ULTCLASS);
pag.set_rata(i-1, importo, scad, tipop, ulc, paid);
}
} //else
}
else
return FALSE;
pag.set_sheet(ps);
_pag_rows = ps.rows_array();
// se la prima rata e' fissa non si tocca
if (pag.tipo_prima_rata() > 3)
ps.disable_cell(0,-1);
return TRUE;
}
void TPrimanota_application::write_scadenze(const TMask& m)
{
const int anno = m.get_int(F_ANNORIF);
const TString16 numpart(m.get(F_NUMRIF));
const long nreg = m.get_long(F_NUMREG);
TPartita* newgame = NULL;
if (anno > 0 && numpart.not_empty())
{
real imponibile(0.0);
real imposta(0.0);
real spese(0.0);
const TString16 ndoc(m.get(F_NUMDOC));
const int tmov = causale().tipomov();
const TDate ddoc(m.get(F_DATADOC));
const TDate dreg(m.get(F_DATAREG));
const TString16 reg (causale().reg().name());
const TString80 desc(m.get(F_DESCR));
const long protiva = m.get_long(F_PROTIVA);
const TString16 codcaus(causale().codice());
const TString16 codval (m.get(S_VALUTA));
const real cambio (m.get(S_CAMBIO));
const char sezione = get_cgs_imp(0).sezione(); // Dare/Avere
CHECK(sezione > ' ', "Invalid section in row 0");
const TBill clifo(cgs().row(0), 2, 0x3);
newgame = new TPartita(clifo, anno, numpart);
const int row = newgame->prima_fattura(nreg);
TRiga_partite& partita = row <= 0 ? newgame->new_row() : newgame->riga(row);
TPagamento& pag = *pagamento();
// put data on partita
partita.put(PART_TIPOMOV, tmov);
partita.put(PART_NREG, nreg);
partita.put(PART_NUMRIG, 1);
partita.put(PART_DATAREG, dreg);
partita.put(PART_DATADOC, ddoc);
partita.put(PART_NUMDOC, ndoc);
partita.put(PART_REG, reg);
partita.put(PART_DESCR, desc);
partita.put(PART_PROTIVA, protiva);
partita.put(PART_CODCAUS, codcaus);
partita.put(PART_SEZ, sezione);
partita.put(PART_CODVAL, codval);
partita.put(PART_CAMBIO, cambio);
partita.put(PART_IMPORTO, pag.imponibile()+pag.imposta());
partita.put(PART_IMPOSTA, pag.imposta());
partita.put(PART_SPESE, pag.spese());
partita.elimina_rate();
for (int i = 0; i < pag.n_rate(); i++)
{
TRiga_scadenze& scadenza = partita.new_row();
scadenza.put(SCAD_CODPAG, pag.code());
scadenza.put(SCAD_TIPOPAG, pag.tipo_rata(i));
scadenza.put(SCAD_ULTCLASS, pag.ulc_rata(i));
scadenza.put(SCAD_IMPORTO, pag.tpay_rata(i));
scadenza.put(SCAD_DATASCAD, pag.data_rata(i));
scadenza.put(SCAD_PAGATA, pag.ratapagata(i));
// scadenza.put(SCAD_CODABIPR, pag.codabipr(i)); // TBI
// scadenza.put(SCAD_CODCABPR, pag.codcabpr(i)); // TBI
}
}
TPartita* oldgame = partite().first();
if (oldgame != NULL)
{
const int primafatt = oldgame->prima_fattura(nreg);
if (primafatt > 0)
{
if (*oldgame != *newgame)
{
bool sposta = newgame != NULL;
if (sposta)
{
sposta = (oldgame->conto() == newgame->conto());
if (sposta)
sposta = yesno_box("Spostare le righe nella nuova partita?");
}
if (sposta)
oldgame->sposta_riga(primafatt, *newgame);
else
oldgame->scollega_pagamenti(primafatt);
oldgame->rewrite();
}
else
{
oldgame->sposta_riga(primafatt, *newgame);
}
}
}
if (newgame != NULL)
newgame->write();
}