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

user_form_ven.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$ */
00020 // Copyright Author Dany De Bontridder ddebontridder@yahoo.fr
00024 require_once("constant.php");
00025 require_once("class_widget.php");
00026 require_once("preference.php");
00027 require_once("fiche_inc.php");
00028 require_once("user_common.php");
00043 function FormVenInput($p_cn,$p_jrn,$p_periode,$p_array=null,$pview_only=true,$p_article=1)
00044 { 
00045 
00046   if ( $p_array != null ) {
00047     // array contains old value
00048     foreach ( $p_array as $a=>$v) {
00049       ${"$a"}=$v;
00050     }
00051   }
00052   // The date
00053   list ($l_date_start,$l_date_end)=GetPeriode($p_cn,$p_periode);
00054   $op_date=( ! isset($e_date) ) ?substr($l_date_start,2,8):$e_date;
00055   $e_ech=(isset($e_ech))?$e_ech:"";
00056   $e_comm=(isset($e_comm))?$e_comm:"";
00057   //  $e_jrn=(isset($e_jrn))?$e_jrn:"";
00058   // Save old value and set a new one
00059   echo_debug('user_form_ven.php',__LINE__,"form_input.php.FormVentep_op_date is $op_date");
00060   $r="";
00061   if ( $pview_only == false) {
00062     $r.=JS_SEARCH_CARD;
00063     $r.=JS_SHOW_TVA;    
00064     $r.=JS_TVA;
00065     // Compute href
00066     $href=$_SERVER['SCRIPT_NAME'];
00067     switch ($href)
00068       {
00069         // user_jrn.php
00070       case '/user_jrn.php':
00071         $href="user_jrn.php?action=insert_vente&p_jrn=$p_jrn";
00072         break;
00073       case '/commercial.php':
00074         $href="commercial.php?p_action=facture&p_jrn=$p_jrn";
00075         break;
00076       default:
00077         echo_error('user_form_ven.php',__LINE__,'Erreur invalid request uri');
00078         exit (-1);
00079       }
00080       
00081     $r.="<FORM NAME=\"form_detail\" ACTION=\"$href\" METHOD=\"POST\">";
00082 
00083     
00084   }
00085   $sql="select jrn_def_id as value,jrn_def_name as label from jrn_def where jrn_def_type='VEN'";
00086   $list=GetArray($p_cn,$sql);
00087   $r.='<TABLE>';
00088   //  Date
00089   //--
00090   $Date=new widget("text");
00091   $Date->SetReadOnly($pview_only);
00092   $Date->table=1;
00093   $Date->tabindex=1;
00094   $r.="<tr>";
00095   $r.=$Date->IOValue("e_date",$op_date,"Date");
00096   $r.="</tr>";
00097   // Payment limit
00098   //--
00099   $Echeance=new widget("text");
00100   $Echeance->SetReadOnly($pview_only);
00101   $Echeance->table=1;
00102   $Echeance->tabindex=2;
00103   $r.="<tr>";
00104   $r.=$Echeance->IOValue("e_ech",$e_ech,"Echeance");
00105   $r.="</tr>";
00106   // Comment
00107   //--
00108   $Commentaire=new widget("text");
00109   $Commentaire->table=1;
00110   $Commentaire->SetReadOnly($pview_only);
00111   $Commentaire->size=80;
00112   $Commentaire->tabindex=3;
00113   $r.="<tr>";
00114   $r.=$Commentaire->IOValue("e_comm",$e_comm,"Description");
00115   $r.="</tr>";
00116   include_once("fiche_inc.php");
00117   // Display the customer
00118   //--
00119   $fiche='deb';
00120   echo_debug('user_form_ven.php',__LINE__,"Client Nombre d'enregistrement ".sizeof($fiche));
00121   // Save old value and set a new one
00122   //--
00123   $e_client=( isset ($e_client) )?$e_client:"";
00124   $e_client_label="";  
00125 
00126   // retrieve e_client_label
00127   //--
00128   if ( isNumber($e_client) == 1 ) {
00129     if ( isFicheOfJrn($p_cn,$p_jrn,$e_client,'deb') == 0 ) {
00130       $msg="Fiche inexistante !!! ";
00131       echo_error($msg); echo_error($msg);       
00132       echo "<SCRIPT>alert('$msg');</SCRIPT>";
00133       $e_client="";
00134     } else {
00135       $a_client=GetFicheAttribut($p_cn,$e_client);
00136       if ( $a_client != null)   
00137           $e_client_label=$a_client['vw_name']."  adresse ".$a_client['vw_addr']."  ".$a_client['vw_cp'];
00138     }
00139   }
00140 
00141   $W1=new widget("js_search");
00142   $W1->label="Client";
00143   $W1->name="e_client";
00144   $W1->tabindex=3;
00145   $W1->value=$e_client;
00146   $W1->extra=$fiche;  // list of card
00147   $W1->extra2=$p_jrn;
00148   $r.="<TR>".$W1->IOValue();
00149   $client_label=new widget("span");
00150   $r.= $client_label->IOValue("e_client_label",$e_client_label)."</TD></TR>";
00151   $r.="</TABLE>";
00152 
00153   // Record the current number of article
00154   $Hid=new widget('hidden');
00155   $r.=$Hid->IOValue("nb_item",$p_article);
00156   $e_comment=(isset($e_comment))?$e_comment:"";
00157 
00158 
00159   // Start the div for item to sell
00160   $r.="<DIV>";
00161   $r.='<H2 class="info">Articles</H2>';
00162   $r.='<TABLE>';
00163   $r.='<TR>';
00164   $r.="<th></th>";
00165   $r.="<th>Code</th>";
00166   $r.="<th>Dénomination</th>";
00167   $r.="<th>prix</th>";
00168   $r.="<th>tva</th>";
00169   $r.="<th>quantité</th>";
00170   $r.='</TR>';
00171   // For each article
00172   //--
00173   for ($i=0;$i< $p_article;$i++) {
00174     // Code id, price & vat code
00175     //--
00176     $march=(isset(${"e_march$i"}))?${"e_march$i"}:"";
00177     $march_sell=(isset(${"e_march".$i."_sell"}))?${"e_march".$i."_sell"}:"";
00178     $march_tva_id=(isset(${"e_march$i"."_tva_id"}))?${"e_march$i"."_tva_id"}:"";
00179 
00180     $march_tva_label="";
00181     $march_label="";
00182 
00183     // If $march has a value
00184     //--
00185     if ( isNumber($march) == 1 ) {
00186       if ( isFicheOfJrn($p_cn,$p_jrn,$march,'cred') == 0 ) {
00187         $msg="Fiche inexistante !!! ";
00188         echo_error($msg); echo_error($msg);     
00189         echo "<SCRIPT>alert('$msg');</SCRIPT>";
00190         $march="";
00191       } else {
00192         // retrieve the tva label and name
00193         //--
00194         $a_fiche=GetFicheAttribut($p_cn, $march);
00195         if ( $a_fiche != null ) {
00196           if ( $march_tva_id == "" ) {
00197             $march_tva_id=$a_fiche['tva_id'];
00198             $march_tva_label=$a_fiche['tva_label'];
00199           }
00200           $march_label=$a_fiche['vw_name'];
00201         }
00202       }
00203     }
00204     // Show input
00205     //--
00206     $W1=new widget("js_search");
00207     $W1->label="";
00208     $W1->name="e_march".$i;
00209     $W1->value=$march;
00210     $W1->extra='cred';  // credits
00211     $W1->extra2=$p_jrn;
00212     $W1->readonly=$pview_only;
00213     $r.="<TR>".$W1->IOValue()."</TD>";
00214     $Span=new widget ("span");
00215     $Span->SetReadOnly($pview_only);
00216     // card's name, price
00217     //--
00218     $r.="<TD>".$Span->IOValue("e_march".$i."_label",$march_label)."</TD>";
00219    // price
00220     $Price=new widget("text");
00221     $Price->SetReadOnly($pview_only);
00222     $Price->table=1;
00223     $r.=$Price->IOValue("e_march".$i."_sell",$march_sell);
00224     // vat label
00225     //--
00226     $select_tva=make_array($p_cn,"select tva_id,tva_label from tva_rate order by tva_id",1);
00227     $Tva=new widget("select");
00228     $Tva->table=1;
00229     $Tva->selected=$march_tva_id;
00230     $r.=$Tva->IOValue("e_march$i"."_tva_id",$select_tva);
00231 
00232     // quantity
00233     //--
00234     $quant=(isset(${"e_quant$i"}))?${"e_quant$i"}:"1";
00235     $Quantity=new widget("text");
00236     $Quantity->SetReadOnly($pview_only);
00237     $Quantity->table=1;
00238     //$r.=InputType("","TEXT","e_quant".$i,$quant,$pview_only);
00239     $r.=$Quantity->IOValue("e_quant".$i,$quant);
00240     $r.='</TR>';
00241   }
00242 
00243 
00244 
00245   $r.="</TABLE>";
00246   $r.="<hr>";
00247   // Set correctly the REQUEST param for jrn_type 
00248   $h=new widget('hidden');
00249   $h->name='jrn_type';
00250   $h->value='VEN';
00251   $r.=$h->IOValue();
00252 
00253   if ($pview_only == false ) {
00254     $r.='<INPUT TYPE="SUBMIT" NAME="add_item" VALUE="Ajout article" TABINDEX="32767">';
00255     $r.='<INPUT TYPE="SUBMIT" NAME="view_invoice" VALUE="Enregistrer" TABINDEX="32767" ID="SubmitButton">';
00256     $r.="</DIV>";
00257     $r.="</FORM>";
00258     $r.=JS_CALC_LINE;
00259   } else {
00260      $r.="</div>";
00261 
00262   }
00263 
00264 
00265 
00266   return $r;
00267 
00268 
00269 }
00284 function form_verify_input($p_cn,$p_jrn,$p_periode,$p_array,$p_number)
00285 {
00286   foreach ($p_array as $name=>$content) {
00287     ${"$name"}=$content;
00288   }
00289   // Verify the date
00290   if ( isDate($e_date) == null ) { 
00291           echo_error("Invalid date $e_date");
00292           echo_debug('user_form_ven.php',__LINE__,"Invalid date $e_date");
00293           echo "<SCRIPT> alert('INVALID DATE $e_date !!!!');</SCRIPT>";
00294           return null;
00295                 }
00296 // Verify the quantity
00297 for ($o = 0;$o < $p_number; $o++) {
00298         if ( isNumber(${"e_quant$o"}) == 0 ) {
00299                 echo_debug('user_form_ven.php',__LINE__,"invalid quantity ".${"e_quant$o"});
00300                 echo_error("invalid quantity ".${"e_quant$o"});
00301                 echo "<SCRIPT> alert('INVALID QUANTITY !!!');</SCRIPT>";
00302                 return null;
00303         }       
00304     // check if vat is correct
00305     if ( strlen(trim(${"e_march$o"."_tva_id"})) !=0 
00306          and 
00307          ${"e_march$o"."_tva_id"} != "-1")
00308       {
00309       // vat is given we check it now check if valid
00310       if (isNumber(${"e_march$o"."_tva_id"}) == 0
00311                or CountSql($p_cn,"select tva_id from tva_rate where tva_id=".${"e_march$o"."_tva_id"}) ==0)
00312         {
00313           $msg="Invalid TVA !!! e_march".$o."_tva_id = ".${"e_march".$o."_tva_id"};
00314           echo_error($msg); echo_error($msg);   
00315           echo "<SCRIPT>alert('$msg');</SCRIPT>";
00316           return null;
00317         
00318       }
00319     }
00320     
00321  }
00322 
00323 // Verify the ech
00324  if (strlen($e_ech) != 0 and isNumber($e_ech)  == 0 and  isDate ($e_ech) == null ) {
00325         $msg="Echeance invalide";
00326                 echo_error($msg); echo_error($msg);     
00327                 echo "<SCRIPT>alert('$msg');</SCRIPT>";
00328                 return null;
00329  } 
00330 
00331  // if ech is a number of days then compute date limit
00332  if ( strlen($e_ech) != 0 and isNumber($e_ech) == 1) {
00333  list($day,$month,$year)=explode(".",$e_date);
00334   echo_debug('user_form_ven.php',__LINE__," date $e_date = $day.$month.$year");
00335   $p_ech=date('d.m.Y',mktime(0,0,0,$month,$day+$e_ech,$year));
00336   echo_debug('user_form_ven.php',__LINE__,"p_ech = $e_ech $p_ech");
00337   $e_ech=$p_ech;
00338   $wHidden=new widget("hidden");
00339   $data.=$wHidden->IOValue("e_ech",$e_ech);
00340  }
00341 
00342  // Check if the fiche is in the jrn
00343  if (IsFicheOfJrn($p_cn , $p_jrn, $e_client,'deb') == 0 ) 
00344    {
00345      $msg="Client invalid please recheck";
00346      echo_error($msg);
00347      echo "<SCRIPT>alert('$msg');</SCRIPT>";
00348      return null;
00349    }
00350 
00351  // check if all e_march are in fiche
00352   for ($i=0;$i<$p_number;$i++) {
00353     if ( trim(${"e_march$i"})  == "" ) {
00354       // no goods to sell 
00355       continue;
00356     }
00357   
00358 
00359     // Check 
00360     if ( isFicheOfJrn($p_cn,$p_jrn,${"e_march$i"},'cred') == 0 ) {
00361       $msg="Fiche inexistante !!! ";
00362       echo_error($msg); echo_error($msg);       
00363       echo "<SCRIPT>alert('$msg');</SCRIPT>";
00364       return null;
00365     }
00366         // check if the  ATTR_DEF_ACCOUNT is set
00367         $poste=GetFicheAttribut($p_cn,${"e_march$i"},ATTR_DEF_ACCOUNT);
00368         if ( $poste == null ) 
00369         {       
00370                 $msg="La fiche ".${"e_march$i"}." n\'a pas de poste comptable";
00371       echo_error($msg); echo_debug('user_form_ven.php',__LINE__,$msg);  
00372       echo "<SCRIPT>alert('$msg');</SCRIPT>";
00373       return null;
00374         
00375         }
00376         if ( strlen(trim($poste))==0 )
00377         {
00378                 $msg="La fiche ".$tiers." n\'a pas de poste comptable";
00379                 echo_error($msg); echo_debug('user_form_ven.php',__LINE__,$msg);        
00380                 echo "<SCRIPT>alert('$msg');</SCRIPT>";
00381         }
00382 
00383   }
00384 // Verify the userperiode
00385 
00386 // p_periode contient la periode par default
00387   list ($l_date_start,$l_date_end)=GetPeriode($p_cn,$p_periode);
00388   
00389   // Date dans la periode active
00390   echo_debug ('user_form_ven',__LINE__,"date start periode $l_date_start date fin periode $l_date_end date demande $e_date");
00391   if ( cmpDate($e_date,$l_date_start)<0 || 
00392        cmpDate($e_date,$l_date_end)>0 )
00393     {
00394       $msg="Not in the active periode please change your preference";
00395       echo_error($msg); echo_error($msg);       
00396       echo "<SCRIPT>alert('$msg');</SCRIPT>";
00397       return null;
00398     }
00399     // Periode ferme
00400     if ( PeriodeClosed ($p_cn,$p_periode)=='t' )
00401       {
00402                 $msg="This periode is closed please change your preference";
00403                 echo_error($msg); echo_error($msg);     
00404                 echo "<SCRIPT>alert('$msg');</SCRIPT>";
00405                 return null;
00406       }
00407     return true;
00408 }
00425 function FormVenteView ($p_cn,$p_jrn,$p_periode,$p_array,$p_number,$p_doc='form',$p_comment='') 
00426 {
00427   $r="";
00428   $data="";
00429   // Keep all the data if hidden
00430   // and store the array in variables
00431   $hidden=new widget("hidden");
00432   foreach ($p_array as $name=>$content) {
00433     $data.=$hidden->IOValue($name,$content);
00434     ${"$name"}=$content;
00435   }
00436   
00437   // start table
00438   $r.='<TABLE>';
00439   // Show the Date
00440   $r.="<tr>";
00441   $r.="<TD>Date : $e_date</TD>";
00442   $r.="</tr>";
00443   // Show the customer Name
00444   $r.="<tr>";
00445   $r.="<TD>Client : ".getFicheName($p_cn,$e_client)."</TD>";
00446   $r.="</tr>";
00447   
00448   // show date limit
00449   $r.="<tr>";
00450   $r.="<TD> Echeance : $e_ech </TD>";
00451   $r.="</tr>";
00452   // Show desc
00453   $r.="<tr>";
00454   $r.="<TD> Description : $e_comm</TD>";
00455   $r.="</tr>";
00456   
00457   $sum_with_vat=0.0;
00458     $sum_march=0.0;
00459   // show all article, price vat and sum
00460     $r.="<TR>";
00461     $r.="<TH>Article</TH>";
00462     $r.="<TH>quantité</TH>";
00463     $r.="<TH>prix unit.</TH>";
00464     $r.="<TH>taux tva</TH>";
00465     $r.="<TH>Montant HTVA</TH>";
00466     $r.="<TH>Montant TVA</TH>";
00467     $r.="<TH>Total</TH>";
00468     $r.="</TR>";
00469   for ($i=0;$i<$p_number;$i++) {
00470     if ( trim(${"e_march$i"})  == "" ) {
00471       // no goods to sell 
00472       continue;
00473     }
00474   
00475     // Get the name
00476     $fiche_name=getFicheName($p_cn,${"e_march$i"});
00477 
00478     // Quantity
00479     $fiche_quant=${"e_quant$i"};
00480 
00481     // No  row if there is quantity
00482     if ( $fiche_quant == 0.0 ) continue;
00483 
00484 
00485     // If the price is not a number, retrieve the price from the database
00486     if ( isNumber(${"e_march$i"."_sell"}) == 0 ) {
00487            $fiche_price=getFicheAttribut($p_cn,${"e_march$i"},ATTR_DEF_PRIX_VENTE);
00488          } else {
00489       $fiche_price=${"e_march$i"."_sell"};
00490     }
00491     
00492     
00493     // VAT 
00494     $vat=(isNumber(${"e_march$i"."_tva_id"})==0)?getFicheAttribut($p_cn,${"e_march$i"},ATTR_DEF_TVA):${"e_march$i"."_tva_id"};
00495         
00496     // vat label
00497     // vat rate
00498     $a_vat=GetTvaRate($p_cn,$vat);
00499     if ( $a_vat == null ) {
00500       $vat_label="";
00501       $vat_rate=0.0;
00502     } else { 
00503       $vat_label=$a_vat['tva_label'];
00504       $vat_rate=$a_vat['tva_rate'];
00505     }           
00506         
00507     // Total card without vat
00508     $fiche_sum=$fiche_price*$fiche_quant;
00509     // Sum of invoice
00510     $sum_march+=$fiche_sum;
00511     // vat of the card
00512     $fiche_amount_vat=$fiche_price*$fiche_quant*$vat_rate;
00513     // value card + vat
00514     $fiche_with_vat=$fiche_price*$fiche_quant*(1+$vat_rate);
00515     // Sum of invoice vat 
00516     $sum_with_vat+=$fiche_with_vat;
00517     // Show the data
00518     $r.='<TR>';
00519     $r.='<TD>'.$fiche_name.'</TD>';
00520     $r.='<TD ALIGN="CENTER">'.$fiche_quant.'</TD>';
00521     $r.='<TD ALIGN="right">'.$fiche_price.'</TD>';
00522     $r.="<TD  ALIGN=\"RIGHT\"> $vat_label </TD>";
00523     $r.='<TD  ALIGN="RIGHT">'.round($fiche_sum,2).'</TD>';
00524     $r.='<TD ALIGN="RIGHT">'.round($fiche_amount_vat,2).'</TD>';
00525 
00526     $r.='<TD>'.round($fiche_with_vat,2).'</TD>';
00527 
00528     $r.="</TR>";
00529   }
00530   
00531   // end table
00532   $r.='</TABLE> ';
00533   $r.='<DIV style="padding:30px;font-size:14px">';
00534   $r.="Total HTVA =".round( $sum_march,2)." <br>";
00535   $r.="Total = ".round($sum_with_vat,2);
00536 
00537  
00538   $r.="</DIV>";
00539   if ( $p_doc == 'form' ) {
00540     // Compute href
00541     $href=$_SERVER['SCRIPT_NAME'];
00542     switch ($href)
00543       {
00544         // user_jrn.php
00545       case '/user_jrn.php':
00546         $href="user_jrn.php?action=record&p_jrn=$p_jrn";
00547         break;
00548       case '/commercial.php':
00549         $href="commercial.php?p_action=facture&sa=record&p_jrn=$p_jrn";
00550         break;
00551       default:
00552         echo_error('user_form_ven.php',__LINE__,'Erreur invalid request uri');
00553         exit (-1);
00554       }
00555       
00556 
00557     $r.='<FORM METHOD="POST" enctype="multipart/form-data" ACTION="'.$href.'">';
00558 
00559     // check for upload piece
00560     $file=new widget("file");
00561     $file->table=1;
00562     $r.="<hr>";
00563     $r.= "<table>"; 
00564     $r.="<TR>".$file->IOValue("pj","","Pièce justificative")."</TR>";
00565     $r.="</table>";
00566     $r.="<hr>";
00567 
00568 
00569     // if we were in the management module, appl. propose to generate an invoice
00570     if ( $_SERVER['SCRIPT_NAME'] == '/commercial.php')
00571       {
00572         // if a template exists propose to choose an invoice template
00573         if ( CountSql($p_cn,
00574                       "select md_id,md_name from document_modele where md_type=4") > 0 )
00575           {
00576             $r.='G&eacute;n&eacute;rer une facture <input type="checkbox" name="gen_invoice" CHECKED>';
00577             // We propose to generate  the invoice and some template
00578             $doc_gen=new widget("select");
00579             $doc_gen->name="gen_doc";
00580             $doc_gen->value=make_array($p_cn,
00581                                        "select md_id,md_name from document_modele where md_type=4");
00582             $r.=$doc_gen->IOValue();  
00583 
00584             $r.="<hr>";
00585           }
00586       }
00587 
00588     
00589     $r.=$data;
00590     if ( $sum_with_vat != 0 ) {
00591       $r.='<INPUT TYPE="SUBMIT" name="record_and_print_invoice" value="Sauver" >';
00592     }
00593     $r.='<INPUT TYPE="SUBMIT" name="correct_new_invoice" value="Corriger">';
00594     
00595     $r.='</FORM>';
00596   } 
00597   return $r;
00598   
00599 }
00600 
00627 function RecordInvoice($p_cn,$p_array,$p_user,$p_jrn)
00628 {
00629   foreach ( $p_array as $v => $e)
00630   {
00631     ${"$v"}=$e;
00632   }
00633 
00634   // Get the default period
00635   $periode=$p_user->GetPeriode();
00636   $amount=0.0;
00637   $amount_jrn=0.0;
00638   // Computing total customer
00639   //--
00640   for ($i=0;$i<$nb_item;$i++) {
00641     // store quantity & goods in array
00642     $a_good[$i]=${"e_march$i"};
00643     $a_quant[$i]=${"e_quant$i"};
00644     $a_price[$i]=0;
00645     $a_vat[$i]=${"e_march$i"."_tva_id"};
00646     // check whether the price is set or no
00647     if ( isNumber(${"e_march$i"."_sell"}) == 0 ) {
00648       if ( $a_good[$i] != "" ) {
00649              // If the price is not set we have to find it from the database
00650              $a_price[$i]=GetFicheAttribut($p_cn,$a_good[$i],ATTR_DEF_PRIX_VENTE);
00651            } 
00652     } else {
00653       // The price is valid
00654       $a_price[$i]=${"e_march$i"."_sell"};
00655     }
00656     $cost=$a_price[$i]*$a_quant[$i];
00657     $amount+=$cost;
00658     $amount_jrn+=$cost;
00659   }// for
00660 
00661   $comm=FormatString($e_comm);
00662   // Compute VAT
00663   //--
00664   $a_vat_new=ComputeTotalVat($p_cn,$a_good,$a_quant,$a_price,$a_vat);
00665   $sum_vat=0.0;
00666   if ( $a_vat_new != null ){
00667     foreach ( $a_vat_new as $element => $t) {
00668       echo_debug('user_form_ven.php',__LINE__," a_vat element $element t $t");
00669       $sum_vat+=$t;
00670       echo_debug('user_form_ven.php',__LINE__,"sum_vat = $sum_vat");
00671     }
00672   }
00673   // First we add in jrnx
00674         
00675   // Compute the j_grpt
00676   $seq=NextSequence($p_cn,'s_grpt');
00677 
00678 
00679   // Debit = client
00680   $poste=GetFicheAttribut($p_cn,$e_client,ATTR_DEF_ACCOUNT);
00681   StartSql($p_cn);      
00682   $r=InsertJrnx($p_cn,'d',$p_user->id,$p_jrn,$poste,$e_date,round($amount,2)+round($sum_vat,2),$seq,$periode);
00683   if ( $r == false) { $Rollback($p_cn);exit("error 'user_form_ven.php' __LINE__");}
00684 
00685   // Credit = goods 
00686   for ( $i = 0; $i < $nb_item;$i++) {
00687     if ( $a_good[$i] == ""  ) continue;
00688     $poste=GetFicheAttribut($p_cn,$a_good[$i],ATTR_DEF_ACCOUNT);
00689           
00690     // don't record operation of 0
00691     if ( $a_price[$i]*$a_quant[$i] == 0 ) continue;
00692           
00693     // record into jrnx
00694     $j_id=InsertJrnx($p_cn,'c',$p_user->id,$p_jrn,$poste,$e_date,round($a_price[$i]*$a_quant[$i],2),$seq,$periode);
00695     if ( $j_id == false) { $Rollback($p_cn);exit("error 'user_form_ven.php' __LINE__");}
00696     // always save quantity but in withStock we can find what card need a stock management
00697     if (  InsertStockGoods($p_cn,$j_id,$a_good[$i],$a_quant[$i],'c') == false ) {
00698       $Rollback($p_cn);exit("error 'user_form_ven.php' __LINE__");}
00699     } // end loop
00700   
00701   // Insert Vat
00702 
00703   if ( $a_vat_new  !=  null  ) // no vat
00704 
00705     {
00706       foreach ($a_vat_new as $tva_id => $tva_amount ) {
00707         $poste=GetTvaPoste($p_cn,$tva_id,'c');
00708         if ($tva_amount == 0 ) continue;
00709         $r=InsertJrnx($p_cn,'c',$p_user->id,$p_jrn,$poste,$e_date,round($tva_amount,2),$seq,$periode);
00710         if ( $r == false ) { Rollback($p_cn); exit(" Error 'user_form_ven.php' __LINE__");}
00711       
00712       }
00713     }
00714   echo_debug('user_form_ven.php',__LINE__,"echeance = $e_ech");
00715   $r=InsertJrn($p_cn,$e_date,$e_ech,$p_jrn,"Invoice",round($amount_jrn,2)+round($sum_vat,2),$seq,$periode);
00716   if ( $r == false ) { Rollback($p_cn); exit(" Error 'user_form_ven.php' __LINE__");}
00717   // Set Internal code and Comment
00718   $internal=SetInternalCode($p_cn,$seq,$p_jrn);
00719   $comment=(FormatString($e_comm) == null )?$internal."  client : ".GetFicheName($p_cn,$e_client):FormatString($e_comm);
00720 
00721   // Update and set the invoice's comment 
00722   $Res=ExecSql($p_cn,"update jrn set jr_comment='".$comment."' where jr_grpt_id=".$seq);
00723   if ( $Res == false ) { Rollback($p_cn); exit(" Error 'user_form_ven.php' __LINE__"); };
00724 
00725   if ( isset ($_FILES)) {
00726     if ( sizeof($_FILES) != 0 )
00727     save_upload_document($p_cn,$seq);
00728   }
00729 
00730 // save the quantity, then we can make an invoice
00731   for ( $i=0;$i < $nb_item;$i++) 
00732         {
00733         // don't record operation of 0
00734         if ( $a_price[$i]*$a_quant[$i] == 0 ) continue;
00735         
00736         // insert into the table quant_sold
00737         // Note that negative value are also saved but not the vat !
00738         if (  $a_vat[$i] == -1) {
00739                 $computed_vat=0;
00740                 $vat_code="null";
00741         } else {
00742                 $computed_vat=ComputeVat($p_cn,$a_good[$i],$a_quant[$i],$a_price[$i],$a_vat[$i]);
00743                 $vat_code=$a_vat[$i];
00744         }
00745          $r=ExecSql($p_cn,"select insert_quant_sold ".
00746                     "('".$internal."','".$a_good[$i]."',".$a_quant[$i].",".$a_price[$i]*$a_quant[$i].
00747                     ",".$computed_vat.
00748                     ",".$vat_code.",'".$e_client."')");
00749          if ( $r == false ) { Rollback($p_cn); exit(" Error 'user_form_ven.php' __LINE__"); };
00750         }
00751   Commit($p_cn);
00752   return array($internal,$comment);
00753 }
00754 
00755 ?>

Generated on Tue Jun 27 01:39:29 2006 for phpcompta by  doxygen 1.4.4