use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
#[cfg(feature = "validation")]
use validator::Validate;
use crate::domain::entity::PosInvoice;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::PosInvoiceStatus;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreatePosInvoiceDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "pos_profile_id")]
pub pos_profile_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "opening_entry_id")]
pub opening_entry_id: Uuid,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "branch_id")]
pub branch_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "customer_id")]
pub customer_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "pos_table_id")]
pub pos_table_id: Option<Uuid>,
#[cfg_attr(feature = "validation", validate(length(max = 40)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "receipt_number")]
pub receipt_number: String,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "client_uuid")]
pub client_uuid: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(alias = "posting_at")]
pub posting_at: DateTime<Utc>,
#[serde(alias = "net_total")]
pub net_total: Decimal,
#[serde(alias = "tax_total")]
pub tax_total: Decimal,
#[serde(alias = "grand_total")]
pub grand_total: Decimal,
#[serde(alias = "rounding_adjustment")]
pub rounding_adjustment: Decimal,
#[serde(alias = "rounded_total")]
pub rounded_total: Decimal,
#[serde(alias = "paid_total")]
pub paid_total: Decimal,
#[serde(alias = "change_due")]
pub change_due: Decimal,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "billing_invoice_id")]
pub billing_invoice_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "payment_entry_id")]
pub payment_entry_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "is_return")]
pub is_return: bool,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "return_against")]
pub return_against: Option<Uuid>,
pub status: PosInvoiceStatus,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdatePosInvoiceDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "pos_profile_id")]
pub pos_profile_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "opening_entry_id")]
pub opening_entry_id: Uuid,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "branch_id")]
pub branch_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "customer_id")]
pub customer_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "pos_table_id")]
pub pos_table_id: Option<Uuid>,
#[cfg_attr(feature = "validation", validate(length(max = 40)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "receipt_number")]
pub receipt_number: String,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(alias = "posting_at")]
pub posting_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchPosInvoiceDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "pos_profile_id")]
pub pos_profile_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "opening_entry_id")]
pub opening_entry_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "branch_id")]
pub branch_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "customer_id")]
pub customer_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "pos_table_id")]
pub pos_table_id: Option<Uuid>,
#[cfg_attr(feature = "validation", validate(length(max = 40)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "receipt_number")]
pub receipt_number: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "posting_at")]
pub posting_at: Option<DateTime<Utc>>,
}
impl PatchPosInvoiceDto {
pub fn has_changes(&self) -> bool {
self.pos_profile_id.is_some() || self.opening_entry_id.is_some() || self.branch_id.is_some() || self.customer_id.is_some() || self.pos_table_id.is_some() || self.receipt_number.is_some() || self.posting_at.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PosInvoiceResponseDto {
#[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 pos_profile_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub opening_entry_id: Uuid,
pub branch_id: Option<Uuid>,
pub customer_id: Option<Uuid>,
pub pos_table_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub receipt_number: String,
pub client_uuid: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
pub posting_at: DateTime<Utc>,
pub net_total: Decimal,
pub tax_total: Decimal,
pub grand_total: Decimal,
pub rounding_adjustment: Decimal,
pub rounded_total: Decimal,
pub paid_total: Decimal,
pub change_due: Decimal,
pub billing_invoice_id: Option<Uuid>,
pub payment_entry_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub is_return: bool,
pub return_against: Option<Uuid>,
pub status: PosInvoiceStatus,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PosInvoiceListResponseDto {
pub items: Vec<PosInvoiceResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl PosInvoiceListResponseDto {
pub fn new(items: Vec<PosInvoiceResponseDto>, 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 PosInvoiceSummaryDto {
pub id: Uuid,
pub pos_profile_id: Uuid,
pub opening_entry_id: Uuid,
pub branch_id: Option<Uuid>,
pub created_at: Option<DateTime<Utc>>,
}
impl From<PosInvoice> for PosInvoiceResponseDto {
fn from(entity: PosInvoice) -> Self {
Self {
id: entity.id,
pos_profile_id: entity.pos_profile_id,
opening_entry_id: entity.opening_entry_id,
branch_id: entity.branch_id,
customer_id: entity.customer_id,
pos_table_id: entity.pos_table_id,
receipt_number: entity.receipt_number,
client_uuid: entity.client_uuid,
posting_at: entity.posting_at,
net_total: entity.net_total,
tax_total: entity.tax_total,
grand_total: entity.grand_total,
rounding_adjustment: entity.rounding_adjustment,
rounded_total: entity.rounded_total,
paid_total: entity.paid_total,
change_due: entity.change_due,
billing_invoice_id: entity.billing_invoice_id,
payment_entry_id: entity.payment_entry_id,
is_return: entity.is_return,
return_against: entity.return_against,
status: entity.status,
metadata: entity.metadata,
}
}
}
impl From<PosInvoice> for PosInvoiceSummaryDto {
fn from(entity: PosInvoice) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
pos_profile_id: entity.pos_profile_id,
opening_entry_id: entity.opening_entry_id,
branch_id: entity.branch_id,
created_at,
}
}
}
impl From<CreatePosInvoiceDto> for PosInvoice {
fn from(dto: CreatePosInvoiceDto) -> Self {
Self {
id: Uuid::new_v4(),
pos_profile_id: dto.pos_profile_id,
opening_entry_id: dto.opening_entry_id,
branch_id: dto.branch_id,
customer_id: dto.customer_id,
pos_table_id: dto.pos_table_id,
receipt_number: dto.receipt_number,
client_uuid: dto.client_uuid,
posting_at: dto.posting_at,
net_total: dto.net_total,
tax_total: dto.tax_total,
grand_total: dto.grand_total,
rounding_adjustment: dto.rounding_adjustment,
rounded_total: dto.rounded_total,
paid_total: dto.paid_total,
change_due: dto.change_due,
billing_invoice_id: dto.billing_invoice_id,
payment_entry_id: dto.payment_entry_id,
is_return: dto.is_return,
return_against: dto.return_against,
status: dto.status,
metadata: AuditMetadata::default(),
}
}
}
impl From<&PosInvoice> for PosInvoiceResponseDto {
fn from(entity: &PosInvoice) -> Self {
Self {
id: entity.id.clone(),
pos_profile_id: entity.pos_profile_id.clone(),
opening_entry_id: entity.opening_entry_id.clone(),
branch_id: entity.branch_id.clone(),
customer_id: entity.customer_id.clone(),
pos_table_id: entity.pos_table_id.clone(),
receipt_number: entity.receipt_number.clone(),
client_uuid: entity.client_uuid.clone(),
posting_at: entity.posting_at.clone(),
net_total: entity.net_total.clone(),
tax_total: entity.tax_total.clone(),
grand_total: entity.grand_total.clone(),
rounding_adjustment: entity.rounding_adjustment.clone(),
rounded_total: entity.rounded_total.clone(),
paid_total: entity.paid_total.clone(),
change_due: entity.change_due.clone(),
billing_invoice_id: entity.billing_invoice_id.clone(),
payment_entry_id: entity.payment_entry_id.clone(),
is_return: entity.is_return.clone(),
return_against: entity.return_against.clone(),
status: entity.status.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreatePosInvoiceDto> for PosInvoice {
fn from_create_dto(dto: CreatePosInvoiceDto) -> backbone_core::ServiceResult<Self> {
Ok(PosInvoice::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdatePosInvoiceDto> for PosInvoice {
fn apply_update(mut self, dto: UpdatePosInvoiceDto) -> backbone_core::ServiceResult<Self> {
self.pos_profile_id = dto.pos_profile_id;
self.opening_entry_id = dto.opening_entry_id;
self.branch_id = dto.branch_id;
self.customer_id = dto.customer_id;
self.pos_table_id = dto.pos_table_id;
self.receipt_number = dto.receipt_number;
self.posting_at = dto.posting_at;
Ok(self)
}
}