Ticket #41 (closed enhancement: fixed)

Opened 18 months ago

Last modified 14 months ago

Converting number to letter

Reported by: dev@… Owned by: florian
Priority: minor Milestone: 0.4
Component: pdf_generation Version: 0.2
Keywords: number to letter Cc:

Description

Hi,

I would like to get the general idea on how to implement a number to letter feature into phpaga's quotation and billing system, i tried to include a class into the billing plugin, that add's a new row in my quotation but it only shows zero and not the number in letter, I think that it is because of a filtering somewhere and I can't figure out where to find that or if there is a bette and cleaner way to do it

I added that to my moded bill plugin

include_once(PHPAGA_LIBPATH."chiffreEnLettre.php"); // the number to letter class

and at the end of the file

//hack chiffre en lettre
    $lettre=new ChiffreEnLettre();
    $lettre->Conversion($endsum);
    $details->add_separator ();
    $details->add_detail ("Arrete la presente a : ", $lettre);
// fin chiffre en lettre
    return PHPAGA_ERR_NONE;

Regards

Attachments

chiffreEnLettre.php (10.0 kB) - added by dev@… 18 months ago.
class to convert numbers to letters ( FR version)

Change History

Changed 18 months ago by dev@…

class to convert numbers to letters ( FR version)

Changed 18 months ago by florian

  • status changed from new to assigned

Hi,

the function add_detail() (as defined in the class phpaga_bill_details in lib/bills.php) expects a float as its second parameter, and any non-numeric text will be "reformatted" to a number.

You can modify add_detail() like this:

    function add_detail ($text = "", $amount = "")
    {

        if (is_numeric($amount))
            $amount = number_format($amount, 
                                    $this->curr_decimals, 
                                    PHPAGA_SEPARATOR_DECIMALS, 
                                    PHPAGA_SEPARATOR_THOUSANDS);

        $this->details[] = array("text" => $text,
                                 "amount" => $amount." ".$this->curr_name);
    }

This way, when a number (int or float) is added, it is formatted correctly, otherwise a textual representation of the number is expected and therefore it is not formatted.

Hope this helps. If it is helpful for you I can also apply this to phpaga, so that you don't have to worry about future upgrades.

Changed 18 months ago by dev@…

I would like to thank you for your quick reply, I wish that I'll not abuse by asking you a way to output the "number to letter" just bellow the table showing the detail of the invoice/quotation in the output pdf right under the total

I have just installed phpaga as a billing application in my company and I haven't yet mastered the way it is writen

thanx again for your support

Changed 18 months ago by florian

The simple solution is to add the textual representation of the number as the last detail to the details array; then it will be printed automatically as the last row of the billing details table. This can be handled from within a billing plugin and is likely to be safe for future upgrades.

If you would like to print the textual representation of the number without adding it to the details array, you will need to modify the file pdftemplates/invoice.tpl.php after the section that outputs the details

$this->ezTable($details,
[...]

You will need to set a variable (for example $numtoletters) with $info['bill_endsum'] converted by your function, and place it manually in the document with

$this->ezText($numtoletters, PHPAGA_PDF_FONT_SIZE_NORMAL);

or with

$this->addText($posx,
               $posy,
               PHPAGA_PDF_FONT_SIZE_NORMAL,
               $numtoletters);

You may need to play with the coordinates in order to place the output at the desired position.

Regards

Changed 18 months ago by dev@…

Thanx man it's working like a charm :)

I used another function to convert my numbers

so I just included it and call the function you told me to print it out on pdf

numtoletters.php

<?
// ======================================================================================

// fonction de convertion de chiffres en chiffres ecrit en lettres pour facturation      

// ======================================================================================



function getChiffre($chiffre) 

{

   $retour=" ";

   switch($chiffre)

   {

      case "0": $retour=" "; break ;

	  case "1": $retour="un"; break ;

	  case "2": $retour="deux"; break ;

	  case "3": $retour="trois"; break ;

	  case "4": $retour="quatre"; break ;

	  case "5": $retour="cinq"; break ;

	  case "6": $retour="six"; break ;

	  case "7": $retour="sept"; break ;

	  case "8": $retour="huit"; break ;

	  case "9": $retour="neuf"; break ;

	  default:return false ; break ;

   }

   return $retour;

}

//*****************************************************



function getDix($nombre) 

{

   $retour=" ";

   $partie_dix="";

   $partie_unitaire="";

   $partie_dix=substr($nombre,0,1);

   $partie_unit=substr($nombre,1,2);

   if($partie_dix==1)

   {

       switch($partie_unit)

       {

	    case 0: $retour="dix"; break ;

	    case 1: $retour="onze"; break ;

	    case 2: $retour="douze"; break ;

	    case 3: $retour="treize"; break ;

	    case 4: $retour="quatorze"; break ;

	    case 5: $retour="quinze"; break ;

	    case 6: $retour="seize"; break ;

	    case 7: $retour="dix-sept"; break ;

	    case 8: $retour="dix-huit"; break ;

	    case 9: $retour="dix-neuf"; break ;       

	    default:return false ; break ;

      }

  }

  else

  {

	   switch($partie_dix)

       {

	    case 0: $retour=" "; break ;

		case 2: $retour="vingt"; break ;

	    case 3: $retour="trente"; break ;

	    case 4: $retour="quarente"; break ;

	    case 5: $retour="cinquante"; break ;

	    case 6: $retour="soixante"; break ;

	    case 7: $retour="soixante";break ;

	    case 8: $retour="quatre-vingt"; break ;

	    case 9: $retour="quatre-vingt"; break ;

	    default:return false ; break ;

       }

	   $partie_unitaire=getChiffre($partie_unit);

	   

	   if(!$partie_unitaire) return false ;  //***dans le cas erreur

	   if(($partie_dix==7) or ($partie_dix==9))

	   { 

	      $partie_plus=getDix("1".$partie_unit);

		  if(!$partie_plus) return false;  //***dans le cas erreur

	      $retour=$retour." ".$partie_plus;

	   }

	   else $retour=$retour." ".$partie_unitaire;

	   

  }



return  $retour; 

}

//***************************************************

function getCent($nombre)

{

	 $retour=" ";

	 $partie_cent=substr($nombre,0,1);

	 $partie_dix=substr($nombre,1);

	 

	 if(getChiffre($partie_cent)) $_cent=getChiffre($partie_cent);

	 else return false;

	 

	 if(getDix($partie_dix)) $_dix=getDix($partie_dix);

	 else return false;

	

	 switch($partie_cent)

	 {

		  case 0: $retour.=$_dix; break;

		  case 1: $retour.=" cent ".$_dix; break;

		  case 2: 

		  case 3: 

		  case 4: 

		  case 5: 

		  case 6: 

		  case 7: 

		  case 8: 

		  case 9: $retour.=$_cent." cent ".$_dix; break;

		  default : return false;

	 }

	

	return  $retour;

}

//***************************************************

function getMille($nombre,$unite,$pos)

{

 $lettre_partie_1="";

 $lettre_partie_2="";

 $retour=" ";

 

 $taille=strlen($nombre);

 $patie_1=substr($nombre,0,$taille-$pos);

 $patie_2=substr($nombre,-$pos,$pos);

 

if(($taille-$pos)==1)  $lettre_partie_1=getChiffre($patie_1);

if(($taille-$pos)==2)  $lettre_partie_1=getDix($patie_1);

if(($taille-$pos)==3)  $lettre_partie_1=getCent($patie_1);

//echo $lettre_partie_1;

$lettre_partie_1=trim($lettre_partie_1);



if($lettre_partie_1=="un" and $unite=="mille") $lettre_partie_1=" ";  //**pour éléminer un dans 1000



$lettre_partie_2=Calculer((String)$patie_2);





if((trim($lettre_partie_1) and $lettre_partie_1!="un")) $pluriel="s"; else $pluriel="";

if($lettre_partie_1) $retour=$lettre_partie_1." ".$unite.$pluriel." ".$lettre_partie_2;

else $retour=" ".$lettre_partie_2;





return  $retour;

}



//***************************************************

function Calculer($nombre)

{

	  $nombre_en_lettre="";

	  $taille=strlen($nombre);

	  switch($taille)

	  {

	   case 1:  $nombre_en_lettre=getChiffre($nombre); break;

	   case 2:  $nombre_en_lettre=getDix($nombre);break;

	   case 3:  $nombre_en_lettre=getCent($nombre);break;

	   case 4:  

	   case 5:  

	   case 6:  $nombre_en_lettre=getMille($nombre,"mille",3); break;

	   case 7:  

	   case 8:  

	   case 9:  $nombre_en_lettre=getMille($nombre,"million",6);break;

	   case 10: 

	   case 11: 

	   case 12: $nombre_en_lettre=getMille($nombre,"milliard",9);break;

	   default:return false;break;

	  }

	

	if(trim($nombre_en_lettre)) return $nombre_en_lettre;

	else return false;

} 

//**********************************************************

//                fonction mére                             

//**********************************************************

function numtoletters($nombre,$devise,$centime)

{

	$nombre_en_lettre="";

	$nombre=trim($nombre);                     //***supprimer les spaces

	if(strpos($nombre," "))  return "";     //*** vérifier que la chaine ne coentient aucun espace
	$nbr = explode(".",$nombre); 	 //*** separateur de decimales "."

	if(count($nbr)>2) return "";

	else if(count($nbr)==1) 

	{

	   if(Calculer($nbr[0])) $nombre_en_lettre=Calculer($nbr[0])." ".$devise;

	else return "";

	}

	else { 

	        $partie1=Calculer($nbr[0]); 

	  $partie2=Calculer(substr($nbr[1]."0",0,2));

	 //if($partie1==false or $partie2==false) return "*$partie2*";

	        if(trim($partie1)) $nombre_en_lettre=$partie1." ".$devise;

	  if(trim($partie2)) $nombre_en_lettre.=" ".$partie2." ".$centime;

	}

	return $nombre_en_lettre;

}

//********************************************
?>

I'll translate it to english if needed

Regards

Changed 18 months ago by anonymous

sorry double post

and add that to invoice.tpl.php @ line 213

//num to letter
include_once(PHPAGA_LIBPATH."numtoletters.php");
if ($info['bill_endsum']>0)
	{
		$the_number=$info['bill_endsum'];
		$the_kind="facture";
	}
else if ($info['quot_endsum']>0)
	{
		$the_number=$info['quot_endsum'];
		$the_kind="proforma";	
	}
$this->ezText(" ", PHPAGA_PDF_FONT_SIZE_NORMAL);
$this->ezText("Arreter la presente ".$the_kind." a la somme de : ", PHPAGA_PDF_HEADER_NEXTLINE);
$this->ezText(numtoletters($the_number,"Dinars,","Centimes."), PHPAGA_PDF_FONT_SIZE_NORMAL);
//end

Changed 14 months ago by florian

  • status changed from assigned to closed
  • resolution set to fixed

Thanks for posting this patch. If there is an interest to include this in "standard" phpaga please reopen this ticket.

Add/Change #41 (Converting number to letter)

Author



Change Properties
<Author field>
Action
as closed
 
Note: See TracTickets for help on using tickets.