diff --git a/api/index.php b/api/index.php index 7b167e9..1813945 100644 --- a/api/index.php +++ b/api/index.php @@ -456,7 +456,42 @@ function useFromInventory(PDO $db): void { $stmt = $db->prepare("INSERT INTO transactions (product_id, type, quantity, location) VALUES (?, 'out', ?, ?)"); $stmt->execute([$productId, $quantity, $location]); - echo json_encode(['success' => true, 'remaining' => $newQty]); + // Auto-add to Bring! if product is completely finished (no inventory left anywhere) + $addedToBring = false; + if ($newQty <= 0) { + $stmt = $db->prepare("SELECT SUM(quantity) as total FROM inventory WHERE product_id = ? AND quantity > 0"); + $stmt->execute([$productId]); + $totalLeft = (float)($stmt->fetchColumn() ?: 0); + + if ($totalLeft <= 0) { + // Get product name and brand for Bring! + $stmt = $db->prepare("SELECT name, brand FROM products WHERE id = ?"); + $stmt->execute([$productId]); + $product = $stmt->fetch(); + + if ($product) { + try { + $auth = bringAuth(); + if ($auth) { + $listUUID = $auth['bringListUUID']; + $bringName = italianToBring($product['name']); + $spec = $product['brand'] ?: ''; + $body = http_build_query([ + 'uuid' => $listUUID, + 'purchase' => $bringName, + 'specification' => $spec, + ]); + $result = bringRequest('PUT', "https://api.getbring.com/rest/v2/bringlists/{$listUUID}", $body); + $addedToBring = ($result !== null); + } + } catch (Exception $e) { + // Silently fail — don't block inventory operation + } + } + } + } + + echo json_encode(['success' => true, 'remaining' => $newQty, 'added_to_bring' => $addedToBring]); } function updateInventory(PDO $db): void { diff --git a/assets/js/app.js b/assets/js/app.js index a2f89dd..66e9e6c 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -1845,6 +1845,9 @@ async function submitUseAll() { showLoading(false); if (result.success) { showToast(`📤 ${currentProduct.name} terminato!`, 'success'); + if (result.added_to_bring) { + setTimeout(() => showToast('🛒 Prodotto finito → aggiunto a Bring!', 'info'), 1500); + } showPage('dashboard'); } else { showToast(result.error || 'Errore', 'error'); @@ -1868,6 +1871,9 @@ async function submitUse(e) { showLoading(false); if (result.success) { showToast(`📤 Usato ${qty} di ${currentProduct.name}. Rimasti: ${result.remaining}`, 'success'); + if (result.added_to_bring) { + setTimeout(() => showToast('🛒 Prodotto finito → aggiunto a Bring!', 'info'), 1500); + } showPage('dashboard'); } else { showToast(result.error || 'Errore', 'error'); @@ -2701,6 +2707,9 @@ async function useRecipeIngredient(idx, productId, location, qtyNumber, btn) { } catch (e) { /* ignore */ } showToast('📦 Ingrediente scalato dalla dispensa!', 'success'); + if (result.added_to_bring) { + setTimeout(() => showToast('🛒 Prodotto finito → aggiunto a Bring!', 'info'), 1500); + } } else { btn.disabled = false; btn.textContent = '📦 Usa'; diff --git a/data/dispensa.db b/data/dispensa.db index 7174fc7..af5d735 100644 Binary files a/data/dispensa.db and b/data/dispensa.db differ