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::Location;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::LocationUsage;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct CreateLocationDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "complete_name")]
pub complete_name: String,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub active: bool,
pub usage: LocationUsage,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "location_id")]
pub location_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "parent_path")]
pub parent_path: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub barcode: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "warehouse_id")]
pub warehouse_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "cyclic_inventory_frequency")]
pub cyclic_inventory_frequency: i32,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "last_inventory_date")]
pub last_inventory_date: Option<NaiveDate>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "next_inventory_date")]
pub next_inventory_date: Option<NaiveDate>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "valuation_account_id")]
pub valuation_account_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "storage_category_id")]
pub storage_category_id: Option<Uuid>,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct UpdateLocationDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "complete_name")]
pub complete_name: String,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub active: bool,
pub usage: LocationUsage,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "location_id")]
pub location_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "parent_path")]
pub parent_path: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub barcode: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "warehouse_id")]
pub warehouse_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "cyclic_inventory_frequency")]
pub cyclic_inventory_frequency: i32,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "last_inventory_date")]
pub last_inventory_date: Option<NaiveDate>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "next_inventory_date")]
pub next_inventory_date: Option<NaiveDate>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "valuation_account_id")]
pub valuation_account_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "storage_category_id")]
pub storage_category_id: Option<Uuid>,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PatchLocationDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "complete_name")]
pub complete_name: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(skip_serializing_if = "Option::is_none")]
pub active: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub usage: Option<LocationUsage>,
#[serde(skip_serializing_if = "Option::is_none", alias = "location_id")]
pub location_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "parent_path")]
pub parent_path: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub barcode: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", alias = "warehouse_id")]
pub warehouse_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "cyclic_inventory_frequency")]
pub cyclic_inventory_frequency: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none", alias = "last_inventory_date")]
pub last_inventory_date: Option<NaiveDate>,
#[serde(skip_serializing_if = "Option::is_none", alias = "next_inventory_date")]
pub next_inventory_date: Option<NaiveDate>,
#[serde(skip_serializing_if = "Option::is_none", alias = "valuation_account_id")]
pub valuation_account_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "storage_category_id")]
pub storage_category_id: Option<Uuid>,
}
impl PatchLocationDto {
pub fn has_changes(&self) -> bool {
self.name.is_some() || self.complete_name.is_some() || self.active.is_some() || self.usage.is_some() || self.location_id.is_some() || self.parent_path.is_some() || self.barcode.is_some() || self.warehouse_id.is_some() || self.cyclic_inventory_frequency.is_some() || self.last_inventory_date.is_some() || self.next_inventory_date.is_some() || self.valuation_account_id.is_some() || self.storage_category_id.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct LocationResponseDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub complete_name: String,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub active: bool,
pub usage: LocationUsage,
pub location_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub parent_path: String,
pub barcode: Option<String>,
pub warehouse_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub cyclic_inventory_frequency: i32,
pub last_inventory_date: Option<NaiveDate>,
pub next_inventory_date: Option<NaiveDate>,
pub valuation_account_id: Option<Uuid>,
pub storage_category_id: Option<Uuid>,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct LocationListResponseDto {
pub items: Vec<LocationResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl LocationListResponseDto {
pub fn new(items: Vec<LocationResponseDto>, 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 LocationSummaryDto {
pub id: Uuid,
pub name: String,
pub complete_name: String,
pub active: bool,
pub created_at: Option<DateTime<Utc>>,
}
impl From<Location> for LocationResponseDto {
fn from(entity: Location) -> Self {
Self {
id: entity.id,
name: entity.name,
complete_name: entity.complete_name,
active: entity.active,
usage: entity.usage,
location_id: entity.location_id,
parent_path: entity.parent_path,
barcode: entity.barcode,
warehouse_id: entity.warehouse_id,
cyclic_inventory_frequency: entity.cyclic_inventory_frequency,
last_inventory_date: entity.last_inventory_date,
next_inventory_date: entity.next_inventory_date,
valuation_account_id: entity.valuation_account_id,
storage_category_id: entity.storage_category_id,
metadata: entity.metadata,
}
}
}
impl From<Location> for LocationSummaryDto {
fn from(entity: Location) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
name: entity.name,
complete_name: entity.complete_name,
active: entity.active,
created_at,
}
}
}
impl From<CreateLocationDto> for Location {
fn from(dto: CreateLocationDto) -> Self {
Self {
id: Uuid::new_v4(),
name: dto.name,
complete_name: dto.complete_name,
active: dto.active,
usage: dto.usage,
location_id: dto.location_id,
parent_path: dto.parent_path,
barcode: dto.barcode,
warehouse_id: dto.warehouse_id,
cyclic_inventory_frequency: dto.cyclic_inventory_frequency,
last_inventory_date: dto.last_inventory_date,
next_inventory_date: dto.next_inventory_date,
valuation_account_id: dto.valuation_account_id,
storage_category_id: dto.storage_category_id,
metadata: AuditMetadata::default(),
}
}
}
impl From<&Location> for LocationResponseDto {
fn from(entity: &Location) -> Self {
Self {
id: entity.id.clone(),
name: entity.name.clone(),
complete_name: entity.complete_name.clone(),
active: entity.active.clone(),
usage: entity.usage.clone(),
location_id: entity.location_id.clone(),
parent_path: entity.parent_path.clone(),
barcode: entity.barcode.clone(),
warehouse_id: entity.warehouse_id.clone(),
cyclic_inventory_frequency: entity.cyclic_inventory_frequency.clone(),
last_inventory_date: entity.last_inventory_date.clone(),
next_inventory_date: entity.next_inventory_date.clone(),
valuation_account_id: entity.valuation_account_id.clone(),
storage_category_id: entity.storage_category_id.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateLocationDto> for Location {
fn from_create_dto(dto: CreateLocationDto) -> backbone_core::ServiceResult<Self> {
Ok(Location::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateLocationDto> for Location {
fn apply_update(mut self, dto: UpdateLocationDto) -> backbone_core::ServiceResult<Self> {
self.name = dto.name;
self.complete_name = dto.complete_name;
self.active = dto.active;
self.usage = dto.usage;
self.location_id = dto.location_id;
self.parent_path = dto.parent_path;
self.barcode = dto.barcode;
self.warehouse_id = dto.warehouse_id;
self.cyclic_inventory_frequency = dto.cyclic_inventory_frequency;
self.last_inventory_date = dto.last_inventory_date;
self.next_inventory_date = dto.next_inventory_date;
self.valuation_account_id = dto.valuation_account_id;
self.storage_category_id = dto.storage_category_id;
Ok(self)
}
}