noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
export_ledger_csv.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  *   This file is part of NOALYSS.
00004  *
00005  *   NOALYSS is free software; you can redistribute it and/or modify
00006  *   it under the terms of the GNU General Public License as published by
00007  *   the Free Software Foundation; either version 2 of the License, or
00008  *   (at your option) any later version.
00009  *
00010  *   NOALYSS is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details.
00014  *
00015  *   You should have received a copy of the GNU General Public License
00016  *   along with NOALYSS; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 // Copyright Author Dany De Bontridder danydb@aevalys.eu
00020 /*! \file
00021  * \brief Send a ledger in CSV format
00022  */
00023 if ( ! defined ('ALLOWED') ) die('Appel direct ne sont pas permis');
00024 $fDate = date('dmy-Hi');
00025 header('Pragma: public');
00026 header('Content-type: application/csv');
00027 header('Content-Disposition: attachment;filename="jrn-'.$fDate.'.csv"',FALSE);
00028 include_once ("ac_common.php");
00029 require_once('class_own.php');
00030 require_once 'class_acc_ledger_sold.php';
00031 require_once 'class_acc_ledger_purchase.php';
00032 require_once('class_dossier.php');
00033 $gDossier=dossier::id();
00034 
00035 require_once('class_database.php');
00036 require_once("class_acc_ledger.php");
00037 
00038 /*
00039  * Variable from $_GET
00040  */
00041 $get_jrn=HtmlInput::default_value_get('jrn_id', -1);
00042 $get_option=HtmlInput::default_value_get('p_simple', -1);
00043 $get_from_periode=  HtmlInput::default_value_get('from_periode', null);
00044 $get_to_periode=HtmlInput::default_value_get('to_periode', NULL);
00045 
00046 //--- Check validity
00047 if ( $get_jrn ==-1  || $get_option == -1 || $get_from_periode == null || $get_to_periode == null)
00048 {
00049     die (_('Options invalides'));
00050 }
00051 
00052 
00053 require_once ('class_user.php');
00054 $g_user->Check();
00055 $g_user->check_dossier($gDossier);
00056 
00057 //----------------------------------------------------------------------------
00058 // $get_jrn == 0 when request for all ledger, in that case, we must filter
00059 // the legder with the security in Acc_Ledger::get_row
00060 //----------------------------------------------------------------------------
00061 if ($get_jrn!=0 &&  $g_user->check_jrn($get_jrn) =='X')
00062 {
00063     NoAccess();
00064     exit();
00065 }
00066 
00067 $Jrn=new Acc_Ledger($cn,$get_jrn);
00068 
00069 $Jrn->get_name();
00070 $jrn_type=$Jrn->get_type();
00071 
00072 //
00073 // With Detail per item which is possible only for VEN or ACH
00074 // 
00075 if ($get_option == 2)
00076 {
00077     if ($jrn_type != 'ACH' && $jrn_type != 'VEN' || $Jrn->id == 0)
00078     {
00079         $get_option = 0;
00080     }
00081     else
00082     {
00083         switch ($jrn_type)
00084         {
00085             case 'VEN':
00086                 $ledger = new Acc_Ledger_Sold($cn, $get_jrn);
00087                 $ret_detail = $ledger->get_detail_sale($get_from_periode, $get_to_periode);
00088                 break;
00089             case 'ACH':
00090                 $ledger = new Acc_Ledger_Purchase($cn, $get_jrn);
00091                 $ret_detail = $ledger->get_detail_purchase($get_from_periode, $get_to_periode);
00092                 
00093                 break;
00094             default:
00095                 die(__FILE__ . ":" . __LINE__ . 'Journal invalide');
00096                 break;
00097         }
00098         if ($ret_detail == null)
00099             return;
00100         $nb = Database::num_row($ret_detail);
00101         $output=fopen("php://output","w");
00102         
00103         for ($i = 0;$i < $nb ; $i++) {
00104             $row=Database::fetch_array($ret_detail, $i);
00105             if ( $i == 0 ) {
00106               foreach ($row as $key=>$value) {
00107                   if (isNumber($key) == 0 )$array_key[]=$key;
00108               }
00109               fputcsv($output,$array_key,';');
00110             }
00111             $a_row=array();
00112             for ($j=0;$j < count($row) / 2;$j++) {
00113                 $a_row[]=$row[$j];
00114             }
00115             fputcsv($output,$a_row,';');
00116             unset($a_row);
00117         }
00118     }
00119 }
00120 //-----------------------------------------------------------------------------
00121 // Detailled printing
00122 // For miscellaneous legder or all ledgers
00123 //-----------------------------------------------------------------------------
00124 if  ( $get_option == 0 )
00125 {
00126     $Jrn->get_row( $get_from_periode, $get_to_periode );
00127 
00128     if ( count($Jrn->row) == 0)
00129         exit;
00130     foreach ( $Jrn->row as $op )
00131     {
00132         // should clean description : remove <b><i> tag and '; char
00133         $desc=$op['description'];
00134         $desc=str_replace("<b>","",$desc);
00135         $desc=str_replace("</b>","",$desc);
00136         $desc=str_replace("<i>","",$desc);
00137         $desc=str_replace("</i>","",$desc);
00138         $desc=str_replace('"',"'",$desc);
00139         $desc=str_replace(";",",",$desc);
00140 
00141         printf("\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";%s;%s\n",
00142                $op['j_id'],
00143                $op['jr_pj_number'],
00144                $op['internal'],
00145                $op['j_date'],
00146                $op['poste'],
00147                $desc,
00148                nb($op['deb_montant']),
00149                nb($op['cred_montant'])
00150               );
00151 
00152     }
00153     exit;
00154 }
00155 //-----------------------------------------------------------------------------
00156 // Detail printing for ACH or VEN : 1 row resume the situation with VAT, DNA
00157 // for Misc the amount 
00158 // For Financial only the tiers and the sign of the amount
00159 //-----------------------------------------------------------------------------
00160 if  ($get_option == 1)
00161 {
00162    
00163 //-----------------------------------------------------
00164      if ( $jrn_type == 'ODS' || $jrn_type == 'FIN' || $jrn_type=='GL')
00165        {
00166           $Row=$Jrn->get_rowSimple($get_from_periode,
00167                              $get_to_periode,
00168                              0);
00169          printf ('" operation";'.
00170                  '"Date";'.
00171                  '"N° Pièce";'.
00172                  '"Tiers";'.
00173                  '"commentaire";'.
00174                  '"internal";'.
00175                  '"montant";'.
00176                  "\r\n");
00177          foreach ($Row as $line)
00178            {
00179 
00180              echo $line['num'].";";
00181              echo $line['date'].";";
00182              echo $line['jr_pj_number'].";";
00183              echo $Jrn->get_tiers($line['jrn_def_type'],$line['jr_id']).";";
00184              echo $line['comment'].";";
00185              echo $line['jr_internal'].";";
00186              //   echo "<TD>".$line['pj'].";";
00187              // If the ledger is financial :
00188              // the credit must be negative and written in red
00189              // Get the jrn type
00190              if ( $line['jrn_def_type'] == 'FIN' ) {
00191                $positive = $cn->get_value("select qf_amount from quant_fin  ".
00192                                           " where jr_id=".$line['jr_id']);
00193 
00194                echo nb($positive);
00195                echo ";";
00196              }
00197              else
00198                {
00199                  echo nb($line['montant']).";";
00200                }
00201 
00202              printf("\r\n");
00203            }
00204        }
00205 
00206 //------------------------------------------------------------------------------
00207 // One line summary with tiers, amount VAT, DNA, tva code ....
00208 // 
00209 //------------------------------------------------------------------------------
00210     if ( $jrn_type=='ACH' || $jrn_type=='VEN')
00211     {
00212         $Row=$Jrn->get_rowSimple($get_from_periode,
00213                              $get_to_periode,
00214                              0);
00215         $cn->prepare('reconcile_date',"select to_char(jr_date,'DD.MM.YY') as str_date,* from jrn where jr_id in (select jra_concerned from jrn_rapt where jr_id = $1 union all select jr_id from jrn_rapt where jra_concerned=$1)");
00216 
00217         $own=new Own($cn);
00218         $col_tva="";
00219 
00220         if ( $own->MY_TVA_USE=='Y')
00221         {
00222             $a_Tva=$cn->get_array("select tva_id,tva_label from tva_rate order by tva_rate,tva_label,tva_id");
00223             foreach($a_Tva as $line_tva)
00224             {
00225                 $col_tva.='"Tva '.$line_tva['tva_label'].'";';
00226             }
00227         }
00228         echo '"Date";"Paiement";"operation";"Pièce";"Client/Fourn.";"Commentaire";"inter.";"HTVA";privé;DNA;tva non ded.;'.$col_tva.'"TVAC";"opérations liées"'."\n\r";
00229         foreach ($Row as $line)
00230         {
00231             printf('"%s";"%s";"%s";"%s";"%s";%s;%s;%s;%s;%s;%s;',
00232                    $line['date'],
00233                    $line['date_paid'],
00234                    $line['num'],
00235                    $line['jr_pj_number'],
00236                    $Jrn->get_tiers($line['jrn_def_type'],$line['jr_id']),
00237                    $line['comment'],
00238                    $line['jr_internal'],
00239                    nb($line['HTVA']),
00240                    nb($line['dep_priv']),
00241                    nb($line['dna']),
00242                    nb($line['tva_dna'])
00243                    );
00244             $a_tva_amount=array();
00245             //- set all TVA to 0
00246             foreach ($a_Tva as $l) {
00247                 $t_id=$l["tva_id"];
00248                 $a_tva_amount[$t_id]=0;
00249             }
00250             foreach ($line['TVA'] as $lineTVA)
00251             {
00252                 $idx_tva=$lineTVA[1][0];
00253                 $a_tva_amount[$idx_tva]=$lineTVA[1][2];
00254              }
00255             if ($own->MY_TVA_USE == 'Y' )
00256             {
00257                 foreach ($a_Tva as $line_tva)
00258                 {
00259                     $a=$line_tva['tva_id'];
00260                     echo nb($a_tva_amount[$a]).';';
00261                 }
00262             }
00263             echo nb ($line['TVAC']);
00264             /**
00265              * Retrieve payment if any
00266              */
00267              $ret_reconcile=$cn->execute('reconcile_date',array($line['jr_id']));
00268              $max=Database::num_row($ret_reconcile);
00269             if ($max > 0) {
00270                 $sep=";";
00271                 for ($e=0;$e<$max;$e++) {
00272                     $row=Database::fetch_array($ret_reconcile, $e);
00273                     echo $sep.$row['str_date'].'; '. $row['jr_internal'];
00274                 }
00275             }
00276             printf("\r\n");
00277 
00278         }
00279     }
00280 }
00281 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations