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                 $a_heading= Acc_Ledger_Sold::heading_detail_sale();
00089                 
00090                 break;
00091             case 'ACH':
00092                 $ledger = new Acc_Ledger_Purchase($cn, $get_jrn);
00093                 $ret_detail = $ledger->get_detail_purchase($get_from_periode, $get_to_periode);
00094                 $a_heading=  Acc_Ledger_Purchase::heading_detail_purchase();
00095                 break;
00096             default:
00097                 die(__FILE__ . ":" . __LINE__ . 'Journal invalide');
00098                 break;
00099         }
00100         if ($ret_detail == null)
00101             return;
00102         $nb = Database::num_row($ret_detail);
00103         $output=fopen("php://output","w");
00104         
00105         for ($i = 0;$i < $nb ; $i++) {
00106             $row=Database::fetch_array($ret_detail, $i);
00107             if ( $i == 0 ) {
00108               fputcsv($output,$a_heading,';');
00109             }
00110             $a_row=array();
00111             for ($j=0;$j < count($row) / 2;$j++) {
00112                 $a_row[]=$row[$j];
00113             }
00114             fputcsv($output,$a_row,';');
00115             unset($a_row);
00116         }
00117     }
00118 }
00119 //-----------------------------------------------------------------------------
00120 // Detailled printing
00121 // For miscellaneous legder or all ledgers
00122 //-----------------------------------------------------------------------------
00123 if  ( $get_option == 0 )
00124 {
00125     $Jrn->get_row( $get_from_periode, $get_to_periode );
00126 
00127     if ( count($Jrn->row) == 0)
00128         exit;
00129     foreach ( $Jrn->row as $op )
00130     {
00131         // should clean description : remove <b><i> tag and '; char
00132         $desc=$op['description'];
00133         $desc=str_replace("<b>","",$desc);
00134         $desc=str_replace("</b>","",$desc);
00135         $desc=str_replace("<i>","",$desc);
00136         $desc=str_replace("</i>","",$desc);
00137         $desc=str_replace('"',"'",$desc);
00138         $desc=str_replace(";",",",$desc);
00139 
00140         printf("\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";%s;%s\n",
00141                $op['j_id'],
00142                $op['jr_pj_number'],
00143                $op['internal'],
00144                $op['j_date'],
00145                $op['poste'],
00146                $desc,
00147                nb($op['deb_montant']),
00148                nb($op['cred_montant'])
00149               );
00150 
00151     }
00152     exit;
00153 }
00154 //-----------------------------------------------------------------------------
00155 // Detail printing for ACH or VEN : 1 row resume the situation with VAT, DNA
00156 // for Misc the amount 
00157 // For Financial only the tiers and the sign of the amount
00158 //-----------------------------------------------------------------------------
00159 if  ($get_option == 1)
00160 {
00161    
00162 //-----------------------------------------------------
00163      if ( $jrn_type == 'ODS' || $jrn_type == 'FIN' || $jrn_type=='GL')
00164        {
00165           $Row=$Jrn->get_rowSimple($get_from_periode,
00166                              $get_to_periode,
00167                              0);
00168          printf ('" operation";'.
00169                  '"Date";'.
00170                  '"N° Pièce";'.
00171                  '"Tiers";'.
00172                  '"commentaire";'.
00173                  '"internal";'.
00174                  '"montant";'.
00175                  "\r\n");
00176          foreach ($Row as $line)
00177            {
00178 
00179              echo $line['num'].";";
00180              echo $line['date'].";";
00181              echo $line['jr_pj_number'].";";
00182              echo $Jrn->get_tiers($line['jrn_def_type'],$line['jr_id']).";";
00183              echo $line['comment'].";";
00184              echo $line['jr_internal'].";";
00185              //   echo "<TD>".$line['pj'].";";
00186              // If the ledger is financial :
00187              // the credit must be negative and written in red
00188              // Get the jrn type
00189              if ( $line['jrn_def_type'] == 'FIN' ) {
00190                $positive = $cn->get_value("select qf_amount from quant_fin  ".
00191                                           " where jr_id=".$line['jr_id']);
00192 
00193                echo nb($positive);
00194                echo ";";
00195              }
00196              else
00197                {
00198                  echo nb($line['montant']).";";
00199                }
00200 
00201              printf("\r\n");
00202            }
00203        }
00204 
00205 //------------------------------------------------------------------------------
00206 // One line summary with tiers, amount VAT, DNA, tva code ....
00207 // 
00208 //------------------------------------------------------------------------------
00209     if ( $jrn_type=='ACH' || $jrn_type=='VEN')
00210     {
00211         $Row=$Jrn->get_rowSimple($get_from_periode,
00212                              $get_to_periode,
00213                              0);
00214         $cn->prepare('reconcile_date',"select to_char(jr_date,'DD.MM.YY') as str_date,* "
00215                 . "from jrn "
00216                 . "where "
00217                 . "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)");
00218 
00219         $own=new Own($cn);
00220         $col_tva="";
00221 
00222         if ( $own->MY_TVA_USE=='Y')
00223         {
00224             $a_Tva=$cn->get_array("select tva_id,tva_label from tva_rate order by tva_rate,tva_label,tva_id");
00225             foreach($a_Tva as $line_tva)
00226             {
00227                 $col_tva.='"Tva '.$line_tva['tva_label'].'";';
00228             }
00229         }
00230         echo '"Date";"Paiement";"operation";"Pièce";"Client/Fourn.";"Commentaire";"inter.";"HTVA";"privé";"DNA";"tva non ded.";"TVA NP";'.$col_tva.'"TVAC";"opérations liées"'."\n\r";
00231         foreach ($Row as $line)
00232         {
00233             printf('"%s";"%s";"%s";"%s";"%s";%s;%s;%s;%s;%s;%s;%s;',
00234                    $line['date'],
00235                    $line['date_paid'],
00236                    $line['num'],
00237                    $line['jr_pj_number'],
00238                    $Jrn->get_tiers($line['jrn_def_type'],$line['jr_id']),
00239                    $line['comment'],
00240                    $line['jr_internal'],
00241                    nb($line['HTVA']),
00242                    nb($line['dep_priv']),
00243                    nb($line['dna']),
00244                    nb($line['tva_dna']),
00245                     nb($line['tva_np'])
00246                    );
00247             $a_tva_amount=array();
00248             //- set all TVA to 0
00249             foreach ($a_Tva as $l) {
00250                 $t_id=$l["tva_id"];
00251                 $a_tva_amount[$t_id]=0;
00252             }
00253             foreach ($line['TVA'] as $lineTVA)
00254             {
00255                 $idx_tva=$lineTVA[1][0];
00256                 $a_tva_amount[$idx_tva]=$lineTVA[1][2];
00257              }
00258             if ($own->MY_TVA_USE == 'Y' )
00259             {
00260                 foreach ($a_Tva as $line_tva)
00261                 {
00262                     $a=$line_tva['tva_id'];
00263                     echo nb($a_tva_amount[$a]).';';
00264                 }
00265             }
00266             echo nb ($line['TVAC']);
00267             /**
00268              * Retrieve payment if any
00269              */
00270              $ret_reconcile=$cn->execute('reconcile_date',array($line['jr_id']));
00271              $max=Database::num_row($ret_reconcile);
00272             if ($max > 0) {
00273                 $sep=";";
00274                 for ($e=0;$e<$max;$e++) {
00275                     $row=Database::fetch_array($ret_reconcile, $e);
00276                     echo $sep.$row['str_date'].'; '. $row['jr_internal'];
00277                 }
00278             }
00279             printf("\r\n");
00280 
00281         }
00282     }
00283 }
00284 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations