Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision e592e0bc

Von G. Richardson vor mehr als 4 Jahren hinzugefügt

  • ID e592e0bc4aaf7ce59e4d5eec9834cb430ab6a33f
  • Vorgänger 0fed2b9a
  • Nachfolger d41fb52a

Jahresabschluß - GLTransaction->post und Tests

Der YearEnd Controller nutzt nun GLTransaction->post, damit muß man die
acc_trans-Einträge nicht mehr von Hand zusammenbauen, und die Buchungen
passieren automatisch als Transaktion, die Buchungen werden validiert
und es wird ein Historieneintrag erstellt.

Unterschiede anzeigen:

SL/Controller/YearEndTransactions.pm
49 49

  
50 50
  $self->_parse_form;
51 51

  
52

  
53 52
  eval {
54 53
    _year_end_bookings( start_date => $self->cb_startdate,
55 54
                        cb_date    => $self->cb_date,
......
66 65
                                               );
67 66

  
68 67
  my $html = $self->render('yearend/_charts', { layout  => 0 , process => 1, output => 0 },
69
                 charts          => $report_data,
70
                 profit_loss_sum => $profit_loss_sum,
71
               );
68
                           charts          => $report_data,
69
                           profit_loss_sum => $profit_loss_sum,
70
                          );
72 71
  return $self->js->flash('info', t8('Year-end bookings were successfully completed!'))
73 72
               ->html('#charts', $html)
74 73
               ->render;
......
105 104
                                               );
106 105

  
107 106
  $self->render('yearend/_charts', { layout  => 0 , process => 1 },
108
                 charts          => $report_data,
109
                 profit_loss_sum => $profit_loss_sum,
107
                charts          => $report_data,
108
                profit_loss_sum => $profit_loss_sum,
110 109
               );
111 110
}
112 111

  
......
121 120
  $self->cb_startdate($::locale->parse_date_to_object($self->get_balance_starting_date($self->cb_date)));
122 121

  
123 122
  die "cb_date must come after start_date" unless $self->cb_date > $self->cb_startdate;
124

  
125 123
}
126 124

  
127 125
sub _year_end_bookings {
......
140 138
                                                cb_date    => $cb_date,
141 139
                                               );
142 140

  
141
  # load all charts from report as objects and store them in a hash
142
  my @report_chart_ids = map { $_->{chart_id} } @{ $report_data };
143
  my %charts_by_id = map { ( $_->id => $_ ) } @{ SL::DB::Manager::Chart->get_all(where => [ id => \@report_chart_ids ]) };
144

  
143 145
  my @asset_accounts       = grep { $_->{account_type} eq 'asset_account' }       @{ $report_data };
144 146
  my @profit_loss_accounts = grep { $_->{account_type} eq 'profit_loss_account' } @{ $report_data };
145 147

  
......
174 176
      description    => 'Automatische SB-Buchungen Bestandskonten Soll für ' . $cb_date->year,
175 177
      ob_transaction => 0,
176 178
      cb_transaction => 1,
179
      taxincluded    => 0,
180
      transactions   => [],
177 181
    );
178 182
    my $asset_ob_debit_entry = SL::DB::GLTransaction->new(
179 183
      employee_id    => $employee_id,
......
182 186
      description    => 'Automatische EB-Buchungen Bestandskonten Haben für ' . $ob_date->year,
183 187
      ob_transaction => 1,
184 188
      cb_transaction => 0,
189
      taxincluded    => 0,
190
      transactions   => [],
185 191
    );
186 192
    my $asset_cb_credit_entry = SL::DB::GLTransaction->new(
187 193
      employee_id    => $employee_id,
......
190 196
      description    => 'Automatische SB-Buchungen Bestandskonten Haben für ' . $cb_date->year,
191 197
      ob_transaction => 0,
192 198
      cb_transaction => 1,
199
      taxincluded    => 0,
200
      transactions   => [],
193 201
    );
194 202
    my $asset_ob_credit_entry = SL::DB::GLTransaction->new(
195 203
      employee_id    => $employee_id,
......
198 206
      description    => 'Automatische EB-Buchungen Bestandskonten Soll für ' . $ob_date->year,
199 207
      ob_transaction => 1,
200 208
      cb_transaction => 0,
209
      taxincluded    => 0,
210
      transactions   => [],
201 211
    );
202
    $asset_cb_debit_entry->transactions([]);
203
    $asset_ob_debit_entry->transactions([]);
204
    $asset_cb_credit_entry->transactions([]);
205
    $asset_ob_credit_entry->transactions([]);
206 212

  
207 213
    foreach my $asset_account ( @asset_accounts ) {
208 214
      next if $asset_account->{amount_with_cb} == 0;
209

  
210
      # create cb and ob acc_trans entry here, but decide which gl entry to add it to later
211
      my $asset_cb_acc = SL::DB::AccTransaction->new(
212
        transdate      => $cb_date,
213
        ob_transaction => 0,
214
        cb_transaction => 1,
215
        chart_id       => $asset_account->{chart_id},
216
        chart_link     => $asset_account->{chart_link},
217
        tax_id         => 0,
218
        taxkey         => 0,
219
        amount         => - $asset_account->{amount_with_cb},
220
      );
221
      my $asset_ob_acc = SL::DB::AccTransaction->new(
222
        transdate      => $ob_date,
223
        ob_transaction => 1,
224
        cb_transaction => 0,
225
        chart_id       => $asset_account->{chart_id},
226
        chart_link     => $asset_account->{chart_link},
227
        tax_id         => 0,
228
        taxkey         => 0,
229
        amount         => $asset_account->{amount_with_cb},
230
      );
215
      my $ass_acc = $charts_by_id{ $asset_account->{chart_id} };
231 216

  
232 217
      if ( $asset_account->{amount_with_cb} < 0 ) {
233
        $debit_balance += $asset_account->{amount_with_cb};
234 218
        # $main::lxdebug->message(0, sprintf("adding accno %s with balance %s to debit", $asset_account->{accno}, $asset_account->{amount_with_cb}));
219
        $debit_balance += $asset_account->{amount_with_cb};
220

  
221
        $asset_cb_debit_entry->add_chart_booking(
222
          chart  => $ass_acc,
223
          credit => - $asset_account->{amount_with_cb},
224
          tax_id => 0
225
        );
226
        $asset_ob_debit_entry->add_chart_booking(
227
          chart  => $ass_acc,
228
          debit  => - $asset_account->{amount_with_cb},
229
          tax_id => 0
230
        );
235 231

  
236
        $asset_cb_debit_entry->add_transactions($asset_cb_acc);
237
        $asset_ob_debit_entry->add_transactions($asset_ob_acc);
238 232
      } else {
239 233
        # $main::lxdebug->message(0, sprintf("adding accno %s with balance %s to credit", $asset_account->{accno}, $asset_account->{amount_with_cb}));
240 234
        $credit_balance += $asset_account->{amount_with_cb};
241
        $asset_cb_credit_entry->add_transactions($asset_cb_acc);
242
        $asset_ob_credit_entry->add_transactions($asset_ob_acc);
235

  
236
        $asset_cb_credit_entry->add_chart_booking(
237
          chart  => $ass_acc,
238
          debit  => $asset_account->{amount_with_cb},
239
          tax_id => 0
240
        );
241
        $asset_ob_credit_entry->add_chart_booking(
242
          chart  => $ass_acc,
243
          credit  => $asset_account->{amount_with_cb},
244
          tax_id => 0
245
        );
243 246
      };
244 247
    };
245 248

  
246
    my $debit_cb_acc = SL::DB::AccTransaction->new(
247
      transdate      => $cb_date,
248
      ob_transaction => 0,
249
      cb_transaction => 1,
250
      chart_id       => $carry_over_chart->id,
251
      chart_link     => $carry_over_chart->link, # maybe leave chart_link empty?
252
      tax_id         => 0,
253
      taxkey         => 0,
254
      amount         => $debit_balance,
255
    );
256
    my $debit_ob_acc = SL::DB::AccTransaction->new(
257
      transdate      => $ob_date,
258
      ob_transaction => 1,
259
      cb_transaction => 0,
260
      chart_id       => $carry_over_chart->id,
261
      chart_link     => $carry_over_chart->link,
262
      tax_id         => 0,
263
      taxkey         => 0,
264
      amount         => - $debit_balance,
265
    );
266
    my $credit_cb_acc = SL::DB::AccTransaction->new(
267
      transdate      => $cb_date,
268
      ob_transaction => 0,
269
      cb_transaction => 1,
270
      chart_id       => $carry_over_chart->id,
271
      chart_link     => $carry_over_chart->link, # maybe leave chart_link empty?
272
      tax_id         => 0,
273
      taxkey         => 0,
274
      amount         => $credit_balance,
275
    );
276
    my $credit_ob_acc = SL::DB::AccTransaction->new(
277
      transdate      => $ob_date,
278
      ob_transaction => 1,
279
      cb_transaction => 0,
280
      chart_id       => $carry_over_chart->id,
281
      chart_link     => $carry_over_chart->link,
282
      tax_id         => 0,
283
      taxkey         => 0,
284
      amount         => - $credit_balance,
285
    );
286
    $asset_cb_debit_entry->add_transactions($debit_cb_acc);
287
    $asset_ob_debit_entry->add_transactions($debit_ob_acc);
288
    $asset_cb_credit_entry->add_transactions($credit_cb_acc);
289
    $asset_ob_credit_entry->add_transactions($credit_ob_acc);
249
    if ( $debit_balance ) {
250
      $asset_cb_debit_entry->add_chart_booking(
251
        chart  => $carry_over_chart,
252
        debit  => -1 * $debit_balance,
253
        tax_id => 0,
254
      );
255

  
256
      $asset_ob_debit_entry->add_chart_booking(
257
        chart  => $carry_over_chart,
258
        credit => -1 * $debit_balance,
259
        tax_id => 0,
260
      );
261
    };
262

  
263
    if ( $credit_balance ) {
264
      $asset_cb_credit_entry->add_chart_booking(
265
        chart  => $carry_over_chart,
266
        credit => $credit_balance,
267
        tax_id => 0,
268
      );
269
      $asset_ob_credit_entry->add_chart_booking(
270
        chart  => $carry_over_chart,
271
        debit  => $credit_balance,
272
        tax_id => 0,
273
      );
274
    };
290 275

  
291
    $asset_cb_debit_entry->save if scalar @{ $asset_cb_debit_entry->transactions } > 1;
292
    $asset_ob_debit_entry->save if scalar @{ $asset_ob_debit_entry->transactions } > 1;
293
    $asset_cb_credit_entry->save if scalar @{ $asset_cb_credit_entry->transactions } > 1;
294
    $asset_ob_credit_entry->save if scalar @{ $asset_ob_credit_entry->transactions } > 1;
276
    $asset_cb_debit_entry->post  if scalar @{ $asset_cb_debit_entry->transactions  } > 1;
277
    $asset_ob_debit_entry->post  if scalar @{ $asset_ob_debit_entry->transactions  } > 1;
278
    $asset_cb_credit_entry->post if scalar @{ $asset_cb_credit_entry->transactions } > 1;
279
    $asset_ob_credit_entry->post if scalar @{ $asset_ob_credit_entry->transactions } > 1;
295 280

  
296 281
    #######  profit-loss accounts #######
297 282
    # these only have a closing balance, the balance is transferred to the profit-loss account
......
301 286
    my $profit_loss_sum = sum map { $_->{amount_with_cb} }
302 287
                              grep { $_->{account_type} eq 'profit_loss_account' }
303 288
                              @{$report_data};
289
    $profit_loss_sum ||= 0;
304 290
    my $pl_chart;
305 291
    if ( $profit_loss_sum > 0 ) {
306 292
      $pl_chart = $profit_chart;
......
318 304
      description    => 'Automatische SB-Buchungen Erfolgskonten Soll für ' . $cb_date->year,
319 305
      ob_transaction => 0,
320 306
      cb_transaction => 1,
307
      taxincluded    => 0,
308
      transactions   => [],
321 309
    );
322 310
    my $pl_cb_credit_entry = SL::DB::GLTransaction->new(
323 311
      employee_id    => $employee_id,
......
326 314
      description    => 'Automatische SB-Buchungen Erfolgskonten Haben für ' . $cb_date->year,
327 315
      ob_transaction => 0,
328 316
      cb_transaction => 1,
317
      taxincluded    => 0,
318
      transactions   => [],
329 319
    );
330
    $pl_cb_debit_entry->transactions([]);
331
    $pl_cb_credit_entry->transactions([]);
332 320

  
333 321
    foreach my $profit_loss_account ( @profit_loss_accounts ) {
334 322
      # $main::lxdebug->message(0, sprintf("found chart %s with balance %s", $profit_loss_account->{accno}, $profit_loss_account->{amount_with_cb}));
323
      my $chart = $charts_by_id{ $profit_loss_account->{chart_id} };
335 324

  
336 325
      next if $profit_loss_account->{amount_with_cb} == 0;
337 326

  
338
      my $debit_cb_acc = SL::DB::AccTransaction->new(
339
        transdate      => $cb_date,
340
        ob_transaction => 0,
341
        cb_transaction => 1,
342
        chart_id       => $profit_loss_account->{chart_id},
343
        chart_link     => $profit_loss_account->{chart_link},
344
        tax_id         => 0,
345
        taxkey         => 0,
346
        amount         => - $profit_loss_account->{amount_with_cb},
347
      );
348
      my $credit_cb_acc = SL::DB::AccTransaction->new(
349
        transdate      => $cb_date,
350
        ob_transaction => 0,
351
        cb_transaction => 1,
352
        chart_id       => $profit_loss_account->{chart_id},
353
        chart_link     => $profit_loss_account->{chart_link},
354
        tax_id         => 0,
355
        taxkey         => 0,
356
        amount         => $profit_loss_account->{amount_with_cb},
357
      );
358
      if ( { $profit_loss_account->{amount_with_cb} < 0 } ) {
359
        $pl_debit_balance += $profit_loss_account->{amount_with_cb};
360
         $pl_cb_debit_entry->add_transactions($debit_cb_acc);
327
      if ( $profit_loss_account->{amount_with_cb} < 0 ) {
328
        $pl_debit_balance -= $profit_loss_account->{amount_with_cb};
329
        $pl_cb_debit_entry->add_chart_booking(
330
          chart  => $chart,
331
          tax_id => 0,
332
          credit => - $profit_loss_account->{amount_with_cb},
333
        );
361 334
      } else {
362 335
        $pl_credit_balance += $profit_loss_account->{amount_with_cb};
363
         $pl_cb_credit_entry->add_transactions($credit_cb_acc);
336
        $pl_cb_credit_entry->add_chart_booking(
337
          chart  => $chart,
338
          tax_id => 0,
339
          debit  => $profit_loss_account->{amount_with_cb},
340
        );
364 341
      };
365 342
    };
366 343

  
367
    $debit_cb_acc = SL::DB::AccTransaction->new(
368
      transdate      => $cb_date,
369
      ob_transaction => 0,
370
      cb_transaction => 1,
371
      chart_id       => $pl_chart->id,
372
      chart_link     => $pl_chart->link,
373
      tax_id         => 0,
374
      taxkey         => 0,
375
      amount         => $pl_debit_balance,
376
    );
377
    $credit_cb_acc = SL::DB::AccTransaction->new(
378
      transdate      => $cb_date,
379
      ob_transaction => 0,
380
      cb_transaction => 1,
381
      chart_id       => $pl_chart->id,
382
      chart_link     => $pl_chart->link,
383
      tax_id         => 0,
384
      taxkey         => 0,
385
      amount         => - $pl_credit_balance,
386
    );
387
    $pl_cb_debit_entry->add_transactions($debit_cb_acc);
388
    $pl_cb_credit_entry->add_transactions($credit_cb_acc);
344
    # $main::lxdebug->message(0, "pl_debit_balance  = $pl_debit_balance");
345
    # $main::lxdebug->message(0, "pl_credit_balance = $pl_credit_balance");
346

  
347
    $pl_cb_debit_entry->add_chart_booking(
348
      chart  => $pl_chart,
349
      tax_id => 0,
350
      debit  => $pl_debit_balance,
351
    ) if $pl_debit_balance;
352

  
353
    $pl_cb_credit_entry->add_chart_booking(
354
      chart  => $pl_chart,
355
      tax_id => 0,
356
      credit => $pl_credit_balance,
357
    ) if $pl_credit_balance;
389 358

  
390
    $pl_cb_debit_entry->save  if scalar @{ $pl_cb_debit_entry->transactions }  > 1;
391
    $pl_cb_credit_entry->save if scalar @{ $pl_cb_credit_entry->transactions } > 1;
359
    # printf("debit : %s -> %s\n", $_->chart->displayable_name, $_->amount) foreach @{ $pl_cb_debit_entry->transactions };
360
    # printf("credit: %s -> %s\n", $_->chart->displayable_name, $_->amount) foreach @{ $pl_cb_credit_entry->transactions };
361

  
362
    $pl_cb_debit_entry->post  if scalar @{ $pl_cb_debit_entry->transactions }  > 1;
363
    $pl_cb_credit_entry->post if scalar @{ $pl_cb_credit_entry->transactions } > 1;
392 364

  
393 365
    ######### profit-loss transfer #########
394 366
    # and finally transfer the new balance of the profit-loss account via the carry-over account
395 367
    # we want to use profit_loss_sum with cb!
396 368

  
397
    my $carry_over_cb_entry = SL::DB::GLTransaction->new(
398
      employee_id    => $employee_id,
399
      transdate      => $cb_date,
400
      reference      => 'SB ' . $cb_date->year,
401
      description    => sprintf('Automatische SB-Buchung für %s %s',
402
                                $profit_loss_sum >= 0 ? 'Gewinnvortrag' : 'Verlustvortrag',
403
                                $cb_date->year,
404
                               ),
405
      ob_transaction => 0,
406
      cb_transaction => 1,
407
    );
408
    my $carry_over_ob_entry = SL::DB::GLTransaction->new(
409
      employee_id    => $employee_id,
410
      transdate      => $ob_date,
411
      reference      => 'EB ' . $ob_date->year,
412
      description    => sprintf('Automatische EB-Buchung für %s %s',
413
                                $profit_loss_sum >= 0 ? 'Gewinnvortrag' : 'Verlustvortrag',
414
                                $ob_date->year,
415
                               ),
416
      ob_transaction => 1,
417
      cb_transaction => 0,
418
    );
419
    $carry_over_cb_entry->transactions([]);
420
    $carry_over_ob_entry->transactions([]);
369
    if ( $profit_loss_sum != 0 ) {
421 370

  
422
    my $carry_over_cb_acc_co = SL::DB::AccTransaction->new(
423
      transdate      => $cb_date,
424
      ob_transaction => 0,
425
      cb_transaction => 1,
426
      chart_id       => $carry_over_chart->id,
427
      chart_link     => $carry_over_chart->link,
428
      tax_id         => 0,
429
      taxkey         => 0,
430
      amount         => $profit_loss_sum,
431
    );
432
    my $carry_over_cb_acc_pl = SL::DB::AccTransaction->new(
433
      transdate      => $cb_date,
434
      ob_transaction => 0,
435
      cb_transaction => 1,
436
      chart_id       => $pl_chart->id,
437
      chart_link     => $pl_chart->link,
438
      tax_id         => 0,
439
      taxkey         => 0,
440
      amount         => - $profit_loss_sum,
441
    );
371
      my $carry_over_cb_entry = SL::DB::GLTransaction->new(
372
        employee_id    => $employee_id,
373
        transdate      => $cb_date,
374
        reference      => 'SB ' . $cb_date->year,
375
        description    => sprintf('Automatische SB-Buchung für %s %s',
376
                                  $profit_loss_sum >= 0 ? 'Gewinnvortrag' : 'Verlustvortrag',
377
                                  $cb_date->year,
378
                                 ),
379
        ob_transaction => 0,
380
        cb_transaction => 1,
381
        taxincluded    => 0,
382
        transactions   => [],
383
      );
384
      my $carry_over_ob_entry = SL::DB::GLTransaction->new(
385
        employee_id    => $employee_id,
386
        transdate      => $ob_date,
387
        reference      => 'EB ' . $ob_date->year,
388
        description    => sprintf('Automatische EB-Buchung für %s %s',
389
                                  $profit_loss_sum >= 0 ? 'Gewinnvortrag' : 'Verlustvortrag',
390
                                  $ob_date->year,
391
                                 ),
392
        ob_transaction => 1,
393
        cb_transaction => 0,
394
        taxincluded    => 0,
395
        transactions   => [],
396
      );
442 397

  
443
    $carry_over_cb_entry->add_transactions($carry_over_cb_acc_co);
444
    $carry_over_cb_entry->add_transactions($carry_over_cb_acc_pl);
445
    $carry_over_cb_entry->save if $profit_loss_sum != 0;
398
      my ($amount1, $amount2);
399
      if ( $profit_loss_sum < 0 ) {
400
        $amount1 = 'debit';
401
        $amount2 = 'credit';
402
      } else {
403
        $amount1 = 'credit';
404
        $amount2 = 'debit';
405
      };
446 406

  
447
    my $carry_over_ob_acc_co = SL::DB::AccTransaction->new(
448
      transdate      => $ob_date,
449
      ob_transaction => 1,
450
      cb_transaction => 0,
451
      chart_id       => $pl_chart->id,
452
      chart_link     => $pl_chart->link,
453
      tax_id         => 0,
454
      taxkey         => 0,
455
      amount         => $profit_loss_sum,
456
    );
457
    my $carry_over_ob_acc_pl = SL::DB::AccTransaction->new(
458
      transdate      => $ob_date,
459
      ob_transaction => 1,
460
      cb_transaction => 0,
461
      chart_id       => $carry_over_chart->id,
462
      chart_link     => $carry_over_chart->link,
463
      tax_id         => 0,
464
      taxkey         => 0,
465
      amount         => - $profit_loss_sum,
466
    );
407
      $carry_over_cb_entry->add_chart_booking(
408
        chart    => $carry_over_chart,
409
        tax_id   => 0,
410
        $amount1 => abs($profit_loss_sum),
411
      );
412
      $carry_over_cb_entry->add_chart_booking(
413
        chart    => $pl_chart,
414
        tax_id   => 0,
415
        $amount2 => abs($profit_loss_sum),
416
      );
417
      $carry_over_ob_entry->add_chart_booking(
418
        chart    => $carry_over_chart,
419
        tax_id   => 0,
420
        $amount2 => abs($profit_loss_sum),
421
      );
422
      $carry_over_ob_entry->add_chart_booking(
423
        chart    => $pl_chart,
424
        tax_id   => 0,
425
        $amount1 => abs($profit_loss_sum),
426
      );
427

  
428
      # printf("debit : %s -> %s\n", $_->chart->displayable_name, $_->amount) foreach @{ $carry_over_ob_entry->transactions };
429
      # printf("credit: %s -> %s\n", $_->chart->displayable_name, $_->amount) foreach @{ $carry_over_ob_entry->transactions };
467 430

  
468
    $carry_over_ob_entry->add_transactions($carry_over_ob_acc_co);
469
    $carry_over_ob_entry->add_transactions($carry_over_ob_acc_pl);
470
    $carry_over_ob_entry->save if $profit_loss_sum != 0;
431
      $carry_over_cb_entry->post if scalar @{ $carry_over_cb_entry->transactions } > 1;
432
      $carry_over_ob_entry->post if scalar @{ $carry_over_ob_entry->transactions } > 1;
433
    };
471 434

  
472 435
    my $consistency_query = <<SQL;
473 436
select sum(amount)
......
475 438
 where     (ob_transaction is true or cb_transaction is true)
476 439
       and (transdate = ? or transdate = ?)
477 440
SQL
478
     my ($sum) = my ($empty) = selectrow_query($::form, $db->dbh, $consistency_query,
479
                                               $cb_date,
480
                                               $ob_date
481
                                              );
441
    my ($sum) = selectrow_query($::form, $db->dbh, $consistency_query,
442
                                $cb_date,
443
                                $ob_date
444
                               );
482 445
     die "acc_trans transactions don't add up to zero" unless $sum == 0;
483 446

  
484 447
    1;
......
500 463
select c.id as chart_id,
501 464
       c.accno,
502 465
       c.description,
503
       c.link as chart_link,
504 466
       c.category,
505 467
       sum(a.amount) filter (where cb_transaction is false and ob_transaction is false) as amount,
506 468
       sum(a.amount) filter (where ob_transaction is true                             ) as ob_amount,

Auch abrufbar als: Unified diff