1fd0a23742
Files correlati : ve0.exe ve6.exe Ricompilazione Demo : [ ] Commento : AO20097 In fase di bollettazione voglio evadere un ordine già evaso parzialmente, se premo su "Consegna tutte le righe" nella bolla generata mi riporta anche le righe già evase lasciandole tutte con quantità nulla. GF20100 L'evasione di un ordine con riga merce con qtà a blank non riporta alcuna riga evasa nel d.d.t. git-svn-id: svn://10.65.10.50/trunk@11473 c028cbd2-c16b-5b4b-a496-9718f37d4682
3950 lines
129 KiB
C++
Executable File
3950 lines
129 KiB
C++
Executable File
#include <applicat.h>
|
||
#include <execp.h>
|
||
#include <mailbox.h>
|
||
#include <modaut.h>
|
||
#include <tabutil.h>
|
||
#include <progind.h>
|
||
#include <printer.h>
|
||
#include <viswin.h>
|
||
#include <utility.h>
|
||
|
||
#include <mov.h>
|
||
#include <rmov.h>
|
||
#include <rmoviva.h>
|
||
#include <clifo.h>
|
||
#include <cfven.h>
|
||
#include <comuni.h>
|
||
#include <nditte.h>
|
||
#include <occas.h>
|
||
#include <scadenze.h>
|
||
#include <unloc.h>
|
||
|
||
#include <doc.h>
|
||
#include <rdoc.h>
|
||
|
||
#include "../mg/anamag.h"
|
||
|
||
|
||
#include "velib.h"
|
||
#include "../cg/cg2103.h"
|
||
#include "../cg/cgsaldac.h"
|
||
#include "../cg/cglib02.h"
|
||
|
||
///////////////////////////////////////////////////////////
|
||
// TContabilizzazione
|
||
///////////////////////////////////////////////////////////
|
||
|
||
// TMovimentoPN_VE
|
||
// Classe derivata da TMovimentoPN per calcolare automaticamente le righe conabili
|
||
// una volta settate le righe iva e la riga di totale documento
|
||
// Sostanzialmente di tratta di aggiungere un metodo in piu' :
|
||
// recalc_cg_rows(), liberamente ispirato alla notify_iva() in cg2102.cpp
|
||
|
||
class TMovimentoPN_VE : public TMovimentoPN
|
||
{
|
||
TCausale * _caus;
|
||
bool _valuta;
|
||
|
||
protected:
|
||
// simula il K_SPACE di iva_notify
|
||
void create_row(int i, const TString & descr_cr);
|
||
// simula il K_ENTER di iva_notify
|
||
void enter_row(int i, const TString & descr_cr);
|
||
// verifica se si tratta di iva indetraibile
|
||
bool detraibile(TRectype& rec) const ;
|
||
// cerca la prima tra quelle di contabilita' che corrisponde al tipo indicato
|
||
int type2pos(char tipo);
|
||
// Trova nelle righe contabili un conto nelle righe di tipo prescelto
|
||
int bill2pos(const TBill& conto, const TString & codcms, const TString & fascms, char tipo);
|
||
// trasforma un real in TImporto, in base al tipo riga
|
||
TImporto real2imp(const real& r, char row_type);
|
||
// setta il record delle righe di contabilita'
|
||
int insert_cg_rec(int n, const TImporto& imp, TBill& conto, const char * codcms, const char * fascms, const char* desc, char tipo);
|
||
// setta il record delle righe di contabilita'
|
||
int set_cg_rec(int n, const TImporto& imp, TBill& conto, const char * codcms, const char * fascms, const char* desc, char tipo);
|
||
// aggiunge l'importo indicato alla n-esima riga di contabilita'
|
||
bool add_cg_rec(int n, const TImporto& imp);
|
||
// Legge l'importo della riga n e lo ritorna col segno dovuto
|
||
TImporto get_cg_imp(int n);
|
||
// Setta l'importo della riga n
|
||
void set_cg_imp(int n, const TImporto& imp);
|
||
// verifica se il movimento e' quadrato oppure ha qualche maledetto sbilancio
|
||
// ritorna TRUE, ovviamente, se everything's alright.
|
||
public:
|
||
// ricalcola le righe di contabilita' dalle righe iva presenti
|
||
// e verifica la quadratura del movimento. Ritorna TRUE se il movimento e' scrivibile
|
||
void set_caus(TCausale * c) { _caus = c;}
|
||
bool movement_ok() ;
|
||
void add_row_re(int i);
|
||
bool add_row_cp_re(int i);
|
||
void map_conto_re(TBill & c);
|
||
void destroy_iva_row(int i = -1);
|
||
int recalc_cg_rows(const TString & descr_cr, TCausale & caus);
|
||
TMovimentoPN_VE(bool valuta) : _valuta(valuta), _caus(NULL) {};
|
||
virtual ~TMovimentoPN_VE() {}
|
||
};
|
||
|
||
void TMovimentoPN_VE::destroy_iva_row(int i)
|
||
{
|
||
if (i < 0)
|
||
((TRecord_array&)iva_rows()).destroy_rows();
|
||
else
|
||
((TRecord_array&)iva_rows()).destroy_row(i+1, TRUE);
|
||
}
|
||
|
||
TImporto TMovimentoPN_VE::real2imp(const real& r, char row_type)
|
||
{
|
||
CHECK(_caus,"Orgggssbb..._caus pointer is NULL!");
|
||
bool dare = FALSE;
|
||
if (row_type == 'S')
|
||
{
|
||
dare = _caus->sezione_ritsoc() == 'D';
|
||
}
|
||
else
|
||
{
|
||
if (row_type == 'F')
|
||
{
|
||
dare = _caus->sezione_ritsoc() == 'D';
|
||
}
|
||
else
|
||
{
|
||
dare = _caus->sezione_clifo() == 'D';
|
||
if (row_type != 'T' && row_type != 'F' && row_type != 'P')
|
||
dare = !dare;
|
||
}
|
||
}
|
||
TImporto importo(dare ? 'D' : 'A', r);
|
||
return importo;
|
||
}
|
||
|
||
bool TMovimentoPN_VE::detraibile(TRectype& rec) const
|
||
{
|
||
CHECK(_caus,"Orgggssbb..._caus pointer is NULL!");
|
||
if (_caus->iva() == iva_vendite)
|
||
return TRUE;
|
||
|
||
if (rec.get_int(RMI_TIPODET) != 0)
|
||
return FALSE;
|
||
|
||
const int annodoc = curr().get_date(MOV_DATADOC).year();
|
||
const bool prorata100 = _caus->reg().prorata100(annodoc);
|
||
return !prorata100; // Se prorata = 100% e' indetraibile
|
||
}
|
||
|
||
int TMovimentoPN_VE::bill2pos(const TBill& conto, const TString & codcms, const TString & fascms, char tipo)
|
||
{
|
||
const int items = cg_items();
|
||
const bool has_cm = main_app().has_module(CMAUT, CHK_DONGLE);
|
||
|
||
for (int i = 0; i < items; i++)
|
||
{
|
||
TRectype& s = cg(i);
|
||
const char t = s.get_char(RMV_ROWTYPE);
|
||
if (t == tipo)
|
||
{
|
||
TBill c;
|
||
const int gr = s.get_int(RMV_GRUPPO);
|
||
const int co = s.get_int(RMV_CONTO);
|
||
const long so = s.get_long(RMV_SOTTOCONTO);
|
||
|
||
c.set(gr,co,so);
|
||
if (has_cm)
|
||
{
|
||
if (c == conto && (codcms == s.get(RMV_CODCMS)) && (fascms == s.get(RMV_FASCMS)))
|
||
return i;
|
||
}
|
||
else
|
||
if (c == conto)
|
||
return i;
|
||
}
|
||
}
|
||
return -1;
|
||
}
|
||
|
||
int TMovimentoPN_VE::type2pos(char tipo)
|
||
{
|
||
const int items = cg_items();
|
||
for (int i = 0; i < items; i++)
|
||
{
|
||
TRectype& s = cg(i);
|
||
const char t = s.get_char(RMV_ROWTYPE);
|
||
if (t == tipo)
|
||
return i;
|
||
}
|
||
return -1;
|
||
}
|
||
|
||
void TMovimentoPN_VE::set_cg_imp(int n, const TImporto& imp)
|
||
{
|
||
TRectype& rec = cg(n);
|
||
rec.put(RMV_SEZIONE,imp.sezione());
|
||
rec.put(RMV_IMPORTO,imp.valore());
|
||
}
|
||
|
||
TImporto TMovimentoPN_VE::get_cg_imp(int n)
|
||
{
|
||
TRectype& rec = cg(n);
|
||
TImporto importo;
|
||
const char sez = rec.get_char(RMV_SEZIONE);
|
||
const real valore(rec.get_real(RMV_IMPORTO));
|
||
importo.set(sez,valore);
|
||
return importo;
|
||
}
|
||
|
||
bool TMovimentoPN_VE::add_cg_rec(int n, const TImporto& imp)
|
||
{
|
||
TImporto tot(get_cg_imp(n));
|
||
tot += imp;
|
||
tot.normalize();
|
||
set_cg_imp(n, tot);
|
||
return tot.is_zero();
|
||
}
|
||
|
||
int TMovimentoPN_VE::set_cg_rec(int n, const TImporto& imp, TBill& conto, const char * codcms, const char * fascms,
|
||
const char* desc, char tipo)
|
||
{
|
||
const bool insert = n < 0;
|
||
if (insert) n = cg_items(); // Questa e' la prima riga di contabilita' vuota e disponibile
|
||
TRectype& rec = cg(n);
|
||
|
||
if (insert)
|
||
{
|
||
TRectype& head = lfile().curr();
|
||
const int annoes = head.get_int(MOV_ANNOES);
|
||
const long numreg = head.get_long(MOV_NUMREG);
|
||
TDate datareg(head.get_date(MOV_DATAREG));
|
||
rec.put(RMV_ANNOES,annoes);
|
||
rec.put(RMV_NUMREG,numreg);
|
||
rec.put(RMV_DATAREG,datareg);
|
||
}
|
||
rec.put(RMV_SEZIONE,imp.sezione());
|
||
rec.put(RMV_IMPORTO,imp.valore());
|
||
rec.put(RMV_TIPOC,conto.tipo());
|
||
rec.put(RMV_GRUPPO,conto.gruppo());
|
||
rec.put(RMV_CONTO,conto.conto());
|
||
rec.put(RMV_SOTTOCONTO,conto.sottoconto());
|
||
|
||
if (main_app().has_module(CMAUT, CHK_DONGLE))
|
||
{
|
||
rec.put(RMV_CODCMS,codcms);
|
||
rec.put(RMV_FASCMS,fascms);
|
||
}
|
||
rec.put(RMV_DESCR, desc);
|
||
|
||
if (tipo != ' ')
|
||
{
|
||
if (tipo == 'T') // Calcolo contropartita
|
||
{
|
||
TRectype& irec = iva(0);
|
||
const char t = irec.get_char(RMI_TIPOC);
|
||
const int gr = irec.get_int(RMI_GRUPPO);
|
||
const int co = irec.get_int(RMI_CONTO);
|
||
const long so = irec.get_long(RMI_SOTTOCONTO);
|
||
rec.put(RMV_TIPOCC,t);
|
||
rec.put(RMV_GRUPPOC,gr);
|
||
rec.put(RMV_CONTOC,co);
|
||
rec.put(RMV_SOTTOCONTOC,so);
|
||
}
|
||
else
|
||
{
|
||
int pos = -1;
|
||
if (tipo == 'F')
|
||
{
|
||
pos = type2pos('D');
|
||
if (pos == -1)
|
||
pos = type2pos('N');
|
||
}
|
||
else
|
||
pos = type2pos('T');
|
||
if (pos >= 0)
|
||
{
|
||
TRectype& crec = cg(pos);
|
||
const char t = crec.get_char(RMV_TIPOC);
|
||
const int gr = crec.get_int(RMV_GRUPPO);
|
||
const int co = crec.get_int(RMV_CONTO);
|
||
const long so = crec.get_long(RMV_SOTTOCONTO);
|
||
rec.put(RMV_TIPOCC,t);
|
||
rec.put(RMV_GRUPPOC,gr);
|
||
rec.put(RMV_CONTOC,co);
|
||
rec.put(RMV_SOTTOCONTOC,so);
|
||
}
|
||
}
|
||
}
|
||
|
||
rec.put(RMV_ROWTYPE,tipo);
|
||
return n;
|
||
}
|
||
|
||
int TMovimentoPN_VE::insert_cg_rec(int n, const TImporto& imp, TBill& conto, const char * codcms, const char * fascms,
|
||
const char* desc, char tipo)
|
||
{
|
||
if (n >= 0 && n < cg_items())
|
||
{
|
||
TRectype * row = new TRectype(LF_RMOV);
|
||
TRectype& head = lfile().curr();
|
||
const int annoes = head.get_int(MOV_ANNOES);
|
||
const long numreg = head.get_long(MOV_NUMREG);
|
||
TDate datareg(head.get_date(MOV_DATAREG));
|
||
|
||
row->put(RMV_NUMREG,numreg);
|
||
row->put(RMV_NUMRIG, n + 1);
|
||
row->put(RMV_ANNOES,annoes);
|
||
row->put(RMV_DATAREG,datareg);
|
||
((TRecord_array &) cg_rows()).insert_row(row);
|
||
}
|
||
return set_cg_rec(n, imp, conto, codcms, fascms, desc, tipo);
|
||
}
|
||
|
||
void TMovimentoPN_VE::create_row(int i, const TString & descr_cr)
|
||
{
|
||
CHECK(_caus,"Orgggssbb..._caus pointer is NULL!");
|
||
TRectype& cur = iva(i);
|
||
real oldimp = cur.get_real(RMI_IMPONIBILE);
|
||
real oldiva = cur.get_real(RMI_IMPOSTA);
|
||
|
||
if (oldiva.is_zero() && _caus->corrispettivi()) // In caso di corrispettivi ...
|
||
{
|
||
const TString4 zanicchi(cur.get(RMI_CODIVA)); // Codice IVA
|
||
const TCodiceIVA i(zanicchi);
|
||
oldiva = i.scorpora(oldimp); // ... scorpora imposta dall'imponibile
|
||
}
|
||
|
||
const char tipod = detraibile(cur) ? 'D' : 'N';
|
||
|
||
if (type2pos(tipod) < 0 && !oldiva.is_zero())
|
||
{
|
||
const int ri = tipod == 'D' ? RIGA_IVA_DETRAIBILE : RIGA_IVA_NON_DETRAIBILE;
|
||
TBill c; _caus->bill(ri, c);
|
||
if (c.ok())
|
||
{
|
||
const TString80 d(_caus->desc_agg(ri));
|
||
set_cg_rec(-1, real2imp(ZERO, 'I'), c, "", "", d, tipod);
|
||
}
|
||
else
|
||
if (ri == RIGA_IVA_NON_DETRAIBILE) // Se non esiste il conto IVA indetraibile ...
|
||
{ // ... somma imponibile e imposta
|
||
oldimp += oldiva;
|
||
oldiva = 0.0;
|
||
}
|
||
}
|
||
|
||
TBill oldconto;
|
||
const int gr = cur.get_int(RMI_GRUPPO);
|
||
const int co = cur.get_int(RMI_CONTO);
|
||
const long so = cur.get_long(RMI_SOTTOCONTO);
|
||
const TString80 codcms = cur.get(RMI_CODCMS);
|
||
const TString80 fascms = cur.get(RMI_FASCMS);
|
||
oldconto.set(gr,co,so);
|
||
if (oldconto.ok())
|
||
{
|
||
if (bill2pos(oldconto, codcms, fascms, 'I') < 0)
|
||
{
|
||
const TString d(_caus->desc_agg(2));
|
||
|
||
set_cg_rec(-1, real2imp(ZERO, 'I'), oldconto, codcms, fascms, d.empty() ? descr_cr: d, 'I');
|
||
}
|
||
}
|
||
}
|
||
|
||
void TMovimentoPN_VE::enter_row(int i, const TString & descr_cr)
|
||
{
|
||
CHECK(_caus,"Orgggssbb..._caus pointer is NULL!");
|
||
TRectype& cur = iva(i);
|
||
real imponibile = cur.get_real(RMI_IMPONIBILE);
|
||
real imposta = cur.get_real(RMI_IMPOSTA);
|
||
|
||
if (imposta.is_zero() && _caus->corrispettivi()) // In caso di corrispettivi ...
|
||
{
|
||
const TString4 zanicchi(cur.get(RMI_CODIVA));
|
||
const TCodiceIVA i(zanicchi);
|
||
imposta = i.scorpora(imponibile); // ... scorpora imposta dall'imponibile
|
||
}
|
||
|
||
TBill conto;
|
||
const char t = cur.get_char(RMI_TIPOC);
|
||
const int gr = cur.get_int(RMI_GRUPPO);
|
||
const int co = cur.get_int(RMI_CONTO);
|
||
const long so = cur.get_long(RMI_SOTTOCONTO);
|
||
const TString80 codcms = cur.get(RMI_CODCMS);
|
||
const TString80 fascms = cur.get(RMI_FASCMS);
|
||
conto.set(gr,co,so,t);
|
||
|
||
int newpos = bill2pos(conto, codcms, fascms, 'I'); // Riga in cui andra' l'imponibile
|
||
|
||
const bool detrarre = detraibile(cur); // Determina se IVA detraibile
|
||
|
||
// Calcola riga causale col conto opportuno
|
||
const int ri = detrarre ? RIGA_IVA_DETRAIBILE : RIGA_IVA_NON_DETRAIBILE;
|
||
TBill contoiva; _caus->bill(ri, contoiva);
|
||
|
||
if (ri == RIGA_IVA_NON_DETRAIBILE && !contoiva.ok()) // Se non c'e' il conto IVA indetraibile ...
|
||
{ // ... somma imponibile e imposta
|
||
imponibile += imposta;
|
||
imposta = 0.0;
|
||
}
|
||
|
||
// Aggiorna conto sulla riga contabile
|
||
if (newpos < 0) // conto non esistente: da inserire
|
||
{
|
||
const TImporto val(real2imp(imponibile, 'I'));
|
||
if (conto.ok() && !val.is_zero()) // Se c'e' imponibile ...
|
||
{ // crea una nuova riga contabile
|
||
const TString d(_caus->desc_agg(2));
|
||
set_cg_rec(-1, val, conto, codcms, fascms, d.empty() ? descr_cr : d, 'I');
|
||
}
|
||
}
|
||
else
|
||
{
|
||
TImporto val(real2imp(imponibile, 'I'));
|
||
add_cg_rec(newpos, val);
|
||
}
|
||
cur.put(RMI_RIGAIMP, newpos+1); // Aggiorna riferimento alla riga contabile
|
||
|
||
// Aggiorna conto IVA sulla riga contabile
|
||
|
||
const char tipod = detrarre ? 'D' : 'N';
|
||
int newposiva = type2pos(tipod);
|
||
|
||
if (newposiva < 0)
|
||
{
|
||
if (!imposta.is_zero()) // Se c'e' imposta ...
|
||
{ // ... crea nuova riga per l'IVA
|
||
const TImporto val(real2imp(imposta, 'I'));
|
||
const TString d(_caus->desc_agg(ri));
|
||
newposiva = set_cg_rec(-1, val, contoiva, "", "", d, tipod);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
const TImporto val(real2imp(imposta, 'I'));
|
||
add_cg_rec(newposiva, val);
|
||
}
|
||
}
|
||
|
||
void TMovimentoPN_VE::add_row_re(int i)
|
||
{
|
||
TRectype& cur = iva(i);
|
||
real oldimp = cur.get_real(RMI_IMPONIBILE);
|
||
real oldiva = cur.get_real(RMI_IMPOSTA);
|
||
const TString80 codcms = cur.get(RMI_CODCMS);
|
||
const TString80 fascms = cur.get(RMI_FASCMS);
|
||
|
||
const char tipod = detraibile(cur) ? 'D' : 'N';
|
||
|
||
TBill c;
|
||
if (tipod == 'N' && !oldiva.is_zero())
|
||
{
|
||
_caus->bill(RIGA_IVA_NON_DETRAIBILE, c);
|
||
if (c.ok())
|
||
{
|
||
int ivapos = bill2pos(c, codcms, fascms, 'I');
|
||
if (ivapos < 0)
|
||
ivapos= set_cg_rec(-1, real2imp(ZERO, 'I'), c, codcms, fascms, "", 'I');
|
||
|
||
const TImporto val(real2imp(oldiva, 'I'));
|
||
|
||
add_cg_rec(ivapos, val);
|
||
}
|
||
else
|
||
oldimp += oldiva;
|
||
}
|
||
|
||
const int gr = cur.get_int(RMI_GRUPPO);
|
||
const int co = cur.get_int(RMI_CONTO);
|
||
const long so = cur.get_long(RMI_SOTTOCONTO);
|
||
c.set(gr,co,so);
|
||
if (c.ok())
|
||
{
|
||
int poscg = bill2pos(c, codcms, fascms, ' ');
|
||
|
||
if (poscg < 0)
|
||
poscg = set_cg_rec(-1, real2imp(ZERO, 'I'), c, codcms, fascms, _caus->desc_agg(2), ' ');
|
||
|
||
TImporto val(real2imp(oldimp, 'I'));
|
||
|
||
add_cg_rec(poscg, val);
|
||
}
|
||
}
|
||
|
||
bool TMovimentoPN_VE::add_row_cp_re(int i)
|
||
{
|
||
TRectype& cur = cg(i);
|
||
bool inserted_row = FALSE;
|
||
|
||
TBill c;
|
||
const int gr = cur.get_int(RMV_GRUPPO);
|
||
const int co = cur.get_int(RMV_CONTO);
|
||
const long so = cur.get_long(RMV_SOTTOCONTO);
|
||
|
||
c.set(gr,co,so);
|
||
TBill cp;
|
||
map_conto_re(c);
|
||
|
||
if (c.ok())
|
||
{
|
||
int poscg = bill2pos(c, "", "", ' ');
|
||
if (poscg < 0)
|
||
{
|
||
poscg = insert_cg_rec(0, real2imp(ZERO, 'P'), c, "", "", _caus->desc_agg(1), ' ');
|
||
inserted_row = TRUE;
|
||
}
|
||
TImporto val(real2imp(cur.get_real(RMV_IMPORTO), 'P'));
|
||
|
||
add_cg_rec(poscg, val);
|
||
}
|
||
return inserted_row;
|
||
}
|
||
|
||
void TMovimentoPN_VE::map_conto_re(TBill& c)
|
||
{
|
||
TString key;
|
||
|
||
key.format("%3d%3d%6ld",c.gruppo(), c.conto(), c.sottoconto());
|
||
TRectype & rs = (TRectype &) cache().get("MRE", key);
|
||
if (rs.empty())
|
||
{
|
||
key.cut(6);
|
||
rs = cache().get("MRE", key);
|
||
if (rs.empty())
|
||
{
|
||
key.cut(3);
|
||
rs = cache().get("MRE", key);
|
||
}
|
||
}
|
||
if (rs.empty())
|
||
_caus->bill(1, c);
|
||
else
|
||
{
|
||
const int gr = rs.get_int("I0");
|
||
const int co = rs.get_int("I1");
|
||
const long so = rs.get_int("I2");
|
||
const char tipo = rs.get_char("S6");
|
||
c.set(gr, co, so, tipo);
|
||
}
|
||
if (c.tipo() > ' ')
|
||
c.set(c.gruppo(), c.conto(), curr().get_long(MOV_CODCF), c.tipo());
|
||
}
|
||
|
||
bool TMovimentoPN_VE::movement_ok()
|
||
{
|
||
TImporto tot_imp;
|
||
TImporto imp;
|
||
|
||
// Se siamo in valuta, forzera' la riga totale documento a cio' che si ottiene dalla somma
|
||
// delle singole righe per evitare sbilanci nel movimento di 2,3,4,5 lire dovuti agli arrotondamenti
|
||
const int max = cg_items();
|
||
for (int i = 0; i < max; i++)
|
||
{
|
||
if (i == 0 && _valuta)
|
||
continue;
|
||
TRectype& r = cg(i);
|
||
const char sez = r.get_char(RMV_SEZIONE);
|
||
const real val(r.get_real(RMV_IMPORTO));
|
||
imp.set(sez,val);
|
||
tot_imp+=imp;
|
||
}
|
||
|
||
if (_valuta)
|
||
{
|
||
TRectype& r = cg(0);
|
||
// Setta la riga di totale documento...
|
||
r.put(RMV_IMPORTO,tot_imp.valore());
|
||
r.put(RMV_SEZIONE,tot_imp.sezione() == 'D' ? 'A' : 'D'); // Sezione contraria
|
||
// ...ed anche il totale documento sulla testata
|
||
lfile().curr().put(MOV_TOTDOC,tot_imp.valore());
|
||
tot_imp.valore() = 0.0;
|
||
if (_caus != NULL && _caus->intra())
|
||
{
|
||
const TString& totdoc = curr().get(MOV_TOTDOC);
|
||
curr().put(MOV_CORRLIRE, totdoc);
|
||
}
|
||
}
|
||
|
||
if (!tot_imp.is_zero())
|
||
return FALSE;
|
||
return TRUE;
|
||
}
|
||
|
||
int TMovimentoPN_VE::recalc_cg_rows(const TString & descr_cr, TCausale & caus)
|
||
{
|
||
const int righe = iva_items();
|
||
TRectype& head = lfile().curr();
|
||
|
||
set_caus(&caus);
|
||
for (int i=0; i<righe; i++)
|
||
{
|
||
create_row(i, descr_cr);
|
||
enter_row(i, descr_cr);
|
||
}
|
||
bool ok = TRUE;
|
||
if (_caus->intra() && _caus->iva() == iva_acquisti)
|
||
{
|
||
TBill c; _caus->bill(RIGA_RITENUTE_FISCALI, c);
|
||
ok = c.ok();
|
||
if (ok)
|
||
{
|
||
const TString80 d(_caus->desc_agg(RIGA_RITENUTE_FISCALI));
|
||
const char rowtype = 'F';
|
||
set_cg_rec(-1, real2imp(head.get_real(MOV_RITFIS), rowtype), c, "","", d, rowtype);
|
||
}
|
||
else
|
||
return 2;
|
||
}
|
||
else
|
||
{
|
||
real ritfis = head.get_real(MOV_RITFIS);
|
||
|
||
if (ritfis != ZERO)
|
||
{
|
||
TBill c; _caus->bill(RIGA_RITENUTE_FISCALI, c);
|
||
ok = c.ok();
|
||
if (ok)
|
||
{
|
||
const TString80 d(_caus->desc_agg(RIGA_RITENUTE_FISCALI));
|
||
const char rowtype = 'F';
|
||
set_cg_rec(-1, real2imp(ritfis, rowtype), c, "", "", d, rowtype);
|
||
}
|
||
else
|
||
return 1;
|
||
}
|
||
|
||
real ritsoc = head.get_real(MOV_RITSOC);
|
||
|
||
if (ritsoc != ZERO)
|
||
{
|
||
TBill c; _caus->bill(RIGA_RITENUTE_SOCIALI, c);
|
||
ok = c.ok();
|
||
if (ok)
|
||
{
|
||
const TString80 d(_caus->desc_agg(RIGA_RITENUTE_SOCIALI));
|
||
const char rowtype = 'S';
|
||
set_cg_rec(-1, real2imp(ritsoc, rowtype), c, "", "", d, rowtype);
|
||
}
|
||
else
|
||
return 1;
|
||
}
|
||
}
|
||
|
||
if (_caus->tipomov() == 1) // Elimina eventuali righe vuote dalle fatture
|
||
{
|
||
for (int c = cg_items()-1; c >= 0; c--)
|
||
{
|
||
const TImporto imp = get_cg_imp(c);
|
||
if (imp.is_zero())
|
||
destroy_cg_row(c);
|
||
}
|
||
}
|
||
|
||
return ok && movement_ok() ? 0 : 1;
|
||
}
|
||
|
||
// Parametri da leggere all'inizio dell'elaborazione tramite load_parameters()
|
||
static TBill _sco_perc_bill, _sco_imp_bill, // Conti per gli sconti a percentuale ed importi (dalla configurazione)
|
||
_spin_billa, _spin_billv,
|
||
_spbo_billa, _spbo_billv;
|
||
static TBill _co_cliente, // conto clifo per movimento d'anticipo
|
||
_co_controp; // conto di contropartita per il movimetno d'anticipo
|
||
|
||
static bool _nump_cfg; // se TRUE prende il numero rif. partita dal numero protocollo
|
||
static bool _sc_enabled; // se TRUE il saldaconto di ditta e' abilitato
|
||
static bool _in_enabled; // se TRUE l'intra e' abilitato
|
||
static TToken_string* _search_seq = NULL;
|
||
// Sequenza di ricerca del conto costo/ricavo la correttezza dell'ordinamento
|
||
// va controllata nel programma di modifica parametri:
|
||
// "" = fine ordinamento
|
||
// CF = cliente fornitore
|
||
// CA = causale
|
||
// AR = articolo (costo/ricavo)
|
||
// GM = gruppo merceologico
|
||
// SM = sottogruppo merceologico
|
||
// RF = raggruppamento fiscale
|
||
// CV = categoria di vendita
|
||
// CC = categoria contabile
|
||
// Gli utlimi 6 fanno parte della ricerca per costi ricavi, in particolare AR,GM,SM e RF
|
||
// non possono essere interrotti da CV o CC. Ad es. CA|CF|AR|CV|GM|CC|RF non e' valida come stringa
|
||
// di ricerca.
|
||
static TString16 _ivasto; // Codice IVA per storno articoli Omaggio
|
||
static bool _contsclor; // Contabilizza sconti al netto o al lordo (sconti suddiviso per ogni contropartita)
|
||
//static bool _loaded = FALSE;// Flag per evitare di caricare i parametri pi<70> di una volta
|
||
|
||
static TEsercizi_contabili _esc; // Per sapere a quale esercizio appartiene il documento
|
||
static TCausale *_caus = NULL; // causale del documento corrente
|
||
static TMovimentoPN_VE *_movimento = NULL; // Movimento di prima nota documento vendita
|
||
static TMovimentoPN *_anticipo = NULL; // Movimento di prima nota relativamente all'anticipo indicato sul documento
|
||
|
||
class TIVA_array : public TAssoc_array
|
||
{
|
||
TCausale * _caus; // causale del documento corrente
|
||
|
||
protected:
|
||
void copy(const TIVA_array& a);
|
||
TObject* dup() const { return new TIVA_array(*this); }
|
||
|
||
public:
|
||
error_type add(const TRiga_documento & r, const TBill& conto, const int ndec = ALL_DECIMALS, const real & p = 1.0);
|
||
error_type add(const TRiga_documento * r, const TBill& conto, const int ndec = ALL_DECIMALS, const real & p = 1.0) { return add(*r, conto, ndec, p);}
|
||
error_type add_omaggi(const TRiga_documento & r, const TBill& conto, const int ndec = ALL_DECIMALS, const real & p = 1.0);
|
||
error_type add_omaggi(const TRiga_documento * r, const TBill& conto, const int ndec = ALL_DECIMALS, const real & p = 1.0) { return add_omaggi(*r, conto, ndec, p);}
|
||
void set_caus(TCausale * caus) { _caus = caus; }
|
||
|
||
TIVA_array() {}
|
||
// @cmember Costruttore. Copia tutto l'array associativo e ne duplica gli elementi
|
||
TIVA_array(const TIVA_array& a) { copy(a); }
|
||
virtual ~TIVA_array() {}
|
||
};
|
||
|
||
void TIVA_array::copy(const TIVA_array & a)
|
||
{
|
||
TAssoc_array::copy(a);
|
||
_caus = a._caus;
|
||
}
|
||
|
||
error_type TIVA_array::add_omaggi(const TRiga_documento & r, const TBill& conto, const int ndec, const real & p)
|
||
{
|
||
if (_ivasto.empty())
|
||
return ivasto_error;
|
||
add(r, conto, ndec, p);
|
||
TRiga_documento r_storno(r);
|
||
|
||
r_storno.set_tipo("01");
|
||
r_storno.put(RDOC_CODIVA, _ivasto);
|
||
real prezzo = r_storno.get(RDOC_PREZZO);
|
||
prezzo = -prezzo;
|
||
r_storno.put(RDOC_PREZZO, prezzo);
|
||
add(r_storno, conto, ndec, p);
|
||
return no_error;
|
||
}
|
||
|
||
error_type TIVA_array::add(const TRiga_documento & r, const TBill& conto, const int ndec, const real & p)
|
||
{
|
||
const TTipo_riga_documento & t = r.tipo();
|
||
TString80 key;
|
||
const TCodiceIVA& tiva = r.iva();
|
||
TString16 cod(tiva.codice());
|
||
TBill c(conto);
|
||
char tipo = c.tipo();
|
||
int gr = c.gruppo();
|
||
int co = c.conto();
|
||
long so = c.sottoconto();
|
||
int ord = 0;
|
||
TString80 codcms;
|
||
TString16 fascms;
|
||
int detr = 0;
|
||
real pind = ZERO;
|
||
real impon;
|
||
const bool sconto_lordo = t.tipo() != RIGA_SCONTI && _contsclor && _sco_perc_bill.ok();
|
||
|
||
const TRectype * rdoc = r.find_original_rdoc();
|
||
|
||
if (rdoc != NULL && rdoc->get(RDOC_PROVV).not_empty())
|
||
{
|
||
TString80 key(rdoc->get(RDOC_CODNUM ));
|
||
|
||
const TCodice_numerazione & num = TDocumento::codice_numerazione(key);
|
||
if (num.fattura_emettere_ricevere())
|
||
{
|
||
key.format("%3d%3d%6ld", gr, co, so);
|
||
TRectype & rs = (TRectype &) cache().get("MRE", key);
|
||
if (rs.empty())
|
||
{
|
||
key.cut(6);
|
||
rs = cache().get("MRE", key);
|
||
if (rs.empty())
|
||
{
|
||
key.cut(3);
|
||
rs = cache().get("MRE", key);
|
||
}
|
||
}
|
||
if (rs.empty())
|
||
{
|
||
key = rdoc->get(RDOC_PROVV);
|
||
key << '|' << rdoc->get(RDOC_ANNO);
|
||
key << '|' << rdoc->get(RDOC_CODNUM);
|
||
key << '|' << rdoc->get(RDOC_NDOC);
|
||
|
||
const TRectype & doc = cache().get(LF_DOC, key);
|
||
const TTipo_documento & td = TDocumento::tipo(doc.get(DOC_TIPODOC));
|
||
const TString16 codcau(td.causale());
|
||
TCausale caus(codcau);
|
||
|
||
caus.bill(1, c);
|
||
gr = c.gruppo();
|
||
co = c.conto();
|
||
so = c.sottoconto();
|
||
}
|
||
else
|
||
{
|
||
gr = rs.get_int("I0");
|
||
co = rs.get_int("I1");
|
||
so = rs.get_int("I2");
|
||
tipo = rs.get_char("S6");
|
||
c.set(gr, co, so, tipo);
|
||
}
|
||
if (c.tipo() > ' ')
|
||
c.set(c.gruppo(), c.conto(), _movimento->curr().get_long(MOV_CODCF), c.tipo());
|
||
}
|
||
}
|
||
if (!sconto_lordo) // Al netto dello sconto
|
||
impon = r.imponibile();
|
||
else
|
||
impon = r.importo(FALSE,FALSE,ndec); // Imponibile della riga al lordo dello sconto
|
||
|
||
switch (t.tipo())
|
||
{
|
||
case RIGA_MERCE:
|
||
ord = 1;
|
||
break;
|
||
case RIGA_OMAGGI:
|
||
impon = r.imponibile_omaggio();
|
||
ord = 2;
|
||
break;
|
||
case RIGA_SPESEDOC:
|
||
ord = 3;
|
||
break;
|
||
case RIGA_PRESTAZIONI:
|
||
ord = 4;
|
||
break;
|
||
case RIGA_SCONTI:
|
||
ord = 5;
|
||
break;
|
||
case RIGA_DESCRIZIONI:
|
||
default:
|
||
return no_error;
|
||
break;
|
||
}
|
||
|
||
impon *= p;
|
||
impon.round(ndec);
|
||
|
||
if (impon.is_zero())
|
||
return no_error;
|
||
|
||
real imposta = tiva.imposta(impon, ndec);
|
||
|
||
if (main_app().has_module(CMAUT, CHK_DONGLE))
|
||
{
|
||
codcms = r.codice_commessa();
|
||
fascms = r.fase_commessa();
|
||
}
|
||
if (_caus->iva() == iva_acquisti || _caus->iva() == nessuna_iva)
|
||
{
|
||
detr = t.detraibilita();
|
||
pind = t.perc_indetraibilita();
|
||
|
||
if (codcms.not_empty())
|
||
{
|
||
const TRectype & rec = cache().get("CMS", codcms);
|
||
|
||
if (rec.get("S7") == "NR")
|
||
{
|
||
detr = 9;
|
||
pind = 100.0;
|
||
}
|
||
}
|
||
}
|
||
|
||
real impres = (impon * pind) / 100.0;
|
||
real ivares = (imposta * pind) / 100.0;
|
||
|
||
impres.round(ndec);
|
||
ivares.round(ndec);
|
||
|
||
// Le righe di sconto ad importo o percentuale vanno saltate
|
||
// Casistica sulle righe omaggio:
|
||
// quelle che non hanno addebito IVA devono venire scartate, quelle che hanno
|
||
// addebito IVA vengono aggiunte normalmente ed in piu' viene aggiunta
|
||
// una riga IVA con lo stesso imponibile ma di segno opposto, con un cod. IVA
|
||
// speciale per lo storno, proveniente da configurazione
|
||
|
||
if (pind < 100.0)
|
||
{
|
||
key.format("%d|%-4s|%c|%3d|%3d|%6ld|%s|%s|%d",ord,(const char*)cod,tipo,gr,co,so, (const char *)codcms, (const char *)fascms, 0);
|
||
TRectype * iva = (TRectype *) objptr(key);
|
||
|
||
if (iva == NULL)
|
||
{
|
||
iva = new TRectype(LF_RMOVIVA);
|
||
iva->put(RMI_CODIVA,cod);
|
||
iva->put(RMI_TIPOCR,c.tipo_cr());
|
||
iva->put(RMI_INTRA, _caus->intra());
|
||
iva->put(RMI_TIPOC, c.tipo());
|
||
iva->put(RMI_GRUPPO, c.gruppo());
|
||
iva->put(RMI_CONTO, c.conto());
|
||
iva->put(RMI_SOTTOCONTO, c.sottoconto());
|
||
iva->put(RMI_CODCMS, codcms);
|
||
iva->put(RMI_FASCMS, fascms);
|
||
TAssoc_array::add(key, iva, TRUE);
|
||
}
|
||
real val = iva->get_real(RMI_IMPONIBILE);
|
||
|
||
val += (impon - impres);
|
||
iva->put(RMI_IMPONIBILE,val);
|
||
val = iva->get_real(RMI_IMPOSTA);
|
||
val += (imposta - ivares);
|
||
iva->put(RMI_IMPOSTA, val);
|
||
|
||
}
|
||
if (pind > ZERO)
|
||
{
|
||
key.format("%d|%-4s|%c|%3d|%3d|%6ld|%s|%s|%d",ord,(const char*)cod,tipo,gr,co,so, (const char *)codcms, (const char *)fascms, detr);
|
||
TRectype * iva = (TRectype *) objptr(key);
|
||
|
||
if (iva == NULL)
|
||
{
|
||
iva = new TRectype(LF_RMOVIVA);
|
||
iva->put(RMI_CODIVA,cod);
|
||
iva->put(RMI_TIPOCR,c.tipo_cr());
|
||
iva->put(RMI_INTRA, _caus->intra());
|
||
iva->put(RMI_TIPOC, c.tipo());
|
||
iva->put(RMI_GRUPPO, c.gruppo());
|
||
iva->put(RMI_CONTO, c.conto());
|
||
iva->put(RMI_SOTTOCONTO, c.sottoconto());
|
||
iva->put(RMI_CODCMS, codcms);
|
||
iva->put(RMI_FASCMS, fascms);
|
||
iva->put(RMI_TIPODET, detr);
|
||
TAssoc_array::add(key, iva, TRUE);
|
||
}
|
||
real val = iva->get_real(RMI_IMPONIBILE);
|
||
|
||
val += impres;
|
||
iva->put(RMI_IMPONIBILE,val);
|
||
val = iva->get_real(RMI_IMPOSTA);
|
||
val += ivares;
|
||
iva->put(RMI_IMPOSTA, val);
|
||
}
|
||
if (ord != 5)
|
||
{
|
||
if (sconto_lordo) // Se e' settato il flag di contabilizzare anche gli sconti merce
|
||
{
|
||
real sconto;
|
||
|
||
sconto = - r.sconto(); // Imponibile dello sconto (positivo, quindi si cambia di segno)
|
||
|
||
if (sconto != ZERO) // Le righe Omaggio con Addebito IVA hanno comunque sconto ZERO!
|
||
{
|
||
TRiga_documento r_sconto(r);
|
||
r_sconto.put(RDOC_QTA, "1.00");
|
||
r_sconto.put(RDOC_PREZZO, sconto);
|
||
add(r_sconto, _sco_perc_bill, ndec, p);
|
||
}
|
||
}
|
||
}
|
||
return no_error;
|
||
}
|
||
|
||
|
||
TContabilizzazione::TContabilizzazione(const char* cod)
|
||
: TElaborazione(cod), _auto_data(FALSE)
|
||
{
|
||
_fcaus = new TLocalisamfile(LF_CAUSALI);
|
||
_frcaus = new TLocalisamfile(LF_RCAUSALI);// Per far andare TCausale
|
||
_attiv = new TLocalisamfile(LF_ATTIV); // Altrimenti TRegistro non va!
|
||
_part = new TLocalisamfile(LF_PARTITE);
|
||
_scad = new TLocalisamfile(LF_SCADENZE);
|
||
_pags = new TLocalisamfile(LF_PAGSCA); // Per far funzionare TPartita
|
||
_intra = new TLocalisamfile(LF_INTRA);
|
||
_rintra = new TLocalisamfile(LF_RINTRA);
|
||
_occas = new TLocalisamfile(LF_OCCAS);
|
||
_docfile = new TLocalisamfile(LF_DOC);
|
||
_rdocfile = new TLocalisamfile(LF_RIGHEDOC); // Per far funzionare TDocumento,TPartita ecc..
|
||
_anamag = new TLocalisamfile(LF_ANAMAG);
|
||
_saldi = new TLocalisamfile(LF_SALDI);
|
||
|
||
_cpg = new TTable("%CPG");
|
||
_tri = new TTable("%TRI");
|
||
_val = new TTable("%VAL");
|
||
_prs = new TTable("PRS");
|
||
_spp = new TTable("SPP");
|
||
_caa = new TTable("CAA");
|
||
_cra = new TTable("CRA");
|
||
_gmc = new TTable("GMC");
|
||
_rfa = new TTable("RFA");
|
||
_cve = new TTable("CVE");
|
||
_cco = new TTable("CCO");
|
||
_clifo = new TRelation(LF_CLIFO);
|
||
_clifo->add(LF_CFVEN,"TIPOCF=TIPOCF|CODCF=CODCF");
|
||
_righe_iva = new TIVA_array;
|
||
|
||
_nump_iva = get_bool("B4");
|
||
_can_write = TRUE;
|
||
_error = no_error;
|
||
_total_docs = 0L;
|
||
_caus = NULL;
|
||
}
|
||
|
||
TContabilizzazione::TContabilizzazione(const TRectype& rec)
|
||
: TElaborazione(rec), _auto_data(FALSE), _viswin(NULL)
|
||
{
|
||
_fcaus = new TLocalisamfile(LF_CAUSALI);
|
||
_frcaus = new TLocalisamfile(LF_RCAUSALI);// Per far andare TCausale
|
||
_attiv = new TLocalisamfile(LF_ATTIV); // Altrimenti TRegistro non va!
|
||
_part = new TLocalisamfile(LF_PARTITE);
|
||
_scad = new TLocalisamfile(LF_SCADENZE);
|
||
_pags = new TLocalisamfile(LF_PAGSCA); // Per far funzionare TPartita
|
||
_intra = new TLocalisamfile(LF_INTRA);
|
||
_rintra = new TLocalisamfile(LF_RINTRA);
|
||
_occas = new TLocalisamfile(LF_OCCAS);
|
||
_docfile = new TLocalisamfile(LF_DOC);
|
||
_rdocfile = new TLocalisamfile(LF_RIGHEDOC); // Per far funzionare TDocumento,TPartita ecc..
|
||
_anamag = new TLocalisamfile(LF_ANAMAG);
|
||
_saldi = new TLocalisamfile(LF_SALDI);
|
||
|
||
_cpg = new TTable("%CPG");
|
||
_tri = new TTable("%TRI");
|
||
_val = new TTable("%VAL");
|
||
_prs = new TTable("PRS");
|
||
_spp = new TTable("SPP");
|
||
_caa = new TTable("CAA");
|
||
_cra = new TTable("CRA");
|
||
_gmc = new TTable("GMC");
|
||
_rfa = new TTable("RFA");
|
||
_cve = new TTable("CVE");
|
||
_cco = new TTable("CCO");
|
||
_clifo = new TRelation(LF_CLIFO);
|
||
_clifo->add(LF_CFVEN,"TIPOCF=TIPOCF|CODCF=CODCF");
|
||
_righe_iva = new TIVA_array;
|
||
|
||
_nump_iva = get_bool("B4");
|
||
|
||
_can_write = TRUE;
|
||
_error = no_error;
|
||
_total_docs = 0L;
|
||
_caus = NULL;
|
||
}
|
||
|
||
TContabilizzazione::~TContabilizzazione()
|
||
{
|
||
delete _clifo;
|
||
delete _cpg;
|
||
delete _tri;
|
||
delete _val;
|
||
delete _gmc;
|
||
delete _rfa;
|
||
delete _cve;
|
||
delete _cco;
|
||
delete _prs;
|
||
delete _spp;
|
||
delete _caa;
|
||
delete _cra;
|
||
delete _anamag;
|
||
delete _saldi;
|
||
delete _fcaus;
|
||
delete _frcaus;
|
||
delete _attiv;
|
||
delete _part;
|
||
delete _scad;
|
||
delete _pags;
|
||
delete _intra;
|
||
delete _rintra;
|
||
delete _occas;
|
||
delete _docfile;
|
||
delete _rdocfile;
|
||
delete _righe_iva;
|
||
}
|
||
|
||
bool TContabilizzazione::load_parameters()
|
||
{
|
||
// if (_loaded)
|
||
// return TRUE;
|
||
|
||
TConfig conf(CONFIG_DITTA, "ve");
|
||
|
||
_search_seq = new TToken_string((const char*)conf.get("RICERCACR","ve"));
|
||
|
||
// costruisce la stringa che controlla la ricerca del conto costo/ricavo
|
||
// Attenzione! non esegue alcun controllo di consistenza sulla corretta sequenza
|
||
// presuppone che il programma di configurazione abbia generato correttamente
|
||
// il tutto.
|
||
|
||
if (_search_seq->items() == 0)
|
||
{
|
||
error_box("Non e' abilitata alcuna ricerca per il conto di costo/ricavo in configurazione.");
|
||
return FALSE;
|
||
}
|
||
_sc_enabled = conf.get_bool("GesSal","cg");
|
||
_in_enabled = main_app().has_module(INAUT, CHK_DONGLE);
|
||
_nump_cfg = conf.get_bool("RifPro","cg");
|
||
|
||
int gr,co;
|
||
long so;
|
||
|
||
gr = conf.get_int("SCOPRCODCON","ve",1);
|
||
co = conf.get_int("SCOPRCODCON","ve",2);
|
||
so = conf.get_long("SCOPRCODCON","ve",3);
|
||
_sco_perc_bill.set(gr,co,so);
|
||
|
||
gr = conf.get_int("SCOIMCODCON","ve",1);
|
||
co = conf.get_int("SCOIMCODCON","ve",2);
|
||
so = conf.get_long("SCOIMCODCON","ve",3);
|
||
_sco_imp_bill.set(gr,co,so);
|
||
|
||
_contsclor = conf.get_bool("CONTSCLOR","ve");
|
||
|
||
_ivasto = conf.get("IVASTO","ve");
|
||
|
||
gr = conf.get_int("SPINCODCONA","ve",1);
|
||
co = conf.get_int("SPINCODCONA","ve",2);
|
||
so = conf.get_long("SPINCODCONA","ve",3);
|
||
_spin_billa.set(gr,co,so);
|
||
|
||
gr = conf.get_int("SPINCODCONV","ve",1);
|
||
co = conf.get_int("SPINCODCONV","ve",2);
|
||
so = conf.get_long("SPINCODCONV","ve",3);
|
||
_spin_billv.set(gr,co,so);
|
||
|
||
gr = conf.get_int("SPBOCODCONA","ve",1);
|
||
co = conf.get_int("SPBOCODCONA","ve",2);
|
||
so = conf.get_long("SPBOCODCONA","ve",3);
|
||
_spbo_billa.set(gr,co,so);
|
||
|
||
gr = conf.get_int("SPBOCODCONV","ve",1);
|
||
co = conf.get_int("SPBOCODCONV","ve",2);
|
||
so = conf.get_long("SPBOCODCONV","ve",3);
|
||
_spbo_billv.set(gr,co,so);
|
||
|
||
_spin_cod = conf.get("SPINCODIVA","ve");
|
||
_spbo_cod = conf.get("SPBOCODIVA","ve");
|
||
|
||
_check_prev_cont = conf.get_bool("CHECKPREVCONT","ve");
|
||
|
||
return TRUE;
|
||
}
|
||
|
||
bool TContabilizzazione::test_swap()
|
||
{
|
||
const char sez = _caus->sezione_clifo();
|
||
const bool s = (_caus->reg().iva() == iva_vendite) ^ (sez == 'D');
|
||
return s;
|
||
}
|
||
|
||
error_type TContabilizzazione::get_next_reg_num(long& nr)
|
||
{
|
||
// reperisce l'ultimo numero di registrazione disponibile
|
||
_error = no_error;
|
||
TLocalisamfile& mov = _movimento->lfile();
|
||
nr = 1L;
|
||
if (mov.last() == NOERR)
|
||
{
|
||
nr += mov.get_long(MOV_NUMREG);
|
||
if (nr <= 0) // Quando mai succede?
|
||
_error = nr_reg_error;
|
||
}
|
||
return _error;
|
||
}
|
||
|
||
long TContabilizzazione::doc_contabilized(const TDocumento& doc, bool anticipo) const
|
||
{
|
||
long nreg = doc.get_long(anticipo ? DOC_NUMANT : DOC_NUMREG);
|
||
if (nreg > 0)
|
||
{
|
||
const TRectype& mov = cache().get(LF_MOV, nreg);
|
||
if (!mov.empty()) // Il movimento esiste!
|
||
{
|
||
// Controlla che il movimento abbia ancora il riferimento esatto al documento
|
||
const char* const key[4] = { DOC_PROVV, DOC_ANNO, DOC_CODNUM, DOC_NDOC };
|
||
TString8 dkey, dval;
|
||
for (int k = 0; k < 4; k++)
|
||
{
|
||
dkey = "D"; dkey << key[k];
|
||
dval = mov.get(dkey);
|
||
if (dval != doc.get(key[k]))
|
||
{
|
||
nreg = 0; // La chiave non corrisponde pi<70>: forse l'hanno cancellato e reinserito
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
else
|
||
nreg = 0; // Il movimento non esiste pi<70>: l'hanno cancellato!
|
||
}
|
||
return nreg;
|
||
}
|
||
|
||
error_type TContabilizzazione::compile_head_mov(TDocumento& doc)
|
||
// Compila la testata
|
||
{
|
||
TRectype& mov_rec = _movimento->curr();
|
||
|
||
const bool acquisto = doc.get_char(DOC_TIPOCF) == 'F';
|
||
/* Richiesta ritirata il 13-01-2000
|
||
if (acquisto)
|
||
{
|
||
TDate datarif(doc.get(DOC_DATADOCRIF));
|
||
if (datarif.ok())
|
||
datadoc = datarif;
|
||
}
|
||
*/
|
||
// Reperisce la data documento
|
||
const TDate datadoc(doc.data());
|
||
if (!datadoc.ok())
|
||
{
|
||
_error = datadoc_error;
|
||
return _error;
|
||
}
|
||
|
||
// reperisce la data di registrazione, che e' anche la data di competenza ed
|
||
// eventualmente la data74ter se nel registro della causale vi e' scritta l'informazione
|
||
// sulle agenzie di viaggio.
|
||
// se si e' specificata la data automatica prende invece la data del documento
|
||
const TDate data_reg(_auto_data ? datadoc : _data_reg);
|
||
|
||
// reperisce il codice anno esercizio,
|
||
const int cod_es = _esc.date2esc(data_reg);
|
||
if (cod_es <= 0)
|
||
{
|
||
_error = nr_es_error;
|
||
return _error;
|
||
}
|
||
|
||
// reperisce l'ultimo numero di registrazione disponibile
|
||
long numreg = doc_contabilized(doc, FALSE);
|
||
const bool recontabilizing = numreg > 0;
|
||
if (recontabilizing)
|
||
{
|
||
const TRectype& mov = cache().get(LF_MOV, numreg);
|
||
TString msg; msg.format("*** Il movimento %ld <20> gi<67> stato ", numreg);
|
||
if (mov.get_bool(MOV_REGST))
|
||
{
|
||
msg << "stampato su bollato";
|
||
_viswin->add_line(msg);
|
||
return no_error;
|
||
}
|
||
if (mov.get_bool(MOV_STAMPATO))
|
||
{
|
||
msg << "stampato sul giornale";
|
||
_viswin->add_line(msg);
|
||
return no_error;
|
||
}
|
||
if (mov.get_bool(MOV_INVIATO))
|
||
{
|
||
msg << "inviato ad altra contabilit<69>";
|
||
_viswin->add_line(msg);
|
||
return no_error;
|
||
}
|
||
msg.format("--- Il documento verr<72> ricontabilizzato nel movimento %ld", numreg);
|
||
_viswin->add_line(msg);
|
||
}
|
||
else
|
||
{
|
||
_error = get_next_reg_num(numreg);
|
||
if (_error != no_error)
|
||
return _error;
|
||
}
|
||
|
||
const TCodice_numerazione cod_num(doc.numerazione());
|
||
// calcola il numero documento aggiungendo l'eventuale prefisso/postfisso.
|
||
TString numdoc;
|
||
cod_num.complete_num(doc.numero(), numdoc);
|
||
if (acquisto)
|
||
{
|
||
TString16 numdocrif(doc.get(DOC_NUMDOCRIF));
|
||
if (numdocrif.not_empty())
|
||
numdoc = numdocrif;
|
||
}
|
||
|
||
if (numdoc.empty() || !cod_num.ok())
|
||
{
|
||
_error = nr_doc_error;
|
||
return _error;
|
||
}
|
||
numdoc.upper(); // Il numero documento e' uppercase!
|
||
|
||
// Istanzia la causale del documento corrente...
|
||
const TTipo_documento& tipo = doc.tipo();
|
||
TString16 codcaus(tipo.causale());
|
||
|
||
TToken_string key;
|
||
key.add(doc.get(DOC_TIPOCF));
|
||
key.add(doc.get(DOC_CODCF));
|
||
|
||
const TRectype & cfven = cache().get(LF_CFVEN, key);
|
||
const TString16 caus_cli(cfven.get(CFV_CODCAUS));
|
||
|
||
if (caus_cli.not_empty())
|
||
codcaus = caus_cli;
|
||
|
||
_caus = new TCausale(codcaus,data_reg.year());
|
||
_righe_iva->set_caus(_caus);
|
||
if (!_caus->ok())
|
||
{
|
||
_error = caus_error;
|
||
return _error;
|
||
}
|
||
|
||
if (doc.in_valuta() && !_caus->valuta())
|
||
{
|
||
_error = cauval_error;
|
||
return _error;
|
||
}
|
||
|
||
// per reperire il tipo documento ed il tipo movimento
|
||
// reperisce la descrizione dal tipo documento e la completa con la data documento ed il
|
||
// numero documento
|
||
TString descr; doc.riferimento(descr);
|
||
if (descr.empty()) descr = tipo.descrizione();
|
||
|
||
const TString16 rif = doc.get(DOC_NUMDOCRIF);
|
||
const bool use_rif = _caus->iva() == iva_acquisti && rif.not_empty();
|
||
if (use_rif)
|
||
{
|
||
descr << " n. " << rif;
|
||
const TString & data_rif = doc.get(DOC_DATADOCRIF);
|
||
descr << " del " << data_rif;
|
||
}
|
||
else
|
||
{
|
||
descr << " n. " << doc.numero();
|
||
descr << " del " << datadoc.string();
|
||
}
|
||
|
||
// Codice registro IVA
|
||
TRegistro& registro = _caus->reg();
|
||
if (!registro.ok())
|
||
{
|
||
_error = register_error;
|
||
return _error;
|
||
}
|
||
|
||
long ult_prot = 0;
|
||
if (recontabilizing)
|
||
{
|
||
const TRectype& mov = cache().get(LF_MOV, numreg);
|
||
ult_prot = mov.get_long(MOV_PROTIVA);
|
||
}
|
||
else
|
||
|
||
{
|
||
if (_nump_iva == 1) // Reperisce l'ultimo numero di protocollo dal registro IVA
|
||
{
|
||
ult_prot = registro.protocol() + 1;
|
||
if (ult_prot <= 0)
|
||
{
|
||
_error = ultprot_error;
|
||
return _error;
|
||
}
|
||
}
|
||
else // oppure dal numero di documento
|
||
ult_prot = doc.numero();
|
||
}
|
||
|
||
// Reperisce la valuta
|
||
TDate datacam(doc.get_date(DOC_DATACAMBIO));
|
||
real cambio(doc.cambio());
|
||
TString16 codval(doc.valuta());
|
||
codval.trim();
|
||
if (!doc.in_valuta())
|
||
{
|
||
codval = "";
|
||
cambio = ZERO;
|
||
}
|
||
if (codval.not_empty())
|
||
{
|
||
_val->put("CODTAB",codval);
|
||
if (_val->read() != NOERR)
|
||
{
|
||
_error = val_error;
|
||
return _error;
|
||
}
|
||
}
|
||
// Reperisce il cambio
|
||
if ((cambio != ZERO && codval.empty()) ||
|
||
cambio == ZERO && codval.not_empty())
|
||
{
|
||
_error = change_error;
|
||
return _error;
|
||
}
|
||
|
||
// Dati del cliente...
|
||
TString16 tipocf(doc.get(DOC_TIPOCF));
|
||
long codcf = doc.get_long(DOC_CODCF);
|
||
TString80 occas;
|
||
{
|
||
TLocalisamfile& cli_file = _clifo->lfile();
|
||
cli_file.put(CLI_TIPOCF,tipocf); cli_file.put(CLI_CODCF,codcf);
|
||
if (_clifo->read(_isequal) == NOERR) // posiziona il cliente una volta per tutte
|
||
{
|
||
if (cli_file.get_bool(CLI_OCCAS))
|
||
{
|
||
occas = doc.get(DOC_OCFPI);
|
||
TLocalisamfile ocf(LF_OCCAS);
|
||
ocf.put(OCC_CFPI,occas);
|
||
if (ocf.read() != NOERR)
|
||
{
|
||
_error = clifo_error;
|
||
return _error;
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
_error = clifo_error;
|
||
return _error;
|
||
}
|
||
}
|
||
|
||
// Codice pagamento
|
||
TString16 codpag(doc.get(DOC_CODPAG));
|
||
if (sc_enabled() || codpag.not_empty()) // La condizione di pagamento va controllata
|
||
{ // se e' abilitato il saldaconto o se e' stata inserita
|
||
_cpg->put("CODTAB",codpag);
|
||
if (_cpg->read() != NOERR)
|
||
{
|
||
_error = codpag_error;
|
||
return _error;
|
||
}
|
||
}
|
||
// Mo' riempie il record della incornata (testata)
|
||
mov_rec.zero();
|
||
mov_rec.put(MOV_ANNOES,cod_es); mov_rec.put(MOV_NUMREG,numreg);
|
||
mov_rec.put(MOV_DATAREG,data_reg); mov_rec.put(MOV_DATACOMP,data_reg);
|
||
if (use_rif)
|
||
{
|
||
mov_rec.put(MOV_NUMDOC, rif);
|
||
const TString & data_rif = doc.get(DOC_DATADOCRIF);
|
||
mov_rec.put(MOV_DATADOC,data_rif);
|
||
}
|
||
else
|
||
{
|
||
mov_rec.put(MOV_DATADOC,datadoc);
|
||
mov_rec.put(MOV_NUMDOC,numdoc);
|
||
}
|
||
mov_rec.put(MOV_TIPODOC,_caus->tipo_doc());
|
||
if (registro.agenzia_viaggi())
|
||
mov_rec.put(MOV_DATA74TER,data_reg);
|
||
mov_rec.put(MOV_CODCAUS,_caus->codice()); mov_rec.put(MOV_DESCR,descr);
|
||
mov_rec.put(MOV_TIPOMOV,char(_caus->tipomov()+'0')); mov_rec.put(MOV_ANNOIVA,data_reg.year());
|
||
mov_rec.put(MOV_REG,registro.name());
|
||
mov_rec.put(MOV_PROTIVA,ult_prot);
|
||
mov_rec.put(MOV_CODVAL,codval);
|
||
mov_rec.put(MOV_CAMBIO,cambio); mov_rec.put(MOV_TIPO,tipocf);
|
||
mov_rec.put(MOV_DATACAM,datacam);
|
||
mov_rec.put(MOV_CODCF,codcf); mov_rec.put(MOV_OCFPI,occas);
|
||
mov_rec.put(MOV_CODPAG,codpag);
|
||
TCurrency_documento totdocval(doc.totale_doc(), doc);
|
||
if (_caus->iva() == iva_acquisti)
|
||
{
|
||
const TString16 tdoc_cont(doc.tipo().totale_doc_cont());
|
||
if (tdoc_cont.not_empty())
|
||
totdocval.set_num(doc.get_real(tdoc_cont));
|
||
|
||
if (_caus->intra())
|
||
{
|
||
TCurrency_documento imposta(doc.imposta(), doc);
|
||
totdocval -= imposta;
|
||
|
||
imposta.change_to_firm_val();
|
||
mov_rec.put(MOV_RITFIS, imposta.get_num());
|
||
|
||
TString16 codvali(_clifo->lfile().get("VALINTRA"));
|
||
real cambioi = cambio;
|
||
if (codvali.not_empty() && codvali != codval)
|
||
{
|
||
mov_rec.put(MOV_CODVALI,codvali);
|
||
TExchange c(codvali);
|
||
cambioi = c.get_base_change();
|
||
mov_rec.put(MOV_CAMBIOI, cambioi);
|
||
}
|
||
else
|
||
{
|
||
mov_rec.put(MOV_CODVALI,codval);
|
||
mov_rec.put(MOV_CAMBIOI,cambio);
|
||
codvali = codval;
|
||
}
|
||
TCurrency corrval(totdocval);
|
||
TCurrency corrlire(corrval);
|
||
corrval.change_value(codvali, cambioi);
|
||
corrlire.change_to_firm_val();
|
||
if (::is_true_value(codvali))
|
||
{
|
||
mov_rec.put(MOV_CORRLIRE,corrlire.get_num());
|
||
mov_rec.put(MOV_CORRVALUTA,corrval.get_num());
|
||
}
|
||
else
|
||
mov_rec.put(MOV_CORRLIRE,corrval.get_num());
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (_caus->intra())
|
||
{
|
||
TCurrency corrval(totdocval);
|
||
TCurrency corrlire(corrval);
|
||
|
||
mov_rec.put(MOV_CODVALI,codval);
|
||
mov_rec.put(MOV_CAMBIOI,cambio);
|
||
corrlire.change_to_firm_val();
|
||
if (::is_true_value(codval))
|
||
{
|
||
mov_rec.put(MOV_CORRLIRE,corrlire.get_num());
|
||
mov_rec.put(MOV_CORRVALUTA,corrval.get_num());
|
||
}
|
||
else
|
||
mov_rec.put(MOV_CORRLIRE,corrval.get_num());
|
||
}
|
||
}
|
||
if (doc.in_valuta())
|
||
{
|
||
TCurrency totdoclit(totdocval);
|
||
totdoclit.change_to_firm_val();
|
||
mov_rec.put(MOV_TOTDOC,totdoclit.get_num());
|
||
mov_rec.put(MOV_TOTDOCVAL,totdocval.get_num());
|
||
}
|
||
else
|
||
mov_rec.put(MOV_TOTDOC,totdocval.get_num());
|
||
|
||
mov_rec.put(MOV_CODCF,codcf);
|
||
// Memorizza il movimento contabile di destinazione!
|
||
doc.put(DOC_NUMREG, numreg);
|
||
// Scrive sulla testata del movimento il numero di documento originale
|
||
mov_rec.put(MOV_DPROVV, doc.get(DOC_PROVV));
|
||
mov_rec.put(MOV_DANNO, doc.get(DOC_ANNO));
|
||
mov_rec.put(MOV_DCODNUM, doc.get(DOC_CODNUM));
|
||
mov_rec.put(MOV_DNDOC, doc.get(DOC_NDOC));
|
||
|
||
return _error;
|
||
}
|
||
|
||
error_type TContabilizzazione::compile_head_mov_re(TDocumento& doc)
|
||
// Compila la testata
|
||
{
|
||
TRectype& mov_rec = _movimento->curr();
|
||
|
||
const bool acquisto = doc.get_char(DOC_TIPOCF) == 'F';
|
||
// Reperisce la data documento
|
||
const TDate datadoc(doc.data());
|
||
if (!datadoc.ok())
|
||
{
|
||
_error = datadoc_error;
|
||
return _error;
|
||
}
|
||
|
||
// reperisce la data di registrazione, che e' anche la data di competenza ed
|
||
// eventualmente la data74ter se nel registro della causale vi e' scritta l'informazione
|
||
// sulle agenzie di viaggio.
|
||
// se si e' specificata la data automatica prende invece la data del documento
|
||
const TDate data_reg(_auto_data ? datadoc : _data_reg);
|
||
|
||
// reperisce il codice anno esercizio,
|
||
const int cod_es = _esc.date2esc(data_reg);
|
||
if (cod_es <= 0)
|
||
{
|
||
_error = nr_es_error;
|
||
return _error;
|
||
}
|
||
|
||
// reperisce l'ultimo numero di registrazione disponibile
|
||
long numreg = doc_contabilized(doc, FALSE);
|
||
const bool recontabilizing = numreg > 0;
|
||
if (recontabilizing)
|
||
{
|
||
const TRectype& mov = cache().get(LF_MOV, numreg);
|
||
TString msg; msg.format("*** Il movimento %ld <20> gi<67> stato ", numreg);
|
||
if (mov.get_bool(MOV_REGST))
|
||
{
|
||
msg << "stampato su bollato";
|
||
_viswin->add_line(msg);
|
||
return no_error;
|
||
}
|
||
if (mov.get_bool(MOV_STAMPATO))
|
||
{
|
||
msg << "stampato sul giornale";
|
||
_viswin->add_line(msg);
|
||
return no_error;
|
||
}
|
||
if (mov.get_bool(MOV_INVIATO))
|
||
{
|
||
msg << "inviato ad altra contabilit<69>";
|
||
_viswin->add_line(msg);
|
||
return no_error;
|
||
}
|
||
msg.format("--- Il documento verr<72> ricontabilizzato nel movimento %ld", numreg);
|
||
_viswin->add_line(msg);
|
||
}
|
||
else
|
||
{
|
||
_error = get_next_reg_num(numreg);
|
||
if (_error != no_error)
|
||
return _error;
|
||
}
|
||
|
||
const TCodice_numerazione cod_num(doc.numerazione());
|
||
// calcola il numero documento aggiungendo l'eventuale prefisso/postfisso.
|
||
TString numdoc;
|
||
cod_num.complete_num(doc.numero(), numdoc);
|
||
if (acquisto)
|
||
{
|
||
TString16 numdocrif(doc.get(DOC_NUMDOCRIF));
|
||
if (numdocrif.not_empty())
|
||
numdoc = numdocrif;
|
||
}
|
||
|
||
if (numdoc.empty() || !cod_num.ok())
|
||
{
|
||
_error = nr_doc_error;
|
||
return _error;
|
||
}
|
||
numdoc.upper(); // Il numero documento e' uppercase!
|
||
|
||
// Istanzia la causale del documento corrente...
|
||
|
||
const TTipo_documento& tipo = doc.tipo();
|
||
const TString16 codcaus(tipo.causale());
|
||
|
||
_caus = new TCausale(codcaus, data_reg.year());
|
||
_righe_iva->set_caus(_caus);
|
||
_movimento->set_caus(_caus);
|
||
if (!_caus->ok() || _caus->iva() != nessuna_iva)
|
||
{
|
||
_error = causre_error;
|
||
return _error;
|
||
}
|
||
|
||
// per reperire il tipo documento ed il tipo movimento
|
||
// reperisce la descrizione dal tipo documento e la completa con la data documento ed il
|
||
// numero documento
|
||
TString descr; doc.riferimento(descr);
|
||
if (descr.empty()) descr = tipo.descrizione();
|
||
|
||
const TString16 rif = doc.get(DOC_NUMDOCRIF);
|
||
const bool use_rif = _caus->iva() == iva_acquisti && rif.not_empty();
|
||
if (use_rif)
|
||
{
|
||
descr << " n. " << rif;
|
||
const TString & data_rif = doc.get(DOC_DATADOCRIF);
|
||
descr << " del " << data_rif;
|
||
}
|
||
else
|
||
{
|
||
descr << " n. " << doc.numero();
|
||
descr << " del " << datadoc.string();
|
||
}
|
||
|
||
// Dati del cliente...
|
||
TString16 tipocf(doc.get(DOC_TIPOCF));
|
||
long codcf = doc.get_long(DOC_CODCF);
|
||
TString80 occas;
|
||
{
|
||
TLocalisamfile& cli_file = _clifo->lfile();
|
||
cli_file.put(CLI_TIPOCF,tipocf); cli_file.put(CLI_CODCF,codcf);
|
||
if (_clifo->read(_isequal) == NOERR) // posiziona il cliente una volta per tutte
|
||
{
|
||
if (cli_file.get_bool(CLI_OCCAS))
|
||
{
|
||
occas = doc.get(DOC_OCFPI);
|
||
TLocalisamfile ocf(LF_OCCAS);
|
||
ocf.put(OCC_CFPI,occas);
|
||
if (ocf.read() != NOERR)
|
||
{
|
||
_error = clifo_error;
|
||
return _error;
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
_error = clifo_error;
|
||
return _error;
|
||
}
|
||
}
|
||
|
||
// Mo' riempie il record della incornata (testata)
|
||
mov_rec.zero();
|
||
mov_rec.put(MOV_ANNOES,cod_es); mov_rec.put(MOV_NUMREG,numreg);
|
||
mov_rec.put(MOV_DATAREG,data_reg); mov_rec.put(MOV_DATACOMP,data_reg);
|
||
if (use_rif)
|
||
{
|
||
mov_rec.put(MOV_NUMDOC, rif);
|
||
const TString & data_rif = doc.get(DOC_DATADOCRIF);
|
||
mov_rec.put(MOV_DATADOC,data_rif);
|
||
}
|
||
else
|
||
{
|
||
mov_rec.put(MOV_DATADOC,datadoc);
|
||
mov_rec.put(MOV_NUMDOC,numdoc);
|
||
}
|
||
mov_rec.put(MOV_CODCAUS,_caus->codice());
|
||
mov_rec.put(MOV_TIPODOC,_caus->tipo_doc());
|
||
mov_rec.put(MOV_DESCR,descr);
|
||
mov_rec.put(MOV_CODCF,codcf);
|
||
|
||
// Memorizza il movimento contabile di destinazione!
|
||
doc.put(DOC_NUMREG, numreg);
|
||
// Scrive sulla testata del movimento il numero di documento originale
|
||
mov_rec.put(MOV_DPROVV, doc.get(DOC_PROVV));
|
||
mov_rec.put(MOV_DANNO, doc.get(DOC_ANNO));
|
||
mov_rec.put(MOV_DCODNUM, doc.get(DOC_CODNUM));
|
||
mov_rec.put(MOV_DNDOC, doc.get(DOC_NDOC));
|
||
|
||
return _error;
|
||
}
|
||
|
||
error_type TContabilizzazione::search_costo_ricavo(TBill& conto, const TRiga_documento& r)
|
||
{
|
||
const int items = _search_seq->items();
|
||
TLocalisamfile & cli_file = _clifo->lfile(); // YES, arriva qui dentro quando la relazione e' gia' posizionata
|
||
const bool is_cli = cli_file.get(CLI_TIPOCF) == "C";
|
||
bool skip_art_related = FALSE;
|
||
bool skip_clifo = _clifo->bad();
|
||
TCodiceIVA codiva(r.get(RDOC_CODIVA));
|
||
const char t = r.tipo().tipo();
|
||
int gr,co;
|
||
long so;
|
||
|
||
switch (t)
|
||
{
|
||
case 'O': // righe omaggio come articoli spiaccicato identico (avranno imponibile 0)
|
||
case 'M': // righe di merce
|
||
{
|
||
// posiziona l'anagrafica sull'articolo specificato sulla ..iga
|
||
_anamag->put(ANAMAG_CODART,r.get(RDOC_CODART));
|
||
if (_anamag->read() != NOERR) // se non trova l'articolo saltera' anche gmc,smc,rfa.
|
||
skip_art_related = TRUE;
|
||
|
||
TString16 tok;
|
||
// Scorre la stringa di ricerca
|
||
for (int i=0;good() && i<items;i++)
|
||
{
|
||
tok = _search_seq->get(i);
|
||
if (tok == "CF")
|
||
{
|
||
if (skip_clifo) continue;
|
||
gr = cli_file.get_int(CLI_GRUPPORIC);
|
||
co = cli_file.get_int(CLI_CONTORIC);
|
||
so = cli_file.get_long(CLI_SOTTOCRIC);
|
||
conto.set(gr,co,so);
|
||
if (conto.ok()) break; // se lo trova esce (tutti != 0)
|
||
}
|
||
else
|
||
if (tok == "CA")
|
||
{
|
||
CHECK(_caus,"Causale documento non valida");
|
||
if (_caus->IVA2bill(codiva,conto)) break; // se lo trova esce
|
||
}
|
||
else
|
||
if (tok == "AR")
|
||
{
|
||
if (skip_art_related) continue;
|
||
gr = _anamag->get_int(is_cli ? ANAMAG_GRUPPOV : ANAMAG_GRUPPOA);
|
||
co = _anamag->get_int(is_cli ? ANAMAG_CONTOV : ANAMAG_CONTOA);
|
||
so = _anamag->get_long(is_cli ? ANAMAG_SOTTOCV : ANAMAG_SOTTOCA);
|
||
conto.set(gr,co,so);
|
||
if (!conto.ok()) // se il conto non c'e' guarda la categoria acquisti/vendite
|
||
{
|
||
TTable *t = is_cli ? _cra : _caa;
|
||
|
||
t->put("CODTAB",_anamag->get(is_cli ? ANAMAG_CATCONV : ANAMAG_CATCONA));
|
||
if (t->read() == NOERR)
|
||
{
|
||
gr = atoi(t->get("S1"));
|
||
co = atoi(t->get("S2"));
|
||
so = atol(t->get("S3"));
|
||
conto.set(gr,co,so);
|
||
}
|
||
}
|
||
if (conto.ok()) break;
|
||
}
|
||
else
|
||
if (tok == "GM" || tok == "SM" || tok == "RF")
|
||
{
|
||
if (skip_art_related) continue;
|
||
const bool is_fis = tok == "RF";
|
||
TTable * tab = is_fis ? _rfa : _gmc;
|
||
TString16 codtab(_anamag->get(is_fis ? ANAMAG_RAGGFIS : ANAMAG_GRMERC));
|
||
if (tok == "GM" && codtab.len() > 3)
|
||
codtab.cut(3); // gli ultimi 2 si riferiscono al sottogruppo.
|
||
tab->put("CODTAB",codtab);
|
||
if (tab->read() == NOERR)
|
||
{
|
||
gr = tab->get_int(is_cli ? "I3" : "I0");
|
||
co = tab->get_int(is_cli ? "I4" : "I1");
|
||
so = tab->get_long(is_cli ? "I5" : "I2");
|
||
conto.set(gr,co,so);
|
||
}
|
||
if (conto.ok()) break;
|
||
}
|
||
else
|
||
if (tok == "CV" || tok == "CC")
|
||
{
|
||
const bool is_cve = tok == "CV";
|
||
if (is_cve && !is_cli) continue; // se e' un fornitore salta questa condizione
|
||
TTable* t = is_cve ? _cve : _cco;
|
||
TString16 cod(is_cve ? r.doc().get(DOC_CATVEN) : EMPTY_STRING);
|
||
if (cod.empty())
|
||
{
|
||
if (skip_clifo) continue; // se non aveva trovato il cliente salta al prossimo
|
||
cod = _clifo->lfile(LF_CFVEN).get(is_cve ? CFV_CATVEN : CFV_CODCATC);
|
||
}
|
||
t->put("CODTAB",cod);
|
||
if (t->read() == NOERR)
|
||
{
|
||
const bool x =(is_cve || is_cli);
|
||
gr = t->get_int(x ? "I3" : "I0");
|
||
co = t->get_int(x ? "I4" : "I1");
|
||
so = t->get_long(x ? "I5": "I2");
|
||
conto.set(gr,co,so);
|
||
}
|
||
if (conto.ok()) break;
|
||
}
|
||
}
|
||
break; // case 'M'
|
||
}
|
||
case 'P': // righe prestazione
|
||
case 'S': // righe spese
|
||
{
|
||
TTable* tab = t == 'P' ? _prs : _spp;
|
||
tab->put("CODTAB",r.get(RDOC_CODART));
|
||
if (tab->read()==NOERR)
|
||
{
|
||
gr = tab->get_int(is_cli ? "I0" : "I3");
|
||
co = tab->get_int(is_cli ? "I1" : "I4");
|
||
so = tab->get_long(is_cli ? "I2" : "I5");
|
||
conto.set(gr,co,so);
|
||
if (!is_cli && !conto.ok())
|
||
{
|
||
gr = r.get_int("QTAGG1");
|
||
co = r.get_int("QTAGG2");
|
||
so = r.get_long("QTAGG3");
|
||
conto.set(gr,co,so);
|
||
}
|
||
}
|
||
|
||
if (!conto.find() && t == 'P') // Cerca il conto nella stringa di ricerca (solo per prestazioni)
|
||
{
|
||
TString16 tok;
|
||
// Scorre la stringa di ricerca ma solo per causale o CLI/FO
|
||
for (int i=0;good() && i<items;i++)
|
||
{
|
||
tok = _search_seq->get(i);
|
||
if (tok == "CF")
|
||
{
|
||
if (skip_clifo) continue;
|
||
gr = cli_file.get_int(CLI_GRUPPORIC);
|
||
co = cli_file.get_int(CLI_CONTORIC);
|
||
so = cli_file.get_long(CLI_SOTTOCRIC);
|
||
conto.set(gr,co,so);
|
||
if (conto.ok()) break;
|
||
}
|
||
else
|
||
if (tok == "CA")
|
||
{
|
||
CHECK(_caus,"Causale documento non valida");
|
||
if (_caus->IVA2bill(codiva,conto)) break;
|
||
}
|
||
}
|
||
}
|
||
break; // case 'P','S'
|
||
}
|
||
case 'C':
|
||
// righe sconti: vengono considerate in adjust_sconto_rows()
|
||
case 'D': // righe descrizioni (saltare)
|
||
default :
|
||
break;
|
||
} // end of switch
|
||
if (good())
|
||
{
|
||
if (!conto.ok() || !conto.find())
|
||
{
|
||
_conto_errato = conto;
|
||
_error = conto_error;
|
||
}
|
||
}
|
||
return _error;
|
||
}
|
||
|
||
void TContabilizzazione::calculate_spese(real& spese, real& sp_iva, int ndec, bool is_incasso, bool is_cli, const TString & codiva_es, const TDocumento & doc)
|
||
{
|
||
const TBill& zio = is_incasso ? (is_cli ? _spin_billv : _spin_billa) : (is_cli ? _spbo_billv : _spbo_billa);
|
||
|
||
if (zio.ok())
|
||
{
|
||
TRiga_documento r((TDocumento *) &doc, "02"); // il tipo riga 02 spese a valore
|
||
r.put(RDOC_QTA, "1.00");
|
||
r.put(RDOC_PREZZO, spese);
|
||
r.put(RDOC_CODIVA, codiva_es);
|
||
_righe_iva->add(r, zio, ndec);
|
||
if (doc.tipo().calcolo_lordo()) // Si ricorda che calcolo_lordo() e fattura_commerciale sono esclusivi.
|
||
{
|
||
// Totalizza per ogni codice iva il lordo
|
||
|
||
if (!_totali_lordi.is_key(codiva_es))
|
||
_totali_lordi.add(codiva_es, new real);
|
||
real& rl = (real&) _totali_lordi[codiva_es];
|
||
real iva = r.imposta(FALSE);
|
||
iva.round(ndec);
|
||
rl += spese + iva;
|
||
}
|
||
}
|
||
else
|
||
_error = spinbo_error;
|
||
}
|
||
|
||
error_type TContabilizzazione::add_spese_inbo(TDocumento& doc, const int ndec)
|
||
// Aggiunge le righe di spese incasso/bolli
|
||
{
|
||
real tot_netto, sp_incasso, sp_bolli;
|
||
real iva_sp_incasso, iva_sp_bolli;
|
||
bool is_cli = doc.get(DOC_TIPOCF) == "C";
|
||
// Aggiunge le spese d'incasso
|
||
tot_netto = doc.totale_netto();
|
||
sp_incasso = doc.spese_incasso(tot_netto,ndec,_netto);
|
||
|
||
|
||
if (sp_incasso != 0.0)
|
||
calculate_spese(sp_incasso,iva_sp_incasso,ndec,TRUE,is_cli, doc.codiva_spese(), doc);
|
||
|
||
// Aggiunge le spese bolli
|
||
tot_netto += sp_incasso + iva_sp_incasso;
|
||
sp_bolli = doc.bolli(tot_netto,ndec, _netto);
|
||
if (sp_bolli != 0)
|
||
calculate_spese(sp_bolli, iva_sp_bolli, ndec, FALSE, is_cli, doc.codiva_bolli(), doc);
|
||
|
||
return _error;
|
||
}
|
||
|
||
// Aggiorna le righe di sconto (importo o a percentuale)
|
||
error_type TContabilizzazione::adjust_sconto_rows(TDocumento& doc)
|
||
{
|
||
TAssoc_array aa(doc.tabella_iva()); // no reference
|
||
TRiepilogo_iva * riep;
|
||
TString16 cod; // Codice IVA corrente
|
||
real sconto;
|
||
const int ndec = doc.decimals();
|
||
|
||
if (!_sco_imp_bill.ok() || !_sco_perc_bill.ok())
|
||
{
|
||
_error = sconto_error;
|
||
return _error;
|
||
}
|
||
|
||
// Scorre tutti gli elementi della tabella IVA del documento (elementi per codice iva)
|
||
for (riep = (TRiepilogo_iva*) aa.first_item(); riep != NULL; riep = (TRiepilogo_iva*) aa.succ_item())
|
||
{
|
||
cod = riep->cod_iva().codice(); // Codice IVA
|
||
for (int i = 0; i < 2; i++) // Ciclo per sconto a percentuale (i == 0) e ad importo (i == 1)
|
||
{
|
||
//Importo sconto (sconto_perc() o sconto_imp())
|
||
//Calcola ::iva() sullo sconto
|
||
//Somma alla riga corrispondente o la crea se non esiste gia'
|
||
//I conti per aggiustare l'iva vengono fatti in adjust_iva_rows()
|
||
const bool perc = i == 0;
|
||
TBill& conto = perc ? _sco_perc_bill : _sco_imp_bill;
|
||
sconto = perc ? -riep->sconto_perc() : -riep->sconto_imp();
|
||
if (sconto != ZERO)
|
||
{
|
||
TRiga_documento r(&doc, "07"); // il tipo riga 02 spese a valore
|
||
r.put(RDOC_QTA, "1.00");
|
||
r.put(RDOC_PREZZO, sconto);
|
||
r.put(RDOC_CODIVA, cod);
|
||
_righe_iva->add(r, conto, ndec);
|
||
if (doc.tipo().calcolo_lordo()) // Si ricorda che calcolo_lordo() e fattura_commerciale() sono esclusivi.
|
||
{
|
||
// Totalizza per ogni codice iva il lordo
|
||
if (!_totali_lordi.is_key(cod))
|
||
_totali_lordi.add(cod, new real);
|
||
real& rl = (real&) _totali_lordi[cod];
|
||
real iva = r.imposta(FALSE);
|
||
iva.round(ndec);
|
||
rl += sconto + iva;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return no_error;
|
||
}
|
||
|
||
static real inc_field(TRectype& rec, const char* field, real diff)
|
||
{
|
||
real val = rec.get_real(field);
|
||
val += diff;
|
||
rec.put(field, val);
|
||
return val;
|
||
}
|
||
|
||
// "Aggiusta" l'imposta sulle righe IVA secondo la tabella interna al documento
|
||
// Tratta anche i documenti in valuta. DI solito si tratta di poche lire.
|
||
error_type TContabilizzazione::adjust_iva_rows(TDocumento& doc)
|
||
{
|
||
TAssoc_array& aa = doc.tabella_iva();
|
||
TRiepilogo_iva * riep;
|
||
const int items = _movimento->iva_items();// Numero di righe IVA
|
||
const bool in_valuta = doc.in_valuta();
|
||
const bool calcolo_lordo = doc.tipo().calcolo_lordo();
|
||
real imponibile;
|
||
const int ndec = TCurrency::get_firm_dec(); // Numero di decimali della valuta di ditta
|
||
const TString16 codval(doc.get(DOC_CODVAL));
|
||
const real cambio = doc.cambio();
|
||
const exchange_type et = doc.get_bool(DOC_CONTROEURO) ? _exchange_contro : _exchange_base;
|
||
// Scorre tutti gli elementi della tabella IVA del documento (elementi per codice iva)
|
||
for (riep = (TRiepilogo_iva*) aa.first_item(); riep != NULL; riep = (TRiepilogo_iva*) aa.succ_item())
|
||
{
|
||
const TCodiceIVA & cod = riep->cod_iva(); // Codice IVA
|
||
const TString8 codiva(cod.codice());
|
||
real iva_g;
|
||
if (calcolo_lordo)
|
||
{
|
||
imponibile = _totali_lordi.is_key(codiva) ? (real&)_totali_lordi[codiva] : ZERO;
|
||
iva_g = cod.scorpora(imponibile, doc.decimals());
|
||
}
|
||
else
|
||
iva_g = riep->imposta();
|
||
|
||
if (in_valuta)
|
||
{
|
||
if (calcolo_lordo)
|
||
{
|
||
TCurrency_documento tot(imponibile + iva_g, doc);
|
||
tot.change_to_firm_val();
|
||
imponibile = tot.get_num();
|
||
iva_g = cod.scorpora(imponibile, ndec);
|
||
}
|
||
else
|
||
{
|
||
/*
|
||
TCurrency_documento iva(iva_g, doc);
|
||
iva.change_to_firm_val();
|
||
iva_g = iva.get_num();
|
||
*/
|
||
TCurrency_documento tot(riep->imponibile() + iva_g, doc);
|
||
tot.change_to_firm_val();
|
||
imponibile = tot.get_num();
|
||
iva_g = cod.scorpora(imponibile, ndec);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (ndec == 0)
|
||
{
|
||
TExchange cam(codval, cambio, et);
|
||
|
||
iva_g *= cam.get_base_change();
|
||
if (iva_g < ZERO)
|
||
iva_g.floor(0);
|
||
else
|
||
iva_g.ceil(0);
|
||
}
|
||
else
|
||
{
|
||
TCurrency_documento iva(iva_g);
|
||
iva.change_to_firm_val();
|
||
iva_g = iva.get_num();
|
||
}
|
||
}
|
||
|
||
TGeneric_distrib gd_iva(iva_g, ndec); // Instanzia il TGeneric_ditrib con la vera Imposta
|
||
TGeneric_distrib gd_imp(imponibile, ndec);
|
||
// Adesso scorre tutte le righe IVA contabili con questo codice IVA
|
||
for (int i = 0; i < items; i++)
|
||
{
|
||
TRectype& ie = _movimento->iva(i);
|
||
if (ie.get(RMI_CODIVA) == codiva) // Se il codice IVA e' uguale
|
||
{
|
||
gd_iva.add(ie.get_real(RMI_IMPOSTA)); // Aggiunge al TGeneric_distrib l'imposta corrente
|
||
if (calcolo_lordo || in_valuta)
|
||
gd_imp.add(ie.get_real(RMI_IMPONIBILE));
|
||
}
|
||
}
|
||
// Alla fine per performare tutto il calcolo (Thanx to TGeneric_distrib) si fanno le get
|
||
// E le si mettono nel rispettivo record IVA
|
||
|
||
for (i = 0; i < items; i++)
|
||
{
|
||
TRectype& ie = _movimento->iva(i);
|
||
if (ie.get(RMI_CODIVA) == codiva) // Se il codice IVA e' uguale
|
||
{
|
||
ie.put(RMI_IMPOSTA,gd_iva.get()); // Sostituisce l'imposta con quella ricalcolata al fine di avere tutto giusto
|
||
if (calcolo_lordo || in_valuta)
|
||
ie.put(RMI_IMPONIBILE, gd_imp.get());
|
||
}
|
||
}
|
||
} // Visto che vengono restituiti nello stesso ordine in cui sono state chiamate le rispettive TGeneric_distrib::add()
|
||
|
||
if (_caus->iva() == iva_acquisti && items > 0)
|
||
{
|
||
const TString16 tdoc_cont(doc.tipo().totale_doc_cont());
|
||
if (tdoc_cont.not_empty())
|
||
{
|
||
real diffval = doc.get_real(tdoc_cont) - doc.totale_doc();
|
||
TCurrency_documento dv(diffval, doc);
|
||
dv.change_to_firm_val();
|
||
real difflit = dv.get_num();
|
||
if (!difflit.is_zero()) // Controlla se c'<27> differenza
|
||
{
|
||
TRectype& rigaiva = _movimento->iva(0);
|
||
|
||
TCodiceIVA iva(rigaiva.get(RMI_CODIVA));
|
||
|
||
if (_caus->intra())
|
||
{
|
||
const real imponibile = inc_field(rigaiva, RMI_IMPONIBILE, difflit);
|
||
const real imposta = iva.imposta(imponibile);
|
||
rigaiva.put(RMI_IMPOSTA, imposta);
|
||
}
|
||
else
|
||
{
|
||
real diffimp = iva.scorpora(difflit);
|
||
inc_field(rigaiva, RMI_IMPONIBILE, difflit);
|
||
inc_field(rigaiva, RMI_IMPOSTA, diffimp);
|
||
}
|
||
}
|
||
if (_caus->intra())
|
||
{
|
||
real ritfis = ZERO;
|
||
for (int i = 0; i < items; i++)
|
||
ritfis += _movimento->iva(i).get_real(RMI_IMPOSTA);
|
||
|
||
_movimento->curr().put(MOV_RITFIS, ritfis);
|
||
}
|
||
}
|
||
}
|
||
|
||
return no_error;
|
||
}
|
||
|
||
error_type TContabilizzazione::create_iva_rows(TDocumento& doc)
|
||
{
|
||
const int items = _righe_iva->items();
|
||
const bool in_valuta = doc.in_valuta();
|
||
TRectype& head = _movimento->curr();
|
||
TToken_string key;
|
||
TString_array key_arr;
|
||
TString16 codiva;
|
||
TBill cur_conto;
|
||
char tipo;
|
||
int gruppo;
|
||
int conto;
|
||
long sottoconto;
|
||
const int annoes = head.get_int(MOV_ANNOES);
|
||
const long numreg = head.get_long(MOV_NUMREG);
|
||
real cambio = head.get_real(MOV_CAMBIO);
|
||
real imponibile,imposta;
|
||
TString16 valuta(doc.valuta());
|
||
|
||
_righe_iva->get_keys(key_arr);
|
||
key_arr.sort();
|
||
|
||
TRectype & cur = (TRectype &) (*_righe_iva)[key_arr.row(0)];
|
||
|
||
for (int i = 0, nr = 0; i < items; i++)
|
||
{
|
||
key = key_arr.row(i);
|
||
cur = (TRectype &) (*_righe_iva)[key];
|
||
codiva = cur.get(RMI_CODIVA);
|
||
tipo = cur.get_char(RMI_TIPOC);
|
||
gruppo = cur.get_int(RMI_GRUPPO);
|
||
conto = cur.get_int(RMI_CONTO);
|
||
sottoconto = cur.get_long(RMI_SOTTOCONTO);
|
||
cur_conto.set(gruppo, conto, sottoconto, tipo);
|
||
cur_conto.find();
|
||
|
||
imponibile = cur.get_real(RMI_IMPONIBILE);
|
||
imposta = cur.get_real(RMI_IMPOSTA);
|
||
|
||
if (in_valuta) // I documenti vanno sempre contabilizzati in lire
|
||
{
|
||
TCurrency_documento imponval(imponibile, doc);
|
||
imponval.change_to_firm_val();
|
||
imponibile = imponval.get_num(); // imponibile in lire
|
||
TCurrency_documento impval(imposta, doc);
|
||
impval.change_to_firm_val();
|
||
|
||
imposta = impval.get_num(); // questa e' l'imposta ricalcolata
|
||
}
|
||
if (imponibile != ZERO && codiva.not_empty())
|
||
{
|
||
TRectype& rec_iva = _movimento->iva(nr);
|
||
rec_iva.put(RMI_ANNOES,annoes);
|
||
rec_iva.put(RMI_NUMREG,numreg);
|
||
rec_iva.put(RMI_NUMRIG,nr+1); // La numerazione comincia da 1
|
||
rec_iva.put(RMI_CODIVA,codiva);
|
||
rec_iva.put(RMI_IMPONIBILE,imponibile);
|
||
rec_iva.put(RMI_IMPOSTA,imposta);
|
||
rec_iva.put(RMI_TIPOCR,cur_conto.tipo_cr());
|
||
rec_iva.put(RMI_INTRA, _caus->intra());
|
||
rec_iva.put(RMI_TIPOC, tipo);
|
||
rec_iva.put(RMI_GRUPPO, gruppo);
|
||
rec_iva.put(RMI_CONTO, conto);
|
||
rec_iva.put(RMI_SOTTOCONTO, sottoconto);
|
||
rec_iva.put(RMI_CODCMS, cur.get(RMI_CODCMS));
|
||
rec_iva.put(RMI_FASCMS, cur.get(RMI_FASCMS));
|
||
rec_iva.put(RMI_TIPODET, cur.get(RMI_TIPODET));
|
||
nr++;
|
||
}
|
||
}
|
||
return _error;
|
||
}
|
||
|
||
error_type TContabilizzazione::create_total_doc_row(TDocumento& doc)
|
||
// Crea la riga contabile di totale documento
|
||
{
|
||
TRectype& rec_cg = _movimento->cg(0);
|
||
TRectype& head = _movimento->lfile().curr();
|
||
const int annoes = head.get_int(MOV_ANNOES);
|
||
const long numreg = head.get_long(MOV_NUMREG);
|
||
TDate datareg(head.get_date(MOV_DATAREG));
|
||
real totdoc(head.get_real(MOV_TOTDOC));
|
||
|
||
TLocalisamfile& cli_file = _clifo->lfile();
|
||
TString16 tipocf(cli_file.get(CLI_TIPOCF));
|
||
long codcf = cli_file.get_long(CLI_CODCF);
|
||
int gruppo = 0, conto = 0;
|
||
|
||
TString16 catven(doc.get(DOC_CATVEN));
|
||
|
||
if (search_clifo_bill(catven) == no_error)
|
||
{
|
||
tipocf[0] = _co_cliente.tipo();
|
||
gruppo = _co_cliente.gruppo();
|
||
conto = _co_cliente.conto();
|
||
codcf = _co_cliente.sottoconto();
|
||
}
|
||
else
|
||
{
|
||
_conto_errato = _co_cliente;
|
||
_error = conto_error;
|
||
}
|
||
|
||
TString descr = head.get(MOV_DESCR);// La descrizione della riga di totale documento la prende dalla testata
|
||
|
||
TConto contro;
|
||
if (_movimento->iva_items() > 0)
|
||
{
|
||
TRectype& first_iva_row = _movimento->iva(0);
|
||
contro.get(first_iva_row);
|
||
}
|
||
|
||
rec_cg.put(RMV_ANNOES,annoes);rec_cg.put(RMV_NUMREG,numreg);
|
||
const char sezione = _caus->sezione(1);
|
||
rec_cg.put(RMV_NUMRIG,1); rec_cg.put(RMV_SEZIONE, sezione); // qui
|
||
rec_cg.put(RMV_DATAREG,datareg); rec_cg.put(RMV_TIPOC,tipocf);
|
||
rec_cg.put(RMV_GRUPPO,gruppo);rec_cg.put(RMV_CONTO,conto);
|
||
rec_cg.put(RMV_SOTTOCONTO,codcf); rec_cg.put(RMV_DESCR,descr);
|
||
contro.put(rec_cg, TRUE);
|
||
rec_cg.put(RMV_IMPORTO,totdoc); rec_cg.put(RMV_ROWTYPE,"T");
|
||
if (tipocf == " ")
|
||
{
|
||
head.zero(MOV_TIPO);
|
||
head.zero(MOV_CODCF);
|
||
}
|
||
|
||
return _error;
|
||
}
|
||
|
||
error_type TContabilizzazione::compile_rows_mov(TDocumento& doc)
|
||
// Compila le righe
|
||
{
|
||
TString16 tiporiga, codiva1, codiva2;
|
||
const int rows = doc.rows();
|
||
const int ndec = doc.decimals();
|
||
const bool fat_com = doc.tipo().fattura_commerciale();
|
||
|
||
static TString_array tabella_ripartizione;
|
||
|
||
if (fat_com && tabella_ripartizione.items() == 0)
|
||
{
|
||
TConfig cnf(CONFIG_STUDIO);
|
||
for (int k = 1; k <= MAX_IVA_SLICES; k++)
|
||
{
|
||
TToken_string* tt = new TToken_string();
|
||
tt->add(cnf.get("EXCLUDE_PERC", "ve", k));
|
||
tt->add(cnf.get("EXCLUDE_IVA", "ve", k));
|
||
tabella_ripartizione.add(tt);
|
||
}
|
||
}
|
||
|
||
_righe_iva->destroy(); // resetta l'assoc_array delle righe di iva
|
||
_totali_lordi.destroy();// resetta l'assoc_array dei totali lordi
|
||
for (int i=1; good() && i<=rows; i++) // browse all this fucked document rows
|
||
{
|
||
TRiga_documento & r = doc[i];
|
||
tiporiga = r.get(RDOC_TIPORIGA);
|
||
_tri->put("CODTAB",tiporiga);
|
||
if (_tri->read() == NOERR) // controlla l'esistenza della riga
|
||
{
|
||
TBill conto;
|
||
const char tipo = r.tipo().tipo();
|
||
// Le righe omaggio senza addebito IVA vanno saltate
|
||
const bool riga_omaggio = r.get_bool(RDOC_ADDIVA) && tipo == 'O';
|
||
if ((tipo != 'D' && tipo != 'C') || riga_omaggio)
|
||
{
|
||
search_costo_ricavo(conto,r); // l'errore eventuale viene settato qui dentro
|
||
if (good())
|
||
{
|
||
if (fat_com)
|
||
{
|
||
codiva2 = r.get(RDOC_CODIVA); // Save...
|
||
for (int j=0; j < MAX_IVA_SLICES; j++)
|
||
{
|
||
real perc = tabella_ripartizione.row(j).get(0);
|
||
|
||
perc /= 100.0;
|
||
codiva1 = tabella_ripartizione.row(j).get(1);
|
||
if (perc == ZERO || codiva1.trim().empty())
|
||
continue;
|
||
|
||
r.put(RDOC_CODIVA, codiva1);
|
||
if (riga_omaggio)
|
||
_righe_iva->add_omaggi(r, conto, ALL_DECIMALS, perc);
|
||
else
|
||
_righe_iva->add(r, conto, ALL_DECIMALS, perc);
|
||
}
|
||
r.put(RDOC_CODIVA, codiva2); //Restore
|
||
}
|
||
else
|
||
{
|
||
if (riga_omaggio)
|
||
_righe_iva->add_omaggi(r, conto);
|
||
else
|
||
_righe_iva->add(r, conto);
|
||
const TString16 cod(r.get(RDOC_CODIVA));
|
||
|
||
if (r.doc().tipo().calcolo_lordo()) // Si ricorda che calcolo_lordo() e fattura_commerciale() sono esclusivi.
|
||
{
|
||
// Totalizza per ogni codice iva il lordo
|
||
if (!_totali_lordi.is_key(cod))
|
||
_totali_lordi.add(cod, new real);
|
||
real& rl = (real&) _totali_lordi[cod];
|
||
rl += r.imponibile(TRUE);
|
||
}
|
||
}
|
||
if (r.tipo().tipo() == RIGA_SPESEDOC && !_caus->intra())
|
||
{
|
||
const char tipo_rit = r.spesa().tipo_ritenuta();
|
||
|
||
if (tipo_rit != '\0')
|
||
{
|
||
((TSpesa_prest &)r.spesa()).zero("S9");
|
||
((TRiga_documento &)r).dirty_fields();
|
||
TCurrency_documento c(r.imponibile());
|
||
((TRiga_documento &)r).dirty_fields();
|
||
((TSpesa_prest &)r.spesa()).put("S9", (char) tipo_rit);
|
||
TRectype & h = _movimento->lfile().curr();
|
||
real val;
|
||
if (r.doc().in_valuta())
|
||
{
|
||
val = h.get_real(MOV_TOTDOCVAL) - c.get_num();
|
||
h.put(MOV_TOTDOCVAL, val);
|
||
}
|
||
c.change_to_firm_val();
|
||
val = h.get_real(MOV_TOTDOC) - c.get_num();
|
||
h.put(MOV_TOTDOC, val);
|
||
|
||
if (tipo_rit == 'F')
|
||
{
|
||
val = h.get_real(MOV_RITFIS) + c.get_num();
|
||
h.put(MOV_RITFIS, val);
|
||
}
|
||
else
|
||
if (tipo_rit == 'S')
|
||
{
|
||
val = h.get_real(MOV_RITSOC) + c.get_num();
|
||
h.put(MOV_RITSOC, val);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
else
|
||
_error = row_type_error;
|
||
}
|
||
|
||
if (good() && _righe_iva->items() == 0)
|
||
_error = no_rows_error;
|
||
|
||
// Crea le righe per le spese d'incasso e bolli
|
||
if (good())
|
||
add_spese_inbo(doc,ndec);
|
||
|
||
// Aggiorna le righe di sconto (sconto ad importo o percentuale)
|
||
if (good())
|
||
adjust_sconto_rows(doc);
|
||
|
||
// Crea le righe di IVA
|
||
if (good())
|
||
create_iva_rows(doc);
|
||
|
||
// Controlla che le imposte per ogni aliquota ed eventualmente corregge le imposte stesse sulle righe
|
||
if (good())
|
||
adjust_iva_rows(doc);
|
||
|
||
// Crea la riga di totale documento
|
||
if (good())
|
||
create_total_doc_row(doc);
|
||
// crea le righe di contabilita'
|
||
if (good())
|
||
{
|
||
const TString descr_cr(doc.clifor().get(CLI_RAGSOC));
|
||
|
||
switch (_movimento->recalc_cg_rows(descr_cr, *_caus))
|
||
{
|
||
case 1 :
|
||
_error = movement_error;
|
||
break;
|
||
case 2 :
|
||
_error = cau_ritintra_error;
|
||
break;
|
||
default :
|
||
break;
|
||
}
|
||
}
|
||
return _error;
|
||
}
|
||
|
||
error_type TContabilizzazione::compile_rows_mov_re(TDocumento& doc)
|
||
// Compila le righe
|
||
{
|
||
TString16 tiporiga, codiva1, codiva2;
|
||
const int rows = doc.rows();
|
||
const int ndec = doc.decimals();
|
||
|
||
_righe_iva->destroy(); // resetta l'assoc_array delle righe di iva
|
||
for (int i=1; good() && i<=rows; i++) // browse all this fucked document rows
|
||
{
|
||
TRiga_documento & r = doc[i];
|
||
tiporiga = r.get(RDOC_TIPORIGA);
|
||
_tri->put("CODTAB",tiporiga);
|
||
if (_tri->read() == NOERR) // controlla l'esistenza della riga
|
||
{
|
||
TBill conto;
|
||
const char tipo = r.tipo().tipo();
|
||
// Le righe omaggio senza addebito IVA vanno saltate
|
||
const bool riga_omaggio = r.get_bool(RDOC_ADDIVA) && tipo == 'O';
|
||
if ((tipo != 'D' && tipo != 'C') && !riga_omaggio)
|
||
{
|
||
search_costo_ricavo(conto,r); // l'errore eventuale viene settato qui dentro
|
||
if (good())
|
||
_righe_iva->add(r, conto);
|
||
}
|
||
}
|
||
else
|
||
_error = row_type_error;
|
||
}
|
||
|
||
if (good() && _righe_iva->items() == 0)
|
||
_error = no_rows_error;
|
||
|
||
// Crea le righe per le spese d'incasso e bolli
|
||
if (good())
|
||
add_spese_inbo(doc,ndec);
|
||
|
||
// Crea le righe di IVA
|
||
if (good())
|
||
create_iva_rows(doc);
|
||
|
||
// Aggiorna le righe di sconto (sconto ad importo o percentuale)
|
||
if (good())
|
||
adjust_sconto_rows(doc);
|
||
|
||
// Crea la riga di totale documento
|
||
if (good())
|
||
{
|
||
int righe = _movimento->iva_items();
|
||
for (int i=0; i<righe; i++)
|
||
_movimento->add_row_re(i);
|
||
righe = _movimento->cg_items();
|
||
int row_to_add = righe - 1;
|
||
for (int j = row_to_add; j >= 0; j--)
|
||
if (!_movimento->add_row_cp_re(row_to_add))
|
||
row_to_add--;
|
||
}
|
||
_movimento->destroy_iva_row();
|
||
return _error;
|
||
}
|
||
|
||
error_type TContabilizzazione::change_doc_status(TDocumento& doc)
|
||
// Cambia lo stato del documento
|
||
{
|
||
doc.stato(get_char("S4"));
|
||
if (doc.rewrite() != NOERR)
|
||
_error = chg_stat_error;
|
||
return _error;
|
||
}
|
||
|
||
error_type TContabilizzazione::write_scadenze(TDocumento& doc)
|
||
// Scrive le scadenze. Liberamente tratto da cg2104.cpp.
|
||
{
|
||
TRectype& head = _movimento->curr();
|
||
const long nreg = head.get_long(MOV_NUMREG);
|
||
|
||
// const real change(head.get_real(MOV_CAMBIO));
|
||
int anno = head.get_int(MOV_ANNOIVA);
|
||
TString16 numpart = doc.get(DOC_NUMDOCRIF);
|
||
|
||
if (numpart.blank())
|
||
{
|
||
numpart = head.get(_nump_cfg ? MOV_PROTIVA : MOV_NUMDOC);
|
||
}
|
||
else
|
||
{
|
||
TDate ddr = doc.get(DOC_DATADOCRIF);
|
||
if (ddr.ok())
|
||
anno = ddr.year();
|
||
}
|
||
|
||
TPartita* newgame = NULL;
|
||
if (anno > 0 && !numpart.blank())
|
||
{
|
||
const int tmov = _caus->tipomov();
|
||
const TString80 desc(head.get(MOV_DESCR));
|
||
const TString4 codcaus(_caus->codice());
|
||
const TString4 v(head.get(MOV_CODVAL));
|
||
const TDate d(head.get_date(MOV_DATACAM));
|
||
const real c(head.get_real(MOV_CAMBIO));
|
||
const TValuta cambio(v, d, c); // verificare
|
||
const TString8 agente(doc.get(DOC_CODAG));
|
||
const int ndec = doc.decimals();
|
||
const char sezione = _movimento->cg(0).get_char(RMV_SEZIONE); // Dare/Avere
|
||
|
||
/* const char tipocf = head.get_char(MOV_TIPO);
|
||
const long codcf = head.get_long(MOV_CODCF);
|
||
const TBill clifo(0,0,codcf,tipocf);
|
||
newgame = new TPartita(clifo, anno, numpart); */
|
||
newgame = new TPartita(_co_cliente, anno, numpart);
|
||
newgame->allinea(); // Rispettare sempre l'allineamento del numero partita!
|
||
|
||
int row = 0;
|
||
if (tmov == tm_fattura)
|
||
row = newgame->prima_fattura(nreg); // Riga fattura di questo movimento
|
||
TRiga_partite& partita = row <= 0 ? newgame->new_row() : newgame->riga(row);
|
||
const int nuova_riga = partita.get_int(PART_NRIGA);
|
||
|
||
// put data on partita
|
||
partita.put(PART_TIPOMOV, tmov);
|
||
partita.put(PART_NREG, nreg); // Riferimento alla registrazione contabile
|
||
partita.put(PART_NUMRIG, 1); // Riferimento alla riga contabile del totale
|
||
partita.put(PART_DATAREG, head.get(MOV_DATAREG));
|
||
partita.put(PART_DATADOC, head.get(MOV_DATADOC));
|
||
partita.put(PART_NUMDOC, head.get(MOV_NUMDOC));
|
||
partita.put(PART_DESCR, desc);
|
||
partita.put(PART_CODCAUS, codcaus);
|
||
partita.put(PART_REG, _caus->reg().name());
|
||
partita.put(PART_PROTIVA, head.get(MOV_PROTIVA));
|
||
partita.put(PART_SEZ, sezione);
|
||
|
||
const real totdoc(head.get(MOV_TOTDOC));
|
||
const real totdocval(head.get(MOV_TOTDOCVAL));
|
||
const bool in_valuta = cambio.in_valuta();
|
||
const bool swapped = test_swap();
|
||
const TCurrency_documento td((in_valuta ? totdocval : totdoc) * (swapped ? -1 : 1), doc);
|
||
|
||
partita.put(PART_IMPTOTDOC, totdoc);
|
||
|
||
cambio.put(partita);
|
||
if (in_valuta)
|
||
partita.put(PART_IMPTOTVAL,totdocval);
|
||
|
||
if (partita.is_fattura())
|
||
{
|
||
TPagamento& pag = doc.pagamento();
|
||
const TCurrency_documento totspese(doc.spese(), doc);
|
||
TCurrency_documento totimposte(doc.imposta(TRUE), doc);
|
||
|
||
real imposte;
|
||
for (int j = _movimento->iva_items()-1; j >= 0; j--)
|
||
imposte += _movimento->iva(j).get_real(RMI_IMPOSTA) * (swapped ? -1 : 1);
|
||
if (_caus->iva() == iva_acquisti) // Ricalcola precisamente il totale imposte
|
||
{
|
||
real ti = imposte;
|
||
if (in_valuta)
|
||
cambio.lit2val(ti);
|
||
totimposte.set_num(ti);
|
||
}
|
||
// else
|
||
// {
|
||
// TCurrency ti = totimposte;
|
||
// ti.change_to_firm_val();
|
||
// imposte = ti.get_num();
|
||
// }
|
||
|
||
const TCurrency totimponibili = td - totimposte - totspese;
|
||
TCurrency_documento anticipo(doc.get_real(DOC_IMPPAGATO), doc);
|
||
|
||
if (anticipo.abs() < td.abs())
|
||
{
|
||
TGeneric_distrib d(anticipo.get_num(), ndec);
|
||
|
||
d.add(totimponibili.get_num());
|
||
d.add(totimposte.get_num());
|
||
d.add(totspese.get_num());
|
||
|
||
const TCurrency_documento pagtotimponibili(totimponibili.get_num() - d.get(), doc);
|
||
TCurrency_documento diffiva(d.get(), doc);
|
||
const TCurrency_documento pagtotimposte (totimposte.get_num() - diffiva.get_num(), doc);
|
||
const TCurrency_documento pagtotspese (totspese.get_num() - d.get(), doc);
|
||
|
||
if (in_valuta)
|
||
{
|
||
const real change = cambio.cambio();
|
||
//real val1 = totimponibili * change;
|
||
TCurrency val2(imposte); diffiva.change_to_firm_val(); val2 -= diffiva;
|
||
TCurrency_documento val3(pagtotspese); val3.change_to_firm_val();
|
||
TCurrency val1(totdoc);
|
||
TCurrency_documento ant(anticipo.get_num(), doc);
|
||
ant.change_to_firm_val();
|
||
val1 -= ant + val2 + val3;
|
||
// Cosi' corregge eventuali scompensi di poche lirette
|
||
pag.set_total_valuta(pagtotimponibili, pagtotimposte, pagtotspese, val1, val2, val3);
|
||
}
|
||
else
|
||
pag.set_total( pagtotimponibili, pagtotimposte, pagtotspese);
|
||
pag.set_rate_auto( );
|
||
}
|
||
else
|
||
pag.zap_rate();
|
||
|
||
if (!anticipo.is_zero())
|
||
{
|
||
pag.add_rata();
|
||
|
||
TDate first_date(doc.data());
|
||
TDate first_scad(pag.data_rata(0));
|
||
|
||
if (first_date == first_scad)
|
||
pag.set_datarata(0, ++first_scad); // Sposta in avanti la data della prima scadenza di un giorno.
|
||
|
||
// Shift delle rate verso il basso
|
||
for (int k=pag.n_rate()-1; k>0; k--)
|
||
pag.rata(k) = pag.rata(k-1);
|
||
|
||
// Sostituisce la prima rata con quella dell'anticipo
|
||
|
||
// Se l'anticipo <20> pi<70> grande del totale, si crea una riga di scadenza
|
||
// con l'importo del totale doc.
|
||
if (anticipo.abs() >= td.abs())
|
||
anticipo = td;
|
||
|
||
TCurrency_documento anticipo_base(anticipo); anticipo_base.change_to_firm_val();
|
||
|
||
// Crea una rimessa diretta con la data del documento per il valore dell'anticipo
|
||
pag.set_rata(0, in_valuta ? anticipo.get_num() : ZERO, anticipo_base.get_num(), first_date, 1, "", FALSE);
|
||
}
|
||
|
||
real imponibile, imponibile_val;
|
||
for (int i = pag.n_rate()-1; i >= 0; i--)
|
||
{
|
||
if (in_valuta)
|
||
imponibile_val += pag.tval_rata(i);
|
||
imponibile += pag.tlit_rata(i);
|
||
}
|
||
partita.put(PART_IMPORTO, imponibile);
|
||
partita.put(PART_IMPORTOVAL, imponibile_val);
|
||
partita.put(PART_IMPOSTA, imposte);
|
||
partita.put(PART_SPESE, totspese.get_num());
|
||
|
||
newgame->scollega_pagamenti(nuova_riga); // Sempre meglio che perderli, ma andrebbero ricollegati
|
||
partita.elimina_rata(-1); // Elimina tutte le rate eventuali
|
||
|
||
const TString8 abipr(doc.get(DOC_CODABIP)), cabpr(doc.get(DOC_CODCABP)),
|
||
abi(doc.get(DOC_CODABIA)), cab(doc.get(DOC_CODCABA));
|
||
|
||
const int nr = pag.n_rate();
|
||
const TString16 codpag(head.get(MOV_CODPAG));
|
||
for (i = 0; i < nr; i++)
|
||
{
|
||
TRiga_scadenze& scadenza = partita.new_row();
|
||
|
||
scadenza.put(SCAD_CODPAG, codpag); // Codice pagamento
|
||
scadenza.put(SCAD_CODAG, agente); // Codice agente
|
||
|
||
scadenza.put(SCAD_DATASCAD, pag.data_rata(i)); // Data scadenza
|
||
scadenza.put(SCAD_IMPORTO, pag.tlit_rata(i)); // Importo
|
||
if (in_valuta)
|
||
scadenza.put(SCAD_IMPORTOVAL, pag.tval_rata(i)); // Importo in valuta
|
||
scadenza.put(SCAD_TIPOPAG, pag.tipo_rata(i)); // Tipo pagamento
|
||
scadenza.put(SCAD_ULTCLASS, pag.ulc_rata(i)); // Ulteriore classificazione
|
||
scadenza.put(SCAD_CODABIPR, abipr); // Ns ABI
|
||
scadenza.put(SCAD_CODCABPR, cabpr); // Ns CAB
|
||
scadenza.put(SCAD_CODABI, abi); // Vs ABI
|
||
scadenza.put(SCAD_CODCAB, cab); // Vs CAB
|
||
// scadenza.put(SCAD_DESCR, ????); // Note
|
||
}
|
||
} // if fattura
|
||
else if (doc.is_nota_credito())
|
||
{
|
||
TImporto residuoval(sezione, doc.totale_doc());
|
||
TImporto residuolit(sezione, abs(totdoc));
|
||
|
||
partita.put(PART_DATAPAG, doc.get(DOC_DATADOC));
|
||
partita.put(PART_TIPOPAG, doc.pagamento().tipo_rata(0));
|
||
|
||
// Attenzione: le note di credito possono avere in testata valori negativi! Qui vanno positivi.
|
||
partita.put(PART_IMPTOTDOC, abs(totdoc));
|
||
partita.put(PART_IMPTOTVAL, abs(totdocval));
|
||
|
||
// Attenzione: l'importo giusto viene poi aggiornato dalla modifica pagamento
|
||
partita.zero(PART_IMPORTO);
|
||
partita.zero(PART_IMPORTOVAL);
|
||
|
||
const TDate datadocrif = doc.get_date(DOC_DATADOCRIF);
|
||
const TString16 numdocrif = doc.get(DOC_NUMDOCRIF);
|
||
for (int p = newgame->prima_fattura(); p > 0 && p < nuova_riga; p++)
|
||
{
|
||
const TRiga_partite& fatt = newgame->riga(p);
|
||
const TDate datadoc = fatt.get(PART_DATADOC);
|
||
const TString16 numdoc = fatt.get(PART_NUMDOC);
|
||
if (datadoc.year() == datadocrif.year() && numdoc == numdocrif)
|
||
break;
|
||
}
|
||
if (p > 0 && p < nuova_riga)
|
||
{
|
||
TPagamento& pag = doc.pagamento();
|
||
|
||
TCurrency_documento totdoc(abs(doc.totale_doc()), doc);
|
||
TCurrency_documento totspese(abs(doc.spese()), doc);
|
||
TCurrency_documento totimposte(abs(doc.imposta(TRUE)), doc);
|
||
TCurrency_documento totimponibili = totdoc - totspese - totimposte;
|
||
if (in_valuta)
|
||
{
|
||
TCurrency_documento val2(totimposte); val2.change_to_firm_val();
|
||
TCurrency_documento val3(totspese); val3.change_to_firm_val();
|
||
TCurrency_documento val1(totdoc); val1.change_to_firm_val(); val1 -= val2+val3;
|
||
// Cosi' corregge eventuali scompensi di poche lirette
|
||
pag.set_total_valuta(totimponibili, totimposte, totspese, val1, val2, val3);
|
||
}
|
||
else
|
||
pag.set_total(totimponibili, totimposte, totspese);
|
||
pag.set_rate_auto();
|
||
|
||
const TRiga_partite& fatt = newgame->riga(p);
|
||
for (int r = 1; r <= fatt.rate() && r <= pag.n_rate(); r++)
|
||
{
|
||
const TRiga_scadenze& rata = fatt.rata(r);
|
||
const real imprata = rata.residuo(FALSE).valore();
|
||
const real imprataval = rata.residuo(TRUE).valore();
|
||
real importo_rata_lit = pag.importo_rata(r-1, FALSE);
|
||
real importo_rata_val = in_valuta ? pag.importo_rata(r-1, TRUE) : importo_rata_lit;
|
||
real delta_lit = importo_rata_lit - imprata;
|
||
real delta_val = in_valuta ? importo_rata_val - imprataval : ZERO;
|
||
// Controlla se l'importo della nota di credito supera quello della fattura
|
||
if (delta_lit > ZERO || delta_val > ZERO)
|
||
{
|
||
// Detrae l'eccedenza dalla rata corrente e la sposta nella rata successiva
|
||
importo_rata_lit -= delta_lit;
|
||
importo_rata_val -= delta_val;
|
||
|
||
if (r == pag.n_rate())
|
||
{
|
||
// Crea eventuale ultima rata mancante
|
||
pag.add_rata();
|
||
}
|
||
else
|
||
{
|
||
// Incrementa importo rata
|
||
delta_lit += pag.importo_rata(r, FALSE);
|
||
delta_val += pag.importo_rata(r, TRUE);
|
||
}
|
||
const TDate oggi(TODAY);
|
||
const int tiporata = pag.tipo_rata(r-1);
|
||
pag.set_rata(r, delta_val, delta_lit, oggi, tiporata, "", FALSE);
|
||
}
|
||
if (!importo_rata_lit.is_zero() || (in_valuta && !importo_rata_val.is_zero()))
|
||
{
|
||
TRectype pag = newgame->pagamento(p, r, nuova_riga);
|
||
pag.put(PAGSCA_IMPORTO, importo_rata_lit);
|
||
if (in_valuta)
|
||
{
|
||
pag.put(PAGSCA_IMPORTOVAL, importo_rata_val);
|
||
residuoval -= TImporto(sezione, importo_rata_val);
|
||
}
|
||
else
|
||
residuoval -= TImporto(sezione, importo_rata_lit);
|
||
residuolit -= TImporto(sezione, importo_rata_lit);
|
||
|
||
pag.put(PAGSCA_ACCSAL, "A");
|
||
newgame->modifica_pagamento(pag, cambio, TRUE);
|
||
}
|
||
}
|
||
}
|
||
|
||
if (!residuoval.is_zero())
|
||
{
|
||
// Pagamento non assegnato
|
||
TRectype unpag = newgame->pagamento(TPartita::UNASSIGNED, 0, nuova_riga);
|
||
if (in_valuta)
|
||
{
|
||
unpag.put(PAGSCA_IMPORTOVAL, residuoval.valore());
|
||
// cambio.val2lit(residuoval); //verificare
|
||
// unpag.put(PAGSCA_IMPORTO, residuoval.valore());
|
||
unpag.put(PAGSCA_IMPORTO, residuolit.valore());
|
||
}
|
||
else
|
||
unpag.put(PAGSCA_IMPORTO, residuoval.valore());
|
||
unpag.put(PAGSCA_ACCSAL, "A");
|
||
newgame->modifica_pagamento(unpag, cambio, TRUE);
|
||
}
|
||
}
|
||
}
|
||
|
||
if (newgame != NULL) // Se non ho cancellato il numero partita ...
|
||
{
|
||
if (!newgame->write()) // Salva nuova partita
|
||
_error = write_part_error;
|
||
delete newgame;
|
||
}
|
||
|
||
return _error;
|
||
}
|
||
|
||
error_type TContabilizzazione::write_all(TDocumento& doc, TMovimentoPN_VE & movimento)
|
||
// Scrive il movimento e le scadenze, gestendo la rinumerazione se il movimento e' gia presente
|
||
{
|
||
// N.B: _error non viene settato, per non stampare il messaggio di errore 2 volte.
|
||
// basta solo ritornare qualcosa di != da no_error, per evitare le operazioni successive
|
||
// a write_all
|
||
TRectype& head = movimento.curr();
|
||
long numreg = head.get_long(MOV_NUMREG);
|
||
if (test_swap())
|
||
{
|
||
const real totdoc = -head.get_real(MOV_TOTDOC);
|
||
const real totdocval = -head.get_real(MOV_TOTDOCVAL);
|
||
|
||
head.put(MOV_TOTDOC, totdoc);
|
||
// head.put(MOV_TOTDOCVAL, totdocval); // Non cambio segno! :-( LL700285
|
||
|
||
const int items = movimento.iva_items();
|
||
for (int i = items - 1 ; i >= 0; i--)
|
||
{
|
||
TRectype & rec_iva = movimento.iva(i);
|
||
|
||
const real imponibile = -rec_iva.get_real(RMI_IMPONIBILE);
|
||
const real imposta = -rec_iva.get_real(RMI_IMPOSTA);
|
||
|
||
rec_iva.put(RMI_IMPONIBILE, imponibile);
|
||
rec_iva.put(RMI_IMPOSTA, imposta);
|
||
}
|
||
}
|
||
|
||
TSaldo_agg saldo;
|
||
int err = NOERR;
|
||
const long old_numreg = doc_contabilized(doc, FALSE);
|
||
if (old_numreg > 0)
|
||
{
|
||
TMovimentoPN oldmov;
|
||
oldmov.curr().put(MOV_NUMREG, old_numreg);
|
||
if (oldmov.read() == NOERR)
|
||
aggiorna_saldi(saldo, oldmov, FALSE); // Leggo i vecchi saldi
|
||
err = movimento.rewrite();
|
||
}
|
||
else
|
||
err = movimento.write();
|
||
if (err != NOERR)
|
||
{
|
||
error_box("Errore %d scrivendo il movimento %ld.", err, numreg);
|
||
return generic_error;
|
||
}
|
||
// Aggiorno subito i saldi
|
||
aggiorna_saldi(saldo, movimento, TRUE);
|
||
|
||
if (sc_enabled())
|
||
write_scadenze(doc);
|
||
|
||
if (good() && in_enabled())
|
||
write_intra(doc);
|
||
|
||
if (doc.get_real(DOC_IMPPAGATO) != ZERO)
|
||
if (write_anticipo(doc) != no_error)
|
||
movimento.remove(); // Se si <20> verificato un errore nella scrittura dell'anticipo rimuove il movimento di prima nota
|
||
|
||
if (good())
|
||
{
|
||
_total_docs++;
|
||
change_doc_status(doc);
|
||
|
||
const TRectype& mov = _movimento->curr();
|
||
TString80 msg;
|
||
msg.format("Generazione Movimento $[b,w]%5ld$[n,w]", mov.get_long(MOV_NUMREG));
|
||
msg << " del " << mov.get(MOV_DATAREG);
|
||
_viswin->add_line(msg);
|
||
|
||
if (applicazione_esterna().not_empty())
|
||
call_exe(doc, movimento);
|
||
}
|
||
|
||
return no_error;
|
||
}
|
||
|
||
error_type TContabilizzazione::write_all_re(TDocumento& doc, TMovimentoPN_VE & movimento)
|
||
// Scrive il movimento e le scadenze, gestendo la rinumerazione se il movimento e' gia presente
|
||
{
|
||
// N.B: _error non viene settato, per non stampare il messaggio di errore 2 volte.
|
||
// basta solo ritornare qualcosa di != da no_error, per evitare le operazioni successive
|
||
// a write_all
|
||
TRectype& head = movimento.curr();
|
||
long numreg = head.get_long(MOV_NUMREG);
|
||
TSaldo_agg saldo;
|
||
|
||
int err = NOERR;
|
||
const long old_numreg = doc_contabilized(doc, FALSE);
|
||
if (old_numreg > 0)
|
||
{
|
||
TMovimentoPN oldmov;
|
||
oldmov.curr().put(MOV_NUMREG, old_numreg);
|
||
if (oldmov.read() == NOERR)
|
||
aggiorna_saldi(saldo, oldmov, FALSE); // Leggo i vecchi saldi
|
||
err = movimento.rewrite();
|
||
}
|
||
else
|
||
err = movimento.write();
|
||
if (err != NOERR)
|
||
{
|
||
error_box("Errore %d scrivendo il movimento %ld.", err, numreg);
|
||
return generic_error;
|
||
}
|
||
// Aggiorno subito i saldi
|
||
aggiorna_saldi(saldo, movimento, TRUE);
|
||
|
||
if (good())
|
||
{
|
||
_total_docs++;
|
||
change_doc_status(doc);
|
||
|
||
TString msg("Generazione Movimento ");
|
||
|
||
msg << "$[b,w]" << _movimento->curr().get(MOV_NUMREG) << "$[n,w]";
|
||
msg << " del " << _movimento->curr().get(MOV_DATAREG);
|
||
_viswin->add_line(msg);
|
||
|
||
}
|
||
|
||
return no_error;
|
||
}
|
||
|
||
error_type TContabilizzazione::compile_head_anticipo(TDocumento& doc)
|
||
{
|
||
TString descr;
|
||
TString16 codcaus = doc.tipo().caus_anticipo();
|
||
TDate datareg(_movimento->lfile().get_date(MOV_DATAREG));
|
||
|
||
if (!_caus->read(codcaus,datareg.year()))
|
||
return caus_ant_error;
|
||
|
||
long nr = doc_contabilized(doc, TRUE);
|
||
if (nr <= 0)
|
||
{
|
||
if (get_next_reg_num(nr) != no_error)
|
||
return nr_reg_error;
|
||
}
|
||
|
||
_anticipo = new TMovimentoPN;
|
||
TRectype& head = _anticipo->curr();
|
||
head = _movimento->curr(); // Copia tutti i campi...
|
||
head.put(MOV_NUMREG,nr);
|
||
head.zero(MOV_DATA74TER);
|
||
head.put(MOV_TIPODOC,_caus->tipo_doc());
|
||
head.put(MOV_CODCAUS,_caus->codice());
|
||
descr = doc.get_bool(DOC_ACCSALDO) ? "Saldo fattura" : "Acconto fattura";
|
||
descr << " n. " << doc.numero();
|
||
descr << " del " << doc.get_date(DOC_DATADOC).string();
|
||
head.put(MOV_DESCR,descr);
|
||
head.put(MOV_TIPOMOV,char(_caus->tipomov()+'0'));
|
||
head.zero(MOV_REG);
|
||
head.zero(MOV_PROTIVA);
|
||
head.zero(MOV_CODPAG);
|
||
head.zero(MOV_CORRLIRE);
|
||
head.zero(MOV_CORRVALUTA);
|
||
TString codval = head.get(MOV_CODVALI);
|
||
// real cambio = head.get_real(MOV_CAMBIOI);
|
||
head.zero(MOV_CODVALI);
|
||
head.zero(MOV_CAMBIOI);
|
||
if (sc_enabled())
|
||
{
|
||
TCurrency_documento p(doc.get_real(DOC_IMPPAGATO), doc);
|
||
TCurrency_documento plit(p); p.change_to_firm_val();
|
||
if (doc.in_valuta())
|
||
{
|
||
head.put(MOV_TOTDOC,plit.get_num());
|
||
head.put(MOV_TOTDOCVAL,p.get_num());
|
||
}
|
||
else
|
||
{
|
||
head.put(MOV_TOTDOC,p.get_num());
|
||
head.zero(MOV_TOTDOCVAL);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
head.zero(MOV_TOTDOC);
|
||
head.zero(MOV_TOTDOCVAL);
|
||
}
|
||
|
||
// Memorizza il movimento contabile di destinazione!
|
||
doc.put(DOC_NUMANT, nr);
|
||
// Scrive sulla testata dell'anticipo il numero di documento originale
|
||
head.put(MOV_DPROVV, doc.get(DOC_PROVV));
|
||
head.put(MOV_DANNO, doc.get(DOC_ANNO));
|
||
head.put(MOV_DCODNUM, doc.get(DOC_CODNUM));
|
||
head.put(MOV_DNDOC, doc.get(DOC_NDOC));
|
||
|
||
return no_error;
|
||
}
|
||
|
||
char TContabilizzazione::sezione() const
|
||
{
|
||
char sezione = ' ';
|
||
const char tipoc = _co_cliente.tipo();
|
||
|
||
tipo_movimento tm = (tipo_movimento)_caus->tipomov();
|
||
sezione = _caus->sezione(1); // Usa la sezione della causale
|
||
|
||
if (sezione <= ' ') // Se non c'e' la sezione bell'e' ch'e' pronta
|
||
{
|
||
if (tm == tm_fattura || tm == tm_insoluto) // calcola in base al tipo movimento e
|
||
sezione = (tipoc == 'C') ? 'D' : 'A'; // al tipo cliente/fornitore
|
||
else
|
||
sezione = (tipoc == 'C') ? 'A' : 'D';
|
||
}
|
||
return sezione;
|
||
}
|
||
|
||
error_type TContabilizzazione::search_clifo_bill(TString & catven)
|
||
{
|
||
_error = no_error;
|
||
TLocalisamfile& clifo = _clifo->lfile();
|
||
const long codcf = clifo.get_long(CLI_CODCF);
|
||
const char tipocf = clifo.get_char(CLI_TIPOCF);
|
||
_co_cliente.set(0,0,0);
|
||
|
||
// Reperisce il conto del cliente nel seguente ordine, appena ne trova uno valido:
|
||
// - Categoria vendita
|
||
// - file clienti
|
||
// - causale
|
||
|
||
_cve->put("CODTAB", catven);
|
||
if (_cve->read() == NOERR)
|
||
_co_cliente.set(_cve->get_int("I1"), _cve->get_int("I2"),codcf,tipocf);
|
||
if (!_co_cliente.ok())
|
||
{
|
||
_co_cliente.set(clifo.get_int(CLI_GRUPPO),clifo.get_int(CLI_CONTO),codcf,tipocf);
|
||
if (!_co_cliente.ok()) // se non e' valido, reperiscilo dalla riga #1 della causale
|
||
{
|
||
_caus->bill(1,_co_cliente); // conto della riga 1
|
||
if (_co_cliente.tipo() != ' ')
|
||
_co_cliente.codclifo() = codcf;
|
||
if (!_co_cliente.ok())
|
||
_error = clifo_error;
|
||
}
|
||
}
|
||
return _error;
|
||
}
|
||
|
||
error_type TContabilizzazione::search_counter_bill(TDocumento& doc)
|
||
{
|
||
_error = no_error;
|
||
// cerca il conto relativo alla riga di causale:
|
||
// 1 Rimessa diretta o contanti (banca) riga 2
|
||
// 2 Tratta riga 3
|
||
// 3 Ricevuta bancaria riga 4
|
||
// 4 Cessione riga 5
|
||
// 5 Paghero' riga 6
|
||
// 6 Lettera di credito riga 7
|
||
// 7 Tratta accettata riga 8
|
||
// 8 Rapporti interban. diretti riga 2
|
||
// 9 Bonifici riga 2
|
||
// Se il saldaconto e' attivo prende il conto relativo al tipo pagamento
|
||
// Altrimenti sempre la riga 2
|
||
if (sc_enabled())
|
||
{
|
||
int tipopag = doc.pagamento().tipo_rata(0); // Quello della prima rata... prolly is right .. ;P
|
||
_caus->bill(tipopag>0 && tipopag<8 ? tipopag+1:2,_co_controp);
|
||
if (!_co_controp.ok())
|
||
_caus->bill(2,_co_controp);
|
||
}
|
||
else
|
||
_caus->bill(2,_co_controp);
|
||
if (!_co_controp.ok())
|
||
_error = counter_p_ant_error;
|
||
return _error;
|
||
}
|
||
|
||
error_type TContabilizzazione::compile_rows_anticipo(TDocumento& doc)
|
||
{
|
||
// Per compilare le righe ci si ispira alla contabilizzazione effetti
|
||
// per reperire il conto clienti si guarda prima sul record cliente
|
||
// e poi al limite la riga della causale
|
||
// per il conto di contropartita se non c'e' il saldaconto lo si prende dalla
|
||
// seconda riga. Se il saldaconto esiste si consulta la riga relativa al
|
||
// tipo di pagamento.
|
||
TString16 codpag(doc.get(DOC_CODPAG));
|
||
TString16 catven(doc.get(DOC_CATVEN));
|
||
if (search_clifo_bill(catven) == no_error && search_counter_bill(doc) == no_error)
|
||
{
|
||
TCurrency_documento importo(doc.get_real(DOC_IMPPAGATO), doc); importo.change_to_firm_val();
|
||
TLocalisamfile& mov = _anticipo->lfile();
|
||
TDate datareg = mov.get_date(MOV_DATAREG);
|
||
TString16 codes = mov.get(MOV_ANNOES);
|
||
const long numreg = mov.get_long(MOV_NUMREG);
|
||
TRectype& c_rec = _anticipo->cg(0);
|
||
// setta i valori per la riga cliente
|
||
c_rec.put(RMV_ANNOES,codes);
|
||
c_rec.put(RMV_DATAREG,datareg);
|
||
c_rec.put(RMV_NUMREG,numreg);
|
||
c_rec.put(RMV_NUMRIG,1);
|
||
c_rec.put(RMV_SEZIONE,_caus->sezione_clifo());
|
||
c_rec.put(RMV_TIPOC,_co_cliente.tipo());
|
||
c_rec.put(RMV_GRUPPO,_co_cliente.gruppo());
|
||
c_rec.put(RMV_CONTO,_co_cliente.conto());
|
||
c_rec.put(RMV_SOTTOCONTO,_co_cliente.sottoconto());
|
||
c_rec.put(RMV_GRUPPOC,_co_controp.gruppo());
|
||
c_rec.put(RMV_CONTOC,_co_controp.conto());
|
||
c_rec.put(RMV_SOTTOCONTOC,_co_controp.sottoconto());
|
||
c_rec.put(RMV_ROWTYPE,sc_enabled() ? "K" : " ");
|
||
c_rec.put(RMV_IMPORTO, importo.get_num());
|
||
|
||
TRectype& co_rec = _anticipo->cg(1);
|
||
// setta i valori per la riga contropartita
|
||
co_rec = c_rec; // Swappa i conti ed il numero di riga
|
||
co_rec.put(RMV_NUMRIG,2);
|
||
co_rec.put(RMV_TIPOC,"");
|
||
co_rec.put(RMV_SEZIONE,_caus->sezione_clifo() == 'A' ? 'D' : 'A');
|
||
co_rec.put(RMV_GRUPPO,_co_controp.gruppo());
|
||
co_rec.put(RMV_CONTO,_co_controp.conto());
|
||
co_rec.put(RMV_SOTTOCONTO,_co_controp.sottoconto());
|
||
co_rec.put(RMV_TIPOCC,_co_cliente.tipo());
|
||
co_rec.put(RMV_GRUPPOC,_co_cliente.gruppo());
|
||
co_rec.put(RMV_CONTOC,_co_cliente.conto());
|
||
co_rec.put(RMV_SOTTOCONTOC,_co_cliente.sottoconto());
|
||
co_rec.put(RMV_ROWTYPE,sc_enabled() ? "I" : " ");
|
||
}
|
||
return _error;
|
||
}
|
||
|
||
error_type TContabilizzazione::write_pagamento_anticipo(TDocumento& doc)
|
||
{
|
||
TLocalisamfile& mov = _anticipo->lfile();
|
||
const int anno = mov.get_date(MOV_DATAREG).year();
|
||
TString numpart(mov.get(MOV_NUMDOC)); // Nessun controllo se prot.iva o numdoc xche' tanto proviene dal mov precedentemente scritto
|
||
TPartita * partita = new TPartita(_co_cliente,anno,numpart);
|
||
partita->allinea();
|
||
TRiga_partite& riga_part = partita->new_row();
|
||
// Compila la riga di partita per il pagamento
|
||
riga_part.put(PART_TIPOMOV,_caus->tipomov());
|
||
riga_part.put(PART_TIPOPAG, 1 ); // Pagamento in contanti-> tipo 1
|
||
riga_part.put(PART_NREG,mov.get_long(MOV_NUMREG));
|
||
riga_part.put(PART_NUMRIG,1); // Riferimento alla riga 1 del movimento
|
||
riga_part.put(PART_DATAREG,mov.get_date(MOV_DATAREG));
|
||
riga_part.put(PART_DATADOC,mov.get_date(MOV_DATADOC));
|
||
riga_part.put(PART_DATAPAG,mov.get_date(MOV_DATADOC));
|
||
riga_part.put(PART_NUMDOC,numpart); // E' lo stesso riferimento
|
||
riga_part.put(PART_SEZ,sezione());
|
||
riga_part.put(PART_CODCAUS, _caus->codice());
|
||
|
||
|
||
TCurrency_documento impval(doc.get_real(DOC_IMPPAGATO), doc);
|
||
TCurrency_documento imp(impval); imp.change_to_firm_val();
|
||
real cambio = doc.get_real(DOC_CAMBIO);
|
||
TString16 val(doc.get(DOC_CODVAL));
|
||
TDate datacam(doc.get_date(DOC_DATACAMBIO));
|
||
const bool valuta = val.not_empty();
|
||
riga_part.put(PART_IMPORTO,imp.get_num());
|
||
riga_part.put(PART_IMPORTOVAL,impval.get_num());
|
||
riga_part.put(PART_IMPTOTDOC,imp.get_num());
|
||
riga_part.put(PART_IMPTOTVAL,impval.get_num());
|
||
riga_part.put(PART_CODVAL,val);
|
||
riga_part.put(PART_CAMBIO,cambio);
|
||
riga_part.put(PART_DATACAM,doc.get_date(DOC_DATACAMBIO));
|
||
riga_part.put(PART_TIPOCF,_co_cliente.tipo());
|
||
riga_part.put(PART_SOTTOCONTO,_co_cliente.sottoconto());
|
||
riga_part.put(PART_DESCR, mov.get(MOV_DESCR));
|
||
|
||
// Compila la riga di pagamento:
|
||
// reperire il numero della riga partita (nrigp) appena aggiunta
|
||
int nriga = (int) TPartita::UNASSIGNED, nrigp = riga_part.get_int(PART_NRIGA);
|
||
// Cerca la riga di partita con riferimento alla fattura che si sta pagando
|
||
for (int r = partita->last(); r > 0; r = partita->pred(r))
|
||
{
|
||
TRiga_partite& rpart = partita->riga(r);
|
||
if (rpart.is_fattura())
|
||
{
|
||
TString16 s1 = rpart.get(PART_NUMDOC); s1.trim();
|
||
TString16 s2 = numpart; s2.trim();
|
||
if (s1 == s2) // Bisogna tener conto dell'allineamento!
|
||
{
|
||
nriga = r;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
if (nriga == TPartita::UNASSIGNED)
|
||
{
|
||
delete partita;
|
||
return write_part_error;
|
||
}
|
||
|
||
// Scorre le scadenze di questa fattura, e ne completa il pagamento partendo
|
||
// dalla rata piu' vecchia.
|
||
TRiga_partite& rpp = partita->riga(nriga);
|
||
real abb;
|
||
real ipp = (real) (valuta ? impval.get_num() : imp.get_num());
|
||
TValuta tval(val, datacam, cambio);
|
||
char old_ap, new_ap;
|
||
TImporto old_abb, old_diffcam, new_abb, new_diffcam;
|
||
const int nrate = rpp.rate();
|
||
const bool is_saldo_doc = doc.get_bool(DOC_ACCSALDO);
|
||
char s_a;
|
||
for (int i=1; i<=nrate; i++)
|
||
{
|
||
TRiga_scadenze& rs = rpp.rata(i);
|
||
real res = rs.residuo(valuta).valore(); // Sul residuo da pagare per questa rata
|
||
TRectype& riga_pagamento = partita->pagamento(nriga,i,nrigp); // Nuova riga di pagamento
|
||
// Compila la riga... e scala l'importo residuo partendo dalla piu' vecchia
|
||
// setta i flags di saldato/acconto
|
||
// ANNO, NUMPART, NRIGA, NRATA, NRIGP dovrebbero essere gia' compilati
|
||
riga_pagamento.put(PAGSCA_TIPOC,_co_cliente.tipo());
|
||
riga_pagamento.put(PAGSCA_SOTTOCONTO,_co_cliente.sottoconto());
|
||
s_a = 'S';
|
||
if (ipp <= ZERO)
|
||
ipp = ZERO;
|
||
if (res > ipp || i == nrate) // sull'ultima rata mette tutto il resto...
|
||
{
|
||
res = ipp;
|
||
imp = 0.0;
|
||
if (!is_saldo_doc)
|
||
s_a = 'A';
|
||
}
|
||
else
|
||
ipp -= res;
|
||
riga_pagamento.put(PAGSCA_ACCSAL, s_a);
|
||
riga_pagamento.put(valuta ? PAGSCA_IMPORTOVAL : PAGSCA_IMPORTO, res);
|
||
|
||
if (valuta)
|
||
{
|
||
// Guy was here
|
||
// riga_pagamento.put(PAGSCA_IMPORTO, ((real)(res*cambio)).round());
|
||
TCurrency_documento impval(res);
|
||
impval.change_to_firm_val();
|
||
riga_pagamento.put(PAGSCA_IMPORTO, impval.get_num());
|
||
}
|
||
riga_pagamento.put(PAGSCA_TIPOCC,_co_controp.tipo());
|
||
riga_pagamento.put(PAGSCA_GRUPPOC,_co_controp.gruppo());
|
||
riga_pagamento.put(PAGSCA_CONTOC,_co_controp.conto());
|
||
riga_pagamento.put(PAGSCA_SOTTOCONTC,_co_controp.sottoconto());
|
||
|
||
TRectype rp(riga_pagamento);
|
||
if (is_saldo_doc)
|
||
rp.put(PAGSCA_ACCSAL, 'S');
|
||
|
||
partita->modifica_pagamento(rp, tval, old_ap, old_abb, old_diffcam, new_ap, new_abb, new_diffcam, TRUE);
|
||
abb += new_abb.valore();
|
||
}
|
||
|
||
// Vediamo se vi sono abbuoni, in questo caso si completa il movimento
|
||
// contabile di anticipo, con la riga appropriata, di abbuoni attivi/passivi cambiamo pure la riga relativa
|
||
// all'importo cliente, ovvero lo settiamo al valore totale della fattura... ;)
|
||
// Dentro new_ap avremo se l'abbuono e' attivo o passivo, in new_abb avremo l'importo
|
||
TBill abb_bill;
|
||
if (abb != ZERO)
|
||
{
|
||
// Reperisce il conto di contropartita per gli abbuoni dalla causale: 9 passivi 10 attivi
|
||
const int nrigc = new_ap == 'A' ? RIGA_ABBUONI_ATTIVI : RIGA_ABBUONI_PASSIVI;
|
||
_caus->bill(nrigc, abb_bill);
|
||
if (abb_bill.ok())
|
||
{
|
||
TRectype& first_row = _anticipo->cg(0); // Riga cli/fo
|
||
first_row.put(RMV_IMPORTO, rpp.get_real(PART_IMPTOTDOC)); // Aggiusta l'importo totale
|
||
TRectype& abb_row = _anticipo->cg(2); // Riga abbuoni ...
|
||
abb_row = first_row;
|
||
abb_row.put(RMV_NUMRIG,3);
|
||
abb_row.put(RMV_TIPOC,"");
|
||
|
||
char ab_sez = 'D';
|
||
const char cf_sez = _caus->sezione_clifo();
|
||
if (cf_sez == 'D' && new_ap == 'P' ||
|
||
cf_sez == 'A' && new_ap == 'A')
|
||
ab_sez = 'A';
|
||
abb_row.put(RMV_SEZIONE, ab_sez);
|
||
abb_row.put(RMV_GRUPPO,abb_bill.gruppo());
|
||
abb_row.put(RMV_CONTO,abb_bill.conto());
|
||
abb_row.put(RMV_SOTTOCONTO,abb_bill.sottoconto());
|
||
abb_row.put(RMV_TIPOCC,_co_cliente.tipo());
|
||
abb_row.put(RMV_GRUPPOC,_co_cliente.gruppo());
|
||
abb_row.put(RMV_CONTOC,_co_cliente.conto());
|
||
abb_row.put(RMV_SOTTOCONTOC,_co_cliente.sottoconto());
|
||
abb_row.put(RMV_IMPORTO, abs(abb));
|
||
abb_row.put(RMV_ROWTYPE, new_ap);
|
||
}
|
||
else
|
||
_error = cau_abb_error;
|
||
}
|
||
|
||
// Infine scrive sta partita...
|
||
if (good() && !partita->write())
|
||
//error_box("Si e' verificato un errore scrivendo il pagamento del movimento di anticipo");
|
||
_error = write_part_error;
|
||
delete partita;
|
||
return _error;
|
||
}
|
||
|
||
error_type TContabilizzazione::write_anticipo(TDocumento& doc)
|
||
{
|
||
// Questo movimento viene scritto solo se e' stato versato un anticipo di pagamento
|
||
// Crea 1 testata, 1 riga contabile e la relativa riga di pagamento sulla partita a cui fa riferimento
|
||
// La testata e' praticamente la stessa del movimento appena scritto, tranne che per il numero di
|
||
// protocollo, il numero di registrazione la descrizione, i totali in lire/valuta,
|
||
// la condizione pagamento...
|
||
_error = no_error;
|
||
|
||
_error = compile_head_anticipo(doc);
|
||
if (_error == no_error)
|
||
{
|
||
// Una volta compilata la testa... compila le righe...
|
||
if (_anticipo)
|
||
{
|
||
_error = compile_rows_anticipo(doc);
|
||
if (_error == no_error)
|
||
{
|
||
// Scrive il movimento... gestendo la rinumerazione
|
||
if (sc_enabled())
|
||
// ... il pagamento vero e proprio, scrivendo la partita e modificando
|
||
// il movimento se vi sono abbuoni
|
||
write_pagamento_anticipo(doc);
|
||
|
||
TSaldo_agg saldo;
|
||
|
||
int err = NOERR;
|
||
if (doc_contabilized(doc, TRUE))
|
||
{
|
||
TMovimentoPN oldant;
|
||
oldant.curr().put(MOV_NUMREG, doc.get(DOC_NUMANT));
|
||
if (oldant.read() == NOERR)
|
||
aggiorna_saldi(saldo, oldant, FALSE);
|
||
err = _anticipo->rewrite();
|
||
}
|
||
else
|
||
err = _anticipo->write();
|
||
if (err != NOERR)
|
||
{
|
||
error_box("Errore %d scrivendo il movimento di anticipo pagamento %ld.",
|
||
err, _anticipo->curr().get_long(MOV_NUMREG));
|
||
return generic_error;
|
||
}
|
||
aggiorna_saldi(saldo, *_anticipo, TRUE);
|
||
}
|
||
delete _anticipo;
|
||
_anticipo = NULL;
|
||
}
|
||
}
|
||
|
||
return _error;
|
||
}
|
||
|
||
error_type TContabilizzazione::write_intra(TDocumento& doc)
|
||
{
|
||
// Scrive il movimento INTRA raggruppando per NATURA+NOMENCLATURA+CONSEGNA+TRASPORTO+PAESE+PAESEORIG+PROV
|
||
// Siccome NATURA, CONSEGNA, TRASPORTO e PAESE sono relativi alla testata del documento,
|
||
// il raggruppamento effettivo viene fatto solo per NOMENCLATURA+PAESEORIG+PROV
|
||
// NB: per i profani (come me :P) NOMENCLATURA COMBINATA e CLASSE DOGANALE sono la medesima cosa.
|
||
|
||
TLocalisamfile& intra = *_intra;
|
||
|
||
// Un po' di cache...
|
||
TDB_cache& cchh = cache();
|
||
TRecord_array rintra(LF_RINTRA, "NUMRIG");
|
||
TAssoc_array righe;
|
||
|
||
const TRectype& rm = _movimento->lfile().curr();
|
||
const TString16 codvali(rm.get(MOV_CODVALI));
|
||
const real cambioi = rm.get_real(MOV_CAMBIOI);
|
||
const TRectype& por_rec = cchh.get("%POR", doc.get(DOC_CODPORTO));
|
||
const TString16 consegna(por_rec.get("S3")); // condizioni di consegna
|
||
const int trasporto = por_rec.get_int("I0"); // tipo di trasporto...
|
||
const char natura = doc.tipo().get("S3")[3]; // natura della transazione, valida per tutte le righe del documento
|
||
const long numreg = rm.get_long(MOV_NUMREG);
|
||
TString16 nomenclatura, ums, paeseorig, provincia, paese;
|
||
real totale_righe, massanun, unsuppun;
|
||
const real cambio = doc.cambio();
|
||
const bool is_val = doc.in_valuta();
|
||
const bool is_cessione = rm.get_char(MOV_TIPO)=='C';
|
||
|
||
paese = _clifo->lfile().get(CLI_STATOPAIV); // Paese del cliente/fornitore...
|
||
|
||
// Scorre le righe documento (prendendo solamente quelle relative agli articoli veri e propri)
|
||
// Effettuando il raggruppamento in un comodo TAssoc_array
|
||
TToken_string key;
|
||
const int items = doc.physical_rows();
|
||
int numrig = 1;
|
||
for (int i=1; i<=items; i++)
|
||
{
|
||
const TRiga_documento& rr = doc[i];
|
||
if (rr.is_articolo())
|
||
{
|
||
const TRectype& rec_anamag = cchh.get(LF_ANAMAG, rr.get(RDOC_CODARTMAG));
|
||
nomenclatura = rec_anamag.get(ANAMAG_CLASSDOG);
|
||
ums = cchh.get("%NOC", nomenclatura, "S5");
|
||
massanun = rec_anamag.get_real(ANAMAG_MASSANUN); // Massa KG
|
||
unsuppun = rec_anamag.get_real(ANAMAG_UNSUPPUN); // Massa UMS
|
||
paeseorig = !is_cessione ? rec_anamag.get(ANAMAG_PAESE) : EMPTY_STRING; // Campo solo per Acquisti
|
||
if (is_cessione)
|
||
provincia = rec_anamag.get(ANAMAG_PROV);
|
||
else
|
||
{
|
||
const TString8 codmag = rr.get(RDOC_CODMAG);
|
||
TString4 com = cchh.get("MAG", codmag, "S5"); // Comune del magazzino
|
||
if (com.empty())
|
||
{
|
||
const long codditta = prefix().get_codditta();
|
||
TString8 codulc; codulc.format("%ld|1", codditta);
|
||
const TRectype& unloc = cchh.get(LF_UNLOC, codulc);
|
||
com = unloc.get(ULC_COMULC);
|
||
}
|
||
key.format(" |%s", (const char*)com);
|
||
provincia = cchh.get(LF_COMUNI, key, COM_PROVCOM);
|
||
}
|
||
|
||
if (nomenclatura.blank() || unsuppun.is_zero() || massanun.is_zero() || provincia.blank())
|
||
{
|
||
TString msg;
|
||
msg << "--- L'articolo " << rec_anamag.get(ANAMAG_CODART) << " non riporta tutti i dati necessari per il movimento intracomunitario.";
|
||
_viswin->add_line(msg);
|
||
msg = " Si consiglia di verificare i seguenti valori sull'anagrafica:";
|
||
_viswin->add_line(msg);
|
||
msg = " ";
|
||
if (nomenclatura.blank()) msg << "nomenclatura combinata; ";
|
||
if (unsuppun.is_zero()) msg << "unit<EFBFBD> di misura supplementare; ";
|
||
if (massanun.is_zero()) msg << "massa netta unitaria; ";
|
||
if (provincia.blank()) msg << (is_cessione ? "provincia d'origine; " : "provincia di destinazione; ");
|
||
msg.rtrim(2); msg << '.'; // Sostituisce l'ultimo punto e virgola con un punto
|
||
_viswin->add_line(msg);
|
||
}
|
||
|
||
key.cut(0);
|
||
key.add(nomenclatura);
|
||
key.add(paeseorig);
|
||
key.add(provincia);
|
||
TRectype* rc = (TRectype*) righe.objptr(key);
|
||
if (rc == NULL)
|
||
{
|
||
rc = new TRectype(LF_RINTRA);
|
||
rc->put("NUMREG", numreg);
|
||
rc->put("NUMRIG", numrig++);
|
||
rc->put("NATURA", natura);
|
||
rc->put("CONSEGNA", consegna);
|
||
rc->put("TRASPORTO", trasporto);
|
||
rc->put("UMS", ums);
|
||
rc->put("NOMENCL", nomenclatura);
|
||
rc->put("PAESE", paese);
|
||
rc->put("PAESEORIG", paeseorig);
|
||
rc->put("PROV", provincia);
|
||
righe.add(key, rc);
|
||
}
|
||
const real qta = rr.get(RDOC_QTA);
|
||
TCurrency_documento imp_val(rr.exist("VALINTRA") ? rr.get_real("VALINTRA") :rr.importo(TRUE, FALSE), doc);
|
||
TCurrency_documento imp(imp_val); imp.change_to_firm_val();
|
||
imp_val.change_value(codvali, cambioi);
|
||
real ammlire = rc->get("AMMLIRE");
|
||
real ammvaluta = rc->get("AMMVALUTA");
|
||
real massakg = rc->get("MASSAKG");
|
||
real massaums = rc->get("MASSAUMS");
|
||
real valstat = rc->get("VALSTAT");
|
||
|
||
ammlire += imp.get_num();
|
||
ammvaluta += is_val ? imp_val.get_num() : ZERO;
|
||
massakg += qta * massanun;
|
||
massaums += qta * unsuppun;
|
||
|
||
if (rr.exist("VALSTAT"))
|
||
{
|
||
TCurrency_documento vstat(rr.get_real("VALSTAT"), doc);
|
||
vstat.change_to_firm_val();
|
||
valstat += vstat.get_num();
|
||
}
|
||
else
|
||
valstat += qta * rec_anamag.get_real(ANAMAG_VALSTATUN);
|
||
|
||
rc->put("AMMLIRE", ammlire);
|
||
rc->put("AMMVALUTA", ammvaluta);
|
||
rc->put("MASSAKG", massakg);
|
||
rc->put("MASSAUMS", massaums);
|
||
rc->put("VALSTAT", valstat);
|
||
|
||
totale_righe += imp.get_num(); // Il totale delle righe in Euro!
|
||
}
|
||
}
|
||
|
||
if (_error == no_error)
|
||
{
|
||
// Copia il contenuto dell'assoc nel record array
|
||
TRectype* rc = new TRectype(LF_RINTRA);
|
||
rc->put("NUMREG", numreg);
|
||
rintra.set_key(rc);
|
||
for(rc = (TRectype*) righe.first_item(); rc != NULL; rc = (TRectype*) righe.succ_item())
|
||
rintra.add_row(*rc); // Devo aggiungere una copia della riga dell'assoc array!
|
||
// Testa (de coccio...)
|
||
intra.zero();
|
||
intra.put("NUMREG", numreg);
|
||
intra.put("DATAREG", rm.get_date(MOV_DATAREG));
|
||
intra.put("TIPOMOV", is_cessione ? 'C' : 'A'); // 'C' cessione 'A' acquisto
|
||
intra.put("TIPOCF", rm.get(MOV_TIPO));
|
||
intra.put("CODCF", rm.get_long(MOV_CODCF));
|
||
intra.put("TOTDOC", rm.get_real(MOV_TOTDOC));
|
||
intra.put("TOTDOCIMM", totale_righe);
|
||
intra.put("CODVAL", codvali);
|
||
intra.put("CAMBIO", cambioi);
|
||
if (intra.write() == _isreinsert) // Succede con le ricontabilizzazioni
|
||
intra.rewrite(); // si effettua una riscrittura
|
||
|
||
if (intra.status() == NOERR)
|
||
{
|
||
// righe!
|
||
if (rintra.write(TRUE) != NOERR) // Forza la riscrittura se necessario
|
||
_error = intra_mov_error;
|
||
}
|
||
else
|
||
_error = intra_mov_error;
|
||
}
|
||
|
||
return _error;
|
||
}
|
||
|
||
void TContabilizzazione::aggiorna_saldi(TSaldo_agg& saldo, TMovimentoPN& mv, bool save)
|
||
{
|
||
const TRectype& mov = mv.curr();
|
||
const TDate datareg = mov.get(MOV_DATAREG);
|
||
const TString8 codcaus = mov.get(MOV_CODCAUS);
|
||
|
||
tiposal tsal = normale;
|
||
if (codcaus != _caus->codice()) // Should never happen, but ...
|
||
{
|
||
const TCausale caus(codcaus, datareg.year());
|
||
tsal = caus.apertura() ? apertura : (caus.chiusura() ? chiusura : normale);
|
||
}
|
||
else
|
||
tsal = _caus->apertura() ? apertura : (_caus->chiusura() ? chiusura : normale);
|
||
|
||
if (save)
|
||
saldo.reset(); // Inizializza saldi
|
||
saldo.set_movprovv(FALSE);
|
||
saldo.set_tipo_saldo(tsal);
|
||
saldo.set_anno_es(mov.get_int(MOV_ANNOES));
|
||
saldo.set_num_ulmov(mov.get_long(MOV_NUMREG));
|
||
saldo.set_data_ulmov(datareg);
|
||
saldo.set_movimentato(TRUE);
|
||
|
||
const int cgitems = mv.cg_items();
|
||
for (int i = 0; i < cgitems; i++)
|
||
{
|
||
const TRectype& r = mv.cg(i);
|
||
TBill conto; conto.get(r);
|
||
TImporto import(r.get_char("SEZIONE"), r.get_real("IMPORTO"));
|
||
saldo.aggiorna(conto, import, save);
|
||
}
|
||
if (save)
|
||
saldo.registra();
|
||
}
|
||
|
||
void TContabilizzazione::display_error(TDocumento& doc)
|
||
{
|
||
TToken_string msg(256, '.');
|
||
const TString16 numerazione = doc.numerazione();
|
||
const long numero = doc.numero();
|
||
const TString16 causale = _caus == NULL ? "" : _caus->codice();
|
||
|
||
switch (_error)
|
||
{
|
||
case nr_es_error:
|
||
msg.format("Rilevato un codice esercizio errato contabilizzando il documento %s/%ld."
|
||
"Verificare l'esistenza e la correttezza della tabella esercizi e della data del documento.",(const char*)numerazione,numero);
|
||
break;
|
||
case nr_reg_error:
|
||
msg.format("Rilevato un numero di registrazione errato contabilizzando il documento %s/%ld."
|
||
"Verificare l'integrita' del file movimenti.",(const char*)numerazione,numero);
|
||
break;
|
||
case nr_doc_error:
|
||
msg.format("Rilevato un numero di documento errato contabilizzando il documento %s/%ld."
|
||
"Verificare il numero documento e il codice numerazione inseriti in tabella.",(const char*)numerazione,numero);
|
||
break;
|
||
case chg_stat_error:
|
||
msg.format("Rilevato un errore cambiando lo stato al documento %s/%ld."
|
||
"Verificare l'integrita' del file documenti.",(const char*)numerazione,numero);
|
||
break;
|
||
case clifo_error:
|
||
msg.format("Rilevato un errore caricando le informazioni del Cli/Fo sul documento %s/%ld."
|
||
"Verificare l'esistenza delle informazioni inserite sul file documenti e Cli/Fo.",(const char*)numerazione,numero);
|
||
break;
|
||
case ultprot_error:
|
||
msg.format("Rilevato un numero di protocollo IVA errato relativamente al documento %s/%ld."
|
||
"Verificare le informazioni inserite sul registro %s/%d.",(const char*)numerazione,numero,
|
||
(const char*) _caus->reg().name(),_caus->reg().year());
|
||
break;
|
||
case datadoc_error:
|
||
msg.format("Rilevato una data documento vuota relativamente al documento %s/%ld."
|
||
"Verificare l'informazione inserita.",(const char*)numerazione,numero);
|
||
break;
|
||
case caus_ant_error:
|
||
msg.format("Rilevato un errore caricando la causale per anticipo pagamento relativamente al documento %s/%ld."
|
||
"Verificare l'esistenza del codice causale '%s'.",(const char*)numerazione,numero,(const char*)causale);
|
||
break;
|
||
case counter_p_ant_error:
|
||
msg.format("Rilevato un errore cercando il conto di contropartita per il movimento di anticipo relativamente al documento %s/%ld."
|
||
"Verificare l'esistenza e la correttezza della causale '%s'.",(const char*)numerazione,numero,(const char*)causale);
|
||
break;
|
||
case caus_error:
|
||
msg.format("Rilevato un errore caricando la causale relativamente al documento %s/%ld."
|
||
"Verificare l'esistenza del codice causale '%s' e del relativo registro.",
|
||
(const char*)numerazione,numero,(const char*)causale);
|
||
break;
|
||
case causre_error:
|
||
msg.format("Rilevato un errore caricando la causale relativamente al documento %s/%ld."
|
||
"Non deve essere una causale IVA",
|
||
(const char*)numerazione,numero,(const char*)causale);
|
||
break;
|
||
case cauval_error:
|
||
msg.format("Il documento %s/%ld risulta essere in valuta mentre la causale non la supporta."
|
||
"Verificare la correttezza della causale '%s'.",(const char*)numerazione,numero,(const char*)causale);
|
||
break;
|
||
case ivasto_error:
|
||
msg.format("Impossibile determinare il codice IVA di storno per articoli omaggio relativamente al documento %s/%ld."
|
||
"Verificare la configurazione contabilizzazione.",(const char*)numerazione,numero);
|
||
break;
|
||
case register_error:
|
||
msg.format("Rilevato un errore caricando il registro relativamente al documento %s/%ld."
|
||
"Verificare la correttezza della causale '%s' e relativo registro.",(const char*)numerazione,numero,(const char*)causale);
|
||
break;
|
||
case change_error:
|
||
msg.format("Rilevato un cambio senza valuta relativamente al documento %s/%ld."
|
||
"Verificare la correttezza delle informazioni inserite.",(const char*)numerazione,numero);
|
||
break;
|
||
case val_error:
|
||
msg.format("Rilevato un codice valuta inesistente relativamente al documento %s/%ld."
|
||
"Verificare la correttezza della informazione inserita.",(const char*)numerazione,numero);
|
||
break;
|
||
case codpag_error:
|
||
msg.format("Rilevato un codice pagamento non esistente relativamente al documento %s/%ld."
|
||
"Verificare l'esistenza del codice pagamento inserito.",(const char*)numerazione,numero);
|
||
break;
|
||
case row_type_error:
|
||
msg.format("Rilevato un codice tipo riga non esistente relativamente al documento %s/%ld."
|
||
"Verificare l'esistenza dei vari codici riga inseriti.",(const char*)numerazione,numero);
|
||
break;
|
||
case no_rows_error:
|
||
msg.format("Nessuna riga iva contabile e' stata trovata relativamente al documento %s/%ld."
|
||
"Verificare l'esistenza dei vari codici riga inseriti.",(const char*)numerazione,numero);
|
||
break;
|
||
case cau_ritintra_error:
|
||
msg.format("Conto per il'IVA intracomunitaria errato o mancante."
|
||
"Verificarlo sulla causale alla voce ritenute fiscali.\n");
|
||
break;
|
||
case conto_error:
|
||
msg.format("Rilevato un conto di costo/ricavo inesistente relativamente al documento %s/%ld."
|
||
"Verificare l'esistenza del conto %c %d %d %ld associato alle righe.",
|
||
(const char*)numerazione,numero,
|
||
_conto_errato.tipo(), _conto_errato.gruppo(), _conto_errato.conto(), _conto_errato.sottoconto());
|
||
break;
|
||
case sconto_error:
|
||
msg.format("Non sono stati impostati i conti per la contabilizzazione degli sconti relativamente al documento %s/%ld."
|
||
"Verificare i parametri in configurazione contabilizzazione.",(const char*)numerazione,numero);
|
||
break;
|
||
case spinbo_error:
|
||
msg.format("Non sono stati impostati i conti per la contabilizzazione delle spese incasso e bolli relativamente al documento %s/%ld."
|
||
"Verificare i parametri in configurazione contabilizzazione.",(const char*)numerazione,numero);
|
||
break;
|
||
case movement_error:
|
||
msg.format("Rilevato uno sbilancio nel movimento relativamente al documento %s/%ld."
|
||
"Verificare la correttezza degli importi delle righe.",(const char*)numerazione,numero);
|
||
break;
|
||
case write_error:
|
||
msg.format("Rilevato un errore in scrittura movimento relativamente al documento %s/%ld."
|
||
"Verificare la consistenza dei files.",(const char*)numerazione,numero);
|
||
break;
|
||
case write_part_error:
|
||
msg.format("Rilevato un errore in scrittura partite relativamente al documento %s/%ld."
|
||
"Verificare la consistenza dei files.",(const char*)numerazione,numero);
|
||
break;
|
||
case cau_abb_error:
|
||
msg.format("Mancano i conti per gli abbuoni nella causale indicata per il pagamento anticipo relativamente al documento %s/%ld."
|
||
"Verificare la correttezza della causale.",(const char*)numerazione,numero);
|
||
break;
|
||
case intra_mov_error:
|
||
msg.format("Si <20> verificato un errore nella scrittura del movimento intracomunitario relativamente al documento %s/%ld."
|
||
"Verificare la consistenza dei files relativi ai movimenti intracomunitari.",(const char*)numerazione,numero);
|
||
break;
|
||
case cont_seq_error:
|
||
msg.format("Il documento precedente al %s/%ld non e' stato contabilizzato."
|
||
"E' necessario contabilizzare tutti i documenti in sequenza.",
|
||
(const char*)numerazione, numero);
|
||
break;
|
||
default: // errori generici o non indicati vengono visualizzati nel punto dell'errore
|
||
//msg.format("E' stato rilevato un errore generico contabilizzando il documento %s/%ld.",
|
||
// (const char*)numerazione,numero);
|
||
break;
|
||
}
|
||
|
||
if (_viswin)
|
||
{
|
||
TString riga;
|
||
FOR_EACH_TOKEN(msg, line)
|
||
{
|
||
if (*line)
|
||
{
|
||
riga = riga.empty() ? "*** " : " ";
|
||
riga << line << '.';
|
||
_viswin->add_line(riga);
|
||
}
|
||
}
|
||
}
|
||
else
|
||
error_box(msg);
|
||
_error = no_error; // reset error, as any other one would do, so you can show me the other ones.
|
||
_can_write = FALSE; // But from now on u cannot write anymore. U must exit this program and repair errors occurred.
|
||
}
|
||
|
||
bool TContabilizzazione::sc_enabled() const
|
||
{
|
||
bool rt = _sc_enabled;
|
||
if (_caus != NULL) rt &= _caus->saldaconto();
|
||
if (_clifo != NULL) rt &= !_clifo->lfile().get_bool(CLI_OCCAS); // Saldaconto solo se C/F non occasionale
|
||
return rt;
|
||
}
|
||
|
||
bool TContabilizzazione::in_enabled() const
|
||
{
|
||
bool rt = _in_enabled;
|
||
if (_caus != NULL) rt &= _caus->intra();
|
||
return rt;
|
||
}
|
||
|
||
bool TContabilizzazione::prev_contabilized(const TDocumento& doc) const
|
||
{
|
||
const TString16 codnum = doc.get(DOC_CODNUM);
|
||
const int anno = doc.get_int(DOC_ANNO);
|
||
const char provv = doc.get_char(DOC_PROVV);
|
||
long ndoc = doc.get_long(DOC_NDOC);
|
||
|
||
TLocalisamfile documenti(LF_DOC);
|
||
TRectype& rec = documenti.curr();
|
||
rec.put(DOC_CODNUM, codnum);
|
||
rec.put(DOC_ANNO, anno);
|
||
rec.put(DOC_PROVV, provv);
|
||
rec.put(DOC_NDOC, ndoc-1);
|
||
int err = rec.read(documenti);
|
||
if (err != NOERR)
|
||
{
|
||
rec.zero();
|
||
rec.put(DOC_CODNUM, codnum);
|
||
rec.put(DOC_ANNO, anno);
|
||
rec.put(DOC_PROVV, provv);
|
||
err = rec.read(documenti, _isgteq);
|
||
return err == NOERR && rec.same_key(doc.head(), 1);
|
||
}
|
||
|
||
const TString16 stato_doc = rec.get(DOC_STATO);
|
||
const TString16 stato_ok = stato_finale_doc_iniziale();
|
||
return stato_doc == stato_ok;
|
||
}
|
||
|
||
static void link_handler(int n, const char* nreg)
|
||
{
|
||
if (n == 0)
|
||
{
|
||
TRectype mov(LF_MOV);
|
||
mov.put(MOV_NUMREG, nreg);
|
||
if (!mov.edit())
|
||
{
|
||
// Vecchio modo obsoleto
|
||
const char* app = "cg2 -0";
|
||
TString body; body << "1|" << nreg;
|
||
TMessage msg(app, MSG_LN, body);
|
||
msg.send();
|
||
TExternal_app a(app);
|
||
a.run();
|
||
}
|
||
}
|
||
}
|
||
|
||
bool TContabilizzazione::elabora(TLista_documenti& doc_in, TLista_documenti& doc_out,
|
||
const TDate& data_elab, bool interattivo)
|
||
{
|
||
TString msg;
|
||
|
||
_error = no_error;
|
||
_total_docs = 0L;
|
||
_caus = NULL;
|
||
_data_reg = data_elab;
|
||
|
||
if (interattivo)
|
||
{
|
||
_auto_data = TRUE;
|
||
_nump_iva = 1;
|
||
}
|
||
|
||
_esc.update();
|
||
_cpg->setkey(1); // Setta per sicurezza la chiave 1 nel caso l'elaborazione sia invocata da VE0
|
||
|
||
if (!load_parameters()) // Carica i parametri dalla configurazione
|
||
return FALSE;
|
||
|
||
TPrinter& p = printer();
|
||
p.links().add("Movimento Prima Nota |b|w", 0);
|
||
p.setlinkhandler(link_handler);
|
||
_viswin = new TViswin(NULL, TR("Contabilizzazione documenti"), FALSE, TRUE, TRUE);
|
||
_viswin->open_modal();
|
||
|
||
const clock_t start_time = clock();
|
||
|
||
long txt_scrolled = -1;
|
||
const int items = doc_in.items(); // Numero dei documenti in questa elaborazione
|
||
|
||
if (items > 0)
|
||
{
|
||
const bool acquisto = doc_in[0].get_char(DOC_TIPOCF) == 'F';
|
||
if (acquisto)
|
||
_check_prev_cont = FALSE;
|
||
}
|
||
|
||
for (int i = 0; i < items; i++) // Scorriamo tutti i documenti nella lista
|
||
{
|
||
if (_viswin->frozen())
|
||
break;
|
||
|
||
const long txt_pos = _viswin->lines();
|
||
|
||
TDocumento& doc = doc_in[i];
|
||
|
||
msg = TR("Elaborazione del documento");
|
||
msg << ' ' << doc.anno() << ' ';
|
||
msg << doc.numerazione() << '/';
|
||
msg << doc.numero();
|
||
_viswin->add_line(msg);
|
||
|
||
if (i > 0)
|
||
{
|
||
const clock_t time = (clock() - start_time) / CLOCKS_PER_SEC;
|
||
if (time > 0)
|
||
{
|
||
TString80 stats;
|
||
const int min = int(time / 60L);
|
||
const int sec = int(time % 60L);
|
||
stats.format(" (docs=%d time=%d:%02d docs/min=%ld)", i, min, sec, long(i*60L)/time);
|
||
msg << stats;
|
||
}
|
||
}
|
||
xvt_statbar_set(msg);
|
||
do_events();
|
||
|
||
_movimento = new TMovimentoPN_VE(doc.in_valuta());
|
||
|
||
if (_can_write && _check_prev_cont && !prev_contabilized(doc))
|
||
_error = cont_seq_error;
|
||
|
||
const TCodice_numerazione num(doc.numerazione());
|
||
const bool ft_em_ric = num.fattura_emettere_ricevere();
|
||
|
||
if (good())
|
||
{
|
||
if (ft_em_ric)
|
||
{
|
||
|
||
compile_head_mov_re(doc);
|
||
|
||
if (good())
|
||
compile_rows_mov_re(doc);
|
||
if (good() && _can_write)
|
||
write_all_re(doc, *_movimento); // Se la scrittura e' andata ok...
|
||
}
|
||
else
|
||
{
|
||
compile_head_mov(doc);
|
||
|
||
if (good())
|
||
compile_rows_mov(doc);
|
||
if (good() && _can_write)
|
||
write_all(doc, *_movimento); // Se la scrittura e' andata ok...
|
||
}
|
||
}
|
||
|
||
if (!good())
|
||
{
|
||
display_error(doc);
|
||
if (_error == movement_error || !_movimento->movement_ok())
|
||
{
|
||
TToken_string str(32, ' ');
|
||
msg.format("%24s %24s Conto", "Dare", "Avere");
|
||
_viswin->add_line(msg);
|
||
|
||
const int imax = _movimento->cg_items();
|
||
for (int i = 0; i < imax; i++)
|
||
{
|
||
const TRectype& r = _movimento->cg(i);
|
||
const char sez = r.get_char(RMV_SEZIONE);
|
||
const TCurrency imp(r.get_real(RMV_IMPORTO));
|
||
if (sez == 'D')
|
||
msg.format("%24s %24s", imp.string(TRUE), "");
|
||
else
|
||
msg.format("%24s %24s", "", imp.string(TRUE));
|
||
|
||
TBill bill(r);
|
||
str.cut(0);
|
||
bill.add_to(str, 0, 0x2);
|
||
msg << ' ' << str;
|
||
_viswin->add_line(msg);
|
||
}
|
||
}
|
||
if (txt_scrolled < 0)
|
||
{
|
||
txt_scrolled = txt_pos;
|
||
_viswin->goto_pos(txt_pos, 0);
|
||
do_events();
|
||
}
|
||
}
|
||
|
||
_viswin->add_line("");
|
||
if (_caus != NULL)
|
||
{
|
||
delete _caus;
|
||
_caus = NULL;
|
||
}
|
||
delete _movimento;
|
||
|
||
// Let's free some valuable space
|
||
if (!interattivo)
|
||
doc_in.destroy(i, FALSE);
|
||
}
|
||
_viswin->close_print();
|
||
_viswin->close_modal();
|
||
if (!interattivo)
|
||
{
|
||
if (_viswin->frozen())
|
||
message_box("Contabilizzazione interrotta dall'utente");
|
||
else
|
||
message_box("Contabilizzazione terminata");
|
||
}
|
||
KEY k = _viswin->run();
|
||
if (k == K_CTRL+'S') // Ho premuto Stampa
|
||
printer().print_txt(_viswin->text());
|
||
delete _viswin; _viswin = NULL;
|
||
|
||
return _can_write; // Se non ha riscontrato errori per nessun documento, _can_write = TRUE
|
||
}
|
||
|
||
bool TContabilizzazione::call_exe(const TDocumento& doc, const TMovimentoPN& movimento) const
|
||
{
|
||
TFilename ininame; ininame.temp();
|
||
if (!ininame.exist()) // Dummy test
|
||
{
|
||
TConfig ini(ininame, "Transaction");
|
||
ini.set("Action", "Contabilize");
|
||
|
||
TString8 para;
|
||
para.format("%d", LF_DOC);
|
||
ini.set_paragraph(para);
|
||
ini.set(DOC_PROVV, doc.get(DOC_PROVV));
|
||
ini.set(DOC_ANNO, doc.get(DOC_ANNO));
|
||
ini.set(DOC_CODNUM, doc.get(DOC_CODNUM));
|
||
ini.set(DOC_NDOC, doc.get(DOC_NDOC));
|
||
|
||
para.format("%d", LF_MOV);
|
||
ini.set_paragraph(para);
|
||
ini.set(MOV_NUMREG, movimento.curr().get(MOV_NUMREG));
|
||
}
|
||
|
||
TString shell;
|
||
shell << applicazione_esterna() << " /i" << ininame;
|
||
TExternal_app app(shell);
|
||
const bool ok = app.run() == 0;
|
||
|
||
if (ininame.exist())
|
||
::remove(ininame);
|
||
|
||
return ok;
|
||
}
|
||
|