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