#include <applicat.h>
#include <tabutil.h>
#include <mask.h>                     
#include <prefix.h>
#include <sheet.h>
#include <relation.h>
#include <urldefid.h>
#include <utility.h>

#define F_YEAR      101


// ripristino liquidazione  
// dati anno e ditta scancella tutte
// le tabelle dimmerda  
// piu' che altro serve alla Prassi 
// per non far cazzate quando provano

class TRipristino_liq : public TApplication
{
  long          _firm;
  TString       _year;
  TRelation*    _nditte;
  TArray_sheet* _ditte;
  TArray        _nomiditte;

  virtual bool create();
  virtual bool destroy();
  
  bool menu(MENU_TAG) { return do_restore(); }

  // handuler 
  static bool ch_year_handler(TMask_field& f, KEY key);

public: 

  void          build_ditte_sheet();
  void          build_nomiditte();
  void          set_year(const char* y) { _year = y; }                                                             

  bool          do_restore(); 
  bool          restore_firm(long firm);                             
  
  static TRipristino_liq& app() { return (TRipristino_liq&)main_app(); }

  TRipristino_liq() {} 
  virtual ~TRipristino_liq() {}
};

bool TRipristino_liq::ch_year_handler(TMask_field& f, KEY key)
{                                                    
  if (key == K_TAB && f.focusdirty())
  {                         
    app().begin_wait();
    app().set_year(f.get());
    app().build_nomiditte();
    app().build_ditte_sheet();    
    app().end_wait();
  }
  return TRUE;
} 


void TRipristino_liq::build_ditte_sheet()
{
  // build sheet
  _ditte->destroy();
  for (int i = 0; i < _nomiditte.items(); i++)
  {
    TToken_string* d = new TToken_string(64);
    *d = (TToken_string&)_nomiditte[i];
    const char vers = d->get_char(2);
    
    bool selectable = vers == '?';
    d->insert(" |", 0);        
    
    const long pos = _ditte->add(d);     
    if (selectable)  _ditte->disable(pos);
  }     
}


void TRipristino_liq::build_nomiditte()
{                 
  _nomiditte.destroy();
  // ricostruire _nomiditte e rifare build_ditte_sheet
  TLocalisamfile& dt = _nditte->lfile();
  TString fr("??");
  TTable lia("%LIA");
  
  for (dt.first(); !dt.eof(); dt.next())
  {     
    // check no archivi
    fr = "??";
    bool good = prefix().exist(dt.get_long("CODDITTA"));                        
    
    if (good) 
    {   
      // check no parametri liquidazione                                               
      lia.zero();
      const TString16 cod(format("%05ld%4s", dt.get_long("CODDITTA"), (const char*)_year));
      lia.put("CODTAB", cod);
      if (lia.read() != NOERR) good = FALSE; 
      else fr = lia.get("S7");
    }
    else continue;

    TToken_string* d = new TToken_string(64);
    
    // add record 
    d->add(dt.get("CODDITTA"));
    d->add(dt.get("RAGSOC"));
    d->add(fr);
    
    _nomiditte.add(d);  
  }
  TApplication::set_firm(_firm);
}


bool TRipristino_liq::create()
{                     
  TApplication::create();
  _firm = get_firm();
  TDate oggi(TODAY);                    
  _year.format("%d",oggi.year());
  _nditte = new TRelation(LF_NDITTE);
  _ditte  = new TArray_sheet(-1, -1, 0, 0, "Selezione Ditte",
                             "@1|Cod.@5|Ragione Sociale@50|Vers.");  
  build_nomiditte();
  build_ditte_sheet();
  
  dispatch_e_menu(BAR_ITEM(1));    
  return TRUE;
}

bool TRipristino_liq::destroy()
{
  delete _nditte;
  delete _ditte;
  return TApplication::destroy();
}  

bool TRipristino_liq::do_restore()
{ 
  KEY k; int i;
  TMask m("cg5600a"); 
  
  m.field(F_YEAR).set(_year);
  m.set_handler(F_YEAR, ch_year_handler);
  _ditte->enable_check();
  
  while ((k = m.run()) != K_ESC)
  {                            
    if (k == K_QUIT) break;
    switch(k)
    {
    case K_ENTER: 
      begin_wait();
      for(i = 0; i < _ditte->items(); i++)
      {
        if (_ditte->checked(i))
        {              
          TToken_string& rw = _ditte->row(i);
          restore_firm(rw.get_long(1));
        } 
      }
      end_wait();
      break;
    case DLG_SELECT:
      _ditte->run();
      break;
    }
  }
  
  set_firm(_firm); 
  return k != K_QUIT; 
}

bool TRipristino_liq::restore_firm(long firm)
{  
  // azzera: campi progressivi in LIA e PLA
  // cancella: tutti PPA, LIM, PIM, RMB, PLM, PTM
  set_firm(firm);
  
  TTable lia("%LIA");
  TTable pla("%PLA");
  TTable pim("PIM");
  TTable plm("PLM");
  TTable ptm("PTM");
  TTable ppa("PPA");
  TTable rmb("RMB");
  TTable lim("LIM");   
  
  // LIA
  for (lia.first(); !lia.eof(); lia.next())
  {
    TString codtab(lia.get("CODTAB"));
    
    if (atol(codtab.mid(0,5)) == firm &&
        codtab.mid(5,4) == _year)
    {             
      lia.put("R1","");
      lia.put("R2","");
      lia.put("R3","");
      lia.put("R4","");
      lia.put("R5","");
      lia.put("R6",""); 
      break;
    }   
  }
  lia.rewrite();
  
  // PLA
  for (pla.first(); !pla.eof(); pla.next())
  {
    TString codtab(pla.get("CODTAB"));
    
    if (atol(codtab.mid(0,5)) == firm &&
        codtab.mid(5,4) == _year)
    {
      pla.put("R0","");
      pla.put("R1","");
      pla.put("R2","");
      pla.put("R3","");
      pla.put("R4","");
      pla.put("R9","");
      pla.put("R10","");
      pla.put("S1","");
      pla.put("S2","");
      pla.put("S3","");
      pla.put("S4","");
    }
  }
  pla.rewrite();
  
  // PIM
  for (pim.first(); !pim.eof(); pim.next())
  {
    TString codtab(pim.get("CODTAB"));
    
    if (codtab.mid(0,4) == _year)
      pim.remove();
  }
  //   pim.packfile();
  //   pim.packindex();
  
  // PLM
  for (plm.first(); !plm.eof(); plm.next())
  {
    TString codtab(plm.get("CODTAB"));
    
    if (codtab.mid(0,4) == _year)
      plm.remove();
  }
  //   plm.packfile();
  //   plm.packindex();
  
  // PTM
  for (ptm.first(); !ptm.eof(); ptm.next())
  {
    TString codtab(ptm.get("CODTAB"));
    
    if (codtab.mid(0,4) == _year)
      ptm.remove();
  }
  //   ptm.packfile();
  //   ptm.packindex();
  
  // PPA
  for (ppa.first(); !ppa.eof(); ppa.next())
  {
    TString codtab(ppa.get("CODTAB"));
    
    if (codtab.mid(0,4) == _year)
      ppa.remove();
  }
  //   ppa.packfile();
  //   ppa.packindex();
  
  // RMB
  for (rmb.first(); !rmb.eof(); rmb.next())
  {
    TString codtab(rmb.get("CODTAB"));
    
    if (codtab.mid(0,4) == _year)
      rmb.remove();
  }
  //   rmb.packfile();
  //   rmb.packindex();
  
  // LIM
  for (lim.first(); !lim.eof(); lim.next())
  {
    TString codtab(lim.get("CODTAB"));
    
    if (codtab.mid(0,4) == _year)
      lim.remove();
  }
  //   lim.packfile();
  //   lim.packindex();
  
  return TRUE;
}


int cg5600(int argc, char* argv[])
{   
  TRipristino_liq app;
  app.run(argc, argv, "Ripristino liquidazione"); 
  return 0;
}
