From 9fee3c538c13ad89117be86acf43679b2b999503 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 8 Oct 2023 15:06:34 +1100 Subject: [PATCH] Updates for structure of payment entities --- app/Jobs/Payment/EmailPayment.php | 11 +----- app/Jobs/Payment/EmailRefundPayment.php | 15 +++----- app/Mail/Engine/PaymentEmailEngine.php | 43 +++++++++++++++++++++-- app/Services/Template/TemplateAction.php | 26 +++++++++----- app/Services/Template/TemplateService.php | 20 ++--------- lang/en/texts.php | 2 ++ 6 files changed, 68 insertions(+), 49 deletions(-) diff --git a/app/Jobs/Payment/EmailPayment.php b/app/Jobs/Payment/EmailPayment.php index edcdf83a4c3f..1ec9ecce8b65 100644 --- a/app/Jobs/Payment/EmailPayment.php +++ b/app/Jobs/Payment/EmailPayment.php @@ -31,14 +31,8 @@ class EmailPayment implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; - public $payment; - public $email_builder; - private $contact; - - private $company; - public $settings; /** @@ -49,11 +43,8 @@ class EmailPayment implements ShouldQueue * @param $contact * @param $company */ - public function __construct(Payment $payment, Company $company, ?ClientContact $contact) + public function __construct(public Payment $payment, private Company $company, private ?ClientContact $contact) { - $this->payment = $payment; - $this->contact = $contact; - $this->company = $company; $this->settings = $payment->client->getMergedSettings(); } diff --git a/app/Jobs/Payment/EmailRefundPayment.php b/app/Jobs/Payment/EmailRefundPayment.php index a6e5de75b2a2..5563c6ee8af9 100644 --- a/app/Jobs/Payment/EmailRefundPayment.php +++ b/app/Jobs/Payment/EmailRefundPayment.php @@ -32,14 +32,8 @@ class EmailRefundPayment implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; - public $payment; - public $email_builder; - private $contact; - - private $company; - public $settings; /** @@ -50,11 +44,8 @@ class EmailRefundPayment implements ShouldQueue * @param $contact * @param $company */ - public function __construct(Payment $payment, Company $company, ClientContact $contact) + public function __construct(public Payment $payment, private Company $company, private ?ClientContact $contact) { - $this->payment = $payment; - $this->contact = $contact; - $this->company = $company; $this->settings = $payment->client->getMergedSettings(); } @@ -84,7 +75,9 @@ class EmailRefundPayment implements ShouldQueue $template_data['body'] = ctrans('texts.refunded_payment').' $payment.refunded

$invoices'; $template_data['subject'] = ctrans('texts.refunded_payment'); - $email_builder = (new PaymentEmailEngine($this->payment, $this->contact, $template_data))->build(); + $email_builder = new PaymentEmailEngine($this->payment, $this->contact, $template_data); + $email_builder->is_refund = true; + $email_builder->build(); $invitation = null; diff --git a/app/Mail/Engine/PaymentEmailEngine.php b/app/Mail/Engine/PaymentEmailEngine.php index 0e890e2165e7..df4dd211cf17 100644 --- a/app/Mail/Engine/PaymentEmailEngine.php +++ b/app/Mail/Engine/PaymentEmailEngine.php @@ -15,12 +15,14 @@ use App\Utils\Ninja; use App\Utils\Number; use App\Utils\Helpers; use App\Models\Account; +use App\Models\Payment; use App\Utils\Traits\MakesDates; use App\Jobs\Entity\CreateRawPdf; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\URL; use Illuminate\Support\Facades\Storage; use App\DataMapper\EmailTemplateDefaults; +use App\Services\Template\TemplateAction; class PaymentEmailEngine extends BaseEmailEngine { @@ -44,6 +46,8 @@ class PaymentEmailEngine extends BaseEmailEngine private $payment_template_subject; + public bool $is_refund = false; + public function __construct($payment, $contact, $template_data = null) { $this->payment = $payment; @@ -92,9 +96,44 @@ class PaymentEmailEngine extends BaseEmailEngine if ($this->client->getSetting('pdf_email_attachment') !== false && $this->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) { $this->payment->invoices->each(function ($invoice) { - $pdf = ((new CreateRawPdf($invoice->invitations->first(), $invoice->company->db))->handle()); - $this->setAttachments([['file' => base64_encode($pdf), 'name' => $invoice->numberFormatter().'.pdf']]); + if($this->is_refund && strlen($invoice->client->getSetting('payment_refund_design_id')) > 2) { + $pdf = (new TemplateAction( + [$this->payment->hashed_id], + $invoice->client->getSetting('payment_refund_design_id'), + Payment::class, + $this->payment->user_id, + $this->payment->company, + $this->payment->company->db, + 'nohash', + false + ))->handle(); + + $file_name = ctrans('texts.payment_refund_receipt', ['number' => $this->payment->number ]) . '.pdf'; + $file_name = str_replace(' ', '_', $file_name); + + } + elseif(!$this->is_refund && strlen($invoice->client->getSetting('payment_receipt_design_id')) > 2){ + $pdf = (new TemplateAction( + [$this->payment->hashed_id], + $invoice->client->getSetting('payment_refund_design_id'), + Payment::class, + $this->payment->user_id, + $this->payment->company, + $this->payment->company->db, + 'nohash', + false))->handle(); + + $file_name = ctrans('texts.payment_receipt', ['number' => $this->payment->number ]) . '.pdf'; + $file_name = str_replace(' ', '_', $file_name); + + } + else { + $pdf = ((new CreateRawPdf($invoice->invitations->first(), $invoice->company->db))->handle()); + $file_name = $invoice->numberFormatter().'.pdf'; + } + + $this->setAttachments([['file' => base64_encode($pdf), 'name' => $file_name]]); //attach invoice documents also to payments if ($this->client->getSetting('document_email_attachment') !== false) { diff --git a/app/Services/Template/TemplateAction.php b/app/Services/Template/TemplateAction.php index ff318b640c05..0449a86263ea 100644 --- a/app/Services/Template/TemplateAction.php +++ b/app/Services/Template/TemplateAction.php @@ -74,7 +74,8 @@ class TemplateAction implements ShouldQueue * */ public function handle() - { + { nlog("inside template action"); + MultiDB::setDb($this->db); $key = $this->resolveEntityString(); @@ -85,16 +86,22 @@ class TemplateAction implements ShouldQueue $template_service = new TemplateService($template); - if($this->entity == Invoice::class) { - $resource->with('payments', 'client'); - } + match($this->entity){ + Invoice::class => $resource->with('payments', 'client'), + Quote::class => $resource->with('client'), + Task::class => $resource->with('client'), + Credit::class => $resource->with('client'), + RecurringInvoice::class => $resource->with('client'), + Project::class => $resource->with('client'), + Expense::class => $resource->with('client'), + Payment::class => $resource->with('invoices', 'client'), + }; $result = $resource->withTrashed() ->whereIn('id', $this->transformKeys($this->ids)) ->where('company_id', $this->company->id) ->get(); - if($result->count() <= 1) $data[$key] = collect($result); else @@ -103,15 +110,16 @@ class TemplateAction implements ShouldQueue $ts = $template_service->build($data); nlog($ts->getHtml()); - $pdf = $ts->getPdf(); - if($this->send_email) + if($this->send_email) { + $pdf = $ts->getPdf(); $this->sendEmail($pdf, $template); + } else { - + $pdf = $ts->getPdf(); $filename = "templates/{$this->hash}.pdf"; Storage::disk(config('filesystems.default'))->put($filename, $pdf); - + return $pdf; } } diff --git a/app/Services/Template/TemplateService.php b/app/Services/Template/TemplateService.php index 9ee5d9796402..02abf677a7c9 100644 --- a/app/Services/Template/TemplateService.php +++ b/app/Services/Template/TemplateService.php @@ -514,6 +514,8 @@ class TemplateService 'custom_value2' => $payment->custom_value2 ?? '', 'custom_value3' => $payment->custom_value3 ?? '', 'custom_value4' => $payment->custom_value4 ?? '', + 'created_at' => $this->translateDate($payment->created_at, $payment->client->date_format(), $payment->client->locale()), + 'updated_at' => $this->translateDate($payment->updated_at, $payment->client->date_format(), $payment->client->locale()), 'client' => [ 'name' => $payment->client->present()->name(), 'balance' => $payment->client->balance, @@ -587,27 +589,11 @@ class TemplateService { $payments = $payments->map(function ($payment) { - // nlog(microtime(true)); return $this->transformPayment($payment); })->toArray(); - + return $payments; - // $it = new PaymentTransformer(); - // $it->setDefaultIncludes(['client','invoices','paymentables']); - // $manager = new Manager(); - // $manager->parseIncludes(['client','invoices','paymentables']); - // $resource = new \League\Fractal\Resource\Collection($payments, $it, null); - // $resources = $manager->createData($resource)->toArray(); - // foreach($resources['data'] as $key => $resource) { - - // $resources['data'][$key]['client'] = $resource['client']['data'] ?? []; - // $resources['data'][$key]['client']['contacts'] = $resource['client']['data']['contacts']['data'] ?? []; - // $resources['data'][$key]['invoices'] = $invoice['invoices']['data'] ?? []; - - // } - - // return $resources['data']; } diff --git a/lang/en/texts.php b/lang/en/texts.php index a5bc8f47feda..aa407bc9b054 100644 --- a/lang/en/texts.php +++ b/lang/en/texts.php @@ -5161,6 +5161,8 @@ $LANG = array( 'show_document_preview' => 'Show Document Preview', 'cash_accounting' => 'Cash accounting', 'click_or_drop_files_here' => 'Click or drop files here', + 'payment_refund_receipt' => 'Payment Refund Receipt # :number', + 'payment_receipt' => 'Payment Receipt # :number', ); return $LANG;