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::StockLedgerEntry;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::StockLedgerStatus;
use crate::domain::entity::VoucherType;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct CreateStockLedgerEntryDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "item_id")]
pub item_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "warehouse_id")]
pub warehouse_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
#[serde(alias = "posting_date")]
pub posting_date: NaiveDate,
#[serde(alias = "actual_qty")]
pub actual_qty: Decimal,
#[serde(alias = "qty_after_txn")]
pub qty_after_txn: Decimal,
#[serde(alias = "incoming_rate")]
pub incoming_rate: Decimal,
#[serde(alias = "valuation_rate")]
pub valuation_rate: Decimal,
#[serde(alias = "stock_value")]
pub stock_value: Decimal,
#[serde(alias = "stock_value_difference")]
pub stock_value_difference: Decimal,
#[serde(alias = "voucher_type")]
pub voucher_type: VoucherType,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "voucher_id")]
pub voucher_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "voucher_no")]
pub voucher_no: String,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "sle_no")]
pub sle_no: i32,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct UpdateStockLedgerEntryDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "item_id")]
pub item_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "warehouse_id")]
pub warehouse_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
#[serde(alias = "posting_date")]
pub posting_date: NaiveDate,
#[serde(alias = "actual_qty")]
pub actual_qty: Decimal,
#[serde(alias = "incoming_rate")]
pub incoming_rate: Decimal,
#[serde(alias = "voucher_type")]
pub voucher_type: VoucherType,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "voucher_id")]
pub voucher_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "voucher_no")]
pub voucher_no: String,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "sle_no")]
pub sle_no: i32,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PatchStockLedgerEntryDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "item_id")]
pub item_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "warehouse_id")]
pub warehouse_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "posting_date")]
pub posting_date: Option<NaiveDate>,
#[serde(skip_serializing_if = "Option::is_none", alias = "actual_qty")]
pub actual_qty: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none", alias = "incoming_rate")]
pub incoming_rate: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none", alias = "voucher_type")]
pub voucher_type: Option<VoucherType>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "voucher_id")]
pub voucher_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "voucher_no")]
pub voucher_no: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "sle_no")]
pub sle_no: Option<i32>,
}
impl PatchStockLedgerEntryDto {
pub fn has_changes(&self) -> bool {
self.item_id.is_some() || self.warehouse_id.is_some() || self.posting_date.is_some() || self.actual_qty.is_some() || self.incoming_rate.is_some() || self.voucher_type.is_some() || self.voucher_id.is_some() || self.voucher_no.is_some() || self.sle_no.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct StockLedgerEntryResponseDto {
#[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 item_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub warehouse_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
pub posting_date: NaiveDate,
pub actual_qty: Decimal,
pub qty_after_txn: Decimal,
pub incoming_rate: Decimal,
pub valuation_rate: Decimal,
pub stock_value: Decimal,
pub stock_value_difference: Decimal,
pub voucher_type: VoucherType,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub voucher_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub voucher_no: String,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub sle_no: i32,
pub status: StockLedgerStatus,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct StockLedgerEntryListResponseDto {
pub items: Vec<StockLedgerEntryResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl StockLedgerEntryListResponseDto {
pub fn new(items: Vec<StockLedgerEntryResponseDto>, 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 StockLedgerEntrySummaryDto {
pub id: Uuid,
pub item_id: Uuid,
pub warehouse_id: Uuid,
pub posting_date: NaiveDate,
pub created_at: Option<DateTime<Utc>>,
}
impl From<StockLedgerEntry> for StockLedgerEntryResponseDto {
fn from(entity: StockLedgerEntry) -> Self {
Self {
id: entity.id,
item_id: entity.item_id,
warehouse_id: entity.warehouse_id,
posting_date: entity.posting_date,
actual_qty: entity.actual_qty,
qty_after_txn: entity.qty_after_txn,
incoming_rate: entity.incoming_rate,
valuation_rate: entity.valuation_rate,
stock_value: entity.stock_value,
stock_value_difference: entity.stock_value_difference,
voucher_type: entity.voucher_type,
voucher_id: entity.voucher_id,
voucher_no: entity.voucher_no,
sle_no: entity.sle_no,
status: entity.status,
metadata: entity.metadata,
}
}
}
impl From<StockLedgerEntry> for StockLedgerEntrySummaryDto {
fn from(entity: StockLedgerEntry) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
item_id: entity.item_id,
warehouse_id: entity.warehouse_id,
posting_date: entity.posting_date,
created_at,
}
}
}
impl From<CreateStockLedgerEntryDto> for StockLedgerEntry {
fn from(dto: CreateStockLedgerEntryDto) -> Self {
Self {
id: Uuid::new_v4(),
item_id: dto.item_id,
warehouse_id: dto.warehouse_id,
posting_date: dto.posting_date,
actual_qty: dto.actual_qty,
qty_after_txn: dto.qty_after_txn,
incoming_rate: dto.incoming_rate,
valuation_rate: dto.valuation_rate,
stock_value: dto.stock_value,
stock_value_difference: dto.stock_value_difference,
voucher_type: dto.voucher_type,
voucher_id: dto.voucher_id,
voucher_no: dto.voucher_no,
sle_no: dto.sle_no,
status: StockLedgerStatus::Active,
metadata: AuditMetadata::default(),
}
}
}
impl From<&StockLedgerEntry> for StockLedgerEntryResponseDto {
fn from(entity: &StockLedgerEntry) -> Self {
Self {
id: entity.id.clone(),
item_id: entity.item_id.clone(),
warehouse_id: entity.warehouse_id.clone(),
posting_date: entity.posting_date.clone(),
actual_qty: entity.actual_qty.clone(),
qty_after_txn: entity.qty_after_txn.clone(),
incoming_rate: entity.incoming_rate.clone(),
valuation_rate: entity.valuation_rate.clone(),
stock_value: entity.stock_value.clone(),
stock_value_difference: entity.stock_value_difference.clone(),
voucher_type: entity.voucher_type.clone(),
voucher_id: entity.voucher_id.clone(),
voucher_no: entity.voucher_no.clone(),
sle_no: entity.sle_no.clone(),
status: entity.status.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateStockLedgerEntryDto> for StockLedgerEntry {
fn from_create_dto(dto: CreateStockLedgerEntryDto) -> backbone_core::ServiceResult<Self> {
Ok(StockLedgerEntry::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateStockLedgerEntryDto> for StockLedgerEntry {
fn apply_update(mut self, dto: UpdateStockLedgerEntryDto) -> backbone_core::ServiceResult<Self> {
self.item_id = dto.item_id;
self.warehouse_id = dto.warehouse_id;
self.posting_date = dto.posting_date;
self.actual_qty = dto.actual_qty;
self.incoming_rate = dto.incoming_rate;
self.voucher_type = dto.voucher_type;
self.voucher_id = dto.voucher_id;
self.voucher_no = dto.voucher_no;
self.sle_no = dto.sle_no;
Ok(self)
}
}