ActionBar: existierende Inputs namens »action« vor Submit entfernen
[kivitendo-erp.git] / SL / Dev / Record.pm
1 package SL::Dev::Record;
2
3 use strict;
4 use base qw(Exporter);
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);
6
7 use SL::DB::Invoice;
8 use SL::DB::InvoiceItem;
9 use SL::DB::Employee;
10 use SL::Dev::Part;
11 use SL::Dev::CustomerVendor;
12 use SL::DB::Project;
13 use SL::DB::ProjectStatus;
14 use SL::DB::ProjectType;
15 use DateTime;
16
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',
22                                );
23
24 sub create_sales_invoice {
25   my (%params) = @_;
26
27   my $record_type = 'sales_invoice';
28   my $invoiceitems = delete $params{invoiceitems} // _create_two_items($record_type);
29   _check_items($invoiceitems, $record_type);
30
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';
33
34   my $invoice = SL::DB::Invoice->new(
35     invoice      => 1,
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,
48   );
49   $invoice->assign_attributes(%params) if %params;
50
51   $invoice->post;
52   return $invoice;
53 }
54
55 sub create_credit_note {
56   my (%params) = @_;
57
58   my $record_type = 'credit_note';
59   my $invoiceitems = delete $params{invoiceitems} // _create_two_items($record_type);
60   _check_items($invoiceitems, $record_type);
61
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';
64
65   # adjust qty for credit note items
66   $_->qty( $_->qty * -1) foreach @{$invoiceitems};
67
68   my $invoice = SL::DB::Invoice->new(
69     invoice      => 1,
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,
82   );
83   $invoice->assign_attributes(%params) if %params;
84
85   $invoice->post;
86   return $invoice;
87 }
88
89 sub create_sales_delivery_order {
90   my (%params) = @_;
91
92   my $record_type = 'sales_delivery_order';
93   my $orderitems = delete $params{orderitems} // _create_two_items($record_type);
94   _check_items($orderitems, $record_type);
95
96   my $customer = $params{customer} // SL::Dev::CustomerVendor::create_customer(name => 'Testcustomer')->save;
97   die "illegal customer" unless ref($customer) eq 'SL::DB::Customer';
98
99   my $delivery_order = SL::DB::DeliveryOrder->new(
100     'is_sales'   => 'true',
101     'closed'     => undef,
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,
111   );
112   $delivery_order->assign_attributes(%params) if %params;
113   $delivery_order->save;
114   return $delivery_order;
115 }
116
117 sub create_sales_order {
118   my (%params) = @_;
119
120   my $record_type = 'sales_order';
121   my $orderitems = delete $params{orderitems} // _create_two_items($record_type);
122   _check_items($orderitems, $record_type);
123
124   my $save = delete $params{save} // 0;
125
126   my $customer = $params{customer} // SL::Dev::CustomerVendor::create_customer(name => 'Testcustomer')->save;
127   die "illegal customer" unless ref($customer) eq 'SL::DB::Customer';
128
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,
138   );
139   $order->assign_attributes(%params) if %params;
140
141   if ( $save ) {
142     $order->calculate_prices_and_taxes;
143     $order->save;
144   }
145   return $order;
146 }
147
148 sub create_purchase_order {
149   my (%params) = @_;
150
151   my $record_type = 'purchase_order';
152   my $orderitems = delete $params{orderitems} // _create_two_items($record_type);
153   _check_items($orderitems, $record_type);
154
155   my $save = delete $params{save} // 0;
156
157   my $vendor = $params{vendor} // SL::Dev::CustomerVendor::create_vendor(name => 'Testvendor')->save;
158   die "illegal vendor" unless ref($vendor) eq 'SL::DB::Vendor';
159
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,
166     'closed'     => undef,
167     orderitems   => $orderitems,
168   );
169   $order->assign_attributes(%params) if %params;
170
171   if ( $save ) {
172     $order->calculate_prices_and_taxes; # not tested for purchase orders
173     $order->save;
174   }
175   return $order;
176 };
177
178 sub _check_items {
179   my ($items, $record_type) = @_;
180
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.";
183   }
184 }
185
186 sub create_invoice_item {
187   my (%params) = @_;
188
189   return _create_item(record_type => 'sales_invoice', %params);
190 }
191
192 sub create_order_item {
193   my (%params) = @_;
194
195   return _create_item(record_type => 'sales_order', %params);
196 }
197
198 sub create_delivery_order_item {
199   my (%params) = @_;
200
201   return _create_item(record_type => 'sales_delivery_order', %params);
202 }
203
204 sub _create_item {
205   my (%params) = @_;
206
207   my $record_type = delete($params{record_type});
208   my $part        = delete($params{part});
209
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';
212
213   my ($sellprice, $lastcost);
214
215   if ( $record_type =~ /^sales/ ) {
216     $sellprice = delete $params{sellprice} // $part->sellprice;
217     $lastcost  = delete $params{lastcost}  // $part->lastcost;
218   } else {
219     $sellprice = delete $params{sellprice} // $part->lastcost;
220     $lastcost  = delete $params{lastcost}  // 0; # $part->lastcost;
221   }
222
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,
228     unit        => $part->unit,
229     qty         => $params{qty} || 5,
230   );
231   $item->assign_attributes(%params) if %params;
232   return $item;
233 }
234
235 sub _create_two_items {
236   my ($record_type) = @_;
237
238   my $part1 = SL::Dev::Part::create_part(description => 'Testpart 1',
239                                          sellprice   => 12,
240                                         )->save;
241   my $part2 = SL::Dev::Part::create_part(description => 'Testpart 2',
242                                          sellprice   => 10,
243                                         )->save;
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 ];
247 }
248
249 sub create_project {
250   my (%params) = @_;
251   my $project = SL::DB::Project->new(
252     projectnumber     => 1,
253     description       => "Test project",
254     active            => 1,
255     valid             => 1,
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,
258   )->save;
259   $project->assign_attributes(%params) if %params;
260   return $project;
261 }
262
263 1;
264
265 __END__
266
267 =head1 NAME
268
269 SL::Dev::Record - create record objects for testing, with minimal defaults
270
271 =head1 FUNCTIONS
272
273 =head2 C<create_sales_invoice %PARAMS>
274
275 Creates a new sales invoice (table ar, invoice = 1).
276
277 If neither customer nor invoiceitems are passed as params a customer and two
278 parts are created and used for building the invoice.
279
280 Minimal usage example:
281
282   my $invoice = SL::Dev::Record::create_sales_invoice();
283
284 Example with params:
285
286   my $invoice2 = SL::Dev::Record::create_sales_invoice(
287     invnumber   => 777,
288     transdate   => DateTime->today->subtract(days => 7),
289     taxincluded => 1,
290   );
291
292 =head2 C<create_credit_note %PARAMS>
293
294 Create a credit note (sales). Use positive quantities when adding items.
295
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(
300     invnumber    => '34',
301     taxincluded  => 0,
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),
304                     ]
305   );
306
307 =head2 C<create_sales_order %PARAMS>
308
309 Examples:
310
311 Create a sales order and save it directly via rose, without running
312 calculate_prices_and_taxes:
313
314  my $order = SL::Dev::Record::create_sales_order()->save;
315
316 Let create_sales_order run calculate_prices_and_taxes and save:
317
318  my $order = SL::Dev::Record::create_sales_order(save => 1);
319
320
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(
325     save         => 1,
326     taxincluded  => 0,
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),
329                   ]
330   );
331
332 =head2 C<create_purchase_order %PARAMS>
333
334 See comments for C<create_sales_order>.
335
336 Example:
337  my $purchase_order = SL::Dev::Record::create_purchase_order(save => 1);
338
339
340 =head2 C<create_item %PARAMS>
341
342 Creates an item from a part object that can be added to a record.
343
344 Required params: record_type (sales_invoice, sales_order, sales_delivery_order)
345                  part        (an SL::DB::Part object)
346
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(
351     taxincluded  => 0,
352     invoiceitems => [ $item ],
353   );
354
355 =head2 C<create_project %PARAMS>
356
357 Creates a default project.
358
359 Minimal example, creating a project with status "running" and type "Standard":
360   my $project = SL::Dev::Record::create_project();
361
362   $project = SL::Dev::Record::create_project(
363     projectnumber => 'p1',
364     description   => 'Test project',
365   )
366
367 =head1 TODO
368
369 =head1 BUGS
370
371 Nothing here yet.
372
373 =head1 AUTHOR
374
375 G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>
376
377 =cut