how to get the sum of a field called amount currently visible on the list view

Hi All,

            I want to get the sum of the a field called amount to be visible on the top of the list view as Total

These should only be for the records which are visible on the sugar.

For example

there is an option to load more record at the bottom on the list view,when we click on it we get the other records and the sum of their amount has also to get calculated and placed on the list view.

Can any one please give a start up for this

Regards Tevfik Tümer

Sidhu

  • HI Mehul  Bhandari

    This is what i have done previously

    /custom/modules/mymodule/clients/base/views/list-headerpane/list-headerpane.php


    $viewdefs['mymodule']['base']['view']['list-headerpane'] = array(
        'fields' => array(
            array(
                'name' => 'title',
                'type' => 'label',
                'default_value' => 'LBL_MODULE_NAME',
            ),
            array(
                'name' => 'collection-count',
                'type' => 'collection-count',
            ),
            array(
                'name' => 'total',//added by me
            ),
        ),
        'buttons' => array(
            array(
                'name'    => 'create_button',
                'type'    => 'button',
                'label'   => 'LBL_CREATE_BUTTON_LABEL',
                'css_class' => 'btn-primary',
                'acl_action' => 'create',
                'route' => array(
                    'action'=>'create'
                )
            ),
            array(
                'name' => 'sidebar_toggle',
                'type' => 'sidebartoggle',
            ),
        ),
    );

    /custom/modules/mymodule/clients/base/views/list-headerpane/list-headerpane.js


    ({
        extendsFrom: 'HeaderpaneView',
        initialize: function(options) {
            options.meta = _.extend({}, app.metadata.getView(null, 'list-headerpane'), app.metadata.getView(options.module, 'list-headerpane'), options.meta);
            this._super('initialize', [options]);
            app.shortcuts.register('List:Headerpane:Create', 'a', function() {
                var $createButton = this.$('a[name=create_button]');
                if ($createButton.is(':visible') && !$createButton.hasClass('disabled')) {
                    $createButton.get(0).click();
                }
            }, this);
             this.on('render', this.add_total_amount, this);//added by me
        },
        add_total_amount:function()
        {
            
            
            $.ajax({
                    
                        url      : 'getTotal.php',
                        type     : 'POST',
                        data:   {"total":"total"},                        
                        success: function (data)
                        {
                            sum = data;    
                             $('[data-fieldname="total"]').html("<span style='color:green'>Total:R "+sum+"</span>");
                        }
                            
                    });
        
          
          
        },
    })

    But this calculates for the whole records

    I want only the one for which they are available in the list view.

    Regards

    Sidhu

  • Hi All,

                   Can any one help me out

    Regards

    Sidhu

  • Hi All,

               Previously in 6.5 i was successful doing this

    /custom/modules/Ver/logic_hooks.php

    $hook_array['after_ui_frame'] = Array();
    $hook_array['after_ui_frame'][] = Array(1, 'Display_Sum', 'custom/modules/Ver/sum.php','SumHook', 'displayTotal');


    $hook_array['process_record'] = Array();
    $hook_array['process_record'][] = Array(1, 'Sum_Each', 'custom/modules/Ver/sum.php','SumHook', 'sumTotalFromEachRow');

    custom/modules/Ver/sum.php

    <?php
     
    class SumHook
    {
        protected static $sum = 0.0;
      
        function sumTotalFromEachRow(&$bean, $event, $arguments)
        {
            self::$sum += $bean->amount;
        }

        function displayTotal($event, $arguments)
        {
            if ($_REQUEST['action'] == 'index' || $_REQUEST['action'] == 'ListView')
            {
            
            self::$sum = number_format(self::$sum,2);
           
            ?>
            <span style=""><b>Total : R<?php echo self::$sum?></b></span>
            <?php
            }
        }
    }
    ?>

    But in 7.7.1.1 it is not working

    Regards

    Sidhu Francesca Shiekh

  • Hi All,

                    Finally had some luck

    In /custom/modules/Ver/clients/base/views/list-headerpane/list-headerpane.php copied from parent files

    added the following in the fields array

     array(
                'name' => 'total',//added by me
            ),

    Then

    custom/modules/Ver/clients/base/views/recordlist/recordlist.js

    added

    ({

        extendsFrom: 'RecordlistView',
        initialize: function (options) {
            app.view.invokeParent(this, {type: 'view', name: 'recordlist', method: 'initialize', args:[options]});
            this.on('render', this._setRowFields, this);
        },
        
        _setRowFields:function()
        {
            
             var count=0;
              var accArray = new Array();
            
            
            $('input[type=checkbox]:visible').each(function() {
                
                count++;
                
                    var acc=$(this).closest('tr').attr('name');
                    if(acc != null)
                    {       acc=acc.split("_");
                            accArray.push(acc[2]);
                    }
            });
         
            $.ajax({
                     url: "updateSum.php",
                     async:false,
                     type:"POST",
                     data:{accArray:accArray},
                     success: function (data) {
                            
                                    //console.log('sum is '+data);
                                   $('[data-fieldname="total"]').html("<span style='color:green'>Total:R "+data+"</span>");
                    
                                },
                    });
        },

    })

    And the job is done

    Regards

    Sidhu