Source: Offer_lib/process.js

import("system.translate");
import("system.db");
import("system.eMath");
import("system.tools");
import("Util_lib");

function OfferUtils(){
    var that = this;
    /**
     * Delivers the next valid offer number (has to be unique)
     * 
     * @result {String} next valid offer number
     */
    this.getNextOfferNumber = function(){
        var JdUtils = new JDitoUtils();
        return JdUtils.getNextUniqueNumber("OFFERCODE", "OFFER");
    }
    
    /**
     * Checks if the passed offer number is valid (has to be unique)
     * 
     * @param {String} pOfferNumber offer number to check
     * 
     * @result {boolean} passed number is valid
     */
    this.validateOfferNumber = function(pOfferNumber){
        var JdUtils = new JDitoUtils();
        return JdUtils.validateUniqueNumber(pOfferNumber, "OFFERCODE", "OFFER");
    }
    
    this.getOfferNumberValidationFailString = function(){
        return translate.text("The offer number already exists!");
    }
    
    this.getOfferNetAndVAT = function(pOfferId, pOfferitemIdToDel){
        //TODO: Bessere Möglichkeit als per SQL die zugehörigen Posten aus der DB zu holen?
        var sum = 0;
        var vat = 0;
        var oiUtils = new OfferItemUtils();
        
        var condition = "where OFFER_ID = '" + pOfferId + "'";
        if(pOfferitemIdToDel != undefined)
            condition += " and OFFERITEMID <> '" + pOfferitemIdToDel + "'";
        
        var offerItems = db.table("select QUANTITY, PRICE, DISCOUNT, VAT, OPTIONAL "
                                 + "from OFFERITEM " + condition);
        
        for(var i = 0; i < offerItems.length; i++){
            sum += oiUtils.getItemSum(offerItems[i][0], offerItems[i][1], offerItems[i][2], offerItems[i][4]);
            vat += oiUtils.getItemVAT(offerItems[i][0], offerItems[i][1], offerItems[i][2], offerItems[i][3], offerItems[i][4]);
        }
        
        return [sum, vat];
    }
    
    this.isEditable = function(pStatus){
        
        //TODO: Administrator darf immer ändern, warten auf neue Berechtigungslogik?
        
        //Offer should be editable if offer state not equals "Sent", "Won" or "Lost"
        return pStatus != "2" && pStatus != "3" && pStatus != "4";
    }
}

function OfferItemUtils(){
    
    this.getItemSum = function(pQuantity, pPrice, pDiscount, pOptional){
        
        pQuantity = pQuantity || 0;
        pPrice = pPrice || 0;
        pDiscount = pDiscount || 0;
        
        return Number(pOptional) * parseFloat(pQuantity) * parseFloat(pPrice) * ((100 - parseFloat(pDiscount)) / 100);
    }
    
    this.getItemVAT = function(pQuantity, pPrice, pDiscount, pVAT, pOptional){
        
        pQuantity = pQuantity || 0;
        pPrice = pPrice || 0;
        pDiscount = pDiscount || 0;
        pVAT = pVAT || 0;
        
        return Number(pOptional) * parseFloat(pQuantity) * parseFloat(pPrice) * ((100 - parseFloat(pDiscount)) / 100) * (parseFloat(pVAT) / 100);
    }
    
    this.roundPrice = function(pPrice){
        return eMath.roundDec(pPrice, 2, eMath.ROUND_HALF_UP);
    }
    
    this.insertPartsList = function()
    {
        
        
        
    }
}