Add: mostra totale aggiornato nel toast + auto-rimuovi da Bring dopo acquisto
This commit is contained in:
+48
-1
@@ -521,16 +521,63 @@ function addToInventory(PDO $db): void {
|
|||||||
$stmt = $db->prepare("UPDATE inventory SET quantity = ?, expiry_date = COALESCE(?, expiry_date), updated_at = CURRENT_TIMESTAMP WHERE id = ?");
|
$stmt = $db->prepare("UPDATE inventory SET quantity = ?, expiry_date = COALESCE(?, expiry_date), updated_at = CURRENT_TIMESTAMP WHERE id = ?");
|
||||||
$stmt->execute([$newQty, $expiry, $existing['id']]);
|
$stmt->execute([$newQty, $expiry, $existing['id']]);
|
||||||
} else {
|
} else {
|
||||||
|
$newQty = $quantity;
|
||||||
// Insert new inventory entry
|
// Insert new inventory entry
|
||||||
$stmt = $db->prepare("INSERT INTO inventory (product_id, location, quantity, expiry_date) VALUES (?, ?, ?, ?)");
|
$stmt = $db->prepare("INSERT INTO inventory (product_id, location, quantity, expiry_date) VALUES (?, ?, ?, ?)");
|
||||||
$stmt->execute([$productId, $location, $quantity, $expiry]);
|
$stmt->execute([$productId, $location, $quantity, $expiry]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get total across all locations
|
||||||
|
$stmt = $db->prepare("SELECT SUM(quantity) FROM inventory WHERE product_id = ? AND quantity > 0");
|
||||||
|
$stmt->execute([$productId]);
|
||||||
|
$totalQty = (float)($stmt->fetchColumn() ?: $newQty);
|
||||||
|
|
||||||
|
// Get product unit info for display
|
||||||
|
$stmt = $db->prepare("SELECT unit, default_quantity, package_unit FROM products WHERE id = ?");
|
||||||
|
$stmt->execute([$productId]);
|
||||||
|
$prodInfo = $stmt->fetch();
|
||||||
|
|
||||||
// Log transaction
|
// Log transaction
|
||||||
$stmt = $db->prepare("INSERT INTO transactions (product_id, type, quantity, location) VALUES (?, 'in', ?, ?)");
|
$stmt = $db->prepare("INSERT INTO transactions (product_id, type, quantity, location) VALUES (?, 'in', ?, ?)");
|
||||||
$stmt->execute([$productId, $quantity, $location]);
|
$stmt->execute([$productId, $quantity, $location]);
|
||||||
|
|
||||||
echo json_encode(['success' => true]);
|
// Auto-remove from Bring! if product is on the shopping list
|
||||||
|
$removedFromBring = false;
|
||||||
|
try {
|
||||||
|
$stmt = $db->prepare("SELECT name FROM products WHERE id = ?");
|
||||||
|
$stmt->execute([$productId]);
|
||||||
|
$prodName = $stmt->fetchColumn();
|
||||||
|
if ($prodName) {
|
||||||
|
$auth = bringAuth();
|
||||||
|
if ($auth) {
|
||||||
|
$listUUID = $auth['bringListUUID'];
|
||||||
|
$bringKey = italianToBring($prodName);
|
||||||
|
$listData = bringRequest('GET', "https://api.getbring.com/rest/v2/bringlists/{$listUUID}");
|
||||||
|
if ($listData && isset($listData['purchase'])) {
|
||||||
|
foreach ($listData['purchase'] as $item) {
|
||||||
|
if (strcasecmp($item['name'] ?? '', $bringKey) === 0) {
|
||||||
|
$body = http_build_query(['uuid' => $listUUID, 'remove' => $bringKey]);
|
||||||
|
bringRequest('PUT', "https://api.getbring.com/rest/v2/bringlists/{$listUUID}", $body);
|
||||||
|
$removedFromBring = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception $e) {
|
||||||
|
// Silently fail
|
||||||
|
}
|
||||||
|
|
||||||
|
echo json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'new_qty' => $newQty,
|
||||||
|
'total_qty' => $totalQty,
|
||||||
|
'unit' => $prodInfo['unit'] ?? 'pz',
|
||||||
|
'default_quantity' => (float)($prodInfo['default_quantity'] ?? 0),
|
||||||
|
'package_unit' => $prodInfo['package_unit'] ?? null,
|
||||||
|
'removed_from_bring' => $removedFromBring,
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
function useFromInventory(PDO $db): void {
|
function useFromInventory(PDO $db): void {
|
||||||
|
|||||||
+17
-1
@@ -2948,7 +2948,23 @@ async function submitAdd(e) {
|
|||||||
|
|
||||||
showLoading(false);
|
showLoading(false);
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
showToast(`✅ ${currentProduct.name} aggiunto!`, 'success');
|
// Build quantity info for toast
|
||||||
|
let qtyInfo = '';
|
||||||
|
if (result.total_qty) {
|
||||||
|
const u = result.unit || 'pz';
|
||||||
|
const unitLabels = { 'pz': 'pz', 'kg': 'kg', 'g': 'g', 'l': 'L', 'ml': 'ml', 'conf': 'conf' };
|
||||||
|
const uLabel = unitLabels[u] || u;
|
||||||
|
if (u === 'conf' && result.package_unit && result.default_quantity > 0) {
|
||||||
|
const pkgLabel = unitLabels[result.package_unit] || result.package_unit;
|
||||||
|
qtyInfo = ` (totale: ${result.total_qty} ${uLabel} da ${result.default_quantity}${pkgLabel})`;
|
||||||
|
} else {
|
||||||
|
qtyInfo = ` (totale: ${result.total_qty} ${uLabel})`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
showToast(`✅ ${currentProduct.name} aggiunto!${qtyInfo}`, 'success');
|
||||||
|
if (result.removed_from_bring) {
|
||||||
|
setTimeout(() => showToast('🛒 Rimosso dalla lista della spesa', 'info'), 1500);
|
||||||
|
}
|
||||||
if (!spesaModeAfterAdd()) showPage('dashboard');
|
if (!spesaModeAfterAdd()) showPage('dashboard');
|
||||||
} else {
|
} else {
|
||||||
showToast(result.error || 'Errore', 'error');
|
showToast(result.error || 'Errore', 'error');
|
||||||
|
|||||||
Binary file not shown.
+1
-1
@@ -884,6 +884,6 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="assets/js/app.js?v=20260315a"></script>
|
<script src="assets/js/app.js?v=20260315b"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user