-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e8ebc33
Showing
8 changed files
with
335 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php defined('SYSPATH') or die('No direct script access.'); | ||
|
||
class Bill extends Kohana_Bill {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php defined('SYSPATH') or die('No direct script access.'); | ||
|
||
class Kohana_Bill extends View | ||
{ | ||
static $template = ''; | ||
|
||
public static function factory($file = NULL, array $data = NULL) | ||
{ | ||
if(is_null($data)) | ||
{ | ||
$data = Kohana::config('bill.default.data'); | ||
} | ||
|
||
if(is_null($file) || !array_key_exists($file, Kohana::config('bill'))) | ||
{ | ||
$file = Kohana::config('bill.default.template'); | ||
} | ||
else | ||
{ | ||
$file = Kohana::config('bill.'.$file.'.template'); | ||
} | ||
|
||
self::$template = $file; | ||
|
||
$data['totalHTVA'] = 0; | ||
$data['totalTVA'] = 0; | ||
$data['totalTTC'] = 0; | ||
|
||
if(!array_key_exists('dest', $data)) | ||
{ | ||
$data['dest'] = array(); | ||
} | ||
if(!array_key_exists('infos', $data)) | ||
{ | ||
$data['infos'] = array(); | ||
} | ||
|
||
return new Kohana_Bill($data); | ||
} | ||
|
||
public function __construct(array $data = NULL) | ||
{ | ||
if ($data !== NULL) | ||
{ | ||
$this->_data = $data + $this->_data; | ||
} | ||
} | ||
|
||
public function dest(array $dest) | ||
{ | ||
$this->_data['dest'] = Arr::merge($this->_data['dest'], $dest); | ||
} | ||
|
||
public function infos(array $infos) | ||
{ | ||
$this->_data['infos'] = Arr::merge($this->_data['infos'], $infos); | ||
} | ||
|
||
public function item(array $item) | ||
{ | ||
$this->_data['items'][] = $item; | ||
} | ||
|
||
public function render($file = NULL) | ||
{ | ||
if(isset($this->_data['items'])) | ||
{ | ||
$same_TVA = true; | ||
|
||
$temp_TVA = 0; | ||
|
||
foreach($this->_data['items'] as $item) | ||
{ | ||
if(is_numeric($item['price'])) | ||
{ | ||
if(!isset($item['quantity'])) | ||
{ | ||
$item['quantity'] = 1; | ||
} | ||
|
||
$this->_data['totalHTVA'] = $this->_data['totalHTVA'] + $item['price'] * $item['quantity']; | ||
|
||
if(!isset($item['vat'])) | ||
{ | ||
$item['vat'] = $this->_data['vat']; | ||
} | ||
|
||
if ($temp_TVA == 0) | ||
{ | ||
$temp_TVA = $item['vat']; | ||
} | ||
|
||
$this->_data['totalTVA'] = $this->_data['totalTVA'] + $item['price'] * $item['quantity'] * ($item['vat']/100); | ||
|
||
if($item['vat'] != $temp_TVA) | ||
{ | ||
$same_TVA = false; | ||
} | ||
|
||
$temp_TVA = $item['vat']; | ||
} | ||
} | ||
|
||
$this->_data['totalTTC'] = $this->_data['totalHTVA'] + $this->_data['totalTVA']; | ||
|
||
if($same_TVA) | ||
{ | ||
$this->_data['vat'] = '('.$temp_TVA.' %) '; | ||
} | ||
else | ||
{ | ||
$this->_data['vat'] = ''; | ||
} | ||
} | ||
|
||
$html = Twig::factory(self::$template , $this->_data); | ||
|
||
// Turn off strict errors, DOMPDF is stupid like that | ||
$ER = error_reporting(~E_STRICT); | ||
|
||
// Render the HTML to a PDF | ||
$pdf = new DOMPDF; | ||
$pdf->set_paper("a4"); | ||
$pdf->load_html($html); | ||
$pdf->render(); | ||
|
||
// Restore error reporting settings | ||
error_reporting($ER); | ||
|
||
return $pdf->output(); | ||
} | ||
} | ||
|
||
if ( ! defined('DOMPDF_ENABLE_REMOTE')) | ||
{ | ||
// Unfortunately this is a define, not a setting | ||
define('DOMPDF_ENABLE_REMOTE', TRUE); | ||
} | ||
|
||
// Load DOMPDF configuration, this will prepare DOMPDF | ||
require_once Kohana::find_file('vendor', 'dompdf/dompdf/dompdf_config.inc'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php defined('SYSPATH') or die('No direct script access.'); | ||
|
||
return array( | ||
'default' => array( | ||
'template' => 'pdf/default', | ||
|
||
'data' => array( | ||
'vat' => 21, | ||
'sender' => array( | ||
'vat' => 'TVA BE 0000.000.000', | ||
'name' => 'Default SPRL', | ||
'street' => 'Boulevard par defaut, 1', | ||
'city' => '0000 Default Belgique', | ||
'tel' => '0000/00.00.00', | ||
'email' => '[email protected]', | ||
'bank' => 'MYBANK 001-0000000-00', | ||
'iban' => 'IBAN BE00 0000 0000 0000', | ||
'website' => 'www.default.com', | ||
), | ||
), | ||
), | ||
'reaklab' => array( | ||
'template' => 'pdf/default', | ||
|
||
'data' => array( | ||
'vat' => 21, | ||
'sender' => array( | ||
'vat' => 'TVA BE 0827.675.759', | ||
'name' => 'Reaklab SPRL', | ||
'street' => 'Boulevard Initialis, 1', | ||
'city' => 'B-7000 Mons', | ||
'tel' => '0489/33.24.99', | ||
'email' => '[email protected]', | ||
'bank' => 'MYBANK 001-6123729-01', | ||
'iban' => 'IBAN BE43 0016 1237 2901', | ||
'website' => 'www.reaklab.com', | ||
), | ||
), | ||
), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
return array | ||
( | ||
// Leave this alone | ||
'modules' => array( | ||
|
||
// This should be the path to this modules userguide pages, without the 'guide/'. Ex: '/guide/modulename/' would be 'modulename' | ||
'bill' => array( | ||
|
||
// Whether this modules userguide pages should be shown | ||
'enabled' => TRUE, | ||
|
||
// The name that should show up on the userguide index page | ||
'name' => 'Bill', | ||
|
||
// A short description of this module, shown on the index page | ||
'description' => 'Extension de la classe View de Kohana qui génère un PDF de votre facture à partir d\'un template Twig.', | ||
|
||
// Copyright message, shown in the footer for this module | ||
'copyright' => '© 2010–2011 Kévin Rapaille', | ||
) | ||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Configuration | ||
|
||
Bill contient un fichier de config avec une configuration par defaut qui sera utilisée s'il n'y en a aucune de précisée à la création d'une facture ou si Bill ne trouve pas la configuration spécifiée. | ||
|
||
[!!] A l'installation de Bill, n'oubliez pas de préciser votre template par defaut dans le fichier *modules\bill\config\bill.php*. | ||
|
||
### Créer sa propre configuration | ||
|
||
Vous pouvez créer autant de configuration que vous le souhaitez. Voici les informations minimum requise à la création de votre config : | ||
~~~ | ||
[...] | ||
'myConfig' => array( | ||
'template' => 'pdf/myConfigTemplate', | ||
'data' => array( | ||
'vat' => 21, | ||
), | ||
), | ||
[...] | ||
~~~ | ||
|
||
Après, rien ne vous empêche d'ajouter dans **data** des données supplémentaire par defaut que vous souhaiteriez pour pour votre template. | ||
|
||
Par exemple : | ||
|
||
~~~ | ||
[...] | ||
'myConfig' => array( | ||
'template' => 'path/myConfigTemplate', | ||
'data' => array( | ||
'vat' => 21, | ||
'sender' => array( | ||
'vat' => 'TVA BE 1234.567.890', | ||
'name' => 'Awesome Company', | ||
'street' => 'Boulevard Awesome, 1', | ||
'city' => 'B-1000 Brussel', | ||
'tel' => '0470/11.22.33', | ||
'email' => '[email protected]', | ||
'bank' => 'MYBANK 001-1234567-01', | ||
'iban' => 'IBAN BE43 0012 1234 5678', | ||
'website' => 'www.iamawesome.com', | ||
), | ||
), | ||
), | ||
[...] | ||
~~~ | ||
|
||
Et pour l'utiliser : | ||
|
||
~~~ | ||
$pdf = Bill::factory('myConfig'); | ||
~~~ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Utiliser Bill | ||
|
||
### Pour chaque produits | ||
* Si le prix (**price**) est précisé, il sera pris en compte dans les totaux de l'ensemble des produits. | ||
* Si la quantité (**quantity**) est précié, elle sera prise en compte leur du calcul des totaux. | ||
* Si la tva (**vat**) est précisé, elle sera prise en compte, sinon ce sera la tva par défaut précisée dans le fichier de configuration qui sera prise en compte. | ||
|
||
### Exemple d'utilisation | ||
~~~ | ||
$dest = array( | ||
'name' => 'Company Name', | ||
'contact' => 'Mr. Contact', | ||
); | ||
$product1 = array( | ||
'name' => 'product 1', | ||
'description' => 'description of the first product', | ||
'quantity' => '3', | ||
'price' => '1', | ||
); | ||
$product2 = array( | ||
'name' => 'product 2', | ||
'description' => 'description of the second product', | ||
'quantity' => '5', | ||
'price' => '2', | ||
'tva' => '6', | ||
); | ||
$infos = array( | ||
'fact_nb' => '007', | ||
'client_nb' => '008', | ||
'date' => '24/01/2012', | ||
'object' => 'Object of the bill', | ||
); | ||
$pdf = Bill::factory(); | ||
$pdf->dest($dest); | ||
$pdf->item($product1); | ||
$pdf->item($product2); | ||
$pdf->infos($infos); | ||
// Use the PDF as the request response | ||
$this->response->body($pdf); | ||
// Display the PDF in the browser as "my_bill.pdf" | ||
// Remove "inline = TRUE" to force the PDF to be downloaded | ||
$this->response->send_file(TRUE, 'facture.pdf', array('inline' => TRUE)); | ||
~~~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## [Bill]() | ||
- [Configuration](config) | ||
- [Créer son template](template) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Créer son template | ||
|
||
Afin d'utiliser Bill, il est nécessaire de créer au préalable un [template Twig](http://www.twig-project.org/doc/templates.html). | ||
|
||
Cependant, quelques variables/mot clefs sont spécifique à Bill et peuvent être utile lors de la création de votre template : | ||
|
||
## Les totaux | ||
- `{{ totalHTVA }}` pour le total hors TVA | ||
- `{{ totalTVA }}` pour le total de la TVA | ||
- `{{ totalTTC }}` pour le total TVA comprise | ||
|
||
## L'affichage de la TVA | ||
- Si tous les produits ont la même TVA, `{{ vat }}` contiendra *(21 %)* (dans le cadre d'une tva à 21%). | ||
- Sinon, `{{ vat }}` sera vide. | ||
|
||
# Utilisation du template | ||
|
||
Afin de l'utiliser, il vous suffit de modifier le fichier de config *modules\bill\config\bill.php* et de lui préciser votre nouveau template. |