#include #include #include #include #include #include #include #include "inlib01.h" #include "in0500a.h" /////////////////////////////////////////////////////////// // TIntra_mask // Maschera generica con dati utili a tutte quelle intra /////////////////////////////////////////////////////////// void TIntra_mask::on_firm_change() { if (is_running()) TAutomask::on_firm_change(); long firm = prefix().get_codditta(); const TRectype& ditta = cache().get(LF_NDITTE, firm); _freq_ces = ditta.get_char("FREQCES"); _freq_acq = ditta.get_char("FREQACQ"); if (_freq_ces <= ' ') _freq_ces = 'T'; if (_freq_acq <= ' ') _freq_acq = 'T'; } short TIntra_mask::type_field() const { NFCHECK("Non e' stato specificato il campo del tipo"); return DLG_NULL; } short TIntra_mask::period_field() const { return DLG_NULL; } char TIntra_mask::tipo() const { short id = type_field(); char t = get(id)[0]; return t; } char TIntra_mask::frequenza(int a, char t) const { const TDate d(TODAY); if (a <= 0) { a = anno(); if (a <= 0) a = d.year(); } switch (t) { case 'B': t = 'A'; break; // Rettifiche su Acquisti case 'D': t = 'C'; break; // Rettifiche su Cessioni default : t = tipo(); break; } if (a < d.year()) { TLocalisamfile riep(LF_RIEPRETT); riep.put("TIPO", (char)t); riep.put("ANNO", a); if (riep.read(_isgteq) == NOERR) { if (riep.get_char("TIPO") == t && riep.get_int("ANNO") == a) { const char freq = riep.get_char("FREQUENZA"); if (freq != '\0') return freq; } } } return (t == 'A') ? _freq_acq : _freq_ces; } int TIntra_mask::date2periodo(const TDate & d) { const char freq = frequenza(d.year()); if (freq == 'A') return 1; const int month = d.month(); if (freq == 'T') return (month / 3) + 1; return month; } const char* TIntra_mask::periodo_str() const { const short id = period_field(); const char* pe = "01"; if (id != DLG_NULL) { switch(frequenza(anno())) { case 'M': pe = get(id); break; case 'T': pe = get(id+1); break; default : break; } } else NFCHECK("Non e' stato specificato il campo del periodo"); return pe; } int TIntra_mask::periodo() const { return atoi(periodo_str()); } bool TIntra_mask::on_field_event(TOperable_field& o, TField_event e, long jolly) { if (jolly == 0 && o.dlg() == type_field()) { if (e == fe_modify || e == fe_init) { const short id = period_field(); if (id != DLG_NULL) { const char freq = frequenza(anno()); show(id+0, freq == 'M'); show(id+1, freq == 'T'); show(id+2, freq == 'A'); } } } return TRUE; } TIntra_mask::TIntra_mask(const char* name) : TAutomask(name) { on_firm_change(); } /////////////////////////////////////////////////////////// // TDati_riepilogo /////////////////////////////////////////////////////////// class TDati_riepilogo : public TSortable { TToken_string _key; TCurrency _ammlire, _ammvaluta; real _valstat, _massakg, _massaums; protected: virtual TObject* dup() const { return new TDati_riepilogo(*this); } virtual int compare(const TSortable& s) const; public: TDati_riepilogo& operator +=(const TDati_riepilogo& r); void write(TRectype& rec) const; const TString& stato(TString& c) const { _key.get(0, c); return c; } const TString& partita_iva(TString& c) const { _key.get(1, c); return c; } const TString& natura(TString& c) const { _key.get(3, c); return c; } const TString& nomenclatura(TString& c) const { _key.get(4, c); return c; } const TString& consegna(TString& c) const { _key.get(5, c); return c; } const TString& trasporto(TString& c) const { _key.get(6, c); return c; } const TString& paese(TString& c) const { _key.get(7, c); return c; } const TString& paese_orig(TString& c) const { _key.get(8, c); return c; } const TString& provincia(TString& c) const { _key.get(9, c); return c; } TDati_riepilogo(const TDati_riepilogo& r); TDati_riepilogo(const TToken_string& key, const TRectype& rec, const TString& codval); virtual ~TDati_riepilogo() { } }; int TDati_riepilogo::compare(const TSortable& s) const { const TDati_riepilogo& r = (const TDati_riepilogo&)s; return _key.compare(r._key); } TDati_riepilogo& TDati_riepilogo::operator +=(const TDati_riepilogo& r) { _ammlire += r._ammlire; _ammvaluta += r._ammvaluta; _valstat += r._valstat; _massakg += r._massakg; _massaums += r._massaums; return *this; } void TDati_riepilogo::write(TRectype& rec) const { TString str; rec.put("STATO", stato(str)); rec.put("PIVA", partita_iva(str)); rec.put("NATURA", natura(str)); rec.put("NOMENCL", nomenclatura(str)); rec.put("CONSEGNA", consegna(str)); rec.put("TRASPORTO", trasporto(str)); rec.put("PAESE", paese(str)); rec.put("PAESEORIG", paese_orig(str)); rec.put("PROV", provincia(str)); rec.put("AMMLIRE", _ammlire.get_num()); if (_ammvaluta.is_zero()) { rec.zero("CODVAL"); // Altrimenti genera falsi codici valuta EUR ... rec.zero("AMMVALUTA"); // ... con importi nulli } else { rec.put("CODVAL", _ammvaluta.get_value()); rec.put("AMMVALUTA", _ammvaluta.get_num()); } rec.put("VALSTAT", _valstat); rec.put("MASSAKG", _massakg); rec.put("MASSAUMS", _massaums); } TDati_riepilogo::TDati_riepilogo(const TDati_riepilogo& r) : _key(r._key), _valstat(r._valstat), _ammlire(r._ammlire), _ammvaluta(r._ammvaluta), _massakg(r._massakg), _massaums(r._massaums) { } TDati_riepilogo::TDati_riepilogo(const TToken_string& key, const TRectype& rec, const TString& codval) : _key(key), _ammlire(rec.get_real("AMMLIRE")), _ammvaluta(rec.get_real("AMMVALUTA"), codval), _valstat(rec.get_real("VALSTAT")), _massakg(rec.get_real("MASSAKG")), _massaums(rec.get_real("MASSAUMS")) { } /////////////////////////////////////////////////////////// // TRiepiloghi /////////////////////////////////////////////////////////// class TRiepiloghi : public TObject { TPointer_array _arr; TAssoc_array _ass; public: void add(const TRectype& rec, const TRectype& mov); int items() const { return _arr.items(); } int sort() { _arr.sort(); return items(); } const TDati_riepilogo& operator[](int r) const { return (const TDati_riepilogo&)_arr[r]; } }; void TRiepiloghi::add(const TRectype& rec, const TRectype& mov) { TString16 cod; const char tipocf = mov.get_char("TIPOCF"); cod << tipocf << '|' << mov.get("CODCF"); const TRectype& clifo = cache().get(LF_CLIFO, cod); cod = mov.get("CODVAL"); TToken_string key; key.add(clifo.get("STATOPAIV")); key.add(clifo.get("PAIV")); key.add(cod); // Non e' chiaro se raggruppare per valuta! key.add(rec.get("NATURA")); key.add(rec.get("NOMENCL")); key.add(rec.get("CONSEGNA")); key.add(rec.get("TRASPORTO")); key.add(rec.get("PAESE")); key.add(rec.get("PAESEORIG")); // Campo solo per Acquisti key.add(rec.get("PROV")); TDati_riepilogo* data = (TDati_riepilogo*)_ass.objptr(key); if (data == NULL) { data = new TDati_riepilogo(key, rec, cod); _ass.add(key, data); _arr.add(data); } else { const TDati_riepilogo dr(key, rec, cod); *data += dr; } } /////////////////////////////////////////////////////////// // TGenerazione_mask /////////////////////////////////////////////////////////// class TGenerazione_mask : public TIntra_mask { TRiepiloghi _riep; protected: virtual short type_field() const { return R_TIPO; } virtual short period_field() const { return R_PERIODO_M; } virtual int anno() const { return get_int(R_ANNO); } public: bool genera_riepiloghi(); TGenerazione_mask(); virtual ~TGenerazione_mask() { } }; bool TGenerazione_mask::genera_riepiloghi() { const char tipo = get(R_TIPO)[0]; const int anno_r = anno(); const int peri = periodo(); int da_mese, a_mese; const char freq = frequenza(anno_r); if (is_riepilogo(tipo, anno_r, peri) && !yesno_box("Attenzione esiste gia' il riepilogo\nper il periodo indicato, si desidera continuare")) return FALSE; switch (freq) { case 'T': da_mese = (peri-1) * 3 + 1; a_mese = da_mese+2; break; case 'A': da_mese = 1; a_mese = 12; break; default: da_mese = a_mese = peri; break; } const TDate da_data(1, da_mese, anno_r); const TDate a_data(TDate::last_day(a_mese, anno_r), a_mese, anno_r); TRectype filter_da(LF_INTRA), filter_a(LF_INTRA); filter_da.put("DATAREG", da_data); filter_a.put("DATAREG", a_data); TRelation rel(LF_INTRA); rel.add(LF_RINTRA, "NUMREG==NUMREG"); TString filter; filter << "TIPOMOV==\"" << tipo << '"'; TCursor cur(&rel, filter, 2, &filter_da, &filter_a); const long items = cur.items(); if (items > 0) { TProgind pi(items, TR("Lettura movimenti intra..."), TRUE, TRUE); cur.freeze(); for (cur = 0; cur.pos() < items; ++cur) { pi.addstatus(1); if (pi.iscancelled()) return warning_box(TR("Operazione annullata")); bool rowok = rel.is_first_match(LF_RINTRA); while (rowok) { _riep.add(rel.curr(LF_RINTRA), rel.curr()); rowok = rel.next_match(LF_RINTRA); } } } else return warning_box(TR("Non ci sono movimenti nel periodo specificato")); const int riepiloghi = _riep.sort(); if (riepiloghi > 0) { TProgind pi(riepiloghi, TR("Scrittura riepiloghi intra..."), FALSE, TRUE); TLocalisamfile riep(LF_RIEPRETT); riep.put("TIPO", tipo); riep.put("ANNO", anno_r); riep.put("PERIODO", peri); riep.put("NUMRIG", 1); int err = riep.read(); for (int r = 0; r < riepiloghi; r++) { pi.addstatus(1); riep.put("TIPO", tipo); riep.put("ANNO", anno_r); riep.put("PERIODO", peri); riep.put("NUMRIG", r+1); _riep[r].write(riep.curr()); riep.put("FREQUENZA", freq); const int werr = err == NOERR ? riep.rewrite() : riep.write(); if (werr != NOERR) return error_box(FR("Errore %d durante la scrittura dei riepiloghi"), werr); if (err == NOERR) { err = riep.next(); if (err == NOERR && riep.get_long("NUMRIG") == 1) err = _iseof; } } while (err == NOERR) { riep.remove(); err = riep.next(); if (err == NOERR && riep.get_long("NUMRIG") == 1) err = _iseof; } } return TRUE; } TGenerazione_mask::TGenerazione_mask() : TIntra_mask("in0500b") { } /////////////////////////////////////////////////////////// // Generazione riepiloghi /////////////////////////////////////////////////////////// void genera_riepiloghi(char tipo, int anno, int periodo) { TGenerazione_mask m; int num_fields = 0; if (tipo > ' ' ) { TString16 t; t << tipo; m.set(R_TIPO, t); num_fields++; } if (anno > 0) { m.set(R_ANNO, anno); num_fields++; } if (periodo > 0) { m.set(R_PERIODO_M, periodo); m.set(R_PERIODO_T, periodo); num_fields++; } if (num_fields == 3 || m.run() == K_ENTER) m.genera_riepiloghi(); } bool is_riepilogo(char tipo, int anno, int periodo) { TLocalisamfile riep(LF_RIEPRETT); riep.put("TIPO", tipo); riep.put("ANNO", anno); riep.put("PERIODO", periodo); riep.put("NUMRIG", 1); int err = riep.read(); return err == NOERR; }