Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

 

Introduction

Before starting to build your Supply application there are a few concept you must familiarize yourself with:

  1. Locations (renamed Organizations)
  2. Ledgers
  3. Transactions
  4. Repeat groups

In this document:

  1. Setting up iteration over a product list
  2. Saving data to a ledger: setting up transaction questions
  3. Loading data from a ledger
  4. Summing data across all products
  5. Showing ledgers in the case list
  6. 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:

  • Click “Save”

  • This is the exact same for all product repeats.

Your configuration should look like this:



Extra: If you want to run the product only over a define list of products, you can use the Query Expression to filter the list.
For example the following expression will run the iteration over product of a defined program: 
instance('products')/products/product[./program_id=xxx]/@id

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.



  • No labels