68 lines
2.1 KiB
PHP
68 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace Modules\Accounting\Services;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
class PaymentAccountGlLinkService
|
|
{
|
|
public function linkByNameMatch(int $businessId, bool $onlyUnlinked = true): int
|
|
{
|
|
if (! Schema::hasColumn('accounts', 'accounting_account_id')) {
|
|
return 0;
|
|
}
|
|
|
|
$query = DB::table('accounts')->where('business_id', $businessId);
|
|
if ($onlyUnlinked) {
|
|
$query->whereNull('accounting_account_id');
|
|
}
|
|
|
|
$linked = 0;
|
|
foreach ($query->get(['id', 'name', 'business_id']) as $paymentAccount) {
|
|
$gl = DB::table('accounting_accounts')
|
|
->where('business_id', $paymentAccount->business_id)
|
|
->where('name', $paymentAccount->name)
|
|
->where('status', 'active')
|
|
->first(['id']);
|
|
|
|
if (! $gl) {
|
|
$gl = DB::table('accounting_accounts')
|
|
->where('business_id', $paymentAccount->business_id)
|
|
->where('gl_code', 'like', '%'.substr(preg_replace('/\s+/', '', $paymentAccount->name), 0, 8).'%')
|
|
->where('status', 'active')
|
|
->first(['id']);
|
|
}
|
|
|
|
if ($gl) {
|
|
DB::table('accounts')->where('id', $paymentAccount->id)->update([
|
|
'accounting_account_id' => $gl->id,
|
|
]);
|
|
$linked++;
|
|
}
|
|
}
|
|
|
|
return $linked;
|
|
}
|
|
|
|
public function summary(int $businessId): array
|
|
{
|
|
if (! Schema::hasColumn('accounts', 'accounting_account_id')) {
|
|
return ['total' => 0, 'linked' => 0, 'unlinked' => 0];
|
|
}
|
|
|
|
$total = DB::table('accounts')->where('business_id', $businessId)->where('is_closed', 0)->count();
|
|
$linked = DB::table('accounts')
|
|
->where('business_id', $businessId)
|
|
->where('is_closed', 0)
|
|
->whereNotNull('accounting_account_id')
|
|
->count();
|
|
|
|
return [
|
|
'total' => $total,
|
|
'linked' => $linked,
|
|
'unlinked' => max(0, $total - $linked),
|
|
];
|
|
}
|
|
}
|