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