class_anc_balance_double.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003  *   This file is part of PhpCompta.
00004  *
00005  *   PhpCompta 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  *   PhpCompta 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 PhpCompta; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 /* $Revision: 4843 $ */
00020 
00021 // Copyright Author Dany De Bontridder ddebontridder@yahoo.fr
00022 
00023 /*!\file
00024  * \brief
00025  *  Print the crossed balance between 2 plan
00026  */
00027 
00028 /*! \brief
00029  *  Print the crossed balance between 2 plan
00030  *
00031  */
00032 require_once("class_iselect.php");
00033 require_once("class_itext.php");
00034 require_once("class_ibutton.php");
00035 require_once("class_ihidden.php");
00036 require_once ('class_anc_print.php');
00037 require_once ('class_anc_plan.php');
00038 require_once('class_pdf.php');
00039 
00040 class Anc_Balance_Double extends Anc_Print
00041 {
00042     /*!
00043      * \brief compute the html display
00044      *
00045      *
00046      * \return string
00047      */
00048 
00049     function display_html ()
00050     {
00051         $r="";
00052 
00053         $array=$this->load();
00054         $odd=0;
00055         if ( is_array($array) == false )
00056         {
00057             return $array;
00058 
00059         }
00060         $old="";
00061         $tot_deb=0;
00062         $tot_cred=0;
00063 
00064         foreach ( $array as $row)
00065         {
00066             $odd++;
00067 
00068             $r.=($odd%2==0)?'<tr class="odd">':'</tr>';
00069 
00070             if ( $old == $row['a_po_name'] )
00071             {
00072                 $r.='<td></td>';
00073             }
00074             else
00075             {
00076 
00077                 if ( $tot_deb != 0 || $tot_cred !=0 )
00078                 {
00079                   $r.="<tr>".td('');
00080                   $r.="<td>Total </td>".td(nbm($tot_deb),' class="num"').td(nbm($tot_cred),' class="num"');
00081                     $s=abs($tot_deb-$tot_cred);
00082                     $d=($tot_deb>$tot_cred)?'debit':'credit';
00083                     $r.="<td class=\"num\">".nbm($s)."</td><td>$d</td>";
00084                     $r.="</tr>";
00085                 }
00086                 $tot_deb=0;
00087                 $tot_cred=0;
00088 
00089                 // new
00090                 $r.="</table>";
00091                 $r.="<table class=\"result\" style=\"margin-bottom:3px\">";
00092                 $r.="<tr>";
00093                 $r.="<th style=\"width:30%\" >Poste comptable Analytique</th>";
00094                 $r.="<th style=\"width:30%\">Poste comptable Analytique</th>";
00095                 $r.="<th style=\"text-align:right\">D&eacute;bit</th>";
00096                 $r.="<th style=\"text-align:right\">Cr&eacute;dit</th>";
00097                 $r.="<th style=\"text-align:right\">Solde</th>";
00098                 $r.="<th>D/C</th>";
00099                 $r.="</tr>";
00100                 $r.='<tr>';
00101                 $r.=td($row['a_po_name'].' '.$row['a_po_description']);
00102                 $old=$row['a_po_name'];
00103                 $r.= '</tr>';
00104                 $r.= '<tr>';
00105                 $r.=td('');
00106             }
00107             $tot_deb+=$row['a_d'];
00108             $tot_cred+=$row['a_c'];
00109 
00110             $r.=td($row['b_po_name']." ".$row['b_po_description']);
00111 
00112             $r.=td(nbm($row['a_d']),' class="num"');
00113             $r.=td(nbm($row['a_c']),' class="num"');
00114             $r.=td(nbm($row['a_solde']),' class="num"');
00115             $r.=sprintf("<td>%s</td>",$row['a_debit']);
00116             $r.="</tr>";
00117         } /* end loop */
00118 
00119         if ( $tot_deb != 0 || $tot_cred !=0 )
00120         {
00121           $r.="<tr>".td('');
00122             $r.="<td>Total </td> <td ' class=\"num\"> ".nbm($tot_deb)." </td> <td ' class=\"num\">".nbm($tot_cred)."</td>";
00123             $s=abs($tot_deb-$tot_cred);
00124             $d=($tot_deb>$tot_cred)?'debit':'credit';
00125             $r.=td(nbm($s),' class="num"')."<td>$d</td>";
00126             $r.="</tr>";
00127         }
00128 
00129         $r.="</table>";
00130         $r.=h2info('Résumé');
00131         $r.='<table class="result">';
00132         $r.='<tr>';
00133         $r.=th('Po').
00134           th('Nom').
00135           th('Débit',' style="text-align:right"').
00136           th('Crédit','style="text-align:right" ').
00137           th('Solde',' style="text-align:right"');
00138 
00139         $sum=$this->show_sum($array);
00140         $tot_cred=0;$tot_deb=0;
00141         foreach ($sum as $row)
00142         {
00143             $r.='<tr>';
00144             $r.='<td>'.$row['poste'].'</td>';
00145             $r.='<td>'.$row['desc'].'</td>';
00146             $r.='<td class="num">'.nbm($row['debit']).'</td>';
00147             $r.='<td class="num">'.nbm($row['credit']).'</td>';
00148             $diff=bcsub($row['debit'],$row['credit']);
00149             $tot_cred=bcadd($tot_cred,$row['credit']);
00150             $tot_deb=bcadd($tot_deb,$row['debit']);
00151 
00152             $r.=td(nbm($diff),' class="num" ');
00153 
00154             $r.='<td>'.$row['dc'].'</td>';
00155             $r.='</tr>';
00156         }
00157         $r.=td('');
00158         $r.=td('total');
00159         $r.=td(nbm($tot_deb),'class="num"');
00160         $r.=td(nbm($tot_cred),'class="num"');
00161         $solde=bcsub($tot_deb,$tot_cred);
00162         $sign=($tot_cred<$tot_deb)?" - ":" + ";
00163         $r.=td($sign.nbm($solde),'class="num" style="border:solid 1px blue;font-weight:bold"');
00164         $r.='</tr>';
00165         $r.='</table>';
00166 
00167         return $r;
00168     }
00169     /*!
00170      * \brief Display the result in pdf
00171      *
00172      * \return none
00173      */
00174     function display_pdf()
00175     {
00176         $array=$this->load();
00177         if (empty($array))return;
00178         $pdf=new PDF($this->db);
00179         $pdf->Setdossierinfo(dossier::name());
00180         $pdf->setTitle("Balance analytique",true);
00181         $pdf->SetAuthor('Phpcompta');
00182         $pdf->AliasNbPages();
00183         $pdf->AddPage();
00184 
00185         $pa=new Anc_Plan($this->db,$this->pa_id);
00186         $pa->get();
00187         $pb=new Anc_Plan($this->db,$this->pa_id2);
00188         $pb->get();
00189         $pdf->SetFont('DejaVu','B',9);
00190         $pdf->Cell(0,7,sprintf("Balance croise plan %s %s ",
00191                                $pa->name,
00192                                $pb->name),1,0,'C');
00193         $filtre_date="";
00194         $filtre_pa="";
00195         $filtre_pb="";
00196 
00197         if ( $this->from !="" ||$this->to !="")
00198             $filtre_date=sprintf("Filtre date  %s %s",
00199                                  $this->from,
00200                                  $this->to);
00201         if ( $this->from_poste !="" ||$this->to_poste !="")
00202             $filtre_pa=sprintf("Filtre poste plan1  %s %s",
00203                                ($this->from_poste!="")?"de ".$this->from_poste:" ",
00204                                ($this->to_poste!="")?"jusque ".$this->to_poste:"");
00205 
00206         if ( $this->from_poste2 !="" ||$this->to_poste2 !="")
00207             $filtre_pb=sprintf("Filtre poste plan2   %s  %s",
00208                                ($this->from_poste2!="")?"de ".$this->from_poste2:" ",
00209                                ($this->to_poste2!="")?"jusque ".$this->to_poste2:"");
00210 
00211         $pdf->SetFont('DejaVu','',8);
00212         $pdf->Cell(50,7,$filtre_date);
00213         $pdf->Cell(50,7,$filtre_pa);
00214         $pdf->Cell(50,7,$filtre_pb);
00215         $pdf->Ln();
00216 
00217         $pdf->SetFont('DejaVu','',6);
00218         $pdf->Cell(20,7,'id','B');
00219         $pdf->Cell(100,7,'Poste Comptable','B');
00220         $pdf->Cell(20,7,'Débit','B',0,'L');
00221         $pdf->Cell(20,7,'Crédit','B',0,'L');
00222         $pdf->Cell(20,7,'Solde','B',0,'L');
00223         $pdf->Cell(20,7,'D/C','B',0,'L');
00224         $pdf->Ln();
00225 
00226         for ($i=0;$i<count($array);$i++)
00227         {
00228             $row=$array[$i];
00229             $pdf->Cell(20,6,$row['a_po_name'],0,0,'L');
00230             $pdf->Cell(40,6,mb_substr($row['a_po_description'],0,31),0,0,'L');
00231             $pdf->Cell(20,6,$row['b_po_name'],0,0,'L');
00232             $pdf->Cell(40,6,mb_substr($row['b_po_description'],0,31),0,0,'L');
00233             $pdf->Cell(20,6,$row['a_d'],0,0,'R');
00234             $pdf->Cell(20,6,$row['a_c'],0,0,'R');
00235             $pdf->Cell(20,6,$row['a_solde'],0,0,'R');
00236             $pdf->Cell(20,6,$row['a_debit'],0,0,'C');
00237             $pdf->Ln();
00238         }
00239 
00240         $sum=$this->show_sum($array);
00241         $pdf->SetFont('DejaVu','B',8);
00242         $pdf->Cell(70,6,'Somme',1,0,'C');
00243         $pdf->Ln(5);
00244         $pdf->SetFont('DejaVu','',6);
00245 
00246         $pdf->Cell(20,7,'Poste');
00247         $pdf->Cell(60,7,'Description','B');
00248         $pdf->Cell(20,7,'Débit','B',0,'L');
00249         $pdf->Cell(20,7,'Crédit','B',0,'L');
00250         $pdf->Cell(20,7,'Solde','B',0,'L');
00251         $pdf->Cell(20,7,'D/C','B',0,'L');
00252         $pdf->Ln();
00253 
00254         for ($i=0;$i<count($sum);$i++)
00255         {
00256             $row=$sum[$i];
00257             $pdf->Cell(20,6,$row['poste'],0,0,'L');
00258             $pdf->Cell(60,6,$row['desc'],0,0,'L');
00259             $pdf->Cell(20,6,sprintf('%.2f',$row['debit']),0,0,'R');
00260             $pdf->Cell(20,6,sprintf('%.2f',$row['credit']),0,0,'R');
00261             $pdf->Cell(20,6,sprintf('%.2f',$row['solde']),0,0,'R');
00262             $pdf->Cell(20,6,$row['dc'],0,0,'R');
00263             $pdf->Ln();
00264         }
00265         $fDate=date('dmy-Hi');
00266         $pdf->output('crossbalance-'.$fDate.'.pdf','D');
00267     }
00268 
00269 
00270     /*!
00271      * \brief Compute the csv export
00272      * \return string with the csv
00273      */
00274     function display_csv()
00275     {
00276         $r="";
00277 
00278         $r.='"Poste comptable Analytique";';
00279         $r.='"Poste comptable Analytique";';
00280         $r.='"Debit";';
00281         $r.='"Credit";';
00282         $r.='"Solde";';
00283         $r.='"D/C"';
00284 
00285         $r.="\r\n";
00286 
00287         $array=$this->load();
00288         if ( is_array($array) == false )
00289         {
00290             return $array;
00291 
00292         }
00293         foreach ( $array as $row)
00294         {
00295 
00296             $r.=sprintf('"%s";',$row['a_po_name']);
00297             $r.=sprintf('"%s";',$row['b_po_name']);
00298             $r.=sprintf("%s;",nb($row['a_d']));
00299             $r.=sprintf("%s;",nb($row['a_c']));
00300             $r.=sprintf("%s;",nb($row['a_solde']));
00301             $r.=sprintf('"%s"',$row['a_debit']);
00302             $r.="\r\n";
00303         }
00304 
00305         return $r;
00306 
00307     }
00308     /*!
00309      * \brief Compute  the form to display
00310      * \param $p_hidden hidden tag to be included (gDossier,...)
00311      *
00312      *
00313      * \return string containing the data
00314      */
00315     function display_form($p_string='')
00316     {
00317         $r=parent::display_form($p_string);
00318         // show the second plan
00319         $r.='<span style="padding:5px;margin:5px;border:2px double  blue;display:block;">';
00320         $plan=new Anc_Plan($this->db);
00321         $plan_id=new ISelect("pa_id2");
00322         $plan_id->value=$this->db->make_array("select pa_id, pa_name from plan_analytique order by pa_name");
00323         $plan_id->selected=$this->pa_id2;
00324         $r.= "Plan Analytique :".$plan_id->input();
00325         $r.=HtmlInput::request_to_hidden(array('ac'));
00326         $poste=new IText();
00327         $poste->size=10;
00328         $r.="Entre le poste ".$poste->input("from_poste2",$this->from_poste2);
00329         $choose=new IButton();
00330         $choose->name="Choix Poste";
00331         $choose->label=_("Recherche");
00332         $choose->javascript="onClick=search_ca(".dossier::id().",'from_poste2','pa_id2')";
00333         $r.=$choose->input();
00334 
00335         $r.=" et le poste ".$poste->input("to_poste2",$this->to_poste2);
00336         $choose->javascript="onClick=search_ca(".dossier::id().",'to_poste2','pa_id2')";
00337         $r.=$choose->input();
00338         $r.='<span class="notice" style="display:block">'.
00339             _('Selectionnez le plan qui vous int&eacute;resse avant de cliquer sur Recherche').
00340             '</span>';
00341 
00342         $r.='</span>';
00343         $r.=HtmlInput::submit('Affiche','Affiche');
00344         return $r;
00345     }
00346     /*!
00347      * \brief Show the button to export in PDF or CSV
00348      * \param $p_string hidden data to include in the form
00349      *
00350      *
00351      * \return string with the button
00352      */
00353     function show_button($p_string="")
00354     {
00355         $r="";
00356         $r.= '<form method="GET" action="export.php" style="display:inline">';
00357         $r.= $p_string;
00358         $r.= HtmlInput::hidden("to",$this->to);
00359         $r.= HtmlInput::hidden("act","PDF:AncBalDouble");
00360         $r.= HtmlInput::hidden("from",$this->from);
00361         $r.= HtmlInput::hidden("pa_id",$this->pa_id);
00362         $r.= HtmlInput::hidden("from_poste",$this->from_poste);
00363         $r.= HtmlInput::hidden("to_poste",$this->to_poste);
00364         $r.= HtmlInput::hidden("pa_id2",$this->pa_id2);
00365         $r.= HtmlInput::hidden("from_poste2",$this->from_poste2);
00366         $r.= HtmlInput::hidden("to_poste2",$this->to_poste2);
00367         $r.=dossier::hidden();
00368         $r.=HtmlInput::submit('bt_pdf',"Export en PDF");
00369         $r.= '</form>';
00370 
00371         $r.= '<form method="GET" action="export.php"  style="display:inline">';
00372         $r.= HtmlInput::hidden("to",$this->to);
00373         $r.= HtmlInput::hidden("act","CSV:AncBalDouble");
00374         $r.= HtmlInput::hidden("from",$this->from);
00375         $r.= HtmlInput::hidden("pa_id",$this->pa_id);
00376         $r.= HtmlInput::hidden("from_poste",$this->from_poste);
00377         $r.= HtmlInput::hidden("to_poste",$this->to_poste);
00378         $r.= HtmlInput::hidden("pa_id2",$this->pa_id2);
00379         $r.= HtmlInput::hidden("from_poste2",$this->from_poste2);
00380         $r.= HtmlInput::hidden("to_poste2",$this->to_poste2);
00381         $r.= $p_string;
00382         $r.= dossier::hidden();
00383         $r.=HtmlInput::submit('bt_csv',"Export en CSV");
00384         $r.= '</form>';
00385         return $r;
00386 
00387     }
00388     /*!
00389      * \brief complete the object with the data in $_REQUEST
00390      */
00391     function get_request()
00392     {
00393         parent::get_request();
00394         $this->from_poste2=(isset($_REQUEST['from_poste2']))?$_REQUEST['from_poste2']:"";
00395         $this->to_poste2=(isset($_REQUEST['to_poste2']))?$_REQUEST['to_poste2']:"";
00396         $this->pa_id2=(isset($_REQUEST['pa_id2']))?$_REQUEST['pa_id2']:"";
00397 
00398     }
00399     /*!
00400      * \brief load the data from the database
00401      *
00402      * \return array
00403      */
00404     function load()
00405     {
00406         $filter_poste="";
00407         $and="";
00408         if ( $this->from_poste != "" )
00409         {
00410             $filter_poste.=" $and upper(pa.po_name)>= upper('".Database::escape_string($this->from_poste)."')";
00411             $and=" and ";
00412 
00413         }
00414         if ( $this->to_poste != "" )
00415         {
00416             $filter_poste.=" $and upper(pa.po_name)<= upper('".Database::escape_string($this->to_poste)."')";
00417             $and=" and ";
00418         }
00419 
00420         if ( $this->from_poste2 != "" )
00421         {
00422             $filter_poste.=" $and upper(pb.po_name)>= upper('".Database::escape_string($this->from_poste2)."')";
00423             $and=" and ";
00424         }
00425         if ( $this->to_poste2 != "" )
00426         {
00427             $filter_poste.=" $and upper(pb.po_name)<= upper('".Database::escape_string($this->to_poste2)."')";
00428             $and=" and ";
00429         }
00430         if ( $filter_poste != "")
00431             $filter_poste=" where ".$filter_poste;
00432 
00433         $sql="
00434              select  a_po_id ,
00435              pa.po_name as a_po_name,
00436              pa.po_description as a_po_description,
00437              pb.po_description as b_po_description,
00438 
00439              b_po_id,
00440              pb.po_name as b_po_name,
00441              sum(a_oa_amount_c) as a_c,
00442              sum(a_oa_amount_d) as a_d
00443              from (select
00444                         a.j_id,
00445              a.po_id as a_po_id,
00446              b.po_id as b_po_id,
00447              case when a.oa_debit='t' then a.oa_amount else 0 end as a_oa_amount_d,
00448              case when a.oa_debit='f' then a.oa_amount else 0 end as a_oa_amount_c
00449              from
00450              operation_analytique as a join operation_analytique as b on (a.j_id=b.j_id and a.oa_row=b.oa_row)
00451                 join poste_analytique as poa on (a.po_id=poa.po_id)
00452                 join poste_analytique as pob on (b.po_id=pob.po_id)
00453              where poa.pa_id=".
00454              $this->pa_id."
00455              and pob.pa_id=".$this->pa_id2."  ".$this->set_sql_filter()."
00456              ) as m join poste_analytique as pa on ( a_po_id=pa.po_id)
00457              join poste_analytique as pb on (b_po_id=pb.po_id)
00458 
00459              $filter_poste
00460 
00461              group by a_po_id,b_po_id,pa.po_name,pa.po_description,pb.po_name,pb.po_description
00462              order by 2;
00463              ";
00464 
00465 
00466         $res=$this->db->exec_sql($sql);
00467 
00468         if ( Database::num_row($res) == 0 )
00469             return null;
00470         $a=array();
00471         $count=0;
00472         $array=Database::fetch_all($res);
00473         foreach ($array as $row)
00474         {
00475             $a[$count]['a_po_id']=$row['a_po_id'];
00476             $a[$count]['a_d']=$row['a_d'];
00477             $a[$count]['a_c']=$row['a_c'];
00478             $a[$count]['b_po_id']=$row['b_po_id'];
00479 
00480             $a[$count]['a_po_name']=$row['a_po_name'];
00481             $a[$count]['a_po_description']=$row['a_po_description'];
00482             $a[$count]['b_po_name']=$row['b_po_name'];
00483             $a[$count]['b_po_description']=$row['b_po_description'];
00484             $a[$count]['a_solde']=abs($row['a_d']-$row['a_c']);
00485             $a[$count]['a_debit']=($row['a_d']>$row['a_c'])?"debit":"credit";
00486 
00487             $count++;
00488         }
00489         return $a;
00490 
00491 
00492     }
00493 
00494 
00495     /*!
00496      * \brief add extra lines  with sum of each account
00497      * \param $p_array array returned by load()
00498      */
00499     function show_sum ($p_array)
00500     {
00501         $tot_deb=0;
00502         $tot_cred=0;
00503         $old="";
00504         $first=true;
00505         $array=array();
00506         foreach ( $p_array as $row)
00507         {
00508 
00509             if ( $old != $row['a_po_name'] && $first==false )
00510 
00511             {
00512                 $s=abs($tot_deb-$tot_cred);
00513                 $d=($tot_deb>$tot_cred)?'debit':'credit';
00514                 $array[]=array('poste'=>$old,'desc'=>$old_desc
00515                                                     ,'debit'=>$tot_deb,'credit'=>$tot_cred,
00516                                'solde'=>$s,'dc'=>$d);
00517 
00518                 $tot_deb=0;
00519                 $tot_cred=0;
00520 
00521                 $old=$row['a_po_name'];
00522                 $old_desc=$row['a_po_description'];
00523             }
00524 
00525             if ( $first )
00526             {
00527                 $first=false;
00528                 $old=$row['a_po_name'];
00529                 $old_desc=$row['a_po_description'];
00530             }
00531 
00532             $tot_deb+=$row['a_d'];
00533             $tot_cred+=$row['a_c'];
00534 
00535 
00536         }
00537         $s=abs($tot_deb-$tot_cred);
00538         $d=($tot_deb>$tot_cred)?'debit':'credit';
00539         $array[]=array('poste'=>$old,'desc'=>$old_desc
00540                        ,'debit'=>$tot_deb,'credit'=>$tot_cred,
00541 
00542                        'solde'=>$s,'dc'=>$d);
00543 
00544 
00545         return $array;
00546 
00547     }
00548     /*!
00549      * \brief for testing and debuggind the class
00550      *        it must never be called from production system, it is
00551      *        intended only for developpers
00552      * \param
00553      * \param
00554      * \param
00555      *
00556      *
00557      * \return none
00558      */
00559     static function test_me()
00560     {
00561         $a=new Database(dossier::id());
00562 
00563         $bal=new Anc_Balance_Double($a);
00564         $bal->get_request();
00565 
00566         echo '<form method="GET">';
00567 
00568         echo $bal->display_form();
00569         echo '</form>';
00570         if ( isset($_GET['result']))
00571         {
00572             echo $bal->show_button("","");
00573             echo "<h1>HTML</h1>";
00574             echo $bal->display_html();
00575             echo "<h1>CSV</h1>";
00576             echo $bal->display_csv();
00577 
00578         }
00579     }
00580 }