Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision a565eee3

Von G. Richardson vor etwa 5 Jahren hinzugefügt

  • ID a565eee30f2f2485c561f8996f3dc634e0a5d49f
  • Vorgänger f2f0fb50
  • Nachfolger b30eacc6

Alten YearEndTransactions Test entfernt

Unterschiede anzeigen:

t/bank/cb_ob_transactions.t
1
use Test::More;
2

  
3
use strict;
4

  
5
use lib 't';
6
use utf8;
7

  
8
use Carp;
9
use Support::TestSetup;
10
use Test::Exception;
11
use List::Util qw(sum);
12

  
13
use SL::DB::Buchungsgruppe;
14
use SL::DB::Currency;
15
use SL::DB::Exchangerate;
16
use SL::DB::Customer;
17
use SL::DB::Vendor;
18
use SL::DB::Employee;
19
use SL::DB::Invoice;
20
use SL::DB::Part;
21
use SL::DB::Unit;
22
use SL::DB::TaxZone;
23
use SL::DB::BankAccount;
24
use SL::DB::PaymentTerm;
25
use SL::DB::PurchaseInvoice;
26
use SL::DB::BankTransaction;
27
use SL::DB::AccTransaction;
28
use SL::Controller::YearEndTransactions;
29
use Data::Dumper;
30

  
31
my ($customer, $vendor, $currency_id, @parts, $unit, $employee, $tax, $tax7, $tax_9, $taxzone, $payment_terms, $bank_account);
32
my ($transdate1, $transdate2, $currency);
33
my ($ar_chart,$bank,$ar_amount_chart, $ap_chart, $ap_amount_chart, $saldo_chart);
34
my ($ar_transaction, $ap_transaction);
35

  
36
sub clear_up {
37

  
38
  SL::DB::Manager::BankTransaction->delete_all(all => 1);
39
  SL::DB::Manager::InvoiceItem->delete_all(all => 1);
40
  SL::DB::Manager::InvoiceItem->delete_all(all => 1);
41
  SL::DB::Manager::Invoice->delete_all(all => 1);
42
  SL::DB::Manager::PurchaseInvoice->delete_all(all => 1);
43
  SL::DB::Manager::Part->delete_all(all => 1);
44
  SL::DB::Manager::Customer->delete_all(all => 1);
45
  SL::DB::Manager::Vendor->delete_all(all => 1);
46
  SL::DB::Manager::BankAccount->delete_all(all => 1);
47
  SL::DB::Manager::AccTransaction->delete_all(all => 1);
48
  SL::DB::Manager::GLTransaction->delete_all(all => 1);
49
  SL::DB::Manager::PaymentTerm->delete_all(all => 1);
50
  SL::DB::Manager::Currency->delete_all(where => [ name => 'CUR' ]);
51
};
52

  
53

  
54
# starting test:
55
Support::TestSetup::login();
56

  
57
reset_state(); # initialise customers/vendors/bank/currency/...
58

  
59
test1();
60

  
61
# remove all created data at end of test
62
#clear_up();
63

  
64
done_testing();
65

  
66
###### functions for setting up data
67

  
68
sub reset_state {
69
  my %params = @_;
70

  
71
  $params{$_} ||= {} for qw(unit customer part tax vendor);
72

  
73
  clear_up();
74

  
75
  $transdate1 = DateTime->today;
76
  $transdate2 = DateTime->today->add(days => 5);
77

  
78
  $employee        = SL::DB::Manager::Employee->current                                          || croak "No employee";
79
  $tax             = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax";
80
  $tax7            = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07)                    || croak "No tax for 7\%";
81
  $taxzone         = SL::DB::Manager::TaxZone->find_by( description => 'Inland')                 || croak "No taxzone";
82
  $tax_9           = SL::DB::Manager::Tax->find_by(taxkey => 9, rate => 0.19, %{ $params{tax} }) || croak "No tax";
83

  
84
  $currency_id     = $::instance_conf->get_currency_id;
85

  
86
  $bank_account     =  SL::DB::BankAccount->new(
87
    account_number  => '123',
88
    bank_code       => '123',
89
    iban            => '123',
90
    bic             => '123',
91
    bank            => '123',
92
    chart_id        => SL::DB::Manager::Chart->find_by(description => 'Bank')->id,
93
    name            => SL::DB::Manager::Chart->find_by(description => 'Bank')->description,
94
  )->save;
95

  
96
  $customer     = SL::DB::Customer->new(
97
    name                      => 'Test Customer',
98
    currency_id               => $currency_id,
99
    taxzone_id                => $taxzone->id,
100
    iban                      => 'DE12500105170648489890',
101
    bic                       => 'TESTBIC',
102
    account_number            => '648489890',
103
    mandate_date_of_signature => $transdate1,
104
    mandator_id               => 'foobar',
105
    bank                      => 'Geizkasse',
106
    depositor                 => 'Test Customer',
107
    %{ $params{customer} }
108
  )->save;
109

  
110
  $payment_terms     =  SL::DB::PaymentTerm->new(
111
    description      => 'payment',
112
    description_long => 'payment',
113
    terms_netto      => '30',
114
    terms_skonto     => '5',
115
    percent_skonto   => '0.05',
116
    auto_calculation => 1,
117
  )->save;
118

  
119
  $vendor       = SL::DB::Vendor->new(
120
    name        => 'Test Vendor',
121
    currency_id => $currency_id,
122
    taxzone_id  => $taxzone->id,
123
    payment_id  => $payment_terms->id,
124
    iban                      => 'DE12500105170648489890',
125
    bic                       => 'TESTBIC',
126
    account_number            => '648489890',
127
    bank                      => 'Geizkasse',
128
    depositor                 => 'Test Vendor',
129
    %{ $params{vendor} }
130
  )->save;
131

  
132
  $ar_chart        = SL::DB::Manager::Chart->find_by( accno => '1400' ); # Forderungen
133
  $ap_chart        = SL::DB::Manager::Chart->find_by( accno => '1600' ); # Verbindlichkeiten
134
  $bank            = SL::DB::Manager::Chart->find_by( accno => '1200' ); # Bank
135
  $ar_amount_chart = SL::DB::Manager::Chart->find_by( accno => '8400' ); # Erlöse
136
  $ap_amount_chart = SL::DB::Manager::Chart->find_by( accno => '3400' ); # Wareneingang 19%
137
  $saldo_chart     = SL::DB::Manager::Chart->find_by( accno => '9000' ); # Saldenvorträge
138

  
139
}
140

  
141
sub test_ar_transaction {
142
  my (%params) = @_;
143
  my $netamount = 100;
144
  my $amount    = $params{amount} || $::form->round_amount(100 * 1.19,2);
145
  my $invoice   = SL::DB::Invoice->new(
146
      invoice      => 0,
147
      invnumber    => $params{invnumber} || undef, # let it use its own invnumber
148
      amount       => $amount,
149
      netamount    => $netamount,
150
      transdate    => $transdate1,
151
      taxincluded  => 0,
152
      customer_id  => $customer->id,
153
      taxzone_id   => $customer->taxzone_id,
154
      currency_id  => $currency_id,
155
      transactions => [],
156
      payment_id   => $params{payment_id} || undef,
157
      notes        => 'test_ar_transaction',
158
  );
159
  $invoice->add_ar_amount_row(
160
    amount => $invoice->netamount,
161
    chart  => $ar_amount_chart,
162
    tax_id => $tax->id,
163
  );
164

  
165
  $invoice->create_ar_row(chart => $ar_chart);
166
  $invoice->save;
167

  
168
  is($invoice->currency_id , $currency_id , 'currency_id has been saved');
169
  is($invoice->netamount   , 100          , 'ar amount has been converted');
170
  is($invoice->amount      , 119          , 'ar amount has been converted');
171
  is($invoice->taxincluded , 0            , 'ar transaction doesn\'t have taxincluded');
172

  
173
  is(SL::DB::Manager::AccTransaction->find_by(chart_id => $ar_amount_chart->id , trans_id => $invoice->id)->amount , '100.00000'  , $ar_amount_chart->accno . ': has been converted for currency');
174
  is(SL::DB::Manager::AccTransaction->find_by(chart_id => $ar_chart->id        , trans_id => $invoice->id)->amount , '-119.00000' , $ar_chart->accno . ': has been converted for currency');
175

  
176
  return $invoice;
177
};
178

  
179
sub test_ap_transaction {
180
  my (%params) = @_;
181
  my $netamount = 100;
182
  my $amount    = $::form->round_amount($netamount * 1.19,2);
183
  my $invoice   = SL::DB::PurchaseInvoice->new(
184
      invoice      => 0,
185
      invnumber    => $params{invnumber} || 'test_ap_transaction',
186
      amount       => $amount,
187
      netamount    => $netamount,
188
      transdate    => $transdate1,
189
      taxincluded  => 0,
190
      vendor_id    => $vendor->id,
191
      taxzone_id   => $vendor->taxzone_id,
192
      currency_id  => $currency_id,
193
      transactions => [],
194
      notes        => 'test_ap_transaction',
195
  );
196
  $invoice->add_ap_amount_row(
197
    amount     => $invoice->netamount,
198
    chart      => $ap_amount_chart,
199
    tax_id     => $tax_9->id,
200
  );
201

  
202
  $invoice->create_ap_row(chart => $ap_chart);
203
  $invoice->save;
204

  
205
  is($invoice->currency_id , $currency_id , 'currency_id has been saved');
206
  is($invoice->netamount   , 100          , 'ap amount has been converted');
207
  is($invoice->amount      , 119          , 'ap amount has been converted');
208
  is($invoice->taxincluded , 0            , 'ap transaction doesn\'t have taxincluded');
209

  
210
  is(SL::DB::Manager::AccTransaction->find_by(chart_id => $ap_amount_chart->id , trans_id => $invoice->id)->amount , '-100.00000' , $ap_amount_chart->accno . ': has been converted for currency');
211
  is(SL::DB::Manager::AccTransaction->find_by(chart_id => $ap_chart->id        , trans_id => $invoice->id)->amount , '119.00000'  , $ap_chart->accno . ': has been converted for currency');
212

  
213
  return $invoice;
214
};
215

  
216
###### test cases
217

  
218
sub test1 {
219

  
220
  my $testname = 'test1';
221

  
222
  $ar_transaction = test_ar_transaction(invnumber => 'salesinv1');
223
  $ap_transaction = test_ap_transaction(invnumber => 'purchaseinv1');
224
  my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv_2');
225

  
226
  my $yt_controller = SL::Controller::YearEndTransactions->new;
227
  my $report     = SL::ReportGenerator->new(\%::myconfig, $::form);
228

  
229
  $::form->{"ob_date"} = DateTime->today->truncate(to => 'year')->add(years => 1)->to_kivitendo;
230
  $::form->{"cb_date"} = DateTime->today->truncate(to => 'year')->add(years => 1)->add(days => -1)->to_kivitendo;
231
  #print "ob_date=".$::form->{"ob_date"}." cb_date=".$::form->{"cb_date"}."\n";
232
  $::form->{"cb_reference"} = 'SB-Buchung';
233
  $::form->{"ob_reference"} = 'EB-Buchung';
234
  $::form->{"cb_description"} = 'SB-Buchung Beschreibung';
235
  $::form->{"ob_description"} = 'EB-Buchung Beschreibung';
236
  $::form->{"cbob_chart"} = $saldo_chart->id;
237

  
238
  $yt_controller->prepare_report($report);
239

  
240
  ## check balance of charts
241

  
242
  my $idx = 1;
243
  foreach my $chart (@{ $yt_controller->charts }) {
244
    my $balance = $yt_controller->get_balance($chart);
245
    if ( $balance != 0 ) {
246
      #print "chart_id=".$chart->id."balance=".$balance."\n";
247
      is($balance , '-238.00000' , $chart->accno.' has right balance') if $chart->accno eq '1400';
248
      is($balance ,  '-19.00000' , $chart->accno.' has right balance') if $chart->accno eq '1576';
249
      is($balance ,  '119.00000' , $chart->accno.' has right balance') if $chart->accno eq '1600';
250
      is($balance ,   '38.00000' , $chart->accno.' has right balance') if $chart->accno eq '1776';
251
      is($balance , '-100.00000' , $chart->accno.' has right balance') if $chart->accno eq '3400';
252
      is($balance ,  '200.00000' , $chart->accno.' has right balance') if $chart->accno eq '8400';
253
      $::form->{"multi_id_${idx}"} = $chart->id;
254
      $idx++ ;
255
    }
256
  }
257
  $::form->{"rowcount"} = $idx-1;
258
  #print "rowcount=". $::form->{"rowcount"}."\n";
259
  $::form->{"login"}="unittests";
260

  
261
  $yt_controller->make_booking;
262

  
263
  ## no check cb ob booking :
264

  
265
  my $sum_cb_p = 0;
266
  my $sum_cb_m = 0;
267
  foreach my $acc ( @{ SL::DB::Manager::AccTransaction->get_all(where => [ chart_id => $saldo_chart->id, cb_transaction => 't' ]) }) {
268
    #print "cb amount=".$acc->amount."\n";
269
    $sum_cb_p +=  $acc->amount if $acc->amount > 0;
270
    $sum_cb_m += -$acc->amount if $acc->amount < 0;
271
  }
272
  #print "chart_id=".$saldo_chart->id." sum_cb_p=".$sum_cb_p." sum_cb_m=".$sum_cb_m."\n";
273
  is($sum_cb_p ,  '357' , 'chart '.$saldo_chart->accno.' has right positive close saldo');
274
  is($sum_cb_m ,  '357' , 'chart '.$saldo_chart->accno.' has right negative close saldo');
275
  my $sum_ob_p = 0;
276
  my $sum_ob_m = 0;
277
  foreach my $acc ( @{ SL::DB::Manager::AccTransaction->get_all(where => [ chart_id => $saldo_chart->id, ob_transaction => 't' ]) }) {
278
    #print "ob amount=".$acc->amount."\n";
279
    $sum_ob_p +=  $acc->amount if $acc->amount > 0;
280
    $sum_ob_m += -$acc->amount if $acc->amount < 0;
281
  }
282
  #print "chart_id=".$saldo_chart->id." sum_ob_p=".$sum_ob_p." sum_ob_m=".$sum_ob_m."\n";
283
  is($sum_ob_p ,  '357' , 'chart '.$saldo_chart->accno.' has right positive open saldo');
284
  is($sum_ob_m ,  '357' , 'chart '.$saldo_chart->accno.' has right negative open saldo');
285
}
286

  
287

  
288

  
289
1;

Auch abrufbar als: Unified diff