This sample app is designed to show how to use multiple TinyDB Namespaces in a simple application. It also shows how to add up a list of costs to get a total cost.
The app was developed for https://groups.google.com/d/msg/mitappinventortest/NF6j--NJc-E/hPfG8JDwFQAJ
A List Picker menu lets you set up your price list or jump into setting up a sale.
The Fruit Prices Arrangement lets you set up fruits (i.e. Cackle Fruit, sold by the dozen) and their prices ($3.49 per dozen).
The Purchases Arrangement lets you build a sales list for the current customer, telling which items they are buying and how many (or how much) of each item they want.
For convenience, there is a +1 button to increase the quantity requested on the current item. Here the customer decided he needs an extra dozen Cackle Fruit, so his total cost went up from $8.01 to $11.51. (Extra items in his purchase are not shown in this screen shot. They are in the scrollable label at the bottom.)
At the top of Screen1 is the list picker lpkMenu, which lets you pick which of the two vertical arrangements you want to work in, varPrices or varSales. Only one of them is visible at a time.
Price and Sales Storage
Two TinyDBs are used for this app,
- TinyDB_Prices keeps the price per item type, indexed by item type. It stays relatively stable (assuming low inflation.)
- TinyDB_Purchases keeps the quantity of each item type bought for the current purchase in progress. It is cleared after each purchase for the next customer.
Notice the different Namespace attributes for the two TinyDBs. That’s critical to keep the tag values apart, since all the tags (item types) in TinyDB_Purchases are copied from the tags in TinyDB_Prices.
(see Sample Run for screen shots)
when lpkFruitPrices BeforePicking
The list of fruit types in the price list comes from the tags in TinyDB_Prices, and is used to load the Elements list of lpkFruitPrices, which is used to select the fruit type for which we want to set a unit price. An extra element “(+)” is added to the front of the list to offer the option of adding a new item to the price list.
when lpkFruitPrices AfterPicking
The list picker lpkFruitPrices is used to prime two text boxes with their initial values:
- txbFruit.Text contains the selected fruit name (or blank if new)
- txbUnitPrice.Text contains the lookup value for the selected fruit, or whatever entered value if new.
when btnSave Click
The Save button adds or replaces a unit price into TinyDB_Prices. To signal completion, it clears the txbUnitPrice text box and hides the keyboard.
It does a little bit of data cleaning to make sure the price is a number, or 0.
Starting a Sale
The sales process uses two List Pickers,
- lpkFruit for selecting a fruit type from the price list and adding it to the current sale
- lpkSale for displaying the current running subtotals of this sale and giving the opportunity to go back and change item quantities before sale completion.
when lpkFruit BeforePicking
The options to buy fruit come from the price list in TinyDB_Prices’ tag list.
when lpkFruit AfterPicking
When a fruit type is selected, the quantity for that fruit type in the current sale is retrieved by procedure quantity based on lpkFruit.Text, the current Selection.
when btnAdd Click
Button btnAdd is used to store the new quantity for the Selected fruit type to TinyDB_Purchases. Afterwards, we update the display and ListPicker lpkSales via routine show_sales.
The list picker lpkSale is serving double duty here. Its .Text value is built up to show all the purchases in TinyDB_Purchases, with price and cost, separated by ------- and new lines (\n).
Procedure describe is used to build each Element in lpkSale and procedure cost is used to calculate the cost of each purchase in TinyDB_Purchases. (See Purchasing screenshots)
when lpkSale AfterPicking
Picking an item off the simulated cash register tape in lpkSale sets it up in lpkFruit.Text for the item type and txbQuantity for the current quantity of that purchase, should the customer decide to change it via btnAdd.