diff --git a/app/Http/Controllers/PreviewController.php b/app/Http/Controllers/PreviewController.php index a7f907946ad8..c76152059250 100644 --- a/app/Http/Controllers/PreviewController.php +++ b/app/Http/Controllers/PreviewController.php @@ -16,6 +16,7 @@ use App\Designs\Designer; use App\Factory\InvoiceFactory; use App\Jobs\Invoice\CreateInvoicePdf; use App\Jobs\Util\PreviewPdf; +use App\Services\PdfMaker\Design; use App\Services\PdfMaker\PdfMaker; use App\Utils\HtmlEngine; use App\Utils\Traits\MakesHash; @@ -112,13 +113,14 @@ class PreviewController extends BaseController 'variables' => $html->generateLabelsAndValues(), ]; + $design = new Design(request()->design['name']); $maker = new PdfMaker($state); $maker - ->design($design_namespace) + ->design($design) ->build(); - $file_path = PreviewPdf::dispatchNow($maker->getCompiledHTML(), auth()->user()->company()); + $file_path = PreviewPdf::dispatchNow($maker->getCompiledHTML(true), auth()->user()->company()); return response()->download($file_path)->deleteFileAfterSend(true); } @@ -171,16 +173,13 @@ class PreviewController extends BaseController $html = new HtmlEngine(null, $invoice->invitations()->first(), 'invoice'); - /** TODO: This request() does not contain design string - making it impossible to update its content. */ - $design_namespace = 'App\Services\PdfMaker\Designs\\' . request()->design['name']; - - $design_class = new $design_namespace(); + $design = new Design(strtolower(request()->design['name'])); // $designer = new Designer($entity_obj, $design_object, $entity_obj->client->getSetting('pdf_variables'), lcfirst($entity)); // $html = $this->generateEntityHtml($designer, $entity_obj); $state = [ - 'template' => $design_class->elements([ + 'template' => $design->elements([ 'client' => $invoice->client, 'entity' => $invoice, 'pdf_variables' => (array) $invoice->company->settings->pdf_variables, @@ -191,12 +190,12 @@ class PreviewController extends BaseController $maker = new PdfMaker($state); $maker - ->design($design_namespace) + ->design($design) ->build(); - $maker->getCompiledHTML(); + info($maker->getCompiledHTML(true)); - $file_path = PreviewPdf::dispatchNow($maker->getCompiledHTML(), auth()->user()->company()); + $file_path = PreviewPdf::dispatchNow($maker->getCompiledHTML(true), auth()->user()->company()); DB::rollBack(); diff --git a/app/Jobs/Invoice/CreateInvoicePdf.php b/app/Jobs/Invoice/CreateInvoicePdf.php index 968ed24eeb95..7f62682e2a14 100644 --- a/app/Jobs/Invoice/CreateInvoicePdf.php +++ b/app/Jobs/Invoice/CreateInvoicePdf.php @@ -20,6 +20,7 @@ use App\Models\ClientContact; use App\Models\Company; use App\Models\Design; use App\Models\Invoice; +use App\Services\PdfMaker\Design as PdfMakerDesign; use App\Services\PdfMaker\PdfMaker as PdfMakerService; use App\Utils\HtmlEngine; use App\Utils\PhantomJS\Phantom; @@ -86,12 +87,10 @@ class CreateInvoicePdf implements ShouldQueue $html = new HtmlEngine(null, $this->invitation, 'invoice'); - $design_namespace = 'App\Services\PdfMaker\Designs\\' . $design->name; - - $design_class = new $design_namespace(); + $template = new PdfMakerDesign(strtolower($design->name)); $state = [ - 'template' => $design_class->elements([ + 'template' => $template->elements([ 'client' => $this->invoice->client, 'entity' => $this->invoice, 'pdf_variables' => (array)$this->invoice->company->settings->pdf_variables, @@ -106,15 +105,15 @@ class CreateInvoicePdf implements ShouldQueue $maker = new PdfMakerService($state); $maker - ->design($design_namespace) + ->design($template) ->build(); //todo - move this to the client creation stage so we don't keep hitting this unnecessarily Storage::makeDirectory($path, 0775); - info($maker->getCompiledHTML()); + info($maker->getCompiledHTML(true)); - $pdf = $this->makePdf(null, null, $maker->getCompiledHTML()); + $pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true)); $instance = Storage::disk($this->disk)->put($file_path, $pdf); diff --git a/app/Services/PdfMaker/Design.php b/app/Services/PdfMaker/Design.php index 59a2c0cac1dc..f97c85ed0cbe 100644 --- a/app/Services/PdfMaker/Design.php +++ b/app/Services/PdfMaker/Design.php @@ -12,9 +12,10 @@ namespace App\Services\PdfMaker; +use Illuminate\Support\Str; +use App\Utils\Traits\MakesInvoiceValues; use App\Services\PdfMaker\Designs\Utilities\BaseDesign; use App\Services\PdfMaker\Designs\Utilities\DesignHelpers; -use App\Utils\Traits\MakesInvoiceValues; class Design extends BaseDesign { @@ -35,19 +36,19 @@ class Design extends BaseDesign /** Construct options */ public $options; - const BOLD = 'bold.html'; - const BUSINESS = 'business.html'; - const CLEAN = 'clean.html'; - const CREATIVE = 'creative.html'; - const ELEGANT = 'elegant.html'; - const HIPSTER = 'hipster.html'; - const MODERN = 'modern.html'; - const PLAIN = 'plain.html'; - const PLAYFUL = 'playful.html'; + const BOLD = 'bold'; + const BUSINESS = 'business'; + const CLEAN = 'clean'; + const CREATIVE = 'creative'; + const ELEGANT = 'elegant'; + const HIPSTER = 'hipster'; + const MODERN = 'modern'; + const PLAIN = 'plain'; + const PLAYFUL = 'playful'; public function __construct(string $design = null, array $options = []) { - $this->design = $design; + Str::endsWith('.html', $design) ? $this->design = $design : $this->design = "{$design}.html"; $this->options = $options; } @@ -216,8 +217,13 @@ class Design extends BaseDesign ]; foreach ($variables as $variable) { - ['element' => 'tr', 'properties' => ['hidden' => 'false'], 'elements' => [ - ['element' => 'td', 'content' => $variable . '_label', 'properties' => ['colspan' => $this->calculateColspan(1)]], + if ($variable == '$total_taxes' || $variable == '$line_taxes') { + continue; + } + + $elements[] = ['element' => 'tr', 'elements' => [ + ['element' => 'td', 'properties' => ['colspan' => $this->calculateColspan(2)]], + ['element' => 'td', 'content' => $variable . '_label'], ['element' => 'td', 'content' => $variable], ]]; } diff --git a/app/Services/PdfMaker/PdfMaker.php b/app/Services/PdfMaker/PdfMaker.php index 57460b0f565b..e399f8b2892a 100644 --- a/app/Services/PdfMaker/PdfMaker.php +++ b/app/Services/PdfMaker/PdfMaker.php @@ -28,6 +28,8 @@ class PdfMaker private $filters = [ ' '', + ' '', + ']]]]>]]>' => '', ']]>' => '', '' => '', ]; diff --git a/app/Utils/HtmlEngine.php b/app/Utils/HtmlEngine.php index 0accbcac6f17..c5895f2a19d5 100644 --- a/app/Utils/HtmlEngine.php +++ b/app/Utils/HtmlEngine.php @@ -483,7 +483,8 @@ class HtmlEngine } foreach ($this->entity_calc->getTotalTaxMap() as $tax) { - $data .= ''; + $data .= ''; + $data .= ''; $data .= ''. $tax['name'] .''; $data .= ''. Number::formatMoney($tax['total'], $this->client) .''; } diff --git a/database/seeds/DesignSeeder.php b/database/seeds/DesignSeeder.php index 6b217387baf6..06ce6dd89776 100644 --- a/database/seeds/DesignSeeder.php +++ b/database/seeds/DesignSeeder.php @@ -2,6 +2,7 @@ use App\Models\Bank; use App\Models\Design; +use App\Services\PdfMaker\Design as PdfMakerDesign; use Illuminate\Database\Seeder; class DesignSeeder extends Seeder @@ -36,17 +37,16 @@ class DesignSeeder extends Seeder } foreach (Design::all() as $design) { - $class = 'App\Services\PdfMaker\Designs\\'.$design->name; - $invoice_design = new $class(); - $invoice_design->document(); + $template = new PdfMakerDesign(strtolower($design->name)); + $template->document(); $design_object = new \stdClass; - $design_object->includes = $invoice_design->getSectionHTML('includes'); - $design_object->header = $invoice_design->getSectionHTML('head', false); - $design_object->body = $invoice_design->getSectionHTML('body', false); - $design_object->product = $invoice_design->getSectionHTML('product-table'); - $design_object->task = $invoice_design->getSectionHTML('task-table'); - $design_object->footer = $invoice_design->getSectionHTML('footer', false); + $design_object->includes = $template->getSectionHTML('includes'); + $design_object->header = $template->getSectionHTML('head', false); + $design_object->body = $template->getSectionHTML('body', false); + $design_object->product = $template->getSectionHTML('product-table'); + $design_object->task = $template->getSectionHTML('task-table'); + $design_object->footer = $template->getSectionHTML('footer', false); $design->design = $design_object; $design->save(); diff --git a/tests/Feature/PdfMaker/ExampleIntegrationTest.php b/tests/Feature/PdfMaker/ExampleIntegrationTest.php index c8e87bd5e4e7..fba1adfcc18d 100644 --- a/tests/Feature/PdfMaker/ExampleIntegrationTest.php +++ b/tests/Feature/PdfMaker/ExampleIntegrationTest.php @@ -24,6 +24,8 @@ class ExampleIntegrationTest extends TestCase public function testExample() { + $this->markTestIncomplete(); + $invoice = $this->invoice; $invitation = $invoice->invitations()->first(); @@ -48,9 +50,9 @@ class ExampleIntegrationTest extends TestCase ->design($design) ->build(); - // exec('echo "" > storage/logs/laravel.log'); + exec('echo "" > storage/logs/laravel.log'); - // info($maker->getCompiledHTML(true)); + info($maker->getCompiledHTML(true)); $this->assertTrue(true); } diff --git a/tests/Feature/PdfMaker/PdfMakerTest.php b/tests/Feature/PdfMaker/PdfMakerTest.php index 76af729733c1..de927764172e 100644 --- a/tests/Feature/PdfMaker/PdfMakerTest.php +++ b/tests/Feature/PdfMaker/PdfMakerTest.php @@ -18,7 +18,7 @@ class PdfMakerTest extends TestCase public function testDesignLoadsCorrectly() { - $design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]); + $design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]); $maker = new PdfMaker($this->state); $maker->design($design); @@ -28,7 +28,7 @@ class PdfMakerTest extends TestCase public function testHtmlDesignLoadsCorrectly() { - $design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]); + $design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]); $maker = new PdfMaker($this->state); @@ -41,7 +41,7 @@ class PdfMakerTest extends TestCase public function testGetSectionUtility() { - $design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]); + $design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]); $maker = new PdfMaker($this->state); @@ -71,7 +71,7 @@ class PdfMakerTest extends TestCase ], ]; - $design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]); + $design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]); $maker = new PdfMaker($state); $maker @@ -99,7 +99,7 @@ class PdfMakerTest extends TestCase ], ]; - $design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]); + $design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]); $maker = new PdfMaker($state); $maker @@ -147,7 +147,7 @@ class PdfMakerTest extends TestCase ], ]; - $design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]); + $design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]); $maker = new PdfMaker($state); $maker @@ -161,7 +161,7 @@ class PdfMakerTest extends TestCase public function testConditionalRenderingOfElements() { - $design1 = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]); + $design1 = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]); $maker1 = new PdfMaker([ 'template' => [ @@ -180,7 +180,7 @@ class PdfMakerTest extends TestCase $this->assertStringContainsString('