コード例 #1
0
    def get_data(self):
        from corehq.form_processor.backends.sql.dbaccessors import LedgerAccessorSQL
        locations = self.locations()

        # locations at this point will only have location objects
        # that have supply points associated
        for loc in locations[:self.config.get('max_rows', 100)]:
            # TODO: this is very inefficient since it loads ALL the transactions up to the supplied
            # date but only requires the most recent one. Should rather use a window function.
            transactions = LedgerAccessorSQL.get_ledger_transactions_in_window(
                case_id=loc.supply_point_id,
                section_id=SECTION_TYPE_STOCK,
                entry_id=None,
                window_start=datetime.min,
                window_end=self.datetime,
            )

            if self.program_id:
                transactions = (tx for tx in transactions
                                if tx.entry_id in self.product_ids)

            stock_results = sorted(transactions,
                                   key=lambda tx: tx.report_date,
                                   reverse=False)

            yield (loc.name,
                   {tx.entry_id: tx.updated_balance
                    for tx in stock_results})