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;
use crate::domain::entity::PartialReconcile;
use crate::domain::entity::ReconcileOrigin;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct CreatePartialReconcileDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "debit_move_id")]
pub debit_move_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "credit_move_id")]
pub credit_move_id: Uuid,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "full_reconcile_id")]
pub full_reconcile_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "exchange_move_id")]
pub exchange_move_id: Option<Uuid>,
pub amount: Decimal,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "debit_amount_currency")]
pub debit_amount_currency: Option<Decimal>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "credit_amount_currency")]
pub credit_amount_currency: Option<Decimal>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub currency: String,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
#[serde(alias = "max_date")]
pub max_date: NaiveDate,
pub origin: ReconcileOrigin,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "source_type")]
pub source_type: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "source_id")]
pub source_id: Option<Uuid>,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct UpdatePartialReconcileDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "debit_move_id")]
pub debit_move_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "credit_move_id")]
pub credit_move_id: Uuid,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "full_reconcile_id")]
pub full_reconcile_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "exchange_move_id")]
pub exchange_move_id: Option<Uuid>,
pub amount: Decimal,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "debit_amount_currency")]
pub debit_amount_currency: Option<Decimal>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "credit_amount_currency")]
pub credit_amount_currency: Option<Decimal>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub currency: String,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
#[serde(alias = "max_date")]
pub max_date: NaiveDate,
pub origin: ReconcileOrigin,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "source_type")]
pub source_type: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "source_id")]
pub source_id: Option<Uuid>,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PatchPartialReconcileDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "debit_move_id")]
pub debit_move_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "credit_move_id")]
pub credit_move_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "full_reconcile_id")]
pub full_reconcile_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "exchange_move_id")]
pub exchange_move_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none")]
pub amount: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none", alias = "debit_amount_currency")]
pub debit_amount_currency: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none", alias = "credit_amount_currency")]
pub credit_amount_currency: Option<Decimal>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub currency: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "max_date")]
pub max_date: Option<NaiveDate>,
#[serde(skip_serializing_if = "Option::is_none")]
pub origin: Option<ReconcileOrigin>,
#[serde(skip_serializing_if = "Option::is_none", alias = "source_type")]
pub source_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", alias = "source_id")]
pub source_id: Option<Uuid>,
}
impl PatchPartialReconcileDto {
pub fn has_changes(&self) -> bool {
self.debit_move_id.is_some() || self.credit_move_id.is_some() || self.full_reconcile_id.is_some() || self.exchange_move_id.is_some() || self.amount.is_some() || self.debit_amount_currency.is_some() || self.credit_amount_currency.is_some() || self.currency.is_some() || self.max_date.is_some() || self.origin.is_some() || self.source_type.is_some() || self.source_id.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PartialReconcileResponseDto {
#[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 debit_move_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub credit_move_id: Uuid,
pub full_reconcile_id: Option<Uuid>,
pub exchange_move_id: Option<Uuid>,
pub amount: Decimal,
pub debit_amount_currency: Option<Decimal>,
pub credit_amount_currency: Option<Decimal>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub currency: String,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
pub max_date: NaiveDate,
pub origin: ReconcileOrigin,
pub source_type: Option<String>,
pub source_id: 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: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PartialReconcileListResponseDto {
pub items: Vec<PartialReconcileResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl PartialReconcileListResponseDto {
pub fn new(items: Vec<PartialReconcileResponseDto>, 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 PartialReconcileSummaryDto {
pub id: Uuid,
pub debit_move_id: Uuid,
pub credit_move_id: Uuid,
pub full_reconcile_id: Option<Uuid>,
pub created_at: Option<DateTime<Utc>>,
}
impl From<PartialReconcile> for PartialReconcileResponseDto {
fn from(entity: PartialReconcile) -> Self {
Self {
id: entity.id,
debit_move_id: entity.debit_move_id,
credit_move_id: entity.credit_move_id,
full_reconcile_id: entity.full_reconcile_id,
exchange_move_id: entity.exchange_move_id,
amount: entity.amount,
debit_amount_currency: entity.debit_amount_currency,
credit_amount_currency: entity.credit_amount_currency,
currency: entity.currency,
max_date: entity.max_date,
origin: entity.origin,
source_type: entity.source_type,
source_id: entity.source_id,
created_at: entity.created_at,
updated_at: entity.updated_at,
metadata: entity.metadata,
}
}
}
impl From<PartialReconcile> for PartialReconcileSummaryDto {
fn from(entity: PartialReconcile) -> Self {
Self {
id: entity.id,
debit_move_id: entity.debit_move_id,
credit_move_id: entity.credit_move_id,
full_reconcile_id: entity.full_reconcile_id,
created_at: Some(entity.created_at),
}
}
}
impl From<CreatePartialReconcileDto> for PartialReconcile {
fn from(dto: CreatePartialReconcileDto) -> Self {
Self {
id: Uuid::new_v4(),
debit_move_id: dto.debit_move_id,
credit_move_id: dto.credit_move_id,
full_reconcile_id: dto.full_reconcile_id,
exchange_move_id: dto.exchange_move_id,
amount: dto.amount,
debit_amount_currency: dto.debit_amount_currency,
credit_amount_currency: dto.credit_amount_currency,
currency: dto.currency,
max_date: dto.max_date,
origin: dto.origin,
source_type: dto.source_type,
source_id: dto.source_id,
created_at: Utc::now(),
updated_at: Utc::now(),
metadata: None,
}
}
}
impl From<&PartialReconcile> for PartialReconcileResponseDto {
fn from(entity: &PartialReconcile) -> Self {
Self {
id: entity.id.clone(),
debit_move_id: entity.debit_move_id.clone(),
credit_move_id: entity.credit_move_id.clone(),
full_reconcile_id: entity.full_reconcile_id.clone(),
exchange_move_id: entity.exchange_move_id.clone(),
amount: entity.amount.clone(),
debit_amount_currency: entity.debit_amount_currency.clone(),
credit_amount_currency: entity.credit_amount_currency.clone(),
currency: entity.currency.clone(),
max_date: entity.max_date.clone(),
origin: entity.origin.clone(),
source_type: entity.source_type.clone(),
source_id: entity.source_id.clone(),
created_at: entity.created_at.clone(),
updated_at: entity.updated_at.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreatePartialReconcileDto> for PartialReconcile {
fn from_create_dto(dto: CreatePartialReconcileDto) -> backbone_core::ServiceResult<Self> {
Ok(PartialReconcile::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdatePartialReconcileDto> for PartialReconcile {
fn apply_update(mut self, dto: UpdatePartialReconcileDto) -> backbone_core::ServiceResult<Self> {
self.updated_at = Utc::now();
self.debit_move_id = dto.debit_move_id;
self.credit_move_id = dto.credit_move_id;
self.full_reconcile_id = dto.full_reconcile_id;
self.exchange_move_id = dto.exchange_move_id;
self.amount = dto.amount;
self.debit_amount_currency = dto.debit_amount_currency;
self.credit_amount_currency = dto.credit_amount_currency;
self.currency = dto.currency;
self.max_date = dto.max_date;
self.origin = dto.origin;
self.source_type = dto.source_type;
self.source_id = dto.source_id;
Ok(self)
}
}