diff --git a/Classes/Invoice.php b/Classes/Invoice.php index 1135bc4..e435ee5 100644 --- a/Classes/Invoice.php +++ b/Classes/Invoice.php @@ -150,6 +150,13 @@ class Invoice */ private $pdf; + /** + * Json of currencies + * + * @var object + */ + private $currencies; + /** * Create a new invoice instance. * @@ -225,7 +232,7 @@ public function addItem($name, $price, $ammount = 1, $id = '-', $imageUrl = null 'name' => $name, 'price' => $price, 'ammount' => $ammount, - 'totalPrice' => number_format(bcmul($price, $ammount, $this->decimals), $this->decimals), + 'totalPrice' => number_format(bcmul($price, $ammount, $this->decimals), $this->decimals, $this->getDecPoint(), $this->getThousandsSep()), 'id' => $id, 'imageUrl' => $imageUrl, ])); @@ -256,10 +263,36 @@ public function popItem() */ public function formatCurrency() { - $currencies = json_decode(file_get_contents(__DIR__.'/../Currencies.json')); + if(null === $this->currencies) { + $this->currencies = json_decode(file_get_contents(__DIR__ . '/../Currencies.json')); + } $currency = $this->currency; - return $currencies->$currency; + return $this->currencies->$currency; + } + + /** + * return the decimal point for number format + * + * @method getDecPoint + * + * @return string + */ + protected function getDecPoint() + { + return $this->formatCurrency()->dec_point ?: "."; + } + + /** + * return the thousands_sep for number format + * + * @method getThousandsSep + * + * @return string + */ + protected function getThousandsSep() + { + return $this->formatCurrency()->thousands_sep ?: ","; } /** @@ -269,7 +302,7 @@ public function formatCurrency() * * @return int */ - private function subTotalPrice() + public function subTotalPrice() { return $this->items->sum(function ($item) { return bcmul($item['price'], $item['ammount'], $this->decimals); @@ -285,7 +318,7 @@ private function subTotalPrice() */ public function subTotalPriceFormatted() { - return number_format($this->subTotalPrice(), $this->decimals); + return number_format($this->subTotalPrice(), $this->decimals, $this->getDecPoint(), $this->getThousandsSep()); } /** @@ -295,7 +328,7 @@ public function subTotalPriceFormatted() * * @return int */ - private function totalPrice() + public function totalPrice() { return bcadd($this->subTotalPrice(), $this->taxPrice(), $this->decimals); } @@ -309,7 +342,7 @@ private function totalPrice() */ public function totalPriceFormatted() { - return number_format($this->totalPrice(), $this->decimals); + return number_format($this->totalPrice(), $this->decimals, $this->getDecPoint(), $this->getThousandsSep()); } /** @@ -319,7 +352,7 @@ public function totalPriceFormatted() * * @return float */ - private function taxPrice(Object $tax_rate = null) + public function taxPrice(Object $tax_rate = null) { if (is_null($tax_rate)) { $tax_total = 0; @@ -349,7 +382,7 @@ private function taxPrice(Object $tax_rate = null) */ public function taxPriceFormatted($tax_rate) { - return number_format($this->taxPrice($tax_rate), $this->decimals); + return number_format($this->taxPrice($tax_rate), $this->decimals, $this->getDecPoint(), $this->getThousandsSep()); } /** diff --git a/Classes/PDF.php b/Classes/PDF.php index 30d2cae..31156cd 100644 --- a/Classes/PDF.php +++ b/Classes/PDF.php @@ -1,12 +1,12 @@ - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ + * This file is part of consoletvs/invoices. + * + * (c) Erik Campobadal + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ namespace ConsoleTVs\Invoices\Classes; @@ -33,7 +33,21 @@ class PDF */ public static function generate(Invoice $invoice, $template = 'default') { - $template = strtolower($template); + $customHtmlTemplate = false; + if(self::containsHtml($template)) { + $customHtmlTemplate = true; + $filename = str_replace('.','',uniqid('blade_',true)); + $path = storage_path("framework/views/tmp"); + View::addLocation($path); + $filepath = $path . DIRECTORY_SEPARATOR . "$filename.blade.php"; + if (!is_dir($path)) { + mkdir($path, 0777, true); + } + file_put_contents($filepath, trim($template)); + $template = $filename; + } else { + $template = 'invoices::'.strtolower($template); + } $options = new Options(); @@ -54,9 +68,18 @@ public static function generate(Invoice $invoice, $template = 'default') $GLOBALS['with_pagination'] = $invoice->with_pagination; - $pdf->loadHtml(View::make('invoices::'.$template, ['invoice' => $invoice])); + $pdf->loadHtml(View::make($template, ['invoice' => $invoice])); $pdf->render(); + if($customHtmlTemplate){ + unlink($filepath); + } + return $pdf; } + + protected static function containsHtml($string) + { + return preg_match("/<[^<]+>/",$string,$m) !== 0; + } } diff --git a/Currencies.json b/Currencies.json index d0397fd..6a367a6 100644 --- a/Currencies.json +++ b/Currencies.json @@ -24,7 +24,9 @@ "decimal_digits": 2, "rounding": 0, "code": "EUR", - "name_plural": "euros" + "name_plural": "euros", + "thousands_sep": ".", + "dec_point": "," }, "AED": { "symbol": "AED",