backbone-inventory 0.17.2

Inventory: stock ledger of record (SLE) + moving-average valuation; emits COGS/asset AccountingPost (Delivery/Receipt -> GL)
Documentation
//! ReorderingRule DTOs (Data Transfer Objects)
//!
//! Generated by metaphor-schema. Do not edit manually.
//!
//! DTOs provide a clean separation between domain entities and API
//! representations, with OpenAPI documentation support.

use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc, NaiveDate};
use rust_decimal::Decimal;

#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;

use crate::domain::entity::ReorderingRule;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::OrderpointTrigger;

// =============================================================================
// Create DTO
// =============================================================================

/// Request DTO for creating a new ReorderingRule
///
/// Used in POST requests to create new entities.
/// Excludes auto-generated fields (id, created_at, updated_at, deleted_at).
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct CreateReorderingRuleDto {
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub name: String,
    pub trigger: OrderpointTrigger,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    pub active: bool,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "snoozed_until")]
    pub snoozed_until: Option<NaiveDate>,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "item_id")]
    pub item_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "location_id")]
    pub location_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "warehouse_id")]
    pub warehouse_id: Uuid,
    #[serde(alias = "item_min_qty")]
    pub item_min_qty: Decimal,
    #[serde(alias = "item_max_qty")]
    pub item_max_qty: Decimal,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "route_id")]
    pub route_id: Option<Uuid>,
    #[serde(alias = "qty_on_hand")]
    pub qty_on_hand: Decimal,
    #[serde(alias = "qty_forecast")]
    pub qty_forecast: Decimal,
    #[serde(alias = "qty_to_order")]
    pub qty_to_order: Decimal,
    #[serde(alias = "qty_to_order_manual")]
    pub qty_to_order_manual: Decimal,
    #[serde(alias = "lead_days")]
    pub lead_days: Decimal,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "deadline_date")]
    pub deadline_date: Option<NaiveDate>,
}

// =============================================================================
// Update DTO
// =============================================================================

/// Request DTO for full update of a ReorderingRule
///
/// Used in PUT requests for full entity replacement.
/// All fields are required (except auto-generated ones).
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct UpdateReorderingRuleDto {
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub name: String,
    pub trigger: OrderpointTrigger,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    pub active: bool,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "snoozed_until")]
    pub snoozed_until: Option<NaiveDate>,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "item_id")]
    pub item_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "location_id")]
    pub location_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "warehouse_id")]
    pub warehouse_id: Uuid,
    #[serde(alias = "item_min_qty")]
    pub item_min_qty: Decimal,
    #[serde(alias = "item_max_qty")]
    pub item_max_qty: Decimal,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "route_id")]
    pub route_id: Option<Uuid>,
    #[serde(alias = "qty_on_hand")]
    pub qty_on_hand: Decimal,
    #[serde(alias = "qty_forecast")]
    pub qty_forecast: Decimal,
    #[serde(alias = "qty_to_order")]
    pub qty_to_order: Decimal,
    #[serde(alias = "qty_to_order_manual")]
    pub qty_to_order_manual: Decimal,
    #[serde(alias = "lead_days")]
    pub lead_days: Decimal,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "deadline_date")]
    pub deadline_date: Option<NaiveDate>,
}

// =============================================================================
// Patch DTO
// =============================================================================

/// Request DTO for partial update of a ReorderingRule
///
/// Used in PATCH requests for partial updates.
/// All fields are optional - only provided fields will be updated.
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PatchReorderingRuleDto {
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub trigger: Option<OrderpointTrigger>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub active: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "snoozed_until")]
    pub snoozed_until: Option<NaiveDate>,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "item_id")]
    pub item_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "location_id")]
    pub location_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "warehouse_id")]
    pub warehouse_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "item_min_qty")]
    pub item_min_qty: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "item_max_qty")]
    pub item_max_qty: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "route_id")]
    pub route_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "qty_on_hand")]
    pub qty_on_hand: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "qty_forecast")]
    pub qty_forecast: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "qty_to_order")]
    pub qty_to_order: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "qty_to_order_manual")]
    pub qty_to_order_manual: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "lead_days")]
    pub lead_days: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "deadline_date")]
    pub deadline_date: Option<NaiveDate>,
}

impl PatchReorderingRuleDto {
    /// Check if any field is set
    pub fn has_changes(&self) -> bool {
        self.name.is_some() || self.trigger.is_some() || self.active.is_some() || self.snoozed_until.is_some() || self.item_id.is_some() || self.location_id.is_some() || self.warehouse_id.is_some() || self.item_min_qty.is_some() || self.item_max_qty.is_some() || self.route_id.is_some() || self.qty_on_hand.is_some() || self.qty_forecast.is_some() || self.qty_to_order.is_some() || self.qty_to_order_manual.is_some() || self.lead_days.is_some() || self.deadline_date.is_some()
    }
}

// =============================================================================
// Response DTO
// =============================================================================

/// Response DTO for ReorderingRule entity
///
/// Used in API responses for single entity.
/// Includes all fields including metadata.
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct ReorderingRuleResponseDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub name: String,
    pub trigger: OrderpointTrigger,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    pub active: bool,
    pub snoozed_until: Option<NaiveDate>,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub item_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub location_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub warehouse_id: Uuid,
    pub item_min_qty: Decimal,
    pub item_max_qty: Decimal,
    pub route_id: Option<Uuid>,
    pub qty_on_hand: Decimal,
    pub qty_forecast: Decimal,
    pub qty_to_order: Decimal,
    pub qty_to_order_manual: Decimal,
    pub lead_days: Decimal,
    pub deadline_date: Option<NaiveDate>,
    pub metadata: AuditMetadata,
}

// =============================================================================
// List Response DTO
// =============================================================================

/// Paginated list response for ReorderingRule entities
///
/// Used in API responses for list endpoints.
/// Includes pagination metadata.
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct ReorderingRuleListResponseDto {
    /// List of items
    pub items: Vec<ReorderingRuleResponseDto>,
    /// Total number of items (across all pages)
    pub total: u64,
    /// Current page number (1-indexed)
    pub page: u32,
    /// Number of items per page
    pub per_page: u32,
    /// Total number of pages
    pub total_pages: u32,
    /// Whether there is a next page
    pub has_next: bool,
    /// Whether there is a previous page
    pub has_prev: bool,
}

impl ReorderingRuleListResponseDto {
    /// Create a new list response from items and pagination info
    pub fn new(items: Vec<ReorderingRuleResponseDto>, total: u64, page: u32, per_page: u32) -> Self {
        let total_pages = if per_page > 0 {
            ((total as f64) / (per_page as f64)).ceil() as u32
        } else {
            0
        };
        Self {
            items,
            total,
            page,
            per_page,
            total_pages,
            has_next: page < total_pages,
            has_prev: page > 1,
        }
    }
}

/// Summary DTO for list views (compact version)
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct ReorderingRuleSummaryDto {
    pub id: Uuid,
    pub name: String,
    pub trigger: OrderpointTrigger,
    pub active: bool,
    pub created_at: Option<DateTime<Utc>>,
}

// =============================================================================
// Conversions
// =============================================================================

impl From<ReorderingRule> for ReorderingRuleResponseDto {
    fn from(entity: ReorderingRule) -> Self {
        Self {
            id: entity.id,
            name: entity.name,
            trigger: entity.trigger,
            active: entity.active,
            snoozed_until: entity.snoozed_until,
            item_id: entity.item_id,
            location_id: entity.location_id,
            warehouse_id: entity.warehouse_id,
            item_min_qty: entity.item_min_qty,
            item_max_qty: entity.item_max_qty,
            route_id: entity.route_id,
            qty_on_hand: entity.qty_on_hand,
            qty_forecast: entity.qty_forecast,
            qty_to_order: entity.qty_to_order,
            qty_to_order_manual: entity.qty_to_order_manual,
            lead_days: entity.lead_days,
            deadline_date: entity.deadline_date,
            metadata: entity.metadata,
        }
    }
}

impl From<ReorderingRule> for ReorderingRuleSummaryDto {
    fn from(entity: ReorderingRule) -> Self {
        let created_at = backbone_core::PersistentEntity::created_at(&entity);
        Self {
            id: entity.id,
            name: entity.name,
            trigger: entity.trigger,
            active: entity.active,
            created_at,
        }
    }
}

impl From<CreateReorderingRuleDto> for ReorderingRule {
    fn from(dto: CreateReorderingRuleDto) -> Self {
        Self {
            id: Uuid::new_v4(),
            name: dto.name,
            trigger: dto.trigger,
            active: dto.active,
            snoozed_until: dto.snoozed_until,
            item_id: dto.item_id,
            location_id: dto.location_id,
            warehouse_id: dto.warehouse_id,
            item_min_qty: dto.item_min_qty,
            item_max_qty: dto.item_max_qty,
            route_id: dto.route_id,
            qty_on_hand: dto.qty_on_hand,
            qty_forecast: dto.qty_forecast,
            qty_to_order: dto.qty_to_order,
            qty_to_order_manual: dto.qty_to_order_manual,
            lead_days: dto.lead_days,
            deadline_date: dto.deadline_date,
            metadata: AuditMetadata::default(),
        }
    }
}

impl From<&ReorderingRule> for ReorderingRuleResponseDto {
    fn from(entity: &ReorderingRule) -> Self {
        Self {
            id: entity.id.clone(),
            name: entity.name.clone(),
            trigger: entity.trigger.clone(),
            active: entity.active.clone(),
            snoozed_until: entity.snoozed_until.clone(),
            item_id: entity.item_id.clone(),
            location_id: entity.location_id.clone(),
            warehouse_id: entity.warehouse_id.clone(),
            item_min_qty: entity.item_min_qty.clone(),
            item_max_qty: entity.item_max_qty.clone(),
            route_id: entity.route_id.clone(),
            qty_on_hand: entity.qty_on_hand.clone(),
            qty_forecast: entity.qty_forecast.clone(),
            qty_to_order: entity.qty_to_order.clone(),
            qty_to_order_manual: entity.qty_to_order_manual.clone(),
            lead_days: entity.lead_days.clone(),
            deadline_date: entity.deadline_date.clone(),
            metadata: entity.metadata.clone(),
        }
    }
}

impl backbone_core::FromCreateDto<CreateReorderingRuleDto> for ReorderingRule {
    fn from_create_dto(dto: CreateReorderingRuleDto) -> backbone_core::ServiceResult<Self> {
        Ok(ReorderingRule::from(dto))
    }
}

impl backbone_core::ApplyUpdateDto<UpdateReorderingRuleDto> for ReorderingRule {
    fn apply_update(mut self, dto: UpdateReorderingRuleDto) -> backbone_core::ServiceResult<Self> {
        self.name = dto.name;
        self.trigger = dto.trigger;
        self.active = dto.active;
        self.snoozed_until = dto.snoozed_until;
        self.item_id = dto.item_id;
        self.location_id = dto.location_id;
        self.warehouse_id = dto.warehouse_id;
        self.item_min_qty = dto.item_min_qty;
        self.item_max_qty = dto.item_max_qty;
        self.route_id = dto.route_id;
        self.qty_on_hand = dto.qty_on_hand;
        self.qty_forecast = dto.qty_forecast;
        self.qty_to_order = dto.qty_to_order;
        self.qty_to_order_manual = dto.qty_to_order_manual;
        self.lead_days = dto.lead_days;
        self.deadline_date = dto.deadline_date;
        Ok(self)
    }
}

// =============================================================================
// Custom DTOs
// =============================================================================

// Add custom DTOs specific to ReorderingRule here.
// This section will be preserved during regeneration.
// >>> END CUSTOM DTOs