Revision b14755d0
Von Bernd Bleßmann vor fast 7 Jahren hinzugefügt
SL/DB/Order.pm | ||
---|---|---|
189 | 189 |
return $delivery_order; |
190 | 190 |
} |
191 | 191 |
|
192 |
sub _clone_orderitem_cvar { |
|
193 |
my ($cvar) = @_; |
|
194 |
|
|
195 |
my $cloned = $_->clone_and_reset; |
|
196 |
$cloned->sub_module('orderitems'); |
|
197 |
|
|
198 |
return $cloned; |
|
199 |
} |
|
200 |
|
|
201 |
sub new_from { |
|
202 |
my ($class, $source, %params) = @_; |
|
203 |
|
|
204 |
croak("Unsupported source object type '" . ref($source) . "'") unless ref($source) eq 'SL::DB::Order'; |
|
205 |
croak("A destination type must be given parameter") unless $params{destination_type}; |
|
206 |
|
|
207 |
my $destination_type = delete $params{destination_type}; |
|
208 |
my $src_dst_allowed = ('sales_quotation' eq $source->type && 'sales_order' eq $destination_type) |
|
209 |
|| ('request_quotation' eq $source->type && 'purchase_order' eq $destination_type); |
|
210 |
croak("Cannot convert from '" . $source->type . "' to '" . $destination_type . "'") unless $src_dst_allowed; |
|
211 |
|
|
212 |
my ($item_parent_id_column, $item_parent_column); |
|
213 |
|
|
214 |
if (ref($source) eq 'SL::DB::Order') { |
|
215 |
$item_parent_id_column = 'trans_id'; |
|
216 |
$item_parent_column = 'order'; |
|
217 |
} |
|
218 |
|
|
219 |
my %args = ( map({ ( $_ => $source->$_ ) } qw(amount cp_id currency_id cusordnumber customer_id delivery_customer_id delivery_term_id delivery_vendor_id |
|
220 |
department_id employee_id globalproject_id intnotes marge_percent marge_total language_id netamount notes |
|
221 |
ordnumber payment_id quonumber reqdate salesman_id shippingpoint shipvia taxincluded taxzone_id |
|
222 |
transaction_description vendor_id |
|
223 |
)), |
|
224 |
quotation => 0, |
|
225 |
closed => 0, |
|
226 |
delivered => 0, |
|
227 |
transdate => DateTime->today_local, |
|
228 |
); |
|
229 |
|
|
230 |
# Custom shipto addresses (the ones specific to the sales/purchase |
|
231 |
# record and not to the customer/vendor) are only linked from |
|
232 |
# shipto → delivery_orders. Meaning delivery_orders.shipto_id |
|
233 |
# will not be filled in that case. |
|
234 |
if (!$source->shipto_id && $source->id) { |
|
235 |
$args{custom_shipto} = $source->custom_shipto->clone($class) if $source->can('custom_shipto') && $source->custom_shipto; |
|
236 |
|
|
237 |
} else { |
|
238 |
$args{shipto_id} = $source->shipto_id; |
|
239 |
} |
|
240 |
|
|
241 |
my $order = $class->new(%args); |
|
242 |
$order->assign_attributes(%{ $params{attributes} }) if $params{attributes}; |
|
243 |
my $items = delete($params{items}) || $source->items_sorted; |
|
244 |
my %item_parents; |
|
245 |
|
|
246 |
my @items = map { |
|
247 |
my $source_item = $_; |
|
248 |
my $source_item_id = $_->$item_parent_id_column; |
|
249 |
my @custom_variables = map { _clone_orderitem_cvar($_) } @{ $source_item->custom_variables }; |
|
250 |
|
|
251 |
$item_parents{$source_item_id} ||= $source_item->$item_parent_column; |
|
252 |
my $item_parent = $item_parents{$source_item_id}; |
|
253 |
|
|
254 |
my $current_oe_item = SL::DB::OrderItem->new(map({ ( $_ => $source_item->$_ ) } |
|
255 |
qw(active_discount_source active_price_source base_qty cusordnumber |
|
256 |
description discount lastcost longdescription |
|
257 |
marge_percent marge_price_factor marge_total |
|
258 |
ordnumber parts_id price_factor price_factor_id pricegroup_id |
|
259 |
project_id qty reqdate sellprice serialnumber ship subtotal transdate unit |
|
260 |
)), |
|
261 |
custom_variables => \@custom_variables, |
|
262 |
); |
|
263 |
$current_oe_item->{"converted_from_orderitems_id"} = $_->{id} if ref($item_parent) eq 'SL::DB::Order'; |
|
264 |
$current_oe_item; |
|
265 |
} @{ $items }; |
|
266 |
|
|
267 |
@items = grep { $params{item_filter}->($_) } @items if $params{item_filter}; |
|
268 |
@items = grep { $_->qty * 1 } @items if $params{skip_items_zero_qty}; |
|
269 |
@items = grep { $_->qty >=0 } @items if $params{skip_items_negative_qty}; |
|
270 |
|
|
271 |
$order->items(\@items); |
|
272 |
|
|
273 |
return $order; |
|
274 |
} |
|
275 |
|
|
192 | 276 |
sub number { |
193 | 277 |
my $self = shift; |
194 | 278 |
|
... | ... | |
283 | 367 |
|
284 | 368 |
At the moment only sales quotations and sales orders can be converted. |
285 | 369 |
|
370 |
=head2 C<new_from $source, %params> |
|
371 |
|
|
372 |
Creates a new C<SL::DB::Order> instance and copies as much |
|
373 |
information from C<$source> as possible. At the moment only sales orders from |
|
374 |
sales quotations and purchase orders from requests for quotations can be |
|
375 |
created. |
|
376 |
|
|
377 |
The C<transdate> field will be set to the current date. |
|
378 |
|
|
379 |
The conversion copies the order items as well. |
|
380 |
|
|
381 |
Returns the new order instance. The object returned is not |
|
382 |
saved. |
|
383 |
|
|
384 |
C<%params> can include the following options |
|
385 |
(C<destination_type> is mandatory): |
|
386 |
|
|
387 |
=over 4 |
|
388 |
|
|
389 |
=item C<destination_type> |
|
390 |
|
|
391 |
(mandatory) |
|
392 |
The type of the newly created object. Can be C<sales_order> or |
|
393 |
C<purchase_order> for now. |
|
394 |
|
|
395 |
=item C<items> |
|
396 |
|
|
397 |
An optional array reference of RDBO instances for the items to use. If |
|
398 |
missing then the method C<items_sorted> will be called on |
|
399 |
C<$source>. This option can be used to override the sorting, to |
|
400 |
exclude certain positions or to add additional ones. |
|
401 |
|
|
402 |
=item C<skip_items_negative_qty> |
|
403 |
|
|
404 |
If trueish then items with a negative quantity are skipped. Items with |
|
405 |
a quantity of 0 are not affected by this option. |
|
406 |
|
|
407 |
=item C<skip_items_zero_qty> |
|
408 |
|
|
409 |
If trueish then items with a quantity of 0 are skipped. |
|
410 |
|
|
411 |
=item C<item_filter> |
|
412 |
|
|
413 |
An optional code reference that is called for each item with the item |
|
414 |
as its sole parameter. Items for which the code reference returns a |
|
415 |
falsish value will be skipped. |
|
416 |
|
|
417 |
=item C<attributes> |
|
418 |
|
|
419 |
An optional hash reference. If it exists then it is passed to C<new> |
|
420 |
allowing the caller to set certain attributes for the new delivery |
|
421 |
order. |
|
422 |
|
|
423 |
=back |
|
424 |
|
|
286 | 425 |
=head2 C<create_sales_process> |
287 | 426 |
|
288 | 427 |
Creates and saves a new sales process. Can only be called for sales |
Auch abrufbar als: Unified diff
SL::DB::Order->new_from implementiert.
Im Moment nur von Angeboten zu Aufträgen (Ein- und Verkauf).