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_ihidden.php");
00027 require_once ('class_database.php');
00028 require_once ('class_dossier.php');
00029
00030 class Acc_Account_Ledger
00031 {
00032 var $db;
00033 var $id;
00034 var $label;
00035 var $parent;
00036 var $row;
00037 var $tot_deb;
00038 var $tot_cred;
00039 function __construct ($p_cn,$p_id)
00040 {
00041 $this->db=$p_cn;
00042 $this->id=$p_id;
00043 }
00044
00045
00046
00047
00048
00049 private function get_row_sql($Res)
00050 {
00051 $array=array();
00052 $tot_cred=0.0;
00053 $tot_deb=0.0;
00054 $Max=Database::num_row($Res);
00055 if ( $Max == 0 ) return null;
00056 for ($i=0;$i<$Max;$i++)
00057 {
00058 $array[]=Database::fetch_array($Res,$i);
00059 if ($array[$i]['j_debit']=='t')
00060 {
00061 $tot_deb+=$array[$i]['deb_montant'] ;
00062 }
00063 else
00064 {
00065 $tot_cred+=$array[$i]['cred_montant'] ;
00066 }
00067 }
00068 $this->row=$array;
00069 $this->tot_deb=$tot_deb;
00070 $this->tot_cred=$tot_cred;
00071 return array($array,$tot_deb,$tot_cred);
00072
00073 }
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 function get_row($p_from,$p_to)
00084 {
00085 $periode=sql_filter_per($this->db,$p_from,$p_to,'p_id','jr_tech_per');
00086
00087 $Res=$this->db->exec_sql("select distinct j_id,jr_id,to_char(j_date,'DD.MM.YYYY') as j_date_fmt,j_date,".
00088 "case when j_debit='t' then j_montant else 0 end as deb_montant,".
00089 "case when j_debit='f' then j_montant else 0 end as cred_montant,".
00090 " jr_comment as description,jrn_def_name as jrn_name,".
00091 "j_debit, jr_internal,jr_pj_number ".
00092 " from jrnx left join jrn_def on jrn_def_id=j_jrn_def ".
00093 " left join jrn on jr_grpt_id=j_grpt".
00094 " where j_poste=".$this->id." and ".$periode.
00095 " order by j_date");
00096 return $this->get_row_sql($Res);
00097 }
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 function get_row_date($p_from,$p_to,$let=0,$solded=0)
00111 {
00112 global $g_user;
00113 $filter_sql=$g_user->get_ledger_sql('ALL',3);
00114 $sql_let='';
00115 switch ($let)
00116 {
00117 case 0:
00118 break;
00119 case 1:
00120 $sql_let=' and j_id in (select j_id from letter_cred union select j_id from letter_deb)';
00121 break;
00122 case '2':
00123 $sql_let=' and j_id not in (select j_id from letter_cred union select j_id from letter_deb) ';
00124 break;
00125 }
00126 if ( $solded == 1)
00127 {
00128 $filter=str_replace('jrn_def_id','jr_def_id',$filter_sql);
00129 $bal_sql="select sum(amount_deb) as s_deb,sum(amount_cred) as s_cred, j_poste
00130 from (select case when j_debit='t' then j_montant else 0 end as amount_deb,
00131 case when j_debit='f' then j_montant else 0 end as amount_cred,
00132 j_poste
00133 from jrnx join jrn on (j_grpt = jr_grpt_id)
00134 where
00135 j_poste=$1 and
00136 $filter and
00137 ( to_date($2,'DD.MM.YYYY') <= j_date and
00138 to_date($3,'DD.MM.YYYY') >= j_date )) as signed_amount
00139 group by j_poste
00140 ";
00141 $r=$this->db->get_array($bal_sql,array($this->id,$p_from,$p_to));
00142 if ( $this->db->count() == 0 ) return array();
00143 if ($r[0]['s_deb']==$r[0]['s_cred']) return array();
00144 }
00145 $Res=$this->db->exec_sql("select jr_id,to_char(j_date,'DD.MM.YYYY') as j_date_fmt,j_date,".
00146 "case when j_debit='t' then j_montant else 0 end as deb_montant,".
00147 "case when j_debit='f' then j_montant else 0 end as cred_montant,".
00148 " case when j_text is null or j_text = '' then jr_comment
00149 else jr_comment||' '||j_text end
00150 as description,jrn_def_name as jrn_name,".
00151 "j_debit, jr_internal,jr_pj_number,
00152 coalesce(comptaproc.get_letter_jnt(j_id),-1) as letter ".
00153 ",pcm_lib ".
00154 ",jr_tech_per,p_exercice,jrn_def_name".
00155 " from jrnx left join jrn_def on (jrn_def_id=j_jrn_def )".
00156 " left join jrn on (jr_grpt_id=j_grpt)".
00157 " left join tmp_pcmn on (j_poste=pcm_val)".
00158 " left join parm_periode on (p_id=jr_tech_per) ".
00159 " where j_poste=$1 and ".
00160 " ( to_date($2,'DD.MM.YYYY') <= j_date and ".
00161 " to_date($3,'DD.MM.YYYY') >= j_date )".
00162 " and $filter_sql $sql_let ".
00163 " order by j_date,substring(jr_pj_number,'[0-9]+$') asc",array($this->id,$p_from,$p_to));
00164 return $this->get_row_sql($Res);
00165 }
00166
00167
00168
00169
00170
00171
00172 function get_name()
00173 {
00174 $ret=$this->db->exec_sql(
00175 "select pcm_lib from tmp_pcmn where
00176 pcm_val=$1",array($this->id));
00177 if ( Database::num_row($ret) != 0)
00178 {
00179 $r=Database::fetch_array($ret);
00180 $this->name=$r['pcm_lib'];
00181 }
00182 else
00183 {
00184 $this->name="Poste inconnu";
00185 }
00186 return $this->name;
00187 }
00188
00189
00190
00191 function do_exist()
00192 {
00193 $sql="select pcm_val from tmp_pcmn where pcm_val= $1";
00194 $ret=$this->db->exec_sql($sql,array($this->id));
00195 return Database::num_row($ret) ;
00196 }
00197
00198
00199
00200
00201 function load()
00202 {
00203 $ret=$this->db->exec_sql("select pcm_lib,pcm_val_parent from
00204 tmp_pcmn where pcm_val=$1",array($this->id));
00205 $r=Database::fetch_all($ret);
00206
00207 if ( ! $r ) return false;
00208 $this->label=$r[0]['pcm_lib'];
00209 $this->parent=$r[0]['pcm_val_parent'];
00210 return true;
00211
00212 }
00213
00214
00215
00216
00217 function get()
00218 {
00219 echo "OBSOLETE Acc_Account_Ledger->get(), a remplacer par Acc_Account_Ledger->load()";
00220 return $this->load();
00221 }
00222
00223
00224
00225
00226
00227
00228
00229
00230 function get_solde($p_cond=" true ")
00231 {
00232 $Res=$this->db->exec_sql("select sum(deb) as sum_deb, sum(cred) as sum_cred from
00233 ( select j_poste,
00234 case when j_debit='t' then j_montant else 0 end as deb,
00235 case when j_debit='f' then j_montant else 0 end as cred
00236 from jrnx join tmp_pcmn on j_poste=pcm_val
00237 where
00238 j_poste::text like ('$this->id'::text) and
00239 $p_cond
00240 ) as m ");
00241 $Max=Database::num_row($Res);
00242 if ($Max==0) return 0;
00243 $r=Database::fetch_array($Res,0);
00244
00245 return abs($r['sum_deb']-$r['sum_cred']);
00246 }
00247
00248
00249
00250
00251
00252
00253 function get_solde_detail($p_cond="")
00254 {
00255
00256 if ( $p_cond != "") $p_cond=" and ".$p_cond;
00257 $sql="select sum(deb) as sum_deb, sum(cred) as sum_cred from
00258 ( select j_poste,
00259 case when j_debit='t' then j_montant else 0 end as deb,
00260 case when j_debit='f' then j_montant else 0 end as cred
00261 from jrnx
00262 where
00263 j_poste::text like ('$this->id'::text)
00264 $p_cond
00265 ) as m ";
00266
00267 $Res=$this->db->exec_sql($sql);
00268 $Max=Database::num_row($Res);
00269
00270 if ($Max==0)
00271 {
00272 return array('debit'=>0,
00273 'credit'=>0,
00274 'solde'=>0) ;
00275 }
00276 $r=Database::fetch_array($Res,0);
00277
00278
00279 if ($r['sum_deb']=='')
00280 $r['sum_deb']=0.0;
00281 if ($r['sum_cred']=='')
00282 $r['sum_cred']=0.0;
00283
00284 return array('debit'=>$r['sum_deb'],
00285 'credit'=>$r['sum_cred'],
00286 'solde'=>abs($r['sum_deb']-$r['sum_cred']));
00287 }
00288
00289
00290
00291
00292
00293
00294
00295 function isTVA()
00296 {
00297
00298 $a_TVA=$this->db->get_array('select tva_poste
00299 from tva_rate');
00300 foreach ( $a_TVA as $line_tva)
00301 {
00302 if ( $line_tva['tva_poste'] == '' )
00303 continue;
00304 list($tva_deb,$tva_cred)=explode(',',$line_tva['tva_poste']);
00305 if ( $this->id == $tva_deb ||
00306 $this->id == $tva_cred )
00307 {
00308 return 1;
00309 }
00310 }
00311 return 0;
00312
00313 }
00314
00315
00316
00317
00318
00319
00320 function HtmlTable($p_array=null,$let=0 , $from_div=0)
00321 {
00322 if ( $p_array==null)$p_array=$_REQUEST;
00323 $this->get_name();
00324 list($array,$tot_deb,$tot_cred)=$this->get_row_date( $p_array['from_periode'],
00325 $p_array['to_periode'],$let
00326 );
00327
00328 if ( count($this->row ) == 0 )
00329 return -1;
00330
00331 $rep="";
00332
00333 echo '<h2 class="title">'.$this->id." ".$this->name.'</h2>';
00334 if ( $from_div == 0)
00335 echo "<TABLE class=\"resultfooter\" style=\"border-collapse:separate;margin:1%;width:98%;;border-spacing:0px 5px\">";
00336 else
00337 echo "<TABLE class=\"resultfooter\" style=\"border-collapse:separate;margin:1%;width:98%;;border-spacing:0px 2px\">";
00338 echo '<tbody>';
00339 echo "<TR>".
00340 "<TH style=\"text-align:left\"> Date</TH>".
00341 "<TH style=\"text-align:left\"> n° de pièce </TH>".
00342 "<TH style=\"text-align:left\"> Code interne </TH>".
00343 "<TH style=\"text-align:left\"> Description </TH>".
00344 "<TH style=\"text-align:right\"> Débit </TH>".
00345 "<TH style=\"text-align:right\"> Crédit </TH>".
00346 th('Prog.','style="text-align:right"').
00347 th('Let.','style="text-align:right"');
00348 "</TR>"
00349 ;
00350 $progress=0;$sum_deb=0;$sum_cred=0;
00351 bcscale(2);
00352 $old_exercice="";
00353 $idx=0;
00354 foreach ( $this->row as $op )
00355 {
00356 $vw_operation = sprintf('<A class="detail" style="text-decoration:underline;color:red" HREF="javascript:modifyOperation(\'%s\',\'%s\')" >%s</A>', $op['jr_id'], dossier::id(), $op['jr_internal']);
00357 $let = '';
00358 $html_let = "";
00359 if ($op['letter'] != -1)
00360 {
00361 $let = strtoupper(base_convert($op['letter'], 10, 36));
00362 $html_let = HtmlInput::show_reconcile($from_div, $let);
00363 }
00364 $tmp_diff=bcsub($op['deb_montant'],$op['cred_montant']);
00365
00366
00367
00368
00369 if ( $old_exercice != $op['p_exercice'])
00370 {
00371 if ( $old_exercice != '')
00372 {
00373 $progress=bcsub($sum_deb,$sum_cred);
00374 $side=" ".$this->get_amount_side($progress);
00375 echo "<TR style=\"font-weight:bold\">".
00376 "<TD></TD>".
00377 td('').
00378 "<TD></TD>".
00379 "<TD>Totaux</TD>".
00380 "<TD style=\"text-align:right\">".nbm($sum_deb)."</TD>".
00381 "<TD style=\"text-align:right\">".nbm($sum_cred)."</TD>".
00382 td(nbm(abs($progress)).$side,'style="text-align:right"').
00383 td('').
00384 "</TR>";
00385 $sum_cred=0;
00386 $sum_deb=0;
00387 $progress=0;
00388
00389 }
00390 }
00391 $progress=bcadd($progress,$tmp_diff);
00392 $side=" ".$this->get_amount_side($progress);
00393 $sum_cred=bcadd($sum_cred,$op['cred_montant']);
00394 $sum_deb=bcadd($sum_deb,$op['deb_montant']);
00395 if ($idx%2 == 0) $class='class="odd"'; else $class=' class="even"';
00396 $idx++;
00397
00398 echo "<TR $class name=\"tr_" . $let . "_" . $from_div . "\">" .
00399 "<TD>".smaller_date(format_date($op['j_date']))."</TD>".
00400 td(h($op['jr_pj_number'])).
00401 "<TD>".$vw_operation."</TD>".
00402 "<TD>".h($op['description'])."</TD>".
00403 "<TD style=\"text-align:right\">".nbm($op['deb_montant'])."</TD>".
00404 "<TD style=\"text-align:right\">".nbm($op['cred_montant'])."</TD>".
00405 td(nbm(abs($progress)).$side,'style="text-align:right"').
00406
00407 td($html_let, ' style="color:red;text-align:right"') .
00408 "</TR>";
00409 $old_exercice=$op['p_exercice'];
00410 }
00411 echo '<tfoot>';
00412 $solde_type=($sum_deb>$sum_cred)?"solde débiteur":"solde créditeur";
00413 $diff=bcsub($sum_deb,$sum_cred);
00414 $side=" ".$this->get_amount_side($diff);
00415 echo "<TR style=\"font-weight:bold\">".
00416 "<TD >Totaux</TD><td></td>".
00417 "<TD ></TD>".
00418 "<TD></TD>".
00419 "<TD style=\"text-align:right\">".nbm($sum_deb)."</TD>".
00420 "<TD style=\"text-align:right\">".nbm($sum_cred)."</TD>".
00421 "<TD style=\"text-align:right\">".nbm(abs($diff)).$side."</TD>".
00422
00423 "</TR>";
00424 echo "<tr><TD>$solde_type</TD><td></td>".
00425 "<TD style=\"text-align:right\">".nbm(abs($diff))."</TD>".
00426 "</TR>";
00427 echo '</tfoot>';
00428 echo '</tbody>';
00429
00430 echo "</table>";
00431
00432 return;
00433 }
00434
00435
00436
00437
00438
00439 function get_amount_side($p_amount)
00440 {
00441 if ($p_amount == 0)
00442 return "=";
00443 if ($p_amount < 0)
00444 return "C";
00445 if ($p_amount > 0)
00446 return "D";
00447 }
00448
00449
00450
00451
00452
00453 static function HtmlTableHeader($actiontarget="poste")
00454 {
00455 switch($actiontarget)
00456 {
00457 case 'poste':
00458 $action_csv='CSV:postedetail';
00459 $action_pdf='PDF:postedetail';
00460 break;
00461 case 'gl_comptes':
00462 $action_csv='CSV:glcompte';
00463 $action_pdf='PDF:glcompte';
00464 break;
00465 default:
00466 throw new Exception(" Fonction HtmlTableHeader argument actiontarget invalid");
00467 exit;
00468 }
00469 $hid=new IHidden();
00470
00471 echo "<table >";
00472 echo '<TR>';
00473 $str_ople=(isset($_REQUEST['ople']))?HtmlInput::hidden('ople',$_REQUEST['ople']):'';
00474 if ($actiontarget=='poste')
00475 {
00476 echo '<TD><form method="GET" ACTION="">'.
00477 dossier::hidden().
00478 HtmlInput::submit('bt_other',"Autre poste").
00479 $hid->input("type","poste").$hid->input('ac',$_REQUEST['ac'])."</form></TD>";
00480 }
00481
00482
00483 echo '<TD><form method="GET" ACTION="export.php">'.
00484 dossier::hidden().
00485 HtmlInput::submit('bt_pdf',"Export PDF").
00486 HtmlInput::hidden('act',$action_pdf).
00487 $hid->input("type","poste").$str_ople.
00488 $hid->input('p_action','impress').
00489 $hid->input("from_periode",$_REQUEST['from_periode']).
00490 $hid->input("to_periode",$_REQUEST['to_periode'])
00491 ;
00492
00493 if ( isset($_REQUEST['letter'] )) echo HtmlInput::hidden('letter','2');
00494 if ( isset($_REQUEST['solded'] )) echo HtmlInput::hidden('solded','1');
00495
00496 if (isset($_REQUEST['from_poste']))
00497 echo HtmlInput::hidden('from_poste',$_REQUEST['from_poste']);
00498
00499 if (isset($_REQUEST['to_poste']))
00500 echo HtmlInput::hidden('to_poste',$_REQUEST['to_poste']);
00501
00502 if (isset($_REQUEST['poste_id']))
00503 echo HtmlInput::hidden("poste_id",$_REQUEST['poste_id']);
00504
00505 if (isset($_REQUEST['poste_fille']))
00506 echo $hid->input('poste_fille','on');
00507 if (isset($_REQUEST['oper_detail']))
00508 echo $hid->input('oper_detail','on');
00509
00510 echo "</form></TD>";
00511
00512 echo '<TD><form method="GET" ACTION="export.php">'.
00513 dossier::hidden().
00514 HtmlInput::submit('bt_csv',"Export CSV").
00515 HtmlInput::hidden('act',$action_csv).
00516 $hid->input("type","poste").$str_ople.
00517 $hid->input('p_action','impress').
00518 $hid->input("from_periode",$_REQUEST['from_periode']).
00519 $hid->input("to_periode",$_REQUEST['to_periode']);
00520
00521 if (isset($_REQUEST['from_poste']))
00522 echo HtmlInput::hidden('from_poste',$_REQUEST['from_poste']);
00523
00524 if (isset($_REQUEST['to_poste']))
00525 echo HtmlInput::hidden('to_poste',$_REQUEST['to_poste']);
00526
00527 if (isset($_REQUEST['poste_id']))
00528 echo HtmlInput::hidden("poste_id",$_REQUEST['poste_id']);
00529
00530 if ( isset($_REQUEST['letter'] )) echo HtmlInput::hidden('letter','2');
00531 if ( isset($_REQUEST['solded'] )) echo HtmlInput::hidden('solded','1');
00532
00533 if (isset($_REQUEST['poste_fille']))
00534 echo $hid->input('poste_fille','on');
00535 if (isset($_REQUEST['oper_detail']))
00536 echo $hid->input('oper_detail','on');
00537 if (isset($_REQUEST['poste_id'])) echo $hid->input("poste_id",$_REQUEST['poste_id']);
00538
00539 echo "</form></TD>";
00540 echo "</form></TD>";
00541 echo '<td style="vertical-align:top">';
00542 echo HtmlInput::print_window();
00543 echo '</td>';
00544 echo '</tr>';
00545 echo "</table>";
00546
00547
00548 }
00549
00550
00551
00552
00553
00554 function belong_ledger($p_jrn)
00555 {
00556 $filter=$this->db->get_value("select jrn_def_class_cred from jrn_def where jrn_def_id=$p_jrn");
00557 if ( trim ($filter) == '')
00558 return 0;
00559
00560 $valid_cred=explode(" ",$filter);
00561 $sql="select count(*) as poste from tmp_pcmn where ";
00562
00563 $or="";
00564 $SqlFilter="";
00565 foreach ( $valid_cred as $item_cred)
00566 {
00567 if ( strlen (trim($item_cred)))
00568 {
00569 if ( strstr($item_cred,"*") == true )
00570 {
00571 $item_cred=strtr($item_cred,"*","%");
00572 $SqlItem="$or pcm_val::text like '".sql_string($item_cred)."'";
00573 $or=" or ";
00574 }
00575 else
00576 {
00577 $SqlItem="$or pcm_val::text = '".sql_string($item_cred)."' ";
00578 $or=" or ";
00579 }
00580 $SqlFilter=$SqlFilter.$SqlItem;
00581 }
00582 }
00583 $sql.=$SqlFilter." and pcm_val::text='".sql_string($this->id)."'";
00584 $max=$this->db->get_value($sql);
00585 if ($max > 0 )
00586 return 0;
00587 else
00588 return -1;
00589 }
00590
00591
00592
00593
00594
00595
00596 function get_account_ledger($p_jrn)
00597 {
00598 $l=new Acc_Ledger($this->db,$p_jrn);
00599 $row=$l->get_propertie();
00600 if ( strlen(trim($row['jrn_def_class_deb'])) == 0 ) return array();
00601 $valid_account=explode(" ",$row['jrn_def_class_deb']);
00602 return $valid_account;
00603 }
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614 function build_sql_account($p_jrn)
00615 {
00616 $array=$this->get_account_ledger($p_jrn);
00617 if ( empty($array) ) return "";
00618 $sql="";
00619 foreach ( $array as $item_cred)
00620 {
00621 if ( strlen (trim($item_cred))>0 )
00622 {
00623 if ( strstr($item_cred,"*") == true )
00624 {
00625 $item_cred=strtr($item_cred,"*","%");
00626 $sql_tmp=" pcm_val::text like '$item_cred' or";
00627 }
00628 else
00629 {
00630 $sql_tmp=" pcm_val::text = '$item_cred' or";
00631 }
00632 $sql.=$sql_tmp;
00633 }
00634 }
00635
00636 $sql=substr($sql,0,strlen($sql)-2);
00637 return $sql;
00638 }
00639
00640
00641
00642
00643 function find_card()
00644 {
00645 $sql="select f_id from fiche_detail where ad_id=$1 and ad_value=$2";
00646 $account=$this->db->get_array($sql,array(ATTR_DEF_ACCOUNT,$this->id));
00647 return $account;
00648 }
00649 static function test_me()
00650 {
00651 $cn=new Database(dossier::id());
00652 $a=new Acc_Account_Ledger($cn,550);
00653 echo ' Journal 4 '.$a->belong_ledger(4);
00654
00655 }
00656 }