#include <mask.h>
#include <printapp.h>
#include <relation.h>
#include <tabutil.h>
#include <utility.h>
#include <sort.h>

#include <lffiles.h>
#include <comuni.h>

#include "ba6.h"
#include "ba6200.h"

const  MAXSTR = 128;
static char __tmp [MAXSTR];
static TFixed_string tmp (__tmp, MAXSTR);

class BA6200_application : public TPrintapp
{
  TRelation*        _rel;
  TMask*            _msk;
  TLocalisamfile*   _comuni;
  TParagraph_string * _descr_comune;

  int     _cur1, _cur2;
  TString _provcom_msk, _com, _dencom, _provcom, _capcom;
  TString _codistat;
  int     _uffiidd1, _uffiidd2, _uffiidd3, _uffiva1, _uffiva2, _uffiva3;
  int     _uffreg, _uffconc, _uffcserv, _uffesprov, _uffescom, _terrmon;
  bool    _stampa_alici;
  TDate   _data_stampa;
  
protected:
  virtual bool user_create() ;
  virtual bool user_destroy();
  virtual bool set_print(int m);
  
  virtual bool preprocess_page (int,int);

public:
  void crea_intestazione();
  void setta_righe_descr(TParagraph_string*);
  
  BA6200_application();
};

bool BA6200_application::preprocess_page(int file, int counter)
{
  
  static bool first = TRUE;                       
  reset_print();

  if (counter)
    return TRUE;

  if (file == LF_COMUNI)
  {                        
    first = TRUE;
    _provcom = current_cursor()->curr(LF_COMUNI).get(COM_PROVCOM);
    
    if ((_provcom == _provcom_msk) || (_provcom_msk == ""))
    {  
      _com       = current_cursor()->curr(LF_COMUNI).get(COM_COM);          
      _dencom    = current_cursor()->curr(LF_COMUNI).get(COM_DENCOM);
      *_descr_comune = (const char*) _dencom;
      _capcom    = current_cursor()->curr(LF_COMUNI).get(COM_CAPCOM);
      _codistat  = current_cursor()->curr(LF_COMUNI).get(COM_CODISTAT);
      _uffiidd1  = current_cursor()->curr(LF_COMUNI).get_int(COM_UFFIIDD1);
      _uffiidd2  = current_cursor()->curr(LF_COMUNI).get_int(COM_UFFIIDD2);
      _uffiidd3  = current_cursor()->curr(LF_COMUNI).get_int(COM_UFFIIDD3);
      _uffiva1   = current_cursor()->curr(LF_COMUNI).get_int(COM_UFFIVA1);
      _uffiva2   = current_cursor()->curr(LF_COMUNI).get_int(COM_UFFIVA2);
      _uffiva3   = current_cursor()->curr(LF_COMUNI).get_int(COM_UFFIVA3);
      _uffreg    = current_cursor()->curr(LF_COMUNI).get_int(COM_UFFREG);
      _uffconc   = current_cursor()->curr(LF_COMUNI).get_int(COM_UFFCONC);
      _uffcserv  = current_cursor()->curr(LF_COMUNI).get_int(COM_UFFCSERV);
      _uffesprov = current_cursor()->curr(LF_COMUNI).get_int(COM_UFFESPROV);
      _uffescom  = current_cursor()->curr(LF_COMUNI).get_int(COM_UFFESCOM);
      _terrmon   = current_cursor()->curr(LF_COMUNI).get_int(COM_TERRMON);
      
      setta_righe_descr(_descr_comune);
      
      set_row (1,"@0g%4s",  (const char*) _com);
      set_row (1,"@33g%5s", (const char*) _provcom);
      set_row (1,"@39g%5s", (const char*) _capcom);
      set_row (1,"@46g%6s", (const char*) _codistat);
      
      if (_terrmon != 0)
        set_row (1,"@56g%d",   _terrmon);

      if (_uffiidd1 != 0)
        set_row (1,"@65g%03d",  _uffiidd1);

      if (_uffiidd2 != 0)
        set_row (1,"@69g%03d",  _uffiidd2);

      if (_uffiidd3 != 0)
        set_row (1,"@73g%03d",  _uffiidd3);

      if (_uffiva1 != 0)
        set_row (1,"@78g%03d",  _uffiva1);

      if (_uffiva2 != 0)
        set_row (1,"@82g%03d",  _uffiva2);

      if (_uffiva3 != 0)
        set_row (1,"@86g%03d",  _uffiva3);

      if (_uffreg != 0)
        set_row (1,"@92g%03d",  _uffreg);

      if (_uffconc != 0)
        set_row (1,"@97g%03d", _uffconc);

      if (_uffcserv != 0)
        set_row (1,"@103g%03d", _uffcserv);

      if (_uffesprov != 0)
        set_row (1,"@108g%03d", _uffesprov);
      
      if (_uffescom != 0)
        set_row (1,"@112g%03d", _uffescom);
      
      return TRUE;
    }
    else
      return FALSE;          
  }
  else
    if (file == LF_ALICI)
    {
      if (_rel->lfile().get("STATO").empty())
      {
        int i = 1;
        if (first)
          set_row (i++,"@7gI.C.I. @15gAnno@21gAliquota@31gDetrazioni",  FLD(LF_ALICI, "ANNO"), FLD(LF_ALICI, "ALIQ", "##,###"));
        set_row (i,"@15g@n@23g@pn@30g@pn",  FLD(LF_ALICI, "ANNO", "####"), FLD(LF_ALICI, "ALIQ", "##,###"), FLD(LF_ALICI,"DETR","###.###.###"));
        first = FALSE;
      }
      else return FALSE;
    }
  return TRUE;
}

void BA6200_application::setta_righe_descr(TParagraph_string* str)
{
  const char* r;
  int i = 1;

  while ((r = str->get()) != NULL)
  {
    set_row (i,"@7g%s", r);
    i++;
  }
}

BA6200_application::BA6200_application()
{
}

bool BA6200_application::set_print(int)
{
  KEY tasto;
  TString statoini,statofine,codiceini,codicefine,denomini,denomfine;

  tasto = _msk->run();

  if (tasto == K_ENTER)
  {
    _data_stampa = _msk->get     (F_DATA);
    int scelta   = _msk->get_int (F_SCELTA);
    _provcom_msk = _msk->get     (F_PROVCOM);
    _stampa_alici = _msk->get_bool(F_STAMPAALICI);
    
    reset_files(); 
    add_file(LF_COMUNI);
    if (_stampa_alici) 
      add_file(LF_ALICI);

    if (scelta == 1)
    {
      statoini   = _msk->get(F_STATOINI);
      codiceini  = _msk->get(F_CODICEINI);
      statofine  = _msk->get(F_STATOFINE);
      codicefine = _msk->get(F_CODICEFINE);

      select_cursor(_cur1); 
      TLocalisamfile& fl = current_cursor()->file(LF_COMUNI);
      TRectype da (fl.curr());
      TRectype a  (fl.curr());   

      da.zero();
      a.zero();
      
      da.put(COM_STATO,statoini);         
      da.put(COM_COM  ,codiceini);
      
      a.put(COM_STATO,statofine);         
      a.put(COM_COM  ,codicefine);

      current_cursor()->setregion(da, a);
    }
    else
      if (scelta == 2)
      {
        denomini  = _msk->get(F_NOMEINI);
        denomfine = _msk->get(F_NOMEFINE);
        
        select_cursor(_cur2);

        TLocalisamfile& fl = current_cursor()->file(LF_COMUNI);
        TRectype da (fl.curr());
        TRectype a  (fl.curr());   
        
        da.zero();
        a.zero();
        
        da.put(COM_DENCOM, denomini);
        
        a.put (COM_DENCOM, denomfine);
        
        current_cursor()->setregion(da, a);
      }

    //set_print_zero();
    crea_intestazione();

    return TRUE;
  }
  else
    return FALSE;
}

void BA6200_application::crea_intestazione()
{
  TString sep(132);
  TString data_stampa;
  
  reset_header();
  
  sep.fill('-');
  set_header (1, (const char *) sep);
  sep = "";
  sep << "Pag. @#";
  sep.right_just(132); 
  set_header (2,(const char*) sep);  
  
  set_header (2,"@0gARCHIVIO COMUNI@91gSTUDIO@104gDATA");
  data_stampa = _data_stampa.string();
  set_header (2,"@109g%10s", (const char*) data_stampa);
  
  sep = "";
  sep.fill('-');
  set_header (3, (const char *) sep);
  
  set_header(5,"@0gCodice@7gDenominazione@33gProv.@40gCap@45gCd.Istat@54gMont.@60gUff.@67gII.DD.@80gI.V.A.@92gRegi.@96gConc.@102gServ.@108gEsatt.");
  set_header (6,"@0g------@7g-------------------------@33g-----@39g-----");
  set_header (6,"@45g--------@54g-----@65g-----------");
  set_header (6,"@78g------------@90g-----@96g-----@102g-----@108g-------");  
}

bool BA6200_application::user_create()
{
  _rel    = new TRelation (LF_COMUNI);
  _rel->add(LF_ALICI, "COM=COM");

  _cur1   = add_cursor(new TCursor(_rel,"",1));
  
  _cur2   = add_cursor(new TCursor(_rel,"",2));

  _comuni = new TLocalisamfile (LF_COMUNI);

  _descr_comune   = new TParagraph_string ("",25);
  
  _msk = new TMask("ba6200a");
  return TRUE;
}

bool BA6200_application::user_destroy()
{
  delete _msk;
  delete _rel;
  delete _comuni;
  delete _descr_comune;
  return TRUE;
}

int ba6200(int argc, char* argv[])
{

  BA6200_application a;

  a.run(argc, argv, "Stampa Comuni");

  return 0;
}





