Skip to content

Commit

Permalink
[IMP] fiscal_epos_print: Print courtesy receipt
Browse files Browse the repository at this point in the history
  • Loading branch information
SirAionTech committed Oct 17, 2024
1 parent 42f3cd7 commit fe64ee4
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 6 deletions.
4 changes: 4 additions & 0 deletions fiscal_epos_print/models/point_of_sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class PosConfig(models.Model):
fiscal_printer_serial = fields.Char(string='Fiscal Printer Serial')

fiscal_cashdrawer = fields.Boolean(string='Fiscal Printer Open CashDrawer')
epos_print_courtesy_receipt = fields.Boolean(
string="Print courtesy receipt",
help="Print a courtesy receipt when the order has to be invoiced",
)

@api.constrains('printer_ip', 'iface_reprint_done_order')
def _check_fiscal_epos_reprint(self):
Expand Down
74 changes: 73 additions & 1 deletion fiscal_epos_print/static/src/js/epson_epos_print.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,39 @@ odoo.define("fiscal_epos_print.epson_epos_print", function (require) {
var _t = core._t;
var round_pr = utils.round_precision;

/**
* Return true if the fiscal printer will print a fiscal receipt for `order`.
*/
function eposWillPrintReceipt(order) {
return order.pos.config.printer_ip && !order.is_to_invoice();
};
/**
* Return true if the fiscal printer will print a courtesy receipt for `order`.
*/
function eposWillPrintCourtesyReceipt(order) {
return order.pos.config.printer_ip && order.is_to_invoice() && order.pos.config.epos_print_courtesy_receipt;
};
/**
* Return true if the fiscal printer will print a receipt for `order`.
*/
function eposWillPrint(order) {
return eposWillPrintReceipt(order) || eposWillPrintCourtesyReceipt(order);
};
/**
* Send `receipt` to the fiscal `printer`,
* this might print a receipt (fiscal or not) for `receipt`.
*/
function eposPrint(printer, receipt) {
var order = printer.order;
var result;
if (eposWillPrintReceipt(order)) {
result = printer.printFiscalReceipt(receipt);
} else if (eposWillPrintCourtesyReceipt(order)) {
result = printer.printNonFiscalReceipt(receipt);
}
return result
}

function addPadding(str, padding=4) {
var pad = new Array(padding).fill(0).join('') + str;
return pad.substr(pad.length - padding, padding);
Expand Down Expand Up @@ -436,6 +469,41 @@ odoo.define("fiscal_epos_print.epson_epos_print", function (require) {
console.log(xml);
},

prepareXMLNonFiscalReceipt: function(receipt) {
var xml = document.implementation.createDocument(null, "printerNonFiscal");

var beginNonFiscal = xml.createElement("beginNonFiscal");
beginNonFiscal.setAttribute("operator", receipt.operator || "1");
xml.documentElement.appendChild(beginNonFiscal);

var printNormal = xml.createElement("printNormal");
printNormal.setAttribute("data", _t("Courtesy receipt"));
printNormal.setAttribute("font", "3");
printNormal.setAttribute("operator", receipt.operator || "1");
xml.documentElement.appendChild(printNormal);

for (let line of receipt.orderlines) {
printNormal = xml.createElement("printNormal");
printNormal.setAttribute("data", line.product_name);
printNormal.setAttribute("font", "1");
printNormal.setAttribute("operator", receipt.operator || "1");
xml.documentElement.appendChild(printNormal);
}

var endNonFiscal = xml.createElement("endNonFiscal");
endNonFiscal.setAttribute("operator", receipt.operator || "1");
xml.documentElement.appendChild(endNonFiscal);

return xml;
},

printNonFiscalReceipt: function(receipt) {
var xml = this.prepareXMLNonFiscalReceipt(receipt);
var xmlString = new XMLSerializer().serializeToString(xml);
this.fiscalPrinter.send(this.url, xmlString);
console.log(xmlString);
},

printFiscalReport: function() {
var xml = '<printerFiscalReport>';
xml += '<printZReport operator="" />';
Expand Down Expand Up @@ -474,7 +542,11 @@ odoo.define("fiscal_epos_print.epson_epos_print", function (require) {
});

return {
eposDriver: eposDriver
eposDriver: eposDriver,
eposWillPrintReceipt: eposWillPrintReceipt,
eposWillPrintCourtesyReceipt: eposWillPrintCourtesyReceipt,
eposWillPrint: eposWillPrint,
eposPrint: eposPrint,
}

});
4 changes: 2 additions & 2 deletions fiscal_epos_print/static/src/js/pos_order_mgmt.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ odoo.define("fiscal_epos_print.pos_order_mgmt", function (require) {
// copiato da screens.PaymentScreenWidget
sendToFP90Printer: function(receipt, printer_options) {
var fp90 = new eposDriver(printer_options, this);
fp90.printFiscalReceipt(receipt);
return epson_epos_print.eposPrint(fp90, receipt);
},
action_print: function (order_data, order) {
if (this.pos.config.printer_ip) {
if (epson_epos_print.eposWillPrint(currentOrder)) {
if (order_data.fiscal_receipt_number) {
this.pos.gui.show_popup('error', {
'title': _t('Order already printed'),
Expand Down
6 changes: 3 additions & 3 deletions fiscal_epos_print/static/src/js/screens.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ odoo.define("fiscal_epos_print.screens", function (require) {

sendToFP90Printer: function(receipt, printer_options) {
var fp90 = new eposDriver(printer_options, this);
fp90.printFiscalReceipt(receipt);
return epson_epos_print.eposPrint(printer_options["order"], receipt);
},

render_receipt: function() {
Expand Down Expand Up @@ -59,15 +59,15 @@ odoo.define("fiscal_epos_print.screens", function (require) {
},
sendToFP90Printer: function(receipt, printer_options) {
var fp90 = new eposDriver(printer_options, this);
fp90.printFiscalReceipt(receipt);
return epson_epos_print.eposPrint(fp90, receipt);
},
finalize_validation: function() {
// we need to get currentOrder before calling the _super()
// otherwise we will likely get a empty order when we want to skip
// the receipt preview
var currentOrder = this.pos.get('selectedOrder');
this._super.apply(this, arguments);
if (this.pos.config.printer_ip && !currentOrder.is_to_invoice()) {
if (epson_epos_print.eposWillPrint(currentOrder)) {
this.chrome.loading_show();
this.chrome.loading_message(_t('Connecting to the fiscal printer'));
var printer_options = currentOrder.getPrinterOptions();
Expand Down
10 changes: 10 additions & 0 deletions fiscal_epos_print/views/point_of_sale.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@
<div class="o_setting_right_pane">
<label for="fiscal_cashdrawer"/>
</div>
<br/>
<div class="o_setting_left_pane">
<field name="epos_print_courtesy_receipt"/>
</div>
<div class="o_setting_right_pane">
<label for="epos_print_courtesy_receipt"/>
<div class="text-muted">
Print a courtesy receipt when the order has to be invoiced
</div>
</div>
</div>
<div class="col-12 col-lg-6 o_setting_box" id="fiscal_printer_serial">
<div class="o_setting_right_pane">
Expand Down

0 comments on commit fe64ee4

Please sign in to comment.