143 lines
3.7 KiB
PHP
143 lines
3.7 KiB
PHP
<?php
|
|
namespace Incoviba\old\Proyecto;
|
|
|
|
use Carbon\Carbon;
|
|
use Incoviba\Common\Alias\OldModel as Model;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property Proyecto $proyecto
|
|
* @property TipoMonedaPagare $moneda
|
|
* @property double $capital
|
|
* @property double $tasa
|
|
* @property \DateTime $fecha
|
|
* @property int $duracion
|
|
* @property double $uf
|
|
* @property int $abonado
|
|
* @property int $estado_pago
|
|
*/
|
|
class Pagare extends Model
|
|
{
|
|
public function proyecto()
|
|
{
|
|
return $this->belongsTo(Proyecto::class, 'proyecto')->findOne();
|
|
}
|
|
public function moneda()
|
|
{
|
|
return $this->belongsTo(TipoMonedaPagare::class, 'moneda')->findOne();
|
|
}
|
|
public function renovaciones()
|
|
{
|
|
return $this->hasMany(RenovacionPagare::class, 'pagare')->findMany();
|
|
}
|
|
public function fecha(\DateTime $fecha = null)
|
|
{
|
|
if ($fecha == null) {
|
|
return Carbon::parse($this->fecha, config('app.timezone'));
|
|
}
|
|
$this->fecha = $fecha->format('Y-m-d');
|
|
}
|
|
public function fechaBanco(\DateTime $fecha = null)
|
|
{
|
|
if ($fecha == null) {
|
|
return Carbon::parse($this->fecha_banco, config('app.timezone'));
|
|
}
|
|
$this->fecha_banco = $fecha->format('Y-m-d');
|
|
}
|
|
public function vencimiento()
|
|
{
|
|
return $this->fechaBanco()->addDays($this->duracion);
|
|
}
|
|
public function uf()
|
|
{
|
|
if ($this->uf == 0 and $this->fecha != '0000-00-00') {
|
|
$uf = uf($this->fecha());
|
|
if ($uf->total > 0) {
|
|
$this->uf = $uf->uf->value;
|
|
$this->save();
|
|
}
|
|
}
|
|
return $this->uf;
|
|
}
|
|
protected $valores;
|
|
public function valor($tipo = 'ufs')
|
|
{
|
|
if ($this->valores == null) {
|
|
$valores = [];
|
|
switch (strtolower($this->moneda()->descripcion)) {
|
|
case 'uf':
|
|
$valores = (object) ['uf' => $this->capital, 'pesos' => $this->capital * $this->uf()];
|
|
break;
|
|
case 'pesos':
|
|
$valores = (object) ['uf' => $this->capital / $this->uf(), 'pesos' => $this->capital];
|
|
break;
|
|
}
|
|
$this->valores = $valores;
|
|
}
|
|
switch (strtolower($tipo)) {
|
|
case 'uf':
|
|
case 'ufs':
|
|
return $this->valores->uf;
|
|
case 'peso':
|
|
case 'pesos':
|
|
case '$':
|
|
return $this->valores->pesos;
|
|
}
|
|
}
|
|
public function abonado($tipo = 'ufs')
|
|
{
|
|
if ($tipo == 'ufs') {
|
|
if ($this->uf() > 0) {
|
|
return $this->abonado / $this->uf();
|
|
}
|
|
return 0;
|
|
}
|
|
return $this->abonado;
|
|
}
|
|
public function edit(array $data)
|
|
{
|
|
foreach ($data as $column => $value) {
|
|
if (isset($this->$column) and $this->$column != $value) {
|
|
$this->$column = $value;
|
|
}
|
|
}
|
|
$this->save();
|
|
}
|
|
protected $intereses;
|
|
public function intereses($tipo = 'ufs') {
|
|
if ($this->intereses == null) {
|
|
$this->intereses = (object) ['uf' => 0, 'pesos' => 0];
|
|
if ($this->renovaciones()) {
|
|
$arr = $this->renovaciones();
|
|
$this->intereses->uf = array_reduce($arr, function($accum, $item) {
|
|
return $accum + $item->intereses();
|
|
});
|
|
$this->intereses->pesos = array_reduce($arr, function($accum, $item) {
|
|
return $accum + $item->intereses('pesos');
|
|
});
|
|
}
|
|
}
|
|
switch (strtolower($tipo)) {
|
|
case 'uf':
|
|
case 'ufs':
|
|
return $this->intereses->uf;
|
|
case 'peso':
|
|
case 'pesos':
|
|
case '$':
|
|
return $this->intereses->pesos;
|
|
}
|
|
if ($fecha == null) {
|
|
$fecha = $this->vencimiento();
|
|
}
|
|
$dif = $fecha->diffInDays($this->fecha());
|
|
$duracion = ($dif > $this->duracion) ? $this->duracion : $dif;
|
|
return $this->valor() * ($this->tasa / 365 * $duracion) / 100;
|
|
}
|
|
public function estado_pago() {
|
|
if ($this->estado_pago == 0) {
|
|
return false;
|
|
}
|
|
return $this->belongsTo(AvanceConstruccion::class, 'estado_pago', 'numero')->findOne();
|
|
}
|
|
}
|