use rust_decimal::Decimal;
use uuid::Uuid;
use super::inventory_write_service::{InventoryError, InventoryWriteService};
#[derive(Debug, Clone)]
pub struct Posture {
pub cost_method: String,
pub periodic: bool,
pub anglo_saxon: bool,
pub stock_interim_delivered_account_id: Option<Uuid>,
}
impl Default for Posture {
fn default() -> Self {
Posture {
cost_method: "average".into(),
periodic: false,
anglo_saxon: false,
stock_interim_delivered_account_id: None,
}
}
}
impl InventoryWriteService {
pub(super) async fn posting_posture(
&self,
) -> Result<Posture, InventoryError> {
let row = self.valuation_overlay.fetch_posture(&self.rpool()).await?;
Ok(match row {
None => Posture::default(),
Some(r) => Posture {
cost_method: r.cost_method,
periodic: r.valuation_policy == "periodic",
anglo_saxon: r.anglo_saxon_accounting,
stock_interim_delivered_account_id: r.stock_interim_delivered_account_id,
},
})
}
pub(super) async fn posting_posture_on(
&self,
conn: &mut sqlx::PgConnection,
) -> Result<Posture, InventoryError> {
let row = self.valuation_overlay.fetch_posture_on(conn).await?;
Ok(match row {
None => Posture::default(),
Some(r) => Posture {
cost_method: r.cost_method,
periodic: r.valuation_policy == "periodic",
anglo_saxon: r.anglo_saxon_accounting,
stock_interim_delivered_account_id: r.stock_interim_delivered_account_id,
},
})
}
pub(super) fn delivery_debit_account(
&self,
posture: &Posture,
cogs_account_id: Uuid,
) -> Result<Uuid, InventoryError> {
if !posture.anglo_saxon {
return Ok(cogs_account_id);
}
posture
.stock_interim_delivered_account_id
.ok_or(InventoryError::AngloPostureUnconfigured)
}
pub(super) async fn inventory_leg_account(
&self,
location_id: Uuid,
header_account_id: Uuid,
) -> Result<Uuid, InventoryError> {
Ok(self
.valuation_overlay
.fetch_location_valuation_override(&self.rpool(), location_id)
.await?
.unwrap_or(header_account_id))
}
pub(super) fn should_create_account_move(
value: Decimal,
qty: Decimal,
accounts_configured: bool,
) -> bool {
accounts_configured && !(value.is_zero() && qty.is_zero())
}
}