use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
#[cfg(feature = "validation")]
use validator::Validate;
use crate::domain::entity::SalaryComponent;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::ComponentType;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreateSalaryComponentDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "structure_id")]
pub structure_id: Uuid,
#[cfg_attr(feature = "validation", validate(length(max = 140)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[serde(alias = "component_type")]
pub component_type: ComponentType,
pub amount: Decimal,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "gl_account_id")]
pub gl_account_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "is_statutory")]
pub is_statutory: bool,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateSalaryComponentDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "structure_id")]
pub structure_id: Uuid,
#[cfg_attr(feature = "validation", validate(length(max = 140)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[serde(alias = "component_type")]
pub component_type: ComponentType,
pub amount: Decimal,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "gl_account_id")]
pub gl_account_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "is_statutory")]
pub is_statutory: bool,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchSalaryComponentDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "structure_id")]
pub structure_id: Option<Uuid>,
#[cfg_attr(feature = "validation", validate(length(max = 140)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", alias = "component_type")]
pub component_type: Option<ComponentType>,
#[serde(skip_serializing_if = "Option::is_none")]
pub amount: Option<Decimal>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "gl_account_id")]
pub gl_account_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(skip_serializing_if = "Option::is_none", alias = "is_statutory")]
pub is_statutory: Option<bool>,
}
impl PatchSalaryComponentDto {
pub fn has_changes(&self) -> bool {
self.structure_id.is_some() || self.name.is_some() || self.component_type.is_some() || self.amount.is_some() || self.gl_account_id.is_some() || self.is_statutory.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct SalaryComponentResponseDto {
#[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 structure_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
pub component_type: ComponentType,
pub amount: Decimal,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub gl_account_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub is_statutory: bool,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct SalaryComponentListResponseDto {
pub items: Vec<SalaryComponentResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl SalaryComponentListResponseDto {
pub fn new(items: Vec<SalaryComponentResponseDto>, 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 SalaryComponentSummaryDto {
pub id: Uuid,
pub structure_id: Uuid,
pub name: String,
pub component_type: ComponentType,
pub created_at: Option<DateTime<Utc>>,
}
impl From<SalaryComponent> for SalaryComponentResponseDto {
fn from(entity: SalaryComponent) -> Self {
Self {
id: entity.id,
structure_id: entity.structure_id,
name: entity.name,
component_type: entity.component_type,
amount: entity.amount,
gl_account_id: entity.gl_account_id,
is_statutory: entity.is_statutory,
metadata: entity.metadata,
}
}
}
impl From<SalaryComponent> for SalaryComponentSummaryDto {
fn from(entity: SalaryComponent) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
structure_id: entity.structure_id,
name: entity.name,
component_type: entity.component_type,
created_at,
}
}
}
impl From<CreateSalaryComponentDto> for SalaryComponent {
fn from(dto: CreateSalaryComponentDto) -> Self {
Self {
id: Uuid::new_v4(),
structure_id: dto.structure_id,
name: dto.name,
component_type: dto.component_type,
amount: dto.amount,
gl_account_id: dto.gl_account_id,
is_statutory: dto.is_statutory,
metadata: AuditMetadata::default(),
}
}
}
impl From<&SalaryComponent> for SalaryComponentResponseDto {
fn from(entity: &SalaryComponent) -> Self {
Self {
id: entity.id.clone(),
structure_id: entity.structure_id.clone(),
name: entity.name.clone(),
component_type: entity.component_type.clone(),
amount: entity.amount.clone(),
gl_account_id: entity.gl_account_id.clone(),
is_statutory: entity.is_statutory.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateSalaryComponentDto> for SalaryComponent {
fn from_create_dto(dto: CreateSalaryComponentDto) -> backbone_core::ServiceResult<Self> {
Ok(SalaryComponent::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateSalaryComponentDto> for SalaryComponent {
fn apply_update(mut self, dto: UpdateSalaryComponentDto) -> backbone_core::ServiceResult<Self> {
self.structure_id = dto.structure_id;
self.name = dto.name;
self.component_type = dto.component_type;
self.amount = dto.amount;
self.gl_account_id = dto.gl_account_id;
self.is_statutory = dto.is_statutory;
Ok(self)
}
}