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;
#[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>,
}
#[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>,
}
#[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 {
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()
}
}
#[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,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PayrollEntryListResponseDto {
pub items: Vec<PayrollEntryResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl PayrollEntryListResponseDto {
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,
}
}
}
#[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>>,
}
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)
}
}