Clean code for Takepos

This commit is contained in:
Laurent Destailleur 2026-02-04 14:21:47 +01:00
parent 4b3a8ff24f
commit 8b920938de
7 changed files with 117 additions and 46 deletions

View file

@ -38,7 +38,7 @@ require '../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/receiptprinter.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/dolreceiptprinter.class.php';
require_once DOL_DOCUMENT_ROOT.'/takepos/class/dolreceiptprinter.class.php';
// Load translation files required by the page
$langs->loadLangs(array("admin", "receiptprinter"));

View file

@ -450,7 +450,7 @@ if (isALNERunningVersion()) { // No need to show the custom template when isALN
if (isModEnabled('receiptprinter')) {
// Select printer to use with terminal
require_once DOL_DOCUMENT_ROOT.'/core/class/dolreceiptprinter.class.php';
require_once DOL_DOCUMENT_ROOT.'/dolreceiptprinter/class/dolreceiptprinter.class.php';
$printer = new dolReceiptPrinter($db);
$printer->listprinters();
@ -487,7 +487,7 @@ if (isModEnabled('receiptprinter')) {
if (isModEnabled('receiptprinter') || getDolGlobalString('TAKEPOS_PRINT_METHOD') == "receiptprinter" || getDolGlobalString('TAKEPOS_PRINT_METHOD') == "takeposconnector") {
// Select printer to use with terminal
require_once DOL_DOCUMENT_ROOT.'/core/class/dolreceiptprinter.class.php';
require_once DOL_DOCUMENT_ROOT.'/dolreceiptprinter/class/dolreceiptprinter.class.php';
$printer = new dolReceiptPrinter($db);
$printer->listPrintersTemplates();
$templates = array();

View file

@ -416,7 +416,7 @@ if ($action == 'getProducts' && $user->hasRight('takepos', 'run')) {
} elseif ($action == "opendrawer" && $term != '' && $user->hasRight('takepos', 'run')) {
top_httphead('application/html');
require_once DOL_DOCUMENT_ROOT.'/core/class/dolreceiptprinter.class.php';
require_once DOL_DOCUMENT_ROOT.'/dolreceiptprinter/class/dolreceiptprinter.class.php';
$printer = new dolReceiptPrinter($db);
// check printer for terminal
@ -436,15 +436,35 @@ if ($action == 'getProducts' && $user->hasRight('takepos', 'run')) {
} elseif ($action == "printinvoiceticket" && $term != '' && $id > 0 && $user->hasRight('takepos', 'run') && $user->hasRight('facture', 'lire')) {
top_httphead('application/html');
require_once DOL_DOCUMENT_ROOT.'/core/class/dolreceiptprinter.class.php';
require_once DOL_DOCUMENT_ROOT.'/dolreceiptprinter/class/dolreceiptprinter.class.php';
require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
$printer = new dolReceiptPrinter($db);
// check printer for terminal
if ((getDolGlobalInt('TAKEPOS_PRINTER_TO_USE'.$term) > 0 || getDolGlobalString('TAKEPOS_PRINT_METHOD') == "takeposconnector") && getDolGlobalInt('TAKEPOS_TEMPLATE_TO_USE_FOR_INVOICES'.$term) > 0) {
if ((getDolGlobalInt('TAKEPOS_PRINTER_TO_USE'.$term) > 0 || getDolGlobalString('TAKEPOS_PRINT_METHOD') == "takeposconnector")) {
$object = new Facture($db);
$object->fetch($id);
$printer->sendToPrinter($object, getDolGlobalInt('TAKEPOS_TEMPLATE_TO_USE_FOR_INVOICES'.$term), getDolGlobalInt('TAKEPOS_PRINTER_TO_USE'.$term));
$resultinccounter = 0;
$templateidtouse = 0;
$url = DOL_URL_ROOT."/blockedlog/ajax/block-add.php?id=".((int) $object->id).'&element='.urlencode($object->element)."&action=DOC_PREVIEW&token=".newToken();
$result = getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 2);
if ((string) $result['http_code'] == '200') {
$resultinccounter++;
$object->pos_print_counter++; // increase counter to match the change in database
$templateidtouse = getDolGlobalInt('TAKEPOS_TEMPLATE_TO_USE_FOR_INVOICES'.$term); // Note that this may not be ignored by the sendToPrinter()
}
if ($resultinccounter) {
// Send to printer
$printer->sendToPrinter($object, $templateidtouse, getDolGlobalInt('TAKEPOS_PRINTER_TO_USE'.$term));
} else {
print 'Failed to update print counter for object ID='.$object->id;
}
}
} elseif ($action == 'getInvoice' && $user->hasRight('takepos', 'run')) {
top_httphead('application/json');
@ -461,7 +481,7 @@ if ($action == 'getProducts' && $user->hasRight('takepos', 'run')) {
top_httphead('application/html');
require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/dolreceiptprinter.class.php';
require_once DOL_DOCUMENT_ROOT.'/dolreceiptprinter/class/dolreceiptprinter.class.php';
$object = new Facture($db);
$printer = new dolReceiptPrinter($db);

View file

@ -20,7 +20,7 @@
*/
/**
* \file htdocs/core/class/dolreceiptprinter.class.php
* \file htdocs/takepos/class/dolreceiptprinter.class.php
* \brief Print receipt ticket on various ESC/POS printer
*/
@ -630,7 +630,7 @@ class dolReceiptPrinter extends Printer
}
/**
* Function to Print Receipt Ticket
* Function to Print Receipt Ticket using ESCPOS lib
*
* @param Facture|Commande $object Order or invoice object
* @param int $templateid Template id
@ -645,7 +645,57 @@ class dolReceiptPrinter extends Printer
$langs->load('bills');
$error = 0;
$ret = $this->loadTemplate($templateid);
include_once DOL_DOCUMENT_ROOT.'/blockedlog/lib/blockedlog.lib.php';
$islneverion = isALNERunningVersion();
if ($templateid) {
// Load the custom template from table printer_receipt_template
$ret = $this->loadTemplate($templateid);
// For the moment, we do not allow custom ticket when used in LNE certified context because
if ($islneverion) {
// Test that all required tags are present, if not we fallback on the generic template by setting templateid=0
// TODO
$templateid = 0;
}
}
if (empty($templateid) || $islneverion) {
// The default hard coded template
$this->template = "{dol_align_center}\r\n";
$this->template .= "{dol_print_text}{dol_value_mysoc_name}\r\n";
$this->template .= "{dol_print_text}{dol_value_mysoc_address}\r\n";
$this->template .= "{dol_print_text}{dol_value_mysoc_zip}{dol_value_mysoc_town}\r\n";
$this->template .= "{dol_print_text}{dol_value_mysoc_idprof1}\r\n";
$this->template .= "{dol_print_text}{dol_value_mysoc_vatnumber}\r\n";
$this->template .= "{dol_line_feed}\r\n";
$this->template .= "{dol_print_text}".$langs->transnoentitiesnoconv("Invoice")." {dol_value_object_ref}\r\n";
if ($object->status != 2) {
$this->template .= "{dol_print_text}*** ".$langs->transnoentities("TemporaryReceipt")." ***\r\n";
}
if ($object->pos_print_counter > 1) {
$this->template .= "{dol_print_text}*** ".$langs->transnoentities("DUPLICATA").' (no '.($object->pos_print_counter - 1).") ***\r\n";
}
$this->template .= "{dol_value_date}\r\n";
$this->template .= "{dol_value_currentdate}\r\n";
$this->template .= "{dol_value_signaturehashstart}\r\n";
$this->template .= "{dol_line_feed}\r\n";
$this->template .= "{dol_align_left}\r\n";
$this->template .= "{dol_print_object_lines}\r\n";
$this->template .= "{dol_line_feed}\r\n";
$this->template .= "{dol_print_object_tax}\r\n";
$this->template .= "{dol_line_feed}\r\n";
$this->template .= "{dol_print_object_total}\r\n";
$this->template .= "{dol_line_feed}\r\n";
if ($islneverion) {
$this->template .= "{dol_value_lnemention}\r\n";
$this->template .= "{dol_line_feed}\r\n";
}
$this->template .= "{dol_cut_paper_full}";
}
$now = dol_now('tzuser');
// Placeholders to replace with the actual value before further parsing (dol_value_xxx)

View file

@ -1466,25 +1466,29 @@ if (getDolGlobalString('TAKEPOS_BAR_RESTAURANT')) {
$customprinterallowed = true;
$customprinttemplateallowed = true;
include_once DOL_DOCUMENT_ROOT.'/blockedlog/lib/blockedlog.lib.php';
if (isALNERunningVersion()) { // No need to show this option because it has no effect when isALNERunningVersion is true.
$customprinttemplateallowed = false; // Custom printer may be allowed if mandatory information in template are guaranteed. For the moment, we prefer not allow this.
if (isALNERunningVersion()) {
// Custom printer may be allowed if mandatory information in template are guaranteed. For the moment, we prefer not allow this.
$customprinttemplateallowed = false;
}
}
// Button to print receipt
if (getDolGlobalString('TAKEPOS_PRINT_METHOD') == "takeposconnector") { // deprecated method
if (getDolGlobalString('TAKEPOS_PRINT_SERVER') && filter_var(getDolGlobalString('TAKEPOS_PRINT_SERVER, FILTER_VALIDATE_URL')) == true) {
$menus[$r++] = array('title' => '<span class="fa fa-receipt paddingrightonly"></span><div class="trunc">'.$langs->trans("PrintTicket").'</div>', 'action' => 'TakeposConnector(placeid);');
// If TAKEPOS_PRINT_SERVER is an URL
$menus[$r++] = array('title' => '<span class="fa fa-receipt paddingrightonly"></span><div class="trunc">'.$langs->trans("PrintTicket").'</div>', 'action' => 'PrintByESCPOSOld(placeid);');
} else {
$menus[$r++] = array('title' => '<span class="fa fa-receipt paddingrightonly"></span><div class="trunc">'.$langs->trans("PrintTicket").'</div>', 'action' => 'TakeposPrinting(placeid);');
// If TAKEPOS_PRINT_SERVER is an IP
// Print by calling the receipt.php to get HTML content and send the HTML content to TAKEPOS_PRINT_SERVER:8111/print
$menus[$r++] = array('title' => '<span class="fa fa-receipt paddingrightonly"></span><div class="trunc">'.$langs->trans("PrintTicket").'</div>', 'action' => 'PrintHTMLToSlashPrint(placeid);');
}
} elseif ($customprinterallowed && (isModEnabled('receiptprinter') && getDolGlobalInt('TAKEPOS_PRINTER_TO_USE'.$term) > 0) || getDolGlobalString('TAKEPOS_PRINT_METHOD') == "receiptprinter") { // @phpstan-ignore-line
// Button Print Receipt on special printer
} elseif ($customprinterallowed && $customprinttemplateallowed && (isModEnabled('receiptprinter') && getDolGlobalInt('TAKEPOS_PRINTER_TO_USE'.$term) > 0) || getDolGlobalString('TAKEPOS_PRINT_METHOD') == "receiptprinter") { // @phpstan-ignore-line
// Button Print Receipt on special custom printer using custom template
$nameOfPrinter = dol_getIdFromCode($db, getDolGlobalInt('TAKEPOS_PRINTER_TO_USE'.$term), 'printer_receipt', 'rowid', 'name', 1);
$menus[$r++] = array('title' => '<div title="'.dolPrintHTMLForAttribute($langs->trans("PrintOn", $nameOfPrinter)).'"><span class="fa fa-receipt paddingrightonly"></span><div class="trunc">'.$langs->trans("PrintTicket").'</div></div>', 'action' => 'DolibarrTakeposPrinting(placeid);');
$menus[$r++] = array('title' => '<div title="'.dolPrintHTMLForAttribute($langs->trans("PrintOn", $nameOfPrinter)).'"><span class="fa fa-receipt paddingrightonly"></span><div class="trunc">'.$langs->trans("PrintTicket").'</div></div>', 'action' => 'PrintByESCPOS(placeid);');
} else {
// Button Print Receipt on browser
$menus[$r++] = array('title' => '<span class="fa fa-receipt paddingrightonly"></span><div class="trunc">'.$langs->trans("PrintTicket").'</div>', 'action' => 'Print(placeid);');
$menus[$r++] = array('title' => '<span class="fa fa-receipt paddingrightonly"></span><div class="trunc">'.$langs->trans("PrintTicket").'</div>', 'action' => 'PrintByBrowser(placeid);');
}
if (getDolGlobalString('TAKEPOS_PRINT_METHOD') == "takeposconnector" && getDolGlobalString('TAKEPOS_ORDER_NOTES') == 1) {

View file

@ -1195,7 +1195,7 @@ if (empty($reshook)) {
if ($action == "order" && $placeid != 0 && ($user->hasRight('takepos', 'run') || defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE'))) {
include_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
if ((isModEnabled('receiptprinter') && getDolGlobalInt('TAKEPOS_PRINTER_TO_USE'.$term) > 0) || getDolGlobalString('TAKEPOS_PRINT_METHOD') == "receiptprinter" || getDolGlobalString('TAKEPOS_PRINT_METHOD') == "takeposconnector") {
require_once DOL_DOCUMENT_ROOT.'/core/class/dolreceiptprinter.class.php';
require_once DOL_DOCUMENT_ROOT.'/dolreceiptprinter/class/dolreceiptprinter.class.php';
$printer = new dolReceiptPrinter($db);
}
@ -1343,21 +1343,21 @@ if (empty($reshook)) {
} elseif (getDolGlobalString('TAKEPOS_PRINT_METHOD') == "takeposconnector") {
// Used when the external addon takeposconnector is installed. Deprecated.
if (getDolGlobalString('TAKEPOS_PRINT_SERVER') && filter_var(getDolGlobalString('TAKEPOS_PRINT_SERVER'), FILTER_VALIDATE_URL) == true) {
$sectionwithinvoicelink .= ' <button id="buttonprint" type="button" onclick="TakeposConnector('.$placeid.')">'.$langs->trans('PrintTicket').'</button>';
$sectionwithinvoicelink .= ' <button id="buttonprint" type="button" onclick="PrintByESCPOSOld('.$placeid.')">'.$langs->trans('PrintTicket').'</button>';
} else {
$sectionwithinvoicelink .= ' <button id="buttonprint" type="button" onclick="TakeposPrinting('.$placeid.')">'.$langs->trans('PrintTicket').'</button>';
$sectionwithinvoicelink .= ' <button id="buttonprint" type="button" onclick="PrintHTMLToSlashPrint('.$placeid.')">'.$langs->trans('PrintTicket').'</button>';
}
} elseif ($customprinterallowed && (isModEnabled('receiptprinter') && getDolGlobalInt('TAKEPOS_PRINTER_TO_USE'.$term) > 0) || getDolGlobalString('TAKEPOS_PRINT_METHOD') == "receiptprinter") { // @phpstan-ignore-line
// If we set to use a specific receipt printer
$nameOfPrinter = dol_getIdFromCode($db, getDolGlobalInt('TAKEPOS_PRINTER_TO_USE'.$term), 'printer_receipt', 'rowid', 'name', 1);
$sectionwithinvoicelink .= ' <button id="buttonprint" type="button" onclick="DolibarrTakeposPrinting('.$placeid.')" title="'.dolPrintHTMLForAttribute($langs->trans("SentToPrinter").' '.$nameOfPrinter).'">'.$langs->trans('PrintTicket').'</button>';
$sectionwithinvoicelink .= ' <button id="buttonprint" type="button" onclick="PrintByESCPOS('.$placeid.')" title="'.dolPrintHTMLForAttribute($langs->trans("SentToPrinter").' '.$nameOfPrinter).'">'.$langs->trans('PrintTicket').'</button>';
} else {
$sectionwithinvoicelink .= ' <button id="buttonprint" type="button" onclick="Print('.$placeid.')">'.$langs->trans('PrintTicket').'</button>';
$sectionwithinvoicelink .= ' <button id="buttonprint" type="button" onclick="PrintByBrowser('.$placeid.')">'.$langs->trans('PrintTicket').'</button>';
if ($customprinttemplateallowed && getDolGlobalString('TAKEPOS_PRINT_WITHOUT_DETAILS')) {
$sectionwithinvoicelink .= ' <button id="buttonprint" type="button" onclick="PrintBox('.$placeid.', \'without_details\')">'.$langs->trans('PrintWithoutDetails').'</button>';
}
if ($customprinttemplateallowed && getDolGlobalString('TAKEPOS_GIFT_RECEIPT')) {
$sectionwithinvoicelink .= ' <button id="buttonprint" type="button" onclick="Print('.$placeid.', 1)">'.$langs->trans('GiftReceipt').'</button>';
$sectionwithinvoicelink .= ' <button id="buttonprint" type="button" onclick="PrintByBrowser('.$placeid.', 1)">'.$langs->trans('GiftReceipt').'</button>';
}
}
if (getDolGlobalString('TAKEPOS_EMAIL_TEMPLATE_INVOICE') && getDolGlobalInt('TAKEPOS_EMAIL_TEMPLATE_INVOICE') > 0) {
@ -1540,19 +1540,16 @@ function PrintBox(id, action) {
}
/* Open the popup of the receipt to allow printing */
function Print(id, gift) {
console.log("Call Print() to generate the receipt.");
function PrintByBrowser(id, gift) {
console.log("Call PrintByBrowser() to generate the receipt.");
$.colorbox({href:"receipt.php?facid="+id+"&gift="+gift, width:"40%", height:"90%", transition:"none", iframe:"true", title:'<?php echo dol_escape_js($langs->trans("PrintTicket")); ?>'});
return true;
}
/* Print of configured printer */
function TakeposPrinting(id){
/* Print of configured printer when TAKEPOS_PRINT_SERVER is IP */
function PrintHTMLToSlashPrint(id){
var receipt;
console.log("TakeposPrinting" + id);
console.log("PrintHTMLToSlashPrint" + id);
$.get("receipt.php?facid="+id, function(data, status) {
receipt=data.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '');
$.ajax({
@ -1564,9 +1561,10 @@ function TakeposPrinting(id){
return true;
}
function TakeposConnector(id){
console.log("TakeposConnector" + id);
$.get("<?php echo DOL_URL_ROOT; ?>/takepos/ajax/ajax.php?action=printinvoiceticket&token=<?php echo newToken(); ?>&term=<?php echo urlencode(isset($_SESSION["takeposterminal"]) ? $_SESSION["takeposterminal"] : ''); ?>&id="+id+"&token=<?php echo currentToken(); ?>", function(data, status) {
/* Print of configured printer when TAKEPOS_PRINT_SERVER is URL, using the ESCPOS driver */
function PrintByESCPOSOld(id){
console.log("PrintByESCPOSOld id=" + id);
$.get("<?php echo DOL_URL_ROOT; ?>/takepos/ajax/ajax.php?action=printinvoiceticket&token=<?php echo currentToken(); ?>&term=<?php echo urlencode(isset($_SESSION["takeposterminal"]) ? $_SESSION["takeposterminal"] : ''); ?>&id="+id, function(data, status) {
$.ajax({
type: "POST",
url: '<?php print getDolGlobalString('TAKEPOS_PRINT_SERVER'); ?>/printer/index.php',
@ -1579,15 +1577,15 @@ function TakeposConnector(id){
<?php
$nameOfPrinter = dol_getIdFromCode($db, getDolGlobalInt('TAKEPOS_PRINTER_TO_USE'.$term), 'printer_receipt', 'rowid', 'name', 1);
?>
// Call the ajax to execute the print.
// Call the ajax to execute the printinvoiceticket action, using the ESCPOS driver
// With some external module another method may be called.
function DolibarrTakeposPrinting(id) {
console.log("DolibarrTakeposPrinting Printing invoice ticket by calling takepos/aja/ajax.php" + id);
function PrintByESCPOS(id) {
console.log("PrintByESCPOS Printing invoice ticket by calling takepos/aja/ajax.php id=" + id);
$.ajax({
type: "GET",
data: { token: '<?php echo currentToken(); ?>' },
url: "<?php print DOL_URL_ROOT.'/takepos/ajax/ajax.php?action=printinvoiceticket&token='.newToken().'&term='.urlencode(isset($_SESSION["takeposterminal"]) ? $_SESSION["takeposterminal"] : '').'&id='; ?>" + id,
url: "<?php print DOL_URL_ROOT.'/takepos/ajax/ajax.php?action=printinvoiceticket&token='.currentToken().'&term='.urlencode(isset($_SESSION["takeposterminal"]) ? $_SESSION["takeposterminal"] : '').'&id='; ?>" + id,
success: function(){
showPrintResultPopup('<?php echo dol_escape_js($langs->trans("SentToPrinter").' '.$nameOfPrinter); ?>', 2000);
},

View file

@ -119,7 +119,6 @@ print '<body>';
print "
<script>
console.log('Call /blockedlog/ajax/block-add on output of receipt.php.');
$.post('".DOL_URL_ROOT."/blockedlog/ajax/block-add.php'
, {
@ -288,13 +287,13 @@ $object->pos_print_counter += 1;
// Show if it is a duplicata
$isADuplicata = ($object->pos_print_counter >= 2);
if ($object->status == $object::STATUS_CLOSED) {
if ($isADuplicata) {
print '<br><b>*** DUPLICATA (no '.($object->pos_print_counter - 1).') ***</b>'; // Hard coded string
}
} else {
if ($object->status != $object::STATUS_CLOSED) {
// Not yet paid completely
print '<br><b>*** '.strtoupper($langs->trans("TemporaryReceipt")).' ***</b>'; // Hard coded string
} else {
if ($isADuplicata) {
print '<br><b>*** '.$langs->transnoentities("DUPLICATA").' (no '.($object->pos_print_counter - 1).') ***</b>'; // Hard coded string
}
}
?>
</p>