SL/Dev/Record - Verkaufsrechnungen für Tests erstellen
[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_sales_invoice create_invoice_item);
6
7 use SL::Dev::Part;
8 use SL::Dev::CustomerVendor;
9 use SL::DB::Invoice;
10 use SL::DB::InvoiceItem;
11 use SL::DB::Employee;
12 use DateTime;
13
14 sub create_sales_invoice {
15   my (%params) = @_;
16
17   my ($part1, $part2);
18   my $invoiceitems;
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}};
22   } else {
23     $part1 = SL::Dev::Part::create_part(description  => 'Testpart 1',
24                                         sellprice    => 12,
25                                        )->save;
26     $part2 = SL::Dev::Part::create_part(description  => 'Testpart 2',
27                                         sellprice    => 10,
28                                        )->save;
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 ];
32   }
33
34   my $customer = $params{customer} // SL::Dev::CustomerVendor::create_customer(name => 'Testcustomer')->save;
35   die "illegal customer" unless ref($customer) eq 'SL::DB::Customer';
36
37   my $invoice = SL::DB::Invoice->new(
38     invoice      => 1,
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,
52   );
53
54   $invoice->post;
55   return $invoice;
56 }
57
58 sub create_invoice_item {
59   my (%params) = @_;
60
61 # is not automatically saved so it can get added as part of a the invoice transaction
62
63   my $part = delete($params{part});
64   die "no part passed to _create_invoice_item" unless $part && ref($part) eq 'SL::DB::Part';
65
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,
71     unit        => $part->unit,
72     %params, # override any of the part defaults via %params
73   );
74
75   return $invoice_item;
76 }
77
78 1;
79
80 __END__
81
82 =head1 NAME
83
84 SL::Dev::Record - create record objects for testing, with minimal defaults
85
86 =head1 FUNCTIONS
87
88 =head2 C<create_sales_invoice %PARAMS>
89
90 Creates a new sales invoice (table ar, invoice = 1).
91
92 If neither customer nor invoiceitems are passed as params a customer and two
93 parts are created and used for building the invoice.
94
95 Minimal usage example:
96
97   my $invoice = SL::Dev::Record::create_sales_invoice();
98
99 Example with params:
100
101   my $invoice2 = SL::Dev::Record::create_sales_invoice(
102     invnumber   => 777,
103     transdate   => DateTime->today_local->subtract(days => 7),
104     taxincluded => 1,
105   );
106
107 =head2 C<create_invoice_item %PARAMS>
108
109 Creates an invoice item from a part object that can be added to an invoice.
110
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(
115     taxincluded  => 0,
116     invoiceitems => [ $item ],
117   );
118
119 =head1 TODO
120
121 * create other types of records (order, purchase records, ar transactions, ...)
122
123 =head1 BUGS
124
125 Nothing here yet.
126
127 =head1 AUTHOR
128
129 G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>
130
131 =cut