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

(-)a/C4/Budgets.pm (-3 / +26 lines)
Lines 1117-1122 sub MoveOrders { Link Here
1117
    my ($params)              = @_;
1117
    my ($params)              = @_;
1118
    my $from_budget_period_id = $params->{from_budget_period_id};
1118
    my $from_budget_period_id = $params->{from_budget_period_id};
1119
    my $to_budget_period_id   = $params->{to_budget_period_id};
1119
    my $to_budget_period_id   = $params->{to_budget_period_id};
1120
    my $move_remaining_unspent = $params->{move_remaining_unspent};
1120
    return
1121
    return
1121
      if not $from_budget_period_id
1122
      if not $from_budget_period_id
1122
          or not $to_budget_period_id
1123
          or not $to_budget_period_id
Lines 1135-1140 sub MoveOrders { Link Here
1135
            WHERE ordernumber = ?
1136
            WHERE ordernumber = ?
1136
        |
1137
        |
1137
    );
1138
    );
1139
    my $sth_update_budget_amount = $dbh->prepare(
1140
        q|
1141
            UPDATE aqbudgets
1142
            SET budget_amount = ?
1143
            WHERE budget_id = ?
1144
        |
1145
    );
1138
    my $from_budgets = GetBudgetHierarchy($from_budget_period_id);
1146
    my $from_budgets = GetBudgetHierarchy($from_budget_period_id);
1139
    for my $from_budget (@$from_budgets) {
1147
    for my $from_budget (@$from_budgets) {
1140
        my $new_budget_id = $dbh->selectcol_arrayref(
1148
        my $new_budget_id = $dbh->selectcol_arrayref(
Lines 1151-1157 sub MoveOrders { Link Here
1151
            push @report,
1159
            push @report,
1152
              {
1160
              {
1153
                moved       => 0,
1161
                moved       => 0,
1154
                budget_code => $from_budget->{budget_code},
1162
                budget      => $from_budget,
1155
                error       => 'budget_code_not_exists',
1163
                error       => 'budget_code_not_exists',
1156
              };
1164
              };
1157
            next;
1165
            next;
Lines 1166-1179 sub MoveOrders { Link Here
1166
        my @orders_moved;
1174
        my @orders_moved;
1167
        for my $order (@$orders_to_move) {
1175
        for my $order (@$orders_to_move) {
1168
            $sth_update_aqorders->execute( $new_budget->{budget_id}, $order->{ordernumber} );
1176
            $sth_update_aqorders->execute( $new_budget->{budget_id}, $order->{ordernumber} );
1169
            push @orders_moved, $order->{ordernumber};
1177
            push @orders_moved, $order;
1178
        }
1179
1180
        my $unspent_moved = 0;
1181
        if ($move_remaining_unspent) {
1182
            my $spent   = GetBudgetHierarchySpent( $from_budget->{budget_id} );
1183
            my $unspent = $from_budget->{budget_amount} - $spent;
1184
            my $new_budget_amount = $new_budget->{budget_amount};
1185
            if ( $unspent > 0 ) {
1186
                $new_budget_amount += $unspent;
1187
                $unspent_moved = $unspent;
1188
            }
1189
            $new_budget->{budget_amount} = $new_budget_amount;
1190
            $sth_update_budget_amount->execute( $new_budget_amount,
1191
                $new_budget->{budget_id} );
1170
        }
1192
        }
1171
1193
1172
        push @report,
1194
        push @report,
1173
          {
1195
          {
1174
            budget => $new_budget,
1196
            budget        => $new_budget,
1175
            orders_moved  => \@orders_moved,
1197
            orders_moved  => \@orders_moved,
1176
            moved         => 1,
1198
            moved         => 1,
1199
            unspent_moved => $unspent_moved,
1177
          };
1200
          };
1178
    }
1201
    }
1179
    return \@report;
1202
    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 => 39;
2
use Test::More tests => 69;
3
3
4
BEGIN {use_ok('C4::Budgets') }
4
BEGIN {use_ok('C4::Budgets') }
5
use C4::Context;
5
use C4::Context;
Lines 435-448 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod( Link Here
435
        budget_period_id        => $budget_period_id,
435
        budget_period_id        => $budget_period_id,
436
        budget_period_startdate => '2014-01-01',
436
        budget_period_startdate => '2014-01-01',
437
        budget_period_enddate   => '2014-12-31',
437
        budget_period_enddate   => '2014-12-31',
438
        reset_all_funds         => 1,
439
    }
438
    }
440
);
439
);
441
440
442
my $report = C4::Budgets::MoveOrders(
441
my $report = C4::Budgets::MoveOrders(
443
    {
442
    {
444
        from_budget_period_id => $budget_period_id,
443
        from_budget_period_id  => $budget_period_id,
445
        to_budget_period_id   => $budget_period_id_cloned,
444
        to_budget_period_id    => $budget_period_id_cloned,
445
        move_remaining_unspent => 1,
446
    }
446
    }
447
);
447
);
448
is( scalar( @$report ), 6 , "MoveOrders has processed 6 funds" );
448
is( scalar( @$report ), 6 , "MoveOrders has processed 6 funds" );
Lines 451-456 my $number_of_orders_moved = 0; Link Here
451
$number_of_orders_moved += scalar( @{ $_->{orders_moved} } ) for @$report;
451
$number_of_orders_moved += scalar( @{ $_->{orders_moved} } ) for @$report;
452
is( $number_of_orders_moved, $number_of_orders_to_move, "MoveOrders has moved $number_of_orders_to_move orders" );
452
is( $number_of_orders_moved, $number_of_orders_to_move, "MoveOrders has moved $number_of_orders_to_move orders" );
453
453
454
my @new_budget_ids = map { $_->{budget_id} }
455
  @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
456
my @old_budget_ids = map { $_->{budget_id} }
457
  @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
458
for my $budget_id ( keys %budgets ) {
459
    for my $ordernumber ( @{ $budgets{$budget_id} } ) {
460
        my $budget            = GetBudgetByOrderNumber($ordernumber);
461
        my $is_in_new_budgets = grep /^$budget->{budget_id}$/, @new_budget_ids;
462
        my $is_in_old_budgets = grep /^$budget->{budget_id}$/, @old_budget_ids;
463
        is( $is_in_new_budgets, 1, "MoveOrders changed the budget_id for order $ordernumber" );
464
        is( $is_in_old_budgets, 0, "MoveOrders changed the budget_id for order $ordernumber" );
465
    }
466
}
467
468
469
# MoveOrders with param move_remaining_unspent
470
my @new_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
471
my @old_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
472
473
for my $new_budget ( @new_budgets ) {
474
    my ( $old_budget ) = map { $_->{budget_code} eq $new_budget->{budget_code} ? $_ : () } @old_budgets;
475
    my $new_budget_amount_should_be = $old_budget->{budget_amount} * 2 - $old_budget->{total_spent};
476
    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})" );
477
}
478
454
sub _get_dependencies {
479
sub _get_dependencies {
455
    my ($budget_hierarchy) = @_;
480
    my ($budget_hierarchy) = @_;
456
    my $graph;
481
    my $graph;
457
- 

Return to bug 12164