Total Amount Calculation (Master-Details Table) in Oracle Apex

 


If you have page item "TOTAL_AMOUNT" (MASTER TABLE)

and "AMOUNT" column in the Interactive Grid (DETAILS TABLE)


See my table Structure :

 Master table

SQL> desc Purchase_master

 Name                                      Null?    Type

 ----------------------------------------- -------- -----------------

 BILL_NO                               NOT NULL  NUMBER(10)

 PURCHASE_DATE                                     DATE

 SUPPLIER_ID                                            NUMBER(10)

 DISCOUNT_PCT                                        NUMBER(11,2)

 DISCOUNT_AMOUNT                              NUMBER(11,2)

 TOTAL_AMOUNT                                    NUMBER(11,2)

 VAT_PCT                                                   NUMBER(11,2)

 VAT_AMOUNT                                         NUMBER(11,2)

 NET_AMOUNT                                        NUMBER(11,2)

 PAID                                                         NUMBER(11,2)

 DUE                                                          NUMBER(11,2)

 

Details table

SQL> desc Purchase_details

 Name                                      Null?    Type

 ----------------------------------------- -------- ------------------------

 ID                                                NOT NULL   NUMBER(10)

 BILL_NO                                   NOT NULL    NUMBER(10)

 ANIMAL_TYPE                                               VARCHAR2(25)

BUY_PRICE                                                      NUMBER(10)

 QTY                                                                   NUMBER(10)

 AMOUNT                                                          NUMBER(11,2)

====================================================================


------Flowing this steps---->

You may need to go through the following instructions: 

 AMOUNT    = Interactive Grid Column.

:P30_TOTAL_AMOUNT = Sum/Total in the page Item.


Instructions :-

1 - Static ID - tots ( "tots" copy to past Purchase_details region -- Static ID)

2 - Find and replace if necessary "AMOUNT" and "P30_TOTAL_AMOUNT" in the below code.

3 - Now copy and paste the below jquery code to "Function and Global Variable Declaration" (copy and paste "Page 30: purchase")



var htmldb_delete_message='"DELETE_CONFIRM_MSG"';

// create a private scope where $ is set to apex.jQuery

(function($) {

    // This is the function that calculates over all the rows of the model and then

    // updates something else.

    // Change this to do whatever calculation is needed.

    // Call this whenever the model data changes.

    function update(model) {

        var salKey = model.getFieldKey("AMOUNT"),

            total = 0;


        console.log(">> starting sum AMOUNT column")

        model.forEach(function(record, index, id) {

            var sal = parseFloat(record[salKey]),  // record[salKey] should be a little faster than using model.getValue in a loop

                meta = model.getRecordMetadata(id);


            if (!isNaN(sal) && !meta.deleted && !meta.agg) {

                total += sal;

            }

        });

        console.log(">> setting sum SAL column to " + total)

        $s("P30_TOTAL_AMOUNT", total);

    }


    //

    // This is the general pattern for subscribing to model notifications

    //


    // need to do this here rather than in Execute when Page Loads so that the handler

    // is setup BEFORE the IG is initialized otherwise miss the first model created event

    $(function() {

        // the model gets released and created at various times such as when the report changes

        // listen for model created events so that we can subscribe to model notifications

        $("#tots").on("interactivegridviewmodelcreate", function(event, ui) {

            var sid,

                model = ui.model;


            // note this is only done for the grid veiw. It could be done for

            // other views if desired. The imporant thing to realize is that each

            // view has its own model

            if ( ui.viewId === "grid" ) {

                sid = model.subscribe( {

                    onChange: function(type, change) {

                        console.log(">> model changed ", type, change);

                        if ( type === "set" ) {

                            // don't bother to recalculate if other columns change

                            if (change.field === "AMOUNT" ) {

                                update( model );

                            }

                        } else if (type !== "move" && type !== "metaChange") {

                            // any other change except for move and metaChange affect the calculation

                            update( model );

                        }

                    },

                    progressView: $("#P30_TOTAL_AMOUNT") // this will cause a spinner on this field (P30_TOTAL_AMOUNT is page Item: We can change it)


                } );

                // if not lazy loaded there is no notification for initial data so update

                update( model );

                // just in case fetch all the data. Model notifications will

                // cause calls to update so nothing to do in the callback function.

                // can remove if data will always be less than 50 records

                model.fetchAll(function() {});

            }

        });


    });

})(apex.jQuery);


==============================================================

var htmldb_delete_message='"DELETE_CONFIRM_MSG"';

// create a private scope where $ is set to apex.jQuery

(function($) {

    // This is the function that calculates over all the rows of the model and then

    // updates something else.

    // Change this to do whatever calculation is needed.

    // Call this whenever the model data changes.

    function update(model) {

        var salKey = model.getFieldKey("AMOUNT"),

            total = 0;

        console.log(">> starting sum AMOUNT column")

        model.forEach(function(record, index, id) {

            var sal = parseFloat(record[salKey]).toFixed(2),  // record[salKey] should be a little faster than using model.getValue in a loop

                meta = model.getRecordMetadata(id);

            if (!isNaN(sal) && !meta.deleted && !meta.agg) {

                total = parseFloat(total) +  parseFloat(sal);

            }

        });

        console.log(">> setting sum SAL column to " + total)

        $s("P30_TOTAL_AMOUNT", total.toFixed(2));

    }

    //

    // This is the general pattern for subscribing to model notifications

    //

    // need to do this here rather than in Execute when Page Loads so that the handler

    // is setup BEFORE the IG is initialized otherwise miss the first model created event

    $(function() {

        // the model gets released and created at various times such as when the report changes

        // listen for model created events so that we can subscribe to model notifications

        $("#tots").on("interactivegridviewmodelcreate", function(event, ui) {

            var sid,

                model = ui.model;

            // note this is only done for the grid veiw. It could be done for

            // other views if desired. The imporant thing to realize is that each

            // view has its own model

            if ( ui.viewId === "grid" ) {

                sid = model.subscribe( {

                    onChange: function(type, change) {

                        console.log(">> model changed ", type, change);

                        if ( type === "set" ) {

                            // don't bother to recalculate if other columns change

                            if (change.field === "AMOUNT" ) {

                                update( model );

                            }

                        } else if (type !== "move" && type !== "metaChange") {

                            // any other change except for move and metaChange affect the calculation

                            update( model );

                        }

                    },

                    progressView: $("#P30_TOTAL_AMOUNT") // this will cause a spinner on this field (P30_TOTAL_AMOUNT is page Item: We can change it)

                } );

                // if not lazy loaded there is no notification for initial data so update

                update( model );

                // just in case fetch all the data. Model notifications will

                // cause calls to update so nothing to do in the callback function.

                // can remove if data will always be less than 50 records

                model.fetchAll(function() {});

            }

        });

    });

})(apex.jQuery);



Post a Comment

Previous Post Next Post