1 package SL::Dev::Record;
5 our @EXPORT_OK = qw(create_invoice_item
11 create_delivery_order_item
12 create_sales_delivery_order
13 create_purchase_delivery_order
14 create_project create_department
19 our %EXPORT_TAGS = (ALL => \@EXPORT_OK);
22 use SL::DB::InvoiceItem;
24 use SL::Dev::Part qw(new_part);
25 use SL::Dev::CustomerVendor qw(new_vendor new_customer);
27 use SL::DB::ProjectStatus;
28 use SL::DB::ProjectType;
31 use List::Util qw(sum);
33 use SL::Locale::String qw(t8);
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',
43 sub create_sales_invoice {
46 my $record_type = 'sales_invoice';
47 my $invoiceitems = delete $params{invoiceitems} // _create_two_items($record_type);
48 _check_items($invoiceitems, $record_type);
50 my $customer = delete $params{customer} // new_customer(name => 'Testcustomer')->save;
51 die "illegal customer" unless defined $customer && ref($customer) eq 'SL::DB::Customer';
53 my $invoice = SL::DB::Invoice->new(
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,
68 $invoice->assign_attributes(%params) if %params;
74 sub create_credit_note {
77 my $record_type = 'credit_note';
78 my $invoiceitems = delete $params{invoiceitems} // _create_two_items($record_type);
79 _check_items($invoiceitems, $record_type);
81 my $customer = delete $params{customer} // new_customer(name => 'Testcustomer')->save;
82 die "illegal customer" unless defined $customer && ref($customer) eq 'SL::DB::Customer';
84 # adjust qty for credit note items
85 $_->qty( $_->qty * -1) foreach @{$invoiceitems};
87 my $invoice = SL::DB::Invoice->new(
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,
102 $invoice->assign_attributes(%params) if %params;
108 sub create_sales_delivery_order {
111 my $record_type = 'sales_delivery_order';
112 my $orderitems = delete $params{orderitems} // _create_two_items($record_type);
113 _check_items($orderitems, $record_type);
115 my $customer = $params{customer} // new_customer(name => 'Testcustomer')->save;
116 die "illegal customer" unless ref($customer) eq 'SL::DB::Customer';
118 my $delivery_order = SL::DB::DeliveryOrder->new(
119 'is_sales' => 'true',
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,
131 $delivery_order->assign_attributes(%params) if %params;
132 $delivery_order->save;
133 return $delivery_order;
136 sub create_purchase_delivery_order {
139 my $record_type = 'purchase_delivery_order';
140 my $orderitems = delete $params{orderitems} // _create_two_items($record_type);
141 _check_items($orderitems, $record_type);
143 my $vendor = $params{vendor} // new_vendor(name => 'Testvendor')->save;
144 die "illegal customer" unless ref($vendor) eq 'SL::DB::Vendor';
146 my $delivery_order = SL::DB::DeliveryOrder->new(
147 'is_sales' => 'false',
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,
159 $delivery_order->assign_attributes(%params) if %params;
160 $delivery_order->save;
161 return $delivery_order;
164 sub create_sales_order {
167 my $record_type = 'sales_order';
168 my $orderitems = delete $params{orderitems} // _create_two_items($record_type);
169 _check_items($orderitems, $record_type);
171 my $save = delete $params{save} // 0;
173 my $customer = $params{customer} // new_customer(name => 'Testcustomer')->save;
174 die "illegal customer" unless ref($customer) eq 'SL::DB::Customer';
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,
186 $order->assign_attributes(%params) if %params;
189 $order->calculate_prices_and_taxes;
195 sub create_purchase_order {
198 my $record_type = 'purchase_order';
199 my $orderitems = delete $params{orderitems} // _create_two_items($record_type);
200 _check_items($orderitems, $record_type);
202 my $save = delete $params{save} // 0;
204 my $vendor = $params{vendor} // new_vendor(name => 'Testvendor')->save;
205 die "illegal vendor" unless ref($vendor) eq 'SL::DB::Vendor';
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,
214 orderitems => $orderitems,
216 $order->assign_attributes(%params) if %params;
219 $order->calculate_prices_and_taxes; # not tested for purchase orders
226 my ($items, $record_type) = @_;
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.";
233 sub create_invoice_item {
236 return _create_item(record_type => 'sales_invoice', %params);
239 sub create_order_item {
242 return _create_item(record_type => 'sales_order', %params);
245 sub create_delivery_order_item {
248 return _create_item(record_type => 'sales_delivery_order', %params);
254 my $record_type = delete($params{record_type});
255 my $part = delete($params{part});
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';
260 my ($sellprice, $lastcost);
262 if ( $record_type =~ /^sales/ ) {
263 $sellprice = delete $params{sellprice} // $part->sellprice;
264 $lastcost = delete $params{lastcost} // $part->lastcost;
266 $sellprice = delete $params{sellprice} // $part->lastcost;
267 $lastcost = delete $params{lastcost} // 0; # $part->lastcost;
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,
276 qty => $params{qty} || 5,
278 $item->assign_attributes(%params) if %params;
282 sub _create_two_items {
283 my ($record_type) = @_;
285 my $part1 = new_part(description => 'Testpart 1',
288 my $part2 = new_part(description => 'Testpart 2',
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 ];
298 my $project = SL::DB::Project->new(
299 projectnumber => delete $params{projectnumber} // 1,
300 description => delete $params{description} // "Test project",
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,
306 $project->assign_attributes(%params) if %params;
310 sub create_department {
313 my $department = SL::DB::Department->new(
314 'description' => delete $params{description} // 'Test Department',
317 $department->assign_attributes(%params) if %params;
322 sub create_ap_transaction {
325 my $vendor = delete $params{vendor};
327 die "vendor missing or not a SL::DB::Vendor object" unless ref($vendor) eq 'SL::DB::Vendor';
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;
333 my $taxincluded = $params{taxincluded} // 1;
334 delete $params{taxincluded};
336 my $bookings = delete $params{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');
343 chart => $chart_postage,
347 chart => $chart_telephone,
348 amount => $taxincluded ? 1190 : 1000,
354 my $project_id = delete $params{globalproject_id};
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};
360 my $dec = delete $params{dec} // 2;
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';
366 my $gldate = delete $params{gldate} // $today;
367 die "gldate hat to be DateTime object" unless ref($gldate) eq 'DateTime';
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';
372 my $ap_transaction = SL::DB::PurchaseInvoice->new(
373 vendor_id => $vendor->id,
376 globalproject_id => $project_id,
377 invnumber => delete $params{invnumber} // 'test ap_transaction',
378 notes => delete $params{notes} // 'test ap_transaction',
379 transdate => $transdate,
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,
387 # $ap_transaction->assign_attributes(%params) if %params;
389 foreach my $booking ( @{$bookings} ) {
390 my $chart = delete $booking->{chart};
391 die "illegal chart" unless ref($chart) eq 'SL::DB::Chart';
393 my $tax = _transaction_tax_helper($booking, $chart, $transdate); # will die if tax can't be found
395 $ap_transaction->add_ap_amount_row(
396 amount => $booking->{amount}, # add_ap_amount_row expects the user input amount, does its own calculate_tax
399 project_id => $booking->{project_id},
403 my $acc_trans_sum = sum map { $_->amount } grep { $_->chart_link =~ 'AP_amount' } @{$ap_transaction->transactions};
404 # $main::lxdebug->message(0, sprintf("accno: %s amount: %s chart_link: %s\n",
408 # )) foreach @{$ap_transaction->transactions};
410 # determine netamount and amount from the transactions that were added via bookings
411 $ap_transaction->netamount( -1 * sum map { $_->amount } grep { $_->chart_link =~ 'AP_amount' } @{$ap_transaction->transactions} );
412 # $main::lxdebug->message(0, sprintf('found netamount %s', $ap_transaction->netamount));
414 my $taxamount = -1 * sum map { $_->amount } grep { $_->chart_link =~ /tax/ } @{$ap_transaction->transactions};
415 $ap_transaction->amount( $ap_transaction->netamount + $taxamount );
416 # additional check, add up all transactions before AP-transaction is added
417 my $refamount = -1 * sum map { $_->amount } @{$ap_transaction->transactions};
418 die "refamount = $refamount, ap_transaction->amount = " . $ap_transaction->amount unless $refamount == $ap_transaction->amount;
420 # if amount or netamount were passed as params, check if the values are still
421 # the same after recalculating them from the acc_trans entries
422 if (defined $expected_amount) {
423 die "amount doesn't match acc_trans amounts: $expected_amount != " . $ap_transaction->amount unless $expected_amount == $ap_transaction->amount;
425 if (defined $expected_netamount) {
426 die "netamount doesn't match acc_trans netamounts: $expected_netamount != " . $ap_transaction->netamount unless $expected_netamount == $ap_transaction->netamount;
429 $ap_transaction->create_ap_row(chart => $ap_chart);
430 $ap_transaction->save;
431 # $main::lxdebug->message(0, sprintf("created ap_transaction with invnumber %s and trans_id %s",
432 # $ap_transaction->invnumber,
433 # $ap_transaction->id));
434 return $ap_transaction;
437 sub create_ar_transaction {
440 my $customer = delete $params{customer};
442 die "customer missing or not a SL::DB::Customer object" unless ref($customer) eq 'SL::DB::Customer';
444 # use default SL/Dev vendor if it exists, or create a new one
445 $customer = SL::DB::Manager::Customer->find_by(name => 'Testkunde') // new_customer->save;
448 my $taxincluded = $params{taxincluded} // 1;
449 delete $params{taxincluded};
451 my $bookings = delete $params{bookings};
453 unless ( $bookings ) {
454 my $chart_19 = SL::DB::Manager::Chart->find_by(accno => '8400');
455 my $chart_7 = SL::DB::Manager::Chart->find_by(accno => '8300');
456 my $chart_0 = SL::DB::Manager::Chart->find_by(accno => '8200');
460 amount => $taxincluded ? 119 : 100,
464 amount => $taxincluded ? 107 : 100,
474 my $project_id = delete $params{globalproject_id};
476 # if amount or netamount are given, then it compares them to the final values, and dies if they don't match
477 my $expected_amount = delete $params{amount};
478 my $expected_netamount = delete $params{netamount};
480 my $dec = delete $params{dec} // 2;
482 my $today = DateTime->today_local;
483 my $transdate = delete $params{transdate} // $today;
484 die "transdate hat to be DateTime object" unless ref($transdate) eq 'DateTime';
486 my $gldate = delete $params{gldate} // $today;
487 die "gldate hat to be DateTime object" unless ref($gldate) eq 'DateTime';
489 my $ar_chart = delete $params{ar_chart} // SL::DB::Manager::Chart->find_by( accno => '1400' );
490 die "no ar_chart found or not an AR chart" unless $ar_chart and $ar_chart->link eq 'AR';
492 my $ar_transaction = SL::DB::Invoice->new(
493 customer_id => $customer->id,
496 globalproject_id => $project_id,
497 invnumber => delete $params{invnumber} // 'test ar_transaction',
498 notes => delete $params{notes} // 'test ar_transaction',
499 transdate => $transdate,
501 taxincluded => $taxincluded,
502 taxzone_id => $customer->taxzone_id, # taxzone_id shouldn't have any effect on ar transactions
503 currency_id => $::instance_conf->get_currency_id,
504 type => undef, # isn't set for ar
505 employee_id => SL::DB::Manager::Employee->current->id,
507 # $ar_transaction->assign_attributes(%params) if %params;
509 foreach my $booking ( @{$bookings} ) {
510 my $chart = delete $booking->{chart};
511 die "illegal chart" unless ref($chart) eq 'SL::DB::Chart';
513 my $tax = _transaction_tax_helper($booking, $chart, $transdate); # will die if tax can't be found
515 $ar_transaction->add_ar_amount_row(
516 amount => $booking->{amount}, # add_ar_amount_row expects the user input amount, does its own calculate_tax
519 project_id => $booking->{project_id},
523 my $acc_trans_sum = sum map { $_->amount } grep { $_->chart_link =~ 'AR_amount' } @{$ar_transaction->transactions};
524 # $main::lxdebug->message(0, sprintf("accno: %s amount: %s chart_link: %s\n",
528 # )) foreach @{$ar_transaction->transactions};
530 # determine netamount and amount from the transactions that were added via bookings
531 $ar_transaction->netamount( 1 * sum map { $_->amount } grep { $_->chart_link =~ 'AR_amount' } @{$ar_transaction->transactions} );
532 # $main::lxdebug->message(0, sprintf('found netamount %s', $ar_transaction->netamount));
534 my $taxamount = 1 * sum map { $_->amount } grep { $_->chart_link =~ /tax/ } @{$ar_transaction->transactions};
535 $ar_transaction->amount( $ar_transaction->netamount + $taxamount );
536 # additional check, add up all transactions before AP-transaction is added
537 my $refamount = 1 * sum map { $_->amount } @{$ar_transaction->transactions};
538 die "refamount = $refamount, ar_transaction->amount = " . $ar_transaction->amount unless $refamount == $ar_transaction->amount;
540 # if amount or netamount were passed as params, check if the values are still
541 # the same after recalculating them from the acc_trans entries
542 if (defined $expected_amount) {
543 die "amount doesn't match acc_trans amounts: $expected_amount != " . $ar_transaction->amount unless $expected_amount == $ar_transaction->amount;
545 if (defined $expected_netamount) {
546 die "netamount doesn't match acc_trans netamounts: $expected_netamount != " . $ar_transaction->netamount unless $expected_netamount == $ar_transaction->netamount;
549 $ar_transaction->create_ar_row(chart => $ar_chart);
550 $ar_transaction->save;
551 # $main::lxdebug->message(0, sprintf("created ar_transaction with invnumber %s and trans_id %s",
552 # $ar_transaction->invnumber,
553 # $ar_transaction->id));
554 return $ar_transaction;
557 sub create_gl_transaction {
560 my $ob_transaction = delete $params{ob_transaction} // 0;
561 my $cb_transaction = delete $params{cb_transaction} // 0;
562 my $dec = delete $params{rec} // 2;
564 my $taxincluded = defined $params{taxincluded} ? $params{taxincluded} : 1;
566 my $today = DateTime->today_local;
567 my $transdate = delete $params{transdate} // $today;
568 my $gldate = delete $params{gldate} // $today;
570 my $reference = delete $params{reference} // 'reference';
571 my $description = delete $params{description} // 'description';
573 my $department_id = delete $params{department_id};
575 my $bookings = delete $params{bookings};
576 unless ( $bookings && scalar @{$bookings} ) {
577 # default bookings if left empty
578 my $expense_chart = SL::DB::Manager::Chart->find_by(accno => '4660') or die "Can't find expense chart 4660\n"; # Reisekosten
579 my $cash_chart = SL::DB::Manager::Chart->find_by(accno => '1000') or die "Can't find cash chart 1000\n"; # Kasse
583 $reference = 'Reise';
584 $description = 'Reise';
588 chart => $expense_chart, # has default tax of 19%
593 chart => $cash_chart,
600 my $gl_transaction = SL::DB::GLTransaction->new(
601 reference => $reference,
602 description => $description,
603 transdate => $transdate,
605 taxincluded => $taxincluded,
607 ob_transaction => $ob_transaction,
608 cb_transaction => $cb_transaction,
615 if ( scalar @{$bookings} ) {
616 # there are several ways of determining the tax:
617 # * tax_id : fetches SL::DB::Tax object via id (as used in dropdown in interface)
618 # * tax : SL::DB::Tax object (where $tax->id = tax_id)
619 # * taxkey : tax is determined from startdate
620 # * none of the above defined: use the default tax for that chart
622 foreach my $booking ( @{$bookings} ) {
623 my $chart = delete $booking->{chart};
624 die "illegal chart" unless ref($chart) eq 'SL::DB::Chart';
626 die t8('Empty transaction!')
627 unless $booking->{debit} or $booking->{credit}; # must exist and not be 0
628 die t8('Cannot post transaction with a debit and credit entry for the same account!')
629 if defined($booking->{debit}) and defined($booking->{credit});
631 my $tax = _transaction_tax_helper($booking, $chart, $transdate); # will die if tax can't be found
633 $gl_transaction->add_chart_booking(
635 debit => $booking->{debit},
636 credit => $booking->{credit},
638 source => $booking->{source} // '',
639 memo => $booking->{memo} // '',
640 project_id => $booking->{project_id}
645 $gl_transaction->post;
647 return $gl_transaction;
650 sub _transaction_tax_helper {
651 # checks for hash-entries with key tax, tax_id or taxkey
652 # returns an SL::DB::Tax object
653 # can be used for booking hashref in ar_transaction, ap_transaction and gl_transaction
654 # will modify hashref, e.g. removing taxkey if tax_id was also supplied
656 my ($booking, $chart, $transdate) = @_;
658 die "_transaction_tax_helper: chart missing" unless $chart && ref($chart) eq 'SL::DB::Chart';
659 die "_transaction_tax_helper: transdate missing" unless $transdate && ref($transdate) eq 'DateTime';
663 if ( defined $booking->{tax_id} ) { # tax_id may be 0
664 delete $booking->{taxkey}; # ignore any taxkeys that may have been added, tax_id has precedence
665 $tax = SL::DB::Tax->new(id => $booking->{tax_id})->load( with => [ 'chart' ] );
666 } elsif ( $booking->{tax} ) {
667 die "illegal tax entry" unless ref($booking->{tax}) eq 'SL::DB::Tax';
668 $tax = $booking->{tax};
669 } elsif ( defined $booking->{taxkey} ) {
670 # If a taxkey is given, find the taxkey entry for that chart that
671 # matches the stored taxkey and with the correct transdate. This will only work
672 # if kivitendo has that taxkey configured for that chart, i.e. it should barf if
673 # e.g. the bank chart is called with taxkey 3.
678 # where taxkey_id = 3
679 # and chart_id = (select id from chart where accno = '8400')
680 # and startdate <= '2018-01-01'
681 # order by startdate desc
684 my $taxkey = SL::DB::Manager::TaxKey->get_first(
685 query => [ and => [ chart_id => $chart->id,
686 startdate => { le => $transdate },
687 taxkey => $booking->{taxkey}
690 sort_by => "startdate DESC",
692 with_objects => [ qw(tax) ],
694 die sprintf("Chart %s doesn't have a taxkey chart configured for taxkey %s", $chart->accno, $booking->{taxkey})
699 # use default tax for that chart if neither tax_id, tax or taxkey were defined
700 my $active_taxkey = $chart->get_active_taxkey($transdate);
701 $tax = $active_taxkey->tax;
702 # $main::lxdebug->message(0, sprintf("found default taxrate %s for chart %s", $tax->rate, $chart->displayable_name));
705 die "no tax" unless $tax && ref($tax) eq 'SL::DB::Tax';
715 SL::Dev::Record - create record objects for testing, with minimal defaults
719 =head2 C<create_sales_invoice %PARAMS>
721 Creates a new sales invoice (table ar, invoice = 1).
723 If neither customer nor invoiceitems are passed as params a customer and two
724 parts are created and used for building the invoice.
726 Minimal usage example:
728 my $invoice = SL::Dev::Record::create_sales_invoice();
732 my $invoice2 = SL::Dev::Record::create_sales_invoice(
734 transdate => DateTime->today->subtract(days => 7),
738 =head2 C<create_credit_note %PARAMS>
740 Create a credit note (sales). Use positive quantities when adding items.
742 Example including creation of parts and of credit_note:
744 my $part1 = SL::Dev::Part::new_part( partnumber => 'T4254')->save;
745 my $part2 = SL::Dev::Part::new_service(partnumber => 'Serv1')->save;
746 my $credit_note = SL::Dev::Record::create_credit_note(
749 invoiceitems => [ SL::Dev::Record::create_invoice_item(part => $part1, qty => 3, sellprice => 70),
750 SL::Dev::Record::create_invoice_item(part => $part2, qty => 10, sellprice => 50),
754 =head2 C<create_sales_order %PARAMS>
758 Create a sales order and save it directly via rose, without running
759 calculate_prices_and_taxes:
761 my $order = SL::Dev::Record::create_sales_order()->save;
763 Let create_sales_order run calculate_prices_and_taxes and save:
765 my $order = SL::Dev::Record::create_sales_order(save => 1);
768 Example including creation of part and of sales order:
770 my $part1 = SL::Dev::Part::new_part( partnumber => 'T4254')->save;
771 my $part2 = SL::Dev::Part::new_service(partnumber => 'Serv1')->save;
772 my $order = SL::Dev::Record::create_sales_order(
775 orderitems => [ SL::Dev::Record::create_order_item(part => $part1, qty => 3, sellprice => 70),
776 SL::Dev::Record::create_order_item(part => $part2, qty => 10, sellprice => 50),
780 Example: create 100 orders with the same part for 100 new customers:
782 my $part1 = SL::Dev::Part::new_part(partnumber => 'T6256')->save;
783 SL::Dev::Record::create_sales_order(
786 orderitems => [ SL::Dev::Record::create_order_item(part => $part1, qty => 1, sellprice => 9) ]
789 =head2 C<create_purchase_order %PARAMS>
791 See comments for C<create_sales_order>.
795 my $purchase_order = SL::Dev::Record::create_purchase_order(save => 1);
798 =head2 C<create_item %PARAMS>
800 Creates an item from a part object that can be added to a record.
804 record_type (sales_invoice, sales_order, sales_delivery_order)
805 part (an SL::DB::Part object)
807 Example including creation of part and of invoice:
809 my $part = SL::Dev::Part::new_part( partnumber => 'T4254')->save;
810 my $item = SL::Dev::Record::create_invoice_item(part => $part, qty => 2.5);
811 my $invoice = SL::Dev::Record::create_sales_invoice(
813 invoiceitems => [ $item ],
816 =head2 C<create_project %PARAMS>
818 Creates a default project.
820 Minimal example, creating a project with status "running" and type "Standard":
822 my $project = SL::Dev::Record::create_project();
824 $project = SL::Dev::Record::create_project(
825 projectnumber => 'p1',
826 description => 'Test project',
829 If C<$params{description}> or C<$params{projectnumber}> exists, this will override the
830 default value 'Test project'.
832 C<%params> should only contain alterable keys from the object Project.
834 =head2 C<create_department %PARAMS>
836 Creates a default department.
840 my $department = SL::Dev::Record::create_department();
842 my $department = SL::Dev::Record::create_department(
843 description => 'Hawaii',
846 If C<$params{description}> exists, this will override the
847 default value 'Test Department'.
849 C<%params> should only contain alterable keys from the object Department.
851 =head2 C<create_ap_transaction %PARAMS>
853 Creates a new AP transaction (table ap, invoice = 0), and will try to add as
854 many defaults as possible.
857 * vendor (SL::DB::Vendor object, defaults to SL::Dev default vendor)
858 * taxincluded (0 or 1, defaults to 1)
859 * transdate (DateTime object, defaults to current date)
860 * bookings (arrayref for the charts to be booked, see examples below)
861 * amount (to check if final amount matches this amount)
862 * netamount (to check if final amount matches this amount)
863 * dec (number of decimals to round to, defaults to 2)
864 * ap_chart (SL::DB::Chart object, default to accno 1600)
865 * invnumber (defaults to 'test ap_transaction')
866 * notes (defaults to 'test ap_transaction')
869 Currently doesn't support exchange rates.
871 Minimal usage example, creating an AP transaction with a default vendor and
872 default bookings (telephone, postage):
874 use SL::Dev::Record qw(create_ap_transaction);
875 my $invoice = create_ap_transaction();
877 Create an AP transaction with a specific vendor and specific charts:
879 my $vendor = SL::Dev::CustomerVendor::new_vendor(name => 'My Vendor')->save;
880 my $chart_postage = SL::DB::Manager::Chart->find_by(description => 'Porto');
881 my $chart_telephone = SL::DB::Manager::Chart->find_by(description => 'Telefon');
883 my $ap_transaction = create_ap_transaction(
885 invnumber => 'test invoice taxincluded',
887 amount => 2190, # optional param for checking whether final amount matches
888 netamount => 2000, # optional param for checking whether final netamount matches
891 chart => $chart_postage,
895 chart => $chart_telephone,
901 Or the same example with tax not included, but an old transdate and old taxrate (16%):
903 my $ap_transaction = create_ap_transaction(
905 invnumber => 'test invoice tax not included',
906 transdate => DateTime->new(year => 2000, month => 10, day => 1),
908 amount => 2160, # optional param for checking whether final amount matches
909 netamount => 2000, # optional param for checking whether final netamount matches
912 chart => $chart_postage,
916 chart => $chart_telephone,
922 Don't use the default tax, e.g. postage with 19%:
924 my $tax_9 = SL::DB::Manager::Tax->find_by(taxkey => 9, rate => 0.19);
925 my $chart_postage = SL::DB::Manager::Chart->find_by(description => 'Porto');
926 my $ap_transaction = create_ap_transaction(
927 invnumber => 'postage with tax',
931 chart => $chart_postage,
938 =head2 C<create_ar_transaction %PARAMS>
940 See C<create_ap_transaction>, except use customer instead of vendor.
942 =head2 C<create_gl_transaction %PARAMS>
944 Creates a new GL transaction (table gl), which is basically a wrapper around
945 SL::DB::GLTransaction->new(...) and add_chart_booking and post, while setting
946 as many defaults as possible.
950 * taxincluded (0 or 1, defaults to 1)
951 * transdate (DateTime object, defaults to current date)
952 * dec (number of decimals to round to, defaults to 2)
953 * bookings (arrayref for the charts and taxes to be booked, see examples below)
955 bookings must include a least:
957 * chart as an SL::DB::Chart object
958 * credit or debit, as positive numbers
959 * tax_id, tax (an SL::DB::Tax object) or taxkey (e.g. 9)
961 Can't be used to create storno transactions.
963 Minimal usage example, using all the defaults, creating a GL transaction with
966 use SL::Dev::Record qw(create_gl_transaction);
967 $gl_transaction = create_gl_transaction();
969 Create a GL transaction with a specific charts and taxes (the default taxes for
970 those charts are used if none are explicitly given in bookings):
972 my $cash = SL::DB::Manager::Chart->find_by( description => 'Kasse' );
973 my $betriebsbedarf = SL::DB::Manager::Chart->find_by( description => 'Betriebsbedarf' );
974 $gl_transaction = create_gl_transaction(
975 reference => 'betriebsbedarf',
979 chart => $betriebsbedarf,
985 chart => $betriebsbedarf,
1006 G. Richardson E<lt>grichardson@kivitec.deE<gt>