init
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
require_once 'includes/db.php';
|
||||
require_once 'includes/auth.php';
|
||||
checkAuth();
|
||||
|
||||
header('Content-Type: application/json');
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
if (!$data || empty($data['action']) || empty($data['id'])) {
|
||||
die(json_encode(['success' => false, 'error' => 'Brak danych']));
|
||||
}
|
||||
|
||||
$id = (int)$data['id'];
|
||||
$action = $data['action'];
|
||||
|
||||
try {
|
||||
if ($action === 'archive') {
|
||||
$stmt = $pdo->prepare("UPDATE " . DB_PREFIX . "orders SET is_archived = 1 WHERE id = ?");
|
||||
$stmt->execute([$id]);
|
||||
|
||||
$hist = $pdo->prepare("INSERT INTO " . DB_PREFIX . "order_history (order_id, user_id, action) VALUES (?, ?, ?)");
|
||||
$hist->execute([$id, $_SESSION['user_id'], 'Przeniesiono zamówienie do archiwum.']);
|
||||
|
||||
echo json_encode(['success' => true]);
|
||||
|
||||
} elseif ($action === 'duplicate') {
|
||||
// Pobierz oryginał
|
||||
$stmt = $pdo->prepare("SELECT * FROM " . DB_PREFIX . "orders WHERE id = ?");
|
||||
$stmt->execute([$id]);
|
||||
$orig = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$orig) {
|
||||
die(json_encode(['success' => false, 'error' => 'Nie znaleziono zamówienia']));
|
||||
}
|
||||
|
||||
// Zapisz kopię (zawsze jako nowe, nie zarchiwizowane)
|
||||
$insert = $pdo->prepare("INSERT INTO " . DB_PREFIX . "orders
|
||||
(product_name, part_number, quantity, purchase_place, price_per_unit, delivery_date, notes, recipient, delivery_address, company, status, is_archived)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'nowe', 0)");
|
||||
|
||||
$insert->execute([
|
||||
$orig['product_name'] . ' (Kopia)',
|
||||
$orig['part_number'],
|
||||
$orig['quantity'],
|
||||
$orig['purchase_place'],
|
||||
$orig['price_per_unit'],
|
||||
$orig['delivery_date'],
|
||||
$orig['notes'],
|
||||
$orig['recipient'],
|
||||
$orig['delivery_address'],
|
||||
$orig['company']
|
||||
]);
|
||||
|
||||
$new_id = $pdo->lastInsertId();
|
||||
|
||||
$hist = $pdo->prepare("INSERT INTO " . DB_PREFIX . "order_history (order_id, user_id, action) VALUES (?, ?, ?)");
|
||||
$hist->execute([$new_id, $_SESSION['user_id'], "Sklonowano na podstawie archiwalnego/starego zamówienia #$id."]);
|
||||
|
||||
echo json_encode(['success' => true]);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'error' => 'Nieznana akcja']);
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user