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

(-)a/C4/Budgets.pm (-3 / +26 lines)
Lines 1144-1149 sub MoveOrders { Link Here
1144
    my ($params)              = @_;
1144
    my ($params)              = @_;
1145
    my $from_budget_period_id = $params->{from_budget_period_id};
1145
    my $from_budget_period_id = $params->{from_budget_period_id};
1146
    my $to_budget_period_id   = $params->{to_budget_period_id};
1146
    my $to_budget_period_id   = $params->{to_budget_period_id};
1147
    my $move_remaining_unspent = $params->{move_remaining_unspent};
1147
    return
1148
    return
1148
      if not $from_budget_period_id
1149
      if not $from_budget_period_id
1149
          or not $to_budget_period_id
1150
          or not $to_budget_period_id
Lines 1162-1167 sub MoveOrders { Link Here
1162
            WHERE ordernumber = ?
1163
            WHERE ordernumber = ?
1163
        |
1164
        |
1164
    );
1165
    );
1166
    my $sth_update_budget_amount = $dbh->prepare(
1167
        q|
1168
            UPDATE aqbudgets
1169
            SET budget_amount = ?
1170
            WHERE budget_id = ?
1171
        |
1172
    );
1165
    my $from_budgets = GetBudgetHierarchy($from_budget_period_id);
1173
    my $from_budgets = GetBudgetHierarchy($from_budget_period_id);
1166
    for my $from_budget (@$from_budgets) {
1174
    for my $from_budget (@$from_budgets) {
1167
        my $new_budget_id = $dbh->selectcol_arrayref(
1175
        my $new_budget_id = $dbh->selectcol_arrayref(
Lines 1178-1184 sub MoveOrders { Link Here
1178
            push @report,
1186
            push @report,
1179
              {
1187
              {
1180
                moved       => 0,
1188
                moved       => 0,
1181
                budget_code => $from_budget->{budget_code},
1189
                budget      => $from_budget,
1182
                error       => 'budget_code_not_exists',
1190
                error       => 'budget_code_not_exists',
1183
              };
1191
              };
1184
            next;
1192
            next;
Lines 1193-1206 sub MoveOrders { Link Here
1193
        my @orders_moved;
1201
        my @orders_moved;
1194
        for my $order (@$orders_to_move) {
1202
        for my $order (@$orders_to_move) {
1195
            $sth_update_aqorders->execute( $new_budget->{budget_id}, $order->{ordernumber} );
1203
            $sth_update_aqorders->execute( $new_budget->{budget_id}, $order->{ordernumber} );
1196
            push @orders_moved, $order->{ordernumber};
1204
            push @orders_moved, $order;
1205
        }
1206
1207
        my $unspent_moved = 0;
1208
        if ($move_remaining_unspent) {
1209
            my $spent   = GetBudgetHierarchySpent( $from_budget->{budget_id} );
1210
            my $unspent = $from_budget->{budget_amount} - $spent;
1211
            my $new_budget_amount = $new_budget->{budget_amount};
1212
            if ( $unspent > 0 ) {
1213
                $new_budget_amount += $unspent;
1214
                $unspent_moved = $unspent;
1215
            }
1216
            $new_budget->{budget_amount} = $new_budget_amount;
1217
            $sth_update_budget_amount->execute( $new_budget_amount,
1218
                $new_budget->{budget_id} );
1197
        }
1219
        }
1198
1220
1199
        push @report,
1221
        push @report,
1200
          {
1222
          {
1201
            budget => $new_budget,
1223
            budget        => $new_budget,
1202
            orders_moved  => \@orders_moved,
1224
            orders_moved  => \@orders_moved,
1203
            moved         => 1,
1225
            moved         => 1,
1226
            unspent_moved => $unspent_moved,
1204
          };
1227
          };
1205
    }
1228
    }
1206
    return \@report;
1229
    return \@report;
(-)a/admin/aqbudgetperiods.pl (-5 / +10 lines)
Lines 226-232 elsif ( $op eq 'close_form' ) { Link Here
226
226
227
        # We want to move funds from this budget
227
        # We want to move funds from this budget
228
        my $unreceived_orders = C4::Acquisition::SearchOrders(
228
        my $unreceived_orders = C4::Acquisition::SearchOrders(
229
            { budget_id => $budget->{budget_id}, } );
229
            {
230
                budget_id => $budget->{budget_id},
231
            }
232
        );
230
        $budget->{unreceived_orders} = $unreceived_orders;
233
        $budget->{unreceived_orders} = $unreceived_orders;
231
        $number_of_unreceived_orders += scalar(@$unreceived_orders);
234
        $number_of_unreceived_orders += scalar(@$unreceived_orders);
232
    }
235
    }
Lines 243-253 elsif ( $op eq 'close_form' ) { Link Here
243
}
246
}
244
247
245
elsif ( $op eq 'close_confirmed' ) {
248
elsif ( $op eq 'close_confirmed' ) {
246
    my $to_budget_period_id = $input->param('to_budget_period_id');
249
    my $to_budget_period_id    = $input->param('to_budget_period_id');
247
    my $report              = C4::Budgets::MoveOrders(
250
    my $move_remaining_unspent = $input->param('move_remaining_unspent');
251
    my $report                 = C4::Budgets::MoveOrders(
248
        {
252
        {
249
            from_budget_period_id => $budget_period_id,
253
            from_budget_period_id  => $budget_period_id,
250
            to_budget_period_id   => $to_budget_period_id,
254
            to_budget_period_id    => $to_budget_period_id,
255
            move_remaining_unspent => $move_remaining_unspent,
251
        }
256
        }
252
    );
257
    );
253
}
258
}
(-)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 => 77;
2
use Test::More tests => 107;
3
3
4
BEGIN {
4
BEGIN {
5
    use_ok('C4::Budgets')
5
    use_ok('C4::Budgets')
Lines 467-480 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod( Link Here
467
        budget_period_id        => $budget_period_id,
467
        budget_period_id        => $budget_period_id,
468
        budget_period_startdate => '2014-01-01',
468
        budget_period_startdate => '2014-01-01',
469
        budget_period_enddate   => '2014-12-31',
469
        budget_period_enddate   => '2014-12-31',
470
        reset_all_funds         => 1,
471
    }
470
    }
472
);
471
);
473
472
474
my $report = C4::Budgets::MoveOrders(
473
my $report = C4::Budgets::MoveOrders(
475
    {
474
    {
476
        from_budget_period_id => $budget_period_id,
475
        from_budget_period_id  => $budget_period_id,
477
        to_budget_period_id   => $budget_period_id_cloned,
476
        to_budget_period_id    => $budget_period_id_cloned,
477
        move_remaining_unspent => 1,
478
    }
478
    }
479
);
479
);
480
is( scalar( @$report ), 6 , "MoveOrders has processed 6 funds" );
480
is( scalar( @$report ), 6 , "MoveOrders has processed 6 funds" );
Lines 483-488 my $number_of_orders_moved = 0; Link Here
483
$number_of_orders_moved += scalar( @{ $_->{orders_moved} } ) for @$report;
483
$number_of_orders_moved += scalar( @{ $_->{orders_moved} } ) for @$report;
484
is( $number_of_orders_moved, $number_of_orders_to_move, "MoveOrders has moved $number_of_orders_to_move orders" );
484
is( $number_of_orders_moved, $number_of_orders_to_move, "MoveOrders has moved $number_of_orders_to_move orders" );
485
485
486
my @new_budget_ids = map { $_->{budget_id} }
487
  @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
488
my @old_budget_ids = map { $_->{budget_id} }
489
  @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
490
for my $budget_id ( keys %budgets ) {
491
    for my $ordernumber ( @{ $budgets{$budget_id} } ) {
492
        my $budget            = GetBudgetByOrderNumber($ordernumber);
493
        my $is_in_new_budgets = grep /^$budget->{budget_id}$/, @new_budget_ids;
494
        my $is_in_old_budgets = grep /^$budget->{budget_id}$/, @old_budget_ids;
495
        is( $is_in_new_budgets, 1, "MoveOrders changed the budget_id for order $ordernumber" );
496
        is( $is_in_old_budgets, 0, "MoveOrders changed the budget_id for order $ordernumber" );
497
    }
498
}
499
500
501
# MoveOrders with param move_remaining_unspent
502
my @new_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
503
my @old_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
504
505
for my $new_budget ( @new_budgets ) {
506
    my ( $old_budget ) = map { $_->{budget_code} eq $new_budget->{budget_code} ? $_ : () } @old_budgets;
507
    my $new_budget_amount_should_be = $old_budget->{budget_amount} * 2 - $old_budget->{total_spent};
508
    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})" );
509
}
510
486
sub _get_dependencies {
511
sub _get_dependencies {
487
    my ($budget_hierarchy) = @_;
512
    my ($budget_hierarchy) = @_;
488
    my $graph;
513
    my $graph;
489
- 

Return to bug 12164