use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
use crate::domain::entity::LandedCostAdjustmentLine;
use crate::domain::entity::AuditMetadata;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct CreateLandedCostAdjustmentLineDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "lc_id")]
pub lc_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "move_line_id")]
pub move_line_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "cost_line_id")]
pub cost_line_id: Uuid,
pub share: Decimal,
#[serde(alias = "additional_landed_cost")]
pub additional_landed_cost: Decimal,
#[serde(alias = "remaining_qty")]
pub remaining_qty: Decimal,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct UpdateLandedCostAdjustmentLineDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "lc_id")]
pub lc_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "move_line_id")]
pub move_line_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "cost_line_id")]
pub cost_line_id: Uuid,
pub share: Decimal,
#[serde(alias = "additional_landed_cost")]
pub additional_landed_cost: Decimal,
#[serde(alias = "remaining_qty")]
pub remaining_qty: Decimal,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PatchLandedCostAdjustmentLineDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "lc_id")]
pub lc_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "move_line_id")]
pub move_line_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "cost_line_id")]
pub cost_line_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none")]
pub share: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none", alias = "additional_landed_cost")]
pub additional_landed_cost: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none", alias = "remaining_qty")]
pub remaining_qty: Option<Decimal>,
}
impl PatchLandedCostAdjustmentLineDto {
pub fn has_changes(&self) -> bool {
self.lc_id.is_some() || self.move_line_id.is_some() || self.cost_line_id.is_some() || self.share.is_some() || self.additional_landed_cost.is_some() || self.remaining_qty.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct LandedCostAdjustmentLineResponseDto {
#[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 lc_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub move_line_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub cost_line_id: Uuid,
pub share: Decimal,
pub additional_landed_cost: Decimal,
pub remaining_qty: Decimal,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct LandedCostAdjustmentLineListResponseDto {
pub items: Vec<LandedCostAdjustmentLineResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl LandedCostAdjustmentLineListResponseDto {
pub fn new(items: Vec<LandedCostAdjustmentLineResponseDto>, 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 LandedCostAdjustmentLineSummaryDto {
pub id: Uuid,
pub lc_id: Uuid,
pub move_line_id: Uuid,
pub cost_line_id: Uuid,
pub created_at: Option<DateTime<Utc>>,
}
impl From<LandedCostAdjustmentLine> for LandedCostAdjustmentLineResponseDto {
fn from(entity: LandedCostAdjustmentLine) -> Self {
Self {
id: entity.id,
lc_id: entity.lc_id,
move_line_id: entity.move_line_id,
cost_line_id: entity.cost_line_id,
share: entity.share,
additional_landed_cost: entity.additional_landed_cost,
remaining_qty: entity.remaining_qty,
metadata: entity.metadata,
}
}
}
impl From<LandedCostAdjustmentLine> for LandedCostAdjustmentLineSummaryDto {
fn from(entity: LandedCostAdjustmentLine) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
lc_id: entity.lc_id,
move_line_id: entity.move_line_id,
cost_line_id: entity.cost_line_id,
created_at,
}
}
}
impl From<CreateLandedCostAdjustmentLineDto> for LandedCostAdjustmentLine {
fn from(dto: CreateLandedCostAdjustmentLineDto) -> Self {
Self {
id: Uuid::new_v4(),
lc_id: dto.lc_id,
move_line_id: dto.move_line_id,
cost_line_id: dto.cost_line_id,
share: dto.share,
additional_landed_cost: dto.additional_landed_cost,
remaining_qty: dto.remaining_qty,
metadata: AuditMetadata::default(),
}
}
}
impl From<&LandedCostAdjustmentLine> for LandedCostAdjustmentLineResponseDto {
fn from(entity: &LandedCostAdjustmentLine) -> Self {
Self {
id: entity.id.clone(),
lc_id: entity.lc_id.clone(),
move_line_id: entity.move_line_id.clone(),
cost_line_id: entity.cost_line_id.clone(),
share: entity.share.clone(),
additional_landed_cost: entity.additional_landed_cost.clone(),
remaining_qty: entity.remaining_qty.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateLandedCostAdjustmentLineDto> for LandedCostAdjustmentLine {
fn from_create_dto(dto: CreateLandedCostAdjustmentLineDto) -> backbone_core::ServiceResult<Self> {
Ok(LandedCostAdjustmentLine::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateLandedCostAdjustmentLineDto> for LandedCostAdjustmentLine {
fn apply_update(mut self, dto: UpdateLandedCostAdjustmentLineDto) -> backbone_core::ServiceResult<Self> {
self.lc_id = dto.lc_id;
self.move_line_id = dto.move_line_id;
self.cost_line_id = dto.cost_line_id;
self.share = dto.share;
self.additional_landed_cost = dto.additional_landed_cost;
self.remaining_qty = dto.remaining_qty;
Ok(self)
}
}