Merge branch 'methapon' into dev

This commit is contained in:
Methapon Metanipat 2024-08-20 09:04:59 +07:00
commit 816bda07b8
2 changed files with 141 additions and 195 deletions

View file

@ -309,10 +309,10 @@ export class QuotationController extends Controller {
const nonExistEmployee = body.worker.filter((v) => typeof v !== "string"); const nonExistEmployee = body.worker.filter((v) => typeof v !== "string");
const lastEmployee = await tx.runningNo.upsert({ const lastEmployee = await tx.runningNo.upsert({
where: { where: {
key: `EMPLOYEE_${customerBranch.customer.code}-${customerBranch.branchNo.toString().padStart(2, "0")}-${new Date().getFullYear().toString().slice(-2).padStart(2, "0")}`, key: `EMPLOYEE_${customerBranch.customer.code.slice(0, -6)}${`${new Date().getFullYear()}`.slice(-2).padStart(2, "0")}`,
}, },
create: { create: {
key: `EMPLOYEE_${customerBranch.customer.code}-${customerBranch.branchNo.toString().padStart(2, "0")}-${new Date().getFullYear().toString().slice(-2).padStart(2, "0")}`, key: `EMPLOYEE_${customerBranch.customer.code.slice(0, -6)}${`${new Date().getFullYear()}`.slice(-2).padStart(2, "0")}`,
value: 1, value: 1,
}, },
update: { value: { increment: nonExistEmployee.length } }, update: { value: { increment: nonExistEmployee.length } },
@ -322,7 +322,7 @@ export class QuotationController extends Controller {
tx.employee.create({ tx.employee.create({
data: { data: {
...v, ...v,
code: `${customerBranch.customer.code}-${customerBranch.branchNo.toString().padStart(2, "0")}-${new Date().getFullYear().toString().slice(-2).padStart(2, "0")}${(lastEmployee.value + i).toString().padStart(4, "0")}`, code: `${customerBranch.customer.code.slice(0, -6)}${`${new Date().getFullYear()}`.slice(-2).padStart(2, "0")}${`${lastEmployee.value + i}`.padStart(7, "0")}`,
customerBranchId: customerBranch.id, customerBranchId: customerBranch.id,
}, },
}), }),
@ -406,83 +406,7 @@ export class QuotationController extends Controller {
update: { value: { increment: 1 } }, update: { value: { increment: 1 } },
}); });
const quotation = await tx.quotation.create({ return await tx.quotation.create({
data: {
...rest,
statusOrder: +(rest.status === "INACTIVE"),
code: `${currentYear.toString().padStart(2, "0")}${currentMonth.toString().padStart(2, "0")}${currentDate.toString().padStart(2, "0")}${lastQuotation.value.toString().padStart(4, "0")}`,
worker: {
createMany: {
data: sortedEmployeeId.map((v, i) => ({
no: i,
code: "",
employeeId: v,
})),
},
},
totalPrice: price.totalPrice,
totalDiscount: price.totalDiscount,
vat: price.totalVat,
vatExcluded: 0,
finalPrice: price.totalPrice - price.totalDiscount,
paySplit: {
createMany: {
data: (rest.paySplit || []).map((v, i) => ({
no: i + 1,
date: v,
})),
},
},
createdByUserId: req.user.sub,
updatedByUserId: req.user.sub,
},
});
await Promise.all(
restructureService.map(async (a) => {
const { id: _currentServiceId } = await tx.quotationService.create({
data: {
code: a.code,
name: a.name,
detail: a.detail,
attributes: a.attributes,
quotationId: quotation.id,
refServiceId: a.id,
},
});
await Promise.all(
a.work.map(async (b) => {
await tx.quotationServiceWork.create({
data: {
order: b.order,
name: b.name,
attributes: b.attributes,
serviceId: _currentServiceId,
productOnWork: {
createMany: {
data: b.product.map((v, i) => ({
productId: v.id,
order: i + 1,
vat: v.vat,
amount: v.amount,
discount: v.discount,
pricePerUnit: v.pricePerUnit,
})),
},
},
},
});
}),
);
}),
);
return await tx.quotation.findUnique({
include: { include: {
service: { service: {
include: { include: {
@ -504,7 +428,64 @@ export class QuotationController extends Controller {
select: { service: true }, select: { service: true },
}, },
}, },
where: { id: quotation.id },
data: {
...rest,
statusOrder: +(rest.status === "INACTIVE"),
code: `${currentYear.toString().padStart(2, "0")}${currentMonth.toString().padStart(2, "0")}${currentDate.toString().padStart(2, "0")}${lastQuotation.value.toString().padStart(4, "0")}`,
worker: {
createMany: {
data: sortedEmployeeId.map((v, i) => ({
no: i,
code: "",
employeeId: v,
})),
},
},
totalPrice: price.totalPrice,
totalDiscount: price.totalDiscount,
vat: price.totalVat,
vatExcluded: 0,
finalPrice: price.totalPrice - price.totalDiscount,
paySplit: {
createMany: {
data: (rest.paySplit || []).map((v, i) => ({
no: i + 1,
date: v,
})),
},
},
service: {
create: restructureService.map((a) => ({
code: a.code,
name: a.name,
detail: a.detail,
attributes: a.attributes,
refServiceId: a.id,
work: {
create: a.work.map((b) => ({
order: b.order,
name: b.name,
attributes: b.attributes,
productOnWork: {
createMany: {
data: b.product.map((v, i) => ({
productId: v.id,
order: i + 1,
vat: v.vat,
amount: v.amount,
discount: v.discount,
pricePerUnit: v.pricePerUnit,
})),
},
},
})),
},
})),
},
createdByUserId: req.user.sub,
updatedByUserId: req.user.sub,
},
}); });
}); });
} }
@ -691,7 +672,28 @@ export class QuotationController extends Controller {
}; };
}); });
const quotation = await tx.quotation.update({ return await tx.quotation.update({
include: {
service: {
include: {
work: {
include: {
productOnWork: {
include: { product: true },
},
},
},
},
},
paySplit: true,
worker: true,
customerBranch: {
include: { customer: true },
},
_count: {
select: { service: true },
},
},
where: { id: quotationId }, where: { id: quotationId },
data: { data: {
...rest, ...rest,
@ -731,78 +733,42 @@ export class QuotationController extends Controller {
} }
: undefined, : undefined,
service: body.service ? { deleteMany: {} } : undefined, service:
body.service && restructureService
? {
deleteMany: {},
create: restructureService.map((a) => ({
code: a.code,
name: a.name,
detail: a.detail,
attributes: a.attributes,
refServiceId: a.id,
work: {
create: a.work.map((b) => ({
order: b.order,
name: b.name,
attributes: b.attributes,
productOnWork: {
createMany: {
data: b.product.map((v, i) => ({
productId: v.id,
order: i + 1,
vat: v.vat,
amount: v.amount,
discount: v.discount,
pricePerUnit: v.pricePerUnit,
})),
},
},
})),
},
})),
}
: undefined,
updatedByUserId: req.user.sub, updatedByUserId: req.user.sub,
}, },
}); });
if (restructureService) {
await Promise.all(
restructureService.map(async (a) => {
const { id: _currentServiceId } = await tx.quotationService.create({
data: {
code: a.code,
name: a.name,
detail: a.detail,
attributes: a.attributes,
quotationId: quotation.id,
refServiceId: a.id,
},
});
await Promise.all(
a.work.map(async (b) => {
await tx.quotationServiceWork.create({
data: {
order: b.order,
name: b.name,
attributes: b.attributes,
serviceId: _currentServiceId,
productOnWork: {
createMany: {
data: b.product.map((v, i) => ({
productId: v.id,
order: i + 1,
vat: v.vat,
amount: v.amount,
discount: v.discount,
pricePerUnit: v.pricePerUnit,
})),
},
},
},
});
}),
);
}),
);
}
return await tx.quotation.findUnique({
include: {
service: {
include: {
work: {
include: {
productOnWork: {
include: { product: true },
},
},
},
},
},
paySplit: true,
worker: true,
customerBranch: {
include: { customer: true },
},
_count: {
select: { service: true },
},
},
where: { id: quotation.id },
});
}); });
} }

View file

@ -286,34 +286,12 @@ export class ServiceController extends Controller {
update: { value: { increment: 1 } }, update: { value: { increment: 1 } },
}); });
const workList = await Promise.all(
(work || []).map(async (w, wIdx) =>
tx.work.create({
data: {
name: w.name,
order: wIdx + 1,
attributes: w.attributes,
productOnWork: {
createMany: {
data: w.productId.map((p, pIdx) => ({
productId: p,
order: pIdx + 1,
})),
},
},
},
}),
),
);
return tx.service.create({ return tx.service.create({
include: { include: {
work: { work: {
include: { include: {
productOnWork: { productOnWork: {
include: { include: { product: true },
product: true,
},
orderBy: { order: "asc" }, orderBy: { order: "asc" },
}, },
}, },
@ -326,7 +304,19 @@ export class ServiceController extends Controller {
productTypeId, productTypeId,
statusOrder: +(body.status === "INACTIVE"), statusOrder: +(body.status === "INACTIVE"),
code: `${body.code.toLocaleUpperCase()}${last.value.toString().padStart(3, "0")}`, code: `${body.code.toLocaleUpperCase()}${last.value.toString().padStart(3, "0")}`,
work: { connect: workList.map((v) => ({ id: v.id })) }, work: {
create: (work || []).map((w, wIdx) => ({
name: w.name,
order: wIdx + 1,
attributes: w.attributes,
productOnWork: {
create: w.productId.map((p, pIdx) => ({
productId: p,
order: pIdx + 1,
})),
},
})),
},
createdByUserId: req.user.sub, createdByUserId: req.user.sub,
updatedByUserId: req.user.sub, updatedByUserId: req.user.sub,
}, },
@ -412,26 +402,6 @@ export class ServiceController extends Controller {
} }
const record = await prisma.$transaction(async (tx) => { const record = await prisma.$transaction(async (tx) => {
const workList = await Promise.all(
(work || []).map(async (w, wIdx) =>
tx.work.create({
data: {
name: w.name,
order: wIdx + 1,
attributes: w.attributes,
productOnWork: {
createMany: {
data: w.productId.map((p, pIdx) => ({
productId: p,
order: pIdx + 1,
})),
},
},
},
}),
),
);
return await tx.service.update({ return await tx.service.update({
include: { include: {
createdBy: true, createdBy: true,
@ -442,7 +412,17 @@ export class ServiceController extends Controller {
statusOrder: +(payload.status === "INACTIVE"), statusOrder: +(payload.status === "INACTIVE"),
work: { work: {
deleteMany: {}, deleteMany: {},
connect: workList.map((v) => ({ id: v.id })), create: (work || []).map((w, wIdx) => ({
name: w.name,
order: wIdx + 1,
attributes: w.attributes,
productOnWork: {
create: w.productId.map((p, pIdx) => ({
productId: p,
order: pIdx + 1,
})),
},
})),
}, },
updatedByUserId: req.user.sub, updatedByUserId: req.user.sub,
}, },