function Printing(xmlHandle, rowCount, rowPerPage, currentPage, ipPrinter) { this.xmlHandle = null; this.mode = ""; this.testRowCount = 0; this.testRowPerPage = 0; this.testCurrentPage = 0; this.columnCount = 0; this.head = new PrintingHead(); this.detail = new PrintingDetail(); this.foot = new PrintingFoot(); this.font = new PrintFont(); this.rowPerPage = 0; this.currentPage = 0; this.rowCount = 0; this.totalPage = 0; this.autoPrint = false; this.autoCloseDialog = false; this.printerName = ""; this.ipPrinter = ""; this.pd = null; this.xml_type = "datawindow"; this.runtimeTop = 0; this.runtimeLeft = 0; this.paperNameId = 10; this.paperWidth = -1; this.paperHeight = -1; this.paperInchMilli = 1; // Constructor - Begin this.xmlHandle = xmlHandle; this.mode = this.xmlHandle.mode; if (this.xmlHandle.mode == "runtime") { this.rowCount = parseInt(rowCount + ""); this.rowPerPage = parseInt(rowPerPage + ""); this.currentPage = parseInt(currentPage + ""); this.totalPage = Math.ceil(this.rowCount / this.rowPerPage); } // Constructor - End this.getXmlString = function (xDoc, row, column) { var result = ""; try { result = xDoc.getElementsByTagName(column)[row].childNodes[0].nodeValue; if (result == undefined || result == null) { result = ""; } } catch (err) { result = ""; } result += ""; result = result.replace(/^\s+|\s+$/g, ''); return result; } this.getXmlInt = function (xDoc, row, column) { var result = this.getXmlString(xDoc, row, column); if (result == "") { return 0; } else { try { return parseInt(result); } catch (err) { return 0; } } } this.addColumn = function (printColumn) { if (printColumn.position == "head") { if (this.indexOf(printColumn.name, "head") >= 0) { alert("พบ column name: " + printColumn.name + " ซ้ำ(head)"); } this.head.columns[this.head.count] = printColumn; this.head.count++; } else if (printColumn.position == "foot") { if (this.indexOf(printColumn.name, "foot") >= 0) { alert("พบ column name: " + printColumn.name + " ซ้ำ(foot)"); } this.foot.columns[this.foot.count] = printColumn; this.foot.count++; } else { if (this.indexOf(printColumn.name, "detail") >= 0) { alert("พบ column name: " + printColumn.name + " ซ้ำ(detail)"); } this.detail.columns[this.detail.count] = printColumn; this.detail.count++; } this.columnCount++; } this.getHeadColumn = function (name) { var ii = this.indexOf(name, "head"); if (ii < 0) { return null; } else { return this.head.columns[ii]; } } this.getDetailColumn = function (name) { var ii = this.indexOf(name, "detail"); if (ii < 0) { return null; } else { return this.detail.columns[ii]; } } this.getFootColumn = function (name) { var ii = this.indexOf(name, "foot"); if (ii < 0) { return null; } else { return this.foot.columns[ii]; } } this.indexOf = function (name, position) { if (position == "head") { for (i = 0; i < this.head.count; i++) { if (name == this.head.columns[i].name) { return i; } } } else if (position == "foot") { for (i = 0; i < this.foot.count; i++) { if (name == this.foot.columns[i].name) { return i; } } } else if (position == "detail") { for (i = 0; i < this.detail.count; i++) { if (name == this.detail.columns[i].name) { return i; } } } return -1; } this.write = function (pd) { this.pd = pd; if (this.xmlHandle.mode == "test") { this.rowCount = this.testRowCount; this.rowPerPage = this.testRowPerPage; this.currentPage = this.testCurrentPage; this.totalPage = Math.ceil(this.rowCount / this.rowPerPage); } else { // อ่าน XmlConfig var clientIp = ipAddress; //alert(clientIp); var report = reportName; //alert(report); var xmlUrl = xmlConfigUrl; //alert(xmlUrl); var curDate = new Date(); var curTime = curDate.getTime(); //var parser = new DOMParser(); //var xmlDoc = parser.parseFromString(xmlUrl + "?curTime=" + curTime, "text/xml"); var xmlObj = new XMLHttpRequest(); xmlObj.open("GET", xmlUrl + "?curTime=" + curTime, false); xmlObj.send(); var xmlDoc = xmlObj.responseXML; for (var ii = 0; ii < 100; ii++) { try { var ipData = this.getXmlString(xmlDoc, ii, "client_ip"); if (ipData == "") { break; } //alert(ipData); var reportData = this.getXmlString(xmlDoc, ii, "report_name"); //alert(reportData); var useFlag = this.getXmlInt(xmlDoc, ii, "use_flag"); //alert(useFlag); if (ipData == clientIp && report == reportData && useFlag == 1) { //auto_print var xAutoPrint = this.getXmlString(xmlDoc, ii, "auto_print"); if (xAutoPrint == "true" || xAutoPrint == "false") { this.autoPrint = xAutoPrint == "true"; } //auto_close_dialog var xAutoCloseDialog = this.getXmlString(xmlDoc, ii, "auto_close_dialog"); if (xAutoCloseDialog == "true" || xAutoCloseDialog == "false") { this.autoCloseDialog = xAutoCloseDialog == "true"; } //paper_name_id var xPaperNameId = this.getXmlString(xmlDoc, ii, "paper_name_id"); if (xPaperNameId != "") { this.paperNameId = xPaperNameId; } //paper_width var xPaperWidth = this.getXmlInt(xmlDoc, ii, "paper_width") if (xPaperWidth != 0) { this.paperWidth = xPaperWidth; } //paper_height var xPaperHeight = this.getXmlInt(xmlDoc, ii, "paper_height") if (xPaperHeight != 0) { this.paperHeight = xPaperHeight; } //paper_inch_mm //printer_name var xPrinterName = this.getXmlString(xmlDoc, ii, "printer_name"); if (xPrinterName != "") { this.printerName = xPrinterName; } //head_height var xHeadHeight = this.getXmlInt(xmlDoc, ii, "head_height"); if (xHeadHeight != 0) { this.head.height = xHeadHeight; } //detail_height var xDetailHeight = this.getXmlInt(xmlDoc, ii, "detail_height"); if (xDetailHeight != 0) { this.detail.height = xDetailHeight; } //font_name var xFontName = this.getXmlString(xmlDoc, ii, "font_name"); if (xFontName != "") { this.font.name = xFontName; } //font_size var xFontSize = this.getXmlInt(xmlDoc, ii, "font_size"); if (xFontSize != 0) { this.font.size = xFontSize; } //runtime_top var xRuntimeTop = this.getXmlInt(xmlDoc, ii, "runtime_top"); if (xRuntimeTop != 0) { this.runtimeTop = xRuntimeTop; } //runtime_left var xRuntimeLeft = this.getXmlInt(xmlDoc, ii, "runtime_left"); if (xRuntimeLeft != 0) { this.runtimeLeft = xRuntimeLeft; } //use_flag break; } } catch (err) { break; } } } //ส่วน Head this.preWriteHead(); //ส่วน Detail this.preWriteDetail(); //ส่วน Foot this.preWriteFoot(); //ส่งข้อความให้ Dialog รู้ว่าหน้านี้โหลดเสร็จแล้วเพื่อพร้อม print try { parent.acceptIframe(this.currentPage, this.autoPrint, this.autoCloseDialog, this.printerName); parent.setPaperSize(this.paperNameId, this.paperWidth, this.paperHeight, this.paperInchMilli); } catch (err) { } } this.preWriteHead = function () { for (var j = 0; j < this.head.count; j++) { //ประกาศตัวแปร PrintColumn var pc = this.head.columns[j]; //ประกาศตัวแปร columnType var columnType = pc.columnType; //ประกาศ topNew ดึงค่ามาตรงๆ var topNew = pc.top; if (columnType == "data" || columnType == "text" || columnType == "compute") { //ดึงข้อมูลจาก xml ถ้าดึงค่าไม่ได้ให้ break var v = ""; try { if (columnType == "data" || columnType == "compute") { v = defunction(pc.column, 0, columnType, xmlHandle, this.pd, pc.position, this.xml_type); } else if (columnType == "text") { v = pc.text; } } catch (err1) { v = ""; } pc.text = v; this.pd.addHeadData(pc.name, v); } //ใช้คำสั่ง postWrite this.postWrite(pc, topNew, v); } } this.preWriteDetail = function () { for (var i = 0; i < this.rowCount; i++) { for (var j = 0; j < this.detail.count; j++) { //ประกาศตัวแปร PrintColumn ดึงจาก loop var pc = this.detail.columns[j]; //ประกาศตัวแปร columnType var columnType = pc.columnType; //หาค่า valid เพื่อตัดสินใจจะโชว์แถวในแต่ละหน้า var validRow = false; var loopCurrentPage = Math.ceil((i + 1) / this.rowPerPage); validPage = loopCurrentPage == this.currentPage; //หาค่า topNew เพื่อใส่แกน Y var modPage = i % this.rowPerPage; var topNew = (modPage * this.detail.height) + this.head.height; topNew = topNew + pc.top; //หาค่า v เพื่อนำไปแสดงหน้าใบเสร็จ ตามประเภท columnType var v = ""; try { if (columnType == "data" || columnType == "compute") { //หาค่าจาก xml v = defunction(pc.column, i, columnType, xmlHandle, this.pd, pc.position, this.xml_type); } else if (columnType == "text") { //ดึงจาก text v = pc.text; } else if (columnType == "running") { //หาค่า running v = (i + 1) + ""; } } catch (err1) { v = ""; } //เซ็ตค่าใส่ text pc.text = v; this.pd.addData(i, pc.name, v); if (validPage) { //ใช้คำสั่ง postWrite this.postWrite(pc, topNew, v); } } } } this.preWriteFoot = function () { for (var j = 0; j < this.foot.count; j++) { //ประกาศตัวแปร Print Column var pc = this.foot.columns[j]; //ดึงค่า columnType var columnType = pc.columnType; var v = ""; try { if (columnType == "data" || columnType == "compute") { v = defunction(pc.column, this.rowCount - 1, columnType, xmlHandle, this.pd, pc.position, this.xml_type); } else if (columnType == "text") { v = pc.text; } else if (columnType == "sum") { var vs = 0; var sumCount = this.rowCount; if (this.totalPage > this.currentPage) { sumCount = this.currentPage * this.rowPerPage; } for (var i = 0; i < sumCount; i++) { var vv = 0; try { if (this.xml_type == "datatable") { vv = this.xmlHandle.xmlDoc.getElementsByTagName(pc.column.toUpperCase())[i].childNodes[0].nodeValue + ""; } else { vv = this.xmlHandle.xmlDoc.getElementsByTagName(pc.column.toLowerCase())[i].childNodes[0].nodeValue + ""; } } catch (err) { } try { vv = parseFloat(vv); } catch (err) { vv = 0; } vs += vv; } v = vs + ""; } else if (columnType == "sumc") { var vs = 0; var sumCount = this.rowCount; if (this.totalPage > this.currentPage) { sumCount = this.currentPage * this.rowPerPage; } for (var i = 0; i < sumCount; i++) { var vv = 0; try { vv = parseFloat(this.pd.getData(i, pc.column) + ""); } catch (err) { } vs += vv; } v = vs + ""; } else if (columnType == "count") { var sumCount = this.rowCount; if (this.totalPage > this.currentPage) { sumCount = this.currentPage * this.rowPerPage; } v = sumCount + ""; //alert(pc.name); //alert(v); } } catch (err1) { v = ""; } pc.text = v; this.pd.addFootData(pc.name, v); //ดึงค่าใส่ตรงๆ var topNew = pc.top; //ใช้คำสั่ง postWrite this.postWrite(pc, topNew, v); } } this.postWrite = function (pc, topNew, v) { //สร้าง Element ใหม่ var elNew = document.createElement("div"); elNew.style.position = "absolute"; var delTopRuntime = 0; var delLeftRuntime = 0; if (this.mode == "runtime") { delTopRuntime = this.runtimeTop; //43; delLeftRuntime = this.runtimeLeft; //50; } elNew.style.top = (parseInt(topNew + "") + delTopRuntime) + "px"; //ใช้ topNew elNew.style.left = (parseInt(pc.left + "") + delLeftRuntime) + "px"; elNew.style.textAlign = pc.align; elNew.style.fontFamily = pc.font.name == "" ? this.font.name : pc.font.name; elNew.style.fontSize = pc.font.size == 0 ? this.font.size + "px" : pc.font.size + "px"; if (pc.width > 0) { elNew.style.width = pc.width + "px"; } if (pc.bgColor != "") { elNew.style.backgroundColor = pc.bgColor; } if (pc.font.color != "") { elNew.style.color = pc.font.color; } if (pc.format != "") { v = inputFormat(v, pc.format, this.xml_type); } if (pc.font.i) v = "" + v + ""; if (pc.font.u) v = "" + v + ""; if (pc.font.s) v = "" + v + ""; if (pc.font.b) v = "" + v + ""; if (!pc.visible) elNew.style.visibility = "hidden"; if (pc.columnType == "line") { if (pc.bgColor == "") { elNew.style.backgroundColor = "#000000"; } elNew.style.height = pc.height + "px"; } else { elNew.innerHTML = v; } //นำ Element ใหม่ ใส่ไปใน div_main document.getElementById("div_main").appendChild(elNew); } }