From 401e5f7f36b234f9ac278e5a29e3ad70c147f93c Mon Sep 17 00:00:00 2001 From: bonazzi Date: Fri, 9 Dec 2016 23:03:02 +0000 Subject: [PATCH] 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 --- src/cg/cg1312.cpp | 643 ++++++++++++++++++++++++---------------------- 1 file changed, 336 insertions(+), 307 deletions(-) diff --git a/src/cg/cg1312.cpp b/src/cg/cg1312.cpp index c254ce2f9..597e45b58 100644 --- a/src/cg/cg1312.cpp +++ b/src/cg/cg1312.cpp @@ -1,10 +1,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include @@ -45,8 +47,6 @@ bool TCheck_ivadiff_mask::on_field_event(TOperable_field& o, TField_event e, lon { case F_CODDITTA: break; - case F_ANNO: - break; default: break; } return true; @@ -70,63 +70,39 @@ static bool partita_chiusa_al(const TPartita& p, const TDate& d) class TCheck_ivadiff : public TSkeleton_application { - protected: - bool ivadiff_chiusa(const TRectype& mov, const TDate& fine) const; - void check_year(long firm, int year); - bool is_date_ok(const TDate& d, int liqmonth, int year) const; + void check_year(long firm); public: 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; - 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); + TString msg(300); - if (err == NOERR && rid.get_long(MOV_NUMREG) == numreg) + for (int f = 0; f < trec.fields(); f++) { - bool some_pag = false; // Ci sono pagamenti? - TImporto tot; - for (; err == NOERR && rid.get_long(MOV_NUMREG) == numreg; err = id.next()) - { - const TDate data = rid.get("DATAREGP"); - if (data > fine) - continue; - if (data == fine && rid.get_long("NUMREGP") == numreg) - continue; // Ignora pagamento automatico dopo un anno - - const real imp = rid.get(RMI_IMPOSTA); - if (!imp.is_zero()) - { - const char sez = rid.get_char("SEZIONE"); - tot += TImporto(sez, imp); - if (!some_pag && rid.get_int(MOV_TIPOMOV) > 1) - some_pag = true; - } - } - chiusa = some_pag && tot.valore() < 0.01; - } - return chiusa; + TToken_string tmp = trec.fielddef(f); + const TString val = id.get(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, msg); + msg.cut(0); + for (int i = 0; i < 132; i++) + msg <<'_'; + rep.log(0, msg); } -bool TCheck_ivadiff::is_date_ok(const TDate& d, int liqmonth, int year) const - // 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) +void TCheck_ivadiff::check_year(long firm) { 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; } - 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); rel.add(LF_RMOVIVA,"NUMREG=NUMREG"); 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); TString4 codreg; long items = reg.items(); - const TDate fromdate(1, 1, year); - const TDate todate(31, 12, year); - TString filter; + TLocalisamfile id(LF_IVADIFF); + TRectype& idcurr = id.curr(); + 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) { 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); 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.setregion(from, to); @@ -181,283 +174,319 @@ void TCheck_ivadiff::check_year(long firm, int year) items = cur.items(); 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) - { - if (!pi.set_status(cur.pos())) - break; + for (cur = 0L; cur.pos() < items; ++cur) + { + if (!pi.set_status(cur.pos())) + break; - const TString4 codreg = mov.get("REG"); - TString16 s; s << year; s << format("%-3s", (const char *) codreg); - const TRectype & reg = cache().get("REG", s); + const TDate data = mov.get(MOV_DATAREG); + const int year = data.year(); + 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 TString4 tipodoc = mov.get(MOV_TIPODOC); - const bool corrisp = reg.get_bool("B0"); - tipo_movimento tm = (tipo_movimento)mov.get_int(MOV_TIPOMOV); - const tiporeg tipomov = (tiporeg) reg.get_int("I0"); // 1=Vendite; 2=Acquisti - - - // 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 int liqmonth = mov.get_int(MOV_MESELIQ); + const TString4 tipodoc = mov.get(MOV_TIPODOC); + const bool corrisp = reg.get_bool("B0"); + const tiporeg tipomov = (tiporeg) reg.get_int("I0"); // 1=Vendite; 2=Acquisti + TPartite_array arrpart; // Partite interessate + TPointer_array pagscatt; // Righe di pagsca interessate const bool iva_diff = is_IVA_diff(mov.curr()); - const bool id_chiusa = ivadiff_chiusa(mov.curr(), TDate(31,12,year-1)); - - if (!dok && id_chiusa) - continue; // Salta vecchi movimenti differiti già chiusi - - // 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; + 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'; + const long numreg = mov.get_long(MOV_NUMREG); + real tot_incassato, tot_da_incassare; + bool game_found = false; - arrpart.add_numreg(numreg); - const TPartita* p = arrpart.first(); + arrpart.add_numreg(numreg); + const TPartita* p = arrpart.first(); const int row = p ? p->mov2rig(numreg, 0) : 0; - game_found = row > 0; - if (game_found && !id_chiusa) - { + game_found = row > 0; + if (game_found) + { 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 - TImporto pg_per, nc_per; - - 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) + rp.calcola_pagato_periodo(primo, eotime, pg_per, nc_per, &pagscatt); + if (pagscatt.items() >= 2) { - id.zero(); - id.put(RMI_NUMREG, rmoviva.get(RMI_NUMREG)); - 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") + // Fondo tra loro le righe generate dallo stesso pagamento + for (int p = pagscatt.last(); p > 0; p--) { - const int resto = mesereg % 3; - if (resto > 0) - mesereg += 3-resto; + 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), 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); - id.put("TIPOIVA", tipomov == 2 ? 2 : 1); - id.put("TIPODIFF", 1); - id.put(MOV_DATAREG, mov.get(MOV_DATADOC)); - id.put(RMI_CODIVA, codiva); - id.put("SEZIONE", sezfat); - - id.put("IMPORTO", imponibile_orig+imposta_orig); - id.put(RMI_IMPONIBILE, imponibile_orig); - id.put(RMI_IMPOSTA, imposta_orig); + 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; + char sezfat = (tipomov == vendita) ? 'D' : 'A'; + const TDate datareg = mov.get(MOV_DATAREG); + const int numrig = rmoviva.get_int(RMI_NUMRIG); - const int ew = id.write(); - if (ew != NOERR) - cantwrite_box(id.description()); - } - } - - 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.put(RMI_NUMREG, numreg); + id.put(RMI_NUMRIG, numrig); + id.put("NUMPRO", 0); + if (id.read(_isequal) != NOERR) { id.zero(); - idcurr.put(RMI_NUMREG, rmoviva.get(RMI_NUMREG)); - idcurr.put(RMI_NUMRIG, rmoviva.get(RMI_NUMRIG)); - idcurr.put("NUMPRO", min(nrigp, 999)); - idcurr.put(MOV_DATAREG, mov.get(MOV_DATADOC)); - idcurr.put("TIPOATT", rmi_tipoatt); - idcurr.put("TIPOIVA", tipomov == 2 ? 2 : 1); - idcurr.put("TIPODIFF", 1); - 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(); + id.put(RMI_NUMREG, numreg); + id.put(RMI_NUMRIG, numrig); + id.put("NUMPRO", 0); + id.put(PART_TIPOMOV, tm_fattura); + id.put("TIPOATT", rmi_tipoatt); + id.put("ANNOLIQ", datareg.year()); + int mesereg = datareg.month(); if (freqviva == "T") { - const int resto = meseliq % 3; + const int resto = mesereg % 3; 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; - real val_iva = civa.scorpora(val_imp, TCurrency::get_firm_dec()); - - idcurr.put("IMPORTO", val_imp + val_iva); - idcurr.put(RMI_IMPONIBILE, val_imp); - idcurr.put(RMI_IMPOSTA, val_iva); - - if (id.write() != NOERR) - cantwrite_box(id.description()); + id.put("MESELIQ", mesereg); + id.put("TIPOIVA", tipomov == 2 ? 2 : 1); + id.put("TIPODIFF", 1); + id.put(MOV_DATAREG, mov.get(MOV_DATADOC)); + id.put(RMI_CODIVA, codiva); + id.put("SEZIONE", sezfat); + + id.put("IMPORTO", imponibile_orig+imposta_orig); + id.put(RMI_IMPONIBILE, imponibile_orig); + id.put(RMI_IMPOSTA, imposta_orig); + + if (id.write() == NOERR) + LOG_IVA_DIFF(rep, trec, id.curr()); } - } - } - } while (cur.next_match(LF_RMOVIVA)); + if (!pagscatt.empty()) + { + 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() { TCheck_ivadiff_mask m; 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[])