first commit
This commit is contained in:
@@ -0,0 +1,234 @@
|
||||
/*******************************************************************
|
||||
|
||||
Module : /assets/js/print.js
|
||||
Desc. : JS Class Printer & Printer POS
|
||||
Created By : Siau Pheng ([email protected]).
|
||||
Created Date : May 27th, 2007.
|
||||
Last Modified : December 26th, 2025.
|
||||
|
||||
(c) 2007 - 2025, 3FONIA Software; WWW.3FONIA.COM.
|
||||
|
||||
*******************************************************************/
|
||||
|
||||
var cPRINTER = function(url, options){
|
||||
var vars = {
|
||||
'url': '',
|
||||
'zoom': 90,
|
||||
'param': []
|
||||
};
|
||||
this.construct = function(url, options){
|
||||
$.extend(vars, options);
|
||||
vars.url = url;
|
||||
};
|
||||
this.set_url = function (url){
|
||||
vars.url = url;
|
||||
};
|
||||
this.set_zoom = function (zoom){
|
||||
if (zoom) vars.zoom = zoom;
|
||||
},
|
||||
this.add_param = function (par_id, par_val){
|
||||
var _getvalue = (par_val) ? par_id + "=" + escape(par_val) : par_id;
|
||||
vars.param.push(_getvalue);
|
||||
},
|
||||
this.doPrint = function(){
|
||||
if (!vars.url) {
|
||||
console.warn(vars.param + ", cek url php!");
|
||||
return false;
|
||||
}
|
||||
vars.data = FoniaJS.RequestParam(vars.param);
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url: vars.url,
|
||||
data: (vars.data).join('&'),
|
||||
success: function(txt){
|
||||
var $printData = $('#dataPrint');
|
||||
if (!$printData.length) {
|
||||
var $printData = $('<div/>', {id:'dataPrint','class':'d-none d-print-block print-body'}).appendTo('body');
|
||||
}
|
||||
$printData.html(txt);
|
||||
window.print();
|
||||
},
|
||||
error: (typeof vars.onFailure === "function") ? vars.onFailure : null,
|
||||
});
|
||||
return this;
|
||||
};
|
||||
this.doPreview = function(){
|
||||
if (!vars.url) console.warn(vars.param + ", cek url php!");
|
||||
vars.data = FoniaJS.RequestParam(vars.param);
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url: vars.url,
|
||||
data: (vars.data).join('&'),
|
||||
success: function(txt){
|
||||
if ('' == txt) {
|
||||
FoniaJS.showInfo('Tidak ada data untuk pembuatan laporan !');
|
||||
return false;
|
||||
}
|
||||
var _wtxt =
|
||||
'<div class="modal fade" id="winAPPPrint" tabindex="-1" role="dialog" data-bs-keyboard="false" data-bs-show="false" data-bs-backdrop="static" aria-labelledby="winAPPPrintLabel" aria-hidden="true">'+
|
||||
'<div class="modal-dialog modal-dialog-centered modal-dialog-scrollable modal-fs" role="document">'+
|
||||
'<div class="modal-content">'+
|
||||
'<div class="modal-header"><h5 class="modal-title" id="winAPPPrintLabel">Preview Laporan</h5><div class="ms-auto"><button class="btn me-2" id="tombCetak" type="button" aria-label="Cetak"><i class="bi-printer me-2"></i><span aria-hidden="true">Cetak</span></button><button class="btn" type="button" data-bs-dismiss="modal" aria-label="Tutup"><i class="bi-x-lg me-2"></i><span aria-hidden="true">Tutup</span></button></div></div>'+
|
||||
'<div class="modal-body print-body" id="divPrintP">'+txt+'</div>'+
|
||||
'</div></div></div>';
|
||||
if ($('#winAPPPrint')) $('#winAPPPrint').modal('dispose').remove();
|
||||
$(_wtxt).on('shown.bs.modal', function(e){
|
||||
$('#winAPPPrint').find('#tombCetak').click(function(){
|
||||
var $printData = $('#dataPrint');
|
||||
if (!$printData.length) {
|
||||
var $printData = $('<div/>', {id:'dataPrint','class':'d-none d-print-block print-body'}).appendTo('body');
|
||||
}
|
||||
$printData.html($('#divPrintP').html());
|
||||
FoniaJS.closeWindow($('#winAPPPrint'));
|
||||
window.print();
|
||||
});
|
||||
}).on('hidden.bs.modal', function(e){
|
||||
$(this).remove();
|
||||
}).modal('show');
|
||||
},
|
||||
error: (typeof vars.onFailure === "function") ? vars.onFailure : null,
|
||||
});
|
||||
};
|
||||
this.construct(url, options);
|
||||
};
|
||||
|
||||
var cPRINTERPOS = function(url, options) {
|
||||
var b_default = false;
|
||||
var _options = {
|
||||
'url': '',
|
||||
'param': [],
|
||||
'printer': null,
|
||||
'cut_paper': "\x1D\x56\x41",
|
||||
'open_draw': "\x1B\x70\x30\x19\x19",
|
||||
'chr_norm': "\x1B\x21\x01",
|
||||
'chr_bold': "\x1B\x21\x10",
|
||||
'chr_sbold': "\x1B\x21\x31",
|
||||
'chr_uline': "\x1B\x21\x80"
|
||||
};
|
||||
this.construct = function(url, options) {
|
||||
$.extend(_options, options);
|
||||
_options.url = url;
|
||||
if (FoniaJS._def_printer) _options.printer = FoniaJS._def_printer;
|
||||
};
|
||||
this.detectPrinter = function() {
|
||||
if (!qz.websocket.isActive()) {
|
||||
FoniaJS.showInfo('Status Printer belum Active !');
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
this.getPrinters = function() {
|
||||
return [];
|
||||
};
|
||||
this.setPrinter = function(nprint) {
|
||||
(nprint != null) && (_options.printer = nprint);
|
||||
};
|
||||
this.useDefaultPrinter = function() {
|
||||
b_default = true;
|
||||
};
|
||||
this.set_url = function(url) {
|
||||
_options.url = url;
|
||||
};
|
||||
this.add_param = function(par_id, par_val) {
|
||||
var _getvalue = (par_val) ? par_id + '=' + escape(par_val) : par_id;
|
||||
_options.param.push(_getvalue);
|
||||
};
|
||||
this.exec = function() {
|
||||
if (!this.detectPrinter()) return;
|
||||
if (this.b_default) {
|
||||
var parent = this;
|
||||
qz.printers.getDefault().then(function(data) {
|
||||
var nama_p = data;
|
||||
if (data.name != undefined) {
|
||||
nama_p = data.name;
|
||||
}
|
||||
parent._options.param.push('Printer='+nama_p);
|
||||
return parent.do_print(nama_p, parent._options.url, parent._options.param);
|
||||
}, function(data) {
|
||||
FoniaJS.showInfo(data);
|
||||
});
|
||||
} else {
|
||||
var _nprinter = _options.printer;
|
||||
if (typeof _nprinter === 'object' && _nprinter.name == undefined) {
|
||||
if (_nprinter.file != undefined) _nprinter = _nprinter.file;
|
||||
if (_nprinter.host != undefined) _nprinter = _nprinter.host + ":" + _nprinter.port;
|
||||
} else {
|
||||
if (_nprinter.name != undefined) _nprinter = _nprinter.name;
|
||||
if (_nprinter== undefined) _nprinter = 'None';
|
||||
}
|
||||
_options.param.push('Printer='+_nprinter);
|
||||
this.do_print(_options.printer, _options.url, _options.param);
|
||||
}
|
||||
};
|
||||
this.do_print = function(nama_p, url, params) {
|
||||
var qzcfg = qz.configs.create(nama_p);
|
||||
new Request.JSON({
|
||||
url: url,
|
||||
sendParam: params,
|
||||
onSuccess: function(ajson) {
|
||||
if (typeof ajson === "object") {
|
||||
var hasil_cetak = (ajson.type == "escp") ? this.gen_escp(ajson) : this.gen_epl(ajson);
|
||||
qz.print(qzcfg, hasil_cetak);
|
||||
} else {
|
||||
FoniaJS.showInfo('Error dalam proses Cetak.');
|
||||
}
|
||||
}.bind(this)
|
||||
}).send();
|
||||
};
|
||||
this.gen_escp = function(adata) {
|
||||
var ohead = [];
|
||||
var osheet = [], ocetak = [];
|
||||
ohead.push("\x1B\x40");
|
||||
ohead.push("\x1B\x61\x01");
|
||||
if (adata.config.logo != null) {
|
||||
ohead.push({type:'raw',format:'image',data:adata.config.logo,options:{language:'ESCPOS',dotDensity:'double'}});
|
||||
ohead.push("\x1B\x32");
|
||||
}
|
||||
if (parseInt(adata.config.fontsize) == 2) {
|
||||
_options.chr_norm = "\x1B\x21\x02";
|
||||
}
|
||||
if (adata.header.length > 0) {
|
||||
for (i = 0; i < adata.header.length; i++) {
|
||||
if (i==0 && adata.config.logo==null) ohead.push(_options.chr_bold);
|
||||
ohead.push(adata.header[i] + "\x0A");
|
||||
if (i==0 && adata.config.logo==null) ohead.push(_options.chr_norm);
|
||||
}
|
||||
}
|
||||
for (s = 0; s < adata.content.length; s++) {
|
||||
if (adata.content[s].length == 0) continue;
|
||||
osheet = ohead.slice(0);
|
||||
for (i = 0; i < adata.content[s].length; i++) {
|
||||
var tmp_cont = adata.content[s][i];
|
||||
tmp_cont = tmp_cont.replace('_SBOLD_', _options.chr_sbold)
|
||||
.replace('_BOLD_', _options.chr_bold)
|
||||
.replace('_NORM_', _options.chr_norm)
|
||||
.replace('_ULINE_', _options.chr_uline);
|
||||
osheet.push(tmp_cont + "\x0A");
|
||||
}
|
||||
if (adata.config.cutpaper) {
|
||||
osheet.push(_options.cut_paper + "\x0A");
|
||||
}
|
||||
ocetak = ocetak.concat(osheet);
|
||||
}
|
||||
var notaout = [];
|
||||
if (adata.config.opendraw) {
|
||||
notaout.push(_options.open_draw);
|
||||
}
|
||||
for (i = 0; i < adata.config.times; i++) {
|
||||
notaout = notaout.concat(ocetak);
|
||||
}
|
||||
return notaout;
|
||||
};
|
||||
this.gen_epl = function(adata) {
|
||||
var cetak = [];
|
||||
for (i = 0; i < adata.content.length; i++) {
|
||||
cetak.push(adata.content[i] + "\r\n");
|
||||
}
|
||||
var notaout = [];
|
||||
for (i = 0; i < adata.config.times; i++) {
|
||||
notaout = notaout.concat(cetak);
|
||||
}
|
||||
return notaout;
|
||||
};
|
||||
this.construct(url, options);
|
||||
};
|
||||
Reference in New Issue
Block a user