Tokiva-backend-Node / src /utils /kodeGenerator.ts
anoderb
feat: initial commit of Tokiva POS Node backend for Hugging Face
ef874e6
Raw
History Blame Contribute Delete
2.24 kB
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++;
}
};