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,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;
}
}