-
Notifications
You must be signed in to change notification settings - Fork 12
6. Misc Products
This document continues from 5. Understanding Rules tutorial. Please complete it before starting this tutorial.
In this tutorial I'll show some ways to add Orders to Ticket Cards.
Operators uses Misc. Products to create orders that have custom product name and price. To implement this feature we'll add a Misc
button inside Ticket Cards and ask product name and price when this button is clicked.
To add a Misc
button inside Ticket Cards we need to edit Tickets
Card Type. From Main Menu click Management > Card Types
menu item and click on Tickets
Card Type to edit.
To add Misc button insert Misc=Add Misc Product
line under Payment button definition. This will add a button labelled as Misc
and execute Add Misc Product
command when clicked.
Click Check Mark button to update Card Type.
To capture Add Misc Product
command we need to create a rule. From Main Menu click on Management > Rules
and edit Default Rules
.
We'll append Ask Misc Product
and Add Misc Product
functions at the end of the rule code.
rule AskMiscProduct {
when {
r: Result;
s: State;
a: Action a.type == 'EXECUTE_COMMAND' from s.action;
a: Action a.data.name == 'Add Misc Product' from s.action;
}
then {
r.add('ASK_QUESTION',{
question :'Enter Product Properties',
tag :'Add Misc Product',
parameters : {
'Name': '',
'Price': 0,
'Source': [
'Food',
'Bar'
]
}
});
}
}
rule AddMiscProduct {
when {
r: Result;
s: State;
a: Action a.type == 'ASK_QUESTION' from s.action;
a: Action a.data.tag == 'Add Misc Product' from s.action;
s: State s.state.get('Name');
}
then {
r.add('CREATE_CARD',{type:'Order'});
r.add('SET_CARD_TAG',{
'value': s.state.get('Name'),
'amount': s.state.get('Price'),
'source': s.state.get('Source')
});
}
}
AskMiscProduct
function will display a dialog to recevie name and price from operator. AddMiscProduct
function will add product to Ticket Card.
Save rule by clicking Check Mark button and test if Misc
button works correctly or not.
It is possible to pass command parameters to command buttons. On this section we'll create static product buttons for Hamburger and Coke.
From Main Menu click on Management > Card Types
menu item and Edit Tickets
Card Type.
To add Hamburger
button append this line under Misc
button definition.
Hamburger=Add Static Product:Name=Hamburger,Price=11
This command button will pass additional parameters to action so we can use single rule to handle multiple buttons. Parameter definition separates with :
char from button definition. It sets Name
parameter to Hamburger and Price
parameter to 11.
Now we'll create a rule to handle this button.
From Main Menu click on Management > Rules
and edit Default Rules
.
This button will not show a dialog so only single function will be enough for this. Append this at the end of the rule code.
rule AddStaticProduct {
when {
r: Result;
s: State;
a: Action a.type == 'EXECUTE_COMMAND' from s.action;
a: Action a.data.name == 'Add Static Product' from s.action;
}
then {
r.add('CREATE_CARD',{type:'Order'});
r.add('SET_CARD_TAG',{
'value': a.data.params.Name,
'amount': a.data.params.Price,
'source': 'Kitchen'
});
}
}
We can access parameter values from a.data.params.Name
and a.data.params.Price
. The name of the parameters are case sensitive. As we defined Name
and Price
starting with upper case letters we need to use same casing while reading parameter values.
Now you can test if Hamburger
button works fine or not.
You can add more buttons if you want.
Instead of defining a separate product button for each product we have we can create a new Card Type for products and create buttons automatically for defined products.
First of all we need to create a new Card Type for Products. From Main Menu click on Managment > Card Types
menu item and create a new Card Type by clicking on +
button.
I set Products
as the Card Type Name and Product
as the Reference Name. Click on Check Mark button to save new card tpye.
On this step we'll add few product cards. From Main Menu click on Cards
and change card type to Products
by clicking the drop down arrow.
You should see Products
on title. Click on +
button to add new Product Cards.
You'll see the empty product card. We'll use Name
, Price
and Source
tags to define our product. To set the Name tag click on Set Card Tag
menu item from the card menu button.
Enter Name
as the Tag Name and Pizza
as the Tag Value.
From Card Menu click on Set Card Tag
menu item once more and set Price by entering Price
as the Tag Name and 15
as the Tag Value.
Finally set Source
Tag as Kitchen
by clicking Set Card Tag
menu item.
Click on Check Mark button to save your card and add few more products by repeating these steps.
Now we'll create another rule to handle the case. This rule will load cards to read order properties instead of reading it from command parameters.
From Main Menu click on Management > Rules
and edit Default Rules
by clicking on the rule.
Append the function to the end of the rule code.
rule AddProduct {
when {
r: Result;
s: State;
a: Action a.type == 'EXECUTE_COMMAND' && a.data.name == 'Add Product' from s.action;
p: Object from s.load('Products',a.params.Name);
}
then {
r.add('CREATE_CARD',{type:'Order'});
r.add('SET_CARD_TAG',{
tag: 'add-product',
value: p.name,
amount: p.getTagValue('Price',0),
quantity: 1,
source: p.getTagValue('Source','Kitchen')
});
}
}
This rule introduces p
variable to load the Product Card by name. We'll access card tag values by using p.getTagValue()
function. The first parameter of the function is Tag Name
and the second parameter is Default Value
. If you check source parameter on the rule you'll see Source will be default Kitchen
if you forget to set Source
tag on Product card.
Finally we need to edit Tickets
card type to add the command button definition.
From Main Menu click on Management > Card Types
and edit Tickets
Card Type by clicking on it.
I removed the static button definitions that I've added on previous tutorial and added Add Product:Products
instead. This syntax is slightly different from other definitions. When we use a Card Type Name
in definition and if it is separated from the Command Name
with :
character that means it will loop all cards and generate a button labelled by card name. We can use this name to load cards and access tag values as shown on the rule example.
Now we can use Product Cards
to add product buttons and change prices.