purespectrum-lib
Version:
shared code between front and backend projects
399 lines • 16.9 kB
JavaScript
/* eslint-disable complexity */
/* eslint-disable camelcase */
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
exports.Blend = exports.Flexibility = exports.AllocationFactory = void 0;
var Supplier = /** @class */ (function () {
function Supplier(id, quantity, percentage, group_name, flexValue) {
if (quantity === void 0) { quantity = 0; }
if (percentage === void 0) { percentage = 0; }
if (group_name === void 0) { group_name = ''; }
if (flexValue === void 0) { flexValue = 100; }
this.id = id;
this.quantity = quantity;
this.percentage = percentage;
this.group_name = group_name;
this.flexValue = flexValue;
this.suppliers = [{ id: id }];
this.quantity = quantity;
this.percentage = percentage;
this.group_name = group_name;
this.flexValue = flexValue;
}
Supplier.prototype.incrementQuantity = function (value) {
this.quantity += value;
};
return Supplier;
}());
var EvenlyAdditionAllocation = /** @class */ (function () {
function EvenlyAdditionAllocation(suppliers, aditionalQuantity, completes) {
var suppliersPrepared = [];
for (var index = 0; index < suppliers.length; index++) {
var supplier = suppliers[index];
var quantity = supplier.quantity;
suppliersPrepared.push(new Supplier(supplier.id, quantity, supplier.percentage));
}
this.suppliers = suppliersPrepared;
this.aditionalQuantity = aditionalQuantity;
this.completes = completes;
}
EvenlyAdditionAllocation.prototype.distribute = function () {
if (this.suppliers.length == 0) {
return;
}
var allocationQty = 1;
//distribute the aditional allocation evenly
while (this.aditionalQuantity > 0) {
for (var index = 0; index < this.suppliers.length; index++) {
var supplier = this.suppliers[index];
if (this.aditionalQuantity > 0 && supplier.quantity >= 0) {
supplier.quantity = supplier.quantity + allocationQty;
this.aditionalQuantity = this.aditionalQuantity - allocationQty;
}
}
}
this.suppliers = Percentage.recalculate(this.suppliers, this.completes);
};
EvenlyAdditionAllocation.prototype.quantity = function (id) {
return new Quantity(this.suppliers, id).value();
};
EvenlyAdditionAllocation.prototype.percentage = function (id) {
return new Percentage(this.suppliers, id).value();
};
return EvenlyAdditionAllocation;
}());
var EvenlyDistributionAllocation = /** @class */ (function () {
function EvenlyDistributionAllocation(suppliers, completes) {
this.completes = completes;
var suppliersPrepared = [];
for (var index = 0; index < suppliers.length; index++) {
var supplier = suppliers[index];
var quantity = 0;
suppliersPrepared.push(new Supplier(supplier.id, quantity, supplier.percentage));
}
this.suppliers = suppliersPrepared;
this.completes = completes;
}
EvenlyDistributionAllocation.prototype.distribute = function () {
if (this.suppliers.length == 0) {
return;
}
for (var index = 0; index < this.suppliers.length; index++) {
var supplier = this.suppliers[index];
supplier.quantity = 0;
}
var remainingCompletes = this.completes;
if (this.completes < this.suppliers.length) {
remainingCompletes = this.suppliers.length;
}
var allocationQty = 1;
while (remainingCompletes > 0) {
for (var index = 0; index < this.suppliers.length; index++) {
var supplier = this.suppliers[index];
if (remainingCompletes > 0) {
supplier.quantity = supplier.quantity + allocationQty;
remainingCompletes = remainingCompletes - allocationQty;
}
}
}
this.suppliers = Percentage.recalculate(this.suppliers, this.completes);
};
EvenlyDistributionAllocation.prototype.quantity = function (id) {
return new Quantity(this.suppliers, id).value();
};
EvenlyDistributionAllocation.prototype.percentage = function (id) {
return new Percentage(this.suppliers, id).value();
};
return EvenlyDistributionAllocation;
}());
var BlendAllocation = /** @class */ (function () {
function BlendAllocation(suppliers, completes, blend) {
this.completes = completes;
this.blend = blend;
var suppliersPrepared = [];
for (var index = 0; index < suppliers.length; index++) {
var supplier = suppliers[index];
var quantity = 0;
suppliersPrepared.push(new Supplier(supplier.id, quantity, supplier.percentage));
}
this.suppliers = suppliersPrepared;
this.completes = completes;
this.blend = blend;
}
BlendAllocation.prototype.distribute = function () {
if (this.suppliers.length == 0) {
return;
}
var allocation = new Blend(this.blend, this.suppliers, this.completes);
var suppliers = allocation.mapGroupForSrvSuppliers(this.suppliers, this.blend);
var mappedSuppliers = allocation.rearrangeSuppliersAsPerGroup(suppliers);
var finalSupplier = allocation.applyBlendAllocation(mappedSuppliers, this.blend, this.completes);
this.suppliers = finalSupplier;
this.suppliers = Percentage.recalculate(this.suppliers, this.completes);
return finalSupplier;
};
BlendAllocation.prototype.quantity = function (id) {
return new Quantity(this.suppliers, id).value();
};
BlendAllocation.prototype.percentage = function (id) {
return new Percentage(this.suppliers, id).value();
};
return BlendAllocation;
}());
var Quantity = /** @class */ (function () {
function Quantity(suppliers, id) {
this.suppliers = suppliers;
this.id = id;
this.suppliers = suppliers;
this.id = id;
}
Quantity.prototype.value = function () {
var _this = this;
if (this.suppliers.find(function (supplier) { var _a; return (_a = supplier.suppliers) === null || _a === void 0 ? void 0 : _a.map(function (supp) { return supp.id; }).includes(_this.id); })) {
var quantity = this.suppliers.find(function (supplier) { var _a; return (_a = supplier.suppliers) === null || _a === void 0 ? void 0 : _a.map(function (supp) { return supp.id; }).includes(_this.id); });
if (quantity) {
return quantity.quantity;
}
return 0;
}
return 0;
};
return Quantity;
}());
var Percentage = /** @class */ (function () {
function Percentage(suppliers, id) {
this.suppliers = suppliers;
this.id = id;
this.suppliers = suppliers;
this.id = id;
}
Percentage.recalculate = function (suppliers, completes) {
for (var index = 0; index < suppliers.length; index++) {
var supplier = suppliers[index];
supplier.percentage =
supplier.quantity > 0
? Number(Math.floor((supplier.quantity * 100) / completes))
: 0;
}
return suppliers;
};
Percentage.prototype.value = function () {
var _this = this;
if (this.suppliers.find(function (supplier) { var _a; return (_a = supplier.suppliers) === null || _a === void 0 ? void 0 : _a.map(function (supp) { return supp.id; }).includes(_this.id); })) {
var quantity = this.suppliers.find(function (supplier) { var _a; return (_a = supplier.suppliers) === null || _a === void 0 ? void 0 : _a.map(function (supp) { return supp.id; }).includes(_this.id); });
if (quantity) {
return quantity.percentage;
}
return 0;
}
return 0;
};
return Percentage;
}());
var AllocationFactory = /** @class */ (function () {
function AllocationFactory() {
}
AllocationFactory.allocation = function (suppliers, completes, blend, evenlyDistribution, aditionalQuantity) {
var allocation;
if (blend.id != '') {
allocation = new BlendAllocation(suppliers, completes, blend);
}
else if (evenlyDistribution) {
allocation = new EvenlyDistributionAllocation(suppliers, completes);
}
else {
allocation = new EvenlyAdditionAllocation(suppliers, aditionalQuantity, completes);
}
allocation.distribute();
return allocation;
};
return AllocationFactory;
}());
exports.AllocationFactory = AllocationFactory;
var Flexibility = /** @class */ (function () {
function Flexibility(flexValue, allocation, completesNeeded) {
this.flexValue = flexValue;
this.allocation = allocation;
this.completesNeeded = completesNeeded;
this.flexValue = flexValue;
this.allocation = allocation;
this.completesNeeded = completesNeeded;
}
Flexibility.prototype.goal = function () {
var completes_goal = 0;
if (!this.flexValue) {
completes_goal = this.allocation;
}
else if (this.flexValue >= 100) {
completes_goal = this.completesNeeded;
}
else {
var max = this.allocation + (this.allocation * this.flexValue) / 100;
completes_goal = Math.floor(Math.min(max, this.completesNeeded));
}
return completes_goal;
};
return Flexibility;
}());
exports.Flexibility = Flexibility;
var Blend = /** @class */ (function () {
function Blend(blend, suppliers, completes) {
this.blend = blend;
this.suppliers = suppliers;
this.completes = completes;
this.blend = blend;
this.suppliers = suppliers;
this.completes = completes;
}
Blend.empty = function () {
return {
suppliers: [],
config_name: '',
id: '',
is_default: false,
blend_flexibility: 100,
};
};
Blend.getBlendFromGetSupplierBlendInterface = function (blendId, blends) {
if (blendId === void 0) { blendId = ''; }
for (var index = 0; index < blends.length; index++) {
var blend = blends[index];
if (blend.id === blendId) {
return blend;
}
}
return this.empty();
};
Blend.prototype.mapGroupForSrvSuppliers = function (suppliers, blend) {
var suppliersGrouped = suppliers.map(function (srvSup) {
var group = blend.suppliers.filter(function (sup) {
return sup.supplier_id.map(function (supplier) { return supplier.id; }).includes(srvSup.id) &&
sup.group_name !== '';
});
if (group.length) {
srvSup.group_name = group[0].group_name || '';
}
return srvSup;
});
return suppliersGrouped;
};
Blend.prototype.rearrangeSuppliersAsPerGroup = function (suppliers) {
var mappedSuppliers = [];
var _loop_1 = function (i) {
var srvSup = suppliers[i];
var index = mappedSuppliers.findIndex(function (sup) { return srvSup.group_name && sup.group_name === srvSup.group_name; });
var subSupplier = { id: srvSup.id };
if (index !== -1) {
mappedSuppliers[index].suppliers.push(subSupplier);
mappedSuppliers[index].quantity =
Number(mappedSuppliers[index].quantity) + Number(srvSup.quantity);
}
else {
mappedSuppliers.push(new Supplier(subSupplier.id, srvSup.quantity, srvSup.percentage, srvSup.group_name, srvSup.flexValue));
}
};
for (var i = 0; i < suppliers.length; i++) {
_loop_1(i);
}
return mappedSuppliers;
};
Blend.prototype.blendSuppliers = function (mappedSuppliers, blend) {
var _this = this;
var blendSuppliers = [];
blend.suppliers.forEach(function (blendSupp) {
for (var i in mappedSuppliers) {
if (_this.isSuppEqualBlendSupp(mappedSuppliers[i], blendSupp)) {
blendSuppliers.push(blendSupp);
break;
}
}
});
return blendSuppliers;
};
Blend.prototype.setSuppliersToRedestribute = function (mappedSuppliers, blendSuppliers, remainingCompletes, suppsIndexToRedistribute, completes) {
var _this = this;
var _loop_2 = function (i) {
var notFound = true;
var totalBlendAllocation = 0;
mappedSuppliers[i].quantity = 0;
blendSuppliers.forEach(function (blendSupp) {
if (_this.isSuppEqualBlendSupp(mappedSuppliers[i], blendSupp)) {
mappedSuppliers[i].quantity = Math.ceil((Number(blendSupp.allocation_percentage) * completes) / 100);
remainingCompletes = remainingCompletes - mappedSuppliers[i].quantity;
notFound = false;
}
totalBlendAllocation += Number(blendSupp.allocation_percentage);
});
if (notFound &&
this_1._isRemainingAllocationPercentage(totalBlendAllocation)) {
suppsIndexToRedistribute.push(Number(i));
}
};
var this_1 = this;
for (var i in mappedSuppliers) {
_loop_2(i);
}
return remainingCompletes;
};
Blend.prototype.applyBlendAllocation = function (mappedSuppliers, blend, completes) {
// eslint-disable-next-line prefer-const
var remainingCompletes = completes;
var suppsIndexToRedistribute = [];
var blendSuppliers = this.blendSuppliers(mappedSuppliers, blend);
remainingCompletes = this.setSuppliersToRedestribute(mappedSuppliers, blendSuppliers, remainingCompletes, suppsIndexToRedistribute, completes);
this.distributeRemainingAllocations(suppsIndexToRedistribute, remainingCompletes, mappedSuppliers);
this.setAllocationToElegibleSuppliers(suppsIndexToRedistribute, mappedSuppliers);
return mappedSuppliers;
};
Blend.prototype.distributeRemainingAllocations = function (indexes, remainingCompletes, mappedSuppliers) {
if (this.validateRemainings(indexes.length, remainingCompletes)) {
var allocationQty_1 = 1;
while (remainingCompletes > 0) {
indexes.forEach(function (index) {
if (remainingCompletes > 0) {
mappedSuppliers[index].quantity =
mappedSuppliers[index].quantity + allocationQty_1;
remainingCompletes = remainingCompletes - allocationQty_1;
}
});
}
}
};
Blend.prototype.validateRemainings = function (length, remainingCompletes) {
return length > 0 && remainingCompletes > 0;
};
Blend.prototype.setAllocationToElegibleSuppliers = function (indexes, mappedSuppliers) {
indexes.forEach(function (index) {
if (mappedSuppliers[index].quantity === 0) {
mappedSuppliers[index].quantity = 1;
}
});
};
Blend.prototype.isSuppEqualBlendSupp = function (mappedSupp, blendSupp) {
return (!this.isEmpty(blendSupp.allocation_percentage) &&
(this.validateGroupSupp(mappedSupp, blendSupp) ||
this.validateIndividualSupp(mappedSupp, blendSupp)));
};
Blend.prototype.validateGroupSupp = function (mappedSupp, blendSupp) {
return (mappedSupp.group_name && mappedSupp.group_name === blendSupp.group_name);
};
Blend.prototype.validateIndividualSupp = function (mappedSupp, blendSupp) {
if (typeof mappedSupp.suppliers[0] === 'object') {
return (!mappedSupp.group_name &&
mappedSupp.suppliers.length === 1 &&
mappedSupp.suppliers[0].id === blendSupp.supplier_id[0].id);
}
return (!mappedSupp.group_name &&
mappedSupp.suppliers.length === 1 &&
mappedSupp.suppliers[0] === blendSupp.supplier_id[0]);
};
Blend.prototype.isEmpty = function (value) {
return value === '' || value === undefined || value === null;
};
Blend.prototype._isRemainingAllocationPercentage = function (value) {
return value < 100;
};
return Blend;
}());
exports.Blend = Blend;
//# sourceMappingURL=allocation.js.map