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;
#[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>,
}
#[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>,
}
#[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 {
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()
}
}
#[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,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct StockEntryListResponseDto {
pub items: Vec<StockEntryResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl StockEntryListResponseDto {
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,
}
}
}
#[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>>,
}
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)
}
}