1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
| <?php
if (!class_exists('phpToPDF'))
require 'phpToPDF.php';
if (!class_exists('Invoice2PDF'))
require 'Invoice2PDF.php';
class SilversquareInvoicePDF extends phpToPDF {
/**
* Numéro de facture
*
* @var string
*/
private $invoiceNumber;
/**
* Nom de la société
*
* @var string
*/
private $name;
/**
* Numéro de client de la société
*
* @var string
*/
private $customerId;
/**
* Tableau contenant les informations sur l'adresse de facturation.
* $address[0] => Numéro
* $address[1] => Rue
* $address[2] => Code Postal
* $address[3] => Ville
* $address[4] => Pays
*
* @var array
*/
private $address;
/**
* Numéro de TVA de la société
*
* @var string
*/
private $vat;
/**
* Date de la facture
*
* @var string
*/
private $invoiceDate;
/**
* Echéance
*
* @var string
*/
private $dueDate;
/**
* Montant total brut
*
* @var double
*/
private $subTotal;
/**
* Montant total net
*
* @var double
*/
private $total;
/**
* Remise éventuellement accordée.
*
* @var double
*/
private $discount;
/**
* Tableau contenant le nom de la taxe et son pourcentage.
* $tax[nomTaxe]=pourcentage
*
* @var array
*/
private $tax;
/**
* Tableau contenant les produits
* $products[x][0] => Nom de l'article
* $products[x][1] => Quantité
* $products[x][2] => Prix unitaire
* $products[x][3] => Prix
* $products[x][4] => Commentaire éventuel
*
* @var array
*/
private $products;
/**
* Texte qui sera affiché dans le "Watermark" de la facture
* (Duplicata, Rappel, ...)
*
* @var string
*/
private $waterMarkText = null;
/**
* Variable indiquant la destination du fichier PDF généré.
* cfr Documentation de FPDF: http://www.fpdf.org/fr/doc/output.htm
* @var string
*/
private $destination;
/*
* Ensemble des fonctions "GET".
* @param: -
* @return : var PHP
*/
public function getInvoiceNumber() {
return $this->invoiceNumber;
}
public function getName() {
return $this->name;
}
public function getCustomerId() {
return $this->customerId;
}
public function getAddress() {
return $this->address;
}
public function getVat() {
return $this->vat;
}
public function getInvoiceDate() {
return $this->invoiceDate;
}
public function getDueDate() {
return $this->dueDate;
}
public function getSubTotal() {
return $this->subTotal;
}
public function getTotal() {
return $this->total;
}
public function getDiscount() {
return $this->discount;
}
public function getTax() {
return $this->tax;
}
public function getProducts() {
return $this->products;
}
public function getWaterMarkText() {
return $this->waterMarkText;
}
public function getDestination() {
return $this->destination;
}
/*
* Ensemble des fonctions "SET".
* @param: var PHP
* @return: -
*/
public function setInvoiceNumber ($var) {
$var = trim($var);
if (!isset($var{0}))
throw new BizException("La facture ne possède pas de numéro!");
$this->invoiceNumber = utf8_decode($var);
}
public function setName ($var) {
$var = trim($var);
if (!isset($var{0}))
throw new BizException("Pas de nom de société pour la facture!");
$this->name = utf8_decode($var);
}
public function setCustomerId ($var) {
$var = trim($var);
if (!isset($var{0}))
throw new BizException("La société ne possède pas de numéro de client!");
$this->customerId = utf8_decode($var);
}
public function setAddress ($var) {
if (!is_array($var) || count($var) < 5 )
throw new BizException("L'adresse de la société n'est pas correcte.");
for ($i=0,$count=count($var);$i<$count;$i++)
$var[$i] = utf8_decode($var[$i]);
$this->address = $var;
}
public function setVat ($var) {
$var = trim($var);
if (!isset($var{0}))
throw new BizException("Aucun numéro de TVA de société n'est indiqué.");
$this->vat = utf8_decode($var);
}
public function setInvoiceDate ($var) {
$var = trim($var);
if (!isset($var{0}))
throw new BizException("La facture ne possède pas de date de création.");
$this->invoiceDate = utf8_decode($var);
}
public function setDueDate ($var) {
$var = trim($var);
if (!isset($var{0}))
throw new BizException("La facture ne possède pas de date d'échéance.");
$this->dueDate = utf8_decode($var);
}
public function setSubTotal ($var) {
$var = trim($var);
if (!isset($var))
throw new BizException("La facture ne possède pas de sous-total.");
$this->subTotal = utf8_decode($var);
}
public function setTotal ($var) {
$var = trim($var);
if (!isset($var))
throw new BizException("La facture ne possède pas de total.");
$this->total = utf8_decode($var);
}
public function setDiscount ($var) {
$var = trim($var);
if (!isset($var))
throw new BizException("La facture ne possède pas de réduction.");
$this->discount = utf8_decode($var);
}
public function setTax ($var) {
if (!is_array($var))
throw new BizException("Erreur de donnée pour le tableau des taxes.");
$this->tax = $var;
}
public function setProducts ($var) {
if (!is_array($var) || count($var)<1)
throw new BizException("Les articles de la facture sont manquants.");
for ($i=0,$count=count($var);$i<$count;$i++)
for($j=0;$j<5;$j++)
$var[$i][$j] = utf8_decode($var[$i][$j]);
$this->products = $var;
}
public function setWaterMarkText($var) {
$var = trim($var);
$this->waterMarkText = $var;
}
public function setDestination($var) {
$this->destination = $var;
}
/**
* Constructeur de la classe
*
* @param string $destination
* @param string $waterMarkText
*/
public function __construct($destination = 'D', $waterMarkText = '') {
parent::__construct();
$this->setDestination($destination);
if ($waterMarkText != '')
$this->setWaterMarkText($waterMarkText);
}
/**
* Fonction redéfinie par rapport à la classe FPDF (ancêtre).
* Elle est appelée automatiquement à chaque début de page.
*
*/
public function Header() {
require PATH.'/config.php';
//Logo Silversquare
$this->Image('img/logo.jpg',94,22,30,35);
//Numéro de facture
$this->SetFont('Arial','B',12);
$this->Text(155,54, $invoiceLabel['invoiceNumber'].$this->getInvoiceNumber());
$this->Line(153,56,197,56);
// WaterMark
if ($this->getWaterMarkText() != null)
$this->waterMark($this->getWaterMarkText(),array(10,52));
//Saut de ligne
$this->Ln(65);
}
/**
* Fonction redéfinie par rapport à la classe FPDF (ancêtre).
* Elle est appelée automatiquement à chaque fin de page.
*
*/
public function Footer() {
require PATH.'/config.php';
//Positionnement à 2,5 cm du bas
$this->SetY(-25);
$this->SetFont('Arial','',9);
$this->Write(4,$SilverSquareInfo);
$this->SetY(-15);
$this->Cell(165);
//Numéro de page
$this->Cell(20,5,$invoiceLabel['page'].$this->PageNo().'/{nb}',1,1,'C');
}
/**
* Fonction redéfinie par rapport à la classe FPDF (ancêtre).
* Elle est appelée pour terminer un fichier PDF.
*
*/
public function Output() {
if ($this->destination == "F") {
if (Invoice2PDF::createFolder($this->getInvoiceDate()) != true)
throw new BizException("Erreur lors de l'appel à createFolder depuis SilversquareInvoicePDF");
$url = Invoice2PDF::getURIPDF($this->getInvoiceDate(),$this->getName(),$this->getInvoiceNumber());
parent::Output($url,$this->destination);
} else {
$name = preg_replace('` `','_',$this->getName());
if ($this->getWaterMarkText() == null)
parent::Output($this->getInvoiceNumber().'_'.Invoice2PDF::stripAccents($name).'.pdf',$this->destination);
else
parent::Output($this->getInvoiceNumber().'_'.Invoice2PDF::stripAccents($name).'_'.preg_replace('` `','_',$this->getWaterMarkText()).'.pdf',$this->destination);
}
}
/**
* Fonction qui permet de créer les "watermark" sur la facture
*
* @param string $text
* @param array $positions [0]=>x [1]=>y
*/
public function waterMark( $text, $positions ) {
$this->SetFont('Arial','B',50);
$this->SetTextColor(230,230,230);
$this->Text($positions[0],$positions[1],$text);
$this->SetTextColor(0,0,0);
}
/**
* Fonction qui écrit les informations signalétique de la facture.
*
*/
public function writeAddress() {
require PATH.'/config.php';
//Ajout de l'image Client
$this->Image('img/client.jpg',10,65,124,5);
//Ajout de l'image Details
$this->Image('img/details.jpg',153,65,49,5);
$adresse = $this->getAddress();
$arraySignaletique = array(
array($invoiceLabel['name'],$this->getName(),$invoiceLabel['date'],$this->formatDate($this->getInvoiceDate())),
array($invoiceLabel['street'],$adresse[1],$invoiceLabel['customerId'],$this->getCustomerId()),
);
foreach($arraySignaletique as $arrayLigne) {
$this->SetFont('Arial','U',10);
$this->Cell(20,5,$arrayLigne[0],0,0,'L');
$this->SetFont('Arial','',10);
$this->Cell(103,5,$arrayLigne[1],'B',0,'L');
$this->Cell(19); // Décalage
$this->Cell(24,5,$arrayLigne[2],0,0,'L');
$this->Cell(24,5,$arrayLigne[3],'B',1,'R');
}
$this->SetFont('Arial','U',10);
$this->Cell(20,5,$invoiceLabel['country'],0,0,'L');
$this->SetFont('Arial','',10);
$this->Cell(20,5,$adresse[4],'B',0,'L');
$this->SetFont('Arial','U',10);
$this->Cell(20,5,$invoiceLabel['postCode'],0,0,'L');
$this->SetFont('Arial','',10);
$this->Cell(15,5,$adresse[2],'B',0,'L');
$this->SetFont('Arial','U',10);
$this->Cell(8,5,$invoiceLabel['city'],0,0,'L');
$this->SetFont('Arial','',10);
$this->Cell(40,5,$adresse[3],'B',0,'L');
$this->Cell(19); // Décalage
$this->Cell(24,5,$invoiceLabel['periodFrom'],0,0,'L');
$this->Cell(24,5,$this->getPeriod($this->getDueDate(),true),'B',1,'R');
$this->SetFont('Arial','U',10);
$this->Cell(20,5,$invoiceLabel['vat'],0,0,'L');
$this->SetFont('Arial','',10);
$this->Cell(103,5,$this->getVat(),'B',0,'L');
$this->Cell(19); // Décalage
$this->Cell(24,5,$invoiceLabel['periodTo'],0,0,'L');
$this->Cell(24,5,$this->getPeriod($this->getDueDate(),false),'B',1,'R');
$this->Ln(5);
}
/**
* Fonction qui écrit le tableau des articles.
*/
public function writeProducts() {
require PATH.'/config.php';
/*
* On commence à paramétrer le tableau d'affichage des produits
*/
$proprietesTableau = array(
'BRD_COLOR' => array(128,128,128), // Couleur de la bordure (RVB)
'BRD_SIZE' => '0.3', // Taille de la bordure (mm)
'TB_ALIGN' => 'R', // Alignement par défaut du texte dans le tableau
'L_MARGIN' => 0, // Décalage à gauche du début du tableau.
);
$proprieteHeader = array(
'T_COLOR' => array(0,0,0), // Couleur du header
'T_SIZE' => 10, // Taille du header
'T_FONT' => 'Arial', // Police du header
'T_ALIGN_COL0' => 'C', // Alignement du header sur la 1ère colonne
'T_ALIGN' => 'C', // Alignement du header
'V_ALIGN' => 'M', // Alignement vertical du header
'T_TYPE' => 'B', // Décoration du header (gras, italique,...)
'LN_SIZE' => 7, // Saut de ligne dans les cellules du header
'BG_COLOR_COL0' => array(255, 255, 255),// Couleur de fond de la première colonne
'BG_COLOR' => array(255, 255, 255), // Couleur de fond du header
'BRD_COLOR' => array(128,128,128), // Couleur des bords du header
'BRD_SIZE' => 0.3, // Epaisseur de la bordure (mm)
'BRD_TYPE' => '1', // Type de bordure.
'BRD_TYPE_NEW_PAGE' => '', // ?
);
$contenuHeader = array(
125, 20, 25, 23, // Largeur en mm des colonnes
$invoiceLabel['description'], $invoiceLabel['quantity'], $invoiceLabel['unitPrice'], $invoiceLabel['netPrice'] // Titre (header) des colonnes
);
$proprieteContenu = array(
'T_COLOR' => array(0,0,0), // IDEM que $proprieteHeader
'T_SIZE' => 10,
'T_FONT' => 'Arial',
'T_ALIGN_COL0' => 'L',
'T_ALIGN' => 'R',
'V_ALIGN' => 'M',
'T_TYPE' => '',
'LN_SIZE' => 6,
'BG_COLOR_COL0' => array(255, 255, 255),
'BG_COLOR' => array(255,255,255),
'BRD_COLOR' => array(128,128,128),
'BRD_SIZE' => '0.3',
'BRD_TYPE' => 'LR',
'BRD_TYPE_NEW_PAGE' => '',
);
$contenuTableau = array("[Z]","[Z]","[Z]","[XZ]");
$arrayProduct = $this->getProducts();
if (count($arrayProduct) > 0) {
foreach($arrayProduct as $ligne) {
$countContenu = count($contenuTableau);
$contenuTableau[$countContenu] = "[BUSY]".$ligne[0];
$contenuTableau[++$countContenu] = "[Y]".$ligne[1];
$contenuTableau[++$countContenu] = "[Y]".number_format($ligne[2],2,',','')."";
$contenuTableau[++$countContenu] = "[XY]".number_format($ligne[3],2,',','')."";
if (isset($ligne[4]{0})) {
$arrayComment = preg_split('/[[:cntrl:]]/',$ligne[4],-1,PREG_SPLIT_NO_EMPTY );
for ($j=0,$countj = count($arrayComment); $j<$countj; $j++) {
if ($arrayComment[$j]{1}) {
$contenuTableau[++$countContenu] = "[Y]".$arrayComment[$j];
$contenuTableau[++$countContenu] = "[Y]";
$contenuTableau[++$countContenu] = "[Y]";
$contenuTableau[++$countContenu] = "[XY]";
}
}
}
$contenuTableau[++$countContenu] = " ";
$contenuTableau[++$countContenu] = " ";
$contenuTableau[++$countContenu] = " ";
$contenuTableau[++$countContenu] = "[X]";
}
}
$this->drawTableau($this, $proprietesTableau, $proprieteHeader, $contenuHeader, $proprieteContenu, $contenuTableau);
}
/**
* Fonction qui écrit les informations finales de la facture.
* (Détails de payement, montant total etc)
*/
public function writeEndInvoice() {
require PATH.'/config.php';
/*
* On va indiquer les détails de payement.
*/
$ySauv = $this->GetY();
$this->SetY(-70);
$newY = $this->getY(); // S'il ne reste pas 7cm pour inscrire la fin, on crée une nouvelle page.
if ($newY < $ySauv) {
$this->AddPage();
} else {
$this->SetY($ySauv);
}
$ySauv = $this->GetY();
$this->Ln(4);
$this->Cell(20);
$this->SetFont('Arial','BU',8);
$this->SetFillColor(200,200,200);
$this->Cell(102,10,$invoiceLabel['paymentDetails'],1,1,"C");
$this->Cell(20);
$this->Cell(51,4,$invoiceLabel['dueDate'],"L",0,"C");
$this->Cell(51,4,$invoiceLabel['totalAmount'],"R",1,"C");
$this->SetFont('Arial','',8);
$this->Cell(20);
$this->Cell(51,4,$this->formatDate($this->getDueDate()),"L",0,"C");
$this->Cell(51,4,number_format($this->getTotal(),2,',','').'',"R",1,"C");
$this->SetFont('Arial','BU',8);
$this->Cell(20);
$this->Cell(51,4,$invoiceLabel['communication'],"L",0,"C");
$this->Cell(51,4,$invoiceLabel['bankAccount'],"R",1,"C");
$this->SetFont('Arial','',8);
$this->Cell(20);
$y = $this->GetY();
$x = $this->GetX();
$this->MultiCell(51,4,"FACT ".$this->getInvoiceNumber()."\n\n\n\n","LB","C");
$this->SetY($y);
$this->SetX($x+51);
$this->MultiCell(51,4,$bankInfo,"RB","C");
/*
* On affiche les totaux, les taxes, le total TVAC et la date d'échéance.
*/
$this->SetY($ySauv);
$this->SetFont('Arial','I',10);
$this->Cell(167,4,$invoiceLabel['total'],0,0,"R");
$this->SetFont('Arial','',10);
$this->SetDrawColor(128,128,128);
$this->SetFillColor(200,200,200);
$this->Cell(23,4,number_format($this->getSubTotal(),2,',','').'',1,1,"R",1);
$this->SetFont('Arial','I',10);
if ($this->getDiscount() != '' && $this->getDiscount() != "0.000") {
$this->Cell(167,4,$invoiceLabel['discount'],0,0,"R");
$this->SetFont('Arial','',10);
$this->SetDrawColor(128,128,128);
$this->SetFillColor(200,200,200);
$this->Cell(23,4,number_format($this->getDiscount(),2,',','').'',1,1,"R",1);
}
/*
* Boucle sur les différentes taxes présentes dans le CRM.
* Sauf si le sous-total - remise = total => Pas de taxe.
*/
if (count($this->getTax())!=0) {
$montant = $this->getSubTotal() - $this->getDiscount();
if (floatval($montant) < floatval($this->getTotal())) {
foreach ($this->getTax() as $nom=>$pourcentage) {
$this->Cell(122);
$this->Cell(20,4,$nom,0,0,"C");
$this->Cell(25,4,number_format($pourcentage,2,',','').'%',1,0,"R");
$valPourcentage = $montant/100 * $pourcentage;
$this->Cell(23,4,number_format($valPourcentage,2,',','').'',1,1,"R",1);
}
} else {
$this->Cell(122);
$this->Cell(20,4,'VAT',0,0,"C");
$this->Cell(25,4,'0.00%',1,0,"R");
$this->Cell(23,4,'0.00',1,1,"R",1);
}
}
//Montant total
$this->Cell(142);
$this->SetFont('Arial','B',10);
$this->Cell(25,4,$invoiceLabel['totalAmount'],0,0,"C");
$this->Cell(23,4,number_format($this->getTotal(),2,',','').'',1,1,"R",1);
// Date d'échéance
$this->Ln(5);
$this->Cell(142);
$this->SetFont('Arial','B',10);
$this->Cell(25,4,$invoiceLabel['dueDate'],0,0,"C");
$this->Cell(23,4,$this->formatDate($this->getDueDate()),1,1,"R",1);
}
/**
* Fonction qui retourne une date en format européen à partir d'une
* date au format US
*
* @param string $date au format US yyyy-mm-dd
* @return string date au format européen dd/mm/yyyy
*/
private function formatDate($date) {
$timestamp = strtotime($date);
if ($timestamp == false)
throw new BizException("La date (".$date.") n'a pas le format US yyyy-mm-dd");
return date('d/m/Y',$timestamp);
}
/**
* Fonction qui détermine la période de la facture (mois et année de l'échéance)
*
* @param string $date au format US.
* @param boolean $begin true si c'est pour avoir la date de début, false pour la date de fin.
* @return string date
*/
private function getPeriod($date,$begin) {
$timestamp = strtotime($date);
if ($timestamp == false)
throw new BizException("La date (".$date.") n'a pas le format US yyyy-mm-dd");
if ($begin)
return date('01/m/Y',$timestamp);
else return date('t/m/Y',$timestamp);
}
}
?> |
Partager