#include <applicat.h>
#include <automask.h>
#include <progind.h>
#include <recarray.h>
#include <relation.h>
#include <reprint.h>
#include <reputils.h>

#include "../cg/cglib01.h"

#include "doc.h"

#include "pd0610300a.h"

                                           /////////////////////////////////
                                           ////    TSOST_CONDPAG_MSK    ////
                                           /////////////////////////////////

class TSost_condpag_msk: public TAutomask
{
protected:
  bool elabora_documenti();
  virtual bool on_field_event(TOperable_field& o, TField_event e, long jolly);
public:
	TSost_condpag_msk();
};

bool TSost_condpag_msk::elabora_documenti()
{
  TLog_report rep(TR("Documenti modificati"));

  //se non sono settate le date, compilo gli anni dagli esercizi contabili
  TEsercizi_contabili& esc = esercizi();
  const TDate    dadata = get_date(F_DADATA);
  const TDate    adata  = get_date(F_ADATA);
  const long     daanno = dadata.ok() ? dadata.year() : esc.first();
  const long     aanno  = adata.ok()  ? adata.year()  : esc.last();
  const TString8 tipodoc = get(F_TIPODOC); 
  const TString8 stato = get(F_STATO);
  const long     dacodcf = get_long(F_DACODCF);
  const long     acodcf = get_long(F_ACODCF);
  const TString8 codnum = get(F_CODNUM);
  const real     totdoc = get_real(F_TOTDOC);

  TString query;
  query << "USE DOC SELECT (CODNUM=\"" << codnum << "\")"
        << "&&(TIPODOC=\"" << tipodoc << "\")"
        << "&&(STATO=\"" << stato << "\")"
        << "&&(TIPOCF=\"C\")"
        << "&&(BETWEEN(CODCF," << dacodcf << "," << acodcf << "))"
        << "&&(BETWEEN(DATADOC,#DADATA,#ADATA))";
  if(totdoc > ZERO)
    query << "&&(STR(G1:TOTDOC<=" << totdoc << "))";
  query << "\n"
        << "FROM PROVV=\"D\" ANNO=" << daanno << "\n"
        << "TO PROVV=\"D\" ANNO=" << aanno;

  //instanzio il recordset e gli setto tutte le variabili
  TISAM_recordset doc(query);
  doc.set_var("#DADATA",  dadata);
  doc.set_var("#ADATA",   adata);

  TLocalisamfile& documenti = doc.cursor()->file();

  const TString4 codpag = get(F_CODPAG);

  TProgind pi(doc.items(), TR("Elaborazione documenti in corso..."), true, true);

  //scorro il recordset e aggiorno i documenti che � necessario aggiornare
  for(bool ok = doc.move_first(); ok; ok = doc.move_next())
  {
    if (!pi.addstatus(1))
		    break;

    TRectype& rec = doc.cursor()->curr();

    const TString4 codpagdoc = rec.get(DOC_CODPAG);
    const TString4 tipopag   = cache().get("%CPG", codpagdoc, "S4");

    if (codpagdoc != codpag && tipopag == "3" ) //tipopag = 3-> riba
    {
      rec.put(DOC_CODPAG, codpag);

      const TString8 abi = get(F_ABI);
      if (abi.full())
      {
        const TString8 cab  = get(F_CAB);
        const TString prog  = get(F_PROG);

        rec.put(DOC_CODABIP, abi);
        rec.put(DOC_CODCABP, cab);
        rec.put(DOC_PROGBNP, prog);
      }

      rec.rewrite(documenti);

      TString str;
      str << doc.get(DOC_PROVV).as_string() << '|' << doc.get(DOC_ANNO).as_int() << '|' << doc.get(DOC_CODNUM).as_string() << '|' << doc.get(DOC_NDOC);
      rep.log(0, str);
    }
  }

  TReport_book buc;  
  buc.add(rep);
  if (buc.pages() > 0)
	  buc.preview();
  return doc.items() > 0 ? true : false;
}

bool TSost_condpag_msk::on_field_event(TOperable_field& o, TField_event e, long jolly)
{ 
  switch(o.dlg())
  {
  case DLG_OK:
    if(e == fe_button)
    {
      if(elabora_documenti())
        message_box(TR("Elaborazione terminata"));
      else
        message_box(TR("Non � stato possibile individuare nessuna commessa da modificare con i filtri impostati"));
      return false;
    }
    break;
  default:  break;
  }
  return true;
}

TSost_condpag_msk::TSost_condpag_msk() : TAutomask("pd0610300a")
{
}

                                           /////////////////////////////////
                                           ////    TSOST_CONDPAG_APP    ////
                                           /////////////////////////////////

class TSost_condpag_app: public TSkeleton_application
{
protected:
  virtual bool check_autorization() const {return false;}
  virtual const char * extra_modules() const {return "ve";}
  virtual void main_loop();
  void elabora();
};

void TSost_condpag_app::elabora()
{
}

void TSost_condpag_app::main_loop()
{
  TSost_condpag_msk m;
  while (m.run() == K_ENTER)
	{
		elabora();
	}
}

int pd0610300(int argc, char* argv[])
{  
  TSost_condpag_app app;
  app.run(argc, argv, TR("Sostituzione Condizioni di Pagamento"));
  return 0;
}