Budget Entry: ajax page reload on params change, added atomic functions for budget...
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Fri, 26 Jul 2019 13:29:43 +0000 (15:29 +0200)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Mon, 19 Aug 2019 10:41:58 +0000 (12:41 +0200)
gl/gl_budget.php
gl/includes/gl_db.inc
includes/ui/ui_lists.inc

index 6c2ad64b3fc66b6e05833fcf1554957bb78f5ef1..949d7fd56db7871890c47f1257a99107eb88ec61 100644 (file)
@@ -29,26 +29,27 @@ check_db_has_gl_account_groups(_("There are no account groups defined. Please de
 
 if (isset($_POST['add']) || isset($_POST['delete']))
 {
-       begin_transaction();
 
-       for ($i = 0, $da = $_POST['begin']; date1_greater_date2($_POST['end'], $da); $i++)
-       {
-               if (isset($_POST['add']))
-                       add_update_gl_budget_trans($da, $_POST['account'], $_POST['dim1'], $_POST['dim2'], input_num('amount'.$i));
-               else
-                       delete_gl_budget_trans($da, $_POST['account'], $_POST['dim1'], $_POST['dim2']);
+       $amounts = array();
+       for ($i = 0, $da = $_POST['begin']; date1_greater_date2($_POST['end'], $da); $i++) {
+               $amounts[$da] = input_num('amount'.$i);
                $da = add_months($da, 1);
        }
-       commit_transaction();
+       if (isset($_POST['add'])) {
 
-       if (isset($_POST['add']))
+               add_budget($_POST['account'], $_POST['dim1'], $_POST['dim2'], $amounts);
                display_notification_centered(_("The Budget has been saved."));
-       else
+
+       } else {
+
+               delete_budget($_POST['account'], $_POST['dim1'], $_POST['dim2'], array_keys($amounts));
                display_notification_centered(_("The Budget has been deleted."));
 
+       }
+
        $Ajax->activate('budget_tbl');
 }
-if (isset($_POST['submit']) || isset($_POST['update']))
+if (isset($_POST['submit']) || isset($_POST['update']) || list_updated('fyear') || list_updated('account'))
        $Ajax->activate('budget_tbl');
 
 //-------------------------------------------------------------------------------------
@@ -59,20 +60,20 @@ if (db_has_gl_accounts())
 {
        $dim = get_company_pref('use_dimension');
        start_table(TABLESTYLE2);
-       fiscalyears_list_row(_("Fiscal Year:"), 'fyear', null);
-       gl_all_accounts_list_row(_("Account Code:"), 'account', null);
+       fiscalyears_list_row(_("Fiscal Year:"), 'fyear', null, true);
+       gl_all_accounts_list_row(_("Account Code:"), 'account', null, true, false, false, true);
        if (!isset($_POST['dim1']))
                $_POST['dim1'] = 0;
        if (!isset($_POST['dim2']))
                $_POST['dim2'] = 0;
     if ($dim == 2)
     {
-               dimensions_list_row(_("Dimension")." 1", 'dim1', $_POST['dim1'], true, null, false, 1);
-               dimensions_list_row(_("Dimension")." 2", 'dim2', $_POST['dim2'], true, null, false, 2);
+               dimensions_list_row(_("Dimension")." 1", 'dim1', $_POST['dim1'], true, null, false, 1, true);
+               dimensions_list_row(_("Dimension")." 2", 'dim2', $_POST['dim2'], true, null, false, 2, true);
        }
        elseif ($dim == 1)
        {
-               dimensions_list_row(_("Dimension"), 'dim1', $_POST['dim1'], true, null, false, 1);
+               dimensions_list_row(_("Dimension"), 'dim1', $_POST['dim1'], true, null, false, 1, true);
                hidden('dim2', 0);
        }
        else
index 2577ecc381b7523636d2cca2575888f94a2858d9..951af9ee9832214c4666e36b9038b3d810e85473 100644 (file)
@@ -21,3 +21,23 @@ include_once($path_to_root . "/gl/includes/db/gl_db_rates.inc");
 include_once($path_to_root . "/gl/includes/db/gl_db_accounts.inc");
 include_once($path_to_root . "/gl/includes/db/gl_db_account_types.inc");
 //--------------------------------------------------------------------------------
+
+function add_budget($account, $dim1, $dim2, $amounts)
+{
+       begin_transaction();
+
+       foreach($amounts as $date => $amount) {
+               add_update_gl_budget_trans($date, $account, $dim1, $dim2, $amount);
+       }
+       commit_transaction();
+}
+
+function delete_budget($account, $dim1, $dim2, $dates)
+{
+       begin_transaction();
+
+       foreach($amounts as $date) {
+               delete_gl_budget_trans($date, $account, $dim1, $dim2);
+       }
+       commit_transaction();
+}
index 58a2bae2215f2c2165cb273c54d1d28add045731..b803ac3215b40c4f9231d2ab5742c5e96983b142 100644 (file)
@@ -761,19 +761,19 @@ function _format_fiscalyears($row)
        . "&nbsp;&nbsp;" . ($row[3] ? _('Closed') : _('Active'));
 }
 
-function fiscalyears_list_cells($label, $name, $selected_id=null)
+function fiscalyears_list_cells($label, $name, $selected_id=null, $submit_on_change=false)
 {
        if ($label != null)
                echo "<td>$label</td>\n";
        echo "<td>";
-       echo fiscalyears_list($name, $selected_id);
+       echo fiscalyears_list($name, $selected_id, $submit_on_change);
        echo "</td>\n";
 }
 
-function fiscalyears_list_row($label, $name, $selected_id=null)
+function fiscalyears_list_row($label, $name, $selected_id=null, $submit_on_change=false)
 {
        echo "<tr><td class='label'>$label</td>";
-       fiscalyears_list_cells(null, $name, $selected_id);
+       fiscalyears_list_cells(null, $name, $selected_id, $submit_on_change);
        echo "</tr>\n";
 }
 //------------------------------------------------------------------------------------
@@ -1907,11 +1907,11 @@ function gl_all_accounts_list_cells($label, $name, $selected_id=null,
 }
 
 function gl_all_accounts_list_row($label, $name, $selected_id=null, 
-       $skip_bank_accounts=false, $cells=false, $all_option=false, $type_id=false)
+       $skip_bank_accounts=false, $cells=false, $all_option=false, $submit_on_change=false, $all=false, $type_id=false)
 {
        echo "<tr><td class='label'>$label</td>";
        gl_all_accounts_list_cells(null, $name, $selected_id, 
-               $skip_bank_accounts, $cells, $all_option, $type_id);
+               $skip_bank_accounts, $cells, $all_option, $submit_on_change, $all, $type_id);
        echo "</tr>\n";
 }