untitled

Pasted Nov 10, 2026, 6:39 PM1 file1,059 lines

Scan to open on your phone

Point a camera at the QR code - it opens this exact paste, no app needed.

https://codepastes.com/zzgs85gzvkbmth80
untitled
package com.negocio.desinfectantes.ui.produccion

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.negocio.desinfectantes.data.*
import java.text.NumberFormat
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

private val LoteNavy = Color(0xFF101D35)
private val LoteCardBlue = Color(0xFF1B2D4A)
private val LoteAccentBlue = Color(0xFF2585F5)
private val LoteSoftBlue = Color(0xFFA9BEDB)
private val LoteWhite = Color(0xFFFFFFFF)

private data class FilaEdicionLote(
    val presentacionId: String,
    val cantidadFabricada: String,
    val barrancas: String,
    val valledupar: String
)

@Composable
fun HistorialLotesScreen(
    modifier: Modifier = Modifier,
    lotes: List<LoteProduccionEntity>,
    detalles: List<LoteDetalleEntity>,
    gastos: List<LoteGastoEntity>,
    inventariosLote: List<LoteInventarioEntity>,
    ingresosPorLoteYSede: List<IngresoLoteSedeResultado> = emptyList(),
    productos: List<ProductoEntity>,
    aromas: List<AromaEntity>,
    presentaciones: List<PresentacionEntity>,
    onEditarGasto: (
        gastoId: String,
        loteId: String,
        concepto: String,
        monto: Double,
        observacion: String?
    ) -> Unit,
    onAgregarGasto: (LoteGastoEntity) -> Unit,
    onEliminarGasto: (
        gastoId: String,
        loteId: String
    ) -> Unit,
    onEditarProduccion: (
        loteId: String,
        detalles: List<LoteDetalleEntity>,
        inventarios: List<LoteInventarioEntity>,
        onResultado: (String?) -> Unit
    ) -> Unit,
    onVolver: () -> Unit
) {
    var loteEnEdicion by remember {
        mutableStateOf<LoteProduccionEntity?>(null)
    }

    val filasEdicionLote = remember {
        mutableStateListOf<FilaEdicionLote>()
    }

    var presentacionParaAgregar by remember {
        mutableStateOf("")
    }
    var mostrarSelectorPresentacion by remember {
        mutableStateOf(false)
    }

    var errorEdicionLote by remember {
        mutableStateOf<String?>(null)
    }

    var gastoEnEdicion by remember {
        mutableStateOf<LoteGastoEntity?>(null)
    }

    var gastoAEliminar by remember {
        mutableStateOf<LoteGastoEntity?>(null)
    }

    var conceptoEditado by remember {
        mutableStateOf("")
    }

    var montoEditado by remember {
        mutableStateOf("")
    }

    var observacionEditada by remember {
        mutableStateOf("")
    }
    var loteParaNuevoGasto by remember {
        mutableStateOf<LoteProduccionEntity?>(null)
    }

    var nuevoConcepto by remember {
        mutableStateOf("")
    }

    var nuevoMonto by remember {
        mutableStateOf("")
    }

    var nuevaObservacion by remember {
        mutableStateOf("")
    }

    fun abrirEditorProduccion(lote: LoteProduccionEntity) {
        filasEdicionLote.clear()

        val detallesDelLote = detalles.filter {
            it.loteId == lote.id
        }

        for (detalle in detallesDelLote) {
            val inventariosDePresentacion = inventariosLote.filter {
                it.loteId == lote.id &&
                        it.presentacionId == detalle.presentacionId
            }

            val cantidadBarrancas = inventariosDePresentacion
                .filter { it.sede == "Barrancas" }
                .sumOf { it.cantidadDisponible }

            val cantidadValledupar = inventariosDePresentacion
                .filter { it.sede == "Valledupar" }
                .sumOf { it.cantidadDisponible }

            filasEdicionLote.add(
                FilaEdicionLote(
                    presentacionId = detalle.presentacionId,
                    cantidadFabricada = detalle.cantidadFabricada.toString(),
                    barrancas = cantidadBarrancas.toString(),
                    valledupar = cantidadValledupar.toString()
                )
            )
        }

        presentacionParaAgregar = ""
        loteEnEdicion = lote
    }
    val formatoFecha = SimpleDateFormat(
        "dd/MM/yyyy • hh:mm a",
        Locale("es", "CO")
    )

    val formatoPesos = NumberFormat.getCurrencyInstance(
        Locale("es", "CO")
    ).apply {
        maximumFractionDigits = 0
        minimumFractionDigits = 0
    }

    Column(
        modifier = modifier
            .fillMaxSize()
            .background(LoteNavy)
            .verticalScroll(rememberScrollState())
            .padding(20.dp),
        verticalArrangement = Arrangement.spacedBy(16.dp)
    ) {
        TextButton(onClick = onVolver) {
            Text("← Volver", color = LoteSoftBlue)
        }

        Text(
            "Historial de lotes",
            color = LoteWhite,
            style = MaterialTheme.typography.headlineMedium,
            fontWeight = FontWeight.Bold
        )

        Text(
            "Consulta las producciones y sus gastos registrados.",
            color = LoteSoftBlue
        )

        if (lotes.isEmpty()) {
            Card(
                modifier = Modifier.fillMaxWidth(),
                colors = CardDefaults.cardColors(
                    containerColor = LoteCardBlue
                ),
                shape = RoundedCornerShape(16.dp)
            ) {
                Column(
                    modifier = Modifier.padding(20.dp),
                    verticalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    Text(
                        "Todavía no hay lotes registrados",
                        color = LoteWhite,
                        fontWeight = FontWeight.Bold
                    )
                    Text(
                        "Cuando registres una producción, aparecerá aquí.",
                        color = LoteSoftBlue
                    )
                }
            }
        }

        lotes.forEach { lote ->
            val producto = productos.firstOrNull {
                it.id == lote.productoId
            }

            val detallesLote = detalles.filter {
                it.loteId == lote.id
            }

            val aromasLote = detallesLote
                .mapNotNull { detalle ->
                    presentaciones.firstOrNull {
                        it.id == detalle.presentacionId
                    }?.aromaId
                }
                .distinct()
                .mapNotNull { aromaId ->
                    aromas.firstOrNull { it.id == aromaId }
                }

            val gastosLote = gastos.filter {
                it.loteId == lote.id
            }

            val inventarios = inventariosLote.filter {
                it.loteId == lote.id
            }

            val totalUnidades = detallesLote.sumOf {
                it.cantidadFabricada
            }

            val totalGastos = gastosLote.sumOf {
                it.monto
            }

            val ingresosLote = ingresosPorLoteYSede.filter {
                it.loteId == lote.id
            }

            val ingresosBarrancas = ingresosLote
                .filter { it.sede.equals("Barrancas", ignoreCase = true) }
                .sumOf { it.ingresos }

            val ingresosValledupar = ingresosLote
                .filter { it.sede.equals("Valledupar", ignoreCase = true) }
                .sumOf { it.ingresos }

            val ingresosTotales = ingresosBarrancas + ingresosValledupar

            val gananciaLote = ingresosTotales - totalGastos

            Card(
                modifier = Modifier.fillMaxWidth(),
                colors = CardDefaults.cardColors(
                    containerColor = LoteCardBlue
                ),
                shape = RoundedCornerShape(18.dp)
            ) {
                Column(
                    modifier = Modifier.padding(16.dp),
                    verticalArrangement = Arrangement.spacedBy(12.dp)
                ) {
                    Text(
                        lote.numeroLote,
                        color = LoteAccentBlue,
                        style = MaterialTheme.typography.titleMedium,
                        fontWeight = FontWeight.Bold
                    )
                    Text(
                        producto?.nombre ?: "Producto no encontrado",
                        color = LoteWhite,
                        style = MaterialTheme.typography.titleLarge,
                        fontWeight = FontWeight.Bold
                    )

                    Text(
                        "Aromas: ${
                            aromasLote.joinToString(", ") { it.nombre }
                                .ifBlank { "No encontrados" }
                        }",
                        color = LoteSoftBlue
                    )
                    Text(
                        "Fecha: ${formatoFecha.format(Date(lote.fechaCreacion))}",
                        color = LoteSoftBlue
                    )

                    HorizontalDivider(color = Color(0xFF40536F))

                    Text(
                        "Producción",
                        color = LoteAccentBlue,
                        fontWeight = FontWeight.Bold
                    )

                    detallesLote.forEach { detalle ->
                        val presentacion = presentaciones.firstOrNull {
                            it.id == detalle.presentacionId
                        }

                        val distribuciones = inventarios.filter {
                            it.presentacionId == detalle.presentacionId
                        }

                        Column(
                            verticalArrangement = Arrangement.spacedBy(5.dp)
                        ) {
                            Text(
                                presentacion?.nombre
                                    ?: "Presentación no encontrada",
                                color = LoteWhite,
                                fontWeight = FontWeight.SemiBold
                            )

                            Text(
                                "${detalle.cantidadFabricada} unidades fabricadas",
                                color = LoteSoftBlue
                            )


                            distribuciones.forEach { distribucion ->
                                Text(
                                    "• ${distribucion.sede}: ${distribucion.cantidadDisponible} unidades disponibles",
                                    color = LoteSoftBlue
                                )

                                if (distribucion.cantidadReservada > 0) {
                                    Text(
                                        "   ${distribucion.cantidadReservada} unidades reservadas",
                                        color = LoteAccentBlue
                                    )
                                }
                            }

                        }
                    }



                    Text(
                        "Gastos de producción",
                        color = LoteAccentBlue,
                        fontWeight = FontWeight.Bold
                    )

                    if (gastosLote.isEmpty()) {
                        Text(
                            "No se registraron gastos.",
                            color = LoteSoftBlue
                        )
                    } else {
                        gastosLote.forEach { gasto ->
                            Column(
                                verticalArrangement = Arrangement.spacedBy(6.dp)
                            ) {
                                Row(
                                    modifier = Modifier.fillMaxWidth(),
                                    horizontalArrangement = Arrangement.SpaceBetween
                                ) {
                                    Text(
                                        gasto.concepto,
                                        color = LoteWhite,
                                        modifier = Modifier.weight(1f)
                                    )

                                    Text(
                                        formatoPesos.format(gasto.monto),
                                        color = LoteWhite,
                                        fontWeight = FontWeight.SemiBold
                                    )
                                }

                                if (!gasto.observacion.isNullOrBlank()) {
                                    Text(
                                        gasto.observacion,
                                        color = LoteSoftBlue,
                                        style = MaterialTheme.typography.bodySmall
                                    )
                                }

                                Row(
                                    horizontalArrangement = Arrangement.spacedBy(12.dp)
                                ) {
                                    TextButton(
                                        onClick = {
                                            gastoEnEdicion = gasto
                                            conceptoEditado = gasto.concepto
                                            montoEditado = gasto.monto.toString()
                                            observacionEditada = gasto.observacion ?: ""
                                        }
                                    ) {
                                        Text("Editar", color = LoteAccentBlue)
                                    }

                                    TextButton(
                                        onClick = {
                                            gastoAEliminar = gasto
                                        }
                                    ) {
                                        Text("Eliminar", color = Color(0xFFFF6B6B))
                                    }
                                }
                            }
                        }
                    }
                    TextButton(
                        onClick = {
                            loteParaNuevoGasto = lote
                            nuevoConcepto = ""
                            nuevoMonto = ""
                            nuevaObservacion = ""
                        }
                    ) {
                        Text(
                            "+ Añadir gasto",
                            color = LoteAccentBlue,
                            fontWeight = FontWeight.Bold
                        )
                    }

                    HorizontalDivider(color = Color(0xFF40536F))

                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.SpaceBetween
                    ) {
                        Text(
                            "Total de gastos",
                            color = LoteSoftBlue
                        )
                        Text(
                            formatoPesos.format(totalGastos),
                            color = LoteWhite,
                            fontWeight = FontWeight.Bold
                        )
                    }
                    HorizontalDivider(color = Color(0xFF40536F))

                    Text(
                        "Rentabilidad del lote",
                        color = LoteAccentBlue,
                        fontWeight = FontWeight.Bold
                    )

                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.SpaceBetween
                    ) {
                        Text("Ingresos Barrancas", color = LoteSoftBlue)
                        Text(
                            formatoPesos.format(ingresosBarrancas),
                            color = LoteWhite
                        )
                    }

                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.SpaceBetween
                    ) {
                        Text("Ingresos Valledupar", color = LoteSoftBlue)
                        Text(
                            formatoPesos.format(ingresosValledupar),
                            color = LoteWhite
                        )
                    }

                    HorizontalDivider(color = Color(0xFF40536F))

                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.SpaceBetween
                    ) {
                        Text(
                            "Ingresos totales",
                            color = LoteWhite,
                            fontWeight = FontWeight.Bold
                        )
                        Text(
                            formatoPesos.format(ingresosTotales),
                            color = LoteWhite,
                            fontWeight = FontWeight.Bold
                        )
                    }

                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.SpaceBetween
                    ) {
                        Text(
                            if (gananciaLote >= 0) "Ganancia del lote" else "Pérdida del lote",
                            color = LoteWhite,
                            fontWeight = FontWeight.Bold
                        )
                        Text(
                            formatoPesos.format(gananciaLote),
                            color = if (gananciaLote >= 0) Color(0xFF4ADE80)
                            else Color(0xFFFF6B6B),
                            fontWeight = FontWeight.Bold
                        )
                    }

                    HorizontalDivider(color = Color(0xFF40536F))

                    TextButton(
                        onClick = {
                            abrirEditorProduccion(lote)
                        },
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        Text(
                            "✏️ Editar producción",
                            color = LoteAccentBlue,
                            fontWeight = FontWeight.Bold
                        )
                    }



                }
            }
        }

        Spacer(modifier = Modifier.height(20.dp))
    }
    gastoEnEdicion?.let { gasto ->
        AlertDialog(
            onDismissRequest = {
                gastoEnEdicion = null
            },
            title = {
                Text("Editar gasto")
            },
            text = {
                Column(
                    verticalArrangement = Arrangement.spacedBy(12.dp)
                ) {
                    OutlinedTextField(
                        value = conceptoEditado,
                        onValueChange = {
                            conceptoEditado = it
                        },
                        label = { Text("Concepto") },
                        singleLine = true
                    )

                    OutlinedTextField(
                        value = montoEditado,
                        onValueChange = {
                            montoEditado = it
                        },
                        label = { Text("Monto") },
                        singleLine = true
                    )

                    OutlinedTextField(
                        value = observacionEditada,
                        onValueChange = {
                            observacionEditada = it
                        },
                        label = { Text("Observación (opcional)") }
                    )
                }
            },
            confirmButton = {
                TextButton(
                    onClick = {
                        val monto = montoEditado
                            .replace(",", ".")
                            .toDoubleOrNull()

                        if (
                            conceptoEditado.isNotBlank() &&
                            monto != null &&
                            monto >= 0
                        ) {
                            onEditarGasto(
                                gasto.id,
                                gasto.loteId,
                                conceptoEditado.trim(),
                                monto,
                                observacionEditada.trim().ifBlank { null }
                            )

                            gastoEnEdicion = null
                        }
                    }
                ) {
                    Text("Guardar")
                }
            },
            dismissButton = {
                TextButton(
                    onClick = {
                        gastoEnEdicion = null
                    }
                ) {
                    Text("Cancelar")
                }
            },
            containerColor = LoteCardBlue,
            titleContentColor = LoteWhite,
            textContentColor = LoteWhite
        )
    }
    gastoAEliminar?.let { gasto ->
        AlertDialog(
            onDismissRequest = {
                gastoAEliminar = null
            },
            title = {
                Text("Eliminar gasto")
            },
            text = {
                Text(
                    "¿Estás seguro de que deseas eliminar el gasto \"${gasto.concepto}\" por ${formatoPesos.format(gasto.monto)}? Esta acción no se puede deshacer."
                )
            },
            confirmButton = {
                TextButton(
                    onClick = {
                        onEliminarGasto(
                            gasto.id,
                            gasto.loteId
                        )
                        gastoAEliminar = null
                    }
                ) {
                    Text(
                        "Eliminar",
                        color = Color(0xFFFF6B6B)
                    )
                }
            },
            dismissButton = {
                TextButton(
                    onClick = {
                        gastoAEliminar = null
                    }
                ) {
                    Text("Cancelar")
                }
            },
            containerColor = LoteCardBlue,
            titleContentColor = LoteWhite,
            textContentColor = LoteWhite
        )
    }
    loteParaNuevoGasto?.let { lote ->
        AlertDialog(
            onDismissRequest = {
                loteParaNuevoGasto = null
            },
            title = {
                Text("Añadir gasto a ${lote.numeroLote}")
            },
            text = {
                Column(
                    verticalArrangement = Arrangement.spacedBy(12.dp)
                ) {
                    OutlinedTextField(
                        value = nuevoConcepto,
                        onValueChange = {
                            nuevoConcepto = it
                        },
                        label = { Text("Concepto") },
                        singleLine = true
                    )

                    OutlinedTextField(
                        value = nuevoMonto,
                        onValueChange = {
                            nuevoMonto = it
                        },
                        label = { Text("Monto") },
                        singleLine = true
                    )

                    OutlinedTextField(
                        value = nuevaObservacion,
                        onValueChange = {
                            nuevaObservacion = it
                        },
                        label = { Text("Observación (opcional)") }
                    )
                }
            },
            confirmButton = {
                TextButton(
                    onClick = {
                        val monto = nuevoMonto
                            .replace(",", ".")
                            .toDoubleOrNull()

                        if (
                            nuevoConcepto.isNotBlank() &&
                            monto != null &&
                            monto >= 0
                        ) {
                            onAgregarGasto(
                                LoteGastoEntity(
                                    id = java.util.UUID.randomUUID().toString(),
                                    loteId = lote.id,
                                    concepto = nuevoConcepto.trim(),
                                    monto = monto,
                                    observacion = nuevaObservacion
                                        .trim()
                                        .ifBlank { null }
                                )
                            )

                            loteParaNuevoGasto = null
                        }
                    }
                ) {
                    Text("Guardar")
                }
            },
            dismissButton = {
                TextButton(
                    onClick = {
                        loteParaNuevoGasto = null
                    }
                ) {
                    Text("Cancelar")
                }
            },
            containerColor = LoteCardBlue,
            titleContentColor = LoteWhite,
            textContentColor = LoteWhite
        )
    }
    loteEnEdicion?.let { lote ->

        AlertDialog(
            onDismissRequest = {
                loteEnEdicion = null
                filasEdicionLote.clear()
            },
            title = {
                Text(
                    "Editar producción ${lote.numeroLote}",
                    color = LoteWhite,
                    fontWeight = FontWeight.Bold
                )
            },
            text = {
                Column {
                    Column(
                        modifier = Modifier
                            .heightIn(max = 400.dp)
                            .verticalScroll(rememberScrollState()),
                        verticalArrangement = Arrangement.spacedBy(10.dp)
                    ) {
                        Text(
                            "Modifica las cantidades fabricadas y su distribución.",
                            color = LoteWhite
                        )
                        errorEdicionLote?.let { mensaje ->
                            Text(
                                text = mensaje,
                                color = Color(0xFFFF6B6B),
                                fontWeight = FontWeight.Bold
                            )
                        }

                        filasEdicionLote.forEachIndexed { index, fila ->

                            val nombrePresentacion = presentaciones
                                .firstOrNull {
                                    it.id == fila.presentacionId
                                }?.nombre ?: "Presentación"

                            HorizontalDivider(
                                color = Color(0xFF40536F)
                            )

                            Text(
                                nombrePresentacion,
                                color = LoteAccentBlue,
                                fontWeight = FontWeight.Bold
                            )

                            OutlinedTextField(
                                value = fila.cantidadFabricada,
                                onValueChange = { nuevoValor ->
                                    if (nuevoValor.all { it.isDigit() }) {
                                        filasEdicionLote[index] =
                                            fila.copy(cantidadFabricada = nuevoValor)
                                    }
                                },
                                label = { Text("Cantidad fabricada") },
                                modifier = Modifier.fillMaxWidth(),
                                singleLine = true
                            )

                            OutlinedTextField(
                                value = fila.barrancas,
                                onValueChange = { nuevoValor ->
                                    if (nuevoValor.all { it.isDigit() }) {
                                        filasEdicionLote[index] =
                                            fila.copy(barrancas = nuevoValor)
                                    }
                                },
                                label = { Text("Barrancas") },
                                modifier = Modifier.fillMaxWidth(),
                                singleLine = true
                            )

                            OutlinedTextField(
                                value = fila.valledupar,
                                onValueChange = { nuevoValor ->
                                    if (nuevoValor.all { it.isDigit() }) {
                                        filasEdicionLote[index] =
                                            fila.copy(valledupar = nuevoValor)
                                    }
                                },
                                label = { Text("Valledupar") },
                                modifier = Modifier.fillMaxWidth(),
                                singleLine = true
                            )
                            TextButton(
                                onClick = {
                                    filasEdicionLote.removeAt(index)
                                }
                            ) {
                                Text(
                                    "Quitar presentación",
                                    color = Color(0xFFFF6B6B)
                                )
                            }
                        }
                    }
                    Box {
                        TextButton(
                            onClick = {
                                mostrarSelectorPresentacion = true
                            }
                        ) {
                            Text(
                                "+ Agregar presentación",
                                color = LoteAccentBlue
                            )
                        }



                    }
                }
            },
            confirmButton = {
                TextButton(
                    onClick = {

                        errorEdicionLote = null

                        if (filasEdicionLote.isEmpty()) {
                            errorEdicionLote = "Debes agregar al menos una presentación."
                            return@TextButton
                        }

                        val detallesNuevos = mutableListOf<LoteDetalleEntity>()
                        val inventariosNuevos = mutableListOf<LoteInventarioEntity>()

                        for (fila in filasEdicionLote) {

                            val fabricada = fila.cantidadFabricada.toIntOrNull()
                            val barrancas = fila.barrancas.toIntOrNull()
                            val valledupar = fila.valledupar.toIntOrNull()

                            if (fabricada == null || barrancas == null || valledupar == null) {
                                errorEdicionLote = "Completa todas las cantidades."
                                return@TextButton
                            }

                            if (fabricada <= 0) {
                                errorEdicionLote = "La cantidad fabricada debe ser mayor que cero."
                                return@TextButton
                            }

                            if (barrancas + valledupar != fabricada) {
                                errorEdicionLote =
                                    "La distribución debe coincidir con la cantidad fabricada."
                                return@TextButton
                            }

                            val detalleExistente = detalles.firstOrNull {
                                it.loteId == lote.id &&
                                        it.presentacionId == fila.presentacionId
                            }

                            detallesNuevos.add(
                                LoteDetalleEntity(
                                    id = detalleExistente?.id
                                        ?: java.util.UUID.randomUUID().toString(),
                                    loteId = lote.id,
                                    presentacionId = fila.presentacionId,
                                    cantidadFabricada = fabricada
                                )
                            )

                            if (barrancas > 0) {
                                val inventarioExistente = inventariosLote.firstOrNull {
                                    it.loteId == lote.id &&
                                            it.presentacionId == fila.presentacionId &&
                                            it.sede == "Barrancas"
                                }

                                inventariosNuevos.add(
                                    LoteInventarioEntity(
                                        id = inventarioExistente?.id
                                            ?: java.util.UUID.randomUUID().toString(),
                                        loteId = lote.id,
                                        presentacionId = fila.presentacionId,
                                        sede = "Barrancas",
                                        cantidadDisponible = barrancas,
                                        cantidadReservada = 0
                                    )
                                )
                            }

                            if (valledupar > 0) {
                                val inventarioExistente = inventariosLote.firstOrNull {
                                    it.loteId == lote.id &&
                                            it.presentacionId == fila.presentacionId &&
                                            it.sede == "Valledupar"
                                }

                                inventariosNuevos.add(
                                    LoteInventarioEntity(
                                        id = inventarioExistente?.id
                                            ?: java.util.UUID.randomUUID().toString(),
                                        loteId = lote.id,
                                        presentacionId = fila.presentacionId,
                                        sede = "Valledupar",
                                        cantidadDisponible = valledupar,
                                        cantidadReservada = 0
                                    )
                                )
                            }
                        }

                        onEditarProduccion(
                            lote.id,
                            detallesNuevos,
                            inventariosNuevos
                        ) { resultado ->

                            if (resultado == null) {
                                loteEnEdicion = null
                                filasEdicionLote.clear()
                                errorEdicionLote = null
                            } else {
                                errorEdicionLote = resultado
                            }
                        }
                    }
                ) {
                    Text("Guardar cambios")
                }
            }
        )
    }
    if (mostrarSelectorPresentacion) {
        AlertDialog(
            onDismissRequest = {
                mostrarSelectorPresentacion = false
            },
            title = {
                Text("Agregar presentación")
            },
            text = {
                Column(
                    modifier = Modifier
                        .heightIn(max = 400.dp)
                        .verticalScroll(rememberScrollState()),
                    verticalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    val presentacionesDisponibles = presentaciones.filter { presentacion ->
                        presentacion.activo &&
                                aromas.any { aroma ->
                                    aroma.id == presentacion.aromaId &&
                                            aroma.productoId == loteEnEdicion?.productoId &&
                                            aroma.activo
                                } &&
                                filasEdicionLote.none { fila ->
                                    fila.presentacionId == presentacion.id
                                }
                    }

                    if (presentacionesDisponibles.isEmpty()) {
                        Text("No hay más presentaciones disponibles.")
                    }

                    presentacionesDisponibles.forEach { presentacion ->
                        val aromaNombre = aromas.firstOrNull {
                            it.id == presentacion.aromaId
                        }?.nombre ?: "Aroma desconocido"

                        TextButton(
                            onClick = {
                                filasEdicionLote.add(
                                    FilaEdicionLote(
                                        presentacionId = presentacion.id,
                                        cantidadFabricada = "0",
                                        barrancas = "0",
                                        valledupar = "0"
                                    )
                                )

                                presentacionParaAgregar = presentacion.id
                                mostrarSelectorPresentacion = false
                            },
                            modifier = Modifier.fillMaxWidth()
                        ) {
                            Column(
                                modifier = Modifier.fillMaxWidth()
                            ) {
                                Text(
                                    text = aromaNombre,
                                    color = LoteSoftBlue
                                )
                                Text(
                                    text = presentacion.nombre,
                                    color = Color.White
                                )
                            }
                        }
                    }
                }
            },
            confirmButton = {
                TextButton(
                    onClick = {
                        mostrarSelectorPresentacion = false
                    }
                ) {
                    Text("Cerrar")
                }
            }
        )
    }
}


39,295 chars · codepastes.com