// Programma generico di gestione tabelle modulo provvigioni agenti

#include <tabapp.h>

#include "prtbapr.h"

class TTablePR_app : public TTable_application
{
protected:
  virtual bool user_create() ;
  virtual TString& get_mask_name(TString& t) const;
  static  bool gmc_handler(TMask_field& f, KEY k);
};

static inline TTablePR_app& app() { return (TTablePR_app&)main_app(); }

bool TTablePR_app::gmc_handler(TMask_field& f, KEY k)
{
  if (f.to_check(k))
  {
    TString v1 = f.get(),v2;
    TMask& m = f.mask();
    if (v1 == "E" || v1 == "R")
    {
      for (short id = F_SEQRIC1; id <= F_SEQRIC4; id++)
      {
        if (f.dlg() == id)
          continue;
        v2 = m.get(id);
        if (v1 == "E" && v2 == "R" || v1 == "R" && v2 == "E")
          return f.error_box(TR("Non e' possibile usare contemporaneamente la tabella gruppo merceologico e la tabella sottogruppo merceologico"));
      }
    }
  }
  return TRUE;
}

TString& TTablePR_app::get_mask_name(TString& t) const
{
  if (t[0] == '%' )
  {
    t.ltrim(1);
  }

  t = "prtb"; t << get_tabname();

  TFilename n = t;
  n.ext ("msk");
  if (n.exist())
  { 
    return t;
  }          
  return Tab_application::get_mask_name(t);
}

bool TTablePR_app::user_create()
{
  Tab_application::user_create();
  const TString& name = get_tabname();
  TMask& mask = *get_mask();
  if (name == "APR") 
  {
    mask.set_handler(F_SEQRIC1,gmc_handler);
    mask.set_handler(F_SEQRIC2,gmc_handler);
    mask.set_handler(F_SEQRIC3,gmc_handler);
    mask.set_handler(F_SEQRIC4,gmc_handler);
  }

  return TRUE;
}


int pr0100(int argc, char** argv)
{
  TTablePR_app a;
  a.run(argc, argv, TR("Tabella"));
  return 0;
}