File size: 2,238 Bytes
ef874e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import prisma from './db';

export const generateKodeProduk = async (): Promise<string> => {
  let count = (await prisma.produk.count()) + 1;
  while (true) {
    const code = `PLU${String(count).padStart(5, '0')}`;
    const exists = await prisma.produk.findUnique({ where: { kode: code } });
    if (!exists) return code;
    count++;
  }
};

export const generateKodeMember = async (): Promise<string> => {
  let count = (await prisma.member.count()) + 1;
  while (true) {
    const code = `TKV-${String(count).padStart(4, '0')}`;
    const exists = await prisma.member.findUnique({ where: { kode: code } });
    if (!exists) return code;
    count++;
  }
};

export const generateKodeShift = async (): Promise<string> => {
  const todayStr = new Date().toISOString().slice(0, 10).replace(/-/g, '');
  const prefix = `TKV-${todayStr}-`;
  let count =
    (await prisma.shift.count({
      where: {
        kode: { startsWith: prefix },
      },
    })) + 1;

  while (true) {
    const code = `${prefix}${String(count).padStart(3, '0')}`;
    const exists = await prisma.shift.findFirst({ where: { kode: code } });
    if (!exists) return code;
    count++;
  }
};

export const generateNoTransaksi = async (): Promise<string> => {
  const todayStr = new Date().toISOString().slice(0, 10).replace(/-/g, '');
  const prefix = `TKV-${todayStr}-`;
  let count =
    (await prisma.transaksi.count({
      where: {
        no_transaksi: { startsWith: prefix },
      },
    })) + 1;

  while (true) {
    const code = `${prefix}${String(count).padStart(4, '0')}`;
    const exists = await prisma.transaksi.findUnique({ where: { no_transaksi: code } });
    if (!exists) return code;
    count++;
  }
};

export const generateNoRetur = async (): Promise<string> => {
  const todayStr = new Date().toISOString().slice(0, 10).replace(/-/g, '');
  const prefix = `TKV-RET-${todayStr}-`;
  let count =
    (await prisma.transaksi_retur.count({
      where: {
        no_retur: { startsWith: prefix },
      },
    })) + 1;

  while (true) {
    const code = `${prefix}${String(count).padStart(4, '0')}`;
    const exists = await prisma.transaksi_retur.findUnique({ where: { no_retur: code } });
    if (!exists) return code;
    count++;
  }
};