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

(-)a/C4/Acquisition.pm (+6 lines)
Lines 1731-1736 sub SearchOrders { Link Here
1731
    my $pending = $params->{pending};
1731
    my $pending = $params->{pending};
1732
    my $ordered = $params->{ordered};
1732
    my $ordered = $params->{ordered};
1733
    my $biblionumber = $params->{biblionumber};
1733
    my $biblionumber = $params->{biblionumber};
1734
    my $budget_id = $params->{budget_id};
1734
1735
1735
    my $dbh = C4::Context->dbh;
1736
    my $dbh = C4::Context->dbh;
1736
    my @args = ();
1737
    my @args = ();
Lines 1824-1829 sub SearchOrders { Link Here
1824
        push @args, $userenv->{'number'};
1825
        push @args, $userenv->{'number'};
1825
    }
1826
    }
1826
1827
1828
    if ( $budget_id ) {
1829
        $query .= ' AND aqorders.budget_id = ?';
1830
        push @args, $budget_id;
1831
    }
1832
1827
    $query .= ' ORDER BY aqbasket.basketno';
1833
    $query .= ' ORDER BY aqbasket.basketno';
1828
1834
1829
    my $sth = $dbh->prepare($query);
1835
    my $sth = $dbh->prepare($query);
(-)a/C4/Budgets.pm (+78 lines)
Lines 1041-1046 sub CloneBudgetPeriod { Link Here
1041
1041
1042
    $budget_period->{budget_period_startdate} = $budget_period_startdate;
1042
    $budget_period->{budget_period_startdate} = $budget_period_startdate;
1043
    $budget_period->{budget_period_enddate}   = $budget_period_enddate;
1043
    $budget_period->{budget_period_enddate}   = $budget_period_enddate;
1044
    # The new budget (budget_period) should be active by default
1045
    $budget_period->{budget_period_active}    = 1;
1044
    my $original_budget_period_id = $budget_period->{budget_period_id};
1046
    my $original_budget_period_id = $budget_period->{budget_period_id};
1045
    delete $budget_period->{budget_period_id};
1047
    delete $budget_period->{budget_period_id};
1046
    my $new_budget_period_id = AddBudgetPeriod( $budget_period );
1048
    my $new_budget_period_id = AddBudgetPeriod( $budget_period );
Lines 1127-1132 sub CloneBudgetHierarchy { Link Here
1127
    }
1129
    }
1128
}
1130
}
1129
1131
1132
=head2 MoveOrders
1133
1134
  my $report = MoveOrders({
1135
    from_budget_period_id => $from_budget_period_id,
1136
    to_budget_period_id   => $to_budget_period_id,
1137
  });
1138
1139
Clone a budget hierarchy.
1140
1141
=cut
1142
1143
sub MoveOrders {
1144
    my ($params)              = @_;
1145
    my $from_budget_period_id = $params->{from_budget_period_id};
1146
    my $to_budget_period_id   = $params->{to_budget_period_id};
1147
    return
1148
      if not $from_budget_period_id
1149
          or not $to_budget_period_id
1150
          or $from_budget_period_id == $to_budget_period_id;
1151
1152
    # Can't move orders to an inactive budget (budgetperiod)
1153
    my $budget_period = GetBudgetPeriod($to_budget_period_id);
1154
    return unless $budget_period->{budget_period_active};
1155
1156
    my @report;
1157
    my $dbh     = C4::Context->dbh;
1158
    my $sth_update_aqorders = $dbh->prepare(
1159
        q|
1160
            UPDATE aqorders
1161
            SET budget_id = ?
1162
            WHERE ordernumber = ?
1163
        |
1164
    );
1165
    my $from_budgets = GetBudgetHierarchy($from_budget_period_id);
1166
    for my $from_budget (@$from_budgets) {
1167
        my $new_budget_id = $dbh->selectcol_arrayref(
1168
            q|
1169
                SELECT budget_id
1170
                FROM aqbudgets
1171
                WHERE budget_period_id = ?
1172
                    AND budget_code = ?
1173
            |, {}, $to_budget_period_id, $from_budget->{budget_code}
1174
        );
1175
        $new_budget_id = $new_budget_id->[0];
1176
        my $new_budget = GetBudget( $new_budget_id );
1177
        unless ( $new_budget ) {
1178
            push @report,
1179
              {
1180
                moved       => 0,
1181
                budget_code => $from_budget->{budget_code},
1182
                error       => 'budget_code_not_exists',
1183
              };
1184
            next;
1185
        }
1186
        my $orders_to_move = C4::Acquisition::SearchOrders(
1187
            {
1188
                budget_id => $from_budget->{budget_id},
1189
                pending   => 1,
1190
            }
1191
        );
1192
1193
        my @orders_moved;
1194
        for my $order (@$orders_to_move) {
1195
            $sth_update_aqorders->execute( $new_budget->{budget_id}, $order->{ordernumber} );
1196
            push @orders_moved, $order->{ordernumber};
1197
        }
1198
1199
        push @report,
1200
          {
1201
            budget => $new_budget,
1202
            orders_moved  => \@orders_moved,
1203
            moved         => 1,
1204
          };
1205
    }
1206
    return \@report;
1207
}
1130
1208
1131
END { }    # module clean-up code here (global destructor)
1209
END { }    # module clean-up code here (global destructor)
1132
1210
(-)a/admin/aqbudgetperiods.pl (+47 lines)
Lines 205-210 elsif ( $op eq 'duplicate_budget' ){ Link Here
205
    $op = 'else';
205
    $op = 'else';
206
}
206
}
207
207
208
elsif ( $op eq 'close_form' ) {
209
210
    my $budget_period = GetBudgetPeriod($budget_period_id);
211
212
    my $active_budget_periods =
213
      C4::Budgets::GetBudgetPeriods( { budget_period_active => 1 } );
214
215
    # Remove the budget period from the list
216
    $active_budget_periods =
217
      [ map { ( $_->{budget_period_id} == $budget_period_id ) ? () : $_ }
218
          @$active_budget_periods ];
219
220
    my $budgets_to_move = GetBudgetHierarchy($budget_period_id);
221
222
    # C4::Context->userenv->{branchcode}, $show_mine ? $borrower_id : '')
223
224
    my $number_of_unreceived_orders = 0;
225
    for my $budget (@$budgets_to_move) {
226
227
        # We want to move funds from this budget
228
        my $unreceived_orders = C4::Acquisition::SearchOrders(
229
            { budget_id => $budget->{budget_id}, } );
230
        $budget->{unreceived_orders} = $unreceived_orders;
231
        $number_of_unreceived_orders += scalar(@$unreceived_orders);
232
    }
233
234
    $template->param(
235
        close_form       => 1,
236
        budget_period_id => $budget_period_id,
237
        budget_period_description =>
238
          $budget_period->{budget_period_description},
239
        budget_periods              => $active_budget_periods,
240
        budgets_to_move             => $budgets_to_move,
241
        number_of_unreceived_orders => $number_of_unreceived_orders,
242
    );
243
}
244
245
elsif ( $op eq 'close_confirmed' ) {
246
    my $to_budget_period_id = $input->param('to_budget_period_id');
247
    my $report              = C4::Budgets::MoveOrders(
248
        {
249
            from_budget_period_id => $budget_period_id,
250
            to_budget_period_id   => $to_budget_period_id,
251
        }
252
    );
253
}
254
208
# DEFAULT - DISPLAY AQPERIODS TABLE
255
# DEFAULT - DISPLAY AQPERIODS TABLE
209
# -------------------------------------------------------------------
256
# -------------------------------------------------------------------
210
# display the list of budget periods
257
# display the list of budget periods
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgetperiods.tt (-2 / +89 lines)
Lines 4-9 Link Here
4
[% INCLUDE 'doc-head-close.inc' %]
4
[% INCLUDE 'doc-head-close.inc' %]
5
[% INCLUDE 'calendar.inc' %]
5
[% INCLUDE 'calendar.inc' %]
6
[% INCLUDE 'datatables.inc' %]
6
[% INCLUDE 'datatables.inc' %]
7
[% IF close_form %]
8
    <link href="[% interface %]/lib/jquery/plugins/treetable/stylesheets/jquery.treetable.css" rel="stylesheet" type="text/css" />
9
    <script type="text/javascript" src="[% interface %]/lib/jquery/plugins/treetable/jquery.treetable.js"></script>
10
[% END %]
7
<script type="text/javascript" src="[% themelang %]/js/acq.js"></script>
11
<script type="text/javascript" src="[% themelang %]/js/acq.js"></script>
8
<script type="text/javascript">
12
<script type="text/javascript">
9
// #################################################################################
13
// #################################################################################
Lines 84-91 Link Here
84
                { "aTargets": [ -1 ], "bSortable": false, "bSearchable": false },
88
                { "aTargets": [ -1 ], "bSortable": false, "bSearchable": false },
85
                { "sType": "title-string", "aTargets" : [ "title-string" ] }
89
                { "sType": "title-string", "aTargets" : [ "title-string" ] }
86
            ],
90
            ],
87
            "bPaginate": false
91
            "bPaginate": false,
92
            'bAutoWidth': false
88
        } ) );
93
        } ) );
94
95
        [% IF close_form %]
96
          $("#budgeth").dataTable($.extend(true, {}, dataTablesDefaults, {
97
            sDom: "t"
98
          }));
99
          $("#move_form").submit(function(){
100
            var budget_from = "[% budget_period_description %]";
101
            var budget_to = $("#budget_period").find("option:selected").html();
102
            var alert_message = _("You have chosen to move all unreceived orders from '%s' to '%s'.").format(budget_from, budget_to);
103
            alert_message += _("\nThis action cannot be reversed. Do you wish to continue?");
104
            return confirm ( alert_message );
105
          });
106
        [% END %]
89
    });
107
    });
90
</script>
108
</script>
91
109
Lines 106-111 Link Here
106
    [% IF ( delete_confirmed ) %]&rsaquo;
124
    [% IF ( delete_confirmed ) %]&rsaquo;
107
        Data deleted
125
        Data deleted
108
    [% END %]
126
    [% END %]
127
    [% IF close_form %]&rsaquo;
128
      Close budget [% budget_period_description %]
129
    [% END %]
109
</title>
130
</title>
110
131
111
132
Lines 150-155 Link Here
150
    [% IF ( duplicate_form ) %]
171
    [% IF ( duplicate_form ) %]
151
        <a href="/cgi-bin/koha/admin/aqbudgetperiods.pl">Budgets</a> &rsaquo; Duplicate budget
172
        <a href="/cgi-bin/koha/admin/aqbudgetperiods.pl">Budgets</a> &rsaquo; Duplicate budget
152
    [% END %]
173
    [% END %]
174
175
    <!-- close a budget -->
176
    [% IF close_form %]
177
        <a href="/cgi-bin/koha/admin/aqbudgetperiods.pl">Budgets</a> &rsaquo;
178
        Close budget <a href="cgi-bin/koha/admin/aqbudgets.pl?budget_period_id=[% budget_period_id %]">[% budget_period_description %]</a>
179
    [% END %]
153
    <!-- display budget periods list -->
180
    <!-- display budget periods list -->
154
    <!-- ########################################## -->
181
    <!-- ########################################## -->
155
    [% IF ( else ) %]
182
    [% IF ( else ) %]
Lines 162-168 Link Here
162
<div id="yui-main">
189
<div id="yui-main">
163
<div class="yui-b">
190
<div class="yui-b">
164
191
165
[% INCLUDE 'budgets-admin-toolbar.inc' %]
192
[% UNLESS close_form %]
193
  [% INCLUDE 'budgets-admin-toolbar.inc' %]
194
[% END %]
166
195
167
[% IF ( duplicate_form ) %]
196
[% IF ( duplicate_form ) %]
168
<h3>Duplicate budget</h3>
197
<h3>Duplicate budget</h3>
Lines 309-314 Link Here
309
338
310
    </div>
339
    </div>
311
[% END %]
340
[% END %]
341
342
[% IF close_form %]
343
  [% IF budget_periods.size == 0 %]
344
    You cannot move funds of this budget, there is no active budget.
345
    Please create a new active budget and retry.
346
    <a href="/cgi-bin/koha/admin/aqbudgetperiods.pl">Back</a>
347
  [% ELSIF number_of_unreceived_orders == 0 %]
348
    There is no unreceived orders for this budget.
349
    <a href="/cgi-bin/koha/admin/aqbudgetperiods.pl">Back</a>
350
  [% ELSE %]
351
    <h3>Choose the funds you want to move unreceived orders:</h3>
352
      Fund list of budget <a href="cgi-bin/koha/admin/aqbudgets.pl?budget_period_id=[% budget_period_id %]">[% budget_period_description %]</a>:
353
    <table id="budgeth">
354
      <thead>
355
        <tr>
356
            <th>Fund id</th>
357
            <th>Fund code</th>
358
            <th>Fund name</th>
359
            <th>Unreceived orders</th>
360
        </tr>
361
      </thead>
362
      <tbody>
363
        [% FOREACH budget IN budgets_to_move %]
364
          <tr>
365
            <td>[% budget.budget_id %]</td>
366
            <td>[% budget.budget_code_indent %]</td>
367
            <td>[% budget.budget_name %]</td>
368
            <td>[% budget.unreceived_orders.size %]</td>
369
          </tr>
370
        [% END %]
371
      </tbody>
372
    </table>
373
    <form action="/cgi-bin/koha/admin/aqbudgetperiods.pl" name="f" method="post" id="move_form">
374
      <fieldset class="rows">
375
        <ol>
376
          <li>
377
            <label class="required" for="to_budget_period_id">Select a budget</label>
378
            <select name="to_budget_period_id" id="to_budget_period_id" required="required">
379
              <option value=""></option>
380
              [% FOR budget_period IN budget_periods %]
381
                <option value="[% budget_period.budget_period_id %]">[% budget_period.budget_period_description %]</option>
382
              [% END %]
383
            </select>
384
          </li>
385
        </ol>
386
      </fieldset>
387
      <fieldset class="action">
388
          <input type="hidden" name="op" value="close_confirmed" />
389
          <input type="hidden" name="budget_period_id" value="[% budget_period_id %]" />
390
          <input type="submit" value="Move unreceive orders" />
391
          <a class="cancel" href="/cgi-bin/koha/admin/aqbudgetperiods.pl">Cancel</a>
392
      </fieldset>
393
    </form>
394
  [% END %]
395
[% END %]
396
312
<!--  DEFAULT  display budget periods list -->
397
<!--  DEFAULT  display budget periods list -->
313
[% IF ( else ) %]
398
[% IF ( else ) %]
314
  <h2>Budgets administration</h2>
399
  <h2>Budgets administration</h2>
Lines 350-355 Link Here
350
                  <a href="[% script_name %]?op=add_form&amp;budget_period_id=[% period_active.budget_period_id |html %]">Edit</a>
435
                  <a href="[% script_name %]?op=add_form&amp;budget_period_id=[% period_active.budget_period_id |html %]">Edit</a>
351
                  <a href="[% script_name %]?op=delete_confirm&amp;budget_period_id=[% period_active.budget_period_id %]">Delete</a>
436
                  <a href="[% script_name %]?op=delete_confirm&amp;budget_period_id=[% period_active.budget_period_id %]">Delete</a>
352
                  <a href="[% script_name %]?op=duplicate_form&amp;budget_period_id=[% period_active.budget_period_id %]">Duplicate</a>
437
                  <a href="[% script_name %]?op=duplicate_form&amp;budget_period_id=[% period_active.budget_period_id %]">Duplicate</a>
438
                  <a href="[% script_name %]?op=close_form&amp;budget_period_id=[% period_active.budget_period_id %]">Close</a>
353
                  <a href="/cgi-bin/koha/admin/aqbudgets.pl?op=add_form&amp;budget_period_id=[% period_active.budget_period_id %]">Add fund</a>
439
                  <a href="/cgi-bin/koha/admin/aqbudgets.pl?op=add_form&amp;budget_period_id=[% period_active.budget_period_id %]">Add fund</a>
354
                </td>
440
                </td>
355
                </tr>
441
                </tr>
Lines 390-395 Link Here
390
                      <a href="[% script_name %]?op=add_form&amp;budget_period_id=[% period_loo.budget_period_id |html %]">Edit</a>
476
                      <a href="[% script_name %]?op=add_form&amp;budget_period_id=[% period_loo.budget_period_id |html %]">Edit</a>
391
                      <a href="[% script_name %]?op=delete_confirm&amp;budget_period_id=[% period_loo.budget_period_id %]">Delete</a>
477
                      <a href="[% script_name %]?op=delete_confirm&amp;budget_period_id=[% period_loo.budget_period_id %]">Delete</a>
392
                      <a href="[% script_name %]?op=duplicate_form&amp;budget_period_id=[% period_loo.budget_period_id %]">Duplicate</a>
478
                      <a href="[% script_name %]?op=duplicate_form&amp;budget_period_id=[% period_loo.budget_period_id %]">Duplicate</a>
479
                      <a href="[% script_name %]?op=close_form&amp;budget_period_id=[% period_loo.budget_period_id %]">Close</a>
393
                  <a href="/cgi-bin/koha/admin/aqbudgets.pl?op=add_form&amp;budget_period_id=[% period_loo.budget_period_id %]">Add fund</a>
480
                  <a href="/cgi-bin/koha/admin/aqbudgets.pl?op=add_form&amp;budget_period_id=[% period_loo.budget_period_id %]">Add fund</a>
394
                  </td>
481
                  </td>
395
                  </tr>
482
                  </tr>
(-)a/t/db_dependent/Budgets.t (-2 / +55 lines)
Lines 1-5 Link Here
1
use Modern::Perl;
1
use Modern::Perl;
2
use Test::More tests => 71;
2
use Test::More tests => 77;
3
3
4
BEGIN {
4
BEGIN {
5
    use_ok('C4::Budgets')
5
    use_ok('C4::Budgets')
Lines 18-23 $dbh->{RaiseError} = 1; Link Here
18
$dbh->do(q|DELETE FROM aqbudgetperiods|);
18
$dbh->do(q|DELETE FROM aqbudgetperiods|);
19
$dbh->do(q|DELETE FROM aqbudgets|);
19
$dbh->do(q|DELETE FROM aqbudgets|);
20
20
21
# Mock userenv
22
local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
23
my $userenv;
24
*C4::Context::userenv = \&Mock_userenv;
25
$userenv = { flags => 1, id => 'my_userid', branch => 'CPL' };
26
21
#
27
#
22
# Budget Periods :
28
# Budget Periods :
23
#
29
#
Lines 295-300 my %budgets; Link Here
295
my $invoiceid = AddInvoice(invoicenumber => 'invoice_test_clone', booksellerid => $booksellerid, unknown => "unknown");
301
my $invoiceid = AddInvoice(invoicenumber => 'invoice_test_clone', booksellerid => $booksellerid, unknown => "unknown");
296
my $item_price = 10;
302
my $item_price = 10;
297
my $item_quantity = 2;
303
my $item_quantity = 2;
304
my $number_of_orders_to_move = 0;
298
for my $infos (@order_infos) {
305
for my $infos (@order_infos) {
299
    for ( 1 .. $infos->{pending_quantity} ) {
306
    for ( 1 .. $infos->{pending_quantity} ) {
300
        my ( undef, $ordernumber ) = C4::Acquisition::NewOrder(
307
        my ( undef, $ordernumber ) = C4::Acquisition::NewOrder(
Lines 316-321 for my $infos (@order_infos) { Link Here
316
            }
323
            }
317
        );
324
        );
318
        push @{ $budgets{$infos->{budget_id}} }, $ordernumber;
325
        push @{ $budgets{$infos->{budget_id}} }, $ordernumber;
326
        $number_of_orders_to_move++;
319
    }
327
    }
320
    for ( 1 .. $infos->{spent_quantity} ) {
328
    for ( 1 .. $infos->{spent_quantity} ) {
321
        my ( undef, $ordernumber ) = C4::Acquisition::NewOrder(
329
        my ( undef, $ordernumber ) = C4::Acquisition::NewOrder(
Lines 434-439 is( $number_of_budgets_not_reset, 0, Link Here
434
    'CloneBudgetPeriod has reset all budgets (funds)' );
442
    'CloneBudgetPeriod has reset all budgets (funds)' );
435
443
436
444
445
# MoveOrders
446
my $number_orders_moved = C4::Budgets::MoveOrders();
447
is( $number_orders_moved, undef, 'MoveOrders return undef if no arg passed' );
448
$number_orders_moved =
449
  C4::Budgets::MoveOrders( { from_budget_period_id => $budget_period_id } );
450
is( $number_orders_moved, undef,
451
    'MoveOrders return undef if only 1 arg passed' );
452
$number_orders_moved =
453
  C4::Budgets::MoveOrders( { to_budget_period_id => $budget_period_id } );
454
is( $number_orders_moved, undef,
455
    'MoveOrders return undef if only 1 arg passed' );
456
$number_orders_moved = C4::Budgets::MoveOrders(
457
    {
458
        from_budget_period_id => $budget_period_id,
459
        to_budget_period_id   => $budget_period_id
460
    }
461
);
462
is( $number_orders_moved, undef,
463
    'MoveOrders return undef if 2 budget period id are the same' );
464
465
$budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
466
    {
467
        budget_period_id        => $budget_period_id,
468
        budget_period_startdate => '2014-01-01',
469
        budget_period_enddate   => '2014-12-31',
470
        reset_all_funds         => 1,
471
    }
472
);
473
474
my $report = C4::Budgets::MoveOrders(
475
    {
476
        from_budget_period_id => $budget_period_id,
477
        to_budget_period_id   => $budget_period_id_cloned,
478
    }
479
);
480
is( scalar( @$report ), 6 , "MoveOrders has processed 6 funds" );
481
482
my $number_of_orders_moved = 0;
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" );
485
437
sub _get_dependencies {
486
sub _get_dependencies {
438
    my ($budget_hierarchy) = @_;
487
    my ($budget_hierarchy) = @_;
439
    my $graph;
488
    my $graph;
Lines 458-460 sub _get_budgetname_by_id { Link Here
458
      @$budgets;
507
      @$budgets;
459
    return $budget_name;
508
    return $budget_name;
460
}
509
}
461
- 
510
511
# C4::Context->userenv
512
sub Mock_userenv {
513
    return $userenv;
514
}

Return to bug 12164