Name
global.ProcurementMobileHandler
Description
Handles Purchase Order Line item receive action for mobile
Script
var ProcurementMobileHandler = Class.create();
ProcurementMobileHandler.prototype = {
POL_TABLE: "proc_po_item",
poSysId: "",
currencyCode: "",
currencyValue: "",
initialize: function() {
},
receivePOL: function(assetTag, serialNumber, polID, batchProcessing) {
// Sanity check the POL received
if(!this._validatePOLreceived(polID))
return;
var pol_obj = new JSON().decode(this._buildPOLobj(assetTag, serialNumber, polID));
var pol_arr = [];
pol_arr.push(pol_obj);
var pol_to_process = new JSON().encode(pol_arr);
// Process receive action
return new POReceiveManager().processPOReceiveWithParams(this.poSysId, pol_to_process, batchProcessing);
},
_validatePOLreceived: function(pol_id){
//Validate POL received
var gr = new GlideRecord(this.POL_TABLE);
if(!gr.get(pol_id)){
gs.addErrorMessage(gs.getMessage("Purchase order line item not found!"));
return false;
}
if(gr.getValue("status") === "received"){
gs.addErrorMessage(gs.getMessage("Purchase order line item already received!"));
return false;
}
if(parseInt(gr.getValue("remaining_quantity")) <= 0){
gs.addErrorMessage(gs.getMessage("No remaining items to receive for this purchase order line item!"));
return false;
}
this.poSysId = gr.getValue("purchase_order");
this.currencyCode = gr.cost.getReferenceCurrencyCode();
this.currencyValue = gr.cost.getReferenceValue();
if(gs.nil(this.poSysId)){
gs.addErrorMessage(gs.getMessage("Purchase order linked to this line item not found!"));
return false;
}
return true;
},
_buildPOLobj: function(assetTag, serialNumber, polID){
var pol_arr = new JSON().decode(new POReceiveManager().getPOItemsWithParams(this.poSysId, polID));
if (!Array.isArray(pol_arr) || !pol_arr.length){
// array does not exist, is not an array, or is empty
gs.addErrorMessage(gs.getMessage("Something went wrong getting line item for the purchase order."));
return;
}
var pol_obj = pol_arr[0];
if (this._isEmpty(pol_obj)){
gs.addErrorMessage(gs.getMessage("Problem getting POL record."));
return;
}
// Build pol object with received data
pol_obj.receive = true;
pol_obj.receiving_quantity = "1";
pol_obj.reserved = false;
pol_obj.cost_with_currency = this.currencyCode + ';'+ this.currencyValue;
var assetDetails = {
'asset_tag' : assetTag,
'serial_number' : serialNumber
};
// Check if asset exists
var assetGR = new GlideRecord('alm_asset');
if(!gs.nil(assetTag)) {
assetGR.addQuery('asset_tag', assetTag);
}
if(!gs.nil(serialNumber)) {
assetGR.addQuery('serial_number', serialNumber);
}
assetGR.query();
if(assetGR.next()) {
assetDetails['sys_id'] = assetGR.getUniqueValue();
}
pol_obj.assetTags = [];
pol_obj.assetTags.push(assetDetails);
var ship_to_obj = {
};
if(pol_obj.purchase_order.ship_to.hasOwnProperty("sys_id") && pol_obj.purchase_order.ship_to.hasOwnProperty("name"))
ship_to_obj = {
"value": pol_obj.purchase_order.ship_to.sys_id,
"displayValue": pol_obj.purchase_order.ship_to.name
};
pol_obj.ship_to = ship_to_obj;
return(new JSON().encode(pol_obj));
},
_isEmpty: function(obj){
for(var key in obj) {
if (obj.hasOwnProperty(key)) {
return false;
}
}
return true;
},
type: 'ProcurementMobileHandler'
};
Sys ID
a183d58787f62300a77a31a2f5cb0b12