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::DeliveryNoteItem;
use crate::domain::entity::AuditMetadata;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct CreateDeliveryNoteItemDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "delivery_id")]
pub delivery_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "item_id")]
pub item_id: Uuid,
pub quantity: Decimal,
#[serde(alias = "valuation_rate")]
pub valuation_rate: Decimal,
#[serde(alias = "cogs_amount")]
pub cogs_amount: Decimal,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct UpdateDeliveryNoteItemDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "delivery_id")]
pub delivery_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "item_id")]
pub item_id: Uuid,
pub quantity: Decimal,
#[serde(alias = "valuation_rate")]
pub valuation_rate: Decimal,
#[serde(alias = "cogs_amount")]
pub cogs_amount: Decimal,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PatchDeliveryNoteItemDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "delivery_id")]
pub delivery_id: Option<Uuid>,
#[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>,
#[serde(skip_serializing_if = "Option::is_none")]
pub quantity: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none", alias = "valuation_rate")]
pub valuation_rate: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none", alias = "cogs_amount")]
pub cogs_amount: Option<Decimal>,
}
impl PatchDeliveryNoteItemDto {
pub fn has_changes(&self) -> bool {
self.delivery_id.is_some() || self.item_id.is_some() || self.quantity.is_some() || self.valuation_rate.is_some() || self.cogs_amount.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct DeliveryNoteItemResponseDto {
#[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 delivery_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub item_id: Uuid,
pub quantity: Decimal,
pub valuation_rate: Decimal,
pub cogs_amount: Decimal,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct DeliveryNoteItemListResponseDto {
pub items: Vec<DeliveryNoteItemResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl DeliveryNoteItemListResponseDto {
pub fn new(items: Vec<DeliveryNoteItemResponseDto>, 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 DeliveryNoteItemSummaryDto {
pub id: Uuid,
pub delivery_id: Uuid,
pub item_id: Uuid,
pub quantity: Decimal,
pub created_at: Option<DateTime<Utc>>,
}
impl From<DeliveryNoteItem> for DeliveryNoteItemResponseDto {
fn from(entity: DeliveryNoteItem) -> Self {
Self {
id: entity.id,
delivery_id: entity.delivery_id,
item_id: entity.item_id,
quantity: entity.quantity,
valuation_rate: entity.valuation_rate,
cogs_amount: entity.cogs_amount,
metadata: entity.metadata,
}
}
}
impl From<DeliveryNoteItem> for DeliveryNoteItemSummaryDto {
fn from(entity: DeliveryNoteItem) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
delivery_id: entity.delivery_id,
item_id: entity.item_id,
quantity: entity.quantity,
created_at,
}
}
}
impl From<CreateDeliveryNoteItemDto> for DeliveryNoteItem {
fn from(dto: CreateDeliveryNoteItemDto) -> Self {
Self {
id: Uuid::new_v4(),
delivery_id: dto.delivery_id,
item_id: dto.item_id,
quantity: dto.quantity,
valuation_rate: dto.valuation_rate,
cogs_amount: dto.cogs_amount,
metadata: AuditMetadata::default(),
}
}
}
impl From<&DeliveryNoteItem> for DeliveryNoteItemResponseDto {
fn from(entity: &DeliveryNoteItem) -> Self {
Self {
id: entity.id.clone(),
delivery_id: entity.delivery_id.clone(),
item_id: entity.item_id.clone(),
quantity: entity.quantity.clone(),
valuation_rate: entity.valuation_rate.clone(),
cogs_amount: entity.cogs_amount.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateDeliveryNoteItemDto> for DeliveryNoteItem {
fn from_create_dto(dto: CreateDeliveryNoteItemDto) -> backbone_core::ServiceResult<Self> {
Ok(DeliveryNoteItem::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateDeliveryNoteItemDto> for DeliveryNoteItem {
fn apply_update(mut self, dto: UpdateDeliveryNoteItemDto) -> backbone_core::ServiceResult<Self> {
self.delivery_id = dto.delivery_id;
self.item_id = dto.item_id;
self.quantity = dto.quantity;
self.valuation_rate = dto.valuation_rate;
self.cogs_amount = dto.cogs_amount;
Ok(self)
}
}