52 lines
1.4 KiB
PHP
52 lines
1.4 KiB
PHP
|
<?php
|
||
|
namespace Contabilidad;
|
||
|
|
||
|
use DateTimeInterface;
|
||
|
use DateInterval;
|
||
|
use Carbon\Carbon;
|
||
|
use Carbon\CarbonInterval;
|
||
|
use ProVM\Common\Alias\Model;
|
||
|
|
||
|
/**
|
||
|
* @property int $id
|
||
|
* @property Cuenta $cuenta_id
|
||
|
* @property DateTimeInterface $fecha
|
||
|
* @property DateInterval $periodo
|
||
|
* @property float $saldo
|
||
|
*/
|
||
|
class Consolidado extends Model {
|
||
|
public static $_table = 'consolidados';
|
||
|
|
||
|
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(DateTimeInterface $fecha = null) {
|
||
|
if ($fecha === null) {
|
||
|
return Carbon::parse($this->fecha);
|
||
|
}
|
||
|
if ($this->periodo()->days > 31) {
|
||
|
$this->fecha = $fecha->format('Y-1-1');
|
||
|
} else {
|
||
|
$this->fecha = $fecha->format('Y-m-1');
|
||
|
}
|
||
|
return $this;
|
||
|
}
|
||
|
public function periodo(DateInterval $periodo = null) {
|
||
|
if ($periodo === null) {
|
||
|
return new CarbonInterval($this->periodo);
|
||
|
}
|
||
|
$this->periodo = CarbonInterval::getDateIntervalSpec($periodo);
|
||
|
return $this;
|
||
|
}
|
||
|
public function saldo(DateTimeInterface $fecha = null) {
|
||
|
if ($fecha === null) {
|
||
|
$fecha = $this->fecha();
|
||
|
}
|
||
|
return $this->cuenta()->moneda()->cambiar($fecha, $this->saldo);
|
||
|
}
|
||
|
}
|