예제 #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 = list(
        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
    if stock_transactions:
        domain = stock_transactions[0].report.domain
        ledger_load_counter("rebuild_stock", domain)(len(stock_transactions))
    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 populate_models(self):
        self.populated = True

        interface = FormProcessorInterface(domain=self.domain)
        processor = interface.ledger_processor
        ledger_db = interface.ledger_db

        track_load = ledger_load_counter("process_stock", self.domain)
        normal_helpers = []
        deprecated_helpers = []
        for helper in self.stock_report_helpers:
            assert helper.domain == self.domain
            if not helper.deprecated:
                normal_helpers.append(helper)
            else:
                deprecated_helpers.append(helper)
            track_load(len(helper.transactions))

        models_result = processor.get_models_to_update(
            self.xform.form_id, normal_helpers, deprecated_helpers, ledger_db
        )
        self.models_to_save, self.models_to_delete = models_result

        self.cases_with_deprecated_transactions = {
            trans.case_id for srh in deprecated_helpers for trans in srh.transactions
        }
예제 #3
0
    def populate_models(self):
        self.populated = True

        interface = FormProcessorInterface(domain=self.domain)
        processor = interface.ledger_processor
        ledger_db = interface.ledger_db

        track_load = ledger_load_counter("process_stock", self.domain)
        normal_helpers = []
        deprecated_helpers = []
        for helper in self.stock_report_helpers:
            assert helper.domain == self.domain
            if not helper.deprecated:
                normal_helpers.append(helper)
            else:
                deprecated_helpers.append(helper)
            track_load(len(helper.transactions))

        models_result = processor.get_models_to_update(
            self.xform.form_id, normal_helpers, deprecated_helpers, ledger_db
        )
        self.models_to_save, self.models_to_delete = models_result

        self.cases_with_deprecated_transactions = {
            trans.case_id for srh in deprecated_helpers for trans in srh.transactions
        }
예제 #4
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 = list(
        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
    if stock_transactions:
        domain = stock_transactions[0].report.domain
        ledger_load_counter("rebuild_stock", domain)(len(stock_transactions))
    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)
예제 #5
0
    def _rebuild_ledger_value_from_transactions(ledger_value, transactions, domain):
        track_load = ledger_load_counter("rebuild_ledger", domain)
        balance = 0
        for transaction in transactions:
            updated_values = _compute_ledger_values(balance, transaction)
            new_balance = updated_values.balance
            new_delta = updated_values.balance - balance
            if new_balance != transaction.updated_balance or new_delta != transaction.delta:
                transaction.delta = new_delta
                transaction.updated_balance = new_balance
                ledger_value.track_update(transaction)
            elif not transaction.is_saved():
                ledger_value.track_create(transaction)
            balance = new_balance
            track_load()
        if balance != ledger_value.balance or ledger_value.has_tracked_models():
            ledger_value.balance = balance

        return ledger_value
예제 #6
0
    def _rebuild_ledger_value_from_transactions(ledger_value, transactions, domain):
        track_load = ledger_load_counter("rebuild_ledger", domain)
        balance = 0
        for transaction in transactions:
            updated_values = _compute_ledger_values(balance, transaction)
            new_balance = updated_values.balance
            new_delta = updated_values.balance - balance
            if new_balance != transaction.updated_balance or new_delta != transaction.delta:
                transaction.delta = new_delta
                transaction.updated_balance = new_balance
                ledger_value.track_update(transaction)
            elif not transaction.is_saved():
                ledger_value.track_create(transaction)
            balance = new_balance
            track_load()
        if balance != ledger_value.balance or ledger_value.has_tracked_models():
            ledger_value.balance = balance

        return ledger_value