Patch level : 12.0 310
Files correlati : cg1.exe cg1300m.msk cgmenu.men Commento : Ricostruzione IVA Differita git-svn-id: svn://10.65.10.50/branches/R_10_00@23426 c028cbd2-c16b-5b4b-a496-9718f37d4682
This commit is contained in:
parent
b604499b48
commit
401e5f7f36
@ -1,10 +1,12 @@
|
|||||||
#include <applicat.h>
|
#include <applicat.h>
|
||||||
#include <automask.h>
|
#include <automask.h>
|
||||||
#include <defmask.h>
|
#include <defmask.h>
|
||||||
|
#include <files.h>
|
||||||
#include <isam.h>
|
#include <isam.h>
|
||||||
#include <progind.h>
|
#include <progind.h>
|
||||||
#include <recarray.h>
|
#include <recarray.h>
|
||||||
#include <recset.h>
|
#include <recset.h>
|
||||||
|
#include <reputils.h>
|
||||||
#include <utility.h>
|
#include <utility.h>
|
||||||
#include <validate.h>
|
#include <validate.h>
|
||||||
|
|
||||||
@ -45,8 +47,6 @@ bool TCheck_ivadiff_mask::on_field_event(TOperable_field& o, TField_event e, lon
|
|||||||
{
|
{
|
||||||
case F_CODDITTA:
|
case F_CODDITTA:
|
||||||
break;
|
break;
|
||||||
case F_ANNO:
|
|
||||||
break;
|
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -70,63 +70,39 @@ static bool partita_chiusa_al(const TPartita& p, const TDate& d)
|
|||||||
|
|
||||||
class TCheck_ivadiff : public TSkeleton_application
|
class TCheck_ivadiff : public TSkeleton_application
|
||||||
{
|
{
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool ivadiff_chiusa(const TRectype& mov, const TDate& fine) const;
|
void check_year(long firm);
|
||||||
void check_year(long firm, int year);
|
|
||||||
bool is_date_ok(const TDate& d, int liqmonth, int year) const;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual void main_loop();
|
virtual void main_loop();
|
||||||
};
|
};
|
||||||
|
|
||||||
bool TCheck_ivadiff::ivadiff_chiusa(const TRectype& mov, const TDate& fine) const
|
static void LOG_IVA_DIFF(TLog_report & rep, TTrec & trec, const TRectype& id)
|
||||||
{
|
{
|
||||||
bool chiusa = false;
|
TString msg(300);
|
||||||
const long numreg = mov.get_long(MOV_NUMREG);
|
|
||||||
TLocalisamfile id(LF_IVADIFF);
|
|
||||||
TRectype& rid = id.curr();
|
|
||||||
rid.put(MOV_NUMREG, numreg);
|
|
||||||
int err = id.read(_isgteq);
|
|
||||||
|
|
||||||
if (err == NOERR && rid.get_long(MOV_NUMREG) == numreg)
|
for (int f = 0; f < trec.fields(); f++)
|
||||||
{
|
{
|
||||||
bool some_pag = false; // Ci sono pagamenti?
|
TToken_string tmp = trec.fielddef(f);
|
||||||
TImporto tot;
|
const TString val = id.get(tmp.get(0));
|
||||||
for (; err == NOERR && rid.get_long(MOV_NUMREG) == numreg; err = id.next())
|
const TFieldtypes type = (TFieldtypes) tmp.get_int();
|
||||||
{
|
int len = tmp.get_int(); len = len < 10 ? 10 : len;
|
||||||
const TDate data = rid.get("DATAREGP");
|
|
||||||
if (data > fine)
|
if ((type < _intfld) || (type == _charfld) || (type == _boolfld))
|
||||||
continue;
|
len = -len;
|
||||||
if (data == fine && rid.get_long("NUMREGP") == numreg)
|
|
||||||
continue; // Ignora pagamento automatico dopo un anno
|
const TString16 fmt = format("%%%ds", len);
|
||||||
|
|
||||||
const real imp = rid.get(RMI_IMPOSTA);
|
msg << format(fmt, (const char *) val) << " ";
|
||||||
if (!imp.is_zero())
|
}
|
||||||
{
|
rep.log(0, msg);
|
||||||
const char sez = rid.get_char("SEZIONE");
|
msg.cut(0);
|
||||||
tot += TImporto(sez, imp);
|
for (int i = 0; i < 132; i++)
|
||||||
if (!some_pag && rid.get_int(MOV_TIPOMOV) > 1)
|
msg <<'_';
|
||||||
some_pag = true;
|
rep.log(0, msg);
|
||||||
}
|
|
||||||
}
|
|
||||||
chiusa = some_pag && tot.valore() < 0.01;
|
|
||||||
}
|
|
||||||
return chiusa;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TCheck_ivadiff::is_date_ok(const TDate& d, int liqmonth, int year) const
|
void TCheck_ivadiff::check_year(long firm)
|
||||||
// true se la data passata va considerata nel
|
|
||||||
// ricalcolo dei progressivi mensili per il mese e anno
|
|
||||||
// selezionati. Vedi cg4301.cpp per maggiori informazioni
|
|
||||||
// sul nuovo filtro di selezione movimenti.
|
|
||||||
{
|
|
||||||
const int regyear = d.year();
|
|
||||||
|
|
||||||
return (regyear == year && liqmonth != 12) || (regyear == year + 1 && liqmonth == 12);
|
|
||||||
}
|
|
||||||
|
|
||||||
void TCheck_ivadiff::check_year(long firm, int year)
|
|
||||||
{
|
{
|
||||||
if (!(has_module(SCAUT, CHK_DONGLE) && ini_get_bool(CONFIG_DITTA, "cg", "GesSal")))
|
if (!(has_module(SCAUT, CHK_DONGLE) && ini_get_bool(CONFIG_DITTA, "cg", "GesSal")))
|
||||||
{
|
{
|
||||||
@ -134,13 +110,6 @@ void TCheck_ivadiff::check_year(long firm, int year)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const long old_firm = prefix().get_codditta();
|
|
||||||
|
|
||||||
prefix().set_codditta(firm);
|
|
||||||
|
|
||||||
TString16 key; key.format("%05ld%04d", firm, year);
|
|
||||||
TRectype & lia = (TRectype &) cache().get("%LIA", key);
|
|
||||||
const TString4 freqviva = lia.get("S7");
|
|
||||||
TRelation rel(LF_MOV);
|
TRelation rel(LF_MOV);
|
||||||
rel.add(LF_RMOVIVA,"NUMREG=NUMREG");
|
rel.add(LF_RMOVIVA,"NUMREG=NUMREG");
|
||||||
rel.add(LF_PCON,"GRUPPO=GRUPPO|CONTO=CONTO",1,LF_RMOVIVA,AGR_PCON1);
|
rel.add(LF_PCON,"GRUPPO=GRUPPO|CONTO=CONTO",1,LF_RMOVIVA,AGR_PCON1);
|
||||||
@ -157,10 +126,33 @@ void TCheck_ivadiff::check_year(long firm, int year)
|
|||||||
TCursor reg(®rel);
|
TCursor reg(®rel);
|
||||||
TString4 codreg;
|
TString4 codreg;
|
||||||
long items = reg.items();
|
long items = reg.items();
|
||||||
const TDate fromdate(1, 1, year);
|
TLocalisamfile id(LF_IVADIFF);
|
||||||
const TDate todate(31, 12, year);
|
TRectype& idcurr = id.curr();
|
||||||
TString filter;
|
TString pimsg; pimsg << TR("Ricalcolo IVA differita ditta ") << firm;
|
||||||
|
TLog_report rep(pimsg, "bagn011a");
|
||||||
|
TTrec trec;
|
||||||
|
TToken_string msg;
|
||||||
|
TToken_string tmp;
|
||||||
|
|
||||||
|
rep.set_orientation(2);
|
||||||
|
trec.get(LF_IVADIFF);
|
||||||
|
for (int f = 0; f < trec.fields(); f++)
|
||||||
|
{
|
||||||
|
TToken_string tmp = trec.fielddef(f);
|
||||||
|
const TString val = tmp.get(0);
|
||||||
|
const TFieldtypes type = (TFieldtypes) tmp.get_int();
|
||||||
|
int len = tmp.get_int(); len = len < 10 ? 10 : len;
|
||||||
|
|
||||||
|
if ((type < _intfld) || (type == _charfld) || (type == _boolfld))
|
||||||
|
len = -len;
|
||||||
|
|
||||||
|
const TString16 fmt = format("%%%ds", len);
|
||||||
|
|
||||||
|
msg << format(fmt, (const char *) val) << " ";
|
||||||
|
}
|
||||||
|
rep.log(0, "");
|
||||||
|
rep.log(0, msg);
|
||||||
|
rep.log(0, "");
|
||||||
for (reg = 0L; reg.pos() < items; ++reg)
|
for (reg = 0L; reg.pos() < items; ++reg)
|
||||||
{
|
{
|
||||||
codreg = reg.curr().get("CODTAB").mid(4);
|
codreg = reg.curr().get("CODTAB").mid(4);
|
||||||
@ -173,7 +165,8 @@ void TCheck_ivadiff::check_year(long firm, int year)
|
|||||||
|
|
||||||
from.put(MOV_REG, fromreg);
|
from.put(MOV_REG, fromreg);
|
||||||
to.put(MOV_REG, toreg);
|
to.put(MOV_REG, toreg);
|
||||||
filter.format("BETWEEN(DATAREG,%ld,%ld)&&NUM(LIQDIFF==\"X\")", fromdate.date2ansi(), todate.date2ansi());
|
|
||||||
|
const TString16 filter("LIQDIFF==\"X\"");
|
||||||
|
|
||||||
cur.freeze(false);
|
cur.freeze(false);
|
||||||
cur.setregion(from, to);
|
cur.setregion(from, to);
|
||||||
@ -181,283 +174,319 @@ void TCheck_ivadiff::check_year(long firm, int year)
|
|||||||
items = cur.items();
|
items = cur.items();
|
||||||
cur.freeze();
|
cur.freeze();
|
||||||
|
|
||||||
TString pimsg;
|
{
|
||||||
pimsg << TR("Ricalcolo IVA differita ditta ") << firm << TR(" anno ") << year;
|
TProgress_monitor pi(items, pimsg, false);
|
||||||
|
|
||||||
TProgress_monitor pi(items, pimsg, false);
|
|
||||||
|
|
||||||
for (cur = 0L; cur.pos() < items; ++cur)
|
for (cur = 0L; cur.pos() < items; ++cur)
|
||||||
{
|
{
|
||||||
if (!pi.set_status(cur.pos()))
|
if (!pi.set_status(cur.pos()))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
const TString4 codreg = mov.get("REG");
|
const TDate data = mov.get(MOV_DATAREG);
|
||||||
TString16 s; s << year; s << format("%-3s", (const char *) codreg);
|
const int year = data.year();
|
||||||
const TRectype & reg = cache().get("REG", s);
|
const TDate primo(1, 1, year);
|
||||||
|
const TDate ultimo(31, 12, year);
|
||||||
|
TString16 key; key.format("%05ld%04d", firm, year);
|
||||||
|
TRectype & lia = (TRectype &) cache().get("%LIA", key);
|
||||||
|
const TString4 freqviva = lia.get("S7");
|
||||||
|
const TString4 codreg = mov.get("REG");
|
||||||
|
TString16 s; s << year; s << format("%-3s", (const char *) codreg);
|
||||||
|
const TRectype & reg = cache().get("REG", s);
|
||||||
|
|
||||||
TDate date = mov.get(MOV_DATAREG);
|
const int liqmonth = mov.get_int(MOV_MESELIQ);
|
||||||
const int liqmonth = mov.get_int(MOV_MESELIQ);
|
const TString4 tipodoc = mov.get(MOV_TIPODOC);
|
||||||
const TString4 tipodoc = mov.get(MOV_TIPODOC);
|
const bool corrisp = reg.get_bool("B0");
|
||||||
const bool corrisp = reg.get_bool("B0");
|
const tiporeg tipomov = (tiporeg) reg.get_int("I0"); // 1=Vendite; 2=Acquisti
|
||||||
tipo_movimento tm = (tipo_movimento)mov.get_int(MOV_TIPOMOV);
|
TPartite_array arrpart; // Partite interessate
|
||||||
const tiporeg tipomov = (tiporeg) reg.get_int("I0"); // 1=Vendite; 2=Acquisti
|
TPointer_array pagscatt; // Righe di pagsca interessate
|
||||||
|
|
||||||
|
|
||||||
// Inizio gestione IVA differita
|
|
||||||
bool dok = is_date_ok(date, liqmonth, year);
|
|
||||||
TPartite_array arrpart; // Partite interessate
|
|
||||||
TPointer_array pagscatt; // Righe di pagsca interessate
|
|
||||||
|
|
||||||
if (tm == tm_fattura)
|
|
||||||
{
|
|
||||||
const bool iva_diff = is_IVA_diff(mov.curr());
|
const bool iva_diff = is_IVA_diff(mov.curr());
|
||||||
const bool id_chiusa = ivadiff_chiusa(mov.curr(), TDate(31,12,year-1));
|
const char sezfat = tipomov == vendita ? 'D' : 'A'; // Sezione preferita per fatture decisa in base a vendita->'A' o acquisto->'D'
|
||||||
|
const char sezpag = (sezfat=='D') ? 'A' : 'D';
|
||||||
if (!dok && id_chiusa)
|
const long numreg = mov.get_long(MOV_NUMREG);
|
||||||
continue; // Salta vecchi movimenti differiti già chiusi
|
real tot_incassato, tot_da_incassare;
|
||||||
|
bool game_found = false;
|
||||||
// Sezione preferita per fatture decisa in base a vendita->'A' o acquisto->'D'
|
|
||||||
const char sezfat = tipomov == vendita ? 'D' : 'A';
|
|
||||||
const char sezpag = (sezfat=='D') ? 'A' : 'D';
|
|
||||||
const long numreg = mov.get_long(MOV_NUMREG);
|
|
||||||
real tot_incassato, tot_da_incassare;
|
|
||||||
bool game_found = false;
|
|
||||||
|
|
||||||
arrpart.add_numreg(numreg);
|
arrpart.add_numreg(numreg);
|
||||||
const TPartita* p = arrpart.first();
|
const TPartita* p = arrpart.first();
|
||||||
const int row = p ? p->mov2rig(numreg, 0) : 0;
|
const int row = p ? p->mov2rig(numreg, 0) : 0;
|
||||||
game_found = row > 0;
|
game_found = row > 0;
|
||||||
if (game_found && !id_chiusa)
|
if (game_found)
|
||||||
{
|
{
|
||||||
const TRiga_partite& rp = p->riga(row);
|
const TRiga_partite& rp = p->riga(row);
|
||||||
|
TImporto pg_per, nc_per;
|
||||||
|
const bool chiusa = partita_chiusa_al(*p, ultimo);
|
||||||
|
|
||||||
TDate orizzonte = todate; // caso tradizionale
|
rp.calcola_pagato_periodo(primo, eotime, pg_per, nc_per, &pagscatt);
|
||||||
TImporto pg_per, nc_per;
|
if (pagscatt.items() >= 2)
|
||||||
|
|
||||||
rp.calcola_pagato_periodo(fromdate+1L, orizzonte, pg_per, nc_per, &pagscatt);
|
|
||||||
if (pagscatt.items() >= 2)
|
|
||||||
{
|
|
||||||
// Fondo tra loro le righe generate dallo stesso pagamento
|
|
||||||
for (int p = pagscatt.last(); p > 0; p--)
|
|
||||||
{
|
|
||||||
const TRectype& p0 = (const TRectype&)pagscatt[p];
|
|
||||||
const TRectype& p1 = (const TRectype&)pagscatt[p-1];
|
|
||||||
if (p0.get_int(PAGSCA_NRIGP) == p1.get_int(PAGSCA_NRIGP))
|
|
||||||
{
|
|
||||||
const real imp = p0.get_real(PAGSCA_IMPORTO);
|
|
||||||
((TRectype&)p1).add(PAGSCA_IMPORTO, imp);
|
|
||||||
pagscatt.destroy(p, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (iva_diff)
|
|
||||||
{
|
|
||||||
TPointer_array pagscaold;
|
|
||||||
TImporto pg_tot, nc_tot;
|
|
||||||
|
|
||||||
rp.calcola_pagato_periodo(TDate(0L), orizzonte, pg_tot, nc_tot, &pagscaold);
|
|
||||||
// Controllo se ci siam persi delle note di credito negli anni scorsi
|
|
||||||
if (!nc_tot.is_zero() && pagscaold.items() > pagscatt.items())
|
|
||||||
{
|
|
||||||
FOR_EACH_ARRAY_ITEM(pagscaold, i, obj)
|
|
||||||
{
|
|
||||||
const TRectype& pagsca = *(const TRectype*)obj;
|
|
||||||
const int anno = pagsca.get_int(PAGSCA_ANNO);
|
|
||||||
const int nrigp = pagsca.get_int(PAGSCA_NRIGP);
|
|
||||||
// Appartiene all'anno scorso?
|
|
||||||
if (anno < year && nrigp < 999) // 9999 on pagsca -> 999 on ivadiff
|
|
||||||
{
|
|
||||||
const TPartita& p = arrpart.partita(pagsca);
|
|
||||||
const TRiga_partite& rp = p.riga(nrigp);
|
|
||||||
// E' veramente una nota di credito?
|
|
||||||
if (rp.tipo() == tm_nota_credito)
|
|
||||||
{
|
|
||||||
bool found = false;
|
|
||||||
FOR_EACH_ARRAY_ITEM(pagscatt, j, ps)
|
|
||||||
{
|
|
||||||
if (ps == obj)
|
|
||||||
{
|
|
||||||
found = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!found)
|
|
||||||
{
|
|
||||||
TLocalisamfile id(LF_IVADIFF);
|
|
||||||
id.put(RMI_NUMREG, rmoviva.get(RMI_NUMREG));
|
|
||||||
id.put(RMI_NUMRIG, rmoviva.get(RMI_NUMRIG));
|
|
||||||
id.put("NUMPRO", nrigp);
|
|
||||||
if (id.read(_isequal) != NOERR || id.get_int("ANNOLIQ") == 0)
|
|
||||||
pagscatt.add(obj);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TImporto incasso = pg_tot;
|
|
||||||
TImporto saldo = rp.importo(false);
|
|
||||||
|
|
||||||
incasso += nc_tot;
|
|
||||||
saldo += incasso;
|
|
||||||
saldo.normalize(sezfat);
|
|
||||||
incasso.normalize(sezpag);
|
|
||||||
if (saldo.valore() > ZERO)
|
|
||||||
tot_da_incassare = saldo.valore();
|
|
||||||
if (incasso.valore() > ZERO)
|
|
||||||
tot_incassato = incasso.valore();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
const int rmi_tipoatt = max(rmoviva.get_int(RMI_TIPOATT), 1); // Poteva capitare tipoatt == 0
|
|
||||||
const TString4 codiva = rmoviva.get(RMI_CODIVA);
|
|
||||||
TCodiceIVA civa(codiva);
|
|
||||||
|
|
||||||
if (!civa.ok())
|
|
||||||
{
|
|
||||||
error_box(FR("Codice IVA \"%s\" non riconosciuto alla riga %d del movimento %ld."),
|
|
||||||
(const char*)codiva, rmoviva.get_int(RMI_NUMRIG), rmoviva.get_long(RMI_NUMREG));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const real imponibile_orig = rmoviva.get_real(RMI_IMPONIBILE);
|
|
||||||
const real imposta_orig = rmoviva.get_real(RMI_IMPOSTA);
|
|
||||||
const real lordo_orig = imponibile_orig + imposta_orig;
|
|
||||||
|
|
||||||
|
|
||||||
if (tm == tm_fattura)
|
|
||||||
{
|
|
||||||
const char sezfat = tipomov == vendita ? 'D' : 'A';
|
|
||||||
const TDate datareg = mov.get(MOV_DATAREG);
|
|
||||||
|
|
||||||
TLocalisamfile id(LF_IVADIFF);
|
|
||||||
id.put(RMI_NUMREG, rmoviva.get(RMI_NUMREG));
|
|
||||||
id.put(RMI_NUMRIG, rmoviva.get(RMI_NUMRIG));
|
|
||||||
id.put("NUMPRO", 0);
|
|
||||||
if (id.read(_isequal) != NOERR)
|
|
||||||
{
|
{
|
||||||
id.zero();
|
// Fondo tra loro le righe generate dallo stesso pagamento
|
||||||
id.put(RMI_NUMREG, rmoviva.get(RMI_NUMREG));
|
for (int p = pagscatt.last(); p > 0; p--)
|
||||||
id.put(RMI_NUMRIG, rmoviva.get(RMI_NUMRIG));
|
|
||||||
id.put("NUMPRO", 0);
|
|
||||||
id.put(PART_TIPOMOV, tm);
|
|
||||||
id.put("TIPOATT", rmi_tipoatt);
|
|
||||||
id.put("ANNOLIQ", datareg.year());
|
|
||||||
|
|
||||||
int mesereg = datareg.month();
|
|
||||||
if (freqviva == "T")
|
|
||||||
{
|
{
|
||||||
const int resto = mesereg % 3;
|
const TRectype& p0 = (const TRectype&)pagscatt[p];
|
||||||
if (resto > 0)
|
const TRectype& p1 = (const TRectype&)pagscatt[p-1];
|
||||||
mesereg += 3-resto;
|
if (p0.get_int(PAGSCA_NRIGP) == p1.get_int(PAGSCA_NRIGP))
|
||||||
|
{
|
||||||
|
const real imp = p0.get_real(PAGSCA_IMPORTO);
|
||||||
|
((TRectype&)p1).add(PAGSCA_IMPORTO, imp);
|
||||||
|
pagscatt.destroy(p, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (iva_diff)
|
||||||
|
{
|
||||||
|
TPointer_array pagscaold;
|
||||||
|
TImporto pg_tot, nc_tot;
|
||||||
|
|
||||||
|
rp.calcola_pagato_periodo(TDate(0L), ultimo, pg_tot, nc_tot, &pagscaold);
|
||||||
|
// Controllo se ci siam persi delle note di credito negli anni scorsi
|
||||||
|
if (!nc_tot.is_zero() && pagscaold.items() > pagscatt.items())
|
||||||
|
{
|
||||||
|
FOR_EACH_ARRAY_ITEM(pagscaold, i, obj)
|
||||||
|
{
|
||||||
|
const TRectype& pagsca = *(const TRectype*)obj;
|
||||||
|
const int anno = pagsca.get_int(PAGSCA_ANNO);
|
||||||
|
const int nrigp = pagsca.get_int(PAGSCA_NRIGP);
|
||||||
|
// Appartiene all'anno scorso?
|
||||||
|
if (anno < year && nrigp < 999) // 9999 on pagsca -> 999 on ivadiff
|
||||||
|
{
|
||||||
|
const TPartita& p = arrpart.partita(pagsca);
|
||||||
|
const TRiga_partite& rp = p.riga(nrigp);
|
||||||
|
// E' veramente una nota di credito?
|
||||||
|
if (rp.tipo() == tm_nota_credito)
|
||||||
|
{
|
||||||
|
bool found = false;
|
||||||
|
FOR_EACH_ARRAY_ITEM(pagscatt, j, ps)
|
||||||
|
{
|
||||||
|
if (ps == obj)
|
||||||
|
{
|
||||||
|
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found)
|
||||||
|
{
|
||||||
|
TLocalisamfile id(LF_IVADIFF);
|
||||||
|
id.put(RMI_NUMREG, rmoviva.get(RMI_NUMREG));
|
||||||
|
id.put(RMI_NUMRIG, rmoviva.get(RMI_NUMRIG));
|
||||||
|
id.put("NUMPRO", nrigp);
|
||||||
|
if (id.read(_isequal) != NOERR || id.get_int("ANNOLIQ") == 0)
|
||||||
|
pagscatt.add(obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TImporto incasso = pg_tot;
|
||||||
|
TImporto saldo = rp.importo(false);
|
||||||
|
|
||||||
|
incasso += nc_tot;
|
||||||
|
saldo += incasso;
|
||||||
|
saldo.normalize(sezfat);
|
||||||
|
incasso.normalize(sezpag);
|
||||||
|
if (saldo.valore() > ZERO)
|
||||||
|
tot_da_incassare = saldo.valore();
|
||||||
|
if (incasso.valore() > ZERO)
|
||||||
|
tot_incassato = incasso.valore();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TISAM_recordset recset("USE IVADIFF\nFROM NUMREG=#NR\nTO NUMREG=#NR");
|
||||||
|
recset.set_var("#NR", numreg);
|
||||||
|
TLocalisamfile& id = recset.cursor()->file();
|
||||||
|
for (bool ok = recset.move_first(); ok; ok = recset.move_next())
|
||||||
|
{
|
||||||
|
const bool idchiusa = id.get_bool("CHIUSA");
|
||||||
|
|
||||||
|
if (idchiusa != chiusa)
|
||||||
|
{
|
||||||
|
id.put("CHIUSA", chiusa);
|
||||||
|
id.rewrite();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
do
|
||||||
|
{
|
||||||
|
const int rmi_tipoatt = max(rmoviva.get_int(RMI_TIPOATT), 1); // Poteva capitare tipoatt == 0
|
||||||
|
const TString4 codiva = rmoviva.get(RMI_CODIVA);
|
||||||
|
TCodiceIVA civa(codiva);
|
||||||
|
|
||||||
|
if (!civa.ok())
|
||||||
|
{
|
||||||
|
error_box(FR("Codice IVA \"%s\" non riconosciuto alla riga %d del movimento %ld."),
|
||||||
|
(const char*)codiva, rmoviva.get_int(RMI_NUMRIG), rmoviva.get_long(RMI_NUMREG));
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
id.put("MESELIQ", mesereg);
|
const real imponibile_orig = rmoviva.get_real(RMI_IMPONIBILE);
|
||||||
id.put("TIPOIVA", tipomov == 2 ? 2 : 1);
|
const real imposta_orig = rmoviva.get_real(RMI_IMPOSTA);
|
||||||
id.put("TIPODIFF", 1);
|
const real lordo_orig = imponibile_orig + imposta_orig;
|
||||||
id.put(MOV_DATAREG, mov.get(MOV_DATADOC));
|
char sezfat = (tipomov == vendita) ? 'D' : 'A';
|
||||||
id.put(RMI_CODIVA, codiva);
|
const TDate datareg = mov.get(MOV_DATAREG);
|
||||||
id.put("SEZIONE", sezfat);
|
const int numrig = rmoviva.get_int(RMI_NUMRIG);
|
||||||
|
|
||||||
id.put("IMPORTO", imponibile_orig+imposta_orig);
|
|
||||||
id.put(RMI_IMPONIBILE, imponibile_orig);
|
|
||||||
id.put(RMI_IMPOSTA, imposta_orig);
|
|
||||||
|
|
||||||
const int ew = id.write();
|
id.put(RMI_NUMREG, numreg);
|
||||||
if (ew != NOERR)
|
id.put(RMI_NUMRIG, numrig);
|
||||||
cantwrite_box(id.description());
|
id.put("NUMPRO", 0);
|
||||||
}
|
if (id.read(_isequal) != NOERR)
|
||||||
}
|
|
||||||
|
|
||||||
if (!pagscatt.empty())
|
|
||||||
{
|
|
||||||
const char sezpag = tipomov == vendita ? 'A' : 'D';
|
|
||||||
real totfat = mov.get(MOV_TOTDOC);
|
|
||||||
real tot = totfat;
|
|
||||||
|
|
||||||
TLocalisamfile id(LF_IVADIFF);
|
|
||||||
TRectype& idcurr = id.curr();
|
|
||||||
FOR_EACH_ARRAY_ITEM(pagscatt, r, obj)
|
|
||||||
{
|
|
||||||
const TRectype& pagsca = *(TRectype*)obj;
|
|
||||||
const int nrigp = pagsca.get_int(PAGSCA_NRIGP);
|
|
||||||
|
|
||||||
id.zero();
|
|
||||||
idcurr.put(RMI_NUMREG, rmoviva.get(RMI_NUMREG));
|
|
||||||
idcurr.put(RMI_NUMRIG, rmoviva.get(RMI_NUMRIG));
|
|
||||||
CHECKD(nrigp > 0, "Invalid NRIGP ", nrigp);
|
|
||||||
idcurr.put("NUMPRO", min(nrigp, 999));
|
|
||||||
if (id.read(_isequal, _lock) != NOERR)
|
|
||||||
{
|
{
|
||||||
id.zero();
|
id.zero();
|
||||||
idcurr.put(RMI_NUMREG, rmoviva.get(RMI_NUMREG));
|
id.put(RMI_NUMREG, numreg);
|
||||||
idcurr.put(RMI_NUMRIG, rmoviva.get(RMI_NUMRIG));
|
id.put(RMI_NUMRIG, numrig);
|
||||||
idcurr.put("NUMPRO", min(nrigp, 999));
|
id.put("NUMPRO", 0);
|
||||||
idcurr.put(MOV_DATAREG, mov.get(MOV_DATADOC));
|
id.put(PART_TIPOMOV, tm_fattura);
|
||||||
idcurr.put("TIPOATT", rmi_tipoatt);
|
id.put("TIPOATT", rmi_tipoatt);
|
||||||
idcurr.put("TIPOIVA", tipomov == 2 ? 2 : 1);
|
id.put("ANNOLIQ", datareg.year());
|
||||||
idcurr.put("TIPODIFF", 1);
|
int mesereg = datareg.month();
|
||||||
idcurr.put(RMI_CODIVA, codiva);
|
|
||||||
|
|
||||||
TImporto pagtmp;
|
|
||||||
bool ultimo = false;
|
|
||||||
tipo_movimento tipomov_pag = tm_pagamento;
|
|
||||||
if (nrigp > 0)
|
|
||||||
{
|
|
||||||
const TPartita& p = arrpart.partita(pagsca);
|
|
||||||
const TRiga_partite& rp = p.riga(nrigp);
|
|
||||||
tipomov_pag = rp.tipo();
|
|
||||||
idcurr.put(PART_TIPOMOV, tipomov_pag);
|
|
||||||
idcurr.put("NUMREGP", rp.get(PART_NREG));
|
|
||||||
idcurr.put("NUMRIGP", rp.get(PART_NUMRIG));
|
|
||||||
TDate d = rp.get(PART_DATAPAG);
|
|
||||||
if (!d.ok())
|
|
||||||
d = rp.get(PART_DATADOC);
|
|
||||||
if (!d.ok())
|
|
||||||
d = rp.get(PART_DATAREG);
|
|
||||||
idcurr.put("DATAREGP", d);
|
|
||||||
idcurr.put("ANNOLIQ", d.year());
|
|
||||||
pagtmp = p.importo_pagsca(pagsca);
|
|
||||||
pagtmp.normalize(sezpag);
|
|
||||||
ultimo = (r == pagscatt.last()) && partita_chiusa_al(p, todate);
|
|
||||||
}
|
|
||||||
|
|
||||||
int meseliq = id.get_date("DATAREGP").month();
|
|
||||||
if (freqviva == "T")
|
if (freqviva == "T")
|
||||||
{
|
{
|
||||||
const int resto = meseliq % 3;
|
const int resto = mesereg % 3;
|
||||||
if (resto > 0)
|
if (resto > 0)
|
||||||
meseliq += 3-resto;
|
mesereg += 3-resto;
|
||||||
}
|
}
|
||||||
idcurr.put("MESELIQ", meseliq);
|
|
||||||
idcurr.put("SEZIONE", pagtmp.sezione());
|
|
||||||
|
|
||||||
real val_imp = pagtmp.valore() * lordo_orig / tot;
|
id.put("MESELIQ", mesereg);
|
||||||
real val_iva = civa.scorpora(val_imp, TCurrency::get_firm_dec());
|
id.put("TIPOIVA", tipomov == 2 ? 2 : 1);
|
||||||
|
id.put("TIPODIFF", 1);
|
||||||
idcurr.put("IMPORTO", val_imp + val_iva);
|
id.put(MOV_DATAREG, mov.get(MOV_DATADOC));
|
||||||
idcurr.put(RMI_IMPONIBILE, val_imp);
|
id.put(RMI_CODIVA, codiva);
|
||||||
idcurr.put(RMI_IMPOSTA, val_iva);
|
id.put("SEZIONE", sezfat);
|
||||||
|
|
||||||
if (id.write() != NOERR)
|
id.put("IMPORTO", imponibile_orig+imposta_orig);
|
||||||
cantwrite_box(id.description());
|
id.put(RMI_IMPONIBILE, imponibile_orig);
|
||||||
|
id.put(RMI_IMPOSTA, imposta_orig);
|
||||||
|
|
||||||
|
if (id.write() == NOERR)
|
||||||
|
LOG_IVA_DIFF(rep, trec, id.curr());
|
||||||
}
|
}
|
||||||
}
|
if (!pagscatt.empty())
|
||||||
}
|
{
|
||||||
} while (cur.next_match(LF_RMOVIVA));
|
const char sezpag = tipomov == vendita ? 'A' : 'D';
|
||||||
|
real totfat = mov.get(MOV_TOTDOC);
|
||||||
|
real tot = totfat;
|
||||||
|
|
||||||
|
FOR_EACH_ARRAY_ITEM(pagscatt, r, obj)
|
||||||
|
{
|
||||||
|
const TRectype& pagsca = *(TRectype*)obj;
|
||||||
|
const int nrigp = pagsca.get_int(PAGSCA_NRIGP);
|
||||||
|
const long numreg = rmoviva.get_long(RMI_NUMREG);
|
||||||
|
const int numrig = rmoviva.get_int(RMI_NUMRIG);
|
||||||
|
const TPartita& p = arrpart.partita(pagsca);
|
||||||
|
const TRiga_partite& rp = p.riga(nrigp);
|
||||||
|
const long nregpag = rp.get_long(PART_NREG);
|
||||||
|
const int nrigpag = rp.get_int(PART_NUMRIG);
|
||||||
|
TImporto pagtmp = p.importo_pagsca(pagsca);
|
||||||
|
pagtmp.normalize(sezpag);
|
||||||
|
|
||||||
|
real val_imp = pagtmp.valore() * lordo_orig / tot;
|
||||||
|
real val_iva;
|
||||||
|
|
||||||
|
if (r == pagscatt.last() && chiusa)
|
||||||
|
{
|
||||||
|
TLocalisamfile rid(LF_IVADIFF);
|
||||||
|
|
||||||
|
rid.put(MOV_NUMREG, numreg);
|
||||||
|
rid.put(RMI_NUMRIG, numrig);
|
||||||
|
val_iva = imposta_orig;
|
||||||
|
|
||||||
|
bool pag_found = false;
|
||||||
|
|
||||||
|
int err = rid.read(_isgteq);
|
||||||
|
|
||||||
|
for (; err == NOERR ; err = rid.next())
|
||||||
|
{
|
||||||
|
const long ridnumreg =rid.get_long(RMI_NUMREG);
|
||||||
|
const int ridnumrig = rid.get_int(RMI_NUMRIG);
|
||||||
|
const tipo_movimento ridtipopag = (tipo_movimento) rid.get_int(PART_TIPOMOV);
|
||||||
|
|
||||||
|
if (ridnumreg == numreg && ridnumrig == numrig)
|
||||||
|
{
|
||||||
|
if ((ridtipopag != tm_fattura) && ((rid.get_long("NUMREGP") != nregpag) &&
|
||||||
|
(rid.get_int("NUMRIGP") != nrigpag)))
|
||||||
|
{
|
||||||
|
pag_found = true;
|
||||||
|
if (sezfat != sezpag)
|
||||||
|
val_iva -= rid.get_real(RMI_IMPOSTA);
|
||||||
|
else
|
||||||
|
val_iva += rid.get_real(RMI_IMPOSTA);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
if (!pag_found)
|
||||||
|
val_iva = civa.scorpora(val_imp, TCurrency::get_firm_dec());
|
||||||
|
else
|
||||||
|
civa.scorpora(val_imp, TCurrency::get_firm_dec());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
val_iva = civa.scorpora(val_imp, TCurrency::get_firm_dec());
|
||||||
|
|
||||||
|
id.zero();
|
||||||
|
id.put(RMI_NUMREG, numreg);
|
||||||
|
id.put(RMI_NUMRIG, numrig);
|
||||||
|
CHECKD(nrigp > 0, "Invalid NRIGP ", nrigp);
|
||||||
|
id.put("NUMPRO", nrigp);
|
||||||
|
if (id.read(_isequal, _lock) != NOERR)
|
||||||
|
{
|
||||||
|
id.zero();
|
||||||
|
id.put(RMI_NUMREG, numreg);
|
||||||
|
id.put(RMI_NUMRIG, numrig);
|
||||||
|
id.put("NUMPRO", nrigp);
|
||||||
|
id.put(MOV_DATAREG, mov.get(MOV_DATADOC));
|
||||||
|
id.put("TIPOATT", rmi_tipoatt);
|
||||||
|
id.put("TIPOIVA", tipomov == 2 ? 2 : 1);
|
||||||
|
id.put("TIPODIFF", 1);
|
||||||
|
id.put(RMI_CODIVA, codiva);
|
||||||
|
|
||||||
|
tipo_movimento tipomov_pag = tm_pagamento;
|
||||||
|
|
||||||
|
if (nrigp <= 0)
|
||||||
|
fatal_box("numero riga pagamento errato partita : %s/%s - %d", (const char *) pagsca.get(PAGSCA_ANNO),
|
||||||
|
(const char *) pagsca.get(PAGSCA_NUMPART),
|
||||||
|
nrigp);
|
||||||
|
TDate d = rp.get(PART_DATAPAG);
|
||||||
|
|
||||||
|
tipomov_pag = rp.tipo();
|
||||||
|
id.put(PART_TIPOMOV, tipomov_pag);
|
||||||
|
id.put("NUMREGP", rp.get(PART_NREG));
|
||||||
|
id.put("NUMRIGP", rp.get(PART_NUMRIG));
|
||||||
|
if (!d.ok())
|
||||||
|
d = rp.get(PART_DATADOC);
|
||||||
|
if (!d.ok())
|
||||||
|
d = rp.get(PART_DATAREG);
|
||||||
|
id.put("DATAREGP", d);
|
||||||
|
id.put("ANNOLIQ", d.year());
|
||||||
|
|
||||||
|
int meseliq = d.month();
|
||||||
|
|
||||||
|
if (freqviva == "T")
|
||||||
|
{
|
||||||
|
const int resto = meseliq % 3;
|
||||||
|
if (resto > 0)
|
||||||
|
meseliq += 3-resto;
|
||||||
|
}
|
||||||
|
id.put("MESELIQ", meseliq);
|
||||||
|
id.put("SEZIONE", pagtmp.sezione());
|
||||||
|
id.put("IMPORTO", val_imp + val_iva);
|
||||||
|
id.put(RMI_IMPONIBILE, val_imp);
|
||||||
|
id.put(RMI_IMPOSTA, val_iva);
|
||||||
|
|
||||||
|
if (id.write() == NOERR)
|
||||||
|
LOG_IVA_DIFF(rep, trec, id.curr());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (cur.next_match(LF_RMOVIVA));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
prefix().set_codditta(firm);
|
rep.print_or_preview();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TCheck_ivadiff::main_loop()
|
void TCheck_ivadiff::main_loop()
|
||||||
{
|
{
|
||||||
TCheck_ivadiff_mask m;
|
TCheck_ivadiff_mask m;
|
||||||
while (m.run() == K_ENTER)
|
while (m.run() == K_ENTER)
|
||||||
check_year(m.get_long(F_CODDITTA), m.get_int(F_ANNO));
|
check_year(m.get_long(F_CODDITTA));
|
||||||
}
|
}
|
||||||
|
|
||||||
void controlla_ivadiff(int argc, char* argv[])
|
void controlla_ivadiff(int argc, char* argv[])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user