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