git-svn-id: svn://10.65.10.50/branches/R_10_00@22935 c028cbd2-c16b-5b4b-a496-9718f37d4682
		
			
				
	
	
		
			924 lines
		
	
	
		
			24 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			924 lines
		
	
	
		
			24 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
#include <diction.h>
 | 
						||
#include <lffiles.h>
 | 
						||
#include <progind.h>
 | 
						||
#include <recarray.h>
 | 
						||
#include <relation.h>
 | 
						||
#include <validate.h>
 | 
						||
 | 
						||
#include "fe0100a.h"
 | 
						||
#include "felib.h"
 | 
						||
 | 
						||
#include "../cg/cg2103.h"
 | 
						||
 | 
						||
#include <alleg.h>
 | 
						||
#include <anafis.h>
 | 
						||
#include <anagr.h>
 | 
						||
#include <attiv.h>
 | 
						||
#include <clifo.h>
 | 
						||
#include <comuni.h>
 | 
						||
#include <doc.h>
 | 
						||
#include <mov.h>
 | 
						||
#include <nditte.h>
 | 
						||
#include <occas.h>
 | 
						||
 | 
						||
///////////////////////////////////////////////////////////
 | 
						||
// Utility
 | 
						||
///////////////////////////////////////////////////////////
 | 
						||
 | 
						||
bool fe_is_nota_variazione(const TRectype& mov)
 | 
						||
{
 | 
						||
  const int logicnum = mov.num();
 | 
						||
 | 
						||
  if (logicnum == LF_MOV)
 | 
						||
  {
 | 
						||
    const real totdoc = mov.get_real(MOV_TOTDOC);
 | 
						||
    if (totdoc < ZERO)
 | 
						||
      return true;
 | 
						||
 | 
						||
    const int tipomov = mov.get_int(MOV_TIPOMOV);
 | 
						||
    if (tipomov == 2) // Nota di credito/debito per saldaconto
 | 
						||
       return true;
 | 
						||
 | 
						||
    if (tipomov <= 1)
 | 
						||
    {
 | 
						||
      const TString& tipodoc = mov.get(MOV_TIPODOC);
 | 
						||
      if (tipodoc == "NC" || tipodoc == "ND") // Nota di credito/debito senza saldaconto
 | 
						||
        return true;
 | 
						||
    }
 | 
						||
  } else
 | 
						||
  if (logicnum == LF_ALLEG)
 | 
						||
  {
 | 
						||
    const TString& tipo = mov.get(ALL_TIPOPE);
 | 
						||
    if (tipo.len() == 2 && tipo != "BL")  
 | 
						||
      return tipo == "NE" || tipo == "NR";
 | 
						||
 | 
						||
    const real importo = mov.get_real(ALL_IMPORTO);
 | 
						||
    const real imposta = mov.get_real(ALL_IMPOSTA);
 | 
						||
    if (importo < ZERO || (importo.is_zero() && imposta < ZERO))
 | 
						||
      return true;
 | 
						||
 | 
						||
    const long numreg = mov.get_long(ALL_PROGR);
 | 
						||
    if (numreg < 900000L)
 | 
						||
    {
 | 
						||
      const TRectype& pn = cache().get(LF_MOV, numreg);
 | 
						||
      if (!pn.empty())
 | 
						||
        return fe_is_nota_variazione(pn);
 | 
						||
    }
 | 
						||
  } else
 | 
						||
  if (logicnum == LF_DOC)
 | 
						||
  {
 | 
						||
    const TString& tipodoc = mov.get(DOC_TIPODOC);
 | 
						||
    const TRectype& td = cache().get("%TIP", tipodoc);
 | 
						||
 | 
						||
    bool yes = td.get_bool("B7");
 | 
						||
    if (!yes)
 | 
						||
    {
 | 
						||
      const TString4 codcaus(td.get("S6"));
 | 
						||
      if (codcaus.full())
 | 
						||
      {
 | 
						||
        const TCausale c(codcaus, mov.get_int(DOC_ANNO));
 | 
						||
        const char sez = c.sezione_clifo();
 | 
						||
        //controllo ulteriore sull'iva
 | 
						||
        TipoIVA tiva = c.reg().iva();
 | 
						||
        const char tcf = mov.get_char(DOC_TIPOCF);
 | 
						||
        if (tiva == nessuna_iva && tcf > ' ')
 | 
						||
          tiva = tcf == 'C' ? iva_vendite : iva_acquisti;
 | 
						||
        if (tiva != nessuna_iva)
 | 
						||
          yes = ((tiva == iva_vendite) ^ (sez == 'D'));
 | 
						||
      }
 | 
						||
    }
 | 
						||
    return yes;
 | 
						||
  }
 | 
						||
 | 
						||
  return false;
 | 
						||
}
 | 
						||
 | 
						||
///////////////////////////////////////////////////////////
 | 
						||
// TAnagrafica
 | 
						||
///////////////////////////////////////////////////////////
 | 
						||
 | 
						||
static bool is_stato_estero(const TString& codcom)
 | 
						||
{ return codcom.full() && codcom.match("Z[0-9][0-9][0-9]"); }
 | 
						||
 | 
						||
static const TString& comune_di(const TString& codcom, int stato)
 | 
						||
{
 | 
						||
  if (codcom.blank() || codcom.len() != 4)
 | 
						||
    return EMPTY_STRING;
 | 
						||
 | 
						||
  TString8 key; 
 | 
						||
  if (stato > 0)
 | 
						||
    key.format("%03d", stato);
 | 
						||
  key << '|' << codcom;
 | 
						||
  return cache().get(LF_COMUNI, key, COM_DENCOM);
 | 
						||
}
 | 
						||
 | 
						||
static const TString& provincia_di(const TString& codcom)
 | 
						||
{
 | 
						||
  if (codcom.blank() || codcom.len() != 4)
 | 
						||
    return EMPTY_STRING;
 | 
						||
  TString& prov = get_tmp_string();
 | 
						||
  if (is_stato_estero(codcom))
 | 
						||
    prov = "EE";
 | 
						||
  else
 | 
						||
  {
 | 
						||
    TString8 key; key << '|' << codcom;
 | 
						||
    prov = cache().get(LF_COMUNI, key, COM_PROVCOM);
 | 
						||
    if (prov.len() > 2) prov = "RM";
 | 
						||
  }
 | 
						||
  return prov;
 | 
						||
}
 | 
						||
 | 
						||
const TString& TAnagrafica::comune_nascita() const 
 | 
						||
{ 
 | 
						||
  if (is_stato_estero(_com_nasc))
 | 
						||
  {
 | 
						||
    const TString& c = comune_di(_com_nasc, 0);
 | 
						||
    if (c.full())
 | 
						||
      return c;
 | 
						||
  }
 | 
						||
  if (_stato_nasc > 0)
 | 
						||
  {
 | 
						||
    TString4 u; u.format("%03d", _stato_nasc);
 | 
						||
    const TString& s = cache().get("%STA", u, "S0");
 | 
						||
    if (s.full())
 | 
						||
      return s;
 | 
						||
  }
 | 
						||
  return comune_di(_com_nasc, _stato_nasc); 
 | 
						||
}
 | 
						||
 | 
						||
const TString& TAnagrafica::provincia_nascita() const 
 | 
						||
{ 
 | 
						||
  if (_stato_nasc > 0 || is_stato_estero(_com_nasc))
 | 
						||
    return get_tmp_string() = "EE";
 | 
						||
  return provincia_di(_com_nasc); 
 | 
						||
}
 | 
						||
 | 
						||
const TString& TAnagrafica::comune_residenza() const 
 | 
						||
{ 
 | 
						||
  const TString& cr = comune_di(_com_res, _stato_estero); 
 | 
						||
  if (cr.full()) 
 | 
						||
    return cr;
 | 
						||
  return _loc_res;
 | 
						||
}
 | 
						||
 | 
						||
const TString& TAnagrafica::provincia_residenza() const 
 | 
						||
{ 
 | 
						||
  if (estero())
 | 
						||
    return get_tmp_string() = "EE";
 | 
						||
  return provincia_di(_com_res); 
 | 
						||
}
 | 
						||
 | 
						||
void TAnagrafica::build_ind_res(const TRectype& rec, const char* ind, const char* civ)
 | 
						||
{
 | 
						||
  TString80 indirizzo = rec.get(ind);
 | 
						||
  if (indirizzo.full())
 | 
						||
  {
 | 
						||
    const TString& numero = rec.get(civ);
 | 
						||
    if (numero.full())
 | 
						||
      indirizzo << ", " << numero;
 | 
						||
    indirizzo.strip_double_spaces();
 | 
						||
    TParagraph_string s(indirizzo, 40);
 | 
						||
    _ind_res = s.get(0);
 | 
						||
  }
 | 
						||
 | 
						||
  // Sistema persone senza stato ma con comune estero
 | 
						||
  if (_stato_estero <= 0 && is_stato_estero(_com_res))
 | 
						||
  {
 | 
						||
    const TString80 naz = comune_di(_com_res, 0);
 | 
						||
    TString query; query.format("USE %%STA KEY 2\nFROM S0=%c\nTO S0=%c", naz[0], naz[0]);
 | 
						||
    TISAM_recordset sta(query);
 | 
						||
    double dBest = 0;
 | 
						||
    int nBest = 0;
 | 
						||
    for (bool ok = sta.move_first(); ok; ok = sta.move_next())
 | 
						||
    {
 | 
						||
      const double s = xvt_str_fuzzy_compare(naz, sta.get("S0").as_string());
 | 
						||
      if (s > dBest)
 | 
						||
      {
 | 
						||
        nBest = sta.get("CODTAB").as_int();
 | 
						||
        dBest = s;
 | 
						||
        if (dBest >= 1.0)
 | 
						||
          break;
 | 
						||
      }
 | 
						||
    }
 | 
						||
    if (dBest > 0.8)
 | 
						||
      _stato_estero = nBest;
 | 
						||
  }
 | 
						||
}
 | 
						||
 | 
						||
const TString& TAnagrafica::cognome() const 
 | 
						||
{ 
 | 
						||
  TString80 c = _ragsoc.left(30); 
 | 
						||
  return get_tmp_string() = c.trim();
 | 
						||
}
 | 
						||
 | 
						||
const TString& TAnagrafica::nome() const 
 | 
						||
{ 
 | 
						||
  CHECK(fisica(), "Non chiedere nome giuridico!"); 
 | 
						||
  return _ragsoc.mid(30,20); 
 | 
						||
}
 | 
						||
 | 
						||
bool TAnagrafica::italiano() const
 | 
						||
{ return _stato_estero == 0 && !is_stato_estero(_com_res); }
 | 
						||
 | 
						||
const TString& TAnagrafica::stato_estero_UNICO() const 
 | 
						||
{
 | 
						||
  if (_stato_estero <= 0)
 | 
						||
    return EMPTY_STRING;
 | 
						||
 | 
						||
  TString4 u; u.format("%03d", _stato_estero);
 | 
						||
  const TString& cod = cache().get("%STA", u, "S8"); // S8=UNICO S9=ISTAT
 | 
						||
  if (cod.len() == 3)
 | 
						||
    return cod;
 | 
						||
  return get_tmp_string() = u;
 | 
						||
}
 | 
						||
 | 
						||
char TAnagrafica::is_black_list(const TDate& datareg) const 
 | 
						||
{
 | 
						||
  char is_black = ' ';
 | 
						||
  if (_stato_estero > 0)
 | 
						||
  {
 | 
						||
    TString4 u; u.format("%03d", _stato_estero);
 | 
						||
    const TRectype& sta = cache().get("%STA", u);
 | 
						||
 | 
						||
    if (sta.get_bool("B1")) // San Marino
 | 
						||
    {
 | 
						||
      if (datareg.date2ansi() < TDate(24,2,2014))
 | 
						||
        is_black = 'M'; 
 | 
						||
    }
 | 
						||
    else
 | 
						||
    {
 | 
						||
      if (sta.get_bool("B0")) // Black List
 | 
						||
        is_black = 'S';
 | 
						||
    }
 | 
						||
  }
 | 
						||
  return is_black;
 | 
						||
}
 | 
						||
 | 
						||
void TAnagrafica::split_ragsoc()
 | 
						||
{
 | 
						||
  if (_tipo == 'F')
 | 
						||
  {
 | 
						||
    if (nome().blank())
 | 
						||
    {
 | 
						||
      const int space = _ragsoc.find(' ');
 | 
						||
      if (space >= 4)
 | 
						||
      {
 | 
						||
        TString80 nom = _ragsoc.mid(space+1);
 | 
						||
        nom.cut(20);
 | 
						||
        _ragsoc.cut(space);
 | 
						||
        _ragsoc.insert(nom, 30);
 | 
						||
      }
 | 
						||
    }
 | 
						||
  }
 | 
						||
  else
 | 
						||
    _ragsoc.strip_double_spaces();
 | 
						||
  _ragsoc.replace('"', '\''); // gli " creano casini nel csv
 | 
						||
}
 | 
						||
 | 
						||
bool TAnagrafica::init(const TRectype& rec)
 | 
						||
{
 | 
						||
  _tipo = '\0';
 | 
						||
  _stato_estero = _stato_nasc = 0;
 | 
						||
  _allegato = 0;
 | 
						||
  _ragsoc.cut(0);
 | 
						||
  if (rec.empty())
 | 
						||
    return false;
 | 
						||
 | 
						||
  switch (rec.num())
 | 
						||
  {
 | 
						||
  case LF_OCCAS:
 | 
						||
    _cofi = rec.get(OCC_COFI);
 | 
						||
    _paiv = rec.get(OCC_PAIV);
 | 
						||
    if (_cofi.blank() || _paiv.blank())
 | 
						||
    {
 | 
						||
      const TString& codice = rec.get(OCC_CFPI);
 | 
						||
      if (_cofi.blank() && cf_check("", codice))
 | 
						||
        _cofi = codice;
 | 
						||
      if (_paiv.blank() && pi_check("", codice))
 | 
						||
        _paiv = codice;
 | 
						||
    }
 | 
						||
    _ragsoc = rec.get(OCC_RAGSOC);
 | 
						||
    _data_nasc = rec.get(OCC_DNASC);
 | 
						||
    _stato_nasc = rec.get_int(OCC_STATONASC);
 | 
						||
    _com_nasc = rec.get(OCC_COMNASC);
 | 
						||
    _stato_estero = rec.get_int(OCC_STATO);
 | 
						||
    _com_res = rec.get(OCC_COM);
 | 
						||
    _loc_res = rec.get(OCC_LOCALITA);
 | 
						||
    build_ind_res(rec, OCC_INDIR, OCC_CIV); 
 | 
						||
    if (_com_nasc.full() || (!_stato_estero && !real::is_natural(_cofi) || cf_check("", _cofi)))
 | 
						||
      _tipo = 'F';
 | 
						||
    else
 | 
						||
      _tipo = 'G';
 | 
						||
    _allegato = _tipo == 'F' ? 6 : 2;
 | 
						||
    break;
 | 
						||
  case LF_ANAG:
 | 
						||
    _tipo = rec.get_char(ANA_TIPOA);
 | 
						||
    _ragsoc = rec.get(ANA_RAGSOC);
 | 
						||
    _cofi = rec.get(ANA_COFI);
 | 
						||
    _paiv = rec.get(ANA_PAIV);   
 | 
						||
    
 | 
						||
    // Comune di residenza fiscale o domicilio
 | 
						||
    _stato_estero = rec.get_int(ANA_STATORES);
 | 
						||
    _com_res = rec.get(ANA_COMRF);
 | 
						||
    if (_com_res.empty())
 | 
						||
      _com_res = rec.get(ANA_COMRES);
 | 
						||
    build_ind_res(rec, ANA_INDRES, ANA_CIVRES); 
 | 
						||
    
 | 
						||
    // Dati di nascita persone fisiche
 | 
						||
    if (_tipo == 'F')
 | 
						||
    {
 | 
						||
      const long codana = rec.get_long(ANA_CODANAGR);
 | 
						||
      if (codana > 0)
 | 
						||
      {
 | 
						||
        const TRectype& anafis = cache().get(LF_ANAGFIS, codana);
 | 
						||
        _data_nasc = anafis.get(ANF_DATANASC);
 | 
						||
        _stato_nasc = anafis.get_int(ANF_STATONASC);
 | 
						||
        _com_nasc = anafis.get(ANF_COMNASC);
 | 
						||
      }
 | 
						||
    }
 | 
						||
    else
 | 
						||
      _tipo = 'G';
 | 
						||
    break;
 | 
						||
  case LF_NDITTE:
 | 
						||
    {
 | 
						||
      const char tipoa = rec.get_char(NDT_TIPOA);
 | 
						||
      const long codana = rec.get_long(NDT_CODANAGR);
 | 
						||
      const bool good = init(LF_ANAG, tipoa, codana);
 | 
						||
      if (tipoa == 'G')
 | 
						||
        _ragsoc = rec.get(NDT_RAGSOC);
 | 
						||
      return good;
 | 
						||
    }
 | 
						||
    break;
 | 
						||
  case LF_CLIFO:
 | 
						||
    _allegato = rec.get_int(CLI_ALLEG);
 | 
						||
    if (_allegato == 4)
 | 
						||
    {
 | 
						||
      const long ca = rec.get_long(CLI_CODALLEG);
 | 
						||
      if (ca > 0)
 | 
						||
        return init(rec.get_char(CLI_TIPOCF), ca, EMPTY_STRING);
 | 
						||
    }
 | 
						||
 
 | 
						||
    _tipo = ' '; // Non faccio assunzioni sulla persona fisica
 | 
						||
 | 
						||
    // Persona Fisica?
 | 
						||
    if (rec.get_char(CLI_TIPOAPER) == 'F' || rec.get(CLI_DATANASC).not_empty()) 
 | 
						||
    {
 | 
						||
      const long cod = rec.get_long(CLI_CODANAGPER);
 | 
						||
      if (cod > 0)
 | 
						||
        init(LF_ANAG, 'F', cod);
 | 
						||
      
 | 
						||
      if (rec.get(CLI_DATANASC).not_empty())
 | 
						||
      {
 | 
						||
        _data_nasc = rec.get(CLI_DATANASC);
 | 
						||
        _tipo = 'F';
 | 
						||
#ifdef DBG
 | 
						||
        if (rec.get(CLI_RAGSOC).starts_with("ACAR"))
 | 
						||
          int cazzone_estero = 1;
 | 
						||
#endif
 | 
						||
      }
 | 
						||
 | 
						||
      if (_com_nasc.blank())
 | 
						||
        _com_nasc = rec.get(CLI_COMNASC);
 | 
						||
      if (_stato_nasc <= 0)
 | 
						||
        _stato_nasc = rec.get_int(CLI_STATONASC);
 | 
						||
      if (_com_nasc.blank() && _stato_nasc > 0)
 | 
						||
      {
 | 
						||
        TString4 sn; sn.format("%03d", _stato_nasc);
 | 
						||
        const TString80 stato = cache().get("%STA", sn, "S0");
 | 
						||
        TISAM_recordset comuni("USE COMUNI\nFROM COM=Z\nTO STATO=\"0~\"");
 | 
						||
        double best = 0.8;
 | 
						||
        for (bool ok = comuni.move_first(); ok; ok = comuni.move_next())
 | 
						||
        {
 | 
						||
          const TString& desc = comuni.get(COM_DENCOM).as_string();
 | 
						||
          const double score = xvt_str_fuzzy_compare_ignoring_case(desc, stato);
 | 
						||
          if (score > best)
 | 
						||
          {
 | 
						||
            best = score;
 | 
						||
            _stato_nasc = 0;
 | 
						||
            _com_nasc = comuni.get(COM_COM).as_string();
 | 
						||
            if (best >= 1.0)
 | 
						||
              break;
 | 
						||
          }
 | 
						||
        }
 | 
						||
      }
 | 
						||
    }
 | 
						||
 | 
						||
    // Assegno codice fiscale e partita IVA se validi, altrimenti mantengo quelli dell'anagrafica
 | 
						||
    if (rec.get(CLI_COFI).not_empty())
 | 
						||
      _cofi = rec.get(CLI_COFI);
 | 
						||
    if (rec.get(CLI_PAIV).not_empty())
 | 
						||
      _paiv = rec.get(CLI_PAIV);
 | 
						||
 | 
						||
    if (_allegato != 5 && _allegato != 9)
 | 
						||
    {
 | 
						||
      const TString4 stato_paiv = rec.get(CLI_STATOPAIV);
 | 
						||
      if (stato_paiv.blank() || stato_paiv == "IT")
 | 
						||
      {
 | 
						||
        // Ci sono anagrafiche con codice fiscale numerico e partita IVA vuota!
 | 
						||
        // I codici fiscali che cominciano per 8 e 9 sono le associazioni riconosciute e non, rispettivamente.
 | 
						||
        if (_paiv.blank() && pi_check(stato_paiv, _cofi) && _cofi[0] < '8')
 | 
						||
          _paiv = _cofi; 
 | 
						||
        if (_paiv.len() == 11 && pi_check(stato_paiv, _paiv) && (_paiv[0] == '8' || _paiv[0] == '9'))
 | 
						||
        {
 | 
						||
          if (_cofi.blank())
 | 
						||
            _cofi = _paiv;
 | 
						||
          _paiv.cut(0);
 | 
						||
        }
 | 
						||
      }
 | 
						||
    }
 | 
						||
 | 
						||
    // Prevale sempre la ragione sociale del cliente: "Il cliente ha sempre ragione".
 | 
						||
    _ragsoc = rec.get(CLI_RAGSOC); 
 | 
						||
    _loc_res = rec.get(CLI_LOCCF);
 | 
						||
 | 
						||
    _stato_estero = rec.get_int(CLI_STATOCF);
 | 
						||
    if (_stato_estero <= 0 && (_allegato == 5 || _allegato == 9)) // Estero CEE o extra-CEE
 | 
						||
      _stato_estero = -1; // Stato estero ignoto
 | 
						||
    _com_res = rec.get(CLI_COMCF);
 | 
						||
    build_ind_res(rec, CLI_INDCF, CLI_CIVCF); 
 | 
						||
 | 
						||
    if (_tipo != 'F' && _tipo != 'G')
 | 
						||
    {
 | 
						||
      switch (_allegato)
 | 
						||
      {
 | 
						||
      case  6: _tipo = 'F'; break; // Privato
 | 
						||
      case  7: _tipo = 'G'; break; // Ente pubblico
 | 
						||
      case  8: _tipo = 'G'; break; // Fallimento
 | 
						||
      default: 
 | 
						||
        if (_com_nasc.full())
 | 
						||
          _tipo = 'F';
 | 
						||
        else
 | 
						||
        {
 | 
						||
          _tipo = rec.get_char(CLI_TIPOPERS);
 | 
						||
          if (_tipo != 'F')
 | 
						||
            _tipo = 'G';
 | 
						||
        }
 | 
						||
        break;
 | 
						||
      }
 | 
						||
    }
 | 
						||
    break;
 | 
						||
  case LF_MOV:
 | 
						||
    return init(rec.get_char(MOV_TIPO), rec.get_long(MOV_CODCF), rec.get(MOV_OCFPI));
 | 
						||
  case LF_ALLEG:
 | 
						||
    return init(rec.get_char(ALL_TIPOCF), rec.get_long(ALL_CODCF), rec.get(ALL_OCFPI));
 | 
						||
  default:
 | 
						||
    CHECKD(false, "Record non valido per TAnagrafica ", rec.num());
 | 
						||
    break;
 | 
						||
  }
 | 
						||
  split_ragsoc();
 | 
						||
  
 | 
						||
  return _tipo == 'F' || _tipo == 'G';
 | 
						||
}
 | 
						||
 | 
						||
bool TAnagrafica::init(int num, const TString& codice) 
 | 
						||
{ return init(cache().get(num, codice)); }
 | 
						||
 | 
						||
bool TAnagrafica::init(int num, long codice) 
 | 
						||
{ return init(cache().get(num, codice)); }
 | 
						||
 | 
						||
bool TAnagrafica::init(int num, char tipo, long codice)
 | 
						||
{
 | 
						||
  TString8 key; key.format("%c|%ld", tipo, codice);
 | 
						||
  return init(cache().get(num, key));
 | 
						||
}
 | 
						||
 | 
						||
bool TAnagrafica::init(char tipocf, long codice, const TString& ocfpi)
 | 
						||
{
 | 
						||
  bool done = false;
 | 
						||
  if (ocfpi.full())
 | 
						||
    done = init(LF_OCCAS, ocfpi);
 | 
						||
  if (!done)
 | 
						||
  {
 | 
						||
    CHECK(tipocf == 'C' || tipocf == 'F', "Tipo cli/for non valido");
 | 
						||
    CHECKD(codice > 0, "Codice cli/for non valido", codice);
 | 
						||
    done = init(LF_CLIFO, tipocf, codice);
 | 
						||
  }
 | 
						||
  return done;
 | 
						||
}
 | 
						||
 | 
						||
///////////////////////////////////////////////////////////
 | 
						||
// TSpesometro_set
 | 
						||
///////////////////////////////////////////////////////////
 | 
						||
 | 
						||
const TVariant& TSpesometro_set::get(unsigned int column) const
 | 
						||
{
 | 
						||
  const TRecordset_column_info& info = column_info(column);
 | 
						||
  if (info._type == _realfld || info._type == _datefld)
 | 
						||
  {
 | 
						||
    TString80 val = row().get(column);
 | 
						||
    TVariant& var = get_tmp_var();
 | 
						||
    if (info._type == _realfld)
 | 
						||
    {
 | 
						||
      val.replace(',', '.');
 | 
						||
      var = real(val);
 | 
						||
    }
 | 
						||
    else
 | 
						||
    {
 | 
						||
      if (val.len() == 8)
 | 
						||
      {
 | 
						||
        val.insert("-", 2);
 | 
						||
        val.insert("-", 5);
 | 
						||
      }
 | 
						||
      var = TDate(val);
 | 
						||
    }
 | 
						||
    return var;
 | 
						||
  }
 | 
						||
  return TCSV_recordset::get(column);
 | 
						||
}
 | 
						||
 | 
						||
bool TSpesometro_set::set(unsigned int fld, const TVariant& var)
 | 
						||
{
 | 
						||
  const TRecordset_column_info& fi = column_info(fld);
 | 
						||
 | 
						||
  // Salva le date in formato GGMMAAAA invece dello standard ANSI AAAAMMGG
 | 
						||
  if (fi._type == _datefld) 
 | 
						||
  {
 | 
						||
    const TDate d = var.as_date();
 | 
						||
    if (d.ok())
 | 
						||
    {
 | 
						||
      TFixed_string str = d.string(full, '\0', full, full, gma_date);
 | 
						||
      row().add(str, fld);
 | 
						||
    }
 | 
						||
    else
 | 
						||
      row().add("", fld);
 | 
						||
    return true;
 | 
						||
  } else
 | 
						||
  // Salva gli importi in italiano (non possono essere negativi)
 | 
						||
  if (fi._type == _realfld)
 | 
						||
  {
 | 
						||
    real r = var.as_real();
 | 
						||
    row().add(r.stringa(0, 2), fld);
 | 
						||
  } else
 | 
						||
  if (fi._type == _boolfld)
 | 
						||
  {
 | 
						||
    row().add(var.as_bool() ? "S" : " ", fld);
 | 
						||
  }
 | 
						||
  else
 | 
						||
  {
 | 
						||
    TString256 str = var.as_string();
 | 
						||
    str.upper();
 | 
						||
    str.replace('<EFBFBD>', 'A');
 | 
						||
    str.replace('<EFBFBD>', 'N');
 | 
						||
    str.replace('<EFBFBD>', 'O');
 | 
						||
    str.replace('<EFBFBD>', 'U');
 | 
						||
    row().add(str, fld);
 | 
						||
  }
 | 
						||
 | 
						||
  return true;
 | 
						||
}
 | 
						||
 | 
						||
void TSpesometro_set::add_header_data(int n, const TString& value)
 | 
						||
{
 | 
						||
  CHECKD(n > 0 && n <= 16, "Bad SO record type ", n);
 | 
						||
  TString8 fld; fld.format("SO%03d", n);
 | 
						||
  new_rec(fld); 
 | 
						||
  set(1, value);
 | 
						||
}
 | 
						||
 | 
						||
const TString& TSpesometro_set::get_recapito(const TRectype& ndt, const char* numero, const char* prefisso) const
 | 
						||
{
 | 
						||
  if (ndt.exist(numero))
 | 
						||
  {
 | 
						||
    TString& tel = get_tmp_string();
 | 
						||
    if (prefisso && *prefisso)
 | 
						||
      tel = ndt.get(prefisso);
 | 
						||
    tel << ndt.get(numero);
 | 
						||
    if (tel.full())
 | 
						||
      return tel;
 | 
						||
  }
 | 
						||
 | 
						||
  if (ndt.num() == LF_NDITTE)
 | 
						||
  {
 | 
						||
    TString16 key;
 | 
						||
    key.format("%c|%ld", ndt.get_char(NDT_TIPOA), ndt.get_long(NDT_CODANAGR));
 | 
						||
    const TRectype& anag = cache().get(LF_ANAG, key);
 | 
						||
    if (prefisso)
 | 
						||
    {
 | 
						||
      if (strcmp(numero, "TEL") == 0)
 | 
						||
      {
 | 
						||
        numero = "TELRF";
 | 
						||
        prefisso = "PTELRF";
 | 
						||
      } else
 | 
						||
      if (strcmp(numero, "FAX") == 0)
 | 
						||
      {
 | 
						||
        numero = "FAXRF";
 | 
						||
        prefisso = "PFAXRF";
 | 
						||
      }
 | 
						||
    }
 | 
						||
    return get_recapito(anag, numero, prefisso);
 | 
						||
  }
 | 
						||
  
 | 
						||
  return EMPTY_STRING;
 | 
						||
}
 | 
						||
 | 
						||
bool TSpesometro_set::add_header(const TMask& msk)
 | 
						||
{
 | 
						||
  const long codditta = prefix().get_codditta();
 | 
						||
  const TAnagrafica ditta(LF_NDITTE, codditta);
 | 
						||
 | 
						||
  add_header_data(1, ditta.codice_fiscale());
 | 
						||
  add_header_data(2, ditta.partita_IVA());
 | 
						||
 | 
						||
  TString16 key; 
 | 
						||
  key.format("%ld", codditta);
 | 
						||
  const TRectype& ndt = cache().get(LF_NDITTE, key);
 | 
						||
  const char* codatt = ndt.get(NDT_CODATTPREV);
 | 
						||
  key.format("%ld|%s", codditta, codatt);
 | 
						||
  const TRectype& att = cache().get(LF_ATTIV, key);
 | 
						||
  const TString8 ateco = att.get(ATT_CODATECO);
 | 
						||
  add_header_data(3, ateco);
 | 
						||
 | 
						||
  add_header_data(4, get_recapito(ndt, NDT_TEL, NDT_PTEL));
 | 
						||
  add_header_data(5, get_recapito(ndt, NDT_FAX, NDT_PFAX));
 | 
						||
  add_header_data(6, get_recapito(ndt, "MAIL", NULL));
 | 
						||
 | 
						||
  if (ditta.fisica())
 | 
						||
  {
 | 
						||
    add_header_data( 7, ditta.cognome());
 | 
						||
    add_header_data( 8, ditta.nome());
 | 
						||
    add_header_data( 9, ditta.sesso() == 'F' ? "F" : "M");
 | 
						||
    add_header_data(10, ditta.data_nascita().string(full, '\0', full, full, gma_date));
 | 
						||
    add_header_data(11, ditta.comune_nascita());
 | 
						||
    add_header_data(12, ditta.provincia_nascita());
 | 
						||
    add_header_data(13, EMPTY_STRING);
 | 
						||
  }
 | 
						||
  else
 | 
						||
  {
 | 
						||
    add_header_data( 7, EMPTY_STRING);
 | 
						||
    add_header_data( 8, EMPTY_STRING);
 | 
						||
    add_header_data( 9, EMPTY_STRING);
 | 
						||
    add_header_data(10, EMPTY_STRING);
 | 
						||
    add_header_data(11, EMPTY_STRING);
 | 
						||
    add_header_data(12, EMPTY_STRING);
 | 
						||
    add_header_data(13, ditta.ragione_sociale());
 | 
						||
  }
 | 
						||
 | 
						||
  add_header_data(14, msk.get(F_ANNO));
 | 
						||
  add_header_data(15, msk.get(F_TIPO)=="BL" ? msk.get(F_PERIODO) : EMPTY_STRING);
 | 
						||
  add_header_data(16, TFixed_string("04879210963"));
 | 
						||
 | 
						||
  if (ateco.blank())
 | 
						||
    return error_box("Manca il codice attivit<69> ATECO nell'anagrafica della ditta %ld", codditta);
 | 
						||
 | 
						||
  return true;
 | 
						||
}
 | 
						||
 | 
						||
bool TSpesometro_set::add_footer(const TMask& /*msk*/)
 | 
						||
{
 | 
						||
  // Elimina elementi composti da un solo spazio
 | 
						||
  for (TRecnotype i = items()-1; i >= 0; i--)
 | 
						||
  {
 | 
						||
    TToken_string& r = row(i);
 | 
						||
    if (r.empty_items())
 | 
						||
    {
 | 
						||
      destroy(i);
 | 
						||
      continue;
 | 
						||
    }
 | 
						||
 | 
						||
    for (;;)
 | 
						||
    {
 | 
						||
      const int p = r.find("| |");
 | 
						||
      if (p > 0)
 | 
						||
      {
 | 
						||
        r.overwrite(r.mid(p+2), p+1);
 | 
						||
        r.rtrim(1);
 | 
						||
      }
 | 
						||
      else
 | 
						||
        break;
 | 
						||
    }
 | 
						||
    r.rtrim();
 | 
						||
  }
 | 
						||
  return true;
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
void TSpesometro_set::init()
 | 
						||
{
 | 
						||
  create_column("Tipo",           _alfafld); // 0
 | 
						||
  create_column("PartitaIVA",     _alfafld);
 | 
						||
  create_column("CodiceFiscale",  _alfafld);
 | 
						||
  create_column("IVANonEsposta",  _boolfld);
 | 
						||
  create_column("Noleggio",       _alfafld); 
 | 
						||
  create_column("Autofattura",    _boolfld); // 5
 | 
						||
  create_column("ReverseCharge",  _boolfld);
 | 
						||
  create_column("DataDoc",        _datefld);
 | 
						||
  create_column("DataReg",        _datefld);
 | 
						||
  create_column("NumDoc",         _alfafld);
 | 
						||
  create_column("TipoOperazione", _alfafld); // 10
 | 
						||
  create_column("TipoDocumento",  _alfafld); 
 | 
						||
  create_column("Imponibile",     _realfld); // 12
 | 
						||
  create_column("Imposta",        _realfld);
 | 
						||
  
 | 
						||
  create_column("Cognome",     _alfafld);    // 14
 | 
						||
  create_column("Nome",        _alfafld);
 | 
						||
  create_column("DataNasc",    _datefld);
 | 
						||
  create_column("ComNasc",     _alfafld);
 | 
						||
  create_column("Provincia",   _alfafld);
 | 
						||
  create_column("StatoEstero", _alfafld);    // 19
 | 
						||
  create_column("CittaEstera", _alfafld);
 | 
						||
  create_column("IndirEstero", _alfafld);
 | 
						||
 | 
						||
  create_column("RagSoc",           _alfafld); // 22
 | 
						||
  create_column("CittaEsteraSede",  _alfafld);
 | 
						||
  create_column("StatoEsteroSede",  _alfafld);
 | 
						||
  create_column("IndirEsteroSede",  _alfafld);
 | 
						||
  create_column("CodiceIVAEstero",  _alfafld);
 | 
						||
 | 
						||
  create_column("BlackList",        _alfafld); // 27  S o M o blank
 | 
						||
  create_column("NonResidente",     _boolfld);
 | 
						||
  create_column("AcquistoDaNonRes", _boolfld);
 | 
						||
  create_column("TipoImponibile",   _alfafld);
 | 
						||
 | 
						||
  create_column("OperazAggAttive",  _intfld); // 31
 | 
						||
  create_column("OperazAggPassive", _intfld);
 | 
						||
 | 
						||
  create_column("FatturaRiepilogativa", _boolfld);
 | 
						||
  create_column("SchedaCarburante",     _boolfld); // 34
 | 
						||
}
 | 
						||
 | 
						||
TSpesometro_set::TSpesometro_set(const TFilename& file) : TCSV_recordset("CSV(|)")
 | 
						||
{
 | 
						||
  init();
 | 
						||
  load_file(file);
 | 
						||
  move_first();
 | 
						||
}
 | 
						||
 | 
						||
TSpesometro_set::TSpesometro_set() : TCSV_recordset("CSV(|)")
 | 
						||
{
 | 
						||
  init();
 | 
						||
}
 | 
						||
 | 
						||
///////////////////////////////////////////////////////////
 | 
						||
// TCofi_cache
 | 
						||
///////////////////////////////////////////////////////////
 | 
						||
 | 
						||
TObject* TCofi_cache::find_ragsoc(bool piva, char tipocf, const TString& code)
 | 
						||
{
 | 
						||
  const int fkey = piva ? 5 : 4;
 | 
						||
  const char* kfield = piva ? CLI_PAIV : CLI_COFI;
 | 
						||
  _clifo.setkey(fkey);
 | 
						||
  _clifo.zero();
 | 
						||
  _clifo.put(CLI_TIPOCF, tipocf);
 | 
						||
  _clifo.put(kfield, code);
 | 
						||
  
 | 
						||
  int err = _clifo.read();
 | 
						||
 | 
						||
  if (err == NOERR && _clifo.get_int(CLI_ALLEG) == 4) // Codice Fiscale o Partita IVA doppia
 | 
						||
  {
 | 
						||
    const long codall = _clifo.get_long(CLI_CODALLEG);
 | 
						||
    if (codall > 0)
 | 
						||
    {
 | 
						||
      const TRecnotype recno = _clifo.recno();
 | 
						||
      _clifo.setkey(1);
 | 
						||
      _clifo.put(CLI_TIPOCF, tipocf);
 | 
						||
      _clifo.put(CLI_CODCF, codall);
 | 
						||
      err = _clifo.read();
 | 
						||
      if (err != NOERR || _clifo.get(kfield) != code)
 | 
						||
        err = _clifo.readat(recno);
 | 
						||
      _clifo.setkey(fkey);
 | 
						||
    }
 | 
						||
  }
 | 
						||
 | 
						||
  if (err != NOERR && piva && pi_check("IT", code))
 | 
						||
  {
 | 
						||
    _clifo.put(CLI_TIPOCF, tipocf);
 | 
						||
    _clifo.put(CLI_STATOPAIV, "IT");
 | 
						||
    _clifo.put(CLI_PAIV, code);
 | 
						||
    err = _clifo.read();
 | 
						||
  }
 | 
						||
 | 
						||
  if (err == NOERR)
 | 
						||
    return new TString80(_clifo.get(CLI_RAGSOC));
 | 
						||
 | 
						||
  if (piva && !pi_check("IT", code)) // cerco partite IVA estere
 | 
						||
  {
 | 
						||
    TString query;
 | 
						||
    query << "USE CLIFO SELECT PAIV=\"" << code << '"'
 | 
						||
          << "\nFROM TIPOCF=" << tipocf
 | 
						||
          << "\nTO TIPOCF=" << tipocf;
 | 
						||
    TISAM_recordset clifo(query);
 | 
						||
    if (clifo.move_first())
 | 
						||
      return new TString80(clifo.get(CLI_RAGSOC).as_string());
 | 
						||
  }
 | 
						||
 | 
						||
  _occas.put(OCC_CFPI, code);
 | 
						||
  if (_occas.read() == NOERR)
 | 
						||
  {
 | 
						||
    const TString& cfpi = _occas.get(piva ? OCC_PAIV : OCC_COFI);
 | 
						||
    if (code == cfpi || cfpi.empty())
 | 
						||
      return new TString80(_occas.get(OCC_RAGSOC));
 | 
						||
  }
 | 
						||
 | 
						||
  TString query;
 | 
						||
  query << "USE OCCAS SELECT ";
 | 
						||
  if (piva)
 | 
						||
    query << "PAIV=\"" << code << '"';
 | 
						||
  else
 | 
						||
    query << "COFI=\"" << code << '"';
 | 
						||
  TISAM_recordset occas(query);
 | 
						||
  if (occas.move_first())
 | 
						||
    return new TString80(occas.get(OCC_RAGSOC).as_string());
 | 
						||
  
 | 
						||
  return NULL;
 | 
						||
}
 | 
						||
 | 
						||
TObject* TCofi_cache::key2obj(const char* key)
 | 
						||
{
 | 
						||
  TToken_string chiave(key);
 | 
						||
  const bool paiv = chiave.get_char(0)=='P';
 | 
						||
  const char tipocf = chiave.get_char(1);
 | 
						||
  TString16 code; chiave.get(2, code);
 | 
						||
 | 
						||
  TObject* ragsoc = NULL;
 | 
						||
  if (code.full())
 | 
						||
  {
 | 
						||
    if (tipocf != 'C' && tipocf != 'F')
 | 
						||
    {
 | 
						||
      ragsoc = find_ragsoc(paiv, 'C', code);
 | 
						||
      if (ragsoc == NULL)
 | 
						||
        ragsoc = find_ragsoc(paiv, 'F', code);
 | 
						||
    }
 | 
						||
    else
 | 
						||
      ragsoc = find_ragsoc(paiv, tipocf, code);
 | 
						||
  }
 | 
						||
  return ragsoc;
 | 
						||
}
 | 
						||
 | 
						||
const TString& TCofi_cache::cofi2ragsoc(char tipocf, const TString& cofi) 
 | 
						||
{ 
 | 
						||
  const TString* ragsoc = NULL;
 | 
						||
  if (cofi.full())
 | 
						||
  {
 | 
						||
    TString80 key;
 | 
						||
    key.format("CF|%c|%s", tipocf, (const char*)cofi);
 | 
						||
    ragsoc = (const TString*)objptr(key); 
 | 
						||
  }
 | 
						||
  if (ragsoc == NULL)
 | 
						||
  {
 | 
						||
    TString& str = get_tmp_string();
 | 
						||
    str << TR("C.F. sconosciuto ") << cofi;
 | 
						||
    ragsoc = &str;
 | 
						||
  }
 | 
						||
  return *ragsoc;
 | 
						||
}
 | 
						||
 | 
						||
const TString& TCofi_cache::paiv2ragsoc(char tipocf, const TString& paiv) 
 | 
						||
{ 
 | 
						||
  const TString* ragsoc = NULL;
 | 
						||
  if (paiv.full())
 | 
						||
  {
 | 
						||
    TString80 key;
 | 
						||
    key.format("PI|%c|%s", tipocf, (const char*)paiv);
 | 
						||
    ragsoc = (const TString*)objptr(key); 
 | 
						||
  }
 | 
						||
  if (ragsoc == NULL)
 | 
						||
  {
 | 
						||
    TString& str = get_tmp_string();
 | 
						||
    str << TR("P.I. sconosciuta ") << paiv;
 | 
						||
    ragsoc = &str;
 | 
						||
  }
 | 
						||
  return *ragsoc;
 | 
						||
}
 | 
						||
 | 
						||
///////////////////////////////////////////////////////////
 | 
						||
// TSpesometro_rep
 | 
						||
///////////////////////////////////////////////////////////
 | 
						||
 | 
						||
bool TSpesometro_rep::get_usr_val(const TString& name, TVariant& var) const
 | 
						||
{
 | 
						||
  if (name == "ANNO")
 | 
						||
  {
 | 
						||
    const TFixed_string d("DataReg");
 | 
						||
    TVariant var;
 | 
						||
    if (!get_record_field(d, var))
 | 
						||
      var = "2013";
 | 
						||
    else
 | 
						||
      var = var.as_string().right(4);
 | 
						||
    return true;
 | 
						||
  } 
 | 
						||
 | 
						||
  return TReport::get_usr_val(name, var);
 | 
						||
}
 | 
						||
 | 
						||
TSpesometro_rep::TSpesometro_rep(const TFilename& file)
 | 
						||
{
 | 
						||
  load("fe0100");
 | 
						||
 | 
						||
  TSpesometro_set* set = new TSpesometro_set(file);
 | 
						||
  
 | 
						||
  // Elimina dati di testata e coda
 | 
						||
  for (TRecnotype i = set->items()-1; i >= 0; i--)
 | 
						||
  {
 | 
						||
    if (set->move_to(i))
 | 
						||
    {
 | 
						||
      const TString& t = set->get(0).as_string();
 | 
						||
      if (t.blank() || t.starts_with("SO"))
 | 
						||
        set->destroy(i);
 | 
						||
    }
 | 
						||
    else
 | 
						||
      break;
 | 
						||
  }
 | 
						||
  
 | 
						||
  set_recordset(set);
 | 
						||
}
 |