From: Janusz Dobrowolski Date: Fri, 26 Jul 2019 12:18:51 +0000 (+0200) Subject: Costs Accruals: code cleanup. X-Git-Url: https://delta.frontaccounting.com/gitweb/?p=fa-stable.git;a=commitdiff_plain;h=947c0dddc6cb94572e1f0cafe6d8c5663c00d695 Costs Accruals: code cleanup. --- diff --git a/gl/accruals.php b/gl/accruals.php index 3fc2b427..aabd9a82 100644 --- a/gl/accruals.php +++ b/gl/accruals.php @@ -30,6 +30,58 @@ include_once($path_to_root . "/includes/ui.inc"); $_SESSION['page_title'] = _($help_context = "Revenue / Cost Accruals"); page($_SESSION['page_title'], false, false,'', $js); +function generate_accruals($amount, $date, $freq, $periods) +{ + $per = $periods - 1; + $date_ = $date; + + if ($freq == 3 || $freq == 4) { + $date_ = begin_month($date_); // avoid skip on shorter months + $date = end_month($date_); // avoid skip on shorter months + } + + $lastdate = ($freq == 1 ? add_days($date_, 7*$per) : + ($freq == 2 ? add_days($date_, 14*$per) : + ($freq == 3 ? end_month(add_months($date_, $per)) : + end_month(add_months($date_, 3*$per))))); + if (!is_date_in_fiscalyears($lastdate, false)) + { + display_error(_("Some of the period dates are outside the fiscal year or are closed for further data entry. Create a new fiscal year first!")); + return null; + } + $amount = input_num('amount'); + $am = round2($amount / $periods, user_price_dec()); + if ($am * $periods != $amount) + $am0 = $am + $amount - $am * $periods; + else + $am0 = $am; + + $amounts = array(); + for ($i = 0; $i < $periods; $i++) + { + $amounts[$date] = $am0; + switch($freq) + { + case 1: + $date = $date_ = add_days($date_, 7); + break; + case 2: + $date = $date_ = add_days($date_, 14); + break; + case 3: + $date_ = add_months($date_, 1); + $date = end_month($date_); + break; + case 4: + $date_ = add_months($date_, 3); + $date = end_month($date_); + break; + } + $am0 = $am; + } + return $amounts; +} + //-------------------------------------------------------------------------------------------------- if (!isset($_POST['freq'])) $_POST['freq'] = 3; @@ -63,40 +115,34 @@ if (isset($_POST['go']) || isset($_POST['show'])) } if ($input_error == 0) { - $periods = input_num('periods'); - $per = $periods - 1; - $date = $date_ = get_post('date_'); - $freq = get_post('freq'); - if ($freq == 3 || $freq == 4) { - $date_ = begin_month($date_); // avoid skip on shorter months - $date = end_month($date_); // avoid skip on shorter months - } - - $lastdate = ($freq == 1 ? add_days($date_, 7*$per) : - ($freq == 2 ? add_days($date_, 14*$per) : - ($freq == 3 ? end_month(add_months($date_, $per)) : - end_month(add_months($date_, 3*$per))))); - if (!is_date_in_fiscalyears($lastdate, false)) - { - display_error(_("Some of the period dates are outside the fiscal year or are closed for further data entry. Create a new fiscal year first!")); - set_focus('date_'); - $input_error = 1; - } - if ($input_error == 0) + $accruals = generate_accruals(input_num('amount'), get_post('date_'), get_post('freq'), get_post('periods')); + + if ($accruals) { - $amount = input_num('amount'); - $am = round2($amount / $periods, user_price_dec()); - if ($am * $periods != $amount) - $am0 = $am + $amount - $am * $periods; - else - $am0 = $am; if (get_post('memo_') != "") $memo = $_POST['memo_']; else - $memo = sprintf(_("Accruals for %s"), $amount); - if (isset($_POST['go'])) + $memo = sprintf(_("Accruals for %s"), input_num('amount')); + + if (isset($_POST['go'])) { + begin_transaction(); - else + foreach($accruals as $date => $amount) { + $cart = new items_cart(ST_JOURNAL); + $cart->memo_ = $memo; + $cart->reference = $Refs->get_next(ST_JOURNAL, null, $date); + $cart->tran_date = $cart->doc_date = $cart->event_date = $date; + $cart->add_gl_item(get_post('acc_act'), 0, 0, -$amount, $cart->reference); + $cart->add_gl_item(get_post('res_act'), get_post('dimension_id'), + get_post('dimension2_id'), $amount, $cart->reference); + write_journal_entries($cart); + $cart->clear_items(); + commit_transaction(); + + display_notification_centered(_("Revenue / Cost Accruals have been processed.")); + $_POST['date_'] = $_POST['amount'] = $_POST['periods'] = ""; + } + } else { start_table(TABLESTYLE); $dim = get_company_pref('use_dimension'); @@ -114,43 +160,7 @@ if (isset($_POST['go']) || isset($_POST['show'])) $th = array_merge($first_cols, $dim_cols, $remaining_cols); table_header($th); $k = 0; - } - for ($i = 0; $i < $periods; $i++) - { - if ($i > 0) - { - switch($freq) - { - case 1: - $date = $date_ = add_days($date_, 7); - break; - case 2: - $date = $date_ = add_days($date_, 14); - break; - case 3: - $date_ = add_months($date_, 1); - $date = end_month($date_); - break; - case 4: - $date_ = add_months($date_, 3); - $date = end_month($date_); - break; - } - $am0 = $am; - } - if (isset($_POST['go'])) - { - $cart = new items_cart(ST_JOURNAL); - $cart->memo_ = $memo; - $cart->reference = $Refs->get_next(ST_JOURNAL, null, $date); - $cart->tran_date = $cart->doc_date = $cart->event_date = $date; - $cart->add_gl_item(get_post('acc_act'), 0, 0, -$am0, $cart->reference); - $cart->add_gl_item(get_post('res_act'), get_post('dimension_id'), - get_post('dimension2_id'), $am0, $cart->reference); - write_journal_entries($cart); - $cart->clear_items(); - } - else + foreach ($accruals as $date => $amount) { alt_table_row_color($k); label_cell($date); @@ -159,7 +169,7 @@ if (isset($_POST['go']) || isset($_POST['show'])) label_cell(""); if ($dim > 1) label_cell(""); - display_debit_or_credit_cells($am0 * -1); + display_debit_or_credit_cells($amount * -1); label_cell($memo); alt_table_row_color($k); label_cell($date); @@ -168,18 +178,9 @@ if (isset($_POST['go']) || isset($_POST['show'])) label_cell(get_dimension_string($_POST['dimension_id'], true)); if ($dim > 1) label_cell(get_dimension_string($_POST['dimension2_id'], true)); - display_debit_or_credit_cells($am0); + display_debit_or_credit_cells($amount); label_cell($memo); } - } - if (isset($_POST['go'])) - { - commit_transaction(); - display_notification_centered(_("Revenue / Cost Accruals have been processed.")); - $_POST['date_'] = $_POST['amount'] = $_POST['periods'] = ""; - } - else - { end_table(1); display_notification_centered(_("Showing GL Transactions.")); }