Basic "Inventory Management Contract" Example

in this post i will be creating a Basic Inventory Management Contract

Inventory management is the management of inventory and stock. As an element of supply chain management, inventory management includes aspects such as controlling and overseeing ordering inventory, storage of inventory, and controlling the amount of product for sale.

So here are the basics of the inventory system that i will be adding to the contract
1. Add a product to the Inventory (Only Creator)
2. Remove a product from the Inventory (Only Creator)
3. Replenish Products in the inventory (Only Creator)
4. allow anyone to order a product


pragma solidity ^0.4.0;

contract InventoryAresh{
    
    struct Product{
        string sku; //always unique to the product
        string itemName;
        string itemDescription;
        uint qty; //available qty
        uint thresholdQty; //minimum qty before reorder
        string location;
        UoM UnitOfMeasure;
        Status status;
        bool isValue;
    }
    
    //limiting unit of measure to specifics
    enum UoM{
            pcs, ea, lbs, bags, kg
    }
    enum Status{
        Available, ForOrdering, OutOfStock, Removed
    }
    //whos the warehouse manager
    address creator; 
    
    mapping(bytes32 => Product) Inventory;
    
    //Events
    event InformManager(string sku, uint qty, address requestor);
    event ShipOrder(string sku, uint qty, address requestor);
    
    //modifier
    modifier onlyBy(address _account)
    {
        require(msg.sender == _account);
        _;
    }
    //This is the constructor
    function InventoryAresh()  public
    {
        creator = msg.sender;
    }
    
    //this function will add a product to the InventoryAresh
    function AddProduct(string _sku,string _name, string _description, uint _qty, string _location, UoM uom) public onlyBy(creator)
    {
        bytes32 hash = keccak256(_sku);
        if (Inventory[hash].isValue)
        {
            revert();
        }
        else
        {
            Inventory[hash] = Product(_sku, _name, _description, _qty, 0, _location, uom, Status.Available, true);
        }
    }
    
    //this function will Remove a product
    function RemoveProduct(string _sku) public view onlyBy(creator)
    {
        bytes32 hash = keccak256(_sku);
        Product memory uprod = Inventory[hash];
        uprod.status = Status.Removed; //set the status to Removed
        uprod.qty = 0; //set the qty to zero
        uprod.isValue = false;
    }
    
    //this function will add to the qty of a product by the owner
    function ReplenishStock(string _sku, uint _qty) public onlyBy(creator)
    {
        bytes32 hash = keccak256(_sku);
        Inventory[hash].qty += _qty;
    }
    
    //This function will allow any user to Order
    function Order(string _sku, uint _qty) public
    {
        bytes32 hash = keccak256(_sku);
        if (QtyExists(hash,_qty))
        {
            Inventory[hash].qty -= _qty; //reduce or reserve qty for next order
            ShipOrder(_sku, _qty, msg.sender); //inform someone of the shipment
        }
        else
        {
            //Qty is not enough so revert remaining funds;
            InformManager(_sku, _qty, msg.sender); //inform the warehouse manager
            revert();
        }
    }
    
    //This function checks if the required quantity is available
    function QtyExists(bytes32 _sku, uint _qty) private view returns (bool)
    {
        Product memory uprod = Inventory[_sku];
        if ((uprod.qty >= _qty)&&(uprod.status != Status.Removed))
            return true;
        return false;
    }
    
}

Updated 11/1/2017 Hash function of a struct

Comments

Popular posts from this blog

Solidity Hash of Structs (Testing hash uniqueness)

Is Making the Crypto Space Legally Compliant Paving the Road to Mass (Blockchain) Adoption?

Parity installation on Ubuntu Virtual Machine