ultimatepos/Modules/Accounting/Console/AccountingSyncExchangeRatesCommand.php

45 lines
1.6 KiB
PHP

<?php
namespace Modules\Accounting\Console;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Modules\Accounting\Services\ExchangeRateService;
class AccountingSyncExchangeRatesCommand extends Command
{
protected $signature = 'accounting:sync-exchange-rates
{--business_id= : Limit to one business}
{--date= : Rate date (Y-m-d), defaults to today}
{--no-entities : Skip pushing rates to holding entity meta}';
protected $description = 'Sync accounting exchange rates and push them to holding entities';
public function handle(ExchangeRateService $exchangeRateService): int
{
$businessId = $this->option('business_id');
$date = $this->option('date') ?: now()->toDateString();
$pushEntities = ! $this->option('no-entities');
$query = DB::table('business');
if ($businessId) {
$query->where('id', $businessId);
}
$totalUpserted = 0;
$totalEntities = 0;
foreach ($query->pluck('id') as $id) {
$result = $exchangeRateService->syncForBusiness((int) $id, $date, $pushEntities);
$totalUpserted += $result['upserted'];
$totalEntities += $result['entities_updated'];
$this->line("Business #{$id}: upserted={$result['upserted']} api={$result['api_fetched']} entities_updated={$result['entities_updated']}");
}
$this->info("Done. upserted={$totalUpserted} entities_updated={$totalEntities}");
return self::SUCCESS;
}
}