From 2a3611b236f4f99f709f023ea2eadf5e0a8d355b Mon Sep 17 00:00:00 2001 From: Erik Dohmen Date: Sat, 2 Jan 2021 20:15:38 +0100 Subject: [PATCH 1/9] make template as a html string possible --- Classes/PDF.php | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/Classes/PDF.php b/Classes/PDF.php index 30d2cae..dece3fd 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 = 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; + } } From 54b27ab0534e783f9a2389b53f28c9409fdf41ba Mon Sep 17 00:00:00 2001 From: Erik Dohmen Date: Sat, 2 Jan 2021 21:13:04 +0100 Subject: [PATCH 2/9] Fix issue with dot in generated blade file name --- Classes/PDF.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/PDF.php b/Classes/PDF.php index dece3fd..31156cd 100644 --- a/Classes/PDF.php +++ b/Classes/PDF.php @@ -36,7 +36,7 @@ public static function generate(Invoice $invoice, $template = 'default') $customHtmlTemplate = false; if(self::containsHtml($template)) { $customHtmlTemplate = true; - $filename = uniqid('blade_',true); + $filename = str_replace('.','',uniqid('blade_',true)); $path = storage_path("framework/views/tmp"); View::addLocation($path); $filepath = $path . DIRECTORY_SEPARATOR . "$filename.blade.php"; From e6c98ec7049c24e488c6c7d6f0ee39f4201144e5 Mon Sep 17 00:00:00 2001 From: Erik Dohmen Date: Wed, 6 Jan 2021 22:59:16 +0100 Subject: [PATCH 3/9] Update Currencies.json --- Currencies.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Currencies.json b/Currencies.json index d0397fd..a012274 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", From 5ad685e31392b72a99a3e1b890670ce9c28ee85a Mon Sep 17 00:00:00 2001 From: Erik Dohmen Date: Wed, 6 Jan 2021 23:00:43 +0100 Subject: [PATCH 4/9] number formatting and public access added formating options for number_format made taxPrice, totalPrice and subTotalPrice public in order to use them for instance for database entries --- Classes/Invoice.php | 48 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/Classes/Invoice.php b/Classes/Invoice.php index 1135bc4..22bc746 100644 --- a/Classes/Invoice.php +++ b/Classes/Invoice.php @@ -225,7 +225,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,12 +256,40 @@ public function popItem() */ public function formatCurrency() { - $currencies = json_decode(file_get_contents(__DIR__.'/../Currencies.json')); - $currency = $this->currency; + if(null === $this->currency) { + $currencies = json_decode(file_get_contents(__DIR__ . '/../Currencies.json')); + $currency = $this->currency; + } return $currencies->$currency; } + /** + * return the decimal point for number format + * + * @method getDecPoint + * + * @return string + */ + protected function getDecPoint() + { + $format = $this->formatCurrency(); + return $this->format->dec_point ?: "."; + } + + /** + * return the thousands_sep for number format + * + * @method getThousandsSep + * + * @return string + */ + protected function getThousandsSep() + { + $format = $this->formatCurrency(); + return $this->format->thousands_sep ?: ","; + } + /** * Return the subtotal invoice price. * @@ -269,10 +297,10 @@ 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); + return bcmul($item['price'], $item['ammount'], $this->decimals, $this->getDecPoint(), $this->getThousandsSep()); }); } @@ -285,7 +313,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 +323,7 @@ public function subTotalPriceFormatted() * * @return int */ - private function totalPrice() + public function totalPrice() { return bcadd($this->subTotalPrice(), $this->taxPrice(), $this->decimals); } @@ -309,7 +337,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 +347,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 +377,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()); } /** From c8e6a7dcce724ba3f948fdfacbad08542f8a0e99 Mon Sep 17 00:00:00 2001 From: Erik Dohmen Date: Wed, 6 Jan 2021 23:06:35 +0100 Subject: [PATCH 5/9] typo --- Classes/Invoice.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Classes/Invoice.php b/Classes/Invoice.php index 22bc746..02885ea 100644 --- a/Classes/Invoice.php +++ b/Classes/Invoice.php @@ -150,6 +150,12 @@ class Invoice */ private $pdf; + /** + * Json of currencies + * + * @var object + */ + /** * Create a new invoice instance. * @@ -256,12 +262,12 @@ public function popItem() */ public function formatCurrency() { - if(null === $this->currency) { - $currencies = json_decode(file_get_contents(__DIR__ . '/../Currencies.json')); - $currency = $this->currency; + if(null === $this->currencies) { + $this->currencies = json_decode(file_get_contents(__DIR__ . '/../Currencies.json')); } + $currency = $this->currency; - return $currencies->$currency; + return $this->currencies->$currency; } /** From 7556d5ae0554dd6bc4612861f4254605e612d9f6 Mon Sep 17 00:00:00 2001 From: Erik Dohmen Date: Wed, 6 Jan 2021 23:08:56 +0100 Subject: [PATCH 6/9] missing property --- Classes/Invoice.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Classes/Invoice.php b/Classes/Invoice.php index 02885ea..0bf1db1 100644 --- a/Classes/Invoice.php +++ b/Classes/Invoice.php @@ -155,6 +155,7 @@ class Invoice * * @var object */ + private $currencies; /** * Create a new invoice instance. From 4b1e845e4f206a3cb62995a37e4e5b7277757ad4 Mon Sep 17 00:00:00 2001 From: Erik Dohmen Date: Wed, 6 Jan 2021 23:11:10 +0100 Subject: [PATCH 7/9] Update Invoice.php --- Classes/Invoice.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Classes/Invoice.php b/Classes/Invoice.php index 0bf1db1..17dfe93 100644 --- a/Classes/Invoice.php +++ b/Classes/Invoice.php @@ -280,8 +280,7 @@ public function formatCurrency() */ protected function getDecPoint() { - $format = $this->formatCurrency(); - return $this->format->dec_point ?: "."; + return $this->formatCurrency()->dec_point ?: "."; } /** @@ -293,8 +292,7 @@ protected function getDecPoint() */ protected function getThousandsSep() { - $format = $this->formatCurrency(); - return $this->format->thousands_sep ?: ","; + return $this->formatCurrency()->thousands_sep ?: ","; } /** From b6879912dac284090d44377e24a6a7e8fe1e6212 Mon Sep 17 00:00:00 2001 From: Erik Dohmen Date: Wed, 6 Jan 2021 23:13:36 +0100 Subject: [PATCH 8/9] Update Invoice.php --- Classes/Invoice.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/Invoice.php b/Classes/Invoice.php index 17dfe93..e435ee5 100644 --- a/Classes/Invoice.php +++ b/Classes/Invoice.php @@ -305,7 +305,7 @@ protected function getThousandsSep() public function subTotalPrice() { return $this->items->sum(function ($item) { - return bcmul($item['price'], $item['ammount'], $this->decimals, $this->getDecPoint(), $this->getThousandsSep()); + return bcmul($item['price'], $item['ammount'], $this->decimals); }); } From cf978389dbaf705bff3ccd8cb8e9d77f7eef2a71 Mon Sep 17 00:00:00 2001 From: Erik Dohmen Date: Wed, 6 Jan 2021 23:17:19 +0100 Subject: [PATCH 9/9] Updating the number formatting details for euro --- Currencies.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Currencies.json b/Currencies.json index a012274..6a367a6 100644 --- a/Currencies.json +++ b/Currencies.json @@ -25,8 +25,8 @@ "rounding": 0, "code": "EUR", "name_plural": "euros", - "thousands_sep": ",", - "dec_point": "." + "thousands_sep": ".", + "dec_point": "," }, "AED": { "symbol": "AED",