118 lines
3.1 KiB
PHP
118 lines
3.1 KiB
PHP
<?php
|
|
namespace Incoviba;
|
|
|
|
use Carbon\Carbon;
|
|
use \Model;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property Proyecto $proyecto_id
|
|
* @property Operador $operador_id
|
|
* @property int $factura
|
|
* @property double $valor_uf
|
|
* @property int $valor_neto
|
|
* @property int $iva
|
|
*/
|
|
class FacturaProyectoOperador extends Model {
|
|
public static $_table = 'factura_proyecto_operador';
|
|
|
|
protected $proyecto;
|
|
public function proyecto() {
|
|
if ($this->proyecto === null) {
|
|
$this->proyecto = $this->belongs_to(Proyecto::class, 'proyecto_id')->find_one();
|
|
}
|
|
return $this->proyecto;
|
|
}
|
|
protected $operador;
|
|
public function operador() {
|
|
if ($this->operador === null) {
|
|
$this->operador = $this->belongs_to(Operador::class, 'operador_id')->find_one();
|
|
}
|
|
return $this->operador;
|
|
}
|
|
public function fecha(\DateTime $fecha = null) {
|
|
if ($fecha === null) {
|
|
return Carbon::parse($this->fecha);
|
|
}
|
|
$this->fecha = $fecha->format('Y-m-d');
|
|
}
|
|
public function valor_total() {
|
|
return $this->valor_neto + $this->iva;
|
|
}
|
|
|
|
protected $ventas;
|
|
public function ventas() {
|
|
if ($this->ventas === null) {
|
|
$this->ventas = $this->has_many(FacturaVenta::class, 'factura_id')->find_many();
|
|
}
|
|
return $this->ventas;
|
|
}
|
|
|
|
public static function add($data) {
|
|
$fields = [
|
|
'proyecto_id',
|
|
'operador_id',
|
|
'factura',
|
|
'valor_uf',
|
|
'valor_neto',
|
|
'iva'
|
|
];
|
|
$input = array_intersect_key($data, array_combine($fields, $fields));
|
|
$validate = [
|
|
'proyecto_id',
|
|
'operador_id',
|
|
'factura'
|
|
];
|
|
$orm = Model::factory(FacturaProyectoOperador::class);
|
|
foreach ($validate as $field) {
|
|
$orm = $orm->where($field, $input[$field]);
|
|
}
|
|
$factura = $orm->find_one();
|
|
$created = false;
|
|
$found = true;
|
|
if (!$factura) {
|
|
$found = false;
|
|
$factura = FacturaProyectoOperador::create($input);
|
|
$created = $factura->save();
|
|
}
|
|
$output = [
|
|
'input' => $data,
|
|
'factura' => $factura->as_array(),
|
|
'new' => !$found,
|
|
'created' => $created
|
|
];
|
|
return $output;
|
|
}
|
|
public function addVenta($data) {
|
|
$data['factura_id'] = $this->id;
|
|
return FacturaVenta::add($data);
|
|
}
|
|
|
|
public function as_array() {
|
|
$arr = parent::as_array();
|
|
$arr['proyecto'] = $this->proyecto()->as_array();
|
|
$arr['operador'] = $this->operador()->as_array();
|
|
$arr['fecha'] = (object) [
|
|
'valor' => $this->fecha,
|
|
'formateada' => $this->fecha()->format('d-m-Y')
|
|
];
|
|
$arr['valor_uf'] = (object) [
|
|
'valor' => $this->valor_uf,
|
|
'formateado' => number_format($this->valor_uf, 2, ',', '.') . ' UF'
|
|
];
|
|
$arr['valor_neto'] = (object) [
|
|
'valor' => $this->valor_neto,
|
|
'formateado' => '$ ' . number_format($this->valor_neto, 0, ',', '.')
|
|
];
|
|
$arr['iva'] = (object) [
|
|
'valor' => $this->iva,
|
|
'formateado' => '$ ' . number_format($this->iva, 0, ',', '.')
|
|
];
|
|
$arr['valor_total'] = (object) [
|
|
'valor' => $this->valor_total(),
|
|
'formateado' => '$ ' . number_format($this->valor_total(), 0, ',', '.')
|
|
];
|
|
return $arr;
|
|
}
|
|
}
|