311 lines
9.6 KiB
JavaScript
311 lines
9.6 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 入库单 A5 默认打印模板(ENTRY_ORDER 业务类型)。
|
||
*
|
||
* 严格参照出库单"拣货单A5模板"(id=18)的格式与渲染引擎约定:
|
||
* - 标签元素(纯文字)使用 custom: true + content + 扁平化样式属性
|
||
* - 数据元素使用 field_code(无 custom)+ 扁平化样式属性
|
||
* - 分割线使用 line 类型
|
||
* - 表格使用 table 类型,rows 数组每项为列定义对象(type: "text", field_code, content, width 等)
|
||
* - 字体大小使用 fontSize(字符串类型,如 "10")
|
||
* - 加粗使用 bold(布尔类型)
|
||
* - textAlign 用于对齐
|
||
*
|
||
* 模板布局(A5 148mm×210mm):
|
||
* 1. 标题:"入 库 单"(fontSize: 18, bold: true, textAlign: center)
|
||
* 2. 分割线
|
||
* 3. 信息区(标签+字段模式,每行左右两列):
|
||
* 入库单号:entry_order_code | 业务类型:order_type
|
||
* 供应商:supplier_name | 采购单号:purchase_order_code
|
||
* 仓库:warehouse_name | 货主:store_name
|
||
* 物流公司:logistics_name | 运单号:express_code
|
||
* 创建时间:order_create_time | 预期到货:expect_start_time
|
||
* 4. 分割线
|
||
* 5. 入库明细标题
|
||
* 6. 表格(6列):商品编码、商品名称、条码、计划数量、已收数量、库存类型
|
||
* - 每列为 type: "text" 的扁平对象(参照 id=18 格式)
|
||
* 7. 分割线
|
||
* 8. 单据总行数:total_order_lines
|
||
* 9. 备注:remark
|
||
* 10. 二维码:entry_order_code
|
||
* 11. 打印时间:print_time
|
||
*/
|
||
|
||
const TABLE = 'wms_print_template';
|
||
|
||
// 基于时间戳生成唯一元素 ID 前缀
|
||
const TS_BASE = Date.now();
|
||
|
||
/**
|
||
* 生成模板元素唯一 ID。
|
||
* @param {string} type 元素类型前缀(text/line/table/barcode/qrcode)
|
||
* @param {number} seq 序号
|
||
* @return {string} 唯一 ID
|
||
*/
|
||
function makeId(type, seq) {
|
||
return `${type}_${TS_BASE}_${String(seq).padStart(4, '0')}`;
|
||
}
|
||
|
||
/**
|
||
* 生成 text 类型标签元素(纯文字标签,如"入库单号:"),有 custom: true。
|
||
* @param {number} seq 序号
|
||
* @param {number} top 顶部位置(mm)
|
||
* @param {number} left 左侧位置(mm)
|
||
* @param {number} width 宽度(mm)
|
||
* @param {number} height 高度(mm)
|
||
* @param {string} content 文字内容
|
||
* @param {string} fontSize 字体大小(字符串类型)
|
||
* @param {object} options 可选配置 { bold, textAlign }
|
||
* @return {object} 标签元素
|
||
*/
|
||
function label(seq, top, left, width, height, content, fontSize, options = {}) {
|
||
return {
|
||
id: makeId('text', seq),
|
||
type: 'text',
|
||
top,
|
||
left,
|
||
width,
|
||
height,
|
||
custom: true,
|
||
content,
|
||
fontSize,
|
||
bold: options.bold || false,
|
||
italic: false,
|
||
underline: false,
|
||
rotate: 0,
|
||
textAlign: options.textAlign || 'left',
|
||
lineHeight: height,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 生成 text 类型数据字段元素(绑定 field_code),无 custom。
|
||
* @param {number} seq 序号
|
||
* @param {number} top 顶部位置(mm)
|
||
* @param {number} left 左侧位置(mm)
|
||
* @param {number} width 宽度(mm)
|
||
* @param {number} height 高度(mm)
|
||
* @param {string} fieldCode 字段编码
|
||
* @param {string} fontSize 字体大小(字符串类型)
|
||
* @param {object} options 可选配置 { bold, textAlign }
|
||
* @return {object} 数据字段元素
|
||
*/
|
||
function field(seq, top, left, width, height, fieldCode, fontSize, options = {}) {
|
||
return {
|
||
id: makeId('text', seq),
|
||
type: 'text',
|
||
top,
|
||
left,
|
||
width,
|
||
height,
|
||
field_code: fieldCode,
|
||
fontSize,
|
||
bold: options.bold || false,
|
||
italic: false,
|
||
underline: false,
|
||
rotate: 0,
|
||
textAlign: options.textAlign || 'left',
|
||
lineHeight: height,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 生成分割线元素。
|
||
* @param {number} seq 序号
|
||
* @param {number} top 顶部位置(mm)
|
||
* @param {number} left 左侧位置(mm)
|
||
* @param {number} width 宽度(mm)
|
||
* @param {number} height 高度(mm)
|
||
* @return {object} 分割线元素
|
||
*/
|
||
function lineEl(seq, top, left, width, height) {
|
||
return {
|
||
id: makeId('line', seq),
|
||
type: 'line',
|
||
top,
|
||
left,
|
||
width,
|
||
height,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 生成二维码元素。
|
||
* @param {number} seq 序号
|
||
* @param {number} top 顶部位置(mm)
|
||
* @param {number} left 左侧位置(mm)
|
||
* @param {number} width 宽度(mm)
|
||
* @param {number} height 高度(mm)
|
||
* @param {string} fieldCode 字段编码
|
||
* @param {string} fontSize 字体大小
|
||
* @return {object} 二维码元素
|
||
*/
|
||
function qrcodeEl(seq, top, left, width, height, fieldCode, fontSize) {
|
||
return {
|
||
id: makeId('qrcode', seq),
|
||
type: 'qrcode',
|
||
top,
|
||
left,
|
||
width,
|
||
height,
|
||
field_code: fieldCode,
|
||
fontSize,
|
||
bold: false,
|
||
italic: false,
|
||
underline: false,
|
||
rotate: 0,
|
||
textAlign: 'center',
|
||
lineHeight: 6,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 生成表格列定义(严格参照 id=18 rows 格式)。
|
||
* 每项为 type: "text" 的扁平对象,同时包含 field_code、content(中文表头)、width、样式属性。
|
||
* @param {number} seq 序号
|
||
* @param {string} fieldCode 字段编码
|
||
* @param {string} content 中文表头内容
|
||
* @param {number} colWidth 列宽(mm)
|
||
* @param {string} fontSize 字体大小
|
||
* @param {boolean} bold 是否加粗
|
||
* @param {string} textAlign 对齐方式
|
||
* @return {object} 列定义对象
|
||
*/
|
||
function tableColumn(seq, fieldCode, content, colWidth, fontSize, bold, textAlign) {
|
||
return {
|
||
id: makeId('text', seq),
|
||
type: 'text',
|
||
field_code: fieldCode,
|
||
content,
|
||
width: colWidth,
|
||
height: 6,
|
||
fontSize,
|
||
bold: bold || false,
|
||
italic: false,
|
||
underline: false,
|
||
rotate: 0,
|
||
textAlign: textAlign || 'left',
|
||
lineHeight: 6,
|
||
};
|
||
}
|
||
|
||
// ─── 构建入库单 A5 模板元素数组 ─────────────────────────────
|
||
|
||
const templateContent = [
|
||
|
||
// 1. 标题:入 库 单
|
||
label(1, 6, 0, 148, 10, '入 库 单', '18', { bold: true, textAlign: 'center' }),
|
||
|
||
// 2. 分割线
|
||
lineEl(2, 18, 10, 128, 0.5),
|
||
|
||
// 3. 信息区 - 第一行:入库单号 | 业务类型
|
||
label(3, 21, 10, 26, 7, '入库单号:', '10'),
|
||
field(4, 21, 36, 46, 7, 'entry_order_code', '10'),
|
||
label(5, 21, 84, 20, 7, '业务类型:', '10'),
|
||
field(6, 21, 104, 34, 7, 'order_type', '10'),
|
||
|
||
// 3. 信息区 - 第二行:供应商 | 采购单号
|
||
label(7, 29, 10, 20, 7, '供应商:', '10'),
|
||
field(8, 29, 30, 52, 7, 'supplier_name', '10'),
|
||
label(9, 29, 84, 26, 7, '采购单号:', '10'),
|
||
field(10, 29, 110, 28, 7, 'purchase_order_code', '10'),
|
||
|
||
// 3. 信息区 - 第三行:仓库 | 货主
|
||
label(11, 37, 10, 18, 7, '仓库:', '10'),
|
||
field(12, 37, 28, 54, 7, 'warehouse_name', '10'),
|
||
label(13, 37, 84, 18, 7, '货主:', '10'),
|
||
field(14, 37, 102, 36, 7, 'store_name', '10'),
|
||
|
||
// 3. 信息区 - 第四行:物流公司 | 运单号
|
||
label(15, 45, 10, 26, 7, '物流公司:', '10'),
|
||
field(16, 45, 36, 46, 7, 'logistics_name', '10'),
|
||
label(17, 45, 84, 20, 7, '运单号:', '10'),
|
||
field(18, 45, 104, 34, 7, 'express_code', '10'),
|
||
|
||
// 3. 信息区 - 第五行:创建时间 | 预期到货
|
||
label(19, 53, 10, 26, 7, '创建时间:', '10'),
|
||
field(20, 53, 36, 46, 7, 'order_create_time', '10'),
|
||
label(21, 53, 84, 26, 7, '预期到货:', '10'),
|
||
field(22, 53, 110, 28, 7, 'expect_start_time', '10'),
|
||
|
||
// 4. 分割线
|
||
lineEl(23, 62, 10, 128, 0.5),
|
||
|
||
// 5. 入库明细标题
|
||
label(24, 65, 10, 128, 7, '入库明细', '11', { bold: true, textAlign: 'left' }),
|
||
|
||
// 6. 表格(严格参照 id=18 rows 格式:每列为 type: "text" 对象)
|
||
{
|
||
id: makeId('table', 25),
|
||
type: 'table',
|
||
top: 74,
|
||
left: 10,
|
||
width: 128,
|
||
height: 75,
|
||
rowHeight: 6,
|
||
headerHeight: 6,
|
||
rows: [
|
||
tableColumn(26, 'item_code', '商品编码', 22, '9', false, 'left'),
|
||
tableColumn(27, 'item_name', '商品名称', 40, '9', false, 'left'),
|
||
tableColumn(28, 'item_barcode', '条码', 22, '9', false, 'left'),
|
||
tableColumn(29, 'plan_qty', '计划数量', 18, '9', false, 'left'),
|
||
tableColumn(30, 'received_qty', '已收数量', 18, '9', false, 'left'),
|
||
tableColumn(31, 'inventory_type', '库存类型', 8, '9', false, 'left'),
|
||
],
|
||
},
|
||
|
||
// 7. 分割线
|
||
lineEl(32, 151, 10, 128, 0.5),
|
||
|
||
// 8. 单据总行数
|
||
label(33, 155, 64, 26, 7, '单据总行数:', '9'),
|
||
field(34, 155, 90, 48, 7, 'total_order_lines', '9'),
|
||
|
||
// 9. 备注
|
||
label(35, 164, 10, 14, 7, '备注:', '9'),
|
||
field(36, 164, 24, 114, 7, 'remark', '9'),
|
||
|
||
// 10. 二维码:entry_order_code
|
||
qrcodeEl(37, 4, 120, 24, 24, 'entry_order_code', '8'),
|
||
|
||
// 11. 打印时间
|
||
label(38, 174, 10, 26, 7, '打印时间:', '9'),
|
||
field(39, 174, 36, 102, 7, 'print_time', '9'),
|
||
|
||
];
|
||
|
||
module.exports.up = async queryInterface => {
|
||
const { sequelize } = queryInterface;
|
||
|
||
// 先删除可能存在的旧版本 id=20
|
||
await sequelize.query(
|
||
`DELETE FROM \`${TABLE}\` WHERE id = 20`,
|
||
{ type: sequelize.QueryTypes.DELETE }
|
||
);
|
||
|
||
// 插入修正后的入库单 A5 模板(id=20)
|
||
await sequelize.query(
|
||
`INSERT INTO \`${TABLE}\`
|
||
(id, business_type, template_name, platform, warehouse_id, page_width, page_height, template_content, template_type, template_url, is_default, status, create_at, update_at)
|
||
VALUES (20, 'ENTRY_ORDER', '入库单A5模板', 'default', NULL, 148, 210, :template_content, 'CUSTOM', '', true, 'ACTIVE', NOW(), NOW())`,
|
||
{
|
||
replacements: {
|
||
template_content: JSON.stringify(templateContent),
|
||
},
|
||
type: sequelize.QueryTypes.INSERT,
|
||
}
|
||
);
|
||
};
|
||
|
||
module.exports.down = async queryInterface => {
|
||
const { sequelize } = queryInterface;
|
||
await sequelize.query(
|
||
`DELETE FROM \`${TABLE}\` WHERE id = 20`,
|
||
{ type: sequelize.QueryTypes.DELETE }
|
||
);
|
||
};
|
||
|
||
module.exports.templateContent = templateContent;
|