Sincronizar notas de producto

This commit is contained in:
Rodrigo 2024-10-08 20:16:25 -03:00
parent 9fc47513c2
commit 6d10fbc0bf
4 changed files with 34 additions and 22 deletions

View File

@ -79,7 +79,7 @@ class SubpedidoController extends Controller
$valid = request()->validate([ $valid = request()->validate([
'cantidad' => 'required|min:0', 'cantidad' => 'required|min:0',
'notas' => 'required', 'notas' => 'nullable',
'producto_id' => [ 'producto_id' => [
'required', 'required',
Rule::in(Producto::all()->pluck('id')), Rule::in(Producto::all()->pluck('id')),
@ -87,7 +87,11 @@ class SubpedidoController extends Controller
]); ]);
$producto = Producto::find($valid['producto_id']); $producto = Producto::find($valid['producto_id']);
$subpedido->syncProducto($producto, $valid['cantidad'], $valid['notas']); $notas = $valid['notas'];
if ($notas == null) {
$notas = "";
}
$subpedido->syncProducto($producto, $valid['cantidad'], $notas);
return new SubpedidoResource($subpedido); return new SubpedidoResource($subpedido);
} }

View File

@ -14,7 +14,7 @@ class NotasProducto extends Migration
public function up() public function up()
{ {
Schema::table('producto_subpedido', function (Blueprint $table) { Schema::table('producto_subpedido', function (Blueprint $table) {
$table->string('notas'); $table->string('notas')->nullable();
}); });
} }

View File

@ -23,6 +23,11 @@
<i class="fas fa-trash-alt"></i> <i class="fas fa-trash-alt"></i>
</span> </span>
</button> </button>
<div v-if="producto.requiere_notas" class="is-full-width">
<label class="label">Notas:</label>
<input v-model="notas" class="input" type="text" />
</div>
</div> </div>
</template> </template>
@ -33,21 +38,23 @@
}, },
data() { data() {
return { return {
cantidad: this.producto.cantidad, cantidad: this.cantidadEnChismosa(),
enChismosa: this.producto.cantidad, notas: this.notasEnChismosa(),
} }
}, },
mounted() { mounted() {
if (this.producto.pivot !== undefined) { Event.$on('sync-subpedido', (cantidad, productoId, notas) => {
this.cantidad = this.producto.pivot.cantidad; if (this.producto.id === productoId)
this.enChismosa = this.cantidad; this.sincronizar(cantidad, notas);
}
Event.$on('sync-subpedido', (cantidad,productoId) => {
if (this.producto.id === productoId)
this.sincronizar(cantidad);
}); });
}, },
methods: { methods: {
notasEnChismosa() {
return this.producto.pivot !== undefined ? this.producto.pivot.notas : "";
},
cantidadEnChismosa() {
return this.producto.pivot !== undefined ? this.producto.pivot.cantidad : 0;
},
decrementar() { decrementar() {
this.cantidad -= 1; this.cantidad -= 1;
}, },
@ -55,26 +62,26 @@
this.cantidad += 1; this.cantidad += 1;
}, },
confirmar() { confirmar() {
Event.$emit('sync-subpedido', this.cantidad, this.producto.id); console.log("Emit sync " + this.cantidad + " " + this.notas);
Event.$emit('sync-subpedido', this.cantidad, this.producto.id, this.notas);
}, },
borrar() { borrar() {
this.cantidad = 0; this.cantidad = 0;
this.confirmar(); this.confirmar();
}, },
sincronizar(cantidad) { sincronizar(cantidad, notas) {
this.notas = notas;
this.cantidad = cantidad; this.cantidad = cantidad;
if (this.producto.pivot != null) { if (this.producto.pivot !== undefined) {
this.producto.pivot.cantidad = cantidad; this.producto.pivot.cantidad = cantidad;
} else { this.producto.pivot.notas = notas;
this.producto.cantidad = cantidad;
} }
this.enChismosa = cantidad;
}, },
hayCambios() { hayCambios() {
return this.cantidad != this.enChismosa; return this.cantidad != this.cantidadEnChismosa() || this.notas != this.notasEnChismosa();
}, },
puedeBorrar() { puedeBorrar() {
return this.enChismosa > 0; return this.cantidadEnChismosa() > 0;
}, },
} }
} }

View File

@ -38,8 +38,9 @@ export default {
}).then(response => { }).then(response => {
this.productos = response.data.data; this.productos = response.data.data;
this.productos.forEach(p => { this.productos.forEach(p => {
p.cantidad = this.$root.cantidad(p); p.pivot = {};
p.notas = this.$root.notas(p); p.pivot.cantidad = this.$root.cantidad(p);
p.pivot.notas = this.$root.notas(p);
}); });
}); });
this.visible = true; this.visible = true;