-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpurchase_management.php.bak
156 lines (137 loc) · 6.5 KB
/
purchase_management.php.bak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
session_start();
require_once 'config.php';
require_once 'auth.php';
requireLogin();
$user_id = getCurrentUserId();
// 檢查表是否存在
$tables = ["purchase_orders", "purchase_order_items"];
foreach ($tables as $table) {
$result = $conn->query("SHOW TABLES LIKE '".$table."'");
if($result->num_rows == 0) {
die("錯誤:表 ".$table." 不存在。請確保已正確創建所有必要的數據庫表。");
}
}
$status_options = [
"pending" => "待處理",
"approved" => "已批准",
"shipped" => "已發貨",
"received" => "已收貨",
"cancelled" => "已取消"
];
// 處理創建新採購訂單
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['create_order'])) {
// ... [保留原有的訂單創建邏輯] ...
}
// 獲取採購訂單列表
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = 10;
$start = ($page - 1) * $perPage;
$stmt = $conn->prepare("SELECT po.*, s.name as supplier_name FROM purchase_orders po
JOIN suppliers s ON po.supplier_id = s.id
WHERE po.user_id = ?
ORDER BY po.order_date DESC
LIMIT ? OFFSET ?");
$stmt->bind_param("iii", $user_id, $perPage, $start);
$stmt->execute();
$result = $stmt->get_result();
$orders = $result->fetch_all(MYSQLI_ASSOC);
// 獲取總訂單數量
$totalStmt = $conn->prepare("SELECT COUNT(*) as total FROM purchase_orders WHERE user_id = ?");
$totalStmt->bind_param("i", $user_id);
$totalStmt->execute();
$totalResult = $totalStmt->get_result();
$totalRow = $totalResult->fetch_assoc();
$totalOrders = $totalRow['total'];
$totalPages = ceil($totalOrders / $perPage);
// 獲取供應商列表(用於創建新訂單)
$supplierStmt = $conn->prepare("SELECT id, name FROM suppliers WHERE user_id = ?");
$supplierStmt->bind_param("i", $user_id);
$supplierStmt->execute();
$supplierResult = $supplierStmt->get_result();
$suppliers = $supplierResult->fetch_all(MYSQLI_ASSOC);
// 獲取產品列表(用於創建新訂單)
$productStmt = $conn->prepare("SELECT id, name, purchase_price FROM products WHERE user_id = ?");
$productStmt->bind_param("i", $user_id);
$productStmt->execute();
$productResult = $productStmt->get_result();
$products = $productResult->fetch_all(MYSQLI_ASSOC);
?>
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>採購管理 - 庫存管理系統</title>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-gray-100">
<?php include "navbar.php"; ?>
<div class="container mx-auto p-6">
<h1 class="text-3xl font-bold mb-4">採購管理</h1>
<?php if (isset($_SESSION['success_message'])): ?>
<div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded relative mb-4" role="alert">
<span class="block sm:inline"><?php echo $_SESSION['success_message']; ?></span>
</div>
<?php unset($_SESSION['success_message']); ?>
<?php endif; ?>
<?php if (isset($_SESSION['error_message'])): ?>
<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative mb-4" role="alert">
<span class="block sm:inline"><?php echo $_SESSION['error_message']; ?></span>
</div>
<?php unset($_SESSION['error_message']); ?>
<?php endif; ?>
<!-- 創建新採購訂單按鈕 -->
<button onclick="openCreateOrderModal()" class="mb-4 bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded">
創建新採購訂單
</button>
<!-- 採購訂單列表 -->
<table class="min-w-full bg-white">
<thead>
<tr>
<th class="py-2 px-4 border-b">訂單ID</th>
<th class="py-2 px-4 border-b">供應商</th>
<th class="py-2 px-4 border-b">訂單日期</th>
<th class="py-2 px-4 border-b">總金額</th>
<th class="py-2 px-4 border-b">狀態</th>
<th class="py-2 px-4 border-b">操作</th>
</tr>
</thead>
<tbody>
<?php foreach ($orders as $order): ?>
<tr>
<td class="py-2 px-4 border-b"><?php echo htmlspecialchars($order['id']); ?></td>
<td class="py-2 px-4 border-b"><?php echo htmlspecialchars($order['supplier_name']); ?></td>
<td class="py-2 px-4 border-b"><?php echo htmlspecialchars($order['order_date']); ?></td>
<td class="py-2 px-4 border-b"><?php echo htmlspecialchars($order['total_amount']); ?></td>
<td class="py-2 px-4 border-b"><?php echo htmlspecialchars($status_options[$order['status']]); ?></td>
<td class="py-2 px-4 border-b">
<a href="generate_purchase_order_image.php?id=<?php echo $order['id']; ?>" target="_blank" class="bg-green-500 hover:bg-green-700 text-white font-bold py-1 px-2 rounded mr-1">下載圖片</a>
<a href="view_purchase_order.php?id=<?php echo $order['id']; ?>" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-1 px-2 rounded mr-1">查看詳情</a>
<select onchange="updateOrderStatus(<?php echo $order['id']; ?>, this.value)" class="bg-yellow-500 text-white py-1 px-2 rounded">
<?php foreach ($status_options as $value => $label): ?>
<option value="<?php echo $value; ?>" <?php echo $order['status'] === $value ? 'selected' : ''; ?>>
<?php echo $label; ?>
</option>
<?php endforeach; ?>
</select>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<!-- 分頁 -->
<div class="mt-4">
<?php for ($i = 1; $i <= $totalPages; $i++): ?>
<a href="?page=<?php echo $i; ?>"
class="inline-block bg-blue-500 text-white px-3 py-1 rounded <?php echo $i === $page ? 'bg-blue-700' : ''; ?>">
<?php echo $i; ?>
</a>
<?php endfor; ?>
</div>
</div>
<!-- 創建新採購訂單模態框 -->
</body>
</html>