backbone-inventory 0.17.2

Inventory: stock ledger of record (SLE) + moving-average valuation; emits COGS/asset AccountingPost (Delivery/Receipt -> GL)
Documentation
//! Scrap 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};
use rust_decimal::Decimal;

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

use crate::domain::entity::Scrap;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::ScrapState;

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

/// Request DTO for creating a new Scrap
///
/// 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 CreateScrapDto {
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub name: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub origin: Option<String>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    #[serde(alias = "date_expected")]
    pub date_expected: DateTime<Utc>,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "item_id")]
    pub item_id: Uuid,
    #[serde(alias = "scrap_qty")]
    pub scrap_qty: Decimal,
    #[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 = "scrap_location_id")]
    pub scrap_location_id: Uuid,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "lot_id")]
    pub lot_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "package_id")]
    pub package_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "owner_id")]
    pub owner_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "picking_id")]
    pub picking_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "move_id")]
    pub move_id: Option<Uuid>,
    #[serde(alias = "scrap_reason_tag_ids")]
    pub scrap_reason_tag_ids: Vec<Uuid>,
}

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

/// Request DTO for full update of a Scrap
///
/// 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 UpdateScrapDto {
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub name: String,
    pub state: ScrapState,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub origin: Option<String>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    #[serde(alias = "date_expected")]
    pub date_expected: DateTime<Utc>,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "item_id")]
    pub item_id: Uuid,
    #[serde(alias = "scrap_qty")]
    pub scrap_qty: Decimal,
    #[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 = "scrap_location_id")]
    pub scrap_location_id: Uuid,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "lot_id")]
    pub lot_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "package_id")]
    pub package_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "owner_id")]
    pub owner_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "picking_id")]
    pub picking_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "move_id")]
    pub move_id: Option<Uuid>,
    #[serde(alias = "scrap_reason_tag_ids")]
    pub scrap_reason_tag_ids: Vec<Uuid>,
}

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

/// Request DTO for partial update of a Scrap
///
/// 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 PatchScrapDto {
    #[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 state: Option<ScrapState>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub origin: Option<String>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "date_expected")]
    pub date_expected: Option<DateTime<Utc>>,
    #[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>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "scrap_qty")]
    pub scrap_qty: Option<Decimal>,
    #[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 = "scrap_location_id")]
    pub scrap_location_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "lot_id")]
    pub lot_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "package_id")]
    pub package_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "owner_id")]
    pub owner_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "picking_id")]
    pub picking_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "move_id")]
    pub move_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "scrap_reason_tag_ids")]
    pub scrap_reason_tag_ids: Option<Vec<Uuid>>,
}

impl PatchScrapDto {
    /// Check if any field is set
    pub fn has_changes(&self) -> bool {
        self.name.is_some() || self.state.is_some() || self.origin.is_some() || self.date_expected.is_some() || self.item_id.is_some() || self.scrap_qty.is_some() || self.location_id.is_some() || self.scrap_location_id.is_some() || self.lot_id.is_some() || self.package_id.is_some() || self.owner_id.is_some() || self.picking_id.is_some() || self.move_id.is_some() || self.scrap_reason_tag_ids.is_some()
    }
}

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

/// Response DTO for Scrap 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 ScrapResponseDto {
    #[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 state: ScrapState,
    pub origin: Option<String>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    pub date_expected: DateTime<Utc>,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub item_id: Uuid,
    pub scrap_qty: Decimal,
    #[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 scrap_location_id: Uuid,
    pub lot_id: Option<Uuid>,
    pub package_id: Option<Uuid>,
    pub owner_id: Option<Uuid>,
    pub picking_id: Option<Uuid>,
    pub move_id: Option<Uuid>,
    pub scrap_reason_tag_ids: Vec<Uuid>,
    pub metadata: AuditMetadata,
}

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

/// Paginated list response for Scrap 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 ScrapListResponseDto {
    /// List of items
    pub items: Vec<ScrapResponseDto>,
    /// 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 ScrapListResponseDto {
    /// Create a new list response from items and pagination info
    pub fn new(items: Vec<ScrapResponseDto>, 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 ScrapSummaryDto {
    pub id: Uuid,
    pub name: String,
    pub state: ScrapState,
    pub origin: Option<String>,
    pub created_at: Option<DateTime<Utc>>,
}

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

impl From<Scrap> for ScrapResponseDto {
    fn from(entity: Scrap) -> Self {
        Self {
            id: entity.id,
            name: entity.name,
            state: entity.state,
            origin: entity.origin,
            date_expected: entity.date_expected,
            item_id: entity.item_id,
            scrap_qty: entity.scrap_qty,
            location_id: entity.location_id,
            scrap_location_id: entity.scrap_location_id,
            lot_id: entity.lot_id,
            package_id: entity.package_id,
            owner_id: entity.owner_id,
            picking_id: entity.picking_id,
            move_id: entity.move_id,
            scrap_reason_tag_ids: entity.scrap_reason_tag_ids,
            metadata: entity.metadata,
        }
    }
}

impl From<Scrap> for ScrapSummaryDto {
    fn from(entity: Scrap) -> Self {
        let created_at = backbone_core::PersistentEntity::created_at(&entity);
        Self {
            id: entity.id,
            name: entity.name,
            state: entity.state,
            origin: entity.origin,
            created_at,
        }
    }
}

impl From<CreateScrapDto> for Scrap {
    fn from(dto: CreateScrapDto) -> Self {
        Self {
            id: Uuid::new_v4(),
            name: dto.name,
            state: ScrapState::Draft,
            origin: dto.origin,
            date_expected: dto.date_expected,
            item_id: dto.item_id,
            scrap_qty: dto.scrap_qty,
            location_id: dto.location_id,
            scrap_location_id: dto.scrap_location_id,
            lot_id: dto.lot_id,
            package_id: dto.package_id,
            owner_id: dto.owner_id,
            picking_id: dto.picking_id,
            move_id: dto.move_id,
            scrap_reason_tag_ids: dto.scrap_reason_tag_ids,
            metadata: AuditMetadata::default(),
        }
    }
}

impl From<&Scrap> for ScrapResponseDto {
    fn from(entity: &Scrap) -> Self {
        Self {
            id: entity.id.clone(),
            name: entity.name.clone(),
            state: entity.state.clone(),
            origin: entity.origin.clone(),
            date_expected: entity.date_expected.clone(),
            item_id: entity.item_id.clone(),
            scrap_qty: entity.scrap_qty.clone(),
            location_id: entity.location_id.clone(),
            scrap_location_id: entity.scrap_location_id.clone(),
            lot_id: entity.lot_id.clone(),
            package_id: entity.package_id.clone(),
            owner_id: entity.owner_id.clone(),
            picking_id: entity.picking_id.clone(),
            move_id: entity.move_id.clone(),
            scrap_reason_tag_ids: entity.scrap_reason_tag_ids.clone(),
            metadata: entity.metadata.clone(),
        }
    }
}

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

impl backbone_core::ApplyUpdateDto<UpdateScrapDto> for Scrap {
    fn apply_update(mut self, dto: UpdateScrapDto) -> backbone_core::ServiceResult<Self> {
        self.name = dto.name;
        self.state = dto.state;
        self.origin = dto.origin;
        self.date_expected = dto.date_expected;
        self.item_id = dto.item_id;
        self.scrap_qty = dto.scrap_qty;
        self.location_id = dto.location_id;
        self.scrap_location_id = dto.scrap_location_id;
        self.lot_id = dto.lot_id;
        self.package_id = dto.package_id;
        self.owner_id = dto.owner_id;
        self.picking_id = dto.picking_id;
        self.move_id = dto.move_id;
        self.scrap_reason_tag_ids = dto.scrap_reason_tag_ids;
        Ok(self)
    }
}

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

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