
1 //收費系統打印機功能:收費/退費,需要使用到lodop 2 var LODOP;//打印機 3 $(function () { 4 //初始化 5 $("body").append('<object id="LODOP_OB" classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" width=0 height=0> <embed id="LODOP_EM" type="application/x-print-lodop" width=0 height=0></embed></object>'); 6 7 LODOP = getLodop(); 8 }); 9 10 //檢測是否安裝了打印插件 11 function CheckLodop() 12 { 13 var oOBJECT = document.getElementById('LODOP_OB'), oEMBED = document.getElementById('LODOP_EM'); 14 var LODOP = oEMBED; 15 try { 16 var isIE = (navigator.userAgent.indexOf('MSIE') >= 0) || (navigator.userAgent.indexOf('Trident') >= 0); 17 var is64IE = isIE && (navigator.userAgent.indexOf('x64') >= 0); 18 if (isIE) LODOP = oOBJECT; 19 if ((LODOP == null) || (typeof (LODOP.VERSION) == "undefined")) { 20 return false; 21 } else 22 if (LODOP.VERSION < "6.1.6.1") { 23 return false; 24 } 25 return true; 26 } catch (err) { 27 return false; 28 } 29 } 30 31 function getLodop() { 32 var oOBJECT = document.getElementById('LODOP_OB'), oEMBED = document.getElementById('LODOP_EM'); 33 /************************** 34 本函數根據瀏覽器類型決定采用哪個對象作為控件實例: 35 IE系列、IE內核系列的瀏覽器采用oOBJECT, 36 其它瀏覽器(Firefox系列、Chrome系列、Opera系列、Safari系列等)采用oEMBED, 37 對於64位瀏覽器指向64位的安裝程序install_lodop64.exe。 38 **************************/ 39 var strHtmInstall = "打印控件未安裝!點擊這里<a href='/patch/install_lodop32.exe' target='_self'>執行安裝</a>,安裝后請刷新頁面或重新進入。"; 40 var strHtmUpdate = "打印控件需要升級!點擊這里<a href='/patch/install_lodop32.exe' target='_self'>執行升級</a>,升級后請重新進入。"; 41 var strHtm64_Install = "打印控件未安裝!點擊這里<a href='/patch/install_lodop64.exe' target='_self'>執行安裝</a>,安裝后請刷新頁面或重新進入。"; 42 var strHtm64_Update = "打印控件需要升級!點擊這里<a href='/patch/install_lodop64.exe' target='_self'>執行升級</a>,升級后請重新進入。"; 43 var strHtmFireFox = "注意:如曾安裝過Lodop舊版附件npActiveXPLugin,請在【工具】->【附加組件】->【擴展】中先卸它。"; 44 var LODOP = oEMBED; 45 try { 46 var isIE = (navigator.userAgent.indexOf('MSIE') >= 0) || (navigator.userAgent.indexOf('Trident') >= 0); 47 var is64IE = isIE && (navigator.userAgent.indexOf('x64') >= 0); 48 var errhtml = ''; 49 if (isIE) LODOP = oOBJECT; 50 if ((LODOP == null) || (typeof (LODOP.VERSION) == "undefined")) { 51 if (navigator.userAgent.indexOf('Firefox') >= 0) 52 { 53 errhtml = strHtmFireFox 54 } 55 if (is64IE) { 56 errhtml = strHtm64_Install; 57 } 58 else if (isIE) { 59 errhtml = strHtmInstall; 60 } 61 else 62 { 63 errhtml = strHtmInstall; 64 } 65 ShowPrintErr(errhtml); 66 return LODOP; 67 } else { 68 if (LODOP.VERSION < "6.1.6.1") { 69 if (is64IE) { 70 errhtml = strHtm64_Update; 71 } 72 else if (isIE) { 73 errhtml = strHtmUpdate; 74 } 75 else { 76 errhtml = strHtmUpdate; 77 } 78 ShowPrintErr(errhtml); 79 return LODOP; 80 } 81 } 82 //=====如下空白位置適合調用統一功能:===== 83 84 85 //======================================= 86 return LODOP; 87 } catch (err) { 88 if (is64IE) 89 { 90 errhtml = "Error:" + strHtm64_Install; 91 } 92 else 93 { 94 errhtml = "Error:" + strHtmInstall; 95 } 96 ShowPrintErr(errhtml); 97 return LODOP; 98 } 99 return LODOP; 100 } 101 102 function ShowPrintErr(h) 103 { 104 if (h) 105 { 106 h = "<p>" + h; 107 h += " <span id='az_close' style='cursor:pointer;display:inline-block;position:absolute;right:0;' title='點擊關閉'>X</span></p>"; 108 $(".az_remind").html(h).fadeIn(2000); 109 $("#az_close").click(function () { 110 $(".az_remind").fadeOut(2000); 111 }); 112 setTimeout(function () { $(".az_remind").fadeOut(2000); }, 5000); 113 } 114 } 115 116 (function ($) { 117 function PrintBase(printObj) { 118 this.nf = { FontName: '宋體', FontSize: 9 }; 119 this.hf = { FontName: '宋體', FontSize: 20 }; 120 this.printObj = printObj; 121 this.printBegin = function () { 122 LODOP.PRINT_INIT("票據" + Math.random()); 123 LODOP.SET_PRINTER_INDEXA(printObj.PrintSet.printerName); 124 LODOP.SET_PRINT_MODE("POS_BASEON_PAPER", true); 125 }; 126 this.DX = function (num) { 127 var strOutput = ""; 128 var strUnit = '仟佰拾億仟佰拾萬仟佰拾元角分'; 129 num += "00"; 130 var intPos = num.indexOf('.'); 131 if (intPos >= 0) 132 num = num.substring(0, intPos) + num.substr(intPos + 1, 2); 133 strUnit = strUnit.substr(strUnit.length - num.length); 134 for (var i = 0; i < num.length; i++) 135 strOutput += '零壹貳叄肆伍陸柒捌玖'.substr(num.substr(i, 1), 1) + strUnit.substr(i, 1); 136 return strOutput.replace(/零角零分$/, '整').replace(/零[仟佰拾]/g, '零').replace(/零{2,}/g, '零').replace(/零([億|萬])/g, '$1').replace(/零+元/, '元').replace(/億零{0,3}萬/, '億').replace(/^元/, "零元"); 137 }; 138 this.printEnd = function () { 139 LODOP.PRINT(); 140 }; 141 this.printFee = function () { }; 142 this.printFeeRefund = function () { }; 143 this.measureString = function (s, f) { 144 //計算文字寬度 145 return { width: s.length * f.FontSize, height: f.FontSize }; 146 } 147 this.printLine = function (x1, y1, x2) { 148 LODOP.ADD_PRINT_Line(y1, x1,y1, x2, 0, 1); 149 }; 150 this.printString = function (s, f, x, y) { 151 LODOP.ADD_PRINT_TEXT(y, x, 1000, 25, s); 152 LODOP.SET_PRINT_STYLEA(0, "FontName", f.FontName); 153 LODOP.SET_PRINT_STYLEA(0, "FontSize", f.FontSize); 154 }; 155 } 156 function PrinterPinA(printObj, printCopys, printCopysYAdd,y5,y6,y7,y8,y9) { 157 PrintBase.call(this, printObj); 158 this.maxCount = 12; 159 this.currentPage = 1; 160 this.xTitle = 370; 161 this.yy = 25; 162 this.x1 = 70; 163 this.x2 = 200; 164 this.x3 = 330; 165 this.x4 = 460; 166 this.x5 = 630; 167 this.x6 = 727; 168 this.y1 = 65; 169 this.y2 = 80; 170 this.y3 = 90; 171 this.y4 = 115; 172 this.y5 = y5; 173 this.y6 = y6; 174 this.y7 = y7; 175 this.y8 = y8; 176 this.y9 = y9; 177 this.printCopys = printCopys; 178 this.printCopysYAdd = printCopysYAdd; 179 this.totalPages = Math.ceil(printObj.listFee.length / this.maxCount); 180 this.printNormal = function (yAdd) { 181 if (yAdd > 0) { 182 this.printLine(0, yAdd, 900); 183 } 184 var title = this.printObj.schName + this.printObj.title; 185 186 //計算文字寬度 187 var xTemp = (844 - this.measureString(title, this.hf).width) / 2; 188 this.printString(title, this.hf, xTemp, this.yy + yAdd); 189 this.printString("班級:" + this.printObj.clsName, this.nf, this.x1, this.y1 + yAdd); 190 this.printString("姓名:" + this.printObj.userName, this.nf, this.x2, this.y1 + yAdd); 191 this.printString("日期:" + this.printObj.payTime, this.nf, this.x4, this.y1 + yAdd); 192 this.printString("單號:" + this.printObj.serialNum, this.nf, this.x5, this.y1 + yAdd); 193 this.printLine(this.x1, this.y2 + yAdd, this.x6); 194 this.printLine(this.x1, this.y6 + yAdd, this.x6); 195 this.printString("合計:", this.nf, this.x1, this.y5 + yAdd); 196 var printphoneX = this.x1; 197 var printaddressY = this.y7; 198 if (printObj.schName) { 199 printphoneX = this.x2; 200 printaddressY = this.y8; 201 this.printString(printObj.schName, this.nf, this.x1, this.y7 + yAdd); 202 } 203 if (printObj.tel) { 204 printaddressY = this.y8; 205 this.printString("電話:" + printObj.tel, this.nf, printphoneX, this.y7 + yAdd); 206 } 207 if (printObj.printSeal) { 208 printaddressY = this.y8; 209 this.printString("蓋章", this.nf, this.x5, this.y7 + yAdd); 210 } 211 if (printObj.schAddress) { 212 this.printString("地址:" + printObj.schAddress, this.nf, this.x1, printaddressY + yAdd); 213 } 214 var fstr = "退款人"; 215 if (printObj.feeType==1) { 216 fstr = "收款人"; 217 } 218 fstr += ":" + printObj.payee; 219 this.printString(fstr, this.nf, this.x5, printaddressY + yAdd); 220 }; 221 this.printFee = function () {//打印收費 222 this.printBegin(); 223 for (var i = 0; i < this.printCopys; i++) { 224 var yAdd = i * this.printCopysYAdd; 225 this.printNormal(yAdd); 226 var total = 0, real = 0, qian = 0, discount = 0; 227 var y = this.y4 + yAdd; 228 this.printString("項目", this.nf, this.x1, this.y3 + yAdd); 229 this.printString("應收金額(元)", this.nf, this.x2, this.y3 + yAdd); 230 this.printString("實收金額(元)", this.nf, this.x3, this.y3 + yAdd); 231 this.printString("備注", this.nf, this.x4, this.y3 + yAdd); 232 233 //遍歷打印 234 for (var j = (this.currentPage - 1) * this.maxCount; j < printObj.listFee.length && j < this.currentPage * this.maxCount; j++) { 235 var temp = printObj.listFee[j]; 236 this.printString(temp.feeName, this.nf, this.x1, y); 237 this.printString(temp.totalMoney.toFixed(2), this.nf, this.x2, y); 238 this.printString(temp.realMoney.toFixed(2), this.nf, this.x3, y); 239 this.printString(temp.payMsg, this.nf, this.x4, y); 240 241 total += temp.totalMoney; 242 real += temp.realMoney; 243 qian += temp.qianOldMoney; 244 discount += temp.discountMoney; 245 y += this.yy; 246 } 247 248 this.printString("應收:" + total.toFixed(2) + "元", this.nf, this.x2, this.y5 + yAdd); 249 this.printString("實收:" + real.toFixed(2) + "元", this.nf, this.x3, this.y5 + yAdd); 250 this.printString("大寫:" + this.DX(real), this.nf, this.x4, this.y5 + yAdd); 251 if (qian) { 252 this.printString("欠費:" + qian.toFixed(2) + "元", this.nf, this.x3, this.y9 + yAdd); 253 } 254 if (discount) { 255 this.printString("減免:" + discount.toFixed(2) + "元", this.nf, this.x4, this.y9 + yAdd); 256 } 257 } 258 this.printEnd(); 259 if (this.currentPage * this.maxCount < printObj.listFee.length) { 260 this.currentPage++; 261 this.printFee(); 262 } 263 }; 264 this.printFeeRefund = function () {//打印退費 265 this.printBegin(); 266 for (var i = 0; i < this.printCopys; i++) { 267 var yAdd = i * this.printCopysYAdd; 268 this.printNormal(yAdd); 269 var real = 0; 270 var y = this.y4 + yAdd; 271 this.printString("退費項目", this.nf, this.x1, this.y3 + yAdd); 272 this.printString("退費金額(元)", this.nf, this.x3, this.y3 + yAdd); 273 this.printString("備注", this.nf, this.x4, this.y3 + yAdd); 274 275 //遍歷打印 276 for (var j = (this.currentPage - 1) * this.maxCount; j < printObj.listFee.length && j < this.currentPage * this.maxCount; j++) { 277 var temp = printObj.listFee[j]; 278 this.printString(temp.feeName, this.nf, this.x1, y); 279 this.printString(temp.realMoney.toFixed(2), this.nf, this.x3, y); 280 this.printString(temp.payMsg, this.nf, this.x4, y); 281 282 real += temp.realMoney; 283 y += this.yy; 284 } 285 286 this.printString("退費:" + real.toFixed(2) + "元", this.nf, this.x3, this.y5 + yAdd); 287 this.printString("大寫:" + this.DX(real), this.nf, this.x4, this.y5 + yAdd); 288 } 289 this.printEnd(); 290 if (this.currentPage * this.maxCount < printObj.listFee.length) { 291 this.currentPage++; 292 this.printFee(); 293 } 294 } 295 } 296 297 function PrinterPin140(printObj) {//a4 2等分 298 PrinterPinA.call(this, printObj, 1, -1, 415, 455, 465, 490, 440); 299 } 300 301 function PrinterPinA4(printObj)//a4 雙份 302 { 303 PrinterPinA.call(this, printObj, 2, 540, 415, 455, 465, 490, 440); 304 } 305 function PrinterPin93(printObj) { 306 PrinterPinA.call(this, printObj, 1, -1, 265, 305, 310, 335, 290); 307 } 308 function PrinterTicket(printObj,x1,x2,x3,x4,totalWidth,hfSize,hf2Size,addrSize) 309 { 310 PrintBase.call(this, printObj); 311 this.y = 5; 312 this.x1 = x1; 313 this.x2 = x2; 314 this.x3 = x3; 315 this.x4 = x4; 316 this.yy = 15; 317 this.yh = 20; 318 this.yx = 10; 319 this.totalWidth = totalWidth; 320 this.addrSize = addrSize; 321 this.hf = { FontName: '宋體', FontSize: hfSize }; 322 this.hf2 = { FontName: '宋體', FontSize: hf2Size }; 323 this.printFoot = function (total, qian, real, discount) { 324 this.printLine(this.x1, this.y, this.totalWidth - this.x1); 325 this.y += this.yx; 326 this.printString("合計:" + printObj.listFee.length + "項", this.nf, this.x1, this.y); 327 this.y += this.yy; 328 if (printObj.feeType) { 329 this.printString("應收:" + total.toFixed(2) + "元", this.nf, this.x1, this.y); 330 this.y += this.yy; 331 if (qian) { 332 this.printString("欠費:" + qian.toFixed(2) + "元", this.nf, this.x1, this.y); 333 this.y += this.yy; 334 } 335 if (discount) { 336 this.printString("減免:" + discount.toFixed(2) + "元", this.nf, this.x1, this.y); 337 this.y += this.yy; 338 } 339 this.printString("實收:" + real.toFixed(2) + "元", this.nf, this.x1, this.y); 340 this.y += this.yy; 341 } else { 342 this.printString("退費:" + real.toFixed(2) + "元", this.nf, this.x1, this.y); 343 this.y += this.yy; 344 } 345 this.printString("大寫:" + this.DX(real), this.nf, this.x1, this.y); 346 this.y += this.yy; 347 var fstr = "退款人"; 348 if (printObj.feeType==1) { 349 fstr = "收款人"; 350 } 351 fstr += ":" + printObj.payee; 352 this.printString(fstr, this.nf, this.x1, this.y); 353 this.y += this.yy; 354 if (printObj.tel || printObj.schAddress) { 355 this.printLine(this.x1, this.y, (this.totalWidth - this.x1)); 356 this.y += this.yx; 357 } 358 if (printObj.tel) { 359 this.printString("電話:" + printObj.tel, this.nf, this.x1, this.y); 360 this.y += this.yy; 361 } 362 if (printObj.schAddress) { 363 var add1 = printObj.schAddress; 364 var add2 = ''; 365 //判斷要打印的行數 366 var rows = Math.ceil(add1.length / this.addrSize); 367 for (var i = 0; i < rows; i++) { 368 var end=(i+1)*this.addrSize; 369 if(end>add1.length)end=add1.length; 370 var temp = add1.substring(i * this.addrSize, end); 371 if (i == 0) { 372 this.printString("地址:" + temp, this.nf, this.x1, this.y); 373 } else { 374 this.printString(temp, this.nf, this.x1 * 8, this.y); 375 } 376 this.y += this.yy; 377 } 378 } 379 }; 380 this.printHead = function () { 381 if (printObj.schName) { 382 var fs = this.measureString(printObj.schName, this.hf); 383 var xtemp1 = (this.totalWidth - fs.width) / 2; 384 this.printString(printObj.schName, this.hf, xtemp1, this.y); 385 this.y += this.yh; 386 } 387 var f2 = this.measureString(printObj.title, this.hf2); 388 var xtemp2 = (this.totalWidth - f2.width) / 2; 389 this.printString(printObj.title, this.hf2, xtemp2, this.y); 390 this.y += this.yh; 391 this.printString("日期:" + printObj.payTime, this.nf, this.x1, this.y); 392 this.printString("單號:" + printObj.serialNum, this.nf, this.x2, this.y); 393 this.y += this.yy; 394 this.printString("姓名:" + printObj.userName, this.nf, this.x1, this.y); 395 this.printString("班級:" + printObj.clsName, this.nf, this.x2, this.y); 396 this.y += this.yy; 397 this.printLine(this.x1, this.y, this.totalWidth - this.x1); 398 this.y += this.yx; 399 }; 400 this.printFee = function () {//打印收費 401 this.printBegin(); 402 this.printHead(); 403 var total = 0, real = 0, qian = 0, discount = 0; 404 this.printString("項目", this.nf, this.x1, this.y); 405 this.printString("應收(元)", this.nf, this.x3, this.y); 406 this.printString("實收(元)", this.nf, this.x4, this.y); 407 this.y += this.yy; 408 409 //遍歷打印 410 for (var j = 0; j < printObj.listFee.length; j++) { 411 var temp = printObj.listFee[j]; 412 var name = temp.feeName; 413 if(temp.payMsg) 414 { 415 name+='(' + temp.payMsg + ')'; 416 } 417 this.printString(name,this.nf, this.x1, this.y); 418 this.y+=this.yy; 419 this.printString(temp.totalMoney.toFixed(2), this.nf, this.x3, this.y); 420 this.printString(temp.realMoney.toFixed(2), this.nf, this.x4, this.y); 421 total += temp.totalMoney; 422 real += temp.realMoney; 423 qian += temp.qianOldMoney; 424 discount += temp.discountMoney; 425 this.y += this.yy; 426 } 427 428 this.printFoot(total, qian, real, discount); 429 this.printEnd(); 430 }; 431 this.printFeeRefund = function () {//打印退費 432 this.printBegin(); 433 this.printHead(); 434 var real = 0; 435 this.printString("項目", this.nf, this.x1, this.y); 436 this.printString("退費(元)", this.nf, this.x4, this.y); 437 this.y += this.yy; 438 439 //遍歷打印 440 for (var j = 0; j < printObj.listFee.length ; j++) { 441 var temp = printObj.listFee[j]; 442 var name = temp.feeName; 443 if (temp.payMsg) { 444 name += '(' + temp.payMsg + ')'; 445 } 446 this.printString(name, this.nf, this.x1, this.y); 447 this.y += this.yy; 448 this.printString(temp.realMoney.toFixed(2), this.nf, this.x4, this.y); 449 real += temp.realMoney; 450 this.y += this.yy; 451 } 452 this.printFoot(0, 0, real, 0); 453 this.printEnd(); 454 }; 455 } 456 function PrinterTicket80(printObj) { 457 PrinterTicket.call(this, printObj, 5, 180, 120, 200, 280, 12, 10,20); 458 } 459 function PrinterTicket76(printObj) { 460 PrinterTicket.call(this, printObj, 5, 155, 105, 185, 266, 12, 10,18); 461 } 462 function PrinterTicket50(printObj) { 463 PrinterTicket.call(this, printObj, 5, 180, 60, 120, 192, 10, 8,11); 464 this.printHead = function () { 465 if (printObj.schName) { 466 var fs = this.measureString(printObj.schName, this.hf); 467 var xtemp1 = (this.totalWidth - fs.width) / 2; 468 this.printString(printObj.schName, this.hf, xtemp1, this.y); 469 this.y += this.yh; 470 } 471 var f2 = this.measureString(printObj.title, this.hf2); 472 var xtemp2 = (this.totalWidth - f2.width) / 2; 473 this.printString(printObj.title, this.hf2, xtemp2, this.y); 474 this.y += this.yh; 475 this.printString("日期:" + printObj.payTime, this.nf, this.x1, this.y); 476 this.y += this.yy; 477 this.printString("單號:" + printObj.serialNum, this.nf, this.x1, this.y); 478 this.y += this.yy; 479 this.printString("班級:" + printObj.clsName, this.nf, this.x1, this.y); 480 this.y += this.yy; 481 this.printString("姓名:" + printObj.userName, this.nf, this.x1, this.y); 482 this.y += this.yy; 483 this.printLine(this.x1, this.y, this.totalWidth - this.x1); 484 this.y += this.yx; 485 }; 486 } 487 $.extend({ 488 Print: function (printObj) { 489 if (printObj.PrintSet.printerName) { 490 var pt; 491 switch (printObj.PrintSet.printerType) { 492 case 1: 493 pt = new PrinterPin140(printObj); 494 break; 495 case 2: 496 pt = new PrinterPin93(printObj); 497 break; 498 case 3: 499 pt = new PrinterTicket80(printObj); 500 break; 501 case 4: 502 pt = new PrinterTicket76(printObj); 503 break; 504 case 5: 505 pt = new PrinterTicket50(printObj); 506 break; 507 case 6: 508 pt = new PrinterPinA4(printObj); 509 break; 510 } 511 if (pt) { 512 var k = 0; 513 var si = setInterval(function () { 514 if (k < printObj.PrintSet.printNumber) { 515 k++; 516 if (printObj.feeType == 1) { 517 pt.printFee(printObj); 518 } else { 519 pt.printFeeRefund(printObj); 520 } 521 } 522 else 523 { 524 clearInterval(si); 525 } 526 }, 200); 527 } 528 } 529 } 530 }); 531 })(jQuery);