Revision d5ab2f7d
Von Jan Büren vor mehr als 1 Jahr hinzugefügt
SL/Controller/BankTransaction.pm | ||
---|---|---|
585 | 585 |
} |
586 | 586 |
my (@warnings); |
587 | 587 |
|
588 |
my $transit_items_account = SL::DB::Manager::Chart->find_by(id => SL::DB::Default->get->transit_items_chart_id); |
|
589 |
|
|
588 | 590 |
my $worker = sub { |
589 | 591 |
my $bt_id = $data{bank_transaction_id}; |
590 | 592 |
my $sign = $bank_transaction->amount < 0 ? -1 : 1; |
591 | 593 |
my $payment_received = $bank_transaction->amount > 0; |
592 | 594 |
my $payment_sent = $bank_transaction->amount < 0; |
595 |
my ($has_negative_record, $has_positive_record); |
|
593 | 596 |
|
594 | 597 |
|
595 | 598 |
foreach my $invoice_id (@{ $params{invoice_ids} }) { |
... | ... | |
601 | 604 |
message => $::locale->text("The ID #1 is not a valid database ID.", $invoice_id), |
602 | 605 |
}; |
603 | 606 |
} |
607 |
$has_positive_record = 1 if $invoice->amount > 0; # invoice |
|
608 |
$has_negative_record = 1 if $invoice->amount < 0; # credit_note |
|
604 | 609 |
push @{ $data{invoices} }, $invoice; |
605 | 610 |
} |
606 | 611 |
|
612 |
if (ref $transit_items_account eq 'SL::DB::Chart' && $has_positive_record |
|
613 |
&& scalar @{ $data{invoices} } == 2 && $has_negative_record) { |
|
614 |
|
|
615 |
$self->_check_and_book_credit_note( |
|
616 |
invoices => $data{invoices}, |
|
617 |
chart_id => $transit_items_account->id, |
|
618 |
bt_id => $bt_id, |
|
619 |
transdate => $bank_transaction->valutadate, |
|
620 |
transit_chart => $transit_items_account ); |
|
621 |
|
|
622 |
} |
|
607 | 623 |
if ( $payment_received |
608 | 624 |
&& any { ( $_->is_sales && ($_->amount < 0)) |
609 | 625 |
|| (!$_->is_sales && ($_->amount > 0)) |
... | ... | |
1006 | 1022 |
); |
1007 | 1023 |
} |
1008 | 1024 |
|
1025 |
sub _check_and_book_credit_note { |
|
1026 |
my $self = shift; |
|
1027 |
my %params = @_; |
|
1028 |
Common::check_params(\%params, qw(chart_id transdate bt_id invoices transit_chart)); |
|
1029 |
|
|
1030 |
croak "No invoice " unless (ref $params{invoices}->[0] eq 'SL::DB::PurchaseInvoice') |
|
1031 |
|| (ref $params{invoices}->[0] eq 'SL::DB::Invoice' ); |
|
1032 |
croak "Not a valid date" unless ref $params{transdate} eq 'DateTime'; |
|
1033 |
croak "Not a valid chart" unless ref $params{transit_chart} eq 'SL::DB::Chart'; |
|
1034 |
croak "Need exactly two records" unless scalar @{ $params{invoices} } == 2; |
|
1035 |
|
|
1036 |
|
|
1037 |
my ($has_one_credit_note, $has_one_invoice, $amount, $credit_note_index, $credit_note_no, $invoice_no); |
|
1038 |
my $index = 0; |
|
1039 |
foreach my $invoice (@{ $params{invoices} }) { |
|
1040 |
if ( ( $invoice->is_sales && $invoice->type eq 'credit_note') |
|
1041 |
|| (!$invoice->is_sales && $invoice->invoice_type eq 'purchase_credit_note')) { |
|
1042 |
# credit_notes | purchase_credit_note |
|
1043 |
# -1397.11000 | AR | 504.74000 | AP |
|
1044 |
# 1397.11000 | AR_paid | -504.74000 | AP_paid |
|
1045 |
|
|
1046 |
my $mult = $invoice->is_sales ? -1 : 1; # multiplier for getting the right sign for credit_notes |
|
1047 |
$amount = ($invoice->amount - $invoice->paid) * $mult; |
|
1048 |
# (-200 - (-10)) * $mult = AR_paid (positive) |AP_paid (negative) |
|
1049 |
|
|
1050 |
$has_one_credit_note += 1; |
|
1051 |
$credit_note_index = $index; |
|
1052 |
$credit_note_no = $invoice->invnumber; |
|
1053 |
} else { |
|
1054 |
$has_one_invoice += 1; |
|
1055 |
$invoice_no = $invoice->invnumber; |
|
1056 |
} |
|
1057 |
$index++; |
|
1058 |
} |
|
1059 |
die "Invalid state" unless ($has_one_credit_note == 1 && $has_one_invoice == 1); |
|
1060 |
|
|
1061 |
foreach my $invoice (@{ $params{invoices} }) { |
|
1062 |
my $is_credit_note = $invoice->invoice_type =~ m/credit_note/ ? 1 : undef; |
|
1063 |
my $sign = $invoice->invoice_type =~ m/credit_note/ ? 1 : -1; # correct sign for bookings |
|
1064 |
my $paid_sign = $invoice->invoice_type =~ m/credit_note/ ? -1 : 1; # paid is always negative for credit_note |
|
1065 |
|
|
1066 |
my $new_acc_trans = SL::DB::AccTransaction->new(trans_id => $invoice->id, |
|
1067 |
chart_id => $params{transit_chart}->id, |
|
1068 |
chart_link => $params{transit_chart}->link, |
|
1069 |
amount => $amount * $sign, |
|
1070 |
transdate => $params{transdate}, |
|
1071 |
source => $is_credit_note ? $invoice_no : $credit_note_no, |
|
1072 |
memo => t8('Automatically assigned with bank transaction'), |
|
1073 |
taxkey => 0, |
|
1074 |
tax_id => SL::DB::Manager::Tax->find_by(taxkey => 0)->id); |
|
1075 |
|
|
1076 |
my $arap_booking= SL::DB::AccTransaction->new(trans_id => $invoice->id, |
|
1077 |
chart_id => $invoice->reference_account->id, |
|
1078 |
chart_link => $invoice->reference_account->link, |
|
1079 |
amount => $amount * $sign * -1, |
|
1080 |
transdate => $params{transdate}, |
|
1081 |
source => '', |
|
1082 |
taxkey => 0, |
|
1083 |
tax_id => SL::DB::Manager::Tax->find_by(taxkey => 0)->id); |
|
1084 |
$new_acc_trans->save; |
|
1085 |
$arap_booking->save; |
|
1086 |
$invoice->update_attributes(paid => $invoice->paid + (abs($amount) * $paid_sign), datepaid => $params{transdate}); |
|
1087 |
|
|
1088 |
# link both acc_trans transactions |
|
1089 |
my $id_type = $invoice->is_sales ? 'ar' : 'ap'; |
|
1090 |
my %props_acc = ( |
|
1091 |
acc_trans_id => $new_acc_trans->acc_trans_id, |
|
1092 |
bank_transaction_id => $params{bt_id}, |
|
1093 |
$id_type => $invoice->id, |
|
1094 |
); |
|
1095 |
SL::DB::BankTransactionAccTrans->new(%props_acc)->save; |
|
1096 |
%props_acc = ( |
|
1097 |
acc_trans_id => $arap_booking->acc_trans_id, |
|
1098 |
bank_transaction_id => $params{bt_id}, |
|
1099 |
$id_type => $invoice->id, |
|
1100 |
); |
|
1101 |
SL::DB::BankTransactionAccTrans->new(%props_acc)->save; |
|
1102 |
# done |
|
1103 |
|
|
1104 |
# Record a record link from the bank transaction to the credit note |
|
1105 |
if ($invoice->invoice_type =~ m/credit_note/) { |
|
1106 |
my %props = ( |
|
1107 |
from_table => 'bank_transactions', |
|
1108 |
from_id => $params{bt_id}, |
|
1109 |
to_table => $id_type, |
|
1110 |
to_id => $invoice->id, |
|
1111 |
); |
|
1112 |
SL::DB::RecordLink->new(%props)->save; |
|
1113 |
} |
|
1114 |
} |
|
1115 |
# throw away the credit note |
|
1116 |
splice @{ $params{invoices} }, $credit_note_index, 1; |
|
1117 |
# and return nothing. hook is completely done |
|
1118 |
} |
|
1119 |
|
|
1009 | 1120 |
sub init_problems { [] } |
1010 | 1121 |
|
1011 | 1122 |
sub init_models { |
... | ... | |
1203 | 1314 |
gl transaction or to the notes field of an ap transaction. |
1204 | 1315 |
You have to write your own custom code. |
1205 | 1316 |
|
1317 |
=item C<_check_and_book_credit_note> |
|
1318 |
|
|
1319 |
This method takes a array of invoices with two entries one one valid credit note |
|
1320 |
and books the amount of the credit note against the invoice via the default |
|
1321 |
transfer items account (i.e. SKR04 1370) and adds a source and memo entry for the |
|
1322 |
payment booking. |
|
1323 |
Logical and visual linking of the payment booking and credit note record to the bank |
|
1324 |
transaction will also be done (necessary cond. for unlinking a bank transaction). |
|
1325 |
If the methods success the credit note will be deleted from |
|
1326 |
the original caller's array and he can further process the data without pondering |
|
1327 |
about the removed credit note data. |
|
1328 |
|
|
1206 | 1329 |
=back |
1207 | 1330 |
|
1208 | 1331 |
=head1 AUTHOR |
Auch abrufbar als: Unified diff
Kontoauszug verbuchen: Gutschriften über durchlaufende Posten verbuchen
Benötigt eine Rechnung und eine Gutschrift.
Bevor die Rechnung über pay_invoice bezahlt wird, wird die Gutschrift
gegen die Rechnung ausgeglichen und für die weitere Verarbeitung gelöscht.
Logisch sind die Buchungen dann mit der Bank Transaktion verknüpft und
somit wieder löschbar. S.a. POD