1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/Jobs/Bank/MatchBankTransactions.php

445 lines
14 KiB
PHP
Raw Normal View History

2022-09-15 07:02:39 +02:00
<?php
/**
* Credit Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Credit Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Bank;
use App\Events\Invoice\InvoiceWasPaid;
use App\Events\Payment\PaymentWasCreated;
2022-09-15 09:31:32 +02:00
use App\Factory\ExpenseCategoryFactory;
use App\Factory\ExpenseFactory;
2022-09-15 07:02:39 +02:00
use App\Factory\PaymentFactory;
use App\Helpers\Bank\Yodlee\Yodlee;
use App\Libraries\Currency\Conversion\CurrencyApi;
use App\Libraries\MultiDB;
use App\Models\BankTransaction;
use App\Models\Company;
use App\Models\Expense;
2022-09-15 09:31:32 +02:00
use App\Models\ExpenseCategory;
2022-09-15 07:02:39 +02:00
use App\Models\Invoice;
use App\Models\Payment;
use App\Utils\Ninja;
2022-09-15 09:31:32 +02:00
use App\Utils\Traits\GeneratesCounter;
2022-09-21 09:00:49 +02:00
use App\Utils\Traits\MakesHash;
2022-09-15 07:02:39 +02:00
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon;
2022-09-22 08:20:54 +02:00
use Illuminate\Support\Facades\Cache;
2022-09-15 07:02:39 +02:00
class MatchBankTransactions implements ShouldQueue
{
2022-09-21 09:00:49 +02:00
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, GeneratesCounter, MakesHash;
2022-09-15 07:02:39 +02:00
private int $company_id;
private string $db;
private array $input;
2023-08-07 07:30:34 +02:00
/** @var \App\Models\Company */
2023-07-26 01:27:16 +02:00
protected ?Company $company;
2022-09-15 07:02:39 +02:00
public Invoice $invoice;
/** @var \App\Models\BankTransaction $bt */
2023-07-26 01:27:16 +02:00
private ?BankTransaction $bt;
2022-09-15 07:02:39 +02:00
2022-09-15 08:15:57 +02:00
private $categories;
2022-09-21 09:00:49 +02:00
private float $available_balance = 0;
2023-12-01 14:30:33 +01:00
private float $applied_amount = 0;
2022-09-21 09:00:49 +02:00
private array $attachable_invoices = [];
2022-10-24 12:20:29 +02:00
public $bts;
2022-09-15 07:02:39 +02:00
/**
* Create a new job instance.
*/
public function __construct(int $company_id, string $db, array $input)
{
$this->company_id = $company_id;
$this->db = $db;
2022-10-24 11:00:49 +02:00
$this->input = $input['transactions'];
2022-09-15 08:28:18 +02:00
$this->categories = collect();
2022-10-24 12:20:29 +02:00
$this->bts = collect();
2022-09-15 07:02:39 +02:00
}
/**
* Execute the job.
*
*/
public function handle()
{
MultiDB::setDb($this->db);
2023-08-07 07:37:30 +02:00
$this->company = Company::query()->find($this->company_id);
2022-09-15 07:02:39 +02:00
if ($this->company->account->bank_integration_account_id) {
$yodlee = new Yodlee($this->company->account->bank_integration_account_id);
2023-02-16 02:36:09 +01:00
} else {
2022-11-10 11:57:55 +01:00
$yodlee = false;
2023-02-16 02:36:09 +01:00
}
2022-09-15 08:28:18 +02:00
2022-09-22 08:20:54 +02:00
$bank_categories = Cache::get('bank_categories');
2023-12-01 14:30:33 +01:00
if (!$bank_categories && $yodlee) {
2022-09-22 08:20:54 +02:00
$_categories = $yodlee->getTransactionCategories();
2022-09-15 08:28:18 +02:00
$this->categories = collect($_categories->transactionCategory);
Cache::forever('bank_categories', $this->categories);
2023-12-01 14:30:33 +01:00
} else {
2022-09-22 08:20:54 +02:00
$this->categories = collect($bank_categories);
2022-09-15 07:02:39 +02:00
}
2023-12-01 14:30:33 +01:00
foreach ($this->input as $input) {
2023-02-16 02:36:09 +01:00
if (array_key_exists('invoice_ids', $input) && strlen($input['invoice_ids']) >= 1) {
2022-10-24 10:57:59 +02:00
$this->matchInvoicePayment($input);
2023-02-16 02:36:09 +01:00
} elseif (array_key_exists('payment_id', $input) && strlen($input['payment_id']) >= 1) {
$this->linkPayment($input);
2023-02-16 02:36:09 +01:00
} elseif (array_key_exists('expense_id', $input) && strlen($input['expense_id']) >= 1) {
$this->linkExpense($input);
2023-02-16 02:36:09 +01:00
} elseif ((array_key_exists('vendor_id', $input) && strlen($input['vendor_id']) >= 1) || (array_key_exists('ninja_category_id', $input) && strlen($input['ninja_category_id']) >= 1)) {
2022-10-24 10:57:59 +02:00
$this->matchExpense($input);
2023-02-16 02:36:09 +01:00
}
2022-10-24 10:57:59 +02:00
}
2022-09-22 08:20:54 +02:00
2023-08-06 09:03:12 +02:00
return BankTransaction::query()->whereIn('id', $this->bts);
2022-09-15 07:02:39 +02:00
}
2023-04-25 23:56:47 +02:00
private function getInvoices(string $invoice_hashed_ids): array
2022-09-15 07:02:39 +02:00
{
2022-09-21 09:00:49 +02:00
$collection = collect();
2022-09-15 07:02:39 +02:00
2022-09-21 09:00:49 +02:00
$invoices = explode(",", $invoice_hashed_ids);
2022-09-21 07:43:35 +02:00
2023-12-01 14:30:33 +01:00
if (count($invoices) >= 1) {
foreach ($invoices as $invoice) {
2023-02-16 02:36:09 +01:00
if (is_string($invoice) && strlen($invoice) > 1) {
2022-09-21 09:00:49 +02:00
$collection->push($this->decodePrimaryKey($invoice));
2023-02-16 02:36:09 +01:00
}
2022-09-21 09:00:49 +02:00
}
}
2023-04-25 23:56:47 +02:00
return $collection->toArray();
2022-09-21 09:00:49 +02:00
}
2023-12-01 14:30:33 +01:00
private function checkPayable($invoices): bool
2022-09-21 09:00:49 +02:00
{
2023-12-01 14:30:33 +01:00
foreach ($invoices as $invoice) {
$invoice->service()->markSent();
2023-12-01 14:30:33 +01:00
2023-02-16 02:36:09 +01:00
if (!$invoice->isPayable()) {
2022-09-21 09:00:49 +02:00
return false;
2023-02-16 02:36:09 +01:00
}
2022-09-21 09:00:49 +02:00
}
return true;
}
private function linkExpense($input)
{
2023-08-03 05:36:15 +02:00
$this->bt = BankTransaction::withTrashed()->find($input['id']);
if (!$this->bt) {
return $this;
2023-02-16 02:36:09 +01:00
}
2023-04-08 10:34:43 +02:00
$_expenses = explode(",", $input['expense_id']);
foreach ($_expenses as $_expense) {
2023-04-08 10:34:43 +02:00
$expense = Expense::withTrashed()
->where('id', $this->decodePrimaryKey($_expense))
->where('company_id', $this->bt->company_id)
->first();
2023-04-08 10:34:43 +02:00
if ($expense && !$expense->transaction_id) {
$expense->transaction_id = $this->bt->id;
$expense->save();
2023-04-08 10:34:43 +02:00
$this->bt->expense_id = $this->coalesceExpenses($expense->hashed_id);
$this->bt->status_id = BankTransaction::STATUS_CONVERTED;
$this->bt->vendor_id = $expense->vendor_id;
$this->bt->ninja_category_id = $expense->category_id;
$this->bt->save();
2022-12-05 08:42:06 +01:00
2023-04-08 10:34:43 +02:00
$this->bts->push($this->bt->id);
}
}
2023-12-01 14:30:33 +01:00
2022-12-05 08:42:06 +01:00
return $this;
}
2023-10-26 04:57:44 +02:00
private function coalesceExpenses($expense): string
{
2023-04-06 03:38:29 +02:00
if (!$this->bt->expense_id || strlen($this->bt->expense_id) < 1) {
return $expense;
}
return collect(explode(",", $this->bt->expense_id))->push($expense)->implode(",");
2022-12-05 08:42:06 +01:00
}
private function linkPayment($input)
{
$this->bt = BankTransaction::query()->withTrashed()->find($input['id']);
2023-02-16 02:36:09 +01:00
if (!$this->bt || $this->bt->status_id == BankTransaction::STATUS_CONVERTED) {
return $this;
2023-02-16 02:36:09 +01:00
}
$payment = Payment::withTrashed()->find($input['payment_id']);
2023-12-01 14:30:33 +01:00
if ($payment && !$payment->transaction_id) {
$payment->transaction_id = $this->bt->id;
2023-04-25 23:56:47 +02:00
$payment->saveQuietly();
$this->bt->payment_id = $payment->id;
$this->bt->status_id = BankTransaction::STATUS_CONVERTED;
$this->bt->invoice_ids = collect($payment->invoices)->pluck('hashed_id')->implode(',');
$this->bt->save();
2022-12-05 08:42:06 +01:00
$this->bts->push($this->bt->id);
}
2022-12-05 08:42:06 +01:00
return $this;
}
2023-12-01 14:30:33 +01:00
private function matchInvoicePayment($input): self
{
2023-08-05 04:26:07 +02:00
$this->bt = BankTransaction::withTrashed()->find($input['id']);
2022-09-21 09:00:49 +02:00
2023-02-16 02:36:09 +01:00
if (!$this->bt || $this->bt->status_id == BankTransaction::STATUS_CONVERTED) {
2023-12-01 14:30:33 +01:00
return $this;
2023-02-16 02:36:09 +01:00
}
$_invoices = Invoice::query()
->withTrashed()
->where('company_id', $this->bt->company_id)
->whereIn('id', $this->getInvoices($input['invoice_ids']));
2023-12-01 14:30:33 +01:00
2022-11-10 11:57:55 +01:00
$amount = $this->bt->amount;
2022-09-15 07:02:39 +02:00
2023-12-01 14:30:33 +01:00
if ($_invoices && $this->checkPayable($_invoices)) {
2022-09-21 09:00:49 +02:00
$this->createPayment($_invoices, $amount);
2022-09-15 07:02:39 +02:00
2022-12-05 08:42:06 +01:00
$this->bts->push($this->bt->id);
}
2022-10-24 12:20:29 +02:00
2022-09-22 08:20:54 +02:00
return $this;
2022-09-15 07:02:39 +02:00
}
2023-12-01 14:30:33 +01:00
private function matchExpense($input): self
{
2022-09-15 08:15:57 +02:00
//if there is a category id, pull it from Yodlee and insert - or just reuse!!
$this->bt = BankTransaction::query()->withTrashed()->find($input['id']);
2022-09-15 08:28:18 +02:00
2023-02-16 02:36:09 +01:00
if (!$this->bt || $this->bt->status_id == BankTransaction::STATUS_CONVERTED) {
2023-12-01 14:30:33 +01:00
return $this;
2023-02-16 02:36:09 +01:00
}
2022-09-15 09:31:32 +02:00
$expense = ExpenseFactory::create($this->bt->company_id, $this->bt->user_id);
2022-10-24 10:57:59 +02:00
$expense->category_id = $this->resolveCategory($input);
2022-09-15 09:31:32 +02:00
$expense->amount = $this->bt->amount;
$expense->number = $this->getNextExpenseNumber($expense);
2022-09-22 07:54:58 +02:00
$expense->currency_id = $this->bt->currency_id;
2022-09-15 09:31:32 +02:00
$expense->date = Carbon::parse($this->bt->date);
2022-09-23 04:42:41 +02:00
$expense->payment_date = Carbon::parse($this->bt->date);
$expense->transaction_reference = $this->bt->description;
$expense->transaction_id = $this->bt->id;
2022-11-24 21:52:47 +01:00
2023-02-16 02:36:09 +01:00
if (array_key_exists('vendor_id', $input)) {
2022-11-24 21:52:47 +01:00
$expense->vendor_id = $input['vendor_id'];
2023-02-16 02:36:09 +01:00
}
2022-11-24 21:52:47 +01:00
$expense->invoice_documents = $this->company->invoice_expense_documents;
$expense->should_be_invoiced = $this->company->mark_expenses_invoiceable;
2022-09-15 09:31:32 +02:00
$expense->save();
$this->bt->expense_id = $this->coalesceExpenses($expense->hashed_id);
2022-11-24 21:52:47 +01:00
2023-02-16 02:36:09 +01:00
if (array_key_exists('vendor_id', $input)) {
2022-11-24 21:52:47 +01:00
$this->bt->vendor_id = $input['vendor_id'];
2023-02-16 02:36:09 +01:00
}
2023-12-01 14:30:33 +01:00
2022-09-23 04:42:41 +02:00
$this->bt->status_id = BankTransaction::STATUS_CONVERTED;
2022-09-23 04:34:52 +02:00
$this->bt->save();
2022-10-24 12:23:52 +02:00
$this->bts->push($this->bt->id);
2022-10-24 12:20:29 +02:00
2022-09-22 08:20:54 +02:00
return $this;
2022-09-15 07:02:39 +02:00
}
2023-12-01 14:30:33 +01:00
private function createPayment($invoices, float $amount): void
2022-09-15 07:02:39 +02:00
{
2022-09-21 09:00:49 +02:00
$this->available_balance = $amount;
2023-12-01 14:30:33 +01:00
\DB::connection(config('database.default'))->transaction(function () use ($invoices) {
2023-08-08 12:39:46 +02:00
$invoices->each(function ($invoice) {
2022-09-21 09:00:49 +02:00
$this->invoice = Invoice::withTrashed()->where('id', $invoice->id)->lockForUpdate()->first();
2023-12-01 14:30:33 +01:00
$_amount = false;
if (floatval($this->invoice->balance) < floatval($this->available_balance) && $this->available_balance > 0) {
$_amount = $this->invoice->balance;
$this->applied_amount += $this->invoice->balance;
$this->available_balance = $this->available_balance - $this->invoice->balance;
} elseif (floatval($this->invoice->balance) >= floatval($this->available_balance) && $this->available_balance > 0) {
$_amount = $this->available_balance;
$this->applied_amount += $this->available_balance;
$this->available_balance = 0;
}
if ($_amount) {
$this->attachable_invoices[] = ['id' => $this->invoice->id, 'amount' => $_amount];
$this->invoice
->service()
->setExchangeRate()
->updateBalance($_amount * -1)
->updatePaidToDate($_amount)
->setCalculatedStatus()
->save();
}
});
}, 2);
2022-09-15 07:02:39 +02:00
2023-02-16 02:36:09 +01:00
if (!$this->invoice) {
2022-11-24 22:25:34 +01:00
return;
2023-02-16 02:36:09 +01:00
}
2023-12-01 14:30:33 +01:00
2022-09-15 07:02:39 +02:00
/* Create Payment */
$payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id);
$payment->amount = $this->bt->amount;
$payment->applied = $this->applied_amount;
2022-09-15 07:02:39 +02:00
$payment->status_id = Payment::STATUS_COMPLETED;
$payment->client_id = $this->invoice->client_id;
2022-09-21 09:00:49 +02:00
$payment->transaction_reference = $this->bt->description;
$payment->transaction_id = $this->bt->id;
2022-09-22 07:54:58 +02:00
$payment->currency_id = $this->bt->currency_id;
2022-09-15 07:02:39 +02:00
$payment->is_manual = false;
2022-09-15 08:15:57 +02:00
$payment->date = $this->bt->date ? Carbon::parse($this->bt->date) : now();
2023-12-01 14:30:33 +01:00
2022-09-15 07:02:39 +02:00
/* Bank Transfer! */
$payment_type_id = 1;
$payment->saveQuietly();
$payment->service()->applyNumber()->save();
2023-12-01 14:30:33 +01:00
2023-02-16 02:36:09 +01:00
if ($payment->client->getSetting('send_email_on_mark_paid')) {
2022-09-15 07:02:39 +02:00
$payment->service()->sendEmail();
2023-02-16 02:36:09 +01:00
}
2022-09-15 07:02:39 +02:00
$this->setExchangeRate($payment);
/* Create a payment relationship to the invoice entity */
2023-12-01 14:30:33 +01:00
foreach ($this->attachable_invoices as $attachable_invoice) {
2022-09-21 09:00:49 +02:00
$payment->invoices()->attach($attachable_invoice['id'], [
'amount' => $attachable_invoice['amount'],
]);
}
2022-09-15 07:02:39 +02:00
event('eloquent.created: App\Models\Payment', $payment);
$this->invoice->next_send_date = null;
$this->invoice
2023-12-01 14:30:33 +01:00
->service()
->applyNumber()
->deletePdf()
2023-12-01 14:30:33 +01:00
->save();
2022-09-15 07:02:39 +02:00
$payment->ledger()
2023-12-01 14:30:33 +01:00
->updatePaymentBalance($amount * -1);
2022-09-15 07:02:39 +02:00
$this->invoice
2023-12-01 14:30:33 +01:00
->client
->service()
->updateBalanceAndPaidToDate($this->applied_amount * -1, $amount)
->save();
2022-09-15 07:02:39 +02:00
$this->invoice = $this->invoice
2023-12-01 14:30:33 +01:00
->service()
->workFlow()
->save();
2022-09-15 07:02:39 +02:00
/* Update Invoice balance */
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
event(new InvoiceWasPaid($this->invoice, $payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
2023-04-25 23:56:47 +02:00
$this->bt->invoice_ids = $invoices->get()->pluck('hashed_id')->implode(',');
2022-09-21 07:43:35 +02:00
$this->bt->status_id = BankTransaction::STATUS_CONVERTED;
2022-12-06 04:50:37 +01:00
$this->bt->payment_id = $payment->id;
2022-09-15 07:02:39 +02:00
$this->bt->save();
}
2023-12-01 14:30:33 +01:00
private function resolveCategory($input): ?int
2022-09-15 08:28:18 +02:00
{
2023-12-01 14:30:33 +01:00
if (array_key_exists('ninja_category_id', $input) && (int) $input['ninja_category_id'] > 1) {
2022-10-24 10:57:59 +02:00
$this->bt->ninja_category_id = $input['ninja_category_id'];
2022-09-23 08:54:22 +02:00
$this->bt->save();
2022-10-24 12:20:29 +02:00
2023-12-01 14:30:33 +01:00
return (int) $input['ninja_category_id'];
2022-09-23 08:54:22 +02:00
}
2022-09-22 08:20:54 +02:00
2022-09-15 09:31:32 +02:00
$category = $this->categories->firstWhere('highLevelCategoryId', $this->bt->category_id);
2023-08-08 11:44:52 +02:00
$ec = ExpenseCategory::query()->where('company_id', $this->bt->company_id)->where('bank_category_id', $this->bt->category_id)->first();
2022-09-15 09:31:32 +02:00
2023-02-16 02:36:09 +01:00
if ($ec) {
2022-09-15 09:31:32 +02:00
return $ec->id;
2023-02-16 02:36:09 +01:00
}
2022-09-15 09:31:32 +02:00
2023-12-01 14:30:33 +01:00
if ($category) {
2022-09-15 09:31:32 +02:00
$ec = ExpenseCategoryFactory::create($this->bt->company_id, $this->bt->user_id);
$ec->bank_category_id = $this->bt->category_id;
$ec->name = $category->highLevelCategoryName;
$ec->save();
return $ec->id;
}
2023-12-01 14:30:33 +01:00
2022-09-15 09:31:32 +02:00
return null;
2022-09-15 08:28:18 +02:00
}
2022-09-15 07:02:39 +02:00
private function setExchangeRate(Payment $payment)
{
if ($payment->exchange_rate != 1) {
return;
}
$client_currency = $payment->client->getSetting('currency_id');
$company_currency = $payment->client->company->settings->currency_id;
if ($company_currency != $client_currency) {
$exchange_rate = new CurrencyApi();
$payment->exchange_rate = $exchange_rate->exchangeRate($client_currency, $company_currency, Carbon::parse($payment->date));
$payment->exchange_currency_id = $company_currency;
$payment->saveQuietly();
}
}
public function middleware()
{
return [new WithoutOverlapping($this->company_id)];
}
2023-12-01 14:30:33 +01:00
}