File size: 4,278 Bytes
0a5bd33
 
 
 
89569c6
0a5bd33
 
89569c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a5bd33
 
 
 
 
 
 
 
 
 
 
89569c6
 
 
0a5bd33
89569c6
 
0a5bd33
 
 
 
 
 
 
 
89569c6
 
 
 
 
 
0a5bd33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89569c6
 
 
0a5bd33
 
89569c6
0a5bd33
 
 
 
 
 
 
 
89569c6
 
 
 
0a5bd33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { NextRequest, NextResponse } from 'next/server';
import { getDb } from '@/lib/db';
import { PaperType } from '@/types';

export async function GET(req: NextRequest) {
  try {
    const db = getDb();
    const { searchParams } = new URL(req.url);
    const supplier = searchParams.get('supplier');
    const q = searchParams.get('q');

    let query = 'SELECT * FROM paper_types WHERE 1=1';
    const params: unknown[] = [];

    if (supplier && supplier !== 'all') {
      query += ' AND supplier = ?';
      params.push(supplier);
    }
    if (q) {
      query += ' AND (name LIKE ? OR code LIKE ? OR description LIKE ?)';
      const term = `%${q}%`;
      params.push(term, term, term);
    }

    query += ' ORDER BY is_active DESC, supplier ASC, name ASC, gsm ASC';

    const rows = db.prepare(query).all(...params) as Record<string, unknown>[];
    const papers: PaperType[] = rows.map((r) => {
      const pAbove = Number(r.price_above_500) || Number(r.price_per_ram) || 0;
      const pBelow = Number(r.price_below_500) || pAbove;
      return {
        id: r.id as number,
        code: r.code as string,
        name: r.name as string,
        gsm: r.gsm as number,
        parentWidthCm: r.parent_width_cm as number,
        parentHeightCm: r.parent_height_cm as number,
        priceAbove500: pAbove,
        priceBelow500: pBelow,
        pricePerRam: pAbove,
        pricePerKg: 0,
        supplier: (r.supplier as string) || 'Thuận Phát',
        unit: 'ram',
        description: (r.description as string) || '',
        isActive: Boolean(r.is_active),
      };
    });
    return NextResponse.json(papers);
  } catch (err: unknown) {
    return NextResponse.json({ error: (err as Error).message }, { status: 500 });
  }
}

export async function POST(req: NextRequest) {
  try {
    const body = await req.json();
    const db = getDb();

    const pAbove = Number(body.priceAbove500 ?? body.pricePerRam) || 0;
    const pBelow = Number(body.priceBelow500) || pAbove;

    const stmt = db.prepare(`
      INSERT INTO paper_types (code, name, gsm, parent_width_cm, parent_height_cm, price_above_500, price_below_500, price_per_ram, price_per_kg, supplier, unit, description, is_active)
      VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    `);

    const result = stmt.run(
      body.code,
      body.name,
      body.gsm,
      body.parentWidthCm,
      body.parentHeightCm,
      pAbove,
      pBelow,
      pAbove,
      0,
      body.supplier || 'Thuận Phát',
      'ram',
      body.description || '',
      body.isActive !== false ? 1 : 0
    );

    return NextResponse.json({ success: true, id: Number(result.lastInsertRowid) });
  } catch (err: unknown) {
    return NextResponse.json({ error: (err as Error).message }, { status: 500 });
  }
}

export async function PUT(req: NextRequest) {
  try {
    const body = await req.json();
    const db = getDb();

    const pAbove = Number(body.priceAbove500 ?? body.pricePerRam) || 0;
    const pBelow = Number(body.priceBelow500) || pAbove;

    const stmt = db.prepare(`
      UPDATE paper_types
      SET name = ?, gsm = ?, parent_width_cm = ?, parent_height_cm = ?, price_above_500 = ?, price_below_500 = ?, price_per_ram = ?, price_per_kg = 0, supplier = ?, unit = 'ram', description = ?, is_active = ?, updated_at = CURRENT_TIMESTAMP
      WHERE id = ?
    `);

    stmt.run(
      body.name,
      body.gsm,
      body.parentWidthCm,
      body.parentHeightCm,
      pAbove,
      pBelow,
      pAbove,
      body.supplier || 'Thuận Phát',
      body.description || '',
      body.isActive ? 1 : 0,
      body.id
    );

    return NextResponse.json({ success: true });
  } catch (err: unknown) {
    return NextResponse.json({ error: (err as Error).message }, { status: 500 });
  }
}

export async function DELETE(req: NextRequest) {
  try {
    const { searchParams } = new URL(req.url);
    const id = searchParams.get('id');
    if (!id) return NextResponse.json({ error: 'Thiếu ID giấy' }, { status: 400 });

    const db = getDb();
    db.prepare('DELETE FROM paper_types WHERE id = ?').run(Number(id));
    return NextResponse.json({ success: true });
  } catch (err: unknown) {
    return NextResponse.json({ error: (err as Error).message }, { status: 500 });
  }
}