1 package SL::Dev::Record;
5 our @EXPORT = qw(create_sales_invoice create_invoice_item);
8 use SL::Dev::CustomerVendor;
10 use SL::DB::InvoiceItem;
14 sub create_sales_invoice {
19 if ( $params{invoiceitems} ) {
20 $invoiceitems = $params{invoiceitems};
21 die "params invoiceitems must be an arrayref of InvoiceItem objects" if scalar @{$invoiceitems} == 0 or grep { ref($_) ne 'SL::DB::InvoiceItem' } @{$params{invoiceitems}};
23 $part1 = SL::Dev::Part::create_part(description => 'Testpart 1',
26 $part2 = SL::Dev::Part::create_part(description => 'Testpart 2',
29 my $invoice_item1 = create_invoice_item(part => $part1, qty => 5);
30 my $invoice_item2 = create_invoice_item(part => $part2, qty => 8);
31 $invoiceitems = [ $invoice_item1, $invoice_item2 ];
34 my $customer = $params{customer} // SL::Dev::CustomerVendor::create_customer(name => 'Testcustomer')->save;
35 die "illegal customer" unless ref($customer) eq 'SL::DB::Customer';
37 my $invoice = SL::DB::Invoice->new(
39 type => 'sales_invoice',
40 customer_id => $customer->id,
41 taxzone_id => $customer->taxzone->id,
42 invnumber => $params{invnumber} // undef,
43 currency_id => $params{currency_id} // $::instance_conf->get_currency_id,
44 taxincluded => $params{taxincluded} // 0,
45 employee_id => $params{employee_id} // SL::DB::Manager::Employee->current->id,
46 salesman_id => $params{employee_id} // SL::DB::Manager::Employee->current->id,
47 transdate => $params{transdate} // DateTime->today_local->to_kivitendo,
48 payment_id => $params{payment_id} // undef,
49 gldate => DateTime->today_local->to_kivitendo,
50 notes => $params{notes} // '',
51 invoiceitems => $invoiceitems,
58 sub create_invoice_item {
61 # is not automatically saved so it can get added as part of a the invoice transaction
63 my $part = delete($params{part});
64 die "no part passed to _create_invoice_item" unless $part && ref($part) eq 'SL::DB::Part';
66 my $invoice_item = SL::DB::InvoiceItem->new(
67 parts_id => $part->id,
68 lastcost => $part->lastcost,
69 sellprice => $part->sellprice,
70 description => $part->description,
72 %params, # override any of the part defaults via %params
84 SL::Dev::Record - create record objects for testing, with minimal defaults
88 =head2 C<create_sales_invoice %PARAMS>
90 Creates a new sales invoice (table ar, invoice = 1).
92 If neither customer nor invoiceitems are passed as params a customer and two
93 parts are created and used for building the invoice.
95 Minimal usage example:
97 my $invoice = SL::Dev::Record::create_sales_invoice();
101 my $invoice2 = SL::Dev::Record::create_sales_invoice(
103 transdate => DateTime->today_local->subtract(days => 7),
107 =head2 C<create_invoice_item %PARAMS>
109 Creates an invoice item from a part object that can be added to an invoice.
111 Example including creation of part and of invoice:
112 my $part = SL::Dev::Part::create_part(partnumber => 'T4254')->save;
113 my $item = SL::Dev::Record::create_invoice_item(part => $part, qty => 2.5);
114 my $invoice = SL::Dev::Record::create_sales_invoice(
116 invoiceitems => [ $item ],
121 * create other types of records (order, purchase records, ar transactions, ...)
129 G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>