Patch level : 12.0 no-patch

Files correlati     : cg
Commento            : Aggiunta classe TMovimentoPN mancante in cglib
This commit is contained in:
Simone Palacino 2020-07-22 15:43:06 +02:00
parent 9d8e2fd6ce
commit 60ad8e3432

View File

@ -19,6 +19,7 @@
#include <rmov.h>
#include <comuni.h>
#include "cgsaldac.h"
///////////////////////////////////////////////////////////
// Causale
@ -1674,3 +1675,391 @@ const char* num2str(const TString& s)
}
return (const char*)str;
}
///////////////////////////////////////////////////////////
// Movimento di prima nota
///////////////////////////////////////////////////////////
TMovimentoPN::TMovimentoPN()
: TRelation(LF_MOV), _cg(LF_RMOV, RMV_NUMRIG), _iva(LF_RMOVIVA, RMI_NUMRIG), _old_iva(LF_RMOVIVA, RMI_NUMRIG)
{
add(LF_RMOV, "NUMREG=NUMREG");
add(LF_RMOVIVA, "NUMREG=NUMREG");
}
void TMovimentoPN::destroy_rows(long num)
{
_cg.destroy_rows();
_cg.renum_key(RMV_NUMREG, num);
_iva.destroy_rows();
_iva.renum_key(RMI_NUMREG, num);
}
TRectype& TMovimentoPN::cg(int i)
{
return _cg.row(i >= 0 ? i + 1 : -1, true);
}
TRectype& TMovimentoPN::iva(int i)
{
return _iva.row(i >= 0 ? i + 1 : -1, true);
}
void TMovimentoPN::destroy_cg_row(int i)
{
if (i < 0)
_cg.destroy_rows();
else
_cg.destroy_row(i + 1, true);
}
void TMovimentoPN::destroy_iva_row(int i)
{
if (i < 0)
_iva.destroy_rows();
else
_iva.destroy_row(i + 1, true);
}
void TMovimentoPN::update_rev_charge()
{
const int year = lfile().get_int(MOV_ANNOIVA);
const TString & codcaus = lfile().get(MOV_CODCAUS);
const TCausale & caus = cached_causale(codcaus, year);
const TipoIVA t = caus.iva();
if (t == iva_acquisti)
{
const bool rev_charge = caus.reverse_charge_pubb();
if (rev_charge)
{
int rows = _iva.rows();
real imp_revcharge;
bool has_revcharge = false;
for (int i = _iva.first_row(); !has_revcharge && i <= rows; i = _iva.succ_row(i))
{
has_revcharge |= _iva[i].get_bool(RMI_REVCHARGE);
imp_revcharge += _iva[i].get_real(RMI_IMPOSTA);
}
if (!has_revcharge)
{
TRectype & h = head();
if (h.get_real(MOV_REVCHARGE) <= ZERO)
{
h.put(MOV_REVCHARGE, imp_revcharge);
h.sub(MOV_RITFIS, imp_revcharge);
if (h.get_real(MOV_RITFIS) < ZERO)
h.zero(MOV_RITFIS);
}
for (int i = _iva.first_row(); i <= rows; i = _iva.succ_row(i))
_iva[i].put(RMI_REVCHARGE, true);
}
}
}
}
int TMovimentoPN::read_mov_rows()
{
const TRectype& mov = curr();
const long numreg = mov.get_long(MOV_NUMREG);
TRectype* cgfilter = new TRectype(LF_RMOV);
cgfilter->put(RMV_NUMREG, numreg);
_cg.read(cgfilter);
TRectype* ivafilter = new TRectype(LF_RMOVIVA);
ivafilter->put(RMI_NUMREG, numreg);
_iva.read(ivafilter);
update_rev_charge();
_old_iva = _iva;
/*
if (_cg.rows() > 0 && _iva.rows() > 0 && cg(0).get_char(RMV_ROWTYPE) != 'T')
adjust_row_types();
*/
return _cg.rows();
}
int TMovimentoPN::read(TIsamop op, TReclock lockop)
{
const int err = TRelation::read(op, lockop);
if (err == NOERR)
{
_olddataliq = file().get(MOV_DATAREG); // Memorizza data liquidazione
const int meseliq = file().get_int(MOV_MESELIQ);
if (meseliq > 0 && meseliq != _olddataliq.month())
{
_olddataliq.set_day(1); // Evita problemi coi mesi corti!
_olddataliq.set_month(meseliq);
}
read_mov_rows(); // Riempie i due record array
}
return err;
}
char TMovimentoPN::frequenza_versamenti(int year) const
{
static int last_year = 0;
static long last_firm = 0;
static char last_freq = ' ';
const long firm = prefix().get_codditta();
if (firm != last_firm || year != last_year)
{
TString16 key; key.format("%05ld%d", firm, year);
TTable lia("%LIA");
lia.put("CODTAB", key);
if (lia.read() != NOERR)
{
TLocalisamfile nditte(LF_NDITTE);
nditte.put("CODDITTA", firm);
nditte.read();
last_freq = nditte.get_char("FREQVIVA");
}
else
last_freq = lia.get_char("S7");
if (last_freq != 'M' && last_freq != 'T')
{
error_box(FR("La frequenza versamenti IVA per la ditta %ld\n"
"non e' valida: la si considera mensile."), firm);
last_freq = 'M';
}
last_firm = firm;
last_year = year;
}
return last_freq;
}
int TMovimentoPN::date2liq(const TDate& data) const
{
const int anno = data.year();
int mese = data.month();
if (frequenza_versamenti(anno) == 'T')
mese += 2 - ((mese - 1) % 3);
return mese;
}
bool TMovimentoPN::controlla_liquidazione(const TDate& data, TRegistro& registro, bool reset) const
{
bool calcolata = false;
const int anno = data.year();
const int mese = date2liq(data);
// Chiave di LIM: Anno (1-4), Mese (5-6)
TString16 key; key.format("%04d%02d", anno, mese);
TTable lim("LIM");
lim.setkey(1);
lim.put("CODTAB", key);
if (lim.read() == NOERR)
{
calcolata = data.month() <= registro.mese_stampa_ultima_liq(); // Controlla se progressivi ricalcolati (registri)
if (reset)
{
// Resetta i flag di calcolato sulla liquidazione IVA del mese di registrazione
lim.zero("B0"); // calcolato
lim.rewrite();
}
}
if (reset)
{
const bool att_mista = registro.name().empty() ? FALSE : registro.attivita_mista();
const int att = att_mista ? 2 : 1;
// Chiave di PLM: Anno (1-4), Cod. Att. (5-9), Tipo att. (10-10), Mese (11-12)
TTable plm("PLM");
for (int a = 1; a <= att; a++)
{
TString16 chiave;
TString8 attivita(registro.attivita()); attivita.right_just(5, '0');
TString4 mese; mese.format("%02d", data.month());
chiave << data.year() << attivita << a << mese;
plm.put("CODTAB", chiave);
if (plm.read() == NOERR)
{
const bool calcolato = plm.get_bool("B0");
if (calcolato)
{
plm.zero("B0");
plm.rewrite();
}
}
}
}
return calcolata;
}
int TMovimentoPN::registra(bool re, bool force)
{
const TRectype& m = curr();
long numreg = m.get_long(MOV_NUMREG);
if (numreg <= 0)
{
if (!re) // Tento di numerare automaticamente in caso di write
{
TLocalisamfile mov(LF_MOV); // Non sposto il file principale della relazione!
numreg = 1;
if (mov.last() == NOERR)
numreg += mov.get_long(MOV_NUMREG);
curr().put(MOV_NUMREG, numreg);
}
else
return _isnocurkey;
}
int err = re ? TRelation::rewrite(force) : TRelation::write(force);
if (err != NOERR)
return err;
_cg.renum_key(MOV_NUMREG, numreg);
err = _cg.write(re);
if (err != NOERR)
return err;
const int annoiva = m.get_int(MOV_ANNOIVA);
const TString4 reg(m.get(MOV_REG));
TRegistro registro(reg, annoiva);
const bool att_mista = reg.empty() ? false : registro.attivita_mista();
update_rev_charge();
for (int i = 0; i < iva_items(); i++)
{
TRectype& r = iva(i);
int tipoatt = 1;
if (att_mista)
{
const char tipo = r.get_char(RMI_TIPOC);
if (tipo <= ' ')
{
TBill c(r.get_int(RMI_GRUPPO), r.get_int(RMI_CONTO), r.get_long(RMI_SOTTOCONTO));
tipoatt = c.tipo_att();
}
}
r.put(RMI_TIPOATT, tipoatt);
const TString & indetr = r.get(RMI_TIPODET);
if (indetr.full())
{
const TRectype& det = cache().get("%DET", indetr);
if (!det.empty() && !det.get_bool("FPC"))
{
TTable tab("%DET");
tab.curr() = det;
tab.curr().put("FPC", "X");
tab.rewrite();
}
}
}
_iva.renum_key(MOV_NUMREG, numreg);
err = _iva.write(re);
if (err != NOERR)
return err;
// Aggiorna data registrazione e protocollo IVA sul registro
const TDate datareg(m.get(MOV_DATAREG));
if (reg.full())
{
const long protiva = m.get_long(MOV_PROTIVA);
const long uprotiva = m.get_long(MOV_UPROTIVA);
const long max = protiva > uprotiva ? protiva : uprotiva;
registro.update(max, datareg);
}
// Aggiorna flags di ricalcolo liquidazione
TDate dataliq(datareg);
const int mese_liq = m.get_int(MOV_MESELIQ);
if (mese_liq > 0 && mese_liq != dataliq.month())
{
dataliq.set_day(1); // Evita problemi coi mesi corti!
dataliq.set_month(mese_liq);
}
bool reset = !re;
if (reg.full())
{
reset = (dataliq.month() != _olddataliq.month() || _old_iva != _iva);
if (dataliq.month() != _olddataliq.month())
controlla_liquidazione(_olddataliq, registro, true);
}
else
{
const TCausale causale(m.get(MOV_CODCAUS), annoiva);
if (causale.saldaconto(datareg) && causale.tipomov() != tm_fattura)
{
TPartite_array partarray;
partarray.add_numreg(numreg);
const int npart = partarray.items();
for (TPartita * part = partarray.first(); !reset && part != nullptr; part = partarray.next())
{
const int nrpart = part->last();
for (int r = part->prima_fattura(); !reset && r >= 0 && r <= nrpart; r = part->succ(r))
{
TRiga_partite & rp = part->riga(r);
if (rp.is_fattura())
{
const TRectype & mov = cache().get(LF_MOV, rp.get(PART_NREG));
reset = mov.get_bool(MOV_LIQDIFF) || mov.get_bool(MOV_IVAXCASSA);
}
}
}
}
}
if (reset)
controlla_liquidazione(dataliq, registro, reset);
return err;
}
int TMovimentoPN::write(bool force)
{
return registra(FALSE, force);
}
int TMovimentoPN::rewrite(bool force)
{
return registra(true, force);
}
int TMovimentoPN::remove()
{
int err = _cg.remove();
if (err == NOERR)
err = _iva.remove();
if (err == NOERR)
err = TRelation::remove();
if (err == NOERR)
{
const TRectype& m = curr();
const TString4 reg(m.get(MOV_REG));
const int annoiva = m.get_int(MOV_ANNOIVA);
TRegistro registro(reg, annoiva);
controlla_liquidazione(_olddataliq, registro, true);
}
return err;
}