use stockledger; use lotround; fn avocet_max2(a: f64, b: f64) -> f64 { if a > b { return a; } b } fn avocet_finite(values: vector) -> bool { let mut i = 0; while i < len(values) { let value = at(values, i); if !(value - value == 0.0) { return false; } i += 1; } true } fn avocet_counts(values: vector) -> bool { let mut i = 0; while i < len(values) { let value = at(values, i); if !(value >= 0.0 && value <= 9007199254740991.0 && value % 1.0 == 0.0) { return false; } i += 1; } true } fn avocet_compute(onhand: f64, holds: vector, target: f64, lot: f64) -> vector { let stock = stockledger_balance(onhand, holds); if len(stock) == 0 { return []; } let missing = avocet_max2(0.0, target - at(stock, 0) + at(stock, 1)); let order = lotround_order(missing, lot); if len(order) == 0 { return []; } [at(stock, 0), at(stock, 1), at(order, 1), at(stock, 0) - at(stock, 1) + at(order, 1)] } // Plan replenishment after existing reservations and supplier lot sizes. // Returns [available, existing shortage, purchase units, projected free units]. Target is the desired free stock after all holds. pub fn avocet_plan(onhand: f64, holds: vector, target: f64, lot: f64) -> vector { if !(onhand >= 0.0 && onhand <= 9007199254740991.0 && onhand % 1.0 == 0.0) { return []; } if !avocet_counts(holds) { return []; } if !(target >= 0.0 && target <= 9007199254740991.0 && target % 1.0 == 0.0) { return []; } if !(lot >= 1.0 && lot <= 9007199254740991.0 && lot % 1.0 == 0.0) { return []; } let result = avocet_compute(onhand, holds, target, lot); if !avocet_finite(result) { return []; } result }