Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision b10162ca

Von Tamino Steinert vor mehr als 2 Jahren hinzugefügt

  • ID b10162ca46d2d05d33befa61f802747fee3aeaba
  • Vorgänger ef8bf303
  • Nachfolger 9a516d26

Reclamation: Test for workflow invoice_to_reclamation

Unterschiede anzeigen:

SL/DB/Invoice.pm
5 5
use Carp;
6 6
use List::Util qw(first sum);
7 7

  
8
use Rose::DB::Object::Helpers qw(has_loaded_related forget_related);
8
use Rose::DB::Object::Helpers qw(has_loaded_related forget_related as_tree strip);
9 9
use SL::DB::MetaSetup::Invoice;
10 10
use SL::DB::Manager::Invoice;
11 11
use SL::DB::Helper::Payment qw(:ALL);
SL/DB/InvoiceItem.pm
17 17
    },
18 18
  },
19 19
);
20
use Rose::DB::Object::Helpers qw(as_tree strip);
20 21

  
21 22
__PACKAGE__->configure_acts_as_list(group_by => [qw(trans_id)]);
22 23

  
SL/DB/PurchaseInvoice.pm
13 13
use SL::DB::Helper::Payment qw(:ALL);
14 14
use SL::DB::Helper::SalesPurchaseInvoice;
15 15
use SL::Locale::String qw(t8);
16
use Rose::DB::Object::Helpers qw(has_loaded_related forget_related);
16
use Rose::DB::Object::Helpers qw(has_loaded_related forget_related as_tree strip);
17 17

  
18 18
# The calculator hasn't been adjusted for purchase invoices yet.
19 19
# use SL::DB::Helper::PriceTaxCalculator;
SL/Dev/Record.pm
37 37
use SL::DATEV;
38 38

  
39 39
my %record_type_to_item_type = ( sales_invoice        => 'SL::DB::InvoiceItem',
40
                                 purchase_invoice     => 'SL::DB::InvoiceItem',
40 41
                                 credit_note          => 'SL::DB::InvoiceItem',
41 42
                                 sales_order          => 'SL::DB::OrderItem',
42 43
                                 purchase_order       => 'SL::DB::OrderItem',
......
77 78
  return $invoice;
78 79
}
79 80

  
81
sub create_purchase_invoice {
82
  my (%params) = @_;
83

  
84
  my $record_type = 'purchase_invoice';
85
  my $invoiceitems = delete $params{invoiceitems} // _create_two_items($record_type);
86
  _check_items($invoiceitems, $record_type);
87

  
88
  my $vendor = delete $params{vendor} // new_vendor(name => 'Testvendor')->save;
89
  die "illegal vendor" unless defined $vendor && ref($vendor) eq 'SL::DB::Vendor';
90

  
91
  my $invoice = SL::DB::PurchaseInvoice->new(
92
    invoice      => 1,
93
    type         => 'purchase_invoice',
94
    vendor       => $vendor->id,
95
    taxzone_id   => $vendor->taxzone->id,
96
    invnumber    => delete $params{invnumber}   // undef,
97
    currency_id  => $params{currency_id} // $::instance_conf->get_currency_id,
98
    taxincluded  => $params{taxincluded} // 0,
99
    employee_id  => $params{employee_id} // SL::DB::Manager::Employee->current->id,
100
    transdate    => $params{transdate}   // DateTime->today_local->to_kivitendo,
101
    payment_id   => $params{payment_id}  // undef,
102
    gldate       => DateTime->today,
103
    invoiceitems => $invoiceitems,
104
  );
105
  $invoice->assign_attributes(%params) if %params;
106

  
107
  $invoice->save;
108
  return $invoice;
109
}
110

  
80 111
sub create_credit_note {
81 112
  my (%params) = @_;
82 113

  
......
147 178
  _check_items($orderitems, $record_type);
148 179

  
149 180
  my $vendor = $params{vendor} // new_vendor(name => 'Testvendor')->save;
150
  die "illegal customer" unless ref($vendor) eq 'SL::DB::Vendor';
181
  die "illegal vendor" unless ref($vendor) eq 'SL::DB::Vendor';
151 182

  
152 183
  my $delivery_order = SL::DB::DeliveryOrder->new(
153 184
    order_type   => PURCHASE_DELIVERY_ORDER_TYPE,
t/workflow/invoice_to_reclamation.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 Data::Compare;
11
use Support::TestSetup;
12
use Test::Exception;
13
use List::Util qw(zip);
14

  
15
use SL::DB::Order;
16
use SL::DB::Reclamation;
17
use SL::DB::ReclamationReason;
18
use SL::DB::Customer;
19
use SL::DB::Vendor;
20
use SL::DB::Department;
21
use SL::DB::Currency;
22
use SL::DB::PaymentTerm;
23
use SL::DB::DeliveryTerm;
24
use SL::DB::Employee;
25
use SL::DB::Part;
26
use SL::DB::Unit;
27

  
28
use Rose::DB::Object::Helpers qw(clone);
29

  
30
use SL::Dev::ALL qw(:ALL);
31

  
32
my (
33
  $customer, $vendor,
34
  $employee,
35
  $payment_term,
36
  $delivery_term,
37
  $unit,
38
  @parts,
39
  $department,
40
  $relamation_reason,
41
);
42

  
43

  
44
sub clear_up {
45
  foreach (qw(
46
    Invoice PurchaseInvoice InvoiceItem
47
    Reclamation ReclamationItem
48
    Part
49
    Customer Vendor
50
    Department PaymentTerm DeliveryTerm
51
    )) {
52
    "SL::DB::Manager::${_}"->delete_all(all => 1);
53
  }
54
  SL::DB::Manager::Employee->delete_all(where => [ login => 'testuser' ]);
55
};
56

  
57
sub reset_state {
58
  my %params = @_;
59

  
60
  clear_up();
61

  
62
  $unit     = SL::DB::Manager::Unit->find_by(name => 'kg') || die "Can't find unit 'kg'";
63

  
64
  $employee = SL::DB::Employee->new(
65
    'login' => 'testuser',
66
    'name'  => 'Test User',
67
  )->save;
68

  
69
  $department = SL::DB::Department->new(
70
    'description' => 'Test Department',
71
  )->save;
72

  
73
  $payment_term = create_payment_terms(
74
     'description'      => '14Tage 2%Skonto, 30Tage netto',
75
     '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.",
76
     'percent_skonto'   => '0.02',
77
     'terms_netto'      => 30,
78
     'terms_skonto'     => 14
79
  );
80

  
81
  $delivery_term = SL::DB::DeliveryTerm->new(
82
    'description'      => 'Test Delivey Term',
83
    'description_long' => 'Test Delivey Term Test Delivey Term',
84
  )->save;
85

  
86
  # some parts/services
87
  @parts = ();
88
  push @parts, new_part(
89
    partnumber => 'Part_1_KG',
90
    unit        => $unit->name,
91
  )->save;
92
  push @parts, new_service(
93
    partnumber => 'Serv_1',
94
  )->save;
95
  push @parts, new_part(
96
    partnumber => 'Part_2',
97
  )->save;
98
  push @parts, new_service(
99
    partnumber => 'Serv_2'
100
  )->save;
101

  
102
  $relamation_reason = SL::DB::ReclamationReason->new(
103
    name => "test_reason",
104
    description => "",
105
    position => 1,
106
  );
107
}
108

  
109
Support::TestSetup::login();
110

  
111
reset_state();
112

  
113
#####
114

  
115
my $sales_reclamation = SL::Dev::Record::create_sales_reclamation(
116
  save                    => 1,
117
  employee                => $employee,
118
  shippingpoint           => "sp",
119
  transaction_description => "td1",
120
  payment                 => $payment_term,
121
  delivery_term           => $delivery_term,
122
  taxincluded             => 0,
123
  reclamation_items       => [
124
    SL::Dev::Record::create_reclamation_item(
125
      part => $parts[0], qty =>  3, sellprice => 70,
126
      reason => $relamation_reason,
127
    ),
128
    SL::Dev::Record::create_reclamation_item(
129
      part => $parts[1], qty => 10, sellprice => 50,
130
      reason => $relamation_reason,
131
    ),
132
  ],
133
)->load;
134

  
135
my $purchase_reclamation = SL::Dev::Record::create_purchase_reclamation(
136
  save                    => 1,
137
  employee                => $employee,
138
  shippingpoint           => "sp",
139
  transaction_description => "td2",
140
  payment                 => $payment_term,
141
  delivery_term           => $delivery_term,
142
  taxincluded             => 0,
143
  reclamation_items       => [
144
    SL::Dev::Record::create_reclamation_item(
145
      part => $parts[0], qty =>  3, sellprice => 70,
146
      reason => $relamation_reason,
147
    ),
148
    SL::Dev::Record::create_reclamation_item(
149
      part => $parts[1], qty => 10, sellprice => 50,
150
      reason => $relamation_reason,
151
    ),
152
  ],
153
)->load;
154

  
155

  
156
my $sales_invoice = SL::Dev::Record::create_sales_invoice(
157
  save                    => 1,
158
  employee                => $employee,
159
  shippingpoint           => "sp",
160
  transaction_description => "td3",
161
  payment_terms           => $payment_term,
162
  delivery_term           => $delivery_term,
163
  taxincluded             => 0,
164
  invoiceitems => [ SL::Dev::Record::create_invoice_item(part => $parts[0], qty =>  3, sellprice => 70),
165
                  SL::Dev::Record::create_invoice_item(part => $parts[1], qty => 10, sellprice => 50),
166
  ]
167
)->load;
168

  
169
my $purchase_invoice = SL::Dev::Record::create_purchase_invoice(
170
  save                    => 1,
171
  employee                => $employee,
172
  invnumber               => "t1",
173
  transaction_description => "td4",
174
  payment_terms           => $payment_term,
175
  delivery_term           => $delivery_term,
176
  taxincluded             => 0,
177
  invoiceitems => [ SL::Dev::Record::create_invoice_item(part => $parts[0], qty =>  3, sellprice => 70),
178
                  SL::Dev::Record::create_invoice_item(part => $parts[1], qty => 10, sellprice => 50),
179
  ]
180
)->load;
181

  
182
# convert invoice → reclamation
183
my $converted_sales_reclamation = $sales_invoice->convert_to_reclamation;
184
$converted_sales_reclamation->items_sorted->[0]->reason($relamation_reason);
185
$converted_sales_reclamation->items_sorted->[1]->reason($relamation_reason);
186
$converted_sales_reclamation->save->load;
187
my $converted_purchase_reclamation = $purchase_invoice->convert_to_reclamation;
188
$converted_purchase_reclamation->items_sorted->[0]->reason($relamation_reason);
189
$converted_purchase_reclamation->items_sorted->[1]->reason($relamation_reason);
190
$converted_purchase_reclamation->save->load;
191

  
192
#get items before strip
193
my @purchase_reclamation_items = $purchase_reclamation->items_sorted;
194
my @sales_reclamation_items    = $sales_reclamation->items_sorted;
195
my @converted_purchase_reclamation_items = $converted_purchase_reclamation->items_sorted;
196
my @converted_sales_reclamation_items    = $converted_sales_reclamation->items_sorted;
197
my @purchase_invoice_items = $purchase_invoice->items_sorted;
198
my @sales_invoice_items    = $sales_invoice->items_sorted;
199

  
200

  
201
### TESTS #####################################################################
202

  
203
## created sales und purchase reclamation should be nearly the same
204
my $sales_reclamation_tmp = clone($sales_reclamation);
205
my $purchase_reclamation_tmp = clone($purchase_reclamation);
206
# clean different values
207
foreach (qw(
208
  customer_id vendor_id
209
  id record_number
210
  salesman_id
211
  transaction_description
212
  itime mtime
213
  )) {
214
  $sales_reclamation_tmp->$_(undef);
215
  $purchase_reclamation_tmp->$_(undef);
216
}
217
foreach my $pair (zip(@purchase_reclamation_items, @sales_reclamation_items)) {
218
  my ($first, $second) = @{$pair};
219
  my $first_tmp = clone($first);
220
  my $second_tmp = clone($second);
221
  foreach (qw(
222
    id reclamation_id
223
    itime mtime
224
    )) {
225
    $first_tmp->$_(undef);
226
    $second_tmp->$_(undef);
227
  }
228
  is_deeply($first_tmp->strip->as_tree, $second_tmp->strip->as_tree);
229
}
230
is_deeply($purchase_reclamation_tmp->strip->as_tree, $sales_reclamation_tmp->strip->as_tree);
231

  
232
## converted have to be linked to parent
233
my $linked_sales_invoice = $converted_sales_reclamation->linked_records->[0];
234
is_deeply($linked_sales_invoice->strip->as_tree, $sales_invoice->strip->as_tree);
235
my $linked_purchase_invoice = $converted_purchase_reclamation->linked_records->[0];
236
is_deeply($linked_purchase_invoice->strip->as_tree, $purchase_invoice->strip->as_tree);
237

  
238

  
239
## converted should be nealy the same
240
foreach my $pair (zip(@sales_invoice_items, @converted_sales_reclamation_items)) {
241
  my ($first, $second) = @{$pair};
242
  ok Compare($first->strip->as_tree, $second->strip->as_tree, {ignore_hash_keys => [qw(
243
        id trans_id reclamation_id itime mtime
244
        allocated assemblyitem cusordnumber deliverydate donumber fxsellprice marge_percent marge_price_factor marge_total optional ordnumber subtotal transdate
245
        reason_description_ext reason_description_int reason_id reqdate
246
      )]});
247
}
248
ok Compare($sales_invoice->strip->as_tree, $converted_sales_reclamation->strip->as_tree, {ignore_hash_keys => [qw(
249
      id employee_id itime mtime transdate
250
      datepaid delivery_customer_id delivery_vendor_id deliverydate direct_debit donumber duedate dunning_config_id gldate invnumber_for_credit_note invoice marge_percent marge_total orddate ordnumber paid qrbill_without_amount quodate quonumber storno storno_id type
251
      delivered closed exchangerate reqdate vendor_id
252
      cp_id contact_id
253
      cusordnumber cv_record_number
254
      invnumber record_number
255
      )]});
256

  
257
foreach my $pair (zip(@purchase_invoice_items, @converted_purchase_reclamation_items)) {
258
  my ($first, $second) = @{$pair};
259
  ok Compare($first->strip->as_tree, $second->strip->as_tree, {ignore_hash_keys => [qw(
260
        id trans_id reclamation_id itime mtime
261
        allocated assemblyitem cusordnumber deliverydate donumber fxsellprice marge_percent marge_price_factor marge_total optional ordnumber subtotal transdate
262
        reason_description_ext reason_description_int reason_id reqdate
263
      )]});
264
}
265
ok Compare($purchase_invoice->strip->as_tree, $converted_purchase_reclamation->strip->as_tree, {ignore_hash_keys => [qw(
266
      id employee_id itime mtime transdate
267
      datepaid deliverydate direct_debit duedate gldate invoice orddate ordnumber paid quodate quonumber storno storno_id type
268
      customer_id cv_record_number delivered closed exchangerate reqdate salesman_id shippingpoint shipto_id
269
      cp_id contact_id
270
      invnumber record_number
271
      )]});
272

  
273

  
274
####
275
clear_up();
276

  
277
done_testing;
278

  
279
1;

Auch abrufbar als: Unified diff