From bfb0e7224625ac2f65799a61ce24e19f2bf72b13 Mon Sep 17 00:00:00 2001 From: sparkyx Date: Fri, 24 Mar 2006 21:47:08 +0000 Subject: [PATCH] =?UTF-8?q?Task=20#4898=20Import=20CSV=20op=C3=A9ration=20?= =?UTF-8?q?banquaire?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- html/admin/sql/patch/upgrade8.sql | 48 ++++++++ html/import.php | 94 +++++++++++++++ include/cbc_be.inc.php | 3 + include/constant.php | 3 +- include/debug.php | 1 - include/import_inc.php | 186 ++++++++++++++++++++++++++++++ include/user_common.php | 43 ++++++- include/user_form_ach.php | 2 +- include/user_form_ven.php | 35 ++++-- include/user_menu.php | 13 ++- 10 files changed, 414 insertions(+), 14 deletions(-) create mode 100644 html/import.php create mode 100644 include/cbc_be.inc.php create mode 100644 include/import_inc.php diff --git a/html/admin/sql/patch/upgrade8.sql b/html/admin/sql/patch/upgrade8.sql index d44f51017..4ba7fa7ad 100644 --- a/html/admin/sql/patch/upgrade8.sql +++ b/html/admin/sql/patch/upgrade8.sql @@ -1,6 +1,54 @@ begin; insert into action values (21,'Import et export des écritures d''ouverture'); +create sequence s_quantity; + +create index idx_qs_internal on quant_sold(qs_internal); +CREATE TABLE quant_sold ( + qs_id integer DEFAULT nextval('s_quantity'::text), + qs_internal text NOT NULL, + qs_fiche integer NOT NULL, + qs_quantite integer NOT NULL, + qs_price numeric(20,4), + qs_vat numeric(20,4), + qs_vat_code integer +); +create table format_csv_banque +( + name text primary key, + include_file text not null +); +drop trigger trim_space on format_csv_banque; + +drop function trim_space_format_csv_banque(); + +create function trim_space_format_csv_banque() returns trigger as $trim$ +declare + modified format_csv_banque%ROWTYPE; +begin + modified.name=trim(NEW.NAME); + modified.include_file=trim(new.include_file); + if ( length(modified.name) = 0 ) then + modified.name=null; + end if; + if ( length(modified.include_file) = 0 ) then + modified.include_file=null; + end if; + + return modified; +end; +$trim$ language plpgsql; + +create trigger trim_space before insert or update on format_csv_banque FOR EACH ROW execute procedure trim_space_format_csv_banque(); + +create unique index idx_case on format_csv_banque (upper(name)); +INSERT INTO format_csv_banque VALUES ('Fortis', 'fortis_be.inc.php'); +INSERT INTO format_csv_banque VALUES ('EUB', 'eub_be.inc.php'); +INSERT INTO format_csv_banque VALUES ('ING', 'ing_be.inc.php'); +INSERT INTO format_csv_banque VALUES ('CBC', 'cbc_be.inc.php'); + + + update version set val=9; diff --git a/html/import.php b/html/import.php new file mode 100644 index 000000000..aee68623b --- /dev/null +++ b/html/import.php @@ -0,0 +1,94 @@ +Check(); + +html_page_start($User->theme); + + +/* Admin. Dossier */ + +include_once("import_inc.php"); + +include_once ("user_menu.php"); +ShowMenuCompta($_SESSION['g_dossier']); + +$cn=DbConnect($_SESSION['g_dossier']); +if ( $User->CheckAction($cn,IMP_BQE)==0){ + /* Cannot Access */ + NoAccess(); + } +echo ShowMenuAdvanced("import.php"); + +ShowMenuImport(); + +if ( isset( $_REQUEST['PHPSESSID'])) { + $sessid = $_REQUEST['PHPSESSID']; +} +echo JS_SEARCH_POSTE; + +// if action is set proceed to it +if ( isset ($_GET["action"]) ) { + $action=$_GET["action"]; +// menu = import cvs + if ($action == "import" ) { + if(isset($_FILES['fupload'])) { + // load the table with the cvs' content + echo '
'; + ImportCSV($cn,$_FILES['fupload']['tmp_name'],$_POST['import_bq'],$_POST['format_csv']); + echo "
"; + } else { + echo '
'; + ShowFormTransfert($cn); + echo "
"; + } + } + if ($action == "verif" ) { + if(isset($_POST['poste'])) { + UpdateCSV($cn, $_POST['code'], $_POST['poste']); + } + echo '
'; + VerifImport($cn); + echo "
"; + } + if ($action == "transfer" ) { + echo '
'; + TransferCSV($cn, $User->GetPeriode()); + echo "
"; + } +} + +html_page_stop(); +?> diff --git a/include/cbc_be.inc.php b/include/cbc_be.inc.php new file mode 100644 index 000000000..a20bbee52 --- /dev/null +++ b/include/cbc_be.inc.php @@ -0,0 +1,3 @@ + diff --git a/include/constant.php b/include/constant.php index 74a822257..b3f3b56f2 100644 --- a/include/constant.php +++ b/include/constant.php @@ -59,7 +59,8 @@ define ("ODS",13); define ("FICHE_WRITE",15); define ("STOCK_WRITE",16); define ("STOCK_READ",17); - +define ("EXP_IMP_ECR",21); +define ("IMP_BQE",22); // Erreur define ("NOERROR",0); define ("BADPARM",1); diff --git a/include/debug.php b/include/debug.php index 1e76c9aa5..a1ecfbc14 100644 --- a/include/debug.php +++ b/include/debug.php @@ -34,7 +34,6 @@ function echo_debug ($file,$line="",$msg="") { $sql= "insert into log (lg_file,lg_line,lg_msg) ". "values ('$file','$line','$msg');"; -// pg_exec ($cn,"\set encoding 'latin1'"); pg_set_client_encoding($cn,'latin1'); pg_exec($cn,$sql); } diff --git a/include/import_inc.php b/include/import_inc.php new file mode 100644 index 000000000..0b92c19fe --- /dev/null +++ b/include/import_inc.php @@ -0,0 +1,186 @@ +
"; + $i=1; + while($val = pg_fetch_array($Res)){ + echo '
'; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ""; + echo ''; + echo '
'.$val['code'].''.$val['date_exec'].''.$val['montant'].' EUR
'.$val['detail'].'
Poste Comptable : n° compte : ".$val['num_compte']."
'; + echo '
'; + $i++; + } +} + +function TransferCSV($p_cn, $periode){ + //on obtient la période courante + //$p_user = $_SESSION['g_user']; + //$periode = GetUserPeriode($p_cn,$p_user); + //$periode = $p_user->GetPeriode(); + // on trouve les dates frontières de cette période + $sql = "select p_start,p_end from parm_periode where p_id = '".$periode."'"; + $Res=ExecSql($p_cn,$sql); + $val = pg_fetch_array($Res); + $start = $val['p_start']; $end = $val['p_end']; + + $sql = "select * from import_tmp where poste_comptable <> '' AND ok <> TRUE AND date_exec BETWEEN '".$start."' and '".$end."'"; + $Res=ExecSql($p_cn,$sql); + //echo "boucle: ".sizeof($Res)."
"; + //while($val = pg_fetch_array($Res)){ + $Max=pg_NumRows($Res); + echo $Max." opérations à transférer.
"; + for ($i = 0;$i < $Max;$i++) { + $val=pg_fetch_array($Res,$i); + + $code=$val['code']; $date_exec=$val['date_exec']; $montant=$val['montant']; $num_compte=$val['num_compte']; $poste_comptable=$val['poste_comptable']; + + list($annee, $mois, $jour) = explode("-", $date_exec); + $date_exec = $jour.".".$mois.".".$annee; + + // Vérification que le poste comptable trouvé existe + $sqltest = "select * from tmp_pcmn WHERE pcm_val='".$poste_comptable."'"; + $Restest=ExecSql($p_cn,$sqltest); + $test=pg_NumRows($Restest); + if($test == 0) { + $sqlupdate = "update import_tmp set poste_comptable='' WHERE code='".$code."' AND num_compte='".$num_compte."'"; + $Resupdate=ExecSql($p_cn,$sqlupdate); + echo "Poste comptable erronné pour l'opération ".$num_compte."-".$code.", réinitialisation du poste comptable
"; + } else { + + // Finances + $p_jrn = 1; + + //$seq = GetNextId($p_cn,'j_grpt')+1; + $seq=NextSequence($p_cn,'s_grpt'); + $p_user = $_SESSION['g_user']; + //$periode = GetUserPeriode($p_cn,$p_user); + //$periode = $User->GetPeriode(); + StartSql($p_cn); + +// if($num_compte == '001-3983978-70') $r=InsertJrnx($p_cn,"d",$p_user,$p_jrn,'55000001',$date_exec,$montant,$seq,$periode); +// elseif ($num_compte == '360-1062770-44' ) $r=InsertJrnx($p_cn,"d",$p_user,$p_jrn,'55000002',$date_exec,$montant,$seq,$periode); +// elseif ($num_compte == '671-9032279-01' ) $r=InsertJrnx($p_cn,"d",$p_user,$p_jrn,'55000003',$date_exec,$montant,$seq,$periode); + $r=InsertJrnx($p_cn,"d",$p_user,$p_jrn,$poste_comptable,$date_exec,$montant,$seq,$periode); + if ( $r == false) { $Rollback($p_cn);exit("error __FILE__ __LINE__");} + + $r=InsertJrnx($p_cn,"c",$p_user,$p_jrn,$poste_comptable,$date_exec,$montant,$seq,$periode); + if ( $r == false) { $Rollback($p_cn);exit("error __FILE__ __LINE__");} + + $r=InsertJrn($p_cn,$date_exec,NULL,$p_jrn,$num_compte." ".$code,$montant,$seq,$periode); + if ( $r == false ) { Rollback($p_cn); exit(" Error __FILE__ __LINE__");} + + //$sql = "insert into jrn (jr_def_id,jr_montant,jr_comment,jr_date,jr_grpt_id,jr_tech_per) values ( ".$p_jrn.", abs(".round($montant,2)."), '".$num_compte." ".$code."','".$date_valeur."','".$seq."','".$periode."')"; + SetInternalCode($p_cn,$seq,$p_jrn); + + Commit($p_cn); + + echo "Tranfer de l'opération ".$code." effectué
"; + $sql2 = "update import_tmp set ok=TRUE where code='".$code."'"; + $Res2=ExecSql($p_cn,$sql2); + + } + } +} +/* function ShowForm + ************************************************** + * Purpose : ShowForm for getting data about + * the bank transfert in cvs + * + * parm : database connection + * - + * gen : + * - + * return: none + */ + +function ShowFormTransfert($p_cn){ +$w=new widget("select"); + echo '
'; + echo '
'; + // ask for the journal target + $jrn=make_array ($p_cn,"select jrn_def_id,jrn_def_name from jrn_def where jrn_def_type='FIN';"); + $w->label='Journal'; + echo $w->label." :".$w->IOValue('import_jrn',$jrn)."
"; + // choose the bank account + $bq=make_array($p_cn,"select pcm_val,pcm_lib from tmp_pcmn where pcm_val like '550%'"); + $w->label='Banque'; + echo "Compte en banque :".$w->IOValue('import_bq',$bq)."
"; + $format_csv=make_array($p_cn,"select include_file,name from format_csv_banque;"); + $w->label="Format import"; + echo $w->label.$w->IOValue('format_csv',$format_csv).'
'; + echo ''; + echo '
'; +} +?> diff --git a/include/user_common.php b/include/user_common.php index f12a648d5..2a5e707fb 100644 --- a/include/user_common.php +++ b/include/user_common.php @@ -47,7 +47,7 @@ function GetTvaRate($p_cn,$p_tva_id) { return $r; } -/* function ComputeVat($p_cn,$a_fiche,$a_quant,$a_price,$ap_vat) +/* function ComputeTotalVat($p_cn,$a_fiche,$a_quant,$a_price,$ap_vat) ************************************************** * Purpose : Compute the vat, * the fiche.f_id are in a_fiche @@ -65,8 +65,8 @@ function GetTvaRate($p_cn,$p_tva_id) { * return: array * a[tva_id] = amount vat */ -function ComputeVat($p_cn, $a_fiche,$a_quant,$a_price,$ap_vat ) { -echo_debug(__FILE__,__LINE__,"ComputeVat $a_fiche $a_quant $a_price"); +function ComputeTotalVat($p_cn, $a_fiche,$a_quant,$a_price,$ap_vat ) { +echo_debug(__FILE__,__LINE__,"ComputeTotalVat $a_fiche $a_quant $a_price"); foreach ( $a_fiche as $t=>$el) { echo_debug(__FILE__,__LINE__,"t $t e $el"); } @@ -104,6 +104,43 @@ echo_debug(__FILE__,__LINE__,"ComputeVat $a_fiche $a_quant $a_price"); } +/* function ComputeVat($p_cn,$a_fiche,$a_quant,$a_price,$ap_vat) + ************************************************** + * Purpose : Compute the vat for only one elt, + * the fiche.f_id are in p_fiche + * the quantity in p_quant + * + * + * parm : + * - database connection + * - fiche id int + * 1- quantity int + * - price float + * - Tva_id + * gen : + * - + * return: the amount of vat + */ +function ComputeVat($p_cn, $p_fiche,$p_quant,$p_price,$p_vat ) { + // Get the tva_id + if ( $p_vat != null and isNumber($p_vat)== 1) + $tva_id=$p_vat; + else + $tva_id=GetFicheAttribut($p_cn,$p_fiche,ATTR_DEF_TVA); + + if ( $tva_id == 'Unknown' ) return -1; + // for each fiche find the tva_rate and tva_id + $a_vat=GetTvaRate($p_cn,$tva_id); + $vat_amount=-1; + // Get the attribut price of the card(fiche) + if ( $a_vat != null and $a_vat['tva_id'] != "" ) + { $a=$a_vat['tva_id']; + $vat_amount=$p_price*$a_vat['tva_rate']*$p_quant; + } + return $vat_amount; + + +} /* function GetTvaPoste($p_cn,$tva_id,$p_type); diff --git a/include/user_form_ach.php b/include/user_form_ach.php index 08947514d..eedba9e8a 100644 --- a/include/user_form_ach.php +++ b/include/user_form_ach.php @@ -582,7 +582,7 @@ function RecordSell($p_cn,$p_array,$p_user,$p_jrn) $amount_jrn+=($cost<0)?0:$cost; } $comm=FormatString($e_comm); - $a_vat=ComputeVat($p_cn,$a_good,$a_quant,$a_price,$a_vat); + $a_vat=ComputeTotalVat($p_cn,$a_good,$a_quant,$a_price,$a_vat); $sum_vat=0.0; if ( $a_vat != null ){ diff --git a/include/user_form_ven.php b/include/user_form_ven.php index afdb528eb..d4b4de0d6 100644 --- a/include/user_form_ven.php +++ b/include/user_form_ven.php @@ -603,10 +603,10 @@ function RecordInvoice($p_cn,$p_array,$p_user,$p_jrn) $comm=FormatString($e_comm); // Compute VAT //-- - $a_vat=ComputeVat($p_cn,$a_good,$a_quant,$a_price,$a_vat); + $a_vat_new=ComputeTotalVat($p_cn,$a_good,$a_quant,$a_price,$a_vat); $sum_vat=0.0; - if ( $a_vat != null ){ - foreach ( $a_vat as $element => $t) { + if ( $a_vat_new != null ){ + foreach ( $a_vat_new as $element => $t) { echo_debug(__FILE__,__LINE__," a_vat element $element t $t"); $sum_vat+=$t; echo_debug(__FILE__,__LINE__,"sum_vat = $sum_vat"); @@ -638,14 +638,14 @@ function RecordInvoice($p_cn,$p_array,$p_user,$p_jrn) // always save quantity but in withStock we can find what card need a stock management if ( InsertStockGoods($p_cn,$j_id,$a_good[$i],$a_quant[$i],'c') == false ) { $Rollback($p_cn);exit("error __FILE__ __LINE__");} - } + } // end loop // Insert Vat - if ( $a_vat != null ) // no vat + if ( $a_vat_new != null ) // no vat { - foreach ($a_vat as $tva_id => $tva_amount ) { + foreach ($a_vat_new as $tva_id => $tva_amount ) { $poste=GetTvaPoste($p_cn,$tva_id,'c'); if ($tva_amount == 0 ) continue; $r=InsertJrnx($p_cn,'c',$p_user->id,$p_jrn,$poste,$e_date,round($tva_amount,2),$seq,$periode); @@ -667,7 +667,28 @@ function RecordInvoice($p_cn,$p_array,$p_user,$p_jrn) if ( isset ($_FILES)) save_upload_document($p_cn,$seq); - +// save the quantity, then we can make an invoice + for ( $i=0;$i < $nb_item;$i++) + { + // don't record operation of 0 + if ( $a_price[$i]*$a_quant[$i] == 0 ) continue; + + // insert into the table quant_sold + // Note that negative value are also saved but not the vat ! + if ( $a_vat[$i] == -1) { + $computed_vat=0; + $vat_code="null"; + } else { + $computed_vat=ComputeVat($p_cn,$a_good[$i],$a_quant[$i],$a_price[$i],$a_vat[$i]); + $vat_code=$a_vat[$i]; + } + $r=ExecSql($p_cn,"insert into quant_sold". + "(qs_internal,qs_fiche,qs_quantite,qs_price,qs_vat,qs_vat_code) values". + "('".$internal."',".$a_good[$i].",".$a_quant[$i].",".$a_price[$i]. + ",".$computed_vat. + ",".$vat_code.")"); + if ( $r == false ) { Rollback($p_cn); exit(" Error __FILE__ __LINE__"); }; + } Commit($p_cn); return $comment; diff --git a/include/user_menu.php b/include/user_menu.php index 8838c0619..255bb563d 100644 --- a/include/user_menu.php +++ b/include/user_menu.php @@ -492,7 +492,9 @@ $left_menu=ShowItem(array( array('central.php','Centralise'), array('pcmn_update.php?p_start=1','Plan Comptable'), array('stock.php','Stock'), - array('form.php','Rapport') + array('form.php','Rapport'), + array('import.php','Import Banque'), + array('ecrit_ouv.php','Ecriture ouverture') ), 'H',"cell","mtitle",$default); return $left_menu; @@ -708,4 +710,13 @@ function ShowMenuComptaForm($p_dossier) { echo ""; echo ''; } +function ShowMenuImport(){ + echo ''; + echo ''; + echo ''; + echo ''; + + echo "
Importer CSV
Verif CSV
Transfert CSV
"; +} + ?>