Fashion MRP
26 Jun 2019 JavaScript, VueJS, ExpressJS, NodeJS, MongoDBIntroduction
In 2018 I decided to help out a start-up which was starting to grow their fashion business. Their sales and production had really taken off and they needed to track their warehouses, production and supplier purchases. At this point in time, companies need to decide either to build or to buy an MRP. Thankfully for me, they decided to build.
VueJS was becoming extremely popular and after testing it with some small ideas, it seemed like a good choice. The MRP was going to include complex calls to the database, so I decided to use MongoDB and NodeJS.
The basic outline of the project was decided.
The REST server API was built using ExpressJS, creating what is know as the MEVN stack (Mongodb, ExpressJS, VueJS, NodeJS).
Project Outline
VueJS Frontend
The user interface had to be as simple as possible. Any requirement from the company was considered and developed.
The main functionality from the client’s point of view was:
- Control the
finished products
stock - Control the
production orders
made to theproducers
- Control the
materials
stock - Control the
purchase orders
made to thesuppliers
The final approach that tied everything in was:
- Creating a
bill of materials
for eachfinished product
Finished products
With a simple glance, the user can see how many products are in stock and how many are ordered to be produced. The “ordered” column, shows a call to the database which adds up all the production orders which include that product.
Checking and modifying stock
Bill of Materials
When making each finished product, it is very important to track how much material is used. Creating a bill of materials
helps the user define and adjust the amount of material that will be removed from the database each time a product is manufactured.
Adding a bill of materials
Materials
The materials
are bought from the suppliers
and stored at the producers
facilities. Taking into account the amount of materials
, the production orders
and the purchase order
it is easy to see if there is enough material stock for the near future.
From a producer’s point of view, the user can see how much material they have, and how much they will need for production.
Checking materials in stock at a Producer's warehouse
From a supplier’s point of view, the user can see how much material they have at each producer facility, and how much they will need to order.
Checking supplier materials in stock
NodeJS + ExpressJS: Sessions and API
Keeping track of user’s sessions, privileges and roles was done using express-session and MongoDB and passport.
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
// User's passwords are hashed when created and verified
const passwordHash = require('password-hash');
Each time a user calls an API endpoint, NodeJS checks to see if the user’s cookie is valid, and wether the user has a specific role for the app. For example, deleting something from the database can only be done for a user with administrator priviledges.
NodeJS + MongoDB: Database
The database is split into 7 main collections:
materials
: Material info and stockproducts
: Product info and stockboms
: Bill of materials relating products and materialssuppliers
: Supplier information.suppliers.orders
: Purchase orders to suppliers.producers
: Producer information.producers.orders
: Production orders to producers.
Every item in these collections has a unique _id
which is used to relate information from one to collection to another.
For example, controling the material
stock in each producer
facility is done:
//material Collection
{"_id" : "MATERIAL-1-id",
"name" : "Material 1",
"stock" : [
{"producer_id": "PRODUCER-1-id",
"stock": 3},
{"producer_id": "PRODUCER-2-id",
"stock": 5}
],
"supplier_id" : "SUPPLIER-1-id",
...
}
//suppliers Collection
{"_id": "SUPPLIER-1-id"
...
}
//producers Collection
{"_id": "PRODUCER-1-id"
...
}
An example of a Bill Of Material would be:
//boms collection
{"_id" : "BOM-x-id",
"product_id" : "PRODUCT-1-id",
"materials" : [
{"material_id": "MATERIAL-1-id"
"supplier_id" : "SUPPLIER-1-id",
"quantity" : 4},
{"material_id": "MATERIAL-2-id",
"supplier_id" : "SUPPLIER-1-id",
"quantity" : 0.2}
],
...
}
MongoDB Aggregation Framework
Most of the complex queries to the database are done using the Aggregation Pipeline Framework, which is much faster than running javascript functions inside NodeJS.