Consolidar command
This commit is contained in:
@ -13,7 +13,7 @@ class Consolidar {
|
||||
}
|
||||
public function __invoke(Request $request, Handler $handler): Response {
|
||||
if (!$this->service->isConsolidado()) {
|
||||
$this->service->consolidar();
|
||||
$this->service->queue();
|
||||
}
|
||||
return $handler->handle($request);
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
namespace Contabilidad\Common\Service;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Contabilidad\Queue;
|
||||
use \Model;
|
||||
use ProVM\Common\Factory\Model as ModelFactory;
|
||||
use Contabilidad\Consolidado;
|
||||
@ -21,24 +22,30 @@ class Consolidar {
|
||||
return $this->cuentas;
|
||||
}
|
||||
|
||||
public function isConsolidado() {
|
||||
$consolidado = true;
|
||||
public function isConsolidado(): bool {
|
||||
$cuentas = $this->getCuentas();
|
||||
if ($cuentas === null) {
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
foreach ($cuentas as $cuenta) {
|
||||
$transacciones = $cuenta->hasTransacciones();
|
||||
if (!$transacciones) {
|
||||
continue;
|
||||
}
|
||||
$consolidados = $cuenta->hasConsolidados();
|
||||
if (!$consolidados and $transacciones) {
|
||||
$consolidado = false;
|
||||
break;
|
||||
$pendientes = $cuenta->hasConsolidadosPending();
|
||||
if ($pendientes) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return $consolidado;
|
||||
return true;
|
||||
}
|
||||
public function queue() {
|
||||
$data = [
|
||||
'command' => 'consolidar',
|
||||
'created' => Carbon::now()->format('Y-m-d H:i:s')
|
||||
];
|
||||
$queue = Queue::add($this->factory, $data);
|
||||
$queue->save();
|
||||
}
|
||||
public function consolidar() {
|
||||
ini_set('max_execution_time', 60*5);
|
||||
|
4
api/setup/middlewares/03_consolidar.php
Normal file
4
api/setup/middlewares/03_consolidar.php
Normal file
@ -0,0 +1,4 @@
|
||||
<?php
|
||||
use Contabilidad\Common\Middleware\Consolidar;
|
||||
|
||||
$app->add(new Consolidar($app->getContainer()->get(\Contabilidad\Common\Service\Consolidar::class)));
|
@ -60,10 +60,19 @@ class Cuenta extends Model {
|
||||
return $this->consolidados;
|
||||
}
|
||||
public function hasConsolidados(): bool {
|
||||
$t = Carbon::now();
|
||||
return (bool) Model::factory(Consolidado::class)
|
||||
->whereEqual('cuenta_id', $this->id)
|
||||
->count('id');
|
||||
}
|
||||
public function hasConsolidadosPending(): bool {
|
||||
$t = Carbon::now();
|
||||
return !(bool) Model::factory(Consolidado::class)
|
||||
->whereEqual('cuenta_id', $this->id)
|
||||
->whereGte('fecha', $t->copy()->subMonthNoOverflow()->startOfMonth()->format('Y-m-d'))
|
||||
->orderByDesc('fecha')
|
||||
->count('id');
|
||||
}
|
||||
public function hasTransacciones(): bool {
|
||||
return (bool) Model::factory(Transaccion::class)
|
||||
->select('transacciones.*')
|
||||
|
@ -22,16 +22,16 @@ class Consolidar extends Command {
|
||||
}
|
||||
|
||||
public function execute(InputInterface $input, OutputInterface $output) {
|
||||
error_log('Starting Consolidar');
|
||||
try {
|
||||
$response = $this->getClient()->get('/consolidar');
|
||||
if ($response->getStatusCode() === 200) {
|
||||
return 0;
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
return $response->getStatusCode();
|
||||
error_log($response->getReasonPhrase());
|
||||
return Command::FAILURE;
|
||||
} catch (\Exception $e) {
|
||||
error_log($e);
|
||||
return $e->getCode();
|
||||
return Command::FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user