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::LandedCost;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::GlPostingState;
use crate::domain::entity::LandedCostState;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct CreateLandedCostDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "lc_number")]
pub lc_number: String,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "branch_id")]
pub branch_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "target_receipt_id")]
pub target_receipt_id: Uuid,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "transfer_id")]
pub transfer_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub currency: String,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
#[serde(alias = "posting_date")]
pub posting_date: NaiveDate,
#[serde(alias = "amount_total")]
pub amount_total: Decimal,
#[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>>,
#[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 UpdateLandedCostDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "lc_number")]
pub lc_number: String,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "branch_id")]
pub branch_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "target_receipt_id")]
pub target_receipt_id: Uuid,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "transfer_id")]
pub transfer_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub currency: String,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
#[serde(alias = "posting_date")]
pub posting_date: NaiveDate,
pub state: LandedCostState,
#[serde(alias = "amount_total")]
pub amount_total: Decimal,
#[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>>,
#[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 PatchLandedCostDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "lc_number")]
pub lc_number: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", alias = "branch_id")]
pub branch_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "target_receipt_id")]
pub target_receipt_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "transfer_id")]
pub transfer_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub currency: Option<String>,
#[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")]
pub state: Option<LandedCostState>,
#[serde(skip_serializing_if = "Option::is_none", alias = "amount_total")]
pub amount_total: Option<Decimal>,
#[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>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub notes: Option<String>,
}
impl PatchLandedCostDto {
pub fn has_changes(&self) -> bool {
self.lc_number.is_some() || self.branch_id.is_some() || self.target_receipt_id.is_some() || self.transfer_id.is_some() || self.currency.is_some() || self.posting_date.is_some() || self.state.is_some() || self.amount_total.is_some() || self.posting_state.is_some() || self.journal_id.is_some() || self.accounting_post_id.is_some() || self.posted_at.is_some() || self.notes.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct LandedCostResponseDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub lc_number: String,
pub branch_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub target_receipt_id: Uuid,
pub transfer_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub currency: String,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
pub posting_date: NaiveDate,
pub state: LandedCostState,
pub amount_total: Decimal,
pub posting_state: GlPostingState,
pub journal_id: Option<Uuid>,
pub accounting_post_id: Option<Uuid>,
pub posted_at: Option<DateTime<Utc>>,
pub notes: Option<String>,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct LandedCostListResponseDto {
pub items: Vec<LandedCostResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl LandedCostListResponseDto {
pub fn new(items: Vec<LandedCostResponseDto>, 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 LandedCostSummaryDto {
pub id: Uuid,
pub lc_number: String,
pub branch_id: Option<Uuid>,
pub target_receipt_id: Uuid,
pub created_at: Option<DateTime<Utc>>,
}
impl From<LandedCost> for LandedCostResponseDto {
fn from(entity: LandedCost) -> Self {
Self {
id: entity.id,
lc_number: entity.lc_number,
branch_id: entity.branch_id,
target_receipt_id: entity.target_receipt_id,
transfer_id: entity.transfer_id,
currency: entity.currency,
posting_date: entity.posting_date,
state: entity.state,
amount_total: entity.amount_total,
posting_state: entity.posting_state,
journal_id: entity.journal_id,
accounting_post_id: entity.accounting_post_id,
posted_at: entity.posted_at,
notes: entity.notes,
metadata: entity.metadata,
}
}
}
impl From<LandedCost> for LandedCostSummaryDto {
fn from(entity: LandedCost) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
lc_number: entity.lc_number,
branch_id: entity.branch_id,
target_receipt_id: entity.target_receipt_id,
created_at,
}
}
}
impl From<CreateLandedCostDto> for LandedCost {
fn from(dto: CreateLandedCostDto) -> Self {
Self {
id: Uuid::new_v4(),
lc_number: dto.lc_number,
branch_id: dto.branch_id,
target_receipt_id: dto.target_receipt_id,
transfer_id: dto.transfer_id,
currency: dto.currency,
posting_date: dto.posting_date,
state: LandedCostState::Draft,
amount_total: dto.amount_total,
posting_state: dto.posting_state,
journal_id: dto.journal_id,
accounting_post_id: dto.accounting_post_id,
posted_at: dto.posted_at,
notes: dto.notes,
metadata: AuditMetadata::default(),
}
}
}
impl From<&LandedCost> for LandedCostResponseDto {
fn from(entity: &LandedCost) -> Self {
Self {
id: entity.id.clone(),
lc_number: entity.lc_number.clone(),
branch_id: entity.branch_id.clone(),
target_receipt_id: entity.target_receipt_id.clone(),
transfer_id: entity.transfer_id.clone(),
currency: entity.currency.clone(),
posting_date: entity.posting_date.clone(),
state: entity.state.clone(),
amount_total: entity.amount_total.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(),
notes: entity.notes.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateLandedCostDto> for LandedCost {
fn from_create_dto(dto: CreateLandedCostDto) -> backbone_core::ServiceResult<Self> {
Ok(LandedCost::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateLandedCostDto> for LandedCost {
fn apply_update(mut self, dto: UpdateLandedCostDto) -> backbone_core::ServiceResult<Self> {
self.lc_number = dto.lc_number;
self.branch_id = dto.branch_id;
self.target_receipt_id = dto.target_receipt_id;
self.transfer_id = dto.transfer_id;
self.currency = dto.currency;
self.posting_date = dto.posting_date;
self.state = dto.state;
self.amount_total = dto.amount_total;
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;
self.notes = dto.notes;
Ok(self)
}
}