示例#1
0
def plan_rebuild_stock_state(case_id, section_id, product_id):
    """
    planner for rebuild_stock_state

    yields actions for rebuild_stock_state to take,
    facilitating doing a dry run

    Warning: since some important things are still done through signals
    rather than here explicitly, there may be some effects that aren't
    represented in the plan. For example, inferred transaction creation
    will not be represented, nor will updates to the StockState object.

    """

    # these come out latest first, so reverse them below
    stock_transactions = (
        StockTransaction.get_ordered_transactions_for_stock(
            case_id=case_id, section_id=section_id, product_id=product_id).
        reverse()  # we want earliest transactions first
        .select_related('report'))
    balance = None
    for stock_transaction in stock_transactions:
        if stock_transaction.subtype == stockconst.TRANSACTION_SUBTYPE_INFERRED:
            yield _DeleteStockTransaction(stock_transaction)
        else:
            before = LedgerValues(balance=stock_transaction.stock_on_hand,
                                  delta=stock_transaction.quantity)
            after = _compute_ledger_values(balance, stock_transaction)
            # update balance for the next iteration
            balance = after.balance
            yield _SaveStockTransaction(stock_transaction, before, after)
示例#2
0
def get_couch_transactions(ref):
    return [
        ledger_transaction_json(tx) for tx in reversed(
            StockTransaction.get_ordered_transactions_for_stock(
                case_id=ref.case_id,
                section_id=ref.section_id,
                product_id=ref.entry_id,
            ).select_related("report"))
    ]
示例#3
0
def recalculate_domain_consumption(domain):
    """
    Given a domain, recalculate all saved consumption settings in that domain.
    """
    # note: might get slow as this gets huge
    found_doc_ids = DocDomainMapping.objects.filter(
        domain_name=domain,
        doc_type='CommCareCase',
    ).values_list('doc_id', flat=True)
    products = Product.by_domain(domain)
    for supply_point_id in found_doc_ids:
        for product in products:
            filtered_transactions = StockTransaction.get_ordered_transactions_for_stock(
                supply_point_id, const.SECTION_TYPE_STOCK, product._id
            )
            if filtered_transactions:
                update_stock_state_for_transaction(filtered_transactions[0])
示例#4
0
def recalculate_domain_consumption(domain):
    """
    Given a domain, recalculate all saved consumption settings in that domain.
    """
    # note: might get slow as this gets huge
    found_doc_ids = DocDomainMapping.objects.filter(
        domain_name=domain,
        doc_type='CommCareCase',
    ).values_list('doc_id', flat=True)
    products = Product.by_domain(domain)
    for supply_point_id in found_doc_ids:
        for product in products:
            try:
                latest_transaction = StockTransaction.get_ordered_transactions_for_stock(
                    supply_point_id, const.SECTION_TYPE_STOCK, product._id)[0]
            except IndexError:
                pass
            else:
                state = get_stock_state_for_transaction(latest_transaction)
                daily_consumption = get_consumption_for_ledger(state)
                state.daily_consumption = daily_consumption
                with drop_connected_signals(post_save):
                    state.save()
示例#5
0
def get_stock_state_json(sql_ledger):
    """Build stock state JSON from latest transaction

    Returns empty dict if stock transactions do not exist.
    """
    # similar to StockTransaction.latest(), but more efficient
    transactions = list(StockTransaction.get_ordered_transactions_for_stock(
        sql_ledger.case_id,
        sql_ledger.section_id,
        sql_ledger.product_id,
    ).select_related("report")[:1])
    if not transactions:
        return {}
    transaction = transactions[0]
    return StockState(
        case_id=sql_ledger.case_id,
        section_id=sql_ledger.section_id,
        product_id=sql_ledger.product_id,
        sql_location=SQLLocation.objects.get_or_None(supply_point_id=sql_ledger.case_id),
        last_modified_date=transaction.report.server_date,
        last_modified_form_id=transaction.report.form_id,
        stock_on_hand=transaction.stock_on_hand,
    ).to_json()
示例#6
0
def recalculate_domain_consumption(domain):
    """
    Given a domain, recalculate all saved consumption settings in that domain.
    """
    # note: might get slow as this gets huge
    found_doc_ids = DocDomainMapping.objects.filter(
        domain_name=domain,
        doc_type='CommCareCase',
    ).values_list('doc_id', flat=True)
    products = Product.by_domain(domain)
    for supply_point_id in found_doc_ids:
        for product in products:
            try:
                latest_transaction = StockTransaction.get_ordered_transactions_for_stock(
                    supply_point_id, const.SECTION_TYPE_STOCK, product._id
                )[0]
            except IndexError:
                pass
            else:
                state = get_stock_state_for_transaction(latest_transaction)
                daily_consumption = get_consumption_for_ledger(state)
                state.daily_consumption = daily_consumption
                with drop_connected_signals(post_save):
                    state.save()
示例#7
0
 def _get_stats(self):
     stock_state = StockState.objects.get(**self._stock_state_key)
     latest_txn = StockTransaction.latest(**self._stock_state_key)
     all_txns = StockTransaction.get_ordered_transactions_for_stock(
         **self._stock_state_key)
     return stock_state, latest_txn, all_txns
示例#8
0
 def _get_stats(self):
     stock_state = StockState.objects.get(**self._stock_state_key)
     latest_txn = StockTransaction.latest(**self._stock_state_key)
     all_txns = StockTransaction.get_ordered_transactions_for_stock(
         **self._stock_state_key)
     return stock_state, latest_txn, all_txns
示例#9
0
def get_couch_transactions(case_id, section_id, product_id):
    return list(reversed(
        StockTransaction.get_ordered_transactions_for_stock(
            case_id=case_id, section_id=section_id, product_id=product_id)
        .select_related("report")
    ))