Skip to content
Snippets Groups Projects
process.js 12.39 KiB
import("Util_lib");
import("system.datetime");
import("system.translate");
import("system.db");
import("system.text");
import("Binary_lib");
import("Report_lib");
import("Sql_lib");
import("Keyword_lib");
import("Attribute_lib");
import("PostalAddress_lib");
import("Communication_lib");
import("KeywordRegistry_basic");

/**
 * a static Utility class for the Org context.
 * 
 * Do not create an instance of this!
 * @class
 */
function OrgUtils() {}//TODO: there exsits a OrgUtils and OrganisationUtils, this is inconvenient

/**
 * checks if a organisationid is the id of the dummy organisation "private"
 * 
 * @param {String} pOrganisationId the id that shall be checked
 *
 * @return {boolean} true if passed organisationid is organisationid of the dummy organisation "private"
 */
OrgUtils.isPrivateOrganisationId = function(pOrganisationId)
{
    //TODO: use this function everywhere instead of manual checks
    return pOrganisationId.trim() == OrgUtils.getPrivateOrganisationId();
}

/**
 * checks if a organsations contactid is the id of the dummy organisation "private"
 * 
 * @param {String} pOrganisationContactId the id that shall be checked
 *
 * @return {boolean} true if passed contactid is the contactid of the dummy organisation "private"
 */
OrgUtils.isPrivateOrganisationContactId = function(pOrganisationContactId)
{
    //contactid and organisationid in private dummy are equal, so it's okay to check the orgnisation-contactid against the orgnisationid
    return pOrganisationContactId.trim() == OrgUtils.getPrivateOrganisationId();
}

/**
 * returns the image for a organisation
 * 
 * @return {String} hard coded organisationid of the dummy organisation "private"
 */
OrgUtils.getPrivateOrganisationId = function()
{
    //TODO: use this function everywhere instead of hardcoded organisationids "0"
    return "0";
}

/**
 * returns the image for a organisation
 * 
 * @param {String} pOrgId the id of the organisation.
 * @param {String} pDefaultText the text, to use for default image generation.
 * @return {String} base64 coded String of the image. If none existed, the given String is used to create an image.
 */
OrgUtils.getImage = function(pOrgId, pDefaultText)
{
    return ImageUtils.get("ORGANISATION", "IMAGE", pOrgId, pDefaultText);
}

/**
 * sets the image of a organisation
 * 
 * @param {String} pOrgId the id of the organisation.
 * @param {String} pImageDateBase64 base64 coded String of the image.
 * @return {Boolean} if image could be set
 */
OrgUtils.setImage = function(pOrgId, pImageDateBase64)
{
    return ImageUtils.set("ORGANISATION", "IMAGE", pOrgId, pImageDateBase64, "OrgImage", "Image of the organisation");
}

/**
 * deletes the image of a organisation
 * 
 * @param {String} pOrgId the id of the organisation.
 * @return {Boolean} if image could be removed
 */
OrgUtils.removeImage = function(pOrgId)
{
    return ImageUtils.remove("ORGANISATION", "IMAGE", pOrgId);
}

/**
 * opens the org-report
 * 
 * @param {String} pOrgId the id of the organization
 * @param {String} pContactId the OrganisationContactId
 */
OrgUtils.openOrgReport = function(pOrgId, pContactId)
{
    //org info
    var info = newSelect("ORGANISATION.INFO")
                    .from("ORGANISATION")
                    .where("ORGANISATION.ORGANISATIONID", pOrgId)
                    .cell();
    
    //communication data of the organization
    var commData = newSelect("MEDIUM_ID, ADDR")
                    .from("COMMUNICATION")
                    .where("COMMUNICATION.CONTACT_ID", pContactId)
                    .and("ISSTANDARD = 1")
                    .table();
    
    //resolve keyword
    commData.forEach(function (row)
    {
        row[0] = KeywordUtils.getViewValue($KeywordRegistry.communicationMedium(), row[0]);
    });
    commData = ReportData.begin(["KINDOFCOMM", "COMMVALUE"]).add(commData);
    
    //select people from the organization
    var persData = newSelect("SALUTATION, TITLE, FIRSTNAME, LASTNAME "
                            + ",CONTACTROLE as PERSFUNCITON, DEPARTMENT as PERSDEPARTMENT "
                            + ",(" + CommUtil.getStandardSubSqlMail() + ")"
                            + ",(" + CommUtil.getStandardSubSqlPhone() + ")"
                            + ",ORGANISATION_ID, CONTACTID")
                        .from("PERSON")
                        .join("CONTACT", "PERSONID = PERSON_ID")
                        .where("CONTACT.ORGANISATION_ID", pOrgId)
                        .and("CONTACT.STATUS", $KeywordRegistry.contactStatus$active()) 
                        .orderBy("PERSON.LASTNAME asc")
                        .table();
    
    for (let i = 0; i < persData.length; i++)
    {
        _joinArrayVals(persData[i], 0, 4); //join the full name together
        _joinArrayVals(persData[i], 3, 2, "\n"); //join communication together
    }
    persData = ReportData.begin(["PERSNAMECOMPLETE", "PERSFUNCTION", "PERSDEPARTMENT", "PERSCOMM", "ORGANISATION_ID", "CONTACT_ID"]).add(persData);

    var activityQuery = newSelect("ENTRYDATE, CATEGORY, FIRSTNAME, LASTNAME, INFO")
                    .from("ACTIVITY")
                    .join("ACTIVITYLINK", "ACTIVITYLINK.ACTIVITY_ID = ACTIVITYID")
                    .leftJoin("CONTACT", "ACTIVITY.RESPONSIBLE = CONTACTID")
                    .leftJoin("PERSON", "CONTACT.PERSON_ID = PERSONID")
                    .where("ACTIVITYLINK.OBJECT_ROWID", pContactId)
                    .and("ACTIVITYLINK.OBJECT_TYPE", "Organisation")
                    .orderBy("ENTRYDATE desc");
                    
var activityData = activityQuery.table()
    
    var dateFormat = translate.text("dd.MM.yyyy");
    activityData.forEach(function (row) 
    {
        row[0] = datetime.toDate(row[0], dateFormat);
        // convert html to text
        row[4] = text.html2text(row[4]);
        
        row[1] = KeywordUtils.getViewValue($KeywordRegistry.activityCategory(), row[1]);
        _joinArrayVals(row, 2, 2);
    });
    activityData = ReportData.begin(["ENTRYDATE", "MEDIUM", "LOGIN", "INFO"]).add(activityData);
    
    var attr = new AttributeRelationQuery(pOrgId)
        .includeDisplayValue()
        .includeFullAttributeName()
        .getAttributes()
        .map(function (row) {return row.value ? row.fullAttributeName + ": " + row.displayValue : row.fullAttributeName})
        .join("\n");
    
    //tasks
    var taskData = newSelect("TASK.SUBJECT, TASK.DESCRIPTION, TASK.STATUS, FIRSTNAME, LASTNAME")
                    .from("TASK")
                    .join("CONTACT", "EDITOR_CONTACT_ID = CONTACTID")
                    .leftJoin("PERSON", "CONTACT.PERSON_ID = PERSONID")
                    .where("CONTACT.ORGANISATION_ID", pOrgId)
                    .table();
    
    taskData.forEach(function (row) 
    {
        row[2] = KeywordUtils.getViewValue($KeywordRegistry.taskStatus(), row[2]);
        _joinArrayVals(row, 3, 2); //join FIRSTNAME and LASTNAME together
    });
    taskData = ReportData.begin(["SUBJECT", "INFOTEXT", "STATUS", "RESPONSIBLE"]).add(taskData);
    
    var params = {
        "ORGAddr" : AddressUtils.getAddress(pContactId).toString(), //TODO: use new address logic when available
        "ORGAttr" : attr,
        "INFO" : info
    };
    
    var orgReport = new Report("Organisation_report", params);
    
    //add subreport data
    orgReport.addSubReportData("subdataComm", commData);
    orgReport.addSubReportData("subdataPers", persData);
    orgReport.addSubReportData("subdataHist", activityData);
    orgReport.addSubReportData("subdataTask", taskData);
    
    //add logo
    //TODO: use an function to get the image when available
    var imgData = [
        "meineFirma | Wilhelm-Str. 2  |  DE 80807 München",
        "base64:iVBORw0KGgoAAAANSUhEUgAAAM4AAABRCAYAAACaL5lSAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYxIDY0LjE0MDk0OSwgMjAxMC8xMi8wNy0xMDo1NzowMSAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNS4xIFdpbmRvd3MiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6M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L7bPJG/69hsqmKNaVkISIRxHPHCBvhHbk6CAwaSOw47x0JIkCscBDfRzU76frdnTuSNcPqRBDEISLRyjDfWSKUdzMCV6o+edQagcF2hPmOzf4UAyJByn8R405bsMMCU6ZneO+8bQf5nyfXSnzdznawhJhXAcxtQQL8fszpioNurnefCMEB53wJ3bvDpOMiEch7NqGHNgotpOU54+MOw7DPCOgHMqSjyEdpmHjmRROA6X1YZ07IPpAzETGqJwmJ8GU21g1gAnpI1QMt6wiZcF4r1niJLhbTQfcfxCkkQuyFwghJDZLOAuIITCIYTCIYTCIYTCIYTCIYRQOIRQOIRQOIRQOIRQOISQWvi/AAMA9UczDEaG0p8AAAAASUVORK5CYII="
    ];
    params["myAddr"] = imgData[0];
    orgReport.addImage("myLogo", imgData[1]);
    
    orgReport.openReport();
    
    /*
     * merges multiple columns in an two-dimensional array into one
     */
    function _joinArrayVals (pArr, pIndex, pHowMany, pJoinSeparator)
    {
        if (pJoinSeparator == undefined)
            pJoinSeparator = " ";
        pArr.splice(pIndex, pHowMany, ArrayUtils.joinNonEmptyFields(
            pArr.slice(pIndex, pIndex + pHowMany), pJoinSeparator)
        );
    }
}