noalyss Version-9
anc_table.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/*!
23 * \file
24 * \brief printing analytic and accountancy as a table
25 */
26
27/*!
28 * \class Anc_Table
29 * \brief printing analytic and accountancy as a table
30 */
31
33{
34 function __construct($p_cn)
35 {
36 parent::__construct($p_cn);
37 }
38
39 /**
40 * @brief display form to get the parameter
41 * - card_poste 1 by card, 2 by account
42 * - from_poste
43 * - to_poste
44 * - from from date
45 * - to until date
46 * - pa_id Analytic plan to use
47 */
48 function display_form($p_hidden = '')
49 {
50 $r = parent::display_form($p_hidden);
51 $icard = new ISelect('card_poste');
52 $icard->value = array(
53 array('value' => 1, 'label' => 'Par fiche'),
54 array('value' => 2, 'label' => 'Par poste comptable')
55 );
56 $icard->selected = $this->card_poste;
57 $r .= $icard->input();
58 $r .= HtmlInput::request_to_hidden(array('ac'));
59 return $r;
60 }
61
62 /**
63 * @brief create the temporary table TABLE_ANALYTIC to store values
64 * for an account.
65 */
66 private function create_temp_table_account()
67 {
68 $sWhere = "";
69 $sql_from = "";
70 $sql_to = "";
71 $sql_and = "";
72
73 if (trim($this->from) != "" && isDate($this->from) != NULL) {
74 $sql_from = "oa_date >= to_date('" . $this->from . "','DD.MM.YYYY')";
75 $sWhere = " where ";
76 }
77 if (trim($this->to) != "" && isDate($this->to) != NULL) {
78 $sql_to = "oa_date <= to_date('" . $this->to . "','DD.MM.YYYY')";
79 $sWhere = " where ";
80 }
81 if ($sql_to != "" && $sql_from != "") {
82 $sql_and = " and ";
83 }
84 $sResult = $sWhere . $sql_from . $sql_and . $sql_to;
85
86 $sql = "
87 create temporary table table_analytic as
88 SELECT po.po_id,
89 po.pa_id, po.po_name,
90 po.po_description,sum(
91 CASE
92 WHEN oa1.oa_debit = true THEN oa1.oa_amount * (-1)::numeric
93 ELSE oa1.oa_amount
94 END) AS sum_amount,
95 coalesce(jrnx.j_poste,fd1.ad_value) as card_account,
96 pcm_lib as name
97 FROM operation_analytique as oa1
98 JOIN poste_analytique po USING (po_id)
99 left join fiche_detail as fd1 on (oa1.f_id=fd1.f_id and fd1.ad_id=5)
100 left JOIN jrnx USING (j_id)
101 left join tmp_pcmn ON (jrnx.j_poste::text = tmp_pcmn.pcm_val::text)
102 {$sResult}
103 GROUP BY po.po_id, po.po_name, po.pa_id, coalesce(jrnx.j_poste,fd1.ad_value), tmp_pcmn.pcm_lib, po.po_description
104 HAVING sum(
105 CASE
106 WHEN oa1.oa_debit = true THEN oa1.oa_amount * (-1)::numeric
107 ELSE oa1.oa_amount
108 END) <> 0::numeric ";
109 $this->db->exec_sql($sql);
110 }
111
112 /**
113 * @brief create the temporary table TABLE_ANALYTIC to store values for a card
114 */
115 private function create_temp_table_card()
116 {
117 $sWhere = "";
118 $sql_from = "";
119 $sql_to = "";
120 $sql_and = "";
121
122 if (trim($this->from) != "" && isDate($this->from) != NULL) {
123 $sql_from = "oa_date >= to_date('" . $this->from . "','DD.MM.YYYY')";
124 $sWhere = " where ";
125 }
126 if (trim($this->to) != "" && isDate($this->to) != NULL) {
127 $sql_to = "oa_date <= to_date('" . $this->to . "','DD.MM.YYYY')";
128 $sWhere = " where ";
129 }
130 if ($sql_to != "" && $sql_from != "") {
131 $sql_and = " and ";
132 }
133 $sResult = $sWhere . $sql_from . $sql_and . $sql_to;
134 $sql = "
135 create temporary table table_analytic as
136 with m as (select oa_id,po_id,
137 coalesce(jrnx.f_id,operation_analytique.f_id) as f_id1,
138 case when jrnx.j_qcode is not null then
139 ( SELECT fiche_detail.ad_value
140 FROM fiche_detail
141 WHERE fiche_detail.ad_id = 1 AND fiche_detail.f_id = jrnx.f_id)
142 when jrnx.f_id is null and operation_analytique.f_id is not null then
143 ( SELECT fiche_detail.ad_value
144 FROM fiche_detail
145 WHERE fiche_detail.ad_id = 1 AND fiche_detail.f_id = operation_analytique.f_id)
146 end
147 AS name,
148 case when jrnx.j_qcode is not null then
149 jrnx.j_qcode
150 when jrnx.f_id is null then
151 (SELECT fiche_detail.ad_value
152 FROM fiche_detail
153 WHERE fiche_detail.ad_id = 23 AND fiche_detail.f_id = operation_analytique.f_id) end as j_qcode
154 FROM operation_analytique
155 left JOIN jrnx USING (j_id) )
156 SELECT po.po_id, po.pa_id, po.po_name, po.po_description, sum(
157 CASE
158 WHEN operation_analytique.oa_debit = true THEN operation_analytique.oa_amount * (-1)::numeric
159 ELSE operation_analytique.oa_amount
160 END) AS sum_amount,
161 m.f_id1 as f_id,
162 m.name,
163 m.j_qcode as card_account
164 FROM operation_analytique
165 JOIN poste_analytique po USING (po_id)
166 join m using (oa_id)
167 {$sResult}
168 GROUP BY po.po_id, po.po_name, po.pa_id, m.f_id1, m.j_qcode,m.name, po.po_description
169 HAVING sum(
170 CASE
171 WHEN operation_analytique.oa_debit = true THEN operation_analytique.oa_amount * (-1)::numeric
172 ELSE operation_analytique.oa_amount
173 END) <> 0::numeric";
174 $this->db->exec_sql($sql);
175 }
176
177
178 /**
179 * @brief load the data
180 * does not return anything but give a value to this->aheader and this->arow
181 */
182 function load_poste()
183 {
184 $sql_from_poste = ($this->from_poste != '') ? " and po_name >= upper('" . Database::escape_string($this->from_poste) . "')" : '';
185 $sql_to_poste = ($this->to_poste != '') ? " and po_name <= upper('" . Database::escape_string($this->to_poste) . "')" : '';
186
187
189
190 $header = "select distinct po_id,po_name from table_analytic
191 where
192 pa_id=$1 " . $sql_from_poste . $sql_to_poste . " order by po_name";
193 $this->aheader = $this->db->get_array($header, array($this->pa_id));
194
195 $this->arow = $this->db->get_array("select distinct card_account,name
196 from table_analytic
197 where
198 pa_id=$1 " . $sql_from_poste . $sql_to_poste . " order by card_account", array($this->pa_id));
199
200 $this->sql = 'select sum_amount from table_analytic where card_account=$1 and po_id=$2 and pa_id=' . $this->pa_id . ' ' . $sql_from_poste . $sql_to_poste;
201 }
202
203 /**
204 * @brief load the data
205 * does not return anything but give a value to this->aheader and this->arow
206 */
207 function load_card()
208 {
209 $sql_from_poste = ($this->from_poste != '') ? " and po_name >= upper('" . Database::escape_string($this->from_poste) . "')" : '';
210 $sql_to_poste = ($this->to_poste != '') ? " and po_name <= upper('" . Database::escape_string($this->to_poste) . "')" : '';
211 // $this->db->exec_sql('create temporary table table_analytic as select * from comptaproc.table_analytic_card(\''.$this->from.'\',\''.$this->to.'\')');
212 $this->create_temp_table_card();
213 $header = "select distinct po_id,po_name from table_analytic
214 where
215 pa_id=$1 " . $sql_from_poste . $sql_to_poste . " order by po_name";
216 $this->aheader = $this->db->get_array($header, array($this->pa_id));
217
218 $this->arow = $this->db->get_array("select distinct f_id,card_account,name from table_analytic
219 where
220 pa_id=$1 " . $sql_from_poste . $sql_to_poste . " order by name", array($this->pa_id));
221 $this->sql = 'select sum_amount from table_analytic where f_id=$1 and po_id=$2 and pa_id=' . $this->pa_id . ' ' . $sql_from_poste . $sql_to_poste;
222 }
223
224 /**
225 * @brief display the button export CSV
226 * @param $p_hidden is a string containing hidden items
227 * @return html string
228 */
229 function show_button($p_hidden)
230 {
231 $r = "";
232 $r .= '<form method="GET" action="export.php" style="display:inline">';
233 $r .= HtmlInput::hidden("act", "CSV:AncTable");
234 $r .= HtmlInput::hidden("to", $this->to);
235 $r .= HtmlInput::hidden("from", $this->from);
236 $r .= HtmlInput::hidden("pa_id", $this->pa_id);
237 $r .= HtmlInput::hidden("from_poste", $this->from_poste);
238 $r .= HtmlInput::hidden("to_poste", $this->to_poste);
239 $r .= HtmlInput::hidden("card_poste", $this->card_poste);
240 $r .= $p_hidden;
241 $r .= dossier::hidden();
242 $r .= HtmlInput::submit('bt_csv', _("Export en CSV"));
243 $r .= '</form>';
244 return $r;
245 }
246
247 function display_html()
248 {
249 bcscale(2);
250 if ($this->check() != 0) {
251 alert(_("Date invalide"));
252 return;
253 }
254
255 if ($this->card_poste == '1') {
256 $this->load_card();
257
258 echo '<table class="result">';
259 echo '<tr>';
260 echo th('Fiche');
261 foreach ($this->aheader as $h) {
262 echo '<th style="text-align:right">' . h($h['po_name']) . '</th>';
263 }
264 echo th('Total', ' style="text-align:right"');
265 echo '</tr>';
266 /*
267 * Show all the result
268 */
269 $tot_global = 0;
270 for ($i = 0; $i < count($this->arow); $i++) {
271 $tr = ($i % 2 == 0) ? '<tr class="even">' : '<tr class="odd">';
272 echo $tr;
273 echo td(HtmlInput::history_card($this->arow[$i]['f_id'], $this->arow[$i]['card_account'] . ' ' . $this->arow[$i]['name']));
274 $tot_row = 0;
275 for ($x = 0; $x < count($this->aheader); $x++) {
276 $amount = $this->db->get_value($this->sql, array($this->arow[$i]['f_id'], $this->aheader[$x]['po_id']));
277 if ($amount == null) $amount = 0;
278 if (isset($tot_col[$x])) {
279 $tot_col[$x] = bcadd($tot_col[$x], $amount);
280 } else {
281 $tot_col[$x] = $amount;
282 }
283 $side = ($amount < 0) ? 'D' : 'C';
284 $side = ($amount == 0) ? "=" : $side;
285 echo td(nbm(abs($amount)) . " " . $side, ' class="num" ');
286 $tot_row = bcadd($tot_row, $amount);
287 }
288 $side = ($tot_row < 0) ? 'D' : 'C';
289 $side = ($tot_row == 0) ? "=" : $side;
290 echo td(nbm(abs($tot_row)) . " " . $side, ' class="num"');
291 $tot_global = bcadd($tot_global, $tot_row);
292 echo '</tr>';
293
294
295 }
296 echo '<tr class="highlight">';
297 echo td('Totaux');
298 for ($i = 0; $i < count($this->aheader); $i++) {
299 $side = ($tot_col[$i] < 0) ? "D" : "C";
300 $side = ($tot_global == 0) ? "=" : $side;
301 echo td(nbm(abs($tot_col[$i])) . " " . $side, ' class="num"');
302 }
303 $side = ($tot_global > 0) ? "C" : "D";
304 $side = ($tot_global == 0) ? "=" : $side;
305 echo td(nbm($tot_global) . " " . $side, ' class="num " ');
306 echo '</tr>';
307 echo '</table>';
308 }
309 if ($this->card_poste == '2') {
310 $this->load_poste();
311
312 echo '<table class="result">';
313 echo '<tr>';
314 echo th('poste comptable ');
315 foreach ($this->aheader as $h) {
316 echo '<th style="text-align:right">' . h($h['po_name']) . '</th>';
317 }
318 echo th('Total', ' style="text-align:right"');
319 echo '</tr>';
320 /*
321 * Show all the result
322 */
323 $tot_global = 0;
324 for ($i = 0; $i < count($this->arow); $i++) {
325 $tr = ($i % 2 == 0) ? '<tr class="even">' : '<tr class="odd">';
326 echo $tr;
327 echo td(HtmlInput::history_account($this->arow[$i]['card_account'], $this->arow[$i]['card_account'] . ' ' . $this->arow[$i]['name']));
328 $tot_row = 0;
329 for ($x = 0; $x < count($this->aheader); $x++) {
330 $amount = $this->db->get_value($this->sql, array($this->arow[$i]['card_account'], $this->aheader[$x]['po_id']));
331 if ($amount == null) $amount = 0;
332 if (isset($tot_col[$x])) {
333 $tot_col[$x] = bcadd($tot_col[$x], $amount);
334 } else {
335 $tot_col[$x] = $amount;
336 }
337 $side = ($amount < 0) ? 'D' : 'C';
338 $side = ($amount == 0) ? "=" : $side;
339 echo td(nbm(abs($amount)) . " " . $side, ' class="num" ');
340 $tot_row = bcadd($tot_row, $amount);
341 }
342 $side = ($tot_row < 0) ? 'D' : 'C';
343 $side = ($tot_row == 0) ? "=" : $side;
344 echo td(nbm(abs($tot_row)) . " " . $side, ' class="num"');
345 $tot_global = bcadd($tot_global, $tot_row);
346 echo '</tr>';
347
348
349 }
350 echo '<tr class="highlight">';
351
352
353 echo td('Totaux');
354 for ($i = 0; $i < count($this->aheader); $i++) {
355 $side = ($tot_col[$i] < 0) ? "D" : "C";
356 echo td(nbm(abs($tot_col[$i])) . " " . $side, ' class="num"');
357 }
358 $side = ($tot_global > 0) ? "C" : "D";
359 $side = ($tot_global == 0) ? "=" : $side;
360
361 echo td(nbm($tot_global) . " " . $side, ' class="num input_text" ');
362 echo '</tr>';
363 echo '</table>';
364
365 }
366
367 }
368
369 function export_csv()
370 {
371 bcscale(2);
372 if ($this->check() != 0) {
373 throw new Exception ("DATE INVALIDE");
374 }
375 $csv = new Noalyss_Csv("Anc-table");
376 $csv->send_header();
377 //---------------
378 // By Card
379 if ($this->card_poste == '1') {
380 $this->load_card();
381
382 $csv->add("Fiche");
383 foreach ($this->aheader as $h) {
384 $csv->add($h['po_name']);
385 }
386 $csv->add("Total");
387 $csv->write();
388
389 /*
390 * Show all the result
391 */
392
393 for ($i = 0; $i < count($this->arow); $i++) {
394
395 $csv->add($this->arow[$i]['card_account'] . ' ' . $this->arow[$i]['name']);
396 $tot_row = 0;
397 for ($x = 0; $x < count($this->aheader); $x++) {
398 $amount = $this->db->get_value($this->sql, array($this->arow[$i]['f_id'], $this->aheader[$x]['po_id']));
399 if ($amount == null) $amount = 0;
400 if (isset($tot_col[$x])) {
401 $tot_col[$x] = bcadd($tot_col[$x], $amount);
402 } else {
403 $tot_col[$x] = $amount;
404 }
405 $csv->add($amount, "number");
406 $tot_row = bcadd($tot_row, $amount);
407 }
408 $csv->add($tot_row, "number");
409 $csv->write();
410
411 }
412 /* ----
413 * Sum for each column
414 */
415 $csv->add("");
416 $sum_col = 0;
417 for ($x = 0; $x < count($this->aheader); $x++) {
418 $csv->add($tot_col[$x], "number");
419 $sum_col = bcadd($sum_col, $tot_col[$x]);
420 }
421 $csv->add($sum_col, "number");
422 $csv->write();
423 }
424 //---------------
425 // By Accounting
426 if ($this->card_poste == '2') {
427 $this->load_poste();
428
429 $csv->add(_("Poste"));
430 $csv->add(_("Description"));
431 foreach ($this->aheader as $h) {
432 $csv->add($h['po_name']);
433 }
434 $csv->add("Total");
435 $csv->write();
436 /*
437 * Show all the result
438 */
439
440 for ($i = 0; $i < count($this->arow); $i++) {
441
442 $csv->add($this->arow[$i]['card_account']);
443 $csv->add($this->arow[$i]['name']);
444 $tot_row = 0;
445 for ($x = 0; $x < count($this->aheader); $x++) {
446 $amount = $this->db->get_value($this->sql, array($this->arow[$i]['card_account'], $this->aheader[$x]['po_id']));
447 if ($amount == null) $amount = 0;
448 if (isset($tot_col[$x])) {
449 $tot_col[$x] = bcadd($tot_col[$x], $amount);
450 } else {
451 $tot_col[$x] = $amount;
452 }
453 $csv->add($amount, "number");
454 $tot_row = bcadd($tot_row, $amount);
455 }
456 $csv->add($tot_row, "number");
457 $csv->write();
458
459
460 }
461 /* ----
462 * Sum for each column
463 */
464 $csv->add("");
465 $csv->add("");
466 $sum_col = 0;
467 for ($x = 0; $x < count($this->aheader); $x++) {
468 $csv->add($tot_col[$x], "number");
469 $sum_col = bcadd($sum_col, $tot_col[$x]);
470 }
471 $csv->add($sum_col, "number");
472 $csv->write();
473 }
474
475 }
476
477}
th($p_string, $p_extra='', $raw='')
Definition: ac_common.php:58
isDate($p_date)
Definition: ac_common.php:236
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
alert($p_msg, $buffer=false)
alert in javascript
Definition: ac_common.php:738
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
printing analytic and accountancy as a table
load_card()
load the data does not return anything but give a value to this->aheader and this->arow
show_button($p_hidden)
display the button export CSV
create_temp_table_account()
create the temporary table TABLE_ANALYTIC to store values for an account.
load_poste()
load the data does not return anything but give a value to this->aheader and this->arow
display_form($p_hidden='')
display form to get the parameter
__construct($p_cn)
create_temp_table_card()
create the temporary table TABLE_ANALYTIC to store values for a card
static escape_string($p_string)
wrapper for the function pg_escape_string
static history_account($p_account, $p_mesg, $p_style="", $p_exercice="")
display a div with the history of the account
static request_to_hidden(array $array)
transform $_REQUEST data to hidden
static hidden($p_name, $p_value, $p_id="")
static history_card($f_id, $p_mesg, $p_style="", $p_exercice="")
display a div with the history of the card
static submit($p_name, $p_value, $p_javascript="", $p_class="smallbutton")
Html Input , create a tag <SELECT> ... </SELECT> if readonly == true then display the label correspon...
Manage the CSV : manage files and write CSV record.
if(count($a_accounting)==0) $header
$SecUser db
$side