อกกคำสั่ง => report

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2024-06-27 10:25:38 +07:00
parent 4986a17e06
commit 6399e23d28

View file

@ -39,13 +39,11 @@ const fileTailer = ref<any>(null);
const OrderPDFUpload = ref<string>("");
const TailerPDFUpload = ref<string>("");
const orderCoverPdf = ref<any>();
const orderAttachmentPdf = ref<string>("");
const statusOrder = ref<string>();
const orderName = ref<string>("");
const orderStatusName = ref<string>("");
const dataGen = ref<any>();
const orderId = ref<string>(orderId_params.toString());
onMounted(async () => {
@ -108,132 +106,81 @@ const getCommandDetail = async () => {
orderStatusName.value = data.orderStatusName;
fetchReportCover("pdf", orderId.value);
if (attachmentStatus.value) {
fetchReportAttachment("pdf", orderId.value);
}
})
.catch((e) => {
messageError($q, e);
hideLoader();
});
};
//
const downloadCover = async (type: string) => {
showLoader();
if (!genReportStatus.value) {
await http
.get(config.API.fileCover(code.value, type, orderId.value), {
responseType: "blob",
})
.then(async (res) => {
downloadFile(res, `คำสั่ง ${orderName.value}.${type}`);
})
.catch(async (e) => {
messageError($q, JSON.parse(await e.response.data.text()));
})
.finally(() => {
hideLoader();
});
} else if (type === "docx") {
genReport(dataGen.value, `คำสั่ง ${orderName.value}`, "docx");
} else {
const link = document.createElement("a");
var fileName = `คำสั่ง ${orderName.value}.${
type === "docx" ? "docx" : "pdf"
}`;
link.href = orderCoverPdf.value;
link.setAttribute("download", fileName);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
hideLoader();
}
genReport(dataCover.value, `คำสั่ง ${orderName.value}`, type);
};
//
const downloadAttachment = async (type: string) => {
genReport(dataAttachment.value, `คำสั่ง ${orderName.value}`, type);
};
const dataCover = ref<any>();
//
const fetchReportCover = async (type: string, orderId: string) => {
showLoader();
await http
.get(config.API.fileAttachment(code.value, type, orderId.value), {
responseType: "blob",
})
.then(async (res) => {
downloadFile(res, `เอกสารแนบท้าย ${orderName.value}.${type}`);
.get(config.API.reportOrderCover(type, orderId, code.value))
.then(async (res: any) => {
genPDf(res.data.result);
dataCover.value = res.data.result;
})
.catch(async (e) => {
messageError($q, JSON.parse(await e.response.data.text()));
})
.finally(() => {
messageError($q, e);
hideLoader();
});
};
//
const fetchReportCover = async (type: string, orderId: string) => {
await http
.get(
config.API.reportOrderCover(type, orderId, code.value),
!genReportStatus.value
? {
responseType: "blob",
}
: {}
)
.then(async (res: any) => {
if (!genReportStatus.value) {
const blob = new Blob([res.data]);
const objectUrl = URL.createObjectURL(blob);
orderCoverPdf.value = objectUrl;
if (tab.value == "main") {
viewPDF(orderCoverPdf.value);
}
} else {
// report template
dataGen.value = res.data.result;
await axios
.post(`${config.API.reportTemplate}/docx`, res.data.result, {
headers: {
accept: "application/pdf",
"content-Type": "application/json",
},
responseType: "blob",
})
.then((resReport) => {
const data = resReport.data;
if (data) {
const blob = new Blob([data]);
const objectUrl = URL.createObjectURL(blob);
orderCoverPdf.value = objectUrl;
viewPDF(orderCoverPdf.value);
}
})
.catch(async (e) => {
messageError($q, JSON.parse(await e.response.data.text()));
})
.finally(() => {});
}
})
.catch(async (e) => {
!genReportStatus.value
? messageError($q, JSON.parse(await e.response.data.text()))
: messageError($q, e);
});
};
const dataAttachment = ref<any>();
//
const fetchReportAttachment = async (type: string, orderId: string) => {
showLoader();
await http
.get(config.API.reportOrderAttachment(type, orderId, code.value), {
.get(config.API.reportOrderAttachment(type, orderId, code.value))
.then(async (res) => {
genPDf(res.data.result);
dataAttachment.value = res.data.result;
})
.catch(async (e) => {
messageError($q, e);
hideLoader();
});
};
function genPDf(data: any) {
axios
.post(config.API.reportTemplate + `/docx`, data, {
headers: {
accept: "application/pdf",
"content-Type": "application/json",
},
responseType: "blob",
})
.then(async (res) => {
const blob = new Blob([res.data]);
const objectUrl = URL.createObjectURL(blob);
orderAttachmentPdf.value = objectUrl;
const pdfData = await usePDF(`${objectUrl}`);
showLoader();
setTimeout(() => {
pdfSrc.value = pdfData.pdf.value;
numOfPages.value = pdfData.pages.value;
hideLoader();
}, 1500);
})
.catch(async (e) => {
messageError($q, JSON.parse(await e.response.data.text()));
messageError($q, e);
hideLoader();
});
};
}
// Fetch file upload & detail
const fetchAttachment = async (orderId: string) => {
@ -241,6 +188,7 @@ const fetchAttachment = async (orderId: string) => {
.get(config.API.attachmentOrder(orderId))
.then(async (res) => {
let response = res.data.result;
order.value = response.orderNo;
years.value = Number(response.orderYear);
if (response.signDate !== undefined && response.signDate != "") {
@ -292,10 +240,10 @@ const viewPDFUpload = async (pdf: string) => {
watch(tab, () => {
if (tab.value === "main") {
viewPDF(orderCoverPdf.value);
fetchReportCover("pdf", orderId.value);
}
if (tab.value === "second") {
viewPDF(orderAttachmentPdf.value);
fetchReportAttachment("pdf", orderId.value);
}
});
@ -327,16 +275,6 @@ const onchangePage = () => {
vuePDFRef.value.reload();
}
};
//
const downloadFile = (response: any, filename: string) => {
const link = document.createElement("a");
var fileName = filename;
link.href = window.URL.createObjectURL(new Blob([response.data]));
link.setAttribute("download", fileName);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
//
const saveUpload = () => {
@ -526,9 +464,6 @@ const viewFileUpload = async (url: string) => {
v-close-popup
@click="downloadCover('pdf')"
>
<!-- type="a"
:href="orderCoverPdf"
target="_blank" -->
<q-item-section avatar
><q-icon color="red" name="mdi-file-pdf"
/></q-item-section>
@ -539,9 +474,6 @@ const viewFileUpload = async (url: string) => {
v-close-popup
@click="downloadCover('docx')"
>
<!-- type="a"
:href="orderCoverDocs"
target="_blank" -->
<q-item-section avatar
><q-icon color="blue" name="mdi-file-word"
/></q-item-section>