campo-sirio/pe/pe0500.cpp
alex 9b9eed604f Patch level : 10.0 577
Files correlati     :  pe0.exe
Ricompilazione Demo : [ ]
Commento            :

Fatturazione 02 e velocizzazione calcolo consegnato mensile


git-svn-id: svn://10.65.10.50/trunk@19915 c028cbd2-c16b-5b4b-a496-9718f37d4682
2010-01-12 21:51:14 +00:00

423 lines
10 KiB
C++
Executable File

#include <applicat.h>
#include <automask.h>
#include <progind.h>
#include <relation.h>
#include <utility.h>
#include "pe0500.h"
#include "../ve/velib.h"
#include "../ve/velib04.h"
///////////////////////////////////////////////////////////
// TGenerazione_esecutivo
///////////////////////////////////////////////////////////
class TGenerazione_esecutivo : public TCopia_documento
{
protected:
virtual void post_process(TLista_documenti& doc_out, TLista_documenti& doc_in);
void genera_matricola(TRiga_documento & row);
public:
TGenerazione_esecutivo(const char* cod) : TCopia_documento(cod) {}
virtual ~TGenerazione_esecutivo() { }
};
void TGenerazione_esecutivo::genera_matricola(TRiga_documento & row)
{
row.zero(RDOC_LIVELLO);
for (int livello = 0; livello < 4; livello++)
{
TToken_string * str = row.tipo().genconf(livello);
if (str != NULL)
{
const TString codart = row.get(RDOC_CODART);
if (codart.full())
{
TString code;
const TRectype & anamag = cache().get(LF_ANAMAG, codart);
const int items = str->items();
if (anamag.get_bool(ANAMAG_ARTPROD))
{
for (int i = 0; i < items; i++)
{
const TString & name = str->get(i);
if (name.starts_with("PROG"))
{
TTable mat(format("VE%1d", livello + 1));
const int chars = atoi(name.after("PROG"));
mat.put("CODTAB", code);
if (mat.read(_isequal) != NOERR)
{
mat.zero();
mat.put("CODTAB", code);
mat.put("I0", 0L);
mat.write();
}
const int prog = mat.get_int("I0") + 1;
mat.put("I0", prog);
mat.rewrite();
TString s; s << prog; s.lpad(chars, '0');
code << s;
}
else
if (name.starts_with("33."))
{
TFieldref fld(name.after("33."), LF_DOC);
code << fld.read(row.doc());
}
else
if (name.starts_with("34."))
{
TFieldref fld(name.after("34."), LF_RIGHEDOC);
code << fld.read(row);
}
else
if (name.starts_with("47."))
{
TFieldref fld(name.after("47."), LF_ANAMAG);
code << fld.read(anamag);
}
}
TString c(row.get(RDOC_LIVELLO));
c << code;
row.put(RDOC_LIVELLO, c);
TTable l("GCG");
c.cut(0);
c << (livello + 1) << code;
l.put("CODTAB", c);
c.cut(0);
c << "Matricola " << code;
l.put("S0", c);
l.write();
}
}
}
}
}
void TGenerazione_esecutivo::post_process(TLista_documenti& doc_out, TLista_documenti& doc_in)
{
const int items = doc_out.items();
for (int d = 0; d < items; d++)
{
const TDocumento& doc = doc_out[d];
const int rows = doc.physical_rows();
for (int r = 1; r <= rows; r++)
{
TRiga_documento & row = (TRiga_documento &)doc[r];
if (row.get_int(RDOC_LEVEL) == 0)
genera_matricola(row);
}
}
for (int d = 0; d < items; d++)
{
TDocumento& doc = doc_out[d];
const int rows = doc.physical_rows();
for (int r = 1; r <= rows; r++)
{
TRiga_documento & row = doc[r];
if (row.get_int(RDOC_LEVEL) == 0 && row.get(RDOC_LIVELLO).full())
{
const real qta = row.get_real(RDOC_QTA);
const int times = qta.integer() - 1;
if (times > 0)
{
row.put(RDOC_QTA, UNO);
for (int q = 0 ; q < times; q++)
{
TRiga_documento & new_row = doc.new_row(row.tipo().codice());
TDocumento::copy_data(new_row, row);
new_row.put(RDOC_QTA, UNO);
genera_matricola(new_row);
for (int p = r + 1; p <= rows && doc[p].get_int(RDOC_LEVEL) > 0; p++)
{
const TRiga_documento & child_row = doc[p];
TRiga_documento & new_child_row = doc.new_row(child_row.tipo().codice());
TDocumento::copy_data(new_child_row, child_row);
new_child_row.put(RDOC_QTA, child_row.get_real(RDOC_QTA) / qta);
}
}
for (int p = r + 1; p <= rows && doc[p].get_int(RDOC_LEVEL) > 0; p++)
{
TRiga_documento & child_row = doc[p];
child_row.put(RDOC_QTA, child_row.get_real(RDOC_QTA) / qta);
}
}
}
}
}
}
///////////////////////////////////////////////////////////
// TGenerazione_ordine
///////////////////////////////////////////////////////////
class TGenerazione_ordine : public TCopia_documento
{
protected:
virtual void post_process(TLista_documenti& doc_out, TLista_documenti& doc_in);
public:
TGenerazione_ordine(const char* cod) : TCopia_documento(cod) {}
virtual ~TGenerazione_ordine() { }
};
void TGenerazione_ordine::post_process(TLista_documenti& doc_out, TLista_documenti& doc_in)
{
const int items = doc_out.items();
for (int d = 0; d < items; d++)
{
TDocumento& doc = doc_out[d];
const int rows = doc.physical_rows();
real total;
for (int r = rows; r > 0; r--)
{
TRiga_documento & row = (TRiga_documento &)doc[r];
const int level = row.get_int(RDOC_LEVEL);
if (level != 0)
{
const real importo = row.importo(true, false);
total += importo;
}
else
{
if (total != ZERO)
{
const real qta = row.get_real(RDOC_QTA);
const real price = total / qta;
TCurrency_documento c(price, doc, true);
row.put(RDOC_PREZZO, c.get_num());
}
total = ZERO;
}
}
for (int r = rows; r > 0; r--)
{
TRiga_documento & row = (TRiga_documento &)doc[r];
const int level = row.get_int(RDOC_LEVEL);
if (level != 0)
doc.destroy_row(r, true);
}
}
}
///////////////////////////////////////////////////////////
// TGenEsecutivo mask
///////////////////////////////////////////////////////////
class TGenEsecutivo_mask : public TAutomask
{
protected:
virtual bool on_field_event(TOperable_field& o, TField_event e, long jolly);
public:
TGenEsecutivo_mask() : TAutomask("pe0500a") {}
};
bool TGenEsecutivo_mask::on_field_event(TOperable_field& o, TField_event e, long jolly)
{
switch (o.dlg())
{
case F_FROM_FRNDOC:
case F_FROM_TONDOC:
if (e == fe_modify || e == fe_init)
{
const long docfr = get_long(F_FROM_FRNDOC);
const long docto = get_long(F_FROM_TONDOC);
if (docto > 0L && docfr > docto)
return error_box("Il documento finale non puo' essere maggiore di quello iniziale");
}
break;
default:
break;
}
return TRUE;
}
///////////////////////////////////////////////////////////
// Main program
///////////////////////////////////////////////////////////
class TGenEsecutivo_app : public TSkeleton_application
{
protected:
void split_docs(TLista_documenti & list, const TDocumento & doc);
public:
virtual void main_loop();
};
void TGenEsecutivo_app::split_docs(TLista_documenti & list, const TDocumento & doc)
{
list.destroy(-1);
const int rows = doc.physical_rows();
int last_pos = 0, before_pos = 0;
int p = 0;
for (int i = 1; i <= rows; i++)
{
const int level = doc[i].get_int(RDOC_LEVEL);
if (level == 0)
{
if (last_pos > 0)
{
TDocumento & d = list[p];
for (int j = d.physical_rows() ; j >= i; j--)
d.destroy_row(j, true);
}
if (before_pos > 0)
{
TDocumento & d = list[p];
for (int j = before_pos ; j >= 1; j--)
d.destroy_row(j, true);
}
before_pos = last_pos;
p = list.add(doc);
}
last_pos++;
}
if (before_pos > 0)
{
TDocumento & d = list[p];
for (int j = before_pos ; j >= 1; j--)
d.destroy_row(j, true);
}
}
void TGenEsecutivo_app::main_loop()
{
open_files(LF_TAB, LF_TABCOM, LF_DOC, LF_RIGHEDOC, LF_CONDV, LF_RCONDV,
LF_ANAMAG, LF_SCONTI, LF_UMART, LF_CLIFO, LF_CFVEN, LF_INDSP,
LF_OCCAS, LF_MOVMAG, LF_RMOVMAG, LF_SVRIEP, LF_AGENTI, LF_PERCPROV, 0);
TGenEsecutivo_mask m;
while (m.run() == K_ENTER)
{
const TDate data(m.get_date(F_DATA));
TRectype rec_from(LF_DOC);
rec_from.put(DOC_ANNO, m.get(F_FROM_ANNO));
rec_from.put(DOC_CODNUM, m.get(F_FROM_NUM));
rec_from.put(DOC_PROVV, m.get(F_FROM_PROVV));
rec_from.put(DOC_NDOC, m.get(F_FROM_FRNDOC));
TRectype rec_to(rec_from);
rec_to.put(DOC_NDOC, m.get(F_FROM_TONDOC));
// Costruisce il filtro sui tipi documento selezionati
// Il pipe significa OR per le espressioni
TRelation rel(LF_DOC);
TCursor cur(&rel, "", 1, &rec_from, &rec_to);
const long tot = cur.items();
if (tot > 0L)
{
TGenerazione_esecutivo e(m.get(F_CODICE_ESEC));
TGenerazione_ordine o(m.get(F_CODICE_ORC));
e.preserve_original_rif();
o.preserve_original_rif();
cur.freeze();
TProgind pi(tot, "Generazione in corso...", FALSE, TRUE);
for (cur = 0; cur.pos() < tot; ++cur)
{
pi.addstatus(1);
TDocumento srcdoc(cur.curr()), newdoc;
if (e.is_document_ok(srcdoc))
{
TLista_documenti doc_in;
TLista_documenti doc_out;
doc_in.add(srcdoc);
e.elabora(doc_in, doc_out, data);
doc_in.rewrite();
doc_out[0].put(DOC_K, doc_in[0].get(DOC_K));
doc_out[0].put(DOC_USEK, doc_in[0].get(DOC_USEK));
doc_in.destroy(-1);
TDocumento esecutivo(doc_out[0]);
doc_in.add(esecutivo);
const int rows = esecutivo.physical_rows();
esecutivo.zero(DOC_K);
esecutivo.zero(DOC_USEK);
for (int i = 1; i <= rows; i++)
{
esecutivo[i].zero(RDOC_PREZZO);
esecutivo[i].zero(RDOC_K);
}
if (m.get_bool(F_SPLIT))
split_docs(doc_out, esecutivo);
doc_out.write();
doc_out.destroy(-1);
o.elabora(doc_in, doc_out, data);
TDocumento & doc_dest = doc_out[0];
int last_found = rows + 1;
for (int r = doc_dest.physical_rows(); r > 0; r--)
{
TRiga_documento & row = (TRiga_documento &) doc_dest[r];
for (int j = last_found - 1; j >= 1; j--)
if (esecutivo[j].get_int(RDOC_LEVEL) == 0)
{
last_found = j;
break;
}
if (last_found >= 1)
{
const TRiga_documento * r0 = (const TRiga_documento *)esecutivo[last_found].find_original_rdoc();
if (r0 != NULL)
row.set_original_rdoc_key(*r0);
}
}
doc_out.write();
}
}
}
else
warning_box("Nessun documento soddisfa i vincoli indicati");
}
}
int pe0500(int argc, char* argv[])
{
TGenEsecutivo_app a;
a.run(argc, argv, "Generazione Preventivi esecutivi e Ordini Clienti");
return 0;
}