Task #4898 Import CSV opération banquaire

This commit is contained in:
sparkyx 2006-03-24 21:47:08 +00:00
parent aa6f8862ab
commit bfb0e72246
10 changed files with 414 additions and 14 deletions

View file

@ -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;

94
html/import.php Normal file
View file

@ -0,0 +1,94 @@
<?
/*
* This file is part of PhpCompta.
*
* PhpCompta is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* PhpCompta is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PhpCompta; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* $Revision$ */
// Copyright Author Dany De Bontridder ddebontridder@yahoo.fr
// Author Olivier Dzwoniarkiewicz
include_once("ac_common.php");
include_once("user_menu.php");
include_once ("constant.php");
include_once ("postgres.php");
include_once ("check_priv.php");
include_once ("class_widget.php");
if ( ! isset ( $_SESSION['g_dossier'] ) ) {
echo "You must choose a Dossier ";
exit -2;
}
$cn=DbConnect($_SESSION['g_dossier']);
include ('class_user.php');
$User=new cl_user($cn);
$User->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 '<DIV class="ccontent">';
ImportCSV($cn,$_FILES['fupload']['tmp_name'],$_POST['import_bq'],$_POST['format_csv']);
echo "</DIV>";
} else {
echo '<DIV class="ccontent">';
ShowFormTransfert($cn);
echo "</DIV>";
}
}
if ($action == "verif" ) {
if(isset($_POST['poste'])) {
UpdateCSV($cn, $_POST['code'], $_POST['poste']);
}
echo '<DIV class="ccontent">';
VerifImport($cn);
echo "</DIV>";
}
if ($action == "transfer" ) {
echo '<DIV class="ccontent">';
TransferCSV($cn, $User->GetPeriode());
echo "</DIV>";
}
}
html_page_stop();
?>

3
include/cbc_be.inc.php Normal file
View file

@ -0,0 +1,3 @@
<?
echo "Encore rien désolé";
?>

View file

@ -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);

View file

@ -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);
}

186
include/import_inc.php Normal file
View file

@ -0,0 +1,186 @@
<?
/*
* This file is part of PhpCompta.
*
* PhpCompta is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* PhpCompta is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PhpCompta; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// Copyright Author Dany De Bontridder ddebontridder@yahoo.fr
// $Revision$
include_once("jrn.php");
include_once("preference.php");
include_once("user_common.php");
/* function ImportCSV
**************************************************
* Purpose : Parse the file and insert the record
* into the table import_tmp. Insert in a temporary table, if
* no confirmation is given then the data are removed otherwise
* records are inserted into import_tmp
*
* parm :
* - p_cn database connection
* - file the uploaded file
* - $p_bq_account
* - p_format_csv file to include (depending of the bank)
* gen :
* -
* return:
*/
function ImportCSV($p_cn,$file,$p_bq_account,$p_format_csv)
{
if(!$handle = fopen($file, "r")) {
print 'could not open file. quitting';
die;
}
$row = 1;
while (($data = fgetcsv($handle, 2000, '#@!')) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
// if $p_format_csv == fortis
require_once($p_format_csv);
}
$row++;
}
echo "Importation terminée.";
fclose($handle);
}
function UpdateCSV($p_cn, $code, $poste){
$sql = "update import_tmp set poste_comptable='".$poste."' where code='".$code."'";
$Res=ExecSql($p_cn,$sql);
}
function VerifImport($p_cn){
$sql = "select * from import_tmp where poste_comptable=''";
$Res=ExecSql($p_cn,$sql);
$Num=pg_NumRows($Res);
echo $Num." opérations à complèter.<br/><br/>";
$i=1;
while($val = pg_fetch_array($Res)){
echo '<form METHOD="POST" action="import.php?action=verif">';
echo '<input type="hidden" name="code" value="'.$val['code'].'">';
echo '<table border="1" width="500">';
echo '<tr><td width="200">'.$val['code'].'</td><td width="200">'.$val['date_exec'].'</td><td width="100">'.$val['montant'].' EUR</td><tr/>';
echo '<tr><td height="50" colspan="3">'.$val['detail'].'</td><tr/>';
echo '<tr><td>Poste Comptable : <input type="text" size="10" name="poste"></td>';
echo "<td>n° compte : ".$val['num_compte']."</td>";
echo '<td><input type="submit" value="modifier"></td><tr/>';
echo '</table>';
echo '</FORM>';
$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)."<br/>";
//while($val = pg_fetch_array($Res)){
$Max=pg_NumRows($Res);
echo $Max." opérations à transférer.<br/>";
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<br/>";
} 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é<br/>";
$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 '<FORM METHOD="POST" action="import.php?action=import" enctype="multipart/form-data">';
echo '<INPUT TYPE="file" name="fupload" size="20"><br>';
// 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)."<br>";
// 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)."<br>";
$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).'<br>';
echo '<INPUT TYPE="SUBMIT" Value="Import fiche">';
echo '</FORM>';
}
?>

View file

@ -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);

View file

@ -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 ){

View file

@ -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;

View file

@ -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 "</TABLE>";
echo '</div>';
}
function ShowMenuImport(){
echo '<TABLE>';
echo '<TR><TD class="mtitle"><A class="mtitle" HREF="import.php?action=import">Importer CSV</A></TD></TR>';
echo '<TR><TD class="mtitle"><A class="mtitle" HREF="import.php?action=verif">Verif CSV</A></TD></TR>';
echo '<TR><TD class="mtitle"><A class="mtitle" HREF="import.php?action=transfer">Transfert CSV</A></TD></TR>';
echo "</TABLE>";
}
?>