康虎云报表

你只负责收集数据,打印的事我来

康虎云报表基础模块

============================
基于康虎云报表的打印功能必须依赖此模块。
本模块主要功能:
----------------------------
  • 引入康虎云报表所需的javascript库
  • 实现打印模板管理功能,模板可以存入数据库,便于统一管理(从菜单 设置--技术--报告--康虎云报表 进入)
  • 增加了根据原QWeb报表取值功能,该功能按QWeb模板中的方式取值,但把HTML去掉,否则数据不干净可能导打印失败
  • (功能持续增加中...)

怎样使用康虎云报表

康虎云报表概要用法.

----------------------------
step 1、把报表类型由原来的qweb-pdf改成qweb-html:
<report
string="Warehouse Checklist"
id="action_report_warehouse_checklist"
model="stock.picking"
report_type="qweb-html" <!-- 这里由原来的 qweb- 改为 qweb-html -->
name="stock_cf.report_warehousechecklist"
file="stock_cf.report_warehousechecklist"
/>

step 2、根据康虎云报表的数据规范生成报表数据:
报表数据格式如下(json格式,详细说明请参考康虎云报表相关帮助文档):
=====================================================
<script type="text/javascript">
var cfprint_addr = "127.0.0.1"; //打印服务器监听地址
var _delay_close = -1; //打印完成后关闭窗口的延时时长(毫秒), -1则表示不关闭

/**定义主表结构**/
var _tablePack = {
"Name": "Pack",
"Cols":[
{ "type": "str", "size": 255, "name": "仓库", "required": false },
{ "type": "str", "size": 50, "name": "供应商", "required": false },
{ "type": "str", "size": 30, "name": "日期", "required": false },
{ "type": "str", "size": 255, "name": "入库单号", "required": false },
{ "type": "str", "size": 30, "name": "采购单号", "required": false },
{ "type": "int", "size": 0, "name": "件数", "required": false },
{ "type": "str", "size": 20, "name": "包装种类", "required": false },
{ "type": "str", "size": 30, "name": "车号", "required": false },
{ "type": "str", "size": 30, "name": "柜号", "required": false }
],
"Data":[ ]
};

/**定义从表结构**/
var _tablePackLines = {
"Name": "PackLines",
"Cols":[
{ "type": "str", "size": 30, "name": "入库单号", "required": false },
{ "type": "str", "size": 255, "name": "产品", "required": false },
{ "type": "str", "size": 30, "name": "条形码", "required": false },
{ "type": "float", "size": 0, "name": "采购数量", "required": false },
{ "type": "float", "size": 0, "name": "实际数量", "required": false },
{ "type": "str", "size": 20, "name": "计量单位", "required": false },
],
"Data":[ ]
};
<t t-foreach="docs" t-as="o">
/*增加主表记录*/
_tablePack.Data.push(
{
"仓库":"<span t-field="o.picking_type_id.warehouse_id.partner_id" t-field-options='{"widget": "contact", "fields": ["name"], "no_marker": true, "no_tag_br": true, "data_type": "raw"}' />",
"供应商":"<t t-if="o.partner_id" name="partner_header"><span t-field="o.partner_id" t-field-options='{"widget": "contact", "fields": ["name"], "no_marker": true, "no_tag_br": true, "data_type": "raw"}' /></t>",
"日期":"<t t-esc="context_timestamp(datetime.datetime.now()).strftime('%Y-%m-%d %H:%M')"/>",
"入库单号":"<t t-esc="o.name" class="mt0"/>",
"采购单号":"<t t-if="o.origin"><t t-esc="o.origin"/></t>",
"件数":"<t t-esc="sum(pack_operation.product_qty for pack_operation in o.pack_operation_ids)"/>",
"包装种类":"",
"车号":"",
"柜号":""
});

<t t-foreach="o.move_lines" t-as="move">
/**增加从表记录**/
_tablePackLines.Data.push(
{
"入库单号":"<t t-esc="o.name" class="mt0"/>",
"产品":"<span t-field="move.product_id" t-field-options='{"data_type":"raw"}'/>",
"条形码":"<t t-if="move.product_id and move.product_id.ean13"><span t-field="move.product_id.ean13" t-field-options='{"data_type":"raw"}'/></t>",
"采购数量":"<t t-esc="move.product_uom_qty"/>",
"实际数量":"",
"计量单位":"<span t-field="move.product_id.uom_id" t-field-options='{"data_type":"raw"}'/>"
});
</t>
</t>

//下面把所有表合并到一个json中
var _data = {"template": "warehouse_checklist.fr3", "ver": 4, "Copies": 1, "Duplex": 0, "Tables":[]};
_data["Tables"].push(_tablePack);
_data["Tables"].push(_tablePackLines);
var _reportData = JSON.stringify(_data); //转成json字符串

console.log(_reportData);
//生成数据之后,在cfprint_ext.js中会自动调用进行打印
</script>
=====================================================
以上数据文件中的数据项,可以根据原来QWeb报表模板中的数据项生成,对于关联字段需要生成字段不带HTML标签的内容,
可以在 t-field-options 中增加 data_type属性来指定输出所需要格式:
"data_type":"raw" : 输出纯文本,不同数据项间使用逗号分隔
示例如下:
<address t-field="o.move_lines[0].partner_id" t-field-options='{"widget": "contact", "fields": ["address", "name"], "no_marker": true, "no_tag_br": true,"data_type": "raw"}' />
将生成:
中国福建省厦门市 361000,远海码头便利店


step 3、自动输出到打印机
如果生成的json字符串名为 _reportData 且没有语法错误,则会自动输出到打印机;
如果没有打印,则先用浏览器的脚本调试功能打开看一下,有没有脚本错。

打印模板服务端统一管理

把打印模板保存在数据库中.

step 1、把模板上传到数据库:
从菜单 设置--技术--报告--康虎云报表 进入模板管理功能,其中:
  • 模板ID: 是用以区分模板的唯一标识,最后根据该ID取用模板
  • 名称: 是便于识别模板的名称
  • 说明:是对模板用途或其他信息的说明
  • 模板:上传的模板内容
  • 预览图:是便于了解模板具体格式的一个预览效果截图




step 2、从数据库取用模板:
康虎云报表支持客户端模板也支持服务端模板,客户端模板是默认的方式:
客户端模板:
{
"template": "report_saleorder.fr3", /*模板保存在客户端 cfprint.exe目录下templates目录下*/
"ver": 4,
"Copies": 1,
"Duplex": 0,
"Tables":[]
}

服务端模板:
{ "template": "base64: <这里放模板Base64内容>", "ver": 4,
"Copies": 1,
"Duplex": 0,
"Tables":[]
}


这里主要介绍服务端模板,可以通过两种方式从数据库中取出模板数据:
从数据库中获取打印模板方法1(调用简短方法):
{"template": "base64:<t t-esc="get_cf_template(user.env, '12345')" />", "ver": 4, "Copies": 1, "Duplex": 0, "Tables":[]}

从数据库中获取打印模板方法2(直接查询法):
{"template": "base64:<t t-esc="user.env['cf.template'].search([('templ_id', '=', '12345')], limit=1).template" />", "ver": 4, "Copies": 1, "Duplex": 0, "Tables":[]}