noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_anc_key.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*
00004  *   This file is part of NOALYSS.
00005  *
00006  *   NOALYSS is free software; you can redistribute it and/or modify
00007  *   it under the terms of the GNU General Public License as published by
00008  *   the Free Software Foundation; either version 2 of the License, or
00009  *   (at your option) any later version.
00010  *
00011  *   NOALYSS is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *   GNU General Public License for more details.
00015  *
00016  *   You should have received a copy of the GNU General Public License
00017  *   along with NOALYSS; if not, write to the Free Software
00018  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 // Copyright (2014) Author Dany De Bontridder danydb@aevalys.eu
00022 
00023 /**
00024  * @file
00025  * @brief Class to manage distribution keys for Analytic accountancy
00026  * 
00027  */
00028 require_once 'class_anc_key_sql.php';
00029 
00030 class Anc_Key
00031 {
00032 
00033     private $key; /* !  the distribution key */
00034     /**
00035      * Return the number of keys available.
00036      *  Return the number of keys available for the ledger given in parameter
00037      * 
00038      * @global $cn database connection
00039      * @param $p_jrn number of the ledger (jrn_def.jrn_def_id
00040      * @return number of available keys
00041      */
00042     static function key_avaiable($p_jrn)
00043     {
00044         global $cn;
00045         $count=$cn->get_value (' select count(*) 
00046             from key_distribution_ledger 
00047             join key_distribution using (kd_id)
00048             where
00049             jrn_def_id=$1', array($p_jrn));
00050         return $count;
00051     }
00052     function __construct($p_id=-1)
00053     {
00054         global $cn;
00055         $this->key=new Anc_Key_SQL($cn, $p_id);
00056         $this->a_ledger=null;
00057         $this->a_activity=null;
00058         $this->a_row=null;
00059     }
00060 
00061     /**
00062      * @brief display list of available keys
00063      * @param $p_amount   amount to distribute
00064      * @param $p_target   target to update
00065      * @param $p_ledger   is the jrn_def_id
00066      */
00067     static function display_choice($p_amount, $p_target,$p_ledger)
00068     {
00069         global $cn;
00070         $a_key=$cn->get_array(' select kd_id,
00071                 kd_name,
00072                 kd_description
00073                 from
00074                 key_distribution
00075                 join key_distribution_ledger using (kd_id)
00076                 where
00077                 jrn_def_id=$1',
00078                 array(
00079                     $p_ledger
00080                 ));
00081         if (empty($a_key))
00082         {
00083             echo _('Aucune clef disponible');
00084             echo _('Allez dans ANCKEY pour en ajouter pour ce journal');
00085         }
00086         include 'template/anc_key_display_choice.php';
00087     }
00088 
00089     /**
00090      * @brief display a  list of keys, choose one to modify it
00091      * 
00092      */
00093     static function display_list()
00094     {
00095         global $cn;
00096         $a_key=$cn->get_array('select b.kd_id,b.kd_name,b.kd_description,
00097                 (select sum(ke_percent) from key_distribution_detail as a where a.kd_id=b.kd_id) as distrib 
00098                 from key_distribution as b order by b.kd_name');
00099         if (empty($a_key))
00100         {
00101             echo _('Aucune clef disponible');
00102         }
00103         include 'template/anc_key_display_list.php';
00104     }
00105 
00106     /**
00107      * @brief Show the detail for a key distribution and let you change it
00108      * for adding or update
00109      */
00110     function input()
00111     {
00112         global $cn;
00113 
00114         $plan=$cn->get_array('
00115      select 
00116      pa_id,
00117      pa_name , 
00118      pa_description 
00119      from 
00120        plan_analytique 
00121      order by pa_name');
00122         $count_max=count($plan);
00123 
00124         $a_row=$cn->get_array('select ke_id,ke_row,ke_percent from key_distribution_detail 
00125          where
00126          kd_id=$1 order by ke_row', array($this->key->getp('id')));
00127 
00128         require_once 'template/anc_key_input.php';
00129     }
00130 
00131     /**
00132      * @brief verify that data are ok
00133      * @param type $p_array
00134      */
00135     function verify($p_array)
00136     {
00137         $a_percent=$p_array['percent'];
00138         if (count($a_percent)==0)
00139         {
00140             throw Exception(_('Aucune répartition'));
00141         }
00142         $tot_percent=0;
00143         bcscale(4);
00144         for ($i=0; $i<count($a_percent); $i++)
00145         {
00146             $tot_percent=bcadd($tot_percent, $a_percent[$i]);
00147         }
00148         if ($tot_percent >100)
00149         {
00150             throw new Exception(_('Le total ne vaut pas 100, total calculé = ').$tot_percent);
00151         }
00152         if ($p_array['name_key']=='') {
00153             throw new Exception (_('Le nom ne peut être vide'));
00154         }
00155     }
00156 
00157     /**
00158      * @brief save the data of a repartition key.
00159      * @param received an array
00160      * index :
00161      *   - key_id : key_distribution.kd_id
00162      *   - row : array of key_distribution.ke_id (row
00163      *   - pa : array of plan_analytic.pa_id (column)
00164      *   - po_id : double array, 
00165      *              first index is the row
00166      *              second  index is the first plan, the second the second plan...(column)
00167      *   - percent array, one per row
00168      *   - jrn : array of available ledgers
00169      * @note  if po_id == -1 then it is replaced by null, this why the pa_id is needed : to identify
00170      *  the column
00171      * @verbatim
00172      
00173         'key_id' => string '1' (length=1)
00174         'row' => 
00175           array
00176             0 => string '1' (length=1)
00177             1 => string '2' (length=1)
00178             2 => string '3' (length=1)
00179         'pa' => 
00180           array
00181             0 => string '1' (length=1)
00182             1 => string '2' (length=1)
00183         'po_id' => 
00184           array
00185             0 => 
00186               array
00187                 0 => string '1' (length=1)
00188                 1 => string '8' (length=1)
00189             1 => 
00190               array
00191                 0 => string '2' (length=1)
00192                 1 => string '-1' (length=2)
00193             2 => 
00194               array
00195                 0 => string '3' (length=1)
00196                 1 => string '8' (length=1)
00197         'percent' => 
00198           array
00199             0 => string '50.0000' (length=7)
00200             1 => string '20.0000' (length=7)
00201             2 => string '30.0000' (length=7)
00202         'jrn' => 
00203           array
00204             0 => string '3' (length=1)
00205             1 => string '2' (length=1)
00206       @endverbatim
00207      * 
00208      */
00209     function save($p_array)
00210     {
00211         global $cn;
00212         $this->verify($p_array);
00213         $cn->start();
00214         // for each row
00215         $a_row=$p_array['row'];
00216         $a_ledger=HtmlInput::default_value("jrn",array(),$p_array);
00217         $a_percent=$p_array['percent'];
00218         $a_po_id=$p_array['po_id'];
00219         $a_plan=$p_array['pa'];
00220         try
00221         {
00222             $this->key->setp('name',$p_array['name_key']);
00223             $this->key->setp('description',$p_array['description_key']);
00224             $this->key->save();
00225             for ($i=0; $i<count($a_row); $i++)
00226             {
00227                 //save key_distribution_row
00228                 $key_row=new Anc_Key_Detail_SQL($cn);
00229                 $key_row->setp('id', $a_row[$i]);
00230                 $key_row->setp('key', $this->key->getp('id'));
00231                 $key_row->setp('row', $i+1);
00232                 $key_row->setp('percent', $a_percent[$i]);
00233                 $key_row->save();
00234                 //
00235                 // Save each activity + percent
00236                 $cn->exec_sql('delete from key_distribution_activity where ke_id=$1', array($key_row->getp('id')));
00237 
00238                 // Don't save row with 0 %
00239                 if ($a_percent[$i]==0)
00240                 {
00241                     $key_row->delete();
00242                     continue;
00243                 }
00244                 for ($j=0; $j<count($a_po_id[$i]); $j++)
00245                 {
00246                     $activity=new Anc_Key_Activity_SQL($cn);
00247                     $activity->setp('detail', $key_row->ke_id);
00248                     $value=($a_po_id[$i][$j]==-1)?null:$a_po_id[$i][$j];
00249                     $activity->setp('activity', $value);
00250                     $activity->setp('plan',$a_plan[$j]);
00251                     $activity->save();
00252                 }
00253             }
00254             // delete all from key_distribution_ledger
00255             $cn->exec_sql('delete from key_distribution_ledger where kd_id=$1', array($this->key->getp('id')));
00256             for ($k=0; $k<count($a_ledger); $k++)
00257             {
00258                 $ledger=new Anc_Key_Ledger_SQL($cn);
00259                 $ledger->kd_id=$this->key->getp('id');
00260                 $ledger->jrn_def_id=$a_ledger[$k];
00261                 $ledger->save();
00262             }
00263             
00264             $cn->commit();
00265         }
00266         catch (Exception $e)
00267         {
00268             if ( DEBUG ) { echo $e->getTraceAsString(); var_dump($_POST);} else { echo _('erreur');}
00269             $cn->rollback();
00270         }
00271     }
00272     /**
00273      * @brief Call the Anc_Operation::display_form_plan with the right amounts.
00274      * This function compute the array and amount to pass to the Anc_Operation::display_form_plan
00275      * and replace the current table of activity with the value computed from the key.
00276      * 
00277      * @global $cn database connection
00278      * @param $p_target Table to be replaced
00279      * @param $p_amount amount to distribute among activities
00280      */
00281     function fill_table($p_target,$p_amount)
00282     {
00283         global $cn;
00284         /* number is the index of the plan, he's computed from p_target */
00285         $number=preg_replace('/det[0-9]/', '', $p_target);
00286         $number=str_replace('t', '', $number);
00287         $number=str_replace('popup', '', $number);
00288         
00289         $op[$number]=$p_amount;
00290         $array['op']=$op;
00291         $a_plan=$cn->get_array('select pa_id from plan_analytique order by pa_id');
00292         for ($i=0;$i < count($a_plan);$i++)
00293         {
00294             $array['pa_id'][$i]=$a_plan[$i]['pa_id'];
00295         }
00296         
00297         $a_poste=$cn->get_array('select po_id,ke_percent,pa_id,ke_row
00298                  from key_distribution_activity 
00299                  join key_distribution_detail using (ke_id)  
00300                  where
00301                  kd_id=$1
00302                  order by ke_row,pa_id',
00303                  array($this->key->getp('id')));
00304 
00305         for ($i=0;$i< count($a_poste);$i++)
00306         {
00307             $hplan[$number][$i]=($a_poste[$i]['po_id']==null)?-1:$a_poste[$i]['po_id'];
00308         }
00309         $array['hplan']=$hplan;
00310         
00311          $a_amount=$cn->get_array("select distinct ke_row,ke_percent 
00312                  from key_distribution_activity 
00313                  join key_distribution_detail using (ke_id) 
00314                  where
00315                     kd_id=$1
00316                     and pa_id=$2
00317                  order by ke_row",
00318                  array($this->key->getp('id'),$a_plan[0]['pa_id']));
00319          bcscale(2);
00320         for ($i=0;$i< count($a_amount);$i++)
00321         {
00322             $val[$number][$i]=bcmul($p_amount,$a_amount[$i]['ke_percent'])/100;
00323         }
00324         $array['val']=$val;
00325                
00326         $anc_operation=new Anc_Operation($cn);
00327         echo $anc_operation->display_form_plan($array, 1, 1, $number, $p_amount,'',false);
00328         
00329     }
00330     /**
00331      *@brief show a form for adding a key + button to display it
00332      * 
00333      */
00334     static function key_add()
00335     {
00336         $key=new Anc_Key();
00337         $key->key->setp('name',_('Nouvelle clef'));
00338         $key->key->setp('description',_('Description de la nouvelle clef'));
00339         ?>
00340 <input type="button" class="smallbutton" value="<?php echo  _('Ajout')?>" onclick="$('key_add_div_id').show()">
00341 <div id="key_add_div_id" style="display: none">
00342 <?php
00343         $key->input();
00344         echo '</div>';
00345         
00346     }
00347     /**
00348      *@brief delete the distribution key 
00349      */
00350     function delete ()
00351     {
00352         $this->key->delete();
00353     }
00354 }
 All Data Structures Namespaces Files Functions Variables Enumerations