backbone-inventory 0.17.2

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

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

use crate::domain::entity::StockEntry;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::DocStatus;
use crate::domain::entity::GlPostingState;
use crate::domain::entity::StockEntryType;

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

/// Request DTO for creating a new StockEntry
///
/// 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 CreateStockEntryDto {
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(alias = "entry_number")]
    pub entry_number: String,
    #[serde(alias = "stock_entry_type")]
    pub stock_entry_type: StockEntryType,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "from_warehouse_id")]
    pub from_warehouse_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "to_warehouse_id")]
    pub to_warehouse_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
    #[serde(alias = "posting_date")]
    pub posting_date: NaiveDate,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "transfer_id")]
    pub transfer_id: Option<Uuid>,
    #[serde(alias = "posting_state")]
    pub posting_state: GlPostingState,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub notes: Option<String>,
}

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

/// Request DTO for full update of a StockEntry
///
/// 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 UpdateStockEntryDto {
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(alias = "entry_number")]
    pub entry_number: String,
    #[serde(alias = "stock_entry_type")]
    pub stock_entry_type: StockEntryType,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "from_warehouse_id")]
    pub from_warehouse_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "to_warehouse_id")]
    pub to_warehouse_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
    #[serde(alias = "posting_date")]
    pub posting_date: NaiveDate,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "transfer_id")]
    pub transfer_id: Option<Uuid>,
    pub status: DocStatus,
    #[serde(alias = "posting_state")]
    pub posting_state: GlPostingState,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub notes: Option<String>,
}

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

/// Request DTO for partial update of a StockEntry
///
/// 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 PatchStockEntryDto {
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "entry_number")]
    pub entry_number: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "stock_entry_type")]
    pub stock_entry_type: Option<StockEntryType>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "from_warehouse_id")]
    pub from_warehouse_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "to_warehouse_id")]
    pub to_warehouse_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "posting_date")]
    pub posting_date: Option<NaiveDate>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "transfer_id")]
    pub transfer_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<DocStatus>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "posting_state")]
    pub posting_state: Option<GlPostingState>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub notes: Option<String>,
}

impl PatchStockEntryDto {
    /// Check if any field is set
    pub fn has_changes(&self) -> bool {
        self.entry_number.is_some() || self.stock_entry_type.is_some() || self.from_warehouse_id.is_some() || self.to_warehouse_id.is_some() || self.posting_date.is_some() || self.transfer_id.is_some() || self.status.is_some() || self.posting_state.is_some() || self.notes.is_some()
    }
}

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

/// Response DTO for StockEntry 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 StockEntryResponseDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub entry_number: String,
    pub stock_entry_type: StockEntryType,
    pub from_warehouse_id: Option<Uuid>,
    pub to_warehouse_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
    pub posting_date: NaiveDate,
    pub transfer_id: Option<Uuid>,
    pub status: DocStatus,
    pub posting_state: GlPostingState,
    pub notes: Option<String>,
    pub metadata: AuditMetadata,
}

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

/// Paginated list response for StockEntry 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 StockEntryListResponseDto {
    /// List of items
    pub items: Vec<StockEntryResponseDto>,
    /// 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 StockEntryListResponseDto {
    /// Create a new list response from items and pagination info
    pub fn new(items: Vec<StockEntryResponseDto>, 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 StockEntrySummaryDto {
    pub id: Uuid,
    pub entry_number: String,
    pub stock_entry_type: StockEntryType,
    pub from_warehouse_id: Option<Uuid>,
    pub created_at: Option<DateTime<Utc>>,
}

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

impl From<StockEntry> for StockEntryResponseDto {
    fn from(entity: StockEntry) -> Self {
        Self {
            id: entity.id,
            entry_number: entity.entry_number,
            stock_entry_type: entity.stock_entry_type,
            from_warehouse_id: entity.from_warehouse_id,
            to_warehouse_id: entity.to_warehouse_id,
            posting_date: entity.posting_date,
            transfer_id: entity.transfer_id,
            status: entity.status,
            posting_state: entity.posting_state,
            notes: entity.notes,
            metadata: entity.metadata,
        }
    }
}

impl From<StockEntry> for StockEntrySummaryDto {
    fn from(entity: StockEntry) -> Self {
        let created_at = backbone_core::PersistentEntity::created_at(&entity);
        Self {
            id: entity.id,
            entry_number: entity.entry_number,
            stock_entry_type: entity.stock_entry_type,
            from_warehouse_id: entity.from_warehouse_id,
            created_at,
        }
    }
}

impl From<CreateStockEntryDto> for StockEntry {
    fn from(dto: CreateStockEntryDto) -> Self {
        Self {
            id: Uuid::new_v4(),
            entry_number: dto.entry_number,
            stock_entry_type: dto.stock_entry_type,
            from_warehouse_id: dto.from_warehouse_id,
            to_warehouse_id: dto.to_warehouse_id,
            posting_date: dto.posting_date,
            transfer_id: dto.transfer_id,
            status: DocStatus::Draft,
            posting_state: dto.posting_state,
            notes: dto.notes,
            metadata: AuditMetadata::default(),
        }
    }
}

impl From<&StockEntry> for StockEntryResponseDto {
    fn from(entity: &StockEntry) -> Self {
        Self {
            id: entity.id.clone(),
            entry_number: entity.entry_number.clone(),
            stock_entry_type: entity.stock_entry_type.clone(),
            from_warehouse_id: entity.from_warehouse_id.clone(),
            to_warehouse_id: entity.to_warehouse_id.clone(),
            posting_date: entity.posting_date.clone(),
            transfer_id: entity.transfer_id.clone(),
            status: entity.status.clone(),
            posting_state: entity.posting_state.clone(),
            notes: entity.notes.clone(),
            metadata: entity.metadata.clone(),
        }
    }
}

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

impl backbone_core::ApplyUpdateDto<UpdateStockEntryDto> for StockEntry {
    fn apply_update(mut self, dto: UpdateStockEntryDto) -> backbone_core::ServiceResult<Self> {
        self.entry_number = dto.entry_number;
        self.stock_entry_type = dto.stock_entry_type;
        self.from_warehouse_id = dto.from_warehouse_id;
        self.to_warehouse_id = dto.to_warehouse_id;
        self.posting_date = dto.posting_date;
        self.transfer_id = dto.transfer_id;
        self.status = dto.status;
        self.posting_state = dto.posting_state;
        self.notes = dto.notes;
        Ok(self)
    }
}

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

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