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

82 lines
2.1 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-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-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
$this->invoices = $this->company->invoices()->whereIn('status_id', [1,2,3])
->where('is_deleted', 0)
->get();
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)
->where('is_matched', false)
2022-08-12 07:25:18 +02:00
->where('provisional_match', false)
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)
{
$bt->invoice_id = $invoice->id;
2022-08-12 07:25:18 +02:00
$bt->provisional_match = true;
2022-08-12 06:23:23 +02:00
$bt->save();
}
});
}
2022-08-08 11:07:35 +02:00
}