Revision 866738c6
Von Kivitendo Admin vor fast 8 Jahren hinzugefügt
SL/Dev/Record.pm | ||
---|---|---|
2 | 2 |
|
3 | 3 |
use strict; |
4 | 4 |
use base qw(Exporter); |
5 |
our @EXPORT = qw(create_sales_invoice create_invoice_item);
|
|
5 |
our @EXPORT = qw(create_invoice_item create_sales_invoice create_order_item create_sales_order create_purchase_order create_delivery_order_item create_sales_delivery_order);
|
|
6 | 6 |
|
7 |
use SL::Dev::Part; |
|
8 |
use SL::Dev::CustomerVendor; |
|
9 | 7 |
use SL::DB::Invoice; |
10 | 8 |
use SL::DB::InvoiceItem; |
11 | 9 |
use SL::DB::Employee; |
10 |
use SL::Dev::Part; |
|
11 |
use SL::Dev::CustomerVendor; |
|
12 | 12 |
use DateTime; |
13 | 13 |
|
14 |
my %record_type_to_item_type = ( sales_invoice => 'SL::DB::InvoiceItem', |
|
15 |
sales_order => 'SL::DB::OrderItem', |
|
16 |
purchase_order => 'SL::DB::OrderItem', |
|
17 |
sales_delivery_order => 'SL::DB::DeliveryOrderItem', |
|
18 |
); |
|
19 |
|
|
14 | 20 |
sub create_sales_invoice { |
15 | 21 |
my (%params) = @_; |
16 | 22 |
|
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 |
} |
|
23 |
my $record_type = 'sales_invoice'; |
|
24 |
my $invoiceitems = delete $params{invoiceitems} // _create_two_items($record_type); |
|
25 |
_check_items($invoiceitems, $record_type); |
|
33 | 26 |
|
34 |
my $customer = $params{customer} // SL::Dev::CustomerVendor::create_customer(name => 'Testcustomer')->save; |
|
35 |
die "illegal customer" unless ref($customer) eq 'SL::DB::Customer'; |
|
27 |
my $customer = delete $params{customer} // SL::Dev::CustomerVendor::create_customer(name => 'Testcustomer')->save;
|
|
28 |
die "illegal customer" unless defined $customer && ref($customer) eq 'SL::DB::Customer';
|
|
36 | 29 |
|
37 | 30 |
my $invoice = SL::DB::Invoice->new( |
38 | 31 |
invoice => 1, |
39 | 32 |
type => 'sales_invoice', |
40 | 33 |
customer_id => $customer->id, |
41 | 34 |
taxzone_id => $customer->taxzone->id, |
42 |
invnumber => $params{invnumber} // undef, |
|
35 |
invnumber => delete $params{invnumber} // undef,
|
|
43 | 36 |
currency_id => $params{currency_id} // $::instance_conf->get_currency_id, |
44 | 37 |
taxincluded => $params{taxincluded} // 0, |
45 | 38 |
employee_id => $params{employee_id} // SL::DB::Manager::Employee->current->id, |
... | ... | |
47 | 40 |
transdate => $params{transdate} // DateTime->today_local->to_kivitendo, |
48 | 41 |
payment_id => $params{payment_id} // undef, |
49 | 42 |
gldate => DateTime->today_local->to_kivitendo, |
50 |
notes => $params{notes} // '', |
|
51 | 43 |
invoiceitems => $invoiceitems, |
44 |
%params, |
|
52 | 45 |
); |
53 | 46 |
|
54 | 47 |
$invoice->post; |
55 | 48 |
return $invoice; |
56 | 49 |
} |
57 | 50 |
|
51 |
sub create_sales_delivery_order { |
|
52 |
my (%params) = @_; |
|
53 |
|
|
54 |
my $record_type = 'sales_delivery_order'; |
|
55 |
my $orderitems = delete $params{orderitems} // _create_two_items($record_type); |
|
56 |
_check_items($orderitems, $record_type); |
|
57 |
|
|
58 |
my $customer = $params{customer} // SL::Dev::CustomerVendor::create_customer(name => 'Testcustomer')->save; |
|
59 |
die "illegal customer" unless ref($customer) eq 'SL::DB::Customer'; |
|
60 |
|
|
61 |
my $delivery_order = SL::DB::DeliveryOrder->new( |
|
62 |
'is_sales' => 'true', |
|
63 |
'closed' => undef, |
|
64 |
customer_id => $customer->id, |
|
65 |
taxzone_id => $customer->taxzone_id, |
|
66 |
donumber => $params{donumber} // undef, |
|
67 |
currency_id => $params{currency_id} // $::instance_conf->get_currency_id, |
|
68 |
taxincluded => $params{taxincluded} // 0, |
|
69 |
employee_id => $params{employee_id} // SL::DB::Manager::Employee->current->id, |
|
70 |
salesman_id => $params{employee_id} // SL::DB::Manager::Employee->current->id, |
|
71 |
transdate => $params{transdate} // DateTime->today_local->to_kivitendo, |
|
72 |
orderitems => $orderitems, |
|
73 |
%params |
|
74 |
); |
|
75 |
$delivery_order->save; |
|
76 |
return $delivery_order; |
|
77 |
} |
|
78 |
|
|
79 |
sub create_sales_order { |
|
80 |
my (%params) = @_; |
|
81 |
|
|
82 |
my $record_type = 'sales_order'; |
|
83 |
my $orderitems = delete $params{orderitems} // _create_two_items($record_type); |
|
84 |
_check_items($orderitems, $record_type); |
|
85 |
|
|
86 |
my $save = delete $params{save} // 0; |
|
87 |
|
|
88 |
my $customer = $params{customer} // SL::Dev::CustomerVendor::create_customer(name => 'Testcustomer')->save; |
|
89 |
die "illegal customer" unless ref($customer) eq 'SL::DB::Customer'; |
|
90 |
|
|
91 |
my $order = SL::DB::Order->new( |
|
92 |
customer_id => delete $params{customer_id} // $customer->id, |
|
93 |
taxzone_id => delete $params{taxzone_id} // $customer->taxzone->id, |
|
94 |
currency_id => delete $params{currency_id} // $::instance_conf->get_currency_id, |
|
95 |
taxincluded => delete $params{taxincluded} // 0, |
|
96 |
# employee_id => delete $params{employee_id} // SL::DB::Manager::Employee->current->id, |
|
97 |
# salesman_id => delete $params{employee_id} // SL::DB::Manager::Employee->current->id, |
|
98 |
transdate => delete $params{transdate} // DateTime->today_local->to_kivitendo, |
|
99 |
orderitems => $orderitems, |
|
100 |
%params |
|
101 |
); |
|
102 |
|
|
103 |
if ( $save ) { |
|
104 |
$order->calculate_prices_and_taxes; |
|
105 |
$order->save; |
|
106 |
} |
|
107 |
return $order; |
|
108 |
} |
|
109 |
|
|
110 |
sub create_purchase_order { |
|
111 |
my (%params) = @_; |
|
112 |
|
|
113 |
my $record_type = 'purchase_order'; |
|
114 |
my $orderitems = delete $params{orderitems} // _create_two_items($record_type); |
|
115 |
_check_items($orderitems, $record_type); |
|
116 |
|
|
117 |
my $save = delete $params{save} // 0; |
|
118 |
|
|
119 |
my $vendor = $params{vendor} // SL::Dev::CustomerVendor::create_vendor(name => 'Testvendor')->save; |
|
120 |
die "illegal vendor" unless ref($vendor) eq 'SL::DB::Vendor'; |
|
121 |
|
|
122 |
my $order = SL::DB::Order->new( |
|
123 |
vendor_id => delete $params{vendor_id} // $vendor->id, |
|
124 |
taxzone_id => delete $params{taxzone_id} // $vendor->taxzone->id, |
|
125 |
currency_id => delete $params{currency_id} // $::instance_conf->get_currency_id, |
|
126 |
taxincluded => delete $params{taxincluded} // 0, |
|
127 |
transdate => delete $params{transdate} // DateTime->today_local->to_kivitendo, |
|
128 |
'closed' => undef, |
|
129 |
orderitems => $orderitems, |
|
130 |
%params |
|
131 |
); |
|
132 |
|
|
133 |
if ( $save ) { |
|
134 |
$order->calculate_prices_and_taxes; # not tested for purchase orders |
|
135 |
$order->save; |
|
136 |
} |
|
137 |
return $order; |
|
138 |
}; |
|
139 |
|
|
140 |
sub _check_items { |
|
141 |
my ($items, $record_type) = @_; |
|
142 |
|
|
143 |
if ( scalar @{$items} == 0 or grep { ref($_) ne $record_type_to_item_type{"$record_type"} } @{$items} ) { |
|
144 |
die "Error: items must be an arrayref of " . $record_type_to_item_type{"$record_type"} . "objects."; |
|
145 |
} |
|
146 |
} |
|
147 |
|
|
58 | 148 |
sub create_invoice_item { |
59 | 149 |
my (%params) = @_; |
60 | 150 |
|
61 |
# is not automatically saved so it can get added as part of a the invoice transaction |
|
151 |
return _create_item(record_type => 'sales_invoice', %params); |
|
152 |
} |
|
153 |
|
|
154 |
sub create_order_item { |
|
155 |
my (%params) = @_; |
|
156 |
|
|
157 |
return _create_item(record_type => 'sales_order', %params); |
|
158 |
} |
|
159 |
|
|
160 |
sub create_delivery_order_item { |
|
161 |
my (%params) = @_; |
|
162 |
|
|
163 |
return _create_item(record_type => 'sales_delivery_order', %params); |
|
164 |
} |
|
165 |
|
|
166 |
sub _create_item { |
|
167 |
my (%params) = @_; |
|
168 |
|
|
169 |
my $record_type = delete($params{record_type}); |
|
170 |
my $part = delete($params{part}); |
|
171 |
|
|
172 |
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 }; |
|
173 |
die "part missing as param" unless $part && ref($part) eq 'SL::DB::Part'; |
|
62 | 174 |
|
63 |
my $part = delete($params{part}); |
|
64 |
die "no part passed to _create_invoice_item" unless $part && ref($part) eq 'SL::DB::Part'; |
|
175 |
my ($sellprice, $lastcost); |
|
65 | 176 |
|
66 |
my $invoice_item = SL::DB::InvoiceItem->new( |
|
177 |
if ( $record_type =~ /^sales/ ) { |
|
178 |
$sellprice = delete $params{sellprice} // $part->sellprice; |
|
179 |
$lastcost = delete $params{lastcost} // $part->lastcost; |
|
180 |
} else { |
|
181 |
$sellprice = delete $params{sellprice} // $part->lastcost; |
|
182 |
$lastcost = delete $params{lastcost} // 0; # $part->lastcost; |
|
183 |
} |
|
184 |
|
|
185 |
my $item = "$record_type_to_item_type{$record_type}"->new( |
|
67 | 186 |
parts_id => $part->id, |
68 |
lastcost => $part->lastcost,
|
|
69 |
sellprice => $part->sellprice,
|
|
187 |
sellprice => $sellprice,
|
|
188 |
lastcost => $lastcost,
|
|
70 | 189 |
description => $part->description, |
71 | 190 |
unit => $part->unit, |
72 |
%params, # override any of the part defaults via %params |
|
191 |
qty => $params{qty} || 5, |
|
192 |
%params, |
|
73 | 193 |
); |
194 |
return $item; |
|
195 |
} |
|
74 | 196 |
|
75 |
return $invoice_item; |
|
197 |
sub _create_two_items { |
|
198 |
my ($record_type) = @_; |
|
199 |
|
|
200 |
my $part1 = SL::Dev::Part::create_part(description => 'Testpart 1', |
|
201 |
sellprice => 12, |
|
202 |
)->save; |
|
203 |
my $part2 = SL::Dev::Part::create_part(description => 'Testpart 2', |
|
204 |
sellprice => 10, |
|
205 |
)->save; |
|
206 |
my $item1 = _create_item(record_type => $record_type, part => $part1, qty => 5); |
|
207 |
my $item2 = _create_item(record_type => $record_type, part => $part2, qty => 8); |
|
208 |
return [ $item1, $item2 ]; |
|
76 | 209 |
} |
77 | 210 |
|
78 | 211 |
1; |
... | ... | |
104 | 237 |
taxincluded => 1, |
105 | 238 |
); |
106 | 239 |
|
107 |
=head2 C<create_invoice_item %PARAMS> |
|
240 |
=head2 C<create_sales_order %PARAMS> |
|
241 |
|
|
242 |
Examples: |
|
243 |
|
|
244 |
Create a sales order and save it directly via rose, without running |
|
245 |
calculate_prices_and_taxes: |
|
246 |
|
|
247 |
my $order = SL::Dev::Record::create_sales_order()->save; |
|
248 |
|
|
249 |
Let create_sales_order run calculate_prices_and_taxes and save: |
|
250 |
|
|
251 |
my $order = SL::Dev::Record::create_sales_order(save => 1); |
|
252 |
|
|
253 |
|
|
254 |
Example including creation of part and of sales order. |
|
255 |
my $part1 = SL::Dev::Part::create_part( partnumber => 'T4254')->save; |
|
256 |
my $part2 = SL::Dev::Part::create_service(partnumber => 'Serv1')->save; |
|
257 |
my $order = SL::Dev::Record::create_sales_order( |
|
258 |
save => 1, |
|
259 |
taxincluded => 0, |
|
260 |
orderitems => [ SL::Dev::Record::create_order_item(part => $part1, qty => 3, sellprice => 70), |
|
261 |
SL::Dev::Record::create_order_item(part => $part2, qty => 10, sellprice => 50), |
|
262 |
] |
|
263 |
); |
|
264 |
|
|
265 |
=head2 C<create_purchase_order %PARAMS> |
|
266 |
|
|
267 |
See comments for C<create_sales_order>. |
|
108 | 268 |
|
109 |
Creates an invoice item from a part object that can be added to an invoice. |
|
269 |
Example: |
|
270 |
my $purchase_order = SL::Dev::Record::create_purchase_order(save => 1); |
|
271 |
|
|
272 |
|
|
273 |
=head2 C<create_item %PARAMS> |
|
274 |
|
|
275 |
Creates an item from a part object that can be added to a record. |
|
276 |
|
|
277 |
Required params: record_type (sales_invoice, sales_order, sales_delivery_order) |
|
278 |
part (an SL::DB::Part object) |
|
110 | 279 |
|
111 | 280 |
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);
|
|
281 |
my $part = SL::Dev::Part::create_part( partnumber => 'T4254')->save;
|
|
282 |
my $item = SL::Dev::Record::create_item(record_type => 'sales_invoice', part => $part, qty => 2.5);
|
|
114 | 283 |
my $invoice = SL::Dev::Record::create_sales_invoice( |
115 | 284 |
taxincluded => 0, |
116 | 285 |
invoiceitems => [ $item ], |
... | ... | |
118 | 287 |
|
119 | 288 |
=head1 TODO |
120 | 289 |
|
121 |
* create other types of records (order, purchase records, ar transactions, ...) |
|
122 |
|
|
123 | 290 |
=head1 BUGS |
124 | 291 |
|
125 | 292 |
Nothing here yet. |
Auch abrufbar als: Unified diff
SL::Dev::Record - Aufträge und Lieferscheine erstellen