<?php
namespace Incoviba;

use \Model;

/**
 * @property int $id
 * @property FacturaOperador $factura_id
 * @property Venta $venta_id
 * @property double $valor
 */
class FacturaVenta extends Model {
  public static $_table = 'factura_venta';

  protected $factura;
  public function factura() {
    if ($this->factura === null) {
      $this->factura = $this->belongs_to(FacturaProyectoOperador::class, 'factura_id')->find_one();
    }
    return $this->factura;
  }
  protected $venta;
  public function venta() {
    if ($this->venta === null) {
      $this->venta = $this->belongs_to(Venta::class, 'venta_id')->find_one();
    }
    return $this->venta;
  }

  public function as_array() {
    $arr = parent::as_array();
    $arr['factura'] = $this->factura()->as_array();
    $arr['venta'] = $this->venta()->as_array();
    $arr['valor'] = (object) [
      'valor' => $this->valor,
      'formateado' => number_format($this->valor, 2, ',', '.') . ' UF'
    ];
    return $arr;
  }
}
