Skip to content

Commit

Permalink
Inventory amount days
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterboerner committed Mar 2, 2025
1 parent 140ccab commit 0bc1d9b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
30 changes: 26 additions & 4 deletions lib/scheduler/scheduler_agent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,10 @@ defmodule TheronsErp.Scheduler.SchedulerAgent do
end

if !peer && lt?(inv_amt, quantity) do
{:reply, :ok, add_movements(state, movement) |> sub_inv(inv_amt, product_id)}
{:reply, {:ok, Date.add(MyDate.today(), product.replenishment.lead_time_days)},
add_movements(state, movement) |> sub_inv(inv_amt, product_id)}
else
{:reply, :ok,
{:reply, {:ok, Date.add(MyDate.today(), product.replenishment.lead_time_days)},
add_movements(state, movement) |> sub_inv(Money.new(quantity, :XIT), product_id)}
end
end
Expand Down Expand Up @@ -374,14 +375,17 @@ defmodule TheronsErp.Scheduler.SchedulerAgent do
if peer &&
lt?(get_total_inventory_amount(state, sales_line.product_id), sales_line.quantity) do
{{_peer_location_id, _product_id}, pid} = peer
propagate_pull_route(pid, sales_line.quantity, sales_line.product_id, state.location_id)

{:ok, date} =
propagate_pull_route(pid, sales_line.quantity, sales_line.product_id, state.location_id)

[
%{
quantity: sales_line.quantity,
product_id: sales_line.product_id,
from_inventory_id: state.location_id,
to_inventory_id: location_id
to_inventory_id: location_id,
date: date
}
]
else
Expand Down Expand Up @@ -554,4 +558,22 @@ defmodule TheronsErp.Scheduler.SchedulerAgent do
money
end
end

def get_inv_amount_day(state, product_id, amount_wanted) do
{_, date} =
(state.inventory[product_id] || [])
|> Enum.sort_by(fn {_amount, date} -> date end, Date)
|> Enum.reduce({Money.new(0, :XIT), nil}, fn {amount, new_date}, {acc, date} ->
if Money.compare(acc, amount_wanted) in [:gt, :eq] do
{acc, date}
else
{Money.add!(acc, amount), new_date}
end
end)

date
end

def accelerate_purchase_order(movements, purchase_orders, quantity, product_id) do
end
end
42 changes: 40 additions & 2 deletions test/therons_erp/scheduling_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule TheronsErp.SchedulingTest do
import TheronsErp.Generator
require Ash.Query
alias TheronsErp.Scheduler
alias TheronsErp.Scheduler.SchedulerAgent

test "noop does nothing" do
end
Expand Down Expand Up @@ -363,6 +364,35 @@ defmodule TheronsErp.SchedulingTest do

test "inventory timing"

test "mto fallback"

test "mtso fallback"

test "accelerate purchase order" do
product =
generate(product())

vendor = generate(vendor())

replenishment =
generate(
replenishment(
product_id: product.id,
vendor_id: vendor.id,
trigger_quantity: 0,
quantity_multiple: 5
)
)

product = Ash.load(product, [:replenishment])

loc_a = generate(location())
loc_b = generate(location())

# SchedulerAgent.accelerate_purchase_order(movements, purchase_orders, 1, product_id)
# TODO not done
end

test "Move up purchase order" do
vendor = generate(vendor())
product = generate(product())
Expand Down Expand Up @@ -450,9 +480,17 @@ defmodule TheronsErp.SchedulingTest do
)
end

test "inventory adjustment functions" do
alias TheronsErp.Scheduler.SchedulerAgent
test "inventory amount day" do
state = SchedulerAgent.increase_inv(nil, %{id: 1}, 1, %{inventory: %{}}, MyDate.today())
state = SchedulerAgent.increase_inv(nil, %{id: 1}, 3, state, Date.add(MyDate.today(), 14))

assert SchedulerAgent.get_inv_amount_day(state, 1, Money.new(1, :XIT)) == MyDate.today()

assert SchedulerAgent.get_inv_amount_day(state, 1, Money.new(2, :XIT)) ==
Date.add(MyDate.today(), 14)
end

test "inventory adjustment functions" do
state = SchedulerAgent.increase_inv(nil, %{id: 1}, 1, %{inventory: %{}}, MyDate.today())
state = SchedulerAgent.increase_inv(nil, %{id: 1}, 3, state, Date.add(MyDate.today(), 14))

Expand Down

0 comments on commit 0bc1d9b

Please sign in to comment.