This commit is contained in:
2021-12-06 22:10:41 -03:00
parent 10b2485cfd
commit 8ef4ab1c7d
41 changed files with 1256 additions and 57 deletions

View File

@ -1,7 +1,9 @@
<?php
namespace Contabilidad;
use Carbon\Carbon;
use ProVM\Common\Alias\Model;
use Contabilidad\Common\Service\TiposCambios as Service;
/**
* @property int $id
@ -10,7 +12,7 @@ use ProVM\Common\Alias\Model;
*/
class Categoria extends Model {
public static $_table = 'categorias';
protected static $fields = ['nombre'];
protected static $fields = ['nombre', 'tipo_id'];
protected $cuentas;
public function cuentas() {
@ -27,24 +29,64 @@ class Categoria extends Model {
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 $activos;
public function activos() {
if ($this->activos === null) {
$this->activos = $this->getCuentasOf('Activo');
}
return $this->activos();
}
protected $pasivos;
public function pasivos() {
if ($this->pasivos === null) {
$this->activos = $this->getCuentasOf('Pasivo');
}
return $this->pasivos;
}
protected $ganancias;
public function ganancias() {
if ($this->ganancias === null) {
$this->ganancias = $this->getCuentasOf('Ganancia');
}
return $this->ganancias;
}
protected $perdidas;
public function perdidas() {
if ($this->perdidas === null) {
$this->perdidas = $this->getCuentasOf('Perdida');
}
return $this->perdidas;
}
protected $saldo;
public function saldo() {
public function saldo(Service $service = null) {
if ($this->saldo === null) {
$this->saldo = 0;
if ($this->cuentas() !== null) {
$this->saldo = array_reduce($this->cuentas(), function($sum, $item) {
return $sum + $item->saldo();
});
$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();
$arr['saldo'] = $this->saldo();
$arr['saldoFormateado'] = '$' . number_format($this->saldo(), 0, ',', '.');
return $arr;
}
}

View File

@ -1,17 +1,20 @@
<?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 Categoria $categoria_id
* @property TipoCuenta $tipo_id
* @property Moneda $moneda_id
*/
class Cuenta extends Model {
public static $_table = 'cuentas';
protected static $fields = ['nombre', 'categoria_id', 'tipo_id'];
protected static $fields = ['nombre', 'categoria_id', 'tipo_id', 'moneda_id'];
protected $categoria;
public function categoria() {
@ -20,25 +23,32 @@ class Cuenta extends Model {
}
return $this->categoria;
}
protected $cuenta;
public function cuenta() {
if ($this->cuenta === null) {
$this->cuenta = $this->childOf(TipoCuenta::class, [Model::SELF_KEY => 'tipo_id']);
protected $tipo;
public function tipo() {
if ($this->tipo === null) {
$this->tipo = $this->childOf(TipoCuenta::class, [Model::SELF_KEY => 'tipo_id']);
}
return $this->cuenta;
return $this->tipo;
}
protected $moneda;
public function moneda() {
if ($this->moneda === null) {
$this->moneda = $this->childOf(Moneda::class, [Model::SELF_KEY => 'moneda_id']);
}
return $this->moneda;
}
protected $cargos;
public function cargos() {
if ($this->cargos === null) {
$this->cargos = $this->parentOf(Transaccion::class, [Model::CHILD_KEY => 'hasta_id']);
$this->cargos = $this->parentOf(Transaccion::class, [Model::CHILD_KEY => 'credito_id']);
}
return $this->cargos;
}
protected $abonos;
public function abonos() {
if ($this->abonos === null) {
$this->abonos = $this->parentOf(Transaccion::class, [Model::CHILD_KEY => 'desde_id']);
$this->abonos = $this->parentOf(Transaccion::class, [Model::CHILD_KEY => 'debito_id']);
}
return $this->abonos;
}
@ -46,7 +56,7 @@ class Cuenta extends Model {
public function transacciones($limit = null, $start = 0) {
if ($this->transacciones === null) {
$transacciones = Model::factory(Transaccion::class)
->join('cuentas', 'cuentas.id = transacciones.desde_id OR cuentas.id = transacciones.hasta_id')
->join('cuentas', 'cuentas.id = transacciones.debito_id OR cuentas.id = transacciones.credito_id')
->whereEqual('cuentas.id', $this->id)
->orderByAsc('transacciones.fecha');
if ($limit !== null) {
@ -64,23 +74,40 @@ class Cuenta extends Model {
return $this->transacciones;
}
protected $saldo;
public function saldo() {
public function saldo(Service $service = null, $in_clp = false) {
if ($this->saldo === null) {
$this->saldo = 0;
if (count($this->transacciones()) > 0) {
$this->saldo = array_reduce($this->transacciones(), function($sum, $item) {
return $sum + $item->valor;
return $sum + $item->valor;
});
}
}
if ($in_clp and $this->moneda()->codigo !== 'CLP') {
$fecha = Carbon::today();
if ($this->moneda()->codigo == 'USD') {
$fecha = match ($fecha->weekday()) {
0 => $fecha->subWeek()->weekday(5),
6 => $fecha->weekday(5),
default => $fecha
};
}
$service->get($fecha->format('Y-m-d'), $this->moneda()->id);
return $this->moneda()->cambiar($fecha, $this->saldo);
}
return $this->saldo;
}
public function toArray(): array {
public function format($valor) {
return $this->moneda()->format($valor);
}
public function toArray(Service $service = null, $in_clp = false): array {
$arr = parent::toArray();
$arr['categoria'] = $this->categoria()->toArray();
$arr['saldo'] = $this->saldo();
$arr['saldoFormateado'] = '$' . number_format($this->saldo(), 0, ',', '.');
$arr['tipo'] = $this->tipo()->toArray();
$arr['moneda'] = $this->moneda()->toArray();
$arr['saldo'] = $this->saldo($service, $in_clp);
$arr['saldoFormateado'] = $this->format($this->saldo($service, $in_clp));
return $arr;
}
}

46
api/src/Moneda.php Normal file
View File

@ -0,0 +1,46 @@
<?php
namespace Contabilidad;
use ProVM\Common\Alias\Model;
/**
* @property int $id
* @property string $denominacion
* @property string $codigo
* @property string $sufijo
* @property string $prefijo
* @property int $decimales
*/
class Moneda extends Model {
public static $_table = 'monedas';
protected static $fields = ['denominacion', 'codigo'];
public function format($valor) {
return implode('', [
$this->prefijo,
number_format($valor, $this->decimales, ',', '.'),
$this->sufijo
]);
}
public function cambio(\DateTime $fecha) {
$cambio = $this->factory->find(TipoCambio::class)
->where([['desde_id', $this->id], ['hasta_id', 1], ['fecha', $fecha->format('Y-m-d H:i:s')]])
->one();
if (!$cambio) {
$cambio = $this->factory->find(TipoCambio::class)
->where([['hasta_id', $this->id], ['desde_id', 1], ['fecha', $fecha->format('Y-m-d H:i:s')]])
->one();
}
return $cambio;
}
public function cambiar(\DateTime $fecha, float $valor) {
$cambio = $this->cambio($fecha);
if (!$cambio) {
return $valor;
}
if ($cambio->desde()->id != $this->id) {
return $cambio->transform($valor, TipoCambio::DESDE);
}
return $cambio->transform($valor);
}
}

50
api/src/TipoCambio.php Normal file
View File

@ -0,0 +1,50 @@
<?php
namespace Contabilidad;
use Carbon\Carbon;
use DateTime;
use ProVM\Common\Alias\Model;
/**
* @property int $id
* @property DateTime $fecha
* @property Moneda $desde_id
* @property Moneda $hasta_id
* @property float $valor
*/
class TipoCambio extends Model {
const DESDE = -1;
const HASTA = 1;
public static $_table = 'tipos_cambio';
protected static $fields = ['fecha', 'valor', 'desde_id', 'hasta_id'];
protected $desde;
public function desde() {
if ($this->desde === null) {
$this->desde = $this->childOf(Moneda::class, [Model::SELF_KEY => 'desde_id']);
}
return $this->desde;
}
protected $hasta;
public function hasta() {
if ($this->hasta === null) {
$this->hasta = $this->childOf(Moneda::class, [Model::SELF_KEY => 'hasta_id']);
}
return $this->hasta;
}
public function fecha(DateTime $fecha = null) {
if ($fecha === null) {
return Carbon::parse($this->fecha);
}
$this->fecha = $fecha->format('Y-m-d H:i:s');
return $this;
}
public function transform(float $valor, int $direction = TipoCambio::HASTA): float {
if ($direction == TipoCambio::HASTA) {
return $valor * $this->valor;
}
return $valor / $this->valor;
}
}

View File

@ -2,6 +2,7 @@
namespace Contabilidad;
use ProVM\Common\Alias\Model;
use Contabilidad\Common\Service\TiposCambios as Service;
/**
* @property int $id
@ -11,4 +12,35 @@ use ProVM\Common\Alias\Model;
class TipoCategoria extends Model {
public static $_table = 'tipos_categoria';
protected static $fields = ['descripcion', 'activo'];
protected $categorias;
public function categorias() {
if ($this->categorias === null) {
$this->categorias = $this->parentOf(Categoria::class, [Model::CHILD_KEY => 'tipo_id']);
}
return $this->categorias;
}
public function getCuentasOf($tipo) {
return $this->factory->find(Cuenta::class)
->select([['cuentas', '*']])
->join([
['tipos_cuenta', 'tipos_cuenta.id', 'cuentas.tipo_id'],
['categorias', 'categorias.id', 'cuentas.categoria_id']
])
->where([
['tipos_cuenta.descripcion', $tipo],
['categorias.tipo_id', $this->id]
])->many();
}
protected $saldo;
public function saldo(Service $service = null) {
if ($this->saldo === null) {
$this->saldo = array_reduce($this->categorias(), function($sum, $item) use ($service) {
return $sum + $item->saldo($service);
});
}
return $this->saldo;
}
}

View File

@ -6,8 +6,9 @@ use ProVM\Common\Alias\Model;
/**
* @property int $id
* @property string $descripcion
* @property string $color
*/
class TipoCuenta extends Model {
public static $_table = 'tipos_cuenta';
protected static $fields = ['descripcion'];
}
protected static $fields = ['descripcion', 'color'];
}

View File

@ -1,6 +1,7 @@
<?php
namespace Contabilidad;
use DateTime;
use Carbon\Carbon;
use ProVM\Common\Alias\Model;
@ -8,7 +9,7 @@ use ProVM\Common\Alias\Model;
* @property int $id
* @property Cuenta $debito_id
* @property Cuenta $credito_id
* @property \DateTime $fecha
* @property DateTime $fecha
* @property string $glosa
* @property string $detalle
* @property double $valor
@ -31,7 +32,7 @@ class Transaccion extends Model {
}
return $this->credito;
}
public function fecha(\DateTime $fecha = null) {
public function fecha(DateTime $fecha = null) {
if ($fecha === null) {
return Carbon::parse($this->fecha);
}
@ -43,7 +44,7 @@ class Transaccion extends Model {
$arr['debito'] = $this->debito()->toArray();
$arr['credito'] = $this->credito()->toArray();
$arr['fechaFormateada'] = $this->fecha()->format('d-m-Y');
$arr['valorFormateado'] = '$' . number_format($this->valor, 0, ',', '.');
$arr['valorFormateado'] = $this->debito()->moneda()->format($this->valor);
return $arr;
}
}