git-svn-id: svn://10.65.10.50/branches/R_10_00@23010 c028cbd2-c16b-5b4b-a496-9718f37d4682
		
			
				
	
	
		
			416 lines
		
	
	
		
			9.8 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			416 lines
		
	
	
		
			9.8 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
#include "tc0.h"
 | 
						|
#include "tc0700.h"
 | 
						|
#include "tc0700a.h"
 | 
						|
 | 
						|
#include <progind.h>
 | 
						|
#include <reprint.h>
 | 
						|
#include <utility.h>
 | 
						|
 | 
						|
#include <anagr.h>
 | 
						|
#include <anafis.h>
 | 
						|
#include <clifo.h>
 | 
						|
#include <comuni.h>
 | 
						|
#include <causali.h>
 | 
						|
#include <nditte.h>
 | 
						|
#include <attiv.h>
 | 
						|
#include <mov.h>
 | 
						|
#include <pconti.h>
 | 
						|
#include <rmov.h>
 | 
						|
#include <rmoviva.h>
 | 
						|
#include <multirel.h>
 | 
						|
 | 
						|
#include "../cg/cg2103.h"
 | 
						|
 | 
						|
bool TTS_mask::apply_filter(const TRecordset& righe) const
 | 
						|
{
 | 
						|
  const TDate dal = get(F_DATA_DA);
 | 
						|
  const TDate al = get(F_DATA_AL);
 | 
						|
  if (dal.ok() || al.ok())
 | 
						|
  {
 | 
						|
    const TDate scad = righe.get("DATASCAD").as_date();
 | 
						|
    if ((dal.ok() && scad < dal) || (al.ok() && scad > al))
 | 
						|
      return false;
 | 
						|
  }
 | 
						|
  return true;
 | 
						|
}
 | 
						|
 | 
						|
bool TTS_mask::on_field_event(TOperable_field& o, TField_event e, long jolly)
 | 
						|
{
 | 
						|
  bool ok = true;
 | 
						|
  switch (o.dlg())
 | 
						|
  {
 | 
						|
  case F_DATA_DA:
 | 
						|
  case F_DATA_AL:
 | 
						|
    break;
 | 
						|
  default:
 | 
						|
    break;
 | 
						|
  }
 | 
						|
  return ok;
 | 
						|
}
 | 
						|
 | 
						|
void TTS_mask::serialize(bool bSave)
 | 
						|
{
 | 
						|
  const char* defpar = "tc";
 | 
						|
  TConfig ini(CONFIG_DITTA, defpar);
 | 
						|
  for (int i = fields()-1; i >= 0; i--)
 | 
						|
  {
 | 
						|
    TMask_field& f = fld(i);
 | 
						|
    const TFieldref* fr = f.field();
 | 
						|
    if (fr != NULL)
 | 
						|
    {
 | 
						|
      if (bSave)
 | 
						|
        fr->write(ini, defpar, f.get());
 | 
						|
      else
 | 
						|
        f.set(fr->read(ini, defpar));
 | 
						|
    }
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
TTS_mask::TTS_mask(const char * maskname) 
 | 
						|
         : TAutomask(maskname), _updating(false), _next_update(0)
 | 
						|
{
 | 
						|
  serialize(false);
 | 
						|
}
 | 
						|
 | 
						|
TTS_mask::~TTS_mask()
 | 
						|
{
 | 
						|
  serialize(true);
 | 
						|
}
 | 
						|
 | 
						|
bool TTS_campo_sender::destroy()
 | 
						|
{
 | 
						|
	delete _mask;
 | 
						|
	return TTS_sender::destroy();
 | 
						|
}
 | 
						|
 | 
						|
TRecordset & TTS_campo_sender::movrecset()
 | 
						|
{
 | 
						|
	const TMask & m = get_mask();
 | 
						|
	const TDate from = m.get_date(F_DATA_DA);
 | 
						|
	const TDate to = m.get_date(F_DATA_AL);
 | 
						|
	TRecordset * mov = get_recset(LF_MOV);
 | 
						|
 | 
						|
	if (mov == NULL)
 | 
						|
	{
 | 
						|
		TString query = "USE MOV KEY 2 SELECT INVIATO!=\"X\"";           
 | 
						|
 | 
						|
		query << "\nFROM DATAREG=#DATA_DA";      
 | 
						|
		query << "\nTO DATAREG=#DATA_AL";      
 | 
						|
	
 | 
						|
		mov = set_recset(LF_MOV, ::create_recordset(query));
 | 
						|
	}
 | 
						|
	mov->set_var("#DATA_DA", TVariant(from));
 | 
						|
  mov->set_var("#DATA_AL", TVariant(to));
 | 
						|
	return *mov;
 | 
						|
}
 | 
						|
 | 
						|
TRecordset & TTS_campo_sender::rmovrecset(const TRecordset & mov)
 | 
						|
{
 | 
						|
	TRecordset* rmov = get_recset(LF_RMOV);
 | 
						|
 | 
						|
	if (rmov == NULL)
 | 
						|
		rmov = set_recset(LF_RMOV, ::create_recordset("USE RMOV\nFROM NUMREG=#NUMREG\nTO NUMREG=#NUMREG"));
 | 
						|
 | 
						|
	const TVariant& numreg = mov.get(MOV_NUMREG);
 | 
						|
	if (numreg != rmov->get("#NUMREG"))
 | 
						|
		rmov->set_var("#NUMREG", numreg);
 | 
						|
 | 
						|
  const int numrig = rmov->items();
 | 
						|
  if (numrig > 80)
 | 
						|
  {
 | 
						|
    TString msg;
 | 
						|
    msg.format(FR("Registrazione con %d righe contabili. (Max. 80)"), numrig);
 | 
						|
    log(2, msg);
 | 
						|
  }
 | 
						|
	
 | 
						|
  return *rmov;
 | 
						|
}
 | 
						|
 | 
						|
TRecordset& TTS_campo_sender::rivarecset(const TRecordset & mov)
 | 
						|
{
 | 
						|
	TRecordset* rmoviva = get_recset(LF_RMOVIVA);
 | 
						|
 | 
						|
	if (rmoviva == NULL)
 | 
						|
		rmoviva = set_recset(LF_RMOVIVA, ::create_recordset("USE RMOVIVA\nFROM NUMREG=#NUMREG\nTO NUMREG=#NUMREG"));
 | 
						|
 | 
						|
	const TVariant& numreg = mov.get(MOV_NUMREG);
 | 
						|
	if (numreg != rmoviva->get("#NUMREG"))
 | 
						|
		rmoviva->set_var("#NUMREG", numreg);
 | 
						|
 | 
						|
  return *rmoviva;
 | 
						|
}
 | 
						|
 | 
						|
const TRecordset& TTS_campo_sender::clirecset(const char tipocf, const long codcf)
 | 
						|
{
 | 
						|
	TRecordset * clifo = get_recset(LF_CLIFO);
 | 
						|
 | 
						|
	if (clifo == NULL)
 | 
						|
		clifo = set_recset(LF_CLIFO, ::create_recordset("USE CLIFO\nFROM TIPOCF=#TIPOCF CODCF=#CODCF\nTO TIPOCF=#TIPOCF CODCF=#CODCF"));
 | 
						|
	TString4 w; w << tipocf;
 | 
						|
	TVariant t(w);
 | 
						|
 | 
						|
	clifo->set_var("#TIPOCF", t);
 | 
						|
	clifo->set_var("#CODCF", codcf);
 | 
						|
  clifo->move_first();
 | 
						|
	return *clifo;
 | 
						|
}
 | 
						|
 | 
						|
bool TTS_campo_sender::test_swap(const TRecordset& mov)
 | 
						|
{
 | 
						|
	TRecordset & rmov = rmovrecset(mov);
 | 
						|
	const int anno = mov.get(MOV_ANNOIVA).as_int();
 | 
						|
	TCausale caus(mov.get(MOV_CODCAUS).as_string(), anno);
 | 
						|
	char sez = caus.sezione_clifo();
 | 
						|
  const bool vendite = mov.get(MOV_TIPO).as_string()[0] != 'F';
 | 
						|
  
 | 
						|
  const bool s = vendite ^ (sez == 'D');
 | 
						|
  return s;
 | 
						|
}
 | 
						|
 | 
						|
bool TTS_campo_sender::swap_ritsoc(const TRecordset& mov)
 | 
						|
{
 | 
						|
	TRecordset & rmov = rmovrecset(mov);
 | 
						|
  bool ok = rmov.move_first();
 | 
						|
  const char sez = rmov.get(RMV_SEZIONE).as_string()[0];
 | 
						|
	char sezrit = sez;
 | 
						|
 | 
						|
	for (; ok; ok = rmov.move_next())
 | 
						|
	{
 | 
						|
		const char tiporiga = rmov.get(RMV_ROWTYPE).as_string()[0];
 | 
						|
 | 
						|
		if (tiporiga =='S')
 | 
						|
		{
 | 
						|
		  sezrit = rmov.get(RMV_SEZIONE).as_string()[0];
 | 
						|
			break;
 | 
						|
		}
 | 
						|
	}
 | 
						|
	
 | 
						|
  const bool s = sez != sezrit;
 | 
						|
  return s;
 | 
						|
}
 | 
						|
 | 
						|
bool TTS_campo_sender::mov_regolarizzazione(const TRecordset& mov)
 | 
						|
{
 | 
						|
  const TString& codcaus = mov.get(MOV_CODCAUS).as_string();
 | 
						|
	return _caus_regolarizzazione.is_key(codcaus);
 | 
						|
}
 | 
						|
 | 
						|
bool TTS_campo_sender::mov_intra(const TRecordset& mov)
 | 
						|
{
 | 
						|
  const TRectype& caus = cache().get(LF_CAUSALI, mov.get(MOV_CODCAUS).as_string());
 | 
						|
	
 | 
						|
	return caus.get_bool(CAU_INTRACOM);
 | 
						|
}
 | 
						|
 | 
						|
bool TTS_campo_sender::mov_reverse_charge(const TRecordset& mov)
 | 
						|
{
 | 
						|
  const TRectype& caus = cache().get(LF_CAUSALI, mov.get(MOV_CODCAUS).as_string());
 | 
						|
	const int	regsp = caus.get_int(CAU_REGSPIVA);
 | 
						|
 | 
						|
	return regsp == 13 || regsp == 50 || regsp == 51;
 | 
						|
}
 | 
						|
 | 
						|
bool TTS_campo_sender::regime_agricolo(const TRecordset& mov) const
 | 
						|
{
 | 
						|
	TToken_string key;
 | 
						|
	
 | 
						|
	key.format("%04d%s", mov.get(MOV_ANNOIVA).as_int(), (const char *) mov.get(MOV_REG).as_string());
 | 
						|
 | 
						|
	const TRectype& reg = cache().get("REG", key);
 | 
						|
 | 
						|
	key.cut(0);
 | 
						|
	key.add(prefix().get_codditta());
 | 
						|
	key.add(reg.get("S8"));
 | 
						|
	const TRectype& attiv = cache().get(LF_ATTIV, key);
 | 
						|
	return attiv.get_bool(ATT_REGAGR);
 | 
						|
}
 | 
						|
 | 
						|
TMask & TTS_campo_sender::get_mask()
 | 
						|
{
 | 
						|
	if (_mask == NULL)
 | 
						|
	{
 | 
						|
		_mask = new TTS_mask(mask_name());
 | 
						|
		TDate to(TODAY);
 | 
						|
		TConfig configtc(CONFIG_DITTA, "tc");
 | 
						|
		const TDate from(configtc.get("TSULTINV"));
 | 
						|
 | 
						|
		if (from.ok())
 | 
						|
			to.addmonth(-1);
 | 
						|
		to.set_end_month();
 | 
						|
		if (to <= from)
 | 
						|
		{
 | 
						|
			to = from;
 | 
						|
			to.addmonth(1);
 | 
						|
			to.set_end_month();
 | 
						|
		}
 | 
						|
		_mask->set(F_DATA_DA, from);
 | 
						|
		_mask->set(F_DATA_AL, to);
 | 
						|
	}
 | 
						|
	return * _mask;
 | 
						|
}
 | 
						|
	
 | 
						|
bool TTS_campo_sender::find_regolarizzazione(TRecordset& movset)
 | 
						|
{
 | 
						|
	bool found = false;
 | 
						|
  
 | 
						|
  const TRecnotype pos = movset.current_row(); // Salva la posizione
 | 
						|
 | 
						|
  const TRectype& mov = ((TISAM_recordset&)movset).cursor()->curr();
 | 
						|
  
 | 
						|
	const TString4 causreg = cache().get(LF_CAUSALI, mov.get(MOV_CODCAUS), CAU_CODCAUREG);
 | 
						|
	real totdoc = mov.get_real(MOV_TOTDOC);
 | 
						|
	const real ritfis = mov.get_real(MOV_RITFIS);
 | 
						|
	const real ritsoc = mov.get_real(MOV_RITSOC);
 | 
						|
 | 
						|
	if (!ritfis.is_zero())
 | 
						|
		totdoc += ritfis;
 | 
						|
  if (!ritsoc.is_zero())
 | 
						|
	{
 | 
						|
		if (swap_ritsoc(movset))                      // Somma ritenute sociali con segno
 | 
						|
			totdoc -= ritsoc;
 | 
						|
	  else
 | 
						|
		  totdoc += ritsoc;                        
 | 
						|
	}
 | 
						|
  if (test_swap(movset))
 | 
						|
		totdoc = -totdoc;
 | 
						|
 | 
						|
	TToken_string key(mov.get(MOV_TIPO));
 | 
						|
	key.add(mov.get(MOV_CODCF));
 | 
						|
	const TRectype & cli = cache().get(LF_CLIFO, key);
 | 
						|
	const TString16 paiv = cli.get(CLI_PAIV);
 | 
						|
	const TString16 cf = cli.get(CLI_COFI);
 | 
						|
	TEsercizi_contabili esc;
 | 
						|
	const TEsercizio& e = esc.esercizio(esc.date2esc(mov.get_date(MOV_DATAREG)));
 | 
						|
	const TDate dfin = e.fine();
 | 
						|
 | 
						|
	for (bool ok = movset.move_next(); ok && !found; ok = movset.move_next())
 | 
						|
	{
 | 
						|
		const TDate dreg = mov.get(MOV_DATAREG);
 | 
						|
		if (dreg > dfin)
 | 
						|
			break;
 | 
						|
		
 | 
						|
    const TString4 cod = mov.get(MOV_CODCAUS);
 | 
						|
	  const TRectype& caus = cache().get(LF_CAUSALI, cod);
 | 
						|
		found = (causreg.full() && cod == causreg) || 
 | 
						|
            (causreg.blank() && (_caus_regolarizzazione.objptr(cod) || caus.get_bool(CAU_SOLOIVA)));
 | 
						|
    if (!found)
 | 
						|
      continue; // causale incoerente
 | 
						|
 | 
						|
    const real td = mov.get_real(MOV_TOTDOC);
 | 
						|
		found = abs(totdoc - td) < 0.01;
 | 
						|
    if (!found)
 | 
						|
      continue;  // Importo incoerente
 | 
						|
 | 
						|
		key = mov.get(MOV_TIPO);
 | 
						|
		key.add(mov.get(MOV_CODCF));
 | 
						|
		const TRectype & clireg = cache().get(LF_CLIFO, key);
 | 
						|
		const TString16 paivreg = clireg.get(CLI_PAIV);
 | 
						|
		const TString16 cfreg = clireg.get(CLI_COFI);
 | 
						|
 | 
						|
		found = paiv.full() ? paiv == paivreg : cf == cfreg;
 | 
						|
		if (found)  // partita IVA coerente 
 | 
						|
			break;  
 | 
						|
	}
 | 
						|
 | 
						|
  movset.move_to(pos);
 | 
						|
 | 
						|
	return found;
 | 
						|
}
 | 
						|
 | 
						|
long TTS_campo_sender::iva11_reverse(const TRecordset & mov)
 | 
						|
{
 | 
						|
  const TRectype& caus = cache().get(LF_CAUSALI, mov.get(MOV_CODCAUS).as_string());
 | 
						|
 | 
						|
	return caus.get_int(CAU_REGSPIVA);
 | 
						|
}
 | 
						|
 | 
						|
const char * TTS_campo_sender::decode_causale(const TRecordset& mov)
 | 
						|
{
 | 
						|
  const TRectype& caus = cache().get(LF_CAUSALI, mov.get(MOV_CODCAUS).as_string());
 | 
						|
	
 | 
						|
	return caus.get(CAU_DESCR);
 | 
						|
}
 | 
						|
 | 
						|
void TTS_campo_sender::postprocess_movs(TRecordset & mov)
 | 
						|
{
 | 
						|
	if (yesno_box(TR("Confermare il traferimento")))
 | 
						|
	{
 | 
						|
		TConfig configtc(CONFIG_DITTA);
 | 
						|
		TMask & m = get_mask();
 | 
						|
 | 
						|
		configtc.set("TSULTINV", m.get_date(F_DATA_AL));
 | 
						|
 | 
						|
		if (mov.items() > 0L)
 | 
						|
		{
 | 
						|
			TProgind pi(mov.items(), TR("Conferma movimenti"), true, true);
 | 
						|
			TLocalisamfile cgmov(LF_MOV);
 | 
						|
		
 | 
						|
			for (bool ok = mov.move_first(); ok; ok = mov.move_next())
 | 
						|
			{
 | 
						|
				if (!pi.addstatus(1))
 | 
						|
					break;
 | 
						|
 | 
						|
				const long numreg = mov.get(MOV_NUMREG).as_int();
 | 
						|
 | 
						|
				cgmov.put(MOV_NUMREG, numreg);
 | 
						|
				if (cgmov.read(_isequal, _lock) == NOERR)
 | 
						|
				{
 | 
						|
					cgmov.put(MOV_INVIATO, true);
 | 
						|
					cgmov.rewrite();
 | 
						|
				}
 | 
						|
			}
 | 
						|
		}
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
bool search_reg(const TRelation& rel, void* pJolly)
 | 
						|
{
 | 
						|
	TAssoc_array * _caus_regolarizzazione =	(TAssoc_array *) pJolly;
 | 
						|
	const bool solaiva = rel.lfile().get_bool(CAU_SOLOIVA);
 | 
						|
	const TString codcaus = rel.lfile().get(CAU_CODCAUREG);
 | 
						|
 | 
						|
	if (codcaus.full())
 | 
						|
		_caus_regolarizzazione->add(codcaus, codcaus);
 | 
						|
	
 | 
						|
	return true;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
bool TTS_campo_sender::create()
 | 
						|
{
 | 
						|
	TRelation rel(LF_CAUSALI);
 | 
						|
	TCursor c(&rel);
 | 
						|
  bool ok = c.scan(search_reg, &_caus_regolarizzazione, "Ricerca causali di regolarizzazione");
 | 
						|
	
 | 
						|
	TLocalisamfile clifo(LF_CLIFO);
 | 
						|
	bool big_cli_code = false;
 | 
						|
 | 
						|
 | 
						|
	if (!ini_get_bool(CONFIG_DITTA, "tc", "TSNoCheck"))
 | 
						|
	{
 | 
						|
		clifo.put(CLI_TIPOCF, "C");
 | 
						|
		clifo.put(CLI_CODCF, 100000L);
 | 
						|
		if (!((clifo.read(_isgteq) == _iseof) || (clifo.get(CLI_TIPOCF) == "F")))
 | 
						|
			big_cli_code = true;
 | 
						|
		if (!big_cli_code)
 | 
						|
		{
 | 
						|
			clifo.put(CLI_TIPOCF, "F");
 | 
						|
			clifo.put(CLI_CODCF, 100000L);;
 | 
						|
 | 
						|
			if (clifo.read(_isgteq) != _iseof)
 | 
						|
				big_cli_code = true;
 | 
						|
		}
 | 
						|
		if (big_cli_code)
 | 
						|
			riclassifica().add("TSNOHCLI", EMPTY_STRING);
 | 
						|
	}
 | 
						|
 | 
						|
	return ok && TTS_sender::create();
 | 
						|
}
 | 
						|
 | 
						|
int tc0700(int argc, char* argv[])
 | 
						|
{
 | 
						|
  TTS_campo_sender app;
 | 
						|
  app.run(argc, argv, TR("Invio a TeamSystem"));
 | 
						|
  return 0;
 | 
						|
}
 |