Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 009d45cd

Von Jan Büren vor etwa 2 Jahren hinzugefügt

  • ID 009d45cd4f719b09f5f4b9ff6597e75abcd0527d
  • Vorgänger 8328e037
  • Nachfolger b319acec

bank_transactions: Neue Wechselkurstestfälle

- negative Debitorenbuchung
- negative Kreditorenbuchung
- Zwei Banktransaktionen (eine wieder rückgängig machen)
- Wechselkursgewinn und Verlusttest
- Prüfung ob Gebühren verbucht werden
- Prüfung ob Fehlermeldung bei Gebühren im Verkauf erscheint

Unterschiede anzeigen:

t/bank/bank_transactions.t
1
use Test::More tests => 293;
1
use Test::More tests => 395;
2 2

  
3 3
use strict;
4 4

  
......
16 16
use SL::DB::Currency;
17 17
use SL::DB::Customer;
18 18
use SL::DB::Default;
19
use SL::DB::Exchangerate;
19 20
use SL::DB::Vendor;
20 21
use SL::DB::Invoice;
21 22
use SL::DB::Unit;
......
31 32
use Data::Dumper;
32 33

  
33 34
my ($customer, $vendor, $currency_id, $unit, $tax, $tax0, $tax7, $tax_9, $payment_terms, $bank_account);
34
my ($currency);
35
my ($currency, $currency_usd, $fxgain_chart, $fxloss_chart);
35 36
my ($ar_chart,$bank,$ar_amount_chart, $ap_chart, $ap_amount_chart);
36 37
my ($ar_transaction, $ap_transaction);
37 38
my ($dt, $dt_5, $dt_10, $year);
......
40 41

  
41 42
  SL::DB::Manager::BankTransactionAccTrans->delete_all(all => 1);
42 43
  SL::DB::Manager::BankTransaction->delete_all(all => 1);
44
  SL::DB::Manager::GLTransaction->delete_all(all => 1);
43 45
  SL::DB::Manager::InvoiceItem->delete_all(all => 1);
44 46
  SL::DB::Manager::InvoiceItem->delete_all(all => 1);
45 47
  SL::DB::Manager::Invoice->delete_all(all => 1);
......
51 53
  SL::DB::Manager::SepaExport->delete_all(all => 1);
52 54
  SL::DB::Manager::BankAccount->delete_all(all => 1);
53 55
  SL::DB::Manager::PaymentTerm->delete_all(all => 1);
56
  SL::DB::Manager::Exchangerate->delete_all(all => 1);
54 57
  SL::DB::Manager::Currency->delete_all(where => [ name => 'CUR' ]);
58
  SL::DB::Manager::Currency->delete_all(where => [ name => 'USD' ]);
55 59
  # SL::DB::Manager::Default->delete_all(all => 1);
56 60
};
57 61

  
......
76 80
clear_up();
77 81
reset_state(); # initialise customers/vendors/bank/currency/...
78 82

  
79
test1();
83
negative_ap_transaction_fx_gain_fees();
84
test_negative_ar_transaction_fx_loss();
85
test_ar_transaction_fx_loss();
86
test_ar_transaction_fx_gain();
87
#test_neg_sales_invoice_fx();
88
negative_ap_transaction_fx_gain_fees_error();
89

  
90
ap_transaction_fx_gain_fees();
91
ap_transaction_fx_gain_fees_two_payments();
92
ap_transaction_fx_loss_fees();
80 93

  
94
test1();
81 95
test_overpayment_with_partialpayment();
82 96
test_overpayment();
83 97
reset_state();
......
97 111
test_full_workflow_ar_multiple_inv_skonto_reconciliate_and_undo();
98 112
reset_state();
99 113
test_sepa_export();
100

  
101 114
reset_state();
102 115
test_bt_rule1();
103 116
reset_state();
......
130 143

  
131 144
  $currency_id     = $::instance_conf->get_currency_id;
132 145

  
146
  $currency_usd    = SL::DB::Currency->new(name => 'USD')->save;
147
  $fxgain_chart = SL::DB::Manager::Chart->find_by(accno => '2660') or die "Can't find fxgain_chart in test";
148
  $fxloss_chart = SL::DB::Manager::Chart->find_by(accno => '2150') or die "Can't find fxloss_chart in test";
149
  $currency_usd->db->dbh->do('UPDATE defaults SET fxgain_accno_id = ' . $fxgain_chart->id);
150
  $currency_usd->db->dbh->do('UPDATE defaults SET fxloss_accno_id = ' . $fxloss_chart->id);
151
  $::instance_conf->reload->data;
152
  is($fxgain_chart->id,  $::instance_conf->get_fxgain_accno_id, "fxgain_chart was updated in defaults");
153
  is($fxloss_chart->id,  $::instance_conf->get_fxloss_accno_id, "fxloss_chart was updated in defaults");
154

  
155

  
156

  
133 157
  $bank_account     =  SL::DB::BankAccount->new(
134 158
    account_number  => '123',
135 159
    bank_code       => '123',
......
718 742
                    ]
719 743
  );
720 744
  my $bt            = create_bank_transaction(record        => $credit_note,
721
                                                                amount        => $credit_note->amount,
722
                                                                bank_chart_id => $bank->id,
723
                                                                transdate     => $dt_10,
724
                                                               );
745
                                              amount        => abs($credit_note->amount),
746
                                              bank_chart_id => $bank->id,
747
                                              transdate     => $dt_10,
748
                                             );
725 749
  my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($credit_note);
726 750
  is($agreement, 14, "points for credit note ok");
727 751
  is($rule_matches, 'remote_account_number(3) exact_amount(4) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus14(2) ', "rules_matches for credit note ok");
......
770 794
  is($invoice->amount   , -23.8, "$testname: amount ok");
771 795

  
772 796
  my $bt            = create_bank_transaction(record        => $invoice,
773
                                              amount        => $invoice->amount,
797
                                              amount        => abs($invoice->amount),
774 798
                                              bank_chart_id => $bank->id,
775 799
                                              transdate     => $dt_10,
776 800
                                                               );
......
853 877

  
854 878

  
855 879
  my $bt            = create_bank_transaction(record        => $invoice_two,
856
                                              amount        => $invoice_two->amount + $invoice->amount,
880
                                              amount        => abs($invoice_two->amount + $invoice->amount),
857 881
                                              bank_chart_id => $bank->id,
858 882
                                              transdate     => $dt_10,
859 883
                                                               );
......
1270 1294
  is($bt->invoice_amount     , '113.05000' , "$testname: bt invoice amount was assigned");
1271 1295

  
1272 1296
};
1297
sub ap_transaction_fx_gain_fees {
1298

  
1299
  my $testname     = 'ap_transaction_fx_gain_fees';
1300
  my $usd_amount   = 83300;
1301
  my $fx_rate      = 2;
1302
  my $fx_rate_bank = 1.75;
1303
  my $eur_amount   = $usd_amount * $fx_rate;
1304

  
1305
  my $ap_transaction_fx = create_ap_fx_transaction (invnumber   => 'ap transaction fx amount',
1306
                                                    payment_id  => $payment_terms->id,
1307
                                                    fx_rate     => $fx_rate,
1308
                                                    netamount   => $eur_amount,
1309
                                                    invoice_set => 1,
1310
                                                    buysell     => 'sell',
1311
                                                   );
1312
  # check exchangerate
1313
  is($ap_transaction_fx->currency->name   , 'USD'     , "$testname: USD currency");
1314
  is($ap_transaction_fx->get_exchangerate , '2.00000' , "$testname: fx rate record");
1315
  my $bt = create_bank_transaction(record        => $ap_transaction_fx,
1316
                                   bank_chart_id => $bank->id,
1317
                                   transdate     => $dt,
1318
                                   valutadate    => $dt,
1319
                                   amount        => 145949.93,
1320
                                   exchangerate  => $fx_rate_bank,
1321
                                  ) or die "Couldn't create bank_transaction";
1322
#          'exchangerates' => [
1323
#                               '1.75'
1324
#                             ],
1325
#          'book_fx_bank_fees' => [
1326
#                                   '1'
1327
#                                 ],
1328
# book_fx_bank_fees   => [  map { $::form->{"book_fx_bank_fees_${bank_transaction_id}_${_}"} } @{ $invoice_ids } ],
1329

  
1330
  $::form->{invoice_ids} = {
1331
    $bt->id => [ $ap_transaction_fx->id ]
1332
  };
1333
  $::form->{"book_fx_bank_fees_" . $bt->id . "_" . $ap_transaction_fx->id}  = 1;
1334
  $::form->{"exchangerate_"      . $bt->id . "_" . $ap_transaction_fx->id}  = "1,75"; # will be parsed
1335
  $::form->{"currency_id_"       . $bt->id . "_" . $ap_transaction_fx->id}  = $currency_usd->id;
1336

  
1273 1337

  
1338
  my $ret = save_btcontroller_to_string();
1339
  $ap_transaction_fx->load;
1340
  $bt->load;
1341
  is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )},
1342
       5, "$testname 5 acc_trans entries created");
1343

  
1344
  my $gl_fee_booking = SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id, '!gl_id' => undef ] );
1345
  is(scalar @{ $gl_fee_booking }, 2, 'Two GL acc_trans bookings');
1346

  
1347
  my $gl = SL::DB::Manager::GLTransaction->get_first(where => [ id => $gl_fee_booking->[0]->gl_id ]);
1348
  is (abs($gl->transactions->[0]->amount), '174.93', 'fee abs amount correct');
1349

  
1350
  my $fx_gain_transactions = SL::DB::Manager::AccTransaction->get_all(where =>
1351
                                [ trans_id => $ap_transaction_fx->id, chart_id => $fxgain_chart->id ],
1352
                                  sort_by => ('acc_trans_id'));
1353

  
1354
  is($fx_gain_transactions->[0]->amount,   '20825.00000', "$testname: fx gain amount ok");
1355

  
1356

  
1357
  is($ap_transaction_fx->paid   , '166600.00000' , "$testname: ap transaction skonto was paid");
1358
  is($bt->invoice_amount        , '-145949.93000'   , "$testname: bt invoice amount was assigned");
1359

  
1360
}
1361
sub ap_transaction_fx_gain_fees_two_payments {
1362

  
1363
  my $testname     = 'ap_transaction_fx_gain_fees_two_payments';
1364
  my $usd_amount   = 11901.19;
1365
  my $fx_rate      = 2;
1366
  my $fx_rate_bank = 1.75;
1367
  my $eur_amount   = $usd_amount * $fx_rate;
1368
  my $ap_transaction_fx = create_ap_fx_transaction (invnumber   => 'ap transaction fx amount',
1369
                                                    payment_id  => $payment_terms->id,
1370
                                                    fx_rate     => $fx_rate,
1371
                                                    netamount   => $eur_amount,
1372
                                                    invoice_set => 0,
1373
                                                    buysell     => 'sell',
1374
                                                   );
1375
  # check exchangerate
1376
  is($ap_transaction_fx->currency->name   , 'USD'     , "$testname: USD currency");
1377
  is($ap_transaction_fx->get_exchangerate , '2.00000' , "$testname: fx rate record");
1378
  is($ap_transaction_fx->amount           , 23802.38  , "$testname: record amount");
1379
  is($ap_transaction_fx->paid             , 0         , "$testname: record amount");
1380
  my $bt = create_bank_transaction(record        => $ap_transaction_fx,
1381
                                   bank_chart_id => $bank->id,
1382
                                   transdate     => $dt,
1383
                                   valutadate    => $dt,
1384
                                   amount        => 18698.25,
1385
                                   exchangerate  => $fx_rate_bank,
1386
                                  ) or die "Couldn't create bank_transaction";
1387

  
1388
  is($bt->amount        , '-18698.25'   , "$testname: bt amount ok");
1389

  
1390
  $::form->{invoice_ids} = {
1391
    $bt->id => [ $ap_transaction_fx->id ]
1392
  };
1393
  $::form->{"book_fx_bank_fees_" . $bt->id . "_" . $ap_transaction_fx->id}  = 1;
1394
  $::form->{"exchangerate_"      . $bt->id . "_" . $ap_transaction_fx->id}  = "1,75"; # will be parsed
1395
  $::form->{"currency_id_"       . $bt->id . "_" . $ap_transaction_fx->id}  = $currency_usd->id;
1396

  
1397

  
1398
  my $ret = save_btcontroller_to_string();
1399
  $ap_transaction_fx->load;
1400
  $bt->load;
1401
  is($ap_transaction_fx->get_exchangerate , '2.00000' , "$testname: fx rate record");
1402
  is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )},
1403
       3, "$testname 3 acc_trans entries created");
1404

  
1405
  my $gl_fee_booking = SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id, '!gl_id' => undef ] );
1406
  is(scalar @{ $gl_fee_booking }, 0, 'Zero GL acc_trans bookings');
1407

  
1408
  my $fx_gain_transactions = SL::DB::Manager::AccTransaction->get_all(where =>
1409
                                [ trans_id => $ap_transaction_fx->id, chart_id => $fxgain_chart->id ],
1410
                                  sort_by => ('acc_trans_id'));
1411

  
1412
  is($fx_gain_transactions->[0]->amount,   '2671.18000', "$testname: fx gain amount ok");
1413

  
1414
  is($ap_transaction_fx->paid, '21369.43000'    , "$testname: ap transaction fx two payment was paid");
1415
  is($bt->invoice_amount     , '-18698.25000'   , "$testname: bt invoice amount was assigned");
1416

  
1417
  my $bt2 = create_bank_transaction(record        => $ap_transaction_fx,
1418
                                    bank_chart_id => $bank->id,
1419
                                    transdate     => $dt,
1420
                                    valutadate    => $dt,
1421
                                    amount        => 1264.20,
1422
                                    exchangerate  => 0.1,
1423
                                   ) or die "Couldn't create bank_transaction";
1424

  
1425
  is($bt2->amount        , '-1264.2'   , "$testname: bt amount ok");
1426

  
1427
  $::form->{invoice_ids} = {
1428
    $bt2->id => [ $ap_transaction_fx->id ]
1429
  };
1430
  $::form->{"book_fx_bank_fees_" . $bt2->id . "_" . $ap_transaction_fx->id}  = 1;
1431
  $::form->{"exchangerate_"      . $bt2->id . "_" . $ap_transaction_fx->id}  = "0,1"; # will be parsed
1432
  $::form->{"currency_id_"       . $bt2->id . "_" . $ap_transaction_fx->id}  = $currency_usd->id;
1433

  
1434

  
1435
  $ret = save_btcontroller_to_string();
1436

  
1437
  $ap_transaction_fx->load;
1438
  $bt2->load;
1439
  is($ap_transaction_fx->get_exchangerate , '2.00000' , "$testname: fx rate record");
1440
  is($ap_transaction_fx->paid             , $ap_transaction_fx->amount , "$testname: paid equals amount");
1441
  is($ap_transaction_fx->get_exchangerate , '2.00000' , "$testname: fx rate record");
1442
  is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt2->id ] )},
1443
       5, "$testname 5 acc_trans entries created");
1444

  
1445
  my $gl_fee_booking2 = SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt2->id, '!gl_id' => undef ] );
1446
  is(scalar @{ $gl_fee_booking2 }, 2, 'Two GL acc_trans bookings');
1447

  
1448
  my $gl = SL::DB::Manager::GLTransaction->get_first(where => [ id => $gl_fee_booking2->[0]->gl_id ]);
1449
  is (abs($gl->transactions->[0]->amount), 1142.55, 'fee2 abs amount correct');
1450

  
1451
  $fx_gain_transactions = SL::DB::Manager::AccTransaction->get_all(where =>
1452
                                [ trans_id => $ap_transaction_fx->id, chart_id => $fxgain_chart->id ],
1453
                                  sort_by => ('acc_trans_id'));
1454

  
1455
  is($fx_gain_transactions->[0]->amount,   '2671.18000' ,"$testname: fx gain 1 amount ok");
1456
  is($fx_gain_transactions->[1]->amount,   '2311.30000' ,"$testname: fx gain 2 amount ok");
1457

  
1458

  
1459
  is($ap_transaction_fx->paid   , $ap_transaction_fx->amount, "$testname: paid equals amount");
1460
  is($bt2->invoice_amount       , '-1264.20000'             , "$testname: bt invoice amount was assigned");
1461

  
1462
  # unlink bank transaction bt2 and reassign
1463
  $::form->{ids} = [ $bt2->id ];
1464
  my $bt_controller = SL::Controller::BankTransaction->new;
1465
  $bt_controller->action_unlink_bank_transaction('testcase' => 1);
1466

  
1467
  $bt2->load;
1468
  $ap_transaction_fx->load;
1469

  
1470
  # and check the cleared state of bt and the acc_transactions
1471
  is($bt2->cleared, '0' , "$testname: bt undo cleared");
1472
  is($bt2->invoice_amount, '0.00000' , "$testname: bt undo invoice amount");
1473

  
1474
  is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt2->id ] )},
1475
       0, "$testname 0 acc_trans entries there");
1476

  
1477
  $gl_fee_booking = SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt2->id, '!gl_id' => undef ] );
1478
  is(scalar @{ $gl_fee_booking }, 0, 'Zero GL acc_trans bookings');
1479

  
1480
  $fx_gain_transactions = SL::DB::Manager::AccTransaction->get_all(where =>
1481
                                [ trans_id => $ap_transaction_fx->id, chart_id => $fxgain_chart->id ],
1482
                                  sort_by => ('acc_trans_id'));
1483

  
1484
  is($fx_gain_transactions->[0]->amount, '2671.18000' , "$testname: fx gain amount ok");
1485
  is($ap_transaction_fx->paid          , '21369.43000', "$testname: ap transaction fx two payment was paid");
1486

  
1487

  
1488
}
1489

  
1490

  
1491
sub ap_transaction_fx_loss_fees {
1492

  
1493
  my $testname     = 'ap_transaction_fx_loss_fees';
1494
  my $usd_amount   = 166600;
1495
  my $fx_rate      = 1.5;
1496
  my $fx_rate_bank = 3;
1497
  my $eur_amount   = $usd_amount * $fx_rate;
1498
  my $ap_transaction_fx = create_ap_fx_transaction (invnumber   => 'ap transaction fx amount',
1499
                                                    payment_id  => $payment_terms->id,
1500
                                                    fx_rate     => $fx_rate,
1501
                                                    netamount   => $eur_amount,
1502
                                                    invoice_set => 1,
1503
                                                    buysell     => 'sell',
1504
                                                   );
1505
  # check exchangerate
1506
  is($ap_transaction_fx->currency->name   , 'USD'     , "$testname: USD currency");
1507
  is($ap_transaction_fx->get_exchangerate , '1.50000' , "$testname: fx rate record");
1508
  my $bt = create_bank_transaction(record        => $ap_transaction_fx,
1509
                                   bank_chart_id => $bank->id,
1510
                                   transdate     => $dt,
1511
                                   valutadate    => $dt,
1512
                                   amount        => 505797.60,
1513
                                   exchangerate  => $fx_rate_bank,
1514
                                  ) or die "Couldn't create bank_transaction";
1515

  
1516
  $::form->{invoice_ids} = {
1517
    $bt->id => [ $ap_transaction_fx->id ]
1518
  };
1519
  $::form->{"book_fx_bank_fees_" . $bt->id . "_" . $ap_transaction_fx->id}  = 1;
1520
  $::form->{"exchangerate_"      . $bt->id . "_" . $ap_transaction_fx->id}  = "3,00"; # will be parsed
1521
  $::form->{"currency_id_"       . $bt->id . "_" . $ap_transaction_fx->id}  = $currency_usd->id;
1522

  
1523

  
1524
  my $ret = save_btcontroller_to_string();
1525
  $ap_transaction_fx->load;
1526
  $bt->load;
1527
  is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )},
1528
       5, "$testname 2 acc_trans entries created");
1529

  
1530
  my $gl_fee_booking = SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id, '!gl_id' => undef ] );
1531
  is(scalar @{ $gl_fee_booking }, 2, 'One GL Booking');
1532

  
1533
  my $gl = SL::DB::Manager::GLTransaction->get_first(where => [ id => $gl_fee_booking->[0]->gl_id ]);
1534
  is (abs($gl->transactions->[0]->amount), '5997.6', 'fee abs amount correct');
1535

  
1536
  my $fx_loss_transactions = SL::DB::Manager::AccTransaction->get_all(where =>
1537
                                [ trans_id => $ap_transaction_fx->id, chart_id => $fxloss_chart->id ],
1538
                                  sort_by => ('acc_trans_id'));
1539

  
1540
  is($fx_loss_transactions->[0]->amount,   '-249900.00000', "$testname: fx loss amount ok");
1541

  
1542

  
1543
  is($ap_transaction_fx->paid   , $ap_transaction_fx->amount , "$testname: paid equals amount");
1544
  is($ap_transaction_fx->paid   , '249900.00000'             , "$testname: ap transaction was paid");
1545
  is($bt->invoice_amount        , '-505797.60000'            , "$testname: bt invoice amount was assigned");
1546

  
1547
}
1548
sub negative_ap_transaction_fx_gain_fees_error {
1549

  
1550
  my $testname     = 'negative_ap_transaction_fx_gain_fees';
1551
  my $usd_amount   = -890.90;
1552
  my $fx_rate      = 0.545;
1553
  my $fx_rate_bank = 0.499;
1554
  my $eur_amount   = $usd_amount * $fx_rate;
1555
  my $ap_transaction_fx = create_ap_fx_transaction (invnumber   => 'ap transaction fx amount',
1556
                                                    payment_id  => $payment_terms->id,
1557
                                                    fx_rate     => $fx_rate,
1558
                                                    netamount   => $eur_amount,
1559
                                                    invoice_set => 0,
1560
                                                    buysell     => 'sell',
1561
                                                   );
1562
  # check exchangerate
1563
  is($ap_transaction_fx->currency->name   , 'USD'       , "$testname: USD currency");
1564
  is($ap_transaction_fx->get_exchangerate , '0.54500'   , "$testname: fx rate record");
1565
  is($ap_transaction_fx->amount           , $eur_amount , "$testname: negative record amount");
1566
  is($ap_transaction_fx->invoice_type     , "purchase_credit_note" , "$testname: negative record amount");
1567

  
1568
  my $bt = create_bank_transaction(record        => $ap_transaction_fx,
1569
                                   bank_chart_id => $bank->id,
1570
                                   transdate     => $dt,
1571
                                   valutadate    => $dt,
1572
                                   amount        => 4440,
1573
                                   exchangerate  => $fx_rate_bank,
1574
                                  ) or die "Couldn't create bank_transaction";
1575

  
1576
  is($bt->amount           , '4440' , "$testname: positive bt amount");
1577
  $::form->{invoice_ids} = {
1578
    $bt->id => [ $ap_transaction_fx->id ]
1579
  };
1580
  $::form->{"book_fx_bank_fees_" . $bt->id . "_" . $ap_transaction_fx->id}  = 1;
1581
  $::form->{"exchangerate_"      . $bt->id . "_" . $ap_transaction_fx->id}  = "0,499"; # will be parsed
1582
  $::form->{"currency_id_"       . $bt->id . "_" . $ap_transaction_fx->id}  = $currency_usd->id;
1583

  
1584

  
1585
  save_btcontroller_to_string();
1586
  my @bt_errors = @{ $bt_controller->problems };
1587
  is(substr($bt_errors[0]->{message},0,69), 'Bank Fees can only be added for AP transactions or Sales Credit Notes', "$testname: Fehlermeldung ok");
1588
  $bt->load;
1589

  
1590
  is($bt->invoice_amount        , '0.00000'   , "$testname: bt invoice amount is not yet assigned");
1591

  
1592
}
1593
sub negative_ap_transaction_fx_gain_fees {
1594

  
1595
  my $testname     = 'negative_ap_transaction_fx_gain_fees';
1596
  my $usd_amount   = -890.90;
1597
  my $fx_rate      = 0.545;
1598
  my $fx_rate_bank = 0.499;
1599
  my $eur_amount   = $usd_amount * $fx_rate;
1600
  my $ap_transaction_fx = create_ap_fx_transaction (invnumber   => 'ap transaction fx amount',
1601
                                                    payment_id  => $payment_terms->id,
1602
                                                    fx_rate     => $fx_rate,
1603
                                                    netamount   => $eur_amount,
1604
                                                    invoice_set => 0,
1605
                                                    buysell     => 'sell',
1606
                                                   );
1607
  # check exchangerate
1608
  is($ap_transaction_fx->currency->name   , 'USD'       , "$testname: USD currency");
1609
  is($ap_transaction_fx->get_exchangerate , '0.54500'   , "$testname: fx rate record");
1610
  is($ap_transaction_fx->amount           , $eur_amount , "$testname: negative record amount");
1611
  is($ap_transaction_fx->invoice_type     , "purchase_credit_note" , "$testname: negative record amount");
1612

  
1613
  my $bt = create_bank_transaction(record        => $ap_transaction_fx,
1614
                                   bank_chart_id => $bank->id,
1615
                                   transdate     => $dt,
1616
                                   valutadate    => $dt,
1617
                                   amount        => 4440,
1618
                                   exchangerate  => $fx_rate_bank,
1619
                                  ) or die "Couldn't create bank_transaction";
1620

  
1621
  is($bt->amount           , '4440' , "$testname: positive bt amount");
1622
  $::form->{invoice_ids} = {
1623
    $bt->id => [ $ap_transaction_fx->id ]
1624
  };
1625
  $::form->{"exchangerate_"      . $bt->id . "_" . $ap_transaction_fx->id}  = "0,499"; # will be parsed
1626
  $::form->{"currency_id_"       . $bt->id . "_" . $ap_transaction_fx->id}  = $currency_usd->id;
1627

  
1628

  
1629
  save_btcontroller_to_string();
1630

  
1631
  $ap_transaction_fx->load;
1632
  $bt->load;
1633

  
1634
  is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )},
1635
       3, "$testname 5 acc_trans entries created");
1636

  
1637
  my $gl_fee_booking = SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id, '!gl_id' => undef ] );
1638
  is(scalar @{ $gl_fee_booking }, 0, 'Zero GL acc_trans bookings');
1639

  
1640
  my $fx_loss_transactions = SL::DB::Manager::AccTransaction->get_all(where =>
1641
                                [ trans_id => $ap_transaction_fx->id, chart_id => $fxloss_chart->id ],
1642
                                  sort_by => ('acc_trans_id'));
1643

  
1644
  is($fx_loss_transactions->[0]->amount,   '-40.98000', "$testname: fx loss amount ok");
1645

  
1646

  
1647
  is($ap_transaction_fx->paid   , '-485.54000' , "$testname: ap transaction skonto was paid");
1648
  is($bt->invoice_amount        , '444.56000'  , "$testname: bt invoice amount was assigned");
1649

  
1650
}
1651

  
1652
sub test_neg_sales_invoice_fx {
1653

  
1654
  my $testname = 'test_neg_sales_invoice_fx';
1655

  
1656
  my $part1 = new_part(   partnumber => 'Funkenhaube öhm')->save;
1657
  my $part2 = new_service(partnumber => 'Service-Pauschale Pasch!')->save;
1658

  
1659
  my $ex = SL::DB::Manager::Exchangerate->find_by(currency_id => $currency_usd->id,
1660
                                                  transdate => $dt)
1661
        ||              SL::DB::Exchangerate->new(currency_id => $currency_usd->id,
1662
                                                  transdate   => $dt);
1663
  $ex->update_attributes(buy => 1.772);
1664

  
1665
  my $neg_sales_inv = create_sales_invoice(
1666
    invnumber    => '20222201-USD',
1667
    customer     => $customer,
1668
    taxincluded  => 0,
1669
    currency_id  => $currency_usd->id,
1670
    transdate    => $dt,
1671
    invoiceitems => [ create_invoice_item(part => $part1, qty =>  3, sellprice => 70),
1672
                      create_invoice_item(part => $part2, qty => 10, sellprice => -50),
1673
                    ]
1674
  );
1675
  my $bt            = create_bank_transaction(record        => $neg_sales_inv,
1676
                                              amount        => $neg_sales_inv->amount,
1677
                                              bank_chart_id => $bank->id,
1678
                                              transdate     => $dt,
1679
                                              valutadate    => $dt,
1680
                                              exchangerate  => 2.11,
1681
                                             );
1682

  
1683
  is($neg_sales_inv->amount   , '-611.52000', "$testname: amount ok");
1684
  is($neg_sales_inv->netamount, '-513.88000', "$testname: netamount ok");
1685
  is($neg_sales_inv->currency->name   , 'USD'     , "$testname: USD currency");
1686
  is($neg_sales_inv->get_exchangerate , '1.77200' , "$testname: fx rate record");
1687
  is($bt->exchangerate                , '2.11' , "$testname: bt fx rate record");
1688

  
1689
  $::form->{invoice_ids} = {
1690
    $bt->id => [ $neg_sales_inv->id ]
1691
  };
1692

  
1693
  save_btcontroller_to_string();
1694

  
1695
  $neg_sales_inv->load;
1696
  $bt->load;
1697
  is($neg_sales_inv->paid     , '-611.52000', "$testname: paid ok");
1698
  is($bt->amount              , '-611.52000', "$testname: bt amount ok");
1699
  is($bt->invoice_amount      , '-611.52000', "$testname: bt invoice_amount ok");
1700

  
1701
  my $fx_loss_transactions = SL::DB::Manager::AccTransaction->get_all(where =>
1702
                                [ trans_id => $neg_sales_inv->id, chart_id => $fxloss_chart->id ],
1703
                                  sort_by => ('acc_trans_id'));
1704

  
1705
  is($fx_loss_transactions->[0]->amount,   '-116.64000', "$testname: fx loss amount ok");
1706

  
1707

  
1708

  
1709
}
1710
sub test_negative_ar_transaction_fx_loss {
1711
  my (%params) = @_;
1712
  my $netamount = $::form->round_amount($params{amount}, 2) || 100 * 1.772 * -1;
1713
  my $amount    = $::form->round_amount($netamount * 1.19,2);
1714
  # set exchangerate
1715
  my $ex = SL::DB::Manager::Exchangerate->find_by(currency_id => $currency_usd->id,
1716
                                                  transdate => $dt)
1717
        ||              SL::DB::Exchangerate->new(currency_id => $currency_usd->id,
1718
                                                  transdate   => $dt);
1719
  $ex->update_attributes(buy => 1.772);
1720

  
1721
  my $testname = 'test_negative_ar_transaction_fx_gain';
1722

  
1723
  my $invoice   = SL::DB::Invoice->new(
1724
      invoice      => 0,
1725
      invnumber    => $params{invnumber} || undef, # let it use its own invnumber
1726
      amount       => $amount,
1727
      netamount    => $netamount,
1728
      transdate    => $dt,
1729
      taxincluded  => $params{taxincluded } || 0,
1730
      customer_id  => $customer->id,
1731
      taxzone_id   => $customer->taxzone_id,
1732
      currency_id  => $currency_usd->id,
1733
      transactions => [],
1734
      payment_id   => $params{payment_id} || undef,
1735
      notes        => 'test_ar_transaction',
1736
  );
1737
  $invoice->add_ar_amount_row(
1738
    amount => $invoice->netamount,
1739
    chart  => $ar_amount_chart,
1740
    tax_id => $params{tax_id} || $tax->id,
1741
  );
1742

  
1743
  $invoice->create_ar_row(chart => $ar_chart);
1744
  $invoice->save;
1745

  
1746
  is($invoice->currency_id , $currency_usd->id , 'currency_id has been saved');
1747
  is($invoice->netamount   , '-177.2'          , 'fx ar amount has been converted');
1748
  is($invoice->amount      , '-210.87'         , 'fx ar amount has been converted');
1749
  is($invoice->taxincluded , 0                 , 'fx ar transaction doesn\'t have taxincluded');
1750

  
1751
  my $bt = create_bank_transaction(record        => $invoice,
1752
                                   bank_chart_id => $bank->id,
1753
                                   transdate     => $dt,
1754
                                   valutadate    => $dt,
1755
                                   amount        => (119 + 178.38) * -1,
1756
                                   exchangerate  => 2.499,
1757
                                  ) or die "Couldn't create bank_transaction";
1758

  
1759
  is($bt->amount           , '-297.38' , "$testname: negative bt amount");
1760
  $::form->{invoice_ids} = {
1761
    $bt->id => [ $invoice->id ]
1762
  };
1763
  $::form->{"exchangerate_"      . $bt->id . "_" . $invoice->id}  = "2,499"; # will be parsed
1764
  $::form->{"currency_id_"       . $bt->id . "_" . $invoice->id}  = $currency_usd->id;
1765

  
1766

  
1767
  save_btcontroller_to_string();
1768

  
1769
  $invoice->load;
1770
  $bt->load;
1771

  
1772
  is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )},
1773
       3, "$testname 3 acc_trans entries created");
1774

  
1775
  my $gl_fee_booking = SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id, '!gl_id' => undef ] );
1776
  is(scalar @{ $gl_fee_booking }, 0, 'Zero GL acc_trans bookings');
1777

  
1778
  my $fx_loss_transactions = SL::DB::Manager::AccTransaction->get_all(where =>
1779
                                [ trans_id => $invoice->id, chart_id => $fxloss_chart->id ],
1780
                                  sort_by => ('acc_trans_id'));
1781

  
1782
  is($fx_loss_transactions->[0]->amount,   '-86.51000', "$testname: fx gain amount ok");
1783

  
1784

  
1785
  is($invoice->paid       , '-210.87000' , "$testname: ar transaction was paid");
1786
  is($bt->invoice_amount  , '-297.38000' , "$testname: bt invoice amount was assigned");
1787
}
1788

  
1789

  
1790

  
1791
sub test_ar_transaction_fx_gain {
1792
  my (%params) = @_;
1793
  my $netamount = $::form->round_amount($params{amount}, 2) || 100 * 1.772;
1794
  my $amount    = $::form->round_amount($netamount * 1.19,2);
1795
  # set exchangerate
1796
  my $ex = SL::DB::Manager::Exchangerate->find_by(currency_id => $currency_usd->id,
1797
                                                  transdate => $dt)
1798
        ||              SL::DB::Exchangerate->new(currency_id => $currency_usd->id,
1799
                                                  transdate   => $dt);
1800
  $ex->update_attributes(buy => 1.772);
1801

  
1802
  my $testname = 'test_ar_transaction';
1803

  
1804
  my $invoice   = SL::DB::Invoice->new(
1805
      invoice      => 0,
1806
      invnumber    => $params{invnumber} || undef, # let it use its own invnumber
1807
      amount       => $amount,
1808
      netamount    => $netamount,
1809
      transdate    => $dt,
1810
      taxincluded  => $params{taxincluded } || 0,
1811
      customer_id  => $customer->id,
1812
      taxzone_id   => $customer->taxzone_id,
1813
      currency_id  => $currency_usd->id,
1814
      transactions => [],
1815
      payment_id   => $params{payment_id} || undef,
1816
      notes        => 'test_ar_transaction',
1817
  );
1818
  $invoice->add_ar_amount_row(
1819
    amount => $invoice->netamount,
1820
    chart  => $ar_amount_chart,
1821
    tax_id => $params{tax_id} || $tax->id,
1822
  );
1823

  
1824
  $invoice->create_ar_row(chart => $ar_chart);
1825
  $invoice->save;
1826

  
1827
  is($invoice->currency_id , $currency_usd->id , 'currency_id has been saved');
1828
  is($invoice->netamount   , '177.2'           , 'fx ar amount has been converted');
1829
  is($invoice->amount      , '210.87'          , 'fx ar amount has been converted');
1830
  is($invoice->taxincluded , 0                 , 'fx ar transaction doesn\'t have taxincluded');
1831

  
1832
  my $bt = create_bank_transaction(record        => $invoice,
1833
                                   bank_chart_id => $bank->id,
1834
                                   transdate     => $dt,
1835
                                   valutadate    => $dt,
1836
                                   amount        => 119 + 178.38,
1837
                                   exchangerate  => 2.499,
1838
                                  ) or die "Couldn't create bank_transaction";
1839

  
1840
  is($bt->amount           , '297.38' , "$testname: positive bt amount");
1841
  $::form->{invoice_ids} = {
1842
    $bt->id => [ $invoice->id ]
1843
  };
1844
  $::form->{"exchangerate_"      . $bt->id . "_" . $invoice->id}  = "2,499"; # will be parsed
1845
  $::form->{"currency_id_"       . $bt->id . "_" . $invoice->id}  = $currency_usd->id;
1846

  
1847

  
1848
  save_btcontroller_to_string();
1849

  
1850
  $invoice->load;
1851
  $bt->load;
1852

  
1853
  is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )},
1854
       3, "$testname 3 acc_trans entries created");
1855

  
1856
  my $gl_fee_booking = SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id, '!gl_id' => undef ] );
1857
  is(scalar @{ $gl_fee_booking }, 0, 'Zero GL acc_trans bookings');
1858

  
1859
  my $fx_gain_transactions = SL::DB::Manager::AccTransaction->get_all(where =>
1860
                                [ trans_id => $invoice->id, chart_id => $fxgain_chart->id ],
1861
                                  sort_by => ('acc_trans_id'));
1862

  
1863
  is($fx_gain_transactions->[0]->amount,   '86.51000', "$testname: fx gain amount ok");
1864

  
1865

  
1866
  is($invoice->paid       , '210.87000' , "$testname: ar transaction was paid");
1867
  is($bt->invoice_amount  , '297.38000' , "$testname: bt invoice amount was assigned");
1868
}
1869

  
1870
sub test_ar_transaction_fx_loss {
1871
  my (%params) = @_;
1872
  my $netamount = $::form->round_amount($params{amount}, 2) || 100 * 1.772;
1873
  my $amount    = $::form->round_amount($netamount * 1.19,2);
1874
  # set exchangerate
1875
  my $ex = SL::DB::Manager::Exchangerate->find_by(currency_id => $currency_usd->id,
1876
                                                  transdate => $dt)
1877
        ||              SL::DB::Exchangerate->new(currency_id => $currency_usd->id,
1878
                                                  transdate   => $dt);
1879
  $ex->update_attributes(buy => 1.772);
1880

  
1881
  my $testname = 'test_ar_transaction';
1882

  
1883
  my $invoice   = SL::DB::Invoice->new(
1884
      invoice      => 0,
1885
      invnumber    => $params{invnumber} || undef, # let it use its own invnumber
1886
      amount       => $amount,
1887
      netamount    => $netamount,
1888
      transdate    => $dt,
1889
      taxincluded  => $params{taxincluded } || 0,
1890
      customer_id  => $customer->id,
1891
      taxzone_id   => $customer->taxzone_id,
1892
      currency_id  => $currency_usd->id,
1893
      transactions => [],
1894
      payment_id   => $params{payment_id} || undef,
1895
      notes        => 'test_ar_transaction',
1896
  );
1897
  $invoice->add_ar_amount_row(
1898
    amount => $invoice->netamount,
1899
    chart  => $ar_amount_chart,
1900
    tax_id => $params{tax_id} || $tax->id,
1901
  );
1902

  
1903
  $invoice->create_ar_row(chart => $ar_chart);
1904
  $invoice->save;
1905

  
1906
  is($invoice->currency_id , $currency_usd->id , 'currency_id has been saved');
1907
  is($invoice->netamount   , '177.2'           , 'fx ar amount has been converted');
1908
  is($invoice->amount      , '210.87'          , 'fx ar amount has been converted');
1909
  is($invoice->taxincluded , 0                 , 'fx ar transaction doesn\'t have taxincluded');
1910

  
1911
  my $bt = create_bank_transaction(record        => $invoice,
1912
                                   bank_chart_id => $bank->id,
1913
                                   transdate     => $dt,
1914
                                   valutadate    => $dt,
1915
                                   amount        => 210.87,
1916
                                   exchangerate  => 1.499,
1917
                                  ) or die "Couldn't create bank_transaction";
1918

  
1919
  is($bt->amount           , '210.87' , "$testname: positive bt amount");
1920
  $::form->{invoice_ids} = {
1921
    $bt->id => [ $invoice->id ]
1922
  };
1923
  $::form->{"exchangerate_"      . $bt->id . "_" . $invoice->id}  = "1,499"; # will be parsed
1924
  $::form->{"currency_id_"       . $bt->id . "_" . $invoice->id}  = $currency_usd->id;
1925

  
1926

  
1927
  save_btcontroller_to_string();
1928

  
1929
  $invoice->load;
1930
  $bt->load;
1931

  
1932
  is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )},
1933
       3, "$testname 3 acc_trans entries created");
1934

  
1935
  my $gl_fee_booking = SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id, '!gl_id' => undef ] );
1936
  is(scalar @{ $gl_fee_booking }, 0, 'Zero GL acc_trans bookings');
1937

  
1938
  my $fx_loss_transactions = SL::DB::Manager::AccTransaction->get_all(where =>
1939
                                [ trans_id => $invoice->id, chart_id => $fxloss_chart->id ],
1940
                                  sort_by => ('acc_trans_id'));
1941

  
1942
  is($fx_loss_transactions->[0]->amount,   '-32.49000', "$testname: fx loss amount ok");
1943

  
1944

  
1945
  is($invoice->paid       , '210.87000' , "$testname: ar transaction was paid");
1946
  is($bt->invoice_amount  , '178.38000' , "$testname: bt invoice amount was assigned");
1947
}
1948

  
1949
sub create_ap_fx_transaction {
1950
  my (%params) = @_;
1951

  
1952
  my $netamount     = $params{netamount};
1953
  my $amount        = $params{netamount};
1954
  my $invoice_set   = $params{invoice};
1955
  my $fx_rate       = $params{fx_rate};
1956
  my $buysell       = $params{buysell};
1957

  
1958
  my $ex = SL::DB::Manager::Exchangerate->find_by(currency_id => $currency_usd->id,
1959
                                                  transdate => $dt)
1960
        ||              SL::DB::Exchangerate->new(currency_id => $currency_usd->id,
1961
                                                  transdate   => $dt);
1962
  $ex->update_attributes($buysell => $fx_rate);
1963

  
1964
  my $invoice   = SL::DB::PurchaseInvoice->new(
1965
    invoice      => $invoice_set,
1966
    invnumber    => $params{invnumber} || '27ab',
1967
    amount       => $amount,
1968
    netamount    => $netamount,
1969
    transdate    => $dt,
1970
    taxincluded  => 0,
1971
    vendor_id    => $vendor->id,
1972
    taxzone_id   => $vendor->taxzone_id,
1973
    currency_id  => $currency_usd->id,
1974
    transactions => [],
1975
    notes        => 'ap_transaction_fx',
1976
  );
1977
  $invoice->add_ap_amount_row(
1978
    amount     => $netamount,
1979
    chart      => $ap_amount_chart,
1980
    tax_id     => 0,
1981
  );
1982

  
1983
  $invoice->create_ap_row(chart => $ap_chart);
1984
  $invoice->save;
1985
  return $invoice;
1986
}
1274 1987
1;

Auch abrufbar als: Unified diff