#include <applicat.h>
#include <mask.h>
#include <progind.h>
#include <urldefid.h>

#include "cg2200.h"
#include "cg2101.h"

#include <mov.h>
#include <rmov.h>
#include <rmoviva.h>

TString& add_plural(TString& s, long num, const char* name)
{   
  const TFixed_string n(name);
  const char last = n[n.len()-1];                   

  if (num < 1)
  {
    s << "nessun";
    if (toupper(last) == 'A' || toupper(n[0]) == 'Z' ||
        toupper(n[0]) == 'S' && strchr("aeiouAEIOU", n[1]) == NULL) 
      s << tolower(last);
    s << ' ' << name;
  } 
  else
  {
    s << num << ' ' << name;
    if (num > 1)
      s[s.len()-1] = (last == 'a') ? 'e' : 'i';
  }
  
  return s;
}

class TDeleteprovv_app : public TApplication
{ 
protected:
  virtual bool create();
  virtual bool destroy();
  virtual bool menu(MENU_TAG m);  
  
public:
  TDeleteprovv_app() {};  
};

bool TDeleteprovv_app::create()
{
  dispatch_e_menu(BAR_ITEM(1));
  return TRUE;
}

bool TDeleteprovv_app::destroy()
{
  return TRUE;
}

bool TDeleteprovv_app::menu(MENU_TAG)
{
  TMask m("cg2200a");
  TCursor& cur = *m.efield(F_FROMDATE).browse()->cursor();
  TLocalisamfile& mov = cur.file(LF_MOV);
  TLocalisamfile rmov(LF_RMOV);
  TLocalisamfile rmoviva(LF_RMOVIVA); 
  
  while (m.run() != K_QUIT) 
  {    
    TRecnotype last = cur.items()-1;  
    mov.zero();
    const char* s = m.get(F_TODATE); 
    if (*s)
    {
      mov.put(MOV_DATAREG, s);
      mov.put(MOV_NUMREG, m.get(F_TOREG));
      last = cur.read();
    }
    
    mov.zero();
    mov.put(MOV_DATAREG, m.get(F_FROMDATE)); 
    mov.put(MOV_NUMREG, m.get(F_FROMREG)); 
    const TRecnotype first = cur.read();
    
    const TRecnotype total = last-first+1;
    TString80 caption("Cancellazione di ");
    add_plural(caption, total, "movimento");
    
    if (!yesno_box(caption) || total < 1)
      continue;
    
    TProgind pi(total, caption, FALSE, TRUE, 24);
    
    for (cur = first; cur.pos() <= last; ++cur)
    {
      const long numreg = mov.get_long(MOV_NUMREG);
      
      int err = cur.lock();
      for (int rig = 1; err == NOERR; rig++)
      {
        rmov.put(RMV_NUMREG, numreg);
        rmov.put(RMV_NUMRIG, rig);
        if (rmov.read(_isequal, _lock) != NOERR) break;
        err = rmov.remove();
        if (err != NOERR)
          caption.format("riga contabile %d", rig);
      }  
      for (rig = 1; err == NOERR; rig++)
      {
        rmoviva.put(RMI_NUMREG, numreg);
        rmoviva.put(RMI_NUMRIG, rig);
        if (rmoviva.read(_isequal, _lock) != NOERR) break;
        err = rmov.remove();
        if (err != NOERR)
          caption.format("riga IVA %d", rig);
      }  
      
      if (err == NOERR)
      {
        err = mov.remove(); // Isam bug on remove with key != 1
      }  
      else
        caption = "testata";

      if (err == NOERR)
        pi.addstatus(1);
      else
      {
        error_box("Errore %d nella cancellazione della %s del movimento %ld", 
                  err, (const char*)caption, numreg);
        break;
      }  
    }
  }
  
  return FALSE;
}


int cg2200(int argc, char** argv)
{
  TDeleteprovv_app a;
  a.run(argc, argv, "Eliminazione movimenti provvisori");
  return 0;
}