use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};
#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
#[cfg(feature = "validation")]
use validator::Validate;
use crate::domain::entity::BankAccount;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::BankAccountStatus;
use crate::domain::entity::BankAccountType;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreateBankAccountDto {
#[serde(default, skip_serializing_if = "Option::is_none", alias = "branch_id")]
pub branch_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "bank_id")]
pub bank_id: Uuid,
#[cfg_attr(feature = "validation", validate(length(max = 140)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "account_name")]
pub account_name: String,
#[cfg_attr(feature = "validation", validate(length(max = 60)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "account_number")]
pub account_number: String,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "gl_account_id")]
pub gl_account_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "clearing_account_id")]
pub clearing_account_id: Uuid,
#[cfg_attr(feature = "validation", validate(length(max = 3)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub currency: String,
#[serde(alias = "account_type")]
pub account_type: BankAccountType,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "is_default")]
pub is_default: bool,
pub status: BankAccountStatus,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateBankAccountDto {
#[serde(default, skip_serializing_if = "Option::is_none", alias = "branch_id")]
pub branch_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "bank_id")]
pub bank_id: Uuid,
#[cfg_attr(feature = "validation", validate(length(max = 140)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "account_name")]
pub account_name: String,
#[cfg_attr(feature = "validation", validate(length(max = 60)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "account_number")]
pub account_number: String,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "gl_account_id")]
pub gl_account_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "clearing_account_id")]
pub clearing_account_id: Uuid,
#[cfg_attr(feature = "validation", validate(length(max = 3)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub currency: String,
#[serde(alias = "account_type")]
pub account_type: BankAccountType,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "is_default")]
pub is_default: bool,
pub status: BankAccountStatus,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchBankAccountDto {
#[serde(skip_serializing_if = "Option::is_none", alias = "branch_id")]
pub branch_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "bank_id")]
pub bank_id: Option<Uuid>,
#[cfg_attr(feature = "validation", validate(length(max = 140)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "account_name")]
pub account_name: Option<String>,
#[cfg_attr(feature = "validation", validate(length(max = 60)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "account_number")]
pub account_number: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "gl_account_id")]
pub gl_account_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "clearing_account_id")]
pub clearing_account_id: Option<Uuid>,
#[cfg_attr(feature = "validation", validate(length(max = 3)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub currency: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", alias = "account_type")]
pub account_type: Option<BankAccountType>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(skip_serializing_if = "Option::is_none", alias = "is_default")]
pub is_default: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<BankAccountStatus>,
}
impl PatchBankAccountDto {
pub fn has_changes(&self) -> bool {
self.branch_id.is_some() || self.bank_id.is_some() || self.account_name.is_some() || self.account_number.is_some() || self.gl_account_id.is_some() || self.clearing_account_id.is_some() || self.currency.is_some() || self.account_type.is_some() || self.is_default.is_some() || self.status.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct BankAccountResponseDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub id: Uuid,
pub branch_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub bank_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub account_name: String,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub account_number: String,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub gl_account_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub clearing_account_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub currency: String,
pub account_type: BankAccountType,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub is_default: bool,
pub status: BankAccountStatus,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct BankAccountListResponseDto {
pub items: Vec<BankAccountResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl BankAccountListResponseDto {
pub fn new(items: Vec<BankAccountResponseDto>, 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 BankAccountSummaryDto {
pub id: Uuid,
pub branch_id: Option<Uuid>,
pub bank_id: Uuid,
pub account_name: String,
pub created_at: Option<DateTime<Utc>>,
}
impl From<BankAccount> for BankAccountResponseDto {
fn from(entity: BankAccount) -> Self {
Self {
id: entity.id,
branch_id: entity.branch_id,
bank_id: entity.bank_id,
account_name: entity.account_name,
account_number: entity.account_number,
gl_account_id: entity.gl_account_id,
clearing_account_id: entity.clearing_account_id,
currency: entity.currency,
account_type: entity.account_type,
is_default: entity.is_default,
status: entity.status,
metadata: entity.metadata,
}
}
}
impl From<BankAccount> for BankAccountSummaryDto {
fn from(entity: BankAccount) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
branch_id: entity.branch_id,
bank_id: entity.bank_id,
account_name: entity.account_name,
created_at,
}
}
}
impl From<CreateBankAccountDto> for BankAccount {
fn from(dto: CreateBankAccountDto) -> Self {
Self {
id: Uuid::new_v4(),
branch_id: dto.branch_id,
bank_id: dto.bank_id,
account_name: dto.account_name,
account_number: dto.account_number,
gl_account_id: dto.gl_account_id,
clearing_account_id: dto.clearing_account_id,
currency: dto.currency,
account_type: dto.account_type,
is_default: dto.is_default,
status: dto.status,
metadata: AuditMetadata::default(),
}
}
}
impl From<&BankAccount> for BankAccountResponseDto {
fn from(entity: &BankAccount) -> Self {
Self {
id: entity.id.clone(),
branch_id: entity.branch_id.clone(),
bank_id: entity.bank_id.clone(),
account_name: entity.account_name.clone(),
account_number: entity.account_number.clone(),
gl_account_id: entity.gl_account_id.clone(),
clearing_account_id: entity.clearing_account_id.clone(),
currency: entity.currency.clone(),
account_type: entity.account_type.clone(),
is_default: entity.is_default.clone(),
status: entity.status.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateBankAccountDto> for BankAccount {
fn from_create_dto(dto: CreateBankAccountDto) -> backbone_core::ServiceResult<Self> {
Ok(BankAccount::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateBankAccountDto> for BankAccount {
fn apply_update(mut self, dto: UpdateBankAccountDto) -> backbone_core::ServiceResult<Self> {
self.branch_id = dto.branch_id;
self.bank_id = dto.bank_id;
self.account_name = dto.account_name;
self.account_number = dto.account_number;
self.gl_account_id = dto.gl_account_id;
self.clearing_account_id = dto.clearing_account_id;
self.currency = dto.currency;
self.account_type = dto.account_type;
self.is_default = dto.is_default;
self.status = dto.status;
Ok(self)
}
}