#include <applicat.h>
#include "clifor.h"
#include "../li/letint.h"
#include <modaut.h>
#include <relation.h>

TOccasionale::TOccasionale() : TRectype(LF_OCCAS)
{ }
TOccasionale::TOccasionale(const TRectype& r) : TRectype(r)
{ }

TOccasionale::TOccasionale(const TOccasionale& r) : TRectype(r)
{
}

TRectype& TOccasionale::operator=(const TRectype& r)
{
  return TRectype::operator=(r);
}

TOccasionale& TOccasionale::operator=(const TOccasionale& r)
{        
  TRectype::operator=(r);
  return *this;
}

TOccasionale::~TOccasionale()
{ }

int TCli_for::write_rewrite(TBaseisamfile& f, bool re) const
{  
  int err = TMultiple_rectype::write_rewrite(f, re);

  if (err == NOERR)
  {     
    TLocalisamfile v(LF_CFVEN);
    TRectype & v_rec = vendite();
    err = v.write(v_rec);
    if (err == _isreinsert)
      err = v.write(v_rec);
  }
  return err;
}

TRectype& TCli_for::vendite() const
{                             
  const char t = tipo();
  const long c = codice();
  
  if (_ven_rec.empty() || t != *(const char *) (*_ven_tipo) || c != (long) *_ven_codice)
  {
    TLocalisamfile v(LF_CFVEN);
    TRectype& vr = ((TCli_for *)this)->_ven_rec;  // fool const
  
    vr.zero();
    vr.put(CFV_TIPOCF,t);
    vr.put(CFV_CODCF, c);
    const TRectype v_rec(vr);
    if (vr.read(v) != NOERR)
      vr = v_rec;
  }
  return (TRectype &) _ven_rec;
}

bool TCli_for::read_lettera(const TDate & data, bool extended)
{

	if (_use_lettere)
	{
		if (data != _last_data)
		{
			_letint.zero();
			TRelation * r = new TRelation(LF_LETINT);
			TRectype to(r->curr());

			to.put(LETINT_ANNO, data.year());
			to.put(LETINT_CODCLI, codice());

			TRectype from(to);
			
			from.put(LETINT_ANNO, data.year() - 1);
			
			TCursor c(r, "", 2 ,&from, &to);
			const int items = c.items();

			if (items > 0)
			{
				TDate dal;
				TDate al(31, 12, data.year());
			
				for (long pos = items - 1; pos >= 0L; pos--)
				{
					c = pos;

					const int tipo = c.curr().get_int(LETINT_TIPOOP);

					if (tipo == 3)
					{
						dal = c.curr().get_date(LETINT_DAL);
						al = c.curr().get_date(LETINT_AL);
					}
					else
					{
						dal = c.curr().get_date(LETINT_VSDATA);
						if (c.pos() < items - 1)
						{
							++c;
							const int tipo_next = c.curr().get_int(LETINT_TIPOOP);
							if (tipo_next == 3)
								al = c.curr().get_date(LETINT_DAL);
							else
								al = c.curr().get_date(LETINT_VSDATA);
							--c;
						}
					}
					if (dal <= data)
					{
						_letint = c.curr();
						break;
					}
				}
				_lettera_found = !_letint.empty();
				if (_lettera_found && !extended)
					_lettera_found = data <= al;
			}
			_last_data = data;
		}
	}
	return _lettera_found;
}

int TCli_for::read(const TRectype & rec, word op, word lockop)
{   
  _ven_rec.zero();
  int err = TMultiple_rectype::read(rec, op, lockop);
	_last_data = botime;
  return err;
}

int TCli_for::read(char tipo, long codice, word op, word lockop)
{                      
  int err = NOERR;
  _ven_rec.zero();
  zero();
  if (tipo > ' ' && codice > 0L)
  {
    put(CLI_TIPOCF, tipo);
    put(CLI_CODCF, codice);
    err = TMultiple_rectype::read(op, lockop);
  }
	_last_data = botime;
  return err;
}      

int TCli_for::remove(TBaseisamfile& f) const
{
  TLocalisamfile v(LF_CFVEN);
  
  const int err = vendite().remove(v);
  if (err != NOERR && err != _iskeynotfound)
    return err;
  return TMultiple_rectype:: remove(f);
}

void TCli_for::init()
{
  _tipo = new TRecfield(*this, CLI_TIPOCF);
  _codice = new TRecfield(*this, CLI_CODCF);
  _ven_tipo = new TRecfield(_ven_rec, CFV_TIPOCF);
  _ven_codice = new TRecfield(_ven_rec, CFV_CODCF);
	_use_lettere = main_app().has_module(LIAUT, CHK_DONGLE);
	if (_use_lettere)
	{
		TConfig c(CONFIG_DITTA, "ve");
		_use_lettere = c.get_bool("USELETTERE");
	}
	
}

TCli_for::TCli_for(char tipo, long codice) : TMultiple_rectype( LF_CLIFO ), _ven_rec(LF_CFVEN),
																						 _letint(LF_LETINT), _use_lettere(false), _lettera_found(false)
{
	init();
  add_file(LF_INDSP, IND_CODIND);  
  if (tipo > ' ' && codice > 0L)
    read(tipo, codice);
}  

TCli_for::TCli_for(const TRectype & rec) : TMultiple_rectype(rec), _ven_rec(LF_CFVEN),
																					 _letint(LF_LETINT), _use_lettere(false), _lettera_found(false)
{
	init();
  add_file(LF_INDSP, IND_CODIND);  
  read(rec);
}  

TCli_for::TCli_for(const TCli_for & c) : TMultiple_rectype(c), _ven_rec(c._ven_rec),
																				 _letint(LF_LETINT), _use_lettere(false), _lettera_found(false)
{
	init();
}  
   
TCli_for::~TCli_for()
{
  delete _tipo;
  delete _codice;
  delete _ven_tipo;
  delete _ven_codice;
}