backbone-inventory 0.17.2

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

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

/// Request DTO for creating a new Quant
///
/// 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 CreateQuantDto {
    #[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,
    #[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>,
    pub quantity: Decimal,
    #[serde(alias = "reserved_quantity")]
    pub reserved_quantity: Decimal,
    #[serde(alias = "available_quantity")]
    pub available_quantity: Decimal,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "in_date")]
    pub in_date: Option<DateTime<Utc>>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "inventory_quantity")]
    pub inventory_quantity: Option<Decimal>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "inventory_diff_quantity")]
    pub inventory_diff_quantity: Option<Decimal>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    #[serde(alias = "inventory_quantity_set")]
    pub inventory_quantity_set: bool,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "inventory_date")]
    pub inventory_date: Option<NaiveDate>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    #[serde(alias = "sn_duplicated")]
    pub sn_duplicated: bool,
}

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

/// Request DTO for full update of a Quant
///
/// 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 UpdateQuantDto {
    #[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,
    #[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>,
    pub quantity: Decimal,
    #[serde(alias = "reserved_quantity")]
    pub reserved_quantity: Decimal,
    #[serde(alias = "available_quantity")]
    pub available_quantity: Decimal,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "in_date")]
    pub in_date: Option<DateTime<Utc>>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "inventory_quantity")]
    pub inventory_quantity: Option<Decimal>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "inventory_diff_quantity")]
    pub inventory_diff_quantity: Option<Decimal>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    #[serde(alias = "inventory_quantity_set")]
    pub inventory_quantity_set: bool,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "inventory_date")]
    pub inventory_date: Option<NaiveDate>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    #[serde(alias = "sn_duplicated")]
    pub sn_duplicated: bool,
}

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

/// Request DTO for partial update of a Quant
///
/// 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 PatchQuantDto {
    #[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>,
    #[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")]
    pub quantity: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "reserved_quantity")]
    pub reserved_quantity: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "available_quantity")]
    pub available_quantity: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "in_date")]
    pub in_date: Option<DateTime<Utc>>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "inventory_quantity")]
    pub inventory_quantity: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "inventory_diff_quantity")]
    pub inventory_diff_quantity: Option<Decimal>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "inventory_quantity_set")]
    pub inventory_quantity_set: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "inventory_date")]
    pub inventory_date: Option<NaiveDate>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "sn_duplicated")]
    pub sn_duplicated: Option<bool>,
}

impl PatchQuantDto {
    /// Check if any field is set
    pub fn has_changes(&self) -> bool {
        self.item_id.is_some() || self.location_id.is_some() || self.lot_id.is_some() || self.package_id.is_some() || self.owner_id.is_some() || self.quantity.is_some() || self.reserved_quantity.is_some() || self.available_quantity.is_some() || self.in_date.is_some() || self.inventory_quantity.is_some() || self.inventory_diff_quantity.is_some() || self.inventory_quantity_set.is_some() || self.inventory_date.is_some() || self.sn_duplicated.is_some()
    }
}

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

/// Response DTO for Quant 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 QuantResponseDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub id: Uuid,
    #[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,
    pub lot_id: Option<Uuid>,
    pub package_id: Option<Uuid>,
    pub owner_id: Option<Uuid>,
    pub quantity: Decimal,
    pub reserved_quantity: Decimal,
    pub available_quantity: Decimal,
    pub in_date: Option<DateTime<Utc>>,
    pub inventory_quantity: Option<Decimal>,
    pub inventory_diff_quantity: Option<Decimal>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    pub inventory_quantity_set: bool,
    pub inventory_date: Option<NaiveDate>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    pub sn_duplicated: bool,
    pub metadata: AuditMetadata,
}

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

/// Paginated list response for Quant 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 QuantListResponseDto {
    /// List of items
    pub items: Vec<QuantResponseDto>,
    /// 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 QuantListResponseDto {
    /// Create a new list response from items and pagination info
    pub fn new(items: Vec<QuantResponseDto>, 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 QuantSummaryDto {
    pub id: Uuid,
    pub item_id: Uuid,
    pub location_id: Uuid,
    pub lot_id: Option<Uuid>,
    pub created_at: Option<DateTime<Utc>>,
}

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

impl From<Quant> for QuantResponseDto {
    fn from(entity: Quant) -> Self {
        Self {
            id: entity.id,
            item_id: entity.item_id,
            location_id: entity.location_id,
            lot_id: entity.lot_id,
            package_id: entity.package_id,
            owner_id: entity.owner_id,
            quantity: entity.quantity,
            reserved_quantity: entity.reserved_quantity,
            available_quantity: entity.available_quantity,
            in_date: entity.in_date,
            inventory_quantity: entity.inventory_quantity,
            inventory_diff_quantity: entity.inventory_diff_quantity,
            inventory_quantity_set: entity.inventory_quantity_set,
            inventory_date: entity.inventory_date,
            sn_duplicated: entity.sn_duplicated,
            metadata: entity.metadata,
        }
    }
}

impl From<Quant> for QuantSummaryDto {
    fn from(entity: Quant) -> Self {
        let created_at = backbone_core::PersistentEntity::created_at(&entity);
        Self {
            id: entity.id,
            item_id: entity.item_id,
            location_id: entity.location_id,
            lot_id: entity.lot_id,
            created_at,
        }
    }
}

impl From<CreateQuantDto> for Quant {
    fn from(dto: CreateQuantDto) -> Self {
        Self {
            id: Uuid::new_v4(),
            item_id: dto.item_id,
            location_id: dto.location_id,
            lot_id: dto.lot_id,
            package_id: dto.package_id,
            owner_id: dto.owner_id,
            quantity: dto.quantity,
            reserved_quantity: dto.reserved_quantity,
            available_quantity: dto.available_quantity,
            in_date: dto.in_date,
            inventory_quantity: dto.inventory_quantity,
            inventory_diff_quantity: dto.inventory_diff_quantity,
            inventory_quantity_set: dto.inventory_quantity_set,
            inventory_date: dto.inventory_date,
            sn_duplicated: dto.sn_duplicated,
            metadata: AuditMetadata::default(),
        }
    }
}

impl From<&Quant> for QuantResponseDto {
    fn from(entity: &Quant) -> Self {
        Self {
            id: entity.id.clone(),
            item_id: entity.item_id.clone(),
            location_id: entity.location_id.clone(),
            lot_id: entity.lot_id.clone(),
            package_id: entity.package_id.clone(),
            owner_id: entity.owner_id.clone(),
            quantity: entity.quantity.clone(),
            reserved_quantity: entity.reserved_quantity.clone(),
            available_quantity: entity.available_quantity.clone(),
            in_date: entity.in_date.clone(),
            inventory_quantity: entity.inventory_quantity.clone(),
            inventory_diff_quantity: entity.inventory_diff_quantity.clone(),
            inventory_quantity_set: entity.inventory_quantity_set.clone(),
            inventory_date: entity.inventory_date.clone(),
            sn_duplicated: entity.sn_duplicated.clone(),
            metadata: entity.metadata.clone(),
        }
    }
}

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

impl backbone_core::ApplyUpdateDto<UpdateQuantDto> for Quant {
    fn apply_update(mut self, dto: UpdateQuantDto) -> backbone_core::ServiceResult<Self> {
        self.item_id = dto.item_id;
        self.location_id = dto.location_id;
        self.lot_id = dto.lot_id;
        self.package_id = dto.package_id;
        self.owner_id = dto.owner_id;
        self.quantity = dto.quantity;
        self.reserved_quantity = dto.reserved_quantity;
        self.available_quantity = dto.available_quantity;
        self.in_date = dto.in_date;
        self.inventory_quantity = dto.inventory_quantity;
        self.inventory_diff_quantity = dto.inventory_diff_quantity;
        self.inventory_quantity_set = dto.inventory_quantity_set;
        self.inventory_date = dto.inventory_date;
        self.sn_duplicated = dto.sn_duplicated;
        Ok(self)
    }
}

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

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