diff --git a/app/Events/Credit/CreditWasViewed.php b/app/Events/Credit/CreditWasViewed.php index 2fb428f18268..d7e6a07ddd85 100644 --- a/app/Events/Credit/CreditWasViewed.php +++ b/app/Events/Credit/CreditWasViewed.php @@ -12,7 +12,6 @@ namespace App\Events\Credit; use App\Models\Company; -use App\Models\CreditWasViewed; use Illuminate\Queue\SerializesModels; /** diff --git a/app/Mail/Engine/CreditEmailEngine.php b/app/Mail/Engine/CreditEmailEngine.php new file mode 100644 index 000000000000..2ee6aed08493 --- /dev/null +++ b/app/Mail/Engine/CreditEmailEngine.php @@ -0,0 +1,90 @@ +invitation = $invitation; + $this->reminder_template = $reminder_template; + $this->client = $invitation->contact->client; + $this->credit = $invitation->credit; + $this->contact = $invitation->contact; + } + + public function build() + { + + $body_template = $this->client->getSetting('email_template_'.$this->reminder_template); + + /* Use default translations if a custom message has not been set*/ + if (iconv_strlen($body_template) == 0) { + $body_template = trans( + 'texts.credit_message', + [ + 'credit' => $this->credit->number, + 'company' => $this->credit->company->present()->name(), + 'amount' => Number::formatMoney($this->credit->balance, $this->client), + ], + null, + $this->client->locale() + ); + } + + $subject_template = $this->client->getSetting('email_subject_'.$this->reminder_template); + + if (iconv_strlen($subject_template) == 0) { + + $subject_template = trans( + 'texts.credit_subject', + [ + 'number' => $this->credit->number, + 'account' => $this->credit->company->present()->name(), + ], + null, + $this->client->locale() + ); + + } + + $this->setTemplate($this->client->getSetting('email_style')) + ->setContact($this->contact) + ->setVariables($this->credit->makeValues($this->contact))//move make values into the htmlengine + ->setSubject($subject_template) + ->setBody($body_template) + ->setFooter("".ctrans('texts.view_credit').'') + ->setViewLink($this->invitation->getLink()) + ->setViewText(ctrans('texts.view_credit')); + + if ($this->client->getSetting('pdf_email_attachment') !== false) { + $this->setAttachments($invitation->pdf_file_path()); + } + + return $this; + + } + +} + diff --git a/app/Repositories/ActivityRepository.php b/app/Repositories/ActivityRepository.php index 6e0173f793e4..349e47db1962 100644 --- a/app/Repositories/ActivityRepository.php +++ b/app/Repositories/ActivityRepository.php @@ -17,11 +17,18 @@ use App\Models\Backup; use App\Models\Client; use App\Models\CompanyToken; use App\Models\Credit; +use App\Models\Design; use App\Models\Invoice; use App\Models\Quote; +use App\Models\RecurringInvoice; use App\Models\User; +use App\Utils\HtmlEngine; +use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesInvoiceHtml; use Illuminate\Support\Facades\Log; +use App\Services\PdfMaker\Design as PdfDesignModel; +use App\Services\PdfMaker\Design as PdfMakerDesign; +use App\Services\PdfMaker\PdfMaker as PdfMakerService; /** * Class for activity repository. @@ -29,7 +36,7 @@ use Illuminate\Support\Facades\Log; class ActivityRepository extends BaseRepository { use MakesInvoiceHtml; - + use MakesHash; /** * Save the Activity. * @@ -68,7 +75,7 @@ class ActivityRepository extends BaseRepository if (get_class($entity) == Invoice::class || get_class($entity) == Quote::class || get_class($entity) == Credit::class) { $contact = $entity->client->primary_contact()->first(); - $backup->html_backup = $this->generateEntityHtml($entity->getEntityDesigner(), $entity, $contact); + $backup->html_backup = $this->generateHtml($entity); $backup->amount = $entity->amount; } @@ -90,4 +97,55 @@ class ActivityRepository extends BaseRepository return false; } + + private function generateHtml($entity) + { + $entity_design_id = ''; + + if($entity instanceof Invoice || $entity instanceof RecurringInvoice){ + $entity_design_id = 'invoice_design_id'; + } + elseif($entity instanceof Quote){ + $entity_design_id = 'quote_design_id'; + } + elseif($entity instanceof Credit){ + $entity_design_id = 'credit_design_id'; + } + + $entity_design_id = $entity->design_id ? $entity->design_id : $this->decodePrimaryKey($entity->client->getSetting($entity_design_id)); + + $design = Design::find($entity_design_id); + $html = new HtmlEngine($invitation); + + if ($design->is_custom) { + $options = [ + 'custom_partials' => json_decode(json_encode($design->design), true) + ]; + $template = new PdfMakerDesign(PdfDesignModel::CUSTOM, $options); + } else { + $template = new PdfMakerDesign(strtolower($design->name)); + } + + $state = [ + 'template' => $template->elements([ + 'client' => $entity->client, + 'entity' => $entity, + 'pdf_variables' => (array) $entity->company->settings->pdf_variables, + 'products' => $design->design->product, + ]), + 'variables' => $html->generateLabelsAndValues(), + 'options' => [ + 'all_pages_header' => $entity->client->getSetting('all_pages_header'), + 'all_pages_footer' => $entity->client->getSetting('all_pages_footer'), + ], + ]; + + $maker = new PdfMakerService($state); + + return $maker->design($template) + ->build() + ->getCompiledHTML(true); + + } + } diff --git a/routes/client.php b/routes/client.php index 685482d0ed5b..9cf7fa3a5015 100644 --- a/routes/client.php +++ b/routes/client.php @@ -57,7 +57,8 @@ Route::group(['middleware' => ['auth:contact', 'locale'], 'prefix' => 'client', Route::get('quotes/{quote}', 'ClientPortal\QuoteController@show')->name('quote.show'); Route::get('quotes/{quote_invitation}', 'ClientPortal\QuoteController@show')->name('quote.show_invitation'); - Route::resource('credits', 'ClientPortal\CreditController')->only('index', 'show'); + Route::get('credits', 'ClientPortal\CreditController@index')->name('credits.index'); + Route::get('credits/{credit}', 'ClientPortal\CreditController@show')->name('credits.show'); Route::get('client/switch_company/{contact}', 'ClientPortal\SwitchCompanyController')->name('switch_company');