Merge pull request 'refactor/totales' (#41) from refactor/totales into master

Reviewed-on: nathalie/pedi2#41
This commit is contained in:
Alejandro Tasistro 2025-04-13 23:22:08 -03:00
commit 7eb7dd04d7
14 changed files with 247 additions and 181 deletions

View file

@ -2,6 +2,7 @@
namespace App;
use App\Helpers\TransporteHelper;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use League\Csv\CannotInsertRecord;
@ -16,6 +17,11 @@ class GrupoDeCompra extends Model
protected $table = 'grupos_de_compra';
protected $hidden = ['password'];
public function subpedidos()
{
return $this->hasMany('App\Subpedido');
}
public function toggleDevoluciones()
{
$this->devoluciones_habilitadas = !$this->devoluciones_habilitadas;
@ -23,16 +29,73 @@ class GrupoDeCompra extends Model
return $this->devoluciones_habilitadas;
}
public function subpedidos()
{
return $this->hasMany('App\Subpedido');
}
public function pedidosAprobados()
{
return $this->subpedidos->where('aprobado', 1);
}
public function totalARecaudar()
{
$total = 0;
foreach ($this->pedidosAprobados() as $subpedido) {
$total = $total + $subpedido->total();
}
return $total;
}
public function totalBarrial()
{
$total = 0;
foreach ($this->pedidosAprobados() as $subpedido) {
$total = $total + $subpedido->totalBarrial();
}
return $total;
}
public function totalDevoluciones()
{
$total = 0;
foreach ($this->pedidosAprobados() as $subpedido) {
$total = $total + $subpedido->devoluciones_total;
}
return $total;
}
public function totalATransferir()
{
return $this->totalCentralesQueNoPaganTransporte()
+ $this->totalCentralesQuePaganTransporte()
+ $this->totalTransporte();
}
public function totalCentralesQueNoPaganTransporte()
{
$total = 0;
foreach ($this->pedidosAprobados() as $subpedido) {
$total = $total + $subpedido->totalCentralesQueNoPaganTransporte();
}
return $total;
}
public function totalCentralesQuePaganTransporte()
{
$total = 0;
foreach ($this->pedidosAprobados() as $subpedido) {
$total = $total + $subpedido->totalCentralesQuePaganTransporte();
}
return $total;
}
public function totalTransporte()
{
return TransporteHelper::totalTransporte($this->totalCentralesQuePaganTransporte());
}
public function cantidadTransporte()
{
return TransporteHelper::cantidadTransporte($this->totalCentralesQuePaganTransporte());
}
public function exportarPlanillasAPdf()
{
$subpedidos = $this->pedidosAprobados();

View file

@ -0,0 +1,19 @@
<?php
namespace App\Helpers;
class TransporteHelper
{
const COSTO_TRANSPORTE = 15;
const MONTO_TRANSPORTE = 500;
public static function cantidadTransporte($monto)
{
return ceil($monto / self::MONTO_TRANSPORTE);
}
public static function totalTransporte($monto)
{
return self::cantidadTransporte($monto) * self::COSTO_TRANSPORTE;
}
}

View file

@ -0,0 +1,19 @@
<?php
namespace App\Http\Controllers\Api;
use App\GrupoDeCompra;
use App\Http\Controllers\Controller;
use App\Http\Resources\GrupoDeCompraResource;
class GrupoDeCompraController extends Controller
{
public function index()
{
return GrupoDeCompraResource::collection(GrupoDeCompra::all());
}
public function show(GrupoDeCompra $grupoDeCompra)
{
return new GrupoDeCompraResource($grupoDeCompra);
}
}

View file

@ -0,0 +1,30 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class GrupoDeCompraResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'nombre' => $this->nombre,
'devoluciones_habilitadas' => $this->devoluciones_habilitadas,
'pedidos' => SubpedidoResource::collection($this->subpedidos),
'total_a_recaudar' => number_format($this->totalARecaudar(),2),
'total_barrial' => number_format($this->totalBarrial(),2),
'total_devoluciones' => number_format($this->totalDevoluciones(),2),
'total_a_transferir' => number_format($this->totalATransferir(),2),
'total_transporte' => number_format($this->totalTransporte(),0),
'cantidad_transporte' => number_format($this->cantidadTransporte(),0),
];
}
}

View file

@ -17,16 +17,14 @@ class SubpedidoResource extends JsonResource
return [
'id' => $this->id,
'nombre' => $this->nombre,
'subtotal_productos' => number_format($this->totalSinBonos(),0),
'subtotal_bonos' => number_format($this->getSubtotalBonos(),0),
'bonos_de_transporte' => $this->cantidadBDT(),
'subtotal_bonos_de_transporte' => number_format($this->getSubtotalBDT(),0),
'total' => number_format($this->getTotal(),0),
'total_menos_devoluciones' => number_format($this->getTotalMenosDevoluciones(),0),
'grupo_de_compra' => $this->grupoDeCompra,
'productos' => $this->productos,
'aprobado' => (bool) $this->aprobado,
'devoluciones_total' => (double) $this->devoluciones_total,
'total' => number_format($this->total(),2),
'total_transporte' => number_format($this->totalTransporte(),0),
'cantidad_transporte' => number_format($this->cantidadTransporte(),0),
'total_sin_devoluciones' => number_format($this->totalSinDevoluciones(),2),
'devoluciones_total' => number_format($this->devoluciones_total,2),
'devoluciones_notas' => $this->devoluciones_notas
];
}

View file

@ -2,7 +2,7 @@
namespace App;
use League\Csv\Reader;
use App\Helpers\TransporteHelper;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Log;
@ -10,6 +10,7 @@ use App\Filtros\FiltroDeSubpedido;
class Subpedido extends Model
{
const COSTO_TRANSPORTE = 15;
public $timestamps = false;
protected $fillable = ['grupo_de_compra_id', 'aprobado', 'nombre', 'devoluciones_total', 'devoluciones_notas'];
@ -40,47 +41,65 @@ class Subpedido extends Model
return $filtros->aplicar($query);
}
//Subtotal de dinero de productos del pedido, sin bonos ni transporte
public function totalSinBonos()
public function total()
{
return $this->productosSinBonos()->sum('total');
return $this->totalSinDevoluciones() - $this->devoluciones_total;
}
public function totalParaTransporte() {
$total = 0;
foreach ($this->productos()->get() as $producto) {
if ($producto->pagaTransporte()) {
$total += $producto->precio * $producto->pivot->cantidad;
}
}
return ceil($total);
}
//Cantidad de bonos de transporte
public function cantidadBDT()
public function totalSinDevoluciones()
{
return ceil($this->totalParaTransporte() / 500);
return $this->totalBarrial() + $this->totalCentral();
}
//Subtotal de dinero de bonos de transporte
public function getSubtotalBDT()
public function totalBarrial()
{
return $this->cantidadBDT() * 15;
return DB::table('producto_subpedido')
->join('productos', 'producto_subpedido.producto_id', '=', 'productos.id')
->where('producto_subpedido.subpedido_id', $this->id)
->where('productos.nombre', 'like', '%barrial%')
->selectRaw('SUM(productos.precio * producto_subpedido.cantidad) as total')
->value('total');
}
//Subtotal de dinero de bonos (MPS, Sororo, etc)
public function getSubtotalBonos()
public function totalCentral()
{
return $this->bonos()->sum('total');
return $this->totalCentralesQueNoPaganTransporte() + $this->totalCentralesQuePaganTransporte() + $this->totalTransporte();
}
public function getTotal()
public function totalCentralesQueNoPaganTransporte()
{
return $this->totalSinBonos() + $this->getSubtotalBDT() + $this->getSubtotalBonos();
return DB::table('producto_subpedido')
->join('productos', 'producto_subpedido.producto_id', '=', 'productos.id')
->where('producto_subpedido.subpedido_id', $this->id)
->where('productos.nombre', 'not like', '%barrial%')
->where(function ($query) {
$query->where('productos.categoria', 'like', '%SUBSIDIADO%')
->orWhere('productos.bono', true);
})
->selectRaw('SUM(productos.precio * producto_subpedido.cantidad) as total')
->value('total');
}
public function getTotalMenosDevoluciones() {
return $this->getTotal() - $this->getDevoluciones();
public function totalCentralesQuePaganTransporte()
{
return DB::table('producto_subpedido')
->join('productos', 'producto_subpedido.producto_id', '=', 'productos.id')
->where('producto_subpedido.subpedido_id', $this->id)
->where('productos.nombre', 'not like', '%barrial%')
->where('productos.bono', false)
->where('productos.categoria', 'not like', '%SUBSIDIADO%')
->selectRaw('SUM(productos.precio * producto_subpedido.cantidad) as total')
->value('total');
}
public function totalTransporte()
{
return TransporteHelper::totalTransporte($this->totalCentralesQuePaganTransporte());
}
public function cantidadTransporte()
{
return TransporteHelper::cantidadTransporte($this->totalCentralesQuePaganTransporte());
}
//Actualiza el pedido, agregando o quitando del subpedido según sea necesario. Debe ser llamado desde el controlador de subpedidos, luego de validar que los parámetros $producto y $cantidad son correctos. También calcula el subtotal por producto.
@ -110,13 +129,6 @@ class Subpedido extends Model
return $view->render();
}
public function getDevoluciones() {
return $this->devoluciones_total;
}
public function getNotasDevoluciones() {
return $this->devoluciones_notas;
}
public function syncDevoluciones(float $total, string $notas) {
$this->devoluciones_total = $total;
$this->devoluciones_notas = $notas;

View file

@ -3,27 +3,18 @@
<comunes-tabs-secciones :tabs="tabs" :tabInicial="tabActiva"></comunes-tabs-secciones>
<div class="block" id="pedidos-seccion"
:class="seccionActiva === 'pedidos-seccion' ? 'is-active' : 'is-hidden'">
<div class="block pb-6" id="pedidos-tabla-y-dropdown" v-show="hayPedidos">
<div class="block pb-6" id="pedidos-tabla-y-dropdown" v-if="hayPedidos">
<admin-dropdown-descargar
:gdc="gdc">
:gdc_id="gdc.id">
</admin-dropdown-descargar>
<admin-tabla-pedidos
:pedidos="pedidos" :bonosDeTransporte="bonosDeTransporte" :totalBonosBarriales="totalBonosBarriales">
</admin-tabla-pedidos>
:gdc="this.gdc"
></admin-tabla-pedidos>
</div>
<p class="has-text-centered" v-show="!hayPedidos">
<p class="has-text-centered" v-else>
Todavía no hay ningún pedido para administrar.
</p>
</div>
<div class="block pb-6" id="bonos-seccion"
:class="seccionActiva === 'bonos-seccion' ? 'is-active' : 'is-hidden'">
<admin-tabla-bonos v-show="hayAprobados"
:pedidos="pedidos">
</admin-tabla-bonos>
<p class="has-text-centered" v-show="!hayAprobados">
Todavía no hay pedidos aprobados.
</p>
</div>
<div class="block pb-6" id="caracteristicas-seccion"
:class="seccionActiva === 'caracteristicas-seccion' ? 'is-active' : 'is-hidden'">
<admin-caracteristicas-opcionales>
@ -38,6 +29,7 @@ import TabsSecciones from "../comunes/TabsSecciones.vue";
import DropdownDescargar from "./DropdownDescargar.vue";
import TablaPedidos from "./TablaPedidos.vue";
import TablaBonos from "./TablaBonos.vue";
import axios from "axios";
export default {
components: {
CaracteristicasOpcionales,
@ -48,12 +40,8 @@ export default {
},
data() {
return {
gdc: 0,
pedidos: [],
bonosDeTransporte: 0,
totalBonosBarriales: 0,
gdc: undefined,
tabs: [{ id: "pedidos", nombre: "Pedidos" },
{ id: "bonos", nombre: "Bonos" },
{ id: "caracteristicas", nombre: "Caracteristicas opcionales" }],
tabActiva: "pedidos",
seccionActiva: "pedidos-seccion",
@ -61,46 +49,32 @@ export default {
},
computed: {
hayPedidos: function() {
return this.pedidos.length !== 0
return this.gdc && this.gdc.pedidos.length !== 0
},
hayAprobados: function() {
return this.pedidos.filter(p => p.aprobado).length > 0
return this.gdc && this.gdc.pedidos.filter(p => p.aprobado).length > 0
}
},
methods: {
fetchPedidos() {
axios.get("/api/grupos-de-compra/"+this.gdc+"/bonos-de-transporte", {})
.then(response => this.bonosDeTransporte = response.data.bdt);
axios.get("/api/subpedidos/resources", {
params: {
grupo_de_compra: this.gdc
}})
.then(response => {
this.pedidos = response.data.data
}).get;
axios.get("/api/grupos-de-compra/"+this.gdc+"/bonos-barriales", {})
.then(response => this.totalBonosBarriales = response.data.bonos_barriales)
},
setSeccionActiva(tabId) {
this.tabActiva = tabId;
this.seccionActiva = tabId + "-seccion";
},
getBonosBarriales() {
axios.get("/api/grupos-de-compra/"+this.gdc+"/bonos-barriales", {})
.then(response => this.totalBonosBarriales = response.data.bonos_barriales)
actualizar() {
axios.get('/api/grupos-de-compra/' + this.$root.gdc)
.then(response => {
this.gdc = response.data.data;
console.log(this.gdc);
})
}
},
mounted() {
async mounted() {
Event.$on('sync-aprobacion', (_) => {
this.fetchPedidos();
this.actualizar();
});
axios.get("/admin/obtener_sesion").then(response => {
this.gdc = response.data.gdc;
this.fetchPedidos();
this.bonosBarriales = this.getBonosBarriales()
});
}
await new Promise(r => setTimeout(r, 1000));
this.actualizar();
},
}
</script>

View file

@ -14,13 +14,13 @@
</div>
<div class="dropdown-menu" id="dropdown-menu" role="menu">
<div class="dropdown-content">
<a :href="'/admin/exportar-pedido-a-csv/' + gdc" class="dropdown-item has-background-primary">
<a :href="'/admin/exportar-pedido-a-csv/' + gdc_id" class="dropdown-item has-background-primary">
Planilla para central (CSV)
</a>
<a :href="'/admin/exportar-planillas-a-pdf/' + gdc" class="dropdown-item">
<a :href="'/admin/exportar-planillas-a-pdf/' + gdc_id" class="dropdown-item">
Planillas para armado (PDF)
</a>
<a :href="'/admin/exportar-pedido-con-nucleos-a-csv/' + gdc" class="dropdown-item">
<a :href="'/admin/exportar-pedido-con-nucleos-a-csv/' + gdc_id" class="dropdown-item">
Planilla completa de la canasta (CSV)
</a>
</div>
@ -32,7 +32,7 @@
<script>
export default {
props: {
gdc: {
gdc_id: {
type: Number,
required: true
},

View file

@ -41,7 +41,7 @@ export default {
mounted() {
if (this.$root.gdc) {
this.gdc = this.$root.gdc;
this.obtenerValor(this);
this.obtenerValor();
}
}
}

View file

@ -1,9 +1,9 @@
<template>
<tr>
<td>{{ pedido.nombre }}</td>
<td v-if="$root.devoluciones" class="has-text-right" >{{ this.$limpiarInt(pedido.total) }}</td>
<td v-if="$root.devoluciones" class="has-text-right" >{{ pedido.total_sin_devoluciones }}</td>
<td v-if="$root.devoluciones" class="has-text-right" ><abbr :title="pedido.devoluciones_notas">-{{ pedido.devoluciones_total }}</abbr></td>
<td class="has-text-right" >{{ $root.devoluciones ? pedido.total_menos_devoluciones : pedido.total }}</td>
<td class="has-text-right" >{{ $root.devoluciones ? pedido.total : pedido.total_sin_devoluciones }}</td>
<td>
<admin-switch-aprobacion
:pedido="pedido">

View file

@ -12,7 +12,7 @@
</thead>
<tbody>
<admin-fila-pedido
v-for="pedido in this.pedidos"
v-for="pedido in gdc.pedidos"
:pedido="pedido" :key="pedido.id">
</admin-fila-pedido>
</tbody>
@ -23,23 +23,27 @@
</tr>
<tr>
<th>Total a recaudar:</th>
<td class="has-text-right">$ {{ totalARecaudar }}</td>
<td class="has-text-right">$ {{ gdc.total_a_recaudar }}</td>
</tr>
<tr>
<th>Total bonos barriales:</th>
<td class="has-text-right">$ {{ $parent.totalBonosBarriales }}</td>
<td class="has-text-right">$ {{ gdc.total_barrial }}</td>
</tr>
<tr v-if="$root.devoluciones">
<th>Total devoluciones:</th>
<td class="has-text-right">- $ {{ totalDevoluciones() }}</td>
<td class="has-text-right">- $ {{ gdc.total_devoluciones }}</td>
</tr>
<tr>
<th>Cantidad bonos de transporte:</th>
<td class="has-text-right">{{ gdc.cantidad_transporte }}</td>
</tr>
<tr>
<th>Total bonos de transporte:</th>
<td class="has-text-right">$ {{ bonosDeTransporte * 15 }}</td>
<td class="has-text-right">$ {{ gdc.total_transporte }}</td>
</tr>
<tr>
<th>Total a depositar:</th>
<td class="has-text-right">$ {{ totalAprobadosConTransporteRecalculado() - totalBonosBarriales }}</td>
<td class="has-text-right">$ {{ gdc.total_a_transferir }}</td>
</tr>
</table>
</div>
@ -52,60 +56,11 @@ export default {
FilaPedido
},
props: {
pedidos: {
type: Array,
gdc: {
type: Object,
required: true
},
bonosDeTransporte: {
type: Number,
required: true
},
totalBonosBarriales: {
type: Number,
required: true
},
},
computed: {
totalARecaudar: function() {
return this.$root.devoluciones ? this.totalAprobadosMenosDevoluciones() : this.totalAprobados();
},
},
methods: {
totalDevoluciones() {
let suma = 0
let aprobados = this.pedidos.filter(p => p.aprobado);
for (let i = 0; i < aprobados.length; i++) {
suma += aprobados[i].devoluciones_total
}
return suma;
},
totalAprobados() {
let suma = 0
let aprobados = this.pedidos.filter(p => p.aprobado);
for (let i = 0; i < aprobados.length; i++) {
suma += this.$limpiarFloat(aprobados[i].total)
}
return suma;
},
totalAprobadosMenosDevoluciones() {
let suma = 0
let aprobados = this.pedidos.filter(p => p.aprobado);
for (let i = 0; i < aprobados.length; i++) {
suma += this.$limpiarFloat(aprobados[i].total_menos_devoluciones)
}
return suma;
},
totalAprobadosConTransporteRecalculado() {
let suma = 0
let aprobados = this.pedidos.filter(p => p.aprobado);
for (let i = 0; i < aprobados.length; i++) {
suma += this.$limpiarFloat(aprobados[i].total)
suma -= this.$limpiarFloat(aprobados[i].subtotal_bonos_de_transporte)
}
suma += parseInt(this.bonosDeTransporte)*15
return suma;
}
},
}
</script>

View file

@ -73,17 +73,17 @@
if (this.notas_devoluciones.length > 15) {
this.notas_devoluciones_abbr += "...";
}
this.total = this.$limpiarInt(this.$root.devoluciones ? this.$root.pedido.total_menos_devoluciones : this.$root.pedido.total);
this.productos = this.$root.productos
this.total = this.$root.pedido.total;
this.productos = this.$root.productos;
},
modificarDevoluciones: function() {
Event.$emit("modificar-devoluciones");
},
cantidadBonosDeTransporte: function() {
return this.$limpiarInt(this.$root.pedido.subtotal_bonos_de_transporte) / 15
return this.$root.pedido.cantidad_transporte;
},
totalBonosDeTransporte: function() {
return this.$limpiarInt(this.$root.pedido.subtotal_bonos_de_transporte)
return this.$root.pedido.total_transporte
},
},
}

View file

@ -38,7 +38,7 @@ export default {
Event.$emit("toggle-chismosa", this.activa);
},
actualizar() {
this.total = this.$limpiarInt(this.$root.devoluciones ? this.$root.pedido.total_menos_devoluciones : this.$root.pedido.total);
this.total = this.$root.pedido.total;
},
},
}

View file

@ -28,18 +28,14 @@ Route::middleware('api')->group(function () {
$atributos_a_ocultar = ['telefono', 'cantidad_de_nucleos', 'correo', 'referente_finanzas', 'created_at', 'updated_at'];
return GrupoDeCompra::all()->makeHidden($atributos_a_ocultar)->sortBy('nombre')->groupBy('region');
});
Route::get('/{gdc}/bonos-de-transporte', function($gdc) {
$grupo = GrupoDeCompra::where('id',$gdc)->first();
return ['bdt' => $grupo->calcularCantidadBDT()];
});
Route::get('/{gdc}/bonos-barriales', function($gdc) {
$grupo = GrupoDeCompra::where('id',$gdc)->first();
return ['bonos_barriales' => $grupo->totalBonosBarriales()];
});
Route::get('/{grupoDeCompra}', 'Api\GrupoDeCompraController@show');
Route::get('/{gdc}/devoluciones', function($gdc) {
$habilitadas = GrupoDeCompra::find($gdc)->devoluciones_habilitadas;
return ['devoluciones' => $habilitadas];
});
Route::post('/{gdc}/devoluciones', function($gdc) {
$habilitadas = GrupoDeCompra::find($gdc)->toggleDevoluciones();
return ['devoluciones' => $habilitadas];