$j(document).ready(function () {
    setTimeout(function () {
        income_Total_calc();
    }, 500);
    setTimeout(function () {
        expense_Total_calc();
    }, 500);
    $j(document).on('hidden.bs.modal', '.modal', function () {
        var montant_entree = localStorage.getItem('montant_entree');
        if (montant_entree) {
            montant_entree = parseFloat(montant_entree);
            var total_entree = parseFloat($j('#total_entree').text());
            var total_sortie = parseFloat($j('#total_sortie').text());
            var solde = parseFloat($j('#solde').text());
            if ($j.isNumeric(total_entree) && $j.isNumeric(total_sortie) && $j.isNumeric(solde) && $j.isNumeric(montant_entree)) {
                total_entree = total_entree + montant_entree;
                solde = total_entree - total_sortie;
                $j('#total_entree').html(total_entree.toFixed(2));
                $j('#total_sortie').html(total_sortie.toFixed(2));
                $j('#solde').html(solde.toFixed(2));
                localStorage.removeItem('montant_entree');
                setTimeout(function () {
                    income_Total_calc();
                }, 800);
            } else {
                localStorage.removeItem('montant_entree');
            }
        } else {
            localStorage.removeItem('montant_entree');
        }

        var expense_amount = localStorage.getItem('montant_sortie');
        if (montant_sortie) {
            montant_sortie = parseFloat(montant_sortie);
            var total_entree = parseFloat($j('#total_entree').text());
            var total_sortie = parseFloat($j('#total_sortie').text());
            var solde = parseFloat($j('#solde').text());
            if ($j.isNumeric(total_entree) && $j.isNumeric(total_sortie) && $j.isNumeric(solde) && $j.isNumeric(montant_sortie)) {
                total_sortie = total_sortie + montant_sortie;
                solde = total_entree - total_sortie;
                $j('#total_entree').html(total_entree.toFixed(2));
                $j('#total_sortie').html(total_sortie.toFixed(2));
                $j('#solde').html(solde.toFixed(2));
                localStorage.removeItem('montant_sortie');
                setTimeout(function () {
                    expense_Total_calc();
                }, 800);
            } else {
                localStorage.removeItem('montant_sortie');
            }
        } else {
            localStorage.removeItem('montant_sortie');
        }
    });
});

function income_Total_calc() {
    $j('#panel_incomes-caisse table tbody').append('<tr><td  colspan="3" class="text-right">Total</td> <td class="text-right" id="total_entree_ictable"></td><td></td></tr>');
    var income = 0;
    $j('.incomes-amount').each(function () {
        var new_income = $j(this).text();
        if ($j.isNumeric(new_income)) {
            income = parseFloat(income) + parseFloat(new_income);
            $j('#total_entree_ictable').html(parseFloat(income).toFixed(2));
        }
    });
}

function expense_Total_calc() {
    $j('#panel_expenses-caisse table tbody').append('<tr><td  colspan="3" class="text-right">Total</td> <td class="text-right" id="total_sortie_etable"></td><td></td></tr>');
    var expense = 0;
    $j('.expenses-amount').each(function () {
        var new_expense = $j(this).text();
        if ($j.isNumeric(new_expense)) {
            expense = parseFloat(expense) + parseFloat(new_expense);
            $j('#total_sortie_etable').html(parseFloat(expense).toFixed(2));
        }
    });
}
