From 6c28b4a0ab632b58d09359e1755079af2bd68bf5 Mon Sep 17 00:00:00 2001 From: guy Date: Fri, 13 Dec 2013 09:24:56 +0000 Subject: [PATCH] Collegamento cg <-> fe git-svn-id: svn://10.65.10.50/branches/R_10_00@22901 c028cbd2-c16b-5b4b-a496-9718f37d4682 --- cg/cg0100.cpp | 2 +- cg/cg0100a.uml | 6 +- cg/cg0200a.uml | 3 +- cg/cg2100.cpp | 32 +- cg/cg2100.h | 5 +- cg/cg2100c.uml | 66 +- cg/cg2101.cpp | 2 +- cg/cg3600.cpp | 21 +- cg/cg4301.cpp | 8341 ++++++++++++++++++++++++----------------------- cg/cg4400.cpp | 2 +- cg/cglib03.cpp | 1 - cg/cgmenu.men | 8 +- cg/cgsaldac.cpp | 21 +- cg/f22.dir | 2 +- cg/f22.trr | 25 +- cg/f23.dir | 6 +- cg/f23.trr | 9 +- 17 files changed, 4262 insertions(+), 4290 deletions(-) diff --git a/cg/cg0100.cpp b/cg/cg0100.cpp index d172c1917..e790ad5bf 100755 --- a/cg/cg0100.cpp +++ b/cg/cg0100.cpp @@ -203,7 +203,7 @@ void TPianoconti_app::init_insert_mode(TMask& m) m.show(FLD_CM1_IVACOMP, indbil == RICAVI); m.show(FLD_CM1_DESCIVACOMP, indbil == RICAVI); } - m.show(FLD_CM1_RICSER, indbil == PASSIVITA || indbil == RICAVI); + // m.show(FLD_CM1_RICSER, indbil == PASSIVITA || indbil == RICAVI); // Sempre visibile per spesometro dal 9-10-2013 if (conto != 0) { m.enable(FLD_CM1_SEZIVD, ivd_enable); diff --git a/cg/cg0100a.uml b/cg/cg0100a.uml index f590a5e28..34105cd3b 100755 --- a/cg/cg0100a.uml +++ b/cg/cg0100a.uml @@ -420,11 +420,11 @@ END LIST FLD_CM1_RICSER 1 18 BEGIN - PROMPT 1 12 "Ricavi per servizi " + PROMPT 1 12 "Merce / Servizi " FIELD RICSER - FLAGS "H" - ITEM "0|Altre attivita'" + ITEM "0|" ITEM "1|Servizi" + ITEM "2|Merce" END STRING FLD_CM1_IVACOMP 4 diff --git a/cg/cg0200a.uml b/cg/cg0200a.uml index 1e70c89e1..cdff54b89 100755 --- a/cg/cg0200a.uml +++ b/cg/cg0200a.uml @@ -288,7 +288,7 @@ BEGIN CHECKTYPE NORMAL END -NUMBER F_ALLEG 1 +STRING F_ALLEG 1 BEGIN PROMPT 2 14 "Tipo cliente/fornitore " FIELD ALLEG @@ -305,6 +305,7 @@ BEGIN ITEM "7|Ente pubblico" ITEM "8|Fallimento/proc.concorsuale" ITEM "9|Estero NON CEE" + ITEM "C|Scheda carburante" OUTPUT F_ALLEG END diff --git a/cg/cg2100.cpp b/cg/cg2100.cpp index c50f732e8..ead5d6985 100755 --- a/cg/cg2100.cpp +++ b/cg/cg2100.cpp @@ -585,14 +585,14 @@ void TPrimanota_application::init_mask(TMask& m) m.show(-2, forg); } - m.hide(-7); // Nascondi tutti i campi del modulo FE (compreso FE_MODPAG) + m.hide(-7); // Nascondi tutti i campi del modulo FE if (corrisp) m.hide(F_STATOPAIV); else { if (has_module(FEAUT)) - m.show(FE_MODPAG); // Mostro FE_MODPAG solo se strettamente necessario + m.show(FE_NOLEGGIO); // Mostro FE_MODPAG solo se strettamente necessario } // Show/Hide campi valuta: F_VALUTAINTRA, F_CAMBIOINTRA, F_CORRISPETTIVO, F_CORRVAL (GROUP 4) @@ -716,8 +716,8 @@ bool TPrimanota_application::can_I_write(const TRelation* rel) const << "La preghiamo di contattare Sirio informatica e sistemi ai seguenti riferimenti:


" << "" + << "
  • Casella vocale Hotline Campo:
    " + << "hotlinecampo@sirio-is.it Tel. 0236583540
  • " << ""; warning_box(html); } @@ -2261,28 +2261,28 @@ bool TPrimanota_application::link_intra(const TMask& m, const char* action) TString str; // Stringa jolly di lavoro str.format("%d", LF_INTRA); intro.set_paragraph(str); - intro.set("NUMREG", m.get(F_NUMREG)); - intro.set("DATAREG", m.get(F_DATAREG)); + intro.set(MOV_NUMREG, m.get(F_NUMREG)); + intro.set(MOV_DATAREG, m.get(F_DATAREG)); if (m.field(F_CLIENTE).shown()) { - intro.set("TIPOMOV", "C"); - intro.set("TIPOCF", "C"); - intro.set("CODCF", m.get(F_CLIENTE)); + intro.set(MOV_TIPOMOV, "C"); // Cessione + intro.set("TIPOCF", "C"); // Cliente + intro.set(MOV_CODCF, m.get(F_CLIENTE)); } else { - intro.set("TIPOMOV", "A"); - intro.set("TIPOCF", "F"); - intro.set("CODCF", m.get(F_FORNITORE)); + intro.set(MOV_TIPOMOV, "A"); // Acquisto + intro.set("TIPOCF", "F"); // Fornitore + intro.set(MOV_CODCF, m.get(F_FORNITORE)); } // Controlla flag sulla causale const bool valintra = causale().valintra(); if (valintra) { - intro.set("CODVAL", m.get(F_VALUTAINTRA)); - intro.set("CAMBIO", m.get(F_CAMBIOINTRA)); + intro.set(MOV_CODVAL, m.get(F_VALUTAINTRA)); + intro.set(MOV_CAMBIO, m.get(F_CAMBIOINTRA)); } // Inserisci il totale documento solo in inserimento! @@ -2291,10 +2291,10 @@ bool TPrimanota_application::link_intra(const TMask& m, const char* action) real totdoc = m.get_real(valintra ? F_CORRISPETTIVO : F_IMPONIBILI); if (test_swap(false)) totdoc = -totdoc; - intro.set("TOTDOC", totdoc.string()); + intro.set(MOV_TOTDOC, totdoc.string()); } else - intro.remove("TOTDOC"); // Evita di cambiare il valore preesistente + intro.remove(MOV_TOTDOC); // Evita di cambiare il valore preesistente appname << "in0 -4 /i" << intrini; } diff --git a/cg/cg2100.h b/cg/cg2100.h index 63b029eef..c2c4388e2 100755 --- a/cg/cg2100.h +++ b/cg/cg2100.h @@ -28,10 +28,7 @@ #define F_COFICLIENTE 155 #define F_RICALTCLIENTE 156 -#define FE_CONTRATTO 157 -#define FE_MODPAG 158 -#define FE_DATARETT 159 -#define FE_NUMRETT 160 +#define FE_NOLEGGIO 157 #define F_FORNITORE 161 #define F_RAGSOCFORNITORE 162 diff --git a/cg/cg2100c.uml b/cg/cg2100c.uml index 22f94ccfc..c49d7528d 100755 --- a/cg/cg2100c.uml +++ b/cg/cg2100c.uml @@ -439,65 +439,17 @@ BEGIN FLAGS "H" END -LIST FE_MODPAG 1 16 +LIST FE_NOLEGGIO 2 10 BEGIN - PROMPT 1 12 "Tipo " - ITEM "1|Non frazionato" - MESSAGE HIDE,FE_CONTRATTO|RESET,FE_CONTRATTO|SHOW,FE_DATARETT|SHOW,FE_NUMRETT - ITEM "2|Frazionato" - MESSAGE SHOW,FE_CONTRATTO|HIDE,FE_DATARETT|HIDE,FE_NUMRETT - ITEM "3|Periodico" - MESSAGE SHOW,FE_CONTRATTO|HIDE,FE_DATARETT|HIDE,FE_NUMRETT + PROMPT 1 12 "Tipo veicolo a noleggio (spesometro) " + ITEM " |Nessuno" + ITEM "A|Automobile" + ITEM "B|Camper" + ITEM "C|Altro" + ITEM "D|Diporto" + ITEM "E|Aeromobile" + FIELD NOLEGGIO GROUP 7 - FIELD MODPAG - MODULE fe -END - -STRING FE_CONTRATTO 18 -BEGIN - PROMPT 32 12 "Contratto di riferimento " - FLAGS "U" - FIELD CONTRATTO - USE &FECON - INPUT CODTAB[1,1] F_CLIFO SELECT - INPUT CODTAB[2,7] F_CLIENTE SELECT - INPUT CODTAB[8,25] FE_CONTRATTO - DISPLAY "Contratto" CODTAB[8,] - DISPLAY "Descrizione@50" S0 - DISPLAY "Pagamento" S6 - OUTPUT FE_CONTRATTO CODTAB[8,] - OUTPUT FE_MODPAG S6 - CHECKTYPE REQUIRED - HELP "Inserire un codice contratto di importo non inferiore a 3000 Euro" - ADD RUN fe0 -1 &CON - GROUP 7 -END - -DATE FE_DATARETT -BEGIN - PROMPT 26 12 "Variazione al documento " - USE MOV KEY 3 - INPUT TIPO F_CLIFO SELECT - INPUT CODCF F_CLIENTE SELECT - INPUT DATAREG FE_DATARETT - DISPLAY "Data reg.@10" DATAREG - DISPLAY "Numero@7" NUMDOC - DISPLAY "Num.Reg.@7" NUMREG - DISPLAY "Descrizione@50" DESCR - OUTPUT FE_DATARETT DATAREG - OUTPUT FE_NUMRETT NUMDOC - FIELD DATARETT - GROUP 7 -END - -STRING FE_NUMRETT 7 -BEGIN - PROMPT 66 12 "N." - COPY ALL FE_DATARETT - CHECKTYPE SEARCH - FIELD NUMRETT - GROUP 7 - FLAGS "U" END // Campi per saldaconto in valuta diff --git a/cg/cg2101.cpp b/cg/cg2101.cpp index 91f378096..8e82a54b6 100755 --- a/cg/cg2101.cpp +++ b/cg/cg2101.cpp @@ -260,7 +260,7 @@ int TMovimentoPN::registra(bool re, bool force) // Aggiorna data registrazione e protocollo IVA sul registro const TDate datareg(m.get(MOV_DATAREG)); - if (reg.not_empty()) + if (reg.full()) { const long protiva = m.get_long(MOV_PROTIVA); const long uprotiva = m.get_long(MOV_UPROTIVA); diff --git a/cg/cg3600.cpp b/cg/cg3600.cpp index db724606f..b42a1a11f 100755 --- a/cg/cg3600.cpp +++ b/cg/cg3600.cpp @@ -826,7 +826,6 @@ TGrid_control::TGrid_control( // Controlla se ci sono troppe colonne fisse if ((f_width+max_width)*XI_FU_MULTIPLE > rct.right) fixed_columns = 1; - long list_attr = XI_ATR_ENABLED | XI_ATR_VISIBLE; // if (_read_only) list_attr |= XI_ATR_NAVIGATE; @@ -1092,8 +1091,8 @@ void TGrid_control::update_selection(XI_EVENT* xiev) { const bool is_curr = xiev->v.rec_request.data_rec == _cur_rec; if (_read_only) - { -/* + { +/* if (is_curr) xiev->v.rec_request.attrib |= XI_ATR_SELECTED; else @@ -1180,11 +1179,9 @@ bool TGrid_control::event_handler(XI_OBJ* itf, XI_EVENT *xiev) { if (xiev->v.select.column == 0) { - if (_read_only) - { - refused = !select(rec); - } - else +// if (_read_only) // Commentato 8/11/2013 altrimenti non funziona più collegamento a cg2 +// refused = !select(rec); +// else { if (rec == _cur_rec) // Simulo intercettazione doppio click _grid->on_record_button(rec); @@ -1309,7 +1306,7 @@ XI_OBJ* TGrid_control::find_column(const char* head) const int num = 0; XI_OBJ** column = xi_get_member_list(_obj, &num); - TString80 text; + TString256 text; int i; for (i = num-1; i >= 0; i--) { @@ -1708,9 +1705,9 @@ void TMastrini_grid::cell_request(long rec, short id, TGrid_cell& cell) XI_OBJ* col = grid().find_column(short(1000+id%1000)); if (col != NULL) { - TString80 head; - xi_get_text(col, head.get_buffer(), head.size()); - cell = head; + TString256 str; + xi_get_text(col, str.get_buffer(), str.size()); + cell = str; } return; diff --git a/cg/cg4301.cpp b/cg/cg4301.cpp index ca06b5a6a..a91e88d6b 100755 --- a/cg/cg4301.cpp +++ b/cg/cg4301.cpp @@ -1,4166 +1,4175 @@ -// ------------------------------------------------------------ -// Calcolo liquidazioni -// Part 2: calcolo -// fv 21-1-94 -// ------------------------------------------------------------ - -#include -#include -#include -#include -#include -#include - -#include "cg4300.h" -#include "cgsaldac.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -// -------------------- QUI comincia l'avventura -------------------------- -// Datemi un punto di appoggio ****************** -// e mi ci appoggero' ****************** -// ----------------------------------------------------- ****************** - -bool TLiquidazione_app::recalc_all() -{ - TString msg; - msg = _printonly ? TR("Stampa") : TR("Calcolo"); - msg << TR(" liquidazione:\npreparazione archivi..."); - _prind = new TProgind(_selected.ones()*2, msg, true, true); - - for (int l = 0; l < _ditte->items(); l++) - { - if (_prind->iscancelled()) - break; - - if (_selected[(long)l]) - { - TToken_string& nomeditta = _ditte->row(l); - const long codditta = nomeditta.get_long(1); - const int save_month = _month; - - /* ------------------------------------------------------------- - * leggi parametri ditta necessari; stabilisci se annuale, - * se si' metti _month a 13 - * istanzia _freqviva, _isbenzinaro, _isannual, _isriepilogo - * -------------------------------------------------------------- - */ - - _reg_arr.destroy(); // Azzera la cache dei registri per ogni ditta. - _nditte->zero(); - _nditte->curr().put("CODDITTA",codditta); - _nditte->read(); - _freqviva = nomeditta.get(3); - const bool mens = _freqviva == "M"; - - // determina attivita' prevalente e istanzia cazzuole - // per vedere che Kazzo di liquidazione calcolare - const TString8 attprev = _nditte->curr().get("CODATTPREV"); - TString16 key; - - key.format("%ld|%s", codditta, (const char *)attprev); - - const TRectype & atts = cache().get(LF_ATTIV, key); - // istanzia benzinaro - _isbenzinaro = atts.get_bool("ART74_4"); - - // riaggiusta relazione - _nditte->read(); - - // strasemplificato: per l'annuale si sceglie annuale, altrimenti - // e' la 12ma anche per le trimestrali. - _isannual = _isriepilogo = _month == 13; - - TApplication::set_firm(codditta); - - // Imposta metodo arrotondamento imposte - TIva_round ir; - ir.set_default_iva_mode(atoi(_year), _isannual, codditta); - - _prind->addstatus(1); - - /* - * trimestrali impropriamente selezionate per mesi intermedi - * provocano il ricalcolo dei progressivi mensili ma non la stampa - */ - _canprint = is_month_ok_strict(_month) || _month == 13; - - /* -------------------------------------------------------------- - * Ricalcola i mesi necessari: annuale e' un mese unico da 1/1 al 31/12 - * -------------------------------------------------------------- - */ - - int need_refresh = false; - if (_recalc != ever) - { - int m = _month == 13 ? _month : 1; - for (; m < _month; m++) - if (is_month_ok_strict(m) && (!look_lim(m) || !_lim->get_bool("B0"))) - { - need_refresh = true; - break; - } - - if (need_refresh && yesno_box(TR("Alcuni mesi precedenti non " - "risultano ricalcolati. E' consigliabile il ricalcolo. " - "Si desidera eseguirlo?"))) - _recalc = ever; - } - int m; - for (m = 1; m <= _month; m++) // fino a 13 compreso - { - if (is_month_plain(m) || _recalc == ever) - { - if (_prind->iscancelled()) - break; - update_firm(m); - } - } - - // se ci sono altri mesi dopo l'ultimo calcolato, invalida il - // flag 'calcolato' del primo, per causare il ricalcolo dei - // successivi (evitando problemi per credito precedente) - for (m = _month+1; m <= 13; m++) if (look_lim(m)) - { - _lim->zero("B0"); - _lim->rewrite(); - break; - } - - _month = save_month; - } - } - bool canc = _prind->iscancelled(); - if (!canc) _prind->addstatus(1); - - TApplication::set_firm(__firm); - delete _prind; _prind = NULL; - - return !canc; -} - -bool TLiquidazione_app::update_firm(int month, bool recalc) -{ - // Se recalc e' TRUE considera la necessita' di ricalcolare e le - // opzioni utente. Se FALSE se ne impipa dell'utente e ricalcola - // se necessario (serve nelle chiamate ricorsive) - // Ritorna FALSE soltanto se il ricalcolo era necessario e non e' - // stato effettuato per scelta dello stronzo commercialista. - - _isdiff = ini_get_bool(CONFIG_DITTA, "cg", "GesLiqDiff"); - - if (month == 13 && recalc) - { - TTable pem("PEM"); - for (int reg = 0; reg < 23; reg++) - { - look_pem(pem, reg); - pem.zero("R0"); - pem.zero("R1"); - pem.rewrite(); - } - } - - look_lim(liq_month(month), TRUE); - _isdiffacc = is_differita(); // Gestione differimento acconto IVA - _isdifferita = _isdiffacc; - - look_lia(); - _isintr = _freqviva == "T" && !_lia->get_bool("B3"); // was CNF_DITTA->InTrtr - _gest4 = _isbenzinaro && _lia->get_bool("B4"); // was CNF_DITTA->Gest74 - - const bool has_single_activity = count_activities() == 1; - - // controlla che il periodo corrente non sia l'inizio dell'attivita' - // nel caso, differita va a FALSE - _monthinatt = 1; - const TDate inatt = _nditte->curr().get(NDT_DINIZIOATT); - if (is_in_liq_period(inatt)) - _isdifferita = false; - if (inatt.year() == atoi(_year)) - _monthinatt = inatt.month(); - - _isricacq = (month == 13) && ini_get_bool(CONFIG_STUDIO, "cg", "RicAcq"); - - // ricalcolo normale - // lim c'e' solo per i trimestri - bool ok = _lim->get_bool("B0"); - if (ok && !recalc) - return true; - if (_recalc_regis) - ok = false; // Se sta ricalcolando i PRM/PRP se ne sbatte delle LIM - bool calc = (_recalc == ever || (_recalc == one && is_month_ok(month,_month))); - if (!calc && _recalc != never) calc = !ok; - bool gheravergot = false; - bool quater = false; - - TToken_string atts; - TToken_string cattivs; - - // casini per stampa minchie in coda ai registri - bool riepliq = _isregis ? _riepilogo : FALSE; - bool stliq = TRUE; - - _nditte->save_status(); - if (_nditte->is_first_match(LF_ATTIV)) - { - const TRectype& ndt_att = _nditte->curr(LF_ATTIV); - do - { - const TString8 codatt = ndt_att.get("CODATT"); - const TString80 desatt = ndt_att.get("DESCR"); - quater = _nditte->curr().get_bool("FLIVA11Q"); - - // attività mista: ce ne sono in realtà due - // viene calcolato nel ciclo su tipoatt (che viene ripetuto solo se diventa TRUE); - _mixed = false; - - if (_prind) - { - TString buf(80); - buf.format( _printonly ? FR("Stampa liquidazione (mese %d %d)...\n%s: %s\n") : - FR("Calcolo liquidazione (mese %d %d)...\n%s: %s\n"), - month, atoi(_year), (const char*)_nditte_r->get(NDT_RAGSOC), (const char*)desatt); - _prind->set_text(buf); - } - - // se ricalcola l'annuale si tiene tutte le vendite e corrispettivi di - // tutti i lerci mesi. Analogamente se sta calcolando una trimestrale. - // Nel caso di trimestrale con ricalcolo trimestri precedenti azzera - // ad ogni inizio di trimestre. - if (!(_freqviva == "T" && (month != 1 && month != 4 && month != 7 && month != 10 && month != 13))) - { - _vend_arr.destroy(); - _corr_arr.destroy(); - } - - cattivs = ""; - - for (int tipoatt = 1; tipoatt <= (_mixed ? 2 : 1); tipoatt++) - { - TString8 cattiv(codatt); - cattiv << tipoatt; - - const bool waspla = look_pla(cattiv, has_single_activity); - if (!waspla) - break; // Non calcolare attività inesistenti 29-11-2012 - - if (tipoatt == 1 && waspla) - { - _p8 = _pla->get_real("R5"); - _p8b = _pla->get_real("R6"); - _p9 = _pla->get_real("R7"); - _isplafond = !(_p8.is_zero() && _p8b.is_zero() && _p9.is_zero()); - const char s7 = _pla->get_char("S7"); - _isservizio = (s7 == 'S'); - _mixed = (s7 == 'M') || (s7 == 'E'); - - const TString4 yr(_year); - TAssoc_array& pa = _prorata.perc_array(); - - pa.destroy(); - pa.add("DEF", _pla->get_real("R8")); // Percentuale di default, anno corrente - - const int anno = atoi(_year) - 2; - - for (int i = atoi(_year); i>=anno; i--) - { - _year.format("%d", i); - if (look_pla(cattiv, false)) // Reperisce percentuale prorata anno indicato (se esiste la tabella) - pa.add(_year, _pla->get_real("R8")); - } - _year = yr; // Risetta l'anno corretto e riposiziona la tabella... - look_pla(cattiv, false); - } - else - _isservizio = ndt_att.get("TIPOATT") == "S"; - - _isviaggio = ndt_att.get_bool("REG74TER"); - _isagricolo = ndt_att.get_bool("REGAGR"); - _isagr98 = _isagricolo && atoi(_year) >= 1998; - _isvent = false; - - if (!_recalc_regis && tipoatt == 1 && waspla && month == 13) - { - // azzera pla dove serve - _pla->put("R0",""); - _pla->put("R1",""); - _pla->put("R2",""); - _pla->put("R3",""); - _pla->put("R4",""); - _pla->put("R13",""); - _pla->put("R9",""); - _pla->put("R10",""); - _pla->put("R11",""); - _pla->put("R12",""); - _pla->put("R14",""); - _pla->put("R15",""); - _pla->put("R16",""); - _pla->put("R17",""); - _pla->put("S1","0"); - _pla->put("S2","0"); - _pla->put("S3","0"); - _pla->rewrite(); - } - - for (_reg->first(); _reg->good(); _reg->next()) - { - if (codatt == _reg->get("S8")) - { - if (_reg->get_int("I0") == 2) - if (!_isvent) _isvent = _reg->get_bool("B3"); - } - } - - if (!_isregis) - stliq = riepliq = true; - - if (_comp_acconto) - stliq = riepliq = false; - - if (calc || !recalc) - if (!update_att(month, cattiv) && stliq) - describe_error(TR("Attività non ricalcolate: possibili errori"), - codatt); - if (_recalc_regis) - continue; - - if (month == 13) - recalc_annual(cattiv); - - // vediamo se c'e' qualcosa da raccontare - gheravergot = _isannual || _isriepilogo; - if (!gheravergot) - { - look_plm(month,cattiv); - gheravergot = !_plm->get_real("R0").is_zero() || !_plm->get_real("R1").is_zero(); - } - - if (month == _month && riepliq) - describe_att(month, cattiv, gheravergot, '0' + (_mixed ? tipoatt : 0)); - - atts.add(cattiv); - cattivs.add(cattiv); - } // for tipoatt - - if (_recalc_regis) - continue; - - // se attività mista stampa riepilogo - if (_mixed && month == _month && riepliq) - describe_att(month,cattivs, TRUE, 'M'); - - // se sta calcolando l'annuale, scrive tutti i dati calcolati per il trasferimento - // sui files adibiti a mantenere le informazioni per IVA 11: tab1100a e tab1100b - if (_is_interactive && month == 13) - iva11_set_arr_phase_2(codatt); // scorre tutti i maledetti PIM di questa ditta/attivita' per completare _iva11_arr - } - while (_nditte->next_match(LF_ATTIV)); - - if (_recalc_regis) - { - _nditte->restore_status(); - // Se trattasi di ricalcolo per registri, setta il flag relativo, onde evitare ricalcoli - // nel caso di registro riepilogativo: se i PRM esistono già (perchè creati - // da stampa bollato precedente o altro riepilogo) non vanno ricalcolati. - look_lim(month, TRUE); // Crea se non esiste - _lim->put("B1","X"); - _lim->rewrite(); - return TRUE; - } - - // se quater stampa riepilogo - if (quater && month == _month && riepliq) - describe_att(month,atts, TRUE, 'Q'); - - // occorre poterla chiamare altre volte con mesi diversi - _nditte->restore_status(); - - // aggiorna le liquidazioni - //13/12/1995. - //Concordato con Ferdinando che, visto che qualche - //calcoluccio viene fatto anche in write_liq, essa - //verra' sempre richiamata. - //if (calc || !recalc) - //fine - - // Se siamo in annuale e se non e' stato indicato di ricalcolare (stampa liquidazione chiamata - // dalla stampa registri) non riscrivere i risultati delle liquidazioni, - if ((is_month_ok_strict(month) && _month != 13 && _recalc != never) || _is_interactive /*month == 13*/ ) - write_liq(month, atts); - - // occorre poterla chiamare altre volte con mesi diversi - _nditte->restore_status(); - - // rimborso infraannuale - _DescrItem* rimb_d = NULL; - if (month == _month) - { - // unica nel suo genere, recalc_rimborso ritorna - // un bel descritem (NULL se non si rimborsa un cas) - // da passare a describe_liq - rimb_d = recalc_rimborso(month,atts, stliq); - if (stliq) - { - describe_firm(month); - //describe_liq(_month, atts, _isregis ? NULL : rimb_d); - describe_liq(_month, atts, rimb_d); - } - if (_month == 13 && look_lim(_month)) // scrive i risultati solo in annuale - { - real r0 = _lim->get_real("R0"); - iva11_write(rimb_d == NULL || r0.sign() >= 0); // scrive su tab1100 at last. - } - } - - //in caso di stampa registri e in caso sia - //stato calcolato l'acconto (metodo analitico) - //deve essere stampato il prospetto di liquidazione - //dell'acconto in coda al resto - if (_isregis && stliq) - { - if ((month == 12 && _freqviva == "M") || - (month == 12 && _isbenzinaro && _gest4) || - (month == 13 && _freqviva == "T" && !_isbenzinaro) || - (month == 13 && _isbenzinaro && !_gest4)) - describe_liqacc(); - } - } - return ok || calc; -} - -bool TLiquidazione_app::update_att(int month, const char* codatt, bool recalc) - // viene passato un codice attivita' con codatt+tipoatt - // vedi update_firm per il burdel dei calc e recalc - // occhecasino. Se _comp_acconto e' TRUE, i movimenti considerati - // saranno solo quelli di dicembre per data <= 20/12; - // il ricalcolo e' FORZATO da _recalc messo a one -{ - look_plm(month, codatt, true); - bool ok = _plm->get_bool("B0"); - if (_recalc_regis) - ok = FALSE; // Se sta ricalcolando i PRM/PRP se ne sbatte dei PLM - if (ok && !recalc) - return true; - bool calc = _recalc == ever || (_recalc == one && is_month_ok(month,_month)); - if (!calc && _recalc != never) - calc = !ok; - - if (calc || !recalc) - { - zero_att(month,codatt); - recalc_att(month,codatt); - if (!_recalc_regis) - { - // Ricalcolo ventilazione e ag. viaggio: - // - tutti i mesi se e' mensile - // - nei mesi 3,6,9,12,13 se trimestrale - if (_freqviva == "M" || (_freqviva == "T" && (is_trim(month) || month == 13))) - { - recalc_ventilation(month, codatt); - recalc_viaggio(month, codatt); - } - recalc_corrispettivi(month, codatt); - } - } - - return ok || calc; -} - -void TLiquidazione_app::zero_att(int month, const char* codatt) -{ - TTable *arr[4] = { _pim, _pis, _prm, _prp }; - const char* flds[] = { "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", - "R10", "R11", "R12", "R13", "R14", "R15", "R16", "R17", "R18", "R19", - "R20", "R21", "R22", "R23", "R24", "R25", "R26", "R27", "R28", "R29", - "R30", "R31", "R32", "R33", - "S0", "S1", "S2", NULL }; - - TString80 codtab; - TString16 att; - TString4 year; - int m, start, stop; - - start = 0; stop = 1; - - if (_recalc_regis) - { - // Se stiamo ricalcolando per registri(_recalc_regis) azzera PRM e PRP - start = 2; stop = 3; - } - else - if (atoi(_year) < 1998) - stop = 0; // Solo PIM - // PIM / PIS - for (int i = start; i <= stop; i++) // Ciclo per le tabelle da azzerare - { - TTable * tab = (TTable*) arr[i]; - CHECK(tab, "Invalid table element"); - for (tab->first(); !tab->eof(); tab->next()) // Ciclo per i record di tabella - { - codtab = tab->get("CODTAB"); - m = atoi(codtab.mid(13,2)); - att = codtab.mid(4,6); - year = codtab.mid(0,4); - if (m == month && att == codatt && year == _year) - { - for (int j = 0; flds[j]; j++) // Ciclo per i campi del record da azzerare (34 + 3) - tab->zero(flds[j]); - tab->rewrite(); - } - } - } - - if (!_recalc_regis) - { - if (look_plm(month, codatt)) - { - // zero PLM, POM, PAM, PUM - const TString16 codtab(_plm->get("CODTAB")); - _plm->zero(); - _pom->zero(); - _pam->zero(); - _pum->zero(); - _plm->put("CODTAB", codtab); - _pom->put("CODTAB", codtab); - _pam->put("CODTAB", codtab); - _pum->put("CODTAB", codtab); - _plm->rewrite(); - _pam->rewrite(); - _pom->rewrite(); - _pum->rewrite(); - } - - // Azzera i progressivi per il prospetto agricolo dal 1998 - if (_isagr98) - { - for (_pia->first(); !_pia->eof(); _pia->next()) - { - const int m = atoi(*_pia_mese); - const TString8 att = (const char*)*_pia_codatt; - if (m == month && att == codatt && (_year == *_pia_anno)) - { - _pia->zero("R0"); // Imponibile - _pia->zero("R1"); // Imposta - _pia->rewrite(); - } - } - } - } - if (_isplafond && month != 13) - zero_plafond(month,codatt); - - if (month != 13 && !_recalc_regis) // Aggiunto test per evitare azzeramenti in stampa registri 19-06-2013 - zero_diff(month,codatt); -} - -bool TLiquidazione_app::ivadiff_chiusa(const TRectype& mov, const TDate& fine) const -{ - const long numreg = mov.get_long(MOV_NUMREG); - - TString query; - query << "USA IVADIFF SELECT BETWEEN(DATAREGP,0," << fine.date2ansi() << ')' - << "\nFROM NUMREG=" << numreg - << "\nTO NUMREG=" << numreg; - TISAM_recordset id(query); - const TRectype& rid = id.cursor()->curr(); - - bool some_pag = false; // Ci sono pagamenti? - TImporto tot; - for (bool ok = id.move_first(); ok; ok = id.move_next()) - { - const char sez = rid.get_char("SEZIONE"); - const real imp = rid.get(RMI_IMPOSTA); - if (!imp.is_zero()) - { - if (rid.get_int(MOV_TIPOMOV) > 1) - some_pag = true; - tot += TImporto(sez, imp); - } - } - const bool chiusa = some_pag && tot.valore() < 0.01; - return chiusa; -} - -// Fattura più vecchia di un anno a ente NON pubblico -bool TLiquidazione_app::sarebbe_da_pagare(const TRectype& mov, const TDate& fine) const -{ - if (mov.get_int(MOV_TIPOMOV) != tm_fattura) - return false; - - const TDate datareg = mov.get(MOV_DATAREG); - const long giorni = fine - datareg; - if (giorni < 0) - return false; - - if (mov.get_bool(MOV_LIQDIFF)) - { - const TDate datainc = mov.get(MOV_DATAINC); - if (datainc.ok() && datainc <= fine) - return false; // Già pagata senza saldaconto - } else - if (mov.get_bool(MOV_IVAXCASSA)) - { - if (!gestione_IVAxCassa(fine) || _isviaggio) // Le agenzie viaggio non posso aderire al regime IVA per cassa - return true; // Ho superato la soglia di applicabilità dell'IVA per cassa - } - else - return true; // Regime IVA normale - - TString8 key; key.format("%c|%ld", mov.get_char(MOV_TIPO), mov.get_long(MOV_CODCF)); - const TRectype& clifo = cache().get(LF_CLIFO, key); - const int alleg = clifo.get_int(CLI_ALLEG); - - if (alleg == 7 || alleg == 8) // Ente pubblico e amministrazione controllata ... - return false; // ... possono aspettare le calende greche - - if (mov.get_bool(MOV_IVAXCASSA)) - { - if (alleg == 5 || alleg == 6 || alleg == 9) // Privati, import ed export non hanno IVA per cassa - return true; - - TString16 paiv = clifo.get(CLI_PAIV); - if (paiv.empty() && clifo.get_bool(CLI_OCCAS)) - { - const TRectype& occas = cache().get(LF_OCCAS, mov.get(MOV_OCFPI)); - paiv = occas.get(OCC_PAIV); - } - if (paiv.blank()) - return true; // In assenza di Partita IVA non c'è IVA per cassa - } - - if (ivadiff_chiusa(mov, fine)) - return false; - - if (giorni >= 365) - return true; // E' passato più di un anno! - - return false; -} - -bool TLiquidazione_app::residuo_da_liquidare(long numreg, int numrig, const TDate& last_pag, - real& importo_res, real& imponib_res, real& imposta_res) const -{ - CHECKD(numreg > 0 && numrig > 0, "Numero registrazione non valido ", numreg); - - bool found = false; - - if (!found) - { - TString query; - query << "USE " << LF_IVADIFF - << "\nFROM NUMREG=" << numreg << " NUMRIG=" << numrig - << "\nTO NUMREG=" << numreg << " NUMRIG=" << numrig; - TISAM_recordset id(query); - found = id.items() >= 2; // riga fattura ed almeno un pagamento o nota di credito - if (found) - { - TImporto importo, imponib, imposta; - for (bool ok = id.move_first(); ok; ok = id.move_next()) - { - const char sez = id.get("SEZIONE").as_string()[0]; - const tipo_movimento tipomov = (tipo_movimento)id.get(PART_TIPOMOV).as_int(); - if (tipomov <= tm_nota_credito || !last_pag.ok()) - { - importo += TImporto(sez, id.get(PAGSCA_IMPORTO).as_real()); - imponib += TImporto(sez, id.get(RMI_IMPONIBILE).as_real()); - imposta += TImporto(sez, id.get(RMI_IMPOSTA).as_real()); - } - else - { - const TDate data_pag = id.get("DATAREGP").as_date(); - if (data_pag >= last_pag) - continue; - importo += TImporto(sez, id.get(PAGSCA_IMPORTO).as_real()); - imponib += TImporto(sez, id.get(RMI_IMPONIBILE).as_real()); - imposta += TImporto(sez, id.get(RMI_IMPOSTA).as_real()); - } - } - - bool is_good = imponib.valore() > ZERO; - if (!is_good) - { - const real totdoc = cache().get(LF_MOV, numreg, MOV_TOTDOC); - is_good = totdoc > ZERO; - } - if (is_good) - { - importo_res = importo.valore(); - imponib_res = imponib.valore(); - imposta_res = imposta.valore(); - } - } - } - - if (!found) - { - TLocalisamfile rmi(LF_RMOVIVA); - rmi.put(RMI_NUMREG, numreg); - rmi.put(RMI_NUMRIG, numrig); - found = rmi.read() == NOERR; - if (found) - { - imposta_res = rmi.get_real(RMI_IMPOSTA); - imponib_res = rmi.get_real(RMI_IMPONIBILE); - importo_res = imponib_res + imposta_res; - } - } - - return found; -} - -void TLiquidazione_app::recalc_att(int month, const char* codatt) -// il codatt passato e' codice att + tipo att ( IN {1|2} ) -// occhecasino. Se _comp_acconto e' TRUE, i movimenti considerati -// saranno solo quelli di dicembre per data <= 20/12; -{ - const bool has_sc = has_module(SCAUT, CHK_DONGLE) && ini_get_bool(CONFIG_DITTA, "cg", "GesSal"); - real totintra = ZERO; - real nond19_imp = ZERO; - real nond19_iva = ZERO; - real ammort_det = ZERO; - real ammort_det_iva = ZERO; - real ammort_indet = ZERO; - real ammort_indet_iva = ZERO; - real ammort_6 = ZERO; - real ammort_6_iva = ZERO; - real acq_riv = ZERO; - real acq_riv_iva = ZERO; - real leasing = ZERO; - real leasing_iva = ZERO; - real cess_amm = ZERO; - real cess_amm_iva = ZERO; - real acquisti = ZERO; - real acquisti_iva = ZERO; - real vendite = ZERO; - real vendite_iva = ZERO; - real esenti_c1 = ZERO; - real esenti_c2 = ZERO; - real esenti_c3 = ZERO; - real esenti_c1a = ZERO; - real esenti_c3_bam = ZERO; // Beni ammorizzabili esenti c3 - real esenti_c1a_bam = ZERO; // Beni ammorizzabili esenti c1a - real esenti_b14 = ZERO; - - // Comunicazione dati iva annuale (dal 2003) - real cessioni_cd1_1 = ZERO; // Totale operazioni attive - real cessioni_cd1_2 = ZERO; // non imponibili - real cessioni_cd1_3 = ZERO; // esenti - real cessioni_cd1_4 = ZERO; // intra - real cessioni_cd1_5 = ZERO; // beni strumentali - real acquisti_cd2_1 = ZERO; // Totale operazioni passive - real acquisti_cd2_2 = ZERO; // non imponibili - real acquisti_cd2_3 = ZERO; // esenti - real acquisti_cd2_4 = ZERO; // intra - real acquisti_cd2_5 = ZERO; // beni strumentali - real oroargento_cd3_1 = ZERO; // imponibile acquisto oro e argento - real oroargento_cd3_2 = ZERO; // imposta acquisto oro e argento - real rottami_cd3_3 = ZERO; // imponibile acquisto rottami - real rottami_cd3_4 = ZERO; // imposta acquisto rottami - - real esni_rimb = ZERO; // ci sommo tutti esenti e ni validi per rimborso - real corr_CEE = ZERO; - real corr_noCEE = ZERO; - real acq_CEE = ZERO; - real acq_noCEE = ZERO; - real corr_misCEE = ZERO; - real acq_misCEE = ZERO; - real acq_misnoCEE = ZERO; - real agr_detIA = ZERO; - real agr_1i = ZERO; - real agr_2i = ZERO; - real agr_1 = ZERO; - real agr_2 = ZERO; - real agr_3 = ZERO; - real agr_4 = ZERO; - real agr_5 = ZERO; - real agr_6 = ZERO; - real agr_7 = ZERO; - real acq_ies = ZERO; - real acq_ies_iva = ZERO; - real ult_detr = ZERO; - real acq_pint = ZERO; - real acq_pint_iva = ZERO; - real spgen = ZERO; - real spgen_iva = ZERO; - real assp_imp = ZERO; - real assp_iva = ZERO; - real vssp_imp = ZERO; - real vssp_iva = ZERO; - real assl_imp = ZERO; - real assl_iva = ZERO; - real vssl_imp = ZERO; - real vssl_iva = ZERO; - real bdog_imp = ZERO; - real bdog_iva = ZERO; - real agr_imp = ZERO; - real agr_iva = ZERO; - - real fdiff_imp = ZERO; - real fdiff_iva = ZERO; - real fdiffinc_imp = ZERO; - real fdiffinc_iva = ZERO; - real fdiff_imp_acq = ZERO; - real fdiff_iva_acq = ZERO; - real fdiffinc_imp_acq = ZERO; - real fdiffinc_iva_acq = ZERO; - - _diff_ven_ap = ZERO; - _diff_ven_ap_iva = ZERO; - _diff_acq_ap = ZERO; - _diff_acq_ap_iva = ZERO; - - // PEM - real vt_imponibile[23]; - real vt_imposta[23]; - for (int j = 0; j < 23; j++) - { - vt_imponibile[j] = ZERO; - vt_imposta[j] = ZERO; - } - - // Nel seguente assoc, vengono memorizzati gli acquisti iva registrati quest'anno - // ma con anno documento precedente. Di conseguenza per un corretto calcolo prorata - // e' necessario distinguire gli importi, appunto, per anno. - // Questo nuovo calcolo prorata, vale solo per gli anni oltre il 1998. - TAssoc_array acq_iva_anni_prec; - - TTable *arr[4] = { _pim, _pis, _prm, _prp }; //Tabelle con la stessa struttura - - const int year_int = atoi(_year); // Basta con le atoi()... - - // Nuova selezione filtro dal 1998. Rende molto piu' pesante il calcolo, complimenti a tutti. - // Il campo MESELIQ sulla testata dei movimenti indica il mese di liquidazione in cui considerare - // il movimento stesso. Tale campo e' un intero che va da 0 a 12, dove i numeri da 1 a 12 rappresentano - // i rispettivi mesi (Gennaio...Dicembre) e lo zero significa che questo movimento va considerato - // seguendo la data di registrazione, ovvero NORMALMENTE. - // Questo significa che: - // si includano nel cursore tutti i movimenti da questo inizio mese fino all fine dell'anno - // Il primo periodo dell'anno successivo (1 o 3 mesi se Trim.) va incluso se sto calcolando dicembre o l'annuale: - // puo' esistere un doc. con DATAREG di gennaio dell'anno dopo ma MESELIQ==12; tale movimento va considerato in dicembre - // dell'anno precedente (4 trimestre nel cazo di trimestruali). - // Ovviamente un tale movimento siffatto andra' considerato allo stesso modo anche in liquidazione annuale. - // Default date per agenzie viaggio: da inizio anno fino per considerare la DATA74TER - // Esemplificazione dei cursori nei vari casi: - // month < 12 : la regione andra' dal 01/month/_year al 31/12/_year - // selezione movimenti: datareg.month() == month && meseliq == 0 || meseliq == month - // month == 12 : la regione andra' dal 01/month/_year al lastday/firstperiod/_year+1 - // selezione movimenti: datareg.month() == month && meseliq == 0 || meseliq == month - // month == 13 : la regione andra' dal 01/01/_year al lastday/firstperiod/_year+1 - // selezione movimenti: datareg.year() == _year && meseliq != 12 || datareg.year() == _year+1 && meseliq == 12 - // Attenzione: nel caso si tratti di ricalcolo chiamato dalla stampa registri bollati, il cursore - // sara' leggermente diverso: i movimenti sono solo quelli del mese, tutto l'anno se annuale (month == 13). - // I movimenti del periodo selezionato dovranno essere tutti inclusi; in particolare quelli che - // avranno il mese liquidazione != 0 dovranno essere messi nella tabella PRP, come riferimento - // a Progressivi Registri periodo Precedente. - - TRectype from(_cur->curr()); from.zero(); - TRectype to(from); - TString8 fromreg("~"), toreg("0"); - - { - TRelation regrel("REG"); - TCursor reg(®rel); - TString4 codreg; - const int items = reg.items(); - - for (reg = 0L; reg.pos() < items; ++reg) - { - codreg = reg.curr().get("CODTAB").mid(4); - - if (codreg < fromreg) - fromreg = codreg; - if (codreg > toreg) - toreg = codreg; - } - } - - //TDate f(1, month == 13 ? 1 : month, year_int); - const TDate fromdate(1, month == 13 ? 1 : month, year_int); - TDate t; - TDate f74; - - // Spiegazione dell'arcano segreto sulle agenzie viaggio: - // non viene applicato lo stesso metodo delle ditte normali perche' si deve - // tenere conto della DATA74TER se presente. - // Per le agenzie di viaggio si parte da inizio anno precedente - if (_isviaggio) - f74 = TDate(1, 1, year_int-1); - -// if (_isdiff) -// f = TDate(1, 1, year_int-5); - if (year_int < 1998 || _recalc_regis) // Vecchia selezione o calcolo progoressivi per stampa registri bollati - { - t.set_month(month == 13 ? 12 : month); - t.set_year(year_int); - } - else //Nuova selezione dal 1998 in poi - { - t.set_year(month < 12 ? year_int : year_int+1); - t.set_month(month < 12 ? 12 : _freqviva == "M" ? 1 : 3); - } - t.set_end_month(); - - const TDate inizio(fromdate - 1L); - - TDate todate(1, month == 13 ? 12 : month, year_int); - todate.set_end_month(); - const TDate fine(todate); - - from.put(MOV_REG, fromreg); - to.put(MOV_REG, toreg); - - _cur->freeze(FALSE); - _cur->setregion(from, to); - - TString filter; - //filter.format("((ANSI(DATAREG)>=%ld)&&(ANSI(DATAREG)<=%ld))", fromdate.date2ansi(), t.date2ansi()); - filter.format("(BETWEEN(DATAREG,%ld,%ld))", fromdate.date2ansi(), t.date2ansi()); - if (_isviaggio) - filter << format("||(BETWEEN(DATA74TER,%ld,%ld))", f74.date2ansi(), t.date2ansi()); - if (_isdiff) - filter << "||(NUM(LIQDIFF==\"X\"))"; - if (has_sc && gestione_IVAxCassa(fromdate)) - filter << "||(NUM(IVAXCASSA==\"X\"))"; - - _cur->setfilter(filter); - const long items = _cur->items(); - _cur->freeze(); - - *_cur = 0; - - TString16 trueatt(codatt); - const int tipatt = trueatt[5] - '0'; - trueatt.cut(5); - - TString pimsg; pimsg << TR("Ricalcolo attività ") << trueatt << " (mese " << month << ')'; - TProgind pi(items, pimsg, false, true); - for (; _cur->pos() < items; ++(*_cur)) - { - if (!pi.setstatus(_cur->pos())) - break; - - TDate date = _mov->get(MOV_DATAREG); - const int liqmonth = _mov->get_int(MOV_MESELIQ); - const TString4 reg = _mov->get("REG"); - const bool isreg = look_reg(reg); - const TString4 tipodoc = _mov->get(MOV_TIPODOC); - const bool corrisp = _reg->get_bool("B0"); - const tiporeg tipomov = (tiporeg)_reg->get_int("I0"); - - tipo_movimento tm = (tipo_movimento)_mov->get_int(MOV_TIPOMOV); - if (tm == tm_nessuno) - { - if (tipodoc == "FV" || tipodoc == "FA") - tm = tm_fattura; else - if (tipodoc == "NC") - tm = tm_nota_credito; - } - - // Controlla se la data del documento si riferisce all'anno precedente (PRORATA 1998) - const TDate datedoc = _mov->get(MOV_DATADOC); - - if (_isviaggio) - { - if (tipomov == vendita && _mov->get(MOV_DATA74TER).not_empty()) - date = _mov->get_date(MOV_DATA74TER); - if (date.year() != year_int) // Controlla anno di appartenenza, altrimenti is_date_ok potrebbe non funzionare - continue; - } - const TRectype& rcs = _cur->curr(LF_CAUSALI); - const bool fattrit = rcs.get_bool(CAU_RITFATT); - const bool cau_intra = rcs.get_bool(CAU_INTRACOM); - const bool cau_valintra = rcs.get_bool(CAU_VALINTRA); - - // Inizio gestione IVA differita - const bool iva_diff = _isdiff && _mov->get_bool(MOV_LIQDIFF); - const bool iva_cass = !iva_diff && _mov->get_bool(MOV_IVAXCASSA) && tm > 0; - const bool movdiff = (iva_diff || iva_cass) && !_recalc_regis; // Aggiunto test su ricalcolo da registri 13-12-2012 - bool dok = is_date_ok(date, month, liqmonth, year_int); - - TPartite_array arrpart; // Partite interessate - TPointer_array pagscatt; // Righe di pagsca interessate - - if (movdiff && tm == tm_fattura) - { - // Sezione preferita per fatture decisa in base a vendita->'A' o acquisto->'D' - const char sezfat = tipomov == vendita ? 'D' : 'A'; - const char sezpag = tipomov == vendita ? 'A' : 'D'; - const long numreg = _mov->get_long(MOV_NUMREG); - TDate datainc; - real tot_incassato, tot_da_incassare; - int flag_pg_nc = 0; // Ci sono pagamenti (0x1) e/o note di credito(0x2) ? - bool game_found = false; - if (has_sc) - { - 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 && !ivadiff_chiusa(_mov->curr(), fine)) - { - const TRiga_partite& rp = p->riga(row); - TImporto pg, nc; - flag_pg_nc = rp.calcola_pagato_periodo(inizio+1L, fine, pg, nc, &pagscatt); - if (sarebbe_da_pagare(_mov->curr(), fine)) - { - rp.calcola_pagato_periodo(TDate(0L), fine, pg, nc, NULL); - TImporto saldo = rp.importo(false); - saldo += pg; saldo += nc; - saldo.normalize(sezfat); - if (saldo.valore() > ZERO) - tot_da_incassare = saldo.valore(); - } - } - } - if (!game_found) // No saldaconto o partita assente - { - datainc = iva_diff ? _mov->get_date(MOV_DATAINC) : TDate(); - if (datainc.ok() && datainc <= fine) - tot_incassato = _mov->get_real(MOV_TOTDOC); - else - { - // Fattura più vecchia di un anno a ente NON pubblico - if (sarebbe_da_pagare(_mov->curr(), fine)) - tot_da_incassare = _mov->get_real(MOV_TOTDOC); - } - } - // Simulo incasso se necessario - if (!tot_da_incassare.is_zero()) // già controllato se sarebbe_da_pagare(_mov->curr(), fine) - { - tot_incassato = tot_da_incassare; - datainc = fine; - _mov->put(MOV_DATAINC, datainc); - } - if (tot_incassato > ZERO && is_date_ok(datainc, month, liqmonth, year_int)) - { - flag_pg_nc = 0x1; - TRectype* pagsca = new TRectype(LF_PAGSCA); - pagsca->put(PAGSCA_TIPOC, _mov->get(MOV_TIPO)); - pagsca->put(PAGSCA_SOTTOCONTO, _mov->get(MOV_CODCF)); - pagsca->put(PAGSCA_ANNO, year_int); - pagsca->put(PAGSCA_NRIGA, 1); - pagsca->put(PAGSCA_NRATA, 1); - pagsca->put(PAGSCA_NRIGP, 9999); - pagsca->put(PAGSCA_IMPORTO, tot_incassato); - pagscatt.add(pagsca); - } - if (flag_pg_nc) - dok = true; - } - /* - * check register present, rmoviva present and date OK - * - * In un futuro prossimo, molto remoto, quando implementeremo il Client/Server, questo - * filtro sulla data registrazione/mese liq. andra' nel cursore stesso. - * Ora viene implementato qui [is_date_ok()] per non appesantire ulteriormente - * la lettura dei movimenti. - */ - - const bool sreg = !isreg; - const bool rs8 = _reg->get("S8") != trueatt; - const bool cmt = !_cur->is_first_match(LF_RMOVIVA); - - if (!dok || sreg || rs8 || cmt) - continue; - - const tipo_sospensione sosp_imp = _reg->get_bool("B1") ? (tipo_sospensione) _reg->get_int("I9") : nessuna; - - /* - * Tipo documento da eliminare (Scudler 1994) - * Trattasi per lo piu' di non incassati - * Da oggi vengono sommati nei real di S2 in pim - * assieme alle FS per stampa registri - */ - const bool noninc = (tipodoc == "SN" || tipodoc == "CN" || tipodoc == "RN" || tipodoc == "IN" || tipodoc == "PG"); - - /* - * check date: se si calcola l'acconto, solo da 1/12 a 20/12 - */ - const int accmonth = _isdifferita ? 11 : 12; - - /* - * Patrizia: se è differita i mov. vanno da 1/11 a 30/11 - * potrei allora andarmi a leggere i risultati della liq di 11 - * ma il cliente potrebbe voler calcolare l'acconto prima - * di aver calcolato la liquidazione (perche' cosi' complicato?) - */ - if (_comp_acconto && !_isdifferita && date.month() == accmonth && date.day() > 20) - continue; - - // Ciclo sulle righe iva del movimento - do - { - const int rmi_tipoatt = max(_rmoviva->get_int(RMI_TIPOATT), 1); // Poteva capitare tipoatt == 0 - if (rmi_tipoatt != tipatt) - continue; - - // totali parziali registrati nei pim per motivi ignoti - real bolld_imp = ZERO; // imponibile bolle doganali - real bolld_iva = ZERO; // imposta bolle doganali - real asimp_imp = ZERO; // imponibile acquisti sosp. imposta - real asimp_iva = ZERO; // imposta acquisti sosp. imposta - real vsimp_imp = ZERO; // imponibile vendite sosp. imposta - real vsimp_iva = ZERO; // imposta vendite sosp. imposta - real rit_imp = ZERO; // imponibile fatture in ritardo - real rit_iva = ZERO; // imposta fatture in ritardo - real nin_imp = ZERO; // imponibile non incassati - real nin_iva = ZERO; // imposta non incassati - - look_iva(_rmoviva->get(RMI_CODIVA)); - const TString4 codiva = _iva->get("CODTAB"); - const TString4 tipoiva = _iva->get("S1"); - const TString8 tipoes_v = _iva->get("S2"); - const TString8 tipoes_a = _iva->get("S9"); - const int tipoagr = _iva->get_int("S4"); - const int tipoag = _iva->get_int("S5"); - const int tipopla = _iva->get_int("S3"); - int isrimbinfr = _iva->get_bool("B3"); // vale per calcolo rimborso se ES o NI - const real perciva = _iva->get_real("R0") / CENTO; - int ivarimb = !_iva->get_bool("B4"); // non escluso calcolo rimb. per al. media - - const TString4 tipocr_s = _rmoviva->get(RMI_TIPOCR); - const int tipocr = atoi(tipocr_s); - const bool intra = _rmoviva->get_bool(RMI_INTRA); - - // autofatture art. 17 per non residenti, con trattamento - // speciale in liquidazione annuale - const bool autodafe = tipodoc == "AF"; //Adesso il tipocr==4 non c'entra piu' un tubo - // vendite art. 40 c. 5/6/8 acq.intra., con trattamento - // speciale in liquidazione annuale (VA7 in stampa) - const bool art40 = (tipodoc != "AF" && tipoiva != "NS" && !rcs.get_bool("AUTOFATT") && rcs.get_bool("VALINTRA") && !intra); - // indica acquisti per rivendita; viene messo in B4 di PIM per - // indicare che e' soggetto al ricalcolo IVA (se voluto) in modo - // da evitare di dover controllare mese e anno sul PIM - const bool was_riv = (tipoiva != "NS") && (tipocr == 1 || tipocr ==5); - real percind; - const int tipoind = get_tipodet_from_rmi(_rmoviva->curr(), _mov->curr(), percind, true); - const int decimals = TCurrency::get_firm_dec(); - - const real imponibile_orig = _rmoviva->get_real(RMI_IMPONIBILE); - const real imposta_orig = _rmoviva->get_real(RMI_IMPOSTA); - - real diff_imp; // Liquidazione differita imponibile - real diff_iva; // Liquidazione differita imposta - real incdiff_imp; // Liquidazione differita incassate imponibile - real incdiff_iva; // Liquidazione differita incassate imposta - real impon_ind; // Imponibile indetraibile - real impos_ind; // Imposta indetraibile - real impon_det; // Imponibile detraibile - real impos_det; // Imposta detraibile - - if (movdiff && 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); - id.put(PART_TIPOMOV, tm); - CHECKD(rmi_tipoatt > 0, "Invalid tipo att ", rmi_tipoatt); - id.put("TIPOATT", rmi_tipoatt); - id.put("ANNOLIQ", datareg.year()); - int mesereg = datareg.month(); - if (_freqviva[0] == 'T') - { - const int resto = mesereg % 3; - if (resto > 0) - mesereg += 3-resto; - } - id.put("MESELIQ", mesereg); - id.put("TIPOIVA", tipomov == 2 ? 2 : 1); - id.put("TIPODIFF", iva_cass ? 2 : 1); - id.put(MOV_DATAREG, _mov->get(MOV_DATADOC)); - id.put(RMI_CODIVA, _rmoviva->get(RMI_CODIVA)); - id.put("SEZIONE", sezfat); - - id.put("IMPORTO", imponibile_orig+imposta_orig); - id.put(RMI_IMPONIBILE, imponibile_orig); - - if (percind > ZERO) // Nel caso di IVA indetraibile memorizza solo la parte detraibile! - { - real imp_det, iva_det, imp_ind, iva_ind; - analizza_IVA(imponibile_orig, imposta_orig, percind, corrisp, false, codiva, - imp_det, iva_det, imp_ind, iva_ind); - id.put(RMI_IMPOSTA, iva_det); - } - else - id.put(RMI_IMPOSTA, imposta_orig); - - const int ew = id.write_rewrite(); - if (ew != NOERR) - cantwrite_box(id.description()); - } - - if (!pagscatt.empty()) - { -#ifdef DBG - if (_mov->get_long(MOV_NUMREG) == 13871) - xvt_dm_popup_message("Aggiornamento IVADIFF per movimento 13871"); -#endif - - - const char sezpag = tipomov == vendita ? 'A' : 'D'; - const real totfat = _mov->get(MOV_TOTDOC); - - TLocalisamfile id(LF_IVADIFF); - FOR_EACH_ARRAY_ITEM(pagscatt, r, obj) - { - const TRectype& pagsca = *(TRectype*)obj; - const int nrigp = pagsca.get_int(PAGSCA_NRIGP); - - id.zero(); - id.put(RMI_NUMREG, _rmoviva->get(RMI_NUMREG)); - id.put(RMI_NUMRIG, _rmoviva->get(RMI_NUMRIG)); - id.put(MOV_DATAREG, _mov->get(MOV_DATADOC)); - CHECKD(nrigp > 0, "Invalid NRIGP ", nrigp); - id.put("NUMPRO", min(nrigp, 999)); - CHECKD(rmi_tipoatt > 0, "Invalid tipo att ", rmi_tipoatt); - id.put("TIPOATT", rmi_tipoatt); - id.put("TIPOIVA", tipomov == 2 ? 2 : 1); - id.put("TIPODIFF", iva_cass ? 2 : 1); - id.put(RMI_CODIVA, _rmoviva->get(RMI_CODIVA)); - - TImporto pagtmp; - bool ultimo = false; - tipo_movimento tipomov_pag = tm_pagamento; - if (nrigp > 0 && nrigp < 9999) - { - const TPartita& p = arrpart.partita(pagsca); - const TRiga_partite& rp = p.riga(nrigp); - tipomov_pag = rp.tipo(); - id.put(PART_TIPOMOV, tipomov_pag); - id.put("NUMREGP", rp.get(PART_NREG)); - id.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); - id.put("DATAREGP", d); - pagtmp = p.importo_pagsca(pagsca); - if (p.chiusa() && rp.tipo() >= 3) - { - ultimo = true; - for (int r = p.last(); r > nrigp; r = p.pred(r)) - { - if (p.riga(r).tipo() >= tm_pagamento) - { - ultimo = false; - break; - } - } - } - } - else - { - // Falso record generato da datainc - id.put(PART_TIPOMOV, tipomov_pag); - id.put("NUMREGP", _mov->get(MOV_NUMREG)); - id.put("NUMRIGP", nrigp); - id.put("DATAREGP", _mov->get(MOV_DATAINC)); - pagtmp.set(sezpag, pagsca.get_real(PAGSCA_IMPORTO)); - ultimo = true; - } - - id.put("ANNOLIQ", year_int); - int meseliq = id.get_date("DATAREGP").month(); - if (_freqviva[0] == 'T') - { - const int resto = meseliq % 3; - if (resto > 0) - meseliq += 3-resto; - } - id.put("MESELIQ", meseliq); - - id.put("SEZIONE", pagtmp.sezione()); - if (ultimo) - { - real importo, imponibile, imposta; - residuo_da_liquidare(id.get_long(RMI_NUMREG), id.get_int(RMI_NUMRIG), id.get_date("DATAREGP"), - importo, imponibile, imposta); - id.put(PAGSCA_IMPORTO, importo); - id.put(RMI_IMPONIBILE, imponibile); - id.put(RMI_IMPOSTA, imposta); - - incdiff_imp += imponibile; - incdiff_iva += imposta; - } - else - { - id.put(PAGSCA_IMPORTO, pagtmp.valore()); - pagtmp.normalize(sezpag); - - const real perc = pagtmp.valore() / totfat; - real val = imponibile_orig * perc; - round_imposta(val); - id.put(RMI_IMPONIBILE, val); - - if (tipomov_pag >= tm_pagamento) - incdiff_imp += val; - - val = imposta_orig * perc; - round_imposta(val); - id.put(RMI_IMPOSTA, val); - if (tipomov_pag >= tm_pagamento) - incdiff_iva += val; - } - - int err = id.write_rewrite(); - if (err != NOERR) - error_box("Errore %d in aggiornamento file IVADIFF", err); - - const long numreg = _rmoviva->get_long(RMI_NUMREG); - if (numreg > 0) // Chiude o riapre la partita - { - 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 chiusa = id.get_bool("CHIUSA"); - if (chiusa != ultimo) - { - id.put("CHIUSA", ultimo); - id.rewrite(); - } - } - } - } - diff_imp = imponibile_orig; - diff_iva = imposta_orig; - } - else - { - if (movdiff) - { - diff_imp = imponibile_orig; - diff_iva = imposta_orig; - } - else - analizza_IVA(imponibile_orig, imposta_orig, percind, corrisp, false, codiva, - impon_det, impos_det, impon_ind, impos_ind); - } - - for (int is_detraibile = 0; is_detraibile < 2; is_detraibile++) - { - const int tipodet = is_detraibile ? 0 : tipoind; - const real imponibile = is_detraibile ? impon_det : impon_ind; - const real imposta = is_detraibile ? impos_det : impos_ind; - - //qui da verificare - - if ((imponibile.is_zero() && imposta.is_zero()) && - (_isdiff && - (is_detraibile > 0 || - (diff_imp.is_zero() && diff_iva.is_zero() && - incdiff_imp.is_zero() && incdiff_iva.is_zero()) - ) - ) - ) - continue; - - /* - * Riga imponibile > 9 = ritenute; non si - * considerano (Scudler 1994) - */ - - // Filling dell'array per IVA11 - if (_is_interactive && month == 13) - iva11_set_arr_phase_1(trueatt); - - - // Cumula l'ammontare delle operazioni attive/passive - // per la dichiarazione annuale fati iva - if (month == 13 && !fattrit) - { - if (tipomov == vendita) - { - const long codcf = _mov->get_long(MOV_CODCF); - - if (tipoiva.empty() && codcf != 0) - { - TString8 key; key.format("%c|%ld", _mov->get_char(MOV_TIPO), codcf); - const TRectype& clifo = cache().get(LF_CLIFO, key); - int codreg = 0; // codice regione x privati 22 == partite iva 0 == senza codice - const int tipoalleg = clifo.get_int(CLI_ALLEG); - - if (tipoalleg == 6) // privato - { - if (clifo.get_bool(CLI_OCCAS)) - { - const TString80 key_occ(_mov->get(MOV_OCFPI)); - const TRectype & occas = cache().get(LF_OCCAS, key_occ); - - key = occas.get(OCC_STATO); - key << "|" << occas.get(OCC_COM); - } - else - { - key = clifo.get(CLI_STATOCF); - key << "|" << clifo.get(CLI_COMCF); - } - - const TRectype & comune = cache().get(LF_COMUNI, key); - - codreg = comune.get_int(COM_CODREG) + 1; - } - vt_imponibile[codreg] += imponibile; - vt_imposta[codreg] += imposta; - } - } - - const TString& iva_vpn = _iva->get(tipomov == vendita ? "S10" : "S11"); - if (iva_vpn.full()) - { - switch (tipomov) - { - case vendita: // CD1 - 1 2 3 4 - { - // bool is_valid = !corrisp && tipodoc != "AF" && (cau_intra || !cau_valintra) && - // sosp_imp != normale && sosp_imp != liquidazione; - bool is_valid = tipodoc != "AF" && sosp_imp != normale && sosp_imp != liquidazione; - if (is_valid) - { - if (movdiff) // 21-02-2013 Comprendere IVA differita registrata nell'anno! (imponibile=0) - { - if (date.year() == year_int) - cessioni_cd1_1 += imponibile_orig; - } - else - cessioni_cd1_1 += imponibile; - if (iva_vpn.starts_with("CD1")) - { - if (tipocr == 4) - cessioni_cd1_5 += imponibile; - } - if (!cau_intra) - { - if (iva_vpn == "CD12") - cessioni_cd1_2 += imponibile; else - if (iva_vpn == "CD13") - cessioni_cd1_3 += imponibile; - } - else - { - if (iva_vpn == "CD14") - cessioni_cd1_4 += imponibile; - } - } - - } - break; - case acquisto: // CD2 - 1 2 3 4 - { - acquisti_cd2_1 += imponibile; - if (iva_vpn.starts_with("CD2")) - { - if (tipocr == 2 || tipocr == 3 || tipocr == 8) - acquisti_cd2_5 += imponibile; - } - if (!cau_intra) - { - if (iva_vpn == "CD22") - acquisti_cd2_2 += imponibile; - else - if (iva_vpn == "CD23") - acquisti_cd2_3 += imponibile; - } - else - { - if (iva_vpn == "CD24") - acquisti_cd2_4 += imponibile; - } - if (iva_vpn == "CD31") - { - oroargento_cd3_1 += imponibile; - oroargento_cd3_2 += imposta; - } - if (iva_vpn == "CD33") - { - rottami_cd3_3 += imponibile; - rottami_cd3_4 += imposta; - } - } - break; - default: - break; - } - } - } - - if (noninc) // non incassati: non devono entrare in nessun altro calcolo - { - nin_imp = imponibile; - nin_iva = imposta; - } - else if (sosp_imp != nessuna) // sospensione di imposta - { - if (tipomov == vendita) - { - vsimp_imp = imponibile; - vsimp_iva = imposta; - } - else - { - asimp_imp = imponibile; - asimp_iva = imposta; - } - - switch (sosp_imp) - { - case normale: - assp_imp += asimp_imp; - assp_iva += asimp_iva; - vssp_imp += vsimp_imp; - vssp_iva += vsimp_iva; - break; - case liquidazione: - assl_imp += asimp_imp; - assl_iva += asimp_iva; - vssl_imp += vsimp_imp; - vssl_iva += vsimp_iva; - default: - break; - } - } - else if (fattrit) // Possono essere solo acquisti - { - rit_imp = imponibile; - rit_iva = imposta; - } - - // Le fatture in ritardo non vanno considerate nel periodo ma solo nell'annuale - if (!fattrit || (fattrit /*&& _isagricolo*/ && _month == 13)) // normale - { - - // ***************************************** - // casi particolari - // ***************************************** - - // imposta acquisti intracomunitari - if (tipomov == acquisto && intra) - totintra += imposta; - - // Altre cose di cui tener conto - // Separo pazientemente per movimenti d'acquisto e movimenti di vendita - // onde evitare rompicapi, rompicazzi ecc... - - switch (tipomov) - { - case acquisto: // ACQUISTO (per chi non l'avesse capito) - // Per le anguille delle Antille e le Saracche delle Molucche... - // Gli acquisti con tipocr 1,2,3,5,8 vanno totalizzati se e solo se - // il tipo iva e' diverso da NON SOGGETTO, - if (tipoiva != "NS") - { - if (tipocr == 1 || tipocr ==5) - // Acquisto beni per rivendita - { - acq_riv += imponibile; - acq_riv_iva += imposta; - } - else if (tipocr == 2 && tipodet == 0) - // Acquisto beni ammortizzabili detraibili - { - ammort_det += imponibile; - ammort_det_iva += imposta; - } - else if (tipocr == 2 && tipodet != 0) - // Acquisto beni ammortizzabili indetraibili - { - ammort_indet += imponibile; - ammort_indet_iva += imposta; - } - else if (tipocr == 3) - // Acquisto beni ammortizzabili detr. 6% - { - ammort_6 += imponibile; - ammort_6_iva += imposta; - // ult_detr += imponibile * real(DETRAZIONE_6PERCENTO); // CM500159 - } - else if (tipocr == 8) - // Acquisto beni strum. acquisiti in leasing - { - leasing += imponibile; - leasing_iva += imposta; - } - } // fine if per tipo iva != da NON SOGGETTO - - // Mi chiedo se effettivamente il tipo 9 sia esente da qualsiasi criterio... - if (tipocr == 9) - // Spese generali - { - spgen += imponibile; - spgen_iva += imposta; - } - // else fuck it up, dirty dick head. - break; - case vendita: // VENDITA - if (tipoiva != "NS") /* Segnalazione CM000219: I non soggetti non vanno considerati */ - { - if (tipocr == 4) - /* - * Vendite strum. art 17 (cess. amm.) - * Comprende anche autofatture, anche nell'annuale - * (Sergio 1995) - */ - { - /* NO! - * Invece non le deve comprendere nell'annuale - * (Vladimiro 1995, piu' tardi) - */ - if (!(_month == 13 && autodafe)) - { - cess_amm += imponibile; - cess_amm_iva += imposta; - } - } - } - break; - default: // Chissa'... forse in futuro vi sara' un ulteriore tipo di movimento... - break; - } // Fine del malefico switch - - - // operazioni per calcolo rimborso infraanale - // Scudler 1995 e molti dopo di lei - // La breve estate vissuta dal metodo del 51% rende - // oramai superflua la tabella rimborsi, eliminata - // senza entusiasmo ed interrogandomi sul senso della vita - if (ivarimb) - { - bool ok = _isagricolo ? tipoagr == 2 : false; - - if (tipomov == acquisto && !ok) - ok = (tipocr == 0 || tipocr == 1 || tipocr == 5 || tipocr == 9); - - if (tipomov == vendita && !ok) - ok = tipoiva != "NS" && tipocr != 4; - - ivarimb = ok; - } - - // operazioni esenti - if (tipoiva == "ES") - { - if (tipomov == vendita) - { - if (sosp_imp != normale && sosp_imp != vol_affari) // CM500308 - { - if (tipoes_v == "B1") // C1 - esenti_c1 += imponibile; - else if (tipoes_v == "B2") // C2 - esenti_c2 += imponibile; - else if (tipoes_v == "B3") // C3 - { - esenti_c3 += imponibile; - if (tipocr == 4) - esenti_c3_bam += imponibile; - } - else if (tipoes_v == "B4") // C1A - { - esenti_c1a += imponibile; - if (tipocr == 4) - esenti_c1a_bam += imponibile; - } - // se e' il caso sommare esenti per rimborso - if (isrimbinfr) - esni_rimb += imponibile; - } - } - else if (tipoes_a == "14") - esenti_b14 += imponibile; - } - - // non imponibili per rimborso - if (tipoiva == "NI" && tipomov == vendita && isrimbinfr) - esni_rimb += imponibile; - - // bolle doganali - if (tipodoc == "BD") - { - bolld_imp = imponibile; - bolld_iva = imposta; - - bdog_imp += bolld_imp; - bdog_iva += bolld_iva; - } - - // plafond - if (_isplafond && tipomov == acquisto && month != 13 && - tipoiva == "NI" && tipopla != 0) - { - add_plafond(month, codatt, tipopla, imponibile, tipodoc == "BD"); - } - - // agenzie viaggio (CHECK imponibili etc.) - if (_isviaggio) - switch (tipoag) - { - case 1: - if (tipomov == acquisto) acq_CEE += imponibile + imposta; - else corr_CEE += imponibile + imposta; - break; - case 2: - if (tipomov == acquisto) acq_noCEE += imponibile + imposta; - else corr_noCEE += imponibile + imposta; - break; - case 3: if (tipomov == vendita) - corr_misCEE += imponibile + imposta; - break; - case 4: if (tipomov == acquisto) - acq_misCEE += imponibile + imposta; - break; - case 5: if (tipomov == acquisto) - acq_misnoCEE += imponibile + imposta; - break; - } - - - if (_isagricolo) - { - /* - * rifatto sensu Scudler 1995 con invidiabile sicurezza - * senza porsi domande e mettendo NUMERI uguali - * al TIPO RECORD del frigorifero - */ - if (tipomov == vendita) - { - if (!noninc) // Salta i non incassati - { - agr_imp = imponibile; - agr_iva = imposta; - const real p = perciva; - const bool is_not_fs = tipodoc != "FS"; - - // Se si tratta di Fattura Fiscale o Fattura Scontrino, non scorpora - // In particolare le FS non vengono incluse ne' nello specchietto ne' nel totale - if (corrisp && tipodoc != "FF" && is_not_fs) - lordo2netto(agr_imp,agr_iva,p); - - if (tipoagr == 1 && is_not_fs) // Ora si utilizza solo il segnalino 1 - { - agr_1 += agr_iva; - agr_1i += agr_imp; - - // Nuovo regime agricolo dal 1998: si sommino per codice iva di compensazione (da reperire su PCON) - if (_isagr98) - { - TString ivacomp("00"); // Se non esiste o non si trova il conto questo e' il default (fa un poco schifo ma e' cosi') - - if (_cur->is_first_match(-AGR_PCON1)) // Esiste il conto ? - // Bien, allora controlliamo se e' un ricavo... - if (_pcon_1_r->get_int(PCN_INDBIL) == 4) - if (_cur->is_first_match(-AGR_PCON2)) // Esiste il sottoconto ? - { - if (look_iva(_pcon_2_r->get(PCN_IVACOMP))) - ivacomp = _iva->get("CODTAB"); // Ecco il codice IVA di compensazione - } - look_pia(month, codatt, codiva, ivacomp, TRUE); // Crea il record in tabella se non esiste - real pia_imp = _pia->get_real("R0") + agr_imp; // Aggiorna... - _pia->put("R0", pia_imp); - _pia->put("B0", TRUE); // Seguo l'esempio dei PIM per il ricalcolo acquisti alla fine del ciclo - _pia->rewrite(); // infine scrive - } - } - else if (tipoagr == 2 && is_not_fs) - { - agr_2 += agr_iva; - agr_2i += agr_imp; - } - } - } - else if (tipodet == 0) - { - if (tipoagr == 2) - { - if (tipocr != 2 && tipocr != 3 && tipocr != 8) - agr_3 += imposta; - else - agr_4 += imposta; - } - else if (tipoagr == 3) - { - if (tipocr != 2 && tipocr != 3 && tipocr != 8) - agr_7 += imposta; - } - else // ne' 2 ne' 3 - { - if (tipocr != 2 && tipocr != 3 && tipocr != 8) - agr_5 += imposta; - else - agr_6 += imposta; - } - } - } // __end of _agricolo - - if (tipodet == 1) // acquisti indeducibili su ricavi esenti art. 10 - { - acq_ies += imponibile; - acq_ies_iva += imposta; - } - - if (tipodet == 3) // passaggi interni (solo per ventilaz) - { - acq_pint += imponibile; - acq_pint_iva += imposta; - } - - // acquisti non detraibili art. 19 - // possono valere per la ventilazione - if (tipodet == 9) - { - nond19_imp += imponibile; - nond19_iva += imposta; - } - - // ***************************************** - // Fine casi particolari - // Non che i casi normali siano tanto meglio - // ***************************************** - } // non sosp_imp ne' altre casistiche che escludono la liq. - - /* - * se ign == TRUE non bisogna neppure PROGRESSIVARLI - */ - const bool sosp_vaf = sosp_imp == vol_affari; - const bool sosp_liq = sosp_imp == liquidazione; - bool ign = sosp_imp == normale || (fattrit && month != 13) || noninc; - - /* - * se liq == FALSE gli importi non contano ai fini - * del calcolo liquidazione - */ - bool liq = !(tipodet == 1 || tipodet == 3 || tipodet == 9 || ign || sosp_vaf); - - real imp; // imponibile - real ivp; // IVA su imp. - real lor; // lordo - real lorcor; // lordo corrispettivi (da non ventilare) - - /* - * le maledette fatture con scontrino sono sul registro dei - * corrispettivi, ma vanno riportate nelle vendite in stampa; - * dunque, le sommiamo in R5 e R6 di PIM anche se continuiamo - * a sommarle anche in R0 e R1. La stampa (describe_pim) - * dovra' scorporarle. - * - * 5.7.1996. Mi permetto di aggiungere una cosa: le bastardissime fatture con scontrino - * possono anche essere sul registro vendite. Indi le schiaffo dentro - * a R5 e R6 anche nel caso vi sia un documento FS registrato sul registro - * delle vendite. Nel caso in futuro si accorgessero finalmente della puttanata - * galattica che mi hanno fatto fare basta eliminare il controllo su FS - * nel caso si tratti di non corrispettivi (vedi sotto). - * Da togliere anche nella :describe_pim() in cg4304.cpp. - * - */ - real ifs; // imponibile fatture con scontrino - real vfs; // IVA fatt. con scontrino - - /* - * le maledette autofatture non residenti art. 17 - * vedi al calcolo if (autodafe) si tengono da parte - * La stampa le scorpora solo IN SEDE di dichiarazione annuale - */ - real adf; - real adi; - - /* le fottute fatture in sospensione di imposta */ - real fsi; - real fsv; - - /* le putride bolle doganali */ - real bdi; - real bdv; - - /* i luridi falsi corrispettivi */ - real fci; - real fcv; - - /* i conturbanti art. 40 c. 5/6/8 a.i. */ - real va7i; - real va7v; - - /* le porche fatture in ritardo */ - real rti; - real rtv; - - /* - * la stramadonna dei corrispettivi gia' scorporati - * Gli si aggiungono i non incassati, sperando in bene - */ - real csi; - real csv; - /* incrociamm'e dituozz' */ - csi += nin_imp; - csv += nin_iva; - - rti += rit_imp; - rtv += rit_iva; - - if (tipomov == vendita) - { - fsi += vsimp_imp; - fsv += vsimp_iva; - } - else - { - fsi += asimp_imp; - fsv += asimp_iva; - bdi += bolld_imp; - bdv += bolld_iva; - } - - if (liq && tipomov == vendita && !corrisp) - // i corrispettivi sono gestiti a parte - { - // totale vendite - vendite += imponibile; - vendite_iva += imposta; - } - else if ((liq || fattrit && month == 13) && tipomov == acquisto) - { - // totale acquisti - acquisti += imponibile; - acquisti_iva += imposta; - - const int yr = datedoc.year(); - - if (year_int > 1997 && (yr < (year_int-1) && yr > (year_int-4))) // Prende un intervallo di 2 anni al massimo - { // Quelli relativi all'anno in corso e precedente vanno assieme. - // Se abilitato, somma l'iva acquisti relativi ai documenti memorizzati con anni precedenti - TString4 yy; yy << datedoc.year(); - const bool is_key = acq_iva_anni_prec.is_key(yy); - if (!is_key) - acq_iva_anni_prec.add(yy, new real, is_key); - real& aip = (real&) acq_iva_anni_prec[yy]; - aip += imposta; - } - } - - // corrispettivi - bool true_corrisp = FALSE; - if (corrisp) - { - /* - * ennesimo casino: non tutti i corrispettivi sono - * corrispettivi; in effetti, alcuni corrispettivi - * non sono corrispettivi. Ci si potrebbe domandare - * se gli altri corrispettivi sono corrispettivi o - * no; ebbene, gli altri corrispettivi risultano - * fortunatamente essere corrispettivi, a meno di - * indicazioni contrarie. - */ - if (tipodoc == "CR" || tipodoc == "RF" || tipodoc == "SC") - { - true_corrisp = TRUE; - _pim->put("I0",LORDO); - // questi sono corrispettivi davvero; comportamento normale - if (tipoiva == "VE") // da ventilare - { - lor += imponibile; - add_vendite(month, reg, tipodet, imponibile); - } - else // non da ventilare, con imposta gia' conteggiata - { - lor += imponibile + imposta; - lorcor += imponibile+imposta; - real tot = imponibile+imposta; - if (liq) add_corrisp(month, reg, tot, perciva, tipodet, codiva, codatt); - } - } - else if (tipodoc == "FF") // CM500415: prima non c'era questo if - { - // queste sono fatture fiscali - true_corrisp = TRUE; - - real tot = imponibile + imposta; - lor += tot; - lorcor += tot; - if (liq) - add_corrisp(month, reg, tot, perciva, tipodet, codiva, codatt); - } - else if (tipodoc == "FS") - { - // queste sono fatture che seguono scontrino, il lordo va detratto - // perche' si e' gia' registrato lo scontrino medesimo - true_corrisp = true; // non e' vero ma non devono essere sommate ai falsi - // corrispettivi - if (tipoiva == "VE") - { - const real rr = imponibile + imposta; - imp += rr; - if (liq) - vendite += rr; - add_vendite(month, reg, tipodet, rr); - } - if (liq) - { - csi += imponibile; - csv += imposta; - } - imp -= imponibile; - ivp -= imposta; - lor -= imponibile + imposta; - ifs += imponibile; - vfs += imposta; - } - else // vendite normali a tutti gli effetti - { - if (liq) - { - vendite += imponibile; - vendite_iva += imposta; - csi += imponibile; - csv += imposta; - } - imp += imponibile; - ivp += imposta; - _pim->put("I0",NETTO); - } - } // if corrisp - else // non corrisp - { // imponibile e imposta separata - imp += imponibile; - ivp += imposta; - if (tipodoc == "FS") // Questo e' per l'errore MI3404 - { - ifs += imponibile; - vfs += imposta; - } - _pim->put("I0",NETTO); - } - - // Rettifica il valore da memorizzare in PAM->R1, utilizzato nel calcolo Vol.Affari - // Solo nel caso in cui l'importo corrente deve far parte di tale calcolo - // Se e' valido per calcolo solo vol.aff aggiunge gli importi non aggiunti a causa del !liq - // Se e' valido per calcolo solo liq. sottrae gli importi aggiunti a causa del liq. - // Nel caso di sosp. normale non viene ne progressivato ne liquidato e ne considerato per vol. aff. - if (sosp_vaf || sosp_liq) - { - real iii = imponibile; - real iiimmm = imponibile + imposta; - - if (sosp_liq) - { - iii *= -1; - iiimmm *= -1; - } - - if (corrisp) - if (tipodoc == "FS") - { - if (tipoiva == "VE") - vendite += iiimmm; - } - else - vendite += iii; - else - if (tipomov == vendita) - vendite += iii; - } - - if (autodafe) // autofatture non residenti art.17 - { - /* - * Si devono riportare a parte in annuale - * e non comparire nel riepilogo del codice IVA - * corrispondente, solo se la liq. e' annuale - * Li metto in R7/R8 di pim e li sommo anche nelle - * vendite; la describe_pim() dovra' sommarli su tutti i - * pim, scorporarli dalle rispettive vendite - * e assegnarli al codice IVA AF in caso di liq. annuale - */ - adf += imponibile; - adi += imposta; - } - - if (art40) // ammontare acq. intr. art40 c. 5/6/8 - { // Analogamente alle autofatture, si devono riportare a parte in annuale - // Vengono piazzati in una TToken_string con separatore = a '!'. - // La TToken_string viene registrata in S0. - // La describe_pim() sommera' tutte le storie, scorporandoli - // dalle vendite e assegnandoli al codice VA7 in caso di liq annuale. - va7i += imponibile; - va7v += imposta; - } - - if (corrisp && !true_corrisp) - { - fci += imponibile; - fcv += imposta; - } - - /* - * Calcolo e aggiornamento - * cerca o crea progressivo IVA. - * Onde evitare ripetizioni di codice adotto il metodo sottoindicato, - * come nell'azzeramento di zero_att(). - */ - int start = 0, stop = 1; - if (_recalc_regis) // ricalcolo solo per registri. - { - start = 2; - stop = 3; - } - else - { - if (year_int < 1998) - stop = 0; // Solo PIM - } - for (int i=start; i<=stop; i++) // Ciclo per le tabelle - { - if (i == 1 || i == 3) //Is it a valid PIS/PRP (vedi anche sel. cursore & date_ok())? - if ((liqmonth == 0 && month < 13) || (month == 13 && liqmonth != 12)) - continue; - - switch (i) - { - case 1: // Progressivi Iva mensili da periodo Successivo - look_pis(month, codatt, reg, tipocr_s, codiva, tipodet, TRUE); - break; - case 2: // Progressivi Registri iva Mensili - look_prm(month, codatt, reg, tipocr_s, codiva, tipodet, TRUE); - break; - case 3: // Progressivi Registri iva Mensili da periodo Precedente - look_prp(month, codatt, reg, tipocr_s, codiva, tipodet, TRUE); - break; - default: // Progressivi Iva Mensili normali - look_pim(month, codatt, reg, tipocr_s, codiva, tipodet, TRUE); - break; - } - - TTable *tab = arr[i]; - TRectype& curr = tab->curr(); - - if (!ign) - { - curr.add("R0", imp); - curr.add("R1", ivp); - curr.add("R2", lor); - curr.add("R3", lorcor); // Corrispettivi, non inclusi quelli da ventilare - curr.add("R5", ifs); - curr.add("R6", vfs); - curr.add("R7", adf); - curr.add("R8", adi); - curr.add("R9", bdi); - curr.add("R10", bdv); - curr.add("R13", fci); - curr.add("R14", fcv); - } - // questi servono per i ricalcoli altrui (classify_pim) o - // per trucchetti di ricalcolo successivi - curr.add("R11",fsi); - curr.add("R12",fsv); - - curr.put("I1", (long)tipomov); - curr.put("B3", ivarimb ? "X" : ""); - curr.put("B4", (_isricacq && was_riv) ? "X" : ""); - curr.put("S5", tipoiva); - - // art 40 c. 5/6/8 per VA7. - real r,v; - TToken_string kr(tab->get("S0"),'!'); - r = real(kr.get(0)) + va7i; - v = real(kr.get(1)) + va7v; - kr.add(r.string(), 0); - kr.add(v.string(), 1); - tab->put("S0",kr); - - // fatture in ritardo - TToken_string fr(tab->get("S1"),'!'); - r = real(fr.get(0)) + rti; - v = real(fr.get(1)) + rtv; - fr.add(r.string(), 0); - fr.add(v.string(), 1); - tab->put("S1",fr); - - // corrispettivi gia' scorporati; anche questi servono per - // non cannare la stampa dei registri; contengono anche - // tutti i non incassati (vedi definizione di bool nonimp) - TToken_string cs(tab->get("S2"),'!'); - r = real(cs.get(0)) + csi; - v = real(cs.get(1)) + csv; - cs.add(r.string(), 0); - cs.add(v.string(), 1); - tab->put("S2",cs); - - if (is_detraibile == 0 && - (!diff_imp.is_zero() ||!diff_iva.is_zero() || - !incdiff_imp.is_zero() || !incdiff_iva.is_zero())) - { - if (tipomov == vendita) - { - // fatture vendita a liquidazione differita - tab->curr().add("R26", diff_imp); - tab->curr().add("R27", diff_iva); - tab->curr().add("R28", incdiff_imp); - tab->curr().add("R29", incdiff_iva); - fdiff_imp += diff_imp; - fdiff_iva += diff_iva; - fdiffinc_imp += incdiff_imp; - fdiffinc_iva += incdiff_iva; - if (_mov->get_int(MOV_ANNOIVA) < year_int) - { - _diff_ven_ap += incdiff_imp; - _diff_ven_ap_iva += incdiff_iva; - } - } - else - { - // fatture acquisto a liquidazione differita - tab->curr().add("R30", diff_imp); - tab->curr().add("R31", diff_iva); - tab->curr().add("R32", incdiff_imp); - tab->curr().add("R33", incdiff_iva); - fdiff_imp_acq += diff_imp; - fdiff_iva_acq += diff_iva; - fdiffinc_imp_acq += incdiff_imp; - fdiffinc_iva_acq += incdiff_iva; - if (_mov->get_int(MOV_ANNOIVA) < year_int) - { - _diff_acq_ap += incdiff_imp; - _diff_acq_ap_iva += incdiff_iva; - } - } - } - - tab->rewrite(); // rewrite current table - } // for per tabelle - } // for is_detraibile - } while (_cur->next_match(LF_RMOVIVA)); - } // fine calcolo progressivi IVA - - - if (!_recalc_regis) - { - /* - * calcolati tutti i movimenti e aggiornati i pim - * se necessario risistema le imposte acquisti beni - * per rivendita - */ - - if (_isricacq) // flag settato parametri studio - { - for (_pim->first(); !_pim->eof(); _pim->next()) - { - // this is much furber than doing all of the checks - if (_pim->get_bool("B4")) - { - // ricalcola l'imposta a partire dal codice IVA - look_iva(*_pim_codiva); - real old_iva = _pim->get_real("R1"); - real perc = _iva->get_real("R0"); - - real new_iva = _pim->get_real("R0") * perc / CENTO; - round_al_centesimo(new_iva); - - if (new_iva != old_iva) - { - // ricalcola: acquisti_iva, acq_riv_iva - acquisti_iva -= old_iva; - acquisti_iva += new_iva; - acq_riv_iva -= old_iva; - acq_riv_iva += new_iva; - _pim->put("R1", new_iva); - // resetta in modo da non vederlo il mese dopo - _pim->put("B4", ""); - _pim->rewrite(); - } - } - } - } - - - // Ricalcola le imposte per regime agricolo dal 1998 (PIA) - if (_isagr98) - { - for (_pia->first(); !_pia->eof(); _pia->next()) - { - if (_pia->get_bool("B0")) - { - // calcola l'imposta a partire dal codice IVA di compensazione - look_iva(*_pia_codcom); - real perc = _iva->get_real("R0"); - - real iva = _pia->get_real("R0") * perc / CENTO; - round_al_centesimo(iva); - agr_detIA += iva; - _pia->put("R1",iva); - // resetta in modo da non vederlo il mese dopo - _pia->put("B0", ""); - _pia->rewrite(); - } - } - } - - /* - * calcola il lercio prorata - * solo se liq. periodica - */ - real prorata, prorata_precedente1, prorata_precedente2; - real percentuale1, percentuale2; - if (month != 13) - { - TString4 yy; - // Prorata per documenti registrati con anno doc 2 anni fa... - yy.format("%d", year_int - 2); - real* rr1 = (real*)acq_iva_anni_prec.objptr(yy); - yy.format("%d", year_int - 1); // Reperire la % prorata dell'anno successivo a quello del documento - percentuale1 = _prorata.percentuale(yy); - prorata_precedente1 = rr1 ? _prorata.calc_prorata(*rr1, yy) : ZERO; - // Se sono presenti documenti di anni precedenti senza % prorata, si deve segnalare l'incongruenza (solo in prova) - if (percentuale1 == INVALID_PRORATA && rr1 && *rr1 != ZERO) - percentuale1 = INVALID_PRORATA_ASSERT; - - // Prorata per documenti registrati con anno doc 3 anni fa... - yy.format("%d", year_int - 3); - real *rr2 = (real*)acq_iva_anni_prec.objptr(yy); - yy.format("%d", year_int - 2); // Reperire la % prorata dell'anno successivo a quello del documento - percentuale2 = _prorata.percentuale(yy); - prorata_precedente2 = rr2 ? _prorata.calc_prorata(*rr2, yy) : ZERO; - // Se sono presenti documenti di anni precedenti senza % prorata, si deve segnalare l'incongruenza (solo in prova) - if (percentuale2 == INVALID_PRORATA && rr2 && *rr2 != ZERO) - percentuale2 = INVALID_PRORATA_ASSERT; - - // Prorata per l'anno corrente + quelli eventuali dello scorso anno e quelli che sono oltre 3 anni fa... - // E quelli che non hanno la % prorata settata in tabella - real aaa = acquisti_iva; - if (percentuale1 != INVALID_PRORATA && percentuale1 != INVALID_PRORATA_ASSERT) - aaa -= rr1 ? *rr1 : ZERO; - if (percentuale2 != INVALID_PRORATA && percentuale2 != INVALID_PRORATA_ASSERT) - aaa -= rr2 ? *rr2 : ZERO; - prorata = _prorata.calc_prorata(aaa, _year); - aaa = fdiffinc_iva_acq; - const real prorata_diff = _prorata.calc_prorata(aaa, _year); - - // Somma i vari prorata calcolati per gli anni precedenti - prorata += prorata_precedente1 + prorata_precedente2; - - acquisti_iva -= prorata; // Rettifica acquisti_iva - - prorata += prorata_diff; - fdiffinc_iva_acq -= prorata_diff; // Rettifica acquisti_iva differiti incassati - } - look_plm(month, codatt, TRUE); - - /* - * ACHTUNG: l'iva sulle vendite e' calcolata sommando anche i - * corrispettivi che sono corrispettivi, a differenza dei corrispettivi - * che non sono corrispettivi, ma tanto quelli (come si sa) non sono - * corrispettivi. Dunque, non si tiene conto delle imposte calcolate - * con la ventilazione (che vanno sottratte) ne' di quelle - * calcolate per le agenzie di viaggio (che sembra non vadano - * comunque sottratte nel calcolo del volume di affari) - */ - - /* - * Il prorata pagato in annuale viene scritto a 0, perche' in realta' sarebbe - * la somma di quelli pagati nei mesi precedenti. - */ - _plm->put("R2", prorata); // Prorata complessivo (acq. rif. anno attuale + acq. rif. anni precedenti) - _plm->put("R14", prorata_precedente1); // Prorata acq. rif. anno addietro - _plm->put("R15", prorata_precedente2); // Prorata acq. rif. 2 anni addietro - - if (_isviaggio) - { - _plm->put("R5", corr_CEE); - _plm->put("R6", corr_noCEE); - _plm->put("R7", acq_CEE); - _plm->put("R8", acq_noCEE); - _plm->put("R9", corr_misCEE); - _plm->put("R10",acq_misCEE); - _plm->put("R11",acq_misnoCEE); - - // ---- NOTA BENE ------------------------ - // l'iva vendite nei plm non comprende - // l'iva agenzie di viaggio, calcolata - // in liquidazione (mensile o trimestrale): recalc_viaggio() - } - else if (_isagricolo) - { - _plm->put("R5", agr_1); - _plm->put("R6", agr_2); - _plm->put("R7", agr_3); - _plm->put("R8", agr_4); - _plm->put("R9", agr_5); - _plm->put("R10", agr_6); - _plm->put("R11", agr_7); - _pum->put("R10", agr_1i); - _pum->put("R11", agr_2i); - - // Il porco agricoltore spende poco e paga meno - vendite_iva = agr_2; - acquisti_iva = agr_1 + agr_3; - - // nuovo calcolo IVA detraibile - if (!agr_7.is_zero()) - { - real pdetr = (agr_2i / (agr_1i + agr_2i)); - pdetr.round(2); - acquisti_iva += agr_7 * pdetr; - round_al_centesimo(acquisti_iva); - } - - if (_isagr98) - { - _plm->put("R13", agr_detIA); - acquisti_iva += agr_detIA; - } - } - - _plm->put("R12", _prorata.percentuale(_year)); // per comodita' in stampa - _plm->put("R4", percentuale1); - _plm->put("R16", percentuale2); - _plm->put("R0", vendite_iva); - _plm->put("R1", acquisti_iva); - _plm->put("R3", ult_detr); - _plm->put("B0", "X"); // calcolato (invalidato dalla primanota) - // Setta il flag di stampa errore: se siamo oltre 1997 e non esiste tabella anno prec e vi sono docs con tale riferimento - _pom->put("R9", acq_ies); - _pom->put("R10", acq_ies_iva); - _pum->put("R8", acq_pint); - _pum->put("R9", acq_pint_iva); - _pam->put("R10", spgen); - _pam->put("R11", spgen_iva); - _pom->put("R0", totintra); - _pom->put("R1", nond19_imp); - _pom->put("R2", nond19_iva); - _pom->put("R3", ammort_det); - _pom->put("R4", ammort_det_iva); - _pom->put("R5", acq_riv); - _pom->put("R6", acq_riv_iva); - _pom->put("R7", leasing); - _pom->put("R8", leasing_iva); - _pom->put("R11", ammort_indet); - _pom->put("R12", ammort_indet_iva); - _pum->put("R0", cess_amm); - _pum->put("R1", cess_amm_iva); - _pum->put("R2", ammort_6); - _pum->put("R3", ammort_6_iva); - _pum->put("R4", esenti_c1); - _pum->put("R5", esenti_c2); - _pum->put("R6", esenti_c3); - _pum->put("R13", esenti_c1a); - _pum->put("S4", esenti_c3_bam); - _pum->put("S5", esenti_c1a_bam); - _pum->put("R7", esenti_b14); - _pum->put("R12", esni_rimb); - _pum->put("R14", cessioni_cd1_1); - _pum->put("R15", cessioni_cd1_2); - _pum->put("R16", cessioni_cd1_3); - _pum->put("R17", cessioni_cd1_4); - _pum->put("R18", acquisti_cd2_1); - _pum->put("R19", acquisti_cd2_2); - _pum->put("R20", acquisti_cd2_3); - _pum->put("R21", acquisti_cd2_4); - _pum->put("R22", oroargento_cd3_1); - _pum->put("R23", oroargento_cd3_2); - _pum->put("R24", rottami_cd3_3); - _pum->put("R25", rottami_cd3_4); - _pum->put("R26", acquisti_cd2_5); - _pum->put("R27", cessioni_cd1_5); - _pam->put("R0", acquisti); - _pam->put("R1", vendite); - _pam->put("R6", assp_imp); - _pam->put("R7", assp_iva); - _pam->put("R8", vssp_imp); - _pam->put("R9", vssp_iva); - _pam->put("R4", bdog_imp); - _pam->put("R5", bdog_iva); - _pam->put("R12", assl_imp); - _pam->put("R13", assl_iva); - _pam->put("R14", vssl_imp); - _pam->put("R15", vssl_iva); - - _plm->put("R26", fdiff_imp); - _plm->put("R27", fdiff_iva); - _plm->put("R28", fdiffinc_imp); - _plm->put("R29", fdiffinc_iva); - _plm->put("R30", fdiff_imp_acq); - _plm->put("R31", fdiff_iva_acq); - _plm->put("R32", fdiffinc_imp_acq); - _plm->put("R33", fdiffinc_iva_acq); - - _plm->rewrite(); - _pam->rewrite(); - _pum->rewrite(); - _pom->rewrite(); - - if (month == 13) - { - TTable pem("PEM"); - for (int reg = 0; reg < 23; reg++) - { - look_pem(pem, reg); - real r = pem.get_real("R0") + vt_imponibile[reg]; - pem.put("R0", r); - r = pem.get_real("R1") + vt_imposta[reg]; - pem.put("R1", r); - pem.rewrite(); - } - } - } -} - -void TLiquidazione_app::iva11_set_arr_phase_1(const TString& codatt) -// viene chiamata quando vengono scorsi i movimenti del mese, settando gran parte degli elementi di _iva11_arr. -{ - // My God! Che 2 palle riestrarre tutte le informazioni - const TString4 tipodoc = _mov->get(MOV_TIPODOC); - const bool intra = _rmoviva->get_bool(RMI_INTRA); - const int tipocr = _rmoviva->get_int(RMI_TIPOCR); - // A partire dalla versione 3.1 la gestione dell'IVA 11 non viene piu' supportata ... - // ... ergo non stiamo a gestire la indetraibilita' parziale - real percind; - const int tipodet = get_tipodet_from_rmi(_rmoviva->curr(), _mov->curr(), percind); - real imponibile = _rmoviva->get_real(RMI_IMPONIBILE); - real imposta = _rmoviva->get_real(RMI_IMPOSTA); - const TString4 codiva = _iva->get("CODTAB"); - const TString4 tipoiva = _iva->get("S1"); - const real ali = _iva->get_real("R0"); - const TString16 tipoes_v = _iva->get("S2"); - const TString16 tipoes_a = _iva->get("S9"); - const int tipoagr = _iva->get_int("S4"); - const TRectype& rcs = _cur->curr(LF_CAUSALI); - const bool autofattura = rcs.get_bool("AUTOFATT"); - const bool valintra = rcs.get_bool("VALINTRA"); - const bool intracom = rcs.get_bool("INTRACOM"); - const tipo_sospensione sosp_imp = _reg->get_bool("B1") ? (tipo_sospensione) _reg->get_int("I9") : nessuna; - const bool corrisp = _reg->get_bool("B0"); - const tiporeg tipomov = (tiporeg)_reg->get_long("I0"); - const bool is_vendita = tipomov == vendita; - const bool is_acquisto = tipomov == acquisto; - - const bool is_key = _iva11_arr.is_key(codatt); - if (!is_key) // Aggiunge l'elemento se non c'e' - _iva11_arr.add(codatt,(_Iva11Array*)new _Iva11Array,is_key); - _Iva11Array& array = (_Iva11Array&)_iva11_arr[codatt] ; - - - // TAB11_RQA34 ovvero "RQA34" non viene trasferito! - // Sebbene per motivi di pietà viene comunque lasciato nel tracciato record (esigenze PRASSI) - if (codiva.blank()) - return; - - if (is_acquisto) - { - if (_isagricolo && tipodet == 0) - { - if (tipoagr == 2) - { - if (tipocr == 2 || tipocr == 3 || tipocr == 8) - { - array.sub(imponibile,TAB11_G30I,LF_TAB1100B); - array.sub(imposta,TAB11_G30V,LF_TAB1100B); - } - else - { - array.add(imponibile,TAB11_G30I,LF_TAB1100B); - array.add(imposta,TAB11_G30V,LF_TAB1100B); - } - } - else if (tipoagr == 3 && tipocr !=2 && tipocr != 3 && tipocr != 8) - { - array.add(imponibile,TAB11_G31I,LF_TAB1100B); - array.add(imposta,TAB11_G31V,LF_TAB1100B); - } - } - - if (sosp_imp == nessuna && tipodet == 0 && (tipocr == 2 || tipocr == 8)) - { - array.add(imposta,TAB11_R6,LF_TAB1100B); - } - - if (sosp_imp == nessuna || sosp_imp == vol_affari) - { - if (tipodoc == "BD" && tipodet == 9) - { - array.add(imponibile,TAB11_F76); - if (_sind11) - array.add(imposta,TAB11_F76); - } - - if (tipodoc != "BD") - { // Compila prima i campi FC1nn ed FC2nn, poi i campi SBFnn ed SBInn - if (tipodet != 9) - { - if (ali == 2.00) - { - array.add(imponibile,TAB11_FC101); - array.add(imposta,TAB11_FC201); - } - else if (ali == 4.00) - { - array.add(imponibile,TAB11_FC102); - array.add(imposta,TAB11_FC202); - } - else if (ali == 6.00) - { - array.add(imponibile,TAB11_FC103); - array.add(imposta,TAB11_FC203); - } - else if (ali == 7.50) - { - array.add(imponibile,TAB11_FC104); - array.add(imposta,TAB11_FC204); - } - else if (ali == 8.50) - { - array.add(imponibile,TAB11_FC105); - array.add(imposta,TAB11_FC205); - } - else if (ali == 9.00) - { - array.add(imponibile,TAB11_FC106); - array.add(imposta,TAB11_FC206); - } - else if (ali == 10.00) - { - array.add(imponibile,TAB11_FC107); - array.add(imposta,TAB11_FC207); - } - else if (ali == 16.00) - { - array.add(imponibile,TAB11_FC108); - array.add(imposta,TAB11_FC208); - } - else if (ali == 19.00) - { - array.add(imponibile,TAB11_FC109); - array.add(imposta,TAB11_FC209); - } - else if (ali == 20.00) - { - array.add(imponibile,TAB11_FC110); - array.add(imposta,TAB11_FC210); - } - - if (tipoes_a == "15") - array.add(imponibile,TAB11_FC111); - else if (tipoes_a == "12") - array.add(imponibile,TAB11_FC112); - else if (tipoes_a == "13") - array.add(imponibile,TAB11_FC113); - else if (tipoes_a == "14") - array.add(imponibile,TAB11_FC114); - else if (tipoes_a == "16") - array.add(imponibile,TAB11_FC115); - } - else - { - array.add(imponibile,TAB11_FC116); - if (_sind11) - array.add(imposta,TAB11_FC116); - } - - if (intra) // Compila i campi SBFnn - { - if (tipodet != 9) - { - if (ali == 4.00) - { - array.add(imponibile,TAB11_SBF01); - array.add(imposta,TAB11_SBI01); - } - else if (ali == 6.00) - { - array.add(imponibile,TAB11_SBF02); - array.add(imposta,TAB11_SBI02); - } - else if (ali == 7.50) - { - array.add(imponibile,TAB11_SBF03); - array.add(imposta,TAB11_SBI03); - } - else if (ali == 9.00) - { - array.add(imponibile,TAB11_SBF04); - array.add(imposta,TAB11_SBI04); - } - else if (ali == 10.00) - { - array.add(imponibile,TAB11_SBF05); - array.add(imposta,TAB11_SBI05); - } - else if (ali == 16.00) - { - array.add(imponibile,TAB11_SBF06); - array.add(imposta,TAB11_SBI06); - } - else if (ali == 19.00) - { - array.add(imponibile,TAB11_SBF07); - array.add(imposta,TAB11_SBI07); - } - else if (ali == 20.00) - { - array.add(imponibile,TAB11_SBF08); - array.add(imposta,TAB11_SBI08); - } - - if (tipoes_a == "15") - array.add(imponibile,TAB11_SBF09); - else if (tipoes_a == "12") - array.add(imponibile,TAB11_SBF10); - else if (tipoes_a == "13") - array.add(imponibile,TAB11_SBF11); - else if (tipoes_a == "14") - array.add(imponibile,TAB11_SBF12); - } - else - { - array.add(imponibile,TAB11_SBF13); - if (_sind11) - array.add(imposta,TAB11_SBF13); - } - } - } - } - } - - if (is_vendita) - { - if (tipodoc == "CN" || tipodoc == "SN" || tipodoc == "RN") // salta i non incassati - return; - - if (intracom || !valintra) - { - if (tipoes_v.not_empty()) // Inutile fare troppi controlli... - { - if (tipoes_v == "24") - array.add(imponibile,TAB11_EC106); - if (tipoes_v == "21") - array.add(imponibile,TAB11_EC107); - else if (tipoes_v == "B1") - array.add(imponibile,TAB11_EC108); - else if (tipoes_v == "B2") - array.add(imponibile,TAB11_EC109); - else if (tipoes_v == "B3") - array.add(imponibile,TAB11_EC110); - else if (tipoes_v == "25") - array.add(imponibile,TAB11_EC111); - else if (tipoes_v == "22") - array.add(imponibile,TAB11_EC113); - else if (tipoes_v == "20") - array.add(imponibile,TAB11_EC114); - else if (tipoes_v == "G7A") - array.add(imponibile,TAB11_EC115); - else if (tipoes_v == "G7B") - array.add(imponibile,TAB11_EC116); - else if (tipoes_v == "R9") // Divenuto obsoleto prima ancora di utilizzarlo... - array.add(imponibile,TAB11_R9,LF_TAB1100B); - } - - if (tipocr == 4 && tipodoc != "AF" && (sosp_imp == nessuna || sosp_imp == vol_affari)) - array.add(imponibile,TAB11_EC112); - } - - if (!(corrisp && tipoiva != "VE" && (tipodoc == "CR" || tipodoc == "RF" || tipodoc == "SC"))) - { - if (!autofattura && tipoiva != "NS") - if (tipodoc == "AF") - { - array.add(imponibile,TAB11_A35); - array.add(imposta,TAB11_L2,LF_TAB1100B); - } - else if (valintra && !intra) - { - array.add(imponibile,TAB11_A35BIS); - array.add(imposta,TAB11_L2BIS,LF_TAB1100B); - } - - if (!autofattura && (sosp_imp == nessuna || sosp_imp == vol_affari) && intra && tipoes_v == "22") - array.add(imponibile,TAB11_E52); - - if (_isagricolo && (tipodoc != "CR" && tipodoc != "SC" && tipodoc != "RF" && tipodoc != "FS")) - { - const bool t1 = tipoagr == 1; - const bool t1_3 = tipoagr == 1 || tipoagr == 3; - if (ali == 2.00) - { - if (t1) - { - array.add(imponibile,TAB11_AGME01,LF_TAB1100B); - array.add(imposta,TAB11_AGVE01,LF_TAB1100B); - } - if (intra && t1_3) - { - array.add(imponibile,TAB11_ABME01,LF_TAB1100B); - array.add(imposta,TAB11_ABVE01,LF_TAB1100B); - } - } - else if (ali == 4.00) - { - if (t1) - { - array.add(imponibile,TAB11_AGME02,LF_TAB1100B); - array.add(imposta,TAB11_AGVE02,LF_TAB1100B); - } - if (intra && t1_3) - { - array.add(imponibile,TAB11_ABME02,LF_TAB1100B); - array.add(imposta,TAB11_ABVE02,LF_TAB1100B); - } - } - else if (ali == 6.00) - { - if (t1) - { - array.add(imponibile,TAB11_AGME03,LF_TAB1100B); - array.add(imposta,TAB11_AGVE03,LF_TAB1100B); - } - if (intra && t1_3) - { - array.add(imponibile,TAB11_ABME03,LF_TAB1100B); - array.add(imposta,TAB11_ABVE03,LF_TAB1100B); - } - } - else if (ali == 7.50) - { - if (t1) - { - array.add(imponibile,TAB11_AGME04,LF_TAB1100B); - array.add(imposta,TAB11_AGVE04,LF_TAB1100B); - } - if (intra && t1_3) - { - array.add(imponibile,TAB11_ABME04,LF_TAB1100B); - array.add(imposta,TAB11_ABVE04,LF_TAB1100B); - } - } - else if (ali == 8.50) - { - if (t1) - { - array.add(imponibile,TAB11_AGME05,LF_TAB1100B); - array.add(imposta,TAB11_AGVE05,LF_TAB1100B); - } - if (intra && t1_3) - { - array.add(imponibile,TAB11_ABME05,LF_TAB1100B); - array.add(imposta,TAB11_ABVE05,LF_TAB1100B); - } - } - else if (ali == 9.00) - { - if (t1) - { - array.add(imponibile,TAB11_AGME06,LF_TAB1100B); - array.add(imposta,TAB11_AGVE06,LF_TAB1100B); - } - if (intra && t1_3) - { - array.add(imponibile,TAB11_ABME06,LF_TAB1100B); - array.add(imposta,TAB11_ABVE06,LF_TAB1100B); - } - } - } - } - } - - if (is_acquisto || is_vendita) - if (autofattura) - array.add(imponibile,TAB11_A35TER); -} - -void TLiquidazione_app::iva11_set_arr_phase_2(const TString& codatt) -// scorre (guarda un poco) tutti i PIM della ditta per COMPLETARE _iva11_arr -// Ribadisco: questa funzione COMPLETA solamente l'array scorrendo i PIM e memorizzando i rimanenti -// campi. Gli altri sono compilati scorrendo i movimenti! (fatto prima della chiamata a questa funzione) -// Il motivo per cui il trasferimento e' fatto in 2 fasi (iva11_set_arr_phase_1 ed iva11_set_arr_phase_2) -// e' perche' alcune operazioni di calcolo (scorporo corrispettivi, ventilazione...) vengono effettuate -// dopo la recalc_att(), punto in cui viene chiamata la phase_1. -{ - TString4 codiva, reg, tiva; - TString16 tpla, tvia; - TString16 v11, a11; - TToken_string tt("",'!'); - tiporeg treg; - int tipocr,tipodet,tagr; - real aliq, imp, iva, lor, vtot, atot, ivav, ivaa, adf, adi, va7i, va7v; - TString16 cur_att; - - const bool is_key = _iva11_arr.is_key(codatt); - if (!is_key) - _iva11_arr.add(codatt,(_Iva11Array*)new _Iva11Array,is_key); - _Iva11Array& array = (_Iva11Array&)_iva11_arr[codatt]; - - // Scorporo corretto dei corrispettivi in R3 - TAssoc_array corr_ann; - bool is_present; - - for (_pim->first(); !_pim->eof(); _pim->next()) - { - if (_year != *_pim_anno) continue; - if (atoi(*_pim_mese) != 13) continue; - cur_att = *_pim_codatt; - cur_att.rtrim(1); - if (codatt != cur_att) continue; // considera solo quelli dell'anno, obviously. - // Must consider current activity too... - codiva = *_pim_codiva; - reg = *_pim_codreg; - look_iva(codiva); look_reg(reg); // posiziona la tabella registri e quella IVA - tiva = _iva->get("S1"); // tipo IVA - tpla = _iva->get("S3"); // tipo gestione plafond - tagr = _iva->get_int("S4"); // tipo gestione regime agricolo - tvia = _iva->get("S5"); // tipo gestione ag. viaggio - v11 = _iva->get("S2"); // n.ro riga vendite per mod. IVA11 - a11 = _iva->get("S9"); // n.ro riga acquisti per mod. IVA11 - aliq = _iva->get_real("R0"); // aliquota - treg = (tiporeg)_reg->get_long("I0"); - const bool is_vendita = treg == vendita; - const bool is_acquisto = treg == acquisto; - tipocr = atoi(*_pim_tipocr); - tipodet = atoi(*_pim_tipodet); - imp = _pim->get_real("R0"); - iva = _pim->get_real("R1"); - lor = _pim->get_real("R3"); - - if (is_acquisto) - { - if (tiva != "NS") - switch (tipocr) - { - case 2: - array.add(imp,TAB11_IMA01); - break; - case 8: - array.add(imp,TAB11_IMA02); - break; - case 1: - case 5: - array.add(imp,TAB11_IMA03); - break; - default: - break; - } - - if (tipodet == 1) - array.add(iva,TAB11_B14); - - // XC???: tipo detr != 9 - if (tipodet != 9) - { - if (aliq == 4.00) - { - array.add(_pim->get_real("R9"),TAB11_XC101); - array.add(_pim->get_real("R10"),TAB11_XC201); - } - else if (aliq == 6.00) - { - array.add(_pim->get_real("R9"),TAB11_XC102); - array.add(_pim->get_real("R10"),TAB11_XC202); - } - else if (aliq == 7.50) - { - array.add(_pim->get_real("R9"),TAB11_XC103); - array.add(_pim->get_real("R10"),TAB11_XC203); - } - else if (aliq == 9.00) - { - array.add(_pim->get_real("R9"),TAB11_XC104); - array.add(_pim->get_real("R10"),TAB11_XC204); - } - else if (aliq == 10.00) - { - array.add(_pim->get_real("R9"),TAB11_XC105); - array.add(_pim->get_real("R10"),TAB11_XC205); - } - else if (aliq == 16.00) - { - array.add(_pim->get_real("R9"),TAB11_XC106); - array.add(_pim->get_real("R10"),TAB11_XC206); - } - else if (aliq == 19.00) - { - array.add(_pim->get_real("R9"),TAB11_XC107); - array.add(_pim->get_real("R10"),TAB11_XC207); - } - else if (aliq == 20.00) - { - array.add(_pim->get_real("R9"),TAB11_XC108); - array.add(_pim->get_real("R10"),TAB11_XC208); - } - - if (a11 == "12") - array.add(_pim->get_real("R9"),TAB11_XC109); - if (a11 == "13" || a11 == "14") - array.add(_pim->get_real("R9"),TAB11_XC110); - if (a11 == "16") - array.add(_pim->get_real("R9"),TAB11_XC111); - } - } // is_acquisto - - if (is_vendita) - { - if ((_isagricolo && tagr==2) || !_isagricolo) - { - // Accumula i corrispettivi da scorporare per codice iva - if (lor != ZERO) - { - is_present = corr_ann.is_key(codiva); - if (!is_present) // Aggiungilo se non c'e' - corr_ann.add(codiva,(_CorrItem*)new _CorrItem,is_present); - _CorrItem& ca = (_CorrItem&) corr_ann[codiva]; - ca._totale += lor; - if (!is_present) - ca._aliquota = _iva->get_real("R0"); // Se e' nuovo setta l'aliquota - } - - tt = _pim->get("S0"); - va7i = tt.get(0); - va7v = tt.get(1); - adf = imp - _pim->get_real("R7") - va7i + _pim->get_real("R5"); - adi = iva - _pim->get_real("R8") - va7v + _pim->get_real("R6"); - if (aliq == 4.00) - { - array.add(adf,TAB11_EC101); - array.add(adi,TAB11_EC201); - } - else if (aliq == 10.00) - { - array.add(adf,TAB11_EC102); - array.add(adi,TAB11_EC202); - } - else if (aliq == 16.00) - { - array.add(adf,TAB11_EC103); - array.add(adi,TAB11_EC203); - } - else if (aliq == 19.00) - { - array.add(adf,TAB11_EC104); - array.add(adi,TAB11_EC204); - } - else if (aliq == 20.00) - { - array.add(adf,TAB11_EC105); - array.add(adi,TAB11_EC205); - } - } - } // is_vendita - - // Sommatoria per il calcolo aliquote medie vendite/acquisti... - if (_pim->get_bool("B3")) // E' un record valido per fare questa cosa? - { - if ((tiporeg)_pim->get_long("I1") == vendita) - { //vendite - vtot += imp; - ivav += iva; - } - else - { - //acquisti - if (tipodet != 9) // Evita di sommare quelli con tipo detraibilita' 9 (MI2209) - { - atot += imp; - ivaa += iva; - } - } - } - } - - // Scorre i corrispettivi lordi accumulati, li scorpora ed aggiorna i righi relativi - _CorrItem* cc; - for (cc = (_CorrItem *)corr_ann.first_item(); cc != NULL; cc = (_CorrItem *)corr_ann.succ_item()) - { - lordo2netto(cc->_totale,imp,iva,cc->_aliquota/CENTO); - vtot += imp; ivav += iva; - if (cc->_aliquota == 4.00) - { - array.add(imp,TAB11_EC101); - array.add(iva,TAB11_EC201); - } - else if (cc->_aliquota == 10.00) - { - array.add(imp,TAB11_EC102); - array.add(iva,TAB11_EC202); - } - else if (cc->_aliquota == 16.00) - { - array.add(imp,TAB11_EC103); - array.add(iva,TAB11_EC203); - } - else if (cc->_aliquota == 19.00) - { - array.add(imp,TAB11_EC104); - array.add(iva,TAB11_EC204); - } - else if (cc->_aliquota == 20.00) - { - array.add(imp,TAB11_EC105); - array.add(iva,TAB11_EC205); - } - } - - - // Mmmhh.. Rettifica l'importo di EC114: EC114 = EC115 + EC116 + EC114 - real r15, r16; - if (array.is_key(TAB11_EC115)) - r15 = ((_Iva11Item&)array[TAB11_EC115]).value(); - if (array.is_key(TAB11_EC116)) - r16 = ((_Iva11Item&)array[TAB11_EC116]).value(); - r16+=r15; - array.add(r16,TAB11_EC114); - - - // Calcola le maledette aliquote medie... - real alv,ala; - if (vtot != 0.0) { alv = ivav/vtot; alv *= CENTO; alv.round(2); } - if (atot != 0.0) { ala = ivaa/atot; ala *= CENTO; ala.round(2); } - array.add(ala,TAB11_R1,LF_TAB1100B); - array.add(alv,TAB11_R2,LF_TAB1100B); - - // Mo' si pappa i PLM per compilare T1, T1C, T2, T2C, T3, T5 e T6 - // ATTENZIONE: solo per i regimi 74ter (ag. viaggio) - for (_plm->first();!_plm->eof();_plm->next()) - { - if (_year != *_plm_anno || atoi(*_plm_mese) != 13) continue; - cur_att = *_plm_codatt; - cur_att.rtrim(1); - if (codatt != cur_att) continue; - if (_isviaggio) - { - array.add(_plm->get_real("R5"),TAB11_T1,LF_TAB1100B); - array.add(_plm->get_real("R7"),TAB11_T1C,LF_TAB1100B); - array.add(_plm->get_real("R6"),TAB11_T2,LF_TAB1100B); - array.add(_plm->get_real("R8"),TAB11_T2C,LF_TAB1100B); - array.add(_plm->get_real("R9"),TAB11_T3,LF_TAB1100B); - array.add(_plm->get_real("R10"),TAB11_T5,LF_TAB1100B); - array.add(_plm->get_real("R11"),TAB11_T6,LF_TAB1100B); - } - array.add(_plm->get_real("R0"),TAB11_IVVENR); - } -} - -void TLiquidazione_app::iva11_write(bool reset_r1r2) -// Trasferisce i dati IVA11 annuali dall'assoc_array al file tab1100 -{ - const int items = _iva11_arr.items(); - if (items == 0) return; - - TString ditta,attiv,fld_name; - - _Iva11Array * iva11_arr; - _Iva11Item * iva11; - TLocalisamfile& tab1100a = _tab11->lfile(); - TLocalisamfile& tab1100b = _tab11->lfile(LF_TAB1100B); - - int err; - // Ciclo sulle attivita' - for (iva11_arr = (_Iva11Array *) _iva11_arr.first_item(); iva11_arr != NULL; iva11_arr = (_Iva11Array*)_iva11_arr.succ_item()) - { - // Setta i campi chiave per entrambi i files della relazione - ditta.format("%05ld",_nditte->curr().get_long(NDT_CODDITTA)); - attiv = _iva11_arr.get_hashobj()->key(); - tab1100a.zero(); - tab1100a.put(TAB11_TADITT,ditta); - tab1100a.put(TAB11_TACATT,attiv); - tab1100b.zero(); - tab1100b.put(TAB11_TADITT,ditta); - tab1100b.put(TAB11_TACATT,attiv); - - const int iva11_items = iva11_arr->items(); - // Ciclo sugli elementi per compilare i campi della relazione - for (iva11 = (_Iva11Item *)iva11_arr->first_item(); iva11 != NULL;iva11 = (_Iva11Item *)iva11_arr->succ_item()) - { - fld_name = iva11_arr->get_hashobj()->key(); - if (reset_r1r2 && (fld_name == TAB11_R1 || fld_name == TAB11_R2)) continue; // salta R1/R2 se devono rimanere a zero - _tab11->lfile(iva11->file()).put(fld_name,iva11->value()); - } - - if (_tab11->write() != NOERR) - if ((err=_tab11->rewrite()) != NOERR) - error_box(TR("Errore %d tentando di scrivere sul file tab1100."),err); - } - - _iva11_arr.destroy(); // resetta l'array. -} - -void TLiquidazione_app::write_liq(int month, const char* codatts) - // Calcolo liq. mensili e liq. annuali. Scrive le lim -{ - TToken_string atts(codatts); - const int year_int = atoi(_year); - - if (_ver->read(year_int, _month)!=NOERR) - warning_box(TR("Errore %d in lettura tabella versamenti ed interessi."),_ver->status()); - -/* Cazzata galattica: già gestito BENE da update_firm, prima di chiamare questa funzione - // evita casino se differita ma si sta ricalcolando il primo mese con ricalcolo di piu' mesi - const bool wasdifferita = (_isdifferita && month <= _monthinatt && _month > _monthinatt); - if (wasdifferita) _isdifferita = FALSE; -*/ - // liq. differita: considera mese precedente (solo per i risultati) - int deltam = month; - if (_isdifferita && !is_first_month(month) && month != 13) - month -= (_freqviva == "T" ? 3 : 1); - deltam -= month; - - look_lia(); - const bool new_age_2000 = (year_int >= 2000) && (_lia->get("S9") == "NV"); - - real risultato = ZERO; - real detrazioni = ZERO; - real versamenti = ZERO; - real vers_int = ZERO; - real rimborsi = ZERO; - real rettifiche = ZERO; - real res_debt = ZERO; - real res_cred = ZERO; - real cred_prec = ZERO; - real cred_trasf = ZERO; - real debt_precd = ZERO; - real acc_dec = ZERO; - real iva_vend = ZERO; - real iva_acq = ZERO; - real conguaglio = ZERO; - real variazioni_imposta = ZERO; - real imposta_non_versata = ZERO; - real crediti_speciali = ZERO; - - // totali per regimi agricoli - real acq_noCEE = ZERO; - real imp_agr1 = ZERO; - real imp_agr2 = ZERO; - - // totali per agenzie viaggio - real c_iCEE = ZERO; - real c_eCEE = ZERO; - real a_iCEE = ZERO; - real a_eCEE = ZERO; - real c_mCEE = ZERO; - real a_mCEE = ZERO; - real a_meCEE = ZERO; - real cred_cost = ZERO; - real deb_mens = ZERO; - real perc_r = ZERO; - real fdiff_imp = ZERO; - real fdiff_iva = ZERO; - real fdiffinc_imp = ZERO; - real fdiffinc_iva = ZERO; - real fdiff_imp_acq = ZERO; - real fdiff_iva_acq = ZERO; - real fdiffinc_imp_acq = ZERO; - real fdiffinc_iva_acq = ZERO; - - // totali per annuali - real vol_aff_1 = ZERO; - real vol_aff_2 = ZERO; - real vol_aff_t = ZERO; - real vol_aff_l = ZERO; - real tot_cong = ZERO; - - bool differita = FALSE; - bool almeno_una_agricola = FALSE; - bool almeno_una_normale = FALSE; - int attc = 0; // counter attivita' - - // nuova liquidazione - real credito_utilizzabile_inizio_anno = ZERO; - real credito_utilizzato_inizio_anno = ZERO; - real credito_utilizzato_iva = ZERO; - if (new_age_2000) - { - credito_utilizzato_inizio_anno = _lia->get_real("R15"); - credito_utilizzabile_inizio_anno = _lia->get_real("R0") - credito_utilizzato_inizio_anno; - if (credito_utilizzabile_inizio_anno < ZERO) - credito_utilizzabile_inizio_anno = ZERO; - } - - TLocalisamfile& attiv = _nditte->lfile(LF_ATTIV); - - const char* tmpatt; - while ((tmpatt = atts.get()) != NULL) - { - const TString16 att = tmpatt; - - // Reperisce il flag di regime agricolo per ogni attivita'... - attiv.put(ATT_CODATT,att); - attiv.read(); - const bool attivita_agricola = attiv.get_bool(ATT_REGAGR); - almeno_una_agricola |= attivita_agricola; - almeno_una_normale |= !attivita_agricola; - - if (_isdifferita && is_first_month(month+deltam)) - { - // usa i totali del mese di dicembre dell'anno - // precedente - differita = TRUE; - const TString4 yr(_year); // Salvo anno corrente - _year.format("%d", year_int-1); - if (!look_lim(12)) //controlla solamente, il vero posizionamento lo fa dopo - { - _year = yr; // Ripristino anno corrente - differita = FALSE; - } - } - - for (int m = 1; m <= 13; m++) - { - // ciclo su tutti i mesi del caso (1 o 3; - // tutti se annuale) - if (!is_month_ok(m, month)) - continue; - - look_plm(differita ? 12 : m, att); - bool is_lim = look_lim(_isdifferita ? m+deltam : m); - - // Se trattasi di annuale considera solo PLM/PUM del mese 13 - if ((month == 13 && m == 13) || month != 13) - { - // gia' conteggiato: prorata - // da conteggiare: IVA vendite ag. viaggio - // a debito: IVA vendite, debito precedente - // a credito: IVA acquisti, ulteriori detrazioni 1 e 2 - - if (!attivita_agricola) - { - const real vend = _plm->get_real("R0"); - const real acq = _plm->get_real("R1"); - const real diff_imp = _plm->get_real("R26"); - const real diff_iva = _plm->get_real("R27"); - const real diffinc_imp = _plm->get_real("R28"); - const real diffinc_iva = _plm->get_real("R29"); - const real diff_imp_acq = _plm->get_real("R30"); - const real diff_iva_acq = _plm->get_real("R31"); - const real diffinc_imp_acq = _plm->get_real("R32"); - const real diffinc_iva_acq = _plm->get_real("R33"); - - iva_vend += vend; - iva_acq += acq; - risultato += (vend - acq + diffinc_iva - diffinc_iva_acq); - res_debt += vend + diffinc_iva; - res_cred += acq; - fdiff_imp += diff_imp; - fdiff_iva += diff_iva; - fdiffinc_imp += diffinc_imp; - fdiffinc_iva += diffinc_iva; - fdiff_imp_acq += diff_imp_acq; - fdiff_iva_acq += diff_iva_acq; - fdiffinc_imp_acq += diffinc_imp_acq; - fdiffinc_iva_acq += diffinc_iva_acq; - } - else - { - // Tiene aggiornata l'iva vendite - iva_vend += _plm->get_real("R6") + _plm->get_real("R5"); - // Tiene aggiornata l'iva acquisti, che andra' rettificata all'uscita del mese - // secondo la formula: - // iva_acq = iva_acq + iva detraibile calcolata + iva compensazione - // L'iva di compensazione e' tratta dall'iva vendite agricole con segnalino 1 (vedi tabella PIA) - // il cui totale e' memorizzato in plm->R13 - // l'iva detraibile calcolata e' a sua volta fatta cosi' - // sum(plm->R11) * { sum(pum->R11) / [sum(pum->R11) + sum(pum->R10)]} - // dove plm->R11 = acquisti misti fuori CEE - // pum->R10 = tot. imponibile reg agr. 1 - // pum->R11 = tot. imponibile reg agr. 2 - // L'iva acquisti viene calcolata in modo diverso se siamo dal 1998 in poi: - // somma l'iva ammessa in detr. I parte tab. A (R13) anziche' iva vendite (R5) - iva_acq += _plm->get_real("R7") + _plm->get_real(year_int > 1997 ? "R13" : "R5"); - acq_noCEE += _plm->get_real("R11"); - imp_agr1 += _pum->get_real("R10"); - imp_agr2 += _pum->get_real("R11"); - - const real diff_imp = _plm->get_real("R26"); - const real diff_iva = _plm->get_real("R27"); - const real diffinc_imp = _plm->get_real("R28"); - const real diffinc_iva = _plm->get_real("R29"); - const real diff_imp_acq = _plm->get_real("R30"); - const real diff_iva_acq = _plm->get_real("R31"); - const real diffinc_imp_acq = _plm->get_real("R32"); - const real diffinc_iva_acq = _plm->get_real("R33"); - - fdiff_imp += diff_imp; - fdiff_iva += diff_iva; - fdiffinc_imp += diffinc_imp; - fdiffinc_iva += diffinc_iva; - fdiff_imp_acq += diff_imp_acq; - fdiff_iva_acq += diff_iva_acq; - fdiffinc_imp_acq += diffinc_imp_acq; - fdiffinc_iva_acq += diffinc_iva_acq; - } - detrazioni+= (_plm->get_real("R3")); - - // detrazioni solo non in regime agricolo - if (!attivita_agricola) - { - const real r3 = _plm->get_real("R3"); - risultato -= r3; - res_cred += r3; - } - /* totalizza importi 74 ter */ - if (_isviaggio) - { - // somma totali per calcolo successivo - c_iCEE += _plm->get_real("R5"); - c_eCEE += _plm->get_real("R6"); - a_iCEE += _plm->get_real("R7"); - a_eCEE += _plm->get_real("R8"); - c_mCEE += _plm->get_real("R9"); - a_mCEE += _plm->get_real("R10"); - a_meCEE += _plm->get_real("R11"); - } - } // endif per considerare solo PLM del mese 13 - - // Le seguenti cose (versamenti, rettifiche e rimborsi) - // vanno considerate per tutti i mesi (tabella lim) - - /* se annuale, somma versamenti mesi 1-12 - * solo una volta (non per tutte le attivita') - */ - if (attc == 0 && month == 13) - { - // l'acconto a dicembre si conteggia a parte solo - // nei casi previsti e non in annuale (vedi sotto) - real vs(versamenti_IVA(m , m == 12 ? "1" : "1|7")); - real vi(versamenti_IVA(m,"5")); - versamenti += vs; - vers_int += vi; - res_cred += vs + vi; - } - - /* - * rimborso se chiesto e previsto - * in annuale non si chiede e non va sommato se no lo cumula - */ - if (attc == 0 && is_lim && m < 13 && is_month_ok_strict(m,month)) - { - const real rimborso_richiesto = _lim->get_real("R1"); - risultato += rimborso_richiesto; - rimborsi += rimborso_richiesto; - res_debt += rimborso_richiesto; - } - - /* - * rettifiche gia' col loro bravo segno - */ - if (attc == 0 && is_lim && is_month_ok_strict(m,month)) - { - bool somma_rettifiche = FALSE; - if (year_int < 2000) - { - // se sono per benzinaro le conta solo in annuale, - // se no vanno solo in periodica - // almeno cosi' dicevano fino ad agosto - // poi, sara' il caldo, mi hanno detto che e' sbagliato - const TString& descrett = _lim->get("S0"); - const bool isforbenzinaro = descrett[0] == '$' || (descrett[0] == '>' && descrett[1] == '>'); - somma_rettifiche = !(month == 13 && !isforbenzinaro) && m < 13; - } - else - { - somma_rettifiche = month < 13 || m < 13; // CM00278 - } - if (somma_rettifiche) - { - const real rett = _lim->get_real("R5"); - risultato += rett; - rettifiche += rett; - if (rett.sign() < 0) - res_cred -= rett; - else - res_debt += rett; - } - } - - /* - * Somme varie per 13a - */ - if (attc == 0 && is_lim && m < 13 && is_month_ok_strict(m,month)) - { - const real r17 = _lim->get_real("R17"); - variazioni_imposta += r17; - risultato += r17; - if (r17.sign() >= 0) - res_debt += r17; - else - res_cred -= r17; - - const real r18 = _lim->get_real("R18"); - imposta_non_versata += r18; - risultato += r18; - res_debt += r18; - - const real r19 = _lim->get_real("R19"); - crediti_speciali += r19; - risultato -= r19; - res_cred += r19; - } - } // fine ciclo sul mese - - // counter attivita' per evitare troppi versamenti - attc++; - if (differita) - _year.format("%d", atoi(_year)+1); - - if (!(_isdifferita && is_first_month(month+deltam))) - { - if (_isviaggio) - { - // calcolo credito costo, debito mensile, perc. ripart. - perc_r = a_mCEE.is_zero() ? ZERO : (a_mCEE * CENTO)/(a_mCEE + a_meCEE); perc_r.round(2); - real ivm = (c_mCEE * perc_r)/CENTO; round_al_centesimo(ivm); - real tc = (c_iCEE + ivm); - real ta = (a_iCEE + a_mCEE); - real bi = tc - ta - credito_costo_prec(month); - - if (bi.sign() < 0) - // credito di costo - { - cred_cost = abs(bi); - round_imposta(cred_cost); - } - else - { - const real ali_via = aliquota_agvia(); - // deb_mens = (bi /((CENTO + ali_via)/CENTO)) * (ali_via/CENTO); - deb_mens = bi * ali_via / (ali_via+CENTO); - round_al_centesimo(deb_mens); // ceil voluto MI3074 - risultato += deb_mens; - res_debt += deb_mens; - iva_vend += deb_mens; - } - } - } - if (attivita_agricola) - { - real perc; // percentuale di deducibilita' - real iva_ded; // iva deducibile - - perc = imp_agr2.is_zero() ? ZERO : imp_agr2 / (imp_agr1 + imp_agr2); - perc.round(2); - iva_ded = acq_noCEE * perc; - round_al_centesimo(iva_ded); - iva_acq += iva_ded; - res_debt += iva_vend; - res_cred += iva_acq; - risultato += (iva_vend - iva_acq); - } // __ end of agricolo - } // end ciclo su attivita' - - real credito_utilizzabile = ZERO; - - if (month < 13) // va bene anche se differita sommando deltam (che e' 0 normalmente) - { - // toglie credito precedente - cred_prec = credito_prec(month+deltam); - risultato -= cred_prec; - res_cred += cred_prec; - // vedi se c'era un debito precedente per debiti < 50.000 - debt_precd = debt_prec(month+deltam); - risultato += debt_precd; - res_debt += debt_precd; - - if (new_age_2000 && credito_utilizzabile_inizio_anno > ZERO && - (_freqviva == "M" || is_trim(month+deltam))) - { - real debiti = res_debt; round_imposta(debiti); - real crediti = res_cred; round_imposta(crediti); - const real risul = debiti - crediti; - if (risul > ZERO) - { - credito_utilizzabile = credito_utilizzabile_inizio_anno; - credito_utilizzabile -= credito_utilizzato(month+deltam, TRUE, TRUE); - if (credito_utilizzabile < ZERO) credito_utilizzabile = ZERO; - credito_utilizzato_iva = min(risul, credito_utilizzabile); - res_cred += credito_utilizzato_iva; - risultato -= credito_utilizzato_iva; - } - } - } - else - { - if (new_age_2000) - { - cred_prec = ZERO; - if (credito_utilizzabile_inizio_anno > ZERO) - { - credito_utilizzato_iva = credito_utilizzato(13, TRUE, FALSE); // CM00285 - res_cred += credito_utilizzato_iva; - risultato -= credito_utilizzato_iva; - - // Calcola credito utilizzabile da riportare nell'anno successivo - credito_utilizzabile = credito_utilizzabile_inizio_anno; - credito_utilizzabile -= credito_utilizzato(13, TRUE, TRUE); - if (credito_utilizzabile < ZERO) credito_utilizzabile = ZERO; - } - } - else - { - // per l'annuale considera solo il credito a inizio anno - cred_prec = credito_prec(_freqviva == "M" ? 1 : 3); - cred_trasf = _lia->get_real("R15"); - const real cred_res = cred_prec - cred_trasf; - risultato -= cred_res; - res_cred += cred_res; - } - } - - if (month == 13) - { - // totalizza volumi affari e calcola nuovo prorata - // per tutte le attivita' - - atts.restart(); - while ((tmpatt = atts.get()) != NULL) - { - const TString8 att(tmpatt); - const int tipoatt = att.right(1)[0] - '0'; - if (tipoatt == 1) // su PLA l'attivita' e' sempre 1 - { - if (!look_pla(att)) - continue; - - real vf1 = _pla->get_real("R14"); - real vf2(_pla->get("S1")); - real iaq = _pla->get_real("R11"); // IVA acquisti - real ppg = _pla->get_real("R12"); // pro-rata pagato - - vol_aff_1 += vf1; - vol_aff_2 += vf2; - vol_aff_t = vf1 + vf2; - vol_aff_l += _pla->get_real("R0"); // volume affari lordo - - real es_c1 = _pla->get_real("R1"); - real es_c2 = _pla->get_real("R2"); - real es_c3 = _pla->get_real("R3"); - real es_c1a = _pla->get_real("R15"); - real es_c3_am = _pla->get_real("R16"); - real es_c1a_am = _pla->get_real("R17"); - real csamm = _pla->get_real("R4"); - - real prorata = 0.0; - real conguaglio = 0.0; - real topay = 0.0; - real ris = vol_aff_t; // gia' esclusi: NS, B3, cess. amm - - // calcola nuovo prorata per ogni attivita' (miste: 1+2) - if (year_int > 1997) // Calcolo dal 1998 in poi - { - const real rsa = ris - (es_c1a-es_c1a_am) - (es_c3-es_c3_am); - const real rsn = rsa - es_c1; - if (!rsa.is_zero()) - prorata = CENTO - (rsn * CENTO / rsa); // Percentuale di indetraibilita: reciproco della percentuale di detraibilita' - } - else - { - if (!ris.is_zero()) - prorata = es_c1 * CENTO / ris; - } - - prorata.round(0); - - // calcolo conguaglio -- se positivo e' a debito - if (prorata > ZERO) - { - topay = (iaq + ppg) * prorata / CENTO; - round_imposta(topay); - } - else - prorata = ZERO; - - conguaglio = topay - ppg; - round_imposta(conguaglio); - - _pla->put("R9", conguaglio); - _pla->put("R10",prorata); - - // Prorata delle mie brame... - // Chi e' il piu' sfatto del reame? - // Non va considerata la differenza tra topay e pro-rata pagato - // ma semplicemente topay. (Vedi cg4304.cpp in set_annual()) - //tot_cong += conguaglio; - tot_cong += topay; - _pla->rewrite(); - - // scrivi nuovo prorata in tabella anno successivo - const TString4 yr = _year; - _year.format("%d", atoi(_year) + 1); - look_pla(att, TRUE); - _pla->put("R8", prorata); - _pla->rewrite(); - if (look_lia(0l, TRUE)) - { - if (atoi(_year) >= 2000) - { - real cc = cred_cost; - round_imposta(cc); - _lia->put("R5", cc); - } - else - _lia->put("R5", cred_cost); - _lia->rewrite(); - } - _year = yr; - - // Aggiusta DiffIncIVAAcq sul record della 13ma che non poteva ancora sapere il proata definitivo! - if (!prorata.is_zero() && look_plm(13, att)) - { - const real old_r33 = _plm->get("R33"); - real new_r33 = old_r33 * prorata / CENTO; - round_imposta(new_r33); - _plm->put("R33", new_r33); - _plm->rewrite(); - - fdiffinc_iva_acq = fdiffinc_iva_acq - old_r33 + new_r33; - } - } - } - - look_lia(); - _lia->put("R1", vol_aff_l); - _lia->put("R2", vol_aff_1); - _lia->put("R3", vol_aff_2); - _lia->rewrite(); - } - - // comprende anche il conguaglio prorata - risultato += tot_cong; - if (atoi(_year) <= 1997) - { - if (tot_cong.sign() > 0) res_debt += tot_cong; - if (tot_cong.sign() < 0) res_cred -= tot_cong; - } - else // Nuovo prorata dal 1998, congloba il conguaglio pro-rata nell'IVA ammessa in detrazione - { - if (tot_cong.sign() > 0) - { - iva_acq -= tot_cong; - res_cred -= tot_cong; - } - else - { - iva_acq += abs(tot_cong); - res_cred += abs(tot_cong); - } - } - - look_lim(month+deltam,TRUE); - // azzeriamo tutto (tranne r1, r5, s1, s0, s7) - const TString codtab = _lim->get("CODTAB"); - - real rimborso = _lim->get("R1"); - real r5 = _lim->get("R5"); // Rettifiche - real credito_utilizzato_f24 = _lim->get("R16"); - real r17 = _lim->get("R17"); - real r18 = _lim->get("R18"); - real r19 = _lim->get("R19"); - TString s7 = _lim->get("S7"); - const TString s0 = _lim->get("S0"); // Descrizioni rettifiche - const TString s1 = _lim->get("S1"); - const TString s2 = _lim->get("S2"); - const TString s3 = _lim->get("S3"); - bool wasb0 = _lim->get_bool("B0"); - bool wasb1 = _lim->get_bool("B1"); - - _lim->zero(); - _lim->put("CODTAB", codtab); - _lim->put("R1", rimborso); - _lim->put("R5", r5); // Rettifiche - _lim->put("R16", credito_utilizzato_f24); - _lim->put("R17", r17); - _lim->put("R18", r18); - _lim->put("R19", r19); - _lim->put("S0", s0); - _lim->put("S1", s1); - _lim->put("S2", s2); - _lim->put("S3", s3); - _lim->put("S7", s7); - _lim->put("B0", wasb0 ? "X" : ""); - _lim->put("B1", wasb1 ? "X" : ""); - - - /* - * versamenti effettuati: si conteggiano in R0, - * sono > 0 solo se andavano calcolati (vedi sopra) - */ - risultato -= versamenti + vers_int; - /* - * acconto dicembre se previsto - */ - if (((month+deltam) >= 12 && _freqviva == "M") || // solo freq. M puo' essere differita - (month == 12 && _isbenzinaro && _gest4) || - (month >= (atoi(_year) >= 2000 ? 12 : 13) && _freqviva == "T")) - { - acc_dec = versamenti_IVA(12,"7"); - risultato -= acc_dec; - res_cred += acc_dec; - } - - // in annuale si arrotondera' a 1000 in stampa - // se no il conguaglio prorata fa casino - round_al_centesimo(risultato); - - _lim->put("R0",risultato); - _lim->put("R2",cred_cost); - _lim->put("R3",deb_mens); - _lim->put("R4",perc_r); - - if (almeno_una_normale) - _lim->put("R6",detrazioni); - else // per evitare sbagli nei ricalcoli esterni - _lim->put("R6",""); - - if (month == 13 || differita) - { - // Arrotonda totali in quanto possono avere un arrotondamento diverso dal mensile - TIva_round ir; - ir.round(rimborsi); - ir.round(rettifiche); - ir.round(variazioni_imposta); - ir.round(imposta_non_versata); - ir.round(crediti_speciali); - - // scrivi totali rettifiche e rimborsi - // nella finale oppure se copiati da - // anno precedente - _lim->put("R1",rimborsi); - _lim->put("R5",rettifiche); - - _lim->put("R17", variazioni_imposta); - _lim->put("R18", imposta_non_versata); - _lim->put("R19", crediti_speciali); - } - - /* - * Interessi dovuti solo da trimestrali in periodica, - * Era anche non benzinari, smentito da Cinzia (MI0853); - * Ora gestito esplicitamente nei parametri ditta, forzato - * FALSE per ditte mensili e liq. annuale - * Ulteriormente smentito e rettificato - */ - bool true_trim = (month == 3 || month == 6 || month == 9); - if (_isintr && ( true_trim || (month == 12 && _isbenzinaro && _gest4) - || (month == 13 && !(_isbenzinaro && _gest4)) )) - { - const real interesse = interesse_trimestrale(_month); - const real r = risultato - imposta_non_versata; // CM 26-09-2000 - real ivi = r.sign() > 0 ? (r * interesse / CENTO) : ZERO; - round_al_centesimo(ivi); - _lim->put("R14", ivi); - } - - //questo serve anche per la visualizzazione e per l'estrazione deleghe - if (_isintr && ( true_trim || (month == 12 && _isbenzinaro && _gest4) - || (month == 13 && !(_isbenzinaro && _gest4)) )) - _lim->put("R10",interesse_trimestrale(_month)); - - _lim->put("R7", tot_cong); // totale conguaglio su tutte le attivita' - _lim->put("R8", versamenti); - _lim->put("R9", vers_int); - _lim->put("R11", acc_dec); - _lim->put("R12", res_cred); - _lim->put("R13", res_debt); - - _lim->put("R26", fdiff_imp); // Imponibile IVA diff Fatture di vendita - _lim->put("R27", fdiff_iva); // Imposta IVA diff Fatture di vendita - _lim->put("R28", fdiffinc_imp); // Imponibile IVA diff Incassi - _lim->put("R29", fdiffinc_iva); // Imposta IVA diff Incassi - - _lim->put("R30", fdiff_imp_acq); // Imponibile IVA diff Fatture di acquisto - _lim->put("R31", fdiff_iva_acq); // Imposta IVA diff Fatture di acquisto - _lim->put("R32", fdiffinc_imp_acq); // Imponibile IVA diff Pagamenti - _lim->put("R33", fdiffinc_iva_acq); // Imposta IVA diff Pagamenti - - _lam->put("R0", iva_vend); - _lam->put("R1", iva_acq); - _lam->put("R2", cred_prec); - _lam->put("R3", debt_precd); - - if (!_recalc_only && _recalc != never) - { - _lim->put("B0","X"); - // Dal 1998 questo flag vale solo per i progressivi dei registri IVA, visto che sono separati - // pertanto il suo settaggio andra' fatto solo in fase di ricalcolo (_recalc_only) - if (atoi(_year) < 1998) - _lim->put("B1","X"); - } - - if (_recalc_only) - _lim->put("B1", "X"); - -/* Cazzata galattica: vedi sopra - if (wasdifferita) _isdifferita = TRUE; -*/ - - // AWFUL! se siamo in annuale, occorre arrotondare alle 1000 lire - // schiaffo tutto il codice nell'IF che segue, e se lo si vuole togliere - // non si fara' che scancellare tutto il blocco - const bool after2000 = atoi(_year) >= 2000; // CM000222 - if (month == 13 || after2000) - { - // molto codice duplicato, ma ne val la pena (vedi sopra) - round_imposta(iva_vend); - round_imposta(iva_acq); - round_imposta(detrazioni); - round_imposta(versamenti); - round_imposta(vers_int); - round_imposta(rimborsi); - round_imposta(rettifiche); - round_imposta(deb_mens); - round_imposta(cred_prec); - round_imposta(cred_trasf); - round_imposta(tot_cong); - round_imposta(acc_dec); - round_imposta(debt_precd); - round_imposta(variazioni_imposta); - round_imposta(imposta_non_versata); - round_imposta(credito_utilizzato_iva); - round_imposta(crediti_speciali); - round_imposta(fdiff_iva); - round_imposta(fdiffinc_iva); - round_imposta(fdiff_iva_acq); - round_imposta(fdiffinc_iva_acq); - - res_cred = iva_acq + versamenti + vers_int + cred_prec - cred_trasf + acc_dec + credito_utilizzato_iva + crediti_speciali + fdiffinc_iva_acq; - res_debt = iva_vend + rimborsi + debt_precd + imposta_non_versata + fdiffinc_iva; - - if (atoi(_year) < 1998) // Non vale piu' dal 1998 in poi; iva_acq e' gia' ok e cosi' res_cred. - { - if (tot_cong.sign() > 0) - res_debt += tot_cong; - else - res_cred -= tot_cong; - } - - if (rettifiche.sign() >= 0) - res_debt += rettifiche; - else - res_cred -= rettifiche; - - if (variazioni_imposta.sign() >= 0) - res_debt += variazioni_imposta; - else - res_cred -= variazioni_imposta; - - if (almeno_una_normale) - res_cred += detrazioni; - - risultato = res_debt - res_cred; - - // tut ricalcule', riscrivem - _lam->put("R0", iva_vend); - _lam->put("R1", iva_acq); - _lam->put("R2", cred_prec); - _lam->put("R3", debt_precd); - _lam->put("R4", cred_trasf); - - _lim->put("R0", risultato); - _lim->put("R1", rimborsi); - _lim->put("R2", cred_cost); - _lim->put("R3", deb_mens); - _lim->put("R5", rettifiche); - _lim->put("R6", detrazioni); - _lim->put("R7", tot_cong); - _lim->put("R8", versamenti); - _lim->put("R9", vers_int); - _lim->put("R11", acc_dec); - _lim->put("R12", res_cred); - _lim->put("R13", res_debt); - if (after2000) // Anche R14 - { - real ivi = _lim->get("R14"); - round_imposta(ivi); - _lim->put("R14", ivi); - } - _lim->put("R15", credito_utilizzato_iva); - - if (month == 13) - { - // Riporta eventuale credito nella tabella parametri liquidazione dell'anno successivo - const real riporto = credito_utilizzabile - risultato; - if (riporto >= ZERO) // Risultato a credito - { - const int anno_prossimo = atoi(_year) + 1; - if (look_lia(0l, TRUE, anno_prossimo)) - { - _lia->put("R0", riporto); - _lia->rewrite(); - } - } - } - _lim->put("R26", fdiff_imp); - _lim->put("R27", fdiff_iva); - _lim->put("R28", fdiffinc_imp); - _lim->put("R29", fdiffinc_iva); - _lim->put("R30", fdiff_imp_acq); - _lim->put("R31", fdiff_iva_acq); - _lim->put("R32", fdiffinc_imp_acq); - _lim->put("R33", fdiffinc_iva_acq); - // ciapa - } - - _lim->rewrite(); - _lam->rewrite(); -} - -void TLiquidazione_app::recalc_annual(const char* att) -{ - // viene chiamata 2 volte per le att. miste; PLA e' stata - // azzerata dove serve da update_firm se siamo all'annuale - real es_c1 = 0.0; - real es_c2 = 0.0; - real es_c3 = 0.0; - real es_c1a = 0.0; - real es_c3_am = 0.0; - real es_c1a_am= 0.0; - real cess_amm = 0.0; - real vendite = 0.0; - real pro_pag = 0.0; - real iva_acq = 0.0; - real ven_lrd = 0.0; - real volaff1 = 0.0; - real volaff2 = 0.0; - real imp_ifs = 0.0; - real imp_af = 0.0; - real imp_va7 = 0.0; - TString16 codiva,reg,tiva; - TToken_string va7("",'!'); - - const TString8 aaa(att); - const int tipoatt = aaa.right(1)[0] - '0'; - CHECKD(tipoatt > 0, "Tipo att non valido ", tipoatt); - - look_pla(aaa); - volaff1 = _pla->get_real("R14"); - volaff2 = (const char*)_pla->get("S1"); - vendite = _pla->get_real("R0"); - es_c1 = _pla->get_real("R1"); - es_c2 = _pla->get_real("R2"); - es_c3 = _pla->get_real("R3"); - es_c1a = _pla->get_real("R15"); - es_c3_am = _pla->get_real("R16"); - es_c1a_am= _pla->get_real("R17"); - cess_amm = _pla->get_real("R4"); - pro_pag = _pla->get_real("R12"); - iva_acq = _pla->get_real("R11"); - - // Per avere un risultato corretto, si deve totalizzare PIM->R3 per codice iva - // e quindi effettuare lo scorporo dell'imponibile e aggiungere al relativo volume d'affari - TAssoc_array corr_ann; - _CorrItem *cc; - bool is_key; - - for (_pim->first(); !_pim->eof(); _pim->next()) - { - if (_year != *_pim_anno || _month != atoi(*_pim_mese)) // In questo caso _month vale sempre 13 - continue; - - int tipocr = atoi(*_pim_tipocr); - codiva = *_pim_codiva; - reg = *_pim_codreg; - look_iva(codiva); look_reg(reg); - tiva = _iva->get("S1"); - const bool b3 = _iva->get("S2") == "B3" && atoi(_year) < 1998; - tiporeg treg = (tiporeg)_reg->get_long("I0"); - const bool corrisp = _reg->get_bool("B0"); - const tipo_sospensione simp = _reg->get_bool("B1") ? (tipo_sospensione) _reg->get_int("I9") : nessuna; - imp_ifs = _pim->get_real("R0"); - imp_af = _pim->get_real("R7"); - va7 = _pim->get("S0"); - imp_va7 = va7.get(0); - imp_ifs -= imp_af + imp_va7; // Toglie le autofatture e le op. VA7 - - if ( // ESCLUSI: - strcmp(att,*_pim_codatt) != 0 || - treg != vendita || // non vendite - tipocr == 4 || // cessioni beni ammortizzabili - tiva == "NS" || // non soggetti - b3 || // bi tre - simp == normale || - simp == liquidazione) // sospensione di imposta - continue; - - if (corrisp) - { - imp_ifs += _pim->get_real("R5"); // Se corrisp. aggiunge le FS - is_key = corr_ann.is_key(codiva); - if (!is_key) // Aggiunge se non esiste - corr_ann.add(codiva, (_CorrItem*) new _CorrItem,is_key); - _CorrItem& ca = (_CorrItem&) corr_ann[codiva]; - ca._totale += _pim->get_real("R3"); - if (!is_key) // se non c'e' lo aggiunge - ca._aliquota = _iva->get_real("R0")/CENTO; // Se è nuovo setta l'aliquota - } - if (tipoatt == 1) - volaff1 += imp_ifs; - else - volaff2 += imp_ifs; - } - - // Ora si scorporano i corrispettivi raggruppati per codice IVA e si totalizzano gli imponibili - // calcolati, da aggiungere poi a volaff1 o volaff2 - real imp,iva; - for (cc = (_CorrItem *)corr_ann.first_item(); cc != NULL; cc = (_CorrItem *)corr_ann.succ_item()) - { - lordo2netto(cc->_totale,imp,iva,cc->_aliquota); - if (tipoatt == 1) - volaff1 += imp; - else - volaff2 += imp; - } - - // calcola esenti, cessioni, lordo vendite, prorata pagato - // e IVA acquisti dai plm/ptm/pum/pam/pom - - for (int i = 1; i <= 12; i++) - { - if (!look_plm(i,aaa)) - continue; - - pro_pag += _plm->get_real("R2"); // Can't do anyway else... - } - - // Modifiche per PIM13 - if (look_plm(13,aaa)) - { - vendite += _pam->get_real("R1"); - iva_acq += _plm->get_real("R1"); - es_c1 += _pum->get_real("R4"); - es_c2 += _pum->get_real("R5"); - es_c3 += _pum->get_real("R6"); - es_c1a += _pum->get_real("R13"); - es_c3_am += _pum->get_real("S4"); - es_c1a_am += _pum->get_real("S5"); - cess_amm += _pum->get_real("R0"); - - // Nota: - // l'iva acquisti del mese 13 memorizzata in PLM->R1, e' si' la sommatoria - // di tutte le imposte relative agli acquisti di tutti i mesi, ma andrebbe sottratto il - // totale prorata pagato (pro_pag), reperibile dai mesi precedenti (PLM->R2) - //real iv = _plm->get_real("R1"); // Iva acquisti annuale - //iv -= pro_pag; - iva_acq -= pro_pag; // Si sottraggano i prorata pagati - //_plm->put("R1",iva_acq); // Si riscriva il tutto - //_plm->rewrite(); - } - - _pla->put("R0", vendite); - _pla->put("R1", es_c1); - _pla->put("R2", es_c2); - _pla->put("R3", es_c3); - _pla->put("R15", es_c1a); - _pla->put("R16", es_c3_am); - _pla->put("R17", es_c1a_am); - _pla->put("R4", cess_amm); - _pla->put("R11", iva_acq); - _pla->put("R12", pro_pag); - _pla->put("R14", volaff1); - _pla->put("S1", volaff2.string()); - _pla->rewrite(); -} +// ------------------------------------------------------------ +// Calcolo liquidazioni +// Part 2: calcolo +// fv 21-1-94 +// ------------------------------------------------------------ + +#include +#include +#include +#include +#include +#include + +#include "cg4300.h" +#include "cgsaldac.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// -------------------- QUI comincia l'avventura -------------------------- +// Datemi un punto di appoggio ****************** +// e mi ci appoggero' ****************** +// ----------------------------------------------------- ****************** + +bool TLiquidazione_app::recalc_all() +{ + TString msg; + msg = _printonly ? TR("Stampa") : TR("Calcolo"); + msg << TR(" liquidazione:\npreparazione archivi..."); + _prind = new TProgind(_selected.ones()*2, msg, true, true); + + for (int l = 0; l < _ditte->items(); l++) + { + if (_prind->iscancelled()) + break; + + if (_selected[(long)l]) + { + TToken_string& nomeditta = _ditte->row(l); + const long codditta = nomeditta.get_long(1); + const int save_month = _month; + + /* ------------------------------------------------------------- + * leggi parametri ditta necessari; stabilisci se annuale, + * se si' metti _month a 13 + * istanzia _freqviva, _isbenzinaro, _isannual, _isriepilogo + * -------------------------------------------------------------- + */ + + _reg_arr.destroy(); // Azzera la cache dei registri per ogni ditta. + _nditte->zero(); + _nditte->curr().put("CODDITTA",codditta); + _nditte->read(); + _freqviva = nomeditta.get(3); + const bool mens = _freqviva == "M"; + + // determina attivita' prevalente e istanzia cazzuole + // per vedere che Kazzo di liquidazione calcolare + const TString8 attprev = _nditte->curr().get("CODATTPREV"); + TString16 key; + + key.format("%ld|%s", codditta, (const char *)attprev); + + const TRectype & atts = cache().get(LF_ATTIV, key); + // istanzia benzinaro + _isbenzinaro = atts.get_bool("ART74_4"); + + // riaggiusta relazione + _nditte->read(); + + // strasemplificato: per l'annuale si sceglie annuale, altrimenti + // e' la 12ma anche per le trimestrali. + _isannual = _isriepilogo = _month == 13; + + TApplication::set_firm(codditta); + + // Imposta metodo arrotondamento imposte + TIva_round ir; + ir.set_default_iva_mode(atoi(_year), _isannual, codditta); + + _prind->addstatus(1); + + /* + * trimestrali impropriamente selezionate per mesi intermedi + * provocano il ricalcolo dei progressivi mensili ma non la stampa + */ + _canprint = is_month_ok_strict(_month) || _month == 13; + + /* -------------------------------------------------------------- + * Ricalcola i mesi necessari: annuale e' un mese unico da 1/1 al 31/12 + * -------------------------------------------------------------- + */ + + int need_refresh = false; + if (_recalc != ever) + { + int m = _month == 13 ? _month : 1; + for (; m < _month; m++) + if (is_month_ok_strict(m) && (!look_lim(m) || !_lim->get_bool("B0"))) + { + need_refresh = true; + break; + } + + if (need_refresh && yesno_box(TR("Alcuni mesi precedenti non " + "risultano ricalcolati. E' consigliabile il ricalcolo. " + "Si desidera eseguirlo?"))) + _recalc = ever; + } + int m; + for (m = 1; m <= _month; m++) // fino a 13 compreso + { + if (is_month_plain(m) || _recalc == ever) + { + if (_prind->iscancelled()) + break; + update_firm(m); + } + } + + // se ci sono altri mesi dopo l'ultimo calcolato, invalida il + // flag 'calcolato' del primo, per causare il ricalcolo dei + // successivi (evitando problemi per credito precedente) + for (m = _month+1; m <= 13; m++) if (look_lim(m)) + { + _lim->zero("B0"); + _lim->rewrite(); + break; + } + + _month = save_month; + } + } + bool canc = _prind->iscancelled(); + if (!canc) _prind->addstatus(1); + + TApplication::set_firm(__firm); + delete _prind; _prind = NULL; + + return !canc; +} + +bool TLiquidazione_app::update_firm(int month, bool recalc) +{ + // Se recalc e' TRUE considera la necessita' di ricalcolare e le + // opzioni utente. Se FALSE se ne impipa dell'utente e ricalcola + // se necessario (serve nelle chiamate ricorsive) + // Ritorna FALSE soltanto se il ricalcolo era necessario e non e' + // stato effettuato per scelta dello stronzo commercialista. + + _isdiff = ini_get_bool(CONFIG_DITTA, "cg", "GesLiqDiff"); + + if (month == 13 && recalc) + { + TTable pem("PEM"); + for (int reg = 0; reg < 23; reg++) + { + look_pem(pem, reg); + pem.zero("R0"); + pem.zero("R1"); + pem.rewrite(); + } + } + + look_lim(liq_month(month), TRUE); + _isdiffacc = is_differita(); // Gestione differimento acconto IVA + _isdifferita = _isdiffacc; + + look_lia(); + _isintr = _freqviva == "T" && !_lia->get_bool("B3"); // was CNF_DITTA->InTrtr + _gest4 = _isbenzinaro && _lia->get_bool("B4"); // was CNF_DITTA->Gest74 + + const bool has_single_activity = count_activities() == 1; + + // controlla che il periodo corrente non sia l'inizio dell'attivita' + // nel caso, differita va a FALSE + _monthinatt = 1; + const TDate inatt = _nditte->curr().get(NDT_DINIZIOATT); + if (is_in_liq_period(inatt)) + _isdifferita = false; + if (inatt.year() == atoi(_year)) + _monthinatt = inatt.month(); + + _isricacq = (month == 13) && ini_get_bool(CONFIG_STUDIO, "cg", "RicAcq"); + + // ricalcolo normale + // lim c'e' solo per i trimestri + bool ok = _lim->get_bool("B0"); + if (ok && !recalc) + return true; + if (_recalc_regis) + ok = false; // Se sta ricalcolando i PRM/PRP se ne sbatte delle LIM + bool calc = (_recalc == ever || (_recalc == one && is_month_ok(month,_month))); + if (!calc && _recalc != never) calc = !ok; + bool gheravergot = false; + bool quater = false; + + TToken_string atts; + TToken_string cattivs; + + // casini per stampa minchie in coda ai registri + bool riepliq = _isregis ? _riepilogo : FALSE; + bool stliq = TRUE; + + _nditte->save_status(); + if (_nditte->is_first_match(LF_ATTIV)) + { + const TRectype& ndt_att = _nditte->curr(LF_ATTIV); + do + { + const TString8 codatt = ndt_att.get("CODATT"); + const TString80 desatt = ndt_att.get("DESCR"); + quater = _nditte->curr().get_bool("FLIVA11Q"); + + // attività mista: ce ne sono in realtà due + // viene calcolato nel ciclo su tipoatt (che viene ripetuto solo se diventa TRUE); + _mixed = false; + + if (_prind) + { + TString buf(80); + buf.format( _printonly ? FR("Stampa liquidazione (mese %d %d)...\n%s:\n%s\n") : + FR("Calcolo liquidazione (mese %d %d)...\n%s:\n%s\n"), + month, atoi(_year), (const char*)_nditte_r->get(NDT_RAGSOC), (const char*)desatt); + _prind->set_text(buf); + } + + // se ricalcola l'annuale si tiene tutte le vendite e corrispettivi di + // tutti i lerci mesi. Analogamente se sta calcolando una trimestrale. + // Nel caso di trimestrale con ricalcolo trimestri precedenti azzera + // ad ogni inizio di trimestre. + if (!(_freqviva == "T" && (month != 1 && month != 4 && month != 7 && month != 10 && month != 13))) + { + _vend_arr.destroy(); + _corr_arr.destroy(); + } + + cattivs = ""; + + for (int tipoatt = 1; tipoatt <= (_mixed ? 2 : 1); tipoatt++) + { + TString8 cattiv(codatt); + cattiv << tipoatt; + + const bool waspla = look_pla(cattiv, has_single_activity); + if (!waspla) + break; // Non calcolare attività inesistenti 29-11-2012 + + if (tipoatt == 1 && waspla) + { + _p8 = _pla->get_real("R5"); + _p8b = _pla->get_real("R6"); + _p9 = _pla->get_real("R7"); + _isplafond = !(_p8.is_zero() && _p8b.is_zero() && _p9.is_zero()); + const char s7 = _pla->get_char("S7"); + _isservizio = (s7 == 'S'); + _mixed = (s7 == 'M') || (s7 == 'E'); + + const TString4 yr(_year); + TAssoc_array& pa = _prorata.perc_array(); + + pa.destroy(); + pa.add("DEF", _pla->get_real("R8")); // Percentuale di default, anno corrente + + const int anno = atoi(_year) - 2; + + for (int i = atoi(_year); i>=anno; i--) + { + _year.format("%d", i); + if (look_pla(cattiv, false)) // Reperisce percentuale prorata anno indicato (se esiste la tabella) + pa.add(_year, _pla->get_real("R8")); + } + _year = yr; // Risetta l'anno corretto e riposiziona la tabella... + look_pla(cattiv, false); + } + else + _isservizio = ndt_att.get("TIPOATT") == "S"; + + _isviaggio = ndt_att.get_bool("REG74TER"); + _isagricolo = ndt_att.get_bool("REGAGR"); + _isagr98 = _isagricolo && atoi(_year) >= 1998; + _isvent = false; + + if (!_recalc_regis && tipoatt == 1 && waspla && month == 13) + { + // azzera pla dove serve + _pla->zero("R0"); + _pla->zero("R1"); + _pla->zero("R2"); + _pla->zero("R3"); + _pla->zero("R4"); + _pla->zero("R13"); + _pla->zero("R9"); + _pla->zero("R10"); + _pla->zero("R11"); + _pla->zero("R12"); + _pla->zero("R14"); + _pla->zero("R15"); + _pla->zero("R16"); + _pla->zero("R17"); + _pla->put("S1","0"); + _pla->put("S2","0"); + _pla->put("S3","0"); + _pla->rewrite(); + } + + for (_reg->first(); _reg->good(); _reg->next()) + { + if (codatt == _reg->get("S8")) + { + if (_reg->get_int("I0") == 2) + if (!_isvent) _isvent = _reg->get_bool("B3"); + } + } + + if (!_isregis) + stliq = riepliq = true; + + if (_comp_acconto) + stliq = riepliq = false; + + if (calc || !recalc) + if (!update_att(month, cattiv) && stliq) + describe_error(TR("Attività non ricalcolate: possibili errori"), + codatt); + if (_recalc_regis) + continue; + + if (month == 13) + recalc_annual(cattiv); + + // vediamo se c'e' qualcosa da raccontare + gheravergot = _isannual || _isriepilogo; + if (!gheravergot) + { + look_plm(month,cattiv); + gheravergot = !_plm->get_real("R0").is_zero() || !_plm->get_real("R1").is_zero(); + } + + if (month == _month && riepliq) + describe_att(month, cattiv, gheravergot, '0' + (_mixed ? tipoatt : 0)); + + atts.add(cattiv); + cattivs.add(cattiv); + } // for tipoatt + + if (_recalc_regis) + continue; + + // se attività mista stampa riepilogo + if (_mixed && month == _month && riepliq) + describe_att(month,cattivs, TRUE, 'M'); + + // se sta calcolando l'annuale, scrive tutti i dati calcolati per il trasferimento + // sui files adibiti a mantenere le informazioni per IVA 11: tab1100a e tab1100b + if (_is_interactive && month == 13) + iva11_set_arr_phase_2(codatt); // scorre tutti i maledetti PIM di questa ditta/attivita' per completare _iva11_arr + } + while (_nditte->next_match(LF_ATTIV)); + + if (_recalc_regis) + { + _nditte->restore_status(); + // Se trattasi di ricalcolo per registri, setta il flag relativo, onde evitare ricalcoli + // nel caso di registro riepilogativo: se i PRM esistono già (perchè creati + // da stampa bollato precedente o altro riepilogo) non vanno ricalcolati. + look_lim(month, TRUE); // Crea se non esiste + _lim->put("B1","X"); + _lim->rewrite(); + return TRUE; + } + + // se quater stampa riepilogo + if (quater && month == _month && riepliq) + describe_att(month,atts, TRUE, 'Q'); + + // occorre poterla chiamare altre volte con mesi diversi + _nditte->restore_status(); + + // aggiorna le liquidazioni + //13/12/1995. + //Concordato con Ferdinando che, visto che qualche + //calcoluccio viene fatto anche in write_liq, essa + //verra' sempre richiamata. + //if (calc || !recalc) + //fine + + // Se siamo in annuale e se non e' stato indicato di ricalcolare (stampa liquidazione chiamata + // dalla stampa registri) non riscrivere i risultati delle liquidazioni, + if ((is_month_ok_strict(month) && _month != 13 && _recalc != never) || _is_interactive /*month == 13*/ ) + write_liq(month, atts); + + // occorre poterla chiamare altre volte con mesi diversi + _nditte->restore_status(); + + // rimborso infraannuale + _DescrItem* rimb_d = NULL; + if (month == _month) + { + // unica nel suo genere, recalc_rimborso ritorna + // un bel descritem (NULL se non si rimborsa un cas) + // da passare a describe_liq + rimb_d = recalc_rimborso(month,atts, stliq); + if (stliq) + { + describe_firm(month); + //describe_liq(_month, atts, _isregis ? NULL : rimb_d); + describe_liq(_month, atts, rimb_d); + } + if (_month == 13 && look_lim(_month)) // scrive i risultati solo in annuale + { + real r0 = _lim->get_real("R0"); + iva11_write(rimb_d == NULL || r0.sign() >= 0); // scrive su tab1100 at last. + } + } + + //in caso di stampa registri e in caso sia + //stato calcolato l'acconto (metodo analitico) + //deve essere stampato il prospetto di liquidazione + //dell'acconto in coda al resto + if (_isregis && stliq) + { + if ((month == 12 && _freqviva == "M") || + (month == 12 && _isbenzinaro && _gest4) || + (month == 13 && _freqviva == "T" && !_isbenzinaro) || + (month == 13 && _isbenzinaro && !_gest4)) + describe_liqacc(); + } + } + return ok || calc; +} + +bool TLiquidazione_app::update_att(int month, const char* codatt, bool recalc) + // viene passato un codice attivita' con codatt+tipoatt + // vedi update_firm per il burdel dei calc e recalc + // occhecasino. Se _comp_acconto e' TRUE, i movimenti considerati + // saranno solo quelli di dicembre per data <= 20/12; + // il ricalcolo e' FORZATO da _recalc messo a one +{ + look_plm(month, codatt, true); + bool ok = _plm->get_bool("B0"); + if (_recalc_regis) + ok = FALSE; // Se sta ricalcolando i PRM/PRP se ne sbatte dei PLM + if (ok && !recalc) + return true; + bool calc = _recalc == ever || (_recalc == one && is_month_ok(month,_month)); + if (!calc && _recalc != never) + calc = !ok; + + if (calc || !recalc) + { + zero_att(month,codatt); + recalc_att(month,codatt); + if (!_recalc_regis) + { + // Ricalcolo ventilazione e ag. viaggio: + // - tutti i mesi se e' mensile + // - nei mesi 3,6,9,12,13 se trimestrale + if (_freqviva == "M" || (_freqviva == "T" && (is_trim(month) || month == 13))) + { + recalc_ventilation(month, codatt); + recalc_viaggio(month, codatt); + } + recalc_corrispettivi(month, codatt); + } + } + + return ok || calc; +} + +void TLiquidazione_app::zero_att(int month, const char* codatt) +{ + TTable *arr[4] = { _pim, _pis, _prm, _prp }; + const char* flds[] = { "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", + "R10", "R11", "R12", "R13", "R14", "R15", "R16", "R17", "R18", "R19", + "R20", "R21", "R22", "R23", "R24", "R25", "R26", "R27", "R28", "R29", + "R30", "R31", "R32", "R33", + "S0", "S1", "S2", NULL }; + + TString80 codtab; + TString16 att; + TString4 year; + int m, start, stop; + + start = 0; stop = 1; + + if (_recalc_regis) + { + // Se stiamo ricalcolando per registri(_recalc_regis) azzera PRM e PRP + start = 2; stop = 3; + } + else + if (atoi(_year) < 1998) + stop = 0; // Solo PIM + // PIM / PIS + for (int i = start; i <= stop; i++) // Ciclo per le tabelle da azzerare + { + TTable * tab = (TTable*) arr[i]; + CHECK(tab, "Invalid table element"); + for (tab->first(); !tab->eof(); tab->next()) // Ciclo per i record di tabella + { + codtab = tab->get("CODTAB"); + m = atoi(codtab.mid(13,2)); + att = codtab.mid(4,6); + year = codtab.mid(0,4); + if (m == month && att == codatt && year == _year) + { + for (int j = 0; flds[j]; j++) // Ciclo per i campi del record da azzerare (34 + 3) + tab->zero(flds[j]); + tab->rewrite(); + } + } + } + + if (!_recalc_regis) + { + if (look_plm(month, codatt)) + { + // zero PLM, POM, PAM, PUM + const TString16 codtab(_plm->get("CODTAB")); + _plm->zero(); + _pom->zero(); + _pam->zero(); + _pum->zero(); + _plm->put("CODTAB", codtab); + _pom->put("CODTAB", codtab); + _pam->put("CODTAB", codtab); + _pum->put("CODTAB", codtab); + _plm->rewrite(); + _pam->rewrite(); + _pom->rewrite(); + _pum->rewrite(); + } + + // Azzera i progressivi per il prospetto agricolo dal 1998 + if (_isagr98) + { + for (_pia->first(); !_pia->eof(); _pia->next()) + { + const int m = atoi(*_pia_mese); + const TString8 att = (const char*)*_pia_codatt; + if (m == month && att == codatt && (_year == *_pia_anno)) + { + _pia->zero("R0"); // Imponibile + _pia->zero("R1"); // Imposta + _pia->rewrite(); + } + } + } + } + if (_isplafond && month != 13) + zero_plafond(month,codatt); + + if (month != 13 && !_recalc_regis) // Aggiunto test per evitare azzeramenti in stampa registri 19-06-2013 + zero_diff(month,codatt); +} + +bool TLiquidazione_app::ivadiff_chiusa(const TRectype& mov, const TDate& fine) const +{ + const long numreg = mov.get_long(MOV_NUMREG); + + TString query; + query << "USA IVADIFF SELECT BETWEEN(DATAREGP,0," << fine.date2ansi() << ')' + << "\nFROM NUMREG=" << numreg + << "\nTO NUMREG=" << numreg; + TISAM_recordset id(query); + const TRectype& rid = id.cursor()->curr(); + + bool some_pag = false; // Ci sono pagamenti? + TImporto tot; + for (bool ok = id.move_first(); ok; ok = id.move_next()) + { + const char sez = rid.get_char("SEZIONE"); + const real imp = rid.get(RMI_IMPOSTA); + if (!imp.is_zero()) + { + if (rid.get_int(MOV_TIPOMOV) > 1) + some_pag = true; + tot += TImporto(sez, imp); + } + } + const bool chiusa = some_pag && tot.valore() < 0.01; + return chiusa; +} + +// Fattura più vecchia di un anno a ente NON pubblico +bool TLiquidazione_app::sarebbe_da_pagare(const TRectype& mov, const TDate& fine) const +{ + if (mov.get_int(MOV_TIPOMOV) != tm_fattura) + return false; + + const TDate datareg = mov.get(MOV_DATAREG); + const long giorni = fine - datareg; + if (giorni < 0) + return false; + + if (mov.get_bool(MOV_LIQDIFF)) + { + const TDate datainc = mov.get(MOV_DATAINC); + if (datainc.ok() && datainc <= fine) + return false; // Già pagata senza saldaconto + } else + if (mov.get_bool(MOV_IVAXCASSA)) + { + if (!gestione_IVAxCassa(fine) || _isviaggio) // Le agenzie viaggio non posso aderire al regime IVA per cassa + return true; // Ho superato la soglia di applicabilità dell'IVA per cassa + } + else + return true; // Regime IVA normale + + TString8 key; key.format("%c|%ld", mov.get_char(MOV_TIPO), mov.get_long(MOV_CODCF)); + const TRectype& clifo = cache().get(LF_CLIFO, key); + const int alleg = clifo.get_int(CLI_ALLEG); + + if (alleg == 7 || alleg == 8) // Ente pubblico e amministrazione controllata ... + return false; // ... possono aspettare le calende greche + + if (mov.get_bool(MOV_IVAXCASSA)) + { + if (alleg == 5 || alleg == 6 || alleg == 9) // Privati, import ed export non hanno IVA per cassa + return true; + + TString16 paiv = clifo.get(CLI_PAIV); + if (paiv.empty() && clifo.get_bool(CLI_OCCAS)) + { + const TRectype& occas = cache().get(LF_OCCAS, mov.get(MOV_OCFPI)); + paiv = occas.get(OCC_PAIV); + } + if (paiv.blank()) + return true; // In assenza di Partita IVA non c'è IVA per cassa + } + + if (ivadiff_chiusa(mov, fine)) + return false; + + if (giorni >= 365) + return true; // E' passato più di un anno! + + return false; +} + +bool TLiquidazione_app::residuo_da_liquidare(long numreg, int numrig, const TDate& last_pag, + real& importo_res, real& imponib_res, real& imposta_res) const +{ + CHECKD(numreg > 0 && numrig > 0, "Numero registrazione non valido ", numreg); + + bool found = false; + + if (!found) + { + TString query; + query << "USE " << LF_IVADIFF + << "\nFROM NUMREG=" << numreg << " NUMRIG=" << numrig + << "\nTO NUMREG=" << numreg << " NUMRIG=" << numrig; + TISAM_recordset id(query); + found = id.items() >= 2; // riga fattura ed almeno un pagamento o nota di credito + if (found) + { + TImporto importo, imponib, imposta; + for (bool ok = id.move_first(); ok; ok = id.move_next()) + { + const char sez = id.get("SEZIONE").as_string()[0]; + const tipo_movimento tipomov = (tipo_movimento)id.get(PART_TIPOMOV).as_int(); + if (tipomov <= tm_nota_credito || !last_pag.ok()) + { + importo += TImporto(sez, id.get(PAGSCA_IMPORTO).as_real()); + imponib += TImporto(sez, id.get(RMI_IMPONIBILE).as_real()); + imposta += TImporto(sez, id.get(RMI_IMPOSTA).as_real()); + } + else + { + const TDate data_pag = id.get("DATAREGP").as_date(); + if (data_pag >= last_pag) + continue; + importo += TImporto(sez, id.get(PAGSCA_IMPORTO).as_real()); + imponib += TImporto(sez, id.get(RMI_IMPONIBILE).as_real()); + imposta += TImporto(sez, id.get(RMI_IMPOSTA).as_real()); + } + } + + bool is_good = imponib.valore() > ZERO; + if (!is_good) + { + const real totdoc = cache().get(LF_MOV, numreg, MOV_TOTDOC); + is_good = totdoc > ZERO; + } + if (is_good) + { + importo_res = importo.valore(); + imponib_res = imponib.valore(); + imposta_res = imposta.valore(); + } + } + } + + if (!found) + { + TLocalisamfile rmi(LF_RMOVIVA); + rmi.put(RMI_NUMREG, numreg); + rmi.put(RMI_NUMRIG, numrig); + found = rmi.read() == NOERR; + if (found) + { + imposta_res = rmi.get_real(RMI_IMPOSTA); + imponib_res = rmi.get_real(RMI_IMPONIBILE); + importo_res = imponib_res + imposta_res; + } + } + + return found; +} + +static bool partita_chiusa_al(const TPartita& p, const TDate& d) +{ + bool c = p.chiusa(); + if (c) + { + const TImporto imp = p.calcola_saldo_al(true, d, d, d); + c = imp.is_zero(); + } + return c; +} + +void TLiquidazione_app::recalc_att(int month, const char* codatt) +// il codatt passato e' codice att + tipo att ( IN {1|2} ) +// occhecasino. Se _comp_acconto e' TRUE, i movimenti considerati +// saranno solo quelli di dicembre per data <= 20/12; +{ + const bool has_sc = has_module(SCAUT, CHK_DONGLE) && ini_get_bool(CONFIG_DITTA, "cg", "GesSal"); + real totintra = ZERO; + real nond19_imp = ZERO; + real nond19_iva = ZERO; + real ammort_det = ZERO; + real ammort_det_iva = ZERO; + real ammort_indet = ZERO; + real ammort_indet_iva = ZERO; + real ammort_6 = ZERO; + real ammort_6_iva = ZERO; + real acq_riv = ZERO; + real acq_riv_iva = ZERO; + real leasing = ZERO; + real leasing_iva = ZERO; + real cess_amm = ZERO; + real cess_amm_iva = ZERO; + real acquisti = ZERO; + real acquisti_iva = ZERO; + real vendite = ZERO; + real vendite_iva = ZERO; + real esenti_c1 = ZERO; + real esenti_c2 = ZERO; + real esenti_c3 = ZERO; + real esenti_c1a = ZERO; + real esenti_c3_bam = ZERO; // Beni ammorizzabili esenti c3 + real esenti_c1a_bam = ZERO; // Beni ammorizzabili esenti c1a + real esenti_b14 = ZERO; + + // Comunicazione dati iva annuale (dal 2003) + real cessioni_cd1_1 = ZERO; // Totale operazioni attive + real cessioni_cd1_2 = ZERO; // non imponibili + real cessioni_cd1_3 = ZERO; // esenti + real cessioni_cd1_4 = ZERO; // intra + real cessioni_cd1_5 = ZERO; // beni strumentali + real acquisti_cd2_1 = ZERO; // Totale operazioni passive + real acquisti_cd2_2 = ZERO; // non imponibili + real acquisti_cd2_3 = ZERO; // esenti + real acquisti_cd2_4 = ZERO; // intra + real acquisti_cd2_5 = ZERO; // beni strumentali + real oroargento_cd3_1 = ZERO; // imponibile acquisto oro e argento + real oroargento_cd3_2 = ZERO; // imposta acquisto oro e argento + real rottami_cd3_3 = ZERO; // imponibile acquisto rottami + real rottami_cd3_4 = ZERO; // imposta acquisto rottami + + real esni_rimb = ZERO; // ci sommo tutti esenti e ni validi per rimborso + real corr_CEE = ZERO; + real corr_noCEE = ZERO; + real acq_CEE = ZERO; + real acq_noCEE = ZERO; + real corr_misCEE = ZERO; + real acq_misCEE = ZERO; + real acq_misnoCEE = ZERO; + real agr_detIA = ZERO; + real agr_1i = ZERO; + real agr_2i = ZERO; + real agr_1 = ZERO; + real agr_2 = ZERO; + real agr_3 = ZERO; + real agr_4 = ZERO; + real agr_5 = ZERO; + real agr_6 = ZERO; + real agr_7 = ZERO; + real acq_ies = ZERO; + real acq_ies_iva = ZERO; + real ult_detr = ZERO; + real acq_pint = ZERO; + real acq_pint_iva = ZERO; + real spgen = ZERO; + real spgen_iva = ZERO; + real assp_imp = ZERO; + real assp_iva = ZERO; + real vssp_imp = ZERO; + real vssp_iva = ZERO; + real assl_imp = ZERO; + real assl_iva = ZERO; + real vssl_imp = ZERO; + real vssl_iva = ZERO; + real bdog_imp = ZERO; + real bdog_iva = ZERO; + real agr_imp = ZERO; + real agr_iva = ZERO; + + real fdiff_imp = ZERO; + real fdiff_iva = ZERO; + real fdiffinc_imp = ZERO; + real fdiffinc_iva = ZERO; + real fdiff_imp_acq = ZERO; + real fdiff_iva_acq = ZERO; + real fdiffinc_imp_acq = ZERO; + real fdiffinc_iva_acq = ZERO; + + _diff_ven_ap = ZERO; + _diff_ven_ap_iva = ZERO; + _diff_acq_ap = ZERO; + _diff_acq_ap_iva = ZERO; + + // PEM + real vt_imponibile[23]; + real vt_imposta[23]; + for (int j = 0; j < 23; j++) + { + vt_imponibile[j] = ZERO; + vt_imposta[j] = ZERO; + } + + // Nel seguente assoc, vengono memorizzati gli acquisti iva registrati quest'anno + // ma con anno documento precedente. Di conseguenza per un corretto calcolo prorata + // e' necessario distinguire gli importi, appunto, per anno. + // Questo nuovo calcolo prorata, vale solo per gli anni oltre il 1998. + TAssoc_array acq_iva_anni_prec; + + TTable *arr[4] = { _pim, _pis, _prm, _prp }; //Tabelle con la stessa struttura + + const int year_int = atoi(_year); // Basta con le atoi()... + + // Nuova selezione filtro dal 1998. Rende molto piu' pesante il calcolo, complimenti a tutti. + // Il campo MESELIQ sulla testata dei movimenti indica il mese di liquidazione in cui considerare + // il movimento stesso. Tale campo e' un intero che va da 0 a 12, dove i numeri da 1 a 12 rappresentano + // i rispettivi mesi (Gennaio...Dicembre) e lo zero significa che questo movimento va considerato + // seguendo la data di registrazione, ovvero NORMALMENTE. + // Questo significa che: + // si includano nel cursore tutti i movimenti da questo inizio mese fino all fine dell'anno + // Il primo periodo dell'anno successivo (1 o 3 mesi se Trim.) va incluso se sto calcolando dicembre o l'annuale: + // puo' esistere un doc. con DATAREG di gennaio dell'anno dopo ma MESELIQ==12; tale movimento va considerato in dicembre + // dell'anno precedente (4 trimestre nel cazo di trimestruali). + // Ovviamente un tale movimento siffatto andra' considerato allo stesso modo anche in liquidazione annuale. + // Default date per agenzie viaggio: da inizio anno fino per considerare la DATA74TER + // Esemplificazione dei cursori nei vari casi: + // month < 12 : la regione andra' dal 01/month/_year al 31/12/_year + // selezione movimenti: datareg.month() == month && meseliq == 0 || meseliq == month + // month == 12 : la regione andra' dal 01/month/_year al lastday/firstperiod/_year+1 + // selezione movimenti: datareg.month() == month && meseliq == 0 || meseliq == month + // month == 13 : la regione andra' dal 01/01/_year al lastday/firstperiod/_year+1 + // selezione movimenti: datareg.year() == _year && meseliq != 12 || datareg.year() == _year+1 && meseliq == 12 + // Attenzione: nel caso si tratti di ricalcolo chiamato dalla stampa registri bollati, il cursore + // sara' leggermente diverso: i movimenti sono solo quelli del mese, tutto l'anno se annuale (month == 13). + // I movimenti del periodo selezionato dovranno essere tutti inclusi; in particolare quelli che + // avranno il mese liquidazione != 0 dovranno essere messi nella tabella PRP, come riferimento + // a Progressivi Registri periodo Precedente. + + TRectype from(_cur->curr()); from.zero(); + TRectype to(from); + TString8 fromreg("~"), toreg("0"); + + { + TRelation regrel("REG"); + TCursor reg(®rel); + TString4 codreg; + const int items = reg.items(); + + for (reg = 0L; reg.pos() < items; ++reg) + { + codreg = reg.curr().get("CODTAB").mid(4); + + if (codreg < fromreg) + fromreg = codreg; + if (codreg > toreg) + toreg = codreg; + } + } + + //TDate f(1, month == 13 ? 1 : month, year_int); + const TDate fromdate(1, month == 13 ? 1 : month, year_int); + TDate t; + TDate f74; + + // Spiegazione dell'arcano segreto sulle agenzie viaggio: + // non viene applicato lo stesso metodo delle ditte normali perche' si deve + // tenere conto della DATA74TER se presente. + // Per le agenzie di viaggio si parte da inizio anno precedente + if (_isviaggio) + f74 = TDate(1, 1, year_int-1); + +// if (_isdiff) +// f = TDate(1, 1, year_int-5); + if (year_int < 1998 || _recalc_regis) // Vecchia selezione o calcolo progressivi per stampa registri bollati + { + t.set_month(month == 13 ? 12 : month); + t.set_year(year_int); + } + else //Nuova selezione dal 1998 in poi + { + t.set_year(month < 12 ? year_int : year_int+1); + t.set_month(month < 12 ? 12 : _freqviva == "M" ? 1 : 3); + } + t.set_end_month(); + + const TDate inizio(fromdate - 1L); + + TDate todate(1, month == 13 ? 12 : month, year_int); + todate.set_end_month(); + const TDate fine(todate); + + from.put(MOV_REG, fromreg); + to.put(MOV_REG, toreg); + + _cur->freeze(FALSE); + _cur->setregion(from, to); + + TString filter; + //filter.format("((ANSI(DATAREG)>=%ld)&&(ANSI(DATAREG)<=%ld))", fromdate.date2ansi(), t.date2ansi()); + filter.format("(BETWEEN(DATAREG,%ld,%ld))", fromdate.date2ansi(), t.date2ansi()); + if (_isviaggio) + filter << format("||(BETWEEN(DATA74TER,%ld,%ld))", f74.date2ansi(), t.date2ansi()); + if (_isdiff) + filter << "||(NUM(LIQDIFF==\"X\"))"; + if (has_sc && gestione_IVAxCassa(fromdate)) + filter << "||(NUM(IVAXCASSA==\"X\"))"; + + _cur->setfilter(filter); + const long items = _cur->items(); + _cur->freeze(); + + *_cur = 0; + + TString16 trueatt(codatt); + const int tipatt = trueatt[5] - '0'; + trueatt.cut(5); + + TString pimsg; pimsg << TR("Ricalcolo attività ") << trueatt << " (mese " << month << ')'; + TProgind pi(items, pimsg, false, true); + for (; _cur->pos() < items; ++(*_cur)) + { + if (!pi.setstatus(_cur->pos())) + break; + + TDate date = _mov->get(MOV_DATAREG); + const int liqmonth = _mov->get_int(MOV_MESELIQ); + const TString4 reg = _mov->get("REG"); + const bool isreg = look_reg(reg); + const TString4 tipodoc = _mov->get(MOV_TIPODOC); + const bool corrisp = _reg->get_bool("B0"); + const tiporeg tipomov = (tiporeg)_reg->get_int("I0"); + + tipo_movimento tm = (tipo_movimento)_mov->get_int(MOV_TIPOMOV); + if (tm == tm_nessuno) + { + if (tipodoc == "FV" || tipodoc == "FA") + tm = tm_fattura; else + if (tipodoc == "NC") + tm = tm_nota_credito; + } + + if (_mov->get_long(MOV_NUMREG) == 29103) + int cazzone = 1; + + if (_isviaggio) + { + if (tipomov == vendita && _mov->get(MOV_DATA74TER).not_empty()) + date = _mov->get_date(MOV_DATA74TER); + if (date.year() != year_int) // Controlla anno di appartenenza, altrimenti is_date_ok potrebbe non funzionare + continue; + } + const TRectype& rcs = _cur->curr(LF_CAUSALI); + const bool fattrit = rcs.get_bool(CAU_RITFATT); + const bool cau_intra = rcs.get_bool(CAU_INTRACOM); + const bool cau_valintra = rcs.get_bool(CAU_VALINTRA); + + // Inizio gestione IVA differita + const bool iva_diff = _isdiff && _mov->get_bool(MOV_LIQDIFF); + const bool iva_cass = !iva_diff && _mov->get_bool(MOV_IVAXCASSA) && tm > 0; + const bool movdiff = (iva_diff || iva_cass) && !_recalc_regis; // Aggiunto test su ricalcolo da registri 13-12-2012 + bool dok = is_date_ok(date, month, liqmonth, year_int); + + TPartite_array arrpart; // Partite interessate + TPointer_array pagscatt; // Righe di pagsca interessate + + if (movdiff && tm == tm_fattura) + { + // Sezione preferita per fatture decisa in base a vendita->'A' o acquisto->'D' + const char sezfat = tipomov == vendita ? 'D' : 'A'; + const char sezpag = tipomov == vendita ? 'A' : 'D'; + const long numreg = _mov->get_long(MOV_NUMREG); + TDate datainc; + real tot_incassato, tot_da_incassare; + int flag_pg_nc = 0; // Ci sono pagamenti (0x1) e/o note di credito(0x2) ? + bool game_found = false; + if (has_sc) + { + 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 && !ivadiff_chiusa(_mov->curr(), fine)) + { + const TRiga_partite& rp = p->riga(row); + TImporto pg, nc; + flag_pg_nc = rp.calcola_pagato_periodo(inizio+1L, fine, pg, nc, &pagscatt); + if (sarebbe_da_pagare(_mov->curr(), fine)) + { + rp.calcola_pagato_periodo(TDate(0L), fine, pg, nc, NULL); + TImporto saldo = rp.importo(false); + saldo += pg; saldo += nc; + saldo.normalize(sezfat); + if (saldo.valore() > ZERO) + tot_da_incassare = saldo.valore(); + } + } + } + if (!game_found) // No saldaconto o partita assente + { + datainc = iva_diff ? _mov->get_date(MOV_DATAINC) : TDate(); + if (datainc.ok() && datainc <= fine) + tot_incassato = _mov->get_real(MOV_TOTDOC); + else + { + // Fattura più vecchia di un anno a ente NON pubblico + if (sarebbe_da_pagare(_mov->curr(), fine)) + tot_da_incassare = _mov->get_real(MOV_TOTDOC); + } + } + // Simulo incasso se necessario + if (!tot_da_incassare.is_zero()) // già controllato se sarebbe_da_pagare(_mov->curr(), fine) + { + tot_incassato = tot_da_incassare; + datainc = fine; + _mov->put(MOV_DATAINC, datainc); + } + if (tot_incassato > ZERO && is_date_ok(datainc, month, liqmonth, year_int)) + { + flag_pg_nc = 0x1; + TRectype* pagsca = new TRectype(LF_PAGSCA); + pagsca->put(PAGSCA_TIPOC, _mov->get(MOV_TIPO)); + pagsca->put(PAGSCA_SOTTOCONTO, _mov->get(MOV_CODCF)); + pagsca->put(PAGSCA_ANNO, year_int); + pagsca->put(PAGSCA_NRIGA, 1); + pagsca->put(PAGSCA_NRATA, 1); + pagsca->put(PAGSCA_NRIGP, 9999); + pagsca->put(PAGSCA_IMPORTO, tot_incassato); + pagscatt.add(pagsca); + } + if (flag_pg_nc) + dok = true; + } + /* + * check register present, rmoviva present and date OK + * + * In un futuro prossimo, molto remoto, quando implementeremo il Client/Server, questo + * filtro sulla data registrazione/mese liq. andra' nel cursore stesso. + * Ora viene implementato qui [is_date_ok()] per non appesantire ulteriormente + * la lettura dei movimenti. + */ + + const bool sreg = !isreg; + const bool rs8 = _reg->get("S8") != trueatt; + const bool cmt = !_cur->is_first_match(LF_RMOVIVA); + + if (!dok || sreg || rs8 || cmt) + continue; + + const tipo_sospensione sosp_imp = _reg->get_bool("B1") ? (tipo_sospensione) _reg->get_int("I9") : nessuna; + + /* + * Tipo documento da eliminare (Scudler 1994) + * Trattasi per lo piu' di non incassati + * Da oggi vengono sommati nei real di S2 in pim + * assieme alle FS per stampa registri + */ + const bool noninc = (tipodoc == "SN" || tipodoc == "CN" || tipodoc == "RN" || tipodoc == "IN" || tipodoc == "PG"); + + /* + * check date: se si calcola l'acconto, solo da 1/12 a 20/12 + */ + const int accmonth = _isdifferita ? 11 : 12; + + /* + * Patrizia: se è differita i mov. vanno da 1/11 a 30/11 + * potrei allora andarmi a leggere i risultati della liq di 11 + * ma il cliente potrebbe voler calcolare l'acconto prima + * di aver calcolato la liquidazione (perche' cosi' complicato?) + */ + if (_comp_acconto && !_isdifferita && date.month() == accmonth && date.day() > 20) + continue; + + // Ciclo sulle righe iva del movimento + do + { + const int rmi_tipoatt = max(_rmoviva->get_int(RMI_TIPOATT), 1); // Poteva capitare tipoatt == 0 + if (rmi_tipoatt != tipatt) + continue; + + // totali parziali registrati nei pim per motivi ignoti + real bolld_imp = ZERO; // imponibile bolle doganali + real bolld_iva = ZERO; // imposta bolle doganali + real asimp_imp = ZERO; // imponibile acquisti sosp. imposta + real asimp_iva = ZERO; // imposta acquisti sosp. imposta + real vsimp_imp = ZERO; // imponibile vendite sosp. imposta + real vsimp_iva = ZERO; // imposta vendite sosp. imposta + real rit_imp = ZERO; // imponibile fatture in ritardo + real rit_iva = ZERO; // imposta fatture in ritardo + real nin_imp = ZERO; // imponibile non incassati + real nin_iva = ZERO; // imposta non incassati + + look_iva(_rmoviva->get(RMI_CODIVA)); + const TString4 codiva = _iva->get("CODTAB"); + const TString4 tipoiva = _iva->get("S1"); + const TString8 tipoes_v = _iva->get("S2"); + const TString8 tipoes_a = _iva->get("S9"); + const int tipoagr = _iva->get_int("S4"); + const int tipoag = _iva->get_int("S5"); + const int tipopla = _iva->get_int("S3"); + int isrimbinfr = _iva->get_bool("B3"); // vale per calcolo rimborso se ES o NI + const real perciva = _iva->get_real("R0") / CENTO; + int ivarimb = !_iva->get_bool("B4"); // non escluso calcolo rimb. per al. media + + const TString4 tipocr_s = _rmoviva->get(RMI_TIPOCR); + const int tipocr = atoi(tipocr_s); + const bool intra = _rmoviva->get_bool(RMI_INTRA); + + // autofatture art. 17 per non residenti, con trattamento + // speciale in liquidazione annuale + const bool autodafe = tipodoc == "AF"; //Adesso il tipocr==4 non c'entra piu' un tubo + // vendite art. 40 c. 5/6/8 acq.intra., con trattamento + // speciale in liquidazione annuale (VA7 in stampa) + const bool art40 = (tipodoc != "AF" && tipoiva != "NS" && !rcs.get_bool("AUTOFATT") && rcs.get_bool("VALINTRA") && !intra); + // indica acquisti per rivendita; viene messo in B4 di PIM per + // indicare che e' soggetto al ricalcolo IVA (se voluto) in modo + // da evitare di dover controllare mese e anno sul PIM + const bool was_riv = (tipoiva != "NS") && (tipocr == 1 || tipocr ==5); + real percind; + const int tipoind = get_tipodet_from_rmi(_rmoviva->curr(), _mov->curr(), percind, true); + const int decimals = TCurrency::get_firm_dec(); + + const real imponibile_orig = _rmoviva->get_real(RMI_IMPONIBILE); + const real imposta_orig = _rmoviva->get_real(RMI_IMPOSTA); + + real diff_imp; // Liquidazione differita imponibile + real diff_iva; // Liquidazione differita imposta + real incdiff_imp; // Liquidazione differita incassate imponibile + real incdiff_iva; // Liquidazione differita incassate imposta + real impon_ind; // Imponibile indetraibile + real impos_ind; // Imposta indetraibile + real impon_det; // Imponibile detraibile + real impos_det; // Imposta detraibile + + if (movdiff && 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); + id.put(PART_TIPOMOV, tm); + CHECKD(rmi_tipoatt > 0, "Invalid tipo att ", rmi_tipoatt); + id.put("TIPOATT", rmi_tipoatt); + id.put("ANNOLIQ", datareg.year()); + int mesereg = datareg.month(); + if (_freqviva[0] == 'T') + { + const int resto = mesereg % 3; + if (resto > 0) + mesereg += 3-resto; + } + id.put("MESELIQ", mesereg); + id.put("TIPOIVA", tipomov == 2 ? 2 : 1); + id.put("TIPODIFF", iva_cass ? 2 : 1); + id.put(MOV_DATAREG, _mov->get(MOV_DATADOC)); + id.put(RMI_CODIVA, _rmoviva->get(RMI_CODIVA)); + id.put("SEZIONE", sezfat); + + id.put("IMPORTO", imponibile_orig+imposta_orig); + id.put(RMI_IMPONIBILE, imponibile_orig); + + if (percind > ZERO) // Nel caso di IVA indetraibile memorizza solo la parte detraibile! + { + real imp_det, iva_det, imp_ind, iva_ind; + analizza_IVA(imponibile_orig, imposta_orig, percind, corrisp, false, codiva, + imp_det, iva_det, imp_ind, iva_ind); + id.put(RMI_IMPOSTA, iva_det); + } + else + id.put(RMI_IMPOSTA, imposta_orig); + + const int ew = id.write_rewrite(); + if (ew != NOERR) + cantwrite_box(id.description()); + } + + if (!pagscatt.empty()) + { + const char sezpag = tipomov == vendita ? 'A' : 'D'; + const real totfat = _mov->get(MOV_TOTDOC); + + TLocalisamfile id(LF_IVADIFF); + FOR_EACH_ARRAY_ITEM(pagscatt, r, obj) + { + const TRectype& pagsca = *(TRectype*)obj; + const int nrigp = pagsca.get_int(PAGSCA_NRIGP); + + id.zero(); + id.put(RMI_NUMREG, _rmoviva->get(RMI_NUMREG)); + id.put(RMI_NUMRIG, _rmoviva->get(RMI_NUMRIG)); + id.put(MOV_DATAREG, _mov->get(MOV_DATADOC)); + CHECKD(nrigp > 0, "Invalid NRIGP ", nrigp); + id.put("NUMPRO", min(nrigp, 999)); + CHECKD(rmi_tipoatt > 0, "Invalid tipo att ", rmi_tipoatt); + id.put("TIPOATT", rmi_tipoatt); + id.put("TIPOIVA", tipomov == 2 ? 2 : 1); + id.put("TIPODIFF", iva_cass ? 2 : 1); + id.put(RMI_CODIVA, _rmoviva->get(RMI_CODIVA)); + + TImporto pagtmp; + bool ultimo = false; + tipo_movimento tipomov_pag = tm_pagamento; + if (nrigp > 0 && nrigp < 9999) + { + const TPartita& p = arrpart.partita(pagsca); + const TRiga_partite& rp = p.riga(nrigp); + tipomov_pag = rp.tipo(); + id.put(PART_TIPOMOV, tipomov_pag); + id.put("NUMREGP", rp.get(PART_NREG)); + id.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); + id.put("DATAREGP", d); + pagtmp = p.importo_pagsca(pagsca); + if (tipomov_pag >= tm_pagamento && partita_chiusa_al(p, fine)) + { + ultimo = true; + for (int r = p.last(); r > nrigp; r = p.pred(r)) + { + if (p.riga(r).tipo() >= tm_pagamento) + { + ultimo = false; + break; + } + } + } + } + else + { + // Falso record generato da datainc + id.put(PART_TIPOMOV, tipomov_pag); + id.put("NUMREGP", _mov->get(MOV_NUMREG)); + id.put("NUMRIGP", nrigp); + id.put("DATAREGP", _mov->get(MOV_DATAINC)); + pagtmp.set(sezpag, pagsca.get_real(PAGSCA_IMPORTO)); + ultimo = true; + } + + id.put("ANNOLIQ", year_int); + int meseliq = id.get_date("DATAREGP").month(); + if (_freqviva[0] == 'T') + { + const int resto = meseliq % 3; + if (resto > 0) + meseliq += 3-resto; + } + id.put("MESELIQ", meseliq); + + id.put("SEZIONE", pagtmp.sezione()); + if (ultimo) + { + real importo, imponibile, imposta; + residuo_da_liquidare(id.get_long(RMI_NUMREG), id.get_int(RMI_NUMRIG), id.get_date("DATAREGP"), + importo, imponibile, imposta); + id.put(PAGSCA_IMPORTO, importo); + id.put(RMI_IMPONIBILE, imponibile); + id.put(RMI_IMPOSTA, imposta); + + incdiff_imp += imponibile; + incdiff_iva += imposta; + } + else + { + // Spostato in basso ricalcolo importo ragguagliato 24/09/2013 + // id.put(PAGSCA_IMPORTO, pagtmp.valore()); + // pagtmp.normalize(sezpag); + + const real perc = pagtmp.valore() / totfat; + real val_imp = imponibile_orig * perc; + round_imposta(val_imp); + id.put(RMI_IMPONIBILE, val_imp); + + if (tipomov_pag >= tm_pagamento) + incdiff_imp += val_imp; + + real val_iva = imposta_orig * perc; + round_imposta(val_iva); + id.put(RMI_IMPOSTA, val_iva); + if (tipomov_pag >= tm_pagamento) + incdiff_iva += val_iva; + + id.put(PAGSCA_IMPORTO, val_imp + val_iva); // Ricalcolo importo + } + + int err = id.write_rewrite(); + if (err != NOERR) + error_box("Errore %d in aggiornamento file IVADIFF", err); + + const long numreg = _rmoviva->get_long(RMI_NUMREG); + if (numreg > 0) // Chiude o riapre la partita + { + 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 chiusa = id.get_bool("CHIUSA"); + if (chiusa != ultimo) + { + id.put("CHIUSA", ultimo); + id.rewrite(); + } + } + } + } + diff_imp = imponibile_orig; + diff_iva = imposta_orig; + } + else + { + if (movdiff) + { + diff_imp = imponibile_orig; + diff_iva = imposta_orig; + } + else + analizza_IVA(imponibile_orig, imposta_orig, percind, corrisp, false, codiva, + impon_det, impos_det, impon_ind, impos_ind); + } + + for (int is_detraibile = 0; is_detraibile < 2; is_detraibile++) + { + const int tipodet = is_detraibile ? 0 : tipoind; + const real imponibile = is_detraibile ? impon_det : impon_ind; + const real imposta = is_detraibile ? impos_det : impos_ind; + + //qui da verificare + + if ((imponibile.is_zero() && imposta.is_zero()) && + (_isdiff && + (is_detraibile > 0 || + (diff_imp.is_zero() && diff_iva.is_zero() && + incdiff_imp.is_zero() && incdiff_iva.is_zero()) + ) + ) + ) + continue; + + /* + * Riga imponibile > 9 = ritenute; non si + * considerano (Scudler 1994) + */ + + // Filling dell'array per IVA11 + if (_is_interactive && month == 13) + iva11_set_arr_phase_1(trueatt); + + + // Cumula l'ammontare delle operazioni attive/passive + // per la dichiarazione annuale fati iva + if (month == 13 && !fattrit) + { + if (tipomov == vendita) + { + const long codcf = _mov->get_long(MOV_CODCF); + + if (tipoiva.empty() && codcf != 0) + { + TString8 key; key.format("%c|%ld", _mov->get_char(MOV_TIPO), codcf); + const TRectype& clifo = cache().get(LF_CLIFO, key); + int codreg = 0; // codice regione x privati 22 == partite iva 0 == senza codice + const int tipoalleg = clifo.get_int(CLI_ALLEG); + + if (tipoalleg == 6) // privato + { + if (clifo.get_bool(CLI_OCCAS)) + { + const TString80 key_occ(_mov->get(MOV_OCFPI)); + const TRectype & occas = cache().get(LF_OCCAS, key_occ); + + key = occas.get(OCC_STATO); + key << "|" << occas.get(OCC_COM); + } + else + { + key = clifo.get(CLI_STATOCF); + key << "|" << clifo.get(CLI_COMCF); + } + + const TRectype & comune = cache().get(LF_COMUNI, key); + + codreg = comune.get_int(COM_CODREG) + 1; + } + vt_imponibile[codreg] += imponibile; + vt_imposta[codreg] += imposta; + } + } + + const TString& iva_vpn = _iva->get(tipomov == vendita ? "S10" : "S11"); + if (iva_vpn.full()) + { + switch (tipomov) + { + case vendita: // CD1 - 1 2 3 4 + { + // bool is_valid = !corrisp && tipodoc != "AF" && (cau_intra || !cau_valintra) && + // sosp_imp != normale && sosp_imp != liquidazione; + bool is_valid = tipodoc != "AF" && sosp_imp != normale && sosp_imp != liquidazione; + if (is_valid) + { + if (movdiff) // 21-02-2013 Comprendere IVA differita registrata nell'anno! (imponibile=0) + { + if (date.year() == year_int) + cessioni_cd1_1 += imponibile_orig; + } + else + cessioni_cd1_1 += imponibile; + if (iva_vpn.starts_with("CD1")) + { + if (tipocr == 4) + cessioni_cd1_5 += imponibile; + } + if (!cau_intra) + { + if (iva_vpn == "CD12") + cessioni_cd1_2 += imponibile; else + if (iva_vpn == "CD13") + cessioni_cd1_3 += imponibile; + } + else + { + if (iva_vpn == "CD14") + cessioni_cd1_4 += imponibile; + } + } + + } + break; + case acquisto: // CD2 - 1 2 3 4 + { + acquisti_cd2_1 += imponibile; + if (iva_vpn.starts_with("CD2")) + { + if (tipocr == 2 || tipocr == 3 || tipocr == 8) + acquisti_cd2_5 += imponibile; + } + if (!cau_intra) + { + if (iva_vpn == "CD22") + acquisti_cd2_2 += imponibile; + else + if (iva_vpn == "CD23") + acquisti_cd2_3 += imponibile; + } + else + { + if (iva_vpn == "CD24") + acquisti_cd2_4 += imponibile; + } + if (iva_vpn == "CD31") + { + oroargento_cd3_1 += imponibile; + oroargento_cd3_2 += imposta; + } + if (iva_vpn == "CD33") + { + rottami_cd3_3 += imponibile; + rottami_cd3_4 += imposta; + } + } + break; + default: + break; + } + } + } + + if (noninc) // non incassati: non devono entrare in nessun altro calcolo + { + nin_imp = imponibile; + nin_iva = imposta; + } + else if (sosp_imp != nessuna) // sospensione di imposta + { + if (tipomov == vendita) + { + vsimp_imp = imponibile; + vsimp_iva = imposta; + } + else + { + asimp_imp = imponibile; + asimp_iva = imposta; + } + + switch (sosp_imp) + { + case normale: + assp_imp += asimp_imp; + assp_iva += asimp_iva; + vssp_imp += vsimp_imp; + vssp_iva += vsimp_iva; + break; + case liquidazione: + assl_imp += asimp_imp; + assl_iva += asimp_iva; + vssl_imp += vsimp_imp; + vssl_iva += vsimp_iva; + default: + break; + } + } + else if (fattrit) // Possono essere solo acquisti + { + rit_imp = imponibile; + rit_iva = imposta; + } + + // Le fatture in ritardo non vanno considerate nel periodo ma solo nell'annuale + if (!fattrit || (fattrit /*&& _isagricolo*/ && _month == 13)) // normale + { + + // ***************************************** + // casi particolari + // ***************************************** + + // imposta acquisti intracomunitari + if (tipomov == acquisto && intra) + totintra += imposta; + + // Altre cose di cui tener conto + // Separo pazientemente per movimenti d'acquisto e movimenti di vendita + // onde evitare rompicapi, rompicazzi ecc... + + switch (tipomov) + { + case acquisto: // ACQUISTO (per chi non l'avesse capito) + // Per le anguille delle Antille e le Saracche delle Molucche... + // Gli acquisti con tipocr 1,2,3,5,8 vanno totalizzati se e solo se + // il tipo iva e' diverso da NON SOGGETTO, + if (tipoiva != "NS") + { + if (tipocr == 1 || tipocr ==5) + // Acquisto beni per rivendita + { + acq_riv += imponibile; + acq_riv_iva += imposta; + } + else if (tipocr == 2 && tipodet == 0) + // Acquisto beni ammortizzabili detraibili + { + ammort_det += imponibile; + ammort_det_iva += imposta; + } + else if (tipocr == 2 && tipodet != 0) + // Acquisto beni ammortizzabili indetraibili + { + ammort_indet += imponibile; + ammort_indet_iva += imposta; + } + else if (tipocr == 3) + // Acquisto beni ammortizzabili detr. 6% + { + ammort_6 += imponibile; + ammort_6_iva += imposta; + // ult_detr += imponibile * real(DETRAZIONE_6PERCENTO); // CM500159 + } + else if (tipocr == 8) + // Acquisto beni strum. acquisiti in leasing + { + leasing += imponibile; + leasing_iva += imposta; + } + } // fine if per tipo iva != da NON SOGGETTO + + // Mi chiedo se effettivamente il tipo 9 sia esente da qualsiasi criterio... + if (tipocr == 9) + // Spese generali + { + spgen += imponibile; + spgen_iva += imposta; + } + // else fuck it up, dirty dick head. + break; + case vendita: // VENDITA + if (tipoiva != "NS") /* Segnalazione CM000219: I non soggetti non vanno considerati */ + { + if (tipocr == 4) + /* + * Vendite strum. art 17 (cess. amm.) + * Comprende anche autofatture, anche nell'annuale + * (Sergio 1995) + */ + { + /* NO! + * Invece non le deve comprendere nell'annuale + * (Vladimiro 1995, piu' tardi) + */ + if (!(_month == 13 && autodafe)) + { + cess_amm += imponibile; + cess_amm_iva += imposta; + } + } + } + break; + default: // Chissa'... forse in futuro vi sara' un ulteriore tipo di movimento... + break; + } // Fine del malefico switch + + + // operazioni per calcolo rimborso infraanale + // Scudler 1995 e molti dopo di lei + // La breve estate vissuta dal metodo del 51% rende + // oramai superflua la tabella rimborsi, eliminata + // senza entusiasmo ed interrogandomi sul senso della vita + if (ivarimb) + { + bool ok = _isagricolo ? tipoagr == 2 : false; + + if (tipomov == acquisto && !ok) + ok = (tipocr == 0 || tipocr == 1 || tipocr == 5 || tipocr == 9); + + if (tipomov == vendita && !ok) + ok = tipoiva != "NS" && tipocr != 4; + + ivarimb = ok; + } + + // operazioni esenti + if (tipoiva == "ES") + { + if (tipomov == vendita) + { + if (sosp_imp != normale && sosp_imp != vol_affari) // CM500308 + { + if (tipoes_v == "B1") // C1 + esenti_c1 += imponibile; + else if (tipoes_v == "B2") // C2 + esenti_c2 += imponibile; + else if (tipoes_v == "B3") // C3 + { + esenti_c3 += imponibile; + if (tipocr == 4) + esenti_c3_bam += imponibile; + } + else if (tipoes_v == "B4") // C1A + { + esenti_c1a += imponibile; + if (tipocr == 4) + esenti_c1a_bam += imponibile; + } + // se e' il caso sommare esenti per rimborso + if (isrimbinfr) + esni_rimb += imponibile; + } + } + else if (tipoes_a == "14") + esenti_b14 += imponibile; + } + + // non imponibili per rimborso + if (tipoiva == "NI" && tipomov == vendita && isrimbinfr) + esni_rimb += imponibile; + + // bolle doganali + if (tipodoc == "BD") + { + bolld_imp = imponibile; + bolld_iva = imposta; + + bdog_imp += bolld_imp; + bdog_iva += bolld_iva; + } + + // plafond + if (_isplafond && tipomov == acquisto && month != 13 && + tipoiva == "NI" && tipopla != 0) + { + add_plafond(month, codatt, tipopla, imponibile, tipodoc == "BD"); + } + + // agenzie viaggio (CHECK imponibili etc.) + if (_isviaggio) + switch (tipoag) + { + case 1: + if (tipomov == acquisto) acq_CEE += imponibile + imposta; + else corr_CEE += imponibile + imposta; + break; + case 2: + if (tipomov == acquisto) acq_noCEE += imponibile + imposta; + else corr_noCEE += imponibile + imposta; + break; + case 3: if (tipomov == vendita) + corr_misCEE += imponibile + imposta; + break; + case 4: if (tipomov == acquisto) + acq_misCEE += imponibile + imposta; + break; + case 5: if (tipomov == acquisto) + acq_misnoCEE += imponibile + imposta; + break; + } + + + if (_isagricolo) + { + /* + * rifatto sensu Scudler 1995 con invidiabile sicurezza + * senza porsi domande e mettendo NUMERI uguali + * al TIPO RECORD del frigorifero + */ + if (tipomov == vendita) + { + if (!noninc) // Salta i non incassati + { + agr_imp = imponibile; + agr_iva = imposta; + const real p = perciva; + const bool is_not_fs = tipodoc != "FS"; + + // Se si tratta di Fattura Fiscale o Fattura Scontrino, non scorpora + // In particolare le FS non vengono incluse ne' nello specchietto ne' nel totale + if (corrisp && tipodoc != "FF" && is_not_fs) + lordo2netto(agr_imp,agr_iva,p); + + if (tipoagr == 1 && is_not_fs) // Ora si utilizza solo il segnalino 1 + { + agr_1 += agr_iva; + agr_1i += agr_imp; + + // Nuovo regime agricolo dal 1998: si sommino per codice iva di compensazione (da reperire su PCON) + if (_isagr98) + { + TString ivacomp("00"); // Se non esiste o non si trova il conto questo e' il default (fa un poco schifo ma e' cosi') + + if (_cur->is_first_match(-AGR_PCON1)) // Esiste il conto ? + // Bien, allora controlliamo se e' un ricavo... + if (_pcon_1_r->get_int(PCN_INDBIL) == 4) + if (_cur->is_first_match(-AGR_PCON2)) // Esiste il sottoconto ? + { + if (look_iva(_pcon_2_r->get(PCN_IVACOMP))) + ivacomp = _iva->get("CODTAB"); // Ecco il codice IVA di compensazione + } + look_pia(month, codatt, codiva, ivacomp, TRUE); // Crea il record in tabella se non esiste + real pia_imp = _pia->get_real("R0") + agr_imp; // Aggiorna... + _pia->put("R0", pia_imp); + _pia->put("B0", TRUE); // Seguo l'esempio dei PIM per il ricalcolo acquisti alla fine del ciclo + _pia->rewrite(); // infine scrive + } + } + else if (tipoagr == 2 && is_not_fs) + { + agr_2 += agr_iva; + agr_2i += agr_imp; + } + } + } + else if (tipodet == 0) + { + if (tipoagr == 2) + { + if (tipocr != 2 && tipocr != 3 && tipocr != 8) + agr_3 += imposta; + else + agr_4 += imposta; + } + else if (tipoagr == 3) + { + if (tipocr != 2 && tipocr != 3 && tipocr != 8) + agr_7 += imposta; + } + else // ne' 2 ne' 3 + { + if (tipocr != 2 && tipocr != 3 && tipocr != 8) + agr_5 += imposta; + else + agr_6 += imposta; + } + } + } // __end of _agricolo + + if (tipodet == 1) // acquisti indeducibili su ricavi esenti art. 10 + { + acq_ies += imponibile; + acq_ies_iva += imposta; + } + + if (tipodet == 3) // passaggi interni (solo per ventilaz) + { + acq_pint += imponibile; + acq_pint_iva += imposta; + } + + // acquisti non detraibili art. 19 + // possono valere per la ventilazione + if (tipodet == 9) + { + nond19_imp += imponibile; + nond19_iva += imposta; + } + + // ***************************************** + // Fine casi particolari + // Non che i casi normali siano tanto meglio + // ***************************************** + } // non sosp_imp ne' altre casistiche che escludono la liq. + + /* + * se ign == TRUE non bisogna neppure PROGRESSIVARLI + */ + const bool sosp_vaf = sosp_imp == vol_affari; + const bool sosp_liq = sosp_imp == liquidazione; + bool ign = sosp_imp == normale || (fattrit && month != 13) || noninc; + + /* + * se liq == FALSE gli importi non contano ai fini + * del calcolo liquidazione + */ + bool liq = !(tipodet == 1 || tipodet == 3 || tipodet == 9 || ign || sosp_vaf); + + real imp; // imponibile + real ivp; // IVA su imp. + real lor; // lordo + real lorcor; // lordo corrispettivi (da non ventilare) + + /* + * le maledette fatture con scontrino sono sul registro dei + * corrispettivi, ma vanno riportate nelle vendite in stampa; + * dunque, le sommiamo in R5 e R6 di PIM anche se continuiamo + * a sommarle anche in R0 e R1. La stampa (describe_pim) + * dovra' scorporarle. + * + * 5.7.1996. Mi permetto di aggiungere una cosa: le bastardissime fatture con scontrino + * possono anche essere sul registro vendite. Indi le schiaffo dentro + * a R5 e R6 anche nel caso vi sia un documento FS registrato sul registro + * delle vendite. Nel caso in futuro si accorgessero finalmente della puttanata + * galattica che mi hanno fatto fare basta eliminare il controllo su FS + * nel caso si tratti di non corrispettivi (vedi sotto). + * Da togliere anche nella :describe_pim() in cg4304.cpp. + * + */ + real ifs; // imponibile fatture con scontrino + real vfs; // IVA fatt. con scontrino + + /* + * le maledette autofatture non residenti art. 17 + * vedi al calcolo if (autodafe) si tengono da parte + * La stampa le scorpora solo IN SEDE di dichiarazione annuale + */ + real adf; + real adi; + + /* le fottute fatture in sospensione di imposta */ + real fsi; + real fsv; + + /* le putride bolle doganali */ + real bdi; + real bdv; + + /* i luridi falsi corrispettivi */ + real fci; + real fcv; + + /* i conturbanti art. 40 c. 5/6/8 a.i. */ + real va7i; + real va7v; + + /* le porche fatture in ritardo */ + real rti; + real rtv; + + /* + * la stramadonna dei corrispettivi gia' scorporati + * Gli si aggiungono i non incassati, sperando in bene + */ + real csi; + real csv; + /* incrociamm'e dituozz' */ + csi += nin_imp; + csv += nin_iva; + + rti += rit_imp; + rtv += rit_iva; + + if (tipomov == vendita) + { + fsi += vsimp_imp; + fsv += vsimp_iva; + } + else + { + fsi += asimp_imp; + fsv += asimp_iva; + bdi += bolld_imp; + bdv += bolld_iva; + } + + if (tipomov == vendita && liq && !corrisp) + // i corrispettivi sono gestiti a parte + { + // totale vendite + vendite += imponibile; + vendite_iva += imposta; + } + else if (tipomov == acquisto && (liq || fattrit && month == 13)) + { + // totale acquisti + acquisti += imponibile; + acquisti_iva += imposta; + + // Controlla se la data del documento si riferisce all'anno precedente (PRORATA 1998) + const TDate datedoc = _mov->get_date(MOV_DATADOC); + const int yr = datedoc.year(); + if (!imposta.is_zero() && (yr < (year_int-1) && yr > (year_int-4))) // Prende un intervallo di 2 anni al massimo + { // Quelli relativi all'anno in corso e precedente vanno assieme. + // Se abilitato, somma l'iva acquisti relativi ai documenti memorizzati con anni precedenti + TString4 yy; yy << yr; + const bool is_key = acq_iva_anni_prec.is_key(yy); + if (!is_key) + acq_iva_anni_prec.add(yy, new real, is_key); + real& aip = (real&) acq_iva_anni_prec[yy]; + aip += imposta; + } + } + + // corrispettivi + bool true_corrisp = FALSE; + if (corrisp) + { + /* + * ennesimo casino: non tutti i corrispettivi sono + * corrispettivi; in effetti, alcuni corrispettivi + * non sono corrispettivi. Ci si potrebbe domandare + * se gli altri corrispettivi sono corrispettivi o + * no; ebbene, gli altri corrispettivi risultano + * fortunatamente essere corrispettivi, a meno di + * indicazioni contrarie. + */ + if (tipodoc == "CR" || tipodoc == "RF" || tipodoc == "SC") + { + true_corrisp = TRUE; + _pim->put("I0",LORDO); + // questi sono corrispettivi davvero; comportamento normale + if (tipoiva == "VE") // da ventilare + { + lor += imponibile; + add_vendite(month, reg, tipodet, imponibile); + } + else // non da ventilare, con imposta gia' conteggiata + { + lor += imponibile + imposta; + lorcor += imponibile+imposta; + real tot = imponibile+imposta; + if (liq) add_corrisp(month, reg, tot, perciva, tipodet, codiva, codatt); + } + } + else if (tipodoc == "FF") // CM500415: prima non c'era questo if + { + // queste sono fatture fiscali + true_corrisp = TRUE; + + real tot = imponibile + imposta; + lor += tot; + lorcor += tot; + if (liq) + add_corrisp(month, reg, tot, perciva, tipodet, codiva, codatt); + } + else if (tipodoc == "FS") + { + // queste sono fatture che seguono scontrino, il lordo va detratto + // perche' si e' gia' registrato lo scontrino medesimo + true_corrisp = true; // non e' vero ma non devono essere sommate ai falsi + // corrispettivi + if (tipoiva == "VE") + { + const real rr = imponibile + imposta; + imp += rr; + if (liq) + vendite += rr; + add_vendite(month, reg, tipodet, rr); + } + if (liq) + { + csi += imponibile; + csv += imposta; + } + imp -= imponibile; + ivp -= imposta; + lor -= imponibile + imposta; + ifs += imponibile; + vfs += imposta; + } + else // vendite normali a tutti gli effetti + { + if (liq) + { + vendite += imponibile; + vendite_iva += imposta; + csi += imponibile; + csv += imposta; + } + imp += imponibile; + ivp += imposta; + _pim->put("I0",NETTO); + } + } // if corrisp + else // non corrisp + { // imponibile e imposta separata + imp += imponibile; + ivp += imposta; + if (tipodoc == "FS") // Questo e' per l'errore MI3404 + { + ifs += imponibile; + vfs += imposta; + } + _pim->put("I0",NETTO); + } + + // Rettifica il valore da memorizzare in PAM->R1, utilizzato nel calcolo Vol.Affari + // Solo nel caso in cui l'importo corrente deve far parte di tale calcolo + // Se e' valido per calcolo solo vol.aff aggiunge gli importi non aggiunti a causa del !liq + // Se e' valido per calcolo solo liq. sottrae gli importi aggiunti a causa del liq. + // Nel caso di sosp. normale non viene ne progressivato ne liquidato e ne considerato per vol. aff. + if (sosp_vaf || sosp_liq) + { + real iii = imponibile; + real iiimmm = imponibile + imposta; + + if (sosp_liq) + { + iii *= -1; + iiimmm *= -1; + } + + if (corrisp) + if (tipodoc == "FS") + { + if (tipoiva == "VE") + vendite += iiimmm; + } + else + vendite += iii; + else + if (tipomov == vendita) + vendite += iii; + } + + if (autodafe) // autofatture non residenti art.17 + { + /* + * Si devono riportare a parte in annuale + * e non comparire nel riepilogo del codice IVA + * corrispondente, solo se la liq. e' annuale + * Li metto in R7/R8 di pim e li sommo anche nelle + * vendite; la describe_pim() dovra' sommarli su tutti i + * pim, scorporarli dalle rispettive vendite + * e assegnarli al codice IVA AF in caso di liq. annuale + */ + adf += imponibile; + adi += imposta; + } + + if (art40) // ammontare acq. intr. art40 c. 5/6/8 + { // Analogamente alle autofatture, si devono riportare a parte in annuale + // Vengono piazzati in una TToken_string con separatore = a '!'. + // La TToken_string viene registrata in S0. + // La describe_pim() sommera' tutte le storie, scorporandoli + // dalle vendite e assegnandoli al codice VA7 in caso di liq annuale. + va7i += imponibile; + va7v += imposta; + } + + if (corrisp && !true_corrisp) + { + fci += imponibile; + fcv += imposta; + } + + /* + * Calcolo e aggiornamento + * cerca o crea progressivo IVA. + * Onde evitare ripetizioni di codice adotto il metodo sottoindicato, + * come nell'azzeramento di zero_att(). + */ + int start = 0, stop = 1; + if (_recalc_regis) // ricalcolo solo per registri. + { + start = 2; + stop = 3; + } + else + { + if (year_int < 1998) + stop = 0; // Solo PIM + } + for (int i=start; i<=stop; i++) // Ciclo per le tabelle + { + if (i == 1 || i == 3) //Is it a valid PIS/PRP (vedi anche sel. cursore & date_ok())? + if ((liqmonth == 0 && month < 13) || (month == 13 && liqmonth != 12)) + continue; + + switch (i) + { + case 1: // Progressivi Iva mensili da periodo Successivo + look_pis(month, codatt, reg, tipocr_s, codiva, tipodet, TRUE); + break; + case 2: // Progressivi Registri iva Mensili + look_prm(month, codatt, reg, tipocr_s, codiva, tipodet, TRUE); + break; + case 3: // Progressivi Registri iva Mensili da periodo Precedente + look_prp(month, codatt, reg, tipocr_s, codiva, tipodet, TRUE); + break; + default: // Progressivi Iva Mensili normali + look_pim(month, codatt, reg, tipocr_s, codiva, tipodet, TRUE); + break; + } + + TTable *tab = arr[i]; + TRectype& curr = tab->curr(); + + if (!ign) + { + curr.add("R0", imp); + curr.add("R1", ivp); + curr.add("R2", lor); + curr.add("R3", lorcor); // Corrispettivi, non inclusi quelli da ventilare + curr.add("R5", ifs); + curr.add("R6", vfs); + curr.add("R7", adf); + curr.add("R8", adi); + curr.add("R9", bdi); + curr.add("R10", bdv); + curr.add("R13", fci); + curr.add("R14", fcv); + } + // questi servono per i ricalcoli altrui (classify_pim) o + // per trucchetti di ricalcolo successivi + curr.add("R11",fsi); + curr.add("R12",fsv); + + curr.put("I1", (long)tipomov); + curr.put("B3", ivarimb ? "X" : ""); + curr.put("B4", (_isricacq && was_riv) ? "X" : ""); + curr.put("S5", tipoiva); + + // art 40 c. 5/6/8 per VA7. + real r,v; + TToken_string kr(tab->get("S0"),'!'); + r = real(kr.get(0)) + va7i; + v = real(kr.get(1)) + va7v; + kr.add(r.string(), 0); + kr.add(v.string(), 1); + tab->put("S0",kr); + + // fatture in ritardo + TToken_string fr(tab->get("S1"),'!'); + r = real(fr.get(0)) + rti; + v = real(fr.get(1)) + rtv; + fr.add(r.string(), 0); + fr.add(v.string(), 1); + tab->put("S1",fr); + + // corrispettivi gia' scorporati; anche questi servono per + // non cannare la stampa dei registri; contengono anche + // tutti i non incassati (vedi definizione di bool nonimp) + TToken_string cs(tab->get("S2"),'!'); + r = real(cs.get(0)) + csi; + v = real(cs.get(1)) + csv; + cs.add(r.string(), 0); + cs.add(v.string(), 1); + tab->put("S2",cs); + + if (is_detraibile == 0 && + (!diff_imp.is_zero() ||!diff_iva.is_zero() || + !incdiff_imp.is_zero() || !incdiff_iva.is_zero())) + { + if (tipomov == vendita) + { + // fatture vendita a liquidazione differita + tab->curr().add("R26", diff_imp); + tab->curr().add("R27", diff_iva); + tab->curr().add("R28", incdiff_imp); + tab->curr().add("R29", incdiff_iva); + fdiff_imp += diff_imp; + fdiff_iva += diff_iva; + fdiffinc_imp += incdiff_imp; + fdiffinc_iva += incdiff_iva; + if (_mov->get_int(MOV_ANNOIVA) < year_int) + { + _diff_ven_ap += incdiff_imp; + _diff_ven_ap_iva += incdiff_iva; + } + } + else + { + // fatture acquisto a liquidazione differita + tab->curr().add("R30", diff_imp); + tab->curr().add("R31", diff_iva); + tab->curr().add("R32", incdiff_imp); + tab->curr().add("R33", incdiff_iva); + fdiff_imp_acq += diff_imp; + fdiff_iva_acq += diff_iva; + fdiffinc_imp_acq += incdiff_imp; + fdiffinc_iva_acq += incdiff_iva; + if (_mov->get_int(MOV_ANNOIVA) < year_int) + { + _diff_acq_ap += incdiff_imp; + _diff_acq_ap_iva += incdiff_iva; + } + } + } + + tab->rewrite(); // rewrite current table + } // for per tabelle + } // for is_detraibile + } while (_cur->next_match(LF_RMOVIVA)); + } // fine calcolo progressivi IVA + + + if (!_recalc_regis) + { + /* + * calcolati tutti i movimenti e aggiornati i pim + * se necessario risistema le imposte acquisti beni + * per rivendita + */ + + if (_isricacq) // flag settato parametri studio + { + for (_pim->first(); !_pim->eof(); _pim->next()) + { + // this is much furber than doing all of the checks + if (_pim->get_bool("B4")) + { + // ricalcola l'imposta a partire dal codice IVA + look_iva(*_pim_codiva); + real old_iva = _pim->get_real("R1"); + real perc = _iva->get_real("R0"); + + real new_iva = _pim->get_real("R0") * perc / CENTO; + round_al_centesimo(new_iva); + + if (new_iva != old_iva) + { + // ricalcola: acquisti_iva, acq_riv_iva + acquisti_iva -= old_iva; + acquisti_iva += new_iva; + acq_riv_iva -= old_iva; + acq_riv_iva += new_iva; + _pim->put("R1", new_iva); + // resetta in modo da non vederlo il mese dopo + _pim->put("B4", ""); + _pim->rewrite(); + } + } + } + } + + + // Ricalcola le imposte per regime agricolo dal 1998 (PIA) + if (_isagr98) + { + for (_pia->first(); !_pia->eof(); _pia->next()) + { + if (_pia->get_bool("B0")) + { + // calcola l'imposta a partire dal codice IVA di compensazione + look_iva(*_pia_codcom); + real perc = _iva->get_real("R0"); + + real iva = _pia->get_real("R0") * perc / CENTO; + round_al_centesimo(iva); + agr_detIA += iva; + _pia->put("R1",iva); + // resetta in modo da non vederlo il mese dopo + _pia->put("B0", ""); + _pia->rewrite(); + } + } + } + + /* + * calcola il lercio prorata + * solo se liq. periodica + */ + real prorata, prorata_precedente1, prorata_precedente2; + real percentuale1, percentuale2; + if (month != 13) + { + TString4 yy; + // Prorata per documenti registrati con anno doc 2 anni fa... + yy.format("%d", year_int - 2); + real* rr1 = (real*)acq_iva_anni_prec.objptr(yy); + yy.format("%d", year_int - 1); // Reperire la % prorata dell'anno successivo a quello del documento + percentuale1 = _prorata.percentuale(yy); + prorata_precedente1 = rr1 ? _prorata.calc_prorata(*rr1, yy) : ZERO; + // Se sono presenti documenti di anni precedenti senza % prorata, si deve segnalare l'incongruenza (solo in prova) + if (percentuale1 == INVALID_PRORATA && rr1 && !rr1->is_zero()) + percentuale1 = INVALID_PRORATA_ASSERT; + + // Prorata per documenti registrati con anno doc 3 anni fa... + yy.format("%d", year_int - 3); + real *rr2 = (real*)acq_iva_anni_prec.objptr(yy); + yy.format("%d", year_int - 2); // Reperire la % prorata dell'anno successivo a quello del documento + percentuale2 = _prorata.percentuale(yy); + prorata_precedente2 = rr2 ? _prorata.calc_prorata(*rr2, yy) : ZERO; + // Se sono presenti documenti di anni precedenti senza % prorata, si deve segnalare l'incongruenza (solo in prova) + if (percentuale2 == INVALID_PRORATA && rr2 && !rr2->is_zero()) + percentuale2 = INVALID_PRORATA_ASSERT; + + // Prorata per l'anno corrente + quelli eventuali dello scorso anno e quelli che sono oltre 3 anni fa... + // E quelli che non hanno la % prorata settata in tabella + real aaa = acquisti_iva; + if (percentuale1 != INVALID_PRORATA && percentuale1 != INVALID_PRORATA_ASSERT) + aaa -= rr1 ? *rr1 : ZERO; + if (percentuale2 != INVALID_PRORATA && percentuale2 != INVALID_PRORATA_ASSERT) + aaa -= rr2 ? *rr2 : ZERO; + prorata = _prorata.calc_prorata(aaa, _year); + + const real prorata_diff = _prorata.calc_prorata(fdiffinc_iva_acq, _year); + + // Somma i vari prorata calcolati per gli anni precedenti + prorata += prorata_precedente1 + prorata_precedente2; + + acquisti_iva -= prorata; // Rettifica acquisti_iva + + prorata += prorata_diff; + fdiffinc_iva_acq -= prorata_diff; // Rettifica acquisti_iva differiti incassati + } + look_plm(month, codatt, TRUE); + + /* + * ACHTUNG: l'iva sulle vendite e' calcolata sommando anche i + * corrispettivi che sono corrispettivi, a differenza dei corrispettivi + * che non sono corrispettivi, ma tanto quelli (come si sa) non sono + * corrispettivi. Dunque, non si tiene conto delle imposte calcolate + * con la ventilazione (che vanno sottratte) ne' di quelle + * calcolate per le agenzie di viaggio (che sembra non vadano + * comunque sottratte nel calcolo del volume di affari) + */ + + /* + * Il prorata pagato in annuale viene scritto a 0, perche' in realta' sarebbe + * la somma di quelli pagati nei mesi precedenti. + */ + _plm->put("R2", prorata); // Prorata complessivo (acq. rif. anno attuale + acq. rif. anni precedenti) + _plm->put("R14", prorata_precedente1); // Prorata acq. rif. anno addietro + _plm->put("R15", prorata_precedente2); // Prorata acq. rif. 2 anni addietro + + if (_isviaggio) + { + _plm->put("R5", corr_CEE); + _plm->put("R6", corr_noCEE); + _plm->put("R7", acq_CEE); + _plm->put("R8", acq_noCEE); + _plm->put("R9", corr_misCEE); + _plm->put("R10",acq_misCEE); + _plm->put("R11",acq_misnoCEE); + + // ---- NOTA BENE ------------------------ + // l'iva vendite nei plm non comprende + // l'iva agenzie di viaggio, calcolata + // in liquidazione (mensile o trimestrale): recalc_viaggio() + } + else if (_isagricolo) + { + _plm->put("R5", agr_1); + _plm->put("R6", agr_2); + _plm->put("R7", agr_3); + _plm->put("R8", agr_4); + _plm->put("R9", agr_5); + _plm->put("R10", agr_6); + _plm->put("R11", agr_7); + _pum->put("R10", agr_1i); + _pum->put("R11", agr_2i); + + // Il porco agricoltore spende poco e paga meno + vendite_iva = agr_2; + acquisti_iva = agr_1 + agr_3; + + // nuovo calcolo IVA detraibile + if (!agr_7.is_zero()) + { + real pdetr = (agr_2i / (agr_1i + agr_2i)); + pdetr.round(2); + acquisti_iva += agr_7 * pdetr; + round_al_centesimo(acquisti_iva); + } + + if (_isagr98) + { + _plm->put("R13", agr_detIA); + acquisti_iva += agr_detIA; + } + } + + _plm->put("R12", _prorata.percentuale(_year)); // per comodita' in stampa + _plm->put("R4", percentuale1); + _plm->put("R16", percentuale2); + _plm->put("R0", vendite_iva); + _plm->put("R1", acquisti_iva); + _plm->put("R3", ult_detr); + _plm->put("B0", "X"); // calcolato (invalidato dalla primanota) + // Setta il flag di stampa errore: se siamo oltre 1997 e non esiste tabella anno prec e vi sono docs con tale riferimento + _pom->put("R9", acq_ies); + _pom->put("R10", acq_ies_iva); + _pum->put("R8", acq_pint); + _pum->put("R9", acq_pint_iva); + _pam->put("R10", spgen); + _pam->put("R11", spgen_iva); + _pom->put("R0", totintra); + _pom->put("R1", nond19_imp); + _pom->put("R2", nond19_iva); + _pom->put("R3", ammort_det); + _pom->put("R4", ammort_det_iva); + _pom->put("R5", acq_riv); + _pom->put("R6", acq_riv_iva); + _pom->put("R7", leasing); + _pom->put("R8", leasing_iva); + _pom->put("R11", ammort_indet); + _pom->put("R12", ammort_indet_iva); + _pum->put("R0", cess_amm); + _pum->put("R1", cess_amm_iva); + _pum->put("R2", ammort_6); + _pum->put("R3", ammort_6_iva); + _pum->put("R4", esenti_c1); + _pum->put("R5", esenti_c2); + _pum->put("R6", esenti_c3); + _pum->put("R13", esenti_c1a); + _pum->put("S4", esenti_c3_bam); + _pum->put("S5", esenti_c1a_bam); + _pum->put("R7", esenti_b14); + _pum->put("R12", esni_rimb); + _pum->put("R14", cessioni_cd1_1); + _pum->put("R15", cessioni_cd1_2); + _pum->put("R16", cessioni_cd1_3); + _pum->put("R17", cessioni_cd1_4); + _pum->put("R18", acquisti_cd2_1); + _pum->put("R19", acquisti_cd2_2); + _pum->put("R20", acquisti_cd2_3); + _pum->put("R21", acquisti_cd2_4); + _pum->put("R22", oroargento_cd3_1); + _pum->put("R23", oroargento_cd3_2); + _pum->put("R24", rottami_cd3_3); + _pum->put("R25", rottami_cd3_4); + _pum->put("R26", acquisti_cd2_5); + _pum->put("R27", cessioni_cd1_5); + _pam->put("R0", acquisti); + _pam->put("R1", vendite); + _pam->put("R6", assp_imp); + _pam->put("R7", assp_iva); + _pam->put("R8", vssp_imp); + _pam->put("R9", vssp_iva); + _pam->put("R4", bdog_imp); + _pam->put("R5", bdog_iva); + _pam->put("R12", assl_imp); + _pam->put("R13", assl_iva); + _pam->put("R14", vssl_imp); + _pam->put("R15", vssl_iva); + + _plm->put("R26", fdiff_imp); + _plm->put("R27", fdiff_iva); + _plm->put("R28", fdiffinc_imp); + _plm->put("R29", fdiffinc_iva); + _plm->put("R30", fdiff_imp_acq); + _plm->put("R31", fdiff_iva_acq); + _plm->put("R32", fdiffinc_imp_acq); + _plm->put("R33", fdiffinc_iva_acq); + + _plm->rewrite(); + _pam->rewrite(); + _pum->rewrite(); + _pom->rewrite(); + + if (month == 13) + { + TTable pem("PEM"); + for (int reg = 0; reg < 23; reg++) + { + look_pem(pem, reg); + real r = pem.get_real("R0") + vt_imponibile[reg]; + pem.put("R0", r); + r = pem.get_real("R1") + vt_imposta[reg]; + pem.put("R1", r); + pem.rewrite(); + } + } + } +} + +void TLiquidazione_app::iva11_set_arr_phase_1(const TString& codatt) +// viene chiamata quando vengono scorsi i movimenti del mese, settando gran parte degli elementi di _iva11_arr. +{ + // My God! Che 2 palle riestrarre tutte le informazioni + const TString4 tipodoc = _mov->get(MOV_TIPODOC); + const bool intra = _rmoviva->get_bool(RMI_INTRA); + const int tipocr = _rmoviva->get_int(RMI_TIPOCR); + // A partire dalla versione 3.1 la gestione dell'IVA 11 non viene piu' supportata ... + // ... ergo non stiamo a gestire la indetraibilita' parziale + real percind; + const int tipodet = get_tipodet_from_rmi(_rmoviva->curr(), _mov->curr(), percind); + real imponibile = _rmoviva->get_real(RMI_IMPONIBILE); + real imposta = _rmoviva->get_real(RMI_IMPOSTA); + const TString4 codiva = _iva->get("CODTAB"); + const TString4 tipoiva = _iva->get("S1"); + const real ali = _iva->get_real("R0"); + const TString16 tipoes_v = _iva->get("S2"); + const TString16 tipoes_a = _iva->get("S9"); + const int tipoagr = _iva->get_int("S4"); + const TRectype& rcs = _cur->curr(LF_CAUSALI); + const bool autofattura = rcs.get_bool("AUTOFATT"); + const bool valintra = rcs.get_bool("VALINTRA"); + const bool intracom = rcs.get_bool("INTRACOM"); + const tipo_sospensione sosp_imp = _reg->get_bool("B1") ? (tipo_sospensione) _reg->get_int("I9") : nessuna; + const bool corrisp = _reg->get_bool("B0"); + const tiporeg tipomov = (tiporeg)_reg->get_long("I0"); + const bool is_vendita = tipomov == vendita; + const bool is_acquisto = tipomov == acquisto; + + const bool is_key = _iva11_arr.is_key(codatt); + if (!is_key) // Aggiunge l'elemento se non c'e' + _iva11_arr.add(codatt,(_Iva11Array*)new _Iva11Array,is_key); + _Iva11Array& array = (_Iva11Array&)_iva11_arr[codatt] ; + + + // TAB11_RQA34 ovvero "RQA34" non viene trasferito! + // Sebbene per motivi di pietà viene comunque lasciato nel tracciato record (esigenze PRASSI) + if (codiva.blank()) + return; + + if (is_acquisto) + { + if (_isagricolo && tipodet == 0) + { + if (tipoagr == 2) + { + if (tipocr == 2 || tipocr == 3 || tipocr == 8) + { + array.sub(imponibile,TAB11_G30I,LF_TAB1100B); + array.sub(imposta,TAB11_G30V,LF_TAB1100B); + } + else + { + array.add(imponibile,TAB11_G30I,LF_TAB1100B); + array.add(imposta,TAB11_G30V,LF_TAB1100B); + } + } + else if (tipoagr == 3 && tipocr !=2 && tipocr != 3 && tipocr != 8) + { + array.add(imponibile,TAB11_G31I,LF_TAB1100B); + array.add(imposta,TAB11_G31V,LF_TAB1100B); + } + } + + if (sosp_imp == nessuna && tipodet == 0 && (tipocr == 2 || tipocr == 8)) + { + array.add(imposta,TAB11_R6,LF_TAB1100B); + } + + if (sosp_imp == nessuna || sosp_imp == vol_affari) + { + if (tipodoc == "BD" && tipodet == 9) + { + array.add(imponibile,TAB11_F76); + if (_sind11) + array.add(imposta,TAB11_F76); + } + + if (tipodoc != "BD") + { // Compila prima i campi FC1nn ed FC2nn, poi i campi SBFnn ed SBInn + if (tipodet != 9) + { + if (ali == 2.00) + { + array.add(imponibile,TAB11_FC101); + array.add(imposta,TAB11_FC201); + } + else if (ali == 4.00) + { + array.add(imponibile,TAB11_FC102); + array.add(imposta,TAB11_FC202); + } + else if (ali == 6.00) + { + array.add(imponibile,TAB11_FC103); + array.add(imposta,TAB11_FC203); + } + else if (ali == 7.50) + { + array.add(imponibile,TAB11_FC104); + array.add(imposta,TAB11_FC204); + } + else if (ali == 8.50) + { + array.add(imponibile,TAB11_FC105); + array.add(imposta,TAB11_FC205); + } + else if (ali == 9.00) + { + array.add(imponibile,TAB11_FC106); + array.add(imposta,TAB11_FC206); + } + else if (ali == 10.00) + { + array.add(imponibile,TAB11_FC107); + array.add(imposta,TAB11_FC207); + } + else if (ali == 16.00) + { + array.add(imponibile,TAB11_FC108); + array.add(imposta,TAB11_FC208); + } + else if (ali == 19.00) + { + array.add(imponibile,TAB11_FC109); + array.add(imposta,TAB11_FC209); + } + else if (ali == 20.00) + { + array.add(imponibile,TAB11_FC110); + array.add(imposta,TAB11_FC210); + } + + if (tipoes_a == "15") + array.add(imponibile,TAB11_FC111); + else if (tipoes_a == "12") + array.add(imponibile,TAB11_FC112); + else if (tipoes_a == "13") + array.add(imponibile,TAB11_FC113); + else if (tipoes_a == "14") + array.add(imponibile,TAB11_FC114); + else if (tipoes_a == "16") + array.add(imponibile,TAB11_FC115); + } + else + { + array.add(imponibile,TAB11_FC116); + if (_sind11) + array.add(imposta,TAB11_FC116); + } + + if (intra) // Compila i campi SBFnn + { + if (tipodet != 9) + { + if (ali == 4.00) + { + array.add(imponibile,TAB11_SBF01); + array.add(imposta,TAB11_SBI01); + } + else if (ali == 6.00) + { + array.add(imponibile,TAB11_SBF02); + array.add(imposta,TAB11_SBI02); + } + else if (ali == 7.50) + { + array.add(imponibile,TAB11_SBF03); + array.add(imposta,TAB11_SBI03); + } + else if (ali == 9.00) + { + array.add(imponibile,TAB11_SBF04); + array.add(imposta,TAB11_SBI04); + } + else if (ali == 10.00) + { + array.add(imponibile,TAB11_SBF05); + array.add(imposta,TAB11_SBI05); + } + else if (ali == 16.00) + { + array.add(imponibile,TAB11_SBF06); + array.add(imposta,TAB11_SBI06); + } + else if (ali == 19.00) + { + array.add(imponibile,TAB11_SBF07); + array.add(imposta,TAB11_SBI07); + } + else if (ali == 20.00) + { + array.add(imponibile,TAB11_SBF08); + array.add(imposta,TAB11_SBI08); + } + + if (tipoes_a == "15") + array.add(imponibile,TAB11_SBF09); + else if (tipoes_a == "12") + array.add(imponibile,TAB11_SBF10); + else if (tipoes_a == "13") + array.add(imponibile,TAB11_SBF11); + else if (tipoes_a == "14") + array.add(imponibile,TAB11_SBF12); + } + else + { + array.add(imponibile,TAB11_SBF13); + if (_sind11) + array.add(imposta,TAB11_SBF13); + } + } + } + } + } + + if (is_vendita) + { + if (tipodoc == "CN" || tipodoc == "SN" || tipodoc == "RN") // salta i non incassati + return; + + if (intracom || !valintra) + { + if (tipoes_v.not_empty()) // Inutile fare troppi controlli... + { + if (tipoes_v == "24") + array.add(imponibile,TAB11_EC106); + if (tipoes_v == "21") + array.add(imponibile,TAB11_EC107); + else if (tipoes_v == "B1") + array.add(imponibile,TAB11_EC108); + else if (tipoes_v == "B2") + array.add(imponibile,TAB11_EC109); + else if (tipoes_v == "B3") + array.add(imponibile,TAB11_EC110); + else if (tipoes_v == "25") + array.add(imponibile,TAB11_EC111); + else if (tipoes_v == "22") + array.add(imponibile,TAB11_EC113); + else if (tipoes_v == "20") + array.add(imponibile,TAB11_EC114); + else if (tipoes_v == "G7A") + array.add(imponibile,TAB11_EC115); + else if (tipoes_v == "G7B") + array.add(imponibile,TAB11_EC116); + else if (tipoes_v == "R9") // Divenuto obsoleto prima ancora di utilizzarlo... + array.add(imponibile,TAB11_R9,LF_TAB1100B); + } + + if (tipocr == 4 && tipodoc != "AF" && (sosp_imp == nessuna || sosp_imp == vol_affari)) + array.add(imponibile,TAB11_EC112); + } + + if (!(corrisp && tipoiva != "VE" && (tipodoc == "CR" || tipodoc == "RF" || tipodoc == "SC"))) + { + if (!autofattura && tipoiva != "NS") + if (tipodoc == "AF") + { + array.add(imponibile,TAB11_A35); + array.add(imposta,TAB11_L2,LF_TAB1100B); + } + else if (valintra && !intra) + { + array.add(imponibile,TAB11_A35BIS); + array.add(imposta,TAB11_L2BIS,LF_TAB1100B); + } + + if (!autofattura && (sosp_imp == nessuna || sosp_imp == vol_affari) && intra && tipoes_v == "22") + array.add(imponibile,TAB11_E52); + + if (_isagricolo && (tipodoc != "CR" && tipodoc != "SC" && tipodoc != "RF" && tipodoc != "FS")) + { + const bool t1 = tipoagr == 1; + const bool t1_3 = tipoagr == 1 || tipoagr == 3; + if (ali == 2.00) + { + if (t1) + { + array.add(imponibile,TAB11_AGME01,LF_TAB1100B); + array.add(imposta,TAB11_AGVE01,LF_TAB1100B); + } + if (intra && t1_3) + { + array.add(imponibile,TAB11_ABME01,LF_TAB1100B); + array.add(imposta,TAB11_ABVE01,LF_TAB1100B); + } + } + else if (ali == 4.00) + { + if (t1) + { + array.add(imponibile,TAB11_AGME02,LF_TAB1100B); + array.add(imposta,TAB11_AGVE02,LF_TAB1100B); + } + if (intra && t1_3) + { + array.add(imponibile,TAB11_ABME02,LF_TAB1100B); + array.add(imposta,TAB11_ABVE02,LF_TAB1100B); + } + } + else if (ali == 6.00) + { + if (t1) + { + array.add(imponibile,TAB11_AGME03,LF_TAB1100B); + array.add(imposta,TAB11_AGVE03,LF_TAB1100B); + } + if (intra && t1_3) + { + array.add(imponibile,TAB11_ABME03,LF_TAB1100B); + array.add(imposta,TAB11_ABVE03,LF_TAB1100B); + } + } + else if (ali == 7.50) + { + if (t1) + { + array.add(imponibile,TAB11_AGME04,LF_TAB1100B); + array.add(imposta,TAB11_AGVE04,LF_TAB1100B); + } + if (intra && t1_3) + { + array.add(imponibile,TAB11_ABME04,LF_TAB1100B); + array.add(imposta,TAB11_ABVE04,LF_TAB1100B); + } + } + else if (ali == 8.50) + { + if (t1) + { + array.add(imponibile,TAB11_AGME05,LF_TAB1100B); + array.add(imposta,TAB11_AGVE05,LF_TAB1100B); + } + if (intra && t1_3) + { + array.add(imponibile,TAB11_ABME05,LF_TAB1100B); + array.add(imposta,TAB11_ABVE05,LF_TAB1100B); + } + } + else if (ali == 9.00) + { + if (t1) + { + array.add(imponibile,TAB11_AGME06,LF_TAB1100B); + array.add(imposta,TAB11_AGVE06,LF_TAB1100B); + } + if (intra && t1_3) + { + array.add(imponibile,TAB11_ABME06,LF_TAB1100B); + array.add(imposta,TAB11_ABVE06,LF_TAB1100B); + } + } + } + } + } + + if (is_acquisto || is_vendita) + if (autofattura) + array.add(imponibile,TAB11_A35TER); +} + +void TLiquidazione_app::iva11_set_arr_phase_2(const TString& codatt) +// scorre (guarda un poco) tutti i PIM della ditta per COMPLETARE _iva11_arr +// Ribadisco: questa funzione COMPLETA solamente l'array scorrendo i PIM e memorizzando i rimanenti +// campi. Gli altri sono compilati scorrendo i movimenti! (fatto prima della chiamata a questa funzione) +// Il motivo per cui il trasferimento e' fatto in 2 fasi (iva11_set_arr_phase_1 ed iva11_set_arr_phase_2) +// e' perche' alcune operazioni di calcolo (scorporo corrispettivi, ventilazione...) vengono effettuate +// dopo la recalc_att(), punto in cui viene chiamata la phase_1. +{ + TString4 codiva, reg, tiva; + TString16 tpla, tvia; + TString16 v11, a11; + TToken_string tt("",'!'); + tiporeg treg; + int tipocr,tipodet,tagr; + real aliq, imp, iva, lor, vtot, atot, ivav, ivaa, adf, adi, va7i, va7v; + TString16 cur_att; + + const bool is_key = _iva11_arr.is_key(codatt); + if (!is_key) + _iva11_arr.add(codatt,(_Iva11Array*)new _Iva11Array,is_key); + _Iva11Array& array = (_Iva11Array&)_iva11_arr[codatt]; + + // Scorporo corretto dei corrispettivi in R3 + TAssoc_array corr_ann; + bool is_present; + + for (_pim->first(); !_pim->eof(); _pim->next()) + { + if (_year != *_pim_anno) continue; + if (atoi(*_pim_mese) != 13) continue; + cur_att = *_pim_codatt; + cur_att.rtrim(1); + if (codatt != cur_att) continue; // considera solo quelli dell'anno, obviously. + // Must consider current activity too... + codiva = *_pim_codiva; + reg = *_pim_codreg; + look_iva(codiva); look_reg(reg); // posiziona la tabella registri e quella IVA + tiva = _iva->get("S1"); // tipo IVA + tpla = _iva->get("S3"); // tipo gestione plafond + tagr = _iva->get_int("S4"); // tipo gestione regime agricolo + tvia = _iva->get("S5"); // tipo gestione ag. viaggio + v11 = _iva->get("S2"); // n.ro riga vendite per mod. IVA11 + a11 = _iva->get("S9"); // n.ro riga acquisti per mod. IVA11 + aliq = _iva->get_real("R0"); // aliquota + treg = (tiporeg)_reg->get_long("I0"); + const bool is_vendita = treg == vendita; + const bool is_acquisto = treg == acquisto; + tipocr = atoi(*_pim_tipocr); + tipodet = atoi(*_pim_tipodet); + imp = _pim->get_real("R0"); + iva = _pim->get_real("R1"); + lor = _pim->get_real("R3"); + + if (is_acquisto) + { + if (tiva != "NS") + switch (tipocr) + { + case 2: + array.add(imp,TAB11_IMA01); + break; + case 8: + array.add(imp,TAB11_IMA02); + break; + case 1: + case 5: + array.add(imp,TAB11_IMA03); + break; + default: + break; + } + + if (tipodet == 1) + array.add(iva,TAB11_B14); + + // XC???: tipo detr != 9 + if (tipodet != 9) + { + if (aliq == 4.00) + { + array.add(_pim->get_real("R9"),TAB11_XC101); + array.add(_pim->get_real("R10"),TAB11_XC201); + } + else if (aliq == 6.00) + { + array.add(_pim->get_real("R9"),TAB11_XC102); + array.add(_pim->get_real("R10"),TAB11_XC202); + } + else if (aliq == 7.50) + { + array.add(_pim->get_real("R9"),TAB11_XC103); + array.add(_pim->get_real("R10"),TAB11_XC203); + } + else if (aliq == 9.00) + { + array.add(_pim->get_real("R9"),TAB11_XC104); + array.add(_pim->get_real("R10"),TAB11_XC204); + } + else if (aliq == 10.00) + { + array.add(_pim->get_real("R9"),TAB11_XC105); + array.add(_pim->get_real("R10"),TAB11_XC205); + } + else if (aliq == 16.00) + { + array.add(_pim->get_real("R9"),TAB11_XC106); + array.add(_pim->get_real("R10"),TAB11_XC206); + } + else if (aliq == 19.00) + { + array.add(_pim->get_real("R9"),TAB11_XC107); + array.add(_pim->get_real("R10"),TAB11_XC207); + } + else if (aliq == 20.00) + { + array.add(_pim->get_real("R9"),TAB11_XC108); + array.add(_pim->get_real("R10"),TAB11_XC208); + } + + if (a11 == "12") + array.add(_pim->get_real("R9"),TAB11_XC109); + if (a11 == "13" || a11 == "14") + array.add(_pim->get_real("R9"),TAB11_XC110); + if (a11 == "16") + array.add(_pim->get_real("R9"),TAB11_XC111); + } + } // is_acquisto + + if (is_vendita) + { + if ((_isagricolo && tagr==2) || !_isagricolo) + { + // Accumula i corrispettivi da scorporare per codice iva + if (lor != ZERO) + { + is_present = corr_ann.is_key(codiva); + if (!is_present) // Aggiungilo se non c'e' + corr_ann.add(codiva,(_CorrItem*)new _CorrItem,is_present); + _CorrItem& ca = (_CorrItem&) corr_ann[codiva]; + ca._totale += lor; + if (!is_present) + ca._aliquota = _iva->get_real("R0"); // Se e' nuovo setta l'aliquota + } + + tt = _pim->get("S0"); + va7i = tt.get(0); + va7v = tt.get(1); + adf = imp - _pim->get_real("R7") - va7i + _pim->get_real("R5"); + adi = iva - _pim->get_real("R8") - va7v + _pim->get_real("R6"); + if (aliq == 4.00) + { + array.add(adf,TAB11_EC101); + array.add(adi,TAB11_EC201); + } + else if (aliq == 10.00) + { + array.add(adf,TAB11_EC102); + array.add(adi,TAB11_EC202); + } + else if (aliq == 16.00) + { + array.add(adf,TAB11_EC103); + array.add(adi,TAB11_EC203); + } + else if (aliq == 19.00) + { + array.add(adf,TAB11_EC104); + array.add(adi,TAB11_EC204); + } + else if (aliq == 20.00) + { + array.add(adf,TAB11_EC105); + array.add(adi,TAB11_EC205); + } + } + } // is_vendita + + // Sommatoria per il calcolo aliquote medie vendite/acquisti... + if (_pim->get_bool("B3")) // E' un record valido per fare questa cosa? + { + if ((tiporeg)_pim->get_long("I1") == vendita) + { //vendite + vtot += imp; + ivav += iva; + } + else + { + //acquisti + if (tipodet != 9) // Evita di sommare quelli con tipo detraibilita' 9 (MI2209) + { + atot += imp; + ivaa += iva; + } + } + } + } + + // Scorre i corrispettivi lordi accumulati, li scorpora ed aggiorna i righi relativi + _CorrItem* cc; + for (cc = (_CorrItem *)corr_ann.first_item(); cc != NULL; cc = (_CorrItem *)corr_ann.succ_item()) + { + lordo2netto(cc->_totale,imp,iva,cc->_aliquota/CENTO); + vtot += imp; ivav += iva; + if (cc->_aliquota == 4.00) + { + array.add(imp,TAB11_EC101); + array.add(iva,TAB11_EC201); + } + else if (cc->_aliquota == 10.00) + { + array.add(imp,TAB11_EC102); + array.add(iva,TAB11_EC202); + } + else if (cc->_aliquota == 16.00) + { + array.add(imp,TAB11_EC103); + array.add(iva,TAB11_EC203); + } + else if (cc->_aliquota == 19.00) + { + array.add(imp,TAB11_EC104); + array.add(iva,TAB11_EC204); + } + else if (cc->_aliquota == 20.00) + { + array.add(imp,TAB11_EC105); + array.add(iva,TAB11_EC205); + } + } + + + // Mmmhh.. Rettifica l'importo di EC114: EC114 = EC115 + EC116 + EC114 + real r15, r16; + if (array.is_key(TAB11_EC115)) + r15 = ((_Iva11Item&)array[TAB11_EC115]).value(); + if (array.is_key(TAB11_EC116)) + r16 = ((_Iva11Item&)array[TAB11_EC116]).value(); + r16+=r15; + array.add(r16,TAB11_EC114); + + + // Calcola le maledette aliquote medie... + real alv,ala; + if (vtot != 0.0) { alv = ivav/vtot; alv *= CENTO; alv.round(2); } + if (atot != 0.0) { ala = ivaa/atot; ala *= CENTO; ala.round(2); } + array.add(ala,TAB11_R1,LF_TAB1100B); + array.add(alv,TAB11_R2,LF_TAB1100B); + + // Mo' si pappa i PLM per compilare T1, T1C, T2, T2C, T3, T5 e T6 + // ATTENZIONE: solo per i regimi 74ter (ag. viaggio) + for (_plm->first();!_plm->eof();_plm->next()) + { + if (_year != *_plm_anno || atoi(*_plm_mese) != 13) continue; + cur_att = *_plm_codatt; + cur_att.rtrim(1); + if (codatt != cur_att) continue; + if (_isviaggio) + { + array.add(_plm->get_real("R5"),TAB11_T1,LF_TAB1100B); + array.add(_plm->get_real("R7"),TAB11_T1C,LF_TAB1100B); + array.add(_plm->get_real("R6"),TAB11_T2,LF_TAB1100B); + array.add(_plm->get_real("R8"),TAB11_T2C,LF_TAB1100B); + array.add(_plm->get_real("R9"),TAB11_T3,LF_TAB1100B); + array.add(_plm->get_real("R10"),TAB11_T5,LF_TAB1100B); + array.add(_plm->get_real("R11"),TAB11_T6,LF_TAB1100B); + } + array.add(_plm->get_real("R0"),TAB11_IVVENR); + } +} + +void TLiquidazione_app::iva11_write(bool reset_r1r2) +// Trasferisce i dati IVA11 annuali dall'assoc_array al file tab1100 +{ + const int items = _iva11_arr.items(); + if (items == 0) return; + + TString ditta,attiv,fld_name; + + _Iva11Array * iva11_arr; + _Iva11Item * iva11; + TLocalisamfile& tab1100a = _tab11->lfile(); + TLocalisamfile& tab1100b = _tab11->lfile(LF_TAB1100B); + + int err; + // Ciclo sulle attivita' + for (iva11_arr = (_Iva11Array *) _iva11_arr.first_item(); iva11_arr != NULL; iva11_arr = (_Iva11Array*)_iva11_arr.succ_item()) + { + // Setta i campi chiave per entrambi i files della relazione + ditta.format("%05ld",_nditte->curr().get_long(NDT_CODDITTA)); + attiv = _iva11_arr.get_hashobj()->key(); + tab1100a.zero(); + tab1100a.put(TAB11_TADITT,ditta); + tab1100a.put(TAB11_TACATT,attiv); + tab1100b.zero(); + tab1100b.put(TAB11_TADITT,ditta); + tab1100b.put(TAB11_TACATT,attiv); + + const int iva11_items = iva11_arr->items(); + // Ciclo sugli elementi per compilare i campi della relazione + for (iva11 = (_Iva11Item *)iva11_arr->first_item(); iva11 != NULL;iva11 = (_Iva11Item *)iva11_arr->succ_item()) + { + fld_name = iva11_arr->get_hashobj()->key(); + if (reset_r1r2 && (fld_name == TAB11_R1 || fld_name == TAB11_R2)) continue; // salta R1/R2 se devono rimanere a zero + _tab11->lfile(iva11->file()).put(fld_name,iva11->value()); + } + + if (_tab11->write() != NOERR) + if ((err=_tab11->rewrite()) != NOERR) + error_box(TR("Errore %d tentando di scrivere sul file tab1100."),err); + } + + _iva11_arr.destroy(); // resetta l'array. +} + +void TLiquidazione_app::write_liq(int month, const char* codatts) + // Calcolo liq. mensili e liq. annuali. Scrive le lim +{ + TToken_string atts(codatts); + const int year_int = atoi(_year); + + if (_ver->read(year_int, _month)!=NOERR) + warning_box(TR("Errore %d in lettura tabella versamenti ed interessi."),_ver->status()); + +/* Cazzata galattica: già gestito BENE da update_firm, prima di chiamare questa funzione + // evita casino se differita ma si sta ricalcolando il primo mese con ricalcolo di piu' mesi + const bool wasdifferita = (_isdifferita && month <= _monthinatt && _month > _monthinatt); + if (wasdifferita) _isdifferita = FALSE; +*/ + // liq. differita: considera mese precedente (solo per i risultati) + int deltam = month; + if (_isdifferita && !is_first_month(month) && month != 13) + month -= (_freqviva == "T" ? 3 : 1); + deltam -= month; + + look_lia(); + const bool new_age_2000 = (year_int >= 2000) && (_lia->get("S9") == "NV"); + + real risultato = ZERO; + real detrazioni = ZERO; + real versamenti = ZERO; + real vers_int = ZERO; + real rimborsi = ZERO; + real rettifiche = ZERO; + real res_debt = ZERO; + real res_cred = ZERO; + real cred_prec = ZERO; + real cred_trasf = ZERO; + real debt_precd = ZERO; + real acc_dec = ZERO; + real iva_vend = ZERO; + real iva_acq = ZERO; + real conguaglio = ZERO; + real variazioni_imposta = ZERO; + real imposta_non_versata = ZERO; + real crediti_speciali = ZERO; + + // totali per regimi agricoli + real acq_noCEE = ZERO; + real imp_agr1 = ZERO; + real imp_agr2 = ZERO; + + // totali per agenzie viaggio + real c_iCEE = ZERO; + real c_eCEE = ZERO; + real a_iCEE = ZERO; + real a_eCEE = ZERO; + real c_mCEE = ZERO; + real a_mCEE = ZERO; + real a_meCEE = ZERO; + real cred_cost = ZERO; + real deb_mens = ZERO; + real perc_r = ZERO; + real fdiff_imp = ZERO; + real fdiff_iva = ZERO; + real fdiffinc_imp = ZERO; + real fdiffinc_iva = ZERO; + real fdiff_imp_acq = ZERO; + real fdiff_iva_acq = ZERO; + real fdiffinc_imp_acq = ZERO; + real fdiffinc_iva_acq = ZERO; + + // totali per annuali + real vol_aff_1 = ZERO; + real vol_aff_2 = ZERO; + real vol_aff_t = ZERO; + real vol_aff_l = ZERO; + real tot_cong = ZERO; + + bool differita = FALSE; + bool almeno_una_agricola = FALSE; + bool almeno_una_normale = FALSE; + int attc = 0; // counter attivita' + + // nuova liquidazione + real credito_utilizzabile_inizio_anno = ZERO; + real credito_utilizzato_inizio_anno = ZERO; + real credito_utilizzato_iva = ZERO; + if (new_age_2000) + { + credito_utilizzato_inizio_anno = _lia->get_real("R15"); + credito_utilizzabile_inizio_anno = _lia->get_real("R0") - credito_utilizzato_inizio_anno; + if (credito_utilizzabile_inizio_anno < ZERO) + credito_utilizzabile_inizio_anno = ZERO; + } + + TLocalisamfile& attiv = _nditte->lfile(LF_ATTIV); + + const char* tmpatt; + while ((tmpatt = atts.get()) != NULL) + { + const TString16 att = tmpatt; + + // Reperisce il flag di regime agricolo per ogni attivita'... + attiv.put(ATT_CODATT,att); + attiv.read(); + const bool attivita_agricola = attiv.get_bool(ATT_REGAGR); + almeno_una_agricola |= attivita_agricola; + almeno_una_normale |= !attivita_agricola; + + if (_isdifferita && is_first_month(month+deltam)) + { + // usa i totali del mese di dicembre dell'anno + // precedente + differita = TRUE; + const TString4 yr(_year); // Salvo anno corrente + _year.format("%d", year_int-1); + if (!look_lim(12)) //controlla solamente, il vero posizionamento lo fa dopo + { + _year = yr; // Ripristino anno corrente + differita = FALSE; + } + } + + for (int m = 1; m <= 13; m++) + { + // ciclo su tutti i mesi del caso (1 o 3; + // tutti se annuale) + if (!is_month_ok(m, month)) + continue; + + look_plm(differita ? 12 : m, att); + bool is_lim = look_lim(_isdifferita ? m+deltam : m); + + // Se trattasi di annuale considera solo PLM/PUM del mese 13 + if ((month == 13 && m == 13) || month != 13) + { + // gia' conteggiato: prorata + // da conteggiare: IVA vendite ag. viaggio + // a debito: IVA vendite, debito precedente + // a credito: IVA acquisti, ulteriori detrazioni 1 e 2 + + if (!attivita_agricola) + { + const real vend = _plm->get_real("R0"); + const real acq = _plm->get_real("R1"); + const real diff_imp = _plm->get_real("R26"); + const real diff_iva = _plm->get_real("R27"); + const real diffinc_imp = _plm->get_real("R28"); + const real diffinc_iva = _plm->get_real("R29"); + const real diff_imp_acq = _plm->get_real("R30"); + const real diff_iva_acq = _plm->get_real("R31"); + const real diffinc_imp_acq = _plm->get_real("R32"); + const real diffinc_iva_acq = _plm->get_real("R33"); + + iva_vend += vend; + iva_acq += acq; + risultato += (vend - acq + diffinc_iva - diffinc_iva_acq); + res_debt += vend + diffinc_iva; + res_cred += acq; + fdiff_imp += diff_imp; + fdiff_iva += diff_iva; + fdiffinc_imp += diffinc_imp; + fdiffinc_iva += diffinc_iva; + fdiff_imp_acq += diff_imp_acq; + fdiff_iva_acq += diff_iva_acq; + fdiffinc_imp_acq += diffinc_imp_acq; + fdiffinc_iva_acq += diffinc_iva_acq; + } + else + { + // Tiene aggiornata l'iva vendite + iva_vend += _plm->get_real("R6") + _plm->get_real("R5"); + // Tiene aggiornata l'iva acquisti, che andra' rettificata all'uscita del mese + // secondo la formula: + // iva_acq = iva_acq + iva detraibile calcolata + iva compensazione + // L'iva di compensazione e' tratta dall'iva vendite agricole con segnalino 1 (vedi tabella PIA) + // il cui totale e' memorizzato in plm->R13 + // l'iva detraibile calcolata e' a sua volta fatta cosi' + // sum(plm->R11) * { sum(pum->R11) / [sum(pum->R11) + sum(pum->R10)]} + // dove plm->R11 = acquisti misti fuori CEE + // pum->R10 = tot. imponibile reg agr. 1 + // pum->R11 = tot. imponibile reg agr. 2 + // L'iva acquisti viene calcolata in modo diverso se siamo dal 1998 in poi: + // somma l'iva ammessa in detr. I parte tab. A (R13) anziche' iva vendite (R5) + iva_acq += _plm->get_real("R7") + _plm->get_real(year_int > 1997 ? "R13" : "R5"); + acq_noCEE += _plm->get_real("R11"); + imp_agr1 += _pum->get_real("R10"); + imp_agr2 += _pum->get_real("R11"); + + const real diff_imp = _plm->get_real("R26"); + const real diff_iva = _plm->get_real("R27"); + const real diffinc_imp = _plm->get_real("R28"); + const real diffinc_iva = _plm->get_real("R29"); + const real diff_imp_acq = _plm->get_real("R30"); + const real diff_iva_acq = _plm->get_real("R31"); + const real diffinc_imp_acq = _plm->get_real("R32"); + const real diffinc_iva_acq = _plm->get_real("R33"); + + fdiff_imp += diff_imp; + fdiff_iva += diff_iva; + fdiffinc_imp += diffinc_imp; + fdiffinc_iva += diffinc_iva; + fdiff_imp_acq += diff_imp_acq; + fdiff_iva_acq += diff_iva_acq; + fdiffinc_imp_acq += diffinc_imp_acq; + fdiffinc_iva_acq += diffinc_iva_acq; + } + detrazioni+= (_plm->get_real("R3")); + + // detrazioni solo non in regime agricolo + if (!attivita_agricola) + { + const real r3 = _plm->get_real("R3"); + risultato -= r3; + res_cred += r3; + } + /* totalizza importi 74 ter */ + if (_isviaggio) + { + // somma totali per calcolo successivo + c_iCEE += _plm->get_real("R5"); + c_eCEE += _plm->get_real("R6"); + a_iCEE += _plm->get_real("R7"); + a_eCEE += _plm->get_real("R8"); + c_mCEE += _plm->get_real("R9"); + a_mCEE += _plm->get_real("R10"); + a_meCEE += _plm->get_real("R11"); + } + } // endif per considerare solo PLM del mese 13 + + // Le seguenti cose (versamenti, rettifiche e rimborsi) + // vanno considerate per tutti i mesi (tabella lim) + + /* se annuale, somma versamenti mesi 1-12 + * solo una volta (non per tutte le attivita') + */ + if (attc == 0 && month == 13) + { + // l'acconto a dicembre si conteggia a parte solo + // nei casi previsti e non in annuale (vedi sotto) + real vs(versamenti_IVA(m , m == 12 ? "1" : "1|7")); + real vi(versamenti_IVA(m,"5")); + versamenti += vs; + vers_int += vi; + res_cred += vs + vi; + } + + /* + * rimborso se chiesto e previsto + * in annuale non si chiede e non va sommato se no lo cumula + */ + if (attc == 0 && is_lim && m < 13 && is_month_ok_strict(m,month)) + { + const real rimborso_richiesto = _lim->get_real("R1"); + risultato += rimborso_richiesto; + rimborsi += rimborso_richiesto; + res_debt += rimborso_richiesto; + } + + /* + * rettifiche gia' col loro bravo segno + */ + if (attc == 0 && is_lim && is_month_ok_strict(m,month)) + { + bool somma_rettifiche = FALSE; + if (year_int < 2000) + { + // se sono per benzinaro le conta solo in annuale, + // se no vanno solo in periodica + // almeno cosi' dicevano fino ad agosto + // poi, sara' il caldo, mi hanno detto che e' sbagliato + const TString& descrett = _lim->get("S0"); + const bool isforbenzinaro = descrett[0] == '$' || (descrett[0] == '>' && descrett[1] == '>'); + somma_rettifiche = !(month == 13 && !isforbenzinaro) && m < 13; + } + else + { + somma_rettifiche = month < 13 || m < 13; // CM00278 + } + if (somma_rettifiche) + { + const real rett = _lim->get_real("R5"); + risultato += rett; + rettifiche += rett; + if (rett.sign() < 0) + res_cred -= rett; + else + res_debt += rett; + } + } + + /* + * Somme varie per 13a + */ + if (attc == 0 && is_lim && m < 13 && is_month_ok_strict(m,month)) + { + const real r17 = _lim->get_real("R17"); + variazioni_imposta += r17; + risultato += r17; + if (r17.sign() >= 0) + res_debt += r17; + else + res_cred -= r17; + + const real r18 = _lim->get_real("R18"); + imposta_non_versata += r18; + risultato += r18; + res_debt += r18; + + const real r19 = _lim->get_real("R19"); + crediti_speciali += r19; + risultato -= r19; + res_cred += r19; + } + } // fine ciclo sul mese + + // counter attivita' per evitare troppi versamenti + attc++; + if (differita) + _year.format("%d", atoi(_year)+1); + + if (!(_isdifferita && is_first_month(month+deltam))) + { + if (_isviaggio) + { + // calcolo credito costo, debito mensile, perc. ripart. + perc_r = a_mCEE.is_zero() ? ZERO : (a_mCEE * CENTO)/(a_mCEE + a_meCEE); perc_r.round(2); + real ivm = (c_mCEE * perc_r)/CENTO; round_al_centesimo(ivm); + real tc = (c_iCEE + ivm); + real ta = (a_iCEE + a_mCEE); + real bi = tc - ta - credito_costo_prec(month); + + if (bi.sign() < 0) + // credito di costo + { + cred_cost = abs(bi); + round_imposta(cred_cost); + } + else + { + const real ali_via = aliquota_agvia(); + // deb_mens = (bi /((CENTO + ali_via)/CENTO)) * (ali_via/CENTO); + deb_mens = bi * ali_via / (ali_via+CENTO); + round_al_centesimo(deb_mens); // ceil voluto MI3074 + risultato += deb_mens; + res_debt += deb_mens; + iva_vend += deb_mens; + } + } + } + if (attivita_agricola) + { + real perc; // percentuale di deducibilita' + real iva_ded; // iva deducibile + + perc = imp_agr2.is_zero() ? ZERO : imp_agr2 / (imp_agr1 + imp_agr2); + perc.round(2); + iva_ded = acq_noCEE * perc; + round_al_centesimo(iva_ded); + iva_acq += iva_ded; + res_debt += iva_vend; + res_cred += iva_acq; + risultato += (iva_vend - iva_acq); + } // __ end of agricolo + } // end ciclo su attivita' + + real credito_utilizzabile = ZERO; + + if (month < 13) // va bene anche se differita sommando deltam (che e' 0 normalmente) + { + // toglie credito precedente + cred_prec = credito_prec(month+deltam); + risultato -= cred_prec; + res_cred += cred_prec; + // vedi se c'era un debito precedente per debiti < 50.000 + debt_precd = debt_prec(month+deltam); + risultato += debt_precd; + res_debt += debt_precd; + + if (new_age_2000 && credito_utilizzabile_inizio_anno > ZERO && + (_freqviva == "M" || is_trim(month+deltam))) + { + real debiti = res_debt; round_imposta(debiti); + real crediti = res_cred; round_imposta(crediti); + const real risul = debiti - crediti; + if (risul > ZERO) + { + credito_utilizzabile = credito_utilizzabile_inizio_anno; + credito_utilizzabile -= credito_utilizzato(month+deltam, TRUE, TRUE); + if (credito_utilizzabile < ZERO) credito_utilizzabile = ZERO; + credito_utilizzato_iva = min(risul, credito_utilizzabile); + res_cred += credito_utilizzato_iva; + risultato -= credito_utilizzato_iva; + } + } + } + else + { + if (new_age_2000) + { + cred_prec = ZERO; + if (credito_utilizzabile_inizio_anno > ZERO) + { + credito_utilizzato_iva = credito_utilizzato(13, TRUE, FALSE); // CM00285 + res_cred += credito_utilizzato_iva; + risultato -= credito_utilizzato_iva; + + // Calcola credito utilizzabile da riportare nell'anno successivo + credito_utilizzabile = credito_utilizzabile_inizio_anno; + credito_utilizzabile -= credito_utilizzato(13, TRUE, TRUE); + if (credito_utilizzabile < ZERO) credito_utilizzabile = ZERO; + } + } + else + { + // per l'annuale considera solo il credito a inizio anno + cred_prec = credito_prec(_freqviva == "M" ? 1 : 3); + cred_trasf = _lia->get_real("R15"); + const real cred_res = cred_prec - cred_trasf; + risultato -= cred_res; + res_cred += cred_res; + } + } + + if (month == 13) + { + // totalizza volumi affari e calcola nuovo prorata + // per tutte le attivita' + + atts.restart(); + while ((tmpatt = atts.get()) != NULL) + { + const TString8 att(tmpatt); + const int tipoatt = att.right(1)[0] - '0'; + if (tipoatt == 1) // su PLA l'attivita' e' sempre 1 + { + if (!look_pla(att)) + continue; + + real vf1 = _pla->get_real("R14"); + real vf2(_pla->get("S1")); + real iaq = _pla->get_real("R11"); // IVA acquisti + real ppg = _pla->get_real("R12"); // pro-rata pagato + + vol_aff_1 += vf1; + vol_aff_2 += vf2; + vol_aff_t = vf1 + vf2; + vol_aff_l += _pla->get_real("R0"); // volume affari lordo + + real es_c1 = _pla->get_real("R1"); + real es_c2 = _pla->get_real("R2"); + real es_c3 = _pla->get_real("R3"); + real es_c1a = _pla->get_real("R15"); + real es_c3_am = _pla->get_real("R16"); + real es_c1a_am = _pla->get_real("R17"); + real csamm = _pla->get_real("R4"); + + real prorata = 0.0; + real conguaglio = 0.0; + real topay = 0.0; + real ris = vol_aff_t; // gia' esclusi: NS, B3, cess. amm + + // calcola nuovo prorata per ogni attivita' (miste: 1+2) + if (year_int > 1997) // Calcolo dal 1998 in poi + { + const real rsa = ris - (es_c1a-es_c1a_am) - (es_c3-es_c3_am); + const real rsn = rsa - es_c1; + if (!rsa.is_zero()) + prorata = CENTO - (rsn * CENTO / rsa); // Percentuale di indetraibilita: reciproco della percentuale di detraibilita' + } + else + { + if (!ris.is_zero()) + prorata = es_c1 * CENTO / ris; + } + + prorata.round(0); + + // calcolo conguaglio -- se positivo e' a debito + if (prorata > ZERO) + { + topay = (iaq + ppg) * prorata / CENTO; + round_imposta(topay); + } + else + prorata = ZERO; + + conguaglio = topay - ppg; + round_imposta(conguaglio); + + _pla->put("R9", conguaglio); + _pla->put("R10",prorata); + + // Prorata delle mie brame... + // Chi e' il piu' sfatto del reame? + // Non va considerata la differenza tra topay e pro-rata pagato + // ma semplicemente topay. (Vedi cg4304.cpp in set_annual()) + //tot_cong += conguaglio; + tot_cong += topay; + _pla->rewrite(); + + // scrivi nuovo prorata in tabella anno successivo + const TString4 yr = _year; + _year.format("%d", atoi(_year) + 1); + look_pla(att, TRUE); + _pla->put("R8", prorata); + _pla->rewrite(); + if (look_lia(0l, TRUE)) + { + if (atoi(_year) >= 2000) + { + real cc = cred_cost; + round_imposta(cc); + _lia->put("R5", cc); + } + else + _lia->put("R5", cred_cost); + _lia->rewrite(); + } + _year = yr; + + // Aggiusta DiffIncIVAAcq sul record della 13ma che non poteva ancora sapere il proata definitivo! + if (!prorata.is_zero() && look_plm(13, att)) + { + const real old_r33 = _plm->get("R33"); + real new_r33 = old_r33 * prorata / CENTO; + round_imposta(new_r33); + _plm->put("R33", new_r33); + _plm->rewrite(); + + fdiffinc_iva_acq = fdiffinc_iva_acq - old_r33 + new_r33; + } + } + } + + look_lia(); + _lia->put("R1", vol_aff_l); + _lia->put("R2", vol_aff_1); + _lia->put("R3", vol_aff_2); + _lia->rewrite(); + } + + // comprende anche il conguaglio prorata + risultato += tot_cong; + if (atoi(_year) <= 1997) + { + if (tot_cong.sign() > 0) res_debt += tot_cong; + if (tot_cong.sign() < 0) res_cred -= tot_cong; + } + else // Nuovo prorata dal 1998, congloba il conguaglio pro-rata nell'IVA ammessa in detrazione + { + if (tot_cong.sign() > 0) + { + iva_acq -= tot_cong; + res_cred -= tot_cong; + } + else + { + iva_acq += abs(tot_cong); + res_cred += abs(tot_cong); + } + } + + look_lim(month+deltam,TRUE); + // azzeriamo tutto (tranne r1, r5, s1, s0, s7) + const TString codtab = _lim->get("CODTAB"); + + real rimborso = _lim->get("R1"); + real r5 = _lim->get("R5"); // Rettifiche + real credito_utilizzato_f24 = _lim->get("R16"); + real r17 = _lim->get("R17"); + real r18 = _lim->get("R18"); + real r19 = _lim->get("R19"); + TString s7 = _lim->get("S7"); + const TString s0 = _lim->get("S0"); // Descrizioni rettifiche + const TString s1 = _lim->get("S1"); + const TString s2 = _lim->get("S2"); + const TString s3 = _lim->get("S3"); + bool wasb0 = _lim->get_bool("B0"); + bool wasb1 = _lim->get_bool("B1"); + + _lim->zero(); + _lim->put("CODTAB", codtab); + _lim->put("R1", rimborso); + _lim->put("R5", r5); // Rettifiche + _lim->put("R16", credito_utilizzato_f24); + _lim->put("R17", r17); + _lim->put("R18", r18); + _lim->put("R19", r19); + _lim->put("S0", s0); + _lim->put("S1", s1); + _lim->put("S2", s2); + _lim->put("S3", s3); + _lim->put("S7", s7); + _lim->put("B0", wasb0 ? "X" : ""); + _lim->put("B1", wasb1 ? "X" : ""); + + + /* + * versamenti effettuati: si conteggiano in R0, + * sono > 0 solo se andavano calcolati (vedi sopra) + */ + risultato -= versamenti + vers_int; + /* + * acconto dicembre se previsto + */ + if (((month+deltam) >= 12 && _freqviva == "M") || // solo freq. M puo' essere differita + (month == 12 && _isbenzinaro && _gest4) || + (month >= (atoi(_year) >= 2000 ? 12 : 13) && _freqviva == "T")) + { + acc_dec = versamenti_IVA(12,"7"); + risultato -= acc_dec; + res_cred += acc_dec; + } + + // in annuale si arrotondera' a 1000 in stampa + // se no il conguaglio prorata fa casino + round_al_centesimo(risultato); + + _lim->put("R0",risultato); + _lim->put("R2",cred_cost); + _lim->put("R3",deb_mens); + _lim->put("R4",perc_r); + + if (almeno_una_normale) + _lim->put("R6",detrazioni); + else // per evitare sbagli nei ricalcoli esterni + _lim->put("R6",""); + + if (month == 13 || differita) + { + // Arrotonda totali in quanto possono avere un arrotondamento diverso dal mensile + TIva_round ir; + ir.round(rimborsi); + ir.round(rettifiche); + ir.round(variazioni_imposta); + ir.round(imposta_non_versata); + ir.round(crediti_speciali); + + // scrivi totali rettifiche e rimborsi + // nella finale oppure se copiati da + // anno precedente + _lim->put("R1",rimborsi); + _lim->put("R5",rettifiche); + + _lim->put("R17", variazioni_imposta); + _lim->put("R18", imposta_non_versata); + _lim->put("R19", crediti_speciali); + } + + /* + * Interessi dovuti solo da trimestrali in periodica, + * Era anche non benzinari, smentito da Cinzia (MI0853); + * Ora gestito esplicitamente nei parametri ditta, forzato + * FALSE per ditte mensili e liq. annuale + * Ulteriormente smentito e rettificato + */ + bool true_trim = (month == 3 || month == 6 || month == 9); + if (_isintr && ( true_trim || (month == 12 && _isbenzinaro && _gest4) + || (month == 13 && !(_isbenzinaro && _gest4)) )) + { + const real interesse = interesse_trimestrale(_month); + const real r = risultato - imposta_non_versata; // CM 26-09-2000 + real ivi = r.sign() > 0 ? (r * interesse / CENTO) : ZERO; + round_al_centesimo(ivi); + _lim->put("R14", ivi); + } + + //questo serve anche per la visualizzazione e per l'estrazione deleghe + if (_isintr && ( true_trim || (month == 12 && _isbenzinaro && _gest4) + || (month == 13 && !(_isbenzinaro && _gest4)) )) + _lim->put("R10",interesse_trimestrale(_month)); + + _lim->put("R7", tot_cong); // totale conguaglio su tutte le attivita' + _lim->put("R8", versamenti); + _lim->put("R9", vers_int); + _lim->put("R11", acc_dec); + _lim->put("R12", res_cred); + _lim->put("R13", res_debt); + + _lim->put("R26", fdiff_imp); // Imponibile IVA diff Fatture di vendita + _lim->put("R27", fdiff_iva); // Imposta IVA diff Fatture di vendita + _lim->put("R28", fdiffinc_imp); // Imponibile IVA diff Incassi + _lim->put("R29", fdiffinc_iva); // Imposta IVA diff Incassi + + _lim->put("R30", fdiff_imp_acq); // Imponibile IVA diff Fatture di acquisto + _lim->put("R31", fdiff_iva_acq); // Imposta IVA diff Fatture di acquisto + _lim->put("R32", fdiffinc_imp_acq); // Imponibile IVA diff Pagamenti + _lim->put("R33", fdiffinc_iva_acq); // Imposta IVA diff Pagamenti + + _lam->put("R0", iva_vend); + _lam->put("R1", iva_acq); + _lam->put("R2", cred_prec); + _lam->put("R3", debt_precd); + + if (!_recalc_only && _recalc != never) + { + _lim->put("B0","X"); + // Dal 1998 questo flag vale solo per i progressivi dei registri IVA, visto che sono separati + // pertanto il suo settaggio andra' fatto solo in fase di ricalcolo (_recalc_only) + if (atoi(_year) < 1998) + _lim->put("B1","X"); + } + + if (_recalc_only) + _lim->put("B1", "X"); + +/* Cazzata galattica: vedi sopra + if (wasdifferita) _isdifferita = TRUE; +*/ + + // AWFUL! se siamo in annuale, occorre arrotondare alle 1000 lire + // schiaffo tutto il codice nell'IF che segue, e se lo si vuole togliere + // non si fara' che scancellare tutto il blocco + const bool after2000 = atoi(_year) >= 2000; // CM000222 + if (month == 13 || after2000) + { + // molto codice duplicato, ma ne val la pena (vedi sopra) + round_imposta(iva_vend); + round_imposta(iva_acq); + round_imposta(detrazioni); + round_imposta(versamenti); + round_imposta(vers_int); + round_imposta(rimborsi); + round_imposta(rettifiche); + round_imposta(deb_mens); + round_imposta(cred_prec); + round_imposta(cred_trasf); + round_imposta(tot_cong); + round_imposta(acc_dec); + round_imposta(debt_precd); + round_imposta(variazioni_imposta); + round_imposta(imposta_non_versata); + round_imposta(credito_utilizzato_iva); + round_imposta(crediti_speciali); + round_imposta(fdiff_iva); + round_imposta(fdiffinc_iva); + round_imposta(fdiff_iva_acq); + round_imposta(fdiffinc_iva_acq); + + res_cred = iva_acq + versamenti + vers_int + cred_prec - cred_trasf + acc_dec + credito_utilizzato_iva + crediti_speciali + fdiffinc_iva_acq; + res_debt = iva_vend + rimborsi + debt_precd + imposta_non_versata + fdiffinc_iva; + + if (atoi(_year) < 1998) // Non vale piu' dal 1998 in poi; iva_acq e' gia' ok e cosi' res_cred. + { + if (tot_cong.sign() > 0) + res_debt += tot_cong; + else + res_cred -= tot_cong; + } + + if (rettifiche.sign() >= 0) + res_debt += rettifiche; + else + res_cred -= rettifiche; + + if (variazioni_imposta.sign() >= 0) + res_debt += variazioni_imposta; + else + res_cred -= variazioni_imposta; + + if (almeno_una_normale) + res_cred += detrazioni; + + risultato = res_debt - res_cred; + + // tut ricalcule', riscrivem + _lam->put("R0", iva_vend); + _lam->put("R1", iva_acq); + _lam->put("R2", cred_prec); + _lam->put("R3", debt_precd); + _lam->put("R4", cred_trasf); + + _lim->put("R0", risultato); + _lim->put("R1", rimborsi); + _lim->put("R2", cred_cost); + _lim->put("R3", deb_mens); + _lim->put("R5", rettifiche); + _lim->put("R6", detrazioni); + _lim->put("R7", tot_cong); + _lim->put("R8", versamenti); + _lim->put("R9", vers_int); + _lim->put("R11", acc_dec); + _lim->put("R12", res_cred); + _lim->put("R13", res_debt); + if (after2000) // Anche R14 + { + real ivi = _lim->get("R14"); + round_imposta(ivi); + _lim->put("R14", ivi); + } + _lim->put("R15", credito_utilizzato_iva); + + if (month == 13) + { + // Riporta eventuale credito nella tabella parametri liquidazione dell'anno successivo + const real riporto = credito_utilizzabile - risultato; + if (riporto >= ZERO) // Risultato a credito + { + const int anno_prossimo = atoi(_year) + 1; + if (look_lia(0l, TRUE, anno_prossimo)) + { + _lia->put("R0", riporto); + _lia->rewrite(); + } + } + } + _lim->put("R26", fdiff_imp); + _lim->put("R27", fdiff_iva); + _lim->put("R28", fdiffinc_imp); + _lim->put("R29", fdiffinc_iva); + _lim->put("R30", fdiff_imp_acq); + _lim->put("R31", fdiff_iva_acq); + _lim->put("R32", fdiffinc_imp_acq); + _lim->put("R33", fdiffinc_iva_acq); + // ciapa + } + + _lim->rewrite(); + _lam->rewrite(); +} + +void TLiquidazione_app::recalc_annual(const char* att) +{ + // viene chiamata 2 volte per le att. miste; PLA e' stata + // azzerata dove serve da update_firm se siamo all'annuale + real es_c1 = 0.0; + real es_c2 = 0.0; + real es_c3 = 0.0; + real es_c1a = 0.0; + real es_c3_am = 0.0; + real es_c1a_am= 0.0; + real cess_amm = 0.0; + real vendite = 0.0; + real pro_pag = 0.0; + real iva_acq = 0.0; + real ven_lrd = 0.0; + real volaff1 = 0.0; + real volaff2 = 0.0; + real imp_ifs = 0.0; + real imp_af = 0.0; + real imp_va7 = 0.0; + TString16 codiva,reg,tiva; + TToken_string va7("",'!'); + + const TString8 aaa(att); + const int tipoatt = aaa.right(1)[0] - '0'; + CHECKD(tipoatt > 0, "Tipo att non valido ", tipoatt); + + look_pla(aaa); + volaff1 = _pla->get_real("R14"); + volaff2 = (const char*)_pla->get("S1"); + vendite = _pla->get_real("R0"); + es_c1 = _pla->get_real("R1"); + es_c2 = _pla->get_real("R2"); + es_c3 = _pla->get_real("R3"); + es_c1a = _pla->get_real("R15"); + es_c3_am = _pla->get_real("R16"); + es_c1a_am= _pla->get_real("R17"); + cess_amm = _pla->get_real("R4"); + pro_pag = _pla->get_real("R12"); + iva_acq = _pla->get_real("R11"); + + // Per avere un risultato corretto, si deve totalizzare PIM->R3 per codice iva + // e quindi effettuare lo scorporo dell'imponibile e aggiungere al relativo volume d'affari + TAssoc_array corr_ann; + _CorrItem *cc; + bool is_key; + + for (_pim->first(); !_pim->eof(); _pim->next()) + { + if (_year != *_pim_anno || _month != atoi(*_pim_mese)) // In questo caso _month vale sempre 13 + continue; + + int tipocr = atoi(*_pim_tipocr); + codiva = *_pim_codiva; + reg = *_pim_codreg; + look_iva(codiva); look_reg(reg); + tiva = _iva->get("S1"); + const bool b3 = _iva->get("S2") == "B3" && atoi(_year) < 1998; + tiporeg treg = (tiporeg)_reg->get_long("I0"); + const bool corrisp = _reg->get_bool("B0"); + const tipo_sospensione simp = _reg->get_bool("B1") ? (tipo_sospensione) _reg->get_int("I9") : nessuna; + imp_ifs = _pim->get_real("R0"); + imp_af = _pim->get_real("R7"); + va7 = _pim->get("S0"); + imp_va7 = va7.get(0); + imp_ifs -= imp_af + imp_va7; // Toglie le autofatture e le op. VA7 + + if ( // ESCLUSI: + strcmp(att,*_pim_codatt) != 0 || + treg != vendita || // non vendite + tipocr == 4 || // cessioni beni ammortizzabili + tiva == "NS" || // non soggetti + b3 || // bi tre + simp == normale || + simp == liquidazione) // sospensione di imposta + continue; + + if (corrisp) + { + imp_ifs += _pim->get_real("R5"); // Se corrisp. aggiunge le FS + is_key = corr_ann.is_key(codiva); + if (!is_key) // Aggiunge se non esiste + corr_ann.add(codiva, (_CorrItem*) new _CorrItem,is_key); + _CorrItem& ca = (_CorrItem&) corr_ann[codiva]; + ca._totale += _pim->get_real("R3"); + if (!is_key) // se non c'e' lo aggiunge + ca._aliquota = _iva->get_real("R0")/CENTO; // Se è nuovo setta l'aliquota + } + if (tipoatt == 1) + volaff1 += imp_ifs; + else + volaff2 += imp_ifs; + } + + // Ora si scorporano i corrispettivi raggruppati per codice IVA e si totalizzano gli imponibili + // calcolati, da aggiungere poi a volaff1 o volaff2 + real imp,iva; + for (cc = (_CorrItem *)corr_ann.first_item(); cc != NULL; cc = (_CorrItem *)corr_ann.succ_item()) + { + lordo2netto(cc->_totale,imp,iva,cc->_aliquota); + if (tipoatt == 1) + volaff1 += imp; + else + volaff2 += imp; + } + + // calcola esenti, cessioni, lordo vendite, prorata pagato + // e IVA acquisti dai plm/ptm/pum/pam/pom + + for (int i = 1; i <= 12; i++) + { + if (!look_plm(i,aaa)) + continue; + + pro_pag += _plm->get_real("R2"); // Can't do anyway else... + } + + // Modifiche per PIM13 + if (look_plm(13,aaa)) + { + vendite += _pam->get_real("R1"); + iva_acq += _plm->get_real("R1"); + es_c1 += _pum->get_real("R4"); + es_c2 += _pum->get_real("R5"); + es_c3 += _pum->get_real("R6"); + es_c1a += _pum->get_real("R13"); + es_c3_am += _pum->get_real("S4"); + es_c1a_am += _pum->get_real("S5"); + cess_amm += _pum->get_real("R0"); + + // Nota: + // l'iva acquisti del mese 13 memorizzata in PLM->R1, e' si' la sommatoria + // di tutte le imposte relative agli acquisti di tutti i mesi, ma andrebbe sottratto il + // totale prorata pagato (pro_pag), reperibile dai mesi precedenti (PLM->R2) + //real iv = _plm->get_real("R1"); // Iva acquisti annuale + //iv -= pro_pag; + iva_acq -= pro_pag; // Si sottraggano i prorata pagati + //_plm->put("R1",iva_acq); // Si riscriva il tutto + //_plm->rewrite(); + } + + _pla->put("R0", vendite); + _pla->put("R1", es_c1); + _pla->put("R2", es_c2); + _pla->put("R3", es_c3); + _pla->put("R15", es_c1a); + _pla->put("R16", es_c3_am); + _pla->put("R17", es_c1a_am); + _pla->put("R4", cess_amm); + _pla->put("R11", iva_acq); + _pla->put("R12", pro_pag); + _pla->put("R14", volaff1); + _pla->put("S1", volaff2.string()); + _pla->rewrite(); +} diff --git a/cg/cg4400.cpp b/cg/cg4400.cpp index a7a4bbe06..33bec5e5a 100755 --- a/cg/cg4400.cpp +++ b/cg/cg4400.cpp @@ -1142,7 +1142,7 @@ int TStampa_registri_app::setta_riga(int r, const TRigaiva& riga, real& tot1, if (!riga._impostap.is_zero()) set_row(r, "@98g%r", &riga._impostap); } - r++; // Incrementa sempre: cazzone! + r++; // Incrementa sempre! tot1 += riga._imponibile; tot2 += riga._imposta; diff --git a/cg/cglib03.cpp b/cg/cglib03.cpp index 785d058a7..07e77f1fe 100755 --- a/cg/cglib03.cpp +++ b/cg/cglib03.cpp @@ -63,7 +63,6 @@ real indetraibile_al(const TString& codind, const TCausale& caus, int annodoc, i int analizza_IVA(const real& imptot, const real& ivatot, const real perc_ind, const bool corrispettivo, const bool iva_ind_al_costo, const TString& codiva, real& imp_det, real& iva_det, real& imp_ind, real& iva_ind) - { int flag = 0; diff --git a/cg/cgmenu.men b/cg/cgmenu.men index 1406c7ceb..547343b89 100755 --- a/cg/cgmenu.men +++ b/cg/cgmenu.men @@ -100,7 +100,7 @@ Item_09 = "Tracciabilit [CGMENU_010] Caption = "IVA" Picture = -Module = 5 +Module = 7 Flags = "" Item_01 = "Parametri liquidazione", "cg5 -3", "F" Item_02 = "Liquidazione", "cg4 -2", "F" @@ -287,7 +287,5 @@ Module = 7 Flags = "" Item_01 = "Comunicazione annuale dati IVA", "cg5 -7", "F" Item_02 = "Quadro VT", "cg5 -8", "F" -Item_03 = "Elenco clienti\fornitori", "cg3 -8", "F" -Item_04 = "Comunicazione dati rilevanti", "fe0 -0", "F" -Item_05 = "Contratti per dati rilevanti", "fe0 -1 &CON", "F" -Item_06 = "Controllo archivi dati rilevanti", "fe0 -3", "F" +Item_03 = "Elenco clienti/fornitori", "cg3 -8", "F" +Item_04 = "Comunicazione Polivalente", "fe0 -0", "F" diff --git a/cg/cgsaldac.cpp b/cg/cgsaldac.cpp index d683b18f1..4583c206f 100755 --- a/cg/cgsaldac.cpp +++ b/cg/cgsaldac.cpp @@ -792,11 +792,22 @@ int TRiga_partite::ultima_ratapagata() const // Certified 100% tipo_movimento TRiga_partite::tipo() const { - const tipo_movimento tm = (tipo_movimento)(get_char(PART_TIPOMOV)-'0'); + tipo_movimento tm = (tipo_movimento)(get_int(PART_TIPOMOV)); + + if (tm == tm_nessuno) + { + // Tento salvataggioin extremis + const int nriga = get_int(PART_NRIGA); + if (nriga == 1 && get_date(PART_DATADOC).ok()) + tm = tm_fattura; else + if (nriga > 1 && get_date(PART_DATAPAG).ok()) + tm = tm_pagamento; #ifdef DBG - if (tm == tm_nessuno && get_int(PART_NRIGA) != TPartita::UNASSIGNED) - yesnofatal_box("Riga partite con un tipo losco."); + if (tm == tm_nessuno && nriga != TPartita::UNASSIGNED) + yesnofatal_box("Riga partite con un tipo losco."); #endif + } + return tm; } @@ -996,8 +1007,8 @@ TImporto TRiga_partite::importo(bool valuta, int mode) const return i; } -// Calcola l'esposto e l' esposto SBF -TImporto TRiga_partite::esposto(bool valuta, const TDate & data_scad, const TDate & data_rischio, bool & sbf) const +// Calcola l'esposto e l'esposto SBF +TImporto TRiga_partite::esposto(bool valuta, const TDate& data_scad, const TDate& data_rischio, bool & sbf) const { TImporto esposto; diff --git a/cg/f22.dir b/cg/f22.dir index dff3e57f4..ae05e4c8b 100755 --- a/cg/f22.dir +++ b/cg/f22.dir @@ -1,3 +1,3 @@ 22 0 -$alleg|0|0|112|0|Comunicazione dati rilevanti||| +$alleg|0|0|137|0|Comunicazione Spesometro||| diff --git a/cg/f22.trr b/cg/f22.trr index 9501d9db9..4a73c95a4 100755 --- a/cg/f22.trr +++ b/cg/f22.trr @@ -1,5 +1,5 @@ 22 -16 +27 ANNO|2|4|0|Anno IVA PROGR|3|7|0|Progressivo TIPOCF|1|1|0|Tipo liente ornitore @@ -8,14 +8,25 @@ OCFPI|1|16|0|Codice occasionale FORZATURA|8|1|0|Forzatura a mano IGNORA|2|2|0|Eventuale motivo per ignorare la riga (0 = da inviare) DATAREG|5|8|0|Data registrazione (art. 6 d.P.R.n.633/1972) -NUMDOC|1|7|0|Numero Fattura (Obbligatorio se TIPOPE=1) -MODPAG|2|1|0|Modalità di pagamento (1=Non frazionato;2=Frazionato;3=Periodico) +NUMDOC|1|7|0|Numero Fattura +DATADOC|5|8|0|Data fattura IMPORTO|4|12|2|Importo dovuto IMPOSTA|4|12|2|Imposta -TIPOPE|2|1|0|Tipo operazione (1=Cessione;2=Acquisto) -CONTRATTO|1|18|0|Contratto -DATARETT|5|8|0|Data documento rettificato -NUMRETT|1|7|0|Numero documento rettificato +TIPOPE|1|2|0|Tipo operazione +MODPAG|2|1|0|Modalità di pagamento (1=Non frazionato;2=Frazionato;3=Periodico) (fino 2012) +CONTRATTO|1|18|0|Contratto (fino 2012) +DATARETT|5|8|0|Data documento rettificato (fino 2012) +NUMRETT|1|7|0|Numero documento rettificato (fino 2012) +NOLEGGIO|1|1|0|Tipo veicolo (A=Auto,B=Caravan;C=Altri;D=Diporto;E=Aeromobili) (dal 2013) +REVERSE|8|1|0|Reverse charge (dal 2013) +AUTOFATT|8|1|0|Autofattura (dal 2013) +SERVIZI|8|1|0|Acquisto servizi (dal 2013) +TURISMO|8|1|0|Operazione legate al turismo (dal 2013) +RIEPILOG|8|1|0|Fattura riepilogativa (dal 2013) +CARBURAN|8|1|0|Carta Carburanti (dal 2013) +ATTAGG|2|4|0|Operazioni attive aggregate (dal 2013) +PASAGG|2|4|0|Operazioni passive aggregate (dal 2013) +TIPOIVA|2|1|0|Tipo IVA (dal 2013) 3 ANNO+PROGR| ANNO+TIPOCF+CODCF+OCFPI+PROGR| diff --git a/cg/f23.dir b/cg/f23.dir index e146620f8..22191435b 100755 --- a/cg/f23.dir +++ b/cg/f23.dir @@ -1,3 +1,3 @@ -23 -0 -$mov|0|0|471|0|Movimenti di prima nota||| +23 +0 +$mov|0|0|442|0|Movimenti di prima nota||| diff --git a/cg/f23.trr b/cg/f23.trr index fe65c73a1..1cf8619c5 100755 --- a/cg/f23.trr +++ b/cg/f23.trr @@ -1,5 +1,5 @@ 23 -55 +52 ANNOES|9|4|0|Codice esercizio NUMREG|3|7|0|Numero di operazione DATAREG|5|8|0|Data operazione @@ -49,11 +49,8 @@ DNDOC|3|7|0|Documento originale NDOC DATAINC|5|8|0|Data di incasso per liquidazione differita LIQDIFF|8|1|0|Flag di movimento a liquidazione differita IVAXCASSA|8|1|0|Flag di movimento IVA per cassa -MODPAG|2|1|0|Modalità di pagamento 1=Non frazionato; 2=Frazionato; 3=Periodico -CONTRATTO|1|18|0|Contratto con importo > 3000 Euro -DATARETT|5|8|0|Data documento rettificato -NUMRETT|1|7|0|Numero documento rettificato -ANNOFE|2|4|0|Anno invio definitivo dati rilevanti +NOLEGGIO|1|1|0|Tipo veicolo Auto, Camper, Altro, Diporto, aEreo +INVIOFE|5|8|0|Data invio definitivo dati rilevanti CONTSEP|1|6|0|Contabilità separata 4 NUMREG|