Skip to content

Commit

Permalink
Add material definitions to items as well as a means to insert/delete…
Browse files Browse the repository at this point in the history
…/update
  • Loading branch information
Wolfenheimm committed Nov 19, 2024
1 parent 047a706 commit 512d59a
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 4 deletions.
49 changes: 45 additions & 4 deletions pallets/inventory/src/blogic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::Config;
use crate::Error;
use crate::InventoryLocale;
use crate::{pallet::Pallet, types::*};
use crate::{AdjustInventory, GlobalInventory, Inventory, Recipes, ScrapInventory};
use crate::{AdjustInventory, GlobalInventory, Inventory, Materials, Recipes, ScrapInventory};
use codec::{Encode, MaxEncodedLen};
use frame_support::pallet_prelude::DispatchError;
use frame_support::sp_runtime::DispatchResult;
Expand Down Expand Up @@ -223,9 +223,7 @@ impl<T: Config> Pallet<T> {
}
}
}
AdjustDetails::Location { .. } => {
// Location adjustment does not affect quantity
}
_ => return Err(Error::<T>::InvalidAdjustDetails.into()),
}
Ok(())
} else {
Expand All @@ -250,4 +248,47 @@ impl<T: Config> Pallet<T> {

Ok(())
}

pub fn do_insert_material(material: Material) -> DispatchResult {
// Check if the material already exists
if <Materials<T>>::contains_key(&material.sku) {
return Err(Error::<T>::MaterialAlreadyExists.into());
}

// Insert the material if it does not already exist
<Materials<T>>::insert(material.sku.clone(), material);

Ok(())
}

pub fn do_delete_material(sku: Sku) -> DispatchResult {
// Check if the material exists
if !<Materials<T>>::contains_key(&sku) {
return Err(Error::<T>::MaterialNotFound.into());
}

// Remove the material
<Materials<T>>::remove(&sku);

Ok(())
}

pub fn do_update_material(material: Material) -> DispatchResult {
// Check if the material exists
<Materials<T>>::mutate_exists(
material.sku.clone(),
|existing_material| -> DispatchResult {
if let Some(ref mut current_material) = existing_material {
// Update the existing material in place
*current_material = material;
Ok(())
} else {
// Return an error if the material does not exist
Err(Error::<T>::MaterialNotFound.into())
}
},
)?;

Ok(())
}
}
70 changes: 70 additions & 0 deletions pallets/inventory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ pub mod pallet {
sender: T::AccountId,
recipe: Recipe,
},
AddMaterial {
sender: T::AccountId,
material: Material,
},
DeleteMaterial {
sender: T::AccountId,
sku: Sku,
},
UpdateMaterial {
sender: T::AccountId,
original_material: Material,
new_material: Material,
},
}

/// Global Inventory Storage
Expand Down Expand Up @@ -186,6 +199,9 @@ pub mod pallet {
#[pallet::storage]
pub type Recipes<T: Config> = StorageMap<_, Twox64Concat, Sku, Recipe>;

#[pallet::storage]
pub type Materials<T: Config> = StorageMap<_, Twox64Concat, Sku, Material>;

#[pallet::storage]
pub type InventoryLocale<T: Config> =
StorageMap<_, Twox64Concat, Location, BoundedBTreeMap<SerialNumber, Item, ConstU32<1000>>>;
Expand Down Expand Up @@ -216,6 +232,10 @@ pub mod pallet {
InventoryFull,
/// The location was not found
LocationNotFound,
/// The Material already exists
MaterialAlreadyExists,
/// The material was not found
MaterialNotFound,
}

/// The pallet's dispatchable functions ([`Call`]s).
Expand Down Expand Up @@ -345,5 +365,55 @@ pub mod pallet {

Ok(())
}

#[pallet::call_index(5)]
#[pallet::weight(T::WeightInfo::inventory_insertion())]
pub fn insert_material(origin: OriginFor<T>, material: Material) -> DispatchResult {
let who = ensure_signed(origin)?;

// Adjust the item's quantity
Self::do_insert_material(material.clone())?;

Self::deposit_event(Event::AddMaterial {
sender: who,
material,
});

Ok(())
}

#[pallet::call_index(6)]
#[pallet::weight(T::WeightInfo::inventory_insertion())]
pub fn delete_material(origin: OriginFor<T>, sku: Sku) -> DispatchResult {
let who = ensure_signed(origin)?;

// Adjust the item's quantity
Self::do_delete_material(sku.clone())?;

Self::deposit_event(Event::DeleteMaterial { sender: who, sku });

Ok(())
}

#[pallet::call_index(7)]
#[pallet::weight(T::WeightInfo::inventory_insertion())]
pub fn update_material(origin: OriginFor<T>, material: Material) -> DispatchResult {
let who = ensure_signed(origin)?;

// Fetch the original material
let original_material =
<Materials<T>>::get(&material.sku).ok_or(Error::<T>::MaterialNotFound)?;

// Adjust the item's quantity
Self::do_update_material(material.clone())?;

Self::deposit_event(Event::UpdateMaterial {
sender: who,
original_material,
new_material: material,
});

Ok(())
}
}
}
6 changes: 6 additions & 0 deletions pallets/inventory/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub struct Item {
pub sku: Sku,
pub lot_number: LotNumber,
pub serial_number: SerialNumber,
pub material: Material,
pub abc_code: AbcCode,
pub inventory_type: InventoryType,
pub product_type: ProductType,
Expand All @@ -107,6 +108,11 @@ pub struct Item {
pub location: Location,
}

#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Eq, Debug, Default)]
pub struct Material {
pub sku: Sku,
}

#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Eq, Debug)]
pub struct ScrapItem {
pub item: Item,
Expand Down

0 comments on commit 512d59a

Please sign in to comment.