This commit is contained in:
2021-08-18 19:03:58 -04:00
parent b58cda3e4e
commit 4f7241e146
12 changed files with 143 additions and 25 deletions

View File

@ -41,4 +41,10 @@ class Facturas {
];
return $this->withJson($response, $output);
}
public function add_venta(Request $request, Response $response, $id_factura): Response {
$post = $request->getParsedBody();
$factura = Model::factory(FacturaProyectoOperador::class)->find_one($id_factura);
$output = $factura->addVenta($post);
return $this->withJson($response, $output);
}
}

View File

@ -28,4 +28,20 @@ class Ventas {
];
return $this->withJson($response, $output);
}
public function facturas(Request $request, Response $response, $id_venta): Response {
$venta = Model::factory(Venta::class)->find_one($id_venta);
if (!$venta) {
return $this->withJson($response, ['venta' =>null, 'facturas' => null]);
}
$output = [
'venta' => $venta->as_array(),
'facturas' => null
];
if ($venta->facturas() !== null) {
$output['facturas'] = array_map(function($item) {
return $item->as_array();
}, $venta->facturas());
}
return $this->withJson($response, $output);
}
}

View File

@ -10,6 +10,7 @@ $app->group('/facturas', function($app) {
});
$app->group('/factura/{id_factura}', function($app) {
$app->group('/ventas', function($app) {
$app->post('/add[/]', [Facturas::class, 'add_venta']);
$app->get('[/]', [Facturas::class, 'ventas']);
});
});

View File

@ -8,5 +8,8 @@ $app->group('/venta/{id_venta}', function($app) {
$app->group('/operador', function($app) {
$app->get('[/]', [Ventas::class, 'operador']);
});
$app->group('/facturas', function($app) {
$app->get('[/]', [Ventas::class, 'facturas']);
});
$app->get('[/]', [Ventas::class, 'show']);
});

View File

@ -83,6 +83,10 @@ class FacturaProyectoOperador extends Model {
];
return $output;
}
public function addVenta($data) {
$data['factura_id'] = $this->id;
return FacturaVenta::add($data);
}
public function as_array() {
$arr = parent::as_array();

View File

@ -27,6 +27,37 @@ class FacturaVenta extends Model {
return $this->venta;
}
public static function add($data) {
$fields = [
'factura_id',
'venta_id',
'valor'
];
$input = array_intersect_key($data, array_combine($fields, $fields));
$validate = [
'factura_id',
'venta_id'
];
$orm = Model::factory(FacturaVenta::class);
foreach ($validate as $field) {
$orm = $orm->where($field, $input[$field]);
}
$factura = $orm->find_one();
$created = false;
$found = true;
if (!$factura) {
$found = false;
$factura = FacturaVenta::create($input);
$created = $factura->save();
}
return [
'input' => $input,
'factura' => $factura->as_array(),
'new' => !$found,
'created' => $created
];
}
public function as_array() {
$arr = parent::as_array();
$arr['factura'] = $this->factura()->as_array();

View File

@ -20,11 +20,4 @@ class Unidad extends Model {
}
return $this->proyecto_tipo_unidad;
}
protected $facturas;
public function facturas() {
if ($this->facturas === null) {
$this->facturas = $this->has_many(FacturaUnidad::class, 'unidad_id')->find_many();
}
return $this->facturas;
}
}

View File

@ -72,16 +72,26 @@ class Venta extends Model {
}
return $this->comision;
}
protected $facturas;
public function facturas() {
if ($this->facturas === null) {
$this->facturas = $this->has_many(FacturaVenta::class, 'venta_id')->find_many();
}
return $this->facturas;
}
public function as_array() {
$arr = parent::as_array();
$arr['propietario'] = $this->propietario()->as_array();
$arr['propiedad'] = $this->propiedad()->as_array();
$arr['valor'] = '$ ' . number_format($this->valor_uf, 2, ',', '.');
$arr['valor'] = [
'valor' => $this->valor_uf,
'formateado' => number_format($this->valor_uf, 2, ',', '.') . ' UF'
];
if ($this->operador()) {
$arr['operador'] = $this->operador()->as_array();
$arr['comision'] = (array) $this->comision();
$arr['comision']['formateada'] = '$ ' . number_format($this->comision()->total, 2, ',', '.');
$arr['comision']['formateada'] = number_format($this->comision()->total, 2, ',', '.') . ' UF';
}
return $arr;
}