EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
route.ts
Go to the documentation of this file.
1import { NextRequest, NextResponse } from 'next/server';
2import { fieldpineServerApi } from '@/lib/server/fieldpineApi';
3import { getStoredAuth } from '@/lib/server/auth';
4
5/**
6 * Purchase Orders Summary
7 * BUCK: retailmax.elink.purchaseorder.summary
8 * Returns overview of purchase orders
9 */
10export async function GET(request: NextRequest) {
11 try {
12 const authData = await getStoredAuth();
13 if (!authData || !authData.authenticated) {
14 return NextResponse.json(
15 { error: 'Authentication required' },
16 { status: 401 }
17 );
18 }
19
20 const { searchParams } = new URL(request.url);
21
22 // Filters
23 const startDate = searchParams.get('startDate');
24 const endDate = searchParams.get('endDate');
25 const limit = searchParams.get('limit') || '100';
26 const status = searchParams.get('status'); // pending, received, cancelled
27 const supplierId = searchParams.get('supplierId');
28
29 // Build BUCK parameters
30 const params: string[] = [
31 '3=retailmax.elink.purchaseorder.summary',
32 `8=${limit}`
33 ];
34
35 // Add date filters
36 if (startDate) {
37 params.push(`9=f102,4,${startDate}`); // PO date
38 }
39 if (endDate) {
40 params.push(`9=f102,1,${endDate}`);
41 }
42
43 // Add supplier filter
44 if (supplierId) {
45 params.push(`9=f220,2,${supplierId}`);
46 }
47
48 // Add status filter
49 if (status) {
50 let statusCode = '0';
51 switch (status) {
52 case 'pending':
53 statusCode = '1';
54 break;
55 case 'received':
56 statusCode = '2';
57 break;
58 case 'cancelled':
59 statusCode = '9';
60 break;
61 }
62 params.push(`9=f110,2,${statusCode}`);
63 }
64
65 const query = params.join('&');
66 const url = `/GNAP/j/buck?${query}`;
67
68 console.log('[PO Summary] BUCK query:', url);
69
70 const result = await fieldpineServerApi.apiCall(url, {
71 cookie: authData.apiKey,
72 useOpenApi: false
73 });
74
75 // Process BUCK response
76 if (result && result.DATS && Array.isArray(result.DATS)) {
77 const orders = result.DATS.map((item: any) => ({
78 orderId: item.f100,
79 orderNumber: item.f101,
80 orderDate: item.f102,
81 supplierId: item.f220,
82 supplierName: item.f221,
83 status: item.f110,
84 statusName: item.f111,
85 totalItems: item.f210,
86 totalUnits: item.f211,
87 totalCost: item.f212,
88 receivedDate: item.f103,
89 receivedItems: item.f213,
90 receivedUnits: item.f214,
91 receivedCost: item.f215,
92 locationId: item.f131,
93 locationName: item.f132,
94 notes: item.f120
95 }));
96
97 // Calculate summary statistics
98 const summary = {
99 totalOrders: orders.length,
100 totalCost: orders.reduce((sum: number, o: any) => sum + (o.totalCost || 0), 0),
101 pendingOrders: orders.filter((o: any) => o.status === 1).length,
102 receivedOrders: orders.filter((o: any) => o.status === 2).length,
103 cancelledOrders: orders.filter((o: any) => o.status === 9).length
104 };
105
106 return NextResponse.json({
107 success: true,
108 data: orders,
109 count: orders.length,
110 summary,
111 source: 'buck'
112 });
113 }
114
115 return NextResponse.json({
116 success: true,
117 data: [],
118 count: 0,
119 source: 'buck'
120 });
121
122 } catch (error) {
123 console.error('[PO Summary] Error:', error);
124 return NextResponse.json(
125 { error: 'Failed to fetch purchase orders' },
126 { status: 500 }
127 );
128 }
129}