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::SalarySlip;
use crate::domain::entity::AuditMetadata;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreateSalarySlipDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "payroll_entry_id")]
pub payroll_entry_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "employee_id")]
pub employee_id: Uuid,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "structure_id")]
pub structure_id: Option<Uuid>,
#[serde(alias = "working_days")]
pub working_days: Decimal,
#[serde(alias = "unpaid_days")]
pub unpaid_days: Decimal,
#[serde(alias = "gross_pay")]
pub gross_pay: Decimal,
#[serde(alias = "total_deductions")]
pub total_deductions: Decimal,
#[serde(alias = "net_pay")]
pub net_pay: Decimal,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "overtime_hours")]
pub overtime_hours: Option<Decimal>,
#[cfg_attr(feature = "validation", validate(length(max = 20)))]
#[serde(default, skip_serializing_if = "Option::is_none", alias = "tax_method")]
pub tax_method: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateSalarySlipDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "payroll_entry_id")]
pub payroll_entry_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "employee_id")]
pub employee_id: Uuid,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "structure_id")]
pub structure_id: Option<Uuid>,
#[serde(alias = "working_days")]
pub working_days: Decimal,
#[serde(alias = "unpaid_days")]
pub unpaid_days: Decimal,
#[serde(alias = "gross_pay")]
pub gross_pay: Decimal,
#[serde(alias = "total_deductions")]
pub total_deductions: Decimal,
#[serde(alias = "net_pay")]
pub net_pay: Decimal,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "overtime_hours")]
pub overtime_hours: Option<Decimal>,
#[cfg_attr(feature = "validation", validate(length(max = 20)))]
#[serde(default, skip_serializing_if = "Option::is_none", alias = "tax_method")]
pub tax_method: Option<String>,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchSalarySlipDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "payroll_entry_id")]
pub payroll_entry_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "employee_id")]
pub employee_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "structure_id")]
pub structure_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "working_days")]
pub working_days: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none", alias = "unpaid_days")]
pub unpaid_days: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none", alias = "gross_pay")]
pub gross_pay: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none", alias = "total_deductions")]
pub total_deductions: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none", alias = "net_pay")]
pub net_pay: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none", alias = "overtime_hours")]
pub overtime_hours: Option<Decimal>,
#[cfg_attr(feature = "validation", validate(length(max = 20)))]
#[serde(skip_serializing_if = "Option::is_none", alias = "tax_method")]
pub tax_method: Option<String>,
}
impl PatchSalarySlipDto {
pub fn has_changes(&self) -> bool {
self.payroll_entry_id.is_some() || self.employee_id.is_some() || self.structure_id.is_some() || self.working_days.is_some() || self.unpaid_days.is_some() || self.gross_pay.is_some() || self.total_deductions.is_some() || self.net_pay.is_some() || self.overtime_hours.is_some() || self.tax_method.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct SalarySlipResponseDto {
#[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 payroll_entry_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub employee_id: Uuid,
pub structure_id: Option<Uuid>,
pub working_days: Decimal,
pub unpaid_days: Decimal,
pub gross_pay: Decimal,
pub total_deductions: Decimal,
pub net_pay: Decimal,
pub overtime_hours: Option<Decimal>,
pub tax_method: Option<String>,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct SalarySlipListResponseDto {
pub items: Vec<SalarySlipResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl SalarySlipListResponseDto {
pub fn new(items: Vec<SalarySlipResponseDto>, 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 SalarySlipSummaryDto {
pub id: Uuid,
pub payroll_entry_id: Uuid,
pub employee_id: Uuid,
pub structure_id: Option<Uuid>,
pub created_at: Option<DateTime<Utc>>,
}
impl From<SalarySlip> for SalarySlipResponseDto {
fn from(entity: SalarySlip) -> Self {
Self {
id: entity.id,
payroll_entry_id: entity.payroll_entry_id,
employee_id: entity.employee_id,
structure_id: entity.structure_id,
working_days: entity.working_days,
unpaid_days: entity.unpaid_days,
gross_pay: entity.gross_pay,
total_deductions: entity.total_deductions,
net_pay: entity.net_pay,
overtime_hours: entity.overtime_hours,
tax_method: entity.tax_method,
metadata: entity.metadata,
}
}
}
impl From<SalarySlip> for SalarySlipSummaryDto {
fn from(entity: SalarySlip) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
payroll_entry_id: entity.payroll_entry_id,
employee_id: entity.employee_id,
structure_id: entity.structure_id,
created_at,
}
}
}
impl From<CreateSalarySlipDto> for SalarySlip {
fn from(dto: CreateSalarySlipDto) -> Self {
Self {
id: Uuid::new_v4(),
payroll_entry_id: dto.payroll_entry_id,
employee_id: dto.employee_id,
structure_id: dto.structure_id,
working_days: dto.working_days,
unpaid_days: dto.unpaid_days,
gross_pay: dto.gross_pay,
total_deductions: dto.total_deductions,
net_pay: dto.net_pay,
overtime_hours: dto.overtime_hours,
tax_method: dto.tax_method,
metadata: AuditMetadata::default(),
}
}
}
impl From<&SalarySlip> for SalarySlipResponseDto {
fn from(entity: &SalarySlip) -> Self {
Self {
id: entity.id.clone(),
payroll_entry_id: entity.payroll_entry_id.clone(),
employee_id: entity.employee_id.clone(),
structure_id: entity.structure_id.clone(),
working_days: entity.working_days.clone(),
unpaid_days: entity.unpaid_days.clone(),
gross_pay: entity.gross_pay.clone(),
total_deductions: entity.total_deductions.clone(),
net_pay: entity.net_pay.clone(),
overtime_hours: entity.overtime_hours.clone(),
tax_method: entity.tax_method.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateSalarySlipDto> for SalarySlip {
fn from_create_dto(dto: CreateSalarySlipDto) -> backbone_core::ServiceResult<Self> {
Ok(SalarySlip::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateSalarySlipDto> for SalarySlip {
fn apply_update(mut self, dto: UpdateSalarySlipDto) -> backbone_core::ServiceResult<Self> {
self.payroll_entry_id = dto.payroll_entry_id;
self.employee_id = dto.employee_id;
self.structure_id = dto.structure_id;
self.working_days = dto.working_days;
self.unpaid_days = dto.unpaid_days;
self.gross_pay = dto.gross_pay;
self.total_deductions = dto.total_deductions;
self.net_pay = dto.net_pay;
self.overtime_hours = dto.overtime_hours;
self.tax_method = dto.tax_method;
Ok(self)
}
}