From 8eb2c86a77d91c3b5286d28aa8020d05072f5625 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=BCneyt=20=C5=9Eent=C3=BCrk?= Date: Wed, 2 Oct 2024 15:46:19 +0100 Subject: [PATCH] close #3221 Feature: Ability to change document colour and contact based on invoice.. --- .../View/Components/Documents/Form.php | 87 ++++++++++++++++++- .../View/Components/Documents/Show.php | 23 ++++- .../View/Components/Documents/Template.php | 23 ++++- app/Events/Document/DocumentTemplates.php | 26 ++++++ app/Listeners/Update/V31/Version3112.php | 43 +++++++++ app/Models/Document/Document.php | 2 + app/Providers/Event.php | 1 + app/Traits/Documents.php | 35 ++++++++ .../2024_09_26_000000_core_v3112.php | 34 ++++++++ .../documents/form/advanced.blade.php | 25 ++++++ 10 files changed, 296 insertions(+), 3 deletions(-) create mode 100644 app/Events/Document/DocumentTemplates.php create mode 100644 app/Listeners/Update/V31/Version3112.php create mode 100644 database/migrations/2024_09_26_000000_core_v3112.php diff --git a/app/Abstracts/View/Components/Documents/Form.php b/app/Abstracts/View/Components/Documents/Form.php index 9f9118bf4..dc815dd9c 100644 --- a/app/Abstracts/View/Components/Documents/Form.php +++ b/app/Abstracts/View/Components/Documents/Form.php @@ -9,13 +9,14 @@ use App\Models\Document\Document; use App\Models\Setting\Currency; use App\Models\Setting\Tax; use App\Traits\Documents; +use App\Traits\Tailwind; use App\Traits\ViewComponents; use App\Utilities\Date; use Illuminate\Support\Str; abstract class Form extends Component { - use Documents, ViewComponents; + use Documents, Tailwind, ViewComponents; public const OBJECT_TYPE = 'document'; public const DEFAULT_TYPE = 'invoice'; @@ -263,6 +264,21 @@ abstract class Form extends Component /** @var string */ public $classAttachment; + + /** @var bool */ + public $hideTemplate; + + /** @var bool */ + public $hideBackgroundColor; + + /** @var array */ + public $templates; + + /** @var string */ + public $template; + + /** @var string */ + public $backgroundColor; /* -- Advanced End -- */ /* -- Buttons End -- */ @@ -300,6 +316,8 @@ abstract class Form extends Component bool $hideFooter = false, string $classFooter = '', string $footer = '', bool $hideCategory = false, string $classCategory = '', string $typeCategory = '', $categoryId = '', bool $hideAttachment = false, string $classAttachment = '', + bool $hideTemplate = false, bool $hideBackgroundColor = false, + array $templates = [], string $template = '', string $backgroundColor = '', bool $hideButtons = false, string $cancelRoute = '', $hideSendTo = false ) { $this->type = $type; @@ -426,6 +444,13 @@ abstract class Form extends Component $this->hideAttachment = $hideAttachment; $this->classAttachment = !empty($classAttachment) ? $classAttachment : 'sm:col-span-4'; + + $this->hideTemplate = $hideTemplate; + $this->hideBackgroundColor = $hideBackgroundColor; + + $this->templates = $this->getTemplates($type, $templates); + $this->template = $this->getDocumentTemplate($type, $template); + $this->backgroundColor = $this->getBackgroundColor($type, $backgroundColor); /** Advanced End */ /** Buttons Start */ @@ -1307,4 +1332,64 @@ abstract class Form extends Component return setting('default.' . $this->typeCategory . '_category'); } + + protected function getTemplates($type, $templates) + { + if (! empty($templates)) { + return $templates; + } + + $templates = $this->getDocumentTemplates($type); + + return $templates; + } + + protected function getDocumentTemplate($type, $documentTemplate) + { + if (! empty($documentTemplate)) { + return $documentTemplate; + } + + if (! empty($this->document) && ! empty($this->document->template)) { + return $this->document->template; + } + + if ($template = config('type.document.' . $type . '.template', false)) { + return $template; + } + + $documentTemplate = setting($this->getDocumentSettingKey($type, 'template'), 'default'); + + return $documentTemplate; + } + + protected function getBackgroundColor($type, $backgroundColor) + { + if (! empty($backgroundColor)) { + return $backgroundColor; + } + + if (! empty($this->document) && $this->document->color !== '') { + return $this->getHexCodeOfTailwindClass($this->document->color); + } + + // checking setting color + $key = $this->getDocumentSettingKey($type, 'color'); + + if (! empty(setting($key))) { + $backgroundColor = setting($key); + } + + // checking config color + if (empty($backgroundColor) && $background_color = config('type.document.' . $type . '.color', false)) { + $backgroundColor = $background_color; + } + + // set default color + if (empty($backgroundColor)) { + $backgroundColor = '#55588b'; + } + + return $this->getHexCodeOfTailwindClass($backgroundColor); + } } diff --git a/app/Abstracts/View/Components/Documents/Show.php b/app/Abstracts/View/Components/Documents/Show.php index cf18d9845..7c3c1134f 100644 --- a/app/Abstracts/View/Components/Documents/Show.php +++ b/app/Abstracts/View/Components/Documents/Show.php @@ -925,11 +925,28 @@ abstract class Show extends Component return $documentTemplate; } + if (! empty($this->document) && ! empty($this->document->template)) { + $document_templates = $this->getDocumentTemplates($type); + + $template = 'default'; + + foreach ($document_templates as $template) { + if ($template['id'] != $this->document->template) { + continue; + } + + $template = $template['template']; + break; + } + + return $template; + } + if ($template = config('type.' . static::OBJECT_TYPE . '.' . $type . '.template', false)) { return $template; } - $documentTemplate = setting($this->getDocumentSettingKey($type, 'template'), 'default'); + $documentTemplate = setting($this->getDocumentSettingKey($type, 'template'), 'default'); return $documentTemplate; } @@ -1002,6 +1019,10 @@ abstract class Show extends Component return $backgroundColor; } + if (! empty($this->document) && $this->document->color !== '') { + return $this->getHexCodeOfTailwindClass($this->document->color); + } + // checking setting color $key = $this->getDocumentSettingKey($type, 'color'); diff --git a/app/Abstracts/View/Components/Documents/Template.php b/app/Abstracts/View/Components/Documents/Template.php index 4fdfb09ca..b0dbff800 100644 --- a/app/Abstracts/View/Components/Documents/Template.php +++ b/app/Abstracts/View/Components/Documents/Template.php @@ -205,11 +205,28 @@ abstract class Template extends Component return $documentTemplate; } + if (! empty($this->document) && ! empty($this->document->template)) { + $document_templates = $this->getDocumentTemplates($type); + + $template = 'default'; + + foreach ($document_templates as $template) { + if ($template['id'] != $this->document->template) { + continue; + } + + $template = $template['template']; + break; + } + + return $template; + } + if ($template = config('type.document.' . $type . '.template', false)) { return $template; } - $documentTemplate = setting($this->getDocumentSettingKey($type, 'template'), 'default'); + $documentTemplate = setting($this->getDocumentSettingKey($type, 'template'), 'default'); return $documentTemplate; } @@ -282,6 +299,10 @@ abstract class Template extends Component return $backgroundColor; } + if (! empty($this->document) && $this->document->color !== '') { + return $this->getHexCodeOfTailwindClass($this->document->color); + } + // checking setting color $key = $this->getDocumentSettingKey($type, 'color'); diff --git a/app/Events/Document/DocumentTemplates.php b/app/Events/Document/DocumentTemplates.php new file mode 100644 index 000000000..7079c215e --- /dev/null +++ b/app/Events/Document/DocumentTemplates.php @@ -0,0 +1,26 @@ +type = $type; + $this->templates = $templates; + } +} diff --git a/app/Listeners/Update/V31/Version3112.php b/app/Listeners/Update/V31/Version3112.php new file mode 100644 index 000000000..bf305122e --- /dev/null +++ b/app/Listeners/Update/V31/Version3112.php @@ -0,0 +1,43 @@ +skipThisUpdate($event)) { + return; + } + + Log::channel('stdout')->info('Updating to 3.1.12 version...'); + + $this->updateDatabase(); + + Log::channel('stdout')->info('Done!'); + } + + public function updateDatabase(): void + { + Log::channel('stdout')->info('Updating database...'); + + Artisan::call('migrate', ['--force' => true]); + + Log::channel('stdout')->info('Database updated.'); + } +} diff --git a/app/Models/Document/Document.php b/app/Models/Document/Document.php index 8d4d91140..264a0cdff 100644 --- a/app/Models/Document/Document.php +++ b/app/Models/Document/Document.php @@ -58,6 +58,8 @@ class Document extends Model 'subheading', 'notes', 'footer', + 'template', + 'color', 'parent_id', 'created_from', 'created_by', diff --git a/app/Providers/Event.php b/app/Providers/Event.php index 3afec6cf1..4bd37c575 100644 --- a/app/Providers/Event.php +++ b/app/Providers/Event.php @@ -30,6 +30,7 @@ class Event extends Provider 'App\Listeners\Update\V31\Version315', 'App\Listeners\Update\V31\Version317', 'App\Listeners\Update\V31\Version318', + 'App\Listeners\Update\V31\Version3112', ], 'Illuminate\Auth\Events\Login' => [ 'App\Listeners\Auth\Login', diff --git a/app/Traits/Documents.php b/app/Traits/Documents.php index fcdd08f50..63aa774cb 100644 --- a/app/Traits/Documents.php +++ b/app/Traits/Documents.php @@ -7,6 +7,7 @@ use App\Models\Document\Document; use App\Abstracts\View\Components\Documents\Document as DocumentComponent; use App\Utilities\Date; use App\Traits\Transactions; +use App\Events\Document\DocumentTemplates; use Egulias\EmailValidator\EmailValidator; use Egulias\EmailValidator\Validation\DNSCheckValidation; use Egulias\EmailValidator\Validation\MultipleValidationWithAnd; @@ -270,4 +271,38 @@ trait Documents { return Str::replace('-recurring', '', $recurring_type); } + + public function getDocumentTemplates($type) + { + $templates = new \stdClass(); + $templates->templates = collect(); + $config_templates = config('type.document.' . $type . '.templates', [ + [ + 'id' => 'default', + 'name' => trans('settings.invoice.default'), + 'image' => asset('assets/img/templates/default.png'), + 'template' => 'default', + ], + [ + 'id' => 'classic', + 'name' => trans('settings.invoice.classic'), + 'image' => asset('assets/img/templates/classic.png'), + 'template' => 'classic', + ], + [ + 'id' => 'modern', + 'name' => trans('settings.invoice.modern'), + 'image' => asset('assets/img/templates/modern.png'), + 'template' => 'modern', + ], + ]); + + foreach ($config_templates as $config_template) { + $templates->templates->push($config_template); + } + + event(new DocumentTemplates($type, $templates)); + + return $templates->templates; + } } diff --git a/database/migrations/2024_09_26_000000_core_v3112.php b/database/migrations/2024_09_26_000000_core_v3112.php new file mode 100644 index 000000000..f8336d5bf --- /dev/null +++ b/database/migrations/2024_09_26_000000_core_v3112.php @@ -0,0 +1,34 @@ +string('template')->nullable()->after('footer'); + $table->string('color')->nullable()->after('template'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('documents', function (Blueprint $table) { + $table->dropColumn('template'); + $table->dropColumn('color'); + }); + } +}; diff --git a/resources/views/components/documents/form/advanced.blade.php b/resources/views/components/documents/form/advanced.blade.php index fca27d654..96a66f47c 100644 --- a/resources/views/components/documents/form/advanced.blade.php +++ b/resources/views/components/documents/form/advanced.blade.php @@ -33,6 +33,31 @@ @endif + + @if (! $hideTemplate) + + + + @endif + + @if (! $hideBackgroundColor) + + @endif