1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 00:41:34 +02:00
invoiceninja/app/Services/Bank/BankService.php

85 lines
2.2 KiB
PHP
Raw Normal View History

2022-08-08 11:07:35 +02:00
<?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;
2022-08-12 05:41:55 +02:00
use App\Libraries\MultiDB;
2022-08-12 07:25:18 +02:00
use App\Models\BankTransaction;
2022-08-08 11:07:35 +02:00
use App\Models\Company;
2022-09-07 13:27:53 +02:00
use App\Models\Invoice;
2022-08-12 05:41:55 +02:00
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
2022-08-08 11:07:35 +02:00
2022-08-12 05:41:55 +02:00
class BankService implements ShouldQueue
2022-08-08 11:07:35 +02:00
{
2022-08-12 05:41:55 +02:00
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
2022-08-08 11:07:35 +02:00
2022-08-12 05:41:55 +02:00
private $company_id;
private Company $company;
private $db;
2022-08-08 11:07:35 +02:00
private $invoices;
2022-09-21 09:00:49 +02:00
public $deleteWhenMissingModels = true;
2022-08-12 05:41:55 +02:00
public function __construct($company_id, $db)
2022-08-08 11:07:35 +02:00
{
2022-08-12 05:41:55 +02:00
$this->company_id = $company_id;
$this->db = $db;
}
public function handle()
{
MultiDB::setDb($this->db);
$this->company = Company::find($this->company_id);
2022-08-08 11:07:35 +02:00
2022-09-07 13:27:53 +02:00
$this->invoices = Invoice::where('company_id', $this->company->id)
->whereIn('status_id', [1,2,3])
->where('is_deleted', 0)
->get();
2022-08-08 11:07:35 +02:00
2022-08-12 06:23:23 +02:00
$this->match();
2022-08-08 11:07:35 +02:00
}
2022-08-12 06:23:23 +02:00
private function match()
2022-08-08 11:07:35 +02:00
{
2022-08-18 01:27:28 +02:00
2022-08-12 06:23:23 +02:00
BankTransaction::where('company_id', $this->company->id)
2022-09-21 09:00:49 +02:00
->where('status_id', BankTransaction::STATUS_UNMATCHED)
2022-08-12 06:23:23 +02:00
->cursor()
->each(function ($bt){
$invoice = $this->invoices->first(function ($value, $key) use ($bt){
2022-08-18 01:27:28 +02:00
return str_contains($bt->description, $value->number);
2022-08-12 06:23:23 +02:00
});
if($invoice)
{
2022-09-21 09:00:49 +02:00
$bt->invoice_ids = $invoice->hashed_id;
$bt->status_id = BankTransaction::STATUS_MATCHED;
2022-08-12 06:23:23 +02:00
$bt->save();
}
});
}
2022-08-08 11:07:35 +02:00
}