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::BankClearance;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::MatchMethod;
use crate::domain::entity::MatchedSourceType;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreateBankClearanceDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "bank_transaction_id")]
pub bank_transaction_id: Uuid,
#[serde(alias = "matched_source_type")]
pub matched_source_type: MatchedSourceType,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "matched_source_id")]
pub matched_source_id: Uuid,
#[serde(alias = "matched_amount")]
pub matched_amount: Decimal,
#[serde(alias = "match_method")]
pub match_method: MatchMethod,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
#[serde(alias = "clearance_date")]
pub clearance_date: NaiveDate,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "accounting_post_id")]
pub accounting_post_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "journal_id")]
pub journal_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 UpdateBankClearanceDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "bank_transaction_id")]
pub bank_transaction_id: Uuid,
#[serde(alias = "matched_source_type")]
pub matched_source_type: MatchedSourceType,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "matched_source_id")]
pub matched_source_id: Uuid,
#[serde(alias = "matched_amount")]
pub matched_amount: Decimal,
#[serde(alias = "match_method")]
pub match_method: MatchMethod,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
#[serde(alias = "clearance_date")]
pub clearance_date: NaiveDate,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "accounting_post_id")]
pub accounting_post_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "journal_id")]
pub journal_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 PatchBankClearanceDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "bank_transaction_id")]
pub bank_transaction_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "matched_source_type")]
pub matched_source_type: Option<MatchedSourceType>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "matched_source_id")]
pub matched_source_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "matched_amount")]
pub matched_amount: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none", alias = "match_method")]
pub match_method: Option<MatchMethod>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "clearance_date")]
pub clearance_date: Option<NaiveDate>,
#[serde(skip_serializing_if = "Option::is_none", alias = "accounting_post_id")]
pub accounting_post_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "journal_id")]
pub journal_id: Option<Uuid>,
}
impl PatchBankClearanceDto {
pub fn has_changes(&self) -> bool {
self.bank_transaction_id.is_some() || self.matched_source_type.is_some() || self.matched_source_id.is_some() || self.matched_amount.is_some() || self.match_method.is_some() || self.clearance_date.is_some() || self.accounting_post_id.is_some() || self.journal_id.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct BankClearanceResponseDto {
#[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 bank_transaction_id: Uuid,
pub matched_source_type: MatchedSourceType,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub matched_source_id: Uuid,
pub matched_amount: Decimal,
pub match_method: MatchMethod,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
pub clearance_date: NaiveDate,
pub accounting_post_id: Option<Uuid>,
pub journal_id: Option<Uuid>,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct BankClearanceListResponseDto {
pub items: Vec<BankClearanceResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl BankClearanceListResponseDto {
pub fn new(items: Vec<BankClearanceResponseDto>, 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 BankClearanceSummaryDto {
pub id: Uuid,
pub bank_transaction_id: Uuid,
pub matched_source_type: MatchedSourceType,
pub matched_source_id: Uuid,
pub created_at: Option<DateTime<Utc>>,
}
impl From<BankClearance> for BankClearanceResponseDto {
fn from(entity: BankClearance) -> Self {
Self {
id: entity.id,
bank_transaction_id: entity.bank_transaction_id,
matched_source_type: entity.matched_source_type,
matched_source_id: entity.matched_source_id,
matched_amount: entity.matched_amount,
match_method: entity.match_method,
clearance_date: entity.clearance_date,
accounting_post_id: entity.accounting_post_id,
journal_id: entity.journal_id,
metadata: entity.metadata,
}
}
}
impl From<BankClearance> for BankClearanceSummaryDto {
fn from(entity: BankClearance) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
bank_transaction_id: entity.bank_transaction_id,
matched_source_type: entity.matched_source_type,
matched_source_id: entity.matched_source_id,
created_at,
}
}
}
impl From<CreateBankClearanceDto> for BankClearance {
fn from(dto: CreateBankClearanceDto) -> Self {
Self {
id: Uuid::new_v4(),
bank_transaction_id: dto.bank_transaction_id,
matched_source_type: dto.matched_source_type,
matched_source_id: dto.matched_source_id,
matched_amount: dto.matched_amount,
match_method: dto.match_method,
clearance_date: dto.clearance_date,
accounting_post_id: dto.accounting_post_id,
journal_id: dto.journal_id,
metadata: AuditMetadata::default(),
}
}
}
impl From<&BankClearance> for BankClearanceResponseDto {
fn from(entity: &BankClearance) -> Self {
Self {
id: entity.id.clone(),
bank_transaction_id: entity.bank_transaction_id.clone(),
matched_source_type: entity.matched_source_type.clone(),
matched_source_id: entity.matched_source_id.clone(),
matched_amount: entity.matched_amount.clone(),
match_method: entity.match_method.clone(),
clearance_date: entity.clearance_date.clone(),
accounting_post_id: entity.accounting_post_id.clone(),
journal_id: entity.journal_id.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateBankClearanceDto> for BankClearance {
fn from_create_dto(dto: CreateBankClearanceDto) -> backbone_core::ServiceResult<Self> {
Ok(BankClearance::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateBankClearanceDto> for BankClearance {
fn apply_update(mut self, dto: UpdateBankClearanceDto) -> backbone_core::ServiceResult<Self> {
self.bank_transaction_id = dto.bank_transaction_id;
self.matched_source_type = dto.matched_source_type;
self.matched_source_id = dto.matched_source_id;
self.matched_amount = dto.matched_amount;
self.match_method = dto.match_method;
self.clearance_date = dto.clearance_date;
self.accounting_post_id = dto.accounting_post_id;
self.journal_id = dto.journal_id;
Ok(self)
}
}