Skip to content

Commit

Permalink
Add inventory scrap
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfenheimm committed Nov 1, 2024
1 parent c4558f5 commit 2b53d3e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
31 changes: 30 additions & 1 deletion pallets/inventory/src/blogic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::Config;
use crate::Error;
use crate::Inventory;
use crate::{pallet::Pallet, types::*};
use crate::{Inventory, ScrapInventory};
use codec::{Encode, MaxEncodedLen};
use frame_support::sp_runtime::DispatchResult;
use frame_support::BoundedVec;
Expand Down Expand Up @@ -57,4 +57,33 @@ impl<T: Config> Pallet<T> {

Ok(())
}

pub fn do_inventory_scrap(
who: &T::AccountId,
sku: &Sku,
moved_by: &MovedByAccount,
reason: &ScrapReason,
) -> DispatchResult {
// Fetch the existing inventory for the (who, sku) combination
let items = <Inventory<T>>::get((who, sku));

// Ensure the inventory exists
let items = items.ok_or(Error::<T>::InventoryNotFound)?;

let scrap_details = ScrapDetails {
issuer: moved_by.clone(),
mats: items.clone(),
reason: reason.clone(),
equipment: BoundedVec::try_from("Equipment-U1322-1".as_bytes().to_vec())
.expect("Failed to create BoundedVec"),
};

// Scrap the item by sending it to the ScrapInventory storage
<ScrapInventory<T>>::insert((who, sku), scrap_details);

// Remove the item from the Inventory storage
<Inventory<T>>::remove((who, sku));

Ok(())
}
}
42 changes: 42 additions & 0 deletions pallets/inventory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ pub mod pallet {
cycle_count: CycleCount,
shelf_life: ShelfLife,
},
ItemScrapped {
sender: T::AccountId,
sku: Sku,
moved_by: MovedByAccount,
reason: ScrapReason,
},
}

#[pallet::storage]
Expand All @@ -133,6 +139,17 @@ pub mod pallet {
OptionQuery,
>;

#[pallet::storage]
pub type ScrapInventory<T: Config> = StorageNMap<
_,
(
NMapKey<Blake2_128Concat, T::AccountId>,
NMapKey<Blake2_128Concat, Sku>,
),
ScrapDetails,
OptionQuery,
>;

/// Errors that can be returned by this pallet.
///
/// Errors tell users that something went wrong so it's important that their naming is
Expand All @@ -149,6 +166,7 @@ pub mod pallet {
StorageOverflow,
ConversionFailed,
InvalidSkuLength,
InventoryNotFound,
}

/// The pallet's dispatchable functions ([`Call`]s).
Expand All @@ -165,6 +183,7 @@ pub mod pallet {
/// The [`weight`] macro is used to assign a weight to each call.
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Insert inventory item into storage
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::inventory_insertion())]
pub fn inventory_insertion(
Expand Down Expand Up @@ -216,5 +235,28 @@ pub mod pallet {

Ok(())
}

#[pallet::call_index(1)]
#[pallet::weight(T::WeightInfo::inventory_insertion())]
pub fn inventory_scrap(
origin: OriginFor<T>,
sku: Sku,
moved_by: MovedByAccount,
reason: ScrapReason,
) -> DispatchResult {
let who = ensure_signed(origin)?;

Self::do_inventory_scrap(&who, &sku, &moved_by, &reason)?;

// Emit an event.
Self::deposit_event(Event::ItemScrapped {
sender: who,
sku,
moved_by,
reason,
});

Ok(())
}
}
}
9 changes: 9 additions & 0 deletions pallets/inventory/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub type WeightLbs = u32;
pub type PurchaseDate = u32;
pub type Qty = u32;
pub type Sku = BoundedVec<u8, ConstU32<16>>;
pub type ScrapReason = BoundedVec<u8, ConstU32<128>>;

#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, Default, MaxEncodedLen)]
pub enum AbcCode {
Expand Down Expand Up @@ -82,3 +83,11 @@ pub struct Item {
pub struct Lot {
pub lot_number: LotNumber,
}

#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Eq, Debug)]
pub struct ScrapDetails {
pub issuer: MovedByAccount,
pub mats: BoundedVec<Item, ConstU32<100>>,
pub reason: ScrapReason,
pub equipment: BoundedVec<u8, ConstU32<32>>,
}

0 comments on commit 2b53d3e

Please sign in to comment.