Skip to content
Snippets Groups Projects
process.js 6.23 KiB
import("Contact_lib");
import("system.logging");
import("system.translate");
import("Entity_lib");
import("Product_lib");
import("AttributeRegistry_basic");
import("system.db");
import("system.neon");
import("system.vars");
import("KeywordRegistry_basic");
import("Util_lib");
import("Employee_lib");
import("Sql_lib");

/*
 * Functions for working with Advertisings
 * 
 * @class
 */
function AdvertisingUtils() {}


/*
 * Builds the structure for advertisings and its items
 * Inserts the structure of Advertisings into the database
 * An advertising is always linked to one specific contact
 * 
 * @param {String} pRowId - rowId of the context param e.g Activity -> ActivityId
 * @param {String} pContext - contextname to which the Advertising flow is linked to e.g Activity
 * @param {Array} pContactIds Array with possible linkable Contacts 
 * 
 * @return {void} 
 */
AdvertisingUtils.insertAdvertising = function(pRowId, pContext, pContactIds)
{
    //we can only preset the contactID when exactly one Id is passed
    var contactId = (pContactIds.length == 1) ? pContactIds : "";
    
    var advObj = {
        "ROW_ID" : pRowId,
        "OBJECT_TYPE" : pContext, 
        "CONTACT_ID" : contactId, 
        "RESPONSIBLE_ID" : EmployeeUtils.getCurrentContactId(), 
        "CURRENCY" : $KeywordRegistry.currency$eur(),
        "STATUS" : $KeywordRegistry.advertisingStatus$Open(), 
        "DATE_NEW": vars.get("$sys.date")
    }
    
    new SqlBuilder().insertFields(advObj, "ADVERTISING", "ADVERTISINGID", "ADVERTISINGID");
}

/*
 * checks if the passed rowId is linked to an Advertising
 * 
 * @param {String} pRowId - e.g activityId
 * 
 * @return {Boolean}  
 */
AdvertisingUtils.hasAdvertising = function(pRowId)
{
    var sql = new SqlBuilder()
    .select("ADVERTISING.ADVERTISINGID")
    .from("ADVERTISING")
    .where();
    
    if(pRowId)
    {
        sql.andIfSet("ADVERTISING.ROW_ID", pRowId);
    }
    
    return Utils.toBoolean(sql.cell());
}

/*
 * gets the next available item position for the passed AdvertisingId
 * 
 * @param {String} pAdvertisingId - Id of the Advertising (uId)
 * 
 * @return {int} item position 
 */
AdvertisingUtils.getNextItemPos = function (pAdvertisingId)
{
    var itemPos = 1;
    if(pAdvertisingId)
    {
        var itemPosStr = newSelect("max(ADVERTISINGITEM.ITEMPOSITION)")
        .from("ADVERTISINGITEM")
        .where("ADVERTISINGITEM.ADVERTISING_ID", pAdvertisingId)
        .cell();
        
        if(itemPosStr)
        {
            itemPos = parseInt(itemPosStr, 10); 
            itemPos++;
        }
    }
    return itemPos;
}
/*
* checks if the advertising is editable, currently it's editable when it's not sent and not followed up
* 
*  @param {String} pStatus - the current Status of an advertising 
*  
*  @return {Boolean} true if editable else false
 */
AdvertisingUtils.isEditable = function(pStatus)
{
    return pStatus != $KeywordRegistry.advertisingStatus$followedUp() && pStatus != $KeywordRegistry.advertisingStatus$Sent();
}

/*
 * checks if a product is a digital advertising material
 * 
 * @param {String} pProductId - uId of a product 
 *
 * @return {Boolean} true when it is a digital advertising material otherwise false
 */
AdvertisingUtils.isDigitalAdvertisingProduct = function (pProductId)
{
    return Utils.toBoolean(newSelect("PRODUCT.DIGITAL")
        .from("PRODUCT")
        .where("PRODUCT.PRODUCTID", pProductId)
        .and("PRODUCT.ADVERTISING", "Y")
        .cell()
        );
}

/*
 * returns the condition for the filterextension Condition located in Person
 * 
 * @return {String} additional where condition 
 */
AdvertisingUtils.getAdvertisingFilterExtensionCondition = function()
{
    var filterCond = vars.get("$local.condition");
    var columnPlaceholder = vars.get("$local.columnPlaceholder");

    var condition = StringUtils.replaceAll(filterCond, columnPlaceholder, "ADVERTISING.DATE_NEW");

    var cond = new SqlBuilder().where("CONTACT.CONTACTID", 
        newSelect("ADVERTISING.CONTACT_ID").from("ADVERTISING").where(condition), SqlBuilder.IN()).toString();
        
    return cond;
}

/*
 * gets the Title for a Pricefield with the current currency
 * 
 * @param {String} pTitle current <b>translated</b> Title which should be extended
 * @param {String} pCurrency - currency e.g 'EUR' / 'USD' 
 * 
 * @return {String} concatinated Title e.g 'Price in EUR' 
 */
AdvertisingUtils.getPriceTitleWithCurrency = function(pTitle, pCurrency)
{
    var ret = pTitle;
    
    if(pCurrency)
    {
        ret += " ";
        ret  += translate.text("in") + " " + pCurrency;
    }

    return ret;
}

/*
 * gets Advertising relevant details for the passed productId
 * 
 * @param {String} pProductId - id of the product for which the detail should be loaded
 * @param {String} pAdvertisingId - AdvertisingId
 * @param {String} pQuantity - the quantity of the product, needed to identify the pricelist
 * 
 * @return {Object} Object with productdetails
 */
AdvertisingUtils.getProductObject = function(pProductId, pAdvertisingId, pQuantity)
{
    var productId = pProductId;
    var advertisingId = pAdvertisingId;
    var quantity = pQuantity;

    var productDetails = {};
    
    if(productId)
    {
        var advInfo =  newSelect("ADVERTISING.CURRENCY, ADVERTISING.CONTACT_ID")
        .from("ADVERTISING")
        .where("ADVERTISING.ADVERTISINGID", advertisingId)
        .arrayRow();
    
        var curr = (advInfo[0]) ? advInfo[0] : "";
        var contactid = (advInfo[1]) ? advInfo[1] : "";
        var orgContactId = ContactUtils.getOrganisationContactId(contactid);
        
        var pricelist = new AttributeRelationQuery(orgContactId, $AttributeRegistry.pricelist()).getSingleAttributeValue() || "";
    
        var PriceListFilter = {
            currency: curr, 
            quantity: quantity, 
            relationId: orgContactId, 
            priceList: pricelist
        };
    
        var productInfoSubSql = newSelect("DESCRIPTION")
        .from("DESCRIPTIONTRANSLATION")
        .whereIfSet("DESCRIPTIONTRANSLATION.OBJECT_ROWID", productId)
        .and("DESCRIPTIONTRANSLATION.OBJECT_TYPE", "Product")
        .toString();
        
        productDetails = ProductUtils.getProductDetails(productId, PriceListFilter, 
            [["info", "(" + productInfoSubSql + ")"]
            ]);
    }
    return productDetails;
}