Skip to content

Commit

Permalink
Add Adjust Inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfenheimm committed Nov 6, 2024
1 parent 9eb7b15 commit 6311209
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 12 deletions.
8 changes: 5 additions & 3 deletions pallets/inventory/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ mod benchmarks {
fn inventory_insertion() {
let caller: T::AccountId = whitelisted_caller();

let sku = Sku::default();

#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), Item::default());

let key = (caller.clone(), sku, Item::default().serial_number);
let key = (
caller.clone(),
Item::default().sku,
Item::default().serial_number,
);

assert!(Inventory::<T>::contains_key(key));
}
Expand Down
34 changes: 30 additions & 4 deletions 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::{pallet::Pallet, types::*};
use crate::{Inventory, ScrapInventory};
use crate::{AdjustInventory, Inventory, ScrapInventory};
use codec::{Encode, MaxEncodedLen};
use frame_support::sp_runtime::DispatchResult;

Expand Down Expand Up @@ -32,10 +32,8 @@ impl<T: Config> Pallet<T> {
.ok_or(Error::<T>::InventoryNotFound)?;

let scrap_item = ScrapItem {
issuer: scrap_details.issuer,
item,
reason: scrap_details.reason,
equipment: scrap_details.equipment,
details: scrap_details,
};

// Scrap the item by sending it to the ScrapInventory storage
Expand All @@ -60,4 +58,32 @@ impl<T: Config> Pallet<T> {
});
Ok(())
}

pub fn do_inventory_adjust(
who: &T::AccountId,
issuer: MovedByAccount,
mut item: Item,
adjust_details: AdjustDetails,
) -> DispatchResult {
// Update the item's fields
let adjust_item = AdjustItem {
issuer,
item: item.clone(),
original_qty: item.qty,
adjust_details: adjust_details.clone(),
};

// update the item's quantity
item.qty = adjust_details.new_qty;

// Update the item in storage
<Inventory<T>>::mutate((who, item.sku.clone(), item.serial_number), |stored_item| {
*stored_item = Some(item.clone());
});

// Insert the adjustment for auditing purposes
<AdjustInventory<T>>::insert((who, item.sku, item.serial_number), adjust_item);

Ok(())
}
}
50 changes: 49 additions & 1 deletion pallets/inventory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ pub mod pallet {
original_location: Location,
new_location: Location,
},
AdjustItem {
sender: T::AccountId,
item: Item,
issuer: MovedByAccount,
adjust_details: AdjustDetails,
},
}

#[pallet::storage]
Expand All @@ -147,6 +153,18 @@ pub mod pallet {
OptionQuery,
>;

#[pallet::storage]
pub type AdjustInventory<T: Config> = StorageNMap<
_,
(
NMapKey<Blake2_128Concat, T::AccountId>,
NMapKey<Blake2_128Concat, Sku>,
NMapKey<Blake2_128Concat, SerialNumber>,
),
AdjustItem,
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 Down Expand Up @@ -187,9 +205,10 @@ pub mod pallet {
pub fn inventory_insertion(origin: OriginFor<T>, item: Item) -> DispatchResult {
let who = ensure_signed(origin)?;

// Insert the item into storage
Self::do_inventory_insertion(&who, item.clone())?;

// Emit an event.
// Emit the insertion
Self::deposit_event(Event::AddNewItem { sender: who, item });

Ok(())
Expand Down Expand Up @@ -248,5 +267,34 @@ pub mod pallet {

Ok(())
}

#[pallet::call_index(3)]
#[pallet::weight(T::WeightInfo::inventory_insertion())]
pub fn inventory_adjust(
origin: OriginFor<T>,
issuer: MovedByAccount,
sku: Sku,
serial_number: SerialNumber,
adjust_details: AdjustDetails,
) -> DispatchResult {
let who = ensure_signed(origin)?;

// Check if the item exists in inventory
let item = Inventory::<T>::get((&who, sku.clone(), serial_number))
.ok_or(Error::<T>::InventoryNotFound)?;

// Adjust the item's quantity
Self::do_inventory_adjust(&who, issuer.clone(), item.clone(), adjust_details.clone())?;

// Emit the adjustment
Self::deposit_event(Event::AdjustItem {
sender: who,
item: item.clone(),
issuer,
adjust_details,
});

Ok(())
}
}
}
20 changes: 16 additions & 4 deletions pallets/inventory/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +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>>;
pub type Reason = BoundedVec<u8, ConstU32<128>>;

#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, Default, MaxEncodedLen)]
pub enum AbcCode {
Expand Down Expand Up @@ -103,19 +103,31 @@ pub struct Item {

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

#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Eq, Debug)]
pub struct AdjustItem {
pub issuer: MovedByAccount,
pub item: Item,
pub reason: ScrapReason,
pub equipment: Equipment,
pub original_qty: Qty,
pub adjust_details: AdjustDetails,
}

#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Eq, Debug)]
pub struct ScrapDetails {
pub issuer: MovedByAccount,
pub reason: ScrapReason,
pub reason: Reason,
pub equipment: Equipment,
}

#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Eq, Debug)]
pub struct AdjustDetails {
pub new_qty: Qty,
pub reason: Reason,
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)]
#[scale_info(skip_type_params(T))]
pub struct Lot {
Expand Down

0 comments on commit 6311209

Please sign in to comment.