use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use uuid::Uuid;
use rust_decimal::Decimal;
use super::ValuationMethod;
use super::ServiceTrackingType;
use super::AuditMetadata;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct StockItemId(pub Uuid);
impl StockItemId {
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 StockItemId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::str::FromStr for StockItemId {
type Err = uuid::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(Uuid::parse_str(s)?))
}
}
impl From<Uuid> for StockItemId {
fn from(id: Uuid) -> Self { Self(id) }
}
impl From<StockItemId> for Uuid {
fn from(id: StockItemId) -> Self { id.0 }
}
impl AsRef<Uuid> for StockItemId {
fn as_ref(&self) -> &Uuid { &self.0 }
}
impl std::ops::Deref for StockItemId {
type Target = Uuid;
fn deref(&self) -> &Self::Target { &self.0 }
}
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct StockItem {
pub id: Uuid,
pub item_id: Uuid,
pub stock_uom: String,
pub is_stock_item: bool,
pub has_batch: bool,
pub valuation_method: ValuationMethod,
pub reorder_level: Decimal,
pub weight_per_unit: Decimal,
pub service_tracking: ServiceTrackingType,
pub service_project_id: Option<Uuid>,
pub service_project_template_id: Option<Uuid>,
#[serde(default)]
#[sqlx(json)]
pub metadata: AuditMetadata,
}
impl StockItem {
pub fn builder() -> StockItemBuilder {
<StockItemBuilder as Default>::default()
}
pub fn new(item_id: Uuid, stock_uom: String, is_stock_item: bool, has_batch: bool, valuation_method: ValuationMethod, reorder_level: Decimal, weight_per_unit: Decimal, service_tracking: ServiceTrackingType) -> Self {
Self {
id: Uuid::new_v4(),
item_id,
stock_uom,
is_stock_item,
has_batch,
valuation_method,
reorder_level,
weight_per_unit,
service_tracking,
service_project_id: None,
service_project_template_id: None,
metadata: AuditMetadata::default(),
}
}
pub fn id(&self) -> &Uuid {
&self.id
}
pub fn typed_id(&self) -> StockItemId {
StockItemId(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 with_service_project_id(mut self, value: Uuid) -> Self {
self.service_project_id = Some(value);
self
}
pub fn with_service_project_template_id(mut self, value: Uuid) -> Self {
self.service_project_template_id = 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() {
"item_id" => {
if let Ok(v) = serde_json::from_value(value) { self.item_id = v; }
}
"stock_uom" => {
if let Ok(v) = serde_json::from_value(value) { self.stock_uom = v; }
}
"is_stock_item" => {
if let Ok(v) = serde_json::from_value(value) { self.is_stock_item = v; }
}
"has_batch" => {
if let Ok(v) = serde_json::from_value(value) { self.has_batch = v; }
}
"valuation_method" => {
if let Ok(v) = serde_json::from_value(value) { self.valuation_method = v; }
}
"reorder_level" => {
if let Ok(v) = serde_json::from_value(value) { self.reorder_level = v; }
}
"weight_per_unit" => {
if let Ok(v) = serde_json::from_value(value) { self.weight_per_unit = v; }
}
"service_tracking" => {
if let Ok(v) = serde_json::from_value(value) { self.service_tracking = v; }
}
"service_project_id" => {
if let Ok(v) = serde_json::from_value(value) { self.service_project_id = v; }
}
"service_project_template_id" => {
if let Ok(v) = serde_json::from_value(value) { self.service_project_template_id = v; }
}
_ => {} }
}
}
}
impl super::Entity for StockItem {
type Id = Uuid;
fn entity_id(&self) -> &Self::Id {
&self.id
}
fn entity_type() -> &'static str {
"StockItem"
}
}
impl backbone_core::PersistentEntity for StockItem {
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 StockItem {
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("item_id".to_string(), "uuid".to_string());
m.insert("service_project_id".to_string(), "uuid".to_string());
m.insert("service_project_template_id".to_string(), "uuid".to_string());
m.insert("valuation_method".to_string(), "valuation_method".to_string());
m.insert("service_tracking".to_string(), "service_tracking_type".to_string());
m
}
fn search_fields() -> &'static [&'static str] {
&["stock_uom"]
}
}
#[derive(Debug, Clone, Default)]
pub struct StockItemBuilder {
item_id: Option<Uuid>,
stock_uom: Option<String>,
is_stock_item: Option<bool>,
has_batch: Option<bool>,
valuation_method: Option<ValuationMethod>,
reorder_level: Option<Decimal>,
weight_per_unit: Option<Decimal>,
service_tracking: Option<ServiceTrackingType>,
service_project_id: Option<Uuid>,
service_project_template_id: Option<Uuid>,
}
impl StockItemBuilder {
pub fn item_id(mut self, value: Uuid) -> Self {
self.item_id = Some(value);
self
}
pub fn stock_uom(mut self, value: String) -> Self {
self.stock_uom = Some(value);
self
}
pub fn is_stock_item(mut self, value: bool) -> Self {
self.is_stock_item = Some(value);
self
}
pub fn has_batch(mut self, value: bool) -> Self {
self.has_batch = Some(value);
self
}
pub fn valuation_method(mut self, value: ValuationMethod) -> Self {
self.valuation_method = Some(value);
self
}
pub fn reorder_level(mut self, value: Decimal) -> Self {
self.reorder_level = Some(value);
self
}
pub fn weight_per_unit(mut self, value: Decimal) -> Self {
self.weight_per_unit = Some(value);
self
}
pub fn service_tracking(mut self, value: ServiceTrackingType) -> Self {
self.service_tracking = Some(value);
self
}
pub fn service_project_id(mut self, value: Uuid) -> Self {
self.service_project_id = Some(value);
self
}
pub fn service_project_template_id(mut self, value: Uuid) -> Self {
self.service_project_template_id = Some(value);
self
}
pub fn build(self) -> Result<StockItem, String> {
let item_id = self.item_id.ok_or_else(|| "item_id is required".to_string())?;
let stock_uom = self.stock_uom.ok_or_else(|| "stock_uom is required".to_string())?;
Ok(StockItem {
id: Uuid::new_v4(),
item_id,
stock_uom,
is_stock_item: self.is_stock_item.unwrap_or(true),
has_batch: self.has_batch.unwrap_or(false),
valuation_method: self.valuation_method.unwrap_or_default(),
reorder_level: self.reorder_level.unwrap_or(Decimal::from(0)),
weight_per_unit: self.weight_per_unit.unwrap_or(Decimal::from(0)),
service_tracking: self.service_tracking.unwrap_or_default(),
service_project_id: self.service_project_id,
service_project_template_id: self.service_project_template_id,
metadata: AuditMetadata::default(),
})
}
}