0 votes

Hi,

Efficy version : 10.2

In the TabContentProds (Edit window of Documents and Opportunities), I need that the client scripts in MacroEditS do some computations following the value of some fields of PRODUCTS
I mean for example the have to do 2+2 if PRODUCTS.FAMILY==1 or 2*2 if PRODUCTS.FAMILY != 1

But, I do not want to put the value of PRODUCTS.FAMILY in the html (example) :

<tr class="mission" data-family=<#F=FAMILY;nospace=T> [...] </tr>

and use it in MacroEditS like

$('.mission').each(function() {
  var family = $(this).data('family');
})

I would prefer to use the Model, is it possible to add 'EditEntityGridProd' like 'EditEntityGridActi' in 'JsonEditModel.Docu/editEntityGrids' ?

Am I obliged to use '<%GetRelationGrid([...])%>' in the Model ? This function tag is already used to generate the html grid in 'TabContentProds' so can I not generate this part of the model here ?

Last question : How to access it in the script ?

// 1 because I will place it in the array after EditEntityGridActi
var ProductsData = Model('editEntityGrids[1]')
// or
var ProductsData = Model('editEntityGrids')[1]
asked in How to by (259 points)

1 Answer

0 votes

Dear m.chalavon,

I don't understand why you don't want to use the family field in the tr ?
What do you tried to attend ?

For my point of view, I will have done the customization this way:

In order to be able to access the PRODUCTS.FAMILY from the GetRelationGrid you will need to add it in the SYS_TABLEVIEWS name = 'Docu_Prod', column "SELECTFIELDS" and don't forget to set the columns ISCUSTOM to '1' and to apply the DB structure from Efficy Desginer.

DOCU_PROD.*, PRODUCTS.NAME, PRODUCTS.FAMILY, PRODUCTS.OPENED

Once you have done that you will be able to acces the field FAMILY directly in the GetRelationGrid. And so you can override the Macro "TabContentProds " by adding the column "FAMILY".

TabContentProds.Docu {[
    <div class="content tabs-content-item" id="tab-prod">
        <div class="row">
            <div class="small-12 medium-1 columns">
                <button type="button" class="icon i-add <%IfReadOnly(detail='Prod', then='disabled')%>" title="<%GetLabel('Add Link to Product')%>" data-msg="addProduct"></button>
                <%AddRelationButton(entity='Prod')%>
            </div>
            <div class="small-12 medium-11 columns">
                <h3 class="section-title"><%GetLabel("Linked products")%></h3>
            </div>
        </div>
        <%OnMultiCurrency(0=, else=|<div id="currcyError"><span class="error"><%GetLabel("Warning: The currency must be set before adding products")%></span></div>|)%>
        <%Delay(1)%>GetRelationGrid(columndefs="GridEditColumns.txt", entity="Prod",
            columns="FULLNAME,QUANTITY,PRICE,DISCOUNT,COMMENT,VAT,TOTAL,CONSO_RATE{HIDDEN},CONSO_TOTAL{HIDDEN},FAMILY,<%IfReadOnly(else=|SORTDELETE|)%>")%>
    </div>
]}

In order not do display the value, you will have to add the following code in GridEditColumnsCustom.txt :

th.FAMILY.Prod {[#EMPTY#]}
td.FAMILY.Prod {[<input id="DETAIL-PROD-<#G>-$FIELD$" value="<#F=$FIELD$;nospace=T>" type="hidden">]}

Now in your MacroEditSCustom.txt you can override the standard macro "JsScriptsTRW.Docu", in order to do your calculation (cf comment //Custom where I changed information from standard functions)

function ComputeTotal(K_Product) {
    var
        Mode = Model('multicurrencyMode'),
        MForm = document.forms[0],
        Prefix = "DETAIL-PROD-" + K_Product,
        Qtity = GetScriptNumeric(MForm[Prefix + "-QUANTITY"].value),
        Price = GetScriptNumeric(MForm[Prefix + "-PRICE"].value),
        Family = GetScriptNumeric($('#' + Prefix + '-FAMILY').val()), //<< Custom
        Discount = GetScriptNumeric(MForm[Prefix + "-DISCOUNT"].value),
        Total = (Family == '1') ? Math.round( (Qtity+Qtity) * Price * (100 - Discount)) / 100 : Math.round(Qtity * Qtity * Price * (100 - Discount)) / 100, //<< Custom not sure it is the right calculation you want
        ConsoRate = GetScriptNumeric(MForm[Prefix + "-CONSO_RATE"].value);
    MForm[Prefix + "-TOTAL"].value = GetDatabaseNumeric(Total);
    MForm[Prefix + "-CONSO_TOTAL"].value = GetDatabaseNumeric(Total * ConsoRate);
    ComputeAll();
}

        function ComputeAll() {
        var MForm = document.forms[0]
        var IsInvoice = false
        if (MForm["CATEGORY-DOCU$INVOICING-TOTAL_NO_VAT"]) IsInvoice = true

        var Total = 0
        var TotalVAT = 0
        var TotalNoVAT = 0
        var ConsoRate = 0
        var ConsoTotal = 0
        var ConsoTotalVAT = 0
        var ConsoTotalNoVAT = 0

        if (IsInvoice) {
            for (var i = 0; i < MForm.elements.length; i++) {
                var ProdInfo = MForm.elements[i].name
                if ((ProdInfo) && (ProdInfo.indexOf("DETAIL-PROD-") == 0) && (ProdInfo.indexOf("-TOTAL") > 0)) {
                    var Prefix = ProdInfo.substring(0, ProdInfo.lastIndexOf("-TOTAL"))
                    var Qtity    = GetScriptNumeric(MForm[Prefix + "-QUANTITY"].value)
                    var Price    = GetScriptNumeric(MForm[Prefix + "-PRICE"].value)
                    var Discount = GetScriptNumeric(MForm[Prefix + "-DISCOUNT"].value)
                    var VAT      = GetScriptNumeric(MForm[Prefix + "-VAT"].value)
                    var ConsoRate = GetScriptNumeric(MForm[Prefix + "-CONSO_RATE"].value)

                    var Family = GetScriptNumeric($('#' + Prefix + '-FAMILY').val()) //<< Custom

                    var AmountNoVAT = (Family == '1') ? Math.round( (Qtity + Qtity) * Price * (100 - Discount)) / 100 : Math.round(Qtity * Qtity * Price * (100 - Discount)) / 100; //<< Custom not sure it is the right calculation you want
                    var AmountVAT = Math.round(AmountNoVAT * VAT) / 100;
                    TotalNoVAT += AmountNoVAT
                    TotalVAT += AmountVAT
                    Total += AmountNoVAT + AmountVAT
                    ConsoTotalNoVAT += AmountNoVAT * ConsoRate;
                    ConsoTotalVAT += AmountVAT * ConsoRate;
                    ConsoTotal += (AmountNoVAT + AmountVAT) * ConsoRate;
                }
            }

            var PrePaid = GetScriptNumeric(MForm["CATEGORY-DOCU$INVOICING-PRE_PAID"].value)
            var Expenses = GetScriptNumeric(MForm["CATEGORY-DOCU$INVOICING-EXPENSES"].value)
            var ConsoExpenses = Expenses * ConsoRate;

            MForm["CATEGORY-DOCU$INVOICING-TOTAL_NO_VAT"].value = GetDatabaseNumeric(FormatCurrency(TotalNoVAT))
            MForm["CATEGORY-DOCU$INVOICING-TOTAL_VAT"].value = GetDatabaseNumeric(FormatCurrency(TotalVAT))
            MForm["CATEGORY-DOCU$INVOICING-TOTAL_WITH_VAT"].value = GetDatabaseNumeric(FormatCurrency(Total - Expenses))
            MForm["CATEGORY-DOCU$INVOICING-TOTAL_TO_PAY"].value = GetDatabaseNumeric(FormatCurrency(Total - PrePaid - Expenses))

            MForm["CATEGORY-DOCU$INVOICING-CONSO_TOTAL_NO_VAT"].value = GetDatabaseNumeric(FormatCurrency(ConsoTotalNoVAT))
            MForm["CATEGORY-DOCU$INVOICING-CONSO_TOTAL_VAT"].value = GetDatabaseNumeric(FormatCurrency(ConsoTotalVAT))
            MForm["CATEGORY-DOCU$INVOICING-CONSO_TOTAL_WITH_VAT"].value = GetDatabaseNumeric(FormatCurrency(ConsoTotal - ConsoExpenses))
        }
    }

Best Regards,
Stéphane Roncin

answered by (1.9k points)
edited by
Welcome to Efficy Overflow, where you can ask questions and receive answers from other members of the community.
1,249 questions
1,518 answers
1,858 comments
328 users