Bug 12164: Move the budget period clone logic into C4::Budgets

Note the typo InsertInTable "aqcudgets", note sure about the existing
behavior before this patch!

This patch adds a link "Duplicate" in the action list for budget
periods (budgets).

Signed-off-by: Paola Rossi <paola.rossi@cineca.it>
Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
Bug 12164: On cloning budget period, mark original budget as inactive

This patch adds a checkbox "mark original budget as inactive" (budget
period).
If it is checked, the original budget will be marked as inactive.

Signed-off-by: Paola Rossi <paola.rossi@cineca.it>
Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
Bug 12164: On cloning budget periods, add a "reset all funds" option

This patch adds a checkbox "reset all funds" (budgets).
If it is checked, the new created budgets (funds) will be reset.

Signed-off-by: Paola Rossi <paola.rossi@cineca.it>
Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
Bug 12164: Close a budget period (budget)

This is the main patch.

On closing a budget period, all unreceived orders are moved from the
old/previous fiscal year into the new fiscal year.

You can rollover funds unused in the previous fiscal year to the new
fiscal year.

This patch set is based on bug 12168 (bugfix) and can be tested on top
of bug 11578 (easier to see the fund structure).

The patch set is cut in 6 main patches:

- Move the budget period clone logic into C4::Budgets
  The code is moved from the pl to Budgets.pm and unit tests are provided.
  The original code should certainly be buggy since a typo existed.
- On cloning budget period, mark original budget as inactive
  Cloning a budget period is already possible in Koha, this patch adds a
  checkbox to mark as inactive the original budget. That avoids to edit
  the budget and click the "inactive" checkbox. Both do the same action.
- On cloning budget periods, add a "reset all funds" option
  Same as before, a new checkbox is added on cloning a budget period. If
  you check it, all fund amounts will be set to 0. Otherwise, no change
  compared to the existing behavior.
- Close a budget period (budget)
  The goal of this patch set is to move unreceived orders from a budget to
  another. This patch adds a C4::Budgets::MoveOrders routine which does
  this job.
  This action is only possible if the fund structure is the same for both
  budgets, the budget_code field should be the same.
- On closing budget period, move unspent amount
  Unspent amount will be move from the previous budget structure to the
  new one.
- Add UI report
  This patch only adds a report when closing a budget is done.

Test plan:
Wording: below, budget is a "budget period" and fund is a "budget".
Prerequisite: Having 1 active budget with some funds (with different
levels and different amounts). Order and receive some orders (not all).
1/ Go on the budgets administration page (admin/aqbudgetperiods.pl) and
duplicate the structure of this budget ("Duplicate" link in the
"Actions" column).
2/ Enter start and end date for this budget and mark the original budget
as inactive.
3/ Note that a new budget is created, with the same fund structures (and
same value) and that the old one is marked as inactive (see
admin/aqbudgets.pl page with patches from bug 11578).
4/ Try to close the new budget: it is not possible, there is no
unreceived orders for this budget.
5/ You can close the inactive budget ("Close" link in the "Actions"
column).
6/ Verify the number of "Unreceived orders" is correct and select the
new budget in the budget list. Click on the "Move remaining unspent
funds" if you want to move unspent amounts.
7/ A report view is displayed and show you the ordernumber which have
been impacted (grouped by fund).
8/ Try different configuration, depending on the selected checkboxes.

Signed-off-by: Paola Rossi <paola.rossi@cineca.it>
Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
Bug 12164: On closing budget period, move unspent amount

On closing a budget (budget period), the user will be presented with an
option to move remaining unspent funds from the previous budget to the
newly created one - adding to the amounts already entered in those
funds.

Signed-off-by: Paola Rossi <paola.rossi@cineca.it>
Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
Bug 12164: Add UI report

Signed-off-by: Paola Rossi <paola.rossi@cineca.it>
Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
Bug 12164: follow-up: various fixes

This follow-up fixes issues raised on bug 12164 comment 17.

1/ The unreceived orders columns should only display "pending" orders
2/ Fix some typo from_budget_period_id vs budget_period_id vs
to_budget_period_id

Signed-off-by: Paola Rossi <paola.rossi@cineca.it>
Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
Bug 12164: Fix some typos

Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
Bug 12164: Fix decimal number display

If the funds are not moved, the amount is display like 123.000000.
This patch hardcodes the number of decimal numbers to display (2).

Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
Passes QA script and tests, especially all acquisition tests.

New routines in Budgets.pm:
  - CloneBudgetPeriod
  - CloneBudgetHierarchy
  - MoveOrders

OK t/db_dependent/Budgets.t

Some of the tests done:

1) Duplicate an existing budget

- OK Existing budget is active, duplicate it and mark it inactive
- OK Existing budget is inactive, duplicate and mark it inactive

2) Close budget
- OK Budget was created without funds - no changes are made
- OK Budget was created with funds, all orders moved correctly
  - moving the available money to the new budget is optional
- OK Budget was created with funds, but one fund deleted,
     all orders moved correctly, one missing fund reported,
     orders unchanged

3) Partial receiced undone
- OK Partial receive from last budget is undone, order lines
     are correctly recombined and fund is set to the current budget

Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
Bug 12164: (followup) MoveOrders description corrected

Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>