Skip to content

Commit

Permalink
Test for moving up POs
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterboerner committed Mar 3, 2025
1 parent 0bc1d9b commit 07bee12
Show file tree
Hide file tree
Showing 11 changed files with 758 additions and 88 deletions.
76 changes: 57 additions & 19 deletions lib/scheduler/scheduler_agent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ defmodule TheronsErp.Scheduler.SchedulerAgent do
GenServer.call(pid, {:add_to_peer, peer})
end

def accelerate(pid) do
GenServer.call(pid, :accelerate)
end

def set_product_inventory(pid, {_product, _amount} = inv) do
GenServer.call(pid, {:set_product_inventory, inv})
end
Expand All @@ -36,12 +40,12 @@ defmodule TheronsErp.Scheduler.SchedulerAgent do
GenServer.call(pid, {:add_movement, movement})
end

def propagate_push_route(pid, quantity, product_id) do
GenServer.call(pid, {:propagate_push_route, quantity, product_id})
def propagate_push_route(pid, quantity, product_id, purchase_order) do
GenServer.call(pid, {:propagate_push_route, quantity, product_id, purchase_order})
end

def propagate_pull_route(pid, quantity, product_id, from_location) do
GenServer.call(pid, {:propagate_pull_route, quantity, product_id, from_location})
def propagate_pull_route(pid, quantity, product_id, from_location, sales_line) do
GenServer.call(pid, {:propagate_pull_route, quantity, product_id, from_location, sales_line})
end

def add_purchase_order(pid, purchase_order) do
Expand Down Expand Up @@ -184,9 +188,10 @@ defmodule TheronsErp.Scheduler.SchedulerAgent do
{:reply, :ok, Map.put(state, :stale_movements, [movement | state.stale_movements])}
end

def handle_call({:propagate_push_route, quantity, product_id}, _from, state)
def handle_call({:propagate_push_route, quantity, product_id, purchase_order}, _from, state)
when not is_nil(quantity) do
# TODO modify a stale movement if possible.
# TODO pass in the purchase order so we can set the date
peer = get_to_peer_for_product(state, product_id)

if peer do
Expand All @@ -196,18 +201,24 @@ defmodule TheronsErp.Scheduler.SchedulerAgent do
quantity: quantity,
product_id: product_id,
from_inventory_id: state.inventory_id,
to_inventory_id: location_id
to_inventory_id: location_id,
date: purchase_order.delivery_date,
purchase_order_id: purchase_order.id
}

propagate_push_route(pid, quantity, product_id)
propagate_push_route(pid, quantity, product_id, purchase_order)

{:reply, :ok, add_movements(state, movement)}
else
{:reply, :ok, state}
end
end

def handle_call({:propagate_pull_route, quantity, product_id, from_location}, from, state) do
def handle_call(
{:propagate_pull_route, quantity, product_id, from_location, sales_line},
from,
state
) do
peer = get_from_peer_for_product(state, product_id)

movement =
Expand All @@ -216,7 +227,8 @@ defmodule TheronsErp.Scheduler.SchedulerAgent do
product_id: product_id,
to_inventory_id: from_location,
from_inventory_id: state.location_id,
date: nil
date: nil,
sales_order_id: sales_line.sales_order_id
}

inv_amt = get_total_inventory_amount(state, product_id)
Expand All @@ -229,7 +241,8 @@ defmodule TheronsErp.Scheduler.SchedulerAgent do
pid,
Decimal.sub(quantity, inv_amt.amount),
product_id,
state.location_id
state.location_id,
sales_line
)

{:reply, {:ok, date},
Expand Down Expand Up @@ -283,11 +296,16 @@ defmodule TheronsErp.Scheduler.SchedulerAgent do
end

if !peer && lt?(inv_amt, quantity) do
{:reply, {:ok, Date.add(MyDate.today(), product.replenishment.lead_time_days)},
add_movements(state, movement) |> sub_inv(inv_amt, product_id)}
new_date = Date.add(MyDate.today(), product.replenishment.lead_time_days)

{:reply, {:ok, new_date},
add_movements(state, %{movement | date: new_date}) |> sub_inv(inv_amt, product_id)}
else
{:reply, {:ok, Date.add(MyDate.today(), product.replenishment.lead_time_days)},
add_movements(state, movement) |> sub_inv(Money.new(quantity, :XIT), product_id)}
new_date = Date.add(MyDate.today(), product.replenishment.lead_time_days)

{:reply, {:ok, new_date},
add_movements(state, %{movement | date: new_date})
|> sub_inv(Money.new(quantity, :XIT), product_id)}
end
end
end
Expand Down Expand Up @@ -344,6 +362,11 @@ defmodule TheronsErp.Scheduler.SchedulerAgent do
{:reply, changesets, state}
end

def handle_call(:accelerate, _from, state) do
{sales_lines, purchase_orders, stale_movements} = _accelerate(state)
{:reply, {sales_lines, purchase_orders, stale_movements}, state}
end

def handle_call(:process, _from, state) do
# Move purchase orders through push
for po <- state.purchase_orders do
Expand Down Expand Up @@ -377,24 +400,35 @@ defmodule TheronsErp.Scheduler.SchedulerAgent do
{{_peer_location_id, _product_id}, pid} = peer

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

[
%{
quantity: sales_line.quantity,
product_id: sales_line.product_id,
from_inventory_id: state.location_id,
to_inventory_id: location_id,
date: date
date: date,
sales_order_id: sales_line.sales_order_id
}
]
else
date = get_inv_amount_day(state, sales_line.product_id, sales_line.quantity)

[
%{
quantity: sales_line.quantity,
product_id: sales_line.product_id,
to_inventory_id: location_id,
from_inventory_id: state.location_id
from_inventory_id: state.location_id,
date: date,
sales_order_id: sales_line.sales_order_id
}
]
end
Expand Down Expand Up @@ -504,7 +538,7 @@ defmodule TheronsErp.Scheduler.SchedulerAgent do

if peer do
{{location_id, _product_id}, pid} = peer
propagate_push_route(pid, item.quantity, item.product_id)
propagate_push_route(pid, item.quantity, item.product_id, purchase_order)

[
%{
Expand Down Expand Up @@ -574,6 +608,10 @@ defmodule TheronsErp.Scheduler.SchedulerAgent do
date
end

def accelerate_purchase_order(movements, purchase_orders, quantity, product_id) do
def _accelerate(state) do
sales_lines = state.sales_lines
purchase_orders = state.purchase_orders
stale_movements = state.stale_movements
{sales_lines, purchase_orders, stale_movements}
end
end
8 changes: 6 additions & 2 deletions lib/therons_erp/inventory/movement.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ defmodule TheronsErp.Inventory.Movement do
:actual_transfer_id,
:predicted_transfer_id,
:eager_transfer_id,
:purchase_order_id
:purchase_order_id,
:sales_order_id
]

change fn changeset, _ ->
Expand All @@ -66,7 +67,8 @@ defmodule TheronsErp.Inventory.Movement do
:actual_transfer_id,
:predicted_transfer_id,
:eager_transfer_id,
:purchase_order_id
:purchase_order_id,
:sales_order_id
]
end

Expand Down Expand Up @@ -173,6 +175,8 @@ defmodule TheronsErp.Inventory.Movement do
end

belongs_to :purchase_order, TheronsErp.Purchasing.PurchaseOrder

belongs_to :sales_order, TheronsErp.Sales.SalesOrder
end

def get_acct_id(identifier) do
Expand Down
2 changes: 2 additions & 0 deletions lib/therons_erp/inventory/product.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ defmodule TheronsErp.Inventory.Product do
relationships do
belongs_to :category, TheronsErp.Inventory.ProductCategory

# This is a many to many to support having a pull and a push route. This
# should not be used with multiple pulls or multiple pushes.
many_to_many :routes, TheronsErp.Inventory.Routes do
through TheronsErp.Inventory.ProductRoutes
source_attribute :id
Expand Down
8 changes: 6 additions & 2 deletions lib/therons_erp/sales/sales_order.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ defmodule TheronsErp.Sales.SalesOrder do
end

create :create do
accept [:customer_id, :address_id, :process_date]
accept [:customer_id, :address_id, :process_date, :purchase_date]
primary? true
argument :sales_lines, {:array, :map}

Expand All @@ -105,7 +105,7 @@ defmodule TheronsErp.Sales.SalesOrder do
end

update :update do
accept [:customer_id, :address_id, :process_date]
accept [:customer_id, :address_id, :process_date, :purchase_date]
require_atomic? false
argument :sales_lines, {:array, :map}

Expand All @@ -128,6 +128,10 @@ defmodule TheronsErp.Sales.SalesOrder do

attribute :process_date, :date

attribute :purchase_date, :date do
default &MyDate.today/0
end

timestamps()
end

Expand Down
Loading

0 comments on commit 07bee12

Please sign in to comment.