Task #1231 - Cohérence listes CSV Journaux

#1231 Rewrite code for export CSV for ANC part
This commit is contained in:
Dany De Bontridder 2015-12-02 15:13:17 +01:00
parent 7572b02762
commit df17810dec
14 changed files with 188 additions and 142 deletions

View file

@ -427,11 +427,12 @@ END) <> 0::numeric order by name,po_name",array($this->pa_id));
function export_csv()
{
bcscale(2);
$csv=new Noalyss_CSV('export-anc-list');
$csv->send_header();
if ( $this->check () != 0 ) {throw new Exception (_("date invalide"));}
//---------------------------------------------------------------------------
// Card - Acc
//---------------------------------------------------------------------------
if ( $this->card_poste=='1')
{
$this->load_card();
@ -441,19 +442,22 @@ END) <> 0::numeric order by name,po_name",array($this->pa_id));
*/
$prev='';
for ($i=0;$i<count($this->arow);$i++)
{
printf('"%s";" %s"', $this->arow[$i]['j_qcode'],$this->arow[$i]['name']);
$idx=0;
$a_csv=array();
$a_csv[$idx]=$this->arow[$i]['j_qcode']; $idx++;
$a_csv[$idx]=$this->arow[$i]['name'];$idx++;
$a_csv[$idx]=$this->arow[$i]['name'];$idx++;
$a_csv[$idx]=$this->arow[$i]['po_name'];$idx++;
$a_csv[$idx]=$this->arow[$i]['po_description'];$idx++;
$amount=$this->arow[$i]['sum_amount'];
if ($amount==null)$amount=0;
printf(';"%s";" %s";',
$this->arow[$i]['po_name'],
$this->arow[$i]['po_description']);
printf("%s",nb($amount));
printf("\r\n");
if ($amount==null)$amount=0;
$a_csv[$idx]=$amount;$idx++;
$csv->write_header($a_csv);
}
}
//---------------------------------------------------------------------------
@ -468,16 +472,16 @@ END) <> 0::numeric order by name,po_name",array($this->pa_id));
*/
for ($i=0;$i<count($this->arow);$i++)
{
printf('"%s";" %s"', $this->arow[$i]['j_poste'],$this->arow[$i]['name']);
$amount=$this->arow[$i]['sum_amount'];
$csv->add( $this->arow[$i]['j_poste']);
$csv->add( $this->arow[$i]['name']);
$csv->add( $this->arow[$i]['po_name']);
$csv->add( $this->arow[$i]['po_description']);
$amount=$this->arow[$i]['sum_amount'];
if ($amount==null)$amount=0;
printf(';"%s";" %s";',
$this->arow[$i]['po_name'],
$this->arow[$i]['po_description']);
printf("%s",nb($amount));
printf("\r\n");
$csv->add($amount,"number");
$csv->write();
}
@ -494,16 +498,18 @@ END) <> 0::numeric order by name,po_name",array($this->pa_id));
*/
for ($i=0;$i<count($this->arow);$i++)
{
printf('"%s";" %s";', $this->arow[$i]['po_name'],$this->arow[$i]['po_description']);
$csv->add( $this->arow[$i]['po_name']);
$csv->add( $this->arow[$i]['po_description']);
$csv->add( $this->arow[$i]['j_qcode']);
$csv->add( $this->arow[$i]['name']);
$amount=$this->arow[$i]['sum_amount'];
if ($amount==null)$amount=0;
printf('"%s";"%s";',
$this->arow[$i]['j_qcode'],
$this->arow[$i]['name']);
printf("%s",nb($amount));
printf("\r\n");
$csv->add($amount,"number");
$csv->write();
}
@ -522,17 +528,17 @@ END) <> 0::numeric order by name,po_name",array($this->pa_id));
*/
for ($i=0;$i<count($this->arow);$i++)
{
printf('"%s";"%s";', $this->arow[$i]['po_name'],$this->arow[$i]['po_description']);
$csv->add( $this->arow[$i]['po_name']);
$csv->add( $this->arow[$i]['po_description']);
$csv->add( $this->arow[$i]['j_poste']);
$csv->add( $this->arow[$i]['name']);
$amount=$this->arow[$i]['sum_amount'];
if ($amount==null)$amount=0;
printf('"%s";"%s";',
$this->arow[$i]['j_poste'],
$this->arow[$i]['name']);
printf("%s",nb($amount));
printf("\r\n");
$csv->add($amount,"number");
$csv->write();
}
}

View file

@ -272,16 +272,17 @@ class Anc_Balance_Double extends Anc_Print
*/
function display_csv()
{
$r="";
$csv = new Noalyss_Csv("ca_bal_croise");
$csv->send_header();
$csv->add("Poste comptable Analytique");
$csv->add("Poste comptable Analytique");
$csv->add("Debit");
$csv->add("Credit");
$csv->add("Solde");
$csv->add("D/C");
$r.='"Poste comptable Analytique";';
$r.='"Poste comptable Analytique";';
$r.='"Debit";';
$r.='"Credit";';
$r.='"Solde";';
$r.='"D/C"';
$r.="\r\n";
$csv->write();
$array=$this->load();
if ( is_array($array) == false )
@ -292,16 +293,15 @@ class Anc_Balance_Double extends Anc_Print
foreach ( $array as $row)
{
$r.=sprintf('"%s";',$row['a_po_name']);
$r.=sprintf('"%s";',$row['b_po_name']);
$r.=sprintf("%s;",nb($row['a_d']));
$r.=sprintf("%s;",nb($row['a_c']));
$r.=sprintf("%s;",nb($row['a_solde']));
$r.=sprintf('"%s"',$row['a_debit']);
$r.="\r\n";
$csv->add($row['a_po_name']);
$csv->add($row['b_po_name']);
$csv->add($row['a_d'],'number');
$csv->add($row['a_c'],'number');
$csv->add($row['a_solde'],'number');
$csv->add($row['a_debit'],'text');
$csv->write();
}
return $r;
}
/*!

View file

@ -230,20 +230,23 @@ class Anc_Balance_Simple extends Anc_Print
return $array;
}
$csv=new Noalyss_Csv("ca_bal_simple");
$csv->send_header();
$r="";
foreach ( $array as $row)
{
// the name and po_id
$solde=($row['sum_cred']>$row['sum_deb'])?'C':'D';
$solde=($row['sum_cred']==$row['sum_deb'])?'':$solde;
$r.=sprintf("'%s';",$row['po_id']);
$r.=sprintf("'%s';",$row['po_name']);
$r.=sprintf("%s;",nb($row['sum_deb']));
$r.=sprintf("%s;",nb($row['sum_cred']));
$r.=sprintf("%s;",nb($row['solde']));
$r.=sprintf("'%s'",$row['debit']);
$r.="\r\n";
}
$solde=($row['sum_cred']==$row['sum_deb'])?'0':$solde;
$csv->add($row['po_id']);
$csv->add($row['po_name']);
$csv->add($row['sum_deb'],"number");
$csv->add($row['sum_cred'],"number");
$csv->add($row['solde'],"number");
$csv->add($row['debit'],"text");
$csv->write();
}
return $r;
}

View file

@ -221,19 +221,18 @@ order by ga_description,po_name";
function export_csv()
{
$array=$this->get_result();
printf('"groupe";"activité";"débit";"credit";"solde"');
printf("\r\n");
$cvs=new Noalyss_Csv ('anc-balance-group-export');
$cvs->send_header();
$cvs->write_header(array("groupe","activité","débit","credit","solde"));
bcscale(2);
for ($i=0;$i<count($array);$i++)
{
printf('"%s";"%s";%s;%s;%s',
$array[$i]['ga_id'],
$array[$i]['po_name'],
nb($array[$i]['sum_deb']),
nb($array[$i]['sum_cred']),
nb(bcsub($array[$i]['sum_cred'],$array[$i]['sum_deb']))
);
printf("\r\n");
$cvs->add($array[$i]['ga_id']);
$cvs->add($array[$i]['po_name']);
$cvs->add($array[$i]['sum_deb'],"number");
$cvs->add($array[$i]['sum_cred'],"number");
$cvs->add(bcsub($array[$i]['sum_cred'],$array[$i]['sum_deb']),"number");
$cvs->write();
}
}
static function test_me()

View file

@ -159,22 +159,23 @@ class Anc_Listing extends Anc_Print
return $array;
}
$r="";
$csv=new Noalyss_Csv("anc_listing");
$csv->send_header();
$csv->write_header(array("Date","Poste","QuickCode",
"Activité","description","montant","d/c"));
foreach ( $array as $row)
{
// the name and po_id
$r.=sprintf('"%s";',$row['oa_date']);
$r.=sprintf('"%s";',$row['j_poste']);
$r.=sprintf('"%s";',$row['qcode']);
$r.=sprintf('"%s";',$row['po_name']);
$r.=sprintf('"%s";',$row['oa_description']);
$r.=sprintf('"%s";',$row['oa_description']);
$r.=sprintf("%12.2f;",$row['oa_amount']);
$r.=sprintf("'%s'",(($row['oa_debit']=='f')?'CREDIT':'DEBIT'));
$r.="\r\n";
$csv->add($row['oa_date']);
$csv->add($row['j_poste']);
$csv->add($row['qcode']);
$csv->add($row['po_name']);
$csv->add($row['oa_description']);
$csv->add($row['oa_amount'],"number");
$csv->add(sprintf("'%s'",(($row['oa_debit']=='f')?'CREDIT':'DEBIT')));
$csv->write();
}
return $r;
return;
}

View file

@ -247,18 +247,22 @@ class Anc_Table extends Anc_Acc_Link
{
bcscale(2);
if ( $this->check () != 0 ) {throw new Exception ( "DATE INVALIDE");}
$csv=new Noalyss_Csv("Anc-table");
$csv->send_header();
//---------------
// By Card
if ( $this->card_poste=='1')
{
$this->load_card();
echo '"Fiche"';
$csv->add("Fiche");
foreach ($this->aheader as $h)
{
echo ';"'.$h['po_name'].'"';
$csv->add($h['po_name']);
}
echo ';"Total"';
printf("\r\n");
$csv->add("Total");
$csv->write();
/*
* Show all the result
*/
@ -266,7 +270,7 @@ class Anc_Table extends Anc_Acc_Link
for ($i=0;$i<count($this->arow);$i++)
{
printf('"%s"',$this->arow[$i]['card_account'].' '.$this->arow[$i]['name']);
$csv->add($this->arow[$i]['card_account'].' '.$this->arow[$i]['name']);
$tot_row=0;
for ($x=0;$x<count($this->aheader);$x++)
{
@ -280,26 +284,27 @@ class Anc_Table extends Anc_Acc_Link
{
$tot_col[$x]=$amount;
}
printf(";%s",nb($amount));
$csv->add($amount,"number");
$tot_row=bcadd($tot_row,$amount);
}
printf(";%s",nb($tot_row));
printf("\r\n");
$csv->add($tot_row,"number");
$csv->write();
}
}
//---------------
// By Accounting
if ( $this->card_poste=='2')
{
$this->load_poste();
echo '"Poste"';
$csv->add("Poste");
foreach ($this->aheader as $h)
{
echo ';"'.$h['po_name'].'"';
$csv->add($h['po_name']);
}
echo ';"Total"';
printf("\r\n");
$csv->add("Total");
$csv->write();
/*
* Show all the result
*/
@ -307,7 +312,7 @@ class Anc_Table extends Anc_Acc_Link
for ($i=0;$i<count($this->arow);$i++)
{
printf('"%s"',$this->arow[$i]['card_account'].' '.$this->arow[$i]['name']);
$csv->add($this->arow[$i]['card_account'].' '.$this->arow[$i]['name']);
$tot_row=0;
for ($x=0;$x<count($this->aheader);$x++)
{
@ -321,12 +326,12 @@ class Anc_Table extends Anc_Acc_Link
{
$tot_col[$x]=$amount;
}
printf(";%s",nb($amount));
$csv->add($amount,"number");
$tot_row=bcadd($tot_row,$amount);
}
printf(";%s",nb($tot_row));
printf("\r\n");
$csv->add($tot_row,"number");
$csv->write();
}
}

View file

@ -24,12 +24,9 @@
*
*/
if ( ! defined ('ALLOWED') ) die('Appel direct ne sont pas permis');
header('Pragma: public');
header('Content-type: application/csv');
header('Content-Disposition: attachment;filename="export-anc-list.csv"',FALSE);
require_once NOALYSS_INCLUDE.'/class/class_anc_acc_list.php';
require_once NOALYSS_INCLUDE.'/lib/class_noalyss_csv.php';
$bal=new Anc_Acc_List($cn);
$bal->get_request();

View file

@ -27,14 +27,12 @@
*
*/
if ( ! defined ('ALLOWED') ) die('Appel direct ne sont pas permis');
header('Content-type: application/csv');
header('Pragma: public');
header('Content-Disposition: attachment;filename="ca_bal_croise.csv"',FALSE);
require_once NOALYSS_INCLUDE.'/class/class_anc_balance_double.php';
require_once NOALYSS_INCLUDE.'/lib/class_noalyss_csv.php';
$cn=Dossier::connect();
$bal=new Anc_Balance_Double($cn);
$bal->get_request();
echo $bal->display_csv();
$bal->display_csv();

View file

@ -24,10 +24,7 @@
*/
if ( ! defined ('ALLOWED') ) die('Appel direct ne sont pas permis');
require_once NOALYSS_INCLUDE.'/class/class_anc_group.php';
header('Pragma: public');
header('Content-type: application/csv');
header('Content-Disposition: attachment;filename="anc-balance-group-export.csv"',FALSE);
require_once NOALYSS_INCLUDE.'/lib/class_noalyss_csv.php';
$a=new Anc_Group($cn);
$a->get_request();

View file

@ -23,11 +23,9 @@
* \brief export the operation in pdf
*/
if ( ! defined ('ALLOWED') ) die('Appel direct ne sont pas permis');
header('Pragma: public');
header('Content-type: application/csv');
header('Content-Disposition: attachment;filename="ca_bal_simple.csv"',FALSE);
require_once NOALYSS_INCLUDE.'/class/class_anc_balance_simple.php';
require_once NOALYSS_INCLUDE.'/lib/class_noalyss_csv.php';
$cn=Dossier::connect();

View file

@ -24,11 +24,8 @@
*
*/
if ( ! defined ('ALLOWED') ) die('Appel direct ne sont pas permis');
header('Pragma: public');
header('Content-type: application/csv');
header('Content-Disposition: attachment;filename="jrn.csv"',FALSE);
require_once NOALYSS_INCLUDE.'/class/class_anc_listing.php';
require_once NOALYSS_INCLUDE.'/lib/class_noalyss_csv.php';
$cn=Dossier::connect();

View file

@ -23,11 +23,10 @@
* \brief export the anc tables in CSV
*/
if ( ! defined ('ALLOWED') ) die('Appel direct ne sont pas permis');
header('Pragma: public');
header('Content-type: application/csv');
header('Content-Disposition: attachment;filename="anc-table-export.csv"',FALSE);
require_once NOALYSS_INCLUDE.'/class/class_anc_table.php';
require_once NOALYSS_INCLUDE.'/lib/class_noalyss_csv.php';
$atable=new Anc_Table($cn);
$atable->get_request();
$atable->export_csv();

View file

@ -106,6 +106,7 @@ function j($p_string)
*/
function nb($p_number)
{
$r=trim($p_number);
$r = sprintf('%.2f', $p_number);
$r = str_replace('.', ',', $r);

View file

@ -29,33 +29,33 @@ class Noalyss_Csv
{
private $filename;
private $element;
function __construct($p_filename)
{
$this->filename=$p_filename;
$this->correct_name();
$this->element=array();
$this->size=0;
}
/***
* @brief
* Correct the name of the file , remove forbidden character and
* add extension
* add extension and date
*/
function correct_name()
private function correct_name()
{
if (trim(strlen($this->filename))==0) {
error_log('CSV->correct_name filename is empty');
throw new Exception('CSV-CORRECT_NAME');
}
// add extension if needed
if (strpos($this->filename, ".csv")==0)
{
$this->filename.".csv";
}
$this->filename.="-".date("ymd-Hi");
$this->filename.=".csv";
$this->filename=str_replace(";", "", $this->filename);
$this->filename=str_replace("/", "", $this->filename);
$this->filename=str_replace(":", "", $this->filename);
$this->filename=str_replace("*", "", $this->filename);
$this->filename=str_replace(" ", "_", $this->filename);
$this->filename=strtolower($this->filename);
}
@ -76,30 +76,75 @@ class Noalyss_Csv
}
/***
* write record , the numeric are detected are formatted properly with the
* function nb
* write header
* @param array $p_array Array of 1 dimension with the contains of a row
* @see nb
*
*/
function write($p_array)
function write_header($p_array)
{
$size_array=count($p_array);
$sep="";
for ($i=0; $i<$size_array; $i++)
{
if (isNumber($p_array[$i])==1)
{
printf($sep.'%s', nb($p_array[$i]));
}
else
{
printf($sep.'"%s"', $p_array[$i]);
}
$sep=";";
}
printf("\r\n");
}
/***
* Add column to export to csv , the string are enclosed with
* double-quote,
* @param $p_item value to export
* @param $p_type must be text(default) or number
* @throws Exception
*/
function add($p_item,$p_type="text")
{
if ( ! in_array($p_type, array("text","number"))) {
throw new Exception("NOALYSS_CSV::ADD");
}
$this->element[$this->size]['value']=$p_item;
$this->element[$this->size]['type']=$p_type;
$this->size++;
}
/***
* the string are enclosed with double-quote,
* we remove problematic character and
* the number are formatted.
* Clean the row after exporting
* @return nothing
*/
function write()
{
if ($this->size == 0 ) return;
$sep="";
for ($i=0;$i < $this->size;$i++)
{
if ($this->element[$i]['type'] == 'number' )
{
printf($sep.'%s', nb($this->element[$i]['value']));
}
else
{
// remove break-line,
$export=str_replace("\n"," ",$this->element[$i]['value']);
$export=str_replace("\r"," ", $export);
// remove double quote
$export=str_replace('"',"", $export);
printf($sep.'"%s"', $export);
}
$sep=";";
}
printf("\r\n");
$this->clean();
}
/**
* clean the row
*/
private function clean()
{
$this->element=array();
$this->size=0;
}
}