From 0d01f4b773b50bc91fa0b139c9744cd91369c592 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 7 Mar 2023 22:36:50 +1100 Subject: [PATCH] Working on email refactor --- app/Http/Controllers/EmailController.php | 49 +++- app/Services/Email/Email.php | 34 ++- app/Services/Email/EmailDefaults.php | 20 +- app/Services/Email/EmailMailer.php | 1 + app/Services/Email/EmailObject.php | 2 + app/Utils/PaymentHtmlEngine.php | 349 +++++++++++++++++++++++ 6 files changed, 434 insertions(+), 21 deletions(-) create mode 100644 app/Utils/PaymentHtmlEngine.php diff --git a/app/Http/Controllers/EmailController.php b/app/Http/Controllers/EmailController.php index 69be30768391..0c277da03b24 100644 --- a/app/Http/Controllers/EmailController.php +++ b/app/Http/Controllers/EmailController.php @@ -11,26 +11,28 @@ namespace App\Http\Controllers; -use App\Events\Credit\CreditWasEmailed; -use App\Events\Quote\QuoteWasEmailed; -use App\Http\Requests\Email\SendEmailRequest; -use App\Jobs\Entity\EmailEntity; -use App\Jobs\PurchaseOrder\PurchaseOrderEmail; +use App\Utils\Ninja; +use App\Models\Quote; use App\Models\Credit; use App\Models\Invoice; use App\Models\PurchaseOrder; -use App\Models\Quote; +use App\Services\Email\Email; +use Illuminate\Http\Response; +use App\Utils\Traits\MakesHash; +use App\Jobs\Entity\EmailEntity; use App\Models\RecurringInvoice; use App\Services\Email\MailEntity; use App\Services\Email\MailObject; +use App\Services\Email\EmailObject; +use App\Events\Quote\QuoteWasEmailed; +use App\Transformers\QuoteTransformer; +use App\Events\Credit\CreditWasEmailed; use App\Transformers\CreditTransformer; use App\Transformers\InvoiceTransformer; +use App\Http\Requests\Email\SendEmailRequest; +use App\Jobs\PurchaseOrder\PurchaseOrderEmail; use App\Transformers\PurchaseOrderTransformer; -use App\Transformers\QuoteTransformer; use App\Transformers\RecurringInvoiceTransformer; -use App\Utils\Ninja; -use App\Utils\Traits\MakesHash; -use Illuminate\Http\Response; class EmailController extends BaseController { @@ -127,11 +129,12 @@ class EmailController extends BaseController 'body' => $body, ]; - $mo = new MailObject; + $mo = new EmailObject; $mo->subject = empty($subject) ? null : $subject; $mo->body = empty($body) ? null : $body; - $mo->entity_string = $entity; - $mo->email_template = $template; + $mo->entity_id = $request->input('entity_id'); + $mo->template = $template; + $mo->entity_class = $this->resolveClass($entity); if (Ninja::isHosted() && !$entity_obj->company->account->account_sms_verified) { return response(['message' => 'Please verify your account to send emails.'], 400); @@ -145,8 +148,11 @@ class EmailController extends BaseController if (! $invitation->contact->trashed() && $invitation->contact->email) { $entity_obj->service()->markSent()->save(); - EmailEntity::dispatch($invitation->fresh(), $invitation->company, $template, $data); + // EmailEntity::dispatch($invitation->fresh(), $invitation->company, $template, $data); + $mo->invitation_id = $invitation->id; + + Email::dispatch($mo, $invitation->company); // MailEntity::dispatch($invitation, $invitation->company->db, $mo); } }); @@ -203,4 +209,19 @@ class EmailController extends BaseController return $this->itemResponse($entity_obj); } + + private function resolveClass(string $entity): string + { + match($entity){ + 'invoice' => $class = Invoice::class, + 'credit' => $class = Credit::class, + 'quote' => $class = Quote::class, + 'purchase_order' => $class = PurchaseOrder::class, + 'purchaseOrder' => $class = PurchaseOrder::class, + 'App\Models\PurchaseOrder' => $class = PurchaseOrder::class, + default => $class = Invoice::class, + }; + + return $class; + } } diff --git a/app/Services/Email/Email.php b/app/Services/Email/Email.php index a6fb462fcac6..83dc923013d5 100644 --- a/app/Services/Email/Email.php +++ b/app/Services/Email/Email.php @@ -17,6 +17,7 @@ use App\Models\Client; use App\Models\Vendor; use App\Models\Company; use App\Models\SystemLog; +use App\Utils\HtmlEngine; use App\Libraries\MultiDB; use App\Models\ClientContact; use App\Models\VendorContact; @@ -24,6 +25,7 @@ use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use App\Jobs\Util\SystemLogger; use App\Utils\Traits\MakesHash; +use App\Utils\VendorHtmlEngine; use App\Libraries\Google\Google; use Illuminate\Support\Facades\Mail; use App\Services\Email\EmailMailable; @@ -101,9 +103,20 @@ class Email implements ShouldQueue $this->email_object->invitation_id ? $this->email_object->invitation = $this->email_object->entity->invitations()->where('id', $this->email_object->invitation_id)->first() : $this->email_object->invitation = null; - $this->email_object->client_id ? $this->email_object->client = Client::withTrashed()->find($this->email_object->client_id) : $this->email_object->client = null; + $this->email_object->invitation_id ? $this->email_object->contact = $this->email_object->invitation->contact : null; + $this->email_object->client_id ? $this->email_object->client = Client::withTrashed()->find($this->email_object->client_id) : $this->email_object->client = null; + $this->email_object->vendor_id ? $this->email_object->vendor = Vendor::withTrashed()->find($this->email_object->vendor_id) : $this->email_object->vendor = null; + + if (!$this->email_object->contact) + { + + $this->email_object->vendor_contact_id ? $this->email_object->contact = VendorContact::withTrashed()->find($this->email_object->vendor_contact_id) : null; + + $this->email_object->client_contact_id ? $this->email_object->contact = ClientContact::withTrashed()->find($this->email_object->client_contact_id) : null; + + } $this->email_object->user_id ? $this->email_object->user = User::withTrashed()->find($this->email_object->user_id) : $this->email_object->user = $this->company->owner(); @@ -111,10 +124,6 @@ class Email implements ShouldQueue $this->email_object->company = $this->company; - $this->email_object->vendor_contact_id ? $this->email_object->contact = VendorContact::withTrashed()->find($this->email_object->vendor_contact_id) : null; - - $this->email_object->client_contact_id ? $this->email_object->contact = ClientContact::withTrashed()->find($this->email_object->client_contact_id) : null; - $this->email_object->client_id ? $this->email_object->settings = $this->email_object->client->getMergedSettings() : $this->email_object->settings = $this->company->settings; $this->email_object->whitelabel = $this->company->account->isPaid() ? true : false; @@ -123,6 +132,21 @@ class Email implements ShouldQueue $this->email_object->signature = $this->email_object->settings->email_signature; + $this->resolveVariables(); + + return $this; + } + + private function resolveVariables(): self + { + match(get_class($this->email_object->entity)){ + Invoice::class => $this->email_object->variables = (new HtmlEngine($this->email_object->invitation))->generateLabelsAndValues(), + Quote::class => $this->email_object->variables = (new HtmlEngine($this->email_object->invitation))->generateLabelsAndValues(), + Credit::class => $this->email_object->variables = (new HtmlEngine($this->email_object->invitation))->generateLabelsAndValues(), + PurchaseOrder::class => $this->email_object->variables = (new VendorHtmlEngine($this->email_object->invitation))->generateLabelsAndValues(), + default => null + }; + return $this; } diff --git a/app/Services/Email/EmailDefaults.php b/app/Services/Email/EmailDefaults.php index e67248dedb98..d6ea21fd46e1 100644 --- a/app/Services/Email/EmailDefaults.php +++ b/app/Services/Email/EmailDefaults.php @@ -58,6 +58,7 @@ class EmailDefaults $this->setLocale() // ->setFrom() + ->setTo() ->setTemplate() ->setBody() ->setSubject() @@ -128,6 +129,21 @@ class EmailDefaults return $this; } + /** + * Sets the To address + */ + private function setTo(): self + { + + if ($this->email->email_object->to) { + return $this; + } + + $this->email->email_object->to = [new Address($this->email->email_object->contact->email, $this->email->email_object->contact->present()->name())]; + + return $this; + } + /** * Sets the body of the email */ @@ -135,7 +151,7 @@ class EmailDefaults { if ($this->email->email_object->body) { $this->email->email_object->body = $this->email->email_object->body; - } elseif (strlen($this->email->email_object->settings->{$this->email->email_object->email_template_body}) > 3) { + } elseif (isset($this->email->email_object->email_template_body) && strlen($this->email->email_object->settings->{$this->email->email_object->email_template_body}) > 3) { $this->email->email_object->body = $this->email->email_object->settings->{$this->email->email_object->email_template_body}; } else { $this->email->email_object->body = EmailTemplateDefaults::getDefaultTemplate($this->email->email_object->email_template_body, $this->locale); @@ -155,7 +171,7 @@ class EmailDefaults { if ($this->email->email_object->subject) { //where the user updates the subject from the UI return $this; - } elseif (strlen($this->email->email_object->settings->{$this->email->email_object->email_template_subject}) > 3) { + } elseif (isset($this->email->email_object->email_template_subject) && strlen($this->email->email_object->settings->{$this->email->email_object->email_template_subject}) > 3) { $this->email->email_object->subject = $this->email->email_object->settings->{$this->email->email_object->email_template_subject}; } else { $this->email->email_object->subject = EmailTemplateDefaults::getDefaultTemplate($this->email->email_object->email_template_subject, $this->locale); diff --git a/app/Services/Email/EmailMailer.php b/app/Services/Email/EmailMailer.php index 265b4f574c61..4aa296f270df 100644 --- a/app/Services/Email/EmailMailer.php +++ b/app/Services/Email/EmailMailer.php @@ -37,6 +37,7 @@ use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Mail; use Turbo124\Beacon\Facades\LightLogs; +//@deprecated v5.5.83 class EmailMailer implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesHash; diff --git a/app/Services/Email/EmailObject.php b/app/Services/Email/EmailObject.php index ce018c3429c9..2f52de3b470d 100644 --- a/app/Services/Email/EmailObject.php +++ b/app/Services/Email/EmailObject.php @@ -108,4 +108,6 @@ class EmailObject public ?string $invitation_key = null; public array $documents = []; + + public ?string $template = null; //invoice //quote //reminder1 } diff --git a/app/Utils/PaymentHtmlEngine.php b/app/Utils/PaymentHtmlEngine.php new file mode 100644 index 000000000000..48d847f37c0c --- /dev/null +++ b/app/Utils/PaymentHtmlEngine.php @@ -0,0 +1,349 @@ +payment = $payment; + $this->company = $payment->company; + $this->client = $payment->client; + $this->contact = $contact ?: $this->client->contacts()->first(); + $this->contact->load('client.company'); + $this->settings = $this->client->getMergedSettings(); + $this->helpers = new Helpers(); + } + + public function makePaymentVariables() + { + App::forgetInstance('translator'); + $t = app('translator'); + App::setLocale($this->contact->preferredLocale()); + $t->replace(Ninja::transformTranslations($this->client->getMergedSettings())); + + $data = []; + + $data['$from'] = ['value' => '', 'label' => ctrans('texts.from')]; + $data['$to'] = ['value' => '', 'label' => ctrans('texts.to')]; + $data['$number'] = ['value' => $this->payment->number ?: ' ', 'label' => ctrans('texts.payment_number')]; + $data['$payment.number'] = &$data['$number']; + $data['$entity'] = ['value' => '', 'label' => ctrans('texts.payment')]; + $data['$payment.amount'] = ['value' => Number::formatMoney($this->payment->amount, $this->client) ?: ' ', 'label' => ctrans('texts.amount')]; + $data['$payment.refunded'] = ['value' => Number::formatMoney($this->payment->refunded, $this->client) ?: ' ', 'label' => ctrans('texts.refund')]; + $data['$amount'] = &$data['$payment.amount']; + $data['$payment.date'] = ['value' => $this->translateDate($this->payment->date, $this->client->date_format(), $this->client->locale()), 'label' => ctrans('texts.payment_date')]; + $data['$transaction_reference'] = ['value' => $this->payment->transaction_reference, 'label' => ctrans('texts.transaction_reference')]; + $data['$public_notes'] = ['value' => $this->payment->public_notes, 'label' => ctrans('texts.notes')]; + + $data['$payment1'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'payment1', $this->payment->custom_value1, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'payment1')]; + $data['$payment2'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'payment2', $this->payment->custom_value2, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'payment2')]; + $data['$payment3'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'payment3', $this->payment->custom_value3, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'payment3')]; + $data['$payment4'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'payment4', $this->payment->custom_value4, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'payment4')]; + + $data['$custom1'] = &$data['$payment1']; + $data['$custom2'] = &$data['$payment2']; + $data['$custom3'] = &$data['$payment3']; + $data['$custom4'] = &$data['$payment4']; + + $data['$client1'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'client1', $this->client->custom_value1, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'client1')]; + $data['$client2'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'client2', $this->client->custom_value2, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'client2')]; + $data['$client3'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'client3', $this->client->custom_value3, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'client3')]; + $data['$client4'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'client4', $this->client->custom_value4, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'client4')]; + $data['$address1'] = ['value' => $this->client->address1 ?: ' ', 'label' => ctrans('texts.address1')]; + $data['$address2'] = ['value' => $this->client->address2 ?: ' ', 'label' => ctrans('texts.address2')]; + $data['$id_number'] = ['value' => $this->client->id_number ?: ' ', 'label' => ctrans('texts.id_number')]; + $data['$client.number'] = ['value' => $this->client->number ?: ' ', 'label' => ctrans('texts.number')]; + $data['$vat_number'] = ['value' => $this->client->vat_number ?: ' ', 'label' => ctrans('texts.vat_number')]; + $data['$website'] = ['value' => $this->client->present()->website() ?: ' ', 'label' => ctrans('texts.website')]; + $data['$phone'] = ['value' => $this->client->present()->phone() ?: ' ', 'label' => ctrans('texts.phone')]; + $data['$country'] = ['value' => isset($this->client->country->name) ? $this->client->country->name : '', 'label' => ctrans('texts.country')]; + $data['$email'] = ['value' => isset($this->contact) ? $this->contact->email : 'no contact email on record', 'label' => ctrans('texts.email')]; + $data['$client_name'] = ['value' => $this->client->present()->name() ?: ' ', 'label' => ctrans('texts.client_name')]; + $data['$client.name'] = &$data['$client_name']; + $data['$client'] = &$data['$client_name']; + $data['$client.address1'] = &$data['$address1']; + $data['$client.address2'] = &$data['$address2']; + $data['$client_address'] = ['value' => $this->client->present()->address() ?: ' ', 'label' => ctrans('texts.address')]; + $data['$client.address'] = &$data['$client_address']; + $data['$client.id_number'] = &$data['$id_number']; + $data['$client.vat_number'] = &$data['$vat_number']; + $data['$client.website'] = &$data['$website']; + $data['$client.phone'] = &$data['$phone']; + $data['$city_state_postal'] = ['value' => $this->client->present()->cityStateZip($this->client->city, $this->client->state, $this->client->postal_code, false) ?: ' ', 'label' => ctrans('texts.city_state_postal')]; + $data['$client.city_state_postal'] = &$data['$city_state_postal']; + $data['$postal_city_state'] = ['value' => $this->client->present()->cityStateZip($this->client->city, $this->client->state, $this->client->postal_code, true) ?: ' ', 'label' => ctrans('texts.postal_city_state')]; + $data['$client.postal_city_state'] = &$data['$postal_city_state']; + $data['$postal_city'] = ['value' => $this->client->present()->cityStateZip($this->client->city, null, $this->client->postal_code, true) ?: ' ', 'label' => ctrans('texts.postal_city')]; + $data['$client.postal_city'] = &$data['$postal_city']; + $data['$client.country'] = &$data['$country']; + $data['$client.email'] = &$data['$email']; + + $data['$client.balance'] = ['value' => Number::formatMoney($this->client->balance, $this->client), 'label' => ctrans('texts.account_balance')]; + $data['$outstanding'] = ['value' => Number::formatMoney($this->client->balance, $this->client), 'label' => ctrans('texts.account_balance')]; + $data['$client_balance'] = ['value' => Number::formatMoney($this->client->balance, $this->client), 'label' => ctrans('texts.account_balance')]; + $data['$paid_to_date'] = ['value' => Number::formatMoney($this->client->paid_to_date, $this->client), 'label' => ctrans('texts.paid_to_date')]; + + $data['$contact.full_name'] = ['value' => isset($this->contact) ? $this->contact->present()->name() : '', 'label' => ctrans('texts.name')]; + $data['$contact.email'] = ['value' => isset($this->contact) ? $this->contact->email : '', 'label' => ctrans('texts.email')]; + $data['$contact.phone'] = ['value' => isset($this->contact) ? $this->contact->phone : '', 'label' => ctrans('texts.phone')]; + + $data['$contact.name'] = ['value' => isset($this->contact) ? $this->contact->present()->name() : 'no contact name on record', 'label' => ctrans('texts.contact_name')]; + $data['$contact.first_name'] = ['value' => isset($this->contact) ? $this->contact->first_name : '', 'label' => ctrans('texts.first_name')]; + $data['$contact.last_name'] = ['value' => isset($this->contact) ? $this->contact->last_name : '', 'label' => ctrans('texts.last_name')]; + $data['$contact.custom1'] = ['value' => isset($this->contact) ? $this->contact->custom_value1 : ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'contact1')]; + $data['$contact.custom2'] = ['value' => isset($this->contact) ? $this->contact->custom_value2 : ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'contact1')]; + $data['$contact.custom3'] = ['value' => isset($this->contact) ? $this->contact->custom_value3 : ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'contact1')]; + $data['$contact.custom4'] = ['value' => isset($this->contact) ? $this->contact->custom_value4 : ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'contact1')]; + $data['$firstName'] = &$data['$contact.first_name']; + + $data['$company.city_state_postal'] = ['value' => $this->company->present()->cityStateZip($this->settings->city, $this->settings->state, $this->settings->postal_code, false) ?: ' ', 'label' => ctrans('texts.city_state_postal')]; + $data['$company.postal_city_state'] = ['value' => $this->company->present()->cityStateZip($this->settings->city, $this->settings->state, $this->settings->postal_code, true) ?: ' ', 'label' => ctrans('texts.postal_city_state')]; + $data['$company.postal_city'] = ['value' => $this->company->present()->cityStateZip($this->settings->city, null, $this->settings->postal_code, true) ?: ' ', 'label' => ctrans('texts.postal_city')]; + $data['$company.name'] = ['value' => $this->company->present()->name() ?: ' ', 'label' => ctrans('texts.company_name')]; + $data['$company.address1'] = ['value' => $this->settings->address1 ?: ' ', 'label' => ctrans('texts.address1')]; + $data['$company.address2'] = ['value' => $this->settings->address2 ?: ' ', 'label' => ctrans('texts.address2')]; + $data['$company.city'] = ['value' => $this->settings->city ?: ' ', 'label' => ctrans('texts.city')]; + $data['$company.state'] = ['value' => $this->settings->state ?: ' ', 'label' => ctrans('texts.state')]; + $data['$company.postal_code'] = ['value' => $this->settings->postal_code ?: ' ', 'label' => ctrans('texts.postal_code')]; + //$data['$company.country'] = ['value' => $this->getCountryName(), 'label' => ctrans('texts.country')]; + $data['$company.phone'] = ['value' => $this->settings->phone ?: ' ', 'label' => ctrans('texts.phone')]; + $data['$company.email'] = ['value' => $this->settings->email ?: ' ', 'label' => ctrans('texts.email')]; + $data['$company.vat_number'] = ['value' => $this->settings->vat_number ?: ' ', 'label' => ctrans('texts.vat_number')]; + $data['$company.id_number'] = ['value' => $this->settings->id_number ?: ' ', 'label' => ctrans('texts.id_number')]; + $data['$company.website'] = ['value' => $this->settings->website ?: ' ', 'label' => ctrans('texts.website')]; + $data['$company.address'] = ['value' => $this->company->present()->address($this->settings) ?: ' ', 'label' => ctrans('texts.address')]; + + $logo = $this->company->present()->logo($this->settings); + + $data['$company.logo'] = ['value' => $logo ?: ' ', 'label' => ctrans('texts.logo')]; + $data['$company_logo'] = &$data['$company.logo']; + $data['$company1'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'company1', $this->settings->custom_value1, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'company1')]; + $data['$company2'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'company2', $this->settings->custom_value2, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'company2')]; + $data['$company3'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'company3', $this->settings->custom_value3, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'company3')]; + $data['$company4'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'company4', $this->settings->custom_value4, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'company4')]; + + $data['$view_link'] = ['value' => $this->buildViewButton($this->payment->getLink(), ctrans('texts.view_payment')), 'label' => ctrans('texts.view_payment')]; + $data['$view_button'] = &$data['$view_link']; + $data['$viewButton'] = &$data['$view_link']; + $data['$viewLink'] = &$data['$view_link']; + $data['$paymentLink'] = &$data['$view_link']; + $data['$portalButton'] = ['value' => $this->buildViewButton($this->payment->getPortalLink(), ctrans('texts.login')), 'label' =>'']; + $data['$portal_url'] = &$data['$portalButton']; + + $data['$view_url'] = ['value' => $this->payment->getLink(), 'label' => ctrans('texts.view_payment')]; + $data['$signature'] = ['value' => $this->settings->email_signature ?: ' ', 'label' => '']; + $data['$emailSignature'] = &$data['$signature']; + + $data['$invoices'] = ['value' => $this->formatInvoices(), 'label' => ctrans('texts.invoices')]; + $data['$invoice_references'] = ['value' => $this->formatInvoiceReferences(), 'label' => ctrans('texts.invoices')]; + $data['$invoice'] = ['value' => $this->formatInvoice(), 'label' => ctrans('texts.invoices')]; + $data['$invoice.po_number'] = ['value' => $this->formatPoNumber(), 'label' => ctrans('texts.po_number')]; + $data['$poNumber'] = &$data['$invoice.po_number']; + $data['$payment.status'] = ['value' => $this->payment->stringStatus($this->payment->status_id), 'label' => ctrans('texts.payment_status')]; + $data['$invoices.amount'] = ['value' => $this->formatInvoiceField('amount'), 'label' => ctrans('texts.invoices')]; + $data['$invoices.balance'] = ['value' => $this->formatInvoiceField('balance'), 'label' => ctrans('texts.invoices')]; + $data['$invoices.due_date'] = ['value' => $this->formatInvoiceField('due_date'), 'label' => ctrans('texts.invoices')]; + $data['$invoices.po_number'] = ['value' => $this->formatInvoiceField('po_number'), 'label' => ctrans('texts.invoices')]; + + + if ($this->payment->status_id == 4) { + $data['$status_logo'] = ['value' => '
' . ctrans('texts.paid') .'
', 'label' => '']; + } else { + $data['$status_logo'] = ['value' => '', 'label' => '']; + } + + + $arrKeysLength = array_map('strlen', array_keys($data)); + array_multisort($arrKeysLength, SORT_DESC, $data); + + return $data; + + } + + private function formatInvoiceField($field) + { + $invoicex = ''; + + foreach ($this->payment->invoices as $invoice) { + $invoice_field = $invoice->{$field}; + + if (in_array($field, ['amount', 'balance'])) { + $invoice_field = Number::formatMoney($invoice_field, $this->client); + } + + if ($field == 'due_date') { + $invoice_field = $this->translateDate($invoice_field, $this->client->date_format(), $this->client->locale()); + } + + $invoicex .= ctrans('texts.invoice_number_short') . "{$invoice->number} {$invoice_field}"; + } + + return $invoicex; + } + + private function formatInvoice() + { + $invoice = ''; + + if ($this->payment->invoices()->exists()) { + $invoice = ctrans('texts.invoice_number_short').implode(',', $this->payment->invoices->pluck('number')->toArray()); + } + + return $invoice; + } + + private function formatPoNumber() + { + $invoice = ''; + + if ($this->payment->invoices()->exists()) { + $invoice = ctrans('texts.po_number_short').implode(',', $this->payment->invoices->pluck('po_number')->toArray()); + } + + return $invoice; + } + + private function formatInvoices() + { + $invoice_list = '

'; + + foreach ($this->payment->invoices as $invoice) { + $invoice_list .= ctrans('texts.invoice_number_short')." {$invoice->number} ".Number::formatMoney($invoice->pivot->amount, $this->client).'
'; + } + + return $invoice_list; + } + + private function formatInvoiceReferences() + { + $invoice_list = '

'; + + foreach ($this->payment->invoices as $invoice) { + if (strlen($invoice->po_number) > 1) { + $invoice_list .= ctrans('texts.po_number')." {$invoice->po_number}
"; + } + + $invoice_list .= ctrans('texts.invoice_number_short')." {$invoice->number}
"; + $invoice_list .= ctrans('texts.invoice_amount').' '.Number::formatMoney($invoice->pivot->amount, $this->client).'
'; + $invoice_list .= ctrans('texts.invoice_balance').' '.Number::formatMoney($invoice->fresh()->balance, $this->client).'
'; + $invoice_list .= '-----
'; + } + + return $invoice_list; + } + + public function makeValues() :array + { + $data = []; + + $values = $this->makePaymentVariables(); + + foreach ($values as $key => $value) { + $data[$key] = $value['value']; + } + + return $data; + } + + /** + * generateLabelsAndValues + * + * @return void + */ + public function generateLabelsAndValues() + { + $data = []; + + $values = $this->makePaymentVariables(); + + foreach ($values as $key => $value) { + $data['values'][$key] = $value['value']; + $data['labels'][$key.'_label'] = $value['label']; + } + + return $data; + } + + /** + * buildViewButton + * + * @param string $link + * @param string $text + * @return string + */ + private function buildViewButton(string $link, string $text): string + { + return ' +
+ + + + + + +
+ + '. $text .' + +
+ +
+ '; + + + return ' + + + + +
+ '. $text .' +
+ '; + } + + + + +} \ No newline at end of file