Simplificacion a contabilidad clasica

This commit is contained in:
2021-07-30 16:38:09 -04:00
parent 510b5aa46d
commit 69b823750a
18 changed files with 194 additions and 468 deletions

View File

@ -1,13 +0,0 @@
<?php
namespace Contabilidad;
use ProVM\Common\Alias\Model;
/**
* @property int $id
* @property string $nombre
*/
class Banco extends Model {
public static $_table = 'bancos';
protected static $fields = ['nombre'];
}

View File

@ -18,4 +18,24 @@ class Categoria extends Model {
}
return $this->cuentas;
}
protected $saldo;
public function saldo() {
if ($this->saldo === null) {
$this->saldo = 0;
if ($this->cuentas() !== null) {
$this->saldo = array_reduce($this->cuentas(), function($sum, $item) {
return $sum + $item->saldo();
});
}
}
return $this->saldo;
}
public function toArray(): array {
$arr = parent::toArray();
$arr['saldo'] = $this->saldo();
$arr['saldoFormateado'] = '$' . number_format($this->saldo(), 0, ',', '.');
return $arr;
}
}

View File

@ -28,9 +28,61 @@ class Cuenta extends Model {
return $this->entradas;
}
protected $cargos;
public function cargos() {
if ($this->cargos === null) {
$this->cargos = $this->parentOf(Transaccion::class, [Model::CHILD_KEY => 'hasta_id']);
}
return $this->cargos;
}
protected $abonos;
public function abonos() {
if ($this->abonos === null) {
$this->abonos = $this->parentOf(Transaccion::class, [Model::CHILD_KEY => 'desde_id']);
}
return $this->abonos;
}
protected $transacciones;
public function transacciones() {
if ($this->transacciones === null) {
$this->transacciones = [];
if ($this->abonos() !== null) {
$this->transacciones = array_merge($this->transacciones, $this->abonos());
}
if ($this->cargos() !== null) {
$this->transacciones = array_merge($this->transacciones, array_map(function($item) {
$item->valor = - $item->valor;
return $item;
}, $this->cargos()));
}
usort($this->transacciones, function($a, $b) {
$d = $a->fecha()->diffInDays($b->fecha(), false);
if ($d === 0) {
return $a->valor - $b->valor;
}
return $d;
});
}
return $this->transacciones;
}
protected $saldo;
public function saldo() {
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 $this->saldo;
}
public function toArray(): array {
$arr = parent::toArray();
$arr['categoria'] = $this->categoria()->toArray();
$arr['saldo'] = $this->saldo();
$arr['saldoFormateado'] = '$' . number_format($this->saldo(), 0, ',', '.');
return $arr;
}
}

View File

@ -1,49 +0,0 @@
<?php
namespace Contabilidad;
use Carbon\Carbon;
use ProVM\Common\Alias\Model;
/**
* @property int $id
* @property \DateTime $fecha
* @property Fuente $fuente_id
* @property string $glosa
* @property string $detalle
* @property Cuenta $cuenta_id
* @property double $valor
*/
class Entrada extends Model {
public static $_table = 'entradas';
protected static $fields = ['fecha', 'fuente_id', 'glosa', 'detalle', 'cuenta_id', 'valor'];
protected $fuente;
public function fuente() {
if ($this->fuente === null) {
$this->fuente = $this->childOf(Fuente::class, [Model::SELF_KEY => 'fuente_id']);
}
return $this->fuente;
}
protected $cuenta;
public function cuenta() {
if ($this->cuenta === null) {
$this->cuenta = $this->childOf(Cuenta::class, [Model::SELF_KEY => 'cuenta_id']);
}
return $this->cuenta;
}
public function fecha(\DateTime $fecha = null) {
if ($fecha === null) {
return Carbon::parse($this->fecha);
}
$this->fecha = $fecha->format('Y-m-d');
}
public function toArray(): array {
$arr = parent::toArray();
$arr['fuente'] = $this->fuente()->toArray();
$arr['cuenta'] = $this->cuenta()->toArray();
$arr['fechaFormateada'] = $this->fecha()->format('d-m-Y');
$arr['valorFormateado'] = '$' . number_format($this->valor, 0, ',', '.');
return $arr;
}
}

View File

@ -1,58 +0,0 @@
<?php
namespace Contabilidad;
use ProVM\Common\Alias\Model;
/**
* @property int $id
* @property TipoFuente $tipo_id
* @property Banco $banco_id
*/
class Fuente extends Model {
public static $_table = 'fuentes';
protected static $fields = ['tipo_id', 'banco_id'];
protected $tipo;
public function tipo() {
if ($this->tipo === null) {
$this->tipo = $this->childOf(TipoFuente::class, [Model::SELF_KEY => 'tipo_id']);
}
return $this->tipo;
}
protected $banco;
public function banco() {
if ($this->banco === null) {
$this->banco = $this->childOf(Banco::class, [Model::SELF_KEY => 'banco_id']);
}
return $this->banco;
}
protected $saldo;
public function saldo() {
if ($this->saldo === null) {
$this->saldo = 0;
if ($this->entradas() !== null) {
$this->saldo = array_reduce($this->entradas(), function($sum, $item) {
return $sum + $item->valor;
});
}
}
return $this->saldo;
}
protected $entradas;
public function entradas() {
if ($this->entradas === null) {
$this->entradas = $this->parentOf(Entrada::class, [Model::CHILD_KEY => 'fuente_id']);
}
return $this->entradas;
}
public function toArray(): array {
$arr = parent::toArray();
$arr['tipo'] = $this->tipo()->toArray();
$arr['banco'] = $this->banco()->toArray();
$arr['saldo'] = $this->saldo();
$arr['saldoFormateado'] = '$' . number_format($this->saldo(), 0, ',', '.');
return $arr;
}
}

View File

@ -1,21 +0,0 @@
<?php
namespace Contabilidad;
use ProVM\Common\Alias\Model;
/**
* @property int $id
* @property string $descripcion
*/
class TipoFuente extends Model {
public static $_table = 'tipos_fuente';
protected static $fields = ['descripcion'];
protected $fuentes;
public function fuentes() {
if ($this->fuentes === null) {
$this->fuentes = $this->parentOf(Fuente::class, [Model::CHILD_KEY => 'tipo_id']);
}
return $this->fuentes;
}
}

49
api/src/Transaccion.php Normal file
View File

@ -0,0 +1,49 @@
<?php
namespace Contabilidad;
use Carbon\Carbon;
use ProVM\Common\Alias\Model;
/**
* @property int $id
* @property Cuenta $desde_id
* @property Cuenta $hasta_id
* @property \DateTime $fecha
* @property string $glosa
* @property string $detalle
* @property double $valor
*/
class Transaccion extends Model {
public static $_table = 'transacciones';
protected static $fields = ['desde_id', 'hasta_id', 'fecha', 'glosa', 'detalle', 'valor'];
protected $desde;
public function desde() {
if ($this->desde === null) {
$this->desde = $this->childOf(Cuenta::class, [Model::SELF_KEY => 'desde_id']);
}
return $this->desde;
}
protected $hasta;
public function hasta() {
if ($this->hasta === null) {
$this->hasta = $this->childOf(Cuenta::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');
}
public function toArray(): array {
$arr = parent::toArray();
$arr['desde'] = $this->desde()->toArray();
$arr['hasta'] = $this->hasta()->toArray();
$arr['fechaFormateada'] = $this->fecha()->format('d-m-Y');
$arr['valorFormateado'] = '$' . number_format($this->valor, 0, ',', '.');
return $arr;
}
}