use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};
#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
use crate::domain::entity::InventoryCompanySetting;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::InventoryCostMethod;
use crate::domain::entity::ValuationPolicy;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct CreateInventoryCompanySettingDto {
#[serde(alias = "cost_method")]
pub cost_method: InventoryCostMethod,
#[serde(alias = "valuation_policy")]
pub valuation_policy: ValuationPolicy,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "anglo_saxon_accounting")]
pub anglo_saxon_accounting: bool,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "stock_interim_delivered_account_id")]
pub stock_interim_delivered_account_id: Option<Uuid>,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct UpdateInventoryCompanySettingDto {
#[serde(alias = "cost_method")]
pub cost_method: InventoryCostMethod,
#[serde(alias = "valuation_policy")]
pub valuation_policy: ValuationPolicy,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "anglo_saxon_accounting")]
pub anglo_saxon_accounting: bool,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "stock_interim_delivered_account_id")]
pub stock_interim_delivered_account_id: Option<Uuid>,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PatchInventoryCompanySettingDto {
#[serde(skip_serializing_if = "Option::is_none", alias = "cost_method")]
pub cost_method: Option<InventoryCostMethod>,
#[serde(skip_serializing_if = "Option::is_none", alias = "valuation_policy")]
pub valuation_policy: Option<ValuationPolicy>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(skip_serializing_if = "Option::is_none", alias = "anglo_saxon_accounting")]
pub anglo_saxon_accounting: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none", alias = "stock_interim_delivered_account_id")]
pub stock_interim_delivered_account_id: Option<Uuid>,
}
impl PatchInventoryCompanySettingDto {
pub fn has_changes(&self) -> bool {
self.cost_method.is_some() || self.valuation_policy.is_some() || self.anglo_saxon_accounting.is_some() || self.stock_interim_delivered_account_id.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct InventoryCompanySettingResponseDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub id: Uuid,
pub cost_method: InventoryCostMethod,
pub valuation_policy: ValuationPolicy,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub anglo_saxon_accounting: bool,
pub stock_interim_delivered_account_id: Option<Uuid>,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct InventoryCompanySettingListResponseDto {
pub items: Vec<InventoryCompanySettingResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl InventoryCompanySettingListResponseDto {
pub fn new(items: Vec<InventoryCompanySettingResponseDto>, 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 InventoryCompanySettingSummaryDto {
pub id: Uuid,
pub cost_method: InventoryCostMethod,
pub valuation_policy: ValuationPolicy,
pub anglo_saxon_accounting: bool,
pub created_at: Option<DateTime<Utc>>,
}
impl From<InventoryCompanySetting> for InventoryCompanySettingResponseDto {
fn from(entity: InventoryCompanySetting) -> Self {
Self {
id: entity.id,
cost_method: entity.cost_method,
valuation_policy: entity.valuation_policy,
anglo_saxon_accounting: entity.anglo_saxon_accounting,
stock_interim_delivered_account_id: entity.stock_interim_delivered_account_id,
metadata: entity.metadata,
}
}
}
impl From<InventoryCompanySetting> for InventoryCompanySettingSummaryDto {
fn from(entity: InventoryCompanySetting) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
cost_method: entity.cost_method,
valuation_policy: entity.valuation_policy,
anglo_saxon_accounting: entity.anglo_saxon_accounting,
created_at,
}
}
}
impl From<CreateInventoryCompanySettingDto> for InventoryCompanySetting {
fn from(dto: CreateInventoryCompanySettingDto) -> Self {
Self {
id: Uuid::new_v4(),
cost_method: dto.cost_method,
valuation_policy: dto.valuation_policy,
anglo_saxon_accounting: dto.anglo_saxon_accounting,
stock_interim_delivered_account_id: dto.stock_interim_delivered_account_id,
metadata: AuditMetadata::default(),
}
}
}
impl From<&InventoryCompanySetting> for InventoryCompanySettingResponseDto {
fn from(entity: &InventoryCompanySetting) -> Self {
Self {
id: entity.id.clone(),
cost_method: entity.cost_method.clone(),
valuation_policy: entity.valuation_policy.clone(),
anglo_saxon_accounting: entity.anglo_saxon_accounting.clone(),
stock_interim_delivered_account_id: entity.stock_interim_delivered_account_id.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateInventoryCompanySettingDto> for InventoryCompanySetting {
fn from_create_dto(dto: CreateInventoryCompanySettingDto) -> backbone_core::ServiceResult<Self> {
Ok(InventoryCompanySetting::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateInventoryCompanySettingDto> for InventoryCompanySetting {
fn apply_update(mut self, dto: UpdateInventoryCompanySettingDto) -> backbone_core::ServiceResult<Self> {
self.cost_method = dto.cost_method;
self.valuation_policy = dto.valuation_policy;
self.anglo_saxon_accounting = dto.anglo_saxon_accounting;
self.stock_interim_delivered_account_id = dto.stock_interim_delivered_account_id;
Ok(self)
}
}