I wanted to print a ‘Thought for the Day’ at the end of the ticket.
I could create an entity, populate it, and select one ‘Thought for the Day’ before printing the ticket.
But, that would be cumbersome.
How do I automatically attach a random entity instance to a ticket?
I even thought of creating a separate Database Table in SQL, with the ‘Thought for the Day’s, but, how do I pull one row in random from that new database table into the Ticket Template?
Does anyone know an easier way I could go about this?
Are you happy creating an array in javascript? If so, you can use automation > scripts to create your array of thoughts and then return one at random from the array using a {CALL:handler.funtionName()} tag in your ticket. If you are doing a truly random generator in the script then keep in mind the output will be different on any ticket reprints.
I created a new EntityType called FamousQuotes with only a default field called Name
I populated this Entity Types with data
Then I created a Script as below:
function getRandomQuote() {
qry = “SELECT TOP (1) [Name] FROM [dbo].[Entities] WHERE ([EntityTypeId] = 49) order by newid()”;
var fQ = sql.Query(qry).All;
return fQ[0];
}
And I call this script from the Ticket Template.
It is working beautifully.
And much easier to add and delete the data, as it can be done without getting into the script, and by using the Entities Screen.
Now, I need to figure out, how to word wrap the Thought for the Day, as the text is rather long.
Yes, found a decent piece of code for word wrapping online, I added the tag to the code for it to work inside SambaPOS
function wordwrap(long_string, max_char){
var sum_length_of_words = function(word_array){
var out = 0;
if (word_array.length!=0){
for (var i=0; i<word_array.length; i++){
var word = word_array[i];
out = out + word.length;
}
};
return out;
}
var split_out = [[]];
var split_string = long_string.split(' ');
for (var i=0; i<split_string.length; i++){
var word = split_string[i];
if ((sum_length_of_words(split_out[split_out.length-1]) + word.length) > max_char){
split_out = split_out.concat([[]]);
}
split_out[split_out.length-1] = split_out[split_out.length-1].concat(word);
}
for (var i=0; i<split_out.length; i++){
split_out[i] = split_out[i].join(" ");
}
return split_out.join('\n<L00>');
};
The new <Wxy:cw1,cw2,cw3,...> Tag has the ability to do wrapping in your Template based on comma-placement. That way, you don’t need to do all that Scripting.
The numbers indicate a character-count/width for the column.
Using * in a column will expand that column automatically to use remaining characters, based on Printer character count configuration
Use whitespace after a number to enable wrapping.
<W00:4,4 , 17 , 10>content1|content2|content3|content4
- -- ---- ---
| | || |||
left| || right
| center
|
left with line break (wrap)
The whitespace after number enables left align with word wrapping. If there is no whitespace it is left align with trimming.