View | Details | Raw Unified | Return to bug 12164
Collapse All | Expand All

(-)a/C4/Budgets.pm (-3 / +26 lines)
Lines 1109-1114 sub MoveOrders { Link Here
1109
    my ($params)              = @_;
1109
    my ($params)              = @_;
1110
    my $from_budget_period_id = $params->{from_budget_period_id};
1110
    my $from_budget_period_id = $params->{from_budget_period_id};
1111
    my $to_budget_period_id   = $params->{to_budget_period_id};
1111
    my $to_budget_period_id   = $params->{to_budget_period_id};
1112
    my $move_remaining_unspent = $params->{move_remaining_unspent};
1112
    return
1113
    return
1113
      if not $from_budget_period_id
1114
      if not $from_budget_period_id
1114
          or not $to_budget_period_id
1115
          or not $to_budget_period_id
Lines 1127-1132 sub MoveOrders { Link Here
1127
            WHERE ordernumber = ?
1128
            WHERE ordernumber = ?
1128
        |
1129
        |
1129
    );
1130
    );
1131
    my $sth_update_budget_amount = $dbh->prepare(
1132
        q|
1133
            UPDATE aqbudgets
1134
            SET budget_amount = ?
1135
            WHERE budget_id = ?
1136
        |
1137
    );
1130
    my $from_budgets = GetBudgetHierarchy($from_budget_period_id);
1138
    my $from_budgets = GetBudgetHierarchy($from_budget_period_id);
1131
    for my $from_budget (@$from_budgets) {
1139
    for my $from_budget (@$from_budgets) {
1132
        my $new_budget_id = $dbh->selectcol_arrayref(
1140
        my $new_budget_id = $dbh->selectcol_arrayref(
Lines 1143-1149 sub MoveOrders { Link Here
1143
            push @report,
1151
            push @report,
1144
              {
1152
              {
1145
                moved       => 0,
1153
                moved       => 0,
1146
                budget_code => $from_budget->{budget_code},
1154
                budget      => $from_budget,
1147
                error       => 'budget_code_not_exists',
1155
                error       => 'budget_code_not_exists',
1148
              };
1156
              };
1149
            next;
1157
            next;
Lines 1158-1171 sub MoveOrders { Link Here
1158
        my @orders_moved;
1166
        my @orders_moved;
1159
        for my $order (@$orders_to_move) {
1167
        for my $order (@$orders_to_move) {
1160
            $sth_update_aqorders->execute( $new_budget->{budget_id}, $order->{ordernumber} );
1168
            $sth_update_aqorders->execute( $new_budget->{budget_id}, $order->{ordernumber} );
1161
            push @orders_moved, $order->{ordernumber};
1169
            push @orders_moved, $order;
1170
        }
1171
1172
        my $unspent_moved = 0;
1173
        if ($move_remaining_unspent) {
1174
            my $spent   = GetBudgetHierarchySpent( $from_budget->{budget_id} );
1175
            my $unspent = $from_budget->{budget_amount} - $spent;
1176
            my $new_budget_amount = $new_budget->{budget_amount};
1177
            if ( $unspent > 0 ) {
1178
                $new_budget_amount += $unspent;
1179
                $unspent_moved = $unspent;
1180
            }
1181
            $new_budget->{budget_amount} = $new_budget_amount;
1182
            $sth_update_budget_amount->execute( $new_budget_amount,
1183
                $new_budget->{budget_id} );
1162
        }
1184
        }
1163
1185
1164
        push @report,
1186
        push @report,
1165
          {
1187
          {
1166
            budget => $new_budget,
1188
            budget        => $new_budget,
1167
            orders_moved  => \@orders_moved,
1189
            orders_moved  => \@orders_moved,
1168
            moved         => 1,
1190
            moved         => 1,
1191
            unspent_moved => $unspent_moved,
1169
          };
1192
          };
1170
    }
1193
    }
1171
    return \@report;
1194
    return \@report;
(-)a/admin/aqbudgetperiods.pl (-5 / +10 lines)
Lines 222-228 elsif ( $op eq 'close_form' ) { Link Here
222
222
223
        # We want to move funds from this budget
223
        # We want to move funds from this budget
224
        my $unreceived_orders = C4::Acquisition::SearchOrders(
224
        my $unreceived_orders = C4::Acquisition::SearchOrders(
225
            { budget_id => $budget->{budget_id}, } );
225
            {
226
                budget_id => $budget->{budget_id},
227
            }
228
        );
226
        $budget->{unreceived_orders} = $unreceived_orders;
229
        $budget->{unreceived_orders} = $unreceived_orders;
227
        $number_of_unreceived_orders += scalar(@$unreceived_orders);
230
        $number_of_unreceived_orders += scalar(@$unreceived_orders);
228
    }
231
    }
Lines 239-249 elsif ( $op eq 'close_form' ) { Link Here
239
}
242
}
240
243
241
elsif ( $op eq 'close_confirmed' ) {
244
elsif ( $op eq 'close_confirmed' ) {
242
    my $to_budget_period_id = $input->param('to_budget_period_id');
245
    my $to_budget_period_id    = $input->param('to_budget_period_id');
243
    my $report              = C4::Budgets::MoveOrders(
246
    my $move_remaining_unspent = $input->param('move_remaining_unspent');
247
    my $report                 = C4::Budgets::MoveOrders(
244
        {
248
        {
245
            from_budget_period_id => $budget_period_id,
249
            from_budget_period_id  => $budget_period_id,
246
            to_budget_period_id   => $to_budget_period_id,
250
            to_budget_period_id    => $to_budget_period_id,
251
            move_remaining_unspent => $move_remaining_unspent,
247
        }
252
        }
248
    );
253
    );
249
}
254
}
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgetperiods.tt (+4 lines)
Lines 382-387 Link Here
382
              [% END %]
382
              [% END %]
383
            </select>
383
            </select>
384
          </li>
384
          </li>
385
          <li>
386
          <label for="move_remaining_unspent">Move remaining unspent funds</label>
387
            <input type="checkbox" name="move_remaining_unspent" id="move_remaining_unspent" />
388
          </li>
385
        </ol>
389
        </ol>
386
      </fieldset>
390
      </fieldset>
387
      <fieldset class="action">
391
      <fieldset class="action">
(-)a/t/db_dependent/Budgets.t (-5 / +29 lines)
Lines 1-5 Link Here
1
use Modern::Perl;
1
use Modern::Perl;
2
use Test::More tests => 37;
2
use Test::More tests => 67;
3
3
4
BEGIN {use_ok('C4::Budgets') }
4
BEGIN {use_ok('C4::Budgets') }
5
use C4::Biblio;
5
use C4::Biblio;
Lines 430-443 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod( Link Here
430
        budget_period_id        => $budget_period_id,
430
        budget_period_id        => $budget_period_id,
431
        budget_period_startdate => '2014-01-01',
431
        budget_period_startdate => '2014-01-01',
432
        budget_period_enddate   => '2014-12-31',
432
        budget_period_enddate   => '2014-12-31',
433
        reset_all_funds         => 1,
434
    }
433
    }
435
);
434
);
436
435
437
my $report = C4::Budgets::MoveOrders(
436
my $report = C4::Budgets::MoveOrders(
438
    {
437
    {
439
        from_budget_period_id => $budget_period_id,
438
        from_budget_period_id  => $budget_period_id,
440
        to_budget_period_id   => $budget_period_id_cloned,
439
        to_budget_period_id    => $budget_period_id_cloned,
440
        move_remaining_unspent => 1,
441
    }
441
    }
442
);
442
);
443
is( scalar( @$report ), 6 , "MoveOrders has processed 6 funds" );
443
is( scalar( @$report ), 6 , "MoveOrders has processed 6 funds" );
Lines 446-451 my $number_of_orders_moved = 0; Link Here
446
$number_of_orders_moved += scalar( @{ $_->{orders_moved} } ) for @$report;
446
$number_of_orders_moved += scalar( @{ $_->{orders_moved} } ) for @$report;
447
is( $number_of_orders_moved, $number_of_orders_to_move, "MoveOrders has moved $number_of_orders_to_move orders" );
447
is( $number_of_orders_moved, $number_of_orders_to_move, "MoveOrders has moved $number_of_orders_to_move orders" );
448
448
449
my @new_budget_ids = map { $_->{budget_id} }
450
  @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
451
my @old_budget_ids = map { $_->{budget_id} }
452
  @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
453
for my $budget_id ( keys %budgets ) {
454
    for my $ordernumber ( @{ $budgets{$budget_id} } ) {
455
        my $budget            = GetBudgetByOrderNumber($ordernumber);
456
        my $is_in_new_budgets = grep /^$budget->{budget_id}$/, @new_budget_ids;
457
        my $is_in_old_budgets = grep /^$budget->{budget_id}$/, @old_budget_ids;
458
        is( $is_in_new_budgets, 1, "MoveOrders changed the budget_id for order $ordernumber" );
459
        is( $is_in_old_budgets, 0, "MoveOrders changed the budget_id for order $ordernumber" );
460
    }
461
}
462
463
464
# MoveOrders with param move_remaining_unspent
465
my @new_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
466
my @old_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
467
468
for my $new_budget ( @new_budgets ) {
469
    my ( $old_budget ) = map { $_->{budget_code} eq $new_budget->{budget_code} ? $_ : () } @old_budgets;
470
    my $new_budget_amount_should_be = $old_budget->{budget_amount} * 2 - $old_budget->{total_spent};
471
    is( $new_budget->{budget_amount} + 0, $new_budget_amount_should_be, "MoveOrders updated the budget amount with the previous unspent budget (for budget $new_budget->{budget_code})" );
472
}
473
449
sub _get_dependencies {
474
sub _get_dependencies {
450
    my ($budget_hierarchy) = @_;
475
    my ($budget_hierarchy) = @_;
451
    my $graph;
476
    my $graph;
452
- 

Return to bug 12164