Revision a7502cfd
Von Kivitendo Admin vor etwa 1 Jahr hinzugefügt
t/model/records.t | ||
---|---|---|
1 |
use Test::More; |
|
2 |
|
|
3 |
use strict; |
|
4 |
|
|
5 |
use lib 't'; |
|
6 |
use utf8; |
|
7 |
|
|
8 |
use Carp; |
|
9 |
use Data::Dumper; |
|
10 |
use Support::TestSetup; |
|
11 |
use Test::Exception; |
|
12 |
|
|
13 |
use SL::DB::PaymentTerm; |
|
14 |
use SL::DB::DeliveryTerm; |
|
15 |
use SL::DB::Employee; |
|
16 |
use SL::DB::ReclamationReason; |
|
17 |
use SL::Model::Record; |
|
18 |
|
|
19 |
use SL::Dev::ALL qw(:ALL); |
|
20 |
|
|
21 |
my ($customer, $vendor, $employee, $payment_term, $delivery_term, @parts, $reclamation_reason); |
|
22 |
my ($dbh); |
|
23 |
|
|
24 |
my ($sales_quotation1, $sales_order1, $sales_invoice1, $sales_delivery_order1, $sales_reclamation1); |
|
25 |
my ($purchase_quotation1, $purchase_order1, $purchase_invoice1, $purchase_delivery_order1, $purchase_reclamation1); |
|
26 |
|
|
27 |
sub clear_up { |
|
28 |
foreach (qw(InvoiceItem Invoice |
|
29 |
DeliveryOrderItem DeliveryOrder |
|
30 |
OrderItem Order OrderVersion |
|
31 |
Reclamation ReclamationItem ReclamationReason |
|
32 |
Part Customer Vendor PaymentTerm DeliveryTerm) |
|
33 |
) { |
|
34 |
"SL::DB::Manager::${_}"->delete_all(all => 1); |
|
35 |
} |
|
36 |
SL::DB::Manager::History->delete_all(all => 1); |
|
37 |
SL::DB::Manager::Employee->delete_all(where => [ login => 'testuser' ]); |
|
38 |
}; |
|
39 |
|
|
40 |
sub reset_basic_sales_records { |
|
41 |
$dbh->do("UPDATE defaults SET sonumber = 'ord-00', sqnumber = 'quo-00', sdonumber = 'do-00'"); |
|
42 |
|
|
43 |
$sales_quotation1 = create_sales_quotation( |
|
44 |
save => 1, |
|
45 |
customer => $customer, |
|
46 |
taxincluded => 0, |
|
47 |
orderitems => [ create_order_item(part => $parts[0], qty => 3, sellprice => 70), |
|
48 |
create_order_item(part => $parts[1], qty => 10, sellprice => 50), |
|
49 |
] |
|
50 |
); |
|
51 |
|
|
52 |
$sales_order1 = create_sales_order( |
|
53 |
save => 1, |
|
54 |
customer => $customer, |
|
55 |
taxincluded => 0, |
|
56 |
orderitems => [ create_order_item(part => $parts[0], qty => 3, sellprice => 70), |
|
57 |
create_order_item(part => $parts[1], qty => 10, sellprice => 50), |
|
58 |
] |
|
59 |
); |
|
60 |
|
|
61 |
$sales_delivery_order1 = create_sales_delivery_order( |
|
62 |
save => 1, |
|
63 |
customer => $customer, |
|
64 |
taxincluded => 0, |
|
65 |
orderitems => [ create_delivery_order_item(part => $parts[0], qty => 3, sellprice => 70), |
|
66 |
create_delivery_order_item(part => $parts[1], qty => 10, sellprice => 50), |
|
67 |
] |
|
68 |
); |
|
69 |
|
|
70 |
$sales_reclamation1 = create_sales_reclamation( |
|
71 |
save => 1, |
|
72 |
# customer => $customer, |
|
73 |
employee => $employee, |
|
74 |
taxincluded => 0, |
|
75 |
reclamation_items => [ create_reclamation_item(part => $parts[0], qty => 3, sellprice => 70, reason => $reclamation_reason), |
|
76 |
create_reclamation_item(part => $parts[1], qty => 10, sellprice => 50, reason => $reclamation_reason), |
|
77 |
] |
|
78 |
); |
|
79 |
|
|
80 |
$sales_invoice1 = create_sales_invoice( |
|
81 |
save => 1, |
|
82 |
customer => $customer, |
|
83 |
taxincluded => 0, |
|
84 |
invoiceitems => [ create_invoice_item(part => $parts[0], qty => 3, sellprice => 70), |
|
85 |
create_invoice_item(part => $parts[1], qty => 10, sellprice => 50), |
|
86 |
] |
|
87 |
); |
|
88 |
} |
|
89 |
|
|
90 |
sub reset_basic_purchase_records { |
|
91 |
$dbh->do("UPDATE defaults SET rfqnumber = 'rfq-00', ponumber = 'po-00', pdonumber = 'pdo-00'"); |
|
92 |
|
|
93 |
$purchase_quotation1 = create_purchase_quotation ( |
|
94 |
save => 1, |
|
95 |
vendor => $vendor, |
|
96 |
taxincluded => 0, |
|
97 |
orderitems => [ create_order_item(part => $parts[0], qty => 3, sellprice => 70), |
|
98 |
create_order_item(part => $parts[1], qty => 10, sellprice => 50), |
|
99 |
] |
|
100 |
); |
|
101 |
|
|
102 |
$purchase_order1 = create_purchase_order ( |
|
103 |
save => 1, |
|
104 |
vendor => $vendor, |
|
105 |
taxincluded => 0, |
|
106 |
orderitems => [ create_order_item(part => $parts[0], qty => 3, sellprice => 70), |
|
107 |
create_order_item(part => $parts[1], qty => 10, sellprice => 50), |
|
108 |
] |
|
109 |
); |
|
110 |
|
|
111 |
$purchase_delivery_order1 = create_purchase_delivery_order( |
|
112 |
save => 1, |
|
113 |
vendor => $vendor, |
|
114 |
taxincluded => 0, |
|
115 |
orderitems => [ create_delivery_order_item(part => $parts[0], qty => 3, sellprice => 70), |
|
116 |
create_delivery_order_item(part => $parts[1], qty => 10, sellprice => 50), |
|
117 |
] |
|
118 |
); |
|
119 |
|
|
120 |
$purchase_reclamation1 = create_purchase_reclamation( |
|
121 |
save => 1, |
|
122 |
# vendor => $vendor, |
|
123 |
employee => $employee, |
|
124 |
taxincluded => 0, |
|
125 |
reclamation_items => [ create_reclamation_item(part => $parts[0], qty => 3, sellprice => 70, reason => $reclamation_reason), |
|
126 |
create_reclamation_item(part => $parts[1], qty => 10, sellprice => 50, reason => $reclamation_reason), |
|
127 |
] |
|
128 |
); |
|
129 |
} |
|
130 |
|
|
131 |
sub reset_state { |
|
132 |
my %params = @_; |
|
133 |
|
|
134 |
clear_up(); |
|
135 |
|
|
136 |
$customer = new_customer()->save; |
|
137 |
$vendor = new_vendor()->save; |
|
138 |
|
|
139 |
$employee = SL::DB::Employee->new( |
|
140 |
'login' => 'testuser', |
|
141 |
'name' => 'Test User', |
|
142 |
)->save; |
|
143 |
|
|
144 |
$payment_term = create_payment_terms( |
|
145 |
'description' => '14Tage 2%Skonto, 30Tage netto', |
|
146 |
'description_long' => "Innerhalb von 14 Tagen abzüglich 2 % Skonto, innerhalb von 30 Tagen rein netto.|Bei einer Zahlung bis zum <%skonto_date%> gewähren wir 2 % Skonto (EUR <%skonto_amount%>) entspricht EUR <%total_wo_skonto%>.Bei einer Zahlung bis zum <%netto_date%> ist der fällige Betrag in Höhe von <%total%> <%currency%> zu überweisen.", |
|
147 |
'percent_skonto' => '0.02', |
|
148 |
'terms_netto' => 30, |
|
149 |
'terms_skonto' => 14 |
|
150 |
); |
|
151 |
|
|
152 |
$delivery_term = SL::DB::DeliveryTerm->new( |
|
153 |
'description' => 'Test Delivey Term', |
|
154 |
'description_long' => 'Test Delivey Term Test Delivey Term', |
|
155 |
)->save; |
|
156 |
|
|
157 |
# some parts/services |
|
158 |
@parts = (); |
|
159 |
push @parts, new_part( |
|
160 |
partnumber => 'a', |
|
161 |
)->save; |
|
162 |
push @parts, new_part( |
|
163 |
partnumber => 'b', |
|
164 |
)->save; |
|
165 |
|
|
166 |
$reclamation_reason = SL::DB::ReclamationReason->new( |
|
167 |
name => "test_reason", |
|
168 |
description => "", |
|
169 |
position => 1, |
|
170 |
); |
|
171 |
|
|
172 |
} |
|
173 |
|
|
174 |
Support::TestSetup::login(); |
|
175 |
$dbh = SL::DB->client->dbh; |
|
176 |
|
|
177 |
clear_up(); |
|
178 |
reset_state(); |
|
179 |
reset_basic_sales_records(); |
|
180 |
reset_basic_purchase_records(); |
|
181 |
|
|
182 |
note "testing deletions"; |
|
183 |
|
|
184 |
is(SL::DB::Manager::Order->get_all_count(where => [ quotation => 1 ]), 2, 'number of quotations before delete ok'); |
|
185 |
is(SL::DB::Manager::Order->get_all_count(where => [ quotation => 0 ]), 2, 'number of orders before delete ok'); |
|
186 |
is(SL::DB::Manager::DeliveryOrder->get_all_count(), 2, 'number of delivery orders before delete ok'); |
|
187 |
is(SL::DB::Manager::Reclamation->get_all_count(), 2, 'number of reclamations before delete ok'); |
|
188 |
is(SL::DB::Manager::Invoice->get_all_count(), 1, 'number of invoices before delete ok'); # no purchase_invoice was created |
|
189 |
|
|
190 |
SL::Model::Record->delete($sales_quotation1); |
|
191 |
SL::Model::Record->delete($sales_order1); |
|
192 |
SL::Model::Record->delete($sales_reclamation1); |
|
193 |
SL::Model::Record->delete($sales_invoice1); |
|
194 |
|
|
195 |
SL::Model::Record->delete($purchase_quotation1); |
|
196 |
SL::Model::Record->delete($purchase_order1); |
|
197 |
SL::Model::Record->delete($purchase_reclamation1); |
|
198 |
|
|
199 |
is(SL::DB::Manager::Order->get_all_count(where => [ quotation => 1 ]), 0, 'number of quotations after delete ok'); |
|
200 |
is(SL::DB::Manager::Order->get_all_count(where => [ quotation => 0 ]), 0, 'number of orders after delete ok'); |
|
201 |
is(SL::DB::Manager::Invoice->get_all_count(), 0, 'number of invoices after delete ok'); |
|
202 |
is(SL::DB::Manager::Reclamation->get_all_count(), 0, 'number of orders after delete ok'); |
|
203 |
|
|
204 |
note "testing workflows"; |
|
205 |
reset_state(); |
|
206 |
reset_basic_sales_records(); |
|
207 |
reset_basic_purchase_records(); |
|
208 |
|
|
209 |
note "testing subversion of order"; |
|
210 |
# make current version a final version, currently this is handled via frontend/controller |
|
211 |
is($sales_order1->ordnumber, "ord-01", "ordnumber before increment_subversion ok"); |
|
212 |
SL::DB::OrderVersion->new(oe_id => $sales_order1->id, version => 1, final_version => 1)->save; |
|
213 |
SL::Model::Record->increment_subversion($sales_order1); |
|
214 |
is($sales_order1->ordnumber, "ord-01-2", "ordnumber after increment_subversion ok"); |
|
215 |
is(SL::DB::Manager::Order->get_all_count(where => [quotation => 0]), 2, 'number of orders after incremented subversion ok'); |
|
216 |
|
|
217 |
|
|
218 |
note "testing new_from_workflow for quotation"; |
|
219 |
foreach my $target_record_type ( qw(sales_order sales_delivery_order) ) { |
|
220 |
# TODO: invoice |
|
221 |
note " testing from quotation -> $target_record_type"; |
|
222 |
my $new_record = SL::Model::Record->new_from_workflow($sales_quotation1, $target_record_type); |
|
223 |
|
|
224 |
$new_record->save->load; |
|
225 |
cmp_ok($new_record->netamount, '==', 710, "converted $target_record_type netamount ok") if $new_record->can('netamount'); |
|
226 |
test_record_links($new_record, "converted $target_record_type"); |
|
227 |
}; |
|
228 |
|
|
229 |
note "testing new_from_workflow for order"; |
|
230 |
foreach my $target_record_type ( qw(sales_delivery_order sales_reclamation) ) { |
|
231 |
# TODO: invoice |
|
232 |
note " testing from quotation -> $target_record_type"; |
|
233 |
my $new_record = SL::Model::Record->new_from_workflow($sales_order1, $target_record_type); |
|
234 |
if ( 'SL::DB::Reclamation' eq ref($new_record) ) { |
|
235 |
map { $_->reason($reclamation_reason) } @{ $new_record->items }; |
|
236 |
}; |
|
237 |
$new_record->save->load; |
|
238 |
cmp_ok($new_record->netamount, '==', 710, "converted $target_record_type netamount ok") if $new_record->can('netamount'); |
|
239 |
test_record_links($new_record, "converted $target_record_type"); |
|
240 |
}; |
|
241 |
|
|
242 |
# TODO: { |
|
243 |
# local $TODO = "currently this test fails"; |
|
244 |
# my $invoice_history = SL::DB::Manager::History->find_by(trans_id => $sales_invoice1->id, what_done => 'invoice', addition => 'DELETED'); |
|
245 |
# # is($invoice_history->snumbers , 'foo', "history snumbers of invoice ok"); |
|
246 |
# } |
|
247 |
|
|
248 |
#### |
|
249 |
clear_up(); |
|
250 |
done_testing; |
|
251 |
|
|
252 |
sub test_record_links { |
|
253 |
my $record = shift; |
|
254 |
my $text = shift; |
|
255 |
|
|
256 |
is(@{ $record->linked_records }, 1, "1 record link for $text created ok"); # just check if one exists, not if it is actually correct |
|
257 |
my $number_of_item_record_links; |
|
258 |
foreach my $item ( @{ $record->items } ) { |
|
259 |
$number_of_item_record_links += scalar @{ $item->linked_records }; |
|
260 |
}; |
|
261 |
is($number_of_item_record_links, 2, "2 record links for $text items created ok"); # just check if they exist, not if they are actually correct |
|
262 |
} |
|
263 |
|
|
264 |
1; |
|
265 |
|
|
266 |
# set emacs to perl mode |
|
267 |
# Local Variables: |
|
268 |
# mode: perl |
|
269 |
# End: |
Auch abrufbar als: Unified diff
Tests for SL::Model::Record