44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
import { execFile } from "child_process";
|
|
import { promisify } from "util";
|
|
import path from "path";
|
|
import { cloneDeepWith, deburr } from "es-toolkit";
|
|
|
|
const execFileAsync = promisify(execFile);
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const body = await readBody(event);
|
|
const bodyNonAccent = cloneDeepWith(body, (value) => (typeof value === "string" ? deburr(value) : undefined));
|
|
const { invoice, invoice_details, payment_record } = bodyNonAccent;
|
|
|
|
const items = invoice_details.map((d: any) => ({
|
|
name: d.variant__product__name,
|
|
variant: `${d.variant__color__name} ${d.variant__ram__code}/${d.variant__internal_storage__code}`,
|
|
imei: d.imei_sold,
|
|
price: d.price,
|
|
}));
|
|
|
|
const payload = JSON.stringify({
|
|
code: invoice.code,
|
|
ordered_at: invoice.ordered_at,
|
|
customer_name: invoice.customer_name,
|
|
customer_phone: invoice.customer_phone,
|
|
shipping_address: invoice.shipping_address,
|
|
items,
|
|
product_amount: invoice.product_amount,
|
|
shipping_fee: invoice.shipping_fee,
|
|
discount_amount: invoice.discount_amount,
|
|
final_amount: invoice.final_amount,
|
|
cash_amount: payment_record.cash_amount,
|
|
transfer_amount: payment_record.transfer_amount,
|
|
});
|
|
|
|
const scriptPath = path.join(process.cwd(), "print", "print_receipt.py");
|
|
|
|
try {
|
|
const { stdout } = await execFileAsync("python", [scriptPath, payload]);
|
|
return { success: true, output: stdout };
|
|
} catch (err) {
|
|
throw createError({ statusCode: 500, statusMessage: "Print failed", data: String(err) });
|
|
}
|
|
});
|