def get_models_to_update(self, form_id, stock_report_helpers, deprecated_helpers, ledger_db=None):
        ledger_db = ledger_db or LedgerDBSQL()
        result = StockModelUpdateResult()

        ledgers_needing_rebuild = {
            deprecated_transaction.ledger_reference
            for deprecated_helper in deprecated_helpers
            for deprecated_transaction in deprecated_helper.transactions
        }

        updated_ledgers = []
        for helper in stock_report_helpers:
            for stock_trans in helper.transactions:
                ledger_value = self._process_transaction(helper, stock_trans, ledger_db)
                updated_ledgers.append(ledger_value)

        for ledger_value in updated_ledgers:
            if ledger_value.ledger_reference in ledgers_needing_rebuild:
                rebuilt_ledger_value = self._rebuild_ledger(form_id, ledger_value)
                result.to_save.append(rebuilt_ledger_value)
            else:
                result.to_save.append(ledger_value)

        return result
dataset/ETHPy150Open dimagi/commcare-hq/corehq/form_processor/backends/sql/ledger.py/LedgerProcessorSQL.get_models_to_update/original