// Freeze a bin for a blind stock count and require reconciliation for discrepancies. use std::cli; pub fn cycle_count_start() -> vector { [0, 0, 0] } pub fn cycle_count_step(snapshot: vector, event: str, value: f64) -> vector { if len(snapshot) != 3 { return []; } let phase = at(snapshot, 0); let saved = at(snapshot, 1); let previous = at(snapshot, 2); if !(phase >= 0.0 && phase < 5.0 && phase % 1.0 == 0.0 && previous >= 0.0 && previous < 5.0 && previous % 1.0 == 0.0 && saved >= 0.0 && saved <= 9007199254740991.0 && saved % 1.0 == 0.0) { return []; } if !(value >= 0.0 && value <= 1000000000.0 && value % 1.0 == 0.0) { return [phase, saved, 0]; } // idle if phase == 0.0 { if event == "freeze" { return [1, value, 1]; // store_book_qty } } // counting if phase == 1.0 { if event == "count" && value==saved { return [2, saved, 2]; // unfreeze_bin } if event == "count" && value!=saved { return [3, saved, 3]; // request_recount } } // dispute if phase == 3.0 { if event == "recount" && value==saved { return [2, saved, 2]; // unfreeze_bin } if event == "approve" { return [4, value, 4]; // adjust_book_qty } } [phase, saved, 0] } pub fn cycle_count_command(snapshot: vector) -> str { if len(snapshot) != 3 { return "invalid"; } let action = at(snapshot, 2); if action == 0.0 { return "reject"; } if action == 1.0 { return "store_book_qty"; } if action == 2.0 { return "unfreeze_bin"; } if action == 3.0 { return "request_recount"; } if action == 4.0 { return "adjust_book_qty"; } "invalid" } // Stop at the first rejected event. No host effects run during replay. pub fn cycle_count_replay(events: arglist, values: vector) -> vector { if args_len(events) != len(values) { return []; } let mut state = cycle_count_start(); let mut i = 0; while i < len(values) { state = cycle_count_step(state, args_at(events, i), at(values, i)); if at(state, 2) == 0.0 { return []; } i += 1; } state }