use anyhow::Result;
use rust_decimal::Decimal;
use sqlx::PgPool;
use uuid::Uuid;
use crate::domain::entity::StockEntry;
pub const TABLE_NAME: &str = "inventory.stock_entries";
pub struct StockEntryRepository(
backbone_orm::GenericCrudRepository<StockEntry, backbone_orm::SoftDelete>,
);
impl std::ops::Deref for StockEntryRepository {
type Target = backbone_orm::GenericCrudRepository<StockEntry, backbone_orm::SoftDelete>;
fn deref(&self) -> &Self::Target { &self.0 }
}
impl StockEntryRepository {
pub fn new(pool: PgPool) -> Self {
Self(backbone_orm::GenericCrudRepository::new(pool, TABLE_NAME))
}
}
pub struct NewTransferRow<'a> {
pub id: Uuid,
pub entry_number: &'a str,
pub from_warehouse_id: Uuid,
pub to_warehouse_id: Uuid,
pub posting_date: chrono::NaiveDate,
}
impl StockEntryRepository {
pub async fn insert_transfer(
&self,
conn: &mut sqlx::PgConnection,
t: &NewTransferRow<'_>,
) -> Result<(), sqlx::Error> {
sqlx::query(
r#"INSERT INTO inventory.stock_entries
(id, entry_number, stock_entry_type, from_warehouse_id, to_warehouse_id,
posting_date, status, posting_state)
VALUES ($1,$2,'transfer'::stock_entry_type,$3,$4,$5,'submitted'::doc_status,'not_applicable'::gl_posting_state)"#,
)
.bind(t.id).bind(t.entry_number).bind(t.from_warehouse_id)
.bind(t.to_warehouse_id).bind(t.posting_date)
.execute(conn)
.await?;
Ok(())
}
}
backbone_core::impl_crud_repository!(StockEntryRepository, StockEntry, soft_delete);