Introduction
Before starting to build your Supply application there are a few concept you must familiarize yourself with:
In this document:
- Setting up iteration over a product list
- Saving data to a ledger: setting up transaction questions
- Loading data from a ledger
- Summing data across all products
- Showing ledgers in the case list
- Supply-point parent/child update
Setting up iteration over the product list
Why?
This will allow you to iterate over your product list to either read or write values for each of them
How?
In order to iterate over a list of products, you need to set up a repeat group over the product list
This is configured in the “products” repeat.
Click on the repeat → Advanced → Model Iteration ID Query
Click “Edit”
Set the following:
Query Expression: instance('products')/products/product/@id
Instance ID: products
Instance URI: jr://fixture/commtrack:products
Click “Save”
This is the exact same for all product repeats.
Your configuration should look like this:
Saving date to a ledger: Setting up transaction questions
Why?
- This will allow you to create or update your ledger quantities.
- Used inside a repest group, you can iterate over your product list and update for example the stock values.
How?
Question ID: whatever you want
Source/Destination case: this is the id of the case of the ledger or entity id.
If you want to apply the transaction to a ledger of the current case use: instance('commcaresession')/session/data/case_id
- For supply chain, if you want to use the supply-point case of the user: instance('commcaresession')/session/user/data/commtrack-supply-point
- Any case id can be used here.
Balance ID: This is the name of the ledger. The default should be “stock” but you can create any id you want.
Product: current()/../../../../@id
This exact text should be used for a transfer inside a group.
If the transfer is directly in the repeat remove one set of double dots and use: current()/../../../@id
Quantity: value to set the ledger quantity with. It can reference a question in the repeat where you ask about, or calculate quantity. Normal calculates work fine here.
Loading data from a ledger
Why?
- To read data previously saved into a ledger
Here is the calculate expression:
coalesce(instance('ledger')/ledgerdb/ledger[@entity-id=case_id]/section[@section-id='balance']/entry[@id=current()/../@id], 0)
instance('commcaresession')/session/data/case_id
Let’s break this down:
coalesce([big scary ledger reference], 0)
just says use the leger reference, and if you don’t find a value, use 0.
This is in case it is the first time filling in a form that touches that value.
Looking at the ledger reference
instance('ledger')/ledgerdb/ledger[@entity-id=case_id]/section[@section-id='balance']/entry[@id=current()/../@id]
case_id: put here the id of the case where you want to get your ledger from. For
...
example instance('commcaresession')/session/data/case_id = the current case ID.
So instance('ledger')/ledgerdb/ledger[@entity-id=instance('commcaresession')/session/data/case_id]
Says grab the ledger for this case
/section[@section-id='balance']
Says grab the section called “balance” (this corresponds to the “balance ID”). You could use here 'stock', 'consumption' ...
/entry[@id=current()/../@id]
Says grab the entry value with the ID of the current product
This is pretty scary expression. Feel free to ask for help with it, though it shouldn’t vary too much per form.
Summing data across all products
This is standard app-builder stuff, and can be done using the sum() function.
Outside the repeat group do a sum of a question inside the repeat group.
For example: sum(/data/products/item/data_product_payment) will sum the values of each product data_product_payment.
PS: the additional hidden word “item” in the path that is inserted due to the model iteration structure.