2007-03-06 16:37:44 +00:00
# include "tp0100.h"
# include <expr.h>
2006-07-11 13:10:51 +00:00
# include <recarray.h>
# include <tabutil.h>
# include "..\pr\agenti.h"
# include <clifo.h>
# include <cfven.h>
# include <comuni.h>
# include <doc.h>
# include <indsp.h>
# include <mov.h>
///////////////////////////////////////////////////////////
// Cache banche
///////////////////////////////////////////////////////////
class TCache_ban : public TCache_tp
{
protected :
virtual TObject * key2obj ( const char * key ) ;
public :
virtual const TString & decode ( const TToken_string & tok ) ;
TCache_ban ( TPack_transfer * pt ) : TCache_tp ( pt ) { }
} ;
TObject * TCache_ban : : key2obj ( const char * key )
{
TTable ban ( " %BAN " ) ;
TToken_string code ( key ) ;
TString8 abi ; code . get ( 0 , abi ) ;
TString8 cab ; code . get ( 1 , cab ) ;
ban . put ( " CODTAB " , abi ) ;
if ( ban . read ( ) ! = NOERR )
{
const TString & desc = get_str ( " BankName " ) ;
ban . put ( " CODTAB " , abi ) ;
ban . put ( " S0 " , desc ) ;
test_write ( ban ) ;
}
TString16 str ;
str < < abi ; str < < cab ;
ban . put ( " CODTAB " , str ) ;
if ( ban . read ( ) ! = NOERR )
{
const TString & desc = get_str ( " BankName " ) ;
ban . put ( " CODTAB " , str ) ;
ban . put ( " S0 " , desc ) ;
test_write ( ban ) ;
}
return ban . get ( " S0 " ) . dup ( ) ;
}
const TString & TCache_ban : : decode ( const TToken_string & tok )
{
return * ( const TString * ) objptr ( tok ) ;
}
///////////////////////////////////////////////////////////
// TPack_conti
///////////////////////////////////////////////////////////
bool TPack_conti : : trasferisci ( )
{
log ( TR ( " Azzeramento piano dei conti PACK " ) ) ;
odbc_exec ( " DELETE FROM AccountPlan " ) ;
TRecordset & pcon = create_recordset ( " USE PCON " ) ;
TPack_iterator pi ( this ) ;
while ( + + pi )
{
const int gruppo = pcon . get ( " GRUPPO " ) . as_int ( ) ;
const int conto = pcon . get ( " CONTO " ) . as_int ( ) ;
const long sottoc = pcon . get ( " SOTTOCONTO " ) . as_int ( ) ;
const long gruconto = gruppo * 1000 + conto ;
TToken_string values ;
values . add ( gruconto ) ;
values . add ( sottoc ) ;
values . add ( pcon . get ( " DESCR " ) . as_string ( ) ) ;
odbc_exec ( build_insert_query ( " AccountPlan " , " AccountCode,AccountSubCode,DescAccount " , values ) ) ;
}
return write_enabled ( ) ;
}
///////////////////////////////////////////////////////////
// TPack_pag
///////////////////////////////////////////////////////////
bool TPack_pag : : trasferisci ( )
{
log ( TR ( " Azzeramento tabella pagamenti PACK " ) ) ;
odbc_exec ( " DELETE FROM General_Data WHERE TablesType='P' " ) ;
TRecordset & pag = create_recordset ( " USE %CPG " ) ;
TPack_iterator pi ( this ) ;
while ( + + pi )
{
TToken_string values ;
values . add ( " P " ) ;
values . add ( pag . get ( " CODTAB " ) . as_string ( ) ) ;
values . add ( pag . get ( " S0 " ) . as_string ( ) ) ;
const TString & query = build_insert_query ( " General_Data " , " TablesType,Code,Description1 " , values ) ;
odbc_exec ( query ) ;
}
return write_enabled ( ) ;
}
///////////////////////////////////////////////////////////
// Cache agenti
///////////////////////////////////////////////////////////
class TCache_agenti : public TCache_tp
{
protected :
virtual TObject * key2obj ( const char * key ) ;
public :
const TString & decode ( const TToken_string & tok ) ;
TCache_agenti ( TPack_transfer * pt ) : TCache_tp ( pt ) { }
} ;
TObject * TCache_agenti : : key2obj ( const char * key )
{
TLocalisamfile agenti ( LF_AGENTI ) ;
agenti . setkey ( 3 ) ;
agenti . put ( AGE_CODFORN , key ) ;
if ( agenti . read ( ) = = NOERR )
return agenti . get ( AGE_CODAGE ) . dup ( ) ;
long cod = 1 ;
int err = agenti . last ( ) ;
if ( err = = NOERR )
{
const char * codage = agenti . get ( AGE_CODAGE ) ;
if ( codage [ 0 ] = = ' Z ' )
cod + = atoi ( codage + 1 ) ;
}
TString8 codage ; codage . format ( " Z%04d " , cod ) ;
TString msg ; msg . format ( FR ( " Agente %s " ) , ( const char * ) codage ) ;
agenti . put ( AGE_CODAGE , codage ) ;
agenti . put ( AGE_RAGSOC , msg ) ;
agenti . put ( AGE_CODFORN , key ) ;
test_write ( agenti ) ;
return codage . dup ( ) ;
}
const TString & TCache_agenti : : decode ( const TToken_string & tok )
{
return * ( TString * ) objptr ( tok ) ;
}
///////////////////////////////////////////////////////////
// TPack_clifo
///////////////////////////////////////////////////////////
const TString & TPack_clifo : : decode_agente ( const long codforn )
{
if ( _agenti = = NULL )
_agenti = new TCache_agenti ( this ) ;
TToken_string key ;
key < < codforn ;
return _agenti - > decode ( key ) ;
}
const TString & TPack_clifo : : get_agente ( )
{
const TString & code = get_str ( " AgentCode " ) ;
if ( code . full ( ) )
{
TString query = query_header ( ) ;
query < < " SELECT CodContab FROM Customers_Suppliers WHERE (FlagCustSupp='A') AND (CustSuppCode=#CODCF) " ;
TODBC_recordset clifo ( query ) ;
clifo . set_var ( " #CODCF " , TVariant ( code ) ) ;
if ( clifo . move_first ( ) )
{
2007-01-03 16:42:42 +00:00
TToken_string codice ( clifo . get ( 0u ) . as_string ( ) ) ;
2006-07-11 13:10:51 +00:00
return get_tmp_string ( ) = codice . get ( 1 ) ;
}
else
{
TString msg ;
msg < < TR ( " Impossibile determinare il codice dell'agente " )
< < " ' " < < code < < ' \' ' ;
log_error ( msg ) ;
}
}
return EMPTY_STRING ;
}
int TPack_clifo : : cancella_clifo ( TLocalisamfile & clifo ) const
{
// verificare prima che il cliente sia cancellabile (MOV e DOC)
TRectype & rec_clifo = clifo . curr ( ) ;
const char tipocf = rec_clifo . get ( CLI_TIPOCF ) [ 0 ] ;
const long codcf = rec_clifo . get_long ( CLI_CODCF ) ;
// verifico MOV
TLocalisamfile mov ( LF_MOV ) ;
mov . setkey ( 3 ) ;
mov . curr ( ) . put ( MOV_TIPO , tipocf ) ;
mov . curr ( ) . put ( MOV_CODCF , codcf ) ;
mov . read ( ) ;
bool noncancellare = ( tipocf = = mov . get ( MOV_TIPO ) [ 0 ] & & codcf = = atol ( mov . get ( MOV_CODCF ) ) ) ;
if ( ! noncancellare )
{
// verifico DOC
TLocalisamfile doc ( LF_DOC ) ;
doc . setkey ( 2 ) ;
doc . curr ( ) . put ( DOC_TIPOCF , tipocf ) ;
doc . curr ( ) . put ( DOC_CODCF , codcf ) ;
doc . read ( ) ;
noncancellare = ( tipocf = = doc . get ( DOC_TIPOCF ) [ 0 ] & & codcf = = atol ( doc . get ( DOC_CODCF ) ) ) ;
}
if ( ! noncancellare )
{
if ( clifo . remove ( ) = = NOERR )
{
TLocalisamfile cfven ( LF_CFVEN ) ;
cfven . put ( CFV_TIPOCF , tipocf ) ;
cfven . put ( CFV_CODCF , codcf ) ;
cfven . remove ( ) ;
TString msg ; msg < < TR ( " Cancellato cliente " ) < < ' ' < < codcf ;
log ( msg , 1 ) ; // 0 = messaggio 1 = warning 2 = errore
}
else
{
TString msg ; msg < < TR ( " Errore di cancellazione del cliente " ) < < ' ' < < codcf ;
log ( msg , 1 ) ; // 0 = messaggio 1 = warning 2 = errore
}
}
return ( ( noncancellare ) ? 3 : 9 ) ;
}
long TPack_clifo : : get_next_key ( const char tipocf ) const
{
TLocalisamfile clifo ( LF_CLIFO ) ;
long codcf = 1L ;
if ( ! clifo . empty ( ) )
{
if ( tipocf = = ' C ' )
{
clifo . put ( CLI_TIPOCF , ' F ' ) ;
clifo . read ( _isgteq ) ;
if ( clifo . good ( ) )
clifo . prev ( ) ;
clifo . setstatus ( NOERR ) ;
}
else
clifo . last ( ) ;
if ( clifo . good ( ) )
{
const char tipo = clifo . get ( CLI_TIPOCF ) [ 0 ] ;
if ( tipocf = = tipo )
codcf + = clifo . get_long ( CLI_CODCF ) ;
}
}
return codcf ;
}
void TPack_clifo : : aggiorna_banca ( )
{
TToken_string abicab ;
abicab = get_str ( " ABICode " ) ;
abicab . add ( get_str ( " CABCode " ) ) ;
if ( ! abicab . empty_items ( ) )
{
if ( _banche = = NULL )
_banche = new TCache_ban ( this ) ;
_banche - > decode ( abicab ) ;
}
}
void TPack_clifo : : trasferisci_conai ( TRectype & rec )
{
const long applyenvtax = get_long ( " ApplyEnvTax " ) ;
2006-12-13 16:22:33 +00:00
rec . put ( CFV_ADDCONAI , applyenvtax > 0 ? " X " : " " ) ;
// Azzera tutte le esenzioni CONAI
rec . zero ( CFV_ESACC ) ; rec . zero ( CFV_ESALL ) ; rec . zero ( CFV_ESCAR ) ;
rec . zero ( CFV_ESLEG ) ; rec . zero ( CFV_ESPLA ) ; rec . zero ( CFV_ESVET ) ;
// Imposta le eventuali esenzioni CONAI
if ( applyenvtax = = 2 )
{
TString query ( 128 ) ;
query < < query_header ( ) ;
query < < " SELECT ClassCode, ApplyPercent FROM Customers_EnvironmentTax WHERE (CustCode=#CODCF) " ;
TODBC_recordset conai ( query ) ;
2006-07-11 13:10:51 +00:00
const TVariant & code = recordset ( ) . get ( " CustSuppCode " ) ;
conai . set_var ( " #CODCF " , code ) ;
2006-12-13 16:22:33 +00:00
for ( bool ok = conai . move_first ( ) ; ok ; ok = conai . move_next ( ) )
{
2006-07-11 13:10:51 +00:00
const TString & conai_class = conai . get ( " ClassCode " ) . as_string ( ) ;
if ( conai_class . not_empty ( ) )
{
const TString & conai_field = config ( ) . get ( conai_class , " CFV_CONAI " ) ;
if ( conai_field . not_empty ( ) )
{
const real applypercent = conai . get ( " ApplyPercent " ) . as_real ( ) ;
2006-12-13 16:22:33 +00:00
rec . put ( conai_field , applypercent ) ;
}
}
}
}
2006-07-11 13:10:51 +00:00
}
bool TPack_clifo : : trasferisci ( )
{
TString query =
" SELECT Customers_Suppliers.*, Unit_Measure.UMDesc "
" FROM Customers_Suppliers "
" LEFT JOIN Unit_Measure "
" ON (CurrencyCode = Unit_Measure.UMCode AND Unit_Measure.UMType='9') "
" WHERE (StatusFlag=1 OR StatusFlag=2 OR StatusFlag=3) AND " ;
if ( _only_agenti )
query < < " (FlagCustSupp='A') " ;
else
query < < " (FlagCustSupp='C' OR FlagCustSupp='S') " ;
TRecordset & recset = create_recordset ( query ) ;
TString str ;
TConfig & ini = config ( ) ;
TString_array lista_clifo , lista_cfven , lista_agenti ;
ini . list_variables ( lista_clifo , true , " CLIFO " , true ) ;
ini . list_variables ( lista_cfven , true , " CFVEN " , true ) ;
ini . list_variables ( lista_agenti , true , " AGENTI " , true ) ;
TLocalisamfile clifo ( LF_CLIFO ) ;
TLocalisamfile cfven ( LF_CFVEN ) ;
TRectype & rec_clifo = clifo . curr ( ) ;
TRectype & rec_cfven = cfven . curr ( ) ;
TRecord_cache cache_comuni ( LF_COMUNI , 2 ) ;
TPack_iterator pi ( this ) ;
while ( + + pi )
{
const TString & custsuppcode = get_str ( " CustSuppCode " ) ;
const int statusflag = get_long ( " StatusFlag " ) ;
const char flagcustsupp = get_str ( " FlagCustSupp " ) [ 0 ] ;
const char tipocf = ( flagcustsupp = = ' C ' ? ' C ' : ' F ' ) ; // C -> C (S,A) -> F
long codcf = get_long ( " CodContab " ) ;
bool needs_creation = codcf < = 0 ;
bool good = true ;
if ( ! needs_creation )
{
// il cliente/fornitore va aggiornato
rec_cfven . zero ( ) ;
rec_cfven . put ( CFV_TIPOCF , tipocf ) ;
rec_cfven . put ( CFV_CODCF , codcf ) ;
cfven . read ( ) ;
rec_clifo . zero ( ) ;
rec_clifo . put ( CLI_TIPOCF , tipocf ) ;
rec_clifo . put ( CLI_CODCF , codcf ) ;
good = clifo . read ( ) = = NOERR ;
if ( ! good )
needs_creation = true ;
}
if ( needs_creation & & statusflag ! = 3 )
{
// il cliente/fornitore va inserito in campo previo controllo piva
if ( codcf < = 0 )
codcf = get_next_key ( tipocf ) ;
rec_cfven . zero ( ) ;
rec_cfven . put ( CFV_TIPOCF , tipocf ) ;
rec_cfven . put ( CFV_CODCF , codcf ) ;
// dati standard solo se sono in inserimento
rec_cfven . put ( CFV_RAGGDOC , config ( ) . get ( " RAGGDOC " , " STANDARD " ) ) ;
rec_cfven . put ( CFV_RAGGEFF , config ( ) . get ( " RAGGEFF " , " STANDARD " ) ) ;
rec_cfven . put ( CFV_ADDBOLLI , config ( ) . get ( " ADDBOLLI " , " STANDARD " ) ) ;
rec_cfven . put ( CFV_PERCSPINC , config ( ) . get ( " PERCSPINC " , " STANDARD " ) ) ;
// scrivo
good = test_write ( cfven ) ;
rec_clifo . zero ( ) ;
rec_clifo . put ( CLI_TIPOCF , tipocf ) ;
rec_clifo . put ( CLI_CODCF , codcf ) ;
good & = test_write ( clifo ) ;
}
if ( good )
{
if ( statusflag = = 3 )
{
const int flag = cancella_clifo ( clifo ) ;
str = " " ;
update_statusflag_codcontab ( custsuppcode , flag , str ) ;
// forse occorrera' cancellare anche l'agente se lo e'
}
else
{
// aggiorna i file clifo, cfven, agenti se necessario
aggiorna_banca ( ) ;
// aggiorna dati conai
trasferisci_conai ( rec_cfven ) ;
// aggiormento comune
TString80 dencom = get_str ( " Locality " ) ;
dencom . trim ( ) ; dencom . upper ( ) ;
const TRectype & reccom = cache_comuni . get ( dencom ) ;
if ( dencom = = reccom . get ( COM_DENCOM ) )
rec_clifo . put ( CLI_COMCF , reccom . get ( COM_COM ) ) ;
else
{
rec_clifo . zero ( CLI_COMCF ) ;
rec_clifo . put ( CLI_LOCCF , dencom ) ;
log ( " " ) ;
str . format ( FR ( " Cliente/Fornitore %c %ld: comune non trovato %s " ) , tipocf , codcf , ( const char * ) dencom ) ;
log ( str ) ;
}
// iban
const bool ibanflag = get_long ( " IbanFlag " ) ! = 0 ;
if ( ibanflag )
rec_clifo . put ( CLI_IBAN , get_str ( " IbanCode " ) ) ;
// agente
rec_cfven . put ( CFV_CODAG , get_agente ( ) ) ;
aggiorna_record ( clifo , lista_clifo ) ;
aggiorna_record ( cfven , lista_cfven ) ;
if ( get_str ( " CodIvaNI " ) = = " 0 " ) // Caso speciale: significa NON esenti
cfven . zero ( CFV_ASSFIS ) ; // Azzero il codice di assogettamento fiscale
// aggiorna anche record agenti se e' una spia
if ( flagcustsupp = = ' A ' )
{
const TString8 codage = decode_agente ( codcf ) ;
TLocalisamfile agenti ( LF_AGENTI ) ;
agenti . put ( AGE_CODAGE , codage ) ;
agenti . read ( ) ;
agenti . put ( AGE_CODFORN , codcf ) ;
aggiorna_record ( agenti , lista_agenti ) ;
str . format ( " %ld|%s " , codcf , ( const char * ) codage ) ;
update_statusflag_codcontab ( custsuppcode , 0 , str ) ;
}
else
{
// aggiorno codcontab
str . format ( " %ld " , codcf ) ;
update_statusflag_codcontab ( custsuppcode , 0 , str ) ;
}
}
}
}
return write_enabled ( ) ;
}
void TPack_clifo : : update_statusflag_codcontab ( const TString & custsuppcode , const int statusflag , const TString & codcontab )
{
TString query ;
query . format ( " UPDATE Customers_Suppliers SET StatusFlag=%d, CodContab='%s' WHERE CustSuppCode='%s' " , statusflag , ( const char * ) codcontab , ( const char * ) custsuppcode ) ;
odbc_exec ( query ) ;
}
bool TPack_clifo : : aggiorna_record ( TLocalisamfile & file , const TString_array & lista_campi )
{
TRectype & rec = file . curr ( ) ;
TString campo_dest , campo_orig , valore , str ;
FOR_EACH_ARRAY_ROW ( lista_campi , i , row )
{
row - > get ( 0 , campo_dest ) ;
row - > get ( 1 , campo_orig ) ;
2007-03-06 16:37:44 +00:00
if ( campo_orig . full ( ) )
2006-07-11 13:10:51 +00:00
{
if ( campo_orig [ 0 ] = = ' _ ' )
{
2007-03-06 16:37:44 +00:00
if ( campo_orig . starts_with ( " _FISSO " ) ) // valore fisso indicato in configurazione
{
valore = campo_orig . after ( ' , ' ) ;
valore . trim ( ) ;
} else
if ( campo_orig . starts_with ( " _STREXPR " ) ) // formato _STREXPR, espressione
{
TExpression expr ( campo_orig . after ( ' , ' ) , _strexpr ) ;
for ( int v = 0 ; v < expr . numvar ( ) ; v + + )
{
const char * varname = expr . varname ( v ) ;
expr . setvar ( v , get_str ( varname ) ) ;
}
valore = expr . as_string ( ) ;
valore . trim ( ) ;
} else
if ( campo_orig . starts_with ( " _TAB " ) ) // formato _TAB,<tabella da leggere>,<valore CODTAB>, <campo da leggere>
2006-07-11 13:10:51 +00:00
{
2007-03-06 16:37:44 +00:00
TToken_string elabora ( campo_orig , ' , ' ) ;
const TString4 tab = elabora . get ( 1 ) ; // tabella da leggere
const TString16 codtab = get_str ( elabora . get ( ) ) ;
const TString16 campotab = elabora . get ( ) ;
2006-07-11 13:10:51 +00:00
valore = cache ( ) . get ( tab , codtab , campotab ) ;
2007-03-06 16:37:44 +00:00
}
else
valore . cut ( 0 ) ;
2006-07-11 13:10:51 +00:00
}
else
valore = get_str ( campo_orig ) ;
rec . put ( campo_dest , valore ) ;
}
}
return test_rewrite ( file ) ;
}
TPack_clifo : : TPack_clifo ( bool only_agenti ) : _agenti ( NULL ) , _banche ( NULL ) , _only_agenti ( only_agenti )
{
}
TPack_clifo : : ~ TPack_clifo ( )
{
if ( _agenti ! = NULL )
delete _agenti ;
if ( _banche ! = NULL )
delete _banche ;
}
///////////////////////////////////////////////////////////
// TPack_indsped
///////////////////////////////////////////////////////////
2009-02-27 16:49:30 +00:00
2006-07-11 13:10:51 +00:00
bool TPack_indsped : : get_clifo ( char & tipocf , long & codcf )
{
const TVariant & code = recordset ( ) . get ( " CustSuppRefCode " ) ;
2009-02-27 16:49:30 +00:00
bool ok = ! code . is_empty ( ) ;
2008-11-14 00:02:54 +00:00
2006-07-11 13:10:51 +00:00
if ( ok )
{
2009-02-27 16:49:30 +00:00
TString query = query_header ( ) ;
query < < " SELECT CodContab,FlagCustSupp FROM Customers_Suppliers WHERE CustSuppCode=#CODCF " ;
TODBC_recordset clifo ( query ) ;
2008-11-14 00:02:54 +00:00
2009-02-27 16:49:30 +00:00
clifo . set_var ( " #CODCF " , code ) ;
ok = clifo . move_first ( ) ;
if ( ok )
{
codcf = clifo . get ( 0u ) . as_int ( ) ;
tipocf = clifo . get ( 1u ) . as_string ( ) [ 0 ] ;
if ( tipocf = = ' T ' )
return false ; // Ignora clienti temporanei senza dare segnalazioni
}
2006-07-11 13:10:51 +00:00
}
2009-02-27 16:49:30 +00:00
2006-07-11 13:10:51 +00:00
if ( ! ok | | codcf < = 0 )
{
2008-11-14 00:02:54 +00:00
TString msg ;
msg < < TR ( " Impossibile determinare il codice contabile dell'anagrafica " ) ;
msg < < " Customers_Suppliers.CustSuppCode= " < < code . as_string ( ) ;
2006-07-11 13:10:51 +00:00
ok = log_error ( msg ) ;
}
return ok ;
}
int TPack_indsped : : get_next_key ( const char tipocf , const long codcf ) const
{
TISAM_recordset indsped ( " USE INDSPED \n FROM TIPOCF=#TIPO CODCF=#CODE \n TO TIPOCF=#TIPO CODCF=#CODE " ) ;
TString4 str ; str < < tipocf ;
indsped . set_var ( " #TIPO " , TVariant ( str ) ) ;
indsped . set_var ( " #CODE " , TVariant ( codcf ) ) ;
int codind = 1 ;
if ( indsped . items ( ) > 0 ) // La move_last da errore fatale su recordset vuoti!
{
indsped . move_last ( ) ;
codind + = indsped . get ( " CODIND " ) . as_int ( ) ;
}
return codind ;
}
bool TPack_indsped : : trasferisci ( )
{
const char * const query =
" SELECT * FROM Customers_Suppliers "
" WHERE (FlagCustSupp='D' OR FlagCustSupp='N') AND (StatusFlag=1 OR StatusFlag=2 OR StatusFlag=3) " ;
TRecordset & recset = create_recordset ( query ) ;
TConfig & ini = config ( ) ;
TString_array lista_indsped ;
ini . list_variables ( lista_indsped , true , " INDSPED " , true ) ;
TLocalisamfile clifo ( LF_CLIFO ) ;
TLocalisamfile indsped ( LF_INDSP ) ;
TRectype & rec_clifo = clifo . curr ( ) ;
TRectype & rec_indsped = indsped . curr ( ) ;
TString str ;
TRecord_cache cache_comuni ( LF_COMUNI , 2 ) ;
TPack_iterator pi ( this ) ;
while ( + + pi )
{
const TString & custsuppcode = get_str ( " CustSuppCode " ) ;
const int statusflag = recset . get ( " StatusFlag " ) . as_int ( ) ;
const char flagcustsupp = get_str ( " FlagCustSupp " ) [ 0 ] ;
const char tipocf = flagcustsupp = = ' D ' ? ' C ' : ' F ' ; // D -> C N -> F
// in codcontab di una destinazione diversa ci mettiamo codcf|codind per ritrovarlo subito su indsped
TToken_string codcontab = get_str ( " CodContab " ) ;
codcontab . trim ( ) ;
long codcf = - 1 ;
int codind = 0 ;
2008-11-14 00:02:54 +00:00
if ( codcontab . full ( ) )
2006-07-11 13:10:51 +00:00
{
codcf = codcontab . get_long ( 0 ) ;
codind = codcontab . get_int ( ) ;
}
bool needs_creation = codcf < = 0 ;
bool good = true ;
if ( ! needs_creation )
{
rec_clifo . zero ( ) ;
rec_clifo . put ( CLI_TIPOCF , tipocf ) ;
rec_clifo . put ( CLI_CODCF , codcf ) ;
good = clifo . read ( ) = = NOERR ;
if ( ! good )
{
2008-11-14 00:02:54 +00:00
// custsupprefcode e' il cliente/fornitore a cui si riferisce l'indirizzo di spedizione
2006-07-11 13:10:51 +00:00
const TString & custsupprefcode = get_str ( " CustSuppRefCode " ) ;
str . format ( FR ( " Non esiste l'indirizzo di spedizione %ld/%d corrispondente a %s " ) ,
codcf , codind , ( const char * ) custsupprefcode ) ;
log ( str , 1 ) ;
}
if ( good )
{
rec_indsped . zero ( ) ;
rec_indsped . put ( IND_TIPOCF , tipocf ) ;
rec_indsped . put ( IND_CODCF , codcf ) ;
rec_indsped . put ( IND_CODIND , codind ) ;
good = indsped . read ( ) = = NOERR ;
if ( ! good )
needs_creation = true ;
}
}
if ( needs_creation & & statusflag ! = 3 & & good )
{
// inserisco indirizzo di spedizione
2008-11-14 00:02:54 +00:00
char tipo = ' ' ;
2006-07-11 13:10:51 +00:00
if ( get_clifo ( tipo , codcf ) )
{
codind = get_next_key ( tipocf , codcf ) ;
rec_indsped . zero ( ) ;
rec_indsped . put ( IND_TIPOCF , tipocf ) ;
rec_indsped . put ( IND_CODCF , codcf ) ;
rec_indsped . put ( IND_CODIND , codind ) ;
codcontab . add ( codcf ) ;
codcontab . add ( codind ) ;
good = test_write ( indsped ) ;
}
else
good = false ;
}
if ( good )
{
if ( statusflag = = 3 )
{
2008-11-14 00:02:54 +00:00
// NON cancellare il cliente in quanto potrebbe avere dei movimenti associati
2006-07-11 13:10:51 +00:00
//const int flag = cancella_clifo(clifo);
//update_statusflag_codcontab(custsuppcode, flag, "0");
}
else
{
// aggiormento comune
TString80 dencom = get_str ( " Locality " ) ;
dencom . trim ( ) ; dencom . upper ( ) ;
const TRectype & reccom = cache_comuni . get ( dencom ) ;
2008-11-14 00:02:54 +00:00
TString256 str = reccom . get ( COM_DENCOM ) ;
str . trim ( ) ; str . upper ( ) ;
2006-07-11 13:10:51 +00:00
if ( str = = dencom )
rec_indsped . put ( IND_COM , reccom . get ( COM_COM ) ) ;
else
{
2008-11-14 00:02:54 +00:00
rec_indsped . zero ( IND_COM ) ;
rec_indsped . put ( IND_LOCALITA , dencom ) ;
2006-07-11 13:10:51 +00:00
log ( " " ) ;
2008-11-14 00:02:54 +00:00
str . format ( FR ( " Destinazioni diverse %ld: comune non trovato %s " ) , codcf , ( const char * ) dencom ) ;
2006-07-11 13:10:51 +00:00
log ( str ) ;
}
aggiorna_record ( indsped , lista_indsped ) ;
// in codcontab ci metto codcf|indsped
update_statusflag_codcontab ( custsuppcode , 0 , codcontab ) ;
}
}
}
return write_enabled ( ) ;
}