Skip to content
Snippets Groups Projects
onInsert.js 2.53 KiB
Newer Older
import("Contact_lib");
import("Keyword_lib");
import("Employee_lib");
import("system.translate");
import("system.notification");
import("KeywordRegistry_basic");
import("Sql_lib");
import("system.db");
import("system.vars");
import("system.neon");
import("system.tools");

var planningObj = JSON.parse(vars.get("$param.PlanningIds_param"));
var status = vars.get("$field.STATUS");
var responsible = vars.get("$field.RESPONSIBLE");
var table = "FORECAST";
var cols;
var vals;
var planningIds;


planningIds = planningObj.filter(function(oneObj){

    var cond = oneObj["STATUS"] != $KeywordRegistry.forecastStatus$locked();

    if(status)
    {
        cond = cond || tools.currentUserHasRole("INTERNAL_ADMINISTRATOR");
    }

    return cond;
}).map(function(oneObj)
{
    return oneObj["FORECASTID"];
});


if(status)
{
    cols = ["STATUS"];
    vals = [status];

}
else if(responsible)
{
    cols = ["RESPONSIBLE_CONTACT_ID"];
    vals = [responsible];
}

if(planningIds.length > 0)
{
    db.updateData(table, cols, null, vals, newWhere("FORECAST.FORECASTID", planningIds, SqlBuilder.IN()).toString());
}

var notUpdatedRecordsLength = (planningObj.length - planningIds.length);
var descriptionText = "%0 out of %1 records were changed to \"%2\".\n %3 record/s could not be updated.";

var captionStatus = translate.withArguments("%0 status was/were changed", [planningIds.length]);
var captionResponsible = translate.withArguments("%0 responsilbe/s was/were changed", [planningIds.length]);
var descriptionStatus = translate.withArguments(descriptionText, [planningIds.length, planningObj.length, KeywordUtils.getViewValue($KeywordRegistry.forecastStatus(), status), notUpdatedRecordsLength]);
var descriptionResponsible = translate.withArguments(descriptionText, [planningIds.length, planningObj.length, ContactUtils.getFullTitleByContactId(responsible), notUpdatedRecordsLength]);

var notificationConfig = notification.createConfig()
                            .addUserWithId(EmployeeUtils.getCurrentUserId())
                            .forcedPriority(notification.PRIO_NONE)
                            .notificationType("_____SYSTEM_NOTIFICATION_MESSAGE")
                            .initialState(notification.STATE_UNSEEN)
                            .daysToLive(1);

if(status)
{
    notificationConfig.caption(captionStatus)
                      .description(descriptionStatus);
}
else if(responsible)
{
    notificationConfig.caption(captionResponsible)
                      .description(descriptionResponsible);
}

notification.addNotificationWith(notificationConfig);