1

I'm using dynamicform wbraganca to perform purchasing order operation. In dynamicform, i have item, qty, price and total. if someone edit the item or qty, it will perform calculate its total and sum of total for each row item. What i want to ask how to know the total rows in the dynamicform so i can loop to sum the total. here is my code

<div class="col-sm-8 col-md-3">
    <?= $form->field($detail, "[{$i}]item_id")->widget(Select2::className(), [
        'data' => ArrayHelper::map(Item::find()->all(), 'id', 'name'),
        'language' => 'en',
        'options' => ['placeholder' => 'Select a item ...', 'onchange' => 'getItemPrice($(this))'],
        'pluginOptions' => [
            'allowClear' => true,                               
        ],
    ]);
    ?>
</div>
<div class="col-sm-4 col-md-2">
    <?= $form->field($detail, "[{$i}]qty")->widget(MaskedInput::className(),
        [
            'clientOptions' => [
                'alias' => 'numeric',
                'groupSeparator' => ',',
                'digits' => 0,
                'autoGroup' => true,
                'removeMaskOnSubmit' => true,
                'rightAlign' => false,                                  
            ],
            'options' => [
                'class' => 'form-control',
                'onchange' => 'calculateSubtotal($(this))',                     
            ]                               
        ]) ?>
</div>
<div class="col-sm-4 col-md-2">
    <?= $form->field($detail, "[{$i}]price")->widget(MaskedInput::className(),
        [
            'clientOptions' => [
                'alias' => 'numeric',
                'groupSeparator' => ',',
                'digits' => 0,
                'autoGroup' => true,
                'removeMaskOnSubmit' => true,
                'rightAlign' => false,                              
            ],
            'options' => [
                'class' => 'form-control',
                'onchange' => 'calculateSubtotal($(this))',                                 
            ]
        ]) ?>
</div>
<div class="col-sm-4 col-md-2">
    <?= $form->field($detail, "[{$i}]total")->widget(MaskedInput::className(),
        [
            'clientOptions' => [
                'alias' => 'numeric',
                'groupSeparator' => ',',
                'digits' => 0,
                'autoGroup' => true,
                'removeMaskOnSubmit' => true,
                'rightAlign' => false,
            ]
        ]) ?>
</div>

and my javascript like this

<?php
$script = <<< JS

jQuery(".dynamicform_wrapper").on("afterInsert", function(e, item) {
    jQuery(".dynamicform_wrapper .add-item").each(function(index) {
        calculateTotal(index+1);
    });
});

jQuery(".dynamicform_wrapper").on("afterDelete", function() {
    jQuery(".dynamicform_wrapper .remove-item").each(function(index) {                
        calculateTotal(index+1);
    });
});

function getItemPrice(item){
    var index  = item.attr("id").replace(/[^0-9.]/g, "");
    var item_id = $('#purchaseorderdetail-'+ index + "-item_id").val();
    $.get('../item/get-price', {id : item_id}, function(data){
        $('#purchaseorderdetail-' + index + '-price').val(data);
        $('#purchaseorderdetail-' + index + '-qty').val(1);
        $('#purchaseorderdetail-' + index + '-total').val(data);
        calculateTotal(Number(index)+1);
    });     
}

function calculateSubtotal(item){   
    var index  = item.attr("id").replace(/[^0-9.]/g, "");   
    var qty = $('#purchaseorderdetail-' + index + '-qty').val();
    qty = qty == "" ? 0 : Number(qty.split(",").join(""));
    var price = $('#purchaseorderdetail-' + index + '-price').val();
    price = price == "" ? 0 : Number(price.split(",").join(""));
    $('#purchaseorderdetail-' + index + '-total').val(qty * price);     
    calculateTotal(Number(index)+1);
}

function calculateTotal(index){    
    var total = 0;    
    for(i=0; i< index; i++){        
        var subtotal = $('#purchaseorderdetail-' + i + '-total').val();        
        subtotal = subtotal == "" ? 0 : Number(subtotal.split(",").join(""));    
        total = total + subtotal;
    }
    $('#purchaseorder-total').val(total);
}

JS;
$this->registerJs($script, $this::POS_END);
?>

the problem when doing update, it cannot calculate all.

1 Answer 1

1

First thanks to insaneSkull who help me answer the question. here is the solution to sum the grandtotal

jQuery(".dynamicform_wrapper").on("afterInsert", function(e, item) {
    calculateTotal();   
});

jQuery(".dynamicform_wrapper").on("afterDelete", function(e) {
    calculateTotal();    
});

function getItemPrice(item){
    var index  = item.attr("id").replace(/[^0-9.]/g, "");
    var item_id = $('#purchaseorderdetail-'+ index + "-item_id").val();
    $.get('../item/get-price', {id : item_id}, function(data){
        $('#purchaseorderdetail-' + index + '-price').val(data);
        $('#purchaseorderdetail-' + index + '-qty').val(1);
        $('#purchaseorderdetail-' + index + '-total').val(data);
        calculateTotal();
    });     
}

function calculateSubtotal(item){    
    var index  = item.attr("id").replace(/[^0-9.]/g, "");   
    var qty = $('#purchaseorderdetail-' + index + '-qty').val();
    qty = qty == "" ? 0 : Number(qty.split(",").join(""));
    var price = $('#purchaseorderdetail-' + index + '-price').val();
    price = price == "" ? 0 : Number(price.split(",").join(""));
    $('#purchaseorderdetail-' + index + '-total').val(qty * price);
    calculateTotal();
}

function calculateTotal(){    
    var total = 0;        
    jQuery(".dynamicform_wrapper .remove-item").each(function(index) {
        var subtotal = $('#purchaseorderdetail-' + index + '-total').val();
        if(typeof(subtotal) != 'undefined'){
            subtotal = subtotal == "" ? 0 : Number(subtotal.split(",").join(""));    
            total = total + subtotal;    
        }        
    });
    $('#purchaseorder-total').val(total);
}

many thanks to insaneSkull

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.