00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 require_once("class_iconcerned.php");
00028 require_once ('class_database.php');
00029 require_once ('class_dossier.php');
00030 require_once 'class_lettering.php';
00031
00032
00033
00034
00035
00036 class Acc_Reconciliation
00037 {
00038 var $db;
00039 var $jr_id;
00040
00041 function __construct($cn)
00042 {
00043 $this->db=$cn;
00044 $this->jr_id=0;
00045 }
00046
00047 function set_jr_id($jr_id)
00048 {
00049 $this->jr_id=$jr_id;
00050 }
00051
00052
00053 function widget()
00054 {
00055 $wConcerned=new IConcerned();
00056 $wConcerned->extra=0;
00057
00058 return $wConcerned;
00059
00060 }
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 function insert($jr_id2)
00071 {
00072 if ( trim($jr_id2) == "" )
00073 return;
00074 if ( strpos($jr_id2,',') !== 0 )
00075 {
00076 $aRapt=explode(',',$jr_id2);
00077 foreach ($aRapt as $rRapt)
00078 {
00079 if ( isNumber($rRapt) == 1 )
00080 {
00081 $this->insert_rapt($rRapt);
00082 }
00083 }
00084 }
00085 else
00086 if ( isNumber($jr_id2) == 1 )
00087 {
00088 $this->insert_rapt($jr_id2);
00089 }
00090 }
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 function insert_rapt($jr_id2)
00102 {
00103 if ( isNumber($this->jr_id) == 0 || isNumber($jr_id2) == 0 )
00104 {
00105 return false;
00106 }
00107 if ( $this->jr_id==$jr_id2)
00108 return true;
00109
00110 if ( $this->db->count_sql("select jr_id from jrn where jr_id=".$this->jr_id)==0 )
00111 return false;
00112 if ( $this->db->count_sql("select jr_id from jrn where jr_id=".$jr_id2)==0 )
00113 return false;
00114
00115
00116 if ( $this->db->count_sql(
00117 "select jra_id from jrn_rapt where jra_concerned=".$this->jr_id.
00118 " and jr_id=$jr_id2
00119 union
00120 select jra_id from jrn_rapt where jr_id=".$this->jr_id.
00121 " and jra_concerned=$jr_id2 ")
00122 ==0)
00123 {
00124
00125 $Res=$this->db->exec_sql("insert into jrn_rapt(jr_id,jra_concerned) values ".
00126 "(".$this->jr_id.",$jr_id2)");
00127
00128 $this->auto_letter($jr_id2);
00129
00130
00131 $source_type=$this->db->get_value("select substr(jr_internal,1,1) from jrn where jr_id=$1",array($this->jr_id));
00132 $dest_type=$this->db->get_value("select substr(jr_internal,1,1) from jrn where jr_id=$1",array($jr_id2));
00133 if (($source_type =='A' || $source_type=='V') && ($dest_type != 'A' && $dest_type != 'V'))
00134 {
00135
00136 $date=$this->db->get_value('select jr_date from jrn where jr_id=$1',array($jr_id2));
00137 if ( trim ($date) == '') $date=null;
00138 $this->db->exec_sql('update jrn set jr_date_paid=$1 where jr_id=$2 and jr_date_paid is null ',array($date,$this->jr_id));
00139 }
00140 if (($source_type !='A' && $source_type !='V') && ($dest_type == 'A' || $dest_type == 'V'))
00141 {
00142
00143 $date=$this->db->get_value('select jr_date from jrn where jr_id=$1',array($this->jr_id));
00144 if (trim($date) == '') $date=null;
00145 $this->db->exec_sql('update jrn set jr_date_paid=$1 where jr_id=$2 and jr_date_paid is null ',array($date,$jr_id2));
00146 }
00147 }
00148 return true;
00149 }
00150
00151
00152
00153
00154 function auto_letter($p_jrid)
00155 {
00156
00157 $sql="select j1.f_id as fiche ,coalesce(j1.j_id,-1) as jrnx_id1,coalesce(j2.j_id,-1) as jrnx_id2,
00158 j1.j_poste as poste
00159 from jrnx as j1
00160 join jrn as jr1 on (j1.j_grpt=jr1.jr_grpt_id)
00161 join jrnx as j2 on (coalesce(j1.f_id,-1)=coalesce(j2.f_id,-1) and j1.j_poste=j2.j_poste)
00162 join jrn as jr2 on (j2.j_grpt=jr2.jr_grpt_id)
00163 where
00164 jr1.jr_id=$1
00165 and
00166 jr2.jr_id= $2";
00167 $result=$this->db->get_array($sql,array($this->jr_id,$p_jrid));
00168 if ( count($result) == 0)
00169 {
00170 return;
00171 }
00172 for ($i=0;$i<count($result);$i++)
00173 {
00174 if ( $result[$i]['fiche'] != -1)
00175 {
00176 $letter = new Lettering_Card($this->db);
00177 $letter->insert_couple($result[$i]['jrnx_id1'],$result[$i]['jrnx_id2']);
00178 }
00179 else
00180 {
00181 $letter = new Lettering_Account($this->db);
00182 $letter->insert_couple($result[$i]['jrnx_id1'],$result[$i]['jrnx_id2']);
00183 }
00184 }
00185
00186 }
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 function remove($jr_id2)
00197 {
00198 if ( isNumber($this->jr_id) == 0 or
00199 isNumber($jr_id2) == 0 )
00200 {
00201 return;
00202 }
00203
00204 if ( $this->db->count_sql("select jra_id from jrn_rapt where ".
00205 " jra_concerned=".$this->jr_id." and jr_id=$jr_id2
00206 union
00207 select jra_id from jrn_rapt where jra_concerned=$jr_id2 ".
00208 " and jr_id=".$this->jr_id) !=0)
00209 {
00210
00211
00212
00213 $sql = " delete from
00214 jnt_letter
00215 where jl_id in ( select jl_id from jnt_letter
00216 join letter_cred as lc using(jl_id)
00217 join letter_deb as ld using (jl_id)
00218 where
00219 lc.j_id in (select j_id
00220 from jrnx join jrn on (j_grpt=jr_grpt_id)
00221 where jr_id in ($1,$2))
00222 or
00223 ld.j_id in (select j_id
00224 from jrnx join jrn on (j_grpt=jr_grpt_id)
00225 where jr_id in ($1,$2))
00226
00227
00228
00229 )";
00230 $this->db->exec_sql($sql, array($jr_id2, $this->jr_id));
00231
00232 $Res=$this->db->exec_sql("delete from jrn_rapt where ".
00233 "(jra_concerned=$jr_id2 and jr_id=".$this->jr_id.") or
00234 (jra_concerned=".$this->jr_id." and jr_id=$jr_id2) ");
00235 }
00236 }
00237
00238
00239
00240
00241
00242
00243
00244
00245 function get ( )
00246 {
00247 $sql=" select jr_id as cn from jrn_rapt where jra_concerned=".$this->jr_id.
00248 " union ".
00249 " select jra_concerned as cn from jrn_rapt where jr_id=".$this->jr_id;
00250 $Res=$this->db->exec_sql($sql);
00251
00252
00253 $n=Database::num_row($Res);
00254
00255 if ($n ==0 ) return null;
00256
00257
00258 for ($i=0;$i<$n;$i++)
00259 {
00260 $l=Database::fetch_array($Res,$i);
00261 $r[$i]=$l['cn'];
00262 }
00263 return $r;
00264 }
00265 function fill_info()
00266 {
00267 $sql="select jr_id,jr_date,jr_comment,jr_internal,jr_montant,jr_pj_number,jr_def_id,jrn_def_name,jrn_def_type
00268 from jrn join jrn_def on (jrn_def_id=jr_def_id)
00269 where jr_id=$1";
00270 $a=$this->db->get_array($sql,array($this->jr_id));
00271 return $a[0];
00272 }
00273
00274
00275
00276
00277 function get_not_reconciled()
00278 {
00279 $filter_date=$this->filter_date();
00280
00281 $sql_jrn=$this->ledger_filter();
00282
00283 $array=$this->db->get_array("select distinct jr_id,jr_date from jrn where $filter_date and $sql_jrn and jr_id not in (select jr_id from jrn_rapt union select jra_concerned from jrn_rapt) order by jr_date");
00284 $ret=array();
00285 for ($i=0;$i<count($array);$i++)
00286 {
00287 $this->jr_id=$array[$i]['jr_id'];
00288 $ret[$i]['first']=$this->fill_info();
00289 }
00290 $this->db->prepare('detail_quant','select * from v_quant_detail where jr_id=$1');
00291 return $ret;
00292 }
00293
00294
00295
00296
00297
00298
00299 function ledger_filter ()
00300 {
00301 global $g_user;
00302
00303 $sql=$g_user->get_ledger_sql('ALL',3);
00304 $sql=str_replace('jrn_def_id','jr_def_id',$sql);
00305 $r='';
00306
00307 if ($this->a_jrn != null )
00308 {
00309 $sep='';
00310 $r='and jr_def_id in (';
00311 foreach( $this->a_jrn as $key=>$value)
00312 {
00313 $r.=$sep.$value;
00314 $sep=',';
00315 }
00316 $r.=')';
00317 }
00318 return $sql.' '.$r;
00319 }
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330 function get_reconciled()
00331 {
00332 $filter_date=$this->filter_date();
00333
00334
00335
00336 $sql_jrn=$this->ledger_filter();
00337
00338 $array=$this->db->get_array("select distinct jr_id,jr_date from jrn where $filter_date and $sql_jrn and jr_id in (select jr_id from jrn_rapt union select jra_concerned from jrn_rapt) order by jr_date");
00339 $ret=array();
00340 for ($i=0;$i<count($array);$i++)
00341 {
00342 $this->jr_id=$array[$i]['jr_id'];
00343 $ret[$i]['first']=$this->fill_info();
00344 $atmp=$this->get();
00345 for ( $e=0;$e<count($atmp);$e++)
00346 {
00347 $this->jr_id=$atmp[$e];
00348 $ret[$i]['depend'][$e]=$this->fill_info();
00349 }
00350 }
00351 $this->db->prepare('detail_quant','select * from v_quant_detail where jr_id=$1');
00352 return $ret;
00353 }
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365 function get_reconciled_amount($p_equal=false)
00366 {
00367 $array=$this->get_reconciled();
00368 $ret=array();
00369 bcscale(2);
00370 for ($i=0;$i<count($array);$i++)
00371 {
00372 $first_amount=$array[$i]['first']['jr_montant'];
00373 $second_amount=0;
00374 for ($e=0;$e<count($array[$i]['depend']);$e++)
00375 {
00376 $second_amount=bcadd($second_amount,$array[$i]['depend'][$e]['jr_montant']);
00377 }
00378 if ( $p_equal && $first_amount==$second_amount)
00379 {
00380 $ret[]=$array[$i];
00381 }
00382 if ( ! $p_equal && $first_amount != $second_amount)
00383 {
00384 $ret[]=$array[$i];
00385 }
00386 }
00387 return $ret;
00388 }
00389
00390
00391
00392
00393
00394
00395 function filter_date()
00396 {
00397 global $g_user;
00398 list($start,$end)=$g_user->get_limit_current_exercice();
00399
00400 if (isDate($this->start_day) ==null)
00401 {
00402 $this->start_day=$start;
00403 }
00404 if ( isDate($this->end_day) == null)
00405 {
00406 $this->end_day=$end;
00407 }
00408 $sql=" (jr_date >= to_date('".$this->start_day."','DD.MM.YYYY')
00409 and jr_date <= to_date('".$this->end_day."','DD.MM.YYYY'))";
00410 return $sql;
00411
00412 }
00413 function show_detail($p_ret)
00414 {
00415 if (Database::num_row($p_ret)> 0)
00416 {
00417 echo '<tr class="odd">';
00418 echo '<td></td>';
00419 echo '<td colspan="5" style="border:1px solid black;width:auto">';
00420 include 'template/impress_reconciliation_detail.php';
00421 echo '</td>';
00422 echo '</tr>';
00423 }
00424 }
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435 function export_csv($p_choice)
00436 {
00437 $array = $this->get_data($p_choice);
00438 for ($i = 0; $i < count($array); $i++)
00439 {
00440
00441
00442
00443
00444 $first = $array[$i]['first'];
00445 $a_depend = array();
00446 if (isset($array[$i]['depend']))
00447 {
00448 $a_depend = $array[$i]['depend'];
00449
00450 if ($i == 0)
00451 {
00452 printf('"n°";"Date";"internal";"libellé";"n° pièce";"nom journal";"type journal";"montant"');
00453 printf(';"<->";"Date";"internal";"libellé";"n° pièce";"nom journal";"type journal";"montant"' . "\n\r");
00454 }
00455 }
00456 else
00457 {
00458
00459 if ($i == 0)
00460 {
00461 printf('"n°";"Date";"internal";"libellé";"n° pièce";"nom journal";"type journal";"montant"' . "\n\r");
00462 }
00463 }
00464
00465
00466
00467 printf('%d;"%s";"%s";"%s";"%s";"%s";"%s";%f',$i, $first['jr_date'], $first['jr_internal'], $first['jr_comment'], $first['jr_pj_number'], $first['jrn_def_name'], $first['jrn_def_type'], $first['jr_montant']);
00468 if (count($a_depend) > 0)
00469 {
00470
00471
00472
00473 $depend = $a_depend[0];
00474 printf(';"<->";"%s";"%s";"%s";"%s";"%s";"%s";%f' . "\n\r", $depend['jr_date'], $depend['jr_internal'], $depend['jr_comment'], $depend['jr_pj_number'], $depend['jrn_def_name'], $depend['jrn_def_type'], $depend['jr_montant']);
00475
00476
00477
00478
00479 for ($e = 1; $e < count($a_depend); $e++)
00480 {
00481 $depend = $a_depend[$e];
00482 printf(';;;;;;;"<->";');
00483 printf('"%s";"%s";"%s";"%s";"%s";"%s";%f' . "\n\r", $depend['jr_date'], $depend['jr_internal'], $depend['jr_comment'], $depend['jr_pj_number'], $depend['jrn_def_name'], $depend['jrn_def_type'], $depend['jr_montant']);
00484 }
00485 }
00486 else
00487 {
00488 printf("\n\r");
00489 }
00490 }
00491 }
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502 function get_data($p_choice)
00503 {
00504 switch ($p_choice)
00505 {
00506 case 0:
00507 $array = $this->get_reconciled();
00508 break;
00509 case 1:
00510 $array = $this->get_reconciled_amount(false);
00511 break;
00512 case 2:
00513 $array = $this->get_reconciled_amount(true);
00514 break;
00515 case 3:
00516 $array = $this->get_not_reconciled();
00517 break;
00518 default:
00519 echo "Choix invalid";
00520 exit();
00521 }
00522 return $array;
00523 }
00524
00525 static function test_me()
00526 {
00527 $cn=new Database(dossier::id());
00528 $rap=new Acc_Reconciliation($cn);
00529 var_dump($rap->get_reconciled_amount('',false));
00530 }
00531
00532 }