Main Page | Namespace List | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

user_form_fin.php

Go to the documentation of this file.
00001 <?
00002 /*
00003  *   This file is part of PhpCompta.
00004  *
00005  *   PhpCompta is free software; you can redistribute it and/or modify
00006  *   it under the terms of the GNU General Public License as published by
00007  *   the Free Software Foundation; either version 2 of the License, or
00008  *   (at your option) any later version.
00009  *
00010  *   PhpCompta is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details.
00014  *
00015  *   You should have received a copy of the GNU General Public License
00016  *   along with PhpCompta; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 /* $Revision: 1.13 $ */
00020 // Copyright Author Dany De Bontridder ddebontridder@yahoo.fr
00021 require_once("class_widget.php");
00022 require_once("constant.php");
00023 require_once("preference.php");
00024 require_once("fiche_inc.php");
00025 require_once("user_common.php");
00042 function form_verify_input($p_cn,$p_jrn,$p_periode,$p_array,$p_number)
00043 {
00044   foreach ($p_array as $name=>$content) {
00045     ${"$name"}=$content;
00046   }
00047   // Verify the date
00048   if ( isDate($e_date) == null ) { 
00049           echo_error("Invalid date $e_date");
00050           echo_debug('user_form_fin.php',__LINE__,"Invalid date $e_date");
00051           echo "<SCRIPT> alert('INVALID DATE $e_date !!!!');</SCRIPT>";
00052           return null;
00053                 }
00054 
00055 
00056  // Check if the fiche is in the jrn
00057  if (IsFicheOfJrn($p_cn , $p_jrn, $e_bank_account,'deb') == 0 ) 
00058    {
00059      $msg="Mauvais compte en banque";
00060      echo_error($msg);echo_debug('user_form_fin.php',__LINE__,$msg);    
00061      echo "<SCRIPT>alert('$msg');</SCRIPT>";
00062      return null;
00063    }
00064  // Check if the card has a valid account
00065  if ( CheckPoste($p_cn,$e_bank_account) == null )
00066    return null;
00067 
00068  // check if all e_march are in fiche
00069   for ($i=0;$i<$p_number;$i++) {
00070     if ( trim(${"e_other$i"})  == "" ) {
00071       // nothing to do
00072       continue;
00073     }
00074         // Check amount
00075     if ( isNumber(${"e_other".$i."_amount"}) == 0) {
00076                 $msg="Montant invalide !!! ";
00077                 echo_error($msg); echo_debug('user_form_fin.php',__LINE__,$msg);        
00078                 echo "<SCRIPT>alert('$msg');</SCRIPT>";
00079                 return null;
00080         }
00081     // Check 
00082     if ( isFicheOfJrn($p_cn,$p_jrn,${"e_other$i"},'cred') == 0 ) {
00083       $msg="Fiche inexistante !!! ";
00084       echo_error($msg);echo_debug('user_form_fin.php',__LINE__,$msg);   
00085       echo "<SCRIPT>alert('$msg');</SCRIPT>";
00086       return null;
00087     }
00088     // Check if the card has a valid account
00089     if ( CheckPoste($p_cn,${"e_other".$i}) == null )
00090       return null;
00091   }
00092 // Verify the userperiode
00093 
00094 // p_periode contient la periode par default
00095   list ($l_date_start,$l_date_end)=GetPeriode($p_cn,$p_periode);
00096   
00097   // Date dans la periode active
00098   echo_debug ('user_form_fin',__LINE__,"date start periode $l_date_start date fin periode $l_date_end date demande $e_date");
00099   if ( cmpDate($e_date,$l_date_start)<0 || 
00100        cmpDate($e_date,$l_date_end)>0 )
00101     {
00102       $msg="Not in the active periode please change your preference";
00103       echo_error($msg); echo_error($msg);       
00104       echo "<SCRIPT>alert('$msg');</SCRIPT>";
00105       return null;
00106     }
00107     // Periode ferm� 
00108     if ( PeriodeClosed ($p_cn,$p_periode)=='t' )
00109       {
00110                 $msg="This periode is closed please change your preference";
00111                 echo_error($msg); echo_error($msg);     
00112                 echo "<SCRIPT>alert('$msg');</SCRIPT>";
00113                 return null;
00114       }
00115     return true;
00116 }
00117 
00118 
00135 function FormFin($p_cn,$p_jrn,$p_periode,$p_submit,$p_array=null,$pview_only=true,$p_item=4,$p_save=false)
00136 { 
00137   include_once("poste.php");
00138   if ( $p_array != null ) {
00139     // array contains old value
00140     foreach ( $p_array as $a=>$v) {
00141       ${"$a"}=$v;
00142     }
00143   }
00144   // The date
00145   list ($l_date_start,$l_date_end)=GetPeriode($p_cn,$p_periode);
00146   $flag=(isset($e_date))?1:0;
00147   $e_date=( ! isset($e_date) ) ? substr($l_date_start,2,8):$e_date;
00148 
00149   // Comment
00150   $e_comment=(isset($e_comment))?$e_comment:"";
00151 
00152   $r="";
00153   if ( $pview_only == false) {
00154     $r.=JS_SEARCH_CARD;
00155     $r.=JS_CONCERNED_OP;
00156   }
00157   // Compute href
00158   $href=basename($_SERVER['SCRIPT_NAME']);
00159   switch ($href)
00160     {
00161       // user_jrn.php module "Comptable"
00162     case 'user_jrn.php':
00163       $href="user_jrn.php?action=new&p_jrn=$p_jrn";
00164       break;
00165       // commercial.php  module "Gestion"
00166     case 'commercial.php':
00167       $href="commercial.php?p_action=bank&p_jrn=$p_jrn";
00168       break;
00169     default:
00170       echo_error('user_form_fin.php',__LINE__,'Erreur invalid request uri');
00171       exit (-1);
00172     }
00173 
00174   $r.="<FORM NAME=\"form_detail\" enctype=\"multipart/form-data\" ACTION=\"$href\" METHOD=\"POST\">";
00175   $r.='<TABLE>';
00176   $Date=new widget("text");
00177   $Date->SetReadOnly($pview_only);
00178   $Date->table=1;
00179   $r.="<tr>";
00180   $r.=$Date->IOValue("e_date",$e_date,"Date");
00181   $r.="</tr>";
00182 
00183 
00184   include_once("fiche_inc.php");
00185   $r.='<INPUT TYPE="HIDDEN" name="nb_item" value="'.$p_item.'">';
00186 
00187   // bank_account operation
00188   // Save old value and set a new one
00189   $e_bank_account=( isset ($e_bank_account) )?$e_bank_account:"";
00190   $e_bank_account_label="";  
00191 
00192     // retrieve e_bank_account_label
00193   if ( $e_bank_account != ""  ) {
00194       $a_client=GetFicheAttribut($p_cn,$e_bank_account);
00195       if ( $a_client != null)   
00196         $e_bank_account_label=$a_client['vw_name']."  adresse ".$a_client['vw_addr']."  ".$a_client['vw_cp'];
00197           }  
00198   //  search widget
00199     $W1=new widget("js_search");
00200     $W1->readonly=$pview_only;
00201     $W1->label="Banque";
00202     $W1->name="e_bank_account";
00203     $W1->value=$e_bank_account;
00204     $W1->extra=FICHE_TYPE_FIN;  // credits
00205     $W1->extra2=$p_jrn;
00206     $r.="<TR>".$W1->IOValue()."</TD>";
00207 
00208     $r.="</TABLE>";
00209   
00210     $Span=new widget ("span");
00211     $Span->SetReadOnly($pview_only);
00212     $r.="<TD>".$Span->IOValue("e_bank_account_label",$e_bank_account_label)."</TD>";
00213 
00214   
00215   // ComputeBanqueSaldo
00216   // 
00217   if ( $pview_only == true ) {
00218     $solde=GetSolde($p_cn,GetFicheAttribut($p_cn,$e_bank_account,ATTR_DEF_ACCOUNT));
00219     $r.=" <b> Solde = ".$solde." </b>";
00220     $new_solde=$solde;
00221   }
00222 
00223   // Start the div for item to move money
00224   $r.="<DIV>";
00225   $r.='<H2 class="info">Actions</H2>';
00226   $r.='<TABLE>';
00227   $r.="<TR>";
00228   $r.="<th></TH>";
00229   $r.="<th>code</TH>";
00230   $r.="<th>D&eacute;nomination</TH>";
00231   $r.="<th>Description</TH>";
00232   $r.="<th>Montant</TH>";
00233   $r.='<th colspan="2"> Op. Concern&eacute;</th>';
00234   $r.="</TR>";
00235   // Parse each " tiers" 
00236     for ($i=0; $i < $p_item; $i++) {
00237       $tiers=(isset(${"e_other".$i}))?${"e_other".$i}:"";
00238       $tiers_label="";
00239       $tiers_amount=(isset(${"e_other$i"."_amount"}))?${"e_other$i"."_amount"}:0;
00240   
00241       $tiers_comment=(isset (${"e_other$i"."_comment"}))?${"e_other$i"."_comment"}:"";
00242     // If $tiers has a value
00243     if ( $tiers != ""  ) 
00244         {
00245                 // retrieve the tva label and name
00246                 $a_fiche=GetFicheAttribut($p_cn, $tiers);
00247                 if ( $a_fiche != null ) {
00248                 $tiers_label=$a_fiche['vw_name'];
00249                 }
00250         }
00251     ${"e_other$i"."_amount"}=(isset (${"e_other$i"."_amount"}))?${"e_other$i"."_amount"}:0;
00252 
00253     $W1=new widget("js_search");
00254     $W1->label="";
00255     $W1->name="e_other".$i;
00256     $W1->value=$tiers;
00257     $W1->extra='cred';  // credits
00258     $W1->extra2=$p_jrn;
00259     $W1->readonly=$pview_only;
00260     $r.="<TR>".$W1->IOValue()."</TD>";
00261     // label
00262     $other=new widget("span");
00263     $r.="<TD>";
00264     $r.=$other->IOValue("e_other$i"."_label", $tiers_label);
00265     // Comment
00266     $wComment=new widget("text");
00267     $wComment->table=1;
00268     $wComment->SetReadOnly($pview_only);
00269     $r.=$wComment->IOValue("e_other$i"."_comment",$tiers_comment);
00270     // amount
00271     $wAmount=new widget("text");
00272     $wAmount->table=1;
00273     $wAmount->size=7;
00274     $wAmount->SetReadOnly($pview_only);
00275     $r.=$wAmount->IOValue("e_other$i"."_amount",$tiers_amount);
00276     // concerned
00277     ${"e_concerned".$i}=(isset(${"e_concerned".$i}))?${"e_concerned".$i}:"";
00278     $wConcerned=new widget("js_concerned");
00279     $wConcerned->SetReadOnly($pview_only);
00280     $r.=$wConcerned->IOValue("e_concerned".$i,${"e_concerned".$i});
00281     $r.='</TR>';
00282    // if not recorded the new amount must be recalculate
00283    // if recorded the old amount is recalculated
00284     if ( $pview_only == true)      
00285       $new_solde=($p_save==false)?$new_solde+$tiers_amount:$new_solde-$tiers_amount;
00286  }
00287 
00288 $r.="</TABLE>";
00289 
00290  if ( $pview_only==true && $p_save==false) {
00291    // check for upload piece
00292    $file=new widget("file");
00293    $file->table=1;
00294    $r.="<hr>";
00295    $r.= "<table>"; 
00296    $r.="<TR>".$file->IOValue("pj","","Pi&egrave;ce justificative")."</TR>";
00297    $r.="</table>";
00298    $r.="<hr>";
00299  }
00300  // Set correctly the REQUEST param for jrn_type 
00301  $h=new widget('hidden');
00302  $h->name='jrn_type';
00303  $h->value='FIN';
00304  $r.=$h->IOValue();
00305 
00306 $r.=$p_submit;
00307 $r.="</DIV>";
00308 $r.="</FORM>";
00309 
00310 // if view_only is true
00311 //Put the new saldo here (old saldo - operation)
00312  if ( $pview_only==true)  {
00313    // if not recorded the new amount must be recalculate
00314    if ( $p_save == false) {
00315      $r.=" <b> Ancien Solde = ".$solde." </b><br>";
00316      $r.=" <b> Nouveau Solde = ".$new_solde." </b><br>";
00317    }
00318    // if recorded the old amount is recalculated
00319    if ($p_save == true ) {
00320      $r.=" <b> Ancien Solde = ".$new_solde." </b><br>";
00321      $r.=" <b> Nouveau Solde = ".$solde." </b><br>";
00322    }
00323  }
00324  
00325 return $r;
00326 
00327 
00328 }
00329 
00347 function RecordFin($p_cn,$p_array,$p_user,$p_jrn) {
00348   $internal_code="";
00349   echo_debug('user_form_fin.php',__LINE__,"RecordFin");
00350   foreach ( $p_array as $v => $e)
00351   {
00352     ${"$v"}=$e;
00353   }
00354   // Get the default period
00355   $periode=$p_user->GetPeriode();
00356   
00357   // Debit = banque
00358   $poste_bq=GetFicheAttribut($p_cn,$e_bank_account,ATTR_DEF_ACCOUNT);
00359   StartSql($p_cn);
00360   $amount=0.0;  
00361   // Credit = goods 
00362   for ( $i = 0; $i < $nb_item;$i++) {
00363     // if tiers is set and amount != 0 insert it into the database 
00364     // and quit the loop ?
00365     if ( ${"e_other$i"."_amount"} == 0 ) continue;
00366     $poste=GetFicheAttribut($p_cn,${"e_other$i"},ATTR_DEF_ACCOUNT);
00367 
00368     $amount+=${"e_other$i"."_amount"};
00369     // Record a line for the bank
00370     // Compute the j_grpt
00371     $seq=NextSequence($p_cn,'s_grpt');
00372 
00373     if ( InsertJrnx($p_cn,'d',$p_user->id,$p_jrn,$poste_bq,$e_date,round(${"e_other$i"."_amount"},2),$seq,$periode) == false ) {
00374       $Rollback($p_cn);exit("error 'user_form_fin.php' __LINE__");
00375     }
00376 
00377 
00378     // Record a line for the other account
00379     if ( ($j_id=InsertJrnx($p_cn,'c',$p_user->id,$p_jrn,$poste,$e_date,round(${"e_other$i"."_amount"},2),$seq,$periode)) == false )
00380       { $Rollback($p_cn);exit("error 'user_form_fin.php' __LINE__");}
00381 
00382     echo_debug('user_form_fin.php',__LINE__,"   $j_id=InsertJrnx($p_cn,'d',$p_user,$p_jrn,$poste,$e_date,".${"e_other$i"}."_amount".",$seq,$periode);");
00383 
00384     if ( ($jr_id=InsertJrn($p_cn,$e_date,'',$p_jrn,FormatString(${"e_other$i"."_comment"}),
00385                            round(${"e_other$i"."_amount"},2),$seq,$periode))==false) {
00386       $Rollback($p_cn);exit("error 'user_form_fin.php' __LINE__");}
00387   
00388     if ( isNumber(${"e_concerned".$i}) == 1 ) {
00389 
00390       InsertRapt($p_cn,$jr_id,${"e_concerned$i"});
00391     }
00392 
00393 
00394   // Set Internal code and Comment
00395     $internal_code=SetInternalCode($p_cn,$seq,$p_jrn);
00396     $comment=$internal_code."  compte : ".GetFicheName($p_cn,$e_bank_account);
00397     if ( FormatString(${"e_other$i"."_comment"}) == null ) {
00398       // Update comment if comment is blank
00399       $Res=ExecSql($p_cn,"update jrn set jr_comment='".$comment."' where jr_grpt_id=".$seq);
00400     }
00401     if ( $i == 0 )
00402       {
00403         // first record we upload the files and
00404         // keep variable to update other row of jrn
00405         if ( isset ($_FILES))
00406           $oid=save_upload_document($p_cn,$seq);
00407 
00408       } else {
00409         if ( sizeof($_FILES) != 0 ) 
00410           {
00411             ExecSql($p_cn,"update jrn set jr_pj=".$oid.", jr_pj_name='".$_FILES['pj']['name']."', ".
00412                 "jr_pj_type='".$_FILES['pj']['type']."'  where jr_grpt_id=$seq");
00413           }
00414       }
00415     
00416   } // for nbitem
00417 
00418   Commit($p_cn);
00419   return $internal_code;
00420 }
00421 ?>