+sub test_neg_ap_transaction {
+ my (%params) = @_;
+ my $testname = 'test_neg_ap_transaction' . $params{invoice} ? ' invoice booking' : ' credit booking';
+ my $netamount = -20;
+ my $amount = $::form->round_amount($netamount * 1.19,2);
+ my $invoice = SL::DB::PurchaseInvoice->new(
+ invoice => $params{invoice} // 0,
+ invnumber => $params{invnumber} || 'test_neg_ap_transaction',
+ amount => $amount,
+ netamount => $netamount,
+ transdate => $dt,
+ taxincluded => 0,
+ vendor_id => $vendor->id,
+ taxzone_id => $vendor->taxzone_id,
+ currency_id => $currency_id,
+ transactions => [],
+ notes => 'test_neg_ap_transaction',
+ );
+ $invoice->add_ap_amount_row(
+ amount => $invoice->netamount,
+ chart => $ap_amount_chart,
+ tax_id => $tax_9->id,
+ );
+
+ $invoice->create_ap_row(chart => $ap_chart);
+ $invoice->save;
+
+ is($invoice->netamount, -20 , "$testname: netamount ok");
+ is($invoice->amount , -23.8, "$testname: amount ok");
+
+ my $bt = create_bank_transaction(record => $invoice,
+ amount => $invoice->amount,
+ bank_chart_id => $bank->id,
+ transdate => $dt_10,
+ );
+
+ my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($invoice);
+ is($agreement, 16, "points for negative ap transaction ok");
+
+ $::form->{invoice_ids} = {
+ $bt->id => [ $invoice->id ]
+ };
+
+ save_btcontroller_to_string();
+
+ $invoice->load;
+ $bt->load;
+
+ is($invoice->amount , '-23.80000', "$testname: amount ok");
+ is($invoice->netamount, '-20.00000', "$testname: netamount ok");
+ is($invoice->paid , '-23.80000', "$testname: paid ok");
+ is($bt->invoice_amount, '23.80000', "$testname: bt invoice amount for ap was assigned");
+ is($bt->amount, '23.80000', "$testname: bt amount for ap was assigned");
+
+ return $invoice;
+};
+sub test_two_neg_ap_transaction {
+ my $testname='test_two_neg_ap_transaction';
+ my $netamount = -20;
+ my $amount = $::form->round_amount($netamount * 1.19,2);
+ my $invoice = SL::DB::PurchaseInvoice->new(
+ invoice => 0,
+ invnumber => 'test_neg_ap_transaction',
+ amount => $amount,
+ netamount => $netamount,
+ transdate => $dt,
+ taxincluded => 0,
+ vendor_id => $vendor->id,
+ taxzone_id => $vendor->taxzone_id,
+ currency_id => $currency_id,
+ transactions => [],
+ notes => 'test_neg_ap_transaction',
+ );
+ $invoice->add_ap_amount_row(
+ amount => $invoice->netamount,
+ chart => $ap_amount_chart,
+ tax_id => $tax_9->id,
+ );
+
+ $invoice->create_ap_row(chart => $ap_chart);
+ $invoice->save;
+
+ is($invoice->netamount, -20 , "$testname: netamount ok");
+ is($invoice->amount , -23.8, "$testname: amount ok");
+
+ my $netamount_two = -1.14;
+ my $amount_two = $::form->round_amount($netamount_two * 1.19,2);
+ my $invoice_two = SL::DB::PurchaseInvoice->new(
+ invoice => 0,
+ invnumber => 'test_neg_ap_transaction_two',
+ amount => $amount_two,
+ netamount => $netamount_two,
+ transdate => $dt,
+ taxincluded => 0,
+ vendor_id => $vendor->id,
+ taxzone_id => $vendor->taxzone_id,
+ currency_id => $currency_id,
+ transactions => [],
+ notes => 'test_neg_ap_transaction_two',
+ );
+ $invoice_two->add_ap_amount_row(
+ amount => $invoice_two->netamount,
+ chart => $ap_amount_chart,
+ tax_id => $tax_9->id,
+ );
+
+ $invoice_two->create_ap_row(chart => $ap_chart);
+ $invoice_two->save;
+
+ is($invoice_two->netamount, -1.14 , "$testname: netamount ok");
+ is($invoice_two->amount , -1.36, "$testname: amount ok");
+
+
+ my $bt = create_bank_transaction(record => $invoice_two,
+ amount => $invoice_two->amount + $invoice->amount,
+ bank_chart_id => $bank->id,
+ transdate => $dt_10,
+ );
+ # my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($invoice_two);
+ # is($agreement, 15, "points for negative ap transaction ok");
+
+ $::form->{invoice_ids} = {
+ $bt->id => [ $invoice->id, $invoice_two->id ]
+ };
+
+ save_btcontroller_to_string();
+
+ $invoice->load;
+ $invoice_two->load;
+ $bt->load;
+
+ is($invoice->amount , '-23.80000', "$testname: first inv amount ok");
+ is($invoice->netamount, '-20.00000', "$testname: first inv netamount ok");
+ is($invoice->paid , '-23.80000', "$testname: first inv paid ok");
+ is($invoice_two->amount , '-1.36000', "$testname: second inv amount ok");
+ is($invoice_two->netamount, '-1.14000', "$testname: second inv netamount ok");
+ is($invoice_two->paid , '-1.36000', "$testname: second inv paid ok");
+ is($bt->invoice_amount, '25.16000', "$testname: bt invoice amount for both invoices were assigned");
+
+
+ return ($invoice, $invoice_two);
+};
+
+sub test_ap_payment_transaction {
+ my (%params) = @_;
+ my $testname = 'test_ap_payment_transaction';
+ my $netamount = 115;
+ my $amount = $::form->round_amount($netamount * 1.19,2);
+ my $invoice = SL::DB::PurchaseInvoice->new(
+ invoice => 0,
+ invnumber => $params{invnumber} || $testname,
+ amount => $amount,
+ netamount => $netamount,
+ transdate => $dt,
+ taxincluded => 0,
+ vendor_id => $vendor->id,
+ taxzone_id => $vendor->taxzone_id,
+ currency_id => $currency_id,
+ transactions => [],
+ notes => $testname,
+ );
+ $invoice->add_ap_amount_row(
+ amount => $invoice->netamount,
+ chart => $ap_amount_chart,
+ tax_id => $tax_9->id,
+ );
+
+ $invoice->create_ap_row(chart => $ap_chart);
+ $invoice->save;
+
+ is($invoice->netamount, 115 , "$testname: netamount ok");
+ is($invoice->amount , 136.85, "$testname: amount ok");
+
+ my $bt = create_bank_transaction(record => $invoice,
+ amount => $invoice->amount,
+ bank_chart_id => $bank->id,
+ transdate => $dt_10,
+ );
+ $::form->{invoice_ids} = {
+ $bt->id => [ $invoice->id ]
+ };
+
+ save_btcontroller_to_string();
+
+ $invoice->load;
+ $bt->load;
+
+ is($invoice->amount , '136.85000', "$testname: amount ok");
+ is($invoice->netamount, '115.00000', "$testname: netamount ok");
+ is($bt->amount, '-136.85000', "$testname: bt amount ok");
+ is($invoice->paid , '136.85000', "$testname: paid ok");
+ is($bt->invoice_amount, '-136.85000', "$testname: bt invoice amount for ap was assigned");
+
+ return $invoice;
+};
+
+sub test_ap_payment_part_transaction {
+ my (%params) = @_;
+ my $testname = 'test_ap_payment_p_transaction';
+ my $netamount = 115;
+ my $amount = $::form->round_amount($netamount * 1.19,2);
+ my $invoice = SL::DB::PurchaseInvoice->new(
+ invoice => 0,
+ invnumber => $params{invnumber} || $testname,
+ amount => $amount,
+ netamount => $netamount,
+ transdate => $dt,
+ taxincluded => 0,
+ vendor_id => $vendor->id,
+ taxzone_id => $vendor->taxzone_id,
+ currency_id => $currency_id,
+ transactions => [],
+ notes => $testname,
+ );
+ $invoice->add_ap_amount_row(
+ amount => $invoice->netamount,
+ chart => $ap_amount_chart,
+ tax_id => $tax_9->id,
+ );
+
+ $invoice->create_ap_row(chart => $ap_chart);
+ $invoice->save;
+
+ is($invoice->netamount, 115 , "$testname: netamount ok");
+ is($invoice->amount , 136.85, "$testname: amount ok");
+
+ my $bt = create_bank_transaction(record => $invoice,
+ amount => $invoice->amount-100,
+ bank_chart_id => $bank->id,
+ transdate => $dt_10,
+ );
+ $::form->{invoice_ids} = {
+ $bt->id => [ $invoice->id ]
+ };
+
+ save_btcontroller_to_string();
+
+ $invoice->load;
+ $bt->load;
+
+ is($invoice->amount , '136.85000', "$testname: amount ok");
+ is($invoice->netamount, '115.00000', "$testname: netamount ok");
+ is($bt->amount, '-36.85000', "$testname: bt amount ok");
+ is($invoice->paid , '36.85000', "$testname: paid ok");
+ is($bt->invoice_amount, '-36.85000', "$testname: bt invoice amount for ap was assigned");
+
+ my $bt2 = create_bank_transaction(record => $invoice,
+ amount => 100,
+ bank_chart_id => $bank->id,
+ transdate => $dt_10,
+ );
+ $::form->{invoice_ids} = {
+ $bt2->id => [ $invoice->id ]
+ };
+
+ save_btcontroller_to_string();
+ $invoice->load;
+ $bt2->load;
+
+ is($invoice->amount , '136.85000', "$testname: amount ok");
+ is($invoice->netamount, '115.00000', "$testname: netamount ok");
+ is($bt2->amount, '-100.00000',"$testname: bt amount ok");
+ is($invoice->paid , '136.85000', "$testname: paid ok");
+ is($bt2->invoice_amount,'-100.00000', "$testname: bt invoice amount for ap was assigned");
+
+ return $invoice;
+};
+
+sub test_neg_sales_invoice {
+
+ my $testname = 'test_neg_sales_invoice';
+
+ my $part1 = new_part( partnumber => 'Funkenhaube öhm')->save;
+ my $part2 = new_service(partnumber => 'Service-Pauschale Pasch!')->save;
+
+ my $neg_sales_inv = create_sales_invoice(
+ invnumber => '20172201',
+ customer => $customer,
+ taxincluded => 0,
+ transdate => $dt,
+ invoiceitems => [ create_invoice_item(part => $part1, qty => 3, sellprice => 70),
+ create_invoice_item(part => $part2, qty => 10, sellprice => -50),
+ ]
+ );
+ my $bt = create_bank_transaction(record => $neg_sales_inv,
+ amount => $neg_sales_inv->amount,
+ bank_chart_id => $bank->id,
+ transdate => $dt,
+ valutadate => $dt,
+ );
+ $::form->{invoice_ids} = {
+ $bt->id => [ $neg_sales_inv->id ]
+ };
+
+ save_btcontroller_to_string();
+
+ $neg_sales_inv->load;
+ $bt->load;
+ is($neg_sales_inv->amount , '-345.10000', "$testname: amount ok");
+ is($neg_sales_inv->netamount, '-290.00000', "$testname: netamount ok");
+ is($neg_sales_inv->paid , '-345.10000', "$testname: paid ok");
+ is($bt->amount , '-345.10000', "$testname: bt amount ok");
+ is($bt->invoice_amount , '-345.10000', "$testname: bt invoice_amount ok");
+}
+
+sub test_bt_rule1 {
+
+ my $testname = 'test_bt_rule1';
+
+ $ar_transaction = test_ar_transaction(invnumber => 'bt_rule1', transdate => $dt);
+
+ my $bt = create_bank_transaction(record => $ar_transaction, transdate => $dt) or die "Couldn't create bank_transaction";
+
+ $ar_transaction->load;
+ $bt->load;
+ is($ar_transaction->paid , '0.00000' , "$testname: not paid");
+ is($bt->invoice_amount , '0.00000' , "$testname: bt invoice amount was not assigned");
+
+ my $bt_controller = SL::Controller::BankTransaction->new;
+ my ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
+
+ is(scalar(@$bt_transactions) , 1 , "$testname: one bank_transaction");
+ is($bt_transactions->[0]->{agreement}, 20 , "$testname: agreement == 20");
+ my $match = join ( ' ',@{$bt_transactions->[0]->{rule_matches}});
+ #print "rule_matches='".$match."'\n";
+ is($match,
+ "remote_account_number(3) exact_amount(4) own_invoice_in_purpose(5) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus0(3) ",
+ "$testname: rule_matches ok");
+ $bt->invoice_amount($bt->amount);
+ $bt->save;
+ is($bt->invoice_amount , '119.00000' , "$testname: bt invoice amount now set");
+};
+
+sub test_sepa_export {
+
+ my $testname = 'test_sepa_export';
+
+ $ar_transaction = test_ar_transaction(invnumber => 'sepa1', transdate => $dt);
+
+ my $bt = create_bank_transaction(record => $ar_transaction, transdate => $dt) or die "Couldn't create bank_transaction";
+ my $se = create_sepa_export();
+ my $sei = create_sepa_export_item(
+ chart_id => $bank->id,
+ ar_id => $ar_transaction->id,
+ sepa_export_id => $se->id,
+ vc_iban => $customer->iban,
+ vc_bic => $customer->bic,
+ vc_mandator_id => $customer->mandator_id,
+ vc_depositor => $customer->depositor,
+ amount => $ar_transaction->amount,
+ );
+ require SL::SEPA::XML;
+ my $sepa_xml = SL::SEPA::XML->new('company' => $customer->name,
+ 'creditor_id' => "id",
+ 'src_charset' => 'UTF-8',
+ 'message_id' => "test",
+ 'grouped' => 1,
+ 'collection' => 1,
+ );
+ is($sepa_xml->{company} , 'Test Customer OLE S.L. Ardbaerg AB');
+
+ $ar_transaction->load;
+ $bt->load;
+ $sei->load;
+ is($ar_transaction->paid , '0.00000' , "$testname: sepa1 not paid");
+ is($bt->invoice_amount , '0.00000' , "$testname: bt invoice amount was not assigned");
+ is($bt->amount , '119.00000' , "$testname: bt amount ok");
+ is($sei->amount , '119.00000' , "$testname: sepa export amount ok");
+
+ my $bt_controller = SL::Controller::BankTransaction->new;
+ my ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
+
+ is(scalar(@$bt_transactions) , 1 , "$testname: one bank_transaction");
+ is($bt_transactions->[0]->{agreement}, 25 , "$testname: agreement == 25");
+ my $match = join ( ' ',@{$bt_transactions->[0]->{rule_matches}});
+ is($match,
+ "remote_account_number(3) exact_amount(4) own_invoice_in_purpose(5) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus0(3) sepa_export_item(5) ",
+ "$testname: rule_matches ok");
+};
+
+sub test_two_banktransactions {
+
+ my $testname = 'two_banktransactions';
+
+ my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv10000' , amount => 2912.00 );
+ my $bt1 = create_bank_transaction(record => $ar_transaction_1,
+ amount => $ar_transaction_1->amount,
+ purpose => "Rechnung10000 beinahe",
+ transdate => $dt,
+ bank_chart_id => $bank->id,
+ ) or die "Couldn't create bank_transaction";
+
+ my $bt2 = create_bank_transaction(record => $ar_transaction_1,
+ amount => $ar_transaction_1->amount + 0.01,
+ purpose => "sicher salesinv20000 vielleicht",
+ transdate => $dt,
+ bank_chart_id => $bank->id,
+ ) or die "Couldn't create bank_transaction";
+
+ my ($agreement1, $rule_matches1) = $bt1->get_agreement_with_invoice($ar_transaction_1);
+ is($agreement1, 19, "bt1 19 points for ar_transaction_1 in $testname ok");
+ #print "rule_matches1=".$rule_matches1."\n";
+ is($rule_matches1,
+ "remote_account_number(3) exact_amount(4) own_invnumber_in_purpose(4) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus0(3) ",
+ "$testname: rule_matches ok");
+ my ($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_1);
+ is($agreement2, 11, "bt2 11 points for ar_transaction_1 in $testname ok");
+ is($rule_matches2,
+ "remote_account_number(3) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus0(3) ",
+ "$testname: rule_matches ok");
+
+ my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv20000' , amount => 2912.01 );
+ my $ar_transaction_3 = test_ar_transaction(invnumber => 'zweitemit10000', amount => 2912.00 );
+ ($agreement1, $rule_matches1) = $bt1->get_agreement_with_invoice($ar_transaction_2);
+
+ is($agreement1, 11, "bt1 11 points for ar_transaction_2 in $testname ok");
+
+ ($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_2);
+ is($agreement2, 20, "bt2 20 points for ar_transaction_2 in $testname ok");
+
+ ($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_1);
+ is($agreement2, 11, "bt2 11 points for ar_transaction_1 in $testname ok");
+
+ my $bt3 = create_bank_transaction(record => $ar_transaction_3,
+ amount => $ar_transaction_3->amount,
+ purpose => "sicher Rechnung10000 vielleicht",
+ transdate => $dt,
+ bank_chart_id => $bank->id,
+ ) or die "Couldn't create bank_transaction";
+
+ my ($agreement3, $rule_matches3) = $bt3->get_agreement_with_invoice($ar_transaction_3);
+ is($agreement3, 19, "bt3 19 points for ar_transaction_3 in $testname ok");
+
+ $bt2->delete;
+ $ar_transaction_2->delete;
+
+ #nun sollten zwei gleichwertige Rechnungen $ar_transaction_1 und $ar_transaction_3 für $bt1 gefunden werden
+ #aber es darf keine Proposals geben mit mehreren Rechnungen
+ my $bt_controller = SL::Controller::BankTransaction->new;
+ my ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
+
+ is(scalar(@$bt_transactions) , 2 , "$testname: two bank_transaction");
+ is(scalar(@$proposals) , 0 , "$testname: no proposals");
+
+ $ar_transaction_3->delete;
+
+ # Jetzt gibt es zwei Kontobewegungen mit gleichen Punkten für eine Rechnung.
+ # hier darf es auch keine Proposals geben
+
+ ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
+
+ is(scalar(@$bt_transactions) , 2 , "$testname: two bank_transaction");
+ # odyn testfall - anforderungen so (noch) nicht in kivi
+ # is(scalar(@$proposals) , 0 , "$testname: no proposals");
+
+ # Jetzt gibt es zwei Kontobewegungen für eine Rechnung.
+ # eine Bewegung bekommt mehr Punkte
+ # hier darf es auch keine Proposals geben
+ $bt3->update_attributes( purpose => "fuer Rechnung salesinv10000");
+
+ ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
+
+ is(scalar(@$bt_transactions) , 2 , "$testname: two bank_transaction");
+ # odyn testfall - anforderungen so (noch) nicht in kivi
+ # is(scalar(@$proposals) , 1 , "$testname: one proposal");
+
+};
+sub test_closedto {
+
+ my $testname = 'closedto';
+
+ my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv10000' , amount => 2912.00 );
+ my $bt1 = create_bank_transaction(record => $ar_transaction_1,
+ amount => $ar_transaction_1->amount,
+ purpose => "Rechnung10000 beinahe",
+ bank_chart_id => $bank->id,
+ ) or die "Couldn't create bank_transaction";
+
+ $bt1->valutadate(DateTime->new(year => 2019, month => 12, day => 30));
+ $bt1->save();
+
+ is($bt1->closed_period, 0, "$testname undefined closedto");
+
+ my $defaults = SL::DB::Manager::Default->get_all(limit => 1)->[0];
+ $defaults->closedto(DateTime->new(year => 2019, month => 12, day => 31));
+ $defaults->save();
+ $::instance_conf->reload->data;
+ $bt1->load();
+ is($bt1->closed_period, 1, "$testname defined and next date closedto");
+
+ $bt1->valutadate(DateTime->new(year => 2020, month => 1, day => 1));
+ $bt1->save();
+ $bt1->load();
+
+ is($bt1->closed_period, 0, "$testname defined closedto and next date valuta");
+ $defaults->closedto(undef);
+ $defaults->save();
+
+}
+
+sub test_skonto_exact_ap_transaction {
+
+ my $testname = 'test_skonto_exact_ap_transaction';
+
+ $ap_transaction = test_ap_transaction(invnumber => 'ap transaction skonto',
+ payment_id => $payment_terms->id,
+ );
+
+ my $bt = create_bank_transaction(record => $ap_transaction,
+ bank_chart_id => $bank->id,
+ transdate => $dt,
+ valutadate => $dt,
+ amount => $ap_transaction->amount_less_skonto
+ ) or die "Couldn't create bank_transaction";
+
+ $::form->{invoice_ids} = {
+ $bt->id => [ $ap_transaction->id ]
+ };
+ $::form->{invoice_skontos} = {
+ $bt->id => [ 'with_skonto_pt' ]
+ };
+
+ save_btcontroller_to_string();
+
+ $ap_transaction->load;
+ $bt->load;
+ is($ap_transaction->paid , '119.00000' , "$testname: ap transaction skonto was paid");
+ is($ap_transaction->closed , 1 , "$testname: ap transaction skonto is closed");
+ is($bt->invoice_amount , '113.05000' , "$testname: bt invoice amount was assigned");
+
+};
+