Compare commits

...

2 commits

Author SHA1 Message Date
Rodrigo
b752f9e8c5 Subir saldos como csv 2025-06-15 00:19:30 -03:00
Rodrigo
6787dde711 Editor de saldos en compras 2025-06-14 22:46:47 -03:00
10 changed files with 243 additions and 10 deletions

View file

@ -286,4 +286,9 @@ class GrupoDeCompra extends Model
->get()
->keyBy('producto_id');
}
public function setSaldo(float $saldo) {
$this->saldo = $saldo;
$this->save();
}
}

View file

@ -32,4 +32,13 @@ class GrupoDeCompraController extends Controller
GrupoDeCompra::find($gdc)->toggleDevoluciones();
return response()->noContent();
}
public function setSaldo(int $gdc) {
$valid = request()->validate([
'saldo' => ['required', 'min:0'],
]);
$grupoDeCompra = GrupoDeCompra::find($gdc);
$grupoDeCompra->setSaldo($valid['saldo']);
return new GrupoDeCompraResource($grupoDeCompra);
}
}

View file

@ -8,10 +8,14 @@ use App\Producto;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use League\Csv\Reader;
use DatabaseSeeder;
class ComprasController
{
const CANASTAS_PATH = 'csv/canastas/';
const BARRIO = "Barrio";
const SALDO = "Saldo";
public function indexPedidos() {
return view('compras_pedidos');
@ -59,4 +63,36 @@ class ComprasController
$file = resource_path('csv/productos.csv');
return response()->download($file);
}
public function cargarSaldos(Request $request): JsonResponse
{
$request->validate([
'data' => 'required|file|mimes:csv,txt|max:2048',
]);
$file = $request->file('data')->getPathname();
$csv = Reader::createFromPath($file, 'r');
try {
$csv->setDelimiter("|");
$csv->setEnclosure("'");
$csv->setHeaderOffset(0);
$records = $csv->getRecords();
} catch (InvalidArgument|Exception $e) {
Log::error($e->getMessage());
return response()->json([
'message' => 'No se pudo leer el csv',
]);
}
foreach ($records as $record) {
$barrio = $record[self::BARRIO];
$saldo = $record[self::SALDO];
GrupoDeCompra::where('nombre', $barrio)
->update(['saldo' => $saldo]);
}
return response()->json([
'message' => 'Saldos cargados exitosamente',
]);
}
}

View file

@ -21,14 +21,14 @@ class GrupoDeCompraResource extends JsonResource
'devoluciones_habilitadas' => $this->devoluciones_habilitadas,
'pedidos' => SubpedidoResource::collection($this->subpedidos),
'total_a_recaudar' => number_format($this->totalARecaudar(),2),
'saldo' => number_format($this->saldo,2),
'saldo' => number_format($this->saldo, 2, ".", ""),
'total_sin_devoluciones' => number_format($this->totalSinDevoluciones(),2),
'total_barrial' => number_format($this->totalBarrial(),2),
'total_devoluciones' => number_format($this->totalDevoluciones(),2),
'total_de_pedido' => number_format($this->totalDePedido(),2),
'total_a_transferir' => number_format($this->totalATransferir(),2),
'total_transporte' => number_format($this->totalTransporte()),
'cantidad_transporte' => number_format($this->cantidadTransporte()),
'cantidad_transnumber_formatporte' => number_format($this->cantidadTransporte()),
];
}
}

View file

@ -48,7 +48,7 @@
</tr>
<tr>
<th>Saldo a favor:</th>
<td class="has-text-right">$ {{ saldo }}</td>
<td class="has-text-right">- $ {{ saldo }}</td>
</tr>
<tr>
<th>Total a transferir:</th>

View file

@ -47,11 +47,62 @@
:class="seccionActiva === 'saldos-compras-seccion' ? 'is-active' : 'is-hidden'"
>
<div class="block" id="saldos-compras-seccion">
<table>
<article class="message is-warning">
<div class="message-header">
<p>
<button class="icon" aria-label="foldout" @click="toggleSaldosFileDialog">
<i class="fa" :class="show_saldos_file_dialog ? 'fa-arrow-up' : 'fa-arrow-down'"></i>
</button>
Cargar saldos
</p>
</div>
<div class="message-body" v-if="show_saldos_file_dialog">
<div class="content">
La planilla de saldos tiene que tener el siguiente formato para que la aplicación la lea correctamente:
<ul>
<li>Los valores deben usar punto y no coma decimal</li>
<li>El nombre de las columnas deben ser "Barrio" y "Saldo"</li>
<li>Las celdas deben separarse con '|'</li>
<li>No puede haber "enters" en ninguna celda</li>
<li>El nombre de los barrios debe estar exactamente igual que como est&aacute;n configurados en esta aplicacai&oacute;n</li>
</ul>
<article class="message is-danger mt-2">
<div class="message-body">
<div class="content">
Cargar un archivo de saldos s&oacute;lo reemplazar&aacute; los saldos de los barrios presentes en la tabla.
</div>
</div>
</article>
</div>
<input-file-button text="Subir archivo" @archivo-subido="saldosSubido" />
</div>
</article>
<table class="table container">
<thead>
<tr>
<th>Barrio</th>
<th>Saldo</th>
</tr>
</thead>
<tbody>
<tr v-for="gdc in grupos_de_compra">
<th>{{ gdc.nombre }}</th>
<td>{{ gdc.saldo }}</td>
<td>
<input id="cantidad"
v-model="gdc.saldo"
class="input is-small"
type="number"
style="text-align: center"
@input="saldoModificado(gdc.id)">
</td>
<td>
<button :disabled="!isSaldoModificado(gdc.id)" class="button is-small is-success ml-1" @click="confirmar_saldo(gdc.id)">
<span class="icon">
<i class="fas fa-check"></i>
</span>
</button>
</td>
</tr>
</tbody>
</table>
@ -64,6 +115,7 @@
import TabsSecciones from "../comunes/TabsSecciones.vue";
import DropdownDescargar from "./DropdownDescargar.vue";
import CanastaInput from "./CanastaInput.vue";
import InputFileButton from "../comunes/InputFileButton.vue";
import { mapActions, mapState } from "vuex";
export default {
@ -71,6 +123,7 @@ export default {
TabsSecciones,
DropdownDescargar,
CanastaInput,
InputFileButton,
},
data() {
return {
@ -82,6 +135,8 @@ export default {
tabActiva: "pedidos-compras",
seccionActiva: "pedidos-compras-seccion",
archivo: undefined,
saldo_modificado: {},
show_saldos_file_dialog: false,
}
},
methods: {
@ -89,7 +144,57 @@ export default {
this.tabActiva = tabId;
this.seccionActiva = tabId + "-seccion";
},
...mapActions('comisiones', ['getGruposDeCompra']),
...mapActions('comisiones', ['getGruposDeCompra', 'setSaldo']),
...mapActions('ui',["toast"]),
async confirmar_saldo(gdc_id) {
var saldo = this.getSaldo(gdc_id);
await this.setSaldo({
gdc_id: gdc_id,
saldo: saldo,
});
this.saldo_modificado[gdc_id] = false;
await this.getGruposDeCompra();
},
saldoModificado(gdc_id) {
this.saldo_modificado[gdc_id] = true;
},
isSaldoModificado(gdc_id) {
return gdc_id in this.saldo_modificado && this.saldo_modificado[gdc_id];
},
getSaldo(gdc_id) {
for (var i = 0; i < this.grupos_de_compra.length; i++) {
if (this.grupos_de_compra[i].id == gdc_id) {
return this.grupos_de_compra[i].saldo;
}
}
return 0;
},
async saldosSubido(event) {
var archivo = event.archivo;
if (archivo.type === "text/csv") {
const formData = new FormData();
formData.append("data", archivo);
try {
const response = await axios.post("/compras/saldos", formData, {
headers: {
"Content-Type": "multipart/form-data",
},
});
this.getGruposDeCompra();
this.toast({ mensaje: (response.data.message || "Canasta cargada exitosamente") });
} catch (error) {
console.log(error);
this.toast({ mensaje: (error.response?.data?.message || "Hubo errores.") });
}
event.component.cargando = false;
} else {
this.toast("El archivo debe ser .CSV");
event.component.cargando = false;
}
},
toggleSaldosFileDialog() {
this.show_saldos_file_dialog = !this.show_saldos_file_dialog;
},
},
computed: {
...mapState('comisiones', [

View file

@ -0,0 +1,56 @@
<template>
<div class="file has-name">
<label class="file-label">
<input
class="file-input"
type="file"
name="canasta"
@change="archivoSubido"
/>
<span class="file-cta">
<span class="file-icon">
<i class="fas fa-cloud-upload-alt"></i>
</span>
<span class="file-label">{{ text }}</span>
</span>
<span class="file-name" v-if="cargando">
{{ 'Cargando ' + archivo.nombre }}
</span>
</label>
</div>
</template>
<script>
export default {
name: "InputFileButton",
props: {
text: {
type: String,
required: true,
},
},
data() {
return {
archivo: null,
cargando: false,
};
},
methods: {
archivoSubido(event) {
const archivo = event.target.files[0];
if (archivo) {
this.archivo = { data: archivo, nombre: archivo.name };
this.$emit("archivo-subido", {
component: this,
archivo: archivo
});
this.cargando = true;
}
},
},
};
</script>
<style scoped>
</style>

View file

@ -1,12 +1,21 @@
import axios from "axios";
const state = {
grupos_de_compra: null,
grupos_de_compra: [],
};
const mutations = {
setGruposDeCompra(state, { grupos_de_compra }) {
state.grupos_de_compra = grupos_de_compra;
setGruposDeCompra(state, { data }) {
state.grupos_de_compra = data;
},
setGrupoDeCompra(state, gdc) {
for (var i = 0; i < state.grupos_de_compra.length; i++) {
if (state.grupos_de_compra[i].id == gdc.id) {
state.grupos_de_compra[i] = gdc;
return;
}
}
state.grupos_de_compra.push(gdc);
},
};
@ -15,9 +24,20 @@ const actions = {
const response = await axios.get('/api/grupos-de-compra');
commit('setGruposDeCompra', response.data);
},
async setSaldo({ commit }, { gdc_id, saldo }) {
const response = await axios.post(
"api/grupos-de-compra/" + gdc_id + "/saldo",
{ saldo: saldo }
);
commit('setGrupoDeCompra', response.data.data);
},
};
const getters = {};
const getters = {
getSaldo() {
return (gdc_id) => state.grupos_de_compra.find(gdc => gdc.id === gdc_id)?.saldo ?? 0;
},
};
export default {
namespaced: true,

View file

@ -21,6 +21,7 @@ Route::middleware('api')->group(function() {
Route::get('/', 'Api\GrupoDeCompraController@index');
Route::get('/{grupoDeCompra}', 'Api\GrupoDeCompraController@show');
Route::post('/{gdc}/devoluciones', 'Api\GrupoDeCompraController@toggleDevoluciones');
Route::post('/{gdc}/saldo', 'Api\GrupoDeCompraController@setSaldo');
});
Route::prefix('subpedidos')->group(function () {

View file

@ -54,4 +54,5 @@ Route::middleware(['auth', 'role:comision'])->group( function() {
Route::get('/compras/pedidos/pdf', 'ComprasController@pdf')->name('compras.pedidos.pdf');
Route::get('/compras/canasta/ejemplo', 'ComprasController@descargarCanastaEjemplo')->name('compras.canasta.ejemplo');
Route::post('/compras/canasta', 'ComprasController@cargarCanasta')->name('compras.canasta');
Route::post('/compras/saldos', 'ComprasController@cargarSaldos')->name('compras.canasta');
});