Skip to content

Instantly share code, notes, and snippets.

@guyjin
Created January 24, 2019 23:16
Show Gist options
  • Save guyjin/447800475eb635d5c4003358779ca325 to your computer and use it in GitHub Desktop.
Save guyjin/447800475eb635d5c4003358779ca325 to your computer and use it in GitHub Desktop.
angular.module('fpfs')
.controller('speciesMeasuredPUCtrl', function (pu, $rootScope, contractAbstract, subView,
paymentUnits, contractSpecies, regionalSpecies, growl, Api,
fpfsSession, productsList, unitOfMeasure) {
var pu = this;
pu.regionalSpecies = regionalSpecies;
pu.contractRecord = contractAbstract;
pu.contractAbstract = contractAbstract;
pu.contractSpecies = contractSpecies;
pu.contractSpeciesByPaymentUnit = null;
pu.contractSpeciesNotInPaymentUnit = null;
pu.productsList = productsList;
pu.unitOfMeasure = unitOfMeasure;
pu.paymentUnits = paymentUnits;
pu.paymentUnitsbySPU = [];
pu.showNewPUForm = false;
pu.showNewSPUForm = false;
pu.showEditPUPopup = false;
pu.side = 'pu';
pu.selectedSpecies = {
index: '',
code: [],
productCode: '',
regionalSpeciesId: ''
};
pu.selectedUnit = {
paymentUnitId: '' ,
canSPUBeAdded: false
};
pu.newPUModel = {
"paymentUnitNumber": 0,
"species": []
};
pu.newPUSpeciesModel = {
"paymentUnitId": 0,
"species": []
};
pu.puPopupModel = {
unitIndex: '',
paymentUnitSpeciesIndex: '',
index: '',
quantity: '',
rate: 0.01
};
// Selecting contexts
pu.selectSide = function (side) {
pu.side = side;
pu.selectedSpecies.index = '';
pu.selectedSpecies.code = [];
pu.selectedUnit.paymentUnitId = '';
};
pu.selectSpecies = function (spu) {
if (pu.showNewPUForm) {
// check if speciesCode is in newPUModel.spuID
if (!pu.newPUSpeciesSelected(spu.contractSpeciesId)) {
var newSpu = {
"contractSpeciesId": spu.contractSpeciesId
};
pu.newPUSpeciesModel.species.push(newSpu);
} else {
var tempArr = pu.newPUSpeciesModel.species.filter(function(el) {
return el.contractSpeciesId !== spu.contractSpeciesId;
});
pu.newPUSpeciesModel.species = tempArr;
}
}
};
// function to check if a given species needs to be highlighted or not
pu.speciesSelected = function (code) {
if (code) {
return $.inArray(code, pu.selectedSpecies.code) > -1;
}
};
pu.speciesInPaymentUnit = function() {
var puSpeciesIds = {};
_.each(pu.contractSpeciesByPaymentUnit, function (puspu) { puSpeciesIds[puspu.regionalSpeciesId] = true; });
pu.contractSpeciesNotInPaymentUnit = _.filter(pu.contractSpecies, function(species){
return !puSpeciesIds[species.regionalSpeciesId];
});
};
// check if contractSpeciesId exists in model to be added to the PU
pu.newPUSpeciesSelected = function (contractSpeciesId) {
if (contractSpeciesId) {
return pu.newPUSpeciesModel.species.some(function(el) {
return el.contractSpeciesId === contractSpeciesId;
});
}
};
pu.isPaymentUnitWithSpecies = function (paymentUnitId) {
if (paymentUnitId) {
return _.contains(_.pluck(pu.paymentUnits, 'paymentUnitId'), paymentUnitId);
}
};
pu.selectUnit = function (unit) {
pu.selectedUnit.paymentUnitId = unit.paymentUnitId;
pu.selectedUnit.canSPUBeAdded = unit.canSPUBeAdded;
pu.fillSpeciesByUnit(unit.paymentUnitId);
pu.showNewPUForm = false;
};
pu.fillSpeciesByUnit = function (unitId) {
pu.selectedSpecies.code = [];
Api.getSpeciesUnitByPaymentUnitSID(unitId).then(function (data) {
pu.contractSpeciesByPaymentUnit = data;
pu.selectedSpecies.code = _.pluck(data, 'speciesCode');
pu.speciesInPaymentUnit();
});
};
pu.fillUnitBySpecies = function () {
pu.paymentUnitsbySPU = [];
Api.getPaymentUnitsBySpeciesId(pu.selectedSpecies.regionalSpeciesId,
pu.selectedSpecies.productCode).then(function (data) {
data.map(function (unit) {
pu.paymentUnitsbySPU.push(unit);
});
});
};
pu.newSPUUnitSelected = function (unitId) {
if (unitId) {
return _.contains(pu.createSPU.newSPUModel.paymentUnits, unitId);
}
};
pu.checkForAvailablePU = function() {
var areReleased = false;
_.each(pu.paymentUnits, function(pu){
if(pu.releaseDate !== null) {
areReleased = true;
}
}) ;
return areReleased;
};
// Edit and create new PU
pu.openNewPUForm = function () {
pu.selectSide('pu');
pu.createSPU.showNewSPUForm = false;
pu.showNewPUForm = true;
};
pu.closeNewPUForm = function () {
pu.showNewPUForm = false;
pu.newPUModel = {
"paymentUnitNumber": ''
};
pu.newPUSpeciesModel = {
"paymentUnitId": 0,
"species": []
};
pu.form.newPUForm.$setPristine(true);
pu.form.newPUForm.$dirty = false;
pu.form.newPUForm.$setUntouched(true);
};
pu.saveNewPU = function () {
Api.addPaymentUnitByContractId(pu.contractRecord.contractSID, pu.newPUModel).then(function (data) {
growl.info('Payment Unit added ');
var newPU = data;
var puId = newPU.paymentUnitId;
pu.newPUSpeciesModel.paymentUnitId = puId;
pu.addSPUToPU(pu.newPUSpeciesModel);
});
};
pu.addSPUToPU = function(spus) {
Api.addSpeciesToPaymentUnit(spus).then(function(data) {
growl.success('SPU\'s added ');
pu.closeNewPUForm();
Api.getPaymentUnitByContractSID(pu.contractRecord.contractSID).then(function(data) {
pu.paymentUnits = data;
});
});
};
pu.addSPUToPaymentUnit = function(species, paymentUnitId, paymentUnitNumber) {
var addSPUToPaymentUnitModel = {
"paymentUnitId": paymentUnitId,
"paymentUnitNumber": paymentUnitNumber,
"species": [species]
};
Api.addSpeciesToPaymentUnit(addSPUToPaymentUnitModel).then(function(data) {
growl.success('The SPU has been successfully added to this Payment Unit.');
pu.fillSpeciesByUnit(paymentUnitId);
});
//TODO: build add SPU to existing PU function
};
// edit and create SPU
pu.createSPU = {
showNewSPUForm: false,
openNewSPUForm: function () {
pu.side = 'spu';
pu.selectedSpecies = {
index: '',
code: [],
productCode: ''
};
pu.selectedUnit.paymentUnitNumber = '';
this.showNewPUForm = false;
this.showNewSPUForm = true;
this.newSPUModel.UoM = pu.contractSpecies[0].unitOfMeasureCode;
},
closeNewSPUForm: function () {
this.showNewSPUForm = false;
this.newSPUModel = {
"contractSpeciesId": '',
"conversionFactors": [
{
"factor": '',
"isRequired": true,
"name": ""
}
],
"currentQty": '',
"currentRateStructure": {
"baseRate": '',
"bidRate": '',
"currentRate": '',
"displayBaseRate": true,
"escalationData": {
"indexBaseValue": '',
"rateIndexCode": "",
"speciesCode": ""
},
"maxRate": '',
"rateEffectiveDate": "",
"rateStructureSID": '',
"tpvRate": ''
},
"originalQty": '',
"productCode": "",
"productName": "",
"regionalSpeciesId": '',
"speciesCode": "",
"speciesName": "",
"unitOfMeasureCode": "",
"unitOfMeasureName": ""
};
},
newSPUModel: {
"contractSpeciesId": '',
"conversionFactors": [
{
"factor": '',
"isRequired": true,
"name": ""
}
],
"currentQty": '',
"currentRateStructure": {
"baseRate": '',
"bidRate": '',
"currentRate": '',
"displayBaseRate": true,
"escalationData": {
"indexBaseValue": '',
"rateIndexCode": "",
"speciesCode": ""
},
"maxRate": '',
"rateEffectiveDate": "",
"rateStructureSID": '',
"tpvRate": ''
},
"originalQty": '',
"productCode": "",
"productName": "",
"regionalSpeciesId": '',
"speciesCode": "",
"speciesName": "",
"unitOfMeasureCode": "",
"unitOfMeasureName": ""
},
addSPUFormValidCheck: function () {
console.log(pu.createSPU.newSPUModel);
// return true tells ng-disabled on submit button to turn off.
return true;
},
addSPU: function() {
Api.postPaymentUnitByContractSID(pu.createSPU.newSPUModel, pu.contractRecord.contractSID).then(function(data) {
console.log(data);
});
}
};
// Editing PU data associated with a given SPU.
pu.editPUData = function (index) {
// console.log(index, paymentUnitSpeciesIndex, unitIndex);
// console.log(pu.paymentUnits[unitIndex].paymentUnitSpecies[paymentUnitSpeciesIndex].currentQuantityEstimate);
angular.copy(pu.paymentUnitsbySPU[index], pu.editPUQuantity.originalQuantityModel);
angular.copy(pu.paymentUnitsbySPU[index], pu.editPUQuantity.quantityModel);
pu.editPUQuantity.editPUQuantityModalOpen = true;
};
pu.openEditPUPopup = function () {
pu.showEditPUPopup = true;
};
pu.closeEditPUPopup = function () {
pu.editPUQuantity.editPUQuantityModalOpen = false;
};
pu.saveEditPUPopup = function () {
// do stuff
Api.savePaymentUnitSpecies(pu.editPUQuantity.quantityModel).then(function (data) {
growl.info("Payment Unit updated.");
pu.closeEditPUPopup();
});
return false;
};
pu.resetPUQuantityEstimateModel = function ($event) {
$event.preventDefault();
angular.copy(pu.editPUQuantity.originalQuantityModel, pu.editPUQuantity.quantityModel);
};
// Edit individual SPU Rates
pu.editSPURate = {
that: this,
editSPURateModalOpen: false,
originalRates: {},
ratesModel: {},
saveSPURateStructure: function ($event) {
$event.preventDefault();
Api.putUpdatedRate(this.ratesModel).then(function () {
Api.getSPUsByContractSID(fpfsSession.getSelectedContractSID()).then(function (data) {
pu.contractSpecies = data;
growl.success('SPU current rate updated.');
pu.editSPURate.closeModal();
});
});
},
populateRateModel: function (index) {
angular.copy(pu.contractSpecies[index].currentRateStructure, pu.editSPURate.originalRates);
angular.copy(pu.editSPURate.originalRates, pu.editSPURate.ratesModel);
},
resetSPURatesForm: function ($event) {
$event.preventDefault();
pu.editSPURate.ratesModel = {};
angular.copy(pu.editSPURate.originalRates, pu.editSPURate.ratesModel);
},
openEditSPURateModalOpen: function (index) {
this.editSPURateModalOpen = true;
this.populateRateModel(index);
},
closeModal: function () {
this.editSPURateModalOpen = false;
},
keyListener: pu.$on('key', function ($event, keyEvent) {
if (keyEvent.keyCode === 27) {
pu.editSPURate.closeModal();
}
}),
destroy: pu.$on('$destroy', function() {
pu.editSPURate.keyListener();
})
};
// Edit PU quantity
pu.editPUQuantity = {
that: this,
editPUQuantityModalOpen: false,
closeModal: function () {
this.editPUQuantityModalOpen = false;
},
quantityModel: {},
originalQuantityModel: {}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment