+sub init_h_unit_name { first { SL::DB::Manager::Unit->find_by(name => $_) } qw(Std h Stunde) };
+
+sub load_parts_for_sections {
+ my ($self, %params) = @_;
+
+ $self->parts({ map { ($_->{id} => $_) } @{ SL::DB::Manager::Part->get_all(where => [ id => [ uniq map { $_->{order_part_id} } @{ $params{sections} } ] ]) } });
+}
+
+sub create_order_item {
+ my ($self, %params) = @_;
+
+ my $section = $params{section};
+ my $item = $params{item} || SL::DB::OrderItem->new;
+ my $part = $self->parts->{ $section->order_part_id };
+ my $description = $section->{keep_description} ? $item->description : $part->description;
+
+ if (!$section->{keep_description}) {
+ $description = '<%fb_number%> <%title%>' unless $description =~ m{<%};
+ $description =~ s{<% (.+?) %>}{$section->$1}egx;
+ }
+
+ $item->assign_attributes(
+ parts_id => $part->id,
+ description => $description,
+ qty => $section->time_estimation * 1,
+ unit => $self->h_unit_name,
+ sellprice => $::form->round_amount($self->requirement_spec->hourly_rate, 2),
+ discount => 0,
+ project_id => $self->requirement_spec->project_id,
+ );
+
+ return $item;
+}
+
+sub create_order {
+ my ($self, %params) = @_;
+
+ $self->load_parts_for_sections(%params);
+
+ my @orderitems = map { $self->create_order_item(section => $_) } @{ $params{sections} };
+ my $employee = SL::DB::Manager::Employee->current;
+ my $customer = SL::DB::Customer->new(id => $::form->{customer_id})->load;
+ my $order = SL::DB::Order->new(
+ globalproject_id => $self->requirement_spec->project_id,
+ transdate => DateTime->today_local,
+ reqdate => $::form->{quotation} && $customer->payment_id ? $customer->payment->calc_date : undef,
+ quotation => !!$::form->{quotation},
+ orderitems => \@orderitems,
+ customer_id => $customer->id,
+ taxincluded => $customer->taxincluded,
+ intnotes => $customer->notes,
+ language_id => $customer->language_id,
+ payment_id => $customer->payment_id,
+ taxzone_id => $customer->taxzone_id,
+ employee_id => $employee->id,
+ salesman_id => $employee->id,
+ transaction_description => $self->requirement_spec->displayable_name,
+ currency_id => $::instance_conf->get_currency_id,
+ );
+
+ $order->calculate_prices_and_taxes;
+
+ return $order;
+}