use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use uuid::Uuid;
use rust_decimal::Decimal;
use super::PosInvoiceStatus;
use super::AuditMetadata;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct PosInvoiceId(pub Uuid);
impl PosInvoiceId {
pub fn new(id: Uuid) -> Self { Self(id) }
pub fn generate() -> Self { Self(Uuid::new_v4()) }
pub fn into_inner(self) -> Uuid { self.0 }
}
impl std::fmt::Display for PosInvoiceId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::str::FromStr for PosInvoiceId {
type Err = uuid::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(Uuid::parse_str(s)?))
}
}
impl From<Uuid> for PosInvoiceId {
fn from(id: Uuid) -> Self { Self(id) }
}
impl From<PosInvoiceId> for Uuid {
fn from(id: PosInvoiceId) -> Self { id.0 }
}
impl AsRef<Uuid> for PosInvoiceId {
fn as_ref(&self) -> &Uuid { &self.0 }
}
impl std::ops::Deref for PosInvoiceId {
type Target = Uuid;
fn deref(&self) -> &Self::Target { &self.0 }
}
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct PosInvoice {
pub id: Uuid,
pub pos_profile_id: Uuid,
pub opening_entry_id: Uuid,
pub branch_id: Option<Uuid>,
pub customer_id: Option<Uuid>,
pub pos_table_id: Option<Uuid>,
pub receipt_number: String,
pub client_uuid: Option<Uuid>,
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>,
pub is_return: bool,
pub return_against: Option<Uuid>,
pub status: PosInvoiceStatus,
#[serde(default)]
#[sqlx(json)]
pub metadata: AuditMetadata,
}
impl PosInvoice {
pub fn builder() -> PosInvoiceBuilder {
<PosInvoiceBuilder as Default>::default()
}
pub fn new(pos_profile_id: Uuid, opening_entry_id: Uuid, receipt_number: String, posting_at: DateTime<Utc>, net_total: Decimal, tax_total: Decimal, grand_total: Decimal, rounding_adjustment: Decimal, rounded_total: Decimal, paid_total: Decimal, change_due: Decimal, is_return: bool, status: PosInvoiceStatus) -> Self {
Self {
id: Uuid::new_v4(),
pos_profile_id,
opening_entry_id,
branch_id: None,
customer_id: None,
pos_table_id: None,
receipt_number,
client_uuid: None,
posting_at,
net_total,
tax_total,
grand_total,
rounding_adjustment,
rounded_total,
paid_total,
change_due,
billing_invoice_id: None,
payment_entry_id: None,
is_return,
return_against: None,
status,
metadata: AuditMetadata::default(),
}
}
pub fn id(&self) -> &Uuid {
&self.id
}
pub fn typed_id(&self) -> PosInvoiceId {
PosInvoiceId(self.id)
}
pub fn created_at(&self) -> Option<&DateTime<Utc>> {
self.metadata.created_at.as_ref()
}
pub fn updated_at(&self) -> Option<&DateTime<Utc>> {
self.metadata.updated_at.as_ref()
}
pub fn is_deleted(&self) -> bool {
self.metadata.deleted_at.is_some()
}
pub fn is_active(&self) -> bool {
self.metadata.deleted_at.is_none()
}
pub fn deleted_at(&self) -> Option<&DateTime<Utc>> {
self.metadata.deleted_at.as_ref()
}
pub fn created_by(&self) -> Option<&Uuid> {
self.metadata.created_by.as_ref()
}
pub fn updated_by(&self) -> Option<&Uuid> {
self.metadata.updated_by.as_ref()
}
pub fn deleted_by(&self) -> Option<&Uuid> {
self.metadata.deleted_by.as_ref()
}
pub fn status(&self) -> &PosInvoiceStatus {
&self.status
}
pub fn with_branch_id(mut self, value: Uuid) -> Self {
self.branch_id = Some(value);
self
}
pub fn with_customer_id(mut self, value: Uuid) -> Self {
self.customer_id = Some(value);
self
}
pub fn with_pos_table_id(mut self, value: Uuid) -> Self {
self.pos_table_id = Some(value);
self
}
pub fn with_client_uuid(mut self, value: Uuid) -> Self {
self.client_uuid = Some(value);
self
}
pub fn with_billing_invoice_id(mut self, value: Uuid) -> Self {
self.billing_invoice_id = Some(value);
self
}
pub fn with_payment_entry_id(mut self, value: Uuid) -> Self {
self.payment_entry_id = Some(value);
self
}
pub fn with_return_against(mut self, value: Uuid) -> Self {
self.return_against = Some(value);
self
}
pub fn apply_patch(&mut self, fields: std::collections::HashMap<String, serde_json::Value>) {
for (key, value) in fields {
match key.as_str() {
"pos_profile_id" => {
if let Ok(v) = serde_json::from_value(value) { self.pos_profile_id = v; }
}
"opening_entry_id" => {
if let Ok(v) = serde_json::from_value(value) { self.opening_entry_id = v; }
}
"branch_id" => {
if let Ok(v) = serde_json::from_value(value) { self.branch_id = v; }
}
"customer_id" => {
if let Ok(v) = serde_json::from_value(value) { self.customer_id = v; }
}
"pos_table_id" => {
if let Ok(v) = serde_json::from_value(value) { self.pos_table_id = v; }
}
"receipt_number" => {
if let Ok(v) = serde_json::from_value(value) { self.receipt_number = v; }
}
"client_uuid" => {
if let Ok(v) = serde_json::from_value(value) { self.client_uuid = v; }
}
"posting_at" => {
if let Ok(v) = serde_json::from_value(value) { self.posting_at = v; }
}
"net_total" => {
if let Ok(v) = serde_json::from_value(value) { self.net_total = v; }
}
"tax_total" => {
if let Ok(v) = serde_json::from_value(value) { self.tax_total = v; }
}
"grand_total" => {
if let Ok(v) = serde_json::from_value(value) { self.grand_total = v; }
}
"rounding_adjustment" => {
if let Ok(v) = serde_json::from_value(value) { self.rounding_adjustment = v; }
}
"rounded_total" => {
if let Ok(v) = serde_json::from_value(value) { self.rounded_total = v; }
}
"paid_total" => {
if let Ok(v) = serde_json::from_value(value) { self.paid_total = v; }
}
"change_due" => {
if let Ok(v) = serde_json::from_value(value) { self.change_due = v; }
}
"billing_invoice_id" => {
if let Ok(v) = serde_json::from_value(value) { self.billing_invoice_id = v; }
}
"payment_entry_id" => {
if let Ok(v) = serde_json::from_value(value) { self.payment_entry_id = v; }
}
"is_return" => {
if let Ok(v) = serde_json::from_value(value) { self.is_return = v; }
}
"return_against" => {
if let Ok(v) = serde_json::from_value(value) { self.return_against = v; }
}
"status" => {
if let Ok(v) = serde_json::from_value(value) { self.status = v; }
}
_ => {} }
}
}
}
impl super::Entity for PosInvoice {
type Id = Uuid;
fn entity_id(&self) -> &Self::Id {
&self.id
}
fn entity_type() -> &'static str {
"PosInvoice"
}
}
impl backbone_core::PersistentEntity for PosInvoice {
fn write_protected_fields() -> &'static [&'static str] {
&["client_uuid", "net_total", "tax_total", "grand_total", "rounding_adjustment", "rounded_total", "paid_total", "change_due", "billing_invoice_id", "payment_entry_id", "is_return", "return_against", "status"]
}
fn entity_id(&self) -> String {
self.id.to_string()
}
fn set_entity_id(&mut self, id: String) {
if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
self.id = uuid;
}
}
fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
self.metadata.created_at
}
fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
self.metadata.created_at = Some(ts);
}
fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
self.metadata.updated_at
}
fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
self.metadata.updated_at = Some(ts);
}
fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
self.metadata.deleted_at
}
fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
self.metadata.deleted_at = ts;
}
}
impl backbone_orm::EntityRepoMeta for PosInvoice {
fn column_types() -> std::collections::HashMap<String, String> {
let mut m = std::collections::HashMap::new();
m.insert("id".to_string(), "uuid".to_string());
m.insert("pos_profile_id".to_string(), "uuid".to_string());
m.insert("opening_entry_id".to_string(), "uuid".to_string());
m.insert("branch_id".to_string(), "uuid".to_string());
m.insert("customer_id".to_string(), "uuid".to_string());
m.insert("pos_table_id".to_string(), "uuid".to_string());
m.insert("billing_invoice_id".to_string(), "uuid".to_string());
m.insert("payment_entry_id".to_string(), "uuid".to_string());
m.insert("status".to_string(), "pos_invoice_status".to_string());
m.insert("posting_at".to_string(), "timestamptz".to_string());
m
}
fn search_fields() -> &'static [&'static str] {
&["receipt_number"]
}
}
#[derive(Debug, Clone, Default)]
pub struct PosInvoiceBuilder {
pos_profile_id: Option<Uuid>,
opening_entry_id: Option<Uuid>,
branch_id: Option<Uuid>,
customer_id: Option<Uuid>,
pos_table_id: Option<Uuid>,
receipt_number: Option<String>,
client_uuid: Option<Uuid>,
posting_at: Option<DateTime<Utc>>,
net_total: Option<Decimal>,
tax_total: Option<Decimal>,
grand_total: Option<Decimal>,
rounding_adjustment: Option<Decimal>,
rounded_total: Option<Decimal>,
paid_total: Option<Decimal>,
change_due: Option<Decimal>,
billing_invoice_id: Option<Uuid>,
payment_entry_id: Option<Uuid>,
is_return: Option<bool>,
return_against: Option<Uuid>,
status: Option<PosInvoiceStatus>,
}
impl PosInvoiceBuilder {
pub fn pos_profile_id(mut self, value: Uuid) -> Self {
self.pos_profile_id = Some(value);
self
}
pub fn opening_entry_id(mut self, value: Uuid) -> Self {
self.opening_entry_id = Some(value);
self
}
pub fn branch_id(mut self, value: Uuid) -> Self {
self.branch_id = Some(value);
self
}
pub fn customer_id(mut self, value: Uuid) -> Self {
self.customer_id = Some(value);
self
}
pub fn pos_table_id(mut self, value: Uuid) -> Self {
self.pos_table_id = Some(value);
self
}
pub fn receipt_number(mut self, value: String) -> Self {
self.receipt_number = Some(value);
self
}
pub fn client_uuid(mut self, value: Uuid) -> Self {
self.client_uuid = Some(value);
self
}
pub fn posting_at(mut self, value: DateTime<Utc>) -> Self {
self.posting_at = Some(value);
self
}
pub fn net_total(mut self, value: Decimal) -> Self {
self.net_total = Some(value);
self
}
pub fn tax_total(mut self, value: Decimal) -> Self {
self.tax_total = Some(value);
self
}
pub fn grand_total(mut self, value: Decimal) -> Self {
self.grand_total = Some(value);
self
}
pub fn rounding_adjustment(mut self, value: Decimal) -> Self {
self.rounding_adjustment = Some(value);
self
}
pub fn rounded_total(mut self, value: Decimal) -> Self {
self.rounded_total = Some(value);
self
}
pub fn paid_total(mut self, value: Decimal) -> Self {
self.paid_total = Some(value);
self
}
pub fn change_due(mut self, value: Decimal) -> Self {
self.change_due = Some(value);
self
}
pub fn billing_invoice_id(mut self, value: Uuid) -> Self {
self.billing_invoice_id = Some(value);
self
}
pub fn payment_entry_id(mut self, value: Uuid) -> Self {
self.payment_entry_id = Some(value);
self
}
pub fn is_return(mut self, value: bool) -> Self {
self.is_return = Some(value);
self
}
pub fn return_against(mut self, value: Uuid) -> Self {
self.return_against = Some(value);
self
}
pub fn status(mut self, value: PosInvoiceStatus) -> Self {
self.status = Some(value);
self
}
pub fn build(self) -> Result<PosInvoice, String> {
let pos_profile_id = self.pos_profile_id.ok_or_else(|| "pos_profile_id is required".to_string())?;
let opening_entry_id = self.opening_entry_id.ok_or_else(|| "opening_entry_id is required".to_string())?;
let receipt_number = self.receipt_number.ok_or_else(|| "receipt_number is required".to_string())?;
let posting_at = self.posting_at.ok_or_else(|| "posting_at is required".to_string())?;
Ok(PosInvoice {
id: Uuid::new_v4(),
pos_profile_id,
opening_entry_id,
branch_id: self.branch_id,
customer_id: self.customer_id,
pos_table_id: self.pos_table_id,
receipt_number,
client_uuid: self.client_uuid,
posting_at,
net_total: self.net_total.unwrap_or(Decimal::from(0)),
tax_total: self.tax_total.unwrap_or(Decimal::from(0)),
grand_total: self.grand_total.unwrap_or(Decimal::from(0)),
rounding_adjustment: self.rounding_adjustment.unwrap_or(Decimal::from(0)),
rounded_total: self.rounded_total.unwrap_or(Decimal::from(0)),
paid_total: self.paid_total.unwrap_or(Decimal::from(0)),
change_due: self.change_due.unwrap_or(Decimal::from(0)),
billing_invoice_id: self.billing_invoice_id,
payment_entry_id: self.payment_entry_id,
is_return: self.is_return.unwrap_or(false),
return_against: self.return_against,
status: self.status.unwrap_or_default(),
metadata: AuditMetadata::default(),
})
}
}