1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Jobs/Bank/MatchBankTransactions.php

342 lines
9.9 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\BankIntegration;
use App\Models\BankTransaction;
use App\Models\Company;
use App\Models\Currency;
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\Services\Bank\BankService;
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;
protected Company $company;
public Invoice $invoice;
private BankTransaction $bt;
2022-09-15 08:15:57 +02:00
private $categories;
2022-09-21 09:00:49 +02:00
private float $available_balance = 0;
private array $attachable_invoices = [];
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;
$this->input = $input;
2022-09-15 08:28:18 +02:00
$this->categories = collect();
2022-09-15 07:02:39 +02:00
}
/**
* Execute the job.
*
*
* @return void
*/
public function handle()
{
MultiDB::setDb($this->db);
$this->company = Company::find($this->company_id);
2022-09-15 08:15:57 +02:00
$yodlee = new Yodlee($this->company->account->bank_integration_account_id);
2022-09-15 08:28:18 +02:00
2022-09-22 08:20:54 +02:00
$bank_categories = Cache::get('bank_categories');
if(!$bank_categories){
$_categories = $yodlee->getTransactionCategories();
2022-09-15 08:28:18 +02:00
$this->categories = collect($_categories->transactionCategory);
2022-09-22 08:20:54 +02:00
}
else {
$this->categories = collect($bank_categories);
2022-09-15 07:02:39 +02:00
}
2022-09-22 08:20:54 +02:00
if(array_key_exists('invoice_ids', $this->input) && strlen($this->input['invoice_ids']) > 1)
$this->matchInvoicePayment();
else
$this->matchExpense();
return $this->bt;
2022-09-15 07:02:39 +02:00
}
2022-09-21 09:00:49 +02:00
private function getInvoices(string $invoice_hashed_ids)
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
2022-09-21 09:00:49 +02:00
if(count($invoices) >= 1)
{
foreach($invoices as $invoice){
2022-09-15 07:02:39 +02:00
2022-09-21 09:00:49 +02:00
if(is_string($invoice) && strlen($invoice) > 1)
$collection->push($this->decodePrimaryKey($invoice));
}
}
return $collection;
}
private function checkPayable($invoices) :bool
{
foreach($invoices as $invoice){
$invoice->service()->markSent();
2022-09-21 09:00:49 +02:00
if(!$invoice->isPayable())
return false;
}
return true;
}
2022-09-22 08:20:54 +02:00
private function matchInvoicePayment() :self
2022-09-21 09:00:49 +02:00
{
2022-09-22 08:20:54 +02:00
$this->bt = BankTransaction::find($this->input['id']);
2022-09-21 09:00:49 +02:00
2022-09-22 08:20:54 +02:00
$_invoices = Invoice::withTrashed()->find($this->getInvoices($this->input['invoice_ids']));
2022-09-21 07:43:35 +02:00
2022-09-15 07:02:39 +02:00
$amount = $this->bt->amount;
2022-09-21 09:00:49 +02:00
if($_invoices && $this->checkPayable($_invoices)){
2022-09-15 07:02:39 +02:00
2022-09-21 09:00:49 +02:00
$this->createPayment($_invoices, $amount);
2022-09-15 07:02:39 +02:00
}
2022-09-22 08:20:54 +02:00
return $this;
2022-09-15 07:02:39 +02:00
}
2022-09-22 08:20:54 +02:00
private function matchExpense() :self
2022-09-15 07:02:39 +02:00
{
2022-09-15 08:15:57 +02:00
//if there is a category id, pull it from Yodlee and insert - or just reuse!!
2022-09-22 08:20:54 +02:00
$this->bt = BankTransaction::find($this->input['id']);
2022-09-15 08:28:18 +02:00
2022-09-15 09:31:32 +02:00
$expense = ExpenseFactory::create($this->bt->company_id, $this->bt->user_id);
$expense->category_id = $this->resolveCategory();
$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);
$expense->public_notes = $this->bt->description;
$expense->transaction_id = $this->bt->id;
2022-09-15 09:31:32 +02:00
$expense->save();
2022-09-22 08:20:54 +02:00
return $this;
2022-09-15 07:02:39 +02:00
}
2022-09-21 09:00:49 +02: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;
\DB::connection(config('database.default'))->transaction(function () use($invoices) {
$invoices->each(function ($invoice) use ($invoices){
$this->invoice = Invoice::withTrashed()->where('id', $invoice->id)->lockForUpdate()->first();
if($invoices->count() == 1){
$_amount = $this->available_balance;
}
elseif($invoices->count() > 1 && floatval($this->invoice->balance) < floatval($this->available_balance) && $this->available_balance > 0)
{
$_amount = $this->invoice->balance;
$this->available_balance = $this->available_balance - $this->invoice->balance;
}
elseif($invoices->count() > 1 && floatval($this->invoice->balance) > floatval($this->available_balance) && $this->available_balance > 0)
{
$_amount = $this->available_balance;
$this->available_balance = 0;
}
$this->attachable_invoices[] = ['id' => $this->invoice->id, 'amount' => $_amount];
$this->invoice
->service()
->setExchangeRate()
->updateBalance($_amount * -1)
->updatePaidToDate($_amount)
->setCalculatedStatus()
->save();
});
2022-09-15 07:02:39 +02:00
}, 1);
/* Create Payment */
$payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id);
$payment->amount = $amount;
$payment->applied = $amount;
$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();
2022-09-15 07:02:39 +02:00
/* Bank Transfer! */
$payment_type_id = 1;
$payment->saveQuietly();
$payment->service()->applyNumber()->save();
if($payment->client->getSetting('send_email_on_mark_paid'))
$payment->service()->sendEmail();
$this->setExchangeRate($payment);
/* Create a payment relationship to the invoice entity */
2022-09-21 09:00:49 +02:00
foreach($this->attachable_invoices as $attachable_invoice)
{
$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
->service()
->applyNumber()
->touchPdf()
->save();
$payment->ledger()
2022-09-15 09:31:32 +02:00
->updatePaymentBalance($amount * -1);
2022-09-15 07:02:39 +02:00
$this->invoice
->client
->service()
->updateBalanceAndPaidToDate($amount*-1, $amount)
->save();
$this->invoice = $this->invoice
->service()
->workFlow()
->save();
/* 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)));
2022-09-21 07:43:35 +02:00
$this->bt->status_id = BankTransaction::STATUS_CONVERTED;
2022-09-15 07:02:39 +02:00
$this->bt->save();
}
2022-09-15 08:28:18 +02:00
private function resolveCategory() :?int
{
2022-09-22 08:20:54 +02:00
if(array_key_exists('ninja_category_id', $this->input))
return $this->input['ninja_category_id'];
2022-09-15 09:31:32 +02:00
$category = $this->categories->firstWhere('highLevelCategoryId', $this->bt->category_id);
$ec = ExpenseCategory::where('company_id', $this->bt->company_id)->where('bank_category_id', $this->bt->category_id)->first();
if($ec)
return $ec->id;
if($category)
{
$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;
}
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)];
}
}