-
Notifications
You must be signed in to change notification settings - Fork 0
/
reorder_schedule.py
70 lines (57 loc) · 1.83 KB
/
reorder_schedule.py
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
import datetime
import pandas as pd
import sqlite3
con = sqlite3.connect(':memory:')
con.text_factory = str
cur = con.cursor()
order_df = pd.read_csv('orders.csv', usecols=['date', 'quantity', 'SKU'], parse_dates=['date'])
po_df = pd.read_csv('pos.csv', usecols=['SKU', 'stock_quantity', 'moq', 'lead_time'])
order_df.to_sql('orders', con=con, if_exists='replace')
po_df.to_sql('pos', con=con, if_exists='replace')
q = """
select
order_data.SKU as order_sku,
pos_data.SKU as po_sku,
julianday(max_date) - julianday(min_date) as total_days,
total_sales,
pos_data.moq,
pos_data.lead_time,
pos_data.stock_qty as on_hand_inventory
from (
select
SKU,
max(date) as max_date,
min(date) as min_date,
sum(quantity) as total_sales
from orders
group by SKU
) as order_data
left outer join (
select
SKU,
max(moq) as moq,
max(lead_time) as lead_time,
sum(stock_quantity) as stock_qty
from pos
group by SKU
)pos_data on pos_data.SKU = order_data.SKU
"""
results = pd.read_sql(q, con=con)
def days_between_sales(row):
days = row['total_days']/row['total_sales']
return int(days)
def reorder_days(row):
days = row['days_between_sales'] * row['on_hand_inventory'] + row['lead_time']
return days
def reorder_date(row):
now = datetime.datetime.now().date()
try:
end_date = now + datetime.timedelta(days=row['reorder_days'])
except ValueError:
end_date = now
return end_date
results['days_between_sales'] = results.apply(days_between_sales, axis=1)
results['reorder_days'] = results.apply(reorder_days, axis=1)
results['reorder_day'] = results.apply(reorder_date, axis=1)
results.to_csv('reorder_schedule.csv', index=False)
print results