Ejemplo n.º 1
0
def stockout_process_user(user, test=False):
    if user_has_reporting_location(user):
        location = user.location
        supply_point = SupplyPointCase.get_by_location(location)
        if supply_point and user.get_verified_number():
            products = [
                SQLProduct.objects.get(product_id=state.product_id).name
                for state in StockState.objects.filter(
                    case_id=supply_point._id,
                    stock_on_hand=0,
                    product_id__in=[
                        product.product_id
                        for product in location.sql_location.products
                    ])
            ]
            if products:
                if not test:
                    send_sms_to_verified_number(
                        user.get_verified_number(), STOCKOUT_REPORT % {
                            'name': user.name,
                            'facility': supply_point.name,
                            'date':
                            datetime.datetime.utcnow().strftime('%b %d'),
                            'products': ", ".join(products)
                        })
                else:
                    send_test_message(
                        user.get_verified_number(), STOCKOUT_REPORT % {
                            'name': user.name,
                            'facility': supply_point.name,
                            'date':
                            datetime.datetime.utcnow().strftime('%b %d'),
                            'products': ", ".join(products)
                        })
Ejemplo n.º 2
0
def rrirv_process_user(user, test=False):
    if user_has_reporting_location(user) and user_has_role(user, IN_CHARGE_ROLE):
        if user.get_verified_number():
            if not test:
                send_sms_to_verified_number(user.get_verified_number(), RRIRV_REMINDER % {'name': user.name})
            else:
                send_test_message(user.get_verified_number(), RRIRV_REMINDER % {'name': user.name})
Ejemplo n.º 3
0
def visit_website_process_user(user, test=False):
    date = datetime.datetime.utcnow() - datetime.timedelta(weeks=13)
    if user.last_login < date and user.get_verified_number():
        if not test:
            send_sms_to_verified_number(user.get_verified_number(), WEB_REMINDER % {'name': user.name})
        else:
            send_test_message(user.get_verified_number(), WEB_REMINDER % {'name': user.name})
Ejemplo n.º 4
0
def on_going_process_user(user, test=False):
    now = datetime.datetime.utcnow()
    date = now - datetime.timedelta(days=21)
    user_location = user.sql_location
    if not user_location:
        return

    facilities = []
    if user_location.location_type.name == 'district':
        facilities = user_location.get_children()
    elif user_location.location_type.name == 'region':
        facilities = SQLLocation.objects.filter(
            domain=user.domain, parent__parent__location_id=user.location._id)
    fac = set()
    for facility in facilities:
        sp = facility.supply_point_id
        if not sp:
            continue
        transactions_exist = StockTransaction.objects.filter(
            case_id=sp, type="stockonhand", report__date__gte=date).exists()
        if not transactions_exist:
            fac.add(unicode(facility.name))
    verified_number = user.get_verified_number()
    if fac and verified_number:
        message = ONGOING_NON_REPORTING % " \n".join(fac)
        if not test:
            send_sms_to_verified_number(verified_number, message)
        else:
            send_test_message(verified_number, message)
        if can_receive_email(user, verified_number):
            email = str(user.email)
            send_mail('ONGOING NON REPORTING', message,
                      '*****@*****.**', [email])
Ejemplo n.º 5
0
def report_reminder_process_user(user, test=False):
    now = datetime.datetime.utcnow()
    date = now - datetime.timedelta(days=7)

    if not user.location or user.location.location_type.administrative:
        return
    sp = SupplyPointCase.get_by_location(user.location)
    if not sp:
        return
    transaction_exists = StockTransaction.objects.filter(
        case_id=sp._id,
        type="stockonhand",
        report__date__gte=date
    ).exists()
    if sp and not transaction_exists and user.get_verified_number():
        message = REPORT_REMINDER % (user.name, user.location.name)
        verified_number = user.get_verified_number()
        if not test:
            send_sms_to_verified_number(verified_number, message)
        else:
            send_test_message(verified_number, message)

        if can_receive_email(user, verified_number):
            email = str(user.email)
            send_mail('REPORT REMINDER', message, '*****@*****.**', [email])
Ejemplo n.º 6
0
def stockout_process_user(user, test=False):
    if user_has_reporting_location(user):
        location = user.location
        supply_point = SupplyPointCase.get_by_location(location)
        if supply_point and user.get_verified_number():
            products = [
                SQLProduct.objects.get(product_id=state.product_id).name
                for state in StockState.objects.filter(
                    case_id=supply_point._id, stock_on_hand=0,
                    product_id__in=[product.product_id for product in location.sql_location.products]
                )
            ]
            if products:
                if not test:
                    send_sms_to_verified_number(
                        user.get_verified_number(),
                        STOCKOUT_REPORT % {
                            'name': user.name,
                            'facility': supply_point.name,
                            'date': datetime.datetime.utcnow().strftime('%b %d'),
                            'products': ", ".join(products)
                        }
                    )
                else:
                    send_test_message(
                        user.get_verified_number(),
                        STOCKOUT_REPORT % {
                            'name': user.name,
                            'facility': supply_point.name,
                            'date': datetime.datetime.utcnow().strftime('%b %d'),
                            'products': ", ".join(products)
                        }
                    )
Ejemplo n.º 7
0
def on_going_process_user(user, test=False):
    now = datetime.datetime.utcnow()
    date = now - datetime.timedelta(days=21)
    user_location = user.sql_location
    if not user_location:
        return

    facilities = []
    if user_location.location_type.name == 'district':
        facilities = user_location.get_children()
    elif user_location.location_type.name == 'region':
        facilities = SQLLocation.objects.filter(domain=user.domain,
                                                parent__parent__location_id=user.location._id)
    fac = set()
    for facility in facilities:
        sp = facility.supply_point_id
        if not sp:
            continue
        transactions_exist = StockTransaction.objects.filter(
            case_id=sp,
            type="stockonhand",
            report__date__gte=date
        ).exists()
        if not transactions_exist:
            fac.add(unicode(facility.name))
    verified_number = user.get_verified_number()
    if fac and verified_number:
        message = ONGOING_NON_REPORTING % " \n".join(fac)
        if not test:
            send_sms_to_verified_number(verified_number, message)
        else:
            send_test_message(verified_number, message)
        if can_receive_email(user, verified_number):
            email = str(user.email)
            send_mail('ONGOING NON REPORTING', message, '*****@*****.**', [email])
Ejemplo n.º 8
0
def visit_website_process_user(user, test=False):
    date = datetime.datetime.utcnow() - datetime.timedelta(weeks=13)
    if user.last_login < date and user.get_verified_number():
        if not test:
            send_sms_to_verified_number(user.get_verified_number(),
                                        WEB_REMINDER % {'name': user.name})
        else:
            send_test_message(user.get_verified_number(),
                              WEB_REMINDER % {'name': user.name})
Ejemplo n.º 9
0
def first_soh_process_user(user, test=False):
    if user_has_reporting_location(user):
        if user.get_verified_number():
            message = STOCK_ON_HAND_REMINDER % {'name': user.name}
            if not test:
                send_sms_to_verified_number(user.get_verified_number(),
                                            message)
            else:
                send_test_message(user.get_verified_number(), message)
Ejemplo n.º 10
0
def rrirv_process_user(user, test=False):
    if user_has_reporting_location(user) and user_has_role(
            user, IN_CHARGE_ROLE):
        if user.get_verified_number():
            if not test:
                send_sms_to_verified_number(
                    user.get_verified_number(),
                    RRIRV_REMINDER % {'name': user.name})
            else:
                send_test_message(user.get_verified_number(),
                                  RRIRV_REMINDER % {'name': user.name})
Ejemplo n.º 11
0
def third_soh_process_users_and_facilities(users, facilities, test=False):
    date = datetime.datetime.utcnow() - datetime.timedelta(days=DAYS_UNTIL_LATE)
    for facility in facilities:
        if not facility.supply_point_id:
            continue
        on_time_products = StockState.objects.filter(
            case_id=facility.supply_point_id,
            last_modified_date__gte=date
        )

        location_products = [product.product_id for product in facility.products]
        reported_products = [stock_state.product_id for stock_state in on_time_products]
        missing_products = set(location_products) - set(reported_products)
        if not on_time_products:
            for user in users:
                if user.location and user.location._id == facility.location_id \
                        and user_has_role(user, IN_CHARGE_ROLE) and user.get_verified_number():
                    if not test:
                        send_sms_to_verified_number(
                            user.get_verified_number(),
                            THIRD_STOCK_ON_HAND_REMINDER % {'name': user.name, 'facility': facility.name}
                        )
                    else:
                        send_test_message(
                            user.get_verified_number(),
                            THIRD_STOCK_ON_HAND_REMINDER % {'name': user.name, 'facility': facility.name}
                        )
        elif missing_products:
            for user in users:
                if user.location and user.location._id == facility.location_id and \
                        user_has_role(user, IN_CHARGE_ROLE) and user.get_verified_number():
                    if not test:
                        send_sms_to_verified_number(
                            user.get_verified_number(),
                            INCOMPLETE_SOH_TO_SUPER % {
                                'name': user.name,
                                'facility': facility.name,
                                'products': ", ".join([SQLProduct.objects.get(
                                    product_id=product_id).name for product_id in missing_products])
                            }
                        )
                    else:
                        send_test_message(
                            user.get_verified_number(),
                            INCOMPLETE_SOH_TO_SUPER % {
                                'name': user.name,
                                'facility': facility.name,
                                'products': ", ".join([SQLProduct.objects.get(
                                    product_id=product_id).name for product_id in missing_products])
                            }
                        )
Ejemplo n.º 12
0
def first_soh_process_user(user, test=False):
    if user_has_reporting_location(user):
        if user.get_verified_number():
            message = STOCK_ON_HAND_REMINDER % {'name': user.name}
            if not test:
                send_sms_to_verified_number(
                    user.get_verified_number(),
                    message
                )
            else:
                send_test_message(
                    user.get_verified_number(),
                    message
                )
Ejemplo n.º 13
0
def second_soh_process_user(user, test=False):
    now = datetime.datetime.utcnow()
    date = now - datetime.timedelta(days=DAYS_UNTIL_LATE)
    supply_point = SupplyPointCase.get_by_location(user.location)
    if not supply_point:
        return

    stock_states = StockState.objects.filter(
        case_id=supply_point._id,
        last_modified_date__gte=date
    )
    products = user.sql_location.products
    location_products_ids = [product.product_id for product in products]
    reported_products_ids = [stock_state.product_id for stock_state in stock_states]
    missing_products_ids = set(location_products_ids) - set(reported_products_ids)
    if not user_has_reporting_location(user) or not user.get_verified_number():
        return

    if not stock_states:
        if not test:
            send_sms_to_verified_number(
                user.get_verified_number(),
                SECOND_STOCK_ON_HAND_REMINDER % {'name': user.name}
            )
        else:
            send_test_message(
                user.get_verified_number(),
                SECOND_STOCK_ON_HAND_REMINDER % {'name': user.name}
            )
    elif missing_products_ids:
        products_names = [
            product.name
            for product in products
            if product.product_id in missing_products_ids
        ]
        if not test:
            send_sms_to_verified_number(
                user.get_verified_number(),
                SECOND_INCOMPLETE_SOH_REMINDER %
                {'name': user.name, 'products': ", ".join(products_names)}
            )
        else:
            send_test_message(
                user.get_verified_number(),
                SECOND_INCOMPLETE_SOH_REMINDER %
                {'name': user.name, 'products': ", ".join(products_names)}
            )
Ejemplo n.º 14
0
def urgent_stockout_process_user(user, test=False):
    user_location = user.sql_location
    if not user_location:
        return

    facilities = []
    if user_location.location_type.name == 'district':
        facilities = user_location.get_children()
    elif user_location.location_type.name == 'region':
        facilities = SQLLocation.objects.filter(
            domain=user.domain, parent__parent__location_id=user.location._id)
    elif user_location.location_type.name == 'country':
        facilities = SQLLocation.objects.filter(
            domain=user.domain,
            parent__parent__parent__location_id=user.location._id)
    stocked_out_products = set()
    fac = set()
    no_rep = 0
    for facility in facilities:
        sp = facility.supply_point_id
        if sp:
            stocked_out = StockState.objects.filter(case_id=sp,
                                                    section_id="stockonhand",
                                                    stock_on_hand=0)
            if stocked_out.exists():
                no_rep += 1
                fac.add(unicode(facility))
                for product in stocked_out:
                    sql_product = SQLProduct.objects.get(
                        product_id=product.product_id)
                    stocked_out_products.add(sql_product.name)

    if fac and no_rep >= (len(facilities) / 2) and user.get_verified_number():
        stockout_str = ", ".join(
            sorted([unicode(product) for product in stocked_out_products]))
        message = URGENT_STOCKOUT % (user_location.name, stockout_str)
        verified_number = user.get_verified_number()
        if not test:
            send_sms_to_verified_number(verified_number, message)
        else:
            send_test_message(verified_number, message)

        if can_receive_email(user, verified_number):
            email = str(user.email)
            send_mail('URGENT STOCKOUT', message,
                      '*****@*****.**', [email])
Ejemplo n.º 15
0
def second_soh_process_user(user, test=False):
    now = datetime.datetime.utcnow()
    date = now - datetime.timedelta(days=DAYS_UNTIL_LATE)
    supply_point = SupplyPointCase.get_by_location(user.location)
    if not supply_point:
        return

    stock_states = StockState.objects.filter(case_id=supply_point._id,
                                             last_modified_date__gte=date)
    products = user.sql_location.products
    location_products_ids = [product.product_id for product in products]
    reported_products_ids = [
        stock_state.product_id for stock_state in stock_states
    ]
    missing_products_ids = set(location_products_ids) - set(
        reported_products_ids)
    if not user_has_reporting_location(user) or not user.get_verified_number():
        return

    if not stock_states:
        if not test:
            send_sms_to_verified_number(
                user.get_verified_number(),
                SECOND_STOCK_ON_HAND_REMINDER % {'name': user.name})
        else:
            send_test_message(
                user.get_verified_number(),
                SECOND_STOCK_ON_HAND_REMINDER % {'name': user.name})
    elif missing_products_ids:
        products_names = [
            product.name for product in products
            if product.product_id in missing_products_ids
        ]
        if not test:
            send_sms_to_verified_number(
                user.get_verified_number(), SECOND_INCOMPLETE_SOH_REMINDER % {
                    'name': user.name,
                    'products': ", ".join(products_names)
                })
        else:
            send_test_message(
                user.get_verified_number(), SECOND_INCOMPLETE_SOH_REMINDER % {
                    'name': user.name,
                    'products': ", ".join(products_names)
                })
Ejemplo n.º 16
0
def urgent_stockout_process_user(user, test=False):
    user_location = user.sql_location
    if not user_location:
        return

    facilities = []
    if user_location.location_type.name == 'district':
        facilities = user_location.get_children()
    elif user_location.location_type.name == 'region':
        facilities = SQLLocation.objects.filter(domain=user.domain,
                                                parent__parent__location_id=user.location._id)
    elif user_location.location_type.name == 'country':
        facilities = SQLLocation.objects.filter(domain=user.domain,
                                                parent__parent__parent__location_id=user.location._id)
    stocked_out_products = set()
    fac = set()
    no_rep = 0
    for facility in facilities:
        sp = facility.supply_point_id
        if sp:
            stocked_out = StockState.objects.filter(
                case_id=sp, section_id="stockonhand", stock_on_hand=0
            )
            if stocked_out.exists():
                no_rep += 1
                fac.add(unicode(facility))
                for product in stocked_out:
                    sql_product = SQLProduct.objects.get(product_id=product.product_id)
                    stocked_out_products.add(sql_product.name)

    if fac and no_rep >= (len(facilities) / 2) and user.get_verified_number():
        stockout_str = ", ".join(sorted(
            [unicode(product) for product in stocked_out_products]
        ))
        message = URGENT_STOCKOUT % (user_location.name, stockout_str)
        verified_number = user.get_verified_number()
        if not test:
            send_sms_to_verified_number(verified_number, message)
        else:
            send_test_message(verified_number, message)

        if can_receive_email(user, verified_number):
            email = str(user.email)
            send_mail('URGENT STOCKOUT', message, '*****@*****.**', [email])
Ejemplo n.º 17
0
def first_soh_process_user(user, test=False):
    if user_has_reporting_location(user) and user_has_role(user, IN_CHARGE_ROLE):
        supply_point = SupplyPointCase.get_by_location(user.location)
        transaction_exists = StockTransaction.objects.filter(
            case_id=supply_point._id,
            type='stockonhand'
        ).exists()
        if supply_point and not transaction_exists and user.get_verified_number():
            message = STOCK_ON_HAND_REMINDER % {'name': user.name}
            if not test:
                send_sms_to_verified_number(
                    user.get_verified_number(),
                    message
                )
            else:
                send_test_message(
                    user.get_verified_number(),
                    message
                )
Ejemplo n.º 18
0
def second_soh_process_user(user, date, test=False):
    supply_point = SupplyPointCase.get_by_location(user.location)
    if not supply_point:
        return

    stock_states = StockState.objects.filter(
        case_id=supply_point._id,
        last_modified_date__gte=date
    )

    location_products = [product.product_id for product in user.location.sql_location.products]
    reported_products = [stock_state.product_id for stock_state in stock_states]
    missing_products = set(location_products) - set(reported_products)
    if user_has_reporting_location(user) and user_has_role(user, IN_CHARGE_ROLE):
        if user.get_verified_number():
            if not stock_states:
                if not test:
                    send_sms_to_verified_number(
                        user.get_verified_number(),
                        SECOND_STOCK_ON_HAND_REMINDER % {'name': user.name}
                    )
                else:
                    send_test_message(
                        user.get_verified_number(),
                        SECOND_STOCK_ON_HAND_REMINDER % {'name': user.name}
                    )
            elif missing_products:
                if not test:
                    send_sms_to_verified_number(
                        user.get_verified_number(),
                        SECOND_INCOMPLETE_SOH_REMINDER %
                        {'name': user.name, 'products': ", ".join(missing_products)}
                    )
                else:
                    send_test_message(
                        user.get_verified_number(),
                        SECOND_INCOMPLETE_SOH_REMINDER %
                        {'name': user.name, 'products': ", ".join(missing_products)}
                    )
Ejemplo n.º 19
0
def report_reminder_process_user(user, test=False):
    now = datetime.datetime.utcnow()
    date = now - datetime.timedelta(days=7)

    if not user.location or user.location.location_type.administrative:
        return
    sp = SupplyPointCase.get_by_location(user.location)
    if not sp:
        return
    transaction_exists = StockTransaction.objects.filter(
        case_id=sp._id, type="stockonhand", report__date__gte=date).exists()
    if sp and not transaction_exists and user.get_verified_number():
        message = REPORT_REMINDER % (user.name, user.location.name)
        verified_number = user.get_verified_number()
        if not test:
            send_sms_to_verified_number(verified_number, message)
        else:
            send_test_message(verified_number, message)

        if can_receive_email(user, verified_number):
            email = str(user.email)
            send_mail('REPORT REMINDER', message,
                      '*****@*****.**', [email])
Ejemplo n.º 20
0
def urgent_non_reporting_process_user(user, test=False):
    now = datetime.datetime.utcnow()
    date = now - datetime.timedelta(days=30)
    user_location = user.sql_location
    if not user_location:
        return
    facilities = []
    if user_location.location_type.name == 'district':
        facilities = user_location.get_children()
    elif user_location.location_type.name == 'region':
        facilities = SQLLocation.objects.filter(domain=user.domain,
                                                parent__parent__location_id=user.location._id)
    elif user_location.location_type.name == 'country':
        facilities = SQLLocation.objects.filter(domain=user.domain,
                                                parent__parent__parent__location_id=user.location._id)
    fac = set()
    no_rep = 0
    for facility in facilities:
        sp = facility.supply_point_id
        transaction_exists = StockTransaction.objects.filter(
            case_id=sp,
            type="stockonhand",
            report__date__gte=date
        ).exists()
        if sp and not transaction_exists:
            fac.add(unicode(facility.name))
            no_rep += 1
    if fac and no_rep >= len(facilities) / 2 and user.get_verified_number():
        message = URGENT_NON_REPORTING % user.location.name
        verified_number = user.get_verified_number()
        if not test:
            send_sms_to_verified_number(verified_number, message)
        else:
            send_test_message(verified_number, message)
        if can_receive_email(user, verified_number):
            email = str(user.email)
            send_mail('URGENT NON REPORTING', message, '*****@*****.**', [email])
Ejemplo n.º 21
0
def urgent_non_reporting_process_user(user, test=False):
    now = datetime.datetime.utcnow()
    date = now - datetime.timedelta(days=30)
    user_location = user.sql_location
    if not user_location:
        return
    facilities = []
    if user_location.location_type.name == 'district':
        facilities = user_location.get_children()
    elif user_location.location_type.name == 'region':
        facilities = SQLLocation.objects.filter(
            domain=user.domain, parent__parent__location_id=user.location._id)
    elif user_location.location_type.name == 'country':
        facilities = SQLLocation.objects.filter(
            domain=user.domain,
            parent__parent__parent__location_id=user.location._id)
    fac = set()
    no_rep = 0
    for facility in facilities:
        sp = facility.supply_point_id
        transaction_exists = StockTransaction.objects.filter(
            case_id=sp, type="stockonhand", report__date__gte=date).exists()
        if sp and not transaction_exists:
            fac.add(unicode(facility.name))
            no_rep += 1
    if fac and no_rep >= len(facilities) / 2 and user.get_verified_number():
        message = URGENT_NON_REPORTING % user.location.name
        verified_number = user.get_verified_number()
        if not test:
            send_sms_to_verified_number(verified_number, message)
        else:
            send_test_message(verified_number, message)
        if can_receive_email(user, verified_number):
            email = str(user.email)
            send_mail('URGENT NON REPORTING', message,
                      '*****@*****.**', [email])
Ejemplo n.º 22
0
def third_soh_process_users_and_facilities(users, facilities, test=False):
    date = datetime.datetime.utcnow() - datetime.timedelta(
        days=DAYS_UNTIL_LATE)
    for facility in facilities:
        if not facility.supply_point_id:
            continue
        on_time_products = StockState.objects.filter(
            case_id=facility.supply_point_id, last_modified_date__gte=date)

        location_products = [
            product.product_id for product in facility.products
        ]
        reported_products = [
            stock_state.product_id for stock_state in on_time_products
        ]
        missing_products = set(location_products) - set(reported_products)
        if not on_time_products:
            for user in users:
                if user.location and user.location._id == facility.location_id \
                        and user_has_role(user, IN_CHARGE_ROLE) and user.get_verified_number():
                    if not test:
                        send_sms_to_verified_number(
                            user.get_verified_number(),
                            THIRD_STOCK_ON_HAND_REMINDER % {
                                'name': user.name,
                                'facility': facility.name
                            })
                    else:
                        send_test_message(
                            user.get_verified_number(),
                            THIRD_STOCK_ON_HAND_REMINDER % {
                                'name': user.name,
                                'facility': facility.name
                            })
        elif missing_products:
            for user in users:
                if user.location and user.location._id == facility.location_id and \
                        user_has_role(user, IN_CHARGE_ROLE) and user.get_verified_number():
                    if not test:
                        send_sms_to_verified_number(
                            user.get_verified_number(),
                            INCOMPLETE_SOH_TO_SUPER % {
                                'name':
                                user.name,
                                'facility':
                                facility.name,
                                'products':
                                ", ".join([
                                    SQLProduct.objects.get(
                                        product_id=product_id).name
                                    for product_id in missing_products
                                ])
                            })
                    else:
                        send_test_message(
                            user.get_verified_number(),
                            INCOMPLETE_SOH_TO_SUPER % {
                                'name':
                                user.name,
                                'facility':
                                facility.name,
                                'products':
                                ", ".join([
                                    SQLProduct.objects.get(
                                        product_id=product_id).name
                                    for product_id in missing_products
                                ])
                            })