509 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			509 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
// -------------------------------------------------------------
 | 
						|
// calcolo liquidazioni
 | 
						|
// part 3: utilities
 | 
						|
// fv 2-2-94
 | 
						|
// --------------------------------------------------------------
 | 
						|
 | 
						|
#include <config.h>
 | 
						|
#include <utility.h>
 | 
						|
 | 
						|
#include "cg4300.h"
 | 
						|
 | 
						|
bool TLiquidazione_app::is_trim(int x) 
 | 
						|
    // TRUE se il mese passato e' un trimestre
 | 
						|
{ return x == 3 || x == 6 || x == 9 || x == 12; }
 | 
						|
 | 
						|
bool TLiquidazione_app::is_month_ok_strict(int x, int month)
 | 
						|
     // TRUE se il mese passato e' compatibile con il regime
 | 
						|
     // di liquidazione e (opz) non e' maggiore di quello scelto
 | 
						|
{ 
 | 
						|
  if (month == -1) month = x;
 | 
						|
  return _freqviva == "M" ? 
 | 
						|
    ( x > 0 && x <= month) :
 | 
						|
    ( x <= month && is_trim(x));
 | 
						|
} 
 | 
						|
 | 
						|
bool TLiquidazione_app::is_month_ok(int x, int mtocalc)
 | 
						|
   // TRUE se il mese passato e' compatibile con il mese da calcolare
 | 
						|
   // rispetto al regime di liquidazione scelto
 | 
						|
{ 
 | 
						|
  bool ret = x == mtocalc;
 | 
						|
  if (!ret && _freqviva == "T" && mtocalc != 13)
 | 
						|
  {  
 | 
						|
     // aggiusta al trimestre il mese da calcolare
 | 
						|
  	 mtocalc += 2 - ((mtocalc-1) % 3);	
 | 
						|
  	 ret = x > (mtocalc - 3) && x <= mtocalc; 
 | 
						|
  }
 | 
						|
  else if (!ret && mtocalc == 13)
 | 
						|
  {               
 | 
						|
    // per l'annuale ritorna TRUE per tutti i mesi da liquidare
 | 
						|
  	ret =  x <= 13;
 | 
						|
  }      
 | 
						|
  return ret;
 | 
						|
} 
 | 
						|
 | 
						|
int TLiquidazione_app::liq_month(int x)
 | 
						|
  // ritorna il mese da liquidare (= LIM presente)
 | 
						|
  //  che corrisponde al mese passato
 | 
						|
{         
 | 
						|
   if (x == 13 || _freqviva == "M") 
 | 
						|
     return x;
 | 
						|
   else return x + (2 - ((x-1) % 3));
 | 
						|
}
 | 
						|
 | 
						|
bool TLiquidazione_app::is_first_month(int m)
 | 
						|
{
 | 
						|
  return _freqviva == "M" ? 
 | 
						|
    m == 1 : m == 3;
 | 
						|
}
 | 
						|
 | 
						|
int TLiquidazione_app::previous_month(int m)
 | 
						|
{
 | 
						|
  // vale per LIM (mese o trimestre precedente)
 | 
						|
  if (_freqviva == "M")
 | 
						|
    return m == 1 ? 1 : m - 1;
 | 
						|
  else return m == 3 ? 3 : m - 3;
 | 
						|
}
 | 
						|
 | 
						|
bool TLiquidazione_app::is_date_ok(TDate& d, int month)
 | 
						|
// TRUE se la data (di mov o pim) passata va considerata nel
 | 
						|
// ricalcolo dei progressivi mensili per il mese e anno
 | 
						|
// selezionati; se month == 13 vanno bene tutte purche'
 | 
						|
// sia giusto l'anno
 | 
						|
{
 | 
						|
  if (d.month() > month || d.year() != atoi(_year))
 | 
						|
    return FALSE;
 | 
						|
  if (month == 13) return d.month() <= month;
 | 
						|
  else             return d.month() == month;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
void TLiquidazione_app::add_ventilation(real iva, real howmuch,  const char* codiva)
 | 
						|
{
 | 
						|
  _VentItem* vi = NULL;
 | 
						|
  for (int i = 0; i < _vent_arr.items(); i++)
 | 
						|
    {
 | 
						|
      vi = (_VentItem*)&_vent_arr[i];
 | 
						|
      if (vi->_codiva == codiva)
 | 
						|
	break;
 | 
						|
    }
 | 
						|
  if (i == _vent_arr.items())
 | 
						|
    {
 | 
						|
      _vent_arr.add(vi = new _VentItem);
 | 
						|
      vi->_aliquota    = iva;
 | 
						|
      vi->_codiva      = codiva;
 | 
						|
    }
 | 
						|
  vi->_totale += howmuch;
 | 
						|
}
 | 
						|
 | 
						|
void TLiquidazione_app::add_vendite(int month, const char* codreg, 
 | 
						|
	                                int tipodet, real& r)
 | 
						|
{
 | 
						|
  _VendItem* vi = NULL;
 | 
						|
 
 | 
						|
  for (int i = 0; i < _vend_arr.items(); i++)
 | 
						|
   {
 | 
						|
      vi = (_VendItem*)&_vend_arr[i];
 | 
						|
      if (vi->_codreg == codreg && vi->_month == month && vi->_tipodet == tipodet)
 | 
						|
	  break;
 | 
						|
    }
 | 
						|
  if (i == _vend_arr.items())
 | 
						|
    {
 | 
						|
      _vend_arr.add(vi = new _VendItem);
 | 
						|
      vi->_codreg      = codreg;
 | 
						|
      vi->_month       = month;
 | 
						|
      vi->_tipodet     = tipodet;
 | 
						|
    }
 | 
						|
  vi->_totale += r;
 | 
						|
}
 | 
						|
 | 
						|
void TLiquidazione_app::add_corrisp(int month, const char* codreg, real& r,
 | 
						|
			                 real& p, int tipodet, const char * codiva)
 | 
						|
{
 | 
						|
  _CorrItem* ci = NULL;
 | 
						|
	const int nitems = _corr_arr.items();
 | 
						|
 
 | 
						|
  for (int i = 0; i < nitems; i++)
 | 
						|
	{
 | 
						|
		ci = (_CorrItem*)&_corr_arr[i];
 | 
						|
		if (ci->_codreg == codreg && ci->_month == month &&
 | 
						|
				ci->_codiva == codiva && ci->_tipodet == tipodet)
 | 
						|
			break;
 | 
						|
	}
 | 
						|
  if (i == nitems)
 | 
						|
	{
 | 
						|
		_corr_arr.add(ci = new _CorrItem);
 | 
						|
		ci->_codreg      = codreg;
 | 
						|
		ci->_month       = month;
 | 
						|
		ci->_codiva      = codiva;
 | 
						|
		ci->_tipodet     = tipodet;
 | 
						|
		ci->_aliquota    = p;
 | 
						|
	}
 | 
						|
  ci->_totale += r;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
bool TLiquidazione_app::look_pim(int month, const char* codatt, const char* codreg, 
 | 
						|
			  const char* tipocr, const char* codiva, int tipodet,  
 | 
						|
			  bool create)
 | 
						|
     // ritorna il PIM corrispondente alla chiave passata; se 
 | 
						|
     // create = TRUE lo crea se non lo trova. Ritorna se c'era
 | 
						|
{
 | 
						|
  bool ok = FALSE;
 | 
						|
  _pim_r->zero();
 | 
						|
  (*_pim_anno)    = _year;
 | 
						|
  (*_pim_mese)    = format("%d", month);
 | 
						|
  (*_pim_codreg)  = codreg;
 | 
						|
  (*_pim_codiva)  = codiva;
 | 
						|
  (*_pim_codatt)  = codatt;
 | 
						|
  (*_pim_tipocr)  = tipocr;
 | 
						|
  (*_pim_tipodet) = tipodet;
 | 
						|
  
 | 
						|
  TString s = _pim_r->get("CODTAB"); 
 | 
						|
  
 | 
						|
  _pim->read();
 | 
						|
  ok = _pim->good();
 | 
						|
  
 | 
						|
  if (!ok && create)
 | 
						|
    {
 | 
						|
      _pim_r->zero();
 | 
						|
      _pim_r->put("CODTAB",s);
 | 
						|
      _pim->write();
 | 
						|
    }
 | 
						|
  return ok;
 | 
						|
}
 | 
						|
 | 
						|
bool TLiquidazione_app::look_plm(int m, const char* a, bool create)
 | 
						|
{
 | 
						|
  bool ok = FALSE;
 | 
						|
 | 
						|
  _plm_r->zero();
 | 
						|
  (*_plm_codatt) = a;
 | 
						|
  (*_plm_mese)   = format("%d",m);
 | 
						|
  (*_plm_anno)   = _year;
 | 
						|
 | 
						|
  TString s = _plm_r->get("CODTAB");
 | 
						|
  _plm->read();
 | 
						|
  ok = _plm->good();
 | 
						|
  
 | 
						|
  if (!ok && create)
 | 
						|
    {
 | 
						|
      _plm_r->zero();
 | 
						|
      _plm_r->put("CODTAB",s);
 | 
						|
      _plm->write();
 | 
						|
    }
 | 
						|
  return ok;
 | 
						|
}
 | 
						|
 | 
						|
bool TLiquidazione_app::look_ptm(int m, const char* a, bool create)
 | 
						|
{
 | 
						|
  bool ok = FALSE;
 | 
						|
 | 
						|
  _ptm->zero();
 | 
						|
  (*_ptm_codatt) = a;
 | 
						|
  (*_ptm_mese)   = format("%d",m);
 | 
						|
  (*_ptm_anno)   = _year;
 | 
						|
 | 
						|
  TString s = _ptm->get("CODTAB");
 | 
						|
  _ptm->read();
 | 
						|
  ok = _ptm->good();
 | 
						|
  
 | 
						|
  if (!ok && create)
 | 
						|
    {
 | 
						|
      _ptm->zero();
 | 
						|
      _ptm->put("CODTAB",s);
 | 
						|
      _ptm->write();
 | 
						|
    }
 | 
						|
  return ok;
 | 
						|
}
 | 
						|
 | 
						|
bool TLiquidazione_app::look_lim(int m, bool create)
 | 
						|
{
 | 
						|
  bool ok = FALSE;
 | 
						|
 | 
						|
  _lim_r->zero();
 | 
						|
  (*_lim_mese)   = format("%d",m);
 | 
						|
  (*_lim_anno)   = _year;
 | 
						|
 | 
						|
  TString s = _lim_r->get("CODTAB");
 | 
						|
  _lim->read();
 | 
						|
  ok = _lim->good();
 | 
						|
  
 | 
						|
  if (!ok && create)
 | 
						|
    {
 | 
						|
      _lim_r->zero();
 | 
						|
      _lim_r->put("CODTAB",s);
 | 
						|
      _lim->write();
 | 
						|
    }
 | 
						|
  return ok;
 | 
						|
}
 | 
						|
 | 
						|
bool TLiquidazione_app::look_pla(const char* a, bool create)
 | 
						|
{
 | 
						|
  bool ok = FALSE;                  
 | 
						|
  _pla_r->zero();
 | 
						|
 | 
						|
  // forza il tipoatt a 1
 | 
						|
  char buf[10]; strcpy(buf,a);
 | 
						|
  buf[strlen(buf) - 1] = '1';
 | 
						|
  
 | 
						|
  (*_pla_codatt) = buf;
 | 
						|
  (*_pla_anno)   = _year;
 | 
						|
 | 
						|
  TString s = _pla_r->get("CODTAB");
 | 
						|
  _pla->read();
 | 
						|
  ok = _pla->good();
 | 
						|
  
 | 
						|
  if (!ok && create)
 | 
						|
    {
 | 
						|
      real es_b1, es_b2, es_b3, prorata;
 | 
						|
      // alla creazione del PLA
 | 
						|
      // si prendono i valori di prorata e plafond dall'anno scorso
 | 
						|
      // e (TBI) si controllano incompatibilita' con la normativa
 | 
						|
       
 | 
						|
      _pla_r->zero();
 | 
						|
      (*_pla_codatt) = buf;
 | 
						|
      (*_pla_anno)   = atoi(_year) - 1;
 | 
						|
      if (_pla->read() == NOERR)
 | 
						|
	  {
 | 
						|
	    es_b1 = _pla->get_real("R1");
 | 
						|
	    es_b2 = _pla->get_real("R2");
 | 
						|
	    es_b3 = _pla->get_real("R3");
 | 
						|
	    es_b1 = _pla->get_real("R9");
 | 
						|
	  }
 | 
						|
      _pla->zero();
 | 
						|
      _pla_r->put("CODTAB",s);
 | 
						|
      _pla->put("R5",es_b1);
 | 
						|
      _pla->put("R6",es_b2);
 | 
						|
      _pla->put("R7",es_b3);
 | 
						|
      _pla->put("R8",prorata);
 | 
						|
      _pla->write();
 | 
						|
    }
 | 
						|
  return ok;
 | 
						|
}
 | 
						|
 | 
						|
bool TLiquidazione_app::look_reg(const char* reg)
 | 
						|
{
 | 
						|
  _reg_r->zero();
 | 
						|
  TString s(12); s << _year; s << format("%-3s",reg);
 | 
						|
  _reg_r->put("CODTAB",(const char*)s);
 | 
						|
  _reg->read();
 | 
						|
  return _reg->good();
 | 
						|
}
 | 
						|
 | 
						|
bool TLiquidazione_app::look_iva(const char* cod)
 | 
						|
{
 | 
						|
  _iva->zero();
 | 
						|
  _iva->put("CODTAB",cod);
 | 
						|
  _iva->read();
 | 
						|
  return _iva->good();
 | 
						|
}
 | 
						|
 | 
						|
bool TLiquidazione_app::look_ppa(int month, const char* codatt, int type, bool create)
 | 
						|
{
 | 
						|
  _ppa->zero();
 | 
						|
  (*_ppa_year)   = _year;
 | 
						|
  (*_ppa_month)  = month;
 | 
						|
  (*_ppa_codatt) = codatt;
 | 
						|
  (*_ppa_kind)   = type;
 | 
						|
  TString ctab   = _ppa_r->get("CODTAB");
 | 
						|
  _ppa->read();
 | 
						|
 | 
						|
  bool ok = _ppa->good();
 | 
						|
  
 | 
						|
  if (!ok && create)
 | 
						|
    {
 | 
						|
      _ppa_r->zero();
 | 
						|
      _ppa_r->put("CODTAB",ctab);
 | 
						|
      _ppa->write();
 | 
						|
    }
 | 
						|
  return ok;
 | 
						|
}
 | 
						|
 | 
						|
bool TLiquidazione_app::look_rmb(int month, const char* codiva, tiporeg tr, bool create)
 | 
						|
{
 | 
						|
  _rmb->zero();
 | 
						|
  (*_rmb_anno)   = _year;
 | 
						|
  (*_rmb_mese)  = month;
 | 
						|
  (*_rmb_codiva) = codiva;
 | 
						|
  (*_rmb_tiporeg) = (int)tr;
 | 
						|
  TString ctab   = _rmb->get("CODTAB");
 | 
						|
  _rmb->read();
 | 
						|
 | 
						|
  bool ok = _rmb->good();
 | 
						|
  
 | 
						|
  if (!ok && create)
 | 
						|
    {
 | 
						|
      _rmb->zero();
 | 
						|
      _rmb->put("CODTAB",ctab);
 | 
						|
      _rmb->write();
 | 
						|
    }
 | 
						|
  return ok;
 | 
						|
}
 | 
						|
 | 
						|
bool TLiquidazione_app::look_del(int month, int type, bool create)
 | 
						|
{
 | 
						|
  long ditta = _nditte->curr().get_long("CODDITTA");
 | 
						|
  _del->zero();
 | 
						|
  (*_del_ditta)  = format("%05ld", ditta);
 | 
						|
  (*_del_anno)   = _year;
 | 
						|
  (*_del_mese)   = format("%02d", month);
 | 
						|
  (*_del_tipo)   = format("%1d",  type);     
 | 
						|
  
 | 
						|
  TString16 ctab = _del->get("CODTAB");
 | 
						|
  _del->read();
 | 
						|
 | 
						|
  bool ok = _del->good();
 | 
						|
 | 
						|
  if (!ok && create)
 | 
						|
    {
 | 
						|
      _del->zero();
 | 
						|
      _del->put("CODTAB",ctab);
 | 
						|
 | 
						|
      // TBI infila ufficio concessione in S9
 | 
						|
      
 | 
						|
      // sistema codice tributo
 | 
						|
      int ctri = 6000;
 | 
						|
      if (month == 13 && type == 7) 
 | 
						|
        ctri = 6035;           // acconto IVA annuale (trimestrali?)
 | 
						|
      else if (month == 13 && type == 1)
 | 
						|
        ctri = 6099;           // IVA annuale
 | 
						|
      else if (month < 13 && type == 7)
 | 
						|
        ctri = 6013;           // acconto mensile
 | 
						|
      else if (month < 13 && type == 1)  // regular
 | 
						|
        ctri = _freqviva == "M" ? 6000 + month : 6030 + (month/3); 
 | 
						|
        
 | 
						|
      _del->put("S6", format("%d",ctri));
 | 
						|
      _del->write();    
 | 
						|
    }
 | 
						|
  return ok;
 | 
						|
}
 | 
						|
 | 
						|
bool TLiquidazione_app::look_lia(bool create, int year)
 | 
						|
{
 | 
						|
  if (year == 0) year = atoi(_year);
 | 
						|
  TString16 y; y << year;
 | 
						|
 
 | 
						|
  _lia->zero();
 | 
						|
  _lia->put("CODTAB", y);
 | 
						|
  _lia->read();
 | 
						|
  const bool ok = _lia->good();
 | 
						|
  if (!ok && create)
 | 
						|
  {
 | 
						|
    _lia->zero();
 | 
						|
    _lia->put("CODTAB", y);
 | 
						|
    _lia->put("S7", _freqviva);
 | 
						|
		_lia->write();
 | 
						|
	}
 | 
						|
	return ok;
 | 
						|
}
 | 
						|
 | 
						|
real TLiquidazione_app::result_liq(int month)
 | 
						|
// risultato esatto della liquidazione del mese month, <0 a credito
 | 
						|
// > 0 a debito; comprende TUTTI, anche il conguaglio prorata in annuale
 | 
						|
{  
 | 
						|
   real r(0.0);                               
 | 
						|
   // ulteriori detrazioni, acconti, versamenti,
 | 
						|
   // rettifiche sono gia' comprese in R0
 | 
						|
   if (look_lim(month))
 | 
						|
   	 r = _lim->get_real("R0");
 | 
						|
   return r;
 | 
						|
}                                           
 | 
						|
                                           
 | 
						|
 | 
						|
real TLiquidazione_app::debt_prec(int month)
 | 
						|
{
 | 
						|
  real r(0.0);
 | 
						|
  if (!is_first_month(month))
 | 
						|
  {
 | 
						|
    if (look_lim(previous_month(month)))
 | 
						|
    {
 | 
						|
      r = result_liq(previous_month(month));
 | 
						|
      if (!(r.sign() > 0 && r < IVA_DA_RIPORTARE))
 | 
						|
        r = ZERO;
 | 
						|
    }
 | 
						|
  } 
 | 
						|
  return r;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
real TLiquidazione_app::credito_prec(int month)
 | 
						|
// ritorna l'appropriato credito precedente al mese in corso
 | 
						|
{
 | 
						|
  real c(0.0);
 | 
						|
 | 
						|
  if (is_first_month(month))
 | 
						|
  {
 | 
						|
    // credito inizio anno
 | 
						|
    if (look_lia()) 
 | 
						|
      c = _lia->get_real("R0");
 | 
						|
      // e' positivo o 0
 | 
						|
  }
 | 
						|
  else
 | 
						|
  {
 | 
						|
  	c = result_liq(previous_month(month)); 
 | 
						|
    if (c.sign() < 0) c = abs(c); 
 | 
						|
    else c = real(0.0);
 | 
						|
  }
 | 
						|
 | 
						|
  return c;
 | 
						|
}
 | 
						|
 | 
						|
real TLiquidazione_app::credito_costo_prec(int month)
 | 
						|
// ritorna l'appropriato credito di costo precedente al mese in corso
 | 
						|
// (travel agency only)
 | 
						|
{
 | 
						|
  real c(0.0);
 | 
						|
  if (is_first_month(month))
 | 
						|
    {
 | 
						|
      int yr = atoi(_year)-1;
 | 
						|
      _lim->zero();
 | 
						|
      *_lim_anno = yr;
 | 
						|
      *_lim_mese = 12;
 | 
						|
      if (_lim->read() == NOERR) 
 | 
						|
         c = _lim->get_real("R2");
 | 
						|
    }
 | 
						|
  else
 | 
						|
    {
 | 
						|
      if (look_lim(previous_month(month)))
 | 
						|
      // qui il rimborso non c'e'
 | 
						|
        c = _lim->get_real("R2");
 | 
						|
    }
 | 
						|
  look_lim(month);
 | 
						|
  return c;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
real TLiquidazione_app::versamenti_IVA(int month, const char* types, bool intr)
 | 
						|
{ 
 | 
						|
  real ret(0.0); TToken_string typ(types);
 | 
						|
 | 
						|
  for (int i = 0; i < typ.items(); i++)
 | 
						|
  {                      
 | 
						|
    int tp = typ.get_int(i);
 | 
						|
    if (look_del(month,tp)) 
 | 
						|
      if (_del->get_bool("B0"))   // solo se stampata
 | 
						|
      {
 | 
						|
        ret += _del->get_real("R0"); 
 | 
						|
        if (intr) ret += _del->get_real("R1");   
 | 
						|
      }
 | 
						|
  }
 | 
						|
  return ret;
 | 
						|
}
 | 
						|
 | 
						|
real TLiquidazione_app::aliquota_agvia()
 | 
						|
{
 | 
						|
  TConfig cnf(CONFIG_STUDIO);
 | 
						|
  look_iva(cnf.get("CodAgv"));
 | 
						|
  real r = _iva->get_real("R0");
 | 
						|
  return r;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
real TLiquidazione_app::interesse_trimestrale(int month)
 | 
						|
{
 | 
						|
  month /= 3; month--;
 | 
						|
  TConfig cnf(CONFIG_STUDIO);
 | 
						|
  real r(cnf.get("InTr", NULL, month));
 | 
						|
  return r;
 | 
						|
} |