Exemple #1
0
def process_facility_transactions(facility_id, transactions, start_date,
                                  end_date):
    """
    For a given facility and list of transactions, update the appropriate
    data warehouse tables. This should only be called on supply points
    that are facilities.

    """
    existing_data = ProductAvailabilityData.objects.filter(
        date__range=(datetime(start_date.year, start_date.month,
                              1), datetime(end_date.year, end_date.month, 1)),
        location_id=facility_id)

    product_data_dict = {(pa.date, pa.location_id, pa.product): pa
                         for pa in existing_data}
    for year, month, product_id, stock_on_hand in transactions:
        date = datetime(int(year), int(month), 1)
        if (date, facility_id, product_id) in product_data_dict:
            product_data = product_data_dict[(date, facility_id, product_id)]
        else:
            product_data = ProductAvailabilityData(product=product_id,
                                                   location_id=facility_id,
                                                   date=date)
        product_data.total = 1
        product_data.without_data = 0
        if stock_on_hand <= 0:
            product_data.without_stock = 1
            product_data.with_stock = 0
        else:
            product_data.without_stock = 0
            product_data.with_stock = 1

        product_data.save()
Exemple #2
0
def process_facility_transactions(facility_id, transactions, start_date, end_date):
    """
    For a given facility and list of transactions, update the appropriate
    data warehouse tables. This should only be called on supply points
    that are facilities.

    """
    existing_data = ProductAvailabilityData.objects.filter(
        date__range=(
            datetime(start_date.year, start_date.month, 1),
            datetime(end_date.year, end_date.month, 1)
        ),
        location_id=facility_id
    )

    product_data_dict = {
        (pa.date, pa.location_id, pa.product): pa for pa in existing_data
    }
    for year, month, product_id, stock_on_hand in transactions:
        date = datetime(int(year), int(month), 1)
        if (date, facility_id, product_id) in product_data_dict:
            product_data = product_data_dict[(date, facility_id, product_id)]
        else:
            product_data = ProductAvailabilityData(
                product=product_id,
                location_id=facility_id,
                date=date
            )
        product_data.total = 1
        product_data.without_data = 0
        if stock_on_hand <= 0:
            product_data.without_stock = 1
            product_data.with_stock = 0
        else:
            product_data.without_stock = 0
            product_data.with_stock = 1

        product_data.save()
Exemple #3
0
def update_product_availability_facility_data(facility, products, start_date,
                                              end_date):
    # product availability

    existing_data = ProductAvailabilityData.objects.filter(
        date__range=(datetime(start_date.year, start_date.month,
                              1), datetime(end_date.year, end_date.month, 1)),
        location_id=facility.get_id)

    product_data_dict = {(pa.date, pa.location_id, pa.product): pa
                         for pa in existing_data}

    product_data_list = []
    previous_month = {}
    for year, month in months_between(start_date, end_date):
        window_date = datetime(year, month, 1)
        for p in products:
            now = datetime.utcnow()
            if (window_date, facility.get_id,
                    p.product_id) in product_data_dict:
                previous_month[p.product_id] = product_data_dict[
                    window_date, facility.get_id, p.product_id]
                continue
            else:
                product_data = ProductAvailabilityData(
                    date=window_date,
                    location_id=facility.get_id,
                    product=p.product_id,
                    create_date=now,
                    update_date=now)

            # set defaults
            product_data.total = 1
            prev = None
            if p.product_id in previous_month:
                prev = previous_month[p.product_id]
            if not prev:
                previous_reports = ProductAvailabilityData.objects.filter(
                    product=p.product_id,
                    location_id=facility.location_id,
                    date__lt=window_date,
                    total=1)
                if previous_reports.count():
                    prev = previous_reports.latest('date')

            if prev:
                product_data.with_stock = prev.with_stock
                product_data.without_stock = prev.without_stock
                product_data.without_data = prev.without_data
            else:
                # otherwise we use the defaults
                product_data.with_stock = 0
                product_data.without_stock = 0
                product_data.without_data = 1
            if product_data.pk is not None:
                product_data.save()
            else:
                product_data_list.append(product_data)
            assert (product_data.with_stock + product_data.without_stock + product_data.without_data) == 1, \
                "bad product data config for %s" % product_data
            previous_month[p.product_id] = product_data
    ProductAvailabilityData.objects.bulk_create(product_data_list)
Exemple #4
0
def update_product_availability_facility_data(facility, products, start_date, end_date):
    # product availability

    existing_data = ProductAvailabilityData.objects.filter(
        date__range=(
            datetime(start_date.year, start_date.month, 1),
            datetime(end_date.year, end_date.month, 1)
        ),
        location_id=facility.get_id
    )

    product_data_dict = {
        (pa.date, pa.location_id, pa.product): pa for pa in existing_data
    }

    product_data_list = []
    previous_month = {}
    for year, month in months_between(start_date, end_date):
        window_date = datetime(year, month, 1)
        for p in products:
            now = datetime.utcnow()
            if (window_date, facility.get_id, p.product_id) in product_data_dict:
                previous_month[p.product_id] = product_data_dict[window_date, facility.get_id, p.product_id]
                continue
            else:
                product_data = ProductAvailabilityData(
                    date=window_date,
                    location_id=facility.get_id,
                    product=p.product_id,
                    create_date=now,
                    update_date=now
                )

            # set defaults
            product_data.total = 1
            prev = None
            if p.product_id in previous_month:
                prev = previous_month[p.product_id]
            if not prev:
                previous_reports = ProductAvailabilityData.objects.filter(
                    product=p.product_id,
                    location_id=facility._id,
                    date__lt=window_date,
                    total=1
                )
                if previous_reports.count():
                    prev = previous_reports.latest('date')

            if prev:
                product_data.with_stock = prev.with_stock
                product_data.without_stock = prev.without_stock
                product_data.without_data = prev.without_data
            else:
                # otherwise we use the defaults
                product_data.with_stock = 0
                product_data.without_stock = 0
                product_data.without_data = 1
            if product_data.pk is not None:
                product_data.save()
            else:
                product_data_list.append(product_data)
            assert (product_data.with_stock + product_data.without_stock + product_data.without_data) == 1, \
                "bad product data config for %s" % product_data
            previous_month[p.product_id] = product_data
    ProductAvailabilityData.objects.bulk_create(product_data_list)