noalyss Version-9
anc_balance_simple.class.php
Go to the documentation of this file.
1<?php
2/*
3 * This file is part of NOALYSS.
4 *
5 * NOALYSS is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * NOALYSS is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with NOALYSS; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
19
20// Copyright Author Dany De Bontridder danydb@aevalys.eu
21
22/*!\file
23 \brief manage the simple balance for CA, inherit from balance_ca
24 */
25
26require_once NOALYSS_INCLUDE.'/lib/ac_common.php';
27require_once NOALYSS_INCLUDE.'/header_print.php';
28/*! \brief manage the simple balance for CA, inherit from balance_ca
29 *
30 */
31
33{
34
35 /*!
36 * \brief load the data from the database
37 *
38 * \return array or null
39 */
40 function load()
41 {
42 $filter=$this->set_sql_filter();
43 // sum debit
44
45 $sql="select m.po_id,sum(deb) as sum_deb,sum(cred) as sum_cred,";
46 $sql.=" po_name||' '||coalesce(po_description,'') as po_name";
47 $sql.=",ga_description ";
48 $sql.=" from ";
49 $sql.=" (select po_id,case when oa_debit='t' then oa_amount else 0 end as deb,";
50 $sql.="case when oa_debit='f' then oa_amount else 0 end as cred ";
51 $sql.=" from operation_analytique join poste_analytique using(po_id)";
52 $sql.=(empty($filter) == false)?" where ".$filter:"";
53 $sql.=" ) as m join poste_analytique using (po_id)";
54 $sql.=" left join groupe_analytique on ( poste_analytique.ga_id=groupe_analytique.ga_id)";
55 $sql.=" where poste_analytique.pa_id=".$this->pa_id;
56 $sql.=" group by po_id,po_name,po_description,ga_description";
57 $sql.=" order by po_name";
58
59 $res=$this->db->exec_sql($sql);
60
61 if ( Database::num_row($res) == 0 ) {
62 $this->has_data=0;
63 return null;
64 }
65 $a=array();
66 $count=0;
68 foreach ($array as $row)
69 {
70 $a[$count]['po_id']=$row['po_id'];
71 $a[$count]['sum_deb']=$row['sum_deb'];
72 $a[$count]['sum_cred']=$row['sum_cred'];
73 $a[$count]['po_name']=$row['po_name'];
74 $a[$count]['ga_description']=$row['ga_description'];
75 $a[$count]['solde']=abs($row['sum_deb']-$row['sum_cred']);
76 $a[$count]['debit']=($row['sum_deb']>$row['sum_cred'])?"debit":"credit";
77 $count++;
78 }
79 $this->has_data=$count;
80 return $a;
81
82
83 }
84 /*!
85 * \brief Set the filter (account_date)
86 *
87 * \return return the string to add to load
88 */
89
90
91 function set_sql_filter()
92 {
93 $sql="";
94 $and="";
95 if ( $this->from != "" )
96 {
97 $sql.=" oa_date >= to_date('".$this->from."','DD.MM.YYYY')";
98 $and=" and ";
99 }
100 if ( $this->to != "" )
101 {
102 $sql.=" $and oa_date <= to_date('".$this->to."','DD.MM.YYYY')";
103 $and=" and ";
104 }
105 if ( $this->from_poste != "" )
106 {
107 $sql.=" $and upper(po_name)>= upper('".$this->from_poste."')";
108 $and=" and ";
109 }
110 if ( $this->to_poste != "" )
111 {
112 $sql.=" $and upper(po_name)<= upper('".$this->to_poste."')";
113 $and=" and ";
114 }
115 return $sql;
116
117 }
118 /*!
119 * \brief compute the html display
120 *
121 *
122 * \return string
123 */
124 function display_html()
125 {
126 $r="<table class=\"result\">";
127 $r.="<tr>";
128 $r.="<th>Poste comptable Analytique</th>";
129 $r.="<th>Groupe</th>";
130 $r.="<th>D&eacute;bit</th>";
131 $r.="<th>Cr&eacute;dit</th>";
132 $r.="<th>Solde</th>";
133 $r.="<th>D/C</th>";
134 $r.="</tr>";
135
136 $array=$this->load();
137 $odd=0;
138 if ( is_array($array) == false )
139 {
140 return $array;
141
142 }
143 bcscale(2);
144 $deb_side=0;$cred_side=0;
145 foreach ( $array as $row)
146 {
147 $odd++;
148
149 $r.=($odd%2==0)?'<tr class="odd">':'<tr class="even">';
150 // the name and po_id
151 // $r.=sprintf("<td>%s</td>",$row['po_id']);
152 $r.=sprintf("<td align=\"left\">%s</td>",h($row['po_name']));
153 $r.=sprintf("<td align=\"left\">%s</td>",h($row['ga_description']));
154 $r.=td(nbm($row['sum_deb']),' class="num"');
155 $r.=td(nbm($row['sum_cred']),' class="num"');
156 $r.=td(nbm($row['solde']),' class="num"');
157 $deb=($row['sum_deb'] > $row['sum_cred'])?"D":"C";
158 $deb=($row['solde'] == 0 )?'':$deb;
159 $r.=sprintf("<td>%s</td>",$deb);
160 $r.="</tr>";
161 $deb_side=bcadd($deb_side,$row['sum_deb']);
162 $cred_side=bcadd($cred_side,$row['sum_cred']);
163
164 }
165 $r.='<tr class="highlight">';
166 $r.=td(_("Total"));
167 $r.=td(" ");
168 $r.=td(nbm($deb_side),'class="num"');
169 $r.=td(nbm($cred_side),'class="num"');
170 $solde_side=abs(bcsub($deb_side,$cred_side));
171 $r.=td(nbm($solde_side),'class="num"');
172 if ( $deb_side == $cred_side ) $r.=td("=");
173 else if ( $deb_side > $cred_side ) $r.=td("D");
174 else $r.=td("C");
175 $r.="</table>";
176 return $r;
177 }
178 /*!
179 * \brief Compute the form to display
180 * \param $p_hidden hidden tag to be included (gDossier,...)
181 *
182 *
183 * \return string containing the data
184 */
185 function display_form($p_string="")
186 {
187 $r=parent::display_form($p_string);
188
189 $r.= HtmlInput::submit('Affiche', _('Rechercher'));
190
191 return $r;
192 }
193
194 /*!
195 * \brief Display the result in pdf
196 *
197 * \return none
198 */
199 function display_pdf()
200 {
201 $array=$this->load();
202 $pdf=new PDFBalance_Simple($this->db);
203 $pdf->set_info($this->from_poste,$this->to_poste,$this->from,$this->to);
204 $pdf->AliasNbPages();
205 $pdf->AddPage();
206 $pdf->setTitle("Balance analytique",true);
207
208 $pdf->SetFont('DejaVu','',6);
209 for ($i=0;$i<count($array);$i++)
210 {
211 $row=$array[$i];
212 $pdf->write_cell(50,6,$row['po_name'],0,0,'L');
213 $pdf->write_cell(50,6,$row['ga_description'],0,0,'L');
214 $pdf->write_cell(20,6,sprintf('%s',nbm($row['sum_deb'])),0,0,'R');
215 $pdf->write_cell(20,6,sprintf('%s',nbm($row['sum_cred'])),0,0,'R');
216 $pdf->write_cell(20,6,sprintf('%s',nbm($row['solde'])),0,0,'R');
217 $pdf->write_cell(20,6,$row['debit'],0,0,'R');
218 $pdf->line_new();
219 }
220 $fDate=date('dmy-Hi');
221 $pdf->output('simple-balance-'.$fDate.'.pdf','D');
222
223 }
224 /*!
225 * \brief Compute the csv export
226 * \return string with the csv
227 */
228 function display_csv()
229 {
230 $array=$this->load();
231 if ( is_array($array) == false )
232 {
233 return $array;
234
235 }
236 $csv=new Noalyss_Csv("ca_bal_simple");
237 $csv->send_header();
238 $r="";
239 foreach ( $array as $row)
240 {
241 // the name and po_id
242 $solde=($row['sum_cred']>$row['sum_deb'])?'C':'D';
243 $solde=($row['sum_cred']==$row['sum_deb'])?'0':$solde;
244 $csv->add($row['po_name']);
245 $csv->add($row['ga_description']);
246 $csv->add($row['sum_deb'],"number");
247 $csv->add($row['sum_cred'],"number");
248 $csv->add($row['solde'],"number");
249 $csv->add($row['debit'],"text");
250
251 $csv->write();
252 }
253 return $r;
254
255 }
256 /*!
257 * \brief Show the button to export in PDF or CSV
258 * \param $url_csv url of the csv
259 * \param $url_pdf url of the pdf
260 * \param $p_string hidden data to include in the form
261 *
262 *
263 * \return string with the button
264 */
265 function show_button($p_string="")
266 {
267 $r="";
268 $r.= '<form method="GET" action="export.php" style="display:inline">';
269 $r.= $p_string;
270 $r.= dossier::hidden();
271 $r.= HtmlInput::hidden("to",$this->to);
272 $r.= HtmlInput::hidden("act","PDF:AncBalSimple");
273
274 $r.= HtmlInput::hidden("from",$this->from);
275 $r.= HtmlInput::hidden("pa_id",$this->pa_id);
276 $r.= HtmlInput::hidden("from_poste",$this->from_poste);
277 $r.= HtmlInput::hidden("to_poste",$this->to_poste);
278 $r.=HtmlInput::submit('bt_pdf',"Export en PDF");
279 $r.= '</form>';
280
281 $r.= '<form method="GET" action="export.php" style="display:inline">';
282 $r.= HtmlInput::hidden("act","CSV:AncBalSimple");
283 $r.= HtmlInput::hidden("to",$this->to);
284 $r.= HtmlInput::hidden("from",$this->from);
285 $r.= HtmlInput::hidden("pa_id",$this->pa_id);
286 $r.= HtmlInput::hidden("from_poste",$this->from_poste);
287 $r.= HtmlInput::hidden("to_poste",$this->to_poste);
288 $r.= $p_string;
289 $r.= dossier::hidden();
290 $r.=HtmlInput::submit('bt_csv',"Export en CSV");
291 $r.= '</form>';
292 return $r;
293 }
294
295 /*!
296 * \brief for testing and debuggind the class
297 * it must never be called from production system, it is
298 * intended only for developpers
299 * \param
300 * \param
301 * \param
302 *
303 *
304 * \return none
305 */
306 static function test_me ()
307 {
308 // call the page with ?gDossier=14
310
312 $bal->get_request();
313
314 echo '<form method="GET">';
315
316 echo $bal->display_form();
317 echo '</form>';
318 if ( isset($_GET['result']))
319 {
320 echo $bal->show_button("","");
321 echo "<h1>HTML</h1>";
322 echo $bal->display_html();
323 echo "<h1>CSV</h1>";
324 echo $bal->display_csv();
325 /* echo "<h1>pdf</h1>"; */
326 /* echo $bal->display_pdf(); */
327
328 }
329
330 }
331}
td($p_string='', $p_extra='')
surround the string with td
Definition: ac_common.php:83
nbm($p_number, $p_dec=2)
format the number with a sep.
Definition: ac_common.php:137
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
$anc pa_id
h( $row[ 'oa_description'])
$anc_grandlivre from_poste
$anc_grandlivre to
$anc_grandlivre to_poste
$anc_grandlivre from
$filter
$_GET['qcode']
manage the simple balance for CA, inherit from balance_ca
display_form($p_string="")
Compute the form to display.
display_pdf()
Display the result in pdf.
load()
load the data from the database
display_html()
compute the html display
set_sql_filter()
Set the filter (account_date)
static test_me()
for testing and debuggind the class it must never be called from production system,...
show_button($p_string="")
Show the button to export in PDF or CSV.
display_csv()
Compute the csv export.
this class is the mother class for the CA printing
static fetch_all($ret)
wrapper for the function pg_fetch_all
static num_row($ret)
wrapper for the function pg_num_rows
static connect()
static hidden($p_name, $p_value, $p_id="")
static submit($p_name, $p_value, $p_javascript="", $p_class="smallbutton")
Manage the CSV : manage files and write CSV record.
$count
$SecUser db
for($i=0;$i< $nb_jrn;$i++) $deb