Plugins  LAST
 All Data Structures Files Functions Variables Pages
class_coprop_appel_fond.php
Go to the documentation of this file.
1 <?php
2 /*
3  * Copyright 2010 De Bontridder Dany <dany@alchimerys.be>
4  * This file is part of NOALYSS.
5  *
6  * NOALYSS is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * NOALYSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with NOALYSS; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20 ?>
21  <?php
22 /**
23  *@brief calcul appel de fond
24  */
25 require_once 'class_copro_key.php';
26 require_once 'class_copro_budget.php';
28 {
29  function load()
30  {
31  global $cn;
32  $array=$cn->get_array("SELECT af_id, to_char(af_date,'DD.MM.YYYY') as af_date,
33  af_confirmed, af_percent, af_amount, af_card,
34  af_ledger, tech_per, jr_internal, b_id, cr_id
35  FROM coprop.appel_fond where af_id=$1",
36  array($this->id));
37 
38  $var=array('af_id','af_date','af_confirmed','af_percent','af_amount','af_card','af_ledger','tech_per','jr_internal','b_id','cr_id');
39  for ($i=0;$i<count($array);$i++)
40  {
41  $idx=$var[$i];
42  $this->$idx=$array[0][$idx];
43  }
44 
45  }
46  /**
47  *@brief create a key to save the data in DB
48  */
49  function create_key()
50  {
51  global $cn;
52  $this->id=$cn->get_next_seq('coprop.appel_fond_id');
53  // clean old
54  $cn->exec_sql("delete from coprop.appel_fond where af_confirmed='N' and tech_per < now() - interval '5 hours' ");
55 
56  // insert new unconfirmed
57  $cn->exec_sql("insert into coprop.appel_fond(af_id,af_date,af_confirmed,af_percent,af_amount,af_card,af_ledger,tech_per,b_id,cr_id)
58  values ($1,to_date($2,'DD.MM.YYYY'),'N',$3,$4,$5,$6,now(),$7,$8)",
59  array($this->id,$this->af_date,$this->af_percent,$this->af_amount,$this->af_card,$this->af_ledger,$this->b_id,$this->cr_id));
60 
61  }
62  function compute_key($p_key_id,$p_amount)
63  {
64  global $cn;
65  try
66  {
67  $cn->start();
68  // prend tantieme global
69  $key=new Copro_Key();
70  $key->cr_id=$p_key_id;
71  $key->load();
72 
73  bcscale(4);
74  $a_lot=$key->get_detail();
75  for ($i=0;$i<count($a_lot);$i++)
76  {
77  $afd=new Copro_Appel_Fond_Detail();
78  $afd->af_id=$this->id;
79  $afd->lot_id=$a_lot[$i]['lot_fk'];
80  $afd->key_id=$p_key_id;
81  $afd->key_tantieme=$key->cr_tantieme;
82  $afd->lot_tantieme=$a_lot[$i]['crd_amount'];
83 
84  $temp= bcmul($a_lot[$i]['crd_amount'],$p_amount);
85  $afd->afd_amount= bcdiv($temp,$key->cr_tantieme);
86 
87  if ( $afd->afd_amount != 0 )$afd->insert();
88 
89  }
90  $cn->commit();
91  }
92  catch (Exception $exc)
93  {
94  echo $exc->getTraceAsString();
95  $cn->rollback();
96  throw $exc;
97  }
98  }
99  function compute_amount($p_array)
100  {
101  global $cn;
102  bcscale(4);
103  try
104  {
105  $this->type = "amount";
106  $this->af_amount = $p_array['amount'];
107  $this->cr_id = $p_array['key'];
108  $this->af_percent = 1;
109  $this->b_id = null;
110  // req date valide
111  if (isDate($p_array['p_date']) == null)
112  throw new Exception('La date est invalide');
113  $this->af_ledger = $p_array['p_jrn'];
114  $this->af_date = $p_array['p_date'];
115  $fiche = new Fiche($cn);
116  $fiche->get_by_qcode($p_array['w_categorie_appel']);
117  // req $fiche->id > 0
118  if ($fiche->id < 1)
119  throw new Exception("La fiche pour l'appel de fond n'existe pas");
120  $this->af_card = $fiche->id;
121  $this->create_key();
122  $this->compute_key($this->cr_id,$this->af_amount);
123  }
124  catch (Exception $e)
125  {
126  throw ($e);
127  }
128  }
129  function compute_budget($p_array)
130  {
131  global $cn;
132  bcscale(4);
133  try
134  {
135  // req bud_pct <= 100 && bud_pct > 0
136  if ($p_array['bud_pct'] > 100 || $p_array['bud_pct']<0 ) throw new Exception ('Pourcentage incorrect');
137  // req date valide
138  if ( isDate($p_array['p_date'])==null) throw new Exception('La date est invalide');
139 
140  $this->type="budget";
141  $this->b_id=$p_array['b_id'];
142  $this->af_percent=$p_array['bud_pct']/100;
143  $this->af_ledger=$p_array['p_jrn'];
144  $this->af_date=$p_array['p_date'];
145  $fiche=new Fiche($cn);
146  $fiche->get_by_qcode($p_array['w_categorie_appel']);
147  // req $fiche->id > 0
148  if ( $fiche->id < 1 ) throw new Exception("La fiche pour l'appel de fond n'existe pas");
149  $this->af_card=$fiche->id;
150  $this->cr_id=null;
151 
152  $tot_bud=$cn->get_value('select b_amount from coprop.budget where b_id=$1',array($this->b_id));
153  if ($tot_bud <=0 ) throw new Exception ("Ce budget a un montant de 0 ");
154 
155  $this->af_amount= bcmul($tot_bud,$this->af_percent);
156  $this->create_key();
157 
158  // get all the key of this budget
159  $budget=new Copro_Budget();
160  $budget->b_id=$this->b_id;
161 
162  $a_detail=$budget->get_detail();
163  bcscale(4);
164 
165  // foreach key,
166  for ($i=0;$i<count($a_detail);$i++)
167  {
168  // compute the amount to take
169  $amount= bcmul($a_detail[$i]['bt_amount'],$this->af_percent);
170  $key=$a_detail[$i]['cr_id'];
171 
172  // call compute_key
173  $this->compute_key($key, $amount);
174 
175  }
176  } catch (Exception $e)
177  {
178  throw ($e);
179  }
180  }
181  function display_ledger()
182  {
183  global $cn;
184  $this->load();
185  $ledger = new Acc_Ledger($cn,$this->af_ledger);
186  $adetail = $cn->get_array("
187  select sum(afd_amount) as amount,S.coprop_id
188  from coprop.appel_fond_detail as A
189  join coprop.summary as S on (S.lot_id::numeric=A.lot_id)
190  where af_id=$1 group by S.coprop_id", array($this->id));
191  $array[] = array();
192 
193  $array['e_date'] = $this->af_date;
194  $array['p_jrn'] = $this->af_ledger;
195  $array['desc']="Appel de fond";
196  $fiche=new Fiche($cn,$this->af_card);
197  $array['qc_0']=$fiche->get_quick_code();
198  $array['amount0']=$this->af_amount;
199  $max_line=(MAXROWBUD > count($adetail))?MAXROWBUD:count($adetail);
200  $max_line+=2;
201  $array['nb_item']=$max_line;
202 
203  for ($i=0;$i<count($adetail);$i++){
204  $idx=$i+1;
205  $fiche=new Fiche($cn,$adetail[$i]['coprop_id']);
206  $array['qc_'.$idx]=$fiche->get_quick_code();
207  $array['amount'.$idx]=round($adetail[$i]['amount'],2);
208  $array['ck'.$idx]=1;
209  }
210 
211  echo '<FORM METHOD="GET" class="print">';
212  echo $ledger->input($array,0);
213  echo HtmlInput::request_to_hidden(array('amount','key','w_categorie_appel','b_id','aft','bud_pct','p_date','ac', 'plugin_code','sa'));
214  echo HtmlInput::extension() . dossier::hidden();
215  echo HtmlInput::hidden('action', 'confirm');
216  echo HtmlInput::hidden('af_id', $this->id);
217  echo HtmlInput::submit('save', 'Sauve');
218  echo HtmlInput::button('add', _('Ajout d\'une ligne'), 'onClick="quick_writing_add_row()"');
219  echo '</form>';
220  echo '<div class="info">' .
221  _('Débit') . ' = <span id="totalDeb"></span> ' .
222  _('Crédit') . ' = <span id="totalCred"></span> ' .
223  _('Difference') . ' = <span id="totalDiff"></span></div> ';
224  echo "<script>checkTotalDirect();</script>";
225  echo '</div>';
226  }
227  function confirm()
228  {
229  global $cn;
230  $cn->exec_sql("update coprop.appel_fond set af_confirmed='Y',jr_internal=$2 where af_id=$1",array($this->id,$this->jr_internal));
231  }
232 }
233 ?>
manage the table coprop.appel_fond_detail
if(isset($_POST['remove'])) $array
$bud b_id
$key
compute_key($p_key_id, $p_amount)
calcul appel de fond
create_key()
create a key to save the data in DB
if($p_number->value==0) l les annuités seront recalculées et l pct[] ad_year[] select jr_internal
const MAXROWBUD
global $cn