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_iselect.php");
00028 require_once ('class_database.php');
00029 require_once ('class_dossier.php');
00030 require_once ('class_impress.php');
00031 require_once ('header_print.php');
00032 require_once ('class_acc_account_ledger.php');
00033
00034
00035
00036
00037
00038 class Acc_Bilan
00039 {
00040 var $db;
00041 var $b_id;
00042 var $from;
00043 var $to;
00044
00045 function Acc_Bilan($p_cn)
00046 {
00047 $this->db=$p_cn;
00048 }
00049
00050
00051
00052
00053
00054
00055
00056 function display_form($p_filter_year="")
00057 {
00058 $r="";
00059 $r.=dossier::hidden();
00060 $r.= '<TABLE>';
00061
00062 $r.='<TR>';
00063
00064 $w=new ISelect();
00065 $w->table=1;
00066
00067 $periode_start=$this->db->make_array("select p_id,to_char(p_start,'DD-MM-YYYY') from parm_periode $p_filter_year order by p_start,p_end");
00068
00069 $periode_end=$this->db->make_array("select p_id,to_char(p_end,'DD-MM-YYYY') from parm_periode $p_filter_year order by p_end,p_start");
00070
00071 $w->label=_("Depuis");
00072 $w->value=$this->from;
00073 $w->selected=$this->from;
00074 $r.= td($w->input('from_periode',$periode_start));
00075 $w->label=_(" jusque ");
00076 $w->value=$this->to;
00077 $w->selected=$this->to;
00078 $r.= td($w->input('to_periode',$periode_end));
00079 $r.= "</TR>";
00080 $r.="<tr>";
00081 $mod=new ISelect();
00082 $mod->table=1;
00083 $mod->value=$this->db->make_array("select b_id, b_name from bilan order by b_name");
00084 $mod->label=_("Choix du bilan");
00085 $r.=td($mod->input('b_id'));
00086 $r.="</tr>";
00087 $r.= '</TABLE>';
00088 return $r;
00089 }
00090
00091
00092
00093
00094
00095 private function warning($p_message,$p_type,$p_deb)
00096 {
00097 $sql="select pcm_val,pcm_lib from tmp_pcmn where pcm_type='$p_type'";
00098 $res=$this->db->exec_sql($sql);
00099 if ( Database::num_row($res) ==0 )
00100 return;
00101 $count=0;
00102 $nRow=Database::num_row($res);
00103
00104 $ret="";
00105 $obj=new Acc_Account_Ledger($this->db,0);
00106 for ($i=0;$i<$nRow;$i++)
00107 {
00108
00109 $line=Database::fetch_array($res,$i);
00110
00111 $sql=sql_filter_per($this->db,$this->from,$this->to,'p_id','j_tech_per');
00112 $obj->id=$line['pcm_val'];
00113
00114 $solde=$obj->get_solde_detail($sql);
00115 $solde_signed=$solde['debit']-$solde['credit'];
00116
00117 if (
00118 ($solde_signed < 0 && $p_deb == 'D' ) ||
00119 ($solde_signed > 0 && $p_deb == 'C' )
00120 )
00121 {
00122 $ret.= '<li>Anomalie pour le compte '.$line['pcm_val'].
00123 $line['pcm_lib'].
00124 " D: ".$solde['debit'].
00125 " C: ".$solde['credit']." diff ".$solde['solde'];
00126 $count++;
00127 }
00128
00129 }
00130
00131 echo '<fieldset>';
00132 echo '<legend>'.$p_message.'</legend>';
00133 if ( $count <> 0 )
00134 {
00135 echo '<ol>'.$ret.'</ol>';
00136 echo '<span class="error">Nbres anomalies : '.$count.'</span>';
00137 }
00138 else
00139 echo " Pas d'anomalie détectée";
00140 echo '</fieldset>';
00141
00142
00143 }
00144
00145 function verify()
00146 {
00147 bcscale(2);
00148 echo '<h3> Comptes normaux </h3>';
00149 $this->warning('Actif avec un solde crediteur','ACT','D');
00150 $this->warning('Passif avec un solde debiteur','PAS','C');
00151 $this->warning('Compte de resultat : Charge avec un solde crediteur','CHA','D');
00152 $this->warning('Compte de resultat : produit avec un solde debiteur','PRO','C');
00153 echo '<hr>';
00154 echo '<h3> Comptes inverses </h3>';
00155 $this->warning('Compte inverse : actif avec un solde debiteur','ACTINV','C');
00156 $this->warning('Compte inverse : passif avec un solde crediteur','PASINV','D');
00157 $this->warning('Compte inverse : Charge avec un solde debiteur','CHAINV','C');
00158 $this->warning('Compte inverse : produit avec un solde crediteur','PROINV','D');
00159 echo '<h3>Solde </h3>';
00160
00161 $sql_periode=sql_filter_per($this->db,$this->from,$this->to,'p_id','j_tech_per');
00162
00163 $sql="select sum(j_montant) from jrnx join tmp_pcmn on (j_poste=pcm_val)".
00164 " where j_debit='t' and (pcm_type='ACT' or pcm_type='ACTINV')";
00165 $sql.="and $sql_periode";
00166 $debit_actif=$this->db->get_value($sql);
00167
00168
00169 $sql="select sum(j_montant) from jrnx join tmp_pcmn on (j_poste=pcm_val)".
00170 " where j_debit='f' and (pcm_type='ACT' or pcm_type='ACTINV')";
00171
00172 $sql.="and $sql_periode";
00173
00174 $credit_actif=$this->db->get_value($sql);
00175 $total_actif=abs(bcsub($debit_actif,$credit_actif));
00176 echo '<table >';
00177 echo tr(td( 'Total actif ').td($total_actif,'style="text-align:right"'));
00178
00179
00180 $sql="select sum(j_montant) from jrnx join tmp_pcmn on (j_poste=pcm_val)".
00181 " where j_debit='t' and (pcm_type='PAS' or pcm_type='PASINV') ";
00182 $sql.="and $sql_periode";
00183
00184 $debit_passif=$this->db->get_value($sql);
00185
00186
00187 $sql="select sum(j_montant) from jrnx join tmp_pcmn on (j_poste=pcm_val)".
00188 " where j_debit='f' and (pcm_type='PAS' or pcm_type='PASINV') ";
00189 $sql.="and $sql_periode";
00190 $credit_passif=$this->db->get_value($sql);
00191 $total_passif=abs(bcsub($debit_passif,$credit_passif));
00192
00193
00194 echo tr(td('Total passif ').td($total_passif,'style="text-align:right"'));
00195 if ( $total_actif != $total_passif )
00196 {
00197 $diff=bcsub($total_actif,$total_passif);
00198 echo tr(td(' Difference Actif - Passif ').td($diff,'style="text-align:right"'),'style="font-weight:bolder"');
00199 }
00200
00201
00202 $sql="select sum(j_montant) from jrnx join tmp_pcmn on (j_poste=pcm_val)".
00203 " where j_debit='t' and (pcm_type='CHA' or pcm_type='CHAINV')";
00204 $sql.="and $sql_periode";
00205 $debit_charge=$this->db->get_value($sql);
00206
00207
00208 $sql="select sum(j_montant) from jrnx join tmp_pcmn on (j_poste=pcm_val)".
00209 " where j_debit='f' and (pcm_type='CHA' or pcm_type='CHAINV')";
00210 $sql.="and $sql_periode";
00211 $credit_charge=$this->db->get_value($sql);
00212 $total_charge=abs(bcsub($debit_charge,$credit_charge));
00213 echo tr(td('Total charge ').td($total_charge,'style="text-align:right"'));
00214
00215
00216
00217 $sql="select sum(j_montant) from jrnx join tmp_pcmn on (j_poste=pcm_val)".
00218 " where j_debit='t' and (pcm_type='PRO' or pcm_type='PROINV')";
00219 $sql.="and $sql_periode";
00220 $debit_pro=$this->db->get_value($sql);
00221
00222
00223 $sql="select sum(j_montant) from jrnx join tmp_pcmn on (j_poste=pcm_val)".
00224 " where j_debit='f' and (pcm_type='PRO' or pcm_type='PROINV')";
00225 $sql.="and $sql_periode";
00226 $credit_pro=$this->db->get_value($sql);
00227 $total_pro=abs(bcsub($debit_pro,$credit_pro));
00228 echo tr(td('Total produit ').td($total_pro,'style="text-align:right"'));
00229
00230 $diff=bcsub($total_pro,$total_charge);
00231
00232 echo tr( td("Difference Produit - Charge",'style="padding-right:20px"').td($diff,'style="text-align:right"'),'style="font-weight:bolder"');
00233 echo '</table>';
00234 }
00235
00236
00237
00238
00239 function get_request_get()
00240 {
00241 $this->b_id=(isset($_GET['b_id']))?$_GET['b_id']:"";
00242 $this->from=( isset ($_GET['from_periode']))?$_GET['from_periode']:-1;
00243 $this->to=( isset ($_GET['to_periode']))?$_GET['to_periode']:-1;
00244 }
00245
00246 function load()
00247 {
00248 try
00249 {
00250 if ( $this->b_id=="")
00251 throw new Exception("le formulaire id n'est pas donnee");
00252
00253 $sql="select b_name,b_file_template,b_file_form,lower(b_type) as b_type from bilan where".
00254 " b_id = ".$this->b_id;
00255 $res=$this->db->exec_sql($sql);
00256
00257 if ( Database::num_row($res)==0)
00258 throw new Exception ('Aucun enregistrement trouve');
00259 $array=Database::fetch_array($res,0);
00260 foreach ($array as $name=>$value)
00261 $this->$name=$value;
00262
00263 }
00264 catch(Exception $Ex)
00265 {
00266 echo $Ex->getMessage();
00267 exit();
00268 }
00269 }
00270
00271
00272 function file_open_form()
00273 {
00274 $form=fopen($this->b_file_form,'r');
00275 if ( $form == false)
00276 {
00277 echo 'Cannot Open';
00278 exit();
00279 }
00280 return $form;
00281 }
00282
00283
00284 function file_open_template()
00285 {
00286 $templ=fopen($this->b_file_template,'r');
00287 if ( $templ == false)
00288 {
00289 echo 'Cannot Open';
00290 exit();
00291 }
00292 return $templ;
00293
00294 }
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 function compute_formula($p_handle)
00305 {
00306 while (! feof ($p_handle))
00307 {
00308 $buffer=trim(fgets($p_handle));
00309
00310
00311
00312 if (strlen(trim($buffer))==0)
00313 continue;
00314
00315 if ( strpos($buffer,'#') === true )
00316 continue;
00317
00318
00319 $a=Impress::parse_formula($this->db,"$buffer",$buffer,$this->from,$this->to,false);
00320 $b=str_replace("$","\$this->",$a);
00321 if ( eval("$b;") === false )
00322 echo_debug(__FILE__,__LINE__,"Code failed with $b");
00323
00324
00325 }
00326 }
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336 function generate_odt()
00337 {
00338
00339 $dirname=tempnam($_ENV['TMP'],'bilan_');
00340
00341
00342 unlink($dirname);
00343 mkdir ($dirname);
00344 chdir($dirname);
00345
00346 $file_base=dirname($_SERVER['SCRIPT_FILENAME']).DIRECTORY_SEPARATOR.$this->b_file_template;
00347 $work_file=basename($file_base);
00348 if ( copy ($file_base,$work_file) == false )
00349 {
00350 echo "Je ne peux pas ouvrir ce fichier ";
00351 exit();
00352 }
00353 ob_start();
00354
00355 $zip = new Zip_Extended;
00356 if ($zip->open($work_file) === TRUE)
00357 {
00358 $zip->extractTo($dirname.DIRECTORY_SEPARATOR);
00359 $zip->close();
00360 } else
00361 {
00362 echo __FILE__.":".__LINE__."cannot unzip model ".$filename;
00363 }
00364
00365 ob_end_clean();
00366 unlink($work_file);
00367
00368 $p_file=fopen('content.xml','r');
00369
00370 if ( $p_file == false)
00371 {
00372 echo 'Cannot Open';
00373 exit();
00374 }
00375
00376 $r="";
00377 $regex="/<<\\$[A-Z]*[0-9]*>>/";
00378 $lt="<";
00379 $gt=">";
00380 $header_txt=header_txt($this->db);
00381
00382 while ( !feof($p_file) )
00383 {
00384 $line_rtf=fgets($p_file);
00385
00386
00387
00388
00389 $line_rtf=preg_replace('/<<header>>/',$header_txt,$line_rtf);
00390
00391
00392
00393 $tmp="";
00394
00395
00396 while (preg_match_all($regex,$line_rtf,$f2) > 0 )
00397 {
00398
00399 foreach ($f2 as $f2_array)
00400 {
00401 foreach ($f2_array as $f2_str)
00402 {
00403 $to_remove=$f2_str;
00404 $f2_value=str_replace("<","",$f2_str);
00405 $f2_value=str_replace(">","",$f2_value);
00406 $f2_value=str_replace("$","",$f2_value);
00407
00408
00409
00410
00411 if( ! isset($this->$f2_value))
00412 {
00413
00414 $a = "!!".$f2_value."!!";
00415 if( substr($f2_value, 0, 1) == "N" )
00416 {
00417 $ret = $this->db->get_array("SELECT pcm_lib AS acct_name FROM tmp_pcmn WHERE pcm_val::text LIKE ".
00418 " substr($1, 2)||'%' ORDER BY pcm_val ASC LIMIT 1",array($f2_value));
00419 if($ret[0]['acct_name'])
00420 {
00421 $a = $ret[0]['acct_name'];
00422 $a=str_replace('<','<',$a);
00423 $a=str_replace('>','>',$a);
00424 }
00425 }
00426 }
00427 else
00428 {
00429 $a=$this->$f2_value;
00430 }
00431 if ( $a=='-0' ) $a=0;
00432
00433
00434 if ( is_numeric($a) )
00435 {
00436 $searched='office:value-type="string"><text:p>'.$f2_str;
00437 $replaced='office:value-type="float" office:value="'.$a.'"><text:p>'.$f2_str;
00438 $line_rtf=str_replace($searched, $replaced, $line_rtf);
00439 }
00440
00441
00442 $line_rtf=str_replace($f2_str,$a,$line_rtf);
00443
00444 }
00445 }
00446 }
00447 $r.=$line_rtf;
00448
00449 }
00450
00451 return $r;
00452
00453 }
00454
00455
00456
00457
00458
00459 function generate_plain($p_file)
00460 {
00461 $r="";
00462 if ( $this->b_type=='html')
00463 {
00464 $lt='<';
00465 $gt='>';
00466 $pattern='/<<header>>/';
00467 }
00468 else
00469 {
00470 $lt='<';
00471 $gt='>';
00472 $pattern='/<<header>>/';
00473 }
00474
00475 $header_txt=header_txt($this->db);
00476
00477 while ( !feof($p_file) )
00478 {
00479 $line_rtf=fgets($p_file);
00480
00481 $line_rtf=preg_replace($pattern,$header_txt,$line_rtf);
00482
00483
00484
00485 if (preg_match_all("/".$lt.$lt."\\$[a-zA-Z]*[0-9]*".$gt.$gt."/",$line_rtf,$f2) > 0)
00486 {
00487
00488
00489
00490 foreach ($f2 as $f2_str)
00491 {
00492
00493
00494
00495 $f2_value=str_replace($lt,"",$f2_str);
00496 $f2_value=str_replace($gt,"",$f2_value);
00497 $f2_value=str_replace("$","",$f2_value);
00498 $f2_value=$f2_value[0];
00499
00500
00501 if( ! isset($this->$f2_value))
00502 {
00503 $a = "!!".$f2_value."!!";
00504 if( substr($f2_value, 0, 1) == "N" )
00505 {
00506 $ret = $this->db->get_array("SELECT pcm_lib AS acct_name FROM tmp_pcmn WHERE ".
00507 " pcm_val::text LIKE substr($1, 2)||'%' ORDER BY pcm_val ASC LIMIT 1",
00508 array($f2_value));
00509 if($ret[0]['acct_name'])
00510 {
00511
00512 $a = utf8_decode($ret[0]['acct_name']);
00513 }
00514 }
00515 }
00516 else
00517 {
00518
00519
00520
00521 $a=$this->$f2_value;
00522 }
00523
00524 if ( $a=='-0' ) $a=0;
00525 $line_rtf=str_replace($f2_str,$a,$line_rtf);
00526
00527 }
00528 }
00529 $r.=$line_rtf;
00530
00531 }
00532
00533
00534
00535 return $r;
00536
00537
00538
00539
00540 }
00541
00542
00543 function generate()
00544 {
00545
00546 $this->load();
00547
00548 $form=$this->file_open_form();
00549
00550
00551 $this->compute_formula($form);
00552 fclose($form);
00553
00554 $templ=$this->file_open_template();
00555 switch ($this->b_type)
00556 {
00557 case 'rtf':
00558 $result=$this->generate_plain($templ);
00559 $this->send($result);
00560 break;
00561 case 'txt':
00562 $result=$this->generate_plain($templ);
00563 $this->send($result);
00564 case 'html':
00565 $result=$this->generate_plain($templ);
00566 $this->send($result);
00567
00568 break;
00569 case 'odt':
00570 case 'ods':
00571 $result=$this->generate_odt($templ);
00572 $this->send($result);
00573 break;
00574
00575 }
00576 fclose($templ);
00577 }
00578
00579
00580
00581 function send($p_result)
00582 {
00583 switch ($this->b_type)
00584 {
00585 case 'rtf':
00586
00587 header('Content-type: application/rtf');
00588 header('Content-Disposition: attachment; filename="'.$this->b_name.'.rtf"');
00589 echo $p_result;
00590 break;
00591
00592 case 'txt':
00593
00594 header('Content-type: application/txt');
00595 header('Content-Disposition: attachment; filename="'.$this->b_name.'.txt"');
00596
00597 echo $p_result;
00598 break;
00599 case 'html':
00600
00601 header('Content-type: application/html');
00602 header('Content-Disposition: attachment; filename="'.$this->b_name.'.html"');
00603
00604 echo $p_result;
00605 break;
00606 case 'odt':
00607 case 'ods':
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618 ob_start();
00619
00620
00621 $dirname=tempnam($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'tmp','bilan_');
00622
00623
00624 unlink($dirname);
00625 mkdir ($dirname);
00626 chdir($dirname);
00627
00628 $file_base=dirname($_SERVER['SCRIPT_FILENAME']).DIRECTORY_SEPARATOR.$this->b_file_template;
00629 $work_file=basename($file_base);
00630 if ( copy ($file_base,$work_file) == false )
00631 {
00632 echo "Je ne peux pas ouvrir ce fichier ";
00633 exit();
00634 }
00635
00636
00637
00638 ob_start();
00639 $zip = new Zip_Extended;
00640 if ($zip->open($work_file) === TRUE)
00641 {
00642 $zip->extractTo($dirname.DIRECTORY_SEPARATOR);
00643 $zip->close();
00644 }
00645 else
00646 {
00647 echo __FILE__.":".__LINE__."cannot unzip model ".$filename;
00648 }
00649
00650
00651 unlink ($work_file);
00652
00653
00654
00655 $p_file=fopen($dirname.DIRECTORY_SEPARATOR.'content.xml','wb');
00656 if ( $p_file == false )
00657 {
00658 exit('Je ne peux pas ouvrir content.xml');
00659
00660 }
00661 $a=fwrite($p_file,$p_result);
00662 if ( $a==false)
00663 {
00664 echo "Je ne peux pas ecrire dans content.xml";
00665 exit();
00666 }
00667
00668 $zip = new Zip_Extended;
00669 $res = $zip->open($this->b_name.".".$this->b_type, ZipArchive::CREATE);
00670 if($res !== TRUE)
00671 {
00672 echo __FILE__.":".__LINE__."cannot recreate zip";
00673 exit;
00674 }
00675 $zip->add_recurse_folder($dirname.DIRECTORY_SEPARATOR);
00676 $zip->close();
00677
00678 ob_end_clean();
00679 fclose($p_file);
00680 $fdoc=fopen($dirname.DIRECTORY_SEPARATOR.$this->b_name.'.'.$this->b_type,'r');
00681 if ( $fdoc == false )
00682 {
00683 exit('Je ne peux pas ouvrir ce document');
00684 }
00685 $buffer=fread ($fdoc,filesize($dirname.DIRECTORY_SEPARATOR.$this->b_name.'.'.$this->b_type));
00686 echo $buffer;
00687
00688 break;
00689
00690 }
00691
00692 }
00693 static function test_me()
00694 {
00695
00696 if ( isset($_GET['result']))
00697 {
00698 ob_start();
00699 $cn=new Database(dossier::id());
00700 $a=new Acc_Bilan($cn);
00701 $a->get_request_get();
00702
00703 $a->load();
00704 $form=$a->file_open_form();
00705 $a->compute_formula($form);
00706 fclose($form);
00707
00708 $templ=$a->file_open_template();
00709 $r=$a->generate_odt($templ);
00710 fclose($templ);
00711 ob_end_clean();
00712
00713 $a->send($r);
00714 }
00715 else
00716 {
00717 $cn=new Database(dossier::id());
00718 $a=new Acc_Bilan($cn);
00719 $a->get_request_get();
00720
00721 echo '<form method="get">';
00722 echo $a->display_form();
00723 echo HtmlInput::hidden('test_select',$_GET['test_select']).dossier::hidden();
00724 echo HtmlInput::submit('result','Sauve');
00725 echo '</form>';
00726 }
00727 }
00728 }
00729