From 77ed2d6964232f04c5417de90acadfbe64c55bd8 Mon Sep 17 00:00:00 2001 From: dadaloop82 Date: Tue, 10 Mar 2026 18:18:40 +0000 Subject: [PATCH] feat: auto-aggiunta a Bring! quando un prodotto finisce MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - useFromInventory controlla se il prodotto รจ esaurito ovunque (qty=0 in tutte le location) - Se finito, lo aggiunge automaticamente alla lista Bring! con nome e marca - Toast notifica '๐Ÿ›’ Prodotto finito โ†’ aggiunto a Bring!' in tutte le UI (ricetta, uso diretto, usa tutto) - Prosciutto crudo light e Philadelphia Light aggiunti manualmente a Bring! --- api/index.php | 37 ++++++++++++++++++++++++++++++++++++- assets/js/app.js | 9 +++++++++ data/dispensa.db | Bin 73728 -> 81920 bytes 3 files changed, 45 insertions(+), 1 deletion(-) 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 7174fc7c2777cda42c8e20de072c00fef6347f10..af5d73526e64d475e63b99c82a5603f1760593d6 100644 GIT binary patch delta 461 zcmZoTz|zpbIzgJ1gMooTWTJvSBge*siS~?%n?Kp}3h-;N0)-j*e*;Md5&qws1s&e< zgM@_RHzhC%u<>g#`0$xbW-pk@!l%clw^>o3ix(uw#=tPyr0yG#0y7QjFS76~VepyE z+Ay1i_ag5_pu%46$%Z|}T)Y<Pw=ylv;WQ#lw_ z85eA9tYF=IG}`76k}Q+Q7oW#J`Dw|0e$? vpkJ2p`>7^!Ffa-;it`!==9iY(q!neR=c73mTtXrh8WBm2gLiS~@~n?Kp}3NTtPQ(zR>l)xy!29);W)0@m* zFq4H}i%)N}qCgif8!Iy-W1#AAC+uT<0Mx?7yZOXi8=x4w6lYX4HVy>4!p6wJ$V}J3Sl7r1#xgWeFf_9= pG_^7?#v)|z2Qwf5Y=ACI4Tb?GR)*#