use anyhow::Result;
use rust_decimal::Decimal;
use sqlx::{PgPool, Row};
use uuid::Uuid;
use crate::domain::entity::PosClosingEntry;
pub const TABLE_NAME: &str = "pos.pos_closing_entries";
pub struct PosClosingEntryRepository(
backbone_orm::GenericCrudRepository<PosClosingEntry, backbone_orm::SoftDelete>,
);
impl std::ops::Deref for PosClosingEntryRepository {
type Target = backbone_orm::GenericCrudRepository<PosClosingEntry, backbone_orm::SoftDelete>;
fn deref(&self) -> &Self::Target { &self.0 }
}
impl PosClosingEntryRepository {
pub fn new(pool: PgPool) -> Self {
Self(backbone_orm::GenericCrudRepository::new(pool, TABLE_NAME))
}
}
pub struct NewClosingEntryRow {
pub id: Uuid,
pub pos_profile_id: Uuid,
pub opening_entry_id: Uuid,
pub closed_at: chrono::NaiveDateTime,
pub cashier_party_id: Uuid,
pub totals_by_method: serde_json::Value,
pub grand_total: Decimal,
pub invoice_count: i32,
pub difference_total: Decimal,
}
impl PosClosingEntryRepository {
pub async fn insert_closing_entry(
&self,
conn: &mut sqlx::PgConnection,
c: &NewClosingEntryRow,
) -> Result<(), sqlx::Error> {
sqlx::query(
r#"INSERT INTO pos.pos_closing_entries
(id, pos_profile_id, opening_entry_id, closed_at, cashier_party_id,
totals_by_method, grand_total, invoice_count, difference_total, status)
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,'submitted'::pos_closing_status)"#,
)
.bind(c.id).bind(c.pos_profile_id).bind(c.opening_entry_id).bind(c.closed_at)
.bind(c.cashier_party_id).bind(sqlx::types::Json(&c.totals_by_method)).bind(c.grand_total)
.bind(c.invoice_count).bind(c.difference_total)
.execute(conn)
.await?;
Ok(())
}
}
backbone_core::impl_crud_repository!(PosClosingEntryRepository, PosClosingEntry, soft_delete);