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
00565 function insert_related_action($p_string)
00566 {
00567 if ($p_string == "") return;
00568 $a_action=explode(',',$p_string);
00569 for ($i=0;$i<count($a_action);$i++)
00570 {
00571 $action = new Follow_Up($this->db,$a_action[$i]);
00572 $action->operation=$this->jr_id;
00573 $action->insert_operation();
00574 }
00575 }
00576 static function test_me()
00577 {
00578 $_SESSION['g_user']='phpcompta';
00579 $_SESSION['g_pass']='dany';
00580 global $g_user;
00581 $cn=new Database(dossier::id());
00582 $g_user=new User($cn);
00583 $a=new Acc_Operation($cn);
00584 $a->jr_id=1444;
00585 $b=$a->get_quant();
00586 var_dump($b);
00587 }
00588 }
00589
00590 class Acc_Detail extends Acc_Operation
00591 {
00592 function __construct($p_cn,$p_jrid=0)
00593 {
00594 parent::__construct($p_cn);
00595 $this->jr_id=$p_jrid;
00596 $this->det=new stdClass();
00597 }
00598
00599
00600
00601
00602 function get()
00603 {
00604 $sql="SELECT jr_id, jr_def_id, jr_montant, jr_comment, jr_date, jr_grpt_id,
00605 jr_internal, jr_tech_date, jr_tech_per, jrn_ech, jr_ech, jr_rapt,jr_ech,
00606 jr_valid, jr_opid, jr_c_opid, jr_pj, jr_pj_name, jr_pj_type,
00607 jr_pj_number, jr_mt,jr_rapt,jr_date_paid
00608 FROM jrn where jr_id=$1";
00609 $array=$this->db->get_array($sql,array($this->jr_id));
00610 if ( count($array) == 0 ) throw new Exception('Aucune ligne trouvée');
00611 foreach ($array[0] as $key=>$val)
00612 {
00613 $this->det->$key=$val;
00614 }
00615 $sql="select n_text from jrn_note where jr_id=$1";
00616 $this->det->note=$this->db->get_value($sql,array($this->jr_id));
00617 $this->det->note=strip_tags($this->det->note);
00618 }
00619 }
00620
00621
00622
00623
00624
00625
00626 class Acc_Misc extends Acc_Detail
00627 {
00628 var $signature;
00629 var $array;
00630 function __construct($p_cn,$p_jrid=0)
00631 {
00632 parent::__construct($p_cn,$p_jrid);
00633 $this->signature='ODS';
00634 $this->det=new stdClass();
00635 }
00636 function get()
00637 {
00638 parent::get();
00639 $sql="SELECT j_id, j_date, j_montant, j_poste, j_grpt, j_rapt, j_jrn_def,
00640 j_debit, j_text, j_centralized, j_internal, j_tech_user, j_tech_date,
00641 j_tech_per, j_qcode
00642 FROM jrnx where j_grpt = $1 order by j_debit desc,j_poste";
00643 $this->det->array=$this->db->get_array($sql,array($this->det->jr_grpt_id));
00644 }
00645 }
00646
00647
00648
00649
00650
00651
00652 class Acc_Sold extends Acc_Detail
00653 {
00654 function __construct($p_cn,$p_jrid=0)
00655 {
00656 parent::__construct($p_cn,$p_jrid);
00657 $this->signature='VEN';
00658 $this->det=new stdClass();
00659 }
00660 function get()
00661 {
00662 parent::get();
00663 $sql="SELECT qs_id, qs_internal, qs_fiche, qs_quantite, qs_price, qs_vat,
00664 qs_vat_code, qs_client, qs_valid, j_id,j_text,qs_vat_sided
00665 FROM quant_sold join jrnx using(j_id) where j_grpt=$1";
00666 $this->det->array=$this->db->get_array($sql,array($this->det->jr_grpt_id));
00667 }
00668
00669 }
00670
00671
00672
00673
00674
00675
00676
00677 class Acc_Purchase extends Acc_Detail
00678 {
00679 function __construct($p_cn,$p_jrid=0)
00680 {
00681 parent::__construct($p_cn,$p_jrid);
00682 $this->signature='ACH';
00683 }
00684
00685 function get()
00686 {
00687 parent::get();
00688 $sql="SELECT qp_id, qp_internal, j_id, qp_fiche, qp_quantite, qp_price, qp_vat,
00689 qp_vat_code, qp_nd_amount, qp_nd_tva, qp_nd_tva_recup, qp_supplier,
00690 qp_valid, qp_dep_priv,j_text,qp_vat_sided
00691 FROM quant_purchase join jrnx using(j_id) where j_grpt=$1";
00692 $this->det->array=$this->db->get_array($sql,array($this->det->jr_grpt_id));
00693 }
00694 }
00695
00696
00697
00698
00699
00700
00701 class Acc_Fin extends Acc_Detail
00702 {
00703 function __construct($p_cn,$p_jrid=0)
00704 {
00705 parent::__construct($p_cn,$p_jrid);
00706 $this->signature='FIN';
00707 }
00708
00709 function get()
00710 {
00711 parent::get();
00712 $sql="SELECT qf_id, qf_bank, jr_id, qf_other, qf_amount
00713 FROM quant_fin where jr_id = $1";
00714 $this->det->array=$this->db->get_array($sql,array($this->jr_id));
00715 }
00716 }