@@ -0,0 +1,2176 @@
+from __future__ import absolute_import%0A%0Afrom datetime import datetime%0A%0Afrom corehq.apps.fixtures.models import FixtureDataItem, FixtureDataType%0Afrom corehq.form_processor.backends.sql.dbaccessors import LedgerAccessorSQL%0Afrom corehq.form_processor.interfaces.dbaccessors import LedgerAccessors%0Afrom corehq.form_processor.models import LedgerValue%0A%0A%0Adef get_episode_adherence_ledger(domain, episode_case_id, entry_id):%0A    %22%22%22%0A    :param domain: domain name%0A    :param episode_case_id: episode case id for adherence%0A    :param entry_id: example date_2017-12-09%0A    %22%22%22%0A    ledger_accessor = LedgerAccessors(domain)%0A    return ledger_accessor.get_ledger_value(episode_case_id, %22adherence%22, entry_id)%0A%0A%0Adef update_ledger_with_episode(episode_case, adherence_case, entry_id):%0A    %22%22%22%0A    :param episode_case: episode case for the adherence%0A    :param entry_id: example %22date_2017-12-09%22 which would be the adherence date%0A    %22%22%22%0A    adherence_source = adherence_case.get_case_property('adherence_source')%0A    adherence_value = adherence_case.get_case_property('adherence_value')%0A    fixture = FixtureDataType.by_domain_tag(adherence_case.domain, %22adherence_ledger_values%22).first()%0A    fixture_items_for_source = FixtureDataItem.by_field_value(%0A        adherence_case.domain, fixture,%0A        %22adherence_source%22, adherence_source).all()%0A    fixture_items_for_value = %5B%0A        fixture_item for fixture_item in fixture_items_for_source%0A        if fixture_item.fields%5B'adherence_value'%5D.field_list%5B0%5D.field_value == adherence_value%0A    %5D%0A    fixture_item = fixture_items_for_value%5B0%5D%0A    ledger_value = fixture_item.fields%5B'ledger_value'%5D.field_list%5B0%5D.field_value%0A    ledger = get_episode_adherence_ledger(episode_case.domain, episode_case.case_id, entry_id)%0A    if ledger:%0A        ledger.balance = ledger_value%0A        ledger.save()%0A    else:%0A        ledger = LedgerValue(%0A            domain=adherence_case.domain,%0A            case_id=episode_case.case_id,%0A            section_id=%22adherence%22,%0A            entry_id=entry_id,%0A            balance=ledger_value,%0A            last_modified=datetime.utcnow()%0A        )%0A    LedgerAccessorSQL.save_ledger_values(%5Bledger%5D)%0A    return ledger%0A

add utils to update/create ledger for an adherence [skip ci]