code Copy code
// 先撈已分組的訂單主資料(基本欄位),可依需求加入更多欄位
$t->db->select('o.id as order_id, o.user_id, o.date_added, o.payment_method, o.payment_voucher, o.payment_date, o.delivery_time, o.address_id, a.name as order_recipient_person');
$t->db->from('orders o');
$t->db->join('addresses a', 'a.id = o.address_id', 'left');
if ($user_id !== null) {
$t->db->where('o.user_id', $user_id);
}
if ($start_date !== null && $end_date !== null) {
$t->db->where("DATE(o.date_added) >=", $start_date);
$t->db->where("DATE(o.date_added) <=", $end_date);
}
if ($limit !== null) {
$t->db->limit($limit, $offset);
}
$t->db->order_by($sort, $order);
$order_rows = $t->db->get()->result_array();
$orderData = [];
foreach ($order_rows as $order) {
// 一張訂單
$order_id = $order['id'];
// 撈該訂單所有商品明細,包含賣家資訊和商品資料
$t->db->select('oi.*, p.id as product_id, p.name as product_name, p.type as product_type, p.download_allowed, p.pickup_location, pv.price, pv.special_price, pv.id as variant_id, pv.image as variant_image, sd.store_name as seller_name, u.longitude as seller_longitude, u.latitude as seller_latitude, u.address as seller_address, u.mobile as seller_mobile, sd.logo as seller_icon');
$t->db->from('order_items oi');
$t->db->join('product_variants pv','pv.id = oi.product_variant_id','left');
$t->db->join('products p','p.id = pv.product_id','left');
$t->db->join('users u','u.id=oi.seller_id','left');
$t->db->join('seller_data sd','sd.user_id = oi.seller_id','left');
$t->db->where('oi.order_id', $order_id);
if ($seller_id !== null) {
$t->db->where('oi.seller_id', $seller_id);
}
$items = $t->db->get()->result_array();
// 根據賣家分組商品
$sellers = [];
foreach ($items as $item) {
$sid = $item['seller_id'];
if (!isset($sellers[$sid])) {
$sellers[$sid] = [
'seller_id' => $sid,
'seller_name' => $item['seller_name'] ?? null,
'seller_icon' => $item['seller_icon'] ?? null,
'orders' => [],
];
}
if(!isset($sellers[$sid]['orders'][$order_id])){
$order['payment_date'] = null;
if(!empty($order['method_refer_no'])){
$transactionData = fetch_details("transactions", ["payu_txn_id" => $order['method_refer_no']]);
if(count($transactionData) == 1){
$order['payment_date'] = $transactionData['transaction_date'];
}
}
$sellers[$sid]['orders'][$order_id] = [
'purchase_date' => $order['date_added'],
'order_number' => $order['id'],
'payment_method' => $order['payment_method'] ?? null,
'payment_voucher' => $order['method_refer_no'] ?? null,
'payment_date' => $order['payment_date'] ?? null,
'products' => []
]
}
$statusRecordsDB = fetch_details("order_status_records", ["orderItemId" => $item['id']]);
$statusRecords = [
0 => null,
1 => null,
2 => null,
3 => null,
4 => null,
5 => null
]
for($i = 0; $i < count($statusRecords); $i++){
if(checkStatusForOrderItemId($statusRecordsDB, $i, $item['id'])){
$statusRecords[$i] = $statusRecordsDB['date_created'];
}
}
$product = [
'id' => $item['id'],
'name' => $item['product_name'],
'spec' => $item['variant_name'],
'total_price' => floatval($item['sub_total']),
'quantity' => intval($item['quantity']),
'image' => $item['variant_image'] ?? null,
'status' => intval($item['itemStatus'] ?? 0), /
'eta' => $item['estimated_delivery_time'] ?? null, // TODO,需查詢欄位
'confirmed' => ($item['confirmed'] == 0 ? false : true) ?? false,
'status_times' => $statusRecords,
];
$sellers[$sid]['orders'][$order_id]['products'][] = $product;
}
}
return $orderData;
And I need to store the result as array to front-end which is using javascript for further usage,
and the data structure like:
let orderData = [
{
seller_id: "seller_001",
seller_name: "商店A",
seller_icon: "https://picsum.photos/100 ",
orders: [
{
purchase_date: "2025-06-19 10:00",
order_number: "ORD20250619001",
payment_method: "信用卡",
payment_voucher: "VOUCHER0001",
payment_date: "2025-06-19 10:30",
products: [
{
id: "p_101",
name: "商店A產品",
spec: "紅色 / L碼",
total_price: 350.5,
quantity: 1,
image: "https://picsum.photos/100 ",
status: 4,
eta: null,
confirmed: false,
status_times: {
0: "2025-06-18 08:57",
1: "2025-06-18 09:00",
2: "2025-06-18 11:00",
3: "2025-06-20 13:00",
4: "2025-06-21 15:00",
5: null,
},
},
{
id: "p_103",
name: "商店B產品",
spec: "白色 / M碼",
total_price: 521.5,
quantity: 1,
image: "https://picsum.photos/100 ",
status: 4,
eta: null,
confirmed: true,
status_times: {
0: "2025-06-18 08:57",
1: "2025-06-18 09:00",
2: "2025-06-18 11:00",
3: "2025-06-20 13:00",
4: "2025-06-21 15:00",
5: null,
},
},
],
},
{
purchase_date: "2025-06-17 09:00",
order_number: "ORD20250617001",
payment_method: "ATM轉帳",
payment_voucher: "VOUCHER0002",
payment_date: "2025-06-17 09:30",
products: [
{
id: "p_102",
name: "商店C產品",
spec: "藍色 / XL碼",
total_price: 999.9,
quantity: 2,
image: "https://picsum.photos/100 ",
status: 2,
eta: "2025-06-22 14:00",
confirmed: false,
status_times: {
0: "2025-06-17 09:00",
1: "2025-06-18 11:00",
2: "2025-06-19 08:30",
3: null,
4: null,
},
},
],
},
],
},
{
seller_id: "seller_002",
seller_name: "商店B",
seller_icon: "https://picsum.photos/100 ",
orders: [
{
purchase_date: "2025-06-17 08:00",
order_number: "ORD20250617002",
payment_method: "信用卡",
payment_voucher: "VOUCHER0003",
payment_date: "2025-06-17 08:15",
products: [
{
id: "p_201",
name: "商店B產品",
spec: "綠色 / M碼",
total_price: 150.5,
quantity: 3,
image: "https://picsum.photos/100 ",
status: 3,
eta: "2025-06-20 16:00",
confirmed: false,
status_times: {
0: "2025-06-17 08:00",
1: "2025-06-17 09:00",
2: "2025-06-18 10:00",
3: "2025-06-19 16:00",
4: null,
5: null,
},
},
],
},
],
},
{
seller_id: "seller_003",
seller_name: "網上商店",
seller_icon: "https://picsum.photos/100 ",
orders: [
{
purchase_date: "2025-06-17 08:00",
order_number: "ORD20250617002",
payment_method: "信用卡",
payment_voucher: "VOUCHER0003",
payment_date: "2025-06-17 08:15",
products: [
{
id: "p_201",
name: "網上商店C產品",
spec: "綠色 / M碼",
total_price: 200.0,
quantity: 3,
image: "https://picsum.photos/100 ",
status: 3,
eta: "2025-06-20 16:00",
confirmed: false,
status_times: {
0: "2025-06-17 08:00",
1: "2025-06-18 09:00",
2: "2025-06-19 10:00",
3: "2025-06-21 16:00",
4: null,
5: null,
},
},
],
},
{
purchase_date: "2025-06-17 08:00",
order_number: "ORD20250617003",
payment_method: null,
payment_voucher: null,
payment_date: null,
products: [
{
id: "p_204",
name: "網上商店產品",
spec: "綠色 / M碼",
total_price: 150.5,
quantity: 3,
image: "https://picsum.photos/100 ",
status: 0,
eta: null,
confirmed: false,
status_times: {
0: "2025-06-17 08:00",
1: null,
2: null,
3: null,
4: null,
5: null,
},
},
],
},
],
},
];
function fetch_order_data_structured($user_id = NULL, $seller_id = NULL, $start_date = NULL, $end_date = NULL, $limit = NULL, $offset = NULL, $sort = 'o.date_added', $order = 'DESC')
{
$t =& get_instance();
$t->db->select('o.id as order_id, o.user_id, o.date_added, o.payment_method, o.payment_voucher, o.payment_date, o.delivery_time, o.address_id, a.name as order_recipient_person');
$t->db->from('orders o');
$t->db->join('addresses a', 'a.id = o.address_id', 'left');
if ($user_id !== null) {
$t->db->where('o.user_id', $user_id);
}
if ($start_date !== null && $end_date !== null) {
$t->db->where("DATE(o.date_added) >=", $start_date);
$t->db->where("DATE(o.date_added) <=", $end_date);
}
if ($limit !== null) {
$t->db->limit($limit, $offset);
}
$t->db->order_by($sort, $order);
$order_rows = $t->db->get()->result_array();
$sellers = [];
foreach ($order_rows as $order) {
$order_id = $order['order_id'];
$t->db->select('oi.*, p.id as product_id, p.name as product_name, p.type as product_type, p.download_allowed, p.pickup_location, pv.price, pv.special_price, pv.id as variant_id, pv.image as variant_image, sd.store_name as seller_name, u.longitude as seller_longitude, u.latitude as seller_latitude, u.address as seller_address, u.mobile as seller_mobile, sd.logo as seller_icon');
$t->db->from('order_items oi');
$t->db->join('product_variants pv','pv.id = oi.product_variant_id','left');
$t->db->join('products p','p.id = pv.product_id','left');
$t->db->join('users u','u.id=oi.seller_id','left');
$t->db->join('seller_data sd','sd.user_id = oi.seller_id','left');
$t->db->where('oi.order_id', $order_id);
if ($seller_id !== null) {
$t->db->where('oi.seller_id', $seller_id);
}
$items = $t->db->get()->result_array();
foreach ($items as $item) {
$sid = $item['seller_id'];
if (!isset($sellers[$sid])) {
$sellers[$sid] = [
'seller_id' => (string)$sid,
'seller_name' => $item['seller_name'] ?? null,
'seller_icon' => $item['seller_icon'] ?? null,
'orders' => [],
];
}
// Use order_id as string key to track orders per seller
if(!isset($sellers[$sid]['orders'][$order_id])){
// Optional: fix 'payment_voucher' and method_refer_no confusion
$payment_voucher = $order['payment_voucher'] ?? null;
$payment_date = $order['payment_date'] ?? null;
// If you want to fetch via method_refer_no, you need to select it in order query first
$sellers[$sid]['orders'][$order_id] = [
'purchase_date' => $order['date_added'],
'order_number' => $order['order_id'],
'payment_method' => $order['payment_method'] ?? null,
'payment_voucher' => $payment_voucher,
'payment_date' => $payment_date,
'products' => []
];
}
// Fetch status records for this item (implement fetch_details and checkStatusForOrderItemId accordingly)
$statusRecordsDB = fetch_details("order_status_records", ["orderItemId" => $item['id']]);
$statusRecords = [
0 => null,
1 => null,
2 => null,
3 => null,
4 => null,
5 => null
];
// checking statuses
for($i = 0; $i <= 5; $i++){
if(checkStatusForOrderItemId($statusRecordsDB, $i, $item['id'])){
// You might have multiple records, ensure correct access
// assuming fetch_details returns array of records here, take first found
if (!empty($statusRecordsDB[0]['date_created'])) {
$statusRecords[$i] = $statusRecordsDB[0]['date_created'];
}
}
}
$product = [
'id' => (string)$item['id'],
'name' => $item['product_name'],
'spec' => $item['variant_name'] ?? null,
'total_price' => floatval($item['sub_total']),
'quantity' => intval($item['quantity']),
'image' => $item['variant_image'] ?? null,
'status' => intval($item['itemStatus'] ?? 0),
'eta' => $item['estimated_delivery_time'] ?? null,
'confirmed' => (isset($item['confirmed']) && $item['confirmed'] != 0),
'status_times' => $statusRecords,
];
$sellers[$sid]['orders'][$order_id]['products'][] = $product;
}
}
// Convert each seller's orders from associative by order_id to indexed array
foreach ($sellers as &$seller) {
$seller['orders'] = array_values($seller['orders']);
}
// Return the sellers as indexed array
return array_values($sellers);
}