campo-sirio/cg/cg2102.cpp

2091 lines
58 KiB
C++
Raw Normal View History

#include <progind.h>
#include <tabutil.h>
#include <urldefid.h>
#include <utility.h>
#include "cg2100.h"
#include "cg2102.h"
#include "cg21sld.h"
#include <mov.h>
#include <clifo.h>
///////////////////////////////////////////////////////////
// Funzioni di decodifica/calcolo
///////////////////////////////////////////////////////////
char TPrimanota_application::row_type(const TToken_string& s)
{
const int l = s.len()-1;
return l > 0 ? s[l] : ' ';
}
// Determina il tipo IVA da causale+anno
// Certified 100%
TipoIVA TPrimanota_application::cau2IVA(const char* causale, int annoiva)
{
TipoIVA i = nessuna_iva;
if (*causale > ' ')
{
TCausale& c = app().causale();
if (c.read(causale, annoiva))
i = c.iva();
else
{
error_box("Causale errata: '%s'", causale);
i = iva_errata;
}
}
return i;
}
// Calcolo della percentuale di un dato codice IVA
// Certified 99%
const real& TPrimanota_application::cod2IVA(const TMask& m)
{
static TString16 _codiva;
static real _percent;
if (app().iva() == iva_acquisti && m.get_int(103) == 3)
return ZERO;
const TString& codiva = m.get(102);
if (_codiva != codiva)
{
_codiva = codiva;
TCodiceIVA c(_codiva);
_percent = c.percentuale();
}
return _percent;
}
// Scorpora dall'imponibile la percentuale d'imposta(0.0%-100.0%) e ritorna l'imposta stessa
// Certified 99% Non sono sicurissimo degli imponibili negativi
real TPrimanota_application::scorpora(real& imponibile, const real& percent)
{
real imposta = abs(imponibile) * percent / (percent + 100.0); imposta.ceil();
if (imponibile.sign() < 0) imposta = -imposta;
imponibile -= imposta;
return imposta;
}
// Calcola il totale del documento tenenod conto del segno della prima riga e di quella delle
// ritenute sociali sulla causale
real TPrimanota_application::totale_documento()
{
const TMask& m = curr_mask();
const bool swapt = test_swap(FALSE); // Totale invertito ?
const bool swaps = test_swap(TRUE); // Ritenute sociali invertite ?
real tot(m.get(F_TOTALE)); // Legge totale
const real ritfis(m.get(F_RITFIS));
tot += ritfis; // Somma ritenute fiscali
real ritsoc(m.get(F_RITSOC));
if (swapt ^ swaps)
ritsoc = -ritsoc;
tot += ritsoc; // Somma ritenute sociali con segno
return tot;
}
// Determina se un codice sospeso o no
// Certified 99%
bool TPrimanota_application::suspended_handler(TMask_field& f, KEY k)
{
if (f.to_check(k))
{
const TEdit_field& c = (const TEdit_field&)f;
const TBrowse* b = c.browse();
CHECKD(b, "Can't check suspension of a edit-field without a USE ", f.dlg());
const TLocalisamfile& i = b->cursor()->file();
// Tabella File
const char* sf = i.tab() ? "B2" : "SOSPESO";
const bool suspended = i.get_bool(sf);
if (suspended)
{
sf = f.get();
return f.error_box("Il codice '%s' e' sospeso e non puo' essere utilizzato", sf);
}
}
return TRUE;
}
// Determina se un codice detrazione e' di tipo detraibile o no
// Certified 70%
bool TPrimanota_application::detraibile(TToken_string& row)
{
if (app().iva() == iva_vendite) // Vendite sempre detraibili
return TRUE;
const int tipo_det = row.get_int(2); // Leggi tipo detraibilita
if (tipo_det != 0)
return FALSE;
const real& prorata = app().causale().reg().prorata();
return prorata < 100.0; // Se prorata = 100% e' indetraibile
}
///////////////////////////////////////////////////////////
// Funzioni di ricerca
///////////////////////////////////////////////////////////
int TPrimanota_application::type2pos(char tipo)
{
TString_array& cg = app().cgs().rows_array();
for (int i = 0; i < cg.items(); i++)
{
const TToken_string& s = cg.row(i);
const char t = row_type(s);
if (t == tipo)
return i;
}
return -1;
}
// Trova nelle righe contabili un conto nelle righe di tipo prescelto
int TPrimanota_application::bill2pos(const TBill& conto, char tipo)
{
TString_array& cg = app().cgs().rows_array();
for (int i = 0; i < cg.items(); i++)
{
TToken_string& s = cg.row(i);
const char t = row_type(s);
if (t == tipo)
{
const TBill c(s, 3, 0x0);
if (c == conto)
return i;
}
}
return -1;
}
// Trova nelle righe contabili un conto di contropartita per il conto dato
int TPrimanota_application::bill2contr(const TBill& conto, char sezione) const
{
TString_array& rows = cgs().rows_array();
for (int i = 0; i < rows.items(); i++)
{
TToken_string& r = rows.row(i);
const real dare(r.get(0));
const char sez = dare.is_zero() ? 'A' : 'D';
if (sez == sezione) // Devo cercare sezione contraria
continue;
const TBill c(r, 3, 0x0);
if (conto == c)
return i;
}
return -1;
}
// Controlla se un conto e' usato nelle righe IVA
int TPrimanota_application::bill_used(const TBill& conto) const
{
int users = 0;
const TArray& rows = ivas().rows_array();
for (int i = 0; i < rows.items(); i++)
{
TToken_string& row = (TToken_string&)rows[i];
if (!row.empty_items())
{
const TBill c(row, 6, 0x0);
if (conto == c) users++;
}
}
return users;
}
///////////////////////////////////////////////////////////
// Gestione sheet CG
///////////////////////////////////////////////////////////
TSheet_field& TPrimanota_application::cgs() const
{
TSheet_field& s = (TSheet_field&)curr_mask().field(F_SHEETCG);
return s;
}
// Certified 100%
// Scrive l'importo imp nella opportuna colonna della riga n
void TPrimanota_application::set_cgs_imp(int n, const TImporto& imp)
{
TSheet_field& s = cgs();
imp.add_to(s.row(n), 0);
s.force_update(n);
/*
TMask& m = s.sheet_mask();
if (m.is_running() && s.selected() == n)
{
m.set(101, imp.sezione() == 'D' ? imp.valore().string() : "");
m.set(102, imp.sezione() == 'A' ? imp.valore().string() : "");
}
*/
}
// Legge l'importo della riga n e lo ritorna col segno dovuto
// Certified 100%
TImporto TPrimanota_application::get_cgs_imp(int n)
{
TSheet_field& s = cgs();
TImporto importo;
const TMask& m = s.sheet_mask();
if (m.is_running() && s.selected() == n)
{
const TString& imp = m.get(101);
if (imp.not_empty())
importo.set('D', real(imp));
else
importo.set('A', real(m.get(102)));
}
else
importo = s.row(n);
return importo;
}
// Certified 90%
bool TPrimanota_application::add_cgs_imp(int n, const TImporto& imp)
{
TImporto tot(get_cgs_imp(n));
tot += imp;
tot.normalize();
set_cgs_imp(n, tot);
return tot.is_zero();
}
// Certified 90%
bool TPrimanota_application::sub_cgs_imp(int n, const TImporto& imp)
{
TImporto tot(get_cgs_imp(n));
tot -= imp;
tot.normalize();
set_cgs_imp(n, tot);
return tot.is_zero();
}
TImporto TPrimanota_application::real2imp(const real& r, char row_type)
{
bool dare;
if (row_type == 'S')
{
dare = causale().sezione_ritsoc() == 'D';
}
else
{
dare = causale().sezione_clifo() == 'D';
if (row_type != 'T' && row_type != 'F') dare = !dare;
}
TImporto importo(dare ? 'D' : 'A', r);
return importo;
}
// Disabilita le celle della riga contabile n in base al suo tipo
void TPrimanota_application::disable_cgs_cells(int n, char tipo)
{
TSheet_field& cg = cgs();
int first = 0, last = 0;
switch(tipo)
{
case 'A': // Abbuoni attivi
case 'C': // Differenza cambio
case 'D': // IVA Detraibile
case 'F': // Ritenute Fiscali
case 'L': // Contropartita delle spese
case 'N': // IVA Non detraibile
case 'P': // Abbuoni passsivi
case 'R': // Ritenute professionali
case 'S': // Ritenute Sociali
case 'T': // Totale documento
last = 3;
if (!causale().corrispettivi())
{
cg.disable_cell(n, 5);
cg.disable_cell(n, 6);
}
break;
case 'K': // Riga cliente/fornitore per saldaconto
if (n == cg.items()-1)
break;
first = 2;
case 'I':
last = 7; // Imponibile
break;
default:
last = 0; // Solo contabile
break;
}
for (int i = first; i < last; i++)
cg.disable_cell(n, i);
}
void TPrimanota_application::reset_sheet_row(TSheet_field& s, int n)
{
s.row(s.items()); // Append a new line
s.destroy(n); // Remove line n
}
int TPrimanota_application::set_cgs_row(int n, const TImporto& imp,
TBill& conto, const char* desc,
char tipo)
{
TSheet_field& cg = cgs();
if (n < 0) n = cg.first_empty();
TToken_string& row = cg.row(n);
row = "";
imp.add_to(row, 0);
row.add(conto.string(0x3));
row.add(""); // Codice decrizione
row.add(desc); // Descrizione aggiuntiva
if (tipo == 'T') // Calcolo contropartita
{
TToken_string& irow = ivas().row(0);
for (int i = 5; i < 10; i++)
row.add(irow.get(i == 5 ? 5 : -1));
}
else
{
const int pos = type2pos('T');
if (pos >= 0)
{
TBill contro(cg.row(pos), 2, 0x3);
row.add(contro.string(0x3));
}
else row.add(" | | | | ");
}
row << "| |" << tipo;
disable_cgs_cells(n, tipo);
cg.force_update(n);
return n;
}
HIDDEN int compare_rows(const TObject** o1, const TObject** o2)
{
// Totale, Rit.Fisc., Rit.Soc., da riga IVA, riga contabile, IVA detr., IVA non detr.
const char* const sort_order = "TFSI DNAPRC";
const TToken_string* r1 = (const TToken_string*)*o1;
const TToken_string* r2 = (const TToken_string*)*o2;
const char c1 = app().row_type(*r1);
const char c2 = app().row_type(*r2);
return int(strchr(sort_order, c1) - strchr(sort_order, c2));
}
HIDDEN bool can_remove(TToken_string& s)
{
const char* dare = s.get(0);
bool yes = dare == NULL ;
if (!yes)
{
if (*dare == '\0' || *dare == ' ' || strcmp(dare,"0") == 0)
{
const char* avere = s.get();
yes = (avere == NULL || *avere == '\0' || *avere == ' ' || strcmp(avere,"0") == 0);
}
}
return yes;
}
void TPrimanota_application::cgs_pack()
{
TString_array& rows = cgs().rows_array();
const bool pagamento = is_pagamento();
const long numreg = curr_mask().get_long(F_NUMREG);
for (int i = rows.items()-1; i >= 0; i--)
{
TToken_string& r = rows.row(i);
if (can_remove(r))
{
bool ok = TRUE;
if (pagamento && row_type(r) == 'K')
{
ok = !partite().utilizzata(numreg, i+1);
if (ok)
cg_notify(cgs(), i, K_DEL);
}
if (ok)
rows.destroy(i, TRUE);
}
}
if (!pagamento) // Il pagamento e' gia' ordinato
rows.sort(compare_rows); // Pack and sort array
}
bool TPrimanota_application::ci_sono_importi() const
{
TString_array& rows = cgs().rows_array();
for (int i = 0; i < rows.items(); i++)
{
TToken_string& r = rows.row(i);
const real dare(r.get(0));
if (dare != ZERO) return TRUE;
const real avere(r.get());
if (avere != ZERO) return TRUE;
}
return FALSE;
}
real TPrimanota_application::calcola_saldo() const
{
TString_array& rows = cgs().rows_array();
const int max = rows.items();
TImporto saldaconto;
const bool pag = is_pagamento();
real tdare, tavere;
for (int i = 0; i < max; i++)
{
TToken_string& r = rows.row(i);
const real dare(r.get(0));
const real avere(r.get());
tdare += dare;
tavere += avere;
if (pag)
{
const char tipo = row_type(r);
if (strchr("ACGKP", tipo) != NULL)
{
const char sez = dare.is_zero() ? 'A' : 'D';
const TImporto importo(sez, sez == 'A' ? avere : dare);
saldaconto += importo;
}
}
}
TMask& m = curr_mask();
const real sbilancio = abs(tdare)-abs(tavere);
const real absbilancio = abs(tdare - tavere);
switch (sbilancio.sign())
{
case +1: // Il dare supera l'avere in valore assoluto
m.set(F_DARE, absbilancio.string());
m.reset(F_AVERE);
break;
case -1: // L'avere supera il dare in valore assoluto
m.reset(F_DARE);
m.set(F_AVERE, absbilancio.string());
break;
default: // Sbilancio nullo
m.reset(F_DARE);
m.reset(F_AVERE);
break;
}
if (pag)
{
real t(m.get(F_TOTALE));
t -= saldaconto.normalize().valore();
m.set(K_RESIDUO, t.string());
}
return sbilancio;
}
// Handler dello sheet di contabilita'
// Certified 90%
bool TPrimanota_application::cg_handler(TMask_field& f, KEY k)
{
if (k == K_ENTER)
{
const real saldo = app().calcola_saldo();
if (saldo != ZERO)
{
const char* ss = saldo.string(".");
return f.error_box("Il movimento e' sbilanciato di %s lire.", ss);
}
const long numreg = f.mask().get_long(F_NUMREG);
const bool paga = app().is_pagamento();
const bool nota = app().is_saldaconto() && app().causale().tipomov() == 2;
TImporto saldaconto;
TSheet_field& cg = app().cgs();
bool empty = TRUE;
for (int i = 0; i < cg.items(); i++)
{
TToken_string& r = cg.row(i);
TImporto importo; importo = r;
if (!importo.is_zero())
{
const TBill c(r, 3, 0x0);
if (!c.ok())
return f.error_box("Il conto della riga %d non e' completo", i+1);
const TBill co(r, 10, 0x0);
if (!co.empty() && !co.ok())
return f.error_box("La contropartita della riga %d non e' completa", i+1);
empty = FALSE;
if (paga || nota)
{
const char tipo = row_type(r);
if (tipo == 'K' || tipo == 'T')
{
const int currig = i+1;
const TImporto speso = app().partite().importo_speso(numreg, currig);
if (importo != speso)
{
const char* ss = speso.valore().string(".");
return f.error_box("L'importo della riga %d deve essere %s %c",
currig, ss, speso.sezione());
}
}
if (strchr("ACGKP", tipo) != NULL)
saldaconto += importo;
}
}
}
if (paga)
{
const real totdoc(f.mask().get(F_TOTALE));
const real valore = saldaconto.normalize().valore();
if (totdoc != valore)
{
const TString16 td(totdoc.string("."));
const char* ss = valore.string(".");
return f.error_box("Il totale documento inserito e' %s\n"
"mentre i pagamenti ammontano a %s", (const char*)td, ss);
}
}
if (empty)
return f.error_box("Il movimento non ha nessuna riga contabile con un importo");
}
return TRUE;
}
void TPrimanota_application::generazione_righe_cg(int r)
{
begin_wait();
TSheet_field& cg = cgs();
TToken_string& row = cg.row(r);
if (can_remove(row)) // Ignora righe senza importo
return;
TImporto importo; importo = row;
const bool causale_ok = causale().codice()[0] > ' ';
if (r == 0 && cg.row(1).empty_items())
{
TBill contro(row, 9, 0x3); // Contropartita della prima riga
if (causale_ok && !contro.ok())
{
causale().bill(2, contro); // Prendi contropartita dalla causale
if (contro.ok())
{
contro.add_to(row, 9, 0x3);
cg.force_update(r);
}
}
if (contro.ok())
{
importo.swap_section();
set_cgs_row(1, importo, contro, "", ' ');
TBill conto(row, 2, 0x3);
conto.add_to(cg.row(1), 9, 0x3);
cg.force_update(1);
}
}
if (causale_ok)
{
int first_not_empty = 0;
for (int i = 0; i < r; i++)
{
TToken_string& row = cg.row(i);
if (!can_remove(row))
{
first_not_empty = i;
break;
}
}
TBill conto(row, 2, 0x3);
if (first_not_empty == r) // Sono la prima riga con importo ?
{
int last = -1;
for (i = r+1; i < cg.items(); i++) // Aggiorna tutte le altre contropartite
{
TToken_string& rowi = cg.row(i);
int gruppo = rowi.get_int(3);
if (gruppo != 0) // Considera righe con conto ...
{
gruppo = rowi.get_int(10);
if (gruppo == 0) // ... e senza contropartita
{
char sez = ' '; // Calcola sezione D/A della riga i
if (cg.cell_disabled(i,0)) sez = 'A'; else
if (cg.cell_disabled(i,1)) sez = 'D';
if (sez != ' ' && importo.sezione() != sez) // Considera solo le sezioni opposte
{
conto.add_to(rowi, 9, 0x3);
cg.force_update(i);
if (last < 0) last = i;
else last = 0;
}
}
}
}
if (last > r)
{
importo.swap_section();
set_cgs_imp(last, importo);
const int gruppo = row.get_int(10);
if (gruppo == 0) // Se non ho contropartita ...
{
TBill contro(cg.row(last), 2, 0x3); // ... copiala dalla riga corrispondente
contro.add_to(row, 9, 0x3);
cg.force_update(r);
}
}
}
else
{
TToken_string& first = cg.row(first_not_empty);
int gruppo = first.get_int(10);
if (gruppo == 0) // Se la prima riga non ha contropartita ...
{
conto.add_to(first, 9, 0x3); // ... copiaci la mia partita
cg.force_update(first_not_empty);
}
gruppo = row.get_int(10);
if (gruppo == 0) // Se non ho contropartita ...
{
TBill contro(first, 2, 0x3); // ... copiala dalla prima riga
contro.add_to(row, 9, 0x3);
cg.force_update(r);
}
}
}
end_wait();
}
bool TPrimanota_application::cg_notify(TSheet_field& cg, int r, KEY k)
{
static TImporto old_spesa;
CHECKD(r >= 0, "Chi e' quel ca$$one che notifica la riga ", r);
TToken_string& row = cg.row(r);
const char tipo = row_type(row); // Tipo della riga in esame
switch(k)
{
case K_SPACE:
if (tipo == 'G')
old_spesa = row;
break;
case K_TAB:
cg.sheet_mask().enable(DLG_DELREC, tipo <= ' ' || tipo == 'K' || tipo == 'G');
cg.sheet_mask().enable(100, tipo == 'K');
break;
case K_DEL:
if (tipo == 'G')
{
row.add("", 0);
row.add("", 1);
}
else
{
if (tipo == 'K')
app().notify_cgline_deletion(r+1);
break;
}
case K_ENTER:
if (tipo == 'G')
{
TImporto growth; growth = row; growth -= old_spesa;
if (!growth.is_zero())
{
const int s = type2pos('L');
if (s < 0)
{
TConto cassa; app().causale().bill(2, cassa);
const TString desc(app().causale().desc_agg(2));
growth.swap_section(); growth.normalize();
app().set_cgs_row(s, growth, cassa, desc, 'L');
}
else
app().sub_cgs_imp(s, growth);
app().calcola_saldo();
}
}
else
{
if (app().iva() == nessuna_iva && !app().is_saldaconto())
app().generazione_righe_cg(r);
}
app().calcola_saldo();
break;
case K_CTRL+K_INS: // Post inserimento
if (app().is_pagamento())
{
const char tipo = cg.mask().get(S_TIPORIGA)[0];
if (tipo == 'K' || tipo == 'G')
{
const int k = tipo == 'K' ? 1 : 10;
TBill conto; app().causale().bill(k, conto);
const TString80 desc(app().causale().desc_agg(k));
const char sez = app().causale().sezione(k);
const real imp(cg.mask().get(K_RESIDUO));
app().set_cgs_row(r, TImporto(sez, imp), conto, desc, tipo);
if (tipo == 'K')
{
for (int i = 0; i < r; i++)
{
const TToken_string& row = cg.row(i);
if (row_type(row) != 'K')
{
cg.swap_rows(r, i);
cg.force_update();
cg.select(i);
break;
}
}
}
}
}
break;
default:
break;
}
return TRUE;
}
bool TPrimanota_application::descr_handler(TMask_field& f, KEY k)
{
if (k == K_TAB && f.focusdirty())
{
if (app().iva() != nessuna_iva)
{
const int first = type2pos('T');
if (first >= 0)
{
TSheet_field& cg = app().cgs();
cg.row(first).add(f.get(), 8);
cg.force_update(first);
}
}
}
if (k == K_ENTER && f.get().empty())
{
if (f.mask().get(F_CODCAUS).empty())
return f.error_box("La descrizione del documento e' necessaria in mancanza della causale");
}
return TRUE;
}
// Handler per le colonne 'Dare' e 'Avere' dello sheet contabile.
// Scrivendo qualcosa in dare (101) cancella l'importo in avere (102) e viceversa
bool TPrimanota_application::dareavere_handler(TMask_field& f, KEY k)
{
if (k == K_F8 && app().is_pagamento())
{
const long numreg = app().curr_mask().get_long(F_NUMREG);
const int numrig = app().cgs().selected()+1;
const TImporto speso = app().partite().importo_speso(numreg, numrig);
const char* ss = speso.valore().string();
TMask& m = f.mask();
m.set(101, speso.sezione() == 'D' ? ss : "");
m.set(102, speso.sezione() == 'A' ? ss : "");
}
if (k == K_TAB && f.focusdirty() && f.get().not_empty())
{
const int id = 203-f.dlg(); // Calcola id del campo da resettare
f.mask().reset(id);
}
return TRUE;
}
// cazzorate
TSheet_field& TPrimanota_application::pags() const
{
TSheet_field& s = (TSheet_field&)_msk[2]->field(FS_RATESHEET);
return s;
}
///////////////////////////////////////////////////////////
// Gestione sheet IVA
///////////////////////////////////////////////////////////
TSheet_field& TPrimanota_application::ivas() const
{
TSheet_field& s = (TSheet_field&)_msk[2]->field(F_SHEETIVA);
return s;
}
TBill& TPrimanota_application::ivas_bill(TBill& c)
{
const int spric = c.tipo_cr();
if (spric == 2 || spric == 3)
{
const TFixed_string td(causale().tipo_doc());
if (td == "FV" || td == "NC") c.tipo_cr(4);
}
return c;
}
bool TPrimanota_application::imponibile_handler(TMask_field& f, KEY key)
{
if (key == K_TAB && f.dirty())
{
TMask& m = f.mask();
TString16 iva(m.get(102));
if (iva.empty())
{
iva = app().curr_mask().get(F_CODIVA);
m.set(102, iva);
}
if (iva.not_empty() && !app().causale().corrispettivi())
{
const real& percent = cod2IVA(m);
const real imponibile(f.get());
real imposta = abs(imponibile) * percent / 100.0; imposta.ceil();
if (imponibile.sign() < 0) imposta = -imposta;
m.set(104, imposta);
}
}
return TRUE;
}
bool TPrimanota_application::codiva_handler(TMask_field& f, KEY key)
{
if (!suspended_handler(f, key))
return FALSE;
if (key == K_TAB && f.dirty())
{
TMask& m = f.mask();
if (m.get_int(107) == 0)
{
TCodiceIVA iva(f.get());
TBill b; app().IVA2bill(iva, b);
char cr[2] = { b.tipo_cr() + '0', '\0' };
m.set(105, *cr > '0' ? cr : "");
const char tipo[2] = { b.tipo(), '\0' };
m.set(106, tipo);
m.set(107, b.gruppo());
m.set(108, b.conto());
const short id = b.tipo() == 'C' ? 209 : (b.tipo() == 'F' ? 309 : 109);
m.set(id, b.sottoconto());
m.set(id+1, b.descrizione());
}
TMask_field& im = m.field(101);
im.set_dirty();
im.on_hit();
} else
if (key == K_ENTER)
{
if (f.get().empty() && f.mask().get(101).not_empty())
return f.error_box("Codice IVA obbligatorio");
}
return TRUE;
}
bool TPrimanota_application::detrazione_handler(TMask_field& f, KEY key)
{
if (key == K_TAB && f.dirty() && app().iva() == iva_acquisti)
{
TMask_field& ci = f.mask().field(101);
ci.set_dirty();
ci.on_hit();
}
return TRUE;
}
bool TPrimanota_application::imposta_handler(TMask_field& f, KEY key)
{
if ((key == K_ENTER || key == K_TAB) && f.dirty())
{
const real imponibile(f.mask().get(101));
const real& percent = app().causale().corrispettivi() ? ZERO : cod2IVA(f.mask());
real imposta = abs(imponibile) * percent / 100.0;
imposta.ceil();
if (imponibile.sign() < 0) imposta = -imposta;
const real val(f.get());
if (val != imposta)
{
f.warning_box("L'imposta dovrebbe essere %s", (const char*)imposta.string("."));
}
} else
if (key == K_F8)
{
real imposta(f.get());
if (imposta.is_zero())
{
real imponibile(f.mask().get(101));
const real& percent = cod2IVA(f.mask());
imposta = scorpora(imponibile, percent);
f.mask().set(101, imponibile.string());
f.set(imposta.string());
}
else
f.warning_box("Cancellare l'imposta (tasto F2) prima di effettuare lo scorporo");
}
return TRUE;
}
// Calcola il totale degli imponibili e delle imposte e aggiorna
// i corrispondenti campi della maschera
// Certified 99%
real TPrimanota_application::calcola_imp() const
{
TArray& rows = ivas().rows_array();
const int max = rows.items();
real imponibili, imposte;
for (int r = 0; r < max; r++)
{
TToken_string& row = (TToken_string&)rows[r];
imponibili += real(row.get(0));
imposte += real(row.get(3));
}
curr_mask().set(F_IMPONIBILI, imponibili.string(), TRUE);
curr_mask().set(F_IMPOSTE, imposte.string(), TRUE);
return imponibili+imposte;
}
void TPrimanota_application::ivas_pack()
{
TString_array& rows = ivas().rows_array();
const int max = rows.items();
for (int i = 0; i < max; i++)
{
TToken_string& r = rows.row(i);
const real imponibile(r.get(0));
if (imponibile != ZERO) continue;
const real imposta(r.get(3));
if (imposta != ZERO) continue;
rows.destroy(i, FALSE);
}
rows.pack(); // Pack array
}
// Certified 50%
bool TPrimanota_application::iva_notify(TSheet_field& iva, int r, KEY k)
{
static int oldpos,oldposiva;
static real oldimp, oldiva;
TToken_string& row = iva.row(r);
const TCausale& cau = app().causale();
if (k == K_SPACE)
{
oldimp = real(row.get(0)); // Imponibile
oldiva = real(row.get(3)); // Imposta
if (oldiva.is_zero() && cau.corrispettivi())
{ // In caso di corrispettivi ...
const TCodiceIVA i(row.get(1));
const real percent = i.percentuale();
oldiva = scorpora(oldimp, percent); // ... scorpora imposta dall'imponibile
}
const char tipod = detraibile(row) ? 'D' : 'N';
oldposiva = type2pos(tipod);
if (oldposiva < 0 && !oldiva.is_zero())
{
const int ri = tipod == 'D' ? 3 : 4; // Calcola riga causale per l'IVA
TBill c; cau.bill(ri, c);
if (c.ok())
{
const TString80 d(cau.desc_agg(ri));
oldposiva = app().set_cgs_row(-1, app().real2imp(ZERO, 'I'), c, d, tipod);
}
else
if (ri == 4) // Se non esiste il conto IVA indetraibile ...
{ // ... somma imponibile e imposta
oldimp += oldiva;
oldiva = 0.0;
}
}
TBill oldconto(row, 6, 0x0); // g/c/s 6 7 8
if (oldconto.ok())
{
oldpos = bill2pos(oldconto, 'I');
if (oldpos < 0)
{
const TString80 d(cau.desc_agg(2));
oldpos = app().set_cgs_row(-1, app().real2imp(ZERO, 'I'), oldconto, d, 'I');
}
}
else
oldpos = -1; // Se il conto e' incompleto ignoralo
}
if (k == K_DEL) // Cancellazione di una riga
{
row.add("0", 0); // Azzera imponibile
row.add("0", 3); // Azzera imposta
k = K_ENTER; // Elegante o Sporco trucco (dipende dai gusti!)
}
if (k == K_ENTER)
{
int delimp = -1, deliva = -1; // Eventuali righe contabili da cancellare
if (oldpos >= 0) // Se il conto esisteva anche prima ...
{ // sottrai il vecchio imponibile
TImporto i(app().get_cgs_imp(oldpos));
i.valore() -= oldimp;
app().set_cgs_imp(oldpos, i);
if (i.is_zero()) delimp = oldpos;
}
if (oldposiva >= 0) // Se conto IVA esisteva anche prima ...
{ // sottrai la vecchia imposta
TImporto i(app().get_cgs_imp(oldposiva));
i.valore() -= oldiva;
app().set_cgs_imp(oldposiva, i);
if (i.is_zero()) deliva = oldposiva;
}
real imponibile(row.get(0)); // Nuovo imponibile
real imposta(row.get(3)); // Nuova imposta
if (imposta.is_zero() && app().causale().corrispettivi())
{ // In caso di corrispettivi ...
const TCodiceIVA i(row.get(1));
const real percent = i.percentuale();
imposta = scorpora(imponibile, percent); // ... scorpora imposta dall'imponibile
}
TBill conto(row, 5, 0x3);
int newpos = bill2pos(conto, 'I'); // Riga in cui andra' l'imponibile
const bool detrarre = detraibile(row); // Determina se IVA detraibile
const int ri = detrarre ? 3 : 4; // Calcola riga causale col conto opportuno
TBill contoiva; app().causale().bill(ri, contoiva);
if (ri == 4 && !contoiva.ok()) // Se non c'e' il conto IVA indetraibile ...
{ // ... somma imponibile e imposta
imponibile += imposta;
imposta = 0.0;
}
// Aggiorna conto sulla riga contabile
if (newpos < 0)
{
if (delimp >= 0)
{
app().reset_cgs_row(delimp); // Cancella vecchia riga
if (deliva > delimp) deliva--;
}
const TImporto val(app().real2imp(imponibile, 'I'));
if (conto.ok() && !val.is_zero()) // Se c'e' imponibile ...
{ // crea una nuova riga contabile
const TString80 d(cau.desc_agg(2));
app().set_cgs_row(-1, val, conto, d, 'I');
}
}
else
{
TImporto val(app().real2imp(imponibile, 'I'));
const bool empty = app().add_cgs_imp(newpos, val);
if (empty) // Se la riga si e' azzerata ...
{ // ... cancellala
app().reset_cgs_row(newpos);
newpos = -1;
}
}
oldimp = imponibile;
oldpos = newpos;
// Aggiorna conto IVA sulla riga contabile
const char tipod = detrarre ? 'D' : 'N';
int newposiva = type2pos(tipod);
if (deliva >= 0 && newposiva != deliva) // E' cambiato il tipo d'imposta
app().reset_cgs_row(deliva); // Azzera il vecchio tipo se necessario
if (newposiva < 0)
{
if (!imposta.is_zero()) // Se c'e' imposta ...
{ // ... crea nuova riga per l'IVA
const TImporto val(app().real2imp(imposta, 'I'));
const TString80 d(cau.desc_agg(ri));
newposiva = app().set_cgs_row(-1, val, contoiva, d, tipod);
}
}
else
{
const TImporto val(app().real2imp(imposta, 'I'));
const bool empty = app().add_cgs_imp(newposiva, val);
if (empty) // Se l'imposta si e' azzerata ...
{
app().reset_cgs_row(newposiva); // ... cancellala
newposiva = -1;
}
}
oldiva = imposta;
oldposiva = newposiva;
TMask& m = app().curr_mask();
if (r == 0) // Se cambio la prima riga ...
app().add_cgs_tot(m); // ... ricalcola conti
if (app().is_saldaconto() && m.page_enabled(2))
app().set_scadenze(m);
}
return TRUE;
}
// Handler dello sheet
// Certified 90%
bool TPrimanota_application::iva_handler(TMask_field& f, KEY k)
{
if ((k == K_TAB && !f.mask().is_running()) || k == K_ENTER)
{
const real imp = app().calcola_imp();
const real tot = app().totale_documento();
if (k == K_ENTER)
{
if (imp != tot)
{
TString16 t(tot.string("."));
TString16 i(imp.string("."));
return error_box("La somma del totale documento e delle ritenute (%s) e' diverso dalla "
"somma degli imponibili e delle imposte (%s)", (const char*)t, (const char*)i);
}
TSheet_field& iva = app().ivas();
for (int i = 0; i < iva.items(); i++)
{
TToken_string& row = iva.row(i);
const real im(row.get(0));
if (!im.is_zero())
{
TBill c(row, 5, 0x1);
if (!c.ok() || !c.find())
return error_box("Il conto della riga iva %d e' errato o incompleto", i+1);
}
}
}
}
return TRUE;
}
bool TPrimanota_application::cg_conto_handler(TMask_field& f, KEY key)
{
if (key == K_ENTER)
{
TMask& m = f.mask();
if (m.get(115)[0] == 'T') // Se riga totale documento
{
const char cf = m.get(f.dlg()-2)[0];
char tipo = app().clifo();
if (app().causale().corrispettivi()) tipo = ' ';
if (cf != tipo)
{
const char* d = tipo == ' ' ? "normale" : (tipo == 'C' ? "clienti" : "fornitori");
return f.error_box("E' richiesto un conto %s.", d);
}
}
}
return TRUE;
}
// Gestore del sottoconto dello sheet IVA
bool TPrimanota_application::iva_sottoconto_handler(TMask_field& f, KEY key)
{
if (!suspended_handler(f, key))
return FALSE;
if (key == K_TAB && f.dirty())
{
const TLocalisamfile& piano = ((TEdit_field&)f).browse()->cursor()->file();
int spric = piano.get_int("TIPOSPRIC");
if ((spric == 2 || spric == 3) && !app().causale().corrispettivi())
{
const TFixed_string td(app().causale().tipo_doc());
if (td == "FV" || td == "NC") spric = 4;
}
f.mask().set(105, spric > 0 ? format("%d", spric) : "", TRUE);
}
return TRUE;
}
bool TPrimanota_application::sheet_clifo_handler(TMask_field& f, KEY k)
{
if (!suspended_handler(f, k))
return FALSE;
if (k == K_TAB || k == K_ENTER)
{
TMask& m = f.mask();
const short cid = 100 + (f.dlg() % 100) -1;
const int conto = m.get_int(cid);
if (conto == 0)
{
const long codice = atol(f.get());
if (codice > 0L)
{
TBill c(0, 0, codice, f.dlg() > 300 ? 'F' : 'C');
c.descrizione(); // Carica gruppo e conto
m.set(cid-1, c.gruppo());
m.set(cid, c.conto());
}
}
}
return TRUE;
}
///////////////////////////////////////////////////////////
// Handlers dei campi della testata
///////////////////////////////////////////////////////////
// Handler of the F_NUMREG field on the query mask
// Certified 99%
bool TPrimanota_application::num_handler(TMask_field& f, KEY key)
{
TMask& m = f.mask();
const long num = atol(f.get());
if (key == K_TAB && m.is_running() && num > 0)
{
const long max = app()._lastreg+1;
app()._skip_giornale_check = FALSE;
app()._skip_bollato_check = FALSE;
if (num < max)
{
if (app().find(1))
{
const TLocalisamfile& mov = app()._rel->lfile();
bool ok = TRUE;
if (mov.get_bool("STAMPATO"))
{
ok = yesno_box("Il movimento e' gia' stato stampato sul libro giornale:\n"
"si desidera continuare ugualmente");
app()._skip_giornale_check = ok;
}
if (ok && mov.get_bool("REGST"))
{
ok = yesno_box("Il movimento e' gia' stato stampato sul bollato:\n"
"si desidera continuare ugualmente");
app()._skip_bollato_check = ok;
}
if (ok && mov.get_bool("INVIATO"))
{
ok = yesno_box("Il movimento e' stato inviato ad un'altra contabilita':\n"
"si desidera continuare ugualmente");
}
if (ok) // Riempie a mano i campi necessari nel caso non sia stato usata la ricerca F9
{
m.set(F_DATAREG, mov.get("DATAREG"), TRUE);
m.set(F_DATACOMP, mov.get("DATACOMP"), TRUE);
m.set(F_CODCAUS, mov.get("CODCAUS"));
m.stop_run(K_AUTO_ENTER);
}
}
}
else if (num > max)
{
f.set(format("%ld", max));
return f.error_box("Non e' possibile inserire movimenti superiori al %ld", max);
}
}
return TRUE;
}
// Handler of the F_CODCAUS field on the query mask
// Certified 99%
bool TPrimanota_application::caus_query_handler(TMask_field& f, KEY key)
{
if (!f.mask().is_running()) return TRUE;
if (f.to_check(key))
{
const int ann = f.mask().get_int(F_ANNOIVA);
const char* cau = f.get();
const TipoIVA i = cau2IVA(cau, ann); // Cerca causale e suo tipo
if (i != iva_errata)
{
const bool ok = suspended_handler(f, key); // Controlla sospensione
if (ok && key == K_TAB)
f.mask().stop_run(K_INS); // Entra in modo inserimento
}
else
return FALSE;
}
return TRUE;
}
// Handler of the F_CODCAUS field on the modify mask
// Certified 99%
bool TPrimanota_application::caus_modify_handler(TMask_field& f, KEY key)
{
if (f.to_check(key))
{
bool ok = suspended_handler(f, key);
if (!ok) return FALSE;
const int ann = f.mask().get_int(F_ANNOIVA);
const TString16 cau(f.get());
const TCausale c(cau, ann);
if (!c.ok()) return FALSE;
ok = app().causale().similar(c);
if (!ok) return FALSE;
if (key == K_TAB)
{
app().read_caus(cau, ann);
app().cgs().force_update();
}
}
return TRUE;
}
// Handler of the F_DATAREG field
// Certified 70%
bool TPrimanota_application::datareg_handler(TMask_field& f, KEY key)
{
bool ok = TRUE;
TMask& m = f.mask();
if ((key == K_TAB && m.is_running()) || key == K_ENTER)
{
const TDate dr(f.get()); // Data dell'operazione
if (dr > TDate(TODAY))
return f.error_box("La data dell'operazione e' superiore quella di sistema");
const int ae = date2esc(dr); // Anno esercizio
if (ae == 0)
return f.error_box("La data dell'operazione non appartiene a nessun esercizio");
if (m.query_mode() || app().giornale().year() != ae)
ok = app().giornale().read(ae);
else
ok = TRUE;
if (!ok)
return f.error_box("Non esiste il libro giornale dell'esercizio %d", ae);
if (key == K_ENTER || f.focusdirty())
{
const long numreg = m.get_long(F_NUMREG);
const bool error = numreg == 0 || numreg > app()._lastreg;
if (key != K_ENTER && !app()._skip_giornale_check)
{
const TLibro_giornale& gio = app().giornale();
if (dr < gio.last_print())
{
f.error_box("La data dell'operazione e' antecedente al %s,\n"
"ultima stampa del libro giornale dell'esercizio %d",
gio.last_print().string(), ae);
if (error) return FALSE;
}
if (key == K_TAB && dr < gio.last_reg())
f.warning_box("La data dell'operazione e' antecedente al %s,\n"
"ultima registrazione sul libro giornale dell'esercizio %d",
gio.last_reg().string(), ae);
}
if (m.query_mode())
app().causale().read(m.get(F_CODCAUS), dr.year());
TRegistro& reg = app().causale().reg();
const TString16 codreg(reg.name());
if (codreg.not_empty())
{
if (reg.year() != dr.year())
{
const bool ok = reg.read(codreg, dr.year());
if (!ok) return FALSE;
app().read_caus(NULL, 0);
if (app().iva() != nessuna_iva)
m.field(F_CODREG).on_hit();
}
if (!app()._skip_bollato_check)
{
if (dr < reg.last_print())
{
f.error_box("La data dell'operazione e' antecedente al %s,\n"
"ultima stampa del registro '%s' dell'anno %d",
reg.last_print().string(), (const char*)codreg, dr.year());
if (error) return FALSE;
}
if (key == K_TAB && dr < reg.last_reg())
f.warning_box("La data dell'operazione e' antecedente al %s,\n"
"ultima registrazione sul registro '%s' dell'anno %d",
reg.last_reg().string(), (const char*)codreg, dr.year());
}
if (reg.iva() != nessuna_iva && app()._rel->controlla_liquidazione(dr) == TRUE)
{
const char* m = itom(dr.month());
f.warning_box("La liquidazione IVA relativa al mese di %s e' gia' stata calcolata", m);
}
}
}
}
return ok;
}
// Handler of the F_DATACOMP field on the modify mask
// Certified 90%
bool TPrimanota_application::datacomp_handler(TMask_field& f, KEY key)
{
TMask& m = f.mask();
if (key == K_TAB || key == K_ENTER)
{
const TDate dr(m.get(F_DATAREG)); // Data operazione
TString16 datacomp = f.get();
if (datacomp.empty())
{
datacomp = dr.string();
f.set(datacomp);
}
const TDate dc(datacomp); // Data di competenza
const int ae = date2esc(dc); // Esercizio corrispondente
const char* data = "del 74/ter";
if (f.dlg() == F_DATACOMP)
{
m.set(F_ANNOES, ae);
data = "di competenza";
}
if (ae)
{
int pr; // Esercizio precedente
const int ar = date2esc(dr, &pr); // Esercizio in corso
if (ae != ar && ae != pr)
{
TString80 e;
e << "La data " << data << " deve appartenere all'esercizio " << ar;
if (pr > 0) e << " o al " << pr;
return f.error_box(e);
}
}
else
{
if (m.is_running())
return f.error_box("La data %s non appartiene a nessun esercizio", data);
}
}
return TRUE;
}
// Handler of the F_DATA74TER field on the modify mask
// Certified 90%
bool TPrimanota_application::data74ter_handler(TMask_field& f, KEY key)
{
if (!f.to_check(key)) return TRUE;
bool ok = datacomp_handler(f, key);
if (ok)
{
const TDate d74(f.get());
const TLibro_giornale& g = app().giornale();
if (d74 < g.last_print())
{
ok = f.error_box("La data per il 74/ter e' antecedente alla data di stampa "
"del libro giornale dell'esercizio %d", g.year());
}
}
return ok;
}
bool TPrimanota_application::doc_handler(TMask_field& f, KEY key)
{
if (key == K_TAB && f.mask().is_running())
{
TMask& m = f.mask();
const TString16 val(f.get());
if (!val.empty() && m.field(F_ANNORIF).active())
{ // Se c'e' gestione saldaconto
if (f.dlg() == F_DATADOC)
{
const TDate dd(val);
m.set(F_ANNORIF, dd.year()); // copia data documento
// TBI modifica scadenze pagamento
if (app().pagamento() != NULL)
app().recalc_scadenze(dd);
}
else if (!app().npart_is_prot())
{
if (m.get(F_NUMRIF).empty())
m.set(F_NUMRIF, val); // copia numero documento
}
}
}
return TRUE;
}
bool TPrimanota_application::occas_code_handler(TMask_field& f, KEY key)
{
if (key == K_TAB)
{
const char* code = f.get();
if (*code)
{
TRelation occas(LF_OCCAS);
occas.lfile().put("CFPI", code);
if (occas.read(_isequal) == NOERR)
{
f.mask().autoload(&occas);
f.mask().send_key(K_TAB, O_COMUNE); // Forza decodifica comuni
f.mask().send_key(K_TAB, O_COMUNENAS);
}
}
}
return TRUE;
}
bool TPrimanota_application::occas_handler(TMask_field& f, KEY key)
{
if (key == K_SPACE && f.mask().is_running())
{
TMask& om = app().occas_mask();
om.run();
f.set_focus();
}
return TRUE;
}
// Crea o aggiorna la riga contabile col totale documento
// Certified 99%
void TPrimanota_application::add_cgs_tot(TMask& m)
{
// Lettura del conto dalla maschera
char tipo = app().clifo();
long codice = m.get_long(tipo == 'C' ? F_CLIENTE : F_FORNITORE);
TBill bill(0, 0, codice, tipo);
if (!causale().corrispettivi())
bill.find();
if (bill.conto() == 0)
{
// Se l'utente non ha ancora specificato un conto lo prendo dalla prima riga della causale
causale().bill(1, bill);
if (causale().corrispettivi())
{
tipo = ' ';
codice = bill.sottoconto();
}
}
TBill nuovo(bill.gruppo(), bill.conto(), codice, tipo);
real tot(m.get(F_TOTALE));
const int pos = type2pos('T');
if (pos >= 0)
{
TSheet_field& ss = cgs();
TToken_string& row = ss.row(pos);
const TBill vecchio(row, 2, 0x1);
if (!vecchio.empty() && nuovo != vecchio) // Se cambio cliente/fornitore
{
for (int i = 0; i < ss.items(); i++) if (i != pos)
{
TToken_string& r = ss.row(i);
const TBill tacchia(r, 9, 0x1);
if (tacchia == vecchio)
{
nuovo.add_to(r, 9, 0x3); // Aggiorna contropartite
ss.force_update(i);
}
}
}
}
// Creazione/Aggiornamento riga totale
set_cgs_row(pos, real2imp(tot, 'T'), nuovo, m.get(F_DESCR), 'T');
calcola_imp(); // Ricalcola totale IVA
calcola_saldo(); // Ricalcola sbilanci
}
// Handler of the F_CLIENTE & F_FORNITORE field on the modify mask
// Certified 99%
bool TPrimanota_application::clifo_handler(TMask_field& f, KEY key)
{
if (key == K_TAB && f.active())
{
const long codice = atol(f.get());
if (codice == 0)
return TRUE;
TMask& m = f.mask();
const char cf = app().clifo();
TRelation cliforel(LF_CLIFO); cliforel.add(LF_CFVEN, "TIPOCF=TIPOCF|CODCF=CODCF");
TRectype& clifo = cliforel.curr();
clifo.put(CLI_TIPOCF, cf);
clifo.put(CLI_CODCF, codice);
cliforel.read();
if (!suspended_handler(f, key))
return FALSE;
if (!m.is_running() || f.dirty())
{
app()._conto_ricavo.set(clifo.get_int(CLI_GRUPPORIC),
clifo.get_int(CLI_CONTORIC),
clifo.get_long(CLI_SOTTOCRIC));
const int alleg = clifo.get_int(CLI_ALLEG);
TEdit_field& upi = m.efield(F_RIEPILOGO);
upi.check_type(alleg == 3 ? CHECK_REQUIRED : CHECK_NORMAL);
if (m.get(F_CODPAG).empty())
{
const TString& s = clifo.get(CLI_CODPAG);
if (s.not_empty())
m.set(F_CODPAG, s, TRUE);
}
if (app().is_saldaconto() && f.focusdirty())
{
if (m.field(S_VALUTA).active())
{
const TString& valuta = clifo.get(CLI_CODVAL);
if (valuta.not_empty())
m.set(S_VALUTA, valuta, TRUE);
}
if (clifo.get(CLI_CODCAB).not_empty())
{
m.set(FS_VSABI, clifo.get(CLI_CODABI));
m.set(FS_VSCAB, clifo.get(CLI_CODCAB));
m.send_key(K_TAB, FS_VSCAB);
}
const TString& agente = cliforel.curr(LF_CFVEN).get(CLI_CODAG);
if (agente.not_empty())
{
m.set(FS_AGENTE, agente, TRUE);
m.send_key(K_TAB, FS_AGENTE);
}
}
}
const bool occas = clifo.get_bool(CLI_OCCAS);
m.show(F_OCCASEDIT, occas); // Bottone Dati anagrafici
m.show(F_STATOPAIV, !occas); // Stato partita IVA
m.show(cf == 'C' ? F_PIVACLIENTE : F_PIVAFORNITORE, !occas); // Partita IVA
m.show(cf == 'C' ? F_COFICLIENTE : F_COFIFORNITORE, !occas); // Codice Fiscale
if (f.focusdirty())
{
app().add_cgs_tot(m);
if (occas && app().occas_mask().get(O_CODICE).empty())
m.send_key(K_SPACE, F_OCCASEDIT); // Lancia maschera occasionali
}
}
return TRUE;
}
bool TPrimanota_application::IVA2bill(const TCodiceIVA& iva, TBill& bill)
{
const TCausale& cau = causale();
const TString& tipo = iva.tipo();
if (tipo.not_empty())
{
if (tipo == "ES") cau.bill(5, bill); else
if (tipo == "NI") cau.bill(6, bill); else
if (tipo == "NS") cau.bill(7, bill);
}
if (!bill.ok() && !cau.corrispettivi())
bill = _conto_ricavo;
if (!bill.ok())
cau.bill(2, bill);
ivas_bill(bill);
return bill.ok();
}
// Handler of the F_CODIVA
// Certified 99%
bool TPrimanota_application::main_codiva_handler(TMask_field& f, KEY key)
{
if (key == K_TAB && f.get().not_empty())
{
if (!suspended_handler(f, key))
return FALSE;
const real imp(app().ivas().row(1).get(0)); // Se il totale non e' stato spezzato
if (imp.is_zero())
{
TToken_string& row = app().ivas().row(0);
TMask& m = f.mask();
iva_notify(app().ivas(), 0, K_SPACE);
const TCodiceIVA iva(f.get());
const bool corr = app().causale().corrispettivi();
const bool acq3 = (app().iva() == iva_acquisti) && (row.get_int(2) == 3);
real tot = app().totale_documento(); // Calcola totale documento
real imposta; // Calcola imposta
if (!corr && !acq3)
imposta = app().scorpora(tot, iva.percentuale());
row.add(tot.string(), 0); // imponibile
row.add(imposta.string(), 3); // imposta
if (iva.codice() != row.get(1))
{
row.add(iva.codice(), 1); // Aggiorna codice IVA
TBill bill; // Aggiorna conto della prima riga IVA
app().IVA2bill(iva, bill);
bill.add_to(row, 4, 0x7);
}
app().ivas().force_update(0);
iva_notify(app().ivas(), 0, K_ENTER);
}
}
return TRUE;
}
// Handler of the F_TOTALE
// Certified 99%
bool TPrimanota_application::totdoc_handler(TMask_field& f, KEY key)
{
bool ok = TRUE;
if (key == K_TAB && f.focusdirty())
{
TMask& m = f.mask();
if (app().iva() != nessuna_iva)
{
app().add_cgs_tot(m);
m.field(F_CODIVA).on_hit();
}
else
{
if (app().is_pagamento())
app().calcola_saldo();
}
TMask_field& totval = m.field(S_TOTDOCVAL);
if (totval.active() && totval.get().empty())
{
const real cambio(m.get(S_CAMBIO));
if (!cambio.is_zero())
{
real tot(f.get());
tot /= cambio;
totval.set(tot.string());
}
}
}
if (key == K_ENTER && f.get().empty())
ok = f.yesno_box("Totale documento nullo: continuare ugualmente?");
return ok;
}
bool TPrimanota_application::totdocval_handler(TMask_field& f, KEY key)
{
if (key == K_TAB && f.focusdirty() && f.get().not_empty())
{
TMask& m = f.mask();
real totval(f.get());
real cambio(m.get(S_CAMBIO));
real totale(m.get(F_TOTALE));
if (totale.is_zero())
{
totval *= cambio;
m.set(F_TOTALE, totval.string(), TRUE);
}
else
{
if (cambio.is_zero())
{
totale /= totval;
m.set(S_CAMBIO, totale.string(), TRUE);
}
}
}
return TRUE;
}
void TPrimanota_application::add_cgs_rit(bool fiscali)
{
TMask& m = curr_mask();
const real imp(m.get(fiscali ? F_RITFIS : F_RITSOC));
const char tipo = fiscali ? 'F' : 'S';
const int pos = type2pos(tipo);
if (pos < 0)
{
const int riga = fiscali ? 8 : 9;
TBill conto; causale().bill(riga, conto);
const TString80 desc(causale().desc_agg(riga));
set_cgs_row(-1, real2imp(imp, tipo), conto, desc, tipo);
}
else
{
if (imp.is_zero())
reset_cgs_row(pos);
else
set_cgs_imp(pos, real2imp(imp, tipo));
}
m.field(F_CODIVA).on_hit();
}
// Handler of the F_PROTIVA
bool TPrimanota_application::protiva_handler(TMask_field& f, KEY key)
{
bool ok = TRUE;
TMask& m = f.mask();
if (m.insert_mode())
{
if (key == K_ENTER && f.dirty())
{
const long protiva = atol(f.get());
const long protocol = app().causale().reg().protocol() + 1;
if (protiva != protocol)
ok = f.yesno_box("Accettare il protocollo IVA fuori sequenza: %ld invece di %ld",
protiva, protocol);
}
else if (key == K_TAB && app().is_saldaconto() && m.get(F_NUMRIF).empty())
{
const TString16 piva(f.get());
if (app().npart_is_prot() && piva.not_empty())
m.set(F_NUMRIF, piva);
}
}
return ok;
}
// Handler of the F_RITFIS
// Certified 99%
bool TPrimanota_application::ritfis_handler(TMask_field& f, KEY key)
{
if (key == K_TAB && f.focusdirty())
app().add_cgs_rit(TRUE);
return TRUE;
}
// Handler of F_RITSOC
// Certified 99%
bool TPrimanota_application::ritsoc_handler(TMask_field& f, KEY key)
{
if (key == K_TAB && f.focusdirty())
app().add_cgs_rit(FALSE);
return TRUE;
}
// Handler of F_CORRLIRE
// Certified 99%
bool TPrimanota_application::corrlire_handler(TMask_field& f, KEY key)
{
TMask& m = f.mask();
if (key == K_ENTER && f.get().empty())
{
if (m.get(F_CORRVALUTA).not_empty())
{
TMask_field& cv = m.field(F_CORRVALUTA);
cv.set_focusdirty();
cv.on_hit();
}
else
key = K_F8;
}
if (key == K_F8)
{
f.set(m.get(F_IMPONIBILI));
f.set_dirty();
m.reset(F_CORRVALUTA);
key = K_TAB;
}
if (key == K_TAB && f.focusdirty())
{
if (m.get(F_CORRVALUTA).empty())
{
const real cambio = m.get(F_CAMBIOINTRA);
if (cambio != ZERO && m.get(F_CORRVALUTA).empty())
{
real c(f.get());
c /= cambio;
m.set(F_CORRVALUTA, c.string());
}
}
}
if (key == K_ENTER)
{
const TString16 im(m.get(F_IMPONIBILI));
const char* cl = f.get();
if (im != cl)
warning_box("Il corrispettivo in lire e' diverso dal totale degli imponibili");
}
return TRUE;
}
// Handler of F_CORRVALUTA
// Certified 99%
bool TPrimanota_application::corrvaluta_handler(TMask_field& f, KEY key)
{
if (key == K_TAB && f.focusdirty())
{
TMask& m = f.mask();
if (m.get(F_CORRLIRE).empty())
{
const real cambio = m.get(F_CAMBIOINTRA);
if (cambio != ZERO)
{
real c = f.get();
c *= cambio;
m.set(F_CORRLIRE, c.string());
}
}
} else
if (key == K_ENTER && f.get().empty())
{
TMask_field& cl = f.mask().field(F_CORRLIRE);
cl.set_dirty();
cl.on_hit();
}
return TRUE;
}
bool TPrimanota_application::solaiva_handler(TMask_field& f, KEY key)
{
if (key == K_SPACE)
{
TMask& m = f.mask();
const bool anchecg = !m.get_bool(F_SOLAIVA);
m.show(F_SHEETCG, anchecg);
m.show(F_DARE, anchecg);
m.show(F_AVERE, anchecg);
if (m.is_running() && anchecg)
{
TSheet_field& iva = app().ivas();
const int righe = iva.items();
TProgind pi(righe, "Generazione righe contabilita'", FALSE, TRUE, 16);
app().cgs().reset();
app().add_cgs_tot(m);
if (m.get(F_RITFIS).not_empty()) app().add_cgs_rit(TRUE);
if (m.get(F_RITSOC).not_empty()) app().add_cgs_rit(FALSE);
TToken_string oldrow(128);
for (int i = 0; i < righe; i++)
{
TToken_string& r = iva.row(i);
if (!r.empty_items())
{
oldrow = r;
r = "";
iva_notify(iva, i, K_SPACE);
r = oldrow;
iva_notify(iva, i, K_ENTER);
}
pi.setstatus(i+1);
}
app().fill_sheet(m);
app().cgs().force_update();
m.set_focus();
}
}
return TRUE;
}