feat(customers): 增加商户编号并补齐生日提醒信息
录入、列表和导入导出带上商户编号。生日提醒展示与录入一致的商户资料,旧阴历生日仍按农历提醒。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -11,8 +11,15 @@ import { isSuperAdmin } from "@/lib/acl";
|
||||
import { fmtDate, parseDate } from "@/lib/utils";
|
||||
|
||||
// 校验规则对齐老系统必填项(customer_name/contacts/balance_user/detail/salesman/report_date)
|
||||
const customerNoSchema = z
|
||||
.string()
|
||||
.trim()
|
||||
.min(1, "商户编号必填")
|
||||
.regex(/^[A-Za-z0-9]+$/, "商户编号只能包含数字和字母,区分大小写");
|
||||
|
||||
const schema = z.object({
|
||||
customerName: z.string().trim().min(1, "商户名称必填").max(500),
|
||||
customerNo: customerNoSchema,
|
||||
contacts: z.string().trim().min(1, "联系人必填").max(500),
|
||||
balanceUser: z.string().trim().min(1, "结算人必填").max(500),
|
||||
detail: z.string().trim().min(1, "产品明细必填"),
|
||||
@@ -26,8 +33,8 @@ const schema = z.object({
|
||||
balanceUserPhone: z.string().trim().optional().default(""),
|
||||
deposit: z.string().trim().optional().default(""),
|
||||
buy: z.string().trim().optional().default(""),
|
||||
birthdayType: z.coerce.number().int().min(1).max(2).default(1),
|
||||
birthdayOwner: z.coerce.number().int().min(1).max(2).default(2),
|
||||
birthdayType: z.coerce.number().refine((n) => n === 1, "生日类型只能是阳历"),
|
||||
birthdayOwner: z.coerce.number().refine((n) => n === 1, "生日归属只能是结算人"),
|
||||
birthday: z.string().trim().optional().default(""),
|
||||
remark: z.string().trim().optional().default(""),
|
||||
});
|
||||
@@ -57,6 +64,7 @@ export type ActionState = { error?: string };
|
||||
function pickForm(formData: FormData) {
|
||||
return {
|
||||
customerName: formData.get("customerName"),
|
||||
customerNo: formData.get("customerNo"),
|
||||
contacts: formData.get("contacts"),
|
||||
balanceUser: formData.get("balanceUser"),
|
||||
detail: formData.get("detail"),
|
||||
@@ -66,8 +74,8 @@ function pickForm(formData: FormData) {
|
||||
balanceUserPhone: formData.get("balanceUserPhone") ?? "",
|
||||
deposit: formData.get("deposit") ?? "",
|
||||
buy: formData.get("buy") ?? "",
|
||||
birthdayType: formData.get("birthdayType") ?? 1,
|
||||
birthdayOwner: formData.get("birthdayOwner") ?? 2,
|
||||
birthdayType: 1,
|
||||
birthdayOwner: 1,
|
||||
birthday: formData.get("birthday") ?? "",
|
||||
remark: formData.get("remark") ?? "",
|
||||
};
|
||||
@@ -105,7 +113,15 @@ export async function updateCustomer(
|
||||
const r = schema.safeParse(pickForm(formData));
|
||||
if (!r.success) return { error: r.error.issues[0].message };
|
||||
|
||||
await prisma.customer.update({ where: { id }, data: toData(r.data) });
|
||||
const existing = await prisma.customer.findUnique({
|
||||
where: { id },
|
||||
select: { birthdayType: true },
|
||||
});
|
||||
const data = toData(r.data);
|
||||
// 改版前的阴历生日不能在保存其它字段时改成阳历,否则明年提醒日会错
|
||||
if (existing?.birthdayType === 2) data.birthdayType = 2;
|
||||
|
||||
await prisma.customer.update({ where: { id }, data });
|
||||
revalidatePath("/customers");
|
||||
redirect("/customers");
|
||||
}
|
||||
@@ -125,6 +141,7 @@ export async function deleteCustomer(id: number): Promise<void> {
|
||||
|
||||
const HEADER_MAP: Record<string, string> = {
|
||||
商户名称: "customerName",
|
||||
商户编号: "customerNo",
|
||||
联系人: "contacts",
|
||||
联系人电话: "contactsPhone",
|
||||
结算人: "balanceUser",
|
||||
@@ -134,8 +151,6 @@ const HEADER_MAP: Record<string, string> = {
|
||||
产品明细: "detail",
|
||||
押金: "deposit",
|
||||
买断: "buy",
|
||||
生日类型: "birthdayType",
|
||||
生日归属: "birthdayOwner",
|
||||
生日: "birthday",
|
||||
业务员: "salesman",
|
||||
报单日期: "reportDate",
|
||||
@@ -170,6 +185,8 @@ export async function importCustomers(formData: FormData): Promise<ImportResult>
|
||||
|
||||
const errors: string[] = [];
|
||||
if (!rec.customerName) errors.push("商户名称必填");
|
||||
if (!rec.customerNo) errors.push("商户编号必填");
|
||||
else if (!/^[A-Za-z0-9]+$/.test(rec.customerNo)) errors.push("商户编号只能包含数字和字母");
|
||||
const reportDate = parseDate(rec.reportDate);
|
||||
if (!rec.reportDate || !reportDate) errors.push("报单日期必填且格式为 YYYY-MM-DD");
|
||||
if (rec.birthday && !parseDate(rec.birthday)) errors.push("生日格式应为 YYYY-MM-DD");
|
||||
@@ -179,6 +196,7 @@ export async function importCustomers(formData: FormData): Promise<ImportResult>
|
||||
} else {
|
||||
validData.push({
|
||||
customerName: rec.customerName,
|
||||
customerNo: rec.customerNo,
|
||||
contacts: rec.contacts || null,
|
||||
contactsPhone: rec.contactsPhone || null,
|
||||
balanceUser: rec.balanceUser || null,
|
||||
@@ -189,8 +207,8 @@ export async function importCustomers(formData: FormData): Promise<ImportResult>
|
||||
buy: rec.buy || null,
|
||||
salesman: rec.salesman || null,
|
||||
remark: rec.remark || null,
|
||||
birthdayType: rec.birthdayType === "阴历" || rec.birthdayType === "2" ? 2 : 1,
|
||||
birthdayOwner: rec.birthdayOwner === "结算人" || rec.birthdayOwner === "1" ? 1 : 2,
|
||||
birthdayType: 1,
|
||||
birthdayOwner: 1,
|
||||
birthday: parseDate(rec.birthday),
|
||||
reportDate,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user