Testfall test_ap_transaction auch ausführen
[kivitendo-erp.git] / t / bank / bank_transactions.t
index b5e5cad..9813b74 100644 (file)
@@ -1,4 +1,4 @@
-use Test::More;
+use Test::More tests => 89;
 
 use strict;
 
@@ -46,10 +46,23 @@ sub clear_up {
   SL::DB::Manager::Currency->delete_all(where => [ name => 'CUR' ]);
 };
 
+sub save_btcontroller_to_string {
+  my $output;
+  open(my $outputFH, '>', \$output) or die;
+  my $oldFH = select $outputFH;
+
+  my $bt_controller = SL::Controller::BankTransaction->new;
+  $bt_controller->action_save_invoices;
+
+  select $oldFH;
+  close $outputFH;
+  return $output;
+}
 
 # starting test:
 Support::TestSetup::login();
 
+clear_up();
 reset_state(); # initialise customers/vendors/bank/currency/...
 
 test1();
@@ -59,7 +72,9 @@ test_skonto_exact();
 test_two_invoices();
 test_partial_payment();
 test_credit_note();
-
+test_neg_ap_transaction();
+test_ap_transaction();
+test_ap_payment_transaction();
 # remove all created data at end of test
 clear_up();
 
@@ -169,17 +184,17 @@ sub test_ap_transaction {
   my $netamount = 100;
   my $amount    = $::form->round_amount($netamount * 1.19,2);
   my $invoice   = SL::DB::PurchaseInvoice->new(
-      invoice      => 0,
-      invnumber    => $params{invnumber} || 'test_ap_transaction',
-      amount       => $amount,
-      netamount    => $netamount,
-      transdate    => $transdate1,
-      taxincluded  => 0,
-      vendor_id    => $vendor->id,
-      taxzone_id   => $vendor->taxzone_id,
-      currency_id  => $currency_id,
-      transactions => [],
-      notes        => 'test_ap_transaction',
+    invoice      => 0,
+    invnumber    => $params{invnumber} || 'test_ap_transaction',
+    amount       => $amount,
+    netamount    => $netamount,
+    transdate    => $transdate1,
+    taxincluded  => 0,
+    vendor_id    => $vendor->id,
+    taxzone_id   => $vendor->taxzone_id,
+    currency_id  => $currency_id,
+    transactions => [],
+    notes        => 'test_ap_transaction',
   );
   $invoice->add_ap_amount_row(
     amount     => $invoice->netamount,
@@ -212,11 +227,10 @@ sub test1 {
   my $bt = SL::Dev::Payment::create_bank_transaction(record => $ar_transaction) or die "Couldn't create bank_transaction";
 
   $::form->{invoice_ids} = {
-          $bt->id => [ $ar_transaction->id ]
-        };
+    $bt->id => [ $ar_transaction->id ]
+  };
 
-  my $bt_controller = SL::Controller::BankTransaction->new;
-  $bt_controller->action_save_invoices;
+  save_btcontroller_to_string();
 
   $ar_transaction->load;
   $bt->load;
@@ -240,14 +254,13 @@ sub test_skonto_exact {
                                                     ) or die "Couldn't create bank_transaction";
 
   $::form->{invoice_ids} = {
-          $bt->id => [ $ar_transaction->id ]
-        };
+    $bt->id => [ $ar_transaction->id ]
+  };
   $::form->{invoice_skontos} = {
-          $bt->id => [ 'with_skonto_pt' ]
-        };
+    $bt->id => [ 'with_skonto_pt' ]
+  };
 
-  my $bt_controller = SL::Controller::BankTransaction->new;
-  $bt_controller->action_save_invoices;
+  save_btcontroller_to_string();
 
   $ar_transaction->load;
   $bt->load;
@@ -270,12 +283,14 @@ sub test_two_invoices {
                                                      bank_chart_id => $bank->id,
                                                     ) or die "Couldn't create bank_transaction";
 
+  my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($ar_transaction_1);
+  is($agreement, 16, "points for ar_transaction_1 in test_two_invoices ok");
+
   $::form->{invoice_ids} = {
-          $bt->id => [ $ar_transaction_1->id, $ar_transaction_2->id ]
-        };
+    $bt->id => [ $ar_transaction_1->id, $ar_transaction_2->id ]
+  };
 
-  my $bt_controller = SL::Controller::BankTransaction->new;
-  $bt_controller->action_save_invoices;
+  save_btcontroller_to_string();
 
   $ar_transaction_1->load;
   $ar_transaction_2->load;
@@ -302,11 +317,10 @@ sub test_overpayment {
                                                     ) or die "Couldn't create bank_transaction";
 
   $::form->{invoice_ids} = {
-          $bt->id => [ $ar_transaction->id ]
-        };
+    $bt->id => [ $ar_transaction->id ]
+  };
 
-  my $bt_controller = SL::Controller::BankTransaction->new;
-  $bt_controller->action_save_invoices;
+  save_btcontroller_to_string();
 
   $ar_transaction->load;
   $bt->load;
@@ -338,16 +352,14 @@ sub test_overpayment_with_partialpayment {
                                                       ) or die "Couldn't create bank_transaction";
 
   $::form->{invoice_ids} = {
-          $bt_1->id => [ $ar_transaction->id ]
-        };
-  my $bt_controller_1 = SL::Controller::BankTransaction->new;
-  $bt_controller_1->action_save_invoices;
+    $bt_1->id => [ $ar_transaction->id ]
+  };
+  save_btcontroller_to_string();
 
   $::form->{invoice_ids} = {
-          $bt_2->id => [ $ar_transaction->id ]
-        };
-  my $bt_controller_2 = SL::Controller::BankTransaction->new;
-  $bt_controller_2->action_save_invoices;
+    $bt_2->id => [ $ar_transaction->id ]
+  };
+  save_btcontroller_to_string();
 
   $ar_transaction->load;
   $bt_1->load;
@@ -372,11 +384,10 @@ sub test_partial_payment {
                                                     ) or die "Couldn't create bank_transaction";
 
   $::form->{invoice_ids} = {
-          $bt->id => [ $ar_transaction->id ]
-        };
+    $bt->id => [ $ar_transaction->id ]
+  };
 
-  my $bt_controller = SL::Controller::BankTransaction->new;
-  $bt_controller->action_save_invoices;
+  save_btcontroller_to_string();
 
   $ar_transaction->load;
   $bt->load;
@@ -410,11 +421,10 @@ sub test_credit_note {
   is($rule_matches, 'remote_account_number(3) exact_amount(4) wrong_sign(-1) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus14(2) ', "rules_matches for credit note ok");
 
   $::form->{invoice_ids} = {
-          $bt->id => [ $credit_note->id ]
+    $bt->id => [ $credit_note->id ]
   };
 
-  my $bt_controller = SL::Controller::BankTransaction->new;
-  $bt_controller->action_save_invoices;
+  save_btcontroller_to_string();
 
   $credit_note->load;
   $bt->load;
@@ -423,4 +433,111 @@ sub test_credit_note {
   is($credit_note->paid     , '-844.90000', "$testname: paid ok");
 }
 
+sub test_neg_ap_transaction {
+  my (%params) = @_;
+  my $testname = 'test_neg_ap_transaction';
+  my $netamount = -20;
+  my $amount    = $::form->round_amount($netamount * 1.19,2);
+  my $invoice   = SL::DB::PurchaseInvoice->new(
+    invoice      => 0,
+    invnumber    => $params{invnumber} || 'test_neg_ap_transaction',
+    amount       => $amount,
+    netamount    => $netamount,
+    transdate    => $transdate1,
+    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            = SL::Dev::Payment::create_bank_transaction(record        => $invoice,
+                                                                amount        => $invoice->amount,
+                                                                bank_chart_id => $bank->id,
+                                                                transdate     => DateTime->today->add(days => 10),
+                                                               );
+  my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($invoice);
+  is($agreement, 15, "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");
+
+  return $invoice;
+};
+
+sub test_ap_payment_transaction {
+  my (%params) = @_;
+  my $testname = 'test_ap_two_transaction';
+  my $netamount = 115;
+  my $amount    = $::form->round_amount($netamount * 1.19,2);
+  my $invoice   = SL::DB::PurchaseInvoice->new(
+    invoice      => 0,
+    invnumber    => $params{invnumber} || 'test_ap_two_transaction',
+    amount       => $amount,
+    netamount    => $netamount,
+    transdate    => $transdate1,
+    taxincluded  => 0,
+    vendor_id    => $vendor->id,
+    taxzone_id   => $vendor->taxzone_id,
+    currency_id  => $currency_id,
+    transactions => [],
+    notes        => 'test_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, 115  , "$testname: netamount ok");
+  is($invoice->amount   , 136.85, "$testname: amount ok");
+
+  my $bt            = SL::Dev::Payment::create_bank_transaction(record        => $invoice,
+                                                                amount        => $invoice->amount,
+                                                                bank_chart_id => $bank->id,
+                                                                transdate     => DateTime->today->add(days => 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;
+};
 1;