Added client side calculations for budget
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Tue, 1 Jul 2008 21:17:21 +0000 (21:17 +0000)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Tue, 1 Jul 2008 21:17:21 +0000 (21:17 +0000)
gl/gl_budget.php
js/budget.js [new file with mode: 0644]

index b1a7cae559c603a99d3140aeff06e9692116cdaf..b503a61c5f3db15fccafdad1cfa6c7ad81796b9c 100644 (file)
@@ -4,6 +4,8 @@ $page_security = 10;
 $path_to_root="..";
 include($path_to_root . "/includes/session.inc");
 
+add_js_file('budget.js');
+
 page(_("Budget Entry"));
 
 include($path_to_root . "/includes/ui.inc");
@@ -84,7 +86,10 @@ if (isset($_POST['add']) || isset($_POST['delete']))
                display_notification_centered(_("The Budget has been deleted."));
        
        //meta_forward($_SERVER['PHP_SELF']);           
+       $Ajax->activate('budget_tbl');
 }      
+if (isset($_POST['submit']) || isset($_POST['update']))
+       $Ajax->activate('budget_tbl');
 
 //-------------------------------------------------------------------------------------
 
@@ -115,8 +120,9 @@ if (db_has_gl_accounts())
                hidden('dim1', 0);
                hidden('dim2', 0);
        }
-       submit_row('submit', _("Get"));
+       submit_row('submit', _("Get"), true, '', '', true);
        end_table(1);
+       div_start('budget_tbl');
        start_table($table_style2);
        $showdims = (($dim == 1 && $_POST['dim1'] == 0) || 
                ($dim == 2 && $_POST['dim1'] == 0 && $_POST['dim2'] == 0));
@@ -126,24 +132,26 @@ if (db_has_gl_accounts())
                $th = array(_("Period"), _("Amount"), _("Last Year"));
        table_header($th);      
        $year = $_POST['fyear'];
-       $sql = "SELECT * FROM ".TB_PREF."fiscal_year WHERE id=$year";
+       if (get_post('update') == '') {
+               $sql = "SELECT * FROM ".TB_PREF."fiscal_year WHERE id=$year";
 
-       $result = db_query($sql, "could not get current fiscal year");
+               $result = db_query($sql, "could not get current fiscal year");
 
-       $fyear = db_fetch($result);
-       $begin = sql2date($fyear['begin']);
-       $end = sql2date($fyear['end']);
-       hidden('begin', $begin);
-       hidden('end', $end);
+               $fyear = db_fetch($result);
+               $_POST['begin'] = sql2date($fyear['begin']);
+               $_POST['end'] = sql2date($fyear['end']);
+       }
+       hidden('begin');
+       hidden('end');
        $total = $btotal = $ltotal = 0;
-       for ($i = 0, $date_ = $begin; date1_greater_date2($end, $date_); $i++)
+       for ($i = 0, $date_ = $_POST['begin']; date1_greater_date2($_POST['end'], $date_); $i++)
        {
                start_row();
-               $_POST['amount'.$i] = number_format2(get_only_budget_trans_from_to($date_, $date_, $_POST['account'], $_POST['dim1'], $_POST['dim2']), 0); 
+               if (get_post('update') == '')
+                       $_POST['amount'.$i] = number_format2(get_only_budget_trans_from_to(
+                               $date_, $date_, $_POST['account'], $_POST['dim1'], $_POST['dim2']), 0); 
                
                label_cell($date_);     
-               if (!isset($_POST['amount'.$i]))
-                       $_POST['amount'.$i] = '0';
                amount_cells(null, 'amount'.$i, null, 15, null, 0);
                if ($showdims)
                {
@@ -160,14 +168,16 @@ if (db_has_gl_accounts())
        }
        start_row();
        label_cell("<b>"._("Total")."</b>");
-       label_cell("<b>".number_format2($total, 0)."</b>", 'align=right');
+       label_cell(number_format2($total, 0), 'align=right style="font-weight:bold"', 'Total');
        if ($showdims)
                label_cell("<b>".number_format2($btotal, 0)."</b>", "nowrap align=right");
        label_cell("<b>".number_format2($ltotal, 0)."</b>", "nowrap align=right");
        end_row();
        end_table(1);
-       submit_center_first('add', _("Save"));
-       submit_center_last('delete', _("Delete"));
+       div_end();
+       submit_center_first('update', _("Update"), '', null);
+       submit('add', _("Save"), true, '', true);
+       submit_center_last('delete', _("Delete"), '', true);
 } 
 end_form();
        
diff --git a/js/budget.js b/js/budget.js
new file mode 100644 (file)
index 0000000..9290232
--- /dev/null
@@ -0,0 +1,27 @@
+function focus_budget(i) {
+    save_focus(i);
+       i.setAttribute('_last', get_amount(i.name));
+}
+
+function blur_budget(i) {
+       var amount = get_amount(i.name);
+       var total = get_amount('Total', 1);
+       
+       if(amount<0) amount = 0;                
+       price_format(i.name, amount, 0);
+       price_format('Total', total+amount-i.getAttribute('_last'), 0, 1, 1);
+}
+
+
+var budget_calc = {
+       '.amount': function(e) {
+               e.onblur = function() {
+                       blur_budget(this);
+                 };
+               e.onfocus = function() {
+                       focus_budget(this);
+               };
+       }
+}
+
+Behaviour.register(budget_calc);