1 package SL::Dev::Record;
5 our @EXPORT = qw(create_invoice_item create_sales_invoice create_credit_note create_order_item create_sales_order create_purchase_order create_delivery_order_item create_sales_delivery_order create_project);
8 use SL::DB::InvoiceItem;
11 use SL::Dev::CustomerVendor;
13 use SL::DB::ProjectStatus;
14 use SL::DB::ProjectType;
17 my %record_type_to_item_type = ( sales_invoice => 'SL::DB::InvoiceItem',
18 credit_note => 'SL::DB::InvoiceItem',
19 sales_order => 'SL::DB::OrderItem',
20 purchase_order => 'SL::DB::OrderItem',
21 sales_delivery_order => 'SL::DB::DeliveryOrderItem',
24 sub create_sales_invoice {
27 my $record_type = 'sales_invoice';
28 my $invoiceitems = delete $params{invoiceitems} // _create_two_items($record_type);
29 _check_items($invoiceitems, $record_type);
31 my $customer = delete $params{customer} // SL::Dev::CustomerVendor::create_customer(name => 'Testcustomer')->save;
32 die "illegal customer" unless defined $customer && ref($customer) eq 'SL::DB::Customer';
34 my $invoice = SL::DB::Invoice->new(
36 type => 'sales_invoice',
37 customer_id => $customer->id,
38 taxzone_id => $customer->taxzone->id,
39 invnumber => delete $params{invnumber} // undef,
40 currency_id => $params{currency_id} // $::instance_conf->get_currency_id,
41 taxincluded => $params{taxincluded} // 0,
42 employee_id => $params{employee_id} // SL::DB::Manager::Employee->current->id,
43 salesman_id => $params{employee_id} // SL::DB::Manager::Employee->current->id,
44 transdate => $params{transdate} // DateTime->today_local->to_kivitendo,
45 payment_id => $params{payment_id} // undef,
46 gldate => DateTime->today,
47 invoiceitems => $invoiceitems,
49 $invoice->assign_attributes(%params) if %params;
55 sub create_credit_note {
58 my $record_type = 'credit_note';
59 my $invoiceitems = delete $params{invoiceitems} // _create_two_items($record_type);
60 _check_items($invoiceitems, $record_type);
62 my $customer = delete $params{customer} // SL::Dev::CustomerVendor::create_customer(name => 'Testcustomer')->save;
63 die "illegal customer" unless defined $customer && ref($customer) eq 'SL::DB::Customer';
65 # adjust qty for credit note items
66 $_->qty( $_->qty * -1) foreach @{$invoiceitems};
68 my $invoice = SL::DB::Invoice->new(
70 type => 'credit_note',
71 customer_id => $customer->id,
72 taxzone_id => $customer->taxzone->id,
73 invnumber => delete $params{invnumber} // undef,
74 currency_id => $params{currency_id} // $::instance_conf->get_currency_id,
75 taxincluded => $params{taxincluded} // 0,
76 employee_id => $params{employee_id} // SL::DB::Manager::Employee->current->id,
77 salesman_id => $params{employee_id} // SL::DB::Manager::Employee->current->id,
78 transdate => $params{transdate} // DateTime->today_local->to_kivitendo,
79 payment_id => $params{payment_id} // undef,
80 gldate => DateTime->today,
81 invoiceitems => $invoiceitems,
83 $invoice->assign_attributes(%params) if %params;
89 sub create_sales_delivery_order {
92 my $record_type = 'sales_delivery_order';
93 my $orderitems = delete $params{orderitems} // _create_two_items($record_type);
94 _check_items($orderitems, $record_type);
96 my $customer = $params{customer} // SL::Dev::CustomerVendor::create_customer(name => 'Testcustomer')->save;
97 die "illegal customer" unless ref($customer) eq 'SL::DB::Customer';
99 my $delivery_order = SL::DB::DeliveryOrder->new(
100 'is_sales' => 'true',
102 customer_id => $customer->id,
103 taxzone_id => $customer->taxzone_id,
104 donumber => $params{donumber} // undef,
105 currency_id => $params{currency_id} // $::instance_conf->get_currency_id,
106 taxincluded => $params{taxincluded} // 0,
107 employee_id => $params{employee_id} // SL::DB::Manager::Employee->current->id,
108 salesman_id => $params{employee_id} // SL::DB::Manager::Employee->current->id,
109 transdate => $params{transdate} // DateTime->today,
110 orderitems => $orderitems,
112 $delivery_order->assign_attributes(%params) if %params;
113 $delivery_order->save;
114 return $delivery_order;
117 sub create_sales_order {
120 my $record_type = 'sales_order';
121 my $orderitems = delete $params{orderitems} // _create_two_items($record_type);
122 _check_items($orderitems, $record_type);
124 my $save = delete $params{save} // 0;
126 my $customer = $params{customer} // SL::Dev::CustomerVendor::create_customer(name => 'Testcustomer')->save;
127 die "illegal customer" unless ref($customer) eq 'SL::DB::Customer';
129 my $order = SL::DB::Order->new(
130 customer_id => delete $params{customer_id} // $customer->id,
131 taxzone_id => delete $params{taxzone_id} // $customer->taxzone->id,
132 currency_id => delete $params{currency_id} // $::instance_conf->get_currency_id,
133 taxincluded => delete $params{taxincluded} // 0,
134 employee_id => delete $params{employee_id} // SL::DB::Manager::Employee->current->id,
135 salesman_id => delete $params{employee_id} // SL::DB::Manager::Employee->current->id,
136 transdate => delete $params{transdate} // DateTime->today,
137 orderitems => $orderitems,
139 $order->assign_attributes(%params) if %params;
142 $order->calculate_prices_and_taxes;
148 sub create_purchase_order {
151 my $record_type = 'purchase_order';
152 my $orderitems = delete $params{orderitems} // _create_two_items($record_type);
153 _check_items($orderitems, $record_type);
155 my $save = delete $params{save} // 0;
157 my $vendor = $params{vendor} // SL::Dev::CustomerVendor::create_vendor(name => 'Testvendor')->save;
158 die "illegal vendor" unless ref($vendor) eq 'SL::DB::Vendor';
160 my $order = SL::DB::Order->new(
161 vendor_id => delete $params{vendor_id} // $vendor->id,
162 taxzone_id => delete $params{taxzone_id} // $vendor->taxzone->id,
163 currency_id => delete $params{currency_id} // $::instance_conf->get_currency_id,
164 taxincluded => delete $params{taxincluded} // 0,
165 transdate => delete $params{transdate} // DateTime->today,
167 orderitems => $orderitems,
169 $order->assign_attributes(%params) if %params;
172 $order->calculate_prices_and_taxes; # not tested for purchase orders
179 my ($items, $record_type) = @_;
181 if ( scalar @{$items} == 0 or grep { ref($_) ne $record_type_to_item_type{"$record_type"} } @{$items} ) {
182 die "Error: items must be an arrayref of " . $record_type_to_item_type{"$record_type"} . "objects.";
186 sub create_invoice_item {
189 return _create_item(record_type => 'sales_invoice', %params);
192 sub create_order_item {
195 return _create_item(record_type => 'sales_order', %params);
198 sub create_delivery_order_item {
201 return _create_item(record_type => 'sales_delivery_order', %params);
207 my $record_type = delete($params{record_type});
208 my $part = delete($params{part});
210 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 };
211 die "part missing as param" unless $part && ref($part) eq 'SL::DB::Part';
213 my ($sellprice, $lastcost);
215 if ( $record_type =~ /^sales/ ) {
216 $sellprice = delete $params{sellprice} // $part->sellprice;
217 $lastcost = delete $params{lastcost} // $part->lastcost;
219 $sellprice = delete $params{sellprice} // $part->lastcost;
220 $lastcost = delete $params{lastcost} // 0; # $part->lastcost;
223 my $item = "$record_type_to_item_type{$record_type}"->new(
224 parts_id => $part->id,
225 sellprice => $sellprice,
226 lastcost => $lastcost,
227 description => $part->description,
229 qty => $params{qty} || 5,
231 $item->assign_attributes(%params) if %params;
235 sub _create_two_items {
236 my ($record_type) = @_;
238 my $part1 = SL::Dev::Part::create_part(description => 'Testpart 1',
241 my $part2 = SL::Dev::Part::create_part(description => 'Testpart 2',
244 my $item1 = _create_item(record_type => $record_type, part => $part1, qty => 5);
245 my $item2 = _create_item(record_type => $record_type, part => $part2, qty => 8);
246 return [ $item1, $item2 ];
251 my $project = SL::DB::Project->new(
253 description => "Test project",
256 project_status_id => SL::DB::Manager::ProjectStatus->find_by(name => "running")->id,
257 project_type_id => SL::DB::Manager::ProjectType->find_by(description => "Standard")->id,
259 $project->assign_attributes(%params) if %params;
269 SL::Dev::Record - create record objects for testing, with minimal defaults
273 =head2 C<create_sales_invoice %PARAMS>
275 Creates a new sales invoice (table ar, invoice = 1).
277 If neither customer nor invoiceitems are passed as params a customer and two
278 parts are created and used for building the invoice.
280 Minimal usage example:
282 my $invoice = SL::Dev::Record::create_sales_invoice();
286 my $invoice2 = SL::Dev::Record::create_sales_invoice(
288 transdate => DateTime->today->subtract(days => 7),
292 =head2 C<create_credit_note %PARAMS>
294 Create a credit note (sales). Use positive quantities when adding items.
296 Example including creation of parts and of credit_note
297 my $part1 = SL::Dev::Part::create_part( partnumber => 'T4254')->save;
298 my $part2 = SL::Dev::Part::create_service(partnumber => 'Serv1')->save;
299 my $credit_note = SL::Dev::Record::create_credit_note(
302 invoiceitems => [ SL::Dev::Record::create_invoice_item(part => $part1, qty => 3, sellprice => 70),
303 SL::Dev::Record::create_invoice_item(part => $part2, qty => 10, sellprice => 50),
307 =head2 C<create_sales_order %PARAMS>
311 Create a sales order and save it directly via rose, without running
312 calculate_prices_and_taxes:
314 my $order = SL::Dev::Record::create_sales_order()->save;
316 Let create_sales_order run calculate_prices_and_taxes and save:
318 my $order = SL::Dev::Record::create_sales_order(save => 1);
321 Example including creation of part and of sales order.
322 my $part1 = SL::Dev::Part::create_part( partnumber => 'T4254')->save;
323 my $part2 = SL::Dev::Part::create_service(partnumber => 'Serv1')->save;
324 my $order = SL::Dev::Record::create_sales_order(
327 orderitems => [ SL::Dev::Record::create_order_item(part => $part1, qty => 3, sellprice => 70),
328 SL::Dev::Record::create_order_item(part => $part2, qty => 10, sellprice => 50),
332 =head2 C<create_purchase_order %PARAMS>
334 See comments for C<create_sales_order>.
337 my $purchase_order = SL::Dev::Record::create_purchase_order(save => 1);
340 =head2 C<create_item %PARAMS>
342 Creates an item from a part object that can be added to a record.
344 Required params: record_type (sales_invoice, sales_order, sales_delivery_order)
345 part (an SL::DB::Part object)
347 Example including creation of part and of invoice:
348 my $part = SL::Dev::Part::create_part( partnumber => 'T4254')->save;
349 my $item = SL::Dev::Record::create_item(record_type => 'sales_invoice', part => $part, qty => 2.5);
350 my $invoice = SL::Dev::Record::create_sales_invoice(
352 invoiceitems => [ $item ],
355 =head2 C<create_project %PARAMS>
357 Creates a default project.
359 Minimal example, creating a project with status "running" and type "Standard":
360 my $project = SL::Dev::Record::create_project();
362 $project = SL::Dev::Record::create_project(
363 projectnumber => 'p1',
364 description => 'Test project',
375 G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>