diff --git a/ve/righedoc.cpp b/ve/righedoc.cpp deleted file mode 100755 index 8c1c7206a..000000000 --- a/ve/righedoc.cpp +++ /dev/null @@ -1,93 +0,0 @@ -#ifndef __RIGHEDOC_H -#include "righedoc.h" -#endif - -#ifndef __VE0100_H -#include "ve0100.h" -#endif - -#ifndef __VERIG_H -#include "verig.h" -#endif - -#ifndef __VE0100C_H -#include "ve0100c.h" -#endif - -#ifndef __SCONTI_H -#include "sconti.h" -#endif - - -void TRiga_documento_motore::load(TSheet_field & f, TToken_string& row) -{ - put( "STATORIGA", row.get( f.cid2index(FR_STATORIGA) ) ); - put( "TIPORIGA", row.get( f.cid2index(FR_TIPORIGA )) ); - TString16 codmag(row.get(f.cid2index(FR_CODMAG))); - - codmag.left_just(3); - codmag << row.get( f.cid2index(FR_CODDEP )); - put( "CODMAG", codmag); - put( "CODART", row.get( f.cid2index(FR_CODART )) ); - TString s(row.get(f.cid2index(FR_DESCR))); - int split_pos = s.find('\n'); - if (split_pos > 0) - { - put( "DESCR", s.left(split_pos)); - put("DESCLUNGA", "X"); - s.ltrim(split_pos); - put("DESCEST", s); - } - else - { - put("DESCR", s); - put("DESCLUNGA", ""); - zero("DESCEST"); - } - - put( "PREZZO", row.get( f.cid2index(FR_PREZZO )) ); - put( "UMQTA", row.get( f.cid2index(FR_UMQTA )) ); - put( "QTA", row.get( f.cid2index(FR_QTA )) ); - put( "QTAEVASA", row.get( f.cid2index(FR_QTAEVASA )) ); - put( "RIGAEVASA", row.get( f.cid2index(FR_RIGAEVASA )) ); - put( "TARA", row.get( f.cid2index(FR_TARA )) ); - put( "PNETTO", row.get( f.cid2index(FR_PNETTO )) ); - put( "NCOLLI", row.get( f.cid2index(FR_NCOLLI )) ); - put( "DAEVADERE", row.get( f.cid2index(FR_DAEVADERE )) ); - put( "SCONTO", row.get( f.cid2index(FR_SCONTO )) ); - put( "PERCPROV", row.get( f.cid2index(FR_PERCPROV )) ); - put( "IMPFISSO", row.get( f.cid2index(FR_IMPFISSO )) ); - put( "CODIVA", row.get( f.cid2index(FR_CODIVA )) ); - put( "ADDIVA", row.get( f.cid2index(FR_ADDIVA )) ); - put( "ASPBENI", row.get( f.cid2index(FR_ASPBENI )) ); -} - -void TRiga_documento_motore::save(TSheet_field & f, TToken_string& row ) -{ - row.add( get( "STATORIGA" ), f.cid2index(FR_STATORIGA )); - row.add( get( "TIPORIGA" ), f.cid2index(FR_TIPORIGA )); - const TString codmag(get("CODMAG")); - row.add( codmag.left(3), f.cid2index(FR_CODMAG )); - row.add( codmag.mid(3), f.cid2index(FR_CODDEP )); - row.add( get( "CODART" ), f.cid2index(FR_CODART )); - TString s(get("DESCR")); - if (get_bool("DESCLUNGA")) - s << get("DESCEST"); - row.add(s, f.cid2index(FR_DESCR )); - row.add( get( "UMQTA" ), f.cid2index(FR_UMQTA )); - row.add( get( "PREZZO" ), f.cid2index(FR_PREZZO )); - row.add( get( "QTA" ), f.cid2index(FR_QTA )); - row.add( get( "QTAEVASA" ), f.cid2index(FR_QTAEVASA )); - row.add( get( "RIGAEVASA" ), f.cid2index(FR_RIGAEVASA )); - row.add( get( "TARA" ), f.cid2index(FR_TARA )); - row.add( get( "PNETTO" ), f.cid2index(FR_PNETTO )); - row.add( get( "NCOLLI" ), f.cid2index(FR_NCOLLI )); - row.add( get( "DAEVADERE" ), f.cid2index(FR_DAEVADERE )); - row.add( get( "SCONTO" ), f.cid2index(FR_SCONTO )); - row.add( get( "PERCPROV" ), f.cid2index(FR_PERCPROV )); - row.add( get( "IMPFISSO" ), f.cid2index(FR_IMPFISSO )); - row.add( get( "CODIVA" ), f.cid2index(FR_CODIVA )); - row.add( get( "ADDIVA" ), f.cid2index(FR_ADDIVA )); - row.add( get( "ASPBENI" ), f.cid2index(FR_ASPBENI )); -} - diff --git a/ve/righedoc.h b/ve/righedoc.h deleted file mode 100755 index 9afc5ddc3..000000000 --- a/ve/righedoc.h +++ /dev/null @@ -1,50 +0,0 @@ -#ifndef __RIGHEDOC_H -#define __RIGHEDOC_H -// Numero di colonne presenti sullo sheet totale -#ifndef __ASSOC_H -#include "assoc.h" -#endif - -#ifndef __REAL_H -#include "real.h" -#endif - -#ifndef __ISAM_H -#include "isam.h" -#endif - -#ifndef __EXPR_H -#include "expr.h" -#endif - -#ifndef __TABUTIL_H -#include "tabutil.h" -#endif - -#ifndef __CONFIG_H -#include "config.h" -#endif - -#ifndef __VARMASK_H -#include "varmask.h" -#endif - -#ifndef __VELIB01_H -#include "velib01.h" -#endif - -class TRiga_documento_motore : public TRiga_documento -{ - public: - // Carica la riga dalla riga dello sheet - void load( TSheet_field& f, TToken_string& row); - // Registra la riga sullo sheet - void save( TSheet_field& f, TToken_string& row); - - TRiga_documento_motore(TDocumento* doc) : TRiga_documento(doc) { } - TRiga_documento_motore(const TRiga_documento& rec, TDocumento* doc) : TRiga_documento(rec, doc) { } - - virtual ~TRiga_documento_motore() {} -}; - -#endif diff --git a/ve/sconti.h b/ve/sconti.h index da97de75a..db0e35857 100755 --- a/ve/sconti.h +++ b/ve/sconti.h @@ -9,8 +9,8 @@ #include "tclifor.h" #endif -#ifndef __VELIB01_H -#include "velib01.h" +#ifndef __VELIB_H +#include "velib.h" #endif #ifndef __CONFIG_H diff --git a/ve/tclifor.cpp b/ve/tclifor.cpp index d87258de2..c720af531 100755 --- a/ve/tclifor.cpp +++ b/ve/tclifor.cpp @@ -27,8 +27,8 @@ #include "veini.h" #endif -#ifndef __VELIB01_H -#include +#ifndef __VELIB_H +#include #endif const TString& TData_picker::get( const TString& table, const TString& codtab, const TString& field ) diff --git a/ve/ve0100.h b/ve/ve0100.h index 808ea5de3..b8404eae4 100755 --- a/ve/ve0100.h +++ b/ve/ve0100.h @@ -81,10 +81,6 @@ #include "tclifor.h" #endif -#ifndef __RIGHEDOC_H -#include "righedoc.h" -#endif - #ifndef __VARMASK_H #include #endif @@ -97,8 +93,8 @@ #include "sconti.h" #endif -#ifndef __VELIB01_H -#include "velib01.h" +#ifndef __VELIB_H +#include "velib.h" #endif // Definizione della classe dell'applicazione motore diff --git a/ve/velib01.h b/ve/velib.h similarity index 75% rename from ve/velib01.h rename to ve/velib.h index af55b7f7b..e9ee55375 100755 --- a/ve/velib01.h +++ b/ve/velib.h @@ -1,5 +1,6 @@ -#ifndef __VELIB01_H -#define __VELIB01_H +#ifndef __VELIB_H +#define __VELIB_H + #ifndef __RELATION_H #include @@ -20,6 +21,10 @@ #ifndef __PAGAMENT_H #include "../cg/pagament.h" #endif + +#ifndef __TCLIFOR_H +#include "tclifor.h" +#endif #define RIGA_MERCE 'M' #define RIGA_SPESEDOC 'S' @@ -33,6 +38,7 @@ class TRiga_documento; class TCond_vendita; class TIVA; +// velib01 real lordo2netto(real& lordo, const TString& codiva, int ndec); inline real lordo2netto(real& lordo, const TString& codiva, bool is_valuta = FALSE) { return lordo2netto(lordo, codiva, is_valuta ? 3 : 0);} real netto2lordo(const real& netto, const TString& codiva, int ndec); @@ -76,7 +82,7 @@ class TDocumento_variable_field : public TVariable_field ~TDocumento_variable_field() {} }; -class TSpesa_prest : public TRectype +class TSpesa_prest : public TRectype // velib01 { protected: @@ -102,7 +108,7 @@ public: virtual ~TSpesa_prest() {} }; -class TIVA : public TRectype +class TIVA : public TRectype // velib01 { protected: @@ -122,7 +128,7 @@ public: virtual ~TIVA() {} }; -class TExpr_documento : public TExpression +class TExpr_documento : public TExpression // velib01 { TDocumento * _doc; TRiga_documento * _row; @@ -154,7 +160,7 @@ public: enum TTipo_formula { _documento, _riga }; -class TFormula_documento : public TRectype +class TFormula_documento : public TRectype // velib01 { TString16 _tab; TExpr_documento * _expr; @@ -179,8 +185,7 @@ public: virtual ~TFormula_documento(); }; - -class TTipo_documento : public TRectype +class TTipo_documento : public TRectype // velib03 { enum { _altro, _fattura, _bolla, _ordine}; @@ -232,7 +237,7 @@ public: virtual ~TTipo_documento(); }; -class TTipo_riga_documento : public TRectype +class TTipo_riga_documento : public TRectype // velib02 { static TAssoc_array _formule_riga; @@ -270,7 +275,7 @@ public: }; -class TRiga_documento : public TAuto_variable_rectype +class TRiga_documento : public TAuto_variable_rectype // velib02 { TDocumento * _doc; static TAssoc_array _tipi; @@ -337,7 +342,7 @@ public: enum TTipo_importo { _lordo, _netto, _imposta }; -class TDocumento : public TAuto_variable_rectype +class TDocumento : public TAuto_variable_rectype // velib03 { static TAssoc_array _tipi; @@ -446,7 +451,7 @@ public: virtual ~TDocumento(); }; -class TDocumento_mask : public TVariable_mask +class TDocumento_mask : public TVariable_mask // velib03 { int _progs_page; // pagina in cui cominciano i progressivi int _last_prog; // numero dell'ultimo progressivo @@ -465,7 +470,7 @@ public: virtual ~TDocumento_mask() { } }; -class TLista_documenti : public TObject +class TLista_documenti : public TObject // velib04 { TArray _documenti; @@ -491,7 +496,7 @@ public: virtual ~TLista_documenti() { } }; -class TLista_clifo : public TObject +class TLista_clifo : public TObject // velib04 { class TClifo : public TObject { @@ -545,13 +550,13 @@ public: virtual ~TLista_clifo() { } }; -class TLista_clienti : public TLista_clifo +class TLista_clienti : public TLista_clifo { protected: virtual char tipo() const { return 'C'; } }; -class TLista_fornitori : public TLista_clifo +class TLista_fornitori : public TLista_clifo { protected: virtual char tipo() const { return 'F'; } @@ -559,7 +564,7 @@ protected: enum TTipo_elaborazione {_esterna, _consegna_ordini, _fatturazione_bolle, _contabilizzazione, _copia_documento, _generazione_effetti }; -class TElaborazione : public TRectype +class TElaborazione : public TRectype // velib04 { protected: int read(const char* cod); @@ -594,7 +599,7 @@ public: virtual ~TElaborazione() { } }; -class TEsterna : public TElaborazione +class TEsterna : public TElaborazione // velib04 { public: virtual bool elabora(TLista_documenti& doc_in, TLista_documenti& doc_out, @@ -605,7 +610,7 @@ public: virtual ~TEsterna() { } }; -class TConsegna_ordini : public TElaborazione +class TConsegna_ordini : public TElaborazione // velib05 { public: virtual bool elabora(TLista_documenti& doc_in, TLista_documenti& doc_out, @@ -616,7 +621,7 @@ public: virtual ~TConsegna_ordini() { } }; -class TFatturazione_bolle : public TElaborazione +class TFatturazione_bolle : public TElaborazione // velib04 { TToken_string _cod_desc; @@ -633,7 +638,7 @@ public: virtual ~TFatturazione_bolle() { } }; -class TContabilizzazione : public TElaborazione +class TContabilizzazione : public TElaborazione // velib04 { public: virtual bool elabora(TLista_documenti& doc_in, TLista_documenti& doc_out, @@ -644,7 +649,7 @@ public: virtual ~TContabilizzazione() { } }; -class TCopia_documento : public TElaborazione +class TCopia_documento : public TElaborazione // velib04 { public: virtual bool elabora(TLista_documenti& doc_in, TLista_documenti& doc_out, @@ -656,7 +661,7 @@ public: }; -class TGenerazione_effetti : public TElaborazione +class TGenerazione_effetti : public TElaborazione // velib04 { public: virtual bool elabora(TLista_documenti& doc_in, TLista_documenti& doc_out, @@ -667,7 +672,7 @@ public: virtual ~TGenerazione_effetti() { } }; -class TLista_elaborazioni : public TObject +class TLista_elaborazioni : public TObject // velib04 { TAssoc_array * _elab; @@ -682,4 +687,142 @@ public: virtual ~TLista_elaborazioni(); }; +struct dec_parm { + int pri_lit, + pri_val, + qta_lit, + qta_val, // si lo so non ha senso, ma lo faccio lo stesso + imp_lit, + imp_val; + // add other groups here +}; + +class TCodice_numerazione : public TRectype +{ + int _status; +public: + TObject* dup() const { return new TCodice_numerazione(codice()); } + +public: + int read(const char * cod); + const TString& codice() const { return get("CODTAB"); } + const TString& prefisso() const { return get("S6"); } + const TString& postfisso() const { return get("S7"); } + const bool manual_num() const { return get_bool("B0"); } + const bool auto_num() const { return get_bool("B1"); } + const TString& tipo_doc1() { return get("S2").mid(0,4);} + const TString& tipo_doc2() { return get("S2").mid(4,4);} + const TString& tipo_doc3() { return get("S2").mid(8,4);} + const TString& tipo_doc4() { return get("S2").mid(12,4);} + const TString& tipo_doc5() { return get("S2").mid(16,4);} + + const char * complete_num (long num); + int status() { return _status; } + TCodice_numerazione(const char* codnum = NULL); + TCodice_numerazione(const TRectype& rec); + virtual ~TCodice_numerazione(); +}; + +////////////////////////////////////////////////////////////////////////////////////////////// +// classe TDocumentoEsteso: oggetto che ha come finalita' il calcolo dei riepiloghi IVA +////////////////////////////////////////////////////////////////////////////////////////////// + +class TRiepilogo_Iva : public TObject +{ + real _imp; + real _iva; + real _ali; + byte _tipo; + TString16 _cod; + TString _des; + +protected: + +public: + real& imp() { return _imp;} // Imponibile + real& iva() { return _iva;} // Iva + real& ali() { return _ali;} // Aliquota + TString& cod() { return _cod;} // Descrizione + TString& des() { return _des;} // Descrizione + byte& tipo(){ return _tipo;}// Tipo (Vedi opzioni per la selzione di filtro nella validate()) + void zero(){ _imp = 0.0; _iva = 0.0; _ali = 0.0; _tipo = 0; _cod = ""; _des = ""; } + virtual TObject* dup() const { return new TRiepilogo_Iva(*this); } + TRiepilogo_Iva& operator = (TRiepilogo_Iva& a); + TRiepilogo_Iva() {_imp = 0.0; _iva = 0.0; _ali = 0.0; _tipo = 0;} + ~TRiepilogo_Iva() {}; +}; + +class TDocumentoEsteso : public TDocumento +{ + // Parametri del documento + dec_parm _parm; // Parametri per gli arrotondamenti + TTable * _iva; // Tabella codici IVA + + // Totali del documento ricalcolati non appena la tabellina di riepilogo IVA e' completa + real _importi_netti, _imposte; + + // membri per il calcolo del riepilogo IVA + bool _sum_selected; // TRUE se ha selezionato una riga del riepilogo, funge da semaforo per leggere la prossima + int _sum_filter; // Filtro corrente della riga TRiepilogo_Iva in corso di stampa (-1 se non ha ancora calcolato la tabella) + TString_array _order_array; // Array di TToken_string contenenti i codici IVA soddisfacenti ad ogni tipo di filtro + TAssoc_array _summary_table; // Array dove vengono memorizzate le imposte per aliquota + TArray _summary_array; // Array dove vengono memorizzate le imposte/imponibili per riga + TRiepilogo_Iva _sum_current; // Riga corrente del riepilogo + + // membri per il calcolo del riepilogo scadenze + TString_array _scadenze_array;// Array che contiene le scadenze ("|") + int _scadenze_current; // indice per identificare l'elementi corrente sull'array (-1 se non ha ancora calcolato) + +public: + + + // Funzioni per il riepilogo IVA + const bool summary_compiled() { return _sum_filter > -1; } + void compile_summary(); // Aggiorna la tabella riepilogativa + void summary_filter(byte selector); // filtra la tabellina secondo il filtro corrente se non e' gia' stato fatto + void summary_reset(bool force=FALSE); // riposiziona l'array dei codici IVA (_order_array). Se il parametro e' TRUE forza il ricalcolo della tabella + void summary_set_next(); // seleziona il prossimo elemento del filtro + const TRiepilogo_Iva& sum_current() { return _sum_current; } // ritorna la riga corrente del filtro corrente + const char * summary_get(const TString& w); // ritorna l'informazione richiesta estratta dall'elemento corrente + int summary_items() { return _summary_table.items();} // ritorna il numero di righe in totale della tabellina + const TAssoc_array& summary() { return _summary_table; } + const TArray& summary_array() { return _summary_array;} + + + // Funzioni per il ricalcolo delle scadenze + void scadenze_reset(); // riposiziona sulla prima scadenza + void scadenze_recalc(); // resetta e ricalcola le scadenze + void scadenze_set_next(); // seleziona il prossimo elemento dell'array delle scadenze + const char * scadenze_get(const TString& w); // reperisce l'informazione richiesta dall'elemento corrente + int scadenze_items() { return _scadenze_array.items(); } // restituisce il numero di scadenze + TString_array& scadenze() { return _scadenze_array; } + + // Funzioni di totalizzazione + real& tot_importi_netti(); + real& tot_imposte(); + real tot_spese(); + real tot_documento(); + // restituisce tot_imponibili, tot_esenti, tot_nonsoggetti a seconda del selettore: + // 1 = regime normale + // 2 = da ventilare (non usato) + // 4 = esenti + // 8 = non imponibili + // 16 = non soggetti + // pertanto i non imponibili avranno selettore 1 e gli esenti selettore 4. + // per avere esenti + non soggetti il selettore sara' 20 e cosi' via. + real tot_imponibili(byte selector); + + // Reperisce l'informazione dal campo G1 della testata + const char* get_head_info(const TString& what); + + // Funzioni per settare i parametri + void set_decimals(dec_parm & parm) { _parm = parm ; } + void set_condv(TCliFor * cli); // Cambia le condizioni di vendita + TDocumentoEsteso (const TRectype & rec, dec_parm & parm, TCond_vendita * condv = NULL) ; + TDocumentoEsteso (const TRectype & rec, TCond_vendita * condv = NULL) ; + TDocumentoEsteso () ; + ~TDocumentoEsteso(); +}; + + #endif diff --git a/ve/velib01.cpp b/ve/velib01.cpp index f24660ca2..c8bccc3b5 100755 --- a/ve/velib01.cpp +++ b/ve/velib01.cpp @@ -1,30 +1,12 @@ -#include +#include "velib.h" +#include "vepriv.h" #include -#ifndef __APPLICAT_H -#include -#endif - #ifndef __EXPR_H #include #endif -#ifndef __STACK_H -#include -#endif - -#ifndef __UTILITY_H -#include -#endif - -#include "velib01.h" -#include "sconti.h" - -#ifndef __VE0100C_H -#include "ve0100c.h" -#endif - #ifndef __VERIG_H #include "verig.h" #endif @@ -37,27 +19,14 @@ #include "veuml1.h" #endif -#ifndef __VEINI_H -#include "veini.h" -#endif - #ifndef __DEFMASK_H #include #endif - -#ifndef __PAGAMENT_H -#include "..\cg\pagament.h" -#endif - #ifndef __SCONTI_H #include "sconti.h" #endif -#ifndef __EXECP_H -#include -#endif - TSpesa_prest::TSpesa_prest(const char* codice, char tipo) : TRectype(LF_TAB) { @@ -235,7 +204,7 @@ bool data_hndl( TMask_field& field, KEY key ) // handler delle righe -HIDDEN void row_set_handler( TMask& m, const int field, const int index ) +void row_set_handler( TMask& m, const int field, const int index ) { switch ( index ) { @@ -250,7 +219,7 @@ HIDDEN void row_set_handler( TMask& m, const int field, const int index ) HIDDEN TString16 curr_um; HIDDEN real curr_fc(1.0); -HIDDEN bool iva_handler( TMask_field& f, KEY key ) +bool iva_handler( TMask_field& f, KEY key ) { if (key == 0 || (key == K_ENTER && f.get().empty())) { @@ -264,7 +233,7 @@ HIDDEN bool iva_handler( TMask_field& f, KEY key ) return TRUE; } -HIDDEN bool codart_handler( TMask_field& f, KEY key ) +bool codart_handler( TMask_field& f, KEY key ) { TMask& row_mask = f.mask(); @@ -354,7 +323,7 @@ HIDDEN bool codart_handler( TMask_field& f, KEY key ) return TRUE; } -HIDDEN bool liv_handler( TMask_field& f, KEY key ) +bool liv_handler( TMask_field& f, KEY key ) { if (f.to_check(key, TRUE)) { @@ -372,7 +341,7 @@ HIDDEN bool liv_handler( TMask_field& f, KEY key ) return TRUE; } -HIDDEN bool umart_handler( TMask_field& f, KEY key ) +bool umart_handler( TMask_field& f, KEY key ) { // Se qualcuno cerca di modificare la maschera if ( key == K_TAB && f.focusdirty()) @@ -413,7 +382,7 @@ HIDDEN bool umart_handler( TMask_field& f, KEY key ) return TRUE; } -HIDDEN bool descr_handler( TMask_field& f, KEY key ) +bool descr_handler( TMask_field& f, KEY key ) { if (key == K_TAB && f.focusdirty()) { @@ -436,7 +405,7 @@ HIDDEN bool descr_handler( TMask_field& f, KEY key ) return TRUE; } -HIDDEN bool qta_handler( TMask_field& f, KEY key ) +bool qta_handler( TMask_field& f, KEY key ) { // Se qualcuno cerca di modificare la maschera if ( key == K_TAB && f.focusdirty()) @@ -456,7 +425,7 @@ HIDDEN bool qta_handler( TMask_field& f, KEY key ) return TRUE; } -HIDDEN void sppr_calc(TRectype & rec, const TString & valuta_doc, const real & cambio, real & prezzo) +void sppr_calc(TRectype & rec, const TString & valuta_doc, const real & cambio, real & prezzo) { const TString16 sppr_valuta(rec.get("S4")); @@ -475,7 +444,7 @@ HIDDEN void sppr_calc(TRectype & rec, const TString & valuta_doc, const real & c } } -HIDDEN bool sppr_handler( TMask_field& f, KEY key ) +bool sppr_handler( TMask_field& f, KEY key ) { TMask& row_mask = f.mask(); @@ -584,8 +553,6 @@ real prezzo_scontato(const real& prezzo, const char * sconto) return scontato; } -enum TTipo_calcolo { _nessun_calcolo, _qtaprezzo, _valore, _percentuale, _scontoimp, _scontoperc}; - real iva(real imponibile, const TIVA & iva,int ndec) { real val = imponibile * iva.aliquota() / 100.0; @@ -600,197 +567,10 @@ real iva(real imponibile, const TIVA & iva,int ndec) return val; } -real TDocumento::spese_incasso(real & imp, int ndec, TTipo_importo t) const -{ - real imp_spese; - real percentuale = get_real("PERCSPINC"); - static TArray spese_inc; - - if (percentuale > ZERO) - { - if (spese_inc.objptr(_rim_dir) == NULL) - { - TConfig conf(CONFIG_STUDIO); - - for (TTipo_pag p = _rim_dir; p < _nessun_pag; p = (TTipo_pag) ((int)p + 1)) - { - real r(conf.get("IMPSPINC", "ve", p)); - spese_inc.add(r, p); - } - } - TPagamento & pag = ((TDocumento *)this)->pagamento(); - const int nrate = pag.n_rate(); - - for (int i = 0; i < nrate; i++) - { - const TTipo_pag p = (TTipo_pag) pag.tipo_rata(i); - imp_spese += (real &) spese_inc[p]; - } - imp_spese *= percentuale / 100.0; - - real cambio = get_real("CAMBIO"); - - if (cambio == ZERO) - cambio = 1.0; - static TString16 codiva; - static long firm = -1; - long new_firm = main_app().get_firm(); - - if (firm != new_firm) - { - TConfig conf(CONFIG_DITTA); - - codiva = conf.get("SPINCODIVA", "ve"); - firm = new_firm; - } - real iva_spese(iva(imp_spese, TRiga_documento::iva(codiva), ndec)); - if (t == _lordo) - imp_spese += iva_spese; - else - if (t == _imposta) - imp_spese = iva_spese; - imp_spese /= cambio; - imp_spese.round(ndec); - } - return imp_spese; -} - -real TDocumento::bolli(real & imp, int ndec, TTipo_importo t) const -{ - real tot_bolli; - static TArray sca_bolli; - static TArray imp_bolli; - static int nscagl; - TLocalisamfile clifo(LF_CLIFO); - bool estero = 2; - - if (get_bool("ADDBOLLI")) - { - real cambio = get_real("CAMBIO"); - - if (cambio == ZERO) - cambio = 1.0; - - real importo = imp*cambio; - TPagamento & pag = ((TDocumento*)this)->pagamento(); - const int nrate = pag.n_rate(); - real old_bolli = -1.00; - real iva_bolli; - real imp_orig = imposta(); - real sp_orig = spese(); - - for (int j = 0; j < 5 && tot_bolli != old_bolli; j++) - { - old_bolli = tot_bolli + iva_bolli; - const real imposte = imp_orig * cambio + iva_bolli; - const real imp_spese = sp_orig * cambio + tot_bolli - iva_bolli; - const real imponibile = importo - imposte - imp_spese; - - tot_bolli = ZERO; - pag.set_total(imponibile, imposte, imp_spese); - pag.set_rate_auto(); - - for (int i = 0; i < nrate; i++) - { - const TTipo_pag p = (TTipo_pag) pag.tipo_rata(i); - real imp = pag.importo_rata(i); - - switch (p) - { - case _ric_ban: - { - if (sca_bolli.objptr(0) == NULL) - { - TConfig conf(CONFIG_STUDIO); - - for (nscagl = 0; nscagl < 7; nscagl++) - { - real s(conf.get("SPBOSCA", "ve", nscagl + 1)); - real i(conf.get("SPBOIMP", "ve", nscagl + 1)); - - if (s == ZERO && i == ZERO) - break; - sca_bolli.add(s, nscagl); - imp_bolli.add(i, nscagl); - } - } - for (int i = 0; i < nscagl - 1; i++) - if ((real &) sca_bolli[i] >= imp) - break; - if (imp_bolli.items() > 0) - tot_bolli += (real &) imp_bolli[i]; - } - break; - case _tratta: - case _tratta_acc: - { - if (estero == 2) - { - clifo.put("TIPOCF", get("TIPOCF")); - clifo.put("CODCF", get("CODCF")); - - if (clifo.read() != NOERR) - clifo.zero(); - - const TString16 stato(clifo.get("STATOPAIV")); - - estero = stato.not_empty() && stato != "IT"; - if (!estero) - estero = clifo.get("STATOCF").not_empty() || clifo.get("COMCF")[0] == 'Z'; - } - real r(imp); - - r.ceil(-3); - if (estero) - r *= 0.009; - else - r *= 0.012; - r.round(-2); - tot_bolli += r; - } - break; - case _cessione: - case _paghero: - case _let_cred: - case _rim_dir: - case _rid: - case _bonfico: - default: - break; - } - } - static TString16 codiva; - static long firm = -1; - long new_firm = main_app().get_firm(); - - if (firm != new_firm) - { - TConfig conf(CONFIG_DITTA); - - codiva = conf.get("SPBOCODIVA", "ve"); - firm = new_firm; - } - iva_bolli = iva(tot_bolli, TRiga_documento::iva(codiva), ndec); - importo += (tot_bolli + iva_bolli - old_bolli); - } - if (t == _lordo) - tot_bolli += iva_bolli; - else - if (t == _imposta) - tot_bolli = iva_bolli; - tot_bolli /= cambio; - tot_bolli.round(ndec); - } - - return tot_bolli; -} - /////////////////////////////////////////////////////////// // Formula generica /////////////////////////////////////////////////////////// -HIDDEN enum _formula {_somma, _bolli, _bolli_int, _spinc, _prezzo, _importo, _sconto, _iva, _provv, _tipo, _imponibili, _imposte}; - TExpr_documento::TExpr_documento(const char* expression, TTypeexp type, TDocumento * doc, TRiga_documento * row) : TExpression(type), _doc(doc), _row(row) @@ -1151,2225 +931,3 @@ int TFormula_documento::read(const char* codice, const char * expr, bool numexpr } -/////////////////////////////////////////////////////////// -// Tipo documento -/////////////////////////////////////////////////////////// - -TAssoc_array TTipo_documento::_formule_documento; - -TTipo_documento::TTipo_documento(const char* tipodoc) - : TRectype(LF_TABCOM) -{ - settab("TIP"); - if (tipodoc && *tipodoc) - read(tipodoc); -} - -TTipo_documento::TTipo_documento(const TRectype& rec) - : TRectype(rec) -{ - read_formule(); -} - -TTipo_documento::~TTipo_documento() -{ } - -int TTipo_documento::read(const char* tipodoc) -{ - TTable t("%TIP"); - put("CODTAB", tipodoc); - int err = TRectype::read(t); - _formule = ""; - if (err == NOERR) - read_formule(); - else - yesnofatal_box("Tipo documento errato: %s", tipodoc); - return err; -} - -void TTipo_documento::read_formule() - -{ - TFilename prof(profile_name()); - - prof.ext("ini"); - - TConfig profile(prof); - - _formule = profile.get("CAMPICALC", "MAIN"); - _formule.add(profile.get("CALCOLI", "MAIN")); - _totale = profile.get("TOTALE", "MAIN"); - if (_totale.empty()) - { - _totale = "TOTDOC"; - if (_formule.find(_totale) < 0) - _formule.add("TOTDOC=IMPONIBILI()+IMPOSTE()"); - } - _totale_netto = "_"; - _totale_netto << _totale; - if (_totale.not_empty() && _formule.find(_totale) < 0) - { - error_box("Campo totale documento (%s) sconosciuto nel tipo documento %s", (const char *) _totale, (const char *) codice()); - _totale.cut(0); - } - _basesconto = profile.get("BASESCONTO", "MAIN"); - if (_basesconto.empty()) - { - _basesconto = "BASESCONTO"; - if (_formule.find(_basesconto) < 0) - _formule.add("BASESCONTO=SOMMA(\"IMPONIBILE()\", \"(TIPO() != 'S') && (TIPO() != 'C')\")"); - } - if (_basesconto.not_empty() && _formule.find(_basesconto) < 0) - { - error_box("Campo sconto documento (%s) sconosciuto nel tipo documento %s", (const char *) _basesconto, (const char *) codice()); - _basesconto.cut(0); - } - _spese = profile.get("SPESE", "MAIN"); - if (_spese.empty()) - { - _spese = "SPESE"; - if (_formule.find(_spese) < 0) - _formule.add("SPESE=SOMMA(\"IMPONIBILE()\", \"TIPO() != 'S'\")"); - } - if (_spese.not_empty() && _formule.find(_spese) < 0) - { - error_box("Campo spese (%s) sconosciuto nel tipo documento %s", (const char *) _spese, (const char *) codice()); - _spese.cut(0); - } -} - -TFormula_documento * TTipo_documento::succ_formula(bool restart) -{ - if (restart) - _formule.restart(); - const TString formula(_formule.get()); - if (formula.not_empty()) - { - char *expr = NULL; - const int p = formula.find("="); - if (p > 0) - { - expr = (char *) (const char *) formula + p; - *expr = '\0'; expr++; - } - TFormula_documento * o = (TFormula_documento*)_formule_documento.objptr(formula); - if (o == NULL) - { - o = new TFormula_documento(_documento, formula, expr); - _formule_documento.add(formula, o); - } - return o; - } - else - return NULL; -} - -/////////////////////////////////////////////////////////// -// Tipo riga di un documento -/////////////////////////////////////////////////////////// - -TAssoc_array TTipo_riga_documento::_formule_riga; - -TTipo_riga_documento::TTipo_riga_documento(const char* tiporig) - : TRectype(LF_TABCOM), _mask(NULL) -{ - settab("TRI"); - _name = "verig"; - _name << codice(); - if (tiporig && *tiporig) - read(tiporig); -} - -TTipo_riga_documento::TTipo_riga_documento(const TRectype& rec) - : TRectype(rec), _mask(NULL) -{ - _name = "verig"; - _name << codice(); - read_formule(); -} - -TTipo_riga_documento::~TTipo_riga_documento() -{ - if (_mask) delete _mask; -} - -int TTipo_riga_documento::read(const char* tiporig) -{ - TTable t("%TRI"); - put("CODTAB", tiporig); - int err = TRectype::read(t); - if (err == NOERR) - read_formule(); - else - yesnofatal_box("Tipo riga documento errato: %s", tiporig); - return err; -} - -void TTipo_riga_documento::read_formule() - -{ - TFilename prof(profile_name()); - - prof.ext("ini"); - - TConfig profile(prof); - - _formule = profile.get("CAMPICALC", "MAIN"); - _formule.add(profile.get("CALCOLI", "MAIN")); - _imponibile = profile.get("IMPONIBILE", "MAIN"); - if (_imponibile.empty()) - { - _imponibile = "IMPONIBILE"; - if (_formule.find(_imponibile) < 0) - _formule.add("IMPONIBILE=PREZZO()"); - } - if (_imponibile.not_empty() && _formule.find(_imponibile) < 0) - { - error_box("Campo imponibile (%s) sconosciuto nel tipo riga %s", (const char *) _imponibile, (const char *) codice()); - _imponibile.cut(0); - } -} - -TFormula_documento * TTipo_riga_documento::succ_formula(bool restart) -{ - if (restart) - _formule.restart(); - const TString formula(_formule.get()); - if (formula.not_empty()) - { - char *expr = NULL; - const int p = formula.find("="); - if (p > 0) - { - expr = (char *) (const char *) formula + p; - *expr = '\0'; expr++; - } - TFormula_documento * o = (TFormula_documento*)_formule_riga.objptr(formula); - if (o == NULL) - { - o = new TFormula_documento(_riga, formula, expr); - _formule_riga.add(formula, o); - } - return o; - } - else - return NULL; -} - -TVariable_mask * TTipo_riga_documento::mask() -{ - if (mask_loaded()) - return _mask; - _mask = new TVariable_mask(mask_name()); - TFilename proname(profile_name()); - - proname.ext( "ini" ); - - TConfig pro( proname ); - int numhandler = pro.get_int( "NHANDLER", "HANDLERS" ); - - for( int i = 1; i <= numhandler; i ++ ) - { - TString chiave; - chiave.format( "%d", i ); - TToken_string riga = pro.get( chiave, "HANDLERS" ); - row_set_handler( *_mask, riga.get_int( 0 ), riga.get_int( 1 ) ); - } - - const int pos = _mask->id2pos(FR_CODART); - - if (pos >= 0) - { - const TMask_field & f = _mask->field(FR_CODART); - if (f.is_edit()) - { - TBrowse * browse = ((TEdit_field &) f).browse(); - const char tipo_r = tipo(); - - if (browse ) - { - const TCursor * cur = browse->cursor(); - if (cur) - { - const int num = cur->file().num(); - if (num == LF_ANAMAG) - { - _mask->set_handler( FR_CODART, codart_handler ); - _mask->set_handler( FR_LIV1, liv_handler ); - _mask->set_handler( FR_LIV2, liv_handler ); - _mask->set_handler( FR_LIV3, liv_handler ); - _mask->set_handler( FR_UMQTA, umart_handler ); - _mask->set_handler( FR_DESCR, descr_handler ); - _mask->set_handler( FR_QTA, qta_handler ); - } - else - if (tipo_r == RIGA_SPESEDOC || tipo_r == RIGA_PRESTAZIONI) - _mask->set_handler( FR_CODART, sppr_handler ); - } - } - } - } - const int posiva = _mask->id2pos(FR_CODIVA); - - if (posiva >= 0) - _mask->set_handler( FR_CODIVA, iva_handler ); - - return _mask; -} - -/////////////////////////////////////////////////////////// -// Riga documento per vendite -/////////////////////////////////////////////////////////// - -TAssoc_array TRiga_documento::_tipi; -TAssoc_array TRiga_documento::_spese; -TAssoc_array TRiga_documento::_ive; - -TRiga_documento::TRiga_documento(TDocumento* doc, const char * tipo) - : TAuto_variable_rectype(LF_RIGHEDOC), _doc(doc), _iva_calc(FALSE) -{ - if (tipo) - set_tipo(tipo); -} - -TRiga_documento::TRiga_documento(const TRiga_documento& rec, TDocumento* doc, - const char * tipo) - : TAuto_variable_rectype(rec), _doc(doc), _iva_calc(FALSE) -{ - if (tipo) - set_tipo(tipo); -} - -const TTipo_riga_documento& TRiga_documento::tipo() const -{ - const TString16 tiporig(get("TIPORIGA")); - CHECK(tiporig.not_empty(), "Tipo riga documento nullo"); - TTipo_riga_documento* o = (TTipo_riga_documento*)_tipi.objptr(tiporig); - if (o == NULL) - { - if (_tipi.items() == 0) - { - TTable tri("%TRI"); // Tabella dei tipi riga - for (tri.first(); !tri.eof(); tri.next()) - { - const TString16 codice = tri.get("CODTAB"); - _tipi.add(codice, new TTipo_riga_documento(tri.curr())); - } - } - o = (TTipo_riga_documento*)_tipi.objptr(tiporig); - if (o == NULL) - { - o = new TTipo_riga_documento(tiporig); - _tipi.add(tiporig, o); - } - } - return *o; -} - -const TSpesa_prest & TRiga_documento::spesa() const -{ - const char tipor = tipo().tipo(); - - CHECK(tipor == RIGA_SPESEDOC || tipor == RIGA_PRESTAZIONI, "Tipo riga incompatibile con le spese"); - static long firm = -1; - long new_firm = main_app().get_firm(); - - if (firm != new_firm) - { - _spese.destroy(); - firm = new_firm; - } - - const TString16 codice(get("CODART")); - TString16 index; index << tipor << codice; - - TSpesa_prest * s = (TSpesa_prest *) _spese.objptr(index); - if (s == NULL) - { - s = new TSpesa_prest(codice, tipor); - _spese.add(index, s); - } - return *s; -} - -const TIVA & TRiga_documento::iva(const char *codice) -{ - TIVA * v = (TIVA *) _ive.objptr(codice); - if (v == NULL) - { - v = new TIVA(codice); - _ive.add(codice, v); - } - return *v; -} - -bool TRiga_documento::sola_descrizione() const -{ - char t = tipo().tipo(); - if (t <= ' ' && get("QTA").empty() && get("PREZZO").empty()) - t = RIGA_DESCRIZIONI; - return t == RIGA_DESCRIZIONI; -} - -void TRiga_documento::forza_sola_descrizione() -{ - // In realta' il test serve anche a caricare la lista dei tipi riga! - if (!tipo_valido() || tipo().tipo() != RIGA_DESCRIZIONI) - { - _tipi.restart(); - for (const TObject* o = _tipi.get(); o; o = _tipi.get()) - { - const TTipo_riga_documento* trd = (const TTipo_riga_documento*)o; - if (trd->tipo() == RIGA_DESCRIZIONI) - { - put("TIPORIGA", trd->codice()); - break; - } - } - zero("QTA"); - zero("PREZZO"); - } -} - -TRectype & TRiga_documento::operator =(const TRectype & r) -{ - TRectype::operator=(r); - reset_fields(*this); - set_fields(*this); - return *this; -} - -TRectype & TRiga_documento::operator =(const char * r) -{ - TRectype::operator=(r); - reset_fields(*this); - set_fields(*this); - return *this; -} - -// Ritorna TRUE se le due righe del documento possono essere sommate -bool TRiga_documento::raggruppabile(const TRiga_documento& r, TToken_string& campi) const -{ - bool ok = TRUE; - TString campo; - for (const char* c = campi.get(0); c && ok; c = campi.get()) - { - campo = get(c); // Separare le due get! - ok &= campo == r.get(c); - } - return ok; -} - -TRiga_documento& TRiga_documento::operator +=(const TRiga_documento& r) -{ - TToken_string campi("QTA|NCOLLI|QTAEVASA"); - for (const char* c = campi.get(0); c && ok; c = campi.get()) - { - real num(r.get_real(c)); - if (!num.is_zero()) - { - num += get_real(c); - put(c, num); - } - } - if (!get_bool("RIGAEVASA")) - { - const real qta = get_real("QTA"); - const real qtaeva = get_real("QTAEVASA"); - if (qtaeva >= qta) - put("RIGAEVASA", "X"); - } - - return *this; -} - -void TRiga_documento::set_fields(TAuto_variable_rectype & rec) -{ - if (get("TIPORIGA").not_empty()) - { - TTipo_riga_documento & tipo_riga = (TTipo_riga_documento &) tipo(); - - for (const TFormula_documento * f = tipo_riga.first_formula(); f; f = tipo_riga.succ_formula()) - { - TExpr_documento * exp = f->expr(); - add_field(new TDocumento_variable_field(f->name(), exp)); - if (exp) - { - exp->set_doc(_doc); - exp->set_row(this); - } - } - } -} - -real TRiga_documento::prezzo(bool scontato, bool lordo, int ndec) const -{ - real prezzo = get_real("PREZZO"); - if (scontato) - prezzo = prezzo_scontato(prezzo, get("SCONTO")); - prezzo.round(ndec); - if (lordo) - prezzo = netto2lordo(prezzo, get("CODIVA"), ndec); - prezzo.round(ndec); - return prezzo; -} - -real TRiga_documento::importo(bool scontato, bool lordo, int ndec) const -{ - real importo; - - TTipo_calcolo c = _nessun_calcolo; - const char tipor = tipo().tipo(); - const real qta = get_real("QTA"); - TString16 field_perc; - TCond_vendita cv; - - switch (tipor) - { - case RIGA_MERCE: - c = _qtaprezzo; - break; - case RIGA_PRESTAZIONI: - case RIGA_SPESEDOC: - { - const TSpesa_prest & s = spesa(); - - switch (s.tipo()) - { - case 'Q': - c = _qtaprezzo; - break; - case 'V': - c = _valore; - break; - case 'P': - c = _percentuale; - field_perc = s.field_perc(); - break; - default: - break; - } - } - break; - case RIGA_SCONTI: - cv.set_sconto(get("SCONTO")); - - if (cv.get_sconto().not_empty()) - c = _scontoperc; - else - c = _scontoimp; - break; - case RIGA_OMAGGI: - if (_iva_calc && get_bool("ADDIVA")) - c = _qtaprezzo; - default: - break; - } - switch (c) - { - case _qtaprezzo: - importo = prezzo(scontato, lordo, ndec) * qta; - break; - case _valore: - importo = prezzo(scontato, lordo, ndec); - break; - case _percentuale: - importo = doc().get_real(field_perc) * get_real("PSPESA") / 100; - break; - case _scontoimp: - importo = -prezzo(FALSE, lordo, ndec); - break; - case _scontoperc: - importo = doc().basesconto() * (cv.sconto_val() - 1.0); - break; - default: - break; - } - importo.round(ndec); - - return importo; -} - -real TRiga_documento::iva(int ndec) const -{ - ((TRiga_documento *) this)->_iva_calc = TRUE; - real iva(::iva(imponibile(), iva(), ndec)); - ((TRiga_documento *) this)->_iva_calc = FALSE; - return iva; -} - -real TRiga_documento::imponibile() const -{ - const TString16 field(tipo().imponibile()); - - if (field.not_empty()) - { - if (tipo().tipo() == RIGA_OMAGGI && _iva_calc) - { - TDocumento_variable_field * f = (TDocumento_variable_field *) variable_field(field); - CHECKS(f, "Field UNKNOWN : ", field); - f->set_dirty(TRUE); - const real r = get_real(field); - f->set_dirty(TRUE); - return r; - } - else - return get_real(field); - } - else - return importo(TRUE, FALSE, doc().in_valuta() ? 3 : 0); -} - -real TRiga_documento::imposta(bool round) const -{ - return iva(round ? (doc().in_valuta() ? 3 : 0) : 20); -} - -void TRiga_documento::dirty_fields(bool dirty_document) -{ - for (TDocumento_variable_field * f = (TDocumento_variable_field *) first_variable_field(); - f != NULL; f = (TDocumento_variable_field *) succ_variable_field()) - f->set_dirty(); - if (dirty_document) - ((TDocumento &)doc()).dirty_fields(); -} - -bool TRiga_documento::doc_dependent() const - -{ - if (tipo_valido()) - { - const char tipor = tipo().tipo(); - - if (tipor == RIGA_SPESEDOC) - return spesa().tipo() == 'P'; - else - if (tipor == RIGA_SCONTI) - return get("SCONTO").not_empty(); - } - return FALSE; -} - -void TRiga_documento::put_str(const char* fieldname, const char* val) -{ - TString v(val); - if (strcmp(fieldname, "TIPORIGA") == 0 && TRectype::get("TIPORIGA") != v) - { - TAuto_variable_rectype::put_str(fieldname, v); - reset_fields(*this); - set_fields(*this); - } - else - { - TAuto_variable_rectype::put_str(fieldname, v); - dirty_fields(); - } -} - -void TRiga_documento::zero(const char * fieldname) -{ - if (strcmp(fieldname, "TIPORIGA") == 0) - reset_fields(*this); - TAuto_variable_rectype::zero(fieldname); - dirty_fields(); -} - -void TRiga_documento::zero(char c) -{ - reset_fields(*this); - TAuto_variable_rectype::zero(c); -} - -void TRiga_documento::autosave(TSheet_field & f) -{ - const int num = numero() - 1; - - if (num >= 0 && num < f.items()) - { - TToken_string & row = f.row(num); - - put( "STATORIGA", row.get( f.cid2index(FR_STATORIGA) ) ); - put( "TIPORIGA", row.get( f.cid2index(FR_TIPORIGA )) ); - TString16 codmag(row.get(f.cid2index(FR_CODMAG))); - - codmag.left_just(3); - codmag << row.get( f.cid2index(FR_CODDEP )); - put( "CODMAG", codmag); - put( "CODART", row.get( f.cid2index(FR_CODART )) ); - put( "LIVELLO",row.get( f.cid2index(FR_LIV1 ))); // da modificare - put( "LIVELLO",row.get( f.cid2index(FR_LIV2 ))); - put( "LIVELLO",row.get( f.cid2index(FR_LIV3 ))); - put( "LIVELLO",row.get( f.cid2index(FR_LIV4 ))); - TString s(row.get(f.cid2index(FR_DESCR))); - int split_pos = s.find('\n'); - - const int descr_len = length("DESCR"); - if (split_pos < 0 && s.len() > descr_len) - split_pos = descr_len; - if (split_pos > 0) - { - put( "DESCR", s.left(split_pos)); - put("DESCLUNGA", "X"); - put("DESCEST", s.mid(split_pos)); - } - else - { - put("DESCR", s); - put("DESCLUNGA", ""); - zero("DESCEST"); - } - - put( "PREZZO", row.get( f.cid2index(FR_PREZZO )) ); - put( "UMQTA", row.get( f.cid2index(FR_UMQTA )) ); - TMask * m = ((TTipo_riga_documento &)tipo()).mask(); - const int pos = m->id2pos(FR_QTA); - if (pos >= 0 && m->fld(pos).field()->name() == "PSPESA") - put( "PSPESA", row.get( f.cid2index(FR_QTA )) ); - else - put( "QTA", row.get( f.cid2index(FR_QTA )) ); - put( "QTAEVASA", row.get( f.cid2index(FR_QTAEVASA )) ); - put( "RIGAEVASA", row.get( f.cid2index(FR_RIGAEVASA )) ); - put( "TARA", row.get( f.cid2index(FR_TARA )) ); - put( "PNETTO", row.get( f.cid2index(FR_PNETTO )) ); - put( "NCOLLI", row.get( f.cid2index(FR_NCOLLI )) ); - put( "DAEVADERE", row.get( f.cid2index(FR_DAEVADERE )) ); - put( "SCONTO", row.get( f.cid2index(FR_SCONTO )) ); - put( "PERCPROV", row.get( f.cid2index(FR_PERCPROV )) ); - put( "IMPFISSO", row.get( f.cid2index(FR_IMPFISSO )) ); - put( "CODIVA", row.get( f.cid2index(FR_CODIVA )) ); - put( "ADDIVA", row.get( f.cid2index(FR_ADDIVA )) ); - put( "ASPBENI", row.get( f.cid2index(FR_ASPBENI )) ); - } -} - -void TRiga_documento::autoload(TSheet_field & f) -{ - const int num = numero() - 1; - - TToken_string & row = f.row(num); - row.cut(0); - - row.add( get( "STATORIGA" ), f.cid2index(FR_STATORIGA )); - row.add( get( "TIPORIGA" ), f.cid2index(FR_TIPORIGA )); - const TString codmag(get("CODMAG")); - row.add( codmag.left(3), f.cid2index(FR_CODMAG )); - row.add( codmag.mid(3), f.cid2index(FR_CODDEP )); - row.add( get( "CODART" ), f.cid2index(FR_CODART )); - row.add( get( "LIVELLO" ), f.cid2index(FR_LIV1 )); // da modificare - row.add( get( "LIVELLO" ), f.cid2index(FR_LIV2 )); - row.add( get( "LIVELLO" ), f.cid2index(FR_LIV3 )); - row.add( get( "LIVELLO" ), f.cid2index(FR_LIV4 )); - TString s(get("DESCR")); - if (get_bool("DESCLUNGA")) - s << get("DESCEST"); - row.add(s, f.cid2index(FR_DESCR )); - row.add( get( "UMQTA" ), f.cid2index(FR_UMQTA )); - row.add( get( "PREZZO" ), f.cid2index(FR_PREZZO )); - TMask * m = ((TTipo_riga_documento &)tipo()).mask(); - const int pos = m->id2pos(FR_QTA); - if (pos >= 0 && m->fld(pos).field()->name() == "PSPESA") - row.add( get( "PSPESA" ), f.cid2index(FR_QTA )); - else - row.add( get( "QTA" ), f.cid2index(FR_QTA )); - row.add( get( "QTAEVASA" ), f.cid2index(FR_QTAEVASA )); - row.add( get( "RIGAEVASA" ), f.cid2index(FR_RIGAEVASA )); - row.add( get( "TARA" ), f.cid2index(FR_TARA )); - row.add( get( "PNETTO" ), f.cid2index(FR_PNETTO )); - row.add( get( "NCOLLI" ), f.cid2index(FR_NCOLLI )); - row.add( get( "DAEVADERE" ), f.cid2index(FR_DAEVADERE )); - row.add( get( "SCONTO" ), f.cid2index(FR_SCONTO )); - row.add( get( "PERCPROV" ), f.cid2index(FR_PERCPROV )); - row.add( get( "IMPFISSO" ), f.cid2index(FR_IMPFISSO )); - row.add( get( "CODIVA" ), f.cid2index(FR_CODIVA )); - row.add( get( "ADDIVA" ), f.cid2index(FR_ADDIVA )); - row.add( get( "ASPBENI" ), f.cid2index(FR_ASPBENI )); -} - -TDocumento_mask::TDocumento_mask(const char* name, TDocumento * doc, int num, - int max) - : TVariable_mask(name, num, max), _progs_page(-1), _last_prog(-1), - _doc(doc) -{ - const int pos = id2pos(BASE_PIEDE + 1); - - if (pos >= 0) - { - _progs_page = find_parent_page(fld(pos)); - _last_prog = 0; - while (id2pos(BASE_PIEDE + _last_prog + 1) >= 0) - _last_prog++; - } -} - -void TDocumento_mask::next_page(int p) -{ - TMask::next_page(p); - if (curr_page() == _progs_page) - { - begin_wait(); - autosave(get_relation()); - for (int i = _last_prog; i > 0; i--) - { - const short id = BASE_PIEDE + i; - const TString16 name(field(id).field()->name()); - - set(id , doc().get(name)); - } - end_wait(); - } -} - -bool TDocumento_mask::on_key(KEY key) -{ - if (key == K_SHIFT + K_F12) - { - field(F_STATO).enable(!field(F_STATO).enabled()); - return TRUE; - } - return TVariable_mask::on_key(key); -} - -/////////////////////////////////////////////////////////// -// Documento per vendite -/////////////////////////////////////////////////////////// - -TAssoc_array TDocumento::_tipi; - -TDocumento::TDocumento() - : TAuto_variable_rectype(LF_DOC), _rows(LF_RIGHEDOC, "NRIGA"), _nuovo(TRUE), - _condv(NULL), _rel(NULL), _sconto(NULL), _esenzione(NULL) -{ - set_memo_fld("G1"); -} - -TDocumento::TDocumento(char provv, int anno, const char* codnum, long numdoc, - TCond_vendita * condv, TRelation * rel) - : TAuto_variable_rectype(LF_DOC), _rows(LF_RIGHEDOC, "NRIGA"), _nuovo(TRUE), - _condv(condv), _rel(rel), _sconto(NULL), _esenzione(NULL) -{ - set_memo_fld("G1"); - if (numdoc <= 0) - { - numdoc = 0; - set_key(*this, provv, anno, codnum, numdoc); - - TRiga_documento* key = new TRiga_documento(this); - set_key(*key, provv, anno, codnum, numdoc); - _rows.set_key(key); // ok - } - else - read(provv, anno, codnum, numdoc); -} - -TDocumento::TDocumento(const TRectype& rec, TCond_vendita * condv, TRelation * rel) - : TAuto_variable_rectype(LF_DOC), _rows(LF_RIGHEDOC, "NRIGA"), _nuovo(FALSE), - _condv(condv), _rel(rel), _sconto(NULL), _esenzione(NULL) -{ - set_memo_fld("G1"); - read(rec); -} - -TDocumento::~TDocumento() - -{ - if (_sconto != NULL) delete _sconto; - if (_esenzione != NULL) delete _esenzione; -} - -// Funzione statica utile a tutti gli utenti di LF_DOC e LF_RIGHEDOC -void TDocumento::set_key(TRectype& rec, char provv, int anno, const char* codnum, long numdoc) -{ - CHECK(provv == 'D' || provv == 'P', "Provvisorio o Definitivo?"); - CHECKD(anno > 1900, "Anno non valido: ", anno); - CHECK(codnum && *codnum, "Codice numerazione nullo"); - CHECKD(numdoc >= 0, "Numero documento non valido ", numdoc); - - rec.put("PROVV", provv); - rec.put("ANNO", anno); - rec.put("CODNUM", codnum); - rec.put("NDOC", numdoc); -} - -// Funzione statica utile a tutti gli utenti di LF_DOC e LF_RIGHEDOC -void TDocumento::copy_data(TRectype& dst, const TRectype& src) -{ - // Memorizza tutti i campi chiave - const char provv = dst.get_char("PROVV"); - const int anno = dst.get_int("ANNO"); - const TString16 codnum = dst.get("CODNUM"); - const long numdoc = dst.get_long("NDOC"); - const int nriga = dst.num() == LF_RIGHEDOC ? dst.get_int("NRIGA") : 0; - // Copia tutto il record - dst = src; - // Ripristina tutti i campi chiave - set_key(dst, provv, anno, codnum, numdoc); - if (nriga > 0) - dst.put("NRIGA", nriga); -} - -void TDocumento::copy_contents(const TDocumento & src) -{ - copy_data(head(), src.head()); - destroy_rows(); - const int rows = src.physical_rows(); - - for (int i = 0; i < rows ; i++) - { - const TRiga_documento & s = src[i]; - TRiga_documento & r = new_row(s.tipo().codice()); - - copy_data(r, s); - } -} - -TRiga_documento& TDocumento::insert_row(int row, const char *tipo) -{ - TRiga_documento * r = new TRiga_documento((const TRiga_documento &) _rows.key(), this); // ok - r->set_numero(row); - if (tipo) - r->set_tipo(tipo); - _rows.insert_row(r); // ok - return *r; -} - -TRiga_documento& TDocumento::new_row(const char *tipo) -{ - TRiga_documento & r = (TRiga_documento&)_rows.row(-1, TRUE); // ok - if (tipo) - r.set_tipo(tipo); - return r; -} - -int TDocumento::read(const TRectype& rec) -{ - head() = rec; - TRiga_documento* key = new TRiga_documento(this); - const char pr = tipo_numerazione(); - const int an = anno(); - const TString16 cn = numerazione(); - const long nu = numero(); - CHECK(nu > 0, "Numero documento nullo."); - set_key(*key, pr, an, cn, nu); - - TLocalisamfile doc(LF_DOC); - int err = TRectype::read(doc); - if (err == NOERR) - { - _nuovo = FALSE; - _rows.read(key); //ok - } - else - { - _nuovo = TRUE; - head() = rec; - destroy_rows(); - _rows.set_key(key); // ok - } - set_riga_sconto(); - if (is_fattura()) - set_riga_esenzione(); - return err; -} - -int TDocumento::read(char provv, int anno, const char* codnum, long numdoc) -{ - TRectype rec(LF_DOC); - CHECK(numdoc > 0, "Numero documento nullo."); - set_key(rec, provv, anno, codnum, numdoc); - return read(rec); -} - -long TDocumento::renum(long numdoc) -{ - if (numdoc <= 0) - { - const char tn = tipo_numerazione(); - const int an = anno(); - const TString16 nu = numerazione(); - numdoc = get_next_key(tn, an, nu); - } - char num[16]; sprintf(num, "%ld", numdoc); - renum_key("NDOC", num); // Aggiorna testata - _rows.renum_key("NDOC", num); // Aggiorna righe ok - return numdoc; -} - -void TDocumento::set_riga_sconto() -{ - const TString80 sconto(get("SCONTOPERC")); - - if (sconto.empty()) - { - if(_sconto != NULL) - delete _sconto; - _sconto = NULL; - } - else - { - if (_sconto == NULL) - { - static TString _tipo_riga_sc; - if (_tipo_riga_sc.empty()) - { - TConfig conf(CONFIG_STUDIO); - - _tipo_riga_sc = conf.get("TRSCONTI", "ve"); - } - _sconto = new TRiga_documento(this, _tipo_riga_sc); - _sconto->put("DESCR","Sconto"); - } - _sconto->put("SCONTO", sconto); - } -} - -void TDocumento::set_riga_esenzione() -{ - TCliFor & c = condv().clifo(); - const char tipo = get_char("TIPOCF"); - const long cod = get_long("CODCF"); - const TString80 occ(get("OCFPI")); - c.load(tipo, cod, occ); - const TIVA codes(c.get(LF_CFVEN, CLI_ASSFIS)); - const TString16 v_esenzione(c.get(LF_CFVEN, CLI_VSPROT)); - const TString16 v_data_esenzione(c.get(LF_CFVEN, CLI_VSDATAREG)); -// const TString16 ufficio_IVA; ?? - const TString16 n_registrazione(c.get(LF_CFVEN, CLI_NSPROT)); - const TString16 n_data_registrazione(c.get(LF_CFVEN, CLI_NSDATAREG)); - bool esente = codes.tipo().not_empty() && v_esenzione.not_empty() && - v_data_esenzione.not_empty() && n_registrazione.not_empty() && - n_data_registrazione.not_empty(); - - if (esente) - { - esente = FALSE; - const TString16 codiva(codes.codice()); - for (int i = physical_rows(); !esente && i > 0; i--) - esente = codiva == row(i).get("CODIVA"); - } - - if (!esente) - { - if(_esenzione != NULL) - delete _esenzione; - _esenzione = NULL; - } - else - { - static TString _tipo_riga_es; - static real _bollo_es; - if (_tipo_riga_es.empty()) - { - TConfig conf(CONFIG_STUDIO); - - _tipo_riga_es = conf.get("TRESENZ", "ve"); - _bollo_es = (real)conf.get("BOLLIES", "ve"); - } - if (_esenzione == NULL) - _esenzione = new TRiga_documento(this, _tipo_riga_es); - TString d(256); d.format("Fattura non imponibile come da vostra dichiarazion"); - - _esenzione->put( "DESCR", d); - - /* rilasciata dall' ufficio IVA di %s*/ - d.format("e n. %s del %s da noi annotata al n. %s il %s. Marca da bollo da Lit. %s sull' originale.", - (const char *) v_esenzione, (const char *) v_data_esenzione, - /*(const char *) ufficio_IVA, */(const char *) n_registrazione, - (const char *) n_data_registrazione, _bollo_es.string(".")); - _esenzione->put("DESCLUNGA", "X"); - _esenzione->put("DESCEST", d); - } -} - -void TDocumento::dirty_fields() -{ - for (TDocumento_variable_field * f = (TDocumento_variable_field *) first_variable_field(); - f != NULL; f = (TDocumento_variable_field *) succ_variable_field()) - f->set_dirty(); - for (int i = rows(); i > 0; i--) - { - TRiga_documento & r = (TRiga_documento &) row(i); - - if (r.doc_dependent()) - r.dirty_fields(FALSE); - } -} - -int TDocumento::write(bool re) const -{ - const bool nuovo = _nuovo || numero() <= 0; // E' nuovo di zecca! - if (nuovo && re) // quindi ... - re = FALSE; // ... non fare la rewrite - - TLocalisamfile doc(LF_DOC); - int err = NOERR; - if (re) - { - err = _rows.write(re); - if (err == NOERR) - { - err = TRectype::rewrite(doc); - if (err != NOERR) - err = TRectype::write(doc); - } - } - else - { - if (nuovo) - { - TDocumento& myself = *(TDocumento*)this; - if (numero() <= 0) - myself.renum(); - do - { - err = TRectype::write(doc); - if (err == _isreinsert) - myself.renum(); - } while (err == _isreinsert); - myself._nuovo = FALSE; - } - else - { - err = TRectype::write(doc); - if (err != NOERR) - err = TRectype::rewrite(doc); - } - - if (err == NOERR) - err = _rows.write(re); - } - return err; -} - -int TDocumento::remove() const -{ - TLocalisamfile doc(LF_DOC); - int err = _rows.remove(); - if (err == NOERR) - err = TRectype::remove(doc); - return err; -} - -const bool TDocumento::in_valuta() const -{ - const TString& val = valuta(); - return (val.not_empty() && val != "LIT"); -} - -TRiga_documento & TDocumento::row(int index) -{ - const int nrows = _rows.rows(); - TRiga_documento * r = NULL; - - if (index <= nrows) - r = &((TRiga_documento &) _rows.row(index, FALSE)); - else - { - CHECKD((index == nrows + 1 && (_sconto != NULL || _esenzione != NULL)) || (index == nrows + 2 && _sconto != NULL && _esenzione != NULL), - "Riga documento non esistente ", index); - if (index == nrows + 1) - { - r = _sconto != NULL ? _sconto : _esenzione; - } - if (index == nrows + 2) - r = _esenzione; - } - return *r; -} - -long TDocumento::get_next_key(char provv, int anno, const char* codnum) const -{ - static long n = 0; - - if (n == 0) - { - TLocalisamfile doc(LF_DOC); - TRectype& curr = doc.curr(); - set_key(curr, provv, anno, codnum, 9999999L); - - const int err = doc.read(_isgreat); - - if (err != _isemptyfile) - { - if (err == NOERR) - doc.prev(); - if (curr.get_char("PROVV") == provv && - curr.get_int("ANNO") == anno && - curr.get("CODNUM") == codnum) - n = curr.get_long("NDOC"); - } - } - - n++; - return n; -} - -const TTipo_documento& TDocumento::tipo() const -{ - const TString16 tipodoc(get("TIPODOC")); - CHECK(tipodoc.not_empty(), "Tipo documento nullo"); - TTipo_documento * o = (TTipo_documento*)_tipi.objptr(tipodoc); - if (o == NULL) - { - o = new TTipo_documento(tipodoc); - _tipi.add(tipodoc, o); - } - return *o; -} - -bool TDocumento::raggruppabile(const TDocumento& doc, TToken_string& campi) const -{ - bool ok = raggruppabile() && doc.raggruppabile(); - if (ok) - { - TString campo; - for (const char* c = campi.get(0); c && ok; c = campi.get()) - { - campo = get(c); - ok &= campo == doc.get(c); - } - } - return ok; -} - -void TDocumento::set_fields(TAuto_variable_rectype & rec) -{ - if (tipo_valido()) - { - TTipo_documento & tipo_doc = (TTipo_documento &) tipo(); - const TString16 tot_doc(tipo_doc.totale_doc()); - - for (const TFormula_documento * f = tipo_doc.first_formula(); f; f = tipo_doc.succ_formula()) - { - TExpr_documento * exp = f->expr(); - if (tot_doc == f->name()) - { - TString work_tot_doc(tot_doc); - - work_tot_doc.insert("_"); - TString netto_def(exp->string()); - - if (netto_def.find("IMPONIBILI") == -1) - { - if (netto_def.not_empty()) - netto_def << "+"; - netto_def << "IMPONIBILI()"; - } - - if (netto_def.find("IMPOSTE") == -1) - { - if (netto_def.not_empty()) - netto_def << "+"; - netto_def << "IMPOSTE()"; - } - - TExpr_documento * netto_exp = new TExpr_documento(netto_def, _numexpr, this); - - add_field(new TDocumento_variable_field(work_tot_doc, netto_exp)); - - TExpr_documento * tot_exp = new TExpr_documento( - format("%s + _BOLLI(%s)", (const char *) work_tot_doc, - (const char *) work_tot_doc), _numexpr, this); - - add_field(new TDocumento_variable_field(f->name(), tot_exp)); - } - else - add_field(new TDocumento_variable_field(f->name(), exp)); - if (exp) - exp->set_doc(this); - } - } -} - -real TDocumento::imponibile(bool spese, int ndec) const -{ - real val; - if (ndec > 99) - ndec = in_valuta() ? 3 : 0; - - for (int i = rows(); i > 0; i--) - val += ((TRiga_documento &) ((TDocumento *)this)->row(i)).imponibile(); - if (spese) - { - real tot_doc = val + imposta(FALSE, ndec); - val += spese_incasso(tot_doc, ndec, _netto); - tot_doc += spese_incasso(tot_doc, ndec); - val += bolli(tot_doc, ndec, _netto); - } - val.round(ndec); - return val; -} - -real TDocumento::imposta(bool spese, int ndec) const -{ - TAssoc_array ive; - - if (ndec > 99) - ndec = in_valuta() ? 3 : 0; - - for (int i = rows(); i > 0; i--) - { - TRiga_documento & r = ((TRiga_documento &) ((TDocumento *)this)->row(i)); - real iva(r.imposta(FALSE)); - const TString & cod_iva = r.get("CODIVA"); - - real * tot = (real *) ive.objptr(cod_iva); - if (tot == NULL) - ive.add(cod_iva, iva); - else - *tot += iva; - } - real val; - - ive.restart(); - for (real * iva = (real *) ive.get(); iva != NULL; iva = (real *) ive.get()) - { - if (*iva < ZERO) - iva->floor(ndec); - else - iva->ceil(ndec); - val += *iva; - } - if (spese) - { - real tot_doc = val + imponibile(FALSE, ndec); - val += spese_incasso(tot_doc, ndec, _imposta); - tot_doc += spese_incasso(tot_doc, ndec); - val += bolli(tot_doc, ndec, _imposta); - } - val.round(ndec); - return val; -} - -real TDocumento::totale_doc() const -{ - const TString16 field(tipo().totale_doc()); - - if (field.not_empty()) - return get_real(field); - else - { - real r = imponibile() + imposta(); - r += spese_incasso(r, in_valuta() ? 3 : 0); - r += bolli(r, in_valuta() ? 3 : 0); - return r; - } -} - -real TDocumento::totale_netto() const -{ - const TString16 field(tipo().totale_netto()); - - if (field.not_empty()) - return get_real(field); - else - return imponibile() + imposta(); -} - -real TDocumento::basesconto() const -{ - const TString16 field(tipo().basesconto()); - - if (field.not_empty()) - return get_real(field); - else - return ZERO; -} - -real TDocumento::spese() const -{ - const TString16 field(tipo().spese()); - - if (field.not_empty()) - return get_real(field); - else - return ZERO; -} - -TPagamento & TDocumento::pagamento() -{ - const TString16 codpag(get("CODPAG")); - - if (codpag != _pag.code()) - { - _pag.set_code(codpag); - _pag.read(); - } - return _pag; -} - -void TDocumento::put_str(const char* fieldname, const char* val) -{ - TString v(val); - if (strcmp(fieldname, "TIPODOC") == 0 && TRectype::get("TIPODOC") != v) - { - TAuto_variable_rectype::put_str(fieldname, v); - reset_fields(*this); - set_fields(*this); - } - else - { - TAuto_variable_rectype::put_str(fieldname, v); - dirty_fields(); - if (strcmp(fieldname, "SCONTOPERC") == 0) - set_riga_sconto(); - } -} - -void TDocumento::zero(const char * fieldname) -{ - if (strcmp(fieldname, "TIPODOC") == 0) - reset_fields(*this); - TAuto_variable_rectype::zero(fieldname); - dirty_fields(); -} - -void TDocumento::zero(char c) -{ - reset_fields(*this); - TAuto_variable_rectype::zero(c); - destroy_rows(); -} - -TDocumento & TDocumento::operator =(const TDocumento & d) -{ - zero(); - operator=((TRectype &)d); - _rows = d._rows; - set_riga_sconto(); - if (is_fattura()) - set_riga_esenzione(); - set_relation(d._rel); - set_condv(d._condv); - return *this; -} - -TRectype & TDocumento::operator =(const TRectype & r) -{ - TRectype::operator=(r); - reset_fields(*this); - set_fields(*this); - return *this; -} - -TRectype & TDocumento::operator =(const char * r) -{ - TRectype::operator=(r); - reset_fields(*this); - set_fields(*this); - return *this; -} - -void TDocumento::update_spese_aut(TString_array & spese_aut, bool preserve_old, TSheet_field * sh) - -{ - const bool interactive = sh != NULL; - if (tipo().spese_aut()) - { - const int nrows = _rows.rows(); - for (int i = nrows; i > 0; i--) - { - TRiga_documento & r = row(i); - - if (r.tipo().tipo() == RIGA_SPESEDOC && r.generata()) - { - if (preserve_old) - return; - destroy_row(i, TRUE); - if (interactive) - sh->destroy(i - 1); - } - } - TString16 cod_iva_cli; - const int nspese = spese_aut.items(); - if (nspese > 0) - { - TLocalisamfile cfven(LF_CFVEN); - cfven.put("TIPOCF", get("TIPOCF")); - cfven.put("CODCF", get("CODCF")); - if (cfven.read() == NOERR) - cod_iva_cli = cfven.get("ASSFIS"); - for (i = 0; i < nspese; i++) - { - const TString & s = spese_aut.row(i); - - TSpesa_prest sp(s); - TString16 tipo(sp.tipo_riga()); - TRiga_documento & riga = new_row(tipo); - - riga.put("CODART", s); - riga.set_generata(); - riga.put("DESCR", sp.descrizione()); - switch (sp.tipo()) - { - case 'Q': - { - real qta = sp.qta(); - - if (qta == ZERO) - qta = 1.0; - riga.put("QTA", qta); - } - case 'V': - { - const real cambio = get_real("CAMBIO"); - const TString16 valuta = get("CODVAL"); - real prezzo = sp.prezzo(); - - sppr_calc(sp, valuta, cambio, prezzo); - riga.put("PREZZO", prezzo); - riga.put("UMQTA", sp.um()); - } - break; - case 'P': - default: - break; - } - if (cod_iva_cli.empty()) - riga.put("CODIVA", sp.cod_iva()); - else - riga.put("CODIVA", cod_iva_cli); - if (interactive) - { - const int nrow = sh->insert(-1, FALSE); - riga.autoload(*sh); - sh->check_row(nrow); - } - } - } - } -} - -/////////////////////////////////////////////////////////// -// Lista di documenti -/////////////////////////////////////////////////////////// - -TDate TLista_documenti::num2date(char provv, int anno, const char* codnum, long num) const -{ - TLocalisamfile doc(LF_DOC); - CHECK(num > 0, "Numero documento nullo."); - TDocumento::set_key(doc.curr(), provv, anno, codnum, num); - - if (doc.read(_isgteq) != NOERR) // In caso d'errore ... - doc.last(); // prendi l'ultimo - - return doc.get("DATADOC"); -} - -int TLista_documenti::read(char provv, char tipocf, long clifo, int anno, - TToken_string& tipidoc, TToken_string& statidoc, - const TDate& dd, const TDate& ad, - const char* codnum, long dn, long an) -{ - CHECK(provv == 'D' || provv == 'P', "Provvisorio o Definitivo?"); - CHECK(tipocf == 'C' || tipocf == 'F', "Il tipo deve essere Cliente o Fornitore"); - CHECKD(clifo > 0L, "Codice cliente non valido", clifo); - CHECKD(anno > 1900, "Anno non valido: ", anno); - CHECK(!tipidoc.empty_items(), "Lista dei tipi documento vuota"); - CHECK(!statidoc.empty_items(), "Lista degli stati documento vuota"); - - TRelation doc(LF_DOC); - TRectype start(LF_DOC), stop(LF_DOC); - - start.put("TIPOCF", tipocf); - stop.put("TIPOCF", tipocf); - - start.put("CODCF", clifo); - stop.put("CODCF", clifo); - - start.put("PROVV", provv); - stop.put("PROVV", provv); - - start.put("ANNO", anno); - stop.put("ANNO", anno); - - if (dn > 0) - { - start.put("DATADOC", num2date(provv, anno, codnum, dn)); - start.put("NDOC", dn); - } - else - { - if (dd.ok() && dd > botime) - start.put("DATADOC", dd); - } - - if (an > 0) - { - stop.put("DATADOC", num2date(provv, anno, codnum, an)); - stop.put("NDOC", an); - } - else - { - if (ad.ok() && ad < eotime) - stop.put("DATADOC", ad); - } - - TString filter(16); - if (codnum && *codnum) - { - bool numfilter = FALSE; - - if (start.get("DATADOC").empty()) - numfilter = TRUE; - else - start.put("CODNUM", codnum); - - if (stop.get("DATADOC").empty()) - numfilter = TRUE; - else - stop.put("CODNUM", codnum); - - if (numfilter) - filter << "CODNUM=\"" << codnum << '"'; - } - - TCursor cur(&doc, filter, 2, &start, &stop); - const TRectype& head = cur.curr(); - - _documenti.destroy(); - for (cur = 0; cur.ok(); ++cur) - { - const TString16 tipodoc = head.get("TIPODOC"); - const TString16 statodoc = head.get("STATO"); - if (tipidoc.get_pos(tipodoc) >= 0 && statidoc.get_pos(statodoc) >= 0) - { - TDocumento* d = new TDocumento(head); - _documenti.add(d); - } - } - - return _documenti.items(); -} - -int TLista_documenti::write(bool re) const -{ - int err = NOERR; - for (int i = 0; i < _documenti.items() && err == NOERR; i++) - err = doc(i).write(re); - return err; -} - -/////////////////////////////////////////////////////////// -// Cliente/Fornitore per vendite -/////////////////////////////////////////////////////////// - -void TLista_clifo::TClifo::init(const TRectype& rec, const TRectype& ven) -{ - _codice = rec.get_long(CLI_CODCF); - CHECK(_codice > 0, "Codice cliente nullo"); - - if (!ven.empty()) - { - _agente = ven.get_long(CLI_CODAG); - _zona = ven.get_long(CLI_CODZONA); - } - else - _agente = _zona = 0; -} - -bool TLista_clifo::TClifo::read(char tipo, long cod) -{ - TRelation clifo(LF_CLIFO); - clifo.add(LF_CFVEN, "TIPOCF=TIPOCF|CODCF=CODCF"); - - TRectype& curr = clifo.curr(); - curr.put(CLI_TIPOCF, tipo); - curr.put(CLI_CODCF, cod); - if (clifo.read() == NOERR) - init(curr, clifo.curr(LF_CFVEN)); - else - zero(); - - return ok(); -} - -TLista_clifo::TClifo::TClifo(const TRectype& rec) -{ - CHECK(rec.num() == LF_CLIFO, "Record non clienti"); - const char tipo = rec.get_char(CLI_TIPOCF); - const long codice = rec.get_long(CLI_CODCF); - read(tipo, codice); -} - -int TLista_clifo::leggi(long dc, long ac, long da, long aa, long dz, long az) -{ - TRelation clifo(LF_CLIFO); - clifo.add(LF_CFVEN, "TIPOCF=TIPOCF|CODCF=CODCF"); - - TRectype start(LF_CLIFO), stop(LF_CLIFO); - - start.put(CLI_TIPOCF, tipo()); - if (dc > 0) - start.put(CLI_CODCF, dc); - - stop.put(CLI_TIPOCF, tipo()); - if (ac > 0) - stop.put(CLI_CODCF, ac); - - TString filter(32); - if (da > 0) - filter << '(' << LF_CFVEN << "->" << CLI_CODAG << ">=" << da << ')'; - if (aa > 0) - { - if (filter.not_empty()) filter << "&&"; - filter << '(' << LF_CFVEN << "->" << CLI_CODAG << "<=" << aa << ')'; - } - if (dz > 0) - { - if (filter.not_empty()) filter << "&&"; - filter << '(' << LF_CFVEN << "->" << CLI_CODZONA << ">=" << dz << ')'; - } - if (az > 0) - { - if (filter.not_empty()) filter << "&&"; - filter << '(' << LF_CFVEN << "->" << CLI_CODZONA << "<=" << az << ')'; - } - - TCursor cur(&clifo, filter, 1, &start, &stop); - const TRectype& cli = cur.curr(); - const TRectype& ven = cur.curr(LF_CFVEN); - for (cur = 0; cur.ok(); ++cur) - { - TClifo* c = new TClifo(cli, ven); - _clifo.add(c); - } - - if (dc > 0 || ac > 0) ordina_per_codice(); else - if (da > 0 || aa > 0) ordina_per_agente(); else - if (dz > 0 || az > 0) ordina_per_zona(); - - return _clifo.items(); -} - -int TLista_clifo::sort_by_code(const TObject** o1, const TObject** o2) -{ - TLista_clifo::TClifo* c1 = (TLista_clifo::TClifo*)*o1; - TLista_clifo::TClifo* c2 = (TLista_clifo::TClifo*)*o2; - const long d = c1->codice() - c2->codice(); - return d == 0L ? 0 : (d > 0 ? +1 : -1); -} - -int TLista_clifo::sort_by_agent(const TObject** o1, const TObject** o2) -{ - TLista_clifo::TClifo* c1 = (TLista_clifo::TClifo*)*o1; - TLista_clifo::TClifo* c2 = (TLista_clifo::TClifo*)*o2; - const long d = c1->agente() - c2->agente(); - return d == 0L ? 0 : (d > 0 ? +1 : -1); -} - -int TLista_clifo::sort_by_zone(const TObject** o1, const TObject** o2) -{ - TLista_clifo::TClifo* c1 = (TLista_clifo::TClifo*)*o1; - TLista_clifo::TClifo* c2 = (TLista_clifo::TClifo*)*o2; - const long d = c1->zona() - c2->zona(); - return d == 0L ? 0 : (d > 0 ? +1 : -1); -} - -int TLista_clifo::ordina_per_codice() -{ - _clifo.sort(sort_by_code); - return _clifo.items(); -} - -int TLista_clifo::ordina_per_agente() -{ - _clifo.sort(sort_by_agent); - return _clifo.items(); -} - -int TLista_clifo::ordina_per_zona() -{ - _clifo.sort(sort_by_zone); - return _clifo.items(); -} - -int TLista_clifo::find(long cod) const -{ - for (int i = items()-1; i >= 0; i--) - if (clifo(i).codice() == cod) break; - return i; -} - -int TLista_clifo::add(long cod) -{ - int pos = find(cod); - if (pos < 0) - { - TClifo* c = new TClifo(tipo(), cod); - pos = _clifo.add(c); - } - return pos; -} - -/////////////////////////////////////////////////////////// -// TElaborazione -/////////////////////////////////////////////////////////// - -TElaborazione::TElaborazione(const char* cod) : TRectype(LF_TABCOM) -{ - settab("ELD"); - if (cod && *cod) - read(cod); -} - -int TElaborazione::read(const char* cod) -{ - CHECK(cod && *cod, "Codice elaborazione nullo"); - TTable eld("%ELD"); - put("CODTAB", cod); - const int err = TRectype::read(eld); - if (err != NOERR) - yesnofatal_box("Codice elaborazione non valido: %s", cod); - return err; -} - -/////////////////////////////////////////////////////////// -// TEsterna -/////////////////////////////////////////////////////////// - -TEsterna::TEsterna(const char* cod) - : TElaborazione(cod) -{ -} - -bool TEsterna::elabora(TLista_documenti& doc_in, TLista_documenti& doc_out, - const TDate& data_elab) -{ - CHECK(doc_in.items() == 1, "Si deve specificare uno e un solo documento in entrata"); - CHECK(doc_out.items() == 1, "Si deve specificare uno e un solo documento in uscita"); - TFilename name; name.temp("ext"); - { - TConfig c(name); - TDocumento & d = doc_in[0]; - int nfields = d.items(); - TString par; par.format("%d,0", LF_DOC); - - for (int i = 0; i < nfields; i++) - { - const TString16 fname(d.fieldname(i)); - TFieldref f(fname, LF_DOC); - - f.write(c, par, d.get(fname)); - } - for (TVariable_field * v = d.first_variable_field(); v ; v = d.succ_variable_field()) - c.set(v->name(), v->get(), par); - const int rows = d.physical_rows(); - if (rows > 0) - { - nfields = d[0].items(); - - for (int r = 0; r < rows; r++) - { - TRiga_documento row = d[r]; - - par.format("%d,%d", LF_RIGHEDOC, r + 1); - for (int i = 0; i < nfields; i++) - { - const TString16 fname(row.fieldname(i)); - TFieldref f(fname, LF_RIGHEDOC); - - f.write(c, par, row.get(fname)); - } - } - } - } - TString command_line(applicazione_esterna()); command_line << " -i " << name; - TExternal_app app(command_line); - if (app.run() == 0) - { - TConfig c(name); - TDocumento & d = doc_out[0]; - int nfields = d.items(); - TString par; par.format("%d,0", LF_DOC); - - for (int i = 0; i < nfields; i++) - { - const TString16 fname(d.fieldname(i)); - TFieldref f(fname, LF_DOC); - - d.put(fname, f.read(c, par)); - } - for (TVariable_field * v = d.first_variable_field(); v ; v = d.succ_variable_field()) - v->put(c.get(v->name(), par)); - - TString_array p; - - c.list_paragraphs(p); - nfields = d[0].items(); - d.destroy_rows(); - int r = 1 ; - par.format("%d,%d", LF_RIGHEDOC, r); - while (p.find(par) >= 0) - { - TRiga_documento row = d.new_row(); - for (int i = 0; i < nfields; i++) - { - const TString16 fname(row.fieldname(i)); - TFieldref f(fname, LF_RIGHEDOC); - - row.put(fname, f.read(c, par)); - } - r++; - par.format("%d,%d", LF_RIGHEDOC, r); - } - } - return TRUE; -} - -/////////////////////////////////////////////////////////// -// TConsegna ordini -/////////////////////////////////////////////////////////// - -TConsegna_ordini::TConsegna_ordini(const char* cod) - : TElaborazione(cod) -{ -} - -bool TConsegna_ordini::elabora(TLista_documenti& doc_in, TLista_documenti& doc_out, - const TDate& data_elab) -{ - return TRUE; -} - -/////////////////////////////////////////////////////////// -// TFatturazione bolle -/////////////////////////////////////////////////////////// - -TFatturazione_bolle::TFatturazione_bolle(const char* cod) - : TElaborazione(cod) -{ -} - -void TFatturazione_bolle::tipi_validi(TToken_string& tipi) const -{ - const TString& s2 = get("S2"); - tipi.cut(0); - TString16 t; - for (int i = 0; i < 5; i++) - { - t = s2.mid(i*4, 4); - t.trim(); - if (t.not_empty()) - tipi.add(t); - } - CHECK(!tipi.empty_items(), "Nessun tipo documento valido"); -} - -void TFatturazione_bolle::stati_validi(TToken_string& stati) const -{ - const TString& s7 = get("S7"); - stati.cut(0); - TString16 s; - for (int i = 0; i < 5; i++) - { - s = s7.mid(i*4, 1); - s.trim(); - if (s.not_empty()) - stati.add(s); - } - CHECK(!stati.empty_items(), "Nessuno stato documento valido"); -} - - -bool TFatturazione_bolle::raggruppa(TDocumento& doc_in, TDocumento& doc_out) -{ -#ifdef DBG - const TString tipi = get("S2"); - const TString& tipodoc = doc_in.tipo().codice(); - for (int i = 0; i < 5; i++) - { - if (tipodoc == tipi.mid(i*4, 4)) - break; - } - if (i >= 5) - { - NFCHECK("Tipo documento non valido: '%s'", (const char*)tipodoc); - return FALSE; - } -#endif - - const char stato_finale_in = get_char("S4"); - doc_in.stato(stato_finale_in); - - const TString& tipo_out = get("S8"); - doc_out.put("TIPODOC", tipo_out); - - const char stato_finale_out = get_char("S9"); - doc_out.stato(stato_finale_out); - - if (gestione_riferimenti()) - { - // Determina ed eventualmente crea la riga di riferimento - const int riga_rif = riferimenti_in_testa() ? 1 : doc_out.rows()+1; - if (riga_rif > doc_out.rows()) - { - TRiga_documento& rout = doc_out.new_row(); - rout.forza_sola_descrizione(); - } - - TRiga_documento& rout = doc_out[riga_rif]; - - // Costruisce la stringa di riferimento - TString riferimento(80); - riferimento = doc_in.tipo().riferimento(); - if (riferimento.empty()) - riferimento = doc_in.tipo().descrizione(); - riferimento << " n. " << doc_in.numero(); - riferimento << " del " << doc_in.data().string(); - - // Setta la descrizione se vuota - if (rout.get("DESCR").empty()) - rout.put("DESCR", riferimento); - else - { - // Altrimenti aggiungi il riferimento al memo - TString memo(1024); - memo = rout.get("DESCEST"); - if (memo.empty()) - rout.put("DESCLUNGA", "X"); - else - memo << '\n'; - memo << riferimento; - rout.put("DESCEST", memo); - } - } - - const bool ignora_desc = ignora_descrizioni(); - - TToken_string campi_riga(80); - const bool ragg_rig = raggruppa_righe(); - if (ragg_rig) - { - campi_riga = "CODART|UMQTA"; // Uguali sempre - // Uguali opzionalmente - if (riga_uguale(0)) campi_riga.add("CODMAG"); - if (riga_uguale(1)) campi_riga.add("CODIVA"); - if (riga_uguale(2)) campi_riga.add("PREZZO|SCONTO"); - } - - for (int r = 1; r <= doc_in.rows(); r++) - { - const TRiga_documento& rin = doc_in[r]; - const bool rindesc = rin.sola_descrizione(); // La riga di input e' descrittiva - if (ignora_desc && rindesc) - continue; - - bool elaborata = FALSE; - - // Raggruppo le righe se e' settato il falg di raggruppamento e - // se la riga non contiene solo una descrizione - if (ragg_rig && !rindesc) // Se devo raggruppare le righe ... - { - const int last = doc_out.rows(); - for (int o = 1; o <= last; o++) // ... cerca una riga compatibile - { - TRiga_documento& rout = doc_out[o]; - if (rout.sola_descrizione()) // Ignora le righe descrittive - continue; - if (rin.raggruppabile(rout, campi_riga)) // Se esiste una riga compatibile ... - { - rout += rin; // ... sommaci la quantita' ecc. - elaborata = TRUE; // Ricorda di averla gia' elaborata - break; - } - } - } - if (!elaborata) // Se la riga non e' stata gia' sommata ... - { - TRiga_documento& rout = doc_out.new_row(); // ... crea una riga nuova e ... - doc_out.copy_data(rout, rin); // ... copiaci tutti i campi della riga sorgente. - } - } - - return TRUE; -} - -bool TFatturazione_bolle::elabora(TLista_documenti& doc_in, TLista_documenti& doc_out, - const TDate& data_elab) -{ - bool ok = TRUE; - - TToken_string campi_doc(128); // Lista di campi che devono essere uguali - campi_doc = "TIPOCF|CODCF|CODVAL|CODLIN"; // Uguali sempre - - // Uguali opzionalmente - const char* cond[] = { "CAMBIO", "SCONTO", "TIPODOC", "CODNUM", - "CODPAG", "CODABIA|CODCABA", "CODLIST", "CODAG", - "CODSPMEZZO", "CODPORTO", "CAUSTRASP", "CODVETT1|CODVETT2|CODVETT3", - NULL }; - for (int u = 0; cond[u]; u++) - if (doc_uguale(u)) campi_doc.add(cond[u]); - - for (int id = 0; id < doc_in.items() && ok; id++) - { - TDocumento& campione = doc_in[id]; - const int tot = doc_out.items(); - int od = tot; - - if (campione.raggruppabile()) // Se il documento ha il flag di raggruppabilita' ... - { - for (od = 0; od < tot; od++) // ... cerca un documento compatibile. - { - if (campione.raggruppabile(doc_out[od], campi_doc)) - break; - } - } - if (od >= tot) // Se non ho trovato un documento compatibile ... - { // ... creane uno nuovo (certamente compatibile) - const char provv = tipo_numerazione(); - const int anno = campione.anno(); - const TString codnum = codice_numerazione_finale(); - TDocumento* new_doc = new TDocumento(provv, anno, codnum, -1); - // Copia i dati della testata - TDocumento::copy_data(new_doc->head(), campione.head()); - - new_doc->put("DATADOC", data_elab); - - // Aggiungilo alla lista dei documenti in uscita - od = doc_out.add(new_doc); - } - - ok = raggruppa(campione, doc_out[od]); - } - return ok; -} - -/////////////////////////////////////////////////////////// -// TContabilizzazione -/////////////////////////////////////////////////////////// - -TContabilizzazione::TContabilizzazione(const char* cod) - : TElaborazione(cod) -{ -} - -bool TContabilizzazione::elabora(TLista_documenti& doc_in, TLista_documenti& doc_out, - const TDate& data_elab) -{ - CHECK(doc_in.items() == 1, "Si deve specificare uno e un solo documento in entrata"); - CHECK(doc_out.items() == 1, "Si deve specificare uno e un solo documento in uscita"); - return TRUE; -} -/////////////////////////////////////////////////////////// -// TCopia_documento -/////////////////////////////////////////////////////////// - -TCopia_documento::TCopia_documento(const char* cod) - : TElaborazione(cod) -{ -} - -bool TCopia_documento::elabora(TLista_documenti& doc_in, TLista_documenti& doc_out, - const TDate& data_elab) -{ - CHECK(doc_in.items() == 1, "Si deve specificare uno e un solo documento in entrata"); - CHECK(doc_out.items() == 1, "Si deve specificare uno e un solo documento in uscita"); - TDocumento & doc_src = doc_in[0]; - TDocumento & doc_dest = doc_out[0]; - - doc_dest.copy_contents(doc_src); - return TRUE; -} - - -/////////////////////////////////////////////////////////// -// TGenerazione_effetti -/////////////////////////////////////////////////////////// - -TGenerazione_effetti::TGenerazione_effetti(const char* cod) - : TElaborazione(cod) -{ -} - -bool TGenerazione_effetti::elabora(TLista_documenti& doc_in, TLista_documenti& doc_out, - const TDate& data_elab) -{ - CHECK(doc_in.items() == 1, "Si deve specificare uno e un solo documento in entrata"); - CHECK(doc_out.items() == 1, "Si deve specificare uno e un solo documento in uscita"); - return TRUE; -} - -/////////////////////////////////////////////////////////// -// TLista_elaborazioni -/////////////////////////////////////////////////////////// - -void TLista_elaborazioni::read() -{ - if (_elab == NULL) - { - _elab = new TAssoc_array(); - - TTable eld("%ELD"); - - for (int err = eld.first(); err == NOERR; err = eld.next()) - { - TElaborazione * el = NULL; - switch (eld.curr().get_int("I0")) - { - case _esterna : - el = new TEsterna(eld.curr()); - break; - case _consegna_ordini: - el = new TConsegna_ordini(eld.curr()); - break; - case _fatturazione_bolle : - el = new TFatturazione_bolle(eld.curr()); - break; - case _contabilizzazione : - el = new TContabilizzazione(eld.curr()); - break; - case _copia_documento : - el = new TCopia_documento(eld.curr()); - break; - case _generazione_effetti : - el = new TGenerazione_effetti(eld.curr()); - break; - default : - break; - } - _elab->add(el->codice(), el); - } - } -} - -int TLista_elaborazioni::select(TString_array & result, bool interattivo, bool insert_mode, const char * tipo_iniziale, const char * stato_iniziale, const char * tipo_finale, const char * stato_finale) -{ - read(); - _elab->restart(); - result.destroy(); - for (TElaborazione * el = (TElaborazione *)_elab->get(); el ; el = (TElaborazione *) _elab->get()) - { - bool ok = TRUE; - if (tipo_iniziale && stato_iniziale) - { - bool found = FALSE; - for (int i = 0; !found && i < 5; i++) - found = el->tipo_iniziale(i) == tipo_iniziale && el->stato_iniziale(i) == *stato_iniziale; - ok = found; - } - ok &= (tipo_finale && stato_finale && el->tipo_finale() == tipo_finale && el->stato_finale() == *stato_finale); - if (ok && (!interattivo || interattivo == el->interattivo()) && - (!insert_mode || insert_mode == el->insert_mode())) - result.add(el->codice()); - } - return result.items(); -} - -TElaborazione & TLista_elaborazioni::operator [](const char * key) const -{ - ((TLista_elaborazioni *)this)->read(); - return (TElaborazione &) (*_elab)[key]; -} - -void TLista_elaborazioni::update() -{ - delete _elab; _elab = NULL; - read(); - -} - -TLista_elaborazioni::~TLista_elaborazioni() -{ - if (_elab) - delete _elab; -} diff --git a/ve/velib02.cpp b/ve/velib02.cpp index 37e28f3c6..cc6f25673 100755 --- a/ve/velib02.cpp +++ b/ve/velib02.cpp @@ -1,344 +1,640 @@ -#include "velib02.h" +#include "velib.h" +#include "vepriv.h" -///////////////////////////////////////////////////////////// -// TCodice_numerazione -///////////////////////////////////////////////////////////// -TCodice_numerazione::TCodice_numerazione(const char* codnum) - : TRectype(LF_TABCOM), _status(NOERR) +#ifndef __APPLICAT_H +#include +#endif + +#ifndef __VE0100C_H +#include "ve0100c.h" +#endif + +#ifndef __VERIG_H +#include "verig.h" +#endif + +#ifndef __SCONTI_H +#include "sconti.h" +#endif + +/////////////////////////////////////////////////////////// +// Tipo riga di un documento +/////////////////////////////////////////////////////////// + +TAssoc_array TTipo_riga_documento::_formule_riga; + +TTipo_riga_documento::TTipo_riga_documento(const char* tiporig) + : TRectype(LF_TABCOM), _mask(NULL) { - settab("NUM"); - if (codnum && *codnum) - _status = read(codnum); + settab("TRI"); + _name = "verig"; + _name << codice(); + if (tiporig && *tiporig) + read(tiporig); } -TCodice_numerazione::TCodice_numerazione(const TRectype& rec) - : TRectype(rec), _status(NOERR) -{ } - -TCodice_numerazione::~TCodice_numerazione() -{ -} - -const char * TCodice_numerazione::complete_num(long num) +TTipo_riga_documento::TTipo_riga_documento(const TRectype& rec) + : TRectype(rec), _mask(NULL) { - static TString codnum; - codnum = prefisso(); - codnum << num; - codnum << postfisso(); - return codnum; + _name = "verig"; + _name << codice(); + read_formule(); } -int TCodice_numerazione::read(const char* codnum) +TTipo_riga_documento::~TTipo_riga_documento() +{ + if (_mask) delete _mask; +} + +int TTipo_riga_documento::read(const char* tiporig) { - TTable t("%NUM"); - put("CODTAB", codnum); + TTable t("%TRI"); + put("CODTAB", tiporig); int err = TRectype::read(t); - if (err != NOERR) - yesnofatal_box("Codice numerazione errato: %s", codnum); + if (err == NOERR) + read_formule(); + else + yesnofatal_box("Tipo riga documento errato: %s", tiporig); return err; } -///////////////////////////////////////////////////////////// -// TRiepilogo IVA -///////////////////////////////////////////////////////////// -TRiepilogo_Iva& TRiepilogo_Iva::operator=(TRiepilogo_Iva& a) +void TTipo_riga_documento::read_formule() + +{ + TFilename prof(profile_name()); + + prof.ext("ini"); + + TConfig profile(prof); + + _formule = profile.get("CAMPICALC", "MAIN"); + _formule.add(profile.get("CALCOLI", "MAIN")); + _imponibile = profile.get("IMPONIBILE", "MAIN"); + if (_imponibile.empty()) + { + _imponibile = "IMPONIBILE"; + if (_formule.find(_imponibile) < 0) + _formule.add("IMPONIBILE=PREZZO()"); + } + if (_imponibile.not_empty() && _formule.find(_imponibile) < 0) + { + error_box("Campo imponibile (%s) sconosciuto nel tipo riga %s", (const char *) _imponibile, (const char *) codice()); + _imponibile.cut(0); + } +} + +TFormula_documento * TTipo_riga_documento::succ_formula(bool restart) +{ + if (restart) + _formule.restart(); + const TString formula(_formule.get()); + if (formula.not_empty()) + { + char *expr = NULL; + const int p = formula.find("="); + if (p > 0) + { + expr = (char *) (const char *) formula + p; + *expr = '\0'; expr++; + } + TFormula_documento * o = (TFormula_documento*)_formule_riga.objptr(formula); + if (o == NULL) + { + o = new TFormula_documento(_riga, formula, expr); + _formule_riga.add(formula, o); + } + return o; + } + else + return NULL; +} + +TVariable_mask * TTipo_riga_documento::mask() { - _imp = a.imp(); _iva = a.iva(); _ali = a.ali(); - _cod = a.cod(); _tipo = a.tipo(); _des = a.des(); + if (mask_loaded()) + return _mask; + _mask = new TVariable_mask(mask_name()); + TFilename proname(profile_name()); + + proname.ext( "ini" ); + + TConfig pro( proname ); + int numhandler = pro.get_int( "NHANDLER", "HANDLERS" ); + + for( int i = 1; i <= numhandler; i ++ ) + { + TString chiave; + chiave.format( "%d", i ); + TToken_string riga = pro.get( chiave, "HANDLERS" ); + row_set_handler( *_mask, riga.get_int( 0 ), riga.get_int( 1 ) ); + } + + const int pos = _mask->id2pos(FR_CODART); + + if (pos >= 0) + { + const TMask_field & f = _mask->field(FR_CODART); + if (f.is_edit()) + { + TBrowse * browse = ((TEdit_field &) f).browse(); + const char tipo_r = tipo(); + + if (browse ) + { + const TCursor * cur = browse->cursor(); + if (cur) + { + const int num = cur->file().num(); + if (num == LF_ANAMAG) + { + _mask->set_handler( FR_CODART, codart_handler ); + _mask->set_handler( FR_LIV1, liv_handler ); + _mask->set_handler( FR_LIV2, liv_handler ); + _mask->set_handler( FR_LIV3, liv_handler ); + _mask->set_handler( FR_UMQTA, umart_handler ); + _mask->set_handler( FR_DESCR, descr_handler ); + _mask->set_handler( FR_QTA, qta_handler ); + } + else + if (tipo_r == RIGA_SPESEDOC || tipo_r == RIGA_PRESTAZIONI) + _mask->set_handler( FR_CODART, sppr_handler ); + } + } + } + } + const int posiva = _mask->id2pos(FR_CODIVA); + + if (posiva >= 0) + _mask->set_handler( FR_CODIVA, iva_handler ); + + return _mask; +} + +/////////////////////////////////////////////////////////// +// Riga documento per vendite +/////////////////////////////////////////////////////////// + +TAssoc_array TRiga_documento::_tipi; +TAssoc_array TRiga_documento::_spese; +TAssoc_array TRiga_documento::_ive; + +TRiga_documento::TRiga_documento(TDocumento* doc, const char * tipo) + : TAuto_variable_rectype(LF_RIGHEDOC), _doc(doc), _iva_calc(FALSE) +{ + if (tipo) + set_tipo(tipo); +} + +TRiga_documento::TRiga_documento(const TRiga_documento& rec, TDocumento* doc, + const char * tipo) + : TAuto_variable_rectype(rec), _doc(doc), _iva_calc(FALSE) +{ + if (tipo) + set_tipo(tipo); +} + +const TTipo_riga_documento& TRiga_documento::tipo() const +{ + const TString16 tiporig(get("TIPORIGA")); + CHECK(tiporig.not_empty(), "Tipo riga documento nullo"); + TTipo_riga_documento* o = (TTipo_riga_documento*)_tipi.objptr(tiporig); + if (o == NULL) + { + if (_tipi.items() == 0) + { + TTable tri("%TRI"); // Tabella dei tipi riga + for (tri.first(); !tri.eof(); tri.next()) + { + const TString16 codice = tri.get("CODTAB"); + _tipi.add(codice, new TTipo_riga_documento(tri.curr())); + } + } + o = (TTipo_riga_documento*)_tipi.objptr(tiporig); + if (o == NULL) + { + o = new TTipo_riga_documento(tiporig); + _tipi.add(tiporig, o); + } + } + return *o; +} + +const TSpesa_prest & TRiga_documento::spesa() const +{ + const char tipor = tipo().tipo(); + + CHECK(tipor == RIGA_SPESEDOC || tipor == RIGA_PRESTAZIONI, "Tipo riga incompatibile con le spese"); + static long firm = -1; + long new_firm = main_app().get_firm(); + + if (firm != new_firm) + { + _spese.destroy(); + firm = new_firm; + } + + const TString16 codice(get("CODART")); + TString16 index; index << tipor << codice; + + TSpesa_prest * s = (TSpesa_prest *) _spese.objptr(index); + if (s == NULL) + { + s = new TSpesa_prest(codice, tipor); + _spese.add(index, s); + } + return *s; +} + +const TIVA & TRiga_documento::iva(const char *codice) +{ + TIVA * v = (TIVA *) _ive.objptr(codice); + if (v == NULL) + { + v = new TIVA(codice); + _ive.add(codice, v); + } + return *v; +} + +bool TRiga_documento::sola_descrizione() const +{ + char t = tipo().tipo(); + if (t <= ' ' && get("QTA").empty() && get("PREZZO").empty()) + t = RIGA_DESCRIZIONI; + return t == RIGA_DESCRIZIONI; +} + +void TRiga_documento::forza_sola_descrizione() +{ + // In realta' il test serve anche a caricare la lista dei tipi riga! + if (!tipo_valido() || tipo().tipo() != RIGA_DESCRIZIONI) + { + _tipi.restart(); + for (const TObject* o = _tipi.get(); o; o = _tipi.get()) + { + const TTipo_riga_documento* trd = (const TTipo_riga_documento*)o; + if (trd->tipo() == RIGA_DESCRIZIONI) + { + put("TIPORIGA", trd->codice()); + break; + } + } + zero("QTA"); + zero("PREZZO"); + } +} + +TRectype & TRiga_documento::operator =(const TRectype & r) +{ + TRectype::operator=(r); + reset_fields(*this); + set_fields(*this); + return *this; +} + +TRectype & TRiga_documento::operator =(const char * r) +{ + TRectype::operator=(r); + reset_fields(*this); + set_fields(*this); + return *this; +} + +// Ritorna TRUE se le due righe del documento possono essere sommate +bool TRiga_documento::raggruppabile(const TRiga_documento& r, TToken_string& campi) const +{ + bool ok = TRUE; + TString campo; + for (const char* c = campi.get(0); c && ok; c = campi.get()) + { + campo = get(c); // Separare le due get! + ok &= campo == r.get(c); + } + return ok; +} + +TRiga_documento& TRiga_documento::operator +=(const TRiga_documento& r) +{ + TToken_string campi("QTA|NCOLLI|QTAEVASA"); + for (const char* c = campi.get(0); c && ok; c = campi.get()) + { + real num(r.get_real(c)); + if (!num.is_zero()) + { + num += get_real(c); + put(c, num); + } + } + if (!get_bool("RIGAEVASA")) + { + const real qta = get_real("QTA"); + const real qtaeva = get_real("QTAEVASA"); + if (qtaeva >= qta) + put("RIGAEVASA", "X"); + } + return *this; } -///////////////////////////////////////////////////////////// -//TDocumentoEsteso -///////////////////////////////////////////////////////////// -void TDocumentoEsteso::compile_summary() -{ - _sum_filter = 0; - const int items = rows(); - - _summary_table.destroy(); - _summary_array.destroy(); - _imposte = 0.0; - _importi_netti = 0.0; - const bool val = in_valuta(); - // Scorre tutte le righe e compila la tabellina _summary_table, per codice iva, - // nonche' il galactic_array _summary_array dove le informazioni sono registrate per singola riga - // cio' ad usi generici: porco diavolo! - for (int i = 1; i <= items; i++) - { - TRiga_documento& r = row(i); - int nriga = r.get_int("NRIGA"); - real price = r.get_real("PREZZO"); - real qta = r.get_real("QTA"); - real aliquota, sc, imponibile, iva; - TString sconto(r.get("SCONTO")); - TString codiva(r.get("CODIVA")); - _iva->put("CODTAB", codiva); - if (_iva->read() != NOERR) continue; // Se non trova il codice salta questa riga - - aliquota = _iva->get_real("R0"); - price = prezzo_scontato(price, sconto); - price.round(val ? _parm.pri_val : _parm.pri_lit); // prezzo scontato - qta.round(val ? _parm.qta_val : _parm.qta_lit); - imponibile = price * qta; - imponibile.round (val? _parm.imp_val : _parm.imp_lit); // imponibile di riga - iva = r.iva(val ? _parm.imp_val : _parm.imp_lit); - //iva = (imponibile * aliquota) / 100.0; - //iva.ceil(val ? _parm.imp_val : _parm.imp_lit); // imposta calcolata - - // Aggiorna o aggiunge l'elemento se non esiste - TRiepilogo_Iva riepilogo_tmp,row_riep; - const bool exists = _summary_table.is_key(codiva); - TRiepilogo_Iva& riepilogo = (exists ? (TRiepilogo_Iva&)_summary_table[codiva] : riepilogo_tmp); - // Aggiorna anche il totale importi netti ed il totale imposte - _importi_netti += imponibile; - _imposte += iva; - riepilogo.imp() += imponibile; riepilogo.iva() += iva; - row_riep.imp() = imponibile; row_riep.iva() = iva; - riepilogo.ali() = aliquota; riepilogo.cod() = codiva; - row_riep.ali() = aliquota; row_riep.cod() = codiva; - TString16 tipo(_iva->get("S1")); - int tipo_i = 1;// Regime IVA normale - if (tipo == "VE") tipo = 2; - else if (tipo == "ES") tipo = 4; - else if (tipo == "NI") tipo = 8; - else if (tipo == "NS") tipo= 16; - riepilogo.tipo() = tipo_i; - row_riep.tipo() = tipo_i; - if (riepilogo.tipo() != 1) // Se non e' regime normale salva anche la descrizione - { - TString s(_iva->get("S0")); - riepilogo.des() = s; - row_riep.des() = s; - } - _summary_table.add(codiva,riepilogo,exists); - _summary_array.add(row_riep,nriga); - } - // Inizializza l'array di ordine - for (i = 0; i<32;i++) - { - TToken_string s; - _order_array.add(s); - } -} - -void TDocumentoEsteso::summary_filter(byte selector) -{ - if (_sum_filter == -1) compile_summary(); // Crea la tabella se deve ancora farlo - // se ha selezionato una riga in precedenza deve finire di stamparla - // ovvero non seleziona il filtro fino a quando non ha ricevuto una summary_set_next() - if (_sum_selected) return; - // - // Procedimento: - // Memorizza in un TString_array tante TToken_string quanti sono i filtri possibili - // (al massimo 31 [1+2+4+8+16]). Ogni TToken_string contiene i codici IVA - // delle righe di TRiepilogo_Iva che soddisfano la condizione di filtro - _sum_selected = TRUE; - _sum_filter = selector; - TToken_string& codici = _order_array.row(_sum_filter-1); - if (codici.items() == 0) // Se non c'e' nemmeno un codice IVA allora deve effettuare il filtro - { // ovvero mette in <> tutti i codici IVA che soddisfano tale filtro - // sara' poi la summary_set_next() a selezionare sequenzialmente il giusto codice a seconda del filtro corrente - - // Scorre sequenzialmente la tabella _summary_table e compone la TToken_string con i codici IVA - const int items = _summary_table.items(); - TRiepilogo_Iva* curr = (TRiepilogo_Iva *) _summary_table.first_item(); - for (int i = 0; i < items && curr != NULL; i++) - { - if (curr->tipo() & _sum_filter) // se fa parte del filtro selezionato schiaffa il codice nella TToken_string - codici.add(curr->cod()); - curr = (TRiepilogo_Iva*) _summary_table.succ_item(); - } - codici.restart(); - summary_set_next(); // setta l'elemento corrente - } -} - -void TDocumentoEsteso::summary_reset(bool force) -{ - const int items = _order_array.items(); - if (force) _sum_filter = -1; - for (int i = 0; i 0) _scadenze_current++; -} - -const char * TDocumentoEsteso::scadenze_get(const TString& w) -{ - TString ret; - - if (_scadenze_current == -1) - // calcola le scadenze e le mette in _scadenze_array - scadenze_recalc(); - if (_scadenze_current > -1 && _scadenze_current < _scadenze_array.items()) - { - if (w == "DATA") ret = _scadenze_array.row(_scadenze_current).get(0); // ritorna la data di scadenza - if (w == "IMPORTO") ret = _scadenze_array.row(_scadenze_current).get(1); // ritorna l'importo in scadenza - } - return (const char*)ret; -} - -void TDocumentoEsteso::scadenze_set_next() -{ - if (_scadenze_current < _scadenze_array.items() && _scadenze_current >= 0) - _scadenze_current++; -} - -void TDocumentoEsteso::scadenze_reset() -{ - if (_scadenze_current > 0) - _scadenze_current = 0; -} - -real& TDocumentoEsteso::tot_importi_netti() -{ - if (!summary_compiled()) compile_summary(); - return _importi_netti; -} - -real& TDocumentoEsteso::tot_imposte() -{ - if (!summary_compiled()) compile_summary(); - return _imposte; -} - -real TDocumentoEsteso::tot_spese() -{ - TString16 t("TOTSP"); - real number(get_head_info(t)); - return number; -} - -real TDocumentoEsteso::tot_documento() -{ - if (!summary_compiled()) compile_summary(); - real number = _imposte + _importi_netti; - return number; -} - -real TDocumentoEsteso::tot_imponibili(byte selector) -{ - if (!summary_compiled()) compile_summary(); - - real number = 0.0; - const int items = _summary_table.items(); - TRiepilogo_Iva* curr = (TRiepilogo_Iva *) _summary_table.first_item(); - for (int i = 0; i < items && curr != NULL; i++) - { - if (curr->tipo() & selector) // se fa parte del filtro selezionato schiaffa il codice nella TToken_string - number += curr->imp(); - curr = (TRiepilogo_Iva*) _summary_table.succ_item(); - } - return number; -} - - -const char* TDocumentoEsteso::get_head_info(const TString & what) -{ - TToken_string memo(head().get("G1"),'\n'); // prende il campo memo con i totalizzatori. Un totalizzatore per riga nella forma = - TString rt; - const int items = memo.items(); - for (int i = 0; i_iva_calc = TRUE; + real iva(::iva(imponibile(), iva(), ndec)); + ((TRiga_documento *) this)->_iva_calc = FALSE; + return iva; } -TDocumentoEsteso::~TDocumentoEsteso() +real TRiga_documento::imponibile() const { - if (_iva != NULL) delete _iva; + const TString16 field(tipo().imponibile()); + + if (field.not_empty()) + { + if (tipo().tipo() == RIGA_OMAGGI && _iva_calc) + { + TDocumento_variable_field * f = (TDocumento_variable_field *) variable_field(field); + CHECKS(f, "Field UNKNOWN : ", field); + f->set_dirty(TRUE); + const real r = get_real(field); + f->set_dirty(TRUE); + return r; + } + else + return get_real(field); + } + else + return importo(TRUE, FALSE, doc().in_valuta() ? 3 : 0); } +real TRiga_documento::imposta(bool round) const +{ + return iva(round ? (doc().in_valuta() ? 3 : 0) : 20); +} + +void TRiga_documento::dirty_fields(bool dirty_document) +{ + for (TDocumento_variable_field * f = (TDocumento_variable_field *) first_variable_field(); + f != NULL; f = (TDocumento_variable_field *) succ_variable_field()) + f->set_dirty(); + if (dirty_document) + ((TDocumento &)doc()).dirty_fields(); +} + +bool TRiga_documento::doc_dependent() const + +{ + if (tipo_valido()) + { + const char tipor = tipo().tipo(); + + if (tipor == RIGA_SPESEDOC) + return spesa().tipo() == 'P'; + else + if (tipor == RIGA_SCONTI) + return get("SCONTO").not_empty(); + } + return FALSE; +} + +void TRiga_documento::put_str(const char* fieldname, const char* val) +{ + TString v(val); + if (strcmp(fieldname, "TIPORIGA") == 0 && TRectype::get("TIPORIGA") != v) + { + TAuto_variable_rectype::put_str(fieldname, v); + reset_fields(*this); + set_fields(*this); + } + else + { + TAuto_variable_rectype::put_str(fieldname, v); + dirty_fields(); + } +} + +void TRiga_documento::zero(const char * fieldname) +{ + if (strcmp(fieldname, "TIPORIGA") == 0) + reset_fields(*this); + TAuto_variable_rectype::zero(fieldname); + dirty_fields(); +} + +void TRiga_documento::zero(char c) +{ + reset_fields(*this); + TAuto_variable_rectype::zero(c); +} + +void TRiga_documento::autosave(TSheet_field & f) +{ + const int num = numero() - 1; + + if (num >= 0 && num < f.items()) + { + TToken_string & row = f.row(num); + + put( "STATORIGA", row.get( f.cid2index(FR_STATORIGA) ) ); + put( "TIPORIGA", row.get( f.cid2index(FR_TIPORIGA )) ); + TString16 codmag(row.get(f.cid2index(FR_CODMAG))); + + codmag.left_just(3); + codmag << row.get( f.cid2index(FR_CODDEP )); + put( "CODMAG", codmag); + put( "CODART", row.get( f.cid2index(FR_CODART )) ); + put( "LIVELLO",row.get( f.cid2index(FR_LIV1 ))); // da modificare + put( "LIVELLO",row.get( f.cid2index(FR_LIV2 ))); + put( "LIVELLO",row.get( f.cid2index(FR_LIV3 ))); + put( "LIVELLO",row.get( f.cid2index(FR_LIV4 ))); + TString s(row.get(f.cid2index(FR_DESCR))); + int split_pos = s.find('\n'); + + const int descr_len = length("DESCR"); + if (split_pos < 0 && s.len() > descr_len) + split_pos = descr_len; + if (split_pos > 0) + { + put( "DESCR", s.left(split_pos)); + put("DESCLUNGA", "X"); + put("DESCEST", s.mid(split_pos)); + } + else + { + put("DESCR", s); + put("DESCLUNGA", ""); + zero("DESCEST"); + } + + put( "PREZZO", row.get( f.cid2index(FR_PREZZO )) ); + put( "UMQTA", row.get( f.cid2index(FR_UMQTA )) ); + TMask * m = ((TTipo_riga_documento &)tipo()).mask(); + const int pos = m->id2pos(FR_QTA); + if (pos >= 0 && m->fld(pos).field()->name() == "PSPESA") + put( "PSPESA", row.get( f.cid2index(FR_QTA )) ); + else + put( "QTA", row.get( f.cid2index(FR_QTA )) ); + put( "QTAEVASA", row.get( f.cid2index(FR_QTAEVASA )) ); + put( "RIGAEVASA", row.get( f.cid2index(FR_RIGAEVASA )) ); + put( "TARA", row.get( f.cid2index(FR_TARA )) ); + put( "PNETTO", row.get( f.cid2index(FR_PNETTO )) ); + put( "NCOLLI", row.get( f.cid2index(FR_NCOLLI )) ); + put( "DAEVADERE", row.get( f.cid2index(FR_DAEVADERE )) ); + put( "SCONTO", row.get( f.cid2index(FR_SCONTO )) ); + put( "PERCPROV", row.get( f.cid2index(FR_PERCPROV )) ); + put( "IMPFISSO", row.get( f.cid2index(FR_IMPFISSO )) ); + put( "CODIVA", row.get( f.cid2index(FR_CODIVA )) ); + put( "ADDIVA", row.get( f.cid2index(FR_ADDIVA )) ); + put( "ASPBENI", row.get( f.cid2index(FR_ASPBENI )) ); + } +} + +void TRiga_documento::autoload(TSheet_field & f) +{ + const int num = numero() - 1; + + TToken_string & row = f.row(num); + row.cut(0); + + row.add( get( "STATORIGA" ), f.cid2index(FR_STATORIGA )); + row.add( get( "TIPORIGA" ), f.cid2index(FR_TIPORIGA )); + const TString codmag(get("CODMAG")); + row.add( codmag.left(3), f.cid2index(FR_CODMAG )); + row.add( codmag.mid(3), f.cid2index(FR_CODDEP )); + row.add( get( "CODART" ), f.cid2index(FR_CODART )); + row.add( get( "LIVELLO" ), f.cid2index(FR_LIV1 )); // da modificare + row.add( get( "LIVELLO" ), f.cid2index(FR_LIV2 )); + row.add( get( "LIVELLO" ), f.cid2index(FR_LIV3 )); + row.add( get( "LIVELLO" ), f.cid2index(FR_LIV4 )); + TString s(get("DESCR")); + if (get_bool("DESCLUNGA")) + s << get("DESCEST"); + row.add(s, f.cid2index(FR_DESCR )); + row.add( get( "UMQTA" ), f.cid2index(FR_UMQTA )); + row.add( get( "PREZZO" ), f.cid2index(FR_PREZZO )); + TMask * m = ((TTipo_riga_documento &)tipo()).mask(); + const int pos = m->id2pos(FR_QTA); + if (pos >= 0 && m->fld(pos).field()->name() == "PSPESA") + row.add( get( "PSPESA" ), f.cid2index(FR_QTA )); + else + row.add( get( "QTA" ), f.cid2index(FR_QTA )); + row.add( get( "QTAEVASA" ), f.cid2index(FR_QTAEVASA )); + row.add( get( "RIGAEVASA" ), f.cid2index(FR_RIGAEVASA )); + row.add( get( "TARA" ), f.cid2index(FR_TARA )); + row.add( get( "PNETTO" ), f.cid2index(FR_PNETTO )); + row.add( get( "NCOLLI" ), f.cid2index(FR_NCOLLI )); + row.add( get( "DAEVADERE" ), f.cid2index(FR_DAEVADERE )); + row.add( get( "SCONTO" ), f.cid2index(FR_SCONTO )); + row.add( get( "PERCPROV" ), f.cid2index(FR_PERCPROV )); + row.add( get( "IMPFISSO" ), f.cid2index(FR_IMPFISSO )); + row.add( get( "CODIVA" ), f.cid2index(FR_CODIVA )); + row.add( get( "ADDIVA" ), f.cid2index(FR_ADDIVA )); + row.add( get( "ASPBENI" ), f.cid2index(FR_ASPBENI )); +} diff --git a/ve/velib02.h b/ve/velib02.h deleted file mode 100755 index 86040f8d9..000000000 --- a/ve/velib02.h +++ /dev/null @@ -1,147 +0,0 @@ -#ifndef __ASSOC_H -#include -#endif - -#include "sconti.h" -#include "tclifor.h" -#include "velib01.h" -#include "..\cg\pagament.h" - -struct dec_parm { - int pri_lit, - pri_val, - qta_lit, - qta_val, // si lo so non ha senso, ma lo faccio lo stesso - imp_lit, - imp_val; - // add other groups here -}; - -class TCodice_numerazione : public TRectype -{ - int _status; -public: - TObject* dup() const { return new TCodice_numerazione(codice()); } - -public: - int read(const char * cod); - const TString& codice() const { return get("CODTAB"); } - const TString& prefisso() const { return get("S6"); } - const TString& postfisso() const { return get("S7"); } - const bool manual_num() const { return get_bool("B0"); } - const bool auto_num() const { return get_bool("B1"); } - const TString& tipo_doc1() { return get("S2").mid(0,4);} - const TString& tipo_doc2() { return get("S2").mid(4,4);} - const TString& tipo_doc3() { return get("S2").mid(8,4);} - const TString& tipo_doc4() { return get("S2").mid(12,4);} - const TString& tipo_doc5() { return get("S2").mid(16,4);} - - const char * complete_num (long num); - int status() { return _status; } - TCodice_numerazione(const char* codnum = NULL); - TCodice_numerazione(const TRectype& rec); - virtual ~TCodice_numerazione(); -}; - -////////////////////////////////////////////////////////////////////////////////////////////// -// classe TDocumentoEsteso: oggetto che ha come finalita' il calcolo dei riepiloghi IVA -////////////////////////////////////////////////////////////////////////////////////////////// - -class TRiepilogo_Iva : public TObject -{ - real _imp; - real _iva; - real _ali; - byte _tipo; - TString16 _cod; - TString _des; - -protected: - -public: - real& imp() { return _imp;} // Imponibile - real& iva() { return _iva;} // Iva - real& ali() { return _ali;} // Aliquota - TString& cod() { return _cod;} // Descrizione - TString& des() { return _des;} // Descrizione - byte& tipo(){ return _tipo;}// Tipo (Vedi opzioni per la selzione di filtro nella validate()) - void zero(){ _imp = 0.0; _iva = 0.0; _ali = 0.0; _tipo = 0; _cod = ""; _des = ""; } - virtual TObject* dup() const { return new TRiepilogo_Iva(*this); } - TRiepilogo_Iva& operator = (TRiepilogo_Iva& a); - TRiepilogo_Iva() {_imp = 0.0; _iva = 0.0; _ali = 0.0; _tipo = 0;} - ~TRiepilogo_Iva() {}; -}; - -class TDocumentoEsteso : public TDocumento -{ - // Parametri del documento - dec_parm _parm; // Parametri per gli arrotondamenti - TTable * _iva; // Tabella codici IVA - - // Totali del documento ricalcolati non appena la tabellina di riepilogo IVA e' completa - real _importi_netti, _imposte; - - // membri per il calcolo del riepilogo IVA - bool _sum_selected; // TRUE se ha selezionato una riga del riepilogo, funge da semaforo per leggere la prossima - int _sum_filter; // Filtro corrente della riga TRiepilogo_Iva in corso di stampa (-1 se non ha ancora calcolato la tabella) - TString_array _order_array; // Array di TToken_string contenenti i codici IVA soddisfacenti ad ogni tipo di filtro - TAssoc_array _summary_table; // Array dove vengono memorizzate le imposte per aliquota - TArray _summary_array; // Array dove vengono memorizzate le imposte/imponibili per riga - TRiepilogo_Iva _sum_current; // Riga corrente del riepilogo - - // membri per il calcolo del riepilogo scadenze - TString_array _scadenze_array;// Array che contiene le scadenze ("|") - int _scadenze_current; // indice per identificare l'elementi corrente sull'array (-1 se non ha ancora calcolato) - -public: - - - // Funzioni per il riepilogo IVA - const bool summary_compiled() { return _sum_filter > -1; } - void compile_summary(); // Aggiorna la tabella riepilogativa - void summary_filter(byte selector); // filtra la tabellina secondo il filtro corrente se non e' gia' stato fatto - void summary_reset(bool force=FALSE); // riposiziona l'array dei codici IVA (_order_array). Se il parametro e' TRUE forza il ricalcolo della tabella - void summary_set_next(); // seleziona il prossimo elemento del filtro - const TRiepilogo_Iva& sum_current() { return _sum_current; } // ritorna la riga corrente del filtro corrente - const char * summary_get(const TString& w); // ritorna l'informazione richiesta estratta dall'elemento corrente - int summary_items() { return _summary_table.items();} // ritorna il numero di righe in totale della tabellina - const TAssoc_array& summary() { return _summary_table; } - const TArray& summary_array() { return _summary_array;} - - - // Funzioni per il ricalcolo delle scadenze - void scadenze_reset(); // riposiziona sulla prima scadenza - void scadenze_recalc(); // resetta e ricalcola le scadenze - void scadenze_set_next(); // seleziona il prossimo elemento dell'array delle scadenze - const char * scadenze_get(const TString& w); // reperisce l'informazione richiesta dall'elemento corrente - int scadenze_items() { return _scadenze_array.items(); } // restituisce il numero di scadenze - TString_array& scadenze() { return _scadenze_array; } - - // Funzioni di totalizzazione - real& tot_importi_netti(); - real& tot_imposte(); - real tot_spese(); - real tot_documento(); - // restituisce tot_imponibili, tot_esenti, tot_nonsoggetti a seconda del selettore: - // 1 = regime normale - // 2 = da ventilare (non usato) - // 4 = esenti - // 8 = non imponibili - // 16 = non soggetti - // pertanto i non imponibili avranno selettore 1 e gli esenti selettore 4. - // per avere esenti + non soggetti il selettore sara' 20 e cosi' via. - real tot_imponibili(byte selector); - - // Reperisce l'informazione dal campo G1 della testata - const char* get_head_info(const TString& what); - - // Funzioni per settare i parametri - void set_decimals(dec_parm & parm) { _parm = parm ; } - void set_condv(TCliFor * cli); // Cambia le condizioni di vendita - TDocumentoEsteso (const TRectype & rec, dec_parm & parm, TCond_vendita * condv = NULL) ; - TDocumentoEsteso (const TRectype & rec, TCond_vendita * condv = NULL) ; - TDocumentoEsteso () ; - ~TDocumentoEsteso(); -}; - - diff --git a/ve/velib03.cpp b/ve/velib03.cpp index 9939a5cd9..c1adbefbc 100755 --- a/ve/velib03.cpp +++ b/ve/velib03.cpp @@ -1,420 +1,1081 @@ -#include +#include "velib.h" +#include "vepriv.h" +#include + +#ifndef __APPLICAT_H +#include +#endif + +#ifndef __VEUML_H +#include "veuml.h" +#endif + +#ifndef __UTILITY_H #include -#include -#include -#include -#include -#include -#include -#include -#include "..\ve\velib01.h" -#include "..\ve\sconti.h" -#include "..\ve\velib03.h" -#include "..\ve\veconf.h" +#endif + +#ifndef __VEINI_H +#include "veini.h" +#endif + +#ifndef __DEFMASK_H +//#include +#endif -const real CENTO=real(100.0); +#ifndef __SCONTI_H +#include "sconti.h" +#endif +/////////////////////////////////////////////////////////// +// Tipo documento +/////////////////////////////////////////////////////////// -bool TCondizione_vendita:: ricerca(const char * codice, const real & qta) +TAssoc_array TTipo_documento::_formule_documento; + +TTipo_documento::TTipo_documento(const char* tipodoc) + : TRectype(LF_TABCOM) { - int tiporic; - switch (_condv.get_char("TIPO")) { - case 'L': - tiporic=A_LISTINI; - break; - case 'C': - tiporic=A_CONTRATTI; - break; - case 'O': - tiporic=A_OFFERTE; - break; - } - return cerca(tiporic,codice, qta); + settab("TIP"); + if (tipodoc && *tipodoc) + read(tipodoc); } - -bool TCondizione_vendita::cerca( int tiporicerca, const char * codriga , const real & qta) +TTipo_documento::TTipo_documento(const TRectype& rec) + : TRectype(rec) { - if( config_ditta().get_bool( "GES", "ve", tiporicerca ) ) - { - _condv.setkey( 1 ); + read_formule(); +} - if (_condv.get("COD").empty()) - return FALSE; - switch( tiporicerca ) - { - case A_CONTRATTI: - { - } - break; - case A_LISTINI: - { - _condv.put("TIPOCF", ""); - _condv.put("CODCF", ""); - if( !config_ditta().get_bool("GESLISCV", "ve")) - _condv.put("CATVEN", ""); - } - break; - case A_OFFERTE: - { - } - break; - } - if( _condv.read( ) == NOERR ) - { - // si posiziona sulla riga - const bool gest_scagl = _condv.get_bool("GESTSCAGL"); - const TString16 seqricrighe( _condv.get( "SEQRIC" ) ); - bool found = FALSE; - - for( int i = 0; !found && i < seqricrighe.len( ); i ++ ) - { - _rcondv.zero( ); - _rcondv.put( "TIPO", _condv.get( "TIPO")); - _rcondv.put( "CATVEN", _condv.get( "CATVEN")); - _rcondv.put( "TIPOCF", _condv.get( "TIPOCF")); - _rcondv.put( "CODCF", _condv.get( "CODCF")); - _rcondv.put("COD", _condv.get("COD")); - if (gest_scagl) - _rcondv.put("NSCAGL", 1); - - char ricerca = seqricrighe[ i ]; - _rcondv.put( "TIPORIGA", ricerca ); - switch( ricerca ) - { - case 'A': - { - _rcondv.put( "CODRIGA", codriga); - if (_rcondv.read() != NOERR && - _rcondv.prev() == NOERR && - _rcondv.get("TIPORIGA")[0] == 'A') - { - const TString cod_found(_rcondv.get("CODRIGA")); - return cod_found.compare(codriga, cod_found.len()) == 0; - } - } - break; - case 'R': - _rcondv.put("CODRIGA", anamag().get( "RAGGFIS")); - _rcondv.read(); - break; - case 'S': - { - _rcondv.put( "CODRIGA", anamag().get("GRMERC")); - _rcondv.read( ); - } - break; - case 'G': - { - _rcondv.put( "CODRIGA", anamag().get("GRMERC").left(3)); - _rcondv.read( ); - } - break; - default: - break; - } - found = _rcondv.good(); - } - // individua lo scaglione corretto in base alla quantita' - if (found && gest_scagl) - { - TRectype rec(_rcondv.curr()); - int last_scagl = 0; - int scagl = _rcondv.get_int("NSCAGL"); - real qta_lim(_rcondv.get_real("QLIM")); - while (_rcondv.good() && scagl > last_scagl && qta_lim > ZERO && qta > qta_lim) - { - if (_rcondv.next() == NOERR) - { - last_scagl = scagl; - scagl = _rcondv.get_int("NSCAGL"); - qta_lim =_rcondv.get_real("QLIM"); - rec = _rcondv.curr(); - } - } - _rcondv.read(rec); - } - _prezzo = _rcondv.get_real("PREZZO"); - return found; - } +TTipo_documento::~TTipo_documento() +{ } + +int TTipo_documento::read(const char* tipodoc) +{ + TTable t("%TIP"); + put("CODTAB", tipodoc); + int err = TRectype::read(t); + _formule = ""; + if (err == NOERR) + read_formule(); + else + yesnofatal_box("Tipo documento errato: %s", tipodoc); + return err; +} + +void TTipo_documento::read_formule() + +{ + TFilename prof(profile_name()); + + prof.ext("ini"); + + TConfig profile(prof); + + _formule = profile.get("CAMPICALC", "MAIN"); + _formule.add(profile.get("CALCOLI", "MAIN")); + _totale = profile.get("TOTALE", "MAIN"); + if (_totale.empty()) + { + _totale = "TOTDOC"; + if (_formule.find(_totale) < 0) + _formule.add("TOTDOC=IMPONIBILI()+IMPOSTE()"); + } + _totale_netto = "_"; + _totale_netto << _totale; + if (_totale.not_empty() && _formule.find(_totale) < 0) + { + error_box("Campo totale documento (%s) sconosciuto nel tipo documento %s", (const char *) _totale, (const char *) codice()); + _totale.cut(0); + } + _basesconto = profile.get("BASESCONTO", "MAIN"); + if (_basesconto.empty()) + { + _basesconto = "BASESCONTO"; + if (_formule.find(_basesconto) < 0) + _formule.add("BASESCONTO=SOMMA(\"IMPONIBILE()\", \"(TIPO() != 'S') && (TIPO() != 'C')\")"); + } + if (_basesconto.not_empty() && _formule.find(_basesconto) < 0) + { + error_box("Campo sconto documento (%s) sconosciuto nel tipo documento %s", (const char *) _basesconto, (const char *) codice()); + _basesconto.cut(0); + } + _spese = profile.get("SPESE", "MAIN"); + if (_spese.empty()) + { + _spese = "SPESE"; + if (_formule.find(_spese) < 0) + _formule.add("SPESE=SOMMA(\"IMPONIBILE()\", \"TIPO() != 'S'\")"); + } + if (_spese.not_empty() && _formule.find(_spese) < 0) + { + error_box("Campo spese (%s) sconosciuto nel tipo documento %s", (const char *) _spese, (const char *) codice()); + _spese.cut(0); } - // Ricerca fallita - return FALSE; } - -/*void TCondizione_vendita::ricerca(bool load_um_only, bool load_scagl_only) -{ - TString codart = anamag().get( "CODART" ); - const TString codriga = riga().get( FR_CODART ); - - _ivarid = _clifo->get_bool(LF_CFVEN, "IVARID"); - _load_mask = !load_um_only && !load_scagl_only; - - if(anamag().bad() || codriga != codart ) - { - anamag().setkey(1); - anamag().put("CODART", codriga); - if (anamag().read() != NOERR) - { - set _prezzo(ZERO); - set_sconto(""); - set_iva(""); - set_provv(ZERO); - return; + +TFormula_documento * TTipo_documento::succ_formula(bool restart) +{ + if (restart) + _formule.restart(); + const TString formula(_formule.get()); + if (formula.not_empty()) + { + char *expr = NULL; + const int p = formula.find("="); + if (p > 0) + { + expr = (char *) (const char *) formula + p; + *expr = '\0'; expr++; } - } - codart = umart().get( "CODART" ); - const TString16 umriga(riga().get(FR_UMQTA)); - const TString16 um(umart().get("UM")); - if (umart().bad() || codart != codriga || um != umriga) - { - umart().setkey(2); - umart().put("CODART", codriga); - umart().put("UM", um); - if (umart().read != NOERR) - umart().zero(); - } - - const bool found_condv = cerca(A_CONTRATTI) || cerca(A_OFFERTE) || cerca(A_LISTINI); - const bool cv_scagl = get_bool("GESTSCAGL"); - const bool cv_um = get_bool("GESTUM"); - - if (found_condv) - { - _load_mask |= load_scagl_only && cv_scagl; - _load_mask |= load_um_only && cv_um; - set_prezzo(_rcondv.get_real("PREZZO"), get("CODVAL")); - _load_mask = !load_um_only && !load_scagl_only; + TFormula_documento * o = (TFormula_documento*)_formule_documento.objptr(formula); + if (o == NULL) + { + o = new TFormula_documento(_documento, formula, expr); + _formule_documento.add(formula, o); + } + return o; } else -// if (full_load) - set_prezzo(umart().get_real("PREZZO")); - - -// if (full_load) -// { - if (_ivarid) - set_iva(anamag().get("CODIVAR")); - else - set_iva(anamag().get("CODIVA")); -// } + return NULL; +} - const char gestione = config_ditta().get_char( "GESSCORIGA", "ve" ); - const bool sco_scagl = config_ditta().get_bool("GESCOSCA", "ve"); - const bool sco_um_gest = config_ditta().get_bool("GESSCORIGAUM", "ve"); - -// if (!full_load && gestione != 'A' && !sco_scagl) -// { -// if (gestione == 'L' && found_condv) -// set_sconto(_rcondv.get("SCONTO")); -//} -// else - { - switch (gestione) - { - case 'N': - // Sconti di riga non gestiti - set_sconto(""); - break; - case 'L': - // Percentuale su contratti/offerte/listini/anagrafica - if (found_condv) - { - _load_mask |= load_scagl_only && cv_scagl; - _load_mask |= load_um_only && cv_um; - set_sconto(_rcondv.get("SCONTO")); - _load_mask = !load_um_only && !load_scagl_only; - } - else - set_sconto(anamag().get("SCONTO")); - break; - case 'A': - { - // Posiziono l'anagrafica - const char rigakey = config_ditta().get_char( "SCORIGAKEY", "ve" ); +TDocumento_mask::TDocumento_mask(const char* name, TDocumento * doc, int num, + int max) + : TVariable_mask(name, num, max), _progs_page(-1), _last_prog(-1), + _doc(doc) +{ + const int pos = id2pos(BASE_PIEDE + 1); - _sconti.setkey(1); - _sconti.put("TIPO", "R"); - if( config_ditta().get_bool("GESSCORIGACV", "ve")) - _sconti.put("CODCAT", clifo().get(LF_CFVEN, "CATVEN")); - if (sco_um_gest) - _sconti.put("UM", riga().get( FR_UMQTA)); - _sconti.put("TIPORIGA", rigakey); - - if (sco_scagl) - _sconti.put("NSCAGL", 1); - - switch (rigakey) - { - case 'A': - _sconti.put("CODART", codriga); - _sconti.read(); - break; - case 'R': - _sconti.put("CODART", anamag().get("RAGGFIS")); - _sconti.read(); - break; - case 'C': - { - _sconti.put( "CODART", anamag().get("GRMERC")); - _sconti.read( ); - } - break; - case 'L': - { - _sconti.put("CODART", anamag().get("GRMERC").left(3)); - _sconti.read(); - } - break; - default: - error_box("Tipo di chiave righe sconti '%c' non valida!", rigakey); - break; - } - - const bool found = _sconti.good(); - - if (found && sco_scagl) - { - TRectype rec(_sconti.curr()); - int last_scagl = 0; - int scagl = _sconti.get_int("NSCAGL"); - const real qta(riga().get_real(FR_QTA)); - real qta_lim(_sconti.get_real("QLIM")); - - while (_sconti.good() && scagl > last_scagl && qta_lim > ZERO && qta > qta_lim) - { - if (_sconti.next() == NOERR) - { - last_scagl = scagl; - scagl = _sconti.get_int("NSCAGL"); - qta_lim =_sconti.get_real("QLIM"); - rec = _sconti.curr(); - } - } - _rcondv.read(rec); - } - if (!found) - _sconti.zero(); - _load_mask |= load_scagl_only && sco_scagl; - _load_mask |= load_scagl_only && sco_um_gest; - set_sconto(_sconti.get("SCONTO")); - _load_mask = !load_um_only && !load_scagl_only; - } - break; - case 'C': - set_sconto(clifo().get(LF_CFVEN, "SCONTO")); - break; - default: - error_box("Tipo di gestione sconti '%c' non valido!", gestione ); - break; - } + if (pos >= 0) + { + _progs_page = find_parent_page(fld(pos)); + _last_prog = 0; + while (id2pos(BASE_PIEDE + _last_prog + 1) >= 0) + _last_prog++; } - - if (riga() && riga().get("PERCPROVV") >= 0 && riga().field(FR_PERCPROV).active()) +} + +void TDocumento_mask::next_page(int p) +{ + TMask::next_page(p); + if (curr_page() == _progs_page) + { + begin_wait(); + autosave(get_relation()); + for (int i = _last_prog; i > 0; i--) + { + const short id = BASE_PIEDE + i; + const TString16 name(field(id).field()->name()); + + set(id , doc().get(name)); + } + end_wait(); + } +} + +bool TDocumento_mask::on_key(KEY key) +{ + if (key == K_SHIFT + K_F12) { - const TString16 codage(testa().get(f_CODAG)); + field(F_STATO).enable(!field(F_STATO).enabled()); + return TRUE; + } + return TVariable_mask::on_key(key); +} + +/////////////////////////////////////////////////////////// +// Documento per vendite +/////////////////////////////////////////////////////////// + +TAssoc_array TDocumento::_tipi; + +TDocumento::TDocumento() + : TAuto_variable_rectype(LF_DOC), _rows(LF_RIGHEDOC, "NRIGA"), _nuovo(TRUE), + _condv(NULL), _rel(NULL), _sconto(NULL), _esenzione(NULL) +{ + set_memo_fld("G1"); +} + +TDocumento::TDocumento(char provv, int anno, const char* codnum, long numdoc, + TCond_vendita * condv, TRelation * rel) + : TAuto_variable_rectype(LF_DOC), _rows(LF_RIGHEDOC, "NRIGA"), _nuovo(TRUE), + _condv(condv), _rel(rel), _sconto(NULL), _esenzione(NULL) +{ + set_memo_fld("G1"); + if (numdoc <= 0) + { + numdoc = 0; + set_key(*this, provv, anno, codnum, numdoc); - if (codage.not_empty()) - { - TTable age("AGE"); - - age.put("CODTAB", codage); - if (age.read() == NOERR) + TRiga_documento* key = new TRiga_documento(this); + set_key(*key, provv, anno, codnum, numdoc); + _rows.set_key(key); // ok + } + else + read(provv, anno, codnum, numdoc); +} + +TDocumento::TDocumento(const TRectype& rec, TCond_vendita * condv, TRelation * rel) + : TAuto_variable_rectype(LF_DOC), _rows(LF_RIGHEDOC, "NRIGA"), _nuovo(FALSE), + _condv(condv), _rel(rel), _sconto(NULL), _esenzione(NULL) +{ + set_memo_fld("G1"); + read(rec); +} + +TDocumento::~TDocumento() + +{ + if (_sconto != NULL) delete _sconto; + if (_esenzione != NULL) delete _esenzione; +} + +real TDocumento::spese_incasso(real & imp, int ndec, TTipo_importo t) const +{ + real imp_spese; + real percentuale = get_real("PERCSPINC"); + static TArray spese_inc; + + if (percentuale > ZERO) + { + if (spese_inc.objptr(_rim_dir) == NULL) + { + TConfig conf(CONFIG_STUDIO); + + for (TTipo_pag p = _rim_dir; p < _nessun_pag; p = (TTipo_pag) ((int)p + 1)) { - char tipoprovv = age.get("S6")[0]; + real r(conf.get("IMPSPINC", "ve", p)); + spese_inc.add(r, p); + } + } + TPagamento & pag = ((TDocumento *)this)->pagamento(); + const int nrate = pag.n_rate(); + + for (int i = 0; i < nrate; i++) + { + const TTipo_pag p = (TTipo_pag) pag.tipo_rata(i); + imp_spese += (real &) spese_inc[p]; + } + imp_spese *= percentuale / 100.0; + + real cambio = get_real("CAMBIO"); - if (tipoprovv <= ' ') - tipoprovv = config_ditta().get_char( "AGETIPOPERC", "ve" ); + if (cambio == ZERO) + cambio = 1.0; + static TString16 codiva; + static long firm = -1; + long new_firm = main_app().get_firm(); -// if (!full_load) -// { -// if (tipoprovv == 'L' && found_condv) -// set_provv(_rcondv.get_real("PERCPROVV")); -// } -// else - { - switch (tipoprovv) - { - case 'A': - set_provv(age.get_real("R0")); - break; - case 'C': - set_provv(real(clifo().get(LF_CFVEN, "PERCPROVV"))); - break; - case 'L': - if (found_condv) + if (firm != new_firm) + { + TConfig conf(CONFIG_DITTA); + + codiva = conf.get("SPINCODIVA", "ve"); + firm = new_firm; + } + real iva_spese(iva(imp_spese, TRiga_documento::iva(codiva), ndec)); + if (t == _lordo) + imp_spese += iva_spese; + else + if (t == _imposta) + imp_spese = iva_spese; + imp_spese /= cambio; + imp_spese.round(ndec); + } + return imp_spese; +} + +real TDocumento::bolli(real & imp, int ndec, TTipo_importo t) const +{ + real tot_bolli; + static TArray sca_bolli; + static TArray imp_bolli; + static int nscagl; + TLocalisamfile clifo(LF_CLIFO); + bool estero = 2; + + if (get_bool("ADDBOLLI")) + { + real cambio = get_real("CAMBIO"); + + if (cambio == ZERO) + cambio = 1.0; + + real importo = imp*cambio; + TPagamento & pag = ((TDocumento*)this)->pagamento(); + const int nrate = pag.n_rate(); + real old_bolli = -1.00; + real iva_bolli; + real imp_orig = imposta(); + real sp_orig = spese(); + + for (int j = 0; j < 5 && tot_bolli != old_bolli; j++) + { + old_bolli = tot_bolli + iva_bolli; + const real imposte = imp_orig * cambio + iva_bolli; + const real imp_spese = sp_orig * cambio + tot_bolli - iva_bolli; + const real imponibile = importo - imposte - imp_spese; + + tot_bolli = ZERO; + pag.set_total(imponibile, imposte, imp_spese); + pag.set_rate_auto(); + + for (int i = 0; i < nrate; i++) + { + const TTipo_pag p = (TTipo_pag) pag.tipo_rata(i); + real imp = pag.importo_rata(i); + + switch (p) + { + case _ric_ban: + { + if (sca_bolli.objptr(0) == NULL) { - _load_mask |= load_scagl_only && sco_scagl; - _load_mask |= load_scagl_only && sco_um_gest; - set_provv(_rcondv.get_real("PERCPROVV")); - } - else - set_provv(anamag().get_real("PERCPROVV")); - break; - case 'V': - { - TString16 catven(testa().get("CATVEN")); - - if (catven.empty()) - catven = clifo().get(LF_CFVEN, "CATVEN"); - - if (catven.not_empty()) - { - TTable cve("CVE"); - - cve.put("CODART", codage); - if (cve.read() == NOERR) - set_provv(cve.get_real("R0")); + TConfig conf(CONFIG_STUDIO); + + for (nscagl = 0; nscagl < 7; nscagl++) + { + real s(conf.get("SPBOSCA", "ve", nscagl + 1)); + real i(conf.get("SPBOIMP", "ve", nscagl + 1)); + + if (s == ZERO && i == ZERO) + break; + sca_bolli.add(s, nscagl); + imp_bolli.add(i, nscagl); } + } + for (int i = 0; i < nscagl - 1; i++) + if ((real &) sca_bolli[i] >= imp) + break; + if (imp_bolli.items() > 0) + tot_bolli += (real &) imp_bolli[i]; + } + break; + case _tratta: + case _tratta_acc: + { + if (estero == 2) + { + clifo.put("TIPOCF", get("TIPOCF")); + clifo.put("CODCF", get("CODCF")); + + if (clifo.read() != NOERR) + clifo.zero(); + + const TString16 stato(clifo.get("STATOPAIV")); + + estero = stato.not_empty() && stato != "IT"; + if (!estero) + estero = clifo.get("STATOCF").not_empty() || clifo.get("COMCF")[0] == 'Z'; } - break; - default: - error_box("Tipo di gestione provvigioni '%c' non valido!", tipoprovv ); - break; - } + real r(imp); + + r.ceil(-3); + if (estero) + r *= 0.009; + else + r *= 0.012; + r.round(-2); + tot_bolli += r; + } + break; + case _cessione: + case _paghero: + case _let_cred: + case _rim_dir: + case _rid: + case _bonfico: + default: + break; } } + static TString16 codiva; + static long firm = -1; + long new_firm = main_app().get_firm(); + + if (firm != new_firm) + { + TConfig conf(CONFIG_DITTA); + + codiva = conf.get("SPBOCODIVA", "ve"); + firm = new_firm; + } + iva_bolli = iva(tot_bolli, TRiga_documento::iva(codiva), ndec); + importo += (tot_bolli + iva_bolli - old_bolli); + } + if (t == _lordo) + tot_bolli += iva_bolli; + else + if (t == _imposta) + tot_bolli = iva_bolli; + tot_bolli /= cambio; + tot_bolli.round(ndec); + } + + return tot_bolli; +} + +// Funzione statica utile a tutti gli utenti di LF_DOC e LF_RIGHEDOC +void TDocumento::set_key(TRectype& rec, char provv, int anno, const char* codnum, long numdoc) +{ + CHECK(provv == 'D' || provv == 'P', "Provvisorio o Definitivo?"); + CHECKD(anno > 1900, "Anno non valido: ", anno); + CHECK(codnum && *codnum, "Codice numerazione nullo"); + CHECKD(numdoc >= 0, "Numero documento non valido ", numdoc); + + rec.put("PROVV", provv); + rec.put("ANNO", anno); + rec.put("CODNUM", codnum); + rec.put("NDOC", numdoc); +} + +// Funzione statica utile a tutti gli utenti di LF_DOC e LF_RIGHEDOC +void TDocumento::copy_data(TRectype& dst, const TRectype& src) +{ + // Memorizza tutti i campi chiave + const char provv = dst.get_char("PROVV"); + const int anno = dst.get_int("ANNO"); + const TString16 codnum = dst.get("CODNUM"); + const long numdoc = dst.get_long("NDOC"); + const int nriga = dst.num() == LF_RIGHEDOC ? dst.get_int("NRIGA") : 0; + // Copia tutto il record + dst = src; + // Ripristina tutti i campi chiave + set_key(dst, provv, anno, codnum, numdoc); + if (nriga > 0) + dst.put("NRIGA", nriga); +} + +void TDocumento::copy_contents(const TDocumento & src) +{ + copy_data(head(), src.head()); + destroy_rows(); + const int rows = src.physical_rows(); + + for (int i = 0; i < rows ; i++) + { + const TRiga_documento & s = src[i]; + TRiga_documento & r = new_row(s.tipo().codice()); + + copy_data(r, s); + } +} + +TRiga_documento& TDocumento::insert_row(int row, const char *tipo) +{ + TRiga_documento * r = new TRiga_documento((const TRiga_documento &) _rows.key(), this); // ok + r->set_numero(row); + if (tipo) + r->set_tipo(tipo); + _rows.insert_row(r); // ok + return *r; +} + +TRiga_documento& TDocumento::new_row(const char *tipo) +{ + TRiga_documento & r = (TRiga_documento&)_rows.row(-1, TRUE); // ok + if (tipo) + r.set_tipo(tipo); + return r; +} + +int TDocumento::read(const TRectype& rec) +{ + head() = rec; + TRiga_documento* key = new TRiga_documento(this); + const char pr = tipo_numerazione(); + const int an = anno(); + const TString16 cn = numerazione(); + const long nu = numero(); + CHECK(nu > 0, "Numero documento nullo."); + set_key(*key, pr, an, cn, nu); + + TLocalisamfile doc(LF_DOC); + int err = TRectype::read(doc); + if (err == NOERR) + { + _nuovo = FALSE; + _rows.read(key); //ok + } + else + { + _nuovo = TRUE; + head() = rec; + destroy_rows(); + _rows.set_key(key); // ok + } + set_riga_sconto(); + if (is_fattura()) + set_riga_esenzione(); + return err; +} + +int TDocumento::read(char provv, int anno, const char* codnum, long numdoc) +{ + TRectype rec(LF_DOC); + CHECK(numdoc > 0, "Numero documento nullo."); + set_key(rec, provv, anno, codnum, numdoc); + return read(rec); +} + +long TDocumento::renum(long numdoc) +{ + if (numdoc <= 0) + { + const char tn = tipo_numerazione(); + const int an = anno(); + const TString16 nu = numerazione(); + numdoc = get_next_key(tn, an, nu); + } + char num[16]; sprintf(num, "%ld", numdoc); + renum_key("NDOC", num); // Aggiorna testata + _rows.renum_key("NDOC", num); // Aggiorna righe ok + return numdoc; +} + +void TDocumento::set_riga_sconto() +{ + const TString80 sconto(get("SCONTOPERC")); + + if (sconto.empty()) + { + if(_sconto != NULL) + delete _sconto; + _sconto = NULL; + } + else + { + if (_sconto == NULL) + { + static TString _tipo_riga_sc; + if (_tipo_riga_sc.empty()) + { + TConfig conf(CONFIG_STUDIO); + + _tipo_riga_sc = conf.get("TRSCONTI", "ve"); + } + _sconto = new TRiga_documento(this, _tipo_riga_sc); + _sconto->put("DESCR","Sconto"); + } + _sconto->put("SCONTO", sconto); + } +} + +void TDocumento::set_riga_esenzione() +{ + TCliFor & c = condv().clifo(); + const char tipo = get_char("TIPOCF"); + const long cod = get_long("CODCF"); + const TString80 occ(get("OCFPI")); + c.load(tipo, cod, occ); + const TIVA codes(c.get(LF_CFVEN, CLI_ASSFIS)); + const TString16 v_esenzione(c.get(LF_CFVEN, CLI_VSPROT)); + const TString16 v_data_esenzione(c.get(LF_CFVEN, CLI_VSDATAREG)); +// const TString16 ufficio_IVA; ?? + const TString16 n_registrazione(c.get(LF_CFVEN, CLI_NSPROT)); + const TString16 n_data_registrazione(c.get(LF_CFVEN, CLI_NSDATAREG)); + bool esente = codes.tipo().not_empty() && v_esenzione.not_empty() && + v_data_esenzione.not_empty() && n_registrazione.not_empty() && + n_data_registrazione.not_empty(); + + if (esente) + { + esente = FALSE; + const TString16 codiva(codes.codice()); + for (int i = physical_rows(); !esente && i > 0; i--) + esente = codiva == row(i).get("CODIVA"); + } + + if (!esente) + { + if(_esenzione != NULL) + delete _esenzione; + _esenzione = NULL; + } + else + { + static TString _tipo_riga_es; + static real _bollo_es; + if (_tipo_riga_es.empty()) + { + TConfig conf(CONFIG_STUDIO); + + _tipo_riga_es = conf.get("TRESENZ", "ve"); + _bollo_es = (real)conf.get("BOLLIES", "ve"); + } + if (_esenzione == NULL) + _esenzione = new TRiga_documento(this, _tipo_riga_es); + TString d(256); d.format("Fattura non imponibile come da vostra dichiarazion"); + + _esenzione->put( "DESCR", d); + + /* rilasciata dall' ufficio IVA di %s*/ + d.format("e n. %s del %s da noi annotata al n. %s il %s. Marca da bollo da Lit. %s sull' originale.", + (const char *) v_esenzione, (const char *) v_data_esenzione, + /*(const char *) ufficio_IVA, */(const char *) n_registrazione, + (const char *) n_data_registrazione, _bollo_es.string(".")); + _esenzione->put("DESCLUNGA", "X"); + _esenzione->put("DESCEST", d); + } +} + +void TDocumento::dirty_fields() +{ + for (TDocumento_variable_field * f = (TDocumento_variable_field *) first_variable_field(); + f != NULL; f = (TDocumento_variable_field *) succ_variable_field()) + f->set_dirty(); + for (int i = rows(); i > 0; i--) + { + TRiga_documento & r = (TRiga_documento &) row(i); + + if (r.doc_dependent()) + r.dirty_fields(FALSE); + } +} + +int TDocumento::write(bool re) const +{ + const bool nuovo = _nuovo || numero() <= 0; // E' nuovo di zecca! + if (nuovo && re) // quindi ... + re = FALSE; // ... non fare la rewrite + + TLocalisamfile doc(LF_DOC); + int err = NOERR; + if (re) + { + err = _rows.write(re); + if (err == NOERR) + { + err = TRectype::rewrite(doc); + if (err != NOERR) + err = TRectype::write(doc); + } + } + else + { + if (nuovo) + { + TDocumento& myself = *(TDocumento*)this; + if (numero() <= 0) + myself.renum(); + do + { + err = TRectype::write(doc); + if (err == _isreinsert) + myself.renum(); + } while (err == _isreinsert); + myself._nuovo = FALSE; + } + else + { + err = TRectype::write(doc); + if (err != NOERR) + err = TRectype::rewrite(doc); + } + + if (err == NOERR) + err = _rows.write(re); + } + return err; +} + +int TDocumento::remove() const +{ + TLocalisamfile doc(LF_DOC); + int err = _rows.remove(); + if (err == NOERR) + err = TRectype::remove(doc); + return err; +} + +const bool TDocumento::in_valuta() const +{ + const TString& val = valuta(); + return (val.not_empty() && val != "LIT"); +} + +TRiga_documento & TDocumento::row(int index) +{ + const int nrows = _rows.rows(); + TRiga_documento * r = NULL; + + if (index <= nrows) + r = &((TRiga_documento &) _rows.row(index, FALSE)); + else + { + CHECKD((index == nrows + 1 && (_sconto != NULL || _esenzione != NULL)) || (index == nrows + 2 && _sconto != NULL && _esenzione != NULL), + "Riga documento non esistente ", index); + if (index == nrows + 1) + { + r = _sconto != NULL ? _sconto : _esenzione; + } + if (index == nrows + 2) + r = _esenzione; + } + return *r; +} + +long TDocumento::get_next_key(char provv, int anno, const char* codnum) const +{ + static long n = 0; + + if (n == 0) + { + TLocalisamfile doc(LF_DOC); + TRectype& curr = doc.curr(); + set_key(curr, provv, anno, codnum, 9999999L); + + const int err = doc.read(_isgreat); + + if (err != _isemptyfile) + { + if (err == NOERR) + doc.prev(); + if (curr.get_char("PROVV") == provv && + curr.get_int("ANNO") == anno && + curr.get("CODNUM") == codnum) + n = curr.get_long("NDOC"); + } + } + + n++; + return n; +} + +const TTipo_documento& TDocumento::tipo() const +{ + const TString16 tipodoc(get("TIPODOC")); + CHECK(tipodoc.not_empty(), "Tipo documento nullo"); + TTipo_documento * o = (TTipo_documento*)_tipi.objptr(tipodoc); + if (o == NULL) + { + o = new TTipo_documento(tipodoc); + _tipi.add(tipodoc, o); + } + return *o; +} + +bool TDocumento::raggruppabile(const TDocumento& doc, TToken_string& campi) const +{ + bool ok = raggruppabile() && doc.raggruppabile(); + if (ok) + { + TString campo; + for (const char* c = campi.get(0); c && ok; c = campi.get()) + { + campo = get(c); + ok &= campo == doc.get(c); + } + } + return ok; +} + +void TDocumento::set_fields(TAuto_variable_rectype & rec) +{ + if (tipo_valido()) + { + TTipo_documento & tipo_doc = (TTipo_documento &) tipo(); + const TString16 tot_doc(tipo_doc.totale_doc()); + + for (const TFormula_documento * f = tipo_doc.first_formula(); f; f = tipo_doc.succ_formula()) + { + TExpr_documento * exp = f->expr(); + if (tot_doc == f->name()) + { + TString work_tot_doc(tot_doc); + + work_tot_doc.insert("_"); + TString netto_def(exp->string()); + + if (netto_def.find("IMPONIBILI") == -1) + { + if (netto_def.not_empty()) + netto_def << "+"; + netto_def << "IMPONIBILI()"; + } + + if (netto_def.find("IMPOSTE") == -1) + { + if (netto_def.not_empty()) + netto_def << "+"; + netto_def << "IMPOSTE()"; + } + + TExpr_documento * netto_exp = new TExpr_documento(netto_def, _numexpr, this); + + add_field(new TDocumento_variable_field(work_tot_doc, netto_exp)); + + TExpr_documento * tot_exp = new TExpr_documento( + format("%s + _BOLLI(%s)", (const char *) work_tot_doc, + (const char *) work_tot_doc), _numexpr, this); + + add_field(new TDocumento_variable_field(f->name(), tot_exp)); + } + else + add_field(new TDocumento_variable_field(f->name(), exp)); + if (exp) + exp->set_doc(this); + } + } +} + +real TDocumento::imponibile(bool spese, int ndec) const +{ + real val; + if (ndec > 99) + ndec = in_valuta() ? 3 : 0; + + for (int i = rows(); i > 0; i--) + val += ((TRiga_documento &) ((TDocumento *)this)->row(i)).imponibile(); + if (spese) + { + real tot_doc = val + imposta(FALSE, ndec); + val += spese_incasso(tot_doc, ndec, _netto); + tot_doc += spese_incasso(tot_doc, ndec); + val += bolli(tot_doc, ndec, _netto); + } + val.round(ndec); + return val; +} + +real TDocumento::imposta(bool spese, int ndec) const +{ + TAssoc_array ive; + + if (ndec > 99) + ndec = in_valuta() ? 3 : 0; + + for (int i = rows(); i > 0; i--) + { + TRiga_documento & r = ((TRiga_documento &) ((TDocumento *)this)->row(i)); + real iva(r.imposta(FALSE)); + const TString & cod_iva = r.get("CODIVA"); + + real * tot = (real *) ive.objptr(cod_iva); + if (tot == NULL) + ive.add(cod_iva, iva); + else + *tot += iva; + } + real val; + + ive.restart(); + for (real * iva = (real *) ive.get(); iva != NULL; iva = (real *) ive.get()) + { + if (*iva < ZERO) + iva->floor(ndec); + else + iva->ceil(ndec); + val += *iva; + } + if (spese) + { + real tot_doc = val + imponibile(FALSE, ndec); + val += spese_incasso(tot_doc, ndec, _imposta); + tot_doc += spese_incasso(tot_doc, ndec); + val += bolli(tot_doc, ndec, _imposta); + } + val.round(ndec); + return val; +} + +real TDocumento::totale_doc() const +{ + const TString16 field(tipo().totale_doc()); + + if (field.not_empty()) + return get_real(field); + else + { + real r = imponibile() + imposta(); + r += spese_incasso(r, in_valuta() ? 3 : 0); + r += bolli(r, in_valuta() ? 3 : 0); + return r; + } +} + +real TDocumento::totale_netto() const +{ + const TString16 field(tipo().totale_netto()); + + if (field.not_empty()) + return get_real(field); + else + return imponibile() + imposta(); +} + +real TDocumento::basesconto() const +{ + const TString16 field(tipo().basesconto()); + + if (field.not_empty()) + return get_real(field); + else + return ZERO; +} + +real TDocumento::spese() const +{ + const TString16 field(tipo().spese()); + + if (field.not_empty()) + return get_real(field); + else + return ZERO; +} + +TPagamento & TDocumento::pagamento() +{ + const TString16 codpag(get("CODPAG")); + + if (codpag != _pag.code()) + { + _pag.set_code(codpag); + _pag.read(); + } + return _pag; +} + +void TDocumento::put_str(const char* fieldname, const char* val) +{ + TString v(val); + if (strcmp(fieldname, "TIPODOC") == 0 && TRectype::get("TIPODOC") != v) + { + TAuto_variable_rectype::put_str(fieldname, v); + reset_fields(*this); + set_fields(*this); + } + else + { + TAuto_variable_rectype::put_str(fieldname, v); + dirty_fields(); + if (strcmp(fieldname, "SCONTOPERC") == 0) + set_riga_sconto(); + } +} + +void TDocumento::zero(const char * fieldname) +{ + if (strcmp(fieldname, "TIPODOC") == 0) + reset_fields(*this); + TAuto_variable_rectype::zero(fieldname); + dirty_fields(); +} + +void TDocumento::zero(char c) +{ + reset_fields(*this); + TAuto_variable_rectype::zero(c); + destroy_rows(); +} + +TDocumento & TDocumento::operator =(const TDocumento & d) +{ + zero(); + operator=((TRectype &)d); + _rows = d._rows; + set_riga_sconto(); + if (is_fattura()) + set_riga_esenzione(); + set_relation(d._rel); + set_condv(d._condv); + return *this; +} + +TRectype & TDocumento::operator =(const TRectype & r) +{ + TRectype::operator=(r); + reset_fields(*this); + set_fields(*this); + return *this; +} + +TRectype & TDocumento::operator =(const char * r) +{ + TRectype::operator=(r); + reset_fields(*this); + set_fields(*this); + return *this; +} + +void TDocumento::update_spese_aut(TString_array & spese_aut, bool preserve_old, TSheet_field * sh) + +{ + const bool interactive = sh != NULL; + if (tipo().spese_aut()) + { + const int nrows = _rows.rows(); + for (int i = nrows; i > 0; i--) + { + TRiga_documento & r = row(i); + + if (r.tipo().tipo() == RIGA_SPESEDOC && r.generata()) + { + if (preserve_old) + return; + destroy_row(i, TRUE); + if (interactive) + sh->destroy(i - 1); + } + } + TString16 cod_iva_cli; + const int nspese = spese_aut.items(); + if (nspese > 0) + { + TLocalisamfile cfven(LF_CFVEN); + cfven.put("TIPOCF", get("TIPOCF")); + cfven.put("CODCF", get("CODCF")); + if (cfven.read() == NOERR) + cod_iva_cli = cfven.get("ASSFIS"); + for (i = 0; i < nspese; i++) + { + const TString & s = spese_aut.row(i); + + TSpesa_prest sp(s); + TString16 tipo(sp.tipo_riga()); + TRiga_documento & riga = new_row(tipo); + + riga.put("CODART", s); + riga.set_generata(); + riga.put("DESCR", sp.descrizione()); + switch (sp.tipo()) + { + case 'Q': + { + real qta = sp.qta(); + + if (qta == ZERO) + qta = 1.0; + riga.put("QTA", qta); + } + case 'V': + { + const real cambio = get_real("CAMBIO"); + const TString16 valuta = get("CODVAL"); + real prezzo = sp.prezzo(); + + sppr_calc(sp, valuta, cambio, prezzo); + riga.put("PREZZO", prezzo); + riga.put("UMQTA", sp.um()); + } + break; + case 'P': + default: + break; + } + if (cod_iva_cli.empty()) + riga.put("CODIVA", sp.cod_iva()); + else + riga.put("CODIVA", cod_iva_cli); + if (interactive) + { + const int nrow = sh->insert(-1, FALSE); + riga.autoload(*sh); + sh->check_row(nrow); + } + } } } } -*/ - -void TCondizione_vendita::put_condv(const char *tipocv,const char *codicecv,const char *catven,const char *tipocf,const char *codcf) -{ - _condv.put("TIPO",tipocv); - _condv.put("CATVEN",catven); - _condv.put("TIPOCF",tipocf); - _condv.put("CODCF",codcf); - _condv.put("COD",codicecv); -} - -void TCondizione_vendita::put_listino(const char * codlist,const char *catven) -{ - if( !config_ditta().get_bool("GESLISCV", "ve")) - put_condv("L",codlist,"","",""); - else - put_condv("L",codlist,catven,"",""); -} -void TCondizione_vendita::put_contratto(const char * codcontr,const char *tipocf,const char *codcf) -{ - put_condv("C",codcontr,"",tipocf,codcf); -} -void TCondizione_vendita::put_offerta(const char * codoff) -{ - put_condv("C",codoff,"","",""); -} - - -TCondizione_vendita::TCondizione_vendita(TConfig * ditta, - TLocalisamfile * anamag, TLocalisamfile * umart) - : _condv(LF_CONDV), _rcondv(LF_RCONDV), - _sconti( LF_SCONTI ), - _anamag(anamag), _umart(umart), _config_ditta(ditta), _ivarid(FALSE) -{ -} - - - - - + diff --git a/ve/velib04.cpp b/ve/velib04.cpp new file mode 100755 index 000000000..e72c8051d --- /dev/null +++ b/ve/velib04.cpp @@ -0,0 +1,741 @@ +#include "velib.h" +#include + +#ifndef __EXECP_H +#include +#endif + +/////////////////////////////////////////////////////////// +// Lista di documenti +/////////////////////////////////////////////////////////// + +TDate TLista_documenti::num2date(char provv, int anno, const char* codnum, long num) const +{ + TLocalisamfile doc(LF_DOC); + CHECK(num > 0, "Numero documento nullo."); + TDocumento::set_key(doc.curr(), provv, anno, codnum, num); + + if (doc.read(_isgteq) != NOERR) // In caso d'errore ... + doc.last(); // prendi l'ultimo + + return doc.get("DATADOC"); +} + +int TLista_documenti::read(char provv, char tipocf, long clifo, int anno, + TToken_string& tipidoc, TToken_string& statidoc, + const TDate& dd, const TDate& ad, + const char* codnum, long dn, long an) +{ + CHECK(provv == 'D' || provv == 'P', "Provvisorio o Definitivo?"); + CHECK(tipocf == 'C' || tipocf == 'F', "Il tipo deve essere Cliente o Fornitore"); + CHECKD(clifo > 0L, "Codice cliente non valido", clifo); + CHECKD(anno > 1900, "Anno non valido: ", anno); + CHECK(!tipidoc.empty_items(), "Lista dei tipi documento vuota"); + CHECK(!statidoc.empty_items(), "Lista degli stati documento vuota"); + + TRelation doc(LF_DOC); + TRectype start(LF_DOC), stop(LF_DOC); + + start.put("TIPOCF", tipocf); + stop.put("TIPOCF", tipocf); + + start.put("CODCF", clifo); + stop.put("CODCF", clifo); + + start.put("PROVV", provv); + stop.put("PROVV", provv); + + start.put("ANNO", anno); + stop.put("ANNO", anno); + + if (dn > 0) + { + start.put("DATADOC", num2date(provv, anno, codnum, dn)); + start.put("NDOC", dn); + } + else + { + if (dd.ok() && dd > botime) + start.put("DATADOC", dd); + } + + if (an > 0) + { + stop.put("DATADOC", num2date(provv, anno, codnum, an)); + stop.put("NDOC", an); + } + else + { + if (ad.ok() && ad < eotime) + stop.put("DATADOC", ad); + } + + TString filter(16); + if (codnum && *codnum) + { + bool numfilter = FALSE; + + if (start.get("DATADOC").empty()) + numfilter = TRUE; + else + start.put("CODNUM", codnum); + + if (stop.get("DATADOC").empty()) + numfilter = TRUE; + else + stop.put("CODNUM", codnum); + + if (numfilter) + filter << "CODNUM=\"" << codnum << '"'; + } + + TCursor cur(&doc, filter, 2, &start, &stop); + const TRectype& head = cur.curr(); + + _documenti.destroy(); + for (cur = 0; cur.ok(); ++cur) + { + const TString16 tipodoc = head.get("TIPODOC"); + const TString16 statodoc = head.get("STATO"); + if (tipidoc.get_pos(tipodoc) >= 0 && statidoc.get_pos(statodoc) >= 0) + { + TDocumento* d = new TDocumento(head); + _documenti.add(d); + } + } + + return _documenti.items(); +} + +int TLista_documenti::write(bool re) const +{ + int err = NOERR; + for (int i = 0; i < _documenti.items() && err == NOERR; i++) + err = doc(i).write(re); + return err; +} + +/////////////////////////////////////////////////////////// +// Cliente/Fornitore per vendite +/////////////////////////////////////////////////////////// + +void TLista_clifo::TClifo::init(const TRectype& rec, const TRectype& ven) +{ + _codice = rec.get_long(CLI_CODCF); + CHECK(_codice > 0, "Codice cliente nullo"); + + if (!ven.empty()) + { + _agente = ven.get_long(CLI_CODAG); + _zona = ven.get_long(CLI_CODZONA); + } + else + _agente = _zona = 0; +} + +bool TLista_clifo::TClifo::read(char tipo, long cod) +{ + TRelation clifo(LF_CLIFO); + clifo.add(LF_CFVEN, "TIPOCF=TIPOCF|CODCF=CODCF"); + + TRectype& curr = clifo.curr(); + curr.put(CLI_TIPOCF, tipo); + curr.put(CLI_CODCF, cod); + if (clifo.read() == NOERR) + init(curr, clifo.curr(LF_CFVEN)); + else + zero(); + + return ok(); +} + +TLista_clifo::TClifo::TClifo(const TRectype& rec) +{ + CHECK(rec.num() == LF_CLIFO, "Record non clienti"); + const char tipo = rec.get_char(CLI_TIPOCF); + const long codice = rec.get_long(CLI_CODCF); + read(tipo, codice); +} + +int TLista_clifo::leggi(long dc, long ac, long da, long aa, long dz, long az) +{ + TRelation clifo(LF_CLIFO); + clifo.add(LF_CFVEN, "TIPOCF=TIPOCF|CODCF=CODCF"); + + TRectype start(LF_CLIFO), stop(LF_CLIFO); + + start.put(CLI_TIPOCF, tipo()); + if (dc > 0) + start.put(CLI_CODCF, dc); + + stop.put(CLI_TIPOCF, tipo()); + if (ac > 0) + stop.put(CLI_CODCF, ac); + + TString filter(32); + if (da > 0) + filter << '(' << LF_CFVEN << "->" << CLI_CODAG << ">=" << da << ')'; + if (aa > 0) + { + if (filter.not_empty()) filter << "&&"; + filter << '(' << LF_CFVEN << "->" << CLI_CODAG << "<=" << aa << ')'; + } + if (dz > 0) + { + if (filter.not_empty()) filter << "&&"; + filter << '(' << LF_CFVEN << "->" << CLI_CODZONA << ">=" << dz << ')'; + } + if (az > 0) + { + if (filter.not_empty()) filter << "&&"; + filter << '(' << LF_CFVEN << "->" << CLI_CODZONA << "<=" << az << ')'; + } + + TCursor cur(&clifo, filter, 1, &start, &stop); + const TRectype& cli = cur.curr(); + const TRectype& ven = cur.curr(LF_CFVEN); + for (cur = 0; cur.ok(); ++cur) + { + TClifo* c = new TClifo(cli, ven); + _clifo.add(c); + } + + if (dc > 0 || ac > 0) ordina_per_codice(); else + if (da > 0 || aa > 0) ordina_per_agente(); else + if (dz > 0 || az > 0) ordina_per_zona(); + + return _clifo.items(); +} + +int TLista_clifo::sort_by_code(const TObject** o1, const TObject** o2) +{ + TLista_clifo::TClifo* c1 = (TLista_clifo::TClifo*)*o1; + TLista_clifo::TClifo* c2 = (TLista_clifo::TClifo*)*o2; + const long d = c1->codice() - c2->codice(); + return d == 0L ? 0 : (d > 0 ? +1 : -1); +} + +int TLista_clifo::sort_by_agent(const TObject** o1, const TObject** o2) +{ + TLista_clifo::TClifo* c1 = (TLista_clifo::TClifo*)*o1; + TLista_clifo::TClifo* c2 = (TLista_clifo::TClifo*)*o2; + const long d = c1->agente() - c2->agente(); + return d == 0L ? 0 : (d > 0 ? +1 : -1); +} + +int TLista_clifo::sort_by_zone(const TObject** o1, const TObject** o2) +{ + TLista_clifo::TClifo* c1 = (TLista_clifo::TClifo*)*o1; + TLista_clifo::TClifo* c2 = (TLista_clifo::TClifo*)*o2; + const long d = c1->zona() - c2->zona(); + return d == 0L ? 0 : (d > 0 ? +1 : -1); +} + +int TLista_clifo::ordina_per_codice() +{ + _clifo.sort(sort_by_code); + return _clifo.items(); +} + +int TLista_clifo::ordina_per_agente() +{ + _clifo.sort(sort_by_agent); + return _clifo.items(); +} + +int TLista_clifo::ordina_per_zona() +{ + _clifo.sort(sort_by_zone); + return _clifo.items(); +} + +int TLista_clifo::find(long cod) const +{ + for (int i = items()-1; i >= 0; i--) + if (clifo(i).codice() == cod) break; + return i; +} + +int TLista_clifo::add(long cod) +{ + int pos = find(cod); + if (pos < 0) + { + TClifo* c = new TClifo(tipo(), cod); + pos = _clifo.add(c); + } + return pos; +} + +/////////////////////////////////////////////////////////// +// TElaborazione +/////////////////////////////////////////////////////////// + +TElaborazione::TElaborazione(const char* cod) : TRectype(LF_TABCOM) +{ + settab("ELD"); + if (cod && *cod) + read(cod); +} + +int TElaborazione::read(const char* cod) +{ + CHECK(cod && *cod, "Codice elaborazione nullo"); + TTable eld("%ELD"); + put("CODTAB", cod); + const int err = TRectype::read(eld); + if (err != NOERR) + yesnofatal_box("Codice elaborazione non valido: %s", cod); + return err; +} + +/////////////////////////////////////////////////////////// +// TEsterna +/////////////////////////////////////////////////////////// + +TEsterna::TEsterna(const char* cod) + : TElaborazione(cod) +{ +} + +bool TEsterna::elabora(TLista_documenti& doc_in, TLista_documenti& doc_out, + const TDate& data_elab) +{ + CHECK(doc_in.items() == 1, "Si deve specificare uno e un solo documento in entrata"); + CHECK(doc_out.items() == 1, "Si deve specificare uno e un solo documento in uscita"); + TFilename name; name.temp("ext"); + { + TConfig c(name); + TDocumento & d = doc_in[0]; + int nfields = d.items(); + TString par; par.format("%d,0", LF_DOC); + + for (int i = 0; i < nfields; i++) + { + const TString16 fname(d.fieldname(i)); + TFieldref f(fname, LF_DOC); + + f.write(c, par, d.get(fname)); + } + for (TVariable_field * v = d.first_variable_field(); v ; v = d.succ_variable_field()) + c.set(v->name(), v->get(), par); + const int rows = d.physical_rows(); + if (rows > 0) + { + nfields = d[0].items(); + + for (int r = 0; r < rows; r++) + { + TRiga_documento row = d[r]; + + par.format("%d,%d", LF_RIGHEDOC, r + 1); + for (int i = 0; i < nfields; i++) + { + const TString16 fname(row.fieldname(i)); + TFieldref f(fname, LF_RIGHEDOC); + + f.write(c, par, row.get(fname)); + } + } + } + } + TString command_line(applicazione_esterna()); command_line << " -i " << name; + TExternal_app app(command_line); + if (app.run() == 0) + { + TConfig c(name); + TDocumento & d = doc_out[0]; + int nfields = d.items(); + TString par; par.format("%d,0", LF_DOC); + + for (int i = 0; i < nfields; i++) + { + const TString16 fname(d.fieldname(i)); + TFieldref f(fname, LF_DOC); + + d.put(fname, f.read(c, par)); + } + for (TVariable_field * v = d.first_variable_field(); v ; v = d.succ_variable_field()) + v->put(c.get(v->name(), par)); + + TString_array p; + + c.list_paragraphs(p); + nfields = d[0].items(); + d.destroy_rows(); + int r = 1 ; + par.format("%d,%d", LF_RIGHEDOC, r); + while (p.find(par) >= 0) + { + TRiga_documento row = d.new_row(); + for (int i = 0; i < nfields; i++) + { + const TString16 fname(row.fieldname(i)); + TFieldref f(fname, LF_RIGHEDOC); + + row.put(fname, f.read(c, par)); + } + r++; + par.format("%d,%d", LF_RIGHEDOC, r); + } + } + return TRUE; +} + +/////////////////////////////////////////////////////////// +// TConsegna ordini +/////////////////////////////////////////////////////////// + +TConsegna_ordini::TConsegna_ordini(const char* cod) + : TElaborazione(cod) +{ +} + +bool TConsegna_ordini::elabora(TLista_documenti& doc_in, TLista_documenti& doc_out, + const TDate& data_elab) +{ + return TRUE; +} + +/////////////////////////////////////////////////////////// +// TFatturazione bolle +/////////////////////////////////////////////////////////// + +TFatturazione_bolle::TFatturazione_bolle(const char* cod) + : TElaborazione(cod) +{ +} + +void TFatturazione_bolle::tipi_validi(TToken_string& tipi) const +{ + const TString& s2 = get("S2"); + tipi.cut(0); + TString16 t; + for (int i = 0; i < 5; i++) + { + t = s2.mid(i*4, 4); + t.trim(); + if (t.not_empty()) + tipi.add(t); + } + CHECK(!tipi.empty_items(), "Nessun tipo documento valido"); +} + +void TFatturazione_bolle::stati_validi(TToken_string& stati) const +{ + const TString& s7 = get("S7"); + stati.cut(0); + TString16 s; + for (int i = 0; i < 5; i++) + { + s = s7.mid(i*4, 1); + s.trim(); + if (s.not_empty()) + stati.add(s); + } + CHECK(!stati.empty_items(), "Nessuno stato documento valido"); +} + + +bool TFatturazione_bolle::raggruppa(TDocumento& doc_in, TDocumento& doc_out) +{ +#ifdef DBG + const TString tipi = get("S2"); + const TString& tipodoc = doc_in.tipo().codice(); + for (int i = 0; i < 5; i++) + { + if (tipodoc == tipi.mid(i*4, 4)) + break; + } + if (i >= 5) + { + NFCHECK("Tipo documento non valido: '%s'", (const char*)tipodoc); + return FALSE; + } +#endif + + const char stato_finale_in = get_char("S4"); + doc_in.stato(stato_finale_in); + + const TString& tipo_out = get("S8"); + doc_out.put("TIPODOC", tipo_out); + + const char stato_finale_out = get_char("S9"); + doc_out.stato(stato_finale_out); + + if (gestione_riferimenti()) + { + // Determina ed eventualmente crea la riga di riferimento + const int riga_rif = riferimenti_in_testa() ? 1 : doc_out.rows()+1; + if (riga_rif > doc_out.rows()) + { + TRiga_documento& rout = doc_out.new_row(); + rout.forza_sola_descrizione(); + } + + TRiga_documento& rout = doc_out[riga_rif]; + + // Costruisce la stringa di riferimento + TString riferimento(80); + riferimento = doc_in.tipo().riferimento(); + if (riferimento.empty()) + riferimento = doc_in.tipo().descrizione(); + riferimento << " n. " << doc_in.numero(); + riferimento << " del " << doc_in.data().string(); + + // Setta la descrizione se vuota + if (rout.get("DESCR").empty()) + rout.put("DESCR", riferimento); + else + { + // Altrimenti aggiungi il riferimento al memo + TString memo(1024); + memo = rout.get("DESCEST"); + if (memo.empty()) + rout.put("DESCLUNGA", "X"); + else + memo << '\n'; + memo << riferimento; + rout.put("DESCEST", memo); + } + } + + const bool ignora_desc = ignora_descrizioni(); + + TToken_string campi_riga(80); + const bool ragg_rig = raggruppa_righe(); + if (ragg_rig) + { + campi_riga = "CODART|UMQTA"; // Uguali sempre + // Uguali opzionalmente + if (riga_uguale(0)) campi_riga.add("CODMAG"); + if (riga_uguale(1)) campi_riga.add("CODIVA"); + if (riga_uguale(2)) campi_riga.add("PREZZO|SCONTO"); + } + + for (int r = 1; r <= doc_in.rows(); r++) + { + const TRiga_documento& rin = doc_in[r]; + const bool rindesc = rin.sola_descrizione(); // La riga di input e' descrittiva + if (ignora_desc && rindesc) + continue; + + bool elaborata = FALSE; + + // Raggruppo le righe se e' settato il falg di raggruppamento e + // se la riga non contiene solo una descrizione + if (ragg_rig && !rindesc) // Se devo raggruppare le righe ... + { + const int last = doc_out.rows(); + for (int o = 1; o <= last; o++) // ... cerca una riga compatibile + { + TRiga_documento& rout = doc_out[o]; + if (rout.sola_descrizione()) // Ignora le righe descrittive + continue; + if (rin.raggruppabile(rout, campi_riga)) // Se esiste una riga compatibile ... + { + rout += rin; // ... sommaci la quantita' ecc. + elaborata = TRUE; // Ricorda di averla gia' elaborata + break; + } + } + } + if (!elaborata) // Se la riga non e' stata gia' sommata ... + { + TRiga_documento& rout = doc_out.new_row(); // ... crea una riga nuova e ... + doc_out.copy_data(rout, rin); // ... copiaci tutti i campi della riga sorgente. + } + } + + return TRUE; +} + +bool TFatturazione_bolle::elabora(TLista_documenti& doc_in, TLista_documenti& doc_out, + const TDate& data_elab) +{ + bool ok = TRUE; + + TToken_string campi_doc(128); // Lista di campi che devono essere uguali + campi_doc = "TIPOCF|CODCF|CODVAL|CODLIN"; // Uguali sempre + + // Uguali opzionalmente + const char* cond[] = { "CAMBIO", "SCONTO", "TIPODOC", "CODNUM", + "CODPAG", "CODABIA|CODCABA", "CODLIST", "CODAG", + "CODSPMEZZO", "CODPORTO", "CAUSTRASP", "CODVETT1|CODVETT2|CODVETT3", + NULL }; + for (int u = 0; cond[u]; u++) + if (doc_uguale(u)) campi_doc.add(cond[u]); + + for (int id = 0; id < doc_in.items() && ok; id++) + { + TDocumento& campione = doc_in[id]; + const int tot = doc_out.items(); + int od = tot; + + if (campione.raggruppabile()) // Se il documento ha il flag di raggruppabilita' ... + { + for (od = 0; od < tot; od++) // ... cerca un documento compatibile. + { + if (campione.raggruppabile(doc_out[od], campi_doc)) + break; + } + } + if (od >= tot) // Se non ho trovato un documento compatibile ... + { // ... creane uno nuovo (certamente compatibile) + const char provv = tipo_numerazione(); + const int anno = campione.anno(); + const TString codnum = codice_numerazione_finale(); + TDocumento* new_doc = new TDocumento(provv, anno, codnum, -1); + // Copia i dati della testata + TDocumento::copy_data(new_doc->head(), campione.head()); + + new_doc->put("DATADOC", data_elab); + + // Aggiungilo alla lista dei documenti in uscita + od = doc_out.add(new_doc); + } + + ok = raggruppa(campione, doc_out[od]); + } + return ok; +} + +/////////////////////////////////////////////////////////// +// TContabilizzazione +/////////////////////////////////////////////////////////// + +TContabilizzazione::TContabilizzazione(const char* cod) + : TElaborazione(cod) +{ +} + +bool TContabilizzazione::elabora(TLista_documenti& doc_in, TLista_documenti& doc_out, + const TDate& data_elab) +{ + CHECK(doc_in.items() == 1, "Si deve specificare uno e un solo documento in entrata"); + CHECK(doc_out.items() == 1, "Si deve specificare uno e un solo documento in uscita"); + return TRUE; +} +/////////////////////////////////////////////////////////// +// TCopia_documento +/////////////////////////////////////////////////////////// + +TCopia_documento::TCopia_documento(const char* cod) + : TElaborazione(cod) +{ +} + +bool TCopia_documento::elabora(TLista_documenti& doc_in, TLista_documenti& doc_out, + const TDate& data_elab) +{ + CHECK(doc_in.items() == 1, "Si deve specificare uno e un solo documento in entrata"); + CHECK(doc_out.items() == 1, "Si deve specificare uno e un solo documento in uscita"); + TDocumento & doc_src = doc_in[0]; + TDocumento & doc_dest = doc_out[0]; + + doc_dest.copy_contents(doc_src); + return TRUE; +} + + +/////////////////////////////////////////////////////////// +// TGenerazione_effetti +/////////////////////////////////////////////////////////// + +TGenerazione_effetti::TGenerazione_effetti(const char* cod) + : TElaborazione(cod) +{ +} + +bool TGenerazione_effetti::elabora(TLista_documenti& doc_in, TLista_documenti& doc_out, + const TDate& data_elab) +{ + CHECK(doc_in.items() == 1, "Si deve specificare uno e un solo documento in entrata"); + CHECK(doc_out.items() == 1, "Si deve specificare uno e un solo documento in uscita"); + return TRUE; +} + +/////////////////////////////////////////////////////////// +// TLista_elaborazioni +/////////////////////////////////////////////////////////// + +void TLista_elaborazioni::read() +{ + if (_elab == NULL) + { + _elab = new TAssoc_array(); + + TTable eld("%ELD"); + + for (int err = eld.first(); err == NOERR; err = eld.next()) + { + TElaborazione * el = NULL; + switch (eld.curr().get_int("I0")) + { + case _esterna : + el = new TEsterna(eld.curr()); + break; + case _consegna_ordini: + el = new TConsegna_ordini(eld.curr()); + break; + case _fatturazione_bolle : + el = new TFatturazione_bolle(eld.curr()); + break; + case _contabilizzazione : + el = new TContabilizzazione(eld.curr()); + break; + case _copia_documento : + el = new TCopia_documento(eld.curr()); + break; + case _generazione_effetti : + el = new TGenerazione_effetti(eld.curr()); + break; + default : + break; + } + _elab->add(el->codice(), el); + } + } +} + +int TLista_elaborazioni::select(TString_array & result, bool interattivo, bool insert_mode, const char * tipo_iniziale, const char * stato_iniziale, const char * tipo_finale, const char * stato_finale) +{ + read(); + _elab->restart(); + result.destroy(); + for (TElaborazione * el = (TElaborazione *)_elab->get(); el ; el = (TElaborazione *) _elab->get()) + { + bool ok = TRUE; + if (tipo_iniziale && stato_iniziale) + { + bool found = FALSE; + for (int i = 0; !found && i < 5; i++) + found = el->tipo_iniziale(i) == tipo_iniziale && el->stato_iniziale(i) == *stato_iniziale; + ok = found; + } + ok &= (tipo_finale && stato_finale && el->tipo_finale() == tipo_finale && el->stato_finale() == *stato_finale); + if (ok && (!interattivo || interattivo == el->interattivo()) && + (!insert_mode || insert_mode == el->insert_mode())) + result.add(el->codice()); + } + return result.items(); +} + +TElaborazione & TLista_elaborazioni::operator [](const char * key) const +{ + ((TLista_elaborazioni *)this)->read(); + return (TElaborazione &) (*_elab)[key]; +} + +void TLista_elaborazioni::update() +{ + delete _elab; _elab = NULL; + read(); + +} + +TLista_elaborazioni::~TLista_elaborazioni() +{ + if (_elab) + delete _elab; +} diff --git a/ve/velib05.cpp b/ve/velib05.cpp new file mode 100755 index 000000000..37e28f3c6 --- /dev/null +++ b/ve/velib05.cpp @@ -0,0 +1,344 @@ +#include "velib02.h" + +///////////////////////////////////////////////////////////// +// TCodice_numerazione +///////////////////////////////////////////////////////////// +TCodice_numerazione::TCodice_numerazione(const char* codnum) + : TRectype(LF_TABCOM), _status(NOERR) +{ + settab("NUM"); + if (codnum && *codnum) + _status = read(codnum); +} + +TCodice_numerazione::TCodice_numerazione(const TRectype& rec) + : TRectype(rec), _status(NOERR) +{ } + +TCodice_numerazione::~TCodice_numerazione() +{ +} + +const char * TCodice_numerazione::complete_num(long num) +{ + static TString codnum; + codnum = prefisso(); + codnum << num; + codnum << postfisso(); + return codnum; +} + +int TCodice_numerazione::read(const char* codnum) +{ + TTable t("%NUM"); + put("CODTAB", codnum); + int err = TRectype::read(t); + if (err != NOERR) + yesnofatal_box("Codice numerazione errato: %s", codnum); + return err; +} + +///////////////////////////////////////////////////////////// +// TRiepilogo IVA +///////////////////////////////////////////////////////////// +TRiepilogo_Iva& TRiepilogo_Iva::operator=(TRiepilogo_Iva& a) +{ + _imp = a.imp(); _iva = a.iva(); _ali = a.ali(); + _cod = a.cod(); _tipo = a.tipo(); _des = a.des(); + return *this; +} + +///////////////////////////////////////////////////////////// +//TDocumentoEsteso +///////////////////////////////////////////////////////////// +void TDocumentoEsteso::compile_summary() +{ + _sum_filter = 0; + const int items = rows(); + + _summary_table.destroy(); + _summary_array.destroy(); + _imposte = 0.0; + _importi_netti = 0.0; + const bool val = in_valuta(); + // Scorre tutte le righe e compila la tabellina _summary_table, per codice iva, + // nonche' il galactic_array _summary_array dove le informazioni sono registrate per singola riga + // cio' ad usi generici: porco diavolo! + for (int i = 1; i <= items; i++) + { + TRiga_documento& r = row(i); + int nriga = r.get_int("NRIGA"); + real price = r.get_real("PREZZO"); + real qta = r.get_real("QTA"); + real aliquota, sc, imponibile, iva; + TString sconto(r.get("SCONTO")); + TString codiva(r.get("CODIVA")); + _iva->put("CODTAB", codiva); + if (_iva->read() != NOERR) continue; // Se non trova il codice salta questa riga + + aliquota = _iva->get_real("R0"); + price = prezzo_scontato(price, sconto); + price.round(val ? _parm.pri_val : _parm.pri_lit); // prezzo scontato + qta.round(val ? _parm.qta_val : _parm.qta_lit); + imponibile = price * qta; + imponibile.round (val? _parm.imp_val : _parm.imp_lit); // imponibile di riga + iva = r.iva(val ? _parm.imp_val : _parm.imp_lit); + //iva = (imponibile * aliquota) / 100.0; + //iva.ceil(val ? _parm.imp_val : _parm.imp_lit); // imposta calcolata + + // Aggiorna o aggiunge l'elemento se non esiste + TRiepilogo_Iva riepilogo_tmp,row_riep; + const bool exists = _summary_table.is_key(codiva); + TRiepilogo_Iva& riepilogo = (exists ? (TRiepilogo_Iva&)_summary_table[codiva] : riepilogo_tmp); + // Aggiorna anche il totale importi netti ed il totale imposte + _importi_netti += imponibile; + _imposte += iva; + riepilogo.imp() += imponibile; riepilogo.iva() += iva; + row_riep.imp() = imponibile; row_riep.iva() = iva; + riepilogo.ali() = aliquota; riepilogo.cod() = codiva; + row_riep.ali() = aliquota; row_riep.cod() = codiva; + TString16 tipo(_iva->get("S1")); + int tipo_i = 1;// Regime IVA normale + if (tipo == "VE") tipo = 2; + else if (tipo == "ES") tipo = 4; + else if (tipo == "NI") tipo = 8; + else if (tipo == "NS") tipo= 16; + riepilogo.tipo() = tipo_i; + row_riep.tipo() = tipo_i; + if (riepilogo.tipo() != 1) // Se non e' regime normale salva anche la descrizione + { + TString s(_iva->get("S0")); + riepilogo.des() = s; + row_riep.des() = s; + } + _summary_table.add(codiva,riepilogo,exists); + _summary_array.add(row_riep,nriga); + } + // Inizializza l'array di ordine + for (i = 0; i<32;i++) + { + TToken_string s; + _order_array.add(s); + } +} + +void TDocumentoEsteso::summary_filter(byte selector) +{ + if (_sum_filter == -1) compile_summary(); // Crea la tabella se deve ancora farlo + // se ha selezionato una riga in precedenza deve finire di stamparla + // ovvero non seleziona il filtro fino a quando non ha ricevuto una summary_set_next() + if (_sum_selected) return; + // + // Procedimento: + // Memorizza in un TString_array tante TToken_string quanti sono i filtri possibili + // (al massimo 31 [1+2+4+8+16]). Ogni TToken_string contiene i codici IVA + // delle righe di TRiepilogo_Iva che soddisfano la condizione di filtro + _sum_selected = TRUE; + _sum_filter = selector; + TToken_string& codici = _order_array.row(_sum_filter-1); + if (codici.items() == 0) // Se non c'e' nemmeno un codice IVA allora deve effettuare il filtro + { // ovvero mette in <> tutti i codici IVA che soddisfano tale filtro + // sara' poi la summary_set_next() a selezionare sequenzialmente il giusto codice a seconda del filtro corrente + + // Scorre sequenzialmente la tabella _summary_table e compone la TToken_string con i codici IVA + const int items = _summary_table.items(); + TRiepilogo_Iva* curr = (TRiepilogo_Iva *) _summary_table.first_item(); + for (int i = 0; i < items && curr != NULL; i++) + { + if (curr->tipo() & _sum_filter) // se fa parte del filtro selezionato schiaffa il codice nella TToken_string + codici.add(curr->cod()); + curr = (TRiepilogo_Iva*) _summary_table.succ_item(); + } + codici.restart(); + summary_set_next(); // setta l'elemento corrente + } +} + +void TDocumentoEsteso::summary_reset(bool force) +{ + const int items = _order_array.items(); + if (force) _sum_filter = -1; + for (int i = 0; i 0) _scadenze_current++; +} + +const char * TDocumentoEsteso::scadenze_get(const TString& w) +{ + TString ret; + + if (_scadenze_current == -1) + // calcola le scadenze e le mette in _scadenze_array + scadenze_recalc(); + if (_scadenze_current > -1 && _scadenze_current < _scadenze_array.items()) + { + if (w == "DATA") ret = _scadenze_array.row(_scadenze_current).get(0); // ritorna la data di scadenza + if (w == "IMPORTO") ret = _scadenze_array.row(_scadenze_current).get(1); // ritorna l'importo in scadenza + } + return (const char*)ret; +} + +void TDocumentoEsteso::scadenze_set_next() +{ + if (_scadenze_current < _scadenze_array.items() && _scadenze_current >= 0) + _scadenze_current++; +} + +void TDocumentoEsteso::scadenze_reset() +{ + if (_scadenze_current > 0) + _scadenze_current = 0; +} + +real& TDocumentoEsteso::tot_importi_netti() +{ + if (!summary_compiled()) compile_summary(); + return _importi_netti; +} + +real& TDocumentoEsteso::tot_imposte() +{ + if (!summary_compiled()) compile_summary(); + return _imposte; +} + +real TDocumentoEsteso::tot_spese() +{ + TString16 t("TOTSP"); + real number(get_head_info(t)); + return number; +} + +real TDocumentoEsteso::tot_documento() +{ + if (!summary_compiled()) compile_summary(); + real number = _imposte + _importi_netti; + return number; +} + +real TDocumentoEsteso::tot_imponibili(byte selector) +{ + if (!summary_compiled()) compile_summary(); + + real number = 0.0; + const int items = _summary_table.items(); + TRiepilogo_Iva* curr = (TRiepilogo_Iva *) _summary_table.first_item(); + for (int i = 0; i < items && curr != NULL; i++) + { + if (curr->tipo() & selector) // se fa parte del filtro selezionato schiaffa il codice nella TToken_string + number += curr->imp(); + curr = (TRiepilogo_Iva*) _summary_table.succ_item(); + } + return number; +} + + +const char* TDocumentoEsteso::get_head_info(const TString & what) +{ + TToken_string memo(head().get("G1"),'\n'); // prende il campo memo con i totalizzatori. Un totalizzatore per riga nella forma = + TString rt; + const int items = memo.items(); + for (int i = 0; i last_scagl && qta_lim > ZERO && qta > qta_lim) + { + if (_rcondv.next() == NOERR) + { + last_scagl = scagl; + scagl = _rcondv.get_int("NSCAGL"); + qta_lim =_rcondv.get_real("QLIM"); + rec = _rcondv.curr(); + } + } + _rcondv.read(rec); + } + _prezzo = _rcondv.get_real("PREZZO"); + return found; + } + } + // Ricerca fallita + return FALSE; +} + +/*void TCondizione_vendita::ricerca(bool load_um_only, bool load_scagl_only) +{ + TString codart = anamag().get( "CODART" ); + const TString codriga = riga().get( FR_CODART ); + + _ivarid = _clifo->get_bool(LF_CFVEN, "IVARID"); + _load_mask = !load_um_only && !load_scagl_only; + + if(anamag().bad() || codriga != codart ) + { + anamag().setkey(1); + anamag().put("CODART", codriga); + if (anamag().read() != NOERR) + { + set _prezzo(ZERO); + set_sconto(""); + set_iva(""); + set_provv(ZERO); + return; + } + } + codart = umart().get( "CODART" ); + const TString16 umriga(riga().get(FR_UMQTA)); + const TString16 um(umart().get("UM")); + if (umart().bad() || codart != codriga || um != umriga) + { + umart().setkey(2); + umart().put("CODART", codriga); + umart().put("UM", um); + if (umart().read != NOERR) + umart().zero(); + } + + const bool found_condv = cerca(A_CONTRATTI) || cerca(A_OFFERTE) || cerca(A_LISTINI); + const bool cv_scagl = get_bool("GESTSCAGL"); + const bool cv_um = get_bool("GESTUM"); + + if (found_condv) + { + _load_mask |= load_scagl_only && cv_scagl; + _load_mask |= load_um_only && cv_um; + set_prezzo(_rcondv.get_real("PREZZO"), get("CODVAL")); + _load_mask = !load_um_only && !load_scagl_only; + } + else +// if (full_load) + set_prezzo(umart().get_real("PREZZO")); + + +// if (full_load) +// { + if (_ivarid) + set_iva(anamag().get("CODIVAR")); + else + set_iva(anamag().get("CODIVA")); +// } + + const char gestione = config_ditta().get_char( "GESSCORIGA", "ve" ); + const bool sco_scagl = config_ditta().get_bool("GESCOSCA", "ve"); + const bool sco_um_gest = config_ditta().get_bool("GESSCORIGAUM", "ve"); + +// if (!full_load && gestione != 'A' && !sco_scagl) +// { +// if (gestione == 'L' && found_condv) +// set_sconto(_rcondv.get("SCONTO")); +//} +// else + { + switch (gestione) + { + case 'N': + // Sconti di riga non gestiti + set_sconto(""); + break; + case 'L': + // Percentuale su contratti/offerte/listini/anagrafica + if (found_condv) + { + _load_mask |= load_scagl_only && cv_scagl; + _load_mask |= load_um_only && cv_um; + set_sconto(_rcondv.get("SCONTO")); + _load_mask = !load_um_only && !load_scagl_only; + } + else + set_sconto(anamag().get("SCONTO")); + break; + case 'A': + { + // Posiziono l'anagrafica + const char rigakey = config_ditta().get_char( "SCORIGAKEY", "ve" ); + + _sconti.setkey(1); + _sconti.put("TIPO", "R"); + if( config_ditta().get_bool("GESSCORIGACV", "ve")) + _sconti.put("CODCAT", clifo().get(LF_CFVEN, "CATVEN")); + if (sco_um_gest) + _sconti.put("UM", riga().get( FR_UMQTA)); + _sconti.put("TIPORIGA", rigakey); + + if (sco_scagl) + _sconti.put("NSCAGL", 1); + + switch (rigakey) + { + case 'A': + _sconti.put("CODART", codriga); + _sconti.read(); + break; + case 'R': + _sconti.put("CODART", anamag().get("RAGGFIS")); + _sconti.read(); + break; + case 'C': + { + _sconti.put( "CODART", anamag().get("GRMERC")); + _sconti.read( ); + } + break; + case 'L': + { + _sconti.put("CODART", anamag().get("GRMERC").left(3)); + _sconti.read(); + } + break; + default: + error_box("Tipo di chiave righe sconti '%c' non valida!", rigakey); + break; + } + + const bool found = _sconti.good(); + + if (found && sco_scagl) + { + TRectype rec(_sconti.curr()); + int last_scagl = 0; + int scagl = _sconti.get_int("NSCAGL"); + const real qta(riga().get_real(FR_QTA)); + real qta_lim(_sconti.get_real("QLIM")); + + while (_sconti.good() && scagl > last_scagl && qta_lim > ZERO && qta > qta_lim) + { + if (_sconti.next() == NOERR) + { + last_scagl = scagl; + scagl = _sconti.get_int("NSCAGL"); + qta_lim =_sconti.get_real("QLIM"); + rec = _sconti.curr(); + } + } + _rcondv.read(rec); + } + if (!found) + _sconti.zero(); + _load_mask |= load_scagl_only && sco_scagl; + _load_mask |= load_scagl_only && sco_um_gest; + set_sconto(_sconti.get("SCONTO")); + _load_mask = !load_um_only && !load_scagl_only; + } + break; + case 'C': + set_sconto(clifo().get(LF_CFVEN, "SCONTO")); + break; + default: + error_box("Tipo di gestione sconti '%c' non valido!", gestione ); + break; + } + } + + if (riga() && riga().get("PERCPROVV") >= 0 && riga().field(FR_PERCPROV).active()) + { + const TString16 codage(testa().get(f_CODAG)); + + if (codage.not_empty()) + { + TTable age("AGE"); + + age.put("CODTAB", codage); + if (age.read() == NOERR) + { + char tipoprovv = age.get("S6")[0]; + + if (tipoprovv <= ' ') + tipoprovv = config_ditta().get_char( "AGETIPOPERC", "ve" ); + +// if (!full_load) +// { +// if (tipoprovv == 'L' && found_condv) +// set_provv(_rcondv.get_real("PERCPROVV")); +// } +// else + { + switch (tipoprovv) + { + case 'A': + set_provv(age.get_real("R0")); + break; + case 'C': + set_provv(real(clifo().get(LF_CFVEN, "PERCPROVV"))); + break; + case 'L': + if (found_condv) + { + _load_mask |= load_scagl_only && sco_scagl; + _load_mask |= load_scagl_only && sco_um_gest; + set_provv(_rcondv.get_real("PERCPROVV")); + } + else + set_provv(anamag().get_real("PERCPROVV")); + break; + case 'V': + { + TString16 catven(testa().get("CATVEN")); + + if (catven.empty()) + catven = clifo().get(LF_CFVEN, "CATVEN"); + + if (catven.not_empty()) + { + TTable cve("CVE"); + + cve.put("CODART", codage); + if (cve.read() == NOERR) + set_provv(cve.get_real("R0")); + } + } + break; + default: + error_box("Tipo di gestione provvigioni '%c' non valido!", tipoprovv ); + break; + } + } + } + } + } +} +*/ + +void TCondizione_vendita::put_condv(const char *tipocv,const char *codicecv,const char *catven,const char *tipocf,const char *codcf) +{ + _condv.put("TIPO",tipocv); + _condv.put("CATVEN",catven); + _condv.put("TIPOCF",tipocf); + _condv.put("CODCF",codcf); + _condv.put("COD",codicecv); +} + +void TCondizione_vendita::put_listino(const char * codlist,const char *catven) +{ + if( !config_ditta().get_bool("GESLISCV", "ve")) + put_condv("L",codlist,"","",""); + else + put_condv("L",codlist,catven,"",""); +} +void TCondizione_vendita::put_contratto(const char * codcontr,const char *tipocf,const char *codcf) +{ + put_condv("C",codcontr,"",tipocf,codcf); +} +void TCondizione_vendita::put_offerta(const char * codoff) +{ + put_condv("C",codoff,"","",""); +} + + +TCondizione_vendita::TCondizione_vendita(TConfig * ditta, + TLocalisamfile * anamag, TLocalisamfile * umart) + : _condv(LF_CONDV), _rcondv(LF_RCONDV), + _sconti( LF_SCONTI ), + _anamag(anamag), _umart(umart), _config_ditta(ditta), _ivarid(FALSE) +{ +} + + + + + diff --git a/ve/velib03.h b/ve/velib06.h similarity index 100% rename from ve/velib03.h rename to ve/velib06.h diff --git a/ve/vepriv.h b/ve/vepriv.h new file mode 100755 index 000000000..d41bbc27b --- /dev/null +++ b/ve/vepriv.h @@ -0,0 +1,9 @@ + +enum _formule {_somma, _bolli, _bolli_int, _spinc, _prezzo, _importo, _sconto, _iva, _provv, _tipo, _imponibili, _imposte}; +enum TTipo_calcolo { _nessun_calcolo, _qtaprezzo, _valore, _percentuale, _scontoimp, _scontoperc}; + +void row_set_handler( TMask& m, const int field, const int index ); +bool liv_handler( TMask_field& f, KEY key ); +bool sppr_handler( TMask_field& f, KEY key ); +bool iva_handler( TMask_field& f, KEY key ); +void sppr_calc(TRectype & rec, const TString & valuta_doc, const real & cambio, real & prezzo);