2
2
import * as XLSX from 'xlsx' ;
3
3
4
4
const exportToExcel = ( orderHistory ) => {
5
- const orderData = orderHistory . order . map ( order => {
6
- // Parse the products string to JSON
7
- const products = JSON . parse ( order . products ) ;
5
+ const orders = orderHistory . order ;
6
+ const rows = [
7
+ [
8
+ 'Order ID' ,
9
+ 'Order Date' ,
10
+ 'Expected Delivery Date' ,
11
+ 'Order Status' ,
12
+ 'Payment Method' ,
13
+ 'Products' ,
14
+ 'Shipping Status'
15
+ ]
16
+ ] ;
17
+
18
+ orders . forEach ( order => {
19
+ let productsInfo = '' ;
8
20
9
- return products . map ( product => ( {
10
- orderId : order . id ,
11
- cartId : order . cartId ,
12
- shopId : order . shopId ,
13
- orderDate : order . orderDate ,
14
- paymentMethodId : order . paymentMethodId ,
15
- status : order . status ,
16
- shippingProcess : order . shippingProcess ,
17
- expectedDeliveryDate : order . expectedDeliveryDate ,
18
- createdAt : order . createdAt ,
19
- updatedAt : order . updatedAt ,
20
- productId : product . id ,
21
- productName : product . name ,
22
- productPrice : product . price ,
23
- productDiscount : product . discount ,
24
- productQuantity : product . quantity ,
25
- productTotalPrice : product . totalPrice ,
26
- productDescription : product . description ,
27
- productImage : product . image ,
28
- } ) ) ;
29
- } ) . flat ( ) ; // Flatten the array of arrays
21
+ if ( Array . isArray ( order . products ) ) {
22
+ productsInfo = order . products . map ( p => `${ p . productId } (${ p . status } )` ) . join ( ', ' ) ;
23
+ }
24
+
25
+ rows . push ( [
26
+ order . id ,
27
+ new Date ( order . orderDate ) . toLocaleString ( ) ,
28
+ new Date ( order . expectedDeliveryDate ) . toLocaleString ( ) ,
29
+ order . status ,
30
+ order . paymentMethodId ,
31
+ productsInfo ,
32
+ order . shippingProcess
33
+ ] ) ;
34
+ } ) ;
30
35
31
- // Convert the JSON data to a worksheet
32
- const worksheet = XLSX . utils . json_to_sheet ( orderData ) ;
36
+ // Create a worksheet from the rows
37
+ const worksheet = XLSX . utils . aoa_to_sheet ( rows ) ;
33
38
34
39
// Create a new workbook and append the worksheet
35
40
const workbook = XLSX . utils . book_new ( ) ;
36
41
XLSX . utils . book_append_sheet ( workbook , worksheet , 'OrderHistory' ) ;
37
42
38
43
// Export the workbook to an Excel file
39
- XLSX . writeFile ( workbook , 'OrderHistory .xlsx' ) ;
44
+ XLSX . writeFile ( workbook , 'seller_orders .xlsx' ) ;
40
45
} ;
41
46
42
47
export default exportToExcel ;
0 commit comments