File size: 2,235 Bytes
6739f59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
from collections import OrderedDict

import pandas as pd

from src.core.config import get_settings

settings = get_settings()

CONFIG = {
    "min_net_sales_percentage": 0.005,
    "max_net_sales_percentage": 0.1,
    "min_extra_qty_percentage": 0.1,
    "percentage_threshold": 0.5,
}


def check_net_sales_percentage(data):
    """
    Check the extra net sales percentage of total sales is between the min and max thresholds.
    """
    min_threshold = CONFIG.get("min_net_sales_percentage", 0.005)
    max_threshold = CONFIG.get("max_net_sales_percentage", 0.10)
    pct_threshold = CONFIG.get("percentage_threshold", 0.5)

    data["CHECK_EXTRA_NS_PCT"] = (min_threshold <= data.EXTRA_NET_SALE_PCT) & (
        data.EXTRA_NET_SALE_PCT <= max_threshold
    )
    data.drop(columns=["EXTRA_NET_SALE_PCT"], inplace=True)
    pct = data["CHECK_EXTRA_NS_PCT"].mean()
    return pct > pct_threshold


def check_in_history(reco, history, type):
    """
    Check if the product/basket is already in the history of the store
    """
    pct_threshold = CONFIG.get("percentage_threshold", 0.5)
    # check if the product was sold last week or in the whole history
    reco["CHECK_IN_HISTORY"] = reco.QTY_CURRENT > 0 | reco[
        "DISPLAY_NAME"
    ].isin(history[type])
    pct = reco.CHECK_IN_HISTORY.mean()
    return pct > pct_threshold


def check_extra_qty(reco):
    """
    Check the extra quantity percentage of total sales is above the threshold.
    """
    min_threshold_pct = CONFIG.get("min_extra_qty_percentage", 0.1)
    pct_threshold = CONFIG.get("percentage_threshold", 0.5)
    min_threshold = reco["QTY_CURRENT"] * min_threshold_pct
    reco["CHECK_EXTRA_QTY_PCT"] = reco["QTY_CURRENT"] > min_threshold
    pct = reco["CHECK_EXTRA_QTY_PCT"].mean()
    return pct > pct_threshold


def checks(reco, history, type):
    """
    Run all checks
    """
    if type == "PROD_NAME":
        check_net_sales_percentage(reco)
    check_extra_qty(reco)
    check_in_history(reco, history, type)
    # Filter out the recommendations that don't pass the checks
    check_cols = [c for c in reco.columns if c.startswith("CHECK_")]
    reco = reco[reco[check_cols].all(axis=1)].drop(columns=check_cols)
    return reco