From 4f7241e14674692c7e319ab3589783d38f6594f7 Mon Sep 17 00:00:00 2001 From: Aldarien Date: Wed, 18 Aug 2021 19:03:58 -0400 Subject: [PATCH] v0.1.0 --- .db.env.sample | 4 + .ui.env.sample | 3 + api/common/Controller/Facturas.php | 6 ++ api/common/Controller/Ventas.php | 16 ++++ api/resources/routes/01_facturas.php | 1 + api/resources/routes/01_ventas.php | 3 + api/src/FacturaProyectoOperador.php | 4 + api/src/FacturaVenta.php | 31 ++++++++ api/src/Unidad.php | 7 -- api/src/Venta.php | 14 +++- .../proyectos/operadores/list.blade.php | 1 + ui/resources/views/ventas/show.blade.php | 78 +++++++++++++++---- 12 files changed, 143 insertions(+), 25 deletions(-) create mode 100644 .db.env.sample create mode 100644 .ui.env.sample diff --git a/.db.env.sample b/.db.env.sample new file mode 100644 index 0000000..2f538a3 --- /dev/null +++ b/.db.env.sample @@ -0,0 +1,4 @@ +MYSQL_ROOT_PASSWORD=password +MYSQL_DATABASE=database +MYSQL_USER=user +MYSQL_PASSWORD=password diff --git a/.ui.env.sample b/.ui.env.sample new file mode 100644 index 0000000..1ecfe73 --- /dev/null +++ b/.ui.env.sample @@ -0,0 +1,3 @@ +DEBUG=true +BASE_URL=http://localhost:8080 +API_URL=http://localhost:8081 diff --git a/api/common/Controller/Facturas.php b/api/common/Controller/Facturas.php index 605fabc..36fbe2a 100644 --- a/api/common/Controller/Facturas.php +++ b/api/common/Controller/Facturas.php @@ -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); + } } diff --git a/api/common/Controller/Ventas.php b/api/common/Controller/Ventas.php index 49aad16..b0168ad 100644 --- a/api/common/Controller/Ventas.php +++ b/api/common/Controller/Ventas.php @@ -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); + } } diff --git a/api/resources/routes/01_facturas.php b/api/resources/routes/01_facturas.php index ff7e78b..e807379 100644 --- a/api/resources/routes/01_facturas.php +++ b/api/resources/routes/01_facturas.php @@ -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']); }); }); diff --git a/api/resources/routes/01_ventas.php b/api/resources/routes/01_ventas.php index 7dab3b6..8df7789 100644 --- a/api/resources/routes/01_ventas.php +++ b/api/resources/routes/01_ventas.php @@ -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']); }); diff --git a/api/src/FacturaProyectoOperador.php b/api/src/FacturaProyectoOperador.php index 5c9d5a7..1669ece 100644 --- a/api/src/FacturaProyectoOperador.php +++ b/api/src/FacturaProyectoOperador.php @@ -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(); diff --git a/api/src/FacturaVenta.php b/api/src/FacturaVenta.php index d4e2d14..137f7fe 100644 --- a/api/src/FacturaVenta.php +++ b/api/src/FacturaVenta.php @@ -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(); diff --git a/api/src/Unidad.php b/api/src/Unidad.php index 9a48d78..cba24b9 100644 --- a/api/src/Unidad.php +++ b/api/src/Unidad.php @@ -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; - } } diff --git a/api/src/Venta.php b/api/src/Venta.php index 8499022..ab5c661 100644 --- a/api/src/Venta.php +++ b/api/src/Venta.php @@ -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; } diff --git a/ui/resources/views/facturas/proyectos/operadores/list.blade.php b/ui/resources/views/facturas/proyectos/operadores/list.blade.php index 80e97d5..6a78d6d 100644 --- a/ui/resources/views/facturas/proyectos/operadores/list.blade.php +++ b/ui/resources/views/facturas/proyectos/operadores/list.blade.php @@ -328,6 +328,7 @@ facturas.asignar($(e.target)).then(() => { $(e.target).trigger('reset') $(this.id).modal('toggle') + $(this.id).modal('hide') }) return false }) diff --git a/ui/resources/views/ventas/show.blade.php b/ui/resources/views/ventas/show.blade.php index 0d375fc..005d8c0 100644 --- a/ui/resources/views/ventas/show.blade.php +++ b/ui/resources/views/ventas/show.blade.php @@ -53,6 +53,60 @@ }) } } + class Facturas { + constructor(venta_id) { + this.venta_id = venta_id + this.facturas = [] + } + get() { + return $.ajax({ + url: '{{$urls->api}}/venta/' + this.venta_id + '/facturas', + method: 'get', + dataType: 'json' + }).then((data) => { + if (data.facturas === null || data.facturas.length == 0) { + return + } + this.facturas = data.facturas + this.draw() + }) + } + draw() { + const parent = $('#' + this.venta_id) + let tbody = parent.find('tbody') + if (tbody.length == 0) { + tbody = $('') + const table = $('
').attr('class', 'table').append( + $('').append( + $('').append( + $('').html('Factura') + ).append( + $('').html('Emisor') + ).append( + $('').html('Fecha') + ).append( + $('').html('Valor') + ) + ) + ).append(tbody) + parent.append(table) + } + tbody.html('') + $.each(this.facturas, (i, el) => { + tbody.append( + $('').append( + $('').html(el.factura.factura) + ).append( + $('').html(el.factura.operador.descripcion) + ).append( + $('').html(el.factura.fecha.formateada) + ).append( + $('').html(el.valor.formateado) + ) + ) + }) + } + } const ventas = { id: '#ventas', data: [], @@ -69,9 +123,9 @@ this.draw() }) }, - facturas: (unidad) => { + facturas: (venta) => { return $.ajax({ - url: '{{$urls->api}}/unidad/' + unidad + '/facturas', + url: '{{$urls->api}}/venta/' + venta + '/facturas', method: 'get', dataType: 'json' }) @@ -86,34 +140,26 @@ ).append( $('').html(el.propietario.nombre_completo) ).append( - $('').attr('class', 'text-right').html(el.valor) + $('').attr('class', 'text-right').html(el.valor.formateado) ) if (el.operador) { row.append( $('').html(el.operador.descripcion) ).append( $('').attr('class', 'text-right').html(el.comision.formateada) - ).append( - $('').attr('id', el.propiedad.unidades[0].id) ) - this.get().facturas(el.propiedad.unidades[0].id).then((data) => { - const td = $('td#' + data.unidad.id) - if (data.facturas === null || data.facturas.length == 0) { - return - } - $.each(data.facturas, (k, it) => { - console.debug(it) - }) - }) } else { row.append( $('') ).append( $('') - ).append( - $('') ) } + row.append( + $('').attr('id', el.id) + ) + this.data[i].facturas = new Facturas(el.id) + this.data[i].facturas.get() parent.append(row) }) }