]> wagnertech.de Git - mfinanz.git/blob - SL/Dev/Record.pm
kivitendo 3.9.2-0.2
[mfinanz.git] / SL / Dev / Record.pm
1 package SL::Dev::Record;
2
3 use strict;
4 use base qw(Exporter);
5 our @EXPORT_OK = qw(
6                     create_project
7                     create_department
8
9                     create_order_item
10                     create_delivery_order_item
11                     create_invoice_item
12                     create_reclamation_item
13
14                     create_sales_quotation
15                     create_sales_order
16                     create_sales_delivery_order
17                     create_sales_invoice
18                     create_credit_note
19                     create_sales_reclamation
20
21                     create_purchase_quotation
22                     create_purchase_order
23                     create_purchase_delivery_order
24                     create_minimal_purchase_invoice
25                     create_purchase_reclamation
26
27                     create_ap_transaction
28                     create_ar_transaction
29                     create_gl_transaction
30                    );
31 our %EXPORT_TAGS = (ALL => \@EXPORT_OK);
32
33 use SL::DB::Invoice;
34 use SL::DB::InvoiceItem;
35 use SL::DB::Order;
36 use SL::DB::Order::TypeData qw(:types);
37 use SL::DB::DeliveryOrder;
38 use SL::DB::DeliveryOrder::TypeData qw(:types);
39 use SL::DB::Reclamation;
40 use SL::DB::Reclamation::TypeData qw(:types);
41 use SL::DB::Employee;
42 use SL::Dev::Part qw(new_part);
43 use SL::Dev::CustomerVendor qw(new_vendor new_customer);
44 use SL::DB::Project;
45 use SL::DB::ProjectStatus;
46 use SL::DB::ProjectType;
47 use SL::Form;
48 use DateTime;
49 use List::Util qw(sum);
50 use Data::Dumper;
51 use SL::Locale::String qw(t8);
52 use SL::DATEV;
53 use SL::Model::Record;
54
55 my %record_type_to_item_type = ( sales_invoice        => 'SL::DB::InvoiceItem',
56                                  purchase_invoice     => 'SL::DB::InvoiceItem',
57                                  credit_note          => 'SL::DB::InvoiceItem',
58                                  sales_order          => 'SL::DB::OrderItem',
59                                  sales_quotation      => 'SL::DB::OrderItem',
60                                  request_quotation    => 'SL::DB::OrderItem',
61                                  purchase_order       => 'SL::DB::OrderItem',
62                                  sales_delivery_order => 'SL::DB::DeliveryOrderItem',
63                                  purchase_delivery_order => 'SL::DB::DeliveryOrderItem',
64                                  sales_reclamation    => 'SL::DB::ReclamationItem',
65                                  purchase_reclamation => 'SL::DB::ReclamationItem',
66                                );
67
68 sub create_sales_invoice {
69   my (%params) = @_;
70
71   my $record_type = 'sales_invoice';
72   my $invoiceitems = delete $params{invoiceitems} // _create_two_items($record_type);
73   _check_items($invoiceitems, $record_type);
74
75   my $customer = delete $params{customer} // new_customer(name => 'Testcustomer')->save;
76   die "illegal customer" unless defined $customer && ref($customer) eq 'SL::DB::Customer';
77
78   my $invoice = SL::DB::Invoice->new(
79     invoice      => 1,
80     type         => 'invoice',
81     customer_id  => $customer->id,
82     taxzone_id   => $customer->taxzone->id,
83     invnumber    => delete $params{invnumber}   // undef,
84     currency_id  => $params{currency_id} // $::instance_conf->get_currency_id,
85     taxincluded  => $params{taxincluded} // 0,
86     employee_id  => $params{employee_id} // SL::DB::Manager::Employee->current->id,
87     salesman_id  => $params{employee_id} // SL::DB::Manager::Employee->current->id,
88     transdate    => $params{transdate}   // DateTime->today_local->to_kivitendo,
89     payment_id   => $params{payment_id}  // undef,
90     gldate       => DateTime->today,
91     invoiceitems => $invoiceitems,
92   );
93   $invoice->assign_attributes(%params) if %params;
94
95   $invoice->post;
96   return $invoice;
97 }
98
99 sub create_minimal_purchase_invoice {
100   my (%params) = @_;
101
102   my $record_type = 'purchase_invoice';
103   my $invoiceitems = delete $params{invoiceitems} // _create_two_items($record_type);
104   _check_items($invoiceitems, $record_type);
105
106   my $vendor = delete $params{vendor} // new_vendor(name => 'Testvendor')->save;
107   die "illegal vendor" unless defined $vendor && ref($vendor) eq 'SL::DB::Vendor';
108
109   my $invoice = SL::DB::PurchaseInvoice->new(
110     invoice      => 1,
111     type         => 'purchase_invoice',
112     vendor       => $vendor->id,
113     taxzone_id   => $vendor->taxzone->id,
114     invnumber    => delete $params{invnumber}   // undef,
115     currency_id  => $params{currency_id} // $::instance_conf->get_currency_id,
116     taxincluded  => $params{taxincluded} // 0,
117     employee_id  => $params{employee_id} // SL::DB::Manager::Employee->current->id,
118     transdate    => $params{transdate}   // DateTime->today_local->to_kivitendo,
119     payment_id   => $params{payment_id}  // undef,
120     gldate       => DateTime->today,
121     invoiceitems => $invoiceitems,
122   );
123   $invoice->assign_attributes(%params) if %params;
124
125   # TODO: PTC can't deal with purchase invoices, so for now just save via Rose,
126   # no amount/tax/acc_trans calculations.
127
128   $invoice->save;
129   return $invoice;
130 }
131
132 sub create_credit_note {
133   my (%params) = @_;
134
135   my $record_type = 'credit_note';
136   my $invoiceitems = delete $params{invoiceitems} // _create_two_items($record_type);
137   _check_items($invoiceitems, $record_type);
138
139   my $customer = delete $params{customer} // new_customer(name => 'Testcustomer')->save;
140   die "illegal customer" unless defined $customer && ref($customer) eq 'SL::DB::Customer';
141
142   # adjust qty for credit note items
143   $_->qty( $_->qty * -1) foreach @{$invoiceitems};
144
145   my $invoice = SL::DB::Invoice->new(
146     invoice      => 1,
147     type         => 'credit_note',
148     customer_id  => $customer->id,
149     taxzone_id   => $customer->taxzone->id,
150     invnumber    => delete $params{invnumber}   // undef,
151     currency_id  => $params{currency_id} // $::instance_conf->get_currency_id,
152     taxincluded  => $params{taxincluded} // 0,
153     employee_id  => $params{employee_id} // SL::DB::Manager::Employee->current->id,
154     salesman_id  => $params{employee_id} // SL::DB::Manager::Employee->current->id,
155     transdate    => $params{transdate}   // DateTime->today_local->to_kivitendo,
156     payment_id   => $params{payment_id}  // undef,
157     gldate       => DateTime->today,
158     invoiceitems => $invoiceitems,
159   );
160   $invoice->assign_attributes(%params) if %params;
161
162   $invoice->post;
163   return $invoice;
164 }
165
166 sub create_sales_delivery_order {
167   my (%params) = @_;
168
169   my $record_type = 'sales_delivery_order';
170   my $orderitems = delete $params{orderitems} // _create_two_items($record_type);
171   _check_items($orderitems, $record_type);
172
173   my $customer = $params{customer} // new_customer(name => 'Testcustomer')->save;
174   die "illegal customer" unless ref($customer) eq 'SL::DB::Customer';
175
176   my $delivery_order = SL::DB::DeliveryOrder->new(
177     record_type  => SALES_DELIVERY_ORDER_TYPE,
178     'closed'     => undef,
179     customer_id  => $customer->id,
180     taxzone_id   => $customer->taxzone_id,
181     donumber     => $params{donumber}    // undef,
182     currency_id  => $params{currency_id} // $::instance_conf->get_currency_id,
183     taxincluded  => $params{taxincluded} // 0,
184     employee_id  => $params{employee_id} // SL::DB::Manager::Employee->current->id,
185     salesman_id  => $params{employee_id} // SL::DB::Manager::Employee->current->id,
186     transdate    => $params{transdate}   // DateTime->today,
187     orderitems   => $orderitems,
188   );
189   $delivery_order->assign_attributes(%params) if %params;
190   SL::Model::Record->save($delivery_order);
191   return $delivery_order;
192 }
193
194 sub create_purchase_delivery_order {
195   my (%params) = @_;
196
197   my $record_type = 'purchase_delivery_order';
198   my $orderitems = delete $params{orderitems} // _create_two_items($record_type);
199   _check_items($orderitems, $record_type);
200
201   my $vendor = $params{vendor} // new_vendor(name => 'Testvendor')->save;
202   die "illegal vendor" unless ref($vendor) eq 'SL::DB::Vendor';
203
204   my $delivery_order = SL::DB::DeliveryOrder->new(
205     record_type  => PURCHASE_DELIVERY_ORDER_TYPE,
206     'closed'     => undef,
207     vendor_id    => $vendor->id,
208     taxzone_id   => $vendor->taxzone_id,
209     donumber     => $params{donumber}    // undef,
210     currency_id  => $params{currency_id} // $::instance_conf->get_currency_id,
211     taxincluded  => $params{taxincluded} // 0,
212     employee_id  => $params{employee_id} // SL::DB::Manager::Employee->current->id,
213     salesman_id  => $params{employee_id} // SL::DB::Manager::Employee->current->id,
214     transdate    => $params{transdate}   // DateTime->today,
215     orderitems   => $orderitems,
216   );
217   $delivery_order->assign_attributes(%params) if %params;
218   SL::Model::Record->save($delivery_order);
219   return $delivery_order;
220 }
221
222 sub create_sales_quotation {
223   my (%params) = @_;
224   $params{type} = SALES_QUOTATION_TYPE();
225   _create_sales_order_or_quotation(%params);
226 }
227
228 sub create_sales_order {
229   my (%params) = @_;
230   $params{type} = SALES_ORDER_TYPE();
231   _create_sales_order_or_quotation(%params);
232 }
233
234 sub create_purchase_quotation {
235   my (%params) = @_;
236   $params{type} = REQUEST_QUOTATION_TYPE();
237   # TODO: set a with reqdate
238   _create_purchase_order_or_quotation(%params);
239 }
240
241 sub create_purchase_order {
242   my (%params) = @_;
243   $params{type} = PURCHASE_ORDER_TYPE();
244   _create_purchase_order_or_quotation(%params);
245 }
246
247 sub create_sales_reclamation {
248   my (%params) = @_;
249
250   my $record_type = SALES_RECLAMATION_TYPE();
251   my $reclamation_items = delete $params{reclamation_items} // _create_two_items($record_type);
252   _check_items($reclamation_items, $record_type);
253
254   my $save = delete $params{save} // 0;
255
256   my $customer = $params{customer} // new_customer(name => 'Test_Customer')->save;
257   die "'customer' is not of type SL::DB::Customer" unless ref($customer) eq 'SL::DB::Customer';
258
259   my $reclamation = SL::DB::Reclamation->new(
260     record_type  => $record_type,
261     customer_id  => delete $params{customer_id} // $customer->id,
262     taxzone_id   => delete $params{taxzone_id}  // $customer->taxzone->id,
263     currency_id  => delete $params{currency_id} // $::instance_conf->get_currency_id,
264     taxincluded  => delete $params{taxincluded} // 0,
265     transdate    => delete $params{transdate}   // DateTime->today,
266     'closed'     => undef,
267     reclamation_items => $reclamation_items,
268   );
269   $reclamation->assign_attributes(%params) if %params;
270
271   if ( $save ) {
272     SL::Model::Record->save($reclamation);
273   }
274   return $reclamation;
275 }
276
277 sub create_purchase_reclamation {
278   my (%params) = @_;
279
280   my $record_type = PURCHASE_RECLAMATION_TYPE();
281   my $reclamation_items = delete $params{reclamation_items} // _create_two_items($record_type);
282   _check_items($reclamation_items, $record_type);
283
284   my $save = delete $params{save} // 0;
285
286   my $vendor = $params{vendor} // new_vendor(name => 'Test_Vendor')->save;
287   die "'vendor' is not of type SL::DB::Vendor" unless ref($vendor) eq 'SL::DB::Vendor';
288
289   my $reclamation = SL::DB::Reclamation->new(
290     record_type  => $record_type,
291     vendor_id    => delete $params{vendor_id}   // $vendor->id,
292     taxzone_id   => delete $params{taxzone_id}  // $vendor->taxzone->id,
293     currency_id  => delete $params{currency_id} // $::instance_conf->get_currency_id,
294     taxincluded  => delete $params{taxincluded} // 0,
295     transdate    => delete $params{transdate}   // DateTime->today,
296     'closed'     => undef,
297     reclamation_items => $reclamation_items,
298   );
299   $reclamation->assign_attributes(%params) if %params;
300
301   if ( $save ) {
302     SL::Model::Record->save($reclamation);
303   }
304   return $reclamation;
305 }
306
307 sub _check_items {
308   my ($items, $record_type) = @_;
309
310   if  ( scalar @{$items} == 0 or grep { ref($_) ne $record_type_to_item_type{"$record_type"} } @{$items} ) {
311     die "Error: items must be an arrayref of " . $record_type_to_item_type{"$record_type"} . "objects.";
312   }
313 }
314
315 sub create_invoice_item {
316   my (%params) = @_;
317
318   return _create_item(record_type => 'sales_invoice', %params);
319 }
320
321 sub create_order_item {
322   my (%params) = @_;
323
324   return _create_item(record_type => 'sales_order', %params);
325 }
326
327 sub create_delivery_order_item {
328   my (%params) = @_;
329
330   return _create_item(record_type => 'sales_delivery_order', %params);
331 }
332
333 sub create_reclamation_item {
334   my (%params) = @_;
335
336   # record_type can be sales or purchase; make sure one is set
337   return _create_item(record_type => 'sales_reclamation', %params);
338 }
339
340 sub _create_item {
341   my (%params) = @_;
342
343   my $record_type = delete($params{record_type});
344   my $part        = delete($params{part});
345
346   die "illegal record type: $record_type, must be one of: " . join(' ', keys %record_type_to_item_type) unless $record_type_to_item_type{ $record_type };
347   die "part missing as param" unless $part && ref($part) eq 'SL::DB::Part';
348
349   my ($sellprice, $lastcost);
350
351   if ( $record_type =~ /^sales/ ) {
352     $sellprice = delete $params{sellprice} // $part->sellprice;
353     $lastcost  = delete $params{lastcost}  // $part->lastcost;
354   } else {
355     $sellprice = delete $params{sellprice} // $part->lastcost;
356     $lastcost  = delete $params{lastcost}  // 0; # $part->lastcost;
357   }
358
359   my $item = "$record_type_to_item_type{$record_type}"->new(
360     parts_id    => $part->id,
361     sellprice   => $sellprice,
362     lastcost    => $lastcost,
363     description => $part->description,
364     unit        => $part->unit,
365     qty         => $params{qty} || 5,
366   );
367   $item->assign_attributes(%params) if %params;
368   return $item;
369 }
370
371 sub _create_two_items {
372   my ($record_type) = @_;
373
374   my $part1 = new_part(description => 'Testpart 1',
375                        sellprice   => 12,
376                       )->save;
377   my $part2 = new_part(description => 'Testpart 2',
378                        sellprice   => 10,
379                       )->save;
380   my $item1 = _create_item(record_type => $record_type, part => $part1, qty => 5);
381   my $item2 = _create_item(record_type => $record_type, part => $part2, qty => 8);
382   return [ $item1, $item2 ];
383 }
384
385 sub create_project {
386   my (%params) = @_;
387   my $project = SL::DB::Project->new(
388     projectnumber     => delete $params{projectnumber} // 1,
389     description       => delete $params{description} // "Test project",
390     active            => 1,
391     valid             => 1,
392     project_status_id => SL::DB::Manager::ProjectStatus->find_by(name => "running")->id,
393     project_type_id   => SL::DB::Manager::ProjectType->find_by(description => "Standard")->id,
394   )->save;
395   $project->assign_attributes(%params) if %params;
396   return $project;
397 }
398
399 sub create_department {
400   my (%params) = @_;
401
402   my $department = SL::DB::Department->new(
403     'description' => delete $params{description} // 'Test Department',
404   )->save;
405
406   $department->assign_attributes(%params) if %params;
407   return $department;
408
409 }
410
411 sub create_ap_transaction {
412   my (%params) = @_;
413
414   my $vendor = delete $params{vendor};
415   if ( $vendor ) {
416     die "vendor missing or not a SL::DB::Vendor object" unless ref($vendor) eq 'SL::DB::Vendor';
417   } else {
418     # use default SL/Dev vendor if it exists, or create a new one
419     $vendor = SL::DB::Manager::Vendor->find_by(name => 'Testlieferant') // new_vendor->save;
420   };
421
422   my $taxincluded = $params{taxincluded} // 1;
423   delete $params{taxincluded};
424
425   my $bookings    = delete $params{bookings};
426   # default bookings
427   unless ( $bookings ) {
428     my $chart_postage   = SL::DB::Manager::Chart->find_by(description => 'Porto');
429     my $chart_telephone = SL::DB::Manager::Chart->find_by(description => 'Telefon');
430     $bookings = [
431                   {
432                     chart  => $chart_postage,
433                     amount => 1000,
434                   },
435                   {
436                     chart  => $chart_telephone,
437                     amount => $taxincluded ? 1190 : 1000,
438                   },
439                 ]
440   };
441
442   # optional params:
443   my $project_id         = delete $params{globalproject_id};
444
445   # if amount or netamount are given, then it compares them to the final values, and dies if they don't match
446   my $expected_amount    = delete $params{amount};
447   my $expected_netamount = delete $params{netamount};
448
449   my $dec = delete $params{dec} // 2;
450
451   my $today      = DateTime->today_local;
452   my $transdate  = delete $params{transdate} // $today;
453   die "transdate hat to be DateTime object" unless ref($transdate) eq 'DateTime';
454
455   my $gldate     = delete $params{gldate} // $today;
456   die "gldate hat to be DateTime object" unless ref($gldate) eq 'DateTime';
457
458   my $ap_chart = delete $params{ap_chart} // SL::DB::Manager::Chart->find_by( accno => '1600' );
459   die "no ap_chart found or not an AP chart" unless $ap_chart and $ap_chart->link eq 'AP';
460
461   my $ap_transaction = SL::DB::PurchaseInvoice->new(
462     vendor_id        => $vendor->id,
463     invoice          => 0,
464     transactions     => [],
465     globalproject_id => $project_id,
466     invnumber        => delete $params{invnumber} // 'test ap_transaction',
467     notes            => delete $params{notes}     // 'test ap_transaction',
468     transdate        => $transdate,
469     gldate           => $gldate,
470     taxincluded      => $taxincluded,
471     taxzone_id       => $vendor->taxzone_id, # taxzone_id shouldn't have any effect on ap transactions
472     currency_id      => $::instance_conf->get_currency_id,
473     type             => undef, # isn't set for ap
474     employee_id      => SL::DB::Manager::Employee->current->id,
475   );
476   # assign any parameters that weren't explicitly handled above, e.g. itime
477   $ap_transaction->assign_attributes(%params) if %params;
478
479   foreach my $booking ( @{$bookings} ) {
480     my $chart = delete $booking->{chart};
481     die "illegal chart" unless ref($chart) eq 'SL::DB::Chart';
482
483     my $tax = _transaction_tax_helper($booking, $chart, $transdate); # will die if tax can't be found
484
485     $ap_transaction->add_ap_amount_row(
486       amount     => $booking->{amount}, # add_ap_amount_row expects the user input amount, does its own calculate_tax
487       chart      => $chart,
488       tax_id     => $tax->id,
489       project_id => $booking->{project_id},
490     );
491   }
492
493   my $acc_trans_sum = sum map { $_->amount  } grep { $_->chart_link =~ 'AP_amount' } @{$ap_transaction->transactions};
494   # $main::lxdebug->message(0, sprintf("accno: %s    amount: %s   chart_link: %s\n",
495   #                                    $_->amount,
496   #                                    $_->chart->accno,
497   #                                    $_->chart_link
498   #                                   )) foreach @{$ap_transaction->transactions};
499
500   # determine netamount and amount from the transactions that were added via bookings
501   $ap_transaction->netamount( -1 * sum map { $_->amount  } grep { $_->chart_link =~ 'AP_amount' } @{$ap_transaction->transactions} );
502   # $main::lxdebug->message(0, sprintf('found netamount %s', $ap_transaction->netamount));
503
504   my $taxamount = -1 * sum map { $_->amount  } grep { $_->chart_link =~ /tax/ } @{$ap_transaction->transactions};
505   $ap_transaction->amount( $ap_transaction->netamount + $taxamount );
506   # additional check, add up all transactions before AP-transaction is added
507   my $refamount = -1 * sum map { $_->amount  } @{$ap_transaction->transactions};
508   die "refamount = $refamount, ap_transaction->amount = " . $ap_transaction->amount unless $refamount == $ap_transaction->amount;
509
510   # if amount or netamount were passed as params, check if the values are still
511   # the same after recalculating them from the acc_trans entries
512   if (defined $expected_amount) {
513     die "amount doesn't match acc_trans amounts: $expected_amount != " . $ap_transaction->amount unless $expected_amount == $ap_transaction->amount;
514   }
515   if (defined $expected_netamount) {
516     die "netamount doesn't match acc_trans netamounts: $expected_netamount != " . $ap_transaction->netamount unless $expected_netamount == $ap_transaction->netamount;
517   }
518
519   $ap_transaction->create_ap_row(chart => $ap_chart);
520   $ap_transaction->save;
521   # $main::lxdebug->message(0, sprintf("created ap_transaction with invnumber %s and trans_id %s",
522   #                                     $ap_transaction->invnumber,
523   #                                     $ap_transaction->id));
524   return $ap_transaction;
525 }
526
527 sub create_ar_transaction {
528   my (%params) = @_;
529
530   my $customer = delete $params{customer};
531   if ( $customer ) {
532     die "customer missing or not a SL::DB::Customer object" unless ref($customer) eq 'SL::DB::Customer';
533   } else {
534     # use default SL/Dev vendor if it exists, or create a new one
535     $customer = SL::DB::Manager::Customer->find_by(name => 'Testkunde') // new_customer->save;
536   };
537
538   my $taxincluded = $params{taxincluded} // 1;
539   delete $params{taxincluded};
540
541   my $bookings    = delete $params{bookings};
542   # default bookings
543   unless ( $bookings ) {
544     my $chart_19 = SL::DB::Manager::Chart->find_by(accno => '8400');
545     my $chart_7  = SL::DB::Manager::Chart->find_by(accno => '8300');
546     my $chart_0  = SL::DB::Manager::Chart->find_by(accno => '8200');
547     $bookings = [
548                   {
549                     chart  => $chart_19,
550                     amount => $taxincluded ? 119 : 100,
551                   },
552                   {
553                     chart  => $chart_7,
554                     amount => $taxincluded ? 107 : 100,
555                   },
556                   {
557                     chart  => $chart_0,
558                     amount => 100,
559                   },
560                 ]
561   };
562
563   # optional params:
564   my $project_id = delete $params{globalproject_id};
565
566   # if amount or netamount are given, then it compares them to the final values, and dies if they don't match
567   my $expected_amount    = delete $params{amount};
568   my $expected_netamount = delete $params{netamount};
569
570   my $dec = delete $params{dec} // 2;
571
572   my $today      = DateTime->today_local;
573   my $transdate  = delete $params{transdate} // $today;
574   die "transdate hat to be DateTime object" unless ref($transdate) eq 'DateTime';
575
576   my $gldate     = delete $params{gldate} // $today;
577   die "gldate hat to be DateTime object" unless ref($gldate) eq 'DateTime';
578
579   my $ar_chart = delete $params{ar_chart} // SL::DB::Manager::Chart->find_by( accno => '1400' );
580   die "no ar_chart found or not an AR chart" unless $ar_chart and $ar_chart->link eq 'AR';
581
582   my $ar_transaction = SL::DB::Invoice->new(
583     customer_id      => $customer->id,
584     invoice          => 0,
585     transactions     => [],
586     globalproject_id => $project_id,
587     invnumber        => delete $params{invnumber} // 'test ar_transaction',
588     notes            => delete $params{notes}     // 'test ar_transaction',
589     transdate        => $transdate,
590     gldate           => $gldate,
591     taxincluded      => $taxincluded,
592     taxzone_id       => $customer->taxzone_id, # taxzone_id shouldn't have any effect on ar transactions
593     currency_id      => $::instance_conf->get_currency_id,
594     type             => undef, # isn't set for ar
595     employee_id      => SL::DB::Manager::Employee->current->id,
596   );
597   # assign any parameters that weren't explicitly handled above, e.g. itime
598   $ar_transaction->assign_attributes(%params) if %params;
599
600   foreach my $booking ( @{$bookings} ) {
601     my $chart = delete $booking->{chart};
602     die "illegal chart" unless ref($chart) eq 'SL::DB::Chart';
603
604     my $tax = _transaction_tax_helper($booking, $chart, $transdate); # will die if tax can't be found
605
606     $ar_transaction->add_ar_amount_row(
607       amount     => $booking->{amount}, # add_ar_amount_row expects the user input amount, does its own calculate_tax
608       chart      => $chart,
609       tax_id     => $tax->id,
610       project_id => $booking->{project_id},
611     );
612   }
613
614   my $acc_trans_sum = sum map { $_->amount  } grep { $_->chart_link =~ 'AR_amount' } @{$ar_transaction->transactions};
615   # $main::lxdebug->message(0, sprintf("accno: %s    amount: %s   chart_link: %s\n",
616   #                                    $_->amount,
617   #                                    $_->chart->accno,
618   #                                    $_->chart_link
619   #                                   )) foreach @{$ar_transaction->transactions};
620
621   # determine netamount and amount from the transactions that were added via bookings
622   $ar_transaction->netamount( 1 * sum map { $_->amount  } grep { $_->chart_link =~ 'AR_amount' } @{$ar_transaction->transactions} );
623   # $main::lxdebug->message(0, sprintf('found netamount %s', $ar_transaction->netamount));
624
625   my $taxamount = 1 * sum map { $_->amount  } grep { $_->chart_link =~ /tax/ } @{$ar_transaction->transactions};
626   $ar_transaction->amount( $ar_transaction->netamount + $taxamount );
627   # additional check, add up all transactions before AP-transaction is added
628   my $refamount = 1 * sum map { $_->amount  } @{$ar_transaction->transactions};
629   die "refamount = $refamount, ar_transaction->amount = " . $ar_transaction->amount unless $refamount == $ar_transaction->amount;
630
631   # if amount or netamount were passed as params, check if the values are still
632   # the same after recalculating them from the acc_trans entries
633   if (defined $expected_amount) {
634     die "amount doesn't match acc_trans amounts: $expected_amount != " . $ar_transaction->amount unless $expected_amount == $ar_transaction->amount;
635   }
636   if (defined $expected_netamount) {
637     die "netamount doesn't match acc_trans netamounts: $expected_netamount != " . $ar_transaction->netamount unless $expected_netamount == $ar_transaction->netamount;
638   }
639
640   $ar_transaction->create_ar_row(chart => $ar_chart);
641   $ar_transaction->save;
642   # $main::lxdebug->message(0, sprintf("created ar_transaction with invnumber %s and trans_id %s",
643   #                                     $ar_transaction->invnumber,
644   #                                     $ar_transaction->id));
645   return $ar_transaction;
646 }
647
648 sub create_gl_transaction {
649   my (%params) = @_;
650
651   my $ob_transaction = delete $params{ob_transaction} // 0;
652   my $cb_transaction = delete $params{cb_transaction} // 0;
653   my $dec            = delete $params{rec} // 2;
654
655   my $taxincluded = defined $params{taxincluded} ? $params{taxincluded} : 1;
656
657   my $today      = DateTime->today_local;
658   my $transdate  = delete $params{transdate} // $today;
659   my $gldate     = delete $params{gldate}    // $today;
660
661   my $reference   = delete $params{reference}   // 'reference';
662   my $description = delete $params{description} // 'description';
663
664   my $department_id = delete $params{department_id};
665
666   my $bookings = delete $params{bookings};
667   unless ( $bookings && scalar @{$bookings} ) {
668     # default bookings if left empty
669     my $expense_chart = SL::DB::Manager::Chart->find_by(accno => '4660') or die "Can't find expense chart 4660\n"; # Reisekosten
670     my $cash_chart    = SL::DB::Manager::Chart->find_by(accno => '1000') or die "Can't find cash chart 1000\n";    # Kasse
671
672     $taxincluded = 0;
673
674     $reference   = 'Reise';
675     $description = 'Reise';
676
677     $bookings = [
678                   {
679                     chart  => $expense_chart, # has default tax of 19%
680                     credit => 84.03,
681                     taxkey => 9,
682                   },
683                   {
684                     chart  => $cash_chart,
685                     debit  => 100,
686                     taxkey => 0,
687                   },
688     ];
689   }
690
691   my $gl_transaction = SL::DB::GLTransaction->new(
692     reference      => $reference,
693     description    => $description,
694     transdate      => $transdate,
695     gldate         => $gldate,
696     taxincluded    => $taxincluded,
697     type           => undef,
698     ob_transaction => $ob_transaction,
699     cb_transaction => $cb_transaction,
700     storno         => 0,
701     storno_id      => undef,
702     transactions   => [],
703   );
704   # assign any parameters that weren't explicitly handled above, e.g. itime
705   $gl_transaction->assign_attributes(%params) if %params;
706
707   my @acc_trans;
708   if ( scalar @{$bookings} ) {
709     # there are several ways of determining the tax:
710     # * tax_id : fetches SL::DB::Tax object via id (as used in dropdown in interface)
711     # * tax : SL::DB::Tax object (where $tax->id = tax_id)
712     # * taxkey : tax is determined from startdate
713     # * none of the above defined: use the default tax for that chart
714
715     foreach my $booking ( @{$bookings} ) {
716       my $chart = delete $booking->{chart};
717       die "illegal chart" unless ref($chart) eq 'SL::DB::Chart';
718
719       die t8('Empty transaction!')
720         unless $booking->{debit} or $booking->{credit}; # must exist and not be 0
721       die t8('Cannot post transaction with a debit and credit entry for the same account!')
722         if defined($booking->{debit}) and defined($booking->{credit});
723
724       my $tax = _transaction_tax_helper($booking, $chart, $transdate); # will die if tax can't be found
725
726       $gl_transaction->add_chart_booking(
727         chart      => $chart,
728         debit      => $booking->{debit},
729         credit     => $booking->{credit},
730         tax_id     => $tax->id,
731         source     => $booking->{source} // '',
732         memo       => $booking->{memo}   // '',
733         project_id => $booking->{project_id}
734       );
735     }
736   };
737
738   $gl_transaction->post;
739
740   return $gl_transaction;
741 }
742
743 sub _create_sales_order_or_quotation {
744   my (%params) = @_;
745
746   my $record_type = delete $params{type};
747   die "illegal type" unless $record_type eq SALES_ORDER_TYPE() or $record_type eq SALES_QUOTATION_TYPE();
748
749   my $orderitems = delete $params{orderitems} // _create_two_items($record_type);
750   _check_items($orderitems, $record_type);
751
752   my $save = delete $params{save} // 0;
753
754   my $customer = $params{customer} // new_customer(name => 'Testcustomer')->save;
755   die "illegal customer" unless ref($customer) eq 'SL::DB::Customer';
756
757   my $record = SL::DB::Order->new(
758     record_type  => $record_type,
759     customer_id  => delete $params{customer_id} // $customer->id,
760     taxzone_id   => delete $params{taxzone_id}  // $customer->taxzone->id,
761     currency_id  => delete $params{currency_id} // $::instance_conf->get_currency_id,
762     taxincluded  => delete $params{taxincluded} // 0,
763     employee_id  => delete $params{employee_id} // SL::DB::Manager::Employee->current->id,
764     salesman_id  => delete $params{employee_id} // SL::DB::Manager::Employee->current->id,
765     transdate    => delete $params{transdate}   // DateTime->today,
766     orderitems   => $orderitems,
767   );
768   $record->assign_attributes(%params) if %params;
769
770   if ( $save ) {
771     SL::Model::Record->save($record);
772   }
773   return $record;
774 }
775
776 sub _create_purchase_order_or_quotation {
777   my (%params) = @_;
778
779   my $record_type = delete $params{type};
780   die "illegal type" unless $record_type eq PURCHASE_ORDER_TYPE() or $record_type eq REQUEST_QUOTATION_TYPE();
781   my $orderitems = delete $params{orderitems} // _create_two_items($record_type);
782   _check_items($orderitems, $record_type);
783
784   my $save = delete $params{save} // 0;
785
786   my $vendor = $params{vendor} // new_vendor(name => 'Testvendor')->save;
787   die "illegal vendor" unless ref($vendor) eq 'SL::DB::Vendor';
788
789   my $record = SL::DB::Order->new(
790     record_type  => $record_type,
791     vendor_id    => delete $params{vendor_id}   // $vendor->id,
792     taxzone_id   => delete $params{taxzone_id}  // $vendor->taxzone->id,
793     currency_id  => delete $params{currency_id} // $::instance_conf->get_currency_id,
794     taxincluded  => delete $params{taxincluded} // 0,
795     transdate    => delete $params{transdate}   // DateTime->today,
796     'closed'     => undef,
797     orderitems   => $orderitems,
798   );
799   $record->assign_attributes(%params) if %params;
800
801   if ( $save ) {
802     SL::Model::Record->save($record);
803   }
804   return $record;
805 };
806
807 sub _transaction_tax_helper {
808   # checks for hash-entries with key tax, tax_id or taxkey
809   # returns an SL::DB::Tax object
810   # can be used for booking hashref in ar_transaction, ap_transaction and gl_transaction
811   # will modify hashref, e.g. removing taxkey if tax_id was also supplied
812
813   my ($booking, $chart, $transdate) = @_;
814
815   die "_transaction_tax_helper: chart missing"     unless $chart && ref($chart) eq 'SL::DB::Chart';
816   die "_transaction_tax_helper: transdate missing" unless $transdate && ref($transdate) eq 'DateTime';
817
818   my $tax;
819
820   if ( defined $booking->{tax_id} ) { # tax_id may be 0
821     delete $booking->{taxkey}; # ignore any taxkeys that may have been added, tax_id has precedence
822     $tax = SL::DB::Tax->new(id => $booking->{tax_id})->load( with => [ 'chart' ] );
823   } elsif ( $booking->{tax} ) {
824     die "illegal tax entry" unless ref($booking->{tax}) eq 'SL::DB::Tax';
825     $tax = $booking->{tax};
826   } elsif ( defined $booking->{taxkey} ) {
827     # If a taxkey is given, find the taxkey entry for that chart that
828     # matches the stored taxkey and with the correct transdate. This will only work
829     # if kivitendo has that taxkey configured for that chart, i.e. it should barf if
830     # e.g. the bank chart is called with taxkey 3.
831
832     # example query:
833     #   select *
834     #     from taxkeys
835     #    where     taxkey_id = 3
836     #          and chart_id = (select id from chart where accno = '8400')
837     #          and startdate <= '2018-01-01'
838     # order by startdate desc
839     #    limit 1;
840
841     my $taxkey = SL::DB::Manager::TaxKey->get_first(
842       query        => [ and => [ chart_id  => $chart->id,
843                                  startdate => { le => $transdate },
844                                  taxkey    => $booking->{taxkey}
845                                ]
846                       ],
847       sort_by      => "startdate DESC",
848       limit        => 1,
849       with_objects => [ qw(tax) ],
850     );
851     die sprintf("Chart %s doesn't have a taxkey chart configured for taxkey %s", $chart->accno, $booking->{taxkey})
852       unless $taxkey;
853
854     $tax = $taxkey->tax;
855   } else {
856     # use default tax for that chart if neither tax_id, tax or taxkey were defined
857     my $active_taxkey = $chart->get_active_taxkey($transdate);
858     $tax = $active_taxkey->tax;
859     # $main::lxdebug->message(0, sprintf("found default taxrate %s for chart %s", $tax->rate, $chart->displayable_name));
860   };
861
862   die "no tax" unless $tax && ref($tax) eq 'SL::DB::Tax';
863   return $tax;
864 };
865
866 1;
867
868 __END__
869
870 =head1 NAME
871
872 SL::Dev::Record - create record objects for testing, with minimal defaults
873
874 =head1 FUNCTIONS
875
876 =head2 C<create_sales_invoice %PARAMS>
877
878 Creates a new sales invoice (table ar, invoice = 1).
879
880 If neither customer nor invoiceitems are passed as params a customer and two
881 parts are created and used for building the invoice.
882
883 Minimal usage example:
884
885   my $invoice = SL::Dev::Record::create_sales_invoice();
886
887 Example with params:
888
889   my $invoice2 = SL::Dev::Record::create_sales_invoice(
890     invnumber   => 777,
891     transdate   => DateTime->today->subtract(days => 7),
892     taxincluded => 1,
893   );
894
895 =head2 C<create_minimal_purchase_invoice %PARAMS>
896
897 Creates a new purchase invoice (table ap, invoice = 1), without any acc_trans
898 entries, amount or tax calculations! (PTC can't deal with purchase invoices yet)
899
900 Should only be used for basic testing of conversions from one record_type to
901 another, and specifically for testing the individual items of conversions.
902
903 For usage examples see C<create_sales_invoice>.
904
905 =head2 C<create_credit_note %PARAMS>
906
907 Create a credit note (sales). Use positive quantities when adding items.
908
909 Example including creation of parts and of credit_note:
910
911   my $part1 = SL::Dev::Part::new_part(   partnumber => 'T4254')->save;
912   my $part2 = SL::Dev::Part::new_service(partnumber => 'Serv1')->save;
913   my $credit_note = SL::Dev::Record::create_credit_note(
914     invnumber    => '34',
915     taxincluded  => 0,
916     invoiceitems => [ SL::Dev::Record::create_invoice_item(part => $part1, qty =>  3, sellprice => 70),
917                       SL::Dev::Record::create_invoice_item(part => $part2, qty => 10, sellprice => 50),
918                     ]
919   );
920
921 =head2 C<create_sales_order %PARAMS>
922
923 Examples:
924
925 Create a sales order and save it directly via rose, without running
926 calculate_prices_and_taxes:
927
928   my $order = SL::Dev::Record::create_sales_order()->save;
929
930 Let create_sales_order run calculate_prices_and_taxes and save:
931
932   my $order = SL::Dev::Record::create_sales_order(save => 1);
933
934
935 Example including creation of part and of sales order:
936
937   my $part1 = SL::Dev::Part::new_part(   partnumber => 'T4254')->save;
938   my $part2 = SL::Dev::Part::new_service(partnumber => 'Serv1')->save;
939   my $order = SL::Dev::Record::create_sales_order(
940     save         => 1,
941     taxincluded  => 0,
942     orderitems => [ SL::Dev::Record::create_order_item(part => $part1, qty =>  3, sellprice => 70),
943                     SL::Dev::Record::create_order_item(part => $part2, qty => 10, sellprice => 50),
944                   ]
945   );
946
947 Example: create 100 orders with the same part for 100 new customers:
948
949   my $part1 = SL::Dev::Part::new_part(partnumber => 'T6256')->save;
950   SL::Dev::Record::create_sales_order(
951     save         => 1,
952     taxincluded  => 0,
953     orderitems => [ SL::Dev::Record::create_order_item(part => $part1, qty => 1, sellprice => 9) ]
954   ) for 1 .. 100;
955
956 =head2 C<create_sales_quotation %PARAMS>
957
958 See C<create_sales_order>
959
960 =head2 C<create_purchase_quotation %PARAMS>
961
962 See comments for C<create_sales_quotation>.
963
964 Example:
965
966   my $purchase_quotation = SL::Dev::Record::create_purchase_quotation(save => 1);
967
968 =head2 C<create_purchase_order %PARAMS>
969
970 See comments for C<create_sales_order>.
971
972 Example:
973
974   my $purchase_order = SL::Dev::Record::create_purchase_order(save => 1);
975
976 =head2 C<create_item %PARAMS>
977
978 Creates an item from a part object that can be added to a record.
979
980 Required params:
981
982   record_type (sales_invoice, sales_order, sales_delivery_order)
983   part        (an SL::DB::Part object)
984
985 Example including creation of part and of invoice:
986
987   my $part    = SL::Dev::Part::new_part(  partnumber  => 'T4254')->save;
988   my $item    = SL::Dev::Record::create_invoice_item(part => $part, qty => 2.5);
989   my $invoice = SL::Dev::Record::create_sales_invoice(
990     taxincluded  => 0,
991     invoiceitems => [ $item ],
992   );
993
994 =head2 C<create_project %PARAMS>
995
996 Creates a default project.
997
998 Minimal example, creating a project with status "running" and type "Standard":
999
1000   my $project = SL::Dev::Record::create_project();
1001
1002   $project = SL::Dev::Record::create_project(
1003     projectnumber => 'p1',
1004     description   => 'Test project',
1005   )
1006
1007 If C<$params{description}> or C<$params{projectnumber}> exists, this will override the
1008 default value 'Test project'.
1009
1010 C<%params> should only contain alterable keys from the object Project.
1011
1012 =head2 C<create_department %PARAMS>
1013
1014 Creates a default department.
1015
1016 Minimal example:
1017
1018   my $department = SL::Dev::Record::create_department();
1019
1020   my $department = SL::Dev::Record::create_department(
1021     description => 'Hawaii',
1022   )
1023
1024 If C<$params{description}> exists, this will override the
1025 default value 'Test Department'.
1026
1027 C<%params> should only contain alterable keys from the object Department.
1028
1029 =head2 C<create_ap_transaction %PARAMS>
1030
1031 Creates a new AP transaction (table ap, invoice = 0), and will try to add as
1032 many defaults as possible.
1033
1034 Possible parameters:
1035  * vendor (SL::DB::Vendor object, defaults to SL::Dev default vendor)
1036  * taxincluded (0 or 1, defaults to 1)
1037  * transdate (DateTime object, defaults to current date)
1038  * bookings (arrayref for the charts to be booked, see examples below)
1039  * amount (to check if final amount matches this amount)
1040  * netamount (to check if final amount matches this amount)
1041  * dec (number of decimals to round to, defaults to 2)
1042  * ap_chart (SL::DB::Chart object, default to accno 1600)
1043  * invnumber (defaults to 'test ap_transaction')
1044  * notes (defaults to 'test ap_transaction')
1045  * globalproject_id
1046
1047 Currently doesn't support exchange rates.
1048
1049 Minimal usage example, creating an AP transaction with a default vendor and
1050 default bookings (telephone, postage):
1051
1052   use SL::Dev::Record qw(create_ap_transaction);
1053   my $invoice = create_ap_transaction();
1054
1055 Create an AP transaction with a specific vendor and specific charts:
1056
1057   my $vendor = SL::Dev::CustomerVendor::new_vendor(name => 'My Vendor')->save;
1058   my $chart_postage   = SL::DB::Manager::Chart->find_by(description => 'Porto');
1059   my $chart_telephone = SL::DB::Manager::Chart->find_by(description => 'Telefon');
1060
1061   my $ap_transaction = create_ap_transaction(
1062     vendor      => $vendor,
1063     invnumber   => 'test invoice taxincluded',
1064     taxincluded => 1,
1065     amount      => 2190, # optional param for checking whether final amount matches
1066     netamount   => 2000, # optional param for checking whether final netamount matches
1067     bookings    => [
1068                      {
1069                        chart  => $chart_postage,
1070                        amount => 1000,
1071                      },
1072                      {
1073                        chart  => $chart_telephone,
1074                        amount => 1190,
1075                      },
1076                    ]
1077   );
1078
1079 Or the same example with tax not included, but an old transdate and old taxrate (16%):
1080
1081   my $ap_transaction = create_ap_transaction(
1082     vendor      => $vendor,
1083     invnumber   => 'test invoice tax not included',
1084     transdate   => DateTime->new(year => 2000, month => 10, day => 1),
1085     taxincluded => 0,
1086     amount      => 2160, # optional param for checking whether final amount matches
1087     netamount   => 2000, # optional param for checking whether final netamount matches
1088     bookings    => [
1089                      {
1090                        chart  => $chart_postage,
1091                        amount => 1000,
1092                      },
1093                      {
1094                        chart  => $chart_telephone,
1095                        amount => 1000,
1096                      },
1097                  ]
1098   );
1099
1100 Don't use the default tax, e.g. postage with 19%:
1101
1102   my $tax_9          = SL::DB::Manager::Tax->find_by(taxkey => 9, rate => 0.19);
1103   my $chart_postage  = SL::DB::Manager::Chart->find_by(description => 'Porto');
1104   my $ap_transaction = create_ap_transaction(
1105     invnumber   => 'postage with tax',
1106     taxincluded => 0,
1107     bookings    => [
1108                      {
1109                        chart  => $chart_postage,
1110                        amount => 1000,
1111                        tax    => $tax_9,
1112                      },
1113                    ],
1114   );
1115
1116 =head2 C<create_ar_transaction %PARAMS>
1117
1118 See C<create_ap_transaction>, except use customer instead of vendor.
1119
1120 =head2 C<create_gl_transaction %PARAMS>
1121
1122 Creates a new GL transaction (table gl), which is basically a wrapper around
1123 SL::DB::GLTransaction->new(...) and add_chart_booking and post, while setting
1124 as many defaults as possible.
1125
1126 Possible parameters:
1127
1128  * taxincluded (0 or 1, defaults to 1)
1129  * transdate (DateTime object, defaults to current date)
1130  * dec (number of decimals to round to, defaults to 2)
1131  * bookings (arrayref for the charts and taxes to be booked, see examples below)
1132
1133 bookings must include a least:
1134
1135  * chart as an SL::DB::Chart object
1136  * credit or debit, as positive numbers
1137  * tax_id, tax (an SL::DB::Tax object) or taxkey (e.g. 9)
1138
1139 Can't be used to create storno transactions.
1140
1141 Minimal usage example, using all the defaults, creating a GL transaction with
1142 travel expenses:
1143
1144   use SL::Dev::Record qw(create_gl_transaction);
1145   $gl_transaction = create_gl_transaction();
1146
1147 Create a GL transaction with a specific charts and taxes (the default taxes for
1148 those charts are used if none are explicitly given in bookings):
1149
1150   my $cash           = SL::DB::Manager::Chart->find_by( description => 'Kasse'          );
1151   my $betriebsbedarf = SL::DB::Manager::Chart->find_by( description => 'Betriebsbedarf' );
1152   $gl_transaction = create_gl_transaction(
1153     reference   => 'betriebsbedarf',
1154     taxincluded => 1,
1155     bookings    => [
1156                      {
1157                        chart  => $betriebsbedarf,
1158                        memo   => 'foo 1',
1159                        source => 'foo 1',
1160                        credit => 119,
1161                      },
1162                      {
1163                        chart  => $betriebsbedarf,
1164                        memo   => 'foo 2',
1165                        source => 'foo 2',
1166                        credit => 119,
1167                      },
1168                      {
1169                        chart  => $cash,
1170                        debit  => 238,
1171                        memo   => 'foo 1+2',
1172                        source => 'foo 1+2',
1173                      },
1174                    ],
1175   );
1176
1177
1178 =head1 BUGS
1179
1180 Nothing here yet.
1181
1182 =head1 AUTHOR
1183
1184 G. Richardson E<lt>grichardson@kivitec.deE<gt>
1185
1186 =cut