noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_anc_balance_simple.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 
00020 // Copyright Author Dany De Bontridder danydb@aevalys.eu
00021 
00022 /*!\file
00023   \brief manage the simple balance for CA, inherit from balance_ca
00024  */
00025 
00026 require_once("class_ihidden.php");
00027 require_once ('class_anc_print.php');
00028 require_once ('class_anc_plan.php');
00029 require_once ('ac_common.php');
00030 include_once("class_pdf.php");
00031 require_once ('header_print.php');
00032 /*! \brief manage the simple balance for CA, inherit from balance_ca
00033  *
00034  */
00035 
00036 class Anc_Balance_Simple extends Anc_Print
00037 {
00038 
00039     /*!
00040      * \brief load the data from the database
00041      *
00042      * \return array
00043      */
00044     function load()
00045     {
00046         $filter=$this->set_sql_filter();
00047         // sum debit
00048 
00049         $sql="select m.po_id,sum(deb) as sum_deb,sum(cred) as sum_cred,";
00050         $sql.=" po_name||'  '||coalesce(po_description,'') as po_name";
00051         $sql.=" from ";
00052         $sql.=" (select po_id,case when oa_debit='t' then oa_amount else 0 end as deb,";
00053         $sql.="case when oa_debit='f' then oa_amount else 0 end as cred ";
00054         $sql.=" from operation_analytique join poste_analytique using(po_id)";
00055         $sql.=(empty($filter) == false)?" where ".$filter:"";
00056         $sql.=" ) as m join poste_analytique using (po_id)";
00057         $sql.=" where pa_id=".$this->pa_id;
00058         $sql.=" group by po_id,po_name,po_description";
00059         $sql.=" order by po_id";
00060         $res=$this->db->exec_sql($sql);
00061 
00062         if ( Database::num_row($res) == 0 ) {
00063             $this->has_data=0;
00064             return null;
00065         }
00066         $a=array();
00067         $count=0;
00068         $array=Database::fetch_all($res);
00069         foreach ($array as $row)
00070         {
00071             $a[$count]['po_id']=$row['po_id'];
00072             $a[$count]['sum_deb']=$row['sum_deb'];
00073             $a[$count]['sum_cred']=$row['sum_cred'];
00074             $a[$count]['po_name']=$row['po_name'];
00075             $a[$count]['solde']=abs($row['sum_deb']-$row['sum_cred']);
00076             $a[$count]['debit']=($row['sum_deb']>$row['sum_cred'])?"debit":"credit";
00077             $count++;
00078         }
00079         $this->has_data=$count;
00080         return $a;
00081 
00082 
00083     }
00084     /*!
00085      * \brief Set the filter (account_date)
00086      *
00087      * \return return the string to add to load
00088      */
00089 
00090 
00091     function set_sql_filter()
00092     {
00093         $sql="";
00094         $and="";
00095         if ( $this->from != "" )
00096         {
00097             $sql.=" oa_date >= to_date('".$this->from."','DD.MM.YYYY')";
00098             $and=" and ";
00099         }
00100         if ( $this->to != "" )
00101         {
00102             $sql.=" $and oa_date <= to_date('".$this->to."','DD.MM.YYYY')";
00103             $and=" and ";
00104         }
00105         if ( $this->from_poste != "" )
00106         {
00107             $sql.=" $and upper(po_name)>= upper('".$this->from_poste."')";
00108             $and=" and ";
00109         }
00110         if ( $this->to_poste != "" )
00111         {
00112             $sql.=" $and upper(po_name)<= upper('".$this->to_poste."')";
00113             $and=" and ";
00114         }
00115         return $sql;
00116 
00117     }
00118     /*!
00119      * \brief compute the html display
00120      *
00121      *
00122      * \return string
00123      */
00124     function display_html()
00125     {
00126         $r="<table class=\"result\">";
00127         $r.="<tr>";
00128         $r.="<th>Poste comptable Analytique</th>";
00129         $r.="<th>D&eacute;bit</th>";
00130         $r.="<th>Cr&eacute;dit</th>";
00131         $r.="<th>Solde</th>";
00132         $r.="<th>D/C</th>";
00133         $r.="</tr>";
00134 
00135         $array=$this->load();
00136         $odd=0;
00137         if ( is_array($array) == false )
00138         {
00139             return $array;
00140 
00141         }
00142         foreach ( $array as $row)
00143         {
00144             $odd++;
00145 
00146             $r.=($odd%2==0)?'<tr class="odd">':'<tr class="even">';
00147             // the name and po_id
00148             //    $r.=sprintf("<td>%s</td>",$row['po_id']);
00149             $r.=sprintf("<td align=\"left\">%s</td>",h($row['po_name']));
00150             $r.=td(nbm($row['sum_deb']),' class="num"');
00151             $r.=td(nbm($row['sum_cred']),' class="num"');
00152             $r.=td(nbm($row['solde']),' class="num"');
00153             $deb=($row['sum_deb'] > $row['sum_cred'])?"D":"C";
00154             $deb=($row['solde'] == 0 )?'':$deb;
00155             $r.=sprintf("<td>%s</td>",$deb);
00156             $r.="</tr>";
00157         }
00158         $r.="</table>";
00159         return $r;
00160     }
00161     /*!
00162      * \brief Compute  the form to display
00163      * \param $p_hidden hidden tag to be included (gDossier,...)
00164      *
00165      *
00166      * \return string containing the data
00167      */
00168     function display_form($p_string="")
00169     {
00170         $r=parent::display_form($p_string);
00171 
00172         $r.= HtmlInput::submit('Affiche', _('Rechercher'));
00173 
00174         return $r;
00175     }
00176 
00177     /*!
00178      * \brief Display the result in pdf
00179      *
00180      * \return none
00181      */
00182     function display_pdf()
00183     {
00184         $array=$this->load();
00185         $pdf=new PDFBalance_Simple($this->db);
00186         $pdf->set_info($this->from_poste,$this->to_poste,$this->from,$this->to);
00187         $pdf->AliasNbPages();
00188         $pdf->AddPage();
00189         $pdf->setTitle("Balance analytique",true);
00190 
00191         $pdf->SetFont('DejaVu','',6);
00192         for ($i=0;$i<count($array);$i++)
00193         {
00194             $row=$array[$i];
00195             $pdf->Cell(20,6,$row['po_id'],0,0,'L');
00196             $pdf->Cell(90,6,$row['po_name'],0,0,'L');
00197             $pdf->Cell(20,6,sprintf('%s',nbm($row['sum_deb'])),0,0,'R');
00198             $pdf->Cell(20,6,sprintf('%s',nbm($row['sum_cred'])),0,0,'R');
00199             $pdf->Cell(20,6,sprintf('%s',nbm($row['solde'])),0,0,'R');
00200             $pdf->Cell(20,6,$row['debit'],0,0,'R');
00201             $pdf->Ln();
00202         }
00203         $fDate=date('dmy-Hi');
00204         $pdf->output('simple-balance-'.$fDate.'.pdf','D');
00205 
00206     }
00207     /*!
00208      * \brief Compute the csv export
00209      * \return string with the csv
00210      */
00211     function display_csv()
00212     {
00213         $array=$this->load();
00214         if ( is_array($array) == false )
00215         {
00216             return $array;
00217 
00218         }
00219         $r="";
00220         foreach ( $array as $row)
00221         {
00222             // the name and po_id
00223             $solde=($row['sum_cred']>$row['sum_deb'])?'C':'D';
00224             $solde=($row['sum_cred']==$row['sum_deb'])?'':$solde;
00225             $r.=sprintf("'%s';",$row['po_id']);
00226             $r.=sprintf("'%s';",$row['po_name']);
00227             $r.=sprintf("%s;",nb($row['sum_deb']));
00228             $r.=sprintf("%s;",nb($row['sum_cred']));
00229             $r.=sprintf("%s;",nb($row['solde']));
00230             $r.=sprintf("'%s'",$row['debit']);
00231             $r.="\r\n";
00232         }
00233         return $r;
00234 
00235     }
00236     /*!
00237      * \brief Show the button to export in PDF or CSV
00238      * \param $url_csv url of the csv
00239      * \param $url_pdf url of the pdf
00240      * \param $p_string hidden data to include in the form
00241      *
00242      *
00243      * \return string with the button
00244      */
00245     function show_button($p_string="")
00246     {
00247         $r="";
00248         $r.= '<form method="GET" action="export.php" style="display:inline">';
00249         $r.= $p_string;
00250         $r.= dossier::hidden();
00251         $r.= HtmlInput::hidden("to",$this->to);
00252         $r.= HtmlInput::hidden("act","PDF:AncBalSimple");
00253 
00254         $r.= HtmlInput::hidden("from",$this->from);
00255         $r.= HtmlInput::hidden("pa_id",$this->pa_id);
00256         $r.= HtmlInput::hidden("from_poste",$this->from_poste);
00257         $r.= HtmlInput::hidden("to_poste",$this->to_poste);
00258         $r.=HtmlInput::submit('bt_pdf',"Export en PDF");
00259         $r.= '</form>';
00260 
00261         $r.= '<form method="GET" action="export.php"  style="display:inline">';
00262         $r.= HtmlInput::hidden("act","CSV:AncBalSimple");
00263         $r.= HtmlInput::hidden("to",$this->to);
00264         $r.= HtmlInput::hidden("from",$this->from);
00265         $r.= HtmlInput::hidden("pa_id",$this->pa_id);
00266         $r.= HtmlInput::hidden("from_poste",$this->from_poste);
00267         $r.= HtmlInput::hidden("to_poste",$this->to_poste);
00268         $r.= $p_string;
00269         $r.= dossier::hidden();
00270         $r.=HtmlInput::submit('bt_csv',"Export en CSV");
00271         $r.= '</form>';
00272         return $r;
00273     }
00274 
00275     /*!
00276      * \brief for testing and debuggind the class
00277      *        it must never be called from production system, it is
00278      *        intended only for developpers
00279      * \param
00280      * \param
00281      * \param
00282      *
00283      *
00284      * \return none
00285      */
00286     static  function test_me ()
00287     {
00288         // call the page with ?gDossier=14
00289         $a=new Database(dossier::id());
00290 
00291         $bal=new Anc_Balance_Simple($a);
00292         $bal->get_request();
00293 
00294         echo '<form method="GET">';
00295 
00296         echo $bal->display_form();
00297         echo '</form>';
00298         if ( isset($_GET['result']))
00299         {
00300             echo $bal->show_button("","");
00301             echo "<h1>HTML</h1>";
00302             echo $bal->display_html();
00303             echo "<h1>CSV</h1>";
00304             echo $bal->display_csv();
00305             /*  echo "<h1>pdf</h1>"; */
00306             /*  echo $bal->display_pdf(); */
00307 
00308         }
00309 
00310     }
00311 }
 All Data Structures Namespaces Files Functions Variables Enumerations