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

user_form_ven.php File Reference

Functions for the ledger of sold. More...

Go to the source code of this file.

Functions

 FormVenInput ($p_cn, $p_jrn, $p_periode, $p_array=null, $pview_only=true, $p_article=1)
 Display the form for a sell Used to show detail, encode a new invoice or update one.
 form_verify_input ($p_cn, $p_jrn, $p_periode, $p_array, $p_number)
 verify if the data to insert are valid
 FormVenteView ($p_cn, $p_jrn, $p_periode, $p_array, $p_number, $p_doc='form', $p_comment='')
 Show the invoice before inserting it the database.
 RecordInvoice ($p_cn, $p_array, $p_user, $p_jrn)
 Record an invoice in the table jrn & jrnx.


Detailed Description

Functions for the ledger of sold.

Definition in file user_form_ven.php.


Function Documentation

form_verify_input p_cn,
p_jrn,
p_periode,
p_array,
p_number
 

verify if the data to insert are valid

form_verify_input

parm :

  • p_cn database connection

p_jrn concerned ledger

  • User periode
  • array with the post data
  • p_number number of items gen :
    • return:

Definition at line 285 of file user_form_ven.php.

References $msg, CountSql(), echo_debug(), echo_error(), isDate(), isNumber(), and name.

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  // Check if the card has a valid account
00352  if ( CheckPoste($p_cn,$e_client) == null )
00353    return null;
00354 
00355  // check if all e_march are in fiche
00356   for ($i=0;$i<$p_number;$i++) {
00357     if ( trim(${"e_march$i"})  == "" ) {
00358       // no goods to sell 
00359       continue;
00360     }
00361   
00362 
00363     // Check 
00364     if ( isFicheOfJrn($p_cn,$p_jrn,${"e_march$i"},'cred') == 0 ) {
00365       $msg="Fiche inexistante !!! ";
00366       echo_error($msg); echo_error($msg);       
00367       echo "<SCRIPT>alert('$msg');</SCRIPT>";
00368       return null;
00369     }
00370     // Check if the card has a valid account
00371     if ( CheckPoste($p_cn,${"e_march$i"}) == null )
00372       return null;
00373   }
00374 // Verify the userperiode
00375 
00376 // p_periode contient la periode par default
00377   list ($l_date_start,$l_date_end)=GetPeriode($p_cn,$p_periode);
00378   
00379   // Date dans la periode active
00380   echo_debug ('user_form_ven',__LINE__,"date start periode $l_date_start date fin periode $l_date_end date demande $e_date");
00381   if ( cmpDate($e_date,$l_date_start)<0 || 
00382        cmpDate($e_date,$l_date_end)>0 )
00383     {
00384       $msg="Not in the active periode please change your preference";
00385       echo_error($msg); echo_error($msg);       
00386       echo "<SCRIPT>alert('$msg');</SCRIPT>";
00387       return null;
00388     }
00389     // Periode ferme
00390     if ( PeriodeClosed ($p_cn,$p_periode)=='t' )
00391       {
00392                 $msg="This periode is closed please change your preference";
00393                 echo_error($msg); echo_error($msg);     
00394                 echo "<SCRIPT>alert('$msg');</SCRIPT>";
00395                 return null;
00396       }
00397     return true;
00398 }

FormVenInput p_cn,
p_jrn,
p_periode,
p_array = null,
pview_only = true,
p_article = 1
 

Display the form for a sell Used to show detail, encode a new invoice or update one.

FormVenInput

Parameters:
p_array which can be empty
  • the "journal"
  • $p_periode = periode
  • view_only if we cannot change it (no right or centralized op)
  • $p_article number of article
Returns:
string with the form TODO Add in parameters the infos about the company for making the invoice

Definition at line 43 of file user_form_ven.php.

References $fiche, $list, $msg, $p_jrn, $r, $sql, echo_debug(), echo_error(), exit, GetArray(), GetPeriode(), isNumber(), and make_array().

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 }

FormVenteView p_cn,
p_jrn,
p_periode,
p_array,
p_number,
p_doc = 'form',
p_comment = ''
 

Show the invoice before inserting it the database.

FormVenteView ($p_cn,$p_jrn,$p_periode,$p_array,$p_number,$p_doc='html',$p_comment='')

Parameters:
p_cn database connection
p_jrn journal
p_periode 
array of value
nb of item
p_doc type pdf or html
Returns:
string

Definition at line 415 of file user_form_ven.php.

References $data, $file, $r, CountSql(), echo_error(), exit, getFicheAttribut(), getFicheName(), GetTvaRate(), isNumber(), make_array(), METHOD, name, and value.

00416 {
00417   $r="";
00418   $data="";
00419   // Keep all the data if hidden
00420   // and store the array in variables
00421   $hidden=new widget("hidden");
00422   foreach ($p_array as $name=>$content) {
00423     $data.=$hidden->IOValue($name,$content);
00424     ${"$name"}=$content;
00425   }
00426   
00427   // start table
00428   $r.='<TABLE>';
00429   // Show the Date
00430   $r.="<tr>";
00431   $r.="<TD>Date : $e_date</TD>";
00432   $r.="</tr>";
00433   // Show the customer Name
00434   $r.="<tr>";
00435   $r.="<TD>Client : ".getFicheName($p_cn,$e_client)."</TD>";
00436   $r.="</tr>";
00437   
00438   // show date limit
00439   $r.="<tr>";
00440   $r.="<TD> Echeance : $e_ech </TD>";
00441   $r.="</tr>";
00442   // Show desc
00443   $r.="<tr>";
00444   $r.="<TD> Description : $e_comm</TD>";
00445   $r.="</tr>";
00446   
00447   $sum_with_vat=0.0;
00448     $sum_march=0.0;
00449   // show all article, price vat and sum
00450     $r.="<TR>";
00451     $r.="<TH>Article</TH>";
00452     $r.="<TH>quantité</TH>";
00453     $r.="<TH>prix unit.</TH>";
00454     $r.="<TH>taux tva</TH>";
00455     $r.="<TH>Montant HTVA</TH>";
00456     $r.="<TH>Montant TVA</TH>";
00457     $r.="<TH>Total</TH>";
00458     $r.="</TR>";
00459   for ($i=0;$i<$p_number;$i++) {
00460     if ( trim(${"e_march$i"})  == "" ) {
00461       // no goods to sell 
00462       continue;
00463     }
00464   
00465     // Get the name
00466     $fiche_name=getFicheName($p_cn,${"e_march$i"});
00467 
00468     // Quantity
00469     $fiche_quant=${"e_quant$i"};
00470 
00471     // No  row if there is quantity
00472     if ( $fiche_quant == 0.0 ) continue;
00473 
00474 
00475     // If the price is not a number, retrieve the price from the database
00476     if ( isNumber(${"e_march$i"."_sell"}) == 0 ) {
00477            $fiche_price=getFicheAttribut($p_cn,${"e_march$i"},ATTR_DEF_PRIX_VENTE);
00478          } else {
00479       $fiche_price=${"e_march$i"."_sell"};
00480     }
00481     
00482     
00483     // VAT 
00484     $vat=(isNumber(${"e_march$i"."_tva_id"})==0)?getFicheAttribut($p_cn,${"e_march$i"},ATTR_DEF_TVA):${"e_march$i"."_tva_id"};
00485         
00486     // vat label
00487     // vat rate
00488     $a_vat=GetTvaRate($p_cn,$vat);
00489     if ( $a_vat == null ) {
00490       $vat_label="";
00491       $vat_rate=0.0;
00492     } else { 
00493       $vat_label=$a_vat['tva_label'];
00494       $vat_rate=$a_vat['tva_rate'];
00495     }           
00496         
00497     // Total card without vat
00498     $fiche_sum=$fiche_price*$fiche_quant;
00499     // Sum of invoice
00500     $sum_march+=$fiche_sum;
00501     // vat of the card
00502     $fiche_amount_vat=$fiche_price*$fiche_quant*$vat_rate;
00503     // value card + vat
00504     $fiche_with_vat=$fiche_price*$fiche_quant*(1+$vat_rate);
00505     // Sum of invoice vat 
00506     $sum_with_vat+=$fiche_with_vat;
00507     // Show the data
00508     $r.='<TR>';
00509     $r.='<TD>'.$fiche_name.'</TD>';
00510     $r.='<TD ALIGN="CENTER">'.$fiche_quant.'</TD>';
00511     $r.='<TD ALIGN="right">'.$fiche_price.'</TD>';
00512     $r.="<TD  ALIGN=\"RIGHT\"> $vat_label </TD>";
00513     $r.='<TD  ALIGN="RIGHT">'.round($fiche_sum,2).'</TD>';
00514     $r.='<TD ALIGN="RIGHT">'.round($fiche_amount_vat,2).'</TD>';
00515 
00516     $r.='<TD>'.round($fiche_with_vat,2).'</TD>';
00517 
00518     $r.="</TR>";
00519   }
00520   
00521   // end table
00522   $r.='</TABLE> ';
00523   $r.='<DIV style="padding:30px;font-size:14px">';
00524   $r.="Total HTVA =".round( $sum_march,2)." <br>";
00525   $r.="Total = ".round($sum_with_vat,2);
00526 
00527  
00528   $r.="</DIV>";
00529   if ( $p_doc == 'form' ) {
00530     // Compute href
00531     $href=basename($_SERVER['SCRIPT_NAME']);
00532     switch ($href)
00533       {
00534         // user_jrn.php
00535       case 'user_jrn.php':
00536         $href="user_jrn.php?action=record&p_jrn=$p_jrn";
00537         break;
00538       case 'commercial.php':
00539         $href="commercial.php?p_action=facture&sa=record&p_jrn=$p_jrn";
00540         break;
00541       default:
00542         echo_error('user_form_ven.php',__LINE__,'Erreur invalid request uri');
00543         exit (-1);
00544       }
00545       
00546 
00547     $r.='<FORM METHOD="POST" enctype="multipart/form-data" ACTION="'.$href.'">';
00548 
00549     // check for upload piece
00550     $file=new widget("file");
00551     $file->table=1;
00552     $r.="<hr>";
00553     $r.= "<table>"; 
00554     $r.="<TR>".$file->IOValue("pj","","Pièce justificative")."</TR>";
00555     $r.="</table>";
00556     $r.="<hr>";
00557 
00558 
00559     // if we were in the management module, appl. propose to generate an invoice
00560     if ( $_SERVER['SCRIPT_NAME'] == '/commercial.php')
00561       {
00562         // if a template exists propose to choose an invoice template
00563         if ( CountSql($p_cn,
00564                       "select md_id,md_name from document_modele where md_type=4") > 0 )
00565           {
00566             $r.='G&eacute;n&eacute;rer une facture <input type="checkbox" name="gen_invoice" CHECKED>';
00567             // We propose to generate  the invoice and some template
00568             $doc_gen=new widget("select");
00569             $doc_gen->name="gen_doc";
00570             $doc_gen->value=make_array($p_cn,
00571                                        "select md_id,md_name from document_modele where md_type=4");
00572             $r.=$doc_gen->IOValue();  
00573 
00574             $r.="<hr>";
00575           }
00576       }
00577 
00578     
00579     $r.=$data;
00580     if ( $sum_with_vat != 0 ) {
00581       $r.='<INPUT TYPE="SUBMIT" name="record_and_print_invoice" value="Sauver" >';
00582     }
00583     $r.='<INPUT TYPE="SUBMIT" name="correct_new_invoice" value="Corriger">';
00584     
00585     $r.='</FORM>';
00586   } 
00587   return $r;
00588   
00589 }

RecordInvoice p_cn,
p_array,
p_user,
p_jrn
 

Record an invoice in the table jrn & jrnx.

RecordInvoice($p_cn,$p_array,$p_user,$p_jrn)

Parameters:
$p_cn Database connection
$p_array array contains all the invoice data
  • e_date => e : 01.01.2003
  • e_client => e : 3
  • nb_item => e : 3
  • e_march0 => e : 6
  • e_quant0 => e : 0
  • e_march0_sell=>e:1
  • e_march1 => e : 6
  • e_quant1 => e : 2
  • e_march1_sell=>e:1
  • e_march2 => e : 7
  • e_quant2 => e : 3
  • e_march2_sell=>e:1 * V : view_invoice => e : Voir cette facture * V : record_invoice => e : Sauver
$p_periode periode
$p_jrn current folder (journal)
Returns:
array ( internal_code,true on success)

Definition at line 617 of file user_form_ven.php.

References $comment, $e, $i, $poste, $r, $Res, $seq, $t, Commit(), ComputeTotalVat(), ComputeVat(), echo_debug(), ExecSql(), exit, FormatString(), GetTvaPoste(), InsertJrn(), InsertJrnx(), InsertStockGoods(), isNumber(), NextSequence(), Rollback(), save_upload_document(), SetInternalCode(), and StartSql().

00618 {
00619   foreach ( $p_array as $v => $e)
00620   {
00621     ${"$v"}=$e;
00622   }
00623 
00624   // Get the default period
00625   $periode=$p_user->GetPeriode();
00626   $amount=0.0;
00627   $amount_jrn=0.0;
00628   // Computing total customer
00629   //--
00630   for ($i=0;$i<$nb_item;$i++) {
00631     // store quantity & goods in array
00632     $a_good[$i]=${"e_march$i"};
00633     $a_quant[$i]=${"e_quant$i"};
00634     $a_price[$i]=0;
00635     $a_vat[$i]=${"e_march$i"."_tva_id"};
00636     // check whether the price is set or no
00637     if ( isNumber(${"e_march$i"."_sell"}) == 0 ) {
00638       if ( $a_good[$i] != "" ) {
00639              // If the price is not set we have to find it from the database
00640              $a_price[$i]=GetFicheAttribut($p_cn,$a_good[$i],ATTR_DEF_PRIX_VENTE);
00641            } 
00642     } else {
00643       // The price is valid
00644       $a_price[$i]=${"e_march$i"."_sell"};
00645     }
00646     $cost=$a_price[$i]*$a_quant[$i];
00647     $amount+=$cost;
00648     $amount_jrn+=$cost;
00649   }// for
00650 
00651   $comm=FormatString($e_comm);
00652   // Compute VAT
00653   //--
00654   $a_vat_new=ComputeTotalVat($p_cn,$a_good,$a_quant,$a_price,$a_vat);
00655   $sum_vat=0.0;
00656   if ( $a_vat_new != null ){
00657     foreach ( $a_vat_new as $element => $t) {
00658       echo_debug('user_form_ven.php',__LINE__," a_vat element $element t $t");
00659       $sum_vat+=$t;
00660       echo_debug('user_form_ven.php',__LINE__,"sum_vat = $sum_vat");
00661     }
00662   }
00663   // First we add in jrnx
00664         
00665   // Compute the j_grpt
00666   $seq=NextSequence($p_cn,'s_grpt');
00667 
00668 
00669   // Debit = client
00670   $poste=GetFicheAttribut($p_cn,$e_client,ATTR_DEF_ACCOUNT);
00671   StartSql($p_cn);      
00672   $r=InsertJrnx($p_cn,'d',$p_user->id,$p_jrn,$poste,$e_date,round($amount,2)+round($sum_vat,2),$seq,$periode);
00673   if ( $r == false) { $Rollback($p_cn);exit("error 'user_form_ven.php' __LINE__");}
00674 
00675   // Credit = goods 
00676   for ( $i = 0; $i < $nb_item;$i++) {
00677     if ( $a_good[$i] == ""  ) continue;
00678     $poste=GetFicheAttribut($p_cn,$a_good[$i],ATTR_DEF_ACCOUNT);
00679           
00680     // don't record operation of 0
00681     if ( $a_price[$i]*$a_quant[$i] == 0 ) continue;
00682           
00683     // record into jrnx
00684     $j_id=InsertJrnx($p_cn,'c',$p_user->id,$p_jrn,$poste,$e_date,round($a_price[$i]*$a_quant[$i],2),$seq,$periode);
00685     if ( $j_id == false) { $Rollback($p_cn);exit("error 'user_form_ven.php' __LINE__");}
00686     // always save quantity but in withStock we can find what card need a stock management
00687     if (  InsertStockGoods($p_cn,$j_id,$a_good[$i],$a_quant[$i],'c') == false ) {
00688       $Rollback($p_cn);exit("error 'user_form_ven.php' __LINE__");}
00689     } // end loop
00690   
00691   // Insert Vat
00692 
00693   if ( $a_vat_new  !=  null  ) // no vat
00694 
00695     {
00696       foreach ($a_vat_new as $tva_id => $tva_amount ) {
00697         $poste=GetTvaPoste($p_cn,$tva_id,'c');
00698         if ($tva_amount == 0 ) continue;
00699         $r=InsertJrnx($p_cn,'c',$p_user->id,$p_jrn,$poste,$e_date,round($tva_amount,2),$seq,$periode);
00700         if ( $r == false ) { Rollback($p_cn); exit(" Error 'user_form_ven.php' __LINE__");}
00701       
00702       }
00703     }
00704   echo_debug('user_form_ven.php',__LINE__,"echeance = $e_ech");
00705   $r=InsertJrn($p_cn,$e_date,$e_ech,$p_jrn,"Invoice",round($amount_jrn,2)+round($sum_vat,2),$seq,$periode);
00706   if ( $r == false ) { Rollback($p_cn); exit(" Error 'user_form_ven.php' __LINE__");}
00707   // Set Internal code and Comment
00708   $internal=SetInternalCode($p_cn,$seq,$p_jrn);
00709   $comment=(FormatString($e_comm) == null )?$internal."  client : ".GetFicheName($p_cn,$e_client):FormatString($e_comm);
00710 
00711   // Update and set the invoice's comment 
00712   $Res=ExecSql($p_cn,"update jrn set jr_comment='".$comment."' where jr_grpt_id=".$seq);
00713   if ( $Res == false ) { Rollback($p_cn); exit(" Error 'user_form_ven.php' __LINE__"); };
00714 
00715   if ( isset ($_FILES)) {
00716     if ( sizeof($_FILES) != 0 )
00717     save_upload_document($p_cn,$seq);
00718   }
00719 
00720 // save the quantity, then we can make an invoice
00721   for ( $i=0;$i < $nb_item;$i++) 
00722         {
00723         // don't record operation of 0
00724         if ( $a_price[$i]*$a_quant[$i] == 0 ) continue;
00725         
00726         // insert into the table quant_sold
00727         // Note that negative value are also saved but not the vat !
00728         if (  $a_vat[$i] == -1) {
00729                 $computed_vat=0;
00730                 $vat_code="null";
00731         } else {
00732                 $computed_vat=ComputeVat($p_cn,$a_good[$i],$a_quant[$i],$a_price[$i],$a_vat[$i]);
00733                 $vat_code=$a_vat[$i];
00734         }
00735          $r=ExecSql($p_cn,"select insert_quant_sold ".
00736                     "('".$internal."','".$a_good[$i]."',".$a_quant[$i].",".$a_price[$i]*$a_quant[$i].
00737                     ",".$computed_vat.
00738                     ",".$vat_code.",'".$e_client."')");
00739          if ( $r == false ) { Rollback($p_cn); exit(" Error 'user_form_ven.php' __LINE__"); };
00740         }
00741   Commit($p_cn);
00742   return array($internal,$comment);
00743 }