Exemple #1
0
def update_stock_state_for_transaction(instance):
    # todo: in the worst case, this function makes
    # - three calls to couch (for the case, domain, and commtrack config)
    # - three postgres queries (product, location, and state)
    # - one postgres write (to save the state)
    # and that doesn't even include the consumption calc, which can do a whole
    # bunch more work and hit the database.
    try:
        domain_name = instance.domain
    except AttributeError:
        domain_name = CommCareCase.get(instance.case_id).domain

    domain = Domain.get_by_name(domain_name)

    sql_product = SQLProduct.objects.get(product_id=instance.product_id)

    try:
        sql_location = SQLLocation.objects.get(supply_point_id=instance.case_id)
    except SQLLocation.DoesNotExist:
        sql_location = None

    try:
        state = StockState.include_archived.get(
            section_id=instance.section_id,
            case_id=instance.case_id,
            product_id=instance.product_id,
        )
    except StockState.DoesNotExist:
        state = StockState(
            section_id=instance.section_id,
            case_id=instance.case_id,
            product_id=instance.product_id,
            sql_product=sql_product,
            sql_location=sql_location,
        )

    state.last_modified_date = instance.report.date
    state.stock_on_hand = instance.stock_on_hand

    if domain and domain.commtrack_settings:
        consumption_calc = domain.commtrack_settings.get_consumption_config()
    else:
        consumption_calc = None

    state.daily_consumption = compute_daily_consumption(
        instance.case_id,
        instance.product_id,
        instance.report.date,
        'stock',
        consumption_calc
    )
    # so you don't have to look it up again in the signal receivers
    if domain:
        state.domain = domain.name
    state.save()
Exemple #2
0
    def testDefaultValue(self):
        self._stock_report(25, 5)
        self._stock_report(10, 0)

        self.assertEqual(
            None,
            compute_daily_consumption(
                self.domain.name,
                self.case_id,
                self.product_id,
                now,
                configuration=ConsumptionConfiguration(min_periods=4)))
Exemple #3
0
def update_stock_state_for_transaction(instance):
    try:
        domain_name = instance.domain
    except AttributeError:
        domain_name = CommCareCase.get(instance.case_id).domain

    domain = Domain.get_by_name(domain_name)

    sql_product = SQLProduct.objects.get(product_id=instance.product_id)

    try:
        sql_location = SQLLocation.objects.get(supply_point_id=instance.case_id)
    except SQLLocation.DoesNotExist:
        sql_location = None

    try:
        state = StockState.include_archived.get(
            section_id=instance.section_id,
            case_id=instance.case_id,
            product_id=instance.product_id,
        )
    except StockState.DoesNotExist:
        state = StockState(
            section_id=instance.section_id,
            case_id=instance.case_id,
            product_id=instance.product_id,
            sql_product=sql_product,
            sql_location=sql_location,
        )

    state.last_modified_date = instance.report.date
    state.stock_on_hand = instance.stock_on_hand

    if domain and domain.commtrack_settings:
        consumption_calc = domain.commtrack_settings.get_consumption_config()
    else:
        consumption_calc = None

    state.daily_consumption = compute_daily_consumption(
        instance.case_id,
        instance.product_id,
        instance.report.date,
        'stock',
        consumption_calc
    )
    # so you don't have to look it up again in the signal receivers
    state.domain = domain
    state.save()
Exemple #4
0
def get_consumption_for_ledger_json(ledger_json):
    from corehq.apps.domain.models import Domain
    from casexml.apps.stock.consumption import compute_daily_consumption
    from dimagi.utils.parsing import string_to_utc_datetime

    domain_name = ledger_json['domain']
    domain = Domain.get_by_name(domain_name)
    if domain and domain.commtrack_settings:
        consumption_calc = domain.commtrack_settings.get_consumption_config()
    else:
        consumption_calc = None
    daily_consumption = compute_daily_consumption(
        domain_name, ledger_json['case_id'], ledger_json['entry_id'],
        string_to_utc_datetime(ledger_json['last_modified']), 'stock',
        consumption_calc)
    return daily_consumption
    def testDefaultValue(self):
        self._stock_report(25, 5)
        self._stock_report(10, 0)

        self.assertEqual(None, compute_daily_consumption(
            self.case_id, self.product_id,
            now, configuration=ConsumptionConfiguration(min_periods=4))
        )
        _ten = lambda case_id, product_id: 10
        self.assertAlmostEqual(10., compute_consumption_or_default(
            self.case_id,
            self.product_id,
            now,
            configuration=ConsumptionConfiguration(
                min_periods=4,
                default_monthly_consumption_function=_ten
            )
        ))
Exemple #6
0
    def testDefaultValue(self):
        self._stock_report(25, 5)
        self._stock_report(10, 0)

        self.assertEqual(None, compute_daily_consumption(
            self.domain.name, self.case_id, self.product_id,
            now, configuration=ConsumptionConfiguration(min_periods=4))
        )
        _ten = lambda case_id, product_id: 10
        self.assertAlmostEqual(10., compute_consumption_or_default(
            self.domain.name,
            self.case_id,
            self.product_id,
            now,
            configuration=ConsumptionConfiguration(
                min_periods=4,
                default_monthly_consumption_function=_ten
            )
        ))
Exemple #7
0
def get_consumption_for_ledger_json(ledger_json):
    from corehq.apps.domain.models import Domain
    from casexml.apps.stock.consumption import compute_daily_consumption
    from dimagi.utils.parsing import string_to_utc_datetime

    domain_name = ledger_json['domain']
    domain_obj = Domain.get_by_name(domain_name)
    if domain_obj and domain_obj.commtrack_settings:
        consumption_calc = domain_obj.commtrack_settings.get_consumption_config()
    else:
        consumption_calc = None
    daily_consumption = compute_daily_consumption(
        domain_name,
        ledger_json['case_id'],
        ledger_json['entry_id'],
        string_to_utc_datetime(ledger_json['last_modified']),
        'stock',
        consumption_calc
    )
    return daily_consumption
Exemple #8
0
def update_stock_state_for_transaction(instance):
    from corehq.apps.commtrack.models import StockState
    from corehq.apps.locations.models import SQLLocation
    from corehq.apps.products.models import SQLProduct

    # todo: in the worst case, this function makes
    # - three calls to couch (for the case, domain, and commtrack config)
    # - four postgres queries (transacitons, product, location, and state)
    # - one postgres write (to save the state)
    # and that doesn't even include the consumption calc, which can do a whole
    # bunch more work and hit the database.
    sql_product = SQLProduct.objects.get(product_id=instance.product_id)
    try:
        domain_name = instance.domain
    except AttributeError:
        domain_name = sql_product.domain

    domain = Domain.get_by_name(domain_name)

    try:
        sql_location = SQLLocation.objects.get(
            supply_point_id=instance.case_id)
    except SQLLocation.DoesNotExist:
        sql_location = None

    try:
        state = StockState.include_archived.get(
            section_id=instance.section_id,
            case_id=instance.case_id,
            product_id=instance.product_id,
        )
    except StockState.DoesNotExist:
        state = StockState(
            section_id=instance.section_id,
            case_id=instance.case_id,
            product_id=instance.product_id,
            sql_product=sql_product,
            sql_location=sql_location,
        )

    # we may not be saving the latest transaction so make sure we use that
    # todo: this should change to server date
    latest_transaction = StockTransaction.latest(
        case_id=instance.case_id,
        section_id=instance.section_id,
        product_id=instance.product_id)
    if latest_transaction != instance:
        logging.warning(
            'Just fired signal for a stale stock transaction. Domain: {}, instance: {},latest was {}'
            .format(domain_name, instance, latest_transaction))
        instance = latest_transaction
    state.last_modified_date = instance.report.date
    state.stock_on_hand = instance.stock_on_hand

    if domain and domain.commtrack_settings:
        consumption_calc = domain.commtrack_settings.get_consumption_config()
    else:
        consumption_calc = None

    state.daily_consumption = compute_daily_consumption(
        instance.case_id, instance.product_id, instance.report.date, 'stock',
        consumption_calc)
    # so you don't have to look it up again in the signal receivers
    if domain:
        state.domain = domain.name
    state.save()
Exemple #9
0
def update_stock_state_for_transaction(instance):
    from corehq.apps.commtrack.models import StockState
    from corehq.apps.locations.models import SQLLocation
    from corehq.apps.products.models import SQLProduct

    # todo: in the worst case, this function makes
    # - three calls to couch (for the case, domain, and commtrack config)
    # - four postgres queries (transacitons, product, location, and state)
    # - one postgres write (to save the state)
    # and that doesn't even include the consumption calc, which can do a whole
    # bunch more work and hit the database.
    sql_product = SQLProduct.objects.get(product_id=instance.product_id)
    try:
        domain_name = instance.domain
    except AttributeError:
        domain_name = sql_product.domain

    domain = Domain.get_by_name(domain_name)

    try:
        sql_location = SQLLocation.objects.get(supply_point_id=instance.case_id)
    except SQLLocation.DoesNotExist:
        sql_location = None

    try:
        state = StockState.include_archived.get(
            section_id=instance.section_id,
            case_id=instance.case_id,
            product_id=instance.product_id,
        )
    except StockState.DoesNotExist:
        state = StockState(
            section_id=instance.section_id,
            case_id=instance.case_id,
            product_id=instance.product_id,
            sql_product=sql_product,
            sql_location=sql_location,
        )

    # we may not be saving the latest transaction so make sure we use that
    # todo: this should change to server date
    latest_transaction = StockTransaction.latest(
        case_id=instance.case_id,
        section_id=instance.section_id,
        product_id=instance.product_id
    )
    if latest_transaction != instance:
        logging.warning(
            'Just fired signal for a stale stock transaction. Domain: {}, instance: {},latest was {}'.format(
                domain_name, instance, latest_transaction
            )
        )
        instance = latest_transaction
    state.last_modified_date = instance.report.date
    state.stock_on_hand = instance.stock_on_hand

    if domain and domain.commtrack_settings:
        consumption_calc = domain.commtrack_settings.get_consumption_config()
    else:
        consumption_calc = None

    state.daily_consumption = compute_daily_consumption(
        domain_name,
        instance.case_id,
        instance.product_id,
        instance.report.date,
        'stock',
        consumption_calc
    )
    # so you don't have to look it up again in the signal receivers
    if domain:
        state.domain = domain.name
    state.save()