backbone-payroll 0.3.49

Payroll: salary structures, payroll runs and computed salary slips over effective-dated statutory tables, plus compensation changes
Documentation
//! PayrollEntry DTOs (Data Transfer Objects)
//!
//! Generated by metaphor-schema. Do not edit manually.
//!
//! DTOs provide a clean separation between domain entities and API
//! representations, with validation and OpenAPI documentation support.

use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc, NaiveDate};
use rust_decimal::Decimal;

#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;

#[cfg(feature = "validation")]
use validator::Validate;

use crate::domain::entity::PayrollEntry;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::PayrollStatus;

// =============================================================================
// Create DTO
// =============================================================================

/// Request DTO for creating a new PayrollEntry
///
/// Used in POST requests to create new entities.
/// Excludes auto-generated fields (id, created_at, updated_at, deleted_at).
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreatePayrollEntryDto {
    #[cfg_attr(feature = "openapi", schema(example = 42))]
    #[serde(alias = "period_year")]
    pub period_year: i32,
    #[cfg_attr(feature = "openapi", schema(example = 42))]
    #[serde(alias = "period_month")]
    pub period_month: i32,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "posting_date")]
    pub posting_date: Option<DateTime<Utc>>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "timesheet_approval_id")]
    pub timesheet_approval_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "period_start")]
    pub period_start: Option<NaiveDate>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "period_end")]
    pub period_end: Option<NaiveDate>,
    pub status: PayrollStatus,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "salary_expense_account_id")]
    pub salary_expense_account_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "salary_payable_account_id")]
    pub salary_payable_account_id: Option<Uuid>,
    #[serde(alias = "total_gross")]
    pub total_gross: Decimal,
    #[serde(alias = "total_deductions")]
    pub total_deductions: Decimal,
    #[serde(alias = "total_net")]
    pub total_net: Decimal,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "journal_id")]
    pub journal_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "accounting_post_id")]
    pub accounting_post_id: Option<Uuid>,
}

// =============================================================================
// Update DTO
// =============================================================================

/// Request DTO for full update of a PayrollEntry
///
/// Used in PUT requests for full entity replacement.
/// All fields are required (except auto-generated ones).
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdatePayrollEntryDto {
    #[cfg_attr(feature = "openapi", schema(example = 42))]
    #[serde(alias = "period_year")]
    pub period_year: i32,
    #[cfg_attr(feature = "openapi", schema(example = 42))]
    #[serde(alias = "period_month")]
    pub period_month: i32,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "posting_date")]
    pub posting_date: Option<DateTime<Utc>>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "timesheet_approval_id")]
    pub timesheet_approval_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "period_start")]
    pub period_start: Option<NaiveDate>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "period_end")]
    pub period_end: Option<NaiveDate>,
    pub status: PayrollStatus,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "salary_expense_account_id")]
    pub salary_expense_account_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "salary_payable_account_id")]
    pub salary_payable_account_id: Option<Uuid>,
    #[serde(alias = "total_gross")]
    pub total_gross: Decimal,
    #[serde(alias = "total_deductions")]
    pub total_deductions: Decimal,
    #[serde(alias = "total_net")]
    pub total_net: Decimal,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "journal_id")]
    pub journal_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "accounting_post_id")]
    pub accounting_post_id: Option<Uuid>,
}

// =============================================================================
// Patch DTO
// =============================================================================

/// Request DTO for partial update of a PayrollEntry
///
/// Used in PATCH requests for partial updates.
/// All fields are optional - only provided fields will be updated.
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchPayrollEntryDto {
    #[cfg_attr(feature = "openapi", schema(example = 42))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "period_year")]
    pub period_year: Option<i32>,
    #[cfg_attr(feature = "openapi", schema(example = 42))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "period_month")]
    pub period_month: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "posting_date")]
    pub posting_date: Option<DateTime<Utc>>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "timesheet_approval_id")]
    pub timesheet_approval_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "period_start")]
    pub period_start: Option<NaiveDate>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "period_end")]
    pub period_end: Option<NaiveDate>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<PayrollStatus>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "salary_expense_account_id")]
    pub salary_expense_account_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "salary_payable_account_id")]
    pub salary_payable_account_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "total_gross")]
    pub total_gross: 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 = "total_net")]
    pub total_net: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "journal_id")]
    pub journal_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "accounting_post_id")]
    pub accounting_post_id: Option<Uuid>,
}

impl PatchPayrollEntryDto {
    /// Check if any field is set
    pub fn has_changes(&self) -> bool {
        self.period_year.is_some() || self.period_month.is_some() || self.posting_date.is_some() || self.timesheet_approval_id.is_some() || self.period_start.is_some() || self.period_end.is_some() || self.status.is_some() || self.salary_expense_account_id.is_some() || self.salary_payable_account_id.is_some() || self.total_gross.is_some() || self.total_deductions.is_some() || self.total_net.is_some() || self.journal_id.is_some() || self.accounting_post_id.is_some()
    }
}

// =============================================================================
// Response DTO
// =============================================================================

/// Response DTO for PayrollEntry entity
///
/// Used in API responses for single entity.
/// Includes all fields including metadata.
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PayrollEntryResponseDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = 42))]
    pub period_year: i32,
    #[cfg_attr(feature = "openapi", schema(example = 42))]
    pub period_month: i32,
    pub posting_date: Option<DateTime<Utc>>,
    pub timesheet_approval_id: Option<Uuid>,
    pub period_start: Option<NaiveDate>,
    pub period_end: Option<NaiveDate>,
    pub status: PayrollStatus,
    pub salary_expense_account_id: Option<Uuid>,
    pub salary_payable_account_id: Option<Uuid>,
    pub total_gross: Decimal,
    pub total_deductions: Decimal,
    pub total_net: Decimal,
    pub journal_id: Option<Uuid>,
    pub accounting_post_id: Option<Uuid>,
    pub metadata: AuditMetadata,
}

// =============================================================================
// List Response DTO
// =============================================================================

/// Paginated list response for PayrollEntry entities
///
/// Used in API responses for list endpoints.
/// Includes pagination metadata.
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PayrollEntryListResponseDto {
    /// List of items
    pub items: Vec<PayrollEntryResponseDto>,
    /// Total number of items (across all pages)
    pub total: u64,
    /// Current page number (1-indexed)
    pub page: u32,
    /// Number of items per page
    pub per_page: u32,
    /// Total number of pages
    pub total_pages: u32,
    /// Whether there is a next page
    pub has_next: bool,
    /// Whether there is a previous page
    pub has_prev: bool,
}

impl PayrollEntryListResponseDto {
    /// Create a new list response from items and pagination info
    pub fn new(items: Vec<PayrollEntryResponseDto>, 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,
        }
    }
}

/// Summary DTO for list views (compact version)
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PayrollEntrySummaryDto {
    pub id: Uuid,
    pub period_year: i32,
    pub period_month: i32,
    pub posting_date: Option<DateTime<Utc>>,
    pub created_at: Option<DateTime<Utc>>,
}

// =============================================================================
// Conversions
// =============================================================================

impl From<PayrollEntry> for PayrollEntryResponseDto {
    fn from(entity: PayrollEntry) -> Self {
        Self {
            id: entity.id,
            period_year: entity.period_year,
            period_month: entity.period_month,
            posting_date: entity.posting_date,
            timesheet_approval_id: entity.timesheet_approval_id,
            period_start: entity.period_start,
            period_end: entity.period_end,
            status: entity.status,
            salary_expense_account_id: entity.salary_expense_account_id,
            salary_payable_account_id: entity.salary_payable_account_id,
            total_gross: entity.total_gross,
            total_deductions: entity.total_deductions,
            total_net: entity.total_net,
            journal_id: entity.journal_id,
            accounting_post_id: entity.accounting_post_id,
            metadata: entity.metadata,
        }
    }
}

impl From<PayrollEntry> for PayrollEntrySummaryDto {
    fn from(entity: PayrollEntry) -> Self {
        let created_at = backbone_core::PersistentEntity::created_at(&entity);
        Self {
            id: entity.id,
            period_year: entity.period_year,
            period_month: entity.period_month,
            posting_date: entity.posting_date,
            created_at,
        }
    }
}

impl From<CreatePayrollEntryDto> for PayrollEntry {
    fn from(dto: CreatePayrollEntryDto) -> Self {
        Self {
            id: Uuid::new_v4(),
            period_year: dto.period_year,
            period_month: dto.period_month,
            posting_date: dto.posting_date,
            timesheet_approval_id: dto.timesheet_approval_id,
            period_start: dto.period_start,
            period_end: dto.period_end,
            status: dto.status,
            salary_expense_account_id: dto.salary_expense_account_id,
            salary_payable_account_id: dto.salary_payable_account_id,
            total_gross: dto.total_gross,
            total_deductions: dto.total_deductions,
            total_net: dto.total_net,
            journal_id: dto.journal_id,
            accounting_post_id: dto.accounting_post_id,
            metadata: AuditMetadata::default(),
        }
    }
}

impl From<&PayrollEntry> for PayrollEntryResponseDto {
    fn from(entity: &PayrollEntry) -> Self {
        Self {
            id: entity.id.clone(),
            period_year: entity.period_year.clone(),
            period_month: entity.period_month.clone(),
            posting_date: entity.posting_date.clone(),
            timesheet_approval_id: entity.timesheet_approval_id.clone(),
            period_start: entity.period_start.clone(),
            period_end: entity.period_end.clone(),
            status: entity.status.clone(),
            salary_expense_account_id: entity.salary_expense_account_id.clone(),
            salary_payable_account_id: entity.salary_payable_account_id.clone(),
            total_gross: entity.total_gross.clone(),
            total_deductions: entity.total_deductions.clone(),
            total_net: entity.total_net.clone(),
            journal_id: entity.journal_id.clone(),
            accounting_post_id: entity.accounting_post_id.clone(),
            metadata: entity.metadata.clone(),
        }
    }
}

impl backbone_core::FromCreateDto<CreatePayrollEntryDto> for PayrollEntry {
    fn from_create_dto(dto: CreatePayrollEntryDto) -> backbone_core::ServiceResult<Self> {
        Ok(PayrollEntry::from(dto))
    }
}

impl backbone_core::ApplyUpdateDto<UpdatePayrollEntryDto> for PayrollEntry {
    fn apply_update(mut self, dto: UpdatePayrollEntryDto) -> backbone_core::ServiceResult<Self> {
        self.period_year = dto.period_year;
        self.period_month = dto.period_month;
        self.posting_date = dto.posting_date;
        self.timesheet_approval_id = dto.timesheet_approval_id;
        self.period_start = dto.period_start;
        self.period_end = dto.period_end;
        self.status = dto.status;
        self.salary_expense_account_id = dto.salary_expense_account_id;
        self.salary_payable_account_id = dto.salary_payable_account_id;
        self.total_gross = dto.total_gross;
        self.total_deductions = dto.total_deductions;
        self.total_net = dto.total_net;
        self.journal_id = dto.journal_id;
        self.accounting_post_id = dto.accounting_post_id;
        Ok(self)
    }
}

// =============================================================================
// Custom DTOs
// =============================================================================

// <<< CUSTOM DTOs
// Add custom DTOs specific to PayrollEntry here.
// This section will be preserved during regeneration.
// >>> END CUSTOM DTOs