60 lines
1.8 KiB
PHP
60 lines
1.8 KiB
PHP
<?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 trim(implode('', [
|
|
$this->prefijo,
|
|
number_format($valor, $this->decimales, ',', '.'),
|
|
$this->sufijo
|
|
]));
|
|
}
|
|
public function cambio(\DateTime $fecha, Moneda $moneda = null) {
|
|
if ($moneda === null) {
|
|
$moneda = $this->factory->find(Moneda::class)->one(1);
|
|
}
|
|
$cambio = $this->factory->find(TipoCambio::class)
|
|
->where([['desde_id', $this->id], ['hasta_id', $moneda->id], ['fecha', $fecha->format('Y-m-d H:i:s')]])
|
|
->one();
|
|
if ($cambio === null) {
|
|
$cambio = $this->factory->find(TipoCambio::class)
|
|
->where([['hasta_id', $this->id], ['desde_id', $moneda->id], ['fecha', $fecha->format('Y-m-d H:i:s')]])
|
|
->one();
|
|
}
|
|
return $cambio;
|
|
}
|
|
public function cambiar(\DateTime $fecha, float $valor, Moneda $moneda = null) {
|
|
$cambio = $this->cambio($fecha, $moneda);
|
|
if (!$cambio) {
|
|
return $valor;
|
|
}
|
|
if ($cambio->desde()->id != $this->id) {
|
|
return $cambio->transform($valor, TipoCambio::DESDE);
|
|
}
|
|
return $cambio->transform($valor);
|
|
}
|
|
|
|
public function toArray(): array {
|
|
$arr = parent::toArray();
|
|
$arr['format'] = [
|
|
'prefijo' => $this->prefijo,
|
|
'sufijo' => $this->sufijo,
|
|
'decimales' => $this->decimales
|
|
];
|
|
return $arr;
|
|
}
|
|
}
|