mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-31 15:24:36 -04:00
Add Bank Services class
This commit is contained in:
parent
569fa064e3
commit
dacde3358a
@ -24,7 +24,7 @@ use App\Http\Requests\BankIntegration\UpdateBankIntegrationRequest;
|
|||||||
use App\Jobs\Bank\ProcessBankTransactions;
|
use App\Jobs\Bank\ProcessBankTransactions;
|
||||||
use App\Models\BankIntegration;
|
use App\Models\BankIntegration;
|
||||||
use App\Repositories\BankIntegrationRepository;
|
use App\Repositories\BankIntegrationRepository;
|
||||||
use App\Services\Bank\BankService;
|
use App\Services\Bank\BankMatchingService;
|
||||||
use App\Transformers\BankIntegrationTransformer;
|
use App\Transformers\BankIntegrationTransformer;
|
||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
@ -27,7 +27,7 @@ use App\Http\Requests\Import\PreImportRequest;
|
|||||||
use App\Jobs\Bank\MatchBankTransactions;
|
use App\Jobs\Bank\MatchBankTransactions;
|
||||||
use App\Models\BankTransaction;
|
use App\Models\BankTransaction;
|
||||||
use App\Repositories\BankTransactionRepository;
|
use App\Repositories\BankTransactionRepository;
|
||||||
use App\Services\Bank\BankService;
|
use App\Services\Bank\BankMatchingService;
|
||||||
use App\Transformers\BankTransactionTransformer;
|
use App\Transformers\BankTransactionTransformer;
|
||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
@ -26,7 +26,7 @@ use App\Models\Currency;
|
|||||||
use App\Models\ExpenseCategory;
|
use App\Models\ExpenseCategory;
|
||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
use App\Models\Payment;
|
use App\Models\Payment;
|
||||||
use App\Services\Bank\BankService;
|
use App\Services\Bank\BankMatchingService;
|
||||||
use App\Utils\Ninja;
|
use App\Utils\Ninja;
|
||||||
use App\Utils\Traits\GeneratesCounter;
|
use App\Utils\Traits\GeneratesCounter;
|
||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
|
@ -16,7 +16,7 @@ use App\Libraries\MultiDB;
|
|||||||
use App\Models\BankIntegration;
|
use App\Models\BankIntegration;
|
||||||
use App\Models\BankTransaction;
|
use App\Models\BankTransaction;
|
||||||
use App\Models\Company;
|
use App\Models\Company;
|
||||||
use App\Services\Bank\BankService;
|
use App\Services\Bank\BankMatchingService;
|
||||||
use Illuminate\Bus\Queueable;
|
use Illuminate\Bus\Queueable;
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
use Illuminate\Foundation\Bus\Dispatchable;
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
@ -79,7 +79,7 @@ class ProcessBankTransactions implements ShouldQueue
|
|||||||
}
|
}
|
||||||
while($this->stop_loop);
|
while($this->stop_loop);
|
||||||
|
|
||||||
BankService::dispatch($this->company->id, $this->company->db);
|
BankMatchingService::dispatch($this->company->id, $this->company->db);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@ namespace App\Models;
|
|||||||
|
|
||||||
use App\Models\Filterable;
|
use App\Models\Filterable;
|
||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
|
use App\Services\Bank\BankService;
|
||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
@ -98,22 +99,9 @@ class BankTransaction extends BaseModel
|
|||||||
return $this->belongsTo(Account::class)->withTrashed();
|
return $this->belongsTo(Account::class)->withTrashed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function service() :BankService
|
||||||
public function matchInvoiceNumber()
|
|
||||||
{
|
{
|
||||||
|
return new BankService($this);
|
||||||
if(strlen($this->description) > 1)
|
|
||||||
{
|
|
||||||
|
|
||||||
$i = Invoice::where('company_id', $this->company_id)
|
|
||||||
->whereIn('status_id', [1,2,3])
|
|
||||||
->where('is_deleted', 0)
|
|
||||||
->where('number', 'LIKE', '%'.$this->description.'%')
|
|
||||||
->first();
|
|
||||||
|
|
||||||
return $i ?: false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -31,7 +31,7 @@ class BankTransactionRepository extends BaseRepository
|
|||||||
|
|
||||||
$bank_transaction->save();
|
$bank_transaction->save();
|
||||||
|
|
||||||
if($bank_transaction->base_type == 'CREDIT' && $invoice = $bank_transaction->matchInvoiceNumber())
|
if($bank_transaction->base_type == 'CREDIT' && $invoice = $bank_transaction->service()->matchInvoiceNumber())
|
||||||
{
|
{
|
||||||
$bank_transaction->invoice_ids = $invoice->hashed_id;
|
$bank_transaction->invoice_ids = $invoice->hashed_id;
|
||||||
$bank_transaction->status_id = BankTransaction::STATUS_MATCHED;
|
$bank_transaction->status_id = BankTransaction::STATUS_MATCHED;
|
||||||
|
84
app/Services/Bank/BankMatchingService.php
Normal file
84
app/Services/Bank/BankMatchingService.php
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Invoice Ninja (https://invoiceninja.com).
|
||||||
|
*
|
||||||
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
||||||
|
*
|
||||||
|
* @license https://www.elastic.co/licensing/elastic-license
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Services\Bank;
|
||||||
|
|
||||||
|
use App\Libraries\MultiDB;
|
||||||
|
use App\Models\BankTransaction;
|
||||||
|
use App\Models\Company;
|
||||||
|
use App\Models\Invoice;
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
|
||||||
|
class BankMatchingService implements ShouldQueue
|
||||||
|
{
|
||||||
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
|
private $company_id;
|
||||||
|
|
||||||
|
private Company $company;
|
||||||
|
|
||||||
|
private $db;
|
||||||
|
|
||||||
|
private $invoices;
|
||||||
|
|
||||||
|
public $deleteWhenMissingModels = true;
|
||||||
|
|
||||||
|
public function __construct($company_id, $db)
|
||||||
|
{
|
||||||
|
$this->company_id = $company_id;
|
||||||
|
$this->db = $db;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
|
||||||
|
MultiDB::setDb($this->db);
|
||||||
|
|
||||||
|
$this->company = Company::find($this->company_id);
|
||||||
|
|
||||||
|
$this->invoices = Invoice::where('company_id', $this->company->id)
|
||||||
|
->whereIn('status_id', [1,2,3])
|
||||||
|
->where('is_deleted', 0)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$this->match();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function match()
|
||||||
|
{
|
||||||
|
|
||||||
|
BankTransaction::where('company_id', $this->company->id)
|
||||||
|
->where('status_id', BankTransaction::STATUS_UNMATCHED)
|
||||||
|
->cursor()
|
||||||
|
->each(function ($bt){
|
||||||
|
|
||||||
|
$invoice = $this->invoices->first(function ($value, $key) use ($bt){
|
||||||
|
|
||||||
|
return str_contains($bt->description, $value->number);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
if($invoice)
|
||||||
|
{
|
||||||
|
$bt->invoice_ids = $invoice->hashed_id;
|
||||||
|
$bt->status_id = BankTransaction::STATUS_MATCHED;
|
||||||
|
$bt->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -11,74 +11,40 @@
|
|||||||
|
|
||||||
namespace App\Services\Bank;
|
namespace App\Services\Bank;
|
||||||
|
|
||||||
use App\Libraries\MultiDB;
|
|
||||||
use App\Models\BankTransaction;
|
use App\Models\BankTransaction;
|
||||||
use App\Models\Company;
|
|
||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
use Illuminate\Bus\Queueable;
|
use App\Services\Bank\ProcessBankRule;
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
||||||
use Illuminate\Foundation\Bus\Dispatchable;
|
|
||||||
use Illuminate\Queue\InteractsWithQueue;
|
|
||||||
use Illuminate\Queue\SerializesModels;
|
|
||||||
|
|
||||||
class BankService implements ShouldQueue
|
class BankService
|
||||||
{
|
{
|
||||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
||||||
|
|
||||||
private $company_id;
|
public function __construct(public BankTransaction $bank_transaction) {}
|
||||||
|
|
||||||
private Company $company;
|
|
||||||
|
|
||||||
private $db;
|
public function matchInvoiceNumber()
|
||||||
|
|
||||||
private $invoices;
|
|
||||||
|
|
||||||
public $deleteWhenMissingModels = true;
|
|
||||||
|
|
||||||
public function __construct($company_id, $db)
|
|
||||||
{
|
|
||||||
$this->company_id = $company_id;
|
|
||||||
$this->db = $db;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function handle()
|
|
||||||
{
|
{
|
||||||
|
|
||||||
MultiDB::setDb($this->db);
|
if(strlen($this->bank_transaction->description) > 1)
|
||||||
|
{
|
||||||
|
|
||||||
$this->company = Company::find($this->company_id);
|
$i = Invoice::where('company_id', $this->bank_transaction->company_id)
|
||||||
|
->whereIn('status_id', [1,2,3])
|
||||||
|
->where('is_deleted', 0)
|
||||||
|
->where('number', 'LIKE', '%'.$this->bank_transaction->description.'%')
|
||||||
|
->first();
|
||||||
|
|
||||||
$this->invoices = Invoice::where('company_id', $this->company->id)
|
return $i ?: false;
|
||||||
->whereIn('status_id', [1,2,3])
|
}
|
||||||
->where('is_deleted', 0)
|
|
||||||
->get();
|
return false;
|
||||||
|
|
||||||
$this->match();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function match()
|
public function processRule($rule)
|
||||||
{
|
{
|
||||||
|
(new ProcessBankRule($this->bank_transaction, $rule))->run();
|
||||||
|
|
||||||
BankTransaction::where('company_id', $this->company->id)
|
return $this;
|
||||||
->where('status_id', BankTransaction::STATUS_UNMATCHED)
|
|
||||||
->cursor()
|
|
||||||
->each(function ($bt){
|
|
||||||
|
|
||||||
$invoice = $this->invoices->first(function ($value, $key) use ($bt){
|
|
||||||
|
|
||||||
return str_contains($bt->description, $value->number);
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
if($invoice)
|
|
||||||
{
|
|
||||||
$bt->invoice_ids = $invoice->hashed_id;
|
|
||||||
$bt->status_id = BankTransaction::STATUS_MATCHED;
|
|
||||||
$bt->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
|
27
app/Services/Bank/ProcessBankRule.php
Normal file
27
app/Services/Bank/ProcessBankRule.php
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Invoice Ninja (https://invoiceninja.com).
|
||||||
|
*
|
||||||
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
||||||
|
*
|
||||||
|
* @license https://www.elastic.co/licensing/elastic-license
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Services\Bank;
|
||||||
|
|
||||||
|
use App\Models\BankTransaction;
|
||||||
|
use App\Services\AbstractService;
|
||||||
|
|
||||||
|
class ProcessBankRule extends AbstractService
|
||||||
|
{
|
||||||
|
|
||||||
|
public function __construct(private BankTransaction $bank_transaction, $rule){}
|
||||||
|
|
||||||
|
public function run() : void
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user