noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_anticipation.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 /**
00023  * @file
00024  * @brief  Manage the anticipation, prediction of sales, expense, bank...
00025  *
00026  */
00027 /**
00028  *@brief Manage the anticipation of expense, sales,...
00029  *@see Forecast Forecast_Cat Forecast_Item
00030  *
00031  */
00032 require_once 'class_forecast.php';
00033 require_once 'class_forecast_cat.php';
00034 require_once 'class_forecast_item.php';
00035 require_once 'class_fiche.php';
00036 require_once 'class_acc_account_ledger.php';
00037 require_once 'class_periode.php';
00038 require_once 'class_impress.php';
00039 
00040 class Anticipation
00041 {
00042     /* example private $variable=array("val1"=>1,"val2"=>"Seconde valeur","val3"=>0); */
00043     private static $variable=array ("id"=>"f_id","name"=>"f_name");
00044     private $cn;
00045     var $cat; /*!< array of object categorie (forecast_cat)*/
00046     var $item; /*< array of object item (forecast_item) */
00047     /**
00048      * @brief constructor
00049      * @param $p_init Database object
00050      */
00051     function __construct ($p_init,$p_id=0)
00052     {
00053         $this->cn=$p_init;
00054         $this->f_id=$p_id;
00055     }
00056     public function get_parameter($p_string)
00057     {
00058         if ( array_key_exists($p_string,self::$variable) )
00059         {
00060             $idx=self::$variable[$p_string];
00061             return $this->$idx;
00062         }
00063         else
00064             exit (__FILE__.":".__LINE__."[$p_string]".'Erreur attribut inexistant');
00065     }
00066     public function set_parameter($p_string,$p_value)
00067     {
00068         if ( array_key_exists($p_string,self::$variable) )
00069         {
00070             $idx=self::$variable[$p_string];
00071             $this->$idx=$p_value;
00072         }
00073         else
00074             exit (__FILE__.":".__LINE__."[$p_string]".'Erreur attribut inexistant');
00075 
00076 
00077     }
00078     public function get_info()
00079     {
00080         return var_export(self::$variable,true);
00081     }
00082     public function verify()
00083     {
00084         // Verify that the elt we want to add is correct
00085         // the f_name must be unique (case insensitive)
00086         return 0;
00087     }
00088     public function save()
00089     {
00090         /* please adapt */
00091         if (  $this->get_parameter("id") == 0 )
00092             $this->insert();
00093         else
00094             $this->update();
00095     }
00096 
00097     public function insert()
00098     {
00099         if ( $this->verify() != 0 ) return;
00100     }
00101 
00102     public function update()
00103 {}
00104 
00105     public function load()
00106     {}
00107     /**
00108      *@brief Display the result of the forecast
00109      *@param $p_periode
00110      *@return HTML String with the code
00111      */
00112     public function display()
00113     {
00114                 bcscale(4);
00115         $forecast=new Forecast($this->cn,$this->f_id);
00116         $forecast->load();
00117         $str_name=h($forecast->get_parameter('name'));
00118 
00119         $start=$forecast->get_parameter('start_date');
00120         $end=$forecast->get_parameter('end_date');
00121 
00122         if ( $start=='') throw new Exception (_('Période de début non valable'));
00123         if ( $end=='') throw new Exception (_('Période de fin non valable'));
00124 
00125         $per=new Periode($this->cn,$start);
00126         $str_start=format_date($per->first_day());
00127 
00128         $per=new Periode($this->cn,$end);
00129         $str_end=format_date($per->last_day());
00130 
00131 
00132         $r="";
00133         $aCat=$this->cn->get_array('select fc_id,fc_desc from forecast_cat where f_id=$1 order by fc_order',array($this->f_id));
00134         $aItem=array();
00135         $aReal=array();
00136         $poste=new Acc_Account_Ledger($this->cn,0);
00137         $fiche=new Fiche($this->cn);
00138         $aPeriode=$this->cn->get_array("select p_id,to_char(p_start,'MM.YYYY') as myear from parm_periode
00139                                          where p_start >= (select p_start from parm_periode where p_id=$start)
00140                                          and p_end <= (select p_end from parm_periode where p_id=$end)
00141                                          order by p_start;");
00142         $error=array();
00143         for($j=0;$j<count($aCat);$j++)
00144         {
00145             $aItem[$j]=$this->cn->get_array('select fi_card,fi_account,fi_text,fi_amount,fi_debit from forecast_item where fc_id=$1  and fi_pid=0 order by fi_order ',array($aCat[$j]['fc_id']));
00146             $aPerMonth[$j]=$this->cn->get_array('select fi_pid,fi_card,fi_account,fi_text,fi_amount,fi_debit from forecast_item where fc_id=$1 and fi_pid !=0 order by fi_order ',array($aCat[$j]['fc_id']));
00147 
00148             /* compute the real amount for periode */
00149             for($k=0;$k<count($aItem[$j]);$k++)
00150             {
00151                 /* for each periode */
00152                 for ($l=0;$l<count($aPeriode);$l++)
00153                 {
00154                     if ($aItem[$j][$k]['fi_account']=='')
00155                     {
00156                         $fiche->id=$aItem[$j][$k]['fi_card'];
00157                         $amount=$fiche->get_solde_detail("j_tech_per = ".$aPeriode[$l]['p_id']);
00158                         if ($aItem[$j][$k]['fi_debit']=='C' && $amount['debit']>$amount['credit'])  $amount['solde']=$amount["solde"]*(-1);
00159                         if ($aItem[$j][$k]['fi_debit']=='D' && $amount['debit']<$amount['credit'])  $amount['solde']=$amount["solde"]*(-1);
00160 
00161                     }
00162                     else
00163                     {
00164                         $poste->id=$aItem[$j][$k]['fi_account'];
00165                         $aresult=Impress::parse_formula($this->cn,"OK",$poste->id,$aPeriode[$l]['p_id'],$aPeriode[$l]['p_id']);
00166                         $tmp_label=$aresult['desc'];
00167                         $amount['solde']=$aresult['montant'];
00168 
00169                         if ( $tmp_label != 'OK') $error[]="<li> ".$aItem[$j][$k]['fi_text'].$poste->id.'</li>';
00170                     }
00171                     $aReal[$j][$k][$l]=$amount['solde'];
00172                 }
00173             }
00174 
00175         }
00176         ob_start();
00177         require_once('template/forecast_result.php');
00178         $r.=ob_get_contents();
00179         ob_end_clean();
00180         return $r;
00181     }
00182     public static function div()
00183     {
00184         $r='<div id="div_anti" style="display:none">';
00185         $r.= '</div>';
00186         return $r;
00187     }
00188     public function delete()
00189     {}
00190     /**
00191      *@brief Display a form for modifying the name or/and the category of an existing
00192      * anticipation
00193      *@return html string with the form
00194      */
00195     private function form_cat_mod()
00196     {
00197         global $g_user;
00198         $a=new Forecast($this->cn,$this->f_id);
00199         $a->load();
00200         $name=new IText('an_name');
00201         $name->value=$a->get_parameter("name");
00202         $str_name=$name->input();
00203         $str_action=_('Modification');
00204 
00205         $start_date=new IPeriod('start_date');
00206         $start_date->type=ALL;
00207         $start_date->cn=$this->cn;
00208         $start_date->show_end_date=false;
00209         $start_date->show_start_date=true;
00210         $start_date->user=$g_user;
00211         $start_date->filter_year=false;
00212 
00213         $end_date=new IPeriod('end_date');
00214         $end_date->type=ALL;
00215         $end_date->cn=$this->cn;
00216         $end_date->show_end_date=true;
00217         $end_date->show_start_date=false;
00218         $end_date->user=$g_user;
00219         $end_date->filter_year=false;
00220 
00221         $start_date->value=$a->f_start_date;
00222         $end_date->value=$a->f_end_date;
00223 
00224         $str_start_date=$start_date->input();
00225         $str_end_date=$end_date->input();
00226 
00227 
00228         $r=HtmlInput::hidden('f_id',$this->f_id);
00229         $array=Forecast_Cat::load_all($this->cn,$this->f_id);
00230 
00231         for ($i=0;$i<MAX_CAT;$i++)
00232         {
00233             /* category name */
00234             $name_name=(isset($array[$i]['fc_id']))?'fr_cat'.$array[$i]['fc_id']:'fr_cat_new'.$i;
00235             $name=new IText($name_name);
00236             $name->value=(isset ($array[$i]['fc_desc']))?$array[$i]['fc_desc']:'';
00237             $aCat[$i]['name']=$name->input();
00238 
00239 
00240             /* category order */
00241             $order_name=(isset($array[$i]['fc_id']))?'fc_order'.$array[$i]['fc_id']:'fc_order_new'.$i;
00242             $order=new IText($order_name);
00243             $order->value=(isset($array[$i]['fc_order']))?$array[$i]['fc_order']:$i+1;
00244             $aCat[$i]['order']=$order->input();
00245         }
00246 
00247         ob_start();
00248         require_once('template/forecast_cat.php');
00249         $r.=ob_get_contents();
00250         ob_end_clean();
00251         return $r;
00252     }
00253     /**
00254      *@brief Display a form for adding an new anticipation
00255      *@return html string with the form
00256      */
00257     private function form_cat_new()
00258     {
00259      global $g_user;
00260       $r="";
00261         $str_action=_('Nouveau');
00262 
00263         $name=new IText('an_name');
00264         $str_name=$name->input();
00265 
00266         $start_date=new IPeriod('start_date');
00267         $start_date->type=ALL;
00268         $start_date->cn=$this->cn;
00269         $start_date->show_end_date=false;
00270         $start_date->show_start_date=true;
00271         $start_date->user=$g_user;
00272         $start_date->filter_year=false;
00273 
00274         $end_date=new IPeriod('end_date');
00275         $end_date->type=ALL;
00276         $end_date->cn=$this->cn;
00277         $end_date->show_end_date=true;
00278         $end_date->show_start_date=false;
00279         $end_date->user=$g_user;
00280         $end_date->filter_year=false;
00281 
00282         $period=$g_user->get_periode();
00283         $per=new Periode($this->cn,$period);
00284         $year=$per->get_exercice();
00285 
00286         list($per_start,$per_end)=$per->get_limit($year);
00287         $start_date->value=$per_start->p_id;
00288         $end_date->value=$per_end->p_id;
00289 
00290         $str_start_date=$start_date->input();
00291         $str_end_date=$end_date->input();
00292 
00293         $aLabel=array(_('Ventes'),_('Dépense'),_('Banque'));
00294         $aCat=array();
00295 
00296         for ($i=0;$i<MAX_CAT;$i++)
00297         {
00298             /* category name */
00299             $name=new IText('fr_cat'.$i);
00300             $name->value=(isset($aLabel[$i]))?$aLabel[$i]:'';
00301             $aCat[$i]['name']=$name->input();
00302 
00303 
00304             /* category order */
00305             $order=new IText('fr_order'.$i);
00306             $order->value=$i+1;
00307             $aCat[$i]['order']=$order->input();
00308         }
00309 
00310         ob_start();
00311         require_once('template/forecast_cat.php');
00312         $r.=ob_get_contents();
00313         ob_end_clean();
00314         return $r;
00315 
00316     }
00317     /**
00318      * @brief create an empty object anticipation
00319      * @return html string with the form
00320      */
00321     public  function form_cat()
00322     {
00323         if ($this->f_id != 0)
00324             return $this->form_cat_mod();
00325         else
00326             return $this->form_cat_new();
00327     }
00328     /**
00329      *@brief display a form for modifying or add a forecast
00330      *@return HTML code
00331      */
00332     public function form_item()
00333     {
00334         $forecast=new Forecast($this->cn,$this->f_id);
00335         $forecast->load();
00336         $str_name=$forecast->get_parameter('name');
00337         $str_start=$forecast->get_parameter('start_date');
00338         $str_end=$forecast->get_parameter('end_date');
00339 
00340 
00341         $r="";
00342         $str_action=_("Elements");
00343         $cat=new Forecast_Cat($this->cn);
00344         $array=$cat->make_array($this->f_id);
00345         $periode=new Periode($this->cn);
00346         $aPeriode=$this->cn->make_array("select p_id,to_char(p_start,'MM.YYYY') as label from parm_periode
00347                                   where p_start >= (select p_start from parm_periode where p_id=$str_start)
00348                                    and p_end <= (select p_end from parm_periode where p_id=$str_end)
00349                                    order by p_start");
00350         $aPeriode[]=array('value'=>0,'label'=>'Mensuel');
00351         $value=$this->cn->get_array("select fi_id,fi_text,fi_account,fi_card,fc_id,fi_amount,fi_debit,fi_pid ".
00352                                     " from forecast_item ".
00353                                     "   where fc_id in (select fc_id from forecast_cat where f_id = $1)",array($this->f_id));
00354         $max=(count($value) < MAX_FORECAST_ITEM)?MAX_FORECAST_ITEM:count($value);
00355         $r.=HtmlInput::hidden('nbrow',$max);
00356 
00357         for ($i=0;$i<$max;$i++)
00358         {
00359             if (isset($value[$i]['fi_id']))
00360             {
00361                 $r.=HtmlInput::hidden('fi_id'.$i,$value[$i]['fi_id']);
00362             }
00363             /* category*/
00364             $category=new ISelect();
00365             $category->name='an_cat'.$i;
00366             $category->value=$array;
00367             $category->selected=(isset($value[$i]["fc_id"]))?$value[$i]["fc_id"]:-1;
00368             $aCat[$i]['cat']=$category->input();
00369 
00370             /* amount    */
00371             $amount=new INum('an_cat_amount'.$i);
00372             $amount->value=(isset($value[$i]["fi_amount"]))?$value[$i]["fi_amount"]:0;
00373             $aCat[$i]['amount']=$amount->input();
00374 
00375             /* Accounting*/
00376             $account=new IPoste('an_cat_acc'.$i);
00377             $account->set_attribute('ipopup','ipop_account');
00378             //            $account->set_attribute('label','an_label'.$i);
00379             $account->set_attribute('account','an_cat_acc'.$i);
00380             $account->set_attribute('bracket',1);
00381             $account->set_attribute('no_overwrite',1);
00382             $account->set_attribute('noquery',1);
00383             $account->css_size="85%";
00384             $account->value=(isset($value[$i]["fi_account"]))?$value[$i]["fi_account"]:"";
00385             $aCat[$i]['account']=$account->input();
00386             /*Quick Code */
00387             $qc=new ICard('an_qc'.$i);
00388             // If double click call the javascript fill_ipopcard
00389             $qc->set_dblclick("fill_ipopcard(this);");
00390 
00391             // This attribute is mandatory, it is the name of the IPopup
00392             $qc->set_attribute('ipopup','ipopcard');
00393 
00394             // name of the field to update with the name of the card
00395             $qc->set_attribute('label','an_label'.$i);
00396 
00397             // Type of card : all
00398             $qc->set_attribute('typecard','all');
00399             $qc->set_attribute('jrn',0);
00400             $qc->extra='all';
00401 
00402             // when value selected in the autcomplete
00403             $qc->set_function('fill_data');
00404             if (isset($value[$i]["fi_card"]))
00405             {
00406                 $f=new Fiche($this->cn,$value[$i]["fi_card"]);
00407                 $qc->value=$f->strAttribut(ATTR_DEF_QUICKCODE);
00408                 ;
00409             }
00410 
00411             $aCat[$i]['qc']=$qc->search().$qc->input();
00412             /* Label */
00413             $label=new IText('an_label'.$i);
00414             $label->value=(isset($value[$i]["fi_text"]))?$value[$i]["fi_text"]:"";
00415             $aCat[$i]['name']=$label->input();
00416 
00417             //Deb or Cred
00418             $deb=new ISelect('an_deb'.$i);
00419             $deb->selected=(isset($value[$i]["fi_debit"]))?$value[$i]["fi_debit"]:-1;
00420             $deb->value=array(array('value'=>'D','label'=>_('Débit')),
00421                               array('value'=>'C','label'=>_('Crédit'))
00422                              );
00423             $aCat[$i]['deb']=$deb->input();
00424             //Periode
00425             $isPeriode=new ISelect('month'.$i);
00426             $isPeriode->value=$aPeriode;
00427             $isPeriode->selected=(isset($value[$i]["fi_pid"]))?$value[$i]["fi_pid"]:0;
00428             $aCat[$i]['per']=$isPeriode->input();
00429         }
00430         $add_row=new IButton('add_row');
00431         $add_row->label=_('Ajouter une ligne');
00432         $add_row->javascript='for_add_row(\'fortable\')';
00433         $f_add_row=$add_row->input();
00434         ob_start();
00435         require_once('template/forecast-detail.php');
00436         $r.=ob_get_contents();
00437         ob_end_clean();
00438         return $r;
00439     }
00440     /**
00441      * @brief unit test
00442      */
00443     static function test_me()
00444     {
00445         $cn=new Database(dossier::id());
00446         $test=new Anticipation($cn);
00447 
00448     }
00449 
00450 }
00451 
00452 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations