use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
use crate::domain::entity::PrintedCheck;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct CreatePrintedCheckDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "bank_account_id")]
pub bank_account_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "payment_id")]
pub payment_id: Uuid,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "payment_number")]
pub payment_number: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "check_number")]
pub check_number: String,
pub amount: Decimal,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "payee_name")]
pub payee_name: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub status: String,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(alias = "printed_at")]
pub printed_at: DateTime<Utc>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "printed_by")]
pub printed_by: Option<Uuid>,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct UpdatePrintedCheckDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "bank_account_id")]
pub bank_account_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "payment_id")]
pub payment_id: Uuid,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "payment_number")]
pub payment_number: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "check_number")]
pub check_number: String,
pub amount: Decimal,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "payee_name")]
pub payee_name: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub status: String,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(alias = "printed_at")]
pub printed_at: DateTime<Utc>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "printed_by")]
pub printed_by: Option<Uuid>,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PatchPrintedCheckDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "bank_account_id")]
pub bank_account_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "payment_id")]
pub payment_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "payment_number")]
pub payment_number: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "check_number")]
pub check_number: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub amount: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none", alias = "payee_name")]
pub payee_name: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "printed_at")]
pub printed_at: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none", alias = "printed_by")]
pub printed_by: Option<Uuid>,
}
impl PatchPrintedCheckDto {
pub fn has_changes(&self) -> bool {
self.bank_account_id.is_some() || self.payment_id.is_some() || self.payment_number.is_some() || self.check_number.is_some() || self.amount.is_some() || self.payee_name.is_some() || self.status.is_some() || self.printed_at.is_some() || self.printed_by.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PrintedCheckResponseDto {
#[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_account_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub payment_id: Uuid,
pub payment_number: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub check_number: String,
pub amount: Decimal,
pub payee_name: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub status: String,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
pub printed_at: DateTime<Utc>,
pub printed_by: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
pub created_at: DateTime<Utc>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
pub updated_at: DateTime<Utc>,
pub metadata: serde_json::Value,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PrintedCheckListResponseDto {
pub items: Vec<PrintedCheckResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl PrintedCheckListResponseDto {
pub fn new(items: Vec<PrintedCheckResponseDto>, 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 PrintedCheckSummaryDto {
pub id: Uuid,
pub bank_account_id: Uuid,
pub payment_id: Uuid,
pub payment_number: Option<String>,
pub created_at: Option<DateTime<Utc>>,
}
impl From<PrintedCheck> for PrintedCheckResponseDto {
fn from(entity: PrintedCheck) -> Self {
Self {
id: entity.id,
bank_account_id: entity.bank_account_id,
payment_id: entity.payment_id,
payment_number: entity.payment_number,
check_number: entity.check_number,
amount: entity.amount,
payee_name: entity.payee_name,
status: entity.status,
printed_at: entity.printed_at,
printed_by: entity.printed_by,
created_at: entity.created_at,
updated_at: entity.updated_at,
metadata: entity.metadata,
}
}
}
impl From<PrintedCheck> for PrintedCheckSummaryDto {
fn from(entity: PrintedCheck) -> Self {
Self {
id: entity.id,
bank_account_id: entity.bank_account_id,
payment_id: entity.payment_id,
payment_number: entity.payment_number,
created_at: Some(entity.created_at),
}
}
}
impl From<CreatePrintedCheckDto> for PrintedCheck {
fn from(dto: CreatePrintedCheckDto) -> Self {
Self {
id: Uuid::new_v4(),
bank_account_id: dto.bank_account_id,
payment_id: dto.payment_id,
payment_number: dto.payment_number,
check_number: dto.check_number,
amount: dto.amount,
payee_name: dto.payee_name,
status: dto.status,
printed_at: dto.printed_at,
printed_by: dto.printed_by,
created_at: Utc::now(),
updated_at: Utc::now(),
metadata: serde_json::json!({}),
}
}
}
impl From<&PrintedCheck> for PrintedCheckResponseDto {
fn from(entity: &PrintedCheck) -> Self {
Self {
id: entity.id.clone(),
bank_account_id: entity.bank_account_id.clone(),
payment_id: entity.payment_id.clone(),
payment_number: entity.payment_number.clone(),
check_number: entity.check_number.clone(),
amount: entity.amount.clone(),
payee_name: entity.payee_name.clone(),
status: entity.status.clone(),
printed_at: entity.printed_at.clone(),
printed_by: entity.printed_by.clone(),
created_at: entity.created_at.clone(),
updated_at: entity.updated_at.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreatePrintedCheckDto> for PrintedCheck {
fn from_create_dto(dto: CreatePrintedCheckDto) -> backbone_core::ServiceResult<Self> {
Ok(PrintedCheck::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdatePrintedCheckDto> for PrintedCheck {
fn apply_update(mut self, dto: UpdatePrintedCheckDto) -> backbone_core::ServiceResult<Self> {
self.updated_at = Utc::now();
self.bank_account_id = dto.bank_account_id;
self.payment_id = dto.payment_id;
self.payment_number = dto.payment_number;
self.check_number = dto.check_number;
self.amount = dto.amount;
self.payee_name = dto.payee_name;
self.status = dto.status;
self.printed_at = dto.printed_at;
self.printed_by = dto.printed_by;
Ok(self)
}
}