114 lines
3.3 KiB
PHP
114 lines
3.3 KiB
PHP
<?php
|
|
namespace Contabilidad;
|
|
|
|
use Carbon\Carbon;
|
|
use ProVM\Common\Alias\Model;
|
|
use Contabilidad\Common\Service\TiposCambios as Service;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property string $nombre
|
|
* @property TipoCategoria $tipo_id
|
|
*/
|
|
class Categoria extends Model {
|
|
public static $_table = 'categorias';
|
|
protected static $fields = ['nombre', 'tipo_id'];
|
|
|
|
protected $cuentas;
|
|
public function cuentas() {
|
|
if ($this->cuentas === null) {
|
|
$this->cuentas = $this->parentOf(Cuenta::class, [Model::CHILD_KEY => 'categoria_id']);
|
|
if ($this->cuentas !== null) {
|
|
usort($this->cuentas, function($a, $b) {
|
|
return strcmp($a->nombre, $b->nombre);
|
|
});
|
|
}
|
|
}
|
|
return $this->cuentas;
|
|
}
|
|
protected $tipo;
|
|
public function tipo() {
|
|
if ($this->tipo === null) {
|
|
$this->tipo = $this->childOf(TipoCategoria::class, [Model::SELF_KEY => 'tipo_id']);
|
|
}
|
|
return $this->tipo;
|
|
}
|
|
|
|
public function getCuentasOf($tipo) {
|
|
return $this->factory->find(Cuenta::class)
|
|
->select([['cuentas', '*']])
|
|
->join([
|
|
['tipos_cuenta', 'tipos_cuenta.id', 'cuentas.tipo_id']
|
|
])
|
|
->where([
|
|
['tipos_cuenta.descripcion', $tipo],
|
|
['cuentas.categoria_id', $this->id]
|
|
])
|
|
->many();
|
|
}
|
|
protected $cuentas_of;
|
|
public function getCuentas() {
|
|
if ($this->cuentas_of === null) {
|
|
$tipos = $this->factory->find(TipoCuenta::class)->many();
|
|
$cos = [];
|
|
foreach ($tipos as $tipo) {
|
|
$p = strtolower($tipo->descripcion) . 's';
|
|
$cos[$p] = [];
|
|
$cuentas = $this->getCuentasOf($tipos->descripcion);
|
|
if ($cuentas === null) {
|
|
continue;
|
|
}
|
|
$cos[$p] = $cuentas;
|
|
}
|
|
$this->cuentas_of = $cos;
|
|
}
|
|
return $this->cuentas_of;
|
|
}
|
|
protected $totales;
|
|
public function getTotales(Service $service) {
|
|
if ($this->totales === null) {
|
|
$tipos = $this->factory->find(TipoCuenta::class)->many();
|
|
$totals = [];
|
|
foreach ($tipos as $tipo) {
|
|
$p = strtolower($tipo->descripcion) . 's';
|
|
$totals[$p] = 0;
|
|
$cuentas = $this->getCuentasOf($tipo->descripcion);
|
|
if ($cuentas === null) {
|
|
continue;
|
|
}
|
|
$totals[$p] = array_reduce($cuentas, function($sum, $item) use ($service) {
|
|
return $sum + $item->saldo($service, true);
|
|
});
|
|
}
|
|
$this->totales = $totals;
|
|
}
|
|
return $this->totales;
|
|
}
|
|
|
|
protected $saldo;
|
|
public function saldo(Service $service = null) {
|
|
if ($this->saldo === null) {
|
|
$this->saldo = 0;
|
|
if ($this->cuentas() !== null) {
|
|
$sum = 0;
|
|
$debitos = ['Activo', 'Perdida'];
|
|
foreach ($this->cuentas() as $cuenta) {
|
|
if (array_search($cuenta->tipo()->descripcion, $debitos) !== false) {
|
|
$sum -= $cuenta->saldo($service, true);
|
|
continue;
|
|
}
|
|
$sum += $cuenta->saldo($service, true);
|
|
}
|
|
$this->saldo = $sum;
|
|
}
|
|
}
|
|
return $this->saldo;
|
|
}
|
|
|
|
public function toArray(): array {
|
|
$arr = parent::toArray();
|
|
$arr['tipo'] = $this->tipo()->toArray();
|
|
return $arr;
|
|
}
|
|
}
|