Revision e450ac30
Von G. Richardson vor mehr als 5 Jahren hinzugefügt
SL/Dev/Record.pm | ||
---|---|---|
2 | 2 |
|
3 | 3 |
use strict; |
4 | 4 |
use base qw(Exporter); |
5 |
our @EXPORT_OK = qw(create_invoice_item create_sales_invoice create_credit_note create_order_item create_sales_order create_purchase_order create_delivery_order_item create_sales_delivery_order create_purchase_delivery_order create_project create_department); |
|
5 |
our @EXPORT_OK = qw(create_invoice_item |
|
6 |
create_sales_invoice |
|
7 |
create_credit_note |
|
8 |
create_order_item |
|
9 |
create_sales_order |
|
10 |
create_purchase_order |
|
11 |
create_delivery_order_item |
|
12 |
create_sales_delivery_order |
|
13 |
create_purchase_delivery_order |
|
14 |
create_project create_department |
|
15 |
create_ap_transaction |
|
16 |
create_ar_transaction |
|
17 |
create_gl_transaction |
|
18 |
); |
|
6 | 19 |
our %EXPORT_TAGS = (ALL => \@EXPORT_OK); |
7 | 20 |
|
8 | 21 |
use SL::DB::Invoice; |
... | ... | |
13 | 26 |
use SL::DB::Project; |
14 | 27 |
use SL::DB::ProjectStatus; |
15 | 28 |
use SL::DB::ProjectType; |
29 |
use SL::Form; |
|
16 | 30 |
use DateTime; |
31 |
use List::Util qw(sum); |
|
32 |
use Data::Dumper; |
|
33 |
use SL::Locale::String qw(t8); |
|
34 |
use SL::DATEV; |
|
17 | 35 |
|
18 | 36 |
my %record_type_to_item_type = ( sales_invoice => 'SL::DB::InvoiceItem', |
19 | 37 |
credit_note => 'SL::DB::InvoiceItem', |
... | ... | |
300 | 318 |
return $department; |
301 | 319 |
|
302 | 320 |
} |
321 |
|
|
322 |
sub create_ap_transaction { |
|
323 |
my (%params) = @_; |
|
324 |
|
|
325 |
my $vendor = delete $params{vendor}; |
|
326 |
if ( $vendor ) { |
|
327 |
die "vendor missing or not a SL::DB::Vendor object" unless ref($vendor) eq 'SL::DB::Vendor'; |
|
328 |
} else { |
|
329 |
# use default SL/Dev vendor if it exists, or create a new one |
|
330 |
$vendor = SL::DB::Manager::Vendor->find_by(name => 'Testlieferant') // new_vendor->save; |
|
331 |
}; |
|
332 |
|
|
333 |
my $taxincluded = $params{taxincluded} // 1; |
|
334 |
delete $params{taxincluded}; |
|
335 |
|
|
336 |
my $bookings = delete $params{bookings}; |
|
337 |
# default bookings |
|
338 |
unless ( $bookings ) { |
|
339 |
my $chart_postage = SL::DB::Manager::Chart->find_by(description => 'Porto'); |
|
340 |
my $chart_telephone = SL::DB::Manager::Chart->find_by(description => 'Telefon'); |
|
341 |
$bookings = [ |
|
342 |
{ |
|
343 |
chart => $chart_postage, |
|
344 |
amount => 1000, |
|
345 |
}, |
|
346 |
{ |
|
347 |
chart => $chart_telephone, |
|
348 |
amount => $taxincluded ? 1190 : 1000, |
|
349 |
}, |
|
350 |
] |
|
351 |
}; |
|
352 |
|
|
353 |
# optional params: |
|
354 |
my $project_id = delete $params{globalproject_id}; |
|
355 |
|
|
356 |
# if amount or netamount are given, then it compares them to the final values, and dies if they don't match |
|
357 |
my $expected_amount = delete $params{amount}; |
|
358 |
my $expected_netamount = delete $params{netamount}; |
|
359 |
|
|
360 |
my $dec = delete $params{dec} // 2; |
|
361 |
|
|
362 |
my $transdate = delete $params{transdate} // DateTime->today; |
|
363 |
die "transdate hat to be DateTime object" unless ref($transdate) eq 'DateTime'; |
|
364 |
|
|
365 |
my $ap_chart = delete $params{ap_chart} // SL::DB::Manager::Chart->find_by( accno => '1600' ); |
|
366 |
die "no ap_chart found or not an AP chart" unless $ap_chart and $ap_chart->link eq 'AP'; |
|
367 |
|
|
368 |
my $ap_transaction = SL::DB::PurchaseInvoice->new( |
|
369 |
vendor_id => $vendor->id, |
|
370 |
invoice => 0, |
|
371 |
transactions => [], |
|
372 |
globalproject_id => $project_id, |
|
373 |
invnumber => delete $params{invnumber} // 'test ap_transaction', |
|
374 |
notes => delete $params{notes} // 'test ap_transaction', |
|
375 |
transdate => $transdate, |
|
376 |
taxincluded => $taxincluded, |
|
377 |
taxzone_id => $vendor->taxzone_id, # taxzone_id shouldn't have any effect on ap transactions |
|
378 |
currency_id => $::instance_conf->get_currency_id, |
|
379 |
type => undef, # isn't set for ap |
|
380 |
employee_id => SL::DB::Manager::Employee->current->id, |
|
381 |
); |
|
382 |
# $ap_transaction->assign_attributes(%params) if %params; |
|
383 |
|
|
384 |
foreach my $booking ( @{$bookings} ) { |
|
385 |
my $chart = delete $booking->{chart}; |
|
386 |
die "illegal chart" unless ref($chart) eq 'SL::DB::Chart'; |
|
387 |
|
|
388 |
my $tax = _transaction_tax_helper($booking, $chart, $transdate); # will die if tax can't be found |
|
389 |
|
|
390 |
$ap_transaction->add_ap_amount_row( |
|
391 |
amount => $booking->{amount}, # add_ap_amount_row expects the user input amount, does its own calculate_tax |
|
392 |
chart => $chart, |
|
393 |
tax_id => $tax->id, |
|
394 |
project_id => $booking->{project_id}, |
|
395 |
); |
|
396 |
} |
|
397 |
|
|
398 |
my $acc_trans_sum = sum map { $_->amount } grep { $_->chart_link =~ 'AP_amount' } @{$ap_transaction->transactions}; |
|
399 |
# $main::lxdebug->message(0, sprintf("accno: %s amount: %s chart_link: %s\n", |
|
400 |
# $_->amount, |
|
401 |
# $_->chart->accno, |
|
402 |
# $_->chart_link |
|
403 |
# )) foreach @{$ap_transaction->transactions}; |
|
404 |
|
|
405 |
# determine netamount and amount from the transactions that were added via bookings |
|
406 |
$ap_transaction->netamount( -1 * sum map { $_->amount } grep { $_->chart_link =~ 'AP_amount' } @{$ap_transaction->transactions} ); |
|
407 |
# $main::lxdebug->message(0, sprintf('found netamount %s', $ap_transaction->netamount)); |
|
408 |
|
|
409 |
my $taxamount = -1 * sum map { $_->amount } grep { $_->chart_link =~ /tax/ } @{$ap_transaction->transactions}; |
|
410 |
$ap_transaction->amount( $ap_transaction->netamount + $taxamount ); |
|
411 |
# additional check, add up all transactions before AP-transaction is added |
|
412 |
my $refamount = -1 * sum map { $_->amount } @{$ap_transaction->transactions}; |
|
413 |
die "refamount = $refamount, ap_transaction->amount = " . $ap_transaction->amount unless $refamount == $ap_transaction->amount; |
|
414 |
|
|
415 |
# if amount or netamount were passed as params, check if the values are still |
|
416 |
# the same after recalculating them from the acc_trans entries |
|
417 |
if (defined $expected_amount) { |
|
418 |
die "amount doesn't match acc_trans amounts: $expected_amount != " . $ap_transaction->amount unless $expected_amount == $ap_transaction->amount; |
|
419 |
} |
|
420 |
if (defined $expected_netamount) { |
|
421 |
die "netamount doesn't match acc_trans netamounts: $expected_netamount != " . $ap_transaction->netamount unless $expected_netamount == $ap_transaction->netamount; |
|
422 |
} |
|
423 |
|
|
424 |
$ap_transaction->create_ap_row(chart => $ap_chart); |
|
425 |
$ap_transaction->save; |
|
426 |
# $main::lxdebug->message(0, sprintf("created ap_transaction with invnumber %s and trans_id %s", |
|
427 |
# $ap_transaction->invnumber, |
|
428 |
# $ap_transaction->id)); |
|
429 |
return $ap_transaction; |
|
430 |
} |
|
431 |
|
|
432 |
sub create_ar_transaction { |
|
433 |
my (%params) = @_; |
|
434 |
|
|
435 |
my $customer = delete $params{customer}; |
|
436 |
if ( $customer ) { |
|
437 |
die "customer missing or not a SL::DB::Customer object" unless ref($customer) eq 'SL::DB::Customer'; |
|
438 |
} else { |
|
439 |
# use default SL/Dev vendor if it exists, or create a new one |
|
440 |
$customer = SL::DB::Manager::Customer->find_by(name => 'Testkunde') // new_customer->save; |
|
441 |
}; |
|
442 |
|
|
443 |
my $taxincluded = $params{taxincluded} // 1; |
|
444 |
delete $params{taxincluded}; |
|
445 |
|
|
446 |
my $bookings = delete $params{bookings}; |
|
447 |
# default bookings |
|
448 |
unless ( $bookings ) { |
|
449 |
my $chart_19 = SL::DB::Manager::Chart->find_by(accno => '8400'); |
|
450 |
my $chart_7 = SL::DB::Manager::Chart->find_by(accno => '8300'); |
|
451 |
my $chart_0 = SL::DB::Manager::Chart->find_by(accno => '8200'); |
|
452 |
$bookings = [ |
|
453 |
{ |
|
454 |
chart => $chart_19, |
|
455 |
amount => $taxincluded ? 119 : 100, |
|
456 |
}, |
|
457 |
{ |
|
458 |
chart => $chart_7, |
|
459 |
amount => $taxincluded ? 107 : 100, |
|
460 |
}, |
|
461 |
{ |
|
462 |
chart => $chart_0, |
|
463 |
amount => 100, |
|
464 |
}, |
|
465 |
] |
|
466 |
}; |
|
467 |
|
|
468 |
# optional params: |
|
469 |
my $project_id = delete $params{globalproject_id}; |
|
470 |
|
|
471 |
# if amount or netamount are given, then it compares them to the final values, and dies if they don't match |
|
472 |
my $expected_amount = delete $params{amount}; |
|
473 |
my $expected_netamount = delete $params{netamount}; |
|
474 |
|
|
475 |
my $dec = delete $params{dec} // 2; |
|
476 |
|
|
477 |
my $transdate = delete $params{transdate} // DateTime->today; |
|
478 |
die "transdate hat to be DateTime object" unless ref($transdate) eq 'DateTime'; |
|
479 |
|
|
480 |
my $ar_chart = delete $params{ar_chart} // SL::DB::Manager::Chart->find_by( accno => '1400' ); |
|
481 |
die "no ar_chart found or not an AR chart" unless $ar_chart and $ar_chart->link eq 'AR'; |
|
482 |
|
|
483 |
my $ar_transaction = SL::DB::Invoice->new( |
|
484 |
customer_id => $customer->id, |
|
485 |
invoice => 0, |
|
486 |
transactions => [], |
|
487 |
globalproject_id => $project_id, |
|
488 |
invnumber => delete $params{invnumber} // 'test ar_transaction', |
|
489 |
notes => delete $params{notes} // 'test ar_transaction', |
|
490 |
transdate => $transdate, |
|
491 |
taxincluded => $taxincluded, |
|
492 |
taxzone_id => $customer->taxzone_id, # taxzone_id shouldn't have any effect on ar transactions |
|
493 |
currency_id => $::instance_conf->get_currency_id, |
|
494 |
type => undef, # isn't set for ar |
|
495 |
employee_id => SL::DB::Manager::Employee->current->id, |
|
496 |
); |
|
497 |
# $ar_transaction->assign_attributes(%params) if %params; |
|
498 |
|
|
499 |
foreach my $booking ( @{$bookings} ) { |
|
500 |
my $chart = delete $booking->{chart}; |
|
501 |
die "illegal chart" unless ref($chart) eq 'SL::DB::Chart'; |
|
502 |
|
|
503 |
my $tax = _transaction_tax_helper($booking, $chart, $transdate); # will die if tax can't be found |
|
504 |
|
|
505 |
$ar_transaction->add_ar_amount_row( |
|
506 |
amount => $booking->{amount}, # add_ar_amount_row expects the user input amount, does its own calculate_tax |
|
507 |
chart => $chart, |
|
508 |
tax_id => $tax->id, |
|
509 |
project_id => $booking->{project_id}, |
|
510 |
); |
|
511 |
} |
|
512 |
|
|
513 |
my $acc_trans_sum = sum map { $_->amount } grep { $_->chart_link =~ 'AR_amount' } @{$ar_transaction->transactions}; |
|
514 |
# $main::lxdebug->message(0, sprintf("accno: %s amount: %s chart_link: %s\n", |
|
515 |
# $_->amount, |
|
516 |
# $_->chart->accno, |
|
517 |
# $_->chart_link |
|
518 |
# )) foreach @{$ar_transaction->transactions}; |
|
519 |
|
|
520 |
# determine netamount and amount from the transactions that were added via bookings |
|
521 |
$ar_transaction->netamount( 1 * sum map { $_->amount } grep { $_->chart_link =~ 'AR_amount' } @{$ar_transaction->transactions} ); |
|
522 |
# $main::lxdebug->message(0, sprintf('found netamount %s', $ar_transaction->netamount)); |
|
523 |
|
|
524 |
my $taxamount = 1 * sum map { $_->amount } grep { $_->chart_link =~ /tax/ } @{$ar_transaction->transactions}; |
|
525 |
$ar_transaction->amount( $ar_transaction->netamount + $taxamount ); |
|
526 |
# additional check, add up all transactions before AP-transaction is added |
|
527 |
my $refamount = 1 * sum map { $_->amount } @{$ar_transaction->transactions}; |
|
528 |
die "refamount = $refamount, ar_transaction->amount = " . $ar_transaction->amount unless $refamount == $ar_transaction->amount; |
|
529 |
|
|
530 |
# if amount or netamount were passed as params, check if the values are still |
|
531 |
# the same after recalculating them from the acc_trans entries |
|
532 |
if (defined $expected_amount) { |
|
533 |
die "amount doesn't match acc_trans amounts: $expected_amount != " . $ar_transaction->amount unless $expected_amount == $ar_transaction->amount; |
|
534 |
} |
|
535 |
if (defined $expected_netamount) { |
|
536 |
die "netamount doesn't match acc_trans netamounts: $expected_netamount != " . $ar_transaction->netamount unless $expected_netamount == $ar_transaction->netamount; |
|
537 |
} |
|
538 |
|
|
539 |
$ar_transaction->create_ar_row(chart => $ar_chart); |
|
540 |
$ar_transaction->save; |
|
541 |
# $main::lxdebug->message(0, sprintf("created ar_transaction with invnumber %s and trans_id %s", |
|
542 |
# $ar_transaction->invnumber, |
|
543 |
# $ar_transaction->id)); |
|
544 |
return $ar_transaction; |
|
545 |
} |
|
546 |
|
|
547 |
sub create_gl_transaction { |
|
548 |
my (%params) = @_; |
|
549 |
|
|
550 |
my $ob_transaction = delete $params{ob_transaction} // 0; |
|
551 |
my $cb_transaction = delete $params{cb_transaction} // 0; |
|
552 |
my $dec = delete $params{rec} // 2; |
|
553 |
|
|
554 |
my $taxincluded = defined $params{taxincluded} ? $params{taxincluded} : 1; |
|
555 |
|
|
556 |
my $today = DateTime->today_local; |
|
557 |
my $transdate = delete $params{transdate} // $today; |
|
558 |
|
|
559 |
my $reference = delete $params{reference} // 'reference'; |
|
560 |
my $description = delete $params{description} // 'description'; |
|
561 |
|
|
562 |
my $department_id = delete $params{department_id}; |
|
563 |
|
|
564 |
my $bookings = delete $params{bookings}; |
|
565 |
unless ( $bookings && scalar @{$bookings} ) { |
|
566 |
# default bookings if left empty |
|
567 |
my $expense_chart = SL::DB::Manager::Chart->find_by(accno => '4660') or die "Can't find expense chart 4660\n"; # Reisekosten |
|
568 |
my $cash_chart = SL::DB::Manager::Chart->find_by(accno => '1000') or die "Can't find cash chart 1000\n"; # Kasse |
|
569 |
|
|
570 |
$taxincluded = 0; |
|
571 |
|
|
572 |
$reference = 'Reise'; |
|
573 |
$description = 'Reise'; |
|
574 |
|
|
575 |
$bookings = [ |
|
576 |
{ |
|
577 |
chart => $expense_chart, # has default tax of 19% |
|
578 |
credit => 84.03, |
|
579 |
taxkey => 9, |
|
580 |
}, |
|
581 |
{ |
|
582 |
chart => $cash_chart, |
|
583 |
debit => 100, |
|
584 |
taxkey => 0, |
|
585 |
}, |
|
586 |
]; |
|
587 |
} |
|
588 |
|
|
589 |
my $gl_transaction = SL::DB::GLTransaction->new( |
|
590 |
reference => $reference, |
|
591 |
description => $description, |
|
592 |
transdate => $transdate, |
|
593 |
gldate => $today, |
|
594 |
taxincluded => $taxincluded, |
|
595 |
type => undef, |
|
596 |
ob_transaction => $ob_transaction, |
|
597 |
cb_transaction => $cb_transaction, |
|
598 |
storno => 0, |
|
599 |
storno_id => undef, |
|
600 |
transactions => [], |
|
601 |
); |
|
602 |
|
|
603 |
my @acc_trans; |
|
604 |
if ( scalar @{$bookings} ) { |
|
605 |
# there are several ways of determining the tax: |
|
606 |
# * tax_id : fetches SL::DB::Tax object via id (as used in dropdown in interface) |
|
607 |
# * tax : SL::DB::Tax object (where $tax->id = tax_id) |
|
608 |
# * taxkey : tax is determined from startdate |
|
609 |
# * none of the above defined: use the default tax for that chart |
|
610 |
|
|
611 |
foreach my $booking ( @{$bookings} ) { |
|
612 |
my $chart = delete $booking->{chart}; |
|
613 |
die "illegal chart" unless ref($chart) eq 'SL::DB::Chart'; |
|
614 |
|
|
615 |
die t8('Empty transaction!') |
|
616 |
unless $booking->{debit} or $booking->{credit}; # must exist and not be 0 |
|
617 |
die t8('Cannot post transaction with a debit and credit entry for the same account!') |
|
618 |
if defined($booking->{debit}) and defined($booking->{credit}); |
|
619 |
|
|
620 |
my $tax = _transaction_tax_helper($booking, $chart, $transdate); # will die if tax can't be found |
|
621 |
|
|
622 |
$gl_transaction->add_chart_booking( |
|
623 |
chart => $chart, |
|
624 |
debit => $booking->{debit}, |
|
625 |
credit => $booking->{credit}, |
|
626 |
tax_id => $tax->id, |
|
627 |
source => $booking->{source} // '', |
|
628 |
memo => $booking->{memo} // '', |
|
629 |
project_id => $booking->{project_id} |
|
630 |
); |
|
631 |
} |
|
632 |
}; |
|
633 |
|
|
634 |
$gl_transaction->post; |
|
635 |
|
|
636 |
return $gl_transaction; |
|
637 |
} |
|
638 |
|
|
639 |
sub _transaction_tax_helper { |
|
640 |
# checks for hash-entries with key tax, tax_id or taxkey |
|
641 |
# returns an SL::DB::Tax object |
|
642 |
# can be used for booking hashref in ar_transaction, ap_transaction and gl_transaction |
|
643 |
# will modify hashref, e.g. removing taxkey if tax_id was also supplied |
|
644 |
|
|
645 |
my ($booking, $chart, $transdate) = @_; |
|
646 |
|
|
647 |
die "_transaction_tax_helper: chart missing" unless $chart && ref($chart) eq 'SL::DB::Chart'; |
|
648 |
die "_transaction_tax_helper: transdate missing" unless $transdate && ref($transdate) eq 'DateTime'; |
|
649 |
|
|
650 |
my $tax; |
|
651 |
|
|
652 |
if ( defined $booking->{tax_id} ) { # tax_id may be 0 |
|
653 |
delete $booking->{taxkey}; # ignore any taxkeys that may have been added, tax_id has precedence |
|
654 |
$tax = SL::DB::Tax->new(id => $booking->{tax_id})->load( with => [ 'chart' ] ); |
|
655 |
} elsif ( $booking->{tax} ) { |
|
656 |
die "illegal tax entry" unless ref($booking->{tax}) eq 'SL::DB::Tax'; |
|
657 |
$tax = $booking->{tax}; |
|
658 |
} elsif ( defined $booking->{taxkey} ) { |
|
659 |
# If a taxkey is given, find the taxkey entry for that chart that |
|
660 |
# matches the stored taxkey and with the correct transdate. This will only work |
|
661 |
# if kivitendo has that taxkey configured for that chart, i.e. it should barf if |
|
662 |
# e.g. the bank chart is called with taxkey 3. |
|
663 |
|
|
664 |
# example query: |
|
665 |
# select * |
|
666 |
# from taxkeys |
|
667 |
# where taxkey_id = 3 |
|
668 |
# and chart_id = (select id from chart where accno = '8400') |
|
669 |
# and startdate <= '2018-01-01' |
|
670 |
# order by startdate desc |
|
671 |
# limit 1; |
|
672 |
|
|
673 |
my $taxkey = SL::DB::Manager::TaxKey->get_first( |
|
674 |
query => [ and => [ chart_id => $chart->id, |
|
675 |
startdate => { le => $transdate }, |
|
676 |
taxkey => $booking->{taxkey} |
|
677 |
] |
|
678 |
], |
|
679 |
sort_by => "startdate DESC", |
|
680 |
limit => 1, |
|
681 |
with_objects => [ qw(tax) ], |
|
682 |
); |
|
683 |
die sprintf("Chart %s doesn't have a taxkey chart configured for taxkey %s", $chart->accno, $booking->{taxkey}) |
|
684 |
unless $taxkey; |
|
685 |
|
|
686 |
$tax = $taxkey->tax; |
|
687 |
} else { |
|
688 |
# use default tax for that chart if neither tax_id, tax or taxkey were defined |
|
689 |
my $active_taxkey = $chart->get_active_taxkey($transdate); |
|
690 |
$tax = $active_taxkey->tax; |
|
691 |
# $main::lxdebug->message(0, sprintf("found default taxrate %s for chart %s", $tax->rate, $chart->displayable_name)); |
|
692 |
}; |
|
693 |
|
|
694 |
die "no tax" unless $tax && ref($tax) eq 'SL::DB::Tax'; |
|
695 |
return $tax; |
|
696 |
}; |
|
697 |
|
|
303 | 698 |
1; |
304 | 699 |
|
305 | 700 |
__END__ |
... | ... | |
442 | 837 |
|
443 | 838 |
C<%params> should only contain alterable keys from the object Department. |
444 | 839 |
|
840 |
=head2 C<create_ap_transaction %PARAMS> |
|
841 |
|
|
842 |
Creates a new AP transaction (table ap, invoice = 0), and will try to add as |
|
843 |
many defaults as possible. |
|
844 |
|
|
845 |
Possible parameters: |
|
846 |
* vendor (SL::DB::Vendor object, defaults to SL::Dev default vendor) |
|
847 |
* taxincluded (0 or 1, defaults to 1) |
|
848 |
* transdate (DateTime object, defaults to current date) |
|
849 |
* bookings (arrayref for the charts to be booked, see examples below) |
|
850 |
* amount (to check if final amount matches this amount) |
|
851 |
* netamount (to check if final amount matches this amount) |
|
852 |
* dec (number of decimals to round to, defaults to 2) |
|
853 |
* ap_chart (SL::DB::Chart object, default to accno 1600) |
|
854 |
* invnumber (defaults to 'test ap_transaction') |
|
855 |
* notes (defaults to 'test ap_transaction') |
|
856 |
* globalproject_id |
|
857 |
|
|
858 |
Currently doesn't support exchange rates. |
|
859 |
|
|
860 |
Minimal usage example, creating an AP transaction with a default vendor and |
|
861 |
default bookings (telephone, postage): |
|
862 |
|
|
863 |
use SL::Dev::Record qw(create_ap_transaction); |
|
864 |
my $invoice = create_ap_transaction(); |
|
865 |
|
|
866 |
Create an AP transaction with a specific vendor and specific charts: |
|
867 |
|
|
868 |
my $vendor = SL::Dev::CustomerVendor::new_vendor(name => 'My Vendor')->save; |
|
869 |
my $chart_postage = SL::DB::Manager::Chart->find_by(description => 'Porto'); |
|
870 |
my $chart_telephone = SL::DB::Manager::Chart->find_by(description => 'Telefon'); |
|
871 |
|
|
872 |
my $ap_transaction = create_ap_transaction( |
|
873 |
vendor => $vendor, |
|
874 |
invnumber => 'test invoice taxincluded', |
|
875 |
taxincluded => 1, |
|
876 |
amount => 2190, # optional param for checking whether final amount matches |
|
877 |
netamount => 2000, # optional param for checking whether final netamount matches |
|
878 |
bookings => [ |
|
879 |
{ |
|
880 |
chart => $chart_postage, |
|
881 |
amount => 1000, |
|
882 |
}, |
|
883 |
{ |
|
884 |
chart => $chart_telephone, |
|
885 |
amount => 1190, |
|
886 |
}, |
|
887 |
] |
|
888 |
); |
|
889 |
|
|
890 |
Or the same example with tax not included, but an old transdate and old taxrate (16%): |
|
891 |
|
|
892 |
my $ap_transaction = create_ap_transaction( |
|
893 |
vendor => $vendor, |
|
894 |
invnumber => 'test invoice tax not included', |
|
895 |
transdate => DateTime->new(year => 2000, month => 10, day => 1), |
|
896 |
taxincluded => 0, |
|
897 |
amount => 2160, # optional param for checking whether final amount matches |
|
898 |
netamount => 2000, # optional param for checking whether final netamount matches |
|
899 |
bookings => [ |
|
900 |
{ |
|
901 |
chart => $chart_postage, |
|
902 |
amount => 1000, |
|
903 |
}, |
|
904 |
{ |
|
905 |
chart => $chart_telephone, |
|
906 |
amount => 1000, |
|
907 |
}, |
|
908 |
] |
|
909 |
); |
|
910 |
|
|
911 |
Don't use the default tax, e.g. postage with 19%: |
|
912 |
|
|
913 |
my $tax_9 = SL::DB::Manager::Tax->find_by(taxkey => 9, rate => 0.19); |
|
914 |
my $chart_postage = SL::DB::Manager::Chart->find_by(description => 'Porto'); |
|
915 |
my $ap_transaction = create_ap_transaction( |
|
916 |
invnumber => 'postage with tax', |
|
917 |
taxincluded => 0, |
|
918 |
bookings => [ |
|
919 |
{ |
|
920 |
chart => $chart_postage, |
|
921 |
amount => 1000, |
|
922 |
tax => $tax_9, |
|
923 |
}, |
|
924 |
], |
|
925 |
); |
|
926 |
|
|
927 |
=head2 C<create_ar_transaction %PARAMS> |
|
928 |
|
|
929 |
See C<create_ap_transaction>, except use customer instead of vendor. |
|
930 |
|
|
931 |
=head2 C<create_gl_transaction %PARAMS> |
|
932 |
|
|
933 |
Creates a new GL transaction (table gl), which is basically a wrapper around |
|
934 |
SL::DB::GLTransaction->new(...) and add_chart_booking and post, while setting |
|
935 |
as many defaults as possible. |
|
936 |
|
|
937 |
Possible parameters: |
|
938 |
|
|
939 |
* taxincluded (0 or 1, defaults to 1) |
|
940 |
* transdate (DateTime object, defaults to current date) |
|
941 |
* dec (number of decimals to round to, defaults to 2) |
|
942 |
* bookings (arrayref for the charts and taxes to be booked, see examples below) |
|
943 |
|
|
944 |
bookings must include a least: |
|
945 |
|
|
946 |
* chart as an SL::DB::Chart object |
|
947 |
* credit or debit, as positive numbers |
|
948 |
* tax_id, tax (an SL::DB::Tax object) or taxkey (e.g. 9) |
|
949 |
|
|
950 |
Can't be used to create storno transactions. |
|
951 |
|
|
952 |
Minimal usage example, using all the defaults, creating a GL transaction with |
|
953 |
travel expenses: |
|
954 |
|
|
955 |
use SL::Dev::Record qw(create_gl_transaction); |
|
956 |
$gl_transaction = create_gl_transaction(); |
|
957 |
|
|
958 |
Create a GL transaction with a specific charts and taxes (the default taxes for |
|
959 |
those charts are used if none are explicitly given in bookings): |
|
960 |
|
|
961 |
my $cash = SL::DB::Manager::Chart->find_by( description => 'Kasse' ); |
|
962 |
my $betriebsbedarf = SL::DB::Manager::Chart->find_by( description => 'Betriebsbedarf' ); |
|
963 |
$gl_transaction = create_gl_transaction( |
|
964 |
reference => 'betriebsbedarf', |
|
965 |
taxincluded => 1, |
|
966 |
bookings => [ |
|
967 |
{ |
|
968 |
chart => $betriebsbedarf, |
|
969 |
memo => 'foo 1', |
|
970 |
source => 'foo 1', |
|
971 |
credit => 119, |
|
972 |
}, |
|
973 |
{ |
|
974 |
chart => $betriebsbedarf, |
|
975 |
memo => 'foo 2', |
|
976 |
source => 'foo 2', |
|
977 |
credit => 119, |
|
978 |
}, |
|
979 |
{ |
|
980 |
chart => $cash, |
|
981 |
debit => 238, |
|
982 |
memo => 'foo 1+2', |
|
983 |
source => 'foo 1+2', |
|
984 |
}, |
|
985 |
], |
|
986 |
); |
|
987 |
|
|
445 | 988 |
|
446 | 989 |
=head1 BUGS |
447 | 990 |
|
... | ... | |
449 | 992 |
|
450 | 993 |
=head1 AUTHOR |
451 | 994 |
|
452 |
G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>
|
|
995 |
G. Richardson E<lt>grichardson@kivitec.deE<gt>
|
|
453 | 996 |
|
454 | 997 |
=cut |
Auch abrufbar als: Unified diff
SL Dev Record - neue Methoden für ar/gl/ap Transaktionen
Neue Methoden, um für Tests schnell Debitorenbuchungen,
Kreditorenbuchungen und Dialogbuchungen zu erstellen