1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Jobs/Mail/PaymentFailedMailer.php

129 lines
4.0 KiB
PHP
Raw Normal View History

2021-10-17 12:40:40 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2021-10-17 12:40:40 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Mail;
use App\Libraries\MultiDB;
2021-10-26 05:44:34 +02:00
use App\Mail\Admin\ClientPaymentFailureObject;
2021-10-17 12:40:40 +02:00
use App\Mail\Admin\PaymentFailureObject;
use App\Models\Client;
use App\Models\Company;
2021-11-27 09:59:55 +01:00
use App\Models\Invoice;
2021-10-17 12:40:40 +02:00
use App\Models\PaymentHash;
use App\Utils\Traits\MakesHash;
2021-10-17 12:40:40 +02:00
use App\Utils\Traits\Notifications\UserNotifies;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Mail;
2021-10-17 12:40:40 +02:00
/*Multi Mailer implemented*/
class PaymentFailedMailer implements ShouldQueue
{
2024-01-14 05:05:00 +01:00
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
use UserNotifies;
use MakesHash;
2021-10-17 12:40:40 +02:00
2021-11-04 10:52:03 +01:00
public ?PaymentHash $payment_hash;
2021-10-17 12:40:40 +02:00
2022-08-07 23:54:31 +02:00
public $error;
2021-10-17 12:40:40 +02:00
public Company $company;
public Client $client;
/**
* Create a new job instance.
*
* @param $client
* @param $message
* @param $company
* @param $amount
*/
2022-08-07 23:54:31 +02:00
public function __construct(?PaymentHash $payment_hash, Company $company, Client $client, $error)
2021-10-17 12:40:40 +02:00
{
$this->payment_hash = $payment_hash;
$this->client = $client;
$this->error = $error;
$this->company = $company;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
2024-01-14 05:05:00 +01:00
if (!is_string($this->error) || strlen($this->error) <= 1) {
2023-03-22 07:45:33 +01:00
$this->error = "";
2022-08-07 23:54:31 +02:00
}
2021-10-17 12:40:40 +02:00
//Set DB
MultiDB::setDb($this->company->db);
App::setLocale($this->client->locale());
2021-10-17 12:40:40 +02:00
$settings = $this->client->getMergedSettings();
2021-10-17 12:40:40 +02:00
$amount = 0;
2021-11-27 09:59:55 +01:00
$invoice = false;
2021-10-17 12:40:40 +02:00
if ($this->payment_hash) {
2023-03-24 00:54:13 +01:00
// $amount = array_sum(array_column($this->payment_hash->invoices(), 'amount')) + $this->payment_hash->fee_total;
2024-01-14 05:05:00 +01:00
$amount = $this->payment_hash?->amount_with_fee() ?: 0;
2023-08-06 09:03:12 +02:00
$invoice = Invoice::query()->whereIn('id', $this->transformKeys(array_column($this->payment_hash->invoices(), 'invoice_id')))->withTrashed()->first();
2021-11-27 09:59:55 +01:00
}
2021-10-17 12:40:40 +02:00
//iterate through company_users
$this->company->company_users->each(function ($company_user) use ($amount, $settings, $invoice) {
2021-11-27 09:59:55 +01:00
$methods = $this->findUserEntityNotificationType($invoice ?: $this->client, $company_user, ['payment_failure_user', 'payment_failure_all', 'payment_failure', 'all_notifications']);
2021-10-17 12:40:40 +02:00
//if mail is a method type -fire mail!!
if (($key = array_search('mail', $methods)) !== false) {
unset($methods[$key]);
2023-06-07 08:25:41 +02:00
$mail_obj = (new PaymentFailureObject($this->client, $this->error, $this->company, $amount, $this->payment_hash, $company_user->portalType()))->build();
2021-10-17 12:40:40 +02:00
2024-01-14 05:05:00 +01:00
$nmo = new NinjaMailerObject();
2021-10-17 12:40:40 +02:00
$nmo->mailable = new NinjaMailer($mail_obj);
$nmo->company = $this->company;
$nmo->to_user = $company_user->user;
$nmo->settings = $settings;
NinjaMailerJob::dispatch($nmo);
}
});
//add client payment failures here.
2024-01-14 05:05:00 +01:00
if ($this->client->contacts()->whereNotNull('email')->exists() && $this->payment_hash) {
2022-05-17 03:18:34 +02:00
$contact = $this->client->contacts()->whereNotNull('email')->first();
2021-10-26 05:44:34 +02:00
$mail_obj = (new ClientPaymentFailureObject($this->client, $this->error, $this->company, $this->payment_hash))->build();
2024-01-14 05:05:00 +01:00
$nmo = new NinjaMailerObject();
2021-10-26 05:44:34 +02:00
$nmo->mailable = new NinjaMailer($mail_obj);
$nmo->company = $this->company;
$nmo->to_user = $contact;
$nmo->settings = $settings;
NinjaMailerJob::dispatch($nmo);
}
2021-10-17 12:40:40 +02:00
}
}