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 require_once ('class_user.php');
00027 require_once('class_acc_ledger.php');
00028
00029
00030
00031
00032
00033 class Acc_Operation
00034 {
00035 var $db;
00036 var $jr_id;
00037 var $jrn_id;
00038 var $debit;
00039 var $user;
00040 var $jrn;
00041 var $poste;
00042 var $date;
00043 var $periode;
00044 var $amount;
00045 var $grpt;
00046 var $date_paid;
00047
00048
00049
00050
00051 function __construct($p_cn)
00052 {
00053 global $g_user;
00054 $this->db=$p_cn;
00055 $this->qcode="";
00056 $this->user=$_SESSION['g_user'];
00057 $this->periode=$g_user->get_periode();
00058 $this->jr_id=0;
00059 }
00060
00061
00062
00063
00064 function seek_group()
00065 {
00066 $ret=$this->db->get_value('select jr_grpt_id from jrn where jr_id=$1',
00067 array($this->jr_id));
00068 return $ret;
00069 }
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 function insert_jrnx()
00089 {
00090 if ( $this->poste == "") { return false; throw new Exception (__FILE__.':'.__LINE__.' Poste comptable vide');}
00091
00092 if ( $this->amount < 0 )
00093 {
00094 $this->type=($this->type=='d')?'c':'d';
00095 }
00096 $this->amount=abs($this->amount);
00097 $debit=($this->type=='c')?'false':'true';
00098 $this->desc=(isset($this->desc))?$this->desc:'';
00099 $Res=$this->db->exec_sql("select insert_jrnx
00100 ($1::text,abs($2)::numeric,$3::account_type,$4::integer,$5::integer,$6::bool,$7::text,$8::integer,upper($9),$10::text)",
00101 array(
00102 $this->date,
00103 round($this->amount,2),
00104 $this->poste,
00105 $this->grpt,
00106 $this->jrn,
00107 $debit,
00108 $this->user,
00109 $this->periode,
00110 $this->qcode,
00111 $this->desc));
00112 if ( $Res===false) return $Res;
00113 $this->jrnx_id=$this->db->get_current_seq('s_jrn_op');
00114 return $this->jrnx_id;
00115
00116 }
00117
00118
00119
00120 function set_pj()
00121 {
00122 if ( strlen(trim($this->pj)) == 0 )
00123 {
00124 $sql="update jrn set jr_pj_number=$1 where jr_id=$2";
00125 $this->db->exec_sql($sql,array(null,$this->jr_id));
00126 return '';
00127 }
00128
00129 if ( $this->db->count_sql("select jr_id from jrn where jr_pj_number=$1 and jr_def_id=$2",
00130 array($this->pj,$this->jrn)
00131 ) == 0 )
00132 {
00133 $sql="update jrn set jr_pj_number=$1 where jr_id=$2";
00134 $this->db->exec_sql($sql,array($this->pj,$this->jr_id));
00135 }
00136 else
00137 {
00138
00139 $pref=$this->db->get_value("select jrn_def_pj_pref from jrn_def where jrn_def_id=$1",
00140 array($this->jrn));
00141
00142 $flag=0;
00143 $limit=100;
00144 while ( $flag == 0 )
00145 {
00146
00147 if ( $limit < 1 )
00148 {
00149 $this->pj='';
00150 $flag=2;
00151 break;
00152 }
00153
00154 $seq=$this->db->get_next_seq('s_jrn_pj'.$this->jrn);
00155 $this->pj=$pref.$seq;
00156
00157
00158 $c=$this->db->count_sql("select jr_id from jrn where jr_pj_number=$1 and jr_def_id=$2",
00159 array($this->pj,$this->jrn)
00160 );
00161 if ( $c == 0 )
00162 {
00163 $flag=1;
00164 break;
00165 }
00166 $limit--;
00167 }
00168
00169 if ( $flag == 1 )
00170 {
00171 $sql="update jrn set jr_pj_number=$1 where jr_id=$2";
00172 $this->db->exec_sql($sql,array($this->pj,$this->jr_id));
00173 }
00174 }
00175 return $this->pj;
00176 }
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186 function insert_jrn()
00187 {
00188 $p_comment=$this->desc;
00189
00190 $diff=$this->db->get_value("select check_balance ($1)",array($this->grpt));
00191 if ( $diff != 0 )
00192 {
00193
00194 echo "Erreur : balance incorrecte :diff = $diff";
00195 return false;
00196 }
00197
00198 $echeance=( isset( $this->echeance) && strlen(trim($this->echeance)) != 0)?$this->echeance:null;
00199 if ( ! isset($this->mt) )
00200 {
00201 $this->mt=microtime(true);
00202 }
00203
00204
00205 $Res=$this->db->exec_sql("insert into jrn (jr_def_id,jr_montant,jr_comment,".
00206 "jr_date,jr_ech,jr_grpt_id,jr_tech_per,jr_mt) values (".
00207 "$1,$2,$3,".
00208 "to_date($4,'DD.MM.YYYY'),to_date($5,'DD.MM.YYYY'),$6,$7,$8)",
00209 array ($this->jrn, $this->amount,$p_comment,
00210 $this->date,$echeance,$this->grpt,$this->periode,$this->mt)
00211 );
00212 if ( $Res == false) return false;
00213 $this->jr_id=$this->db->get_current_seq('s_jrn');
00214 return $this->jr_id;
00215 }
00216
00217
00218
00219
00220
00221
00222 function get_internal()
00223 {
00224 if ( ! isset($this->jr_id) )
00225 throw new Exception('jr_id is not set',1);
00226 $Res=$this->db->exec_sql("select jr_internal from jrn where jr_id=".$this->jr_id);
00227 if ( Database::num_row($Res) == 0 ) return null;
00228 $l_line=Database::fetch_array($Res);
00229 $this->jr_internal= $l_line['jr_internal'];
00230 return $this->jr_internal;
00231 }
00232
00233
00234
00235
00236 function seek_internal($p_internal)
00237 {
00238 $res=$this->db->exec_sql('select jr_id from jrn where jr_internal=$1',
00239 array($p_internal));
00240 if ( Database::num_row($Res) == 0 ) return -1;
00241 $this->jr_id=Database::fetch_result($Res,0,0);
00242 return 0;
00243 }
00244
00245
00246
00247
00248 function get_jrnx_detail()
00249 {
00250 global $g_user;
00251 $filter_sql=$g_user->get_ledger_sql('ALL',3);
00252 $filter_sql=str_replace('jrn_def_id','jr_def_id',$filter_sql);
00253 if ( $this->jr_id==0 ) return;
00254 $sql=" select jr_date,j_qcode,j_poste,j_montant,jr_internal,case when j_debit = 'f' then 'C' else 'D' end as debit,jr_comment as description,
00255 vw_name,pcm_lib,j_debit,coalesce(comptaproc.get_letter_jnt(j_id),-1) as letter,jr_def_id ".
00256 " from jrnx join jrn on (jr_grpt_id=j_grpt)
00257 join tmp_pcmn on (j_poste=pcm_val)
00258 left join vw_fiche_attr on (j_qcode=quick_code)
00259 where
00260 jr_id=$1 and $filter_sql order by j_debit desc";
00261 $res=$this->db->exec_sql($sql,array($this->jr_id));
00262 if ( Database::num_row ($res) == 0 ) return array();
00263 $all=Database::fetch_all($res);
00264 return $all;
00265 }
00266
00267 function update_comment($p_text)
00268 {
00269 $sql="update jrnx set j_text=$1 where j_id=$2";
00270 $this->db->exec_sql($sql,array($p_text,$this->jrnx_id));
00271 }
00272
00273 function operation_update_comment($p_text)
00274 {
00275 $sql="update jrn set jr_comment=$1 where jr_id=$2";
00276 $this->db->exec_sql($sql,array($p_text,$this->jr_id));
00277 }
00278
00279 function operation_update_date_limit($p_text)
00280 {
00281 if ( isDate($p_text) == null )
00282 {
00283 $p_text=null;
00284 }
00285 $sql="update jrn set jr_ech=to_date($1,'DD.MM.YYYY') where jr_id=$2";
00286 $this->db->exec_sql($sql,array($p_text,$this->jr_id));
00287 }
00288
00289 function get_ledger()
00290 {
00291 $sql="select jr_def_id from jrn where jr_id=$1";
00292 $row=$this->db->get_value($sql,array($this->jr_id));
00293 return $row;
00294 }
00295
00296
00297
00298
00299
00300 function display_jrnx_detail($p_table)
00301 {
00302 $show=$this->get_jrnx_detail();
00303
00304 $r='';
00305 $r_notable='';
00306 $csv="";
00307 foreach ($show as $l)
00308 {
00309 $border="";
00310 if ( $l['j_poste'] == $this->poste || ($l['j_qcode']==$this->qcode && trim($this->qcode) != ''))
00311 $border=' class="highlight"';
00312 $r.='<tr '.$border.'>';
00313 $r.='<td>';
00314 $a=$l['j_qcode'];
00315
00316 $r_notable.=$a;
00317 $r.=$a;
00318 $csv.='"'.$a.'";';
00319 $r.='</td>';
00320
00321 $r.='<td '.$border.'>';
00322 $a=$l['j_poste'];
00323 $r_notable.=$a;
00324 $r.=$a;
00325 $csv.='"'.$a.'";';
00326 $r.='</td>';
00327
00328 $r.='<td '.$border.'>';
00329
00330 $a=(strlen(trim($l['j_qcode']))==0)?$l['pcm_lib']:$l['vw_name'];
00331 $r_notable.=$a;
00332 $r.=h($a);
00333 $csv.='"'.$a.'";';
00334 $r.='</td>';
00335
00336 $r.='<td '.$border.'>';
00337 $a=$l['j_montant'];
00338 $r_notable.=$a;
00339 $r.=$a;
00340 $csv.=$a.';';
00341 $r.='</td>';
00342
00343 $r.='<td '.$border.'>';
00344 $a=$l['debit'];
00345 $r_notable.=$a;
00346 $r.=$a;
00347 $csv.='"'.$a.'"';
00348
00349 $csv.="\r\n";
00350 $r.='</td>';
00351 $r.='<td '.$border.'>';
00352 $a=($l['letter']!=-1)?$l['letter']:'';
00353 $r_notable.=$a;
00354 $r.=$a;
00355 $csv.='"'.$a.'"';
00356
00357 $csv.="\r\n";
00358 $r.='</td>';
00359
00360
00361 $r.='</tr>';
00362 }
00363 switch ($p_table)
00364 {
00365 case 1:
00366 return $r;
00367 break;
00368 case 0:
00369 return $r_notable;
00370 break;
00371 case 2:
00372 return $csv;
00373 }
00374 return "ERROR PARAMETRE";
00375 }
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408 function get_data ($p_grpt)
00409 {
00410 $Res=$this->db->exec_sql("select
00411 to_char(j_date,'DD.MM.YYYY') as j_date,
00412 j_text,
00413 j_debit,
00414 j_poste,
00415 coalesce(j_qcode,'-') as qcode,
00416 j_montant,
00417 j_id,
00418 jr_comment,
00419 to_char(jr_ech,'DD.MM.YYYY') as jr_ech,
00420 to_char(jr_date,'DD.MM.YYYY') as jr_date,
00421 jr_id,jr_internal,jr_def_id,jr_pj
00422 from jrnx inner join jrn on j_grpt=jr_grpt_id where j_grpt=$1",array($p_grpt));
00423 $MaxLine=Database::num_row($Res);
00424 if ( $MaxLine == 0 ) return null;
00425 $deb=0;
00426 $cred=0;
00427 for ( $i=0; $i < $MaxLine; $i++)
00428 {
00429
00430 $l_line=Database::fetch_array($Res,$i);
00431 $l_array['op_date']=$l_line['j_date'];
00432 if ( $l_line['j_debit'] == 't' )
00433 {
00434 $l_class=sprintf("class_deb%d",$deb);
00435 $l_montant=sprintf("mont_deb%d",$deb);
00436 $l_text=sprintf("text_deb%d",$deb);
00437 $l_qcode=sprintf("qcode_deb%d",$deb);
00438 $l_array[$l_class]=$l_line['j_poste'];
00439 $l_array[$l_montant]=$l_line['j_montant'];
00440 $l_array[$l_text]=$l_line['j_text'];
00441 $l_array[$l_qcode]=$l_line['qcode'];
00442 $l_id=sprintf("op_deb%d",$deb);
00443 $l_array[$l_id]=$l_line['j_id'];
00444 $deb++;
00445 }
00446 if ( $l_line['j_debit'] == 'f' )
00447 {
00448 $l_class=sprintf("class_cred%d",$cred);
00449 $l_montant=sprintf("mont_cred%d",$cred);
00450 $l_array[$l_class]=$l_line['j_poste'];
00451 $l_array[$l_montant]=$l_line['j_montant'];
00452 $l_id=sprintf("op_cred%d",$cred);
00453 $l_array[$l_id]=$l_line['j_id'];
00454 $l_text=sprintf("text_cred%d",$cred);
00455 $l_array[$l_text]=$l_line['j_text'];
00456 $l_qcode=sprintf("qcode_cred%d",$cred);
00457 $l_array[$l_qcode]=$l_line['qcode'];
00458 $cred++;
00459 }
00460 $l_array['jr_internal']=$l_line['jr_internal'];
00461 $l_array['comment']=$l_line['jr_comment'];
00462 $l_array['ech']=$l_line['jr_ech'];
00463 $l_array['jr_id']=$l_line['jr_id'];
00464 $l_array['jr_def_id']=$l_line['jr_def_id'];
00465 }
00466 return array($l_array,$deb,$cred);
00467 }
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477 function get()
00478 {
00479 $ret=new Acc_Misc($this->db,$this->jr_id);
00480 $ret->get();
00481 return $ret;
00482 }
00483
00484
00485
00486
00487
00488
00489 function get_quant()
00490 {
00491 $ledger_id=$this->get_ledger();
00492 if ( $ledger_id=='') throw new Exception('Journal non trouvé');
00493 $oledger=new Acc_Ledger($this->db,$ledger_id);
00494
00495
00496
00497
00498 switch($oledger->get_type())
00499 {
00500 case 'VEN':
00501 $ret=new Acc_Sold($this->db,$this->jr_id);
00502 break;
00503 case 'ACH':
00504 $ret=new Acc_Purchase($this->db,$this->jr_id);
00505 break;
00506 case 'FIN':
00507 $ret=new Acc_Fin($this->db,$this->jr_id);
00508 break;
00509 default:
00510 $ret=new Acc_Misc($this->db,$this->jr_id);
00511 break;
00512 }
00513 $ret->get();
00514 if ( empty($ret->det->array))
00515 {
00516 $ret=new Acc_Misc($this->db,$this->jr_id);
00517 $ret->get();
00518 }
00519 $ret->get_info();
00520 return $ret;
00521 }
00522
00523
00524
00525
00526
00527 function get_info()
00528 {
00529 $this->info=new stdClass();
00530
00531 $array=$this->db->get_value("select ji_value from jrn_info where
00532 jr_id=$1 and id_type=$2",array($this->jr_id,'OTHER'));
00533 $this->info->other= $array;
00534
00535
00536 $array=$this->db->get_value("select ji_value from jrn_info where
00537 jr_id=$1 and id_type=$2",array($this->jr_id,'BON_COMMANDE'));
00538 $this->info->command= $array;
00539
00540 }
00541
00542
00543
00544
00545
00546 function save_info($p_info,$p_type)
00547 {
00548 if ( ! in_array($p_type,array('OTHER','BON_COMMANDE'))) return;
00549 if (trim($p_info)=="") {
00550 $this->db->exec_sql('delete from jrn_info where jr_id=$1 and id_type=$2',array($this->jr_id,$p_type));
00551 return;
00552 }
00553 $exist=$this->db->get_value('select count(ji_id) from jrn_info where jr_id=$1 and id_type=$2',array($this->jr_id,$p_type));
00554 if ( $exist == "0" ) {
00555
00556 $this->db->exec_sql('insert into jrn_info(jr_id,id_type,ji_value) values ($1,$2,$3)',
00557 array($this->jr_id,$p_type,$p_info));
00558 } elseif ( $exist == 1) {
00559
00560 $this->db->exec_sql('update jrn_info set ji_value=$3 where jr_id=$1 and id_type=$2',
00561 array($this->jr_id,$p_type,$p_info));
00562 }
00563 }
00564 static function test_me()
00565 {
00566 $_SESSION['g_user']='phpcompta';
00567 $_SESSION['g_pass']='dany';
00568 global $g_user;
00569 $cn=new Database(dossier::id());
00570 $g_user=new User($cn);
00571 $a=new Acc_Operation($cn);
00572 $a->jr_id=1444;
00573 $b=$a->get_quant();
00574 var_dump($b);
00575 }
00576 }
00577
00578 class Acc_Detail extends Acc_Operation
00579 {
00580 function __construct($p_cn,$p_jrid=0)
00581 {
00582 parent::__construct($p_cn);
00583 $this->jr_id=$p_jrid;
00584 $this->det=new stdClass();
00585 }
00586
00587
00588
00589
00590 function get()
00591 {
00592 $sql="SELECT jr_id, jr_def_id, jr_montant, jr_comment, jr_date, jr_grpt_id,
00593 jr_internal, jr_tech_date, jr_tech_per, jrn_ech, jr_ech, jr_rapt,jr_ech,
00594 jr_valid, jr_opid, jr_c_opid, jr_pj, jr_pj_name, jr_pj_type,
00595 jr_pj_number, jr_mt,jr_rapt,jr_date_paid
00596 FROM jrn where jr_id=$1";
00597 $array=$this->db->get_array($sql,array($this->jr_id));
00598 if ( count($array) == 0 ) throw new Exception('Aucune ligne trouvée');
00599 foreach ($array[0] as $key=>$val)
00600 {
00601 $this->det->$key=$val;
00602 }
00603 $sql="select n_text from jrn_note where jr_id=$1";
00604 $this->det->note=$this->db->get_value($sql,array($this->jr_id));
00605 $this->det->note=strip_tags($this->det->note);
00606 }
00607 }
00608
00609
00610
00611
00612
00613
00614 class Acc_Misc extends Acc_Detail
00615 {
00616 var $signature;
00617 var $array;
00618 function __construct($p_cn,$p_jrid=0)
00619 {
00620 parent::__construct($p_cn,$p_jrid);
00621 $this->signature='ODS';
00622 $this->det=new stdClass();
00623 }
00624 function get()
00625 {
00626 parent::get();
00627 $sql="SELECT j_id, j_date, j_montant, j_poste, j_grpt, j_rapt, j_jrn_def,
00628 j_debit, j_text, j_centralized, j_internal, j_tech_user, j_tech_date,
00629 j_tech_per, j_qcode
00630 FROM jrnx where j_grpt = $1 order by j_debit desc,j_poste";
00631 $this->det->array=$this->db->get_array($sql,array($this->det->jr_grpt_id));
00632 }
00633 }
00634
00635
00636
00637
00638
00639
00640 class Acc_Sold extends Acc_Detail
00641 {
00642 function __construct($p_cn,$p_jrid=0)
00643 {
00644 parent::__construct($p_cn,$p_jrid);
00645 $this->signature='VEN';
00646 $this->det=new stdClass();
00647 }
00648 function get()
00649 {
00650 parent::get();
00651 $sql="SELECT qs_id, qs_internal, qs_fiche, qs_quantite, qs_price, qs_vat,
00652 qs_vat_code, qs_client, qs_valid, j_id,j_text,qs_vat_sided
00653 FROM quant_sold join jrnx using(j_id) where j_grpt=$1";
00654 $this->det->array=$this->db->get_array($sql,array($this->det->jr_grpt_id));
00655 }
00656
00657 }
00658
00659
00660
00661
00662
00663
00664
00665 class Acc_Purchase extends Acc_Detail
00666 {
00667 function __construct($p_cn,$p_jrid=0)
00668 {
00669 parent::__construct($p_cn,$p_jrid);
00670 $this->signature='ACH';
00671 }
00672
00673 function get()
00674 {
00675 parent::get();
00676 $sql="SELECT qp_id, qp_internal, j_id, qp_fiche, qp_quantite, qp_price, qp_vat,
00677 qp_vat_code, qp_nd_amount, qp_nd_tva, qp_nd_tva_recup, qp_supplier,
00678 qp_valid, qp_dep_priv,j_text,qp_vat_sided
00679 FROM quant_purchase join jrnx using(j_id) where j_grpt=$1";
00680 $this->det->array=$this->db->get_array($sql,array($this->det->jr_grpt_id));
00681 }
00682 }
00683
00684
00685
00686
00687
00688
00689 class Acc_Fin extends Acc_Detail
00690 {
00691 function __construct($p_cn,$p_jrid=0)
00692 {
00693 parent::__construct($p_cn,$p_jrid);
00694 $this->signature='FIN';
00695 }
00696
00697 function get()
00698 {
00699 parent::get();
00700 $sql="SELECT qf_id, qf_bank, jr_id, qf_other, qf_amount
00701 FROM quant_fin where jr_id = $1";
00702 $this->det->array=$this->db->get_array($sql,array($this->jr_id));
00703 }
00704 }