noalyss Version-9
anticipation.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 Manage the anticipation, prediction of sales, expense, bank...
25 *
26 */
27/**
28 * @class
29 * @brief Manage the anticipation of expense, sales,...
30 * @see Forecast Forecast_Cat Forecast_Item
31 *
32 */
33require_once NOALYSS_INCLUDE . '/database/forecast_sql.class.php';
34
36{
37 /* example private $variable=array("val1"=>1,"val2"=>"Seconde valeur","val3"=>0); */
38 private $cn;
39 var $cat; /*!< array of object categorie (forecast_category)*/
40 var $item; /*< array of object item (forecast_item) */
41 private $forecast_id; //!< forecast.f_id
42
43 /**
44 * @brief constructor
45 * @param $p_init Database object
46 */
47 function __construct($p_init, $p_id = 0)
48 {
49 $this->cn = $p_init;
50 $this->forecast_id = $p_id;
51 }
52
53 /**
54 * @return mixed
55 */
56 public function getCat()
57 {
58 return $this->cat;
59 }
60
61 /**
62 * @param mixed $cat
63 */
64 public function setCat($cat)
65 {
66 $this->cat = $cat;
67 }
68
69 /**
70 * @return mixed
71 */
72 public function getItem()
73 {
74 return $this->item;
75 }
76
77 /**
78 * @param mixed $item
79 */
80 public function setItem($item)
81 {
82 $this->item = $item;
83 }
84
85 /**
86 * @return int|mixed
87 */
88 public function getForecastId()
89 {
90 return $this->forecast_id;
91 }
92
93 /**
94 * @param int|mixed $forecast_id
95 */
96 public function setForecastId($forecast_id)
97 {
98 $this->forecast_id = $forecast_id;
99 }
100
101 public function input_title()
102 {
103 require_once NOALYSS_TEMPLATE . "/anticipation-input_title.php";
104 }
105
106 public function input_category()
107 {
108 $visible=uniqid();
109 echo '<h2>';
110 echo _("Catégorie");
111 echo '<span style="margin-left:5em;font-style:normal;font-size:150%">';
112 echo Icon_Action::less($visible, "");
113 echo '</span>';
114 echo '</h2>';
115
116 echo '<div id="anticipation_input_category" >';
118 $forecast_category->set_forecast_id($this->forecast_id);
119 $forecast_category->display_table(" where f_id = $1"
120 . " order by fc_order",
121 [$this->forecast_id]);
122 echo $forecast_category->create_js_script();
123 echo '</div>';
124 echo <<<EOF
125 <script>
126 var visible=true;
127 var icon=document.getElementById('{$visible}');
128icon.onclick=function () {
129
130 if ( visible ) { visible = false; $("anticipation_input_category").hide();icon.innerHTML="&#xe824;"}
131 else { visible = true;$("anticipation_input_category").show();icon.innerHTML="&#xe827;"}
132 }
133</script>
134EOF;
135
136 }
137
138 /**
139 * @brief display the detail for the forecast , for modifying item, category or name
140 *
141 */
142 public function input_form()
143 {
144 $this->input_title();
145
146 $this->input_category();
147 $http=new HttpInput();
148 $ac=$http->request("ac");
149 echo h2(_("Eléments"));
151 $forecast_item->set_forecast_id($this->forecast_id);
152 $forecast_item->display_table();
153 echo '<hr>';
154 $href="do.php?".http_build_query(array('ac'=>$ac, 'sa'=>'vw','gDossier'=>Dossier::id(),'f_id'=>$this->forecast_id));
155
156
157 $form=dossier::hidden();
158 $form.=HtmlInput::hidden('action', '');
159 $form.=HtmlInput::hidden('f_id', $this->getForecastId());
161
162 echo '<form method="get" id="form_del" onsubmit="return confirm_box(this,content[\'47\'])" style="display:inline-block">';
163 echo $form;
164 echo HtmlInput::hidden("action","del");
165 echo HtmlInput::submit('del_bt', _('Effacer'));
166 echo '</form>';
167
168 echo '<form method="get" id="form_clone" onsubmit="return confirm_box(this,content[\'47\']) " style="display:inline-block" >';
169 echo $form;
170 echo HtmlInput::hidden("action","clone");
171 echo HtmlInput::submit("clone_bt",_("Clone"));
172 echo '</form>';
173
174 echo HtmlInput::button_anchor(_("Retour"), $href);
175
176 }
177
178 /**
179 * @brief Display the result of the forecast
180 * @param $p_periode
181 * @return HTML String with the code
182 */
183 public function display()
184 {
185 bcscale(4);
186 $forecast = new Forecast_SQL($this->cn, $this->forecast_id);
187 $forecast->load();
188 $str_name = h($forecast->getp('f_name'));
189
190 $start = $forecast->getp('f_start_date');
191 $end = $forecast->getp('f_end_date');
192
193 if ($start == '') throw new Exception (_('Période de début non valable'));
194 if ($end == '') throw new Exception (_('Période de fin non valable'));
195
196 $per = new Periode($this->cn, $start);
197 $str_start = format_date($per->first_day());
198
199 $per = new Periode($this->cn, $end);
200 $str_end = format_date($per->last_day());
201
202
203 $r = "";
204 $aCat = $this->cn->get_array('select fc_id,fc_desc from forecast_category where f_id=$1 order by fc_order',
205 array($this->forecast_id));
206 $aItem = array();
207 $aReal = array();
208 $poste = new Acc_Account_Ledger($this->cn, 0);
209
210 $aPeriode = $this->cn->get_array("select p_id,to_char(p_start,'MM.YYYY') as myear from parm_periode
211 where p_start >= (select p_start from parm_periode where p_id=$1)
212 and p_end <= (select p_end from parm_periode where p_id=$2)
213 order by p_start",[$start,$end]);
214 $error = array();
215 for ($j = 0; $j < count($aCat); $j++) {
216
217 // Item of the category,montly estimation
218 $aItem[$j] = $this->cn->get_array('select fi_account,fi_text,fi_amount,
219 fi_amount_initial
220 from forecast_item where fc_id=$1 and fi_pid=0 order by fi_order ',
221 array($aCat[$j]['fc_id']));
222
223 // Item of the category, estimation for a specific month
224 $aPerMonth[$j] = $this->cn->get_array('select fi_pid,fi_account,fi_text,fi_amount
225 from forecast_item where fc_id=$1 and fi_pid !=0 order by fi_order ',
226 array($aCat[$j]['fc_id']));
227
228 /* compute the real amount for periode */
229 for ($k = 0; $k < count($aItem[$j]); $k++) {
230 /* for each periode */
231 for ($l = 0; $l < count($aPeriode); $l++) {
232 if ($aItem[$j][$k]['fi_account'] != '') {
233
234 $poste->id = $aItem[$j][$k]['fi_account'];
235 $aresult = Impress::parse_formula($this->cn, "OK", $poste->id, $aPeriode[$l]['p_id'], $aPeriode[$l]['p_id']);
236 $tmp_label = $aresult['desc'];
237 $amount['solde'] = $aresult['montant'];
238
239 if ($tmp_label != 'OK') {
240 $error[] = "<li> " . $aItem[$j][$k]['fi_text'] . $poste->id . '</li>';
241 }
242 }
243 $aReal[$j][$k][$l] = $amount['solde'];
244 }
245 }
246
247 }
248 if ( DEBUGNOALYSS < 2 ) { ob_start(); }
249 require_once NOALYSS_TEMPLATE . '/anticipation-display.php';
250 $r .= ob_get_contents();
251 if ( DEBUGNOALYSS < 2 ) { ob_end_clean(); }
252 return $r;
253 }
254
255 public static function div()
256 {
257 $r = '<div id="div_anti" style="display:none">';
258 $r .= '</div>';
259 return $r;
260 }
261
262 /**
263 * @brief Clone completely Forecast and returns the new forecast_id
264 * @return type
265 */
266 public function object_clone()
267 {
268 $this->cn->start();
269 /* save into the table forecast */
270 $sql = "insert into forecast(f_name,f_start_date,f_end_date) select 'clone '||f_name,f_start_date,f_end_date
271 from forecast
272 where f_id=$1 returning f_id";
273 $new = $this->cn->get_value($sql, array($this->forecast_id));
274
275 /* save into forecast_cat */
276 $sql = "insert into forecast_category(fc_desc,f_id,fc_order)
277 select fc_desc,$1,fc_order
278 from forecast_category
279 where f_id=$2 returning fc_id";
280 $array = $this->cn->get_array($sql, array($new, $this->forecast_id));
281
282 $old = $this->cn->get_array("select fc_id from forecast_category where f_id=$1", array($this->forecast_id));
283 /* save into forecast_item */
284 for ($i = 0; $i < count($array); $i++) {
285 $this->cn->exec_sql("insert into forecast_item (fi_text,fi_account,fi_order,fc_id,
286 fi_amount,fi_pid) " .
287 " select fi_text,fi_account,fi_order,$1,fi_amount,fi_pid " .
288 " from forecast_item where fc_id=$2", array($array[$i]['fc_id'], $old[$i]['fc_id']));
289 }
290 $this->cn->commit();
291 return $new;
292 }
293
294}
295
296?>
format_date($p_date, $p_from_format='YYYY-MM-DD', $p_to_format='DD.MM.YYYY')
format the date, when taken from the database the format is MM-DD-YYYY
Definition: ac_common.php:852
h2($p_string, $p_class="", $raw="")
Definition: ac_common.php:68
$href
Definition: adm.inc.php:31
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
h( $row[ 'oa_description'])
catch(Exception $e) $forecast_category
catch(Exception $e) $forecast_item
if(isset( $_REQUEST[ 'show'])) if(isset($_REQUEST['del'])) $ac
$input_from cn
Definition: balance.inc.php:66
Manage the account from the table jrn, jrnx or tmp_pcmn.
display()
Display the result of the forecast.
$forecast_id
forecast.f_id
__construct($p_init, $p_id=0)
constructor
object_clone()
Clone completely Forecast and returns the new forecast_id.
input_form()
display the detail for the forecast , for modifying item, category or name
setForecastId($forecast_id)
static id()
return the 'gDossier' value after a check
ORM abstract of the table public.forecast.
static hidden($p_name, $p_value, $p_id="")
static button_anchor($p_label, $p_value, $p_name="", $p_javascript="", $p_class="smallbutton")
create a button with a ref
static submit($p_name, $p_value, $p_javascript="", $p_class="smallbutton")
manage the http input (get , post, request) and extract from an array
static less($p_id, $p_javascript)
static parse_formula($p_cn, $p_label, $p_formula, $p_start, $p_end, $p_eval=true, $p_type_date=0, $p_sql="")
For the periode tables parm_periode and jrn_periode.
$str_end
Definition: fiche.inc.php:57
$str_start
Definition: fiche.inc.php:56