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::StockReconciliation;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::DocStatus;
use crate::domain::entity::GlPostingState;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct CreateStockReconciliationDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "recon_number")]
pub recon_number: String,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "warehouse_id")]
pub warehouse_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
#[serde(alias = "posting_date")]
pub posting_date: NaiveDate,
#[serde(alias = "net_difference")]
pub net_difference: Decimal,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "inventory_account_id")]
pub inventory_account_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "adjustment_account_id")]
pub adjustment_account_id: Uuid,
#[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", alias = "journal_id")]
pub journal_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "accounting_post_id")]
pub accounting_post_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "posted_at")]
pub posted_at: Option<DateTime<Utc>>,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct UpdateStockReconciliationDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "recon_number")]
pub recon_number: String,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "warehouse_id")]
pub warehouse_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
#[serde(alias = "posting_date")]
pub posting_date: NaiveDate,
#[serde(alias = "net_difference")]
pub net_difference: Decimal,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "inventory_account_id")]
pub inventory_account_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "adjustment_account_id")]
pub adjustment_account_id: Uuid,
#[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", alias = "journal_id")]
pub journal_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "accounting_post_id")]
pub accounting_post_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "posted_at")]
pub posted_at: Option<DateTime<Utc>>,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PatchStockReconciliationDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "recon_number")]
pub recon_number: Option<String>,
#[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>,
#[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 = "net_difference")]
pub net_difference: Option<Decimal>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "inventory_account_id")]
pub inventory_account_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "adjustment_account_id")]
pub adjustment_account_id: Option<Uuid>,
#[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", alias = "journal_id")]
pub journal_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "accounting_post_id")]
pub accounting_post_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "posted_at")]
pub posted_at: Option<DateTime<Utc>>,
}
impl PatchStockReconciliationDto {
pub fn has_changes(&self) -> bool {
self.recon_number.is_some() || self.warehouse_id.is_some() || self.posting_date.is_some() || self.net_difference.is_some() || self.inventory_account_id.is_some() || self.adjustment_account_id.is_some() || self.transfer_id.is_some() || self.status.is_some() || self.posting_state.is_some() || self.journal_id.is_some() || self.accounting_post_id.is_some() || self.posted_at.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct StockReconciliationResponseDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub recon_number: String,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub warehouse_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
pub posting_date: NaiveDate,
pub net_difference: Decimal,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub inventory_account_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub adjustment_account_id: Uuid,
pub transfer_id: Option<Uuid>,
pub status: DocStatus,
pub posting_state: GlPostingState,
pub journal_id: Option<Uuid>,
pub accounting_post_id: Option<Uuid>,
pub posted_at: Option<DateTime<Utc>>,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct StockReconciliationListResponseDto {
pub items: Vec<StockReconciliationResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl StockReconciliationListResponseDto {
pub fn new(items: Vec<StockReconciliationResponseDto>, 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 StockReconciliationSummaryDto {
pub id: Uuid,
pub recon_number: String,
pub warehouse_id: Uuid,
pub posting_date: NaiveDate,
pub created_at: Option<DateTime<Utc>>,
}
impl From<StockReconciliation> for StockReconciliationResponseDto {
fn from(entity: StockReconciliation) -> Self {
Self {
id: entity.id,
recon_number: entity.recon_number,
warehouse_id: entity.warehouse_id,
posting_date: entity.posting_date,
net_difference: entity.net_difference,
inventory_account_id: entity.inventory_account_id,
adjustment_account_id: entity.adjustment_account_id,
transfer_id: entity.transfer_id,
status: entity.status,
posting_state: entity.posting_state,
journal_id: entity.journal_id,
accounting_post_id: entity.accounting_post_id,
posted_at: entity.posted_at,
metadata: entity.metadata,
}
}
}
impl From<StockReconciliation> for StockReconciliationSummaryDto {
fn from(entity: StockReconciliation) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
recon_number: entity.recon_number,
warehouse_id: entity.warehouse_id,
posting_date: entity.posting_date,
created_at,
}
}
}
impl From<CreateStockReconciliationDto> for StockReconciliation {
fn from(dto: CreateStockReconciliationDto) -> Self {
Self {
id: Uuid::new_v4(),
recon_number: dto.recon_number,
warehouse_id: dto.warehouse_id,
posting_date: dto.posting_date,
net_difference: dto.net_difference,
inventory_account_id: dto.inventory_account_id,
adjustment_account_id: dto.adjustment_account_id,
transfer_id: dto.transfer_id,
status: DocStatus::Draft,
posting_state: dto.posting_state,
journal_id: dto.journal_id,
accounting_post_id: dto.accounting_post_id,
posted_at: dto.posted_at,
metadata: AuditMetadata::default(),
}
}
}
impl From<&StockReconciliation> for StockReconciliationResponseDto {
fn from(entity: &StockReconciliation) -> Self {
Self {
id: entity.id.clone(),
recon_number: entity.recon_number.clone(),
warehouse_id: entity.warehouse_id.clone(),
posting_date: entity.posting_date.clone(),
net_difference: entity.net_difference.clone(),
inventory_account_id: entity.inventory_account_id.clone(),
adjustment_account_id: entity.adjustment_account_id.clone(),
transfer_id: entity.transfer_id.clone(),
status: entity.status.clone(),
posting_state: entity.posting_state.clone(),
journal_id: entity.journal_id.clone(),
accounting_post_id: entity.accounting_post_id.clone(),
posted_at: entity.posted_at.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateStockReconciliationDto> for StockReconciliation {
fn from_create_dto(dto: CreateStockReconciliationDto) -> backbone_core::ServiceResult<Self> {
Ok(StockReconciliation::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateStockReconciliationDto> for StockReconciliation {
fn apply_update(mut self, dto: UpdateStockReconciliationDto) -> backbone_core::ServiceResult<Self> {
self.recon_number = dto.recon_number;
self.warehouse_id = dto.warehouse_id;
self.posting_date = dto.posting_date;
self.net_difference = dto.net_difference;
self.inventory_account_id = dto.inventory_account_id;
self.adjustment_account_id = dto.adjustment_account_id;
self.transfer_id = dto.transfer_id;
self.status = dto.status;
self.posting_state = dto.posting_state;
self.journal_id = dto.journal_id;
self.accounting_post_id = dto.accounting_post_id;
self.posted_at = dto.posted_at;
Ok(self)
}
}