Example #1
0
def process_facility_warehouse_data(facility,
                                    start_date,
                                    end_date,
                                    runner=None):
    """
    process all the facility-level warehouse tables
    """
    logging.info("processing facility %s (%s)" %
                 (facility.name, str(facility.location_id)))

    sql_location = facility.sql_location
    if runner:
        runner.location = sql_location
        runner.save()

    for alert_type in [
            const.SOH_NOT_RESPONDING, const.RR_NOT_RESPONDED,
            const.DELIVERY_NOT_RESPONDING
    ]:
        alert = Alert.objects.filter(location_id=facility.location_id,
                                     date__gte=start_date,
                                     date__lt=end_date,
                                     type=alert_type)
        alert.delete()

    supply_point_id = sql_location.supply_point_id
    location_id = facility.location_id
    new_statuses = SupplyPointStatus.objects.filter(
        location_id=facility.location_id,
        status_date__gte=start_date,
        status_date__lt=end_date).order_by('status_date').iterator()
    process_facility_statuses(location_id, new_statuses)

    new_reports = StockReport.objects.filter(
        stocktransaction__case_id=supply_point_id,
        date__gte=start_date,
        date__lt=end_date,
        stocktransaction__type='stockonhand').distinct().order_by(
            'date').iterator()
    process_facility_product_reports(location_id, new_reports)

    new_trans = get_latest_transaction_from_each_month(supply_point_id,
                                                       start_date, end_date)
    process_facility_transactions(location_id, new_trans, start_date, end_date)

    products = SQLProduct.objects.filter(domain=facility.domain,
                                         is_archived=False)
    users = get_users_by_location_id(facility.domain, facility.get_id)

    # go through all the possible values in the date ranges
    # # and make sure there are warehouse tables there
    for year, month in months_between(start_date, end_date):
        window_date = datetime(year, month, 1)
        # create org_summary for every fac/date combo
        org_summary, created = OrganizationSummary.objects.get_or_create(
            location_id=facility.location_id, date=window_date)

        org_summary.total_orgs = 1
        alt = average_lead_time(facility.location_id, window_date)
        if alt:
            alt = alt.days
        org_summary.average_lead_time_in_days = alt or 0
        org_summary.save()

        # create group_summary for every org_summary title combo
        for title in const.NEEDED_STATUS_TYPES:
            GroupSummary.objects.get_or_create(org_summary=org_summary,
                                               title=title)
        # update all the non-response data
        not_responding_facility(org_summary)
        # alerts
        with transaction.atomic():
            populate_no_primary_alerts(facility, window_date, users)
            populate_facility_stockout_alerts(facility, window_date)

    update_product_availability_facility_data(facility, products, start_date,
                                              end_date)
    update_historical_data_for_location(facility)
Example #2
0
def process_facility_warehouse_data(facility, start_date, end_date, runner):
    """
    process all the facility-level warehouse tables
    """
    logging.info("processing facility %s (%s)" % (facility.name, str(facility._id)))
    try:
        runner.location = facility.sql_location
        runner.save()
    except SQLLocation.DoesNotExist:
        # TODO Temporary fix
        facility.delete()
        return

    for alert_type in [const.SOH_NOT_RESPONDING, const.RR_NOT_RESPONDED, const.DELIVERY_NOT_RESPONDING]:
        alert = Alert.objects.filter(location_id=facility._id, date__gte=start_date, date__lt=end_date,
                                     type=alert_type)
        alert.delete()

    supply_point_id = facility.linked_supply_point()._id
    location_id = facility._id
    new_statuses = SupplyPointStatus.objects.filter(
        location_id=facility._id,
        status_date__gte=start_date,
        status_date__lt=end_date
    ).order_by('status_date').iterator()
    process_facility_statuses(location_id, new_statuses)

    new_reports = StockReport.objects.filter(
        stocktransaction__case_id=supply_point_id,
        date__gte=start_date,
        date__lt=end_date,
        stocktransaction__type='stockonhand'
    ).order_by('date').iterator()
    process_facility_product_reports(location_id, new_reports)

    new_trans = StockTransaction.objects.filter(
        case_id=supply_point_id,
        report__date__gte=start_date,
        report__date__lt=end_date,
    ).exclude(type='consumption').order_by('report__date').iterator()
    process_facility_transactions(location_id, new_trans)

    # go through all the possible values in the date ranges
    # and make sure there are warehouse tables there
    for year, month in months_between(start_date, end_date):
        window_date = datetime(year, month, 1)
        # create org_summary for every fac/date combo
        org_summary, created = OrganizationSummary.objects.get_or_create(
            location_id=facility._id,
            date=window_date
        )

        org_summary.total_orgs = 1
        alt = average_lead_time(facility._id, window_date)
        if alt:
            alt = alt.days
        org_summary.average_lead_time_in_days = alt or 0
        org_summary.save()

        # create group_summary for every org_summary title combo
        for title in const.NEEDED_STATUS_TYPES:
            GroupSummary.objects.get_or_create(org_summary=org_summary,
                                               title=title)
        # update all the non-response data
        not_responding_facility(org_summary)

        # update product availability data
        update_product_availability_facility_data(org_summary)

        # alerts
        with transaction.atomic():
            populate_no_primary_alerts(facility, window_date)
            populate_facility_stockout_alerts(facility, window_date)
Example #3
0
def process_facility_warehouse_data(facility, start_date, end_date, runner=None):
    """
    process all the facility-level warehouse tables
    """
    logging.info("processing facility %s (%s)" % (facility.name, str(facility._id)))

    sql_location = facility.sql_location
    if runner:
        runner.location = sql_location
        runner.save()

    for alert_type in [const.SOH_NOT_RESPONDING, const.RR_NOT_RESPONDED, const.DELIVERY_NOT_RESPONDING]:
        alert = Alert.objects.filter(location_id=facility._id, date__gte=start_date, date__lt=end_date,
                                     type=alert_type)
        alert.delete()

    supply_point_id = sql_location.supply_point_id
    location_id = facility._id
    new_statuses = SupplyPointStatus.objects.filter(
        location_id=facility._id,
        status_date__gte=start_date,
        status_date__lt=end_date
    ).order_by('status_date').iterator()
    process_facility_statuses(location_id, new_statuses)

    new_reports = StockReport.objects.filter(
        stocktransaction__case_id=supply_point_id,
        date__gte=start_date,
        date__lt=end_date,
        stocktransaction__type='stockonhand'
    ).distinct().order_by('date').iterator()
    process_facility_product_reports(location_id, new_reports)

    new_trans = get_latest_transaction_from_each_month(supply_point_id, start_date, end_date)
    process_facility_transactions(location_id, new_trans, start_date, end_date)

    products = SQLProduct.objects.filter(domain=facility.domain, is_archived=False)
    users = get_users_by_location_id(facility.domain, facility.get_id)

    # go through all the possible values in the date ranges
    # # and make sure there are warehouse tables there
    for year, month in months_between(start_date, end_date):
        window_date = datetime(year, month, 1)
        # create org_summary for every fac/date combo
        org_summary, created = OrganizationSummary.objects.get_or_create(
            location_id=facility._id,
            date=window_date
        )

        org_summary.total_orgs = 1
        alt = average_lead_time(facility._id, window_date)
        if alt:
            alt = alt.days
        org_summary.average_lead_time_in_days = alt or 0
        org_summary.save()

        # create group_summary for every org_summary title combo
        for title in const.NEEDED_STATUS_TYPES:
            GroupSummary.objects.get_or_create(org_summary=org_summary,
                                               title=title)
        # update all the non-response data
        not_responding_facility(org_summary)
        # alerts
        with transaction.atomic():
            populate_no_primary_alerts(facility, window_date, users)
            populate_facility_stockout_alerts(facility, window_date)

    update_product_availability_facility_data(facility, products, start_date, end_date)
    update_historical_data_for_location(facility)
Example #4
0
def process_facility_warehouse_data(facility, start_date, end_date, runner):
    """
    process all the facility-level warehouse tables
    """
    logging.info("processing facility %s (%s)" %
                 (facility.name, str(facility._id)))
    try:
        runner.location = facility.sql_location
        runner.save()
    except SQLLocation.DoesNotExist:
        # TODO Temporary fix
        facility.delete()
        return

    for alert_type in [
            const.SOH_NOT_RESPONDING, const.RR_NOT_RESPONDED,
            const.DELIVERY_NOT_RESPONDING
    ]:
        alert = Alert.objects.filter(location_id=facility._id,
                                     date__gte=start_date,
                                     date__lt=end_date,
                                     type=alert_type)
        alert.delete()

    supply_point_id = facility.linked_supply_point()._id
    location_id = facility._id
    new_statuses = SupplyPointStatus.objects.filter(
        location_id=facility._id,
        status_date__gte=start_date,
        status_date__lt=end_date).order_by('status_date').iterator()
    process_facility_statuses(location_id, new_statuses)

    new_reports = StockReport.objects.filter(
        stocktransaction__case_id=supply_point_id,
        date__gte=start_date,
        date__lt=end_date,
        stocktransaction__type='stockonhand').order_by('date').iterator()
    process_facility_product_reports(location_id, new_reports)

    new_trans = StockTransaction.objects.filter(
        case_id=supply_point_id,
        report__date__gte=start_date,
        report__date__lt=end_date,
    ).exclude(type='consumption').order_by('report__date').iterator()
    process_facility_transactions(location_id, new_trans)

    # go through all the possible values in the date ranges
    # and make sure there are warehouse tables there
    for year, month in months_between(start_date, end_date):
        window_date = datetime(year, month, 1)
        # create org_summary for every fac/date combo
        org_summary, created = OrganizationSummary.objects.get_or_create(
            location_id=facility._id, date=window_date)

        org_summary.total_orgs = 1
        alt = average_lead_time(facility._id, window_date)
        if alt:
            alt = alt.days
        org_summary.average_lead_time_in_days = alt or 0
        org_summary.save()

        # create group_summary for every org_summary title combo
        for title in const.NEEDED_STATUS_TYPES:
            GroupSummary.objects.get_or_create(org_summary=org_summary,
                                               title=title)
        # update all the non-response data
        not_responding_facility(org_summary)

        # update product availability data
        update_product_availability_facility_data(org_summary)

        # alerts
        with transaction.atomic():
            populate_no_primary_alerts(facility, window_date)
            populate_facility_stockout_alerts(facility, window_date)