use async_trait::async_trait;
use anyhow::Result;
use uuid::Uuid;
use crate::domain::entity::{PurchaseReceipt, DocStatus, GlPostingState};
#[derive(Debug, Clone, Default)]
pub struct PurchaseReceiptPaginationParams {
pub page: u32,
pub per_page: u32,
}
impl PurchaseReceiptPaginationParams {
pub fn new(page: u32, per_page: u32) -> Self {
Self { page, per_page }
}
pub fn offset(&self) -> u64 {
((self.page.saturating_sub(1)) * self.per_page) as u64
}
pub fn limit(&self) -> u64 {
self.per_page as u64
}
}
#[derive(Debug, Clone)]
pub struct PurchaseReceiptPaginatedResult {
pub data: Vec<PurchaseReceipt>,
pub total: u64,
pub page: u32,
pub per_page: u32,
}
#[derive(Debug, Clone, Default)]
pub struct PurchaseReceiptFilter {
pub receipt_number: Option<String>,
pub branch_id: Option<Uuid>,
pub supplier_id: Option<Uuid>,
pub source_po_id: Option<Uuid>,
pub warehouse_id: Option<Uuid>,
pub currency: Option<String>,
pub inventory_account_id: Option<Uuid>,
pub grir_account_id: Option<Uuid>,
pub transfer_id: Option<Uuid>,
pub status: Option<DocStatus>,
pub posting_state: Option<GlPostingState>,
pub journal_id: Option<Uuid>,
pub accounting_post_id: Option<Uuid>,
pub notes: Option<String>,
}
impl PurchaseReceiptFilter {
pub fn has_filters(&self) -> bool {
self.receipt_number.is_some() || self.branch_id.is_some() || self.supplier_id.is_some() || self.source_po_id.is_some() || self.warehouse_id.is_some() || self.currency.is_some() || self.inventory_account_id.is_some() || self.grir_account_id.is_some() || self.transfer_id.is_some() || self.status.is_some() || self.posting_state.is_some() || self.journal_id.is_some() || self.accounting_post_id.is_some() || self.notes.is_some()
}
}
#[async_trait]
pub trait PurchaseReceiptRepository: Send + Sync {
async fn save(&self, entity: &PurchaseReceipt) -> Result<PurchaseReceipt>;
async fn find_by_id(&self, id: &str) -> Result<Option<PurchaseReceipt>>;
async fn find_all(&self) -> Result<Vec<PurchaseReceipt>>;
async fn update(&self, id: &str, entity: &PurchaseReceipt) -> Result<Option<PurchaseReceipt>>;
async fn delete(&self, id: &str) -> Result<bool>;
async fn list(&self, params: PurchaseReceiptPaginationParams) -> Result<PurchaseReceiptPaginatedResult>;
async fn list_with_filters(&self, params: PurchaseReceiptPaginationParams, filters: PurchaseReceiptFilter) -> Result<PurchaseReceiptPaginatedResult>;
async fn count(&self) -> Result<u64>;
async fn count_with_filters(&self, filters: PurchaseReceiptFilter) -> Result<u64>;
async fn exists(&self, id: &str) -> Result<bool>;
async fn soft_delete(&self, id: &str) -> Result<bool>;
async fn restore(&self, id: &str) -> Result<Option<PurchaseReceipt>>;
async fn list_deleted(&self, params: PurchaseReceiptPaginationParams) -> Result<PurchaseReceiptPaginatedResult>;
async fn empty_trash(&self) -> Result<u64>;
async fn bulk_save(&self, entities: &[PurchaseReceipt]) -> Result<Vec<PurchaseReceipt>>;
async fn bulk_delete(&self, ids: &[&str]) -> Result<u64>;
}