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

104 lines
3.8 KiB
PHP
Raw Normal View History

2023-11-24 06:10:10 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Expense;
use App\Libraries\MultiDB;
2023-11-26 08:41:42 +01:00
use App\Models\Activity;
use App\Models\Expense;
2023-11-24 06:10:10 +01:00
use App\Models\VendorContact;
2023-11-26 08:41:42 +01:00
use App\Repositories\ActivityRepository;
2023-11-24 06:10:10 +01:00
use App\Services\Email\Email;
use App\Services\Email\EmailObject;
2023-11-26 08:41:42 +01:00
use App\Utils\Ninja;
use App\Utils\Number;
2023-11-24 06:10:10 +01:00
use App\Utils\Traits\MakesDates;
2023-11-26 08:41:42 +01:00
use Illuminate\Bus\Queueable;
2023-11-24 06:10:10 +01:00
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
2023-11-26 08:41:42 +01:00
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
2023-11-24 06:10:10 +01:00
class VendorExpenseNotify implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesDates;
public $tries = 1;
public function __construct(private Expense $expense, private string $db)
{
}
public function handle()
{
MultiDB::setDB($this->db);
2023-11-30 07:59:17 +01:00
if(!$this->expense->vendor) {
return;
2023-11-30 07:59:17 +01:00
}
2023-11-26 08:41:42 +01:00
$this->expense->vendor->contacts->filter(function (VendorContact $contact) {
2023-11-24 06:10:10 +01:00
return $contact->send_email && $contact->email;
2023-11-26 08:41:42 +01:00
})->each(function (VendorContact $contact) {
2023-11-24 06:10:10 +01:00
$this->notify($contact);
});
}
private function notify(VendorContact $contact)
{
$mo = new EmailObject;
$mo->contact = $contact;
$mo->vendor_contact_id = $contact->id;
$mo->user_id = $this->expense->user_id;
$mo->company_key = $this->expense->company->company_key;
$mo->subject = ctrans('texts.vendor_notification_subject', [
'amount' => Number::formatMoney($this->expense->amount, $contact->vendor),
'vendor' => $contact->vendor->present()->name(),
]);
$mo->body = ctrans('texts.vendor_notification_body', [
'vendor' => $this->expense->vendor->present()->name(),
'amount' => Number::formatMoney($this->expense->amount, $contact->vendor),
'contact' => $contact->present()->name(),
'payment_date' => $this->translateDate($this->expense->payment_date, $this->expense->company->date_format(), $this->expense->vendor->locale()),
'transaction_reference' => $this->expense->transaction_reference ?? '',
'number' => $this->expense->number,
]);
$mo->template = '';
$mo->email_template_body = 'vendor_notification_subject';
$mo->email_template_subject = 'vendor_notification_body';
$mo->vendor_id = $contact->vendor_id ?? null;
$mo->variables = [
'amount' => Number::formatMoney($this->expense->amount, $contact->vendor),
'contact' => $contact->present()->name(),
'vendor' => $contact->vendor->present()->name(),
'payment_date' => $this->translateDate($this->expense->payment_date, $this->expense->company->date_format(), $this->expense->vendor->locale()),
'transaction_reference' => $this->expense->transaction_reference ?? '',
'number' => $this->expense->number,
];
Email::dispatch($mo, $this->expense->company);
$fields = new \stdClass();
$fields->expense_id = $this->expense->id;
$fields->vendor_id = $contact->vendor_id;
$fields->vendor_contact_id = $contact->id;
$fields->user_id = $this->expense->user_id;
$fields->company_id = $contact->company_id;
$fields->activity_type_id = Activity::VENDOR_NOTIFICATION_EMAIL;
$fields->account_id = $this->expense->company->account_id;
$activity_repo = new ActivityRepository();
$activity_repo->save($fields, $this->expense, Ninja::eventVars());
}
2023-11-26 08:41:42 +01:00
}