1 package SL::Dev::Record;
10 create_delivery_order_item
12 create_reclamation_item
14 create_sales_quotation
16 create_sales_delivery_order
19 create_sales_reclamation
21 create_purchase_quotation
23 create_purchase_delivery_order
24 create_minimal_purchase_invoice
25 create_purchase_reclamation
31 our %EXPORT_TAGS = (ALL => \@EXPORT_OK);
34 use SL::DB::InvoiceItem;
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);
42 use SL::Dev::Part qw(new_part);
43 use SL::Dev::CustomerVendor qw(new_vendor new_customer);
45 use SL::DB::ProjectStatus;
46 use SL::DB::ProjectType;
49 use List::Util qw(sum);
51 use SL::Locale::String qw(t8);
53 use SL::Model::Record;
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',
68 sub create_sales_invoice {
71 my $record_type = 'sales_invoice';
72 my $invoiceitems = delete $params{invoiceitems} // _create_two_items($record_type);
73 _check_items($invoiceitems, $record_type);
75 my $customer = delete $params{customer} // new_customer(name => 'Testcustomer')->save;
76 die "illegal customer" unless defined $customer && ref($customer) eq 'SL::DB::Customer';
78 my $invoice = SL::DB::Invoice->new(
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,
93 $invoice->assign_attributes(%params) if %params;
99 sub create_minimal_purchase_invoice {
102 my $record_type = 'purchase_invoice';
103 my $invoiceitems = delete $params{invoiceitems} // _create_two_items($record_type);
104 _check_items($invoiceitems, $record_type);
106 my $vendor = delete $params{vendor} // new_vendor(name => 'Testvendor')->save;
107 die "illegal vendor" unless defined $vendor && ref($vendor) eq 'SL::DB::Vendor';
109 my $invoice = SL::DB::PurchaseInvoice->new(
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,
123 $invoice->assign_attributes(%params) if %params;
125 # TODO: PTC can't deal with purchase invoices, so for now just save via Rose,
126 # no amount/tax/acc_trans calculations.
132 sub create_credit_note {
135 my $record_type = 'credit_note';
136 my $invoiceitems = delete $params{invoiceitems} // _create_two_items($record_type);
137 _check_items($invoiceitems, $record_type);
139 my $customer = delete $params{customer} // new_customer(name => 'Testcustomer')->save;
140 die "illegal customer" unless defined $customer && ref($customer) eq 'SL::DB::Customer';
142 # adjust qty for credit note items
143 $_->qty( $_->qty * -1) foreach @{$invoiceitems};
145 my $invoice = SL::DB::Invoice->new(
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,
160 $invoice->assign_attributes(%params) if %params;
166 sub create_sales_delivery_order {
169 my $record_type = 'sales_delivery_order';
170 my $orderitems = delete $params{orderitems} // _create_two_items($record_type);
171 _check_items($orderitems, $record_type);
173 my $customer = $params{customer} // new_customer(name => 'Testcustomer')->save;
174 die "illegal customer" unless ref($customer) eq 'SL::DB::Customer';
176 my $delivery_order = SL::DB::DeliveryOrder->new(
177 record_type => SALES_DELIVERY_ORDER_TYPE,
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,
189 $delivery_order->assign_attributes(%params) if %params;
190 SL::Model::Record->save($delivery_order);
191 return $delivery_order;
194 sub create_purchase_delivery_order {
197 my $record_type = 'purchase_delivery_order';
198 my $orderitems = delete $params{orderitems} // _create_two_items($record_type);
199 _check_items($orderitems, $record_type);
201 my $vendor = $params{vendor} // new_vendor(name => 'Testvendor')->save;
202 die "illegal vendor" unless ref($vendor) eq 'SL::DB::Vendor';
204 my $delivery_order = SL::DB::DeliveryOrder->new(
205 record_type => PURCHASE_DELIVERY_ORDER_TYPE,
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,
217 $delivery_order->assign_attributes(%params) if %params;
218 SL::Model::Record->save($delivery_order);
219 return $delivery_order;
222 sub create_sales_quotation {
224 $params{type} = SALES_QUOTATION_TYPE();
225 _create_sales_order_or_quotation(%params);
228 sub create_sales_order {
230 $params{type} = SALES_ORDER_TYPE();
231 _create_sales_order_or_quotation(%params);
234 sub create_purchase_quotation {
236 $params{type} = REQUEST_QUOTATION_TYPE();
237 # TODO: set a with reqdate
238 _create_purchase_order_or_quotation(%params);
241 sub create_purchase_order {
243 $params{type} = PURCHASE_ORDER_TYPE();
244 _create_purchase_order_or_quotation(%params);
247 sub create_sales_reclamation {
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);
254 my $save = delete $params{save} // 0;
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';
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,
267 reclamation_items => $reclamation_items,
269 $reclamation->assign_attributes(%params) if %params;
272 SL::Model::Record->save($reclamation);
277 sub create_purchase_reclamation {
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);
284 my $save = delete $params{save} // 0;
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';
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,
297 reclamation_items => $reclamation_items,
299 $reclamation->assign_attributes(%params) if %params;
302 SL::Model::Record->save($reclamation);
308 my ($items, $record_type) = @_;
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.";
315 sub create_invoice_item {
318 return _create_item(record_type => 'sales_invoice', %params);
321 sub create_order_item {
324 return _create_item(record_type => 'sales_order', %params);
327 sub create_delivery_order_item {
330 return _create_item(record_type => 'sales_delivery_order', %params);
333 sub create_reclamation_item {
336 # record_type can be sales or purchase; make sure one is set
337 return _create_item(record_type => 'sales_reclamation', %params);
343 my $record_type = delete($params{record_type});
344 my $part = delete($params{part});
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';
349 my ($sellprice, $lastcost);
351 if ( $record_type =~ /^sales/ ) {
352 $sellprice = delete $params{sellprice} // $part->sellprice;
353 $lastcost = delete $params{lastcost} // $part->lastcost;
355 $sellprice = delete $params{sellprice} // $part->lastcost;
356 $lastcost = delete $params{lastcost} // 0; # $part->lastcost;
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,
365 qty => $params{qty} || 5,
367 $item->assign_attributes(%params) if %params;
371 sub _create_two_items {
372 my ($record_type) = @_;
374 my $part1 = new_part(description => 'Testpart 1',
377 my $part2 = new_part(description => 'Testpart 2',
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 ];
387 my $project = SL::DB::Project->new(
388 projectnumber => delete $params{projectnumber} // 1,
389 description => delete $params{description} // "Test project",
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,
395 $project->assign_attributes(%params) if %params;
399 sub create_department {
402 my $department = SL::DB::Department->new(
403 'description' => delete $params{description} // 'Test Department',
406 $department->assign_attributes(%params) if %params;
411 sub create_ap_transaction {
414 my $vendor = delete $params{vendor};
416 die "vendor missing or not a SL::DB::Vendor object" unless ref($vendor) eq 'SL::DB::Vendor';
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;
422 my $taxincluded = $params{taxincluded} // 1;
423 delete $params{taxincluded};
425 my $bookings = delete $params{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');
432 chart => $chart_postage,
436 chart => $chart_telephone,
437 amount => $taxincluded ? 1190 : 1000,
443 my $project_id = delete $params{globalproject_id};
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};
449 my $dec = delete $params{dec} // 2;
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';
455 my $gldate = delete $params{gldate} // $today;
456 die "gldate hat to be DateTime object" unless ref($gldate) eq 'DateTime';
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';
461 my $ap_transaction = SL::DB::PurchaseInvoice->new(
462 vendor_id => $vendor->id,
465 globalproject_id => $project_id,
466 invnumber => delete $params{invnumber} // 'test ap_transaction',
467 notes => delete $params{notes} // 'test ap_transaction',
468 transdate => $transdate,
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,
476 # assign any parameters that weren't explicitly handled above, e.g. itime
477 $ap_transaction->assign_attributes(%params) if %params;
479 foreach my $booking ( @{$bookings} ) {
480 my $chart = delete $booking->{chart};
481 die "illegal chart" unless ref($chart) eq 'SL::DB::Chart';
483 my $tax = _transaction_tax_helper($booking, $chart, $transdate); # will die if tax can't be found
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
489 project_id => $booking->{project_id},
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",
498 # )) foreach @{$ap_transaction->transactions};
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));
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;
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;
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;
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;
527 sub create_ar_transaction {
530 my $customer = delete $params{customer};
532 die "customer missing or not a SL::DB::Customer object" unless ref($customer) eq 'SL::DB::Customer';
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;
538 my $taxincluded = $params{taxincluded} // 1;
539 delete $params{taxincluded};
541 my $bookings = delete $params{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');
550 amount => $taxincluded ? 119 : 100,
554 amount => $taxincluded ? 107 : 100,
564 my $project_id = delete $params{globalproject_id};
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};
570 my $dec = delete $params{dec} // 2;
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';
576 my $gldate = delete $params{gldate} // $today;
577 die "gldate hat to be DateTime object" unless ref($gldate) eq 'DateTime';
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';
582 my $ar_transaction = SL::DB::Invoice->new(
583 customer_id => $customer->id,
586 globalproject_id => $project_id,
587 invnumber => delete $params{invnumber} // 'test ar_transaction',
588 notes => delete $params{notes} // 'test ar_transaction',
589 transdate => $transdate,
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,
597 # assign any parameters that weren't explicitly handled above, e.g. itime
598 $ar_transaction->assign_attributes(%params) if %params;
600 foreach my $booking ( @{$bookings} ) {
601 my $chart = delete $booking->{chart};
602 die "illegal chart" unless ref($chart) eq 'SL::DB::Chart';
604 my $tax = _transaction_tax_helper($booking, $chart, $transdate); # will die if tax can't be found
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
610 project_id => $booking->{project_id},
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",
619 # )) foreach @{$ar_transaction->transactions};
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));
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;
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;
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;
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;
648 sub create_gl_transaction {
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;
655 my $taxincluded = defined $params{taxincluded} ? $params{taxincluded} : 1;
657 my $today = DateTime->today_local;
658 my $transdate = delete $params{transdate} // $today;
659 my $gldate = delete $params{gldate} // $today;
661 my $reference = delete $params{reference} // 'reference';
662 my $description = delete $params{description} // 'description';
664 my $department_id = delete $params{department_id};
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
674 $reference = 'Reise';
675 $description = 'Reise';
679 chart => $expense_chart, # has default tax of 19%
684 chart => $cash_chart,
691 my $gl_transaction = SL::DB::GLTransaction->new(
692 reference => $reference,
693 description => $description,
694 transdate => $transdate,
696 taxincluded => $taxincluded,
698 ob_transaction => $ob_transaction,
699 cb_transaction => $cb_transaction,
704 # assign any parameters that weren't explicitly handled above, e.g. itime
705 $gl_transaction->assign_attributes(%params) if %params;
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
715 foreach my $booking ( @{$bookings} ) {
716 my $chart = delete $booking->{chart};
717 die "illegal chart" unless ref($chart) eq 'SL::DB::Chart';
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});
724 my $tax = _transaction_tax_helper($booking, $chart, $transdate); # will die if tax can't be found
726 $gl_transaction->add_chart_booking(
728 debit => $booking->{debit},
729 credit => $booking->{credit},
731 source => $booking->{source} // '',
732 memo => $booking->{memo} // '',
733 project_id => $booking->{project_id}
738 $gl_transaction->post;
740 return $gl_transaction;
743 sub _create_sales_order_or_quotation {
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();
749 my $orderitems = delete $params{orderitems} // _create_two_items($record_type);
750 _check_items($orderitems, $record_type);
752 my $save = delete $params{save} // 0;
754 my $customer = $params{customer} // new_customer(name => 'Testcustomer')->save;
755 die "illegal customer" unless ref($customer) eq 'SL::DB::Customer';
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,
768 $record->assign_attributes(%params) if %params;
771 SL::Model::Record->save($record);
776 sub _create_purchase_order_or_quotation {
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);
784 my $save = delete $params{save} // 0;
786 my $vendor = $params{vendor} // new_vendor(name => 'Testvendor')->save;
787 die "illegal vendor" unless ref($vendor) eq 'SL::DB::Vendor';
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,
797 orderitems => $orderitems,
799 $record->assign_attributes(%params) if %params;
802 SL::Model::Record->save($record);
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
813 my ($booking, $chart, $transdate) = @_;
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';
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.
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
841 my $taxkey = SL::DB::Manager::TaxKey->get_first(
842 query => [ and => [ chart_id => $chart->id,
843 startdate => { le => $transdate },
844 taxkey => $booking->{taxkey}
847 sort_by => "startdate DESC",
849 with_objects => [ qw(tax) ],
851 die sprintf("Chart %s doesn't have a taxkey chart configured for taxkey %s", $chart->accno, $booking->{taxkey})
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));
862 die "no tax" unless $tax && ref($tax) eq 'SL::DB::Tax';
872 SL::Dev::Record - create record objects for testing, with minimal defaults
876 =head2 C<create_sales_invoice %PARAMS>
878 Creates a new sales invoice (table ar, invoice = 1).
880 If neither customer nor invoiceitems are passed as params a customer and two
881 parts are created and used for building the invoice.
883 Minimal usage example:
885 my $invoice = SL::Dev::Record::create_sales_invoice();
889 my $invoice2 = SL::Dev::Record::create_sales_invoice(
891 transdate => DateTime->today->subtract(days => 7),
895 =head2 C<create_minimal_purchase_invoice %PARAMS>
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)
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.
903 For usage examples see C<create_sales_invoice>.
905 =head2 C<create_credit_note %PARAMS>
907 Create a credit note (sales). Use positive quantities when adding items.
909 Example including creation of parts and of credit_note:
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(
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),
921 =head2 C<create_sales_order %PARAMS>
925 Create a sales order and save it directly via rose, without running
926 calculate_prices_and_taxes:
928 my $order = SL::Dev::Record::create_sales_order()->save;
930 Let create_sales_order run calculate_prices_and_taxes and save:
932 my $order = SL::Dev::Record::create_sales_order(save => 1);
935 Example including creation of part and of sales order:
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(
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),
947 Example: create 100 orders with the same part for 100 new customers:
949 my $part1 = SL::Dev::Part::new_part(partnumber => 'T6256')->save;
950 SL::Dev::Record::create_sales_order(
953 orderitems => [ SL::Dev::Record::create_order_item(part => $part1, qty => 1, sellprice => 9) ]
956 =head2 C<create_sales_quotation %PARAMS>
958 See C<create_sales_order>
960 =head2 C<create_purchase_quotation %PARAMS>
962 See comments for C<create_sales_quotation>.
966 my $purchase_quotation = SL::Dev::Record::create_purchase_quotation(save => 1);
968 =head2 C<create_purchase_order %PARAMS>
970 See comments for C<create_sales_order>.
974 my $purchase_order = SL::Dev::Record::create_purchase_order(save => 1);
976 =head2 C<create_item %PARAMS>
978 Creates an item from a part object that can be added to a record.
982 record_type (sales_invoice, sales_order, sales_delivery_order)
983 part (an SL::DB::Part object)
985 Example including creation of part and of invoice:
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(
991 invoiceitems => [ $item ],
994 =head2 C<create_project %PARAMS>
996 Creates a default project.
998 Minimal example, creating a project with status "running" and type "Standard":
1000 my $project = SL::Dev::Record::create_project();
1002 $project = SL::Dev::Record::create_project(
1003 projectnumber => 'p1',
1004 description => 'Test project',
1007 If C<$params{description}> or C<$params{projectnumber}> exists, this will override the
1008 default value 'Test project'.
1010 C<%params> should only contain alterable keys from the object Project.
1012 =head2 C<create_department %PARAMS>
1014 Creates a default department.
1018 my $department = SL::Dev::Record::create_department();
1020 my $department = SL::Dev::Record::create_department(
1021 description => 'Hawaii',
1024 If C<$params{description}> exists, this will override the
1025 default value 'Test Department'.
1027 C<%params> should only contain alterable keys from the object Department.
1029 =head2 C<create_ap_transaction %PARAMS>
1031 Creates a new AP transaction (table ap, invoice = 0), and will try to add as
1032 many defaults as possible.
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')
1047 Currently doesn't support exchange rates.
1049 Minimal usage example, creating an AP transaction with a default vendor and
1050 default bookings (telephone, postage):
1052 use SL::Dev::Record qw(create_ap_transaction);
1053 my $invoice = create_ap_transaction();
1055 Create an AP transaction with a specific vendor and specific charts:
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');
1061 my $ap_transaction = create_ap_transaction(
1063 invnumber => 'test invoice taxincluded',
1065 amount => 2190, # optional param for checking whether final amount matches
1066 netamount => 2000, # optional param for checking whether final netamount matches
1069 chart => $chart_postage,
1073 chart => $chart_telephone,
1079 Or the same example with tax not included, but an old transdate and old taxrate (16%):
1081 my $ap_transaction = create_ap_transaction(
1083 invnumber => 'test invoice tax not included',
1084 transdate => DateTime->new(year => 2000, month => 10, day => 1),
1086 amount => 2160, # optional param for checking whether final amount matches
1087 netamount => 2000, # optional param for checking whether final netamount matches
1090 chart => $chart_postage,
1094 chart => $chart_telephone,
1100 Don't use the default tax, e.g. postage with 19%:
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',
1109 chart => $chart_postage,
1116 =head2 C<create_ar_transaction %PARAMS>
1118 See C<create_ap_transaction>, except use customer instead of vendor.
1120 =head2 C<create_gl_transaction %PARAMS>
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.
1126 Possible parameters:
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)
1133 bookings must include a least:
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)
1139 Can't be used to create storno transactions.
1141 Minimal usage example, using all the defaults, creating a GL transaction with
1144 use SL::Dev::Record qw(create_gl_transaction);
1145 $gl_transaction = create_gl_transaction();
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):
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',
1157 chart => $betriebsbedarf,
1163 chart => $betriebsbedarf,
1172 source => 'foo 1+2',
1184 G. Richardson E<lt>grichardson@kivitec.deE<gt>