Untitled

mail@pastecode.io avatar
unknown
javascript
2 years ago
9.3 kB
4
Indexable
Never
/**
 * @NApiVersion 2.0
 * @NScriptType ScheduledScript
 * @NModuleScope Public
 * @Author TC025 Gino
 * @Description Script for updating units for lease
 * Version  Date            Author      Remarks
 * 1.00     27 Apr 2020     TC025       Initial Version
 * 1.01     27 May 2020     TC025       Added updating of removing contract ref of units
 * 1.02     27 May 2020     TC025       Added updating of Unit's Lease End Date
 * 1.03     09 Jun 2020     TC025       Added saved search [SCRIPT] Scheduled Update Unit Status for unit status correction
 */

const MODULES = [
  'moment',
  'N/record',
  'N/search',
  'N/format',
  './Library/moment.js'
];

define(MODULES, function (moment,record,search,format,moment) {
  
    function execute(type){

        //(1) update unit status to occupied when contract start is today
        log.debug('entered 1','entered execute')
        var newDate = new Date();
        var contractsToday = searchItemUnits();
        var contractsEndToday = searchItemUnits2();

        log.debug('contractsToday',contractsToday);
        log.debug('contractsEndToday',contractsEndToday);

        if(contractsToday.length > 0){
            contractsToday.forEach(function (v,i){
                record.submitFields({ //set status to Occupied
                    type: 'noninventoryitem',
                    id: v.unit,
                    values: {
                        'custitem_unit_status' : 1
                    }
                });

                record.submitFields( {// update units' contract refs
                    type: 'noninventoryitem',
                    id: v.unit,
                    values: {
                        'custitem_unit_contractref' : v.contractref
                    }
                });

                record.submitFields({ // update units' end dates
                    type: 'noninventoryitem',
                    id: v.unit,
                    values: {
                        'custitem_contractenddate' : v.endD
                    }
                });

                var jsonForLogs = {
                    unitId : v.property,
                    oldStatus : v.status,
                    newStatus : 'Occupied',
                    contractId : v.contractref,
                    contractDateStart : v.startD,
                    contractDateEnd: v.endD
                };

                log.debug('Contract Starts Today', jsonForLogs);
            });
        }

        if(contractsEndToday.length > 0){
            contractsEndToday.forEach(function (v,i){
                
                record.submitFields({ //set status to Vacant
                    type: 'noninventoryitem',
                    id: v.unit,
                    values: {
                        'custitem_unit_status' : 2
                    }
                });

                record.submitFields({ //set contract ref to empty
                    type: 'noninventoryitem',
                    id: v.unit,
                    values: {
                        'custitem_unit_contractref' : ""
                    }
                });

                record.submitFields({ //set contract date to empty
                    type: 'noninventoryitem',
                    id: v.unit,
                    values: {
                        'custitem_contractenddate' : ""
                    }
                });

                var jsonForLogs = {
                    unitId : v.property,
                    oldStatus : v.status,
                    newStatus : 'Vacant',
                    contractId : v.contractref,
                    contractDateStart : v.startD,
                    contractDateEnd: v.endD
                };

                log.debug('Contract Ends Today', jsonForLogs);
                
            });
        }//End of (1)

        //(2) Correct conflicting statuses from [SCRIPT] Scheduled Update Unit Status
        // Sequence: 0 - Vacant | 1 - Reserved | 2 - Occupied

        var conflictedUFLs = searchConflictStatus();

        if(conflictedUFLs.length > 0){
            
            conflictedUFLs.forEach(function (v){
                var sequence = v.sequence;
                var unitId = v.unit;
                var contractId = v.contractId;
                var startD = v.startD;
                var endD = v.endD;

                log.debug('v',v);

                switch(sequence){
                    case '2': //sequence 2 = (1) Occupied Status
                        var shouldBeStatus = 1;
                        var forLogsStatus = 'Occupied';
                        break;
                    case '1': //sequence 1 = (3) Reserved
                        var shouldBeStatus = 3;
                        var forLogsStatus = 'Reserved';
                        break;
                    case '0': //sequence 0 = (2) Vacant
                        var shouldBeStatus = 2;
                        var forLogsStatus = 'Vacant';
                        break;
                }

                if(!!contractId && sequence == '2'){ //Has contract ID and sequence is Occupied
                    record.submitFields({
                        type: 'noninventoryitem',
                        id: unitId,
                        values: {
                            custitem_unit_contractref : contractId,
                            custitem_contractenddate : endD,
                            custitem_unit_status : shouldBeStatus
                        }
                    })
                }else{
                    record.submitFields({
                        type: 'noninventoryitem',
                        id: unitId,
                        values: {
                            custitem_unit_contractref : "",
                            custitem_contractenddate : "",
                            custitem_unit_status : shouldBeStatus
                        }
                    })
                }

                var jsonForLogs = {
                    unitId : unitId,
                    oldStatus : v.currentStatus,
                    newStatus : forLogsStatus,
                    contractId : contractId,
                    contractDateStart : startD,
                    contractDateEnd: endD
                };

                log.debug('Conflicting UFL Adjustment', jsonForLogs);
            })   
        }
        
    }

    /**
    * DONOT ALTER THIS FUNCTION
    * Retrieves all(even if data is more than 2000) 
    * search results of an nlobjSearchResultSet
    *
    * @param  {resultSet} set search result set to retrieve results
    * @return {Array}     array containing search results
    */
    var getResults = function(set) {
        var holder = [];
        var i = 0;
        while (true) {
            var result = set.getRange({
                start: i,
                end: i + 1000
                });
            if (!result) break;
                holder = holder.concat(result);
            if (result.length < 1000) break;
                i += 1000;
            }
            return holder;
    };
        
    var mapItemUnits = function(data) {
        return {
            property: data.getValue(data.columns[0]) || '',
            contractref: data.getValue(data.columns[1]) || '',
            startD: data.getValue(data.columns[2]) || '',
            endD: data.getValue(data.columns[3]) || '',
            unit: data.getValue(data.columns[4]) || '',
            status: data.getText(data.columns[6]) || '',
        };
    };

    var mapConflictItems = function(data) {
        var statusSet = data.getValue(data.columns[4]).split("|");
        return {
            unitName: data.getValue(data.columns[0]) || '',
            unit: data.getValue(data.columns[1]) || '',
            property: data.getValue(data.columns[2]) || '',
            currentStatus: data.getValue(data.columns[3]) || '',
            shouldBeSet: data.getValue(data.columns[4]) || '',
            sequence: statusSet[0].trim(),
            correctStatus: statusSet[1].trim(),
            contractId: statusSet[2].trim(),
            startD: statusSet[3].trim(),
            endD: statusSet[4].trim()
        };
    };

    var searchItemUnits = function() {
        var query = search.load({
            id: 'customsearch_uflstarttoday'
        });

        var results = getResults(query.run());
        results = results.map(mapItemUnits);
        return results;
    };

    var searchItemUnits2 = function() {
        var query = search.load({
            id: 'customsearch_uflendtoday'
        });

        var results = getResults(query.run());
        results = results.map(mapItemUnits);
        return results;
    };

    var searchConflictStatus = function() {
        var query = search.load({
            id: 'customsearch_ufl_schedupdate'
        });

        var results = getResults(query.run());
        results = results.map(mapConflictItems);
        return results;
    };

    return {
        execute: execute
    };
});