I won't write a python solution as i am not familiar with any python modeling language but i can describe the approach i took in the past to solve problems like this.

I would solve this problem using a technique of finite horizon optimal control where we have an $$x_t$$ a state vector for each time point, a control signal $$u_t$$, a prediction $$p_t$$.

The core of idea is that you define a transition function which defines $$x_t = f_t(x_{t-1},x_{t-2}, ... ,x_{0},u_{t-1},u_{t-2}, ... ,u_{0}, p_t, p_{t+1}, ... , p_{t+h})$$ depending on past state, past controls and future predictions of demand and those as constraint of your model the time interval your model runs over. Futhermore you define constraints involving the predictions $$p_t$$, $$x_t$$, $$u_t$$ that ensure you always have enough stock. You define your objective in terms of $$x_t$$ and $$u_t$$.

The idea is that on day $$\tau$$ you run a the model with updated predictions, the 5 past $$u_{\tau-1}, u_{\tau-2}, u_{\tau-3}, u_{\tau-4}, u_{\tau-5}$$ as you ordered and they will arrive, $$x_{\tau-1}, x_{\tau-2}, x_{\tau-3}, x_{\tau-4}, x_{\tau-5}$$ as the historic stock levels and $$p_t, ..., p_{t+h}$$ as predict the demand on these days, you leave $$x_{\tau}, ... x_{\tau + h}$$ as free variables, constraint by transition function and leave $$u_\tau, ... , u_{\tau + h}$$ as free variables where $$h$$ is the length of time horizon. The choice of $$h$$ is a trade off between model performance and amount of computation. After running the model you get an $$u_\tau$$ and you order that many. The next day you rerun the model with updated predictions and one day further.

In your case the transition constraint would look like $$x_{t} = x_{t-1} + u_{t-5} -p_{t-1}$$. The stock level of the next day is the stock level of the previous day plus what you order 5 days ago minus that you expect to sell. You in addition can pose a contraint how much buffer you want atleast for each $$x_t$$.

The objective would be a sum of a piece wise function applied to how much was order plus storage costs. There is a lot of research how you can express piecewise function as mixed integer problems but some of them are about continous functions which yours is not.

It think you could express this problem completly in the language of Mixed Integer Linear Programming. I hope this gave you an idea how to approach it.

I won't write a python solution as i am not familiar with any python modeling language but i can describe the approach i took in the past to solve problems like this.

I would solve this problem using a technique of finite horizon optimal control where we have an $$x_t$$ a state vector for each time point, a control signal $$u_t$$, a prediction $$p_t$$.

The core of idea is that you define a transition function which defines $$x_t = f_t(x_{t-1},x_{t-2}, ... ,x_{0},u_{t-1},u_{t-2}, ... ,u_{0}, p_t, p_{t+1}, ... , p_{t+h})$$ depending on past state, past controls and future predictions of demand and those as constraint of your model the time interval your model runs over. Futhermore you define constraints involving the predictions $$p_t$$, $$x_t$$, $$u_t$$ that ensure you always have enough stock. You define your objective in terms of $$x_t$$ and $$u_t$$.

The idea is that on day $$\tau$$ you run a the model with updated predictions, the 5 past $$u_{\tau-1}, u_{\tau-2}, u_{\tau-3}, u_{\tau-4}, u_{\tau-5}$$ as you ordered and they will arrive, $$x_{\tau-1}, x_{\tau-2}, x_{\tau-3}, x_{\tau-4}, x_{\tau-5}$$ as the historic stock levels and $$p_t, ..., p_{t+h}$$ as predict the demand on these days, you leave $$x_{\tau}, ... x_{\tau + h}$$ as free variables, constraint by transition function and leave $$u_\tau, ... , u_{\tau + h}$$ as free variables where $$h$$ is the length of time horizon. After running the model you get an $$u_\tau$$ and you order that many. The next day you rerun the model with updated predictions and one day further.

In your case the transition constraint would look like $$x_{t} = x_{t-1} + u_{t-5} -p_{t-1}$$. The stock level of the next day is the stock level of the previous day plus what you order 5 days ago minus that you expect to sell. You in addition can pose a contraint how much buffer you want atleast for each $$x_t$$.

The objective would be a sum of a piece wise function applied to how much was order plus storage costs. There is a lot of research how you can express piecewise function as mixed integer problems but some of them are about continous functions which yours is not.

It think you could express this problem completly in the language of Mixed Integer Linear Programming. I hope this gave you an idea how to approach it.