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