+sub action_return_from_create_part {
+ my ($self) = @_;
+
+ $self->{created_part} = SL::DB::Part->new(id => delete $::form->{new_parts_id})->load if $::form->{new_parts_id};
+
+ $::auth->restore_form_from_session(delete $::form->{previousform});
+
+ # set item ids to new fake id, to identify them as new items
+ foreach my $item (@{$self->order->items_sorted}) {
+ $item->{new_fake_id} = join('_', 'new', Time::HiRes::gettimeofday(), int rand 1000000000000);
+ }
+
+ $self->recalc();
+ $self->get_unalterable_data();
+ $self->pre_render();
+
+ # trigger rendering values for second row/longdescription as hidden,
+ # because they are loaded only on demand. So we need to keep the values
+ # from the source.
+ $_->{render_second_row} = 1 for @{ $self->order->items_sorted };
+ $_->{render_longdescription} = 1 for @{ $self->order->items_sorted };
+
+ $self->render(
+ 'order/form',
+ title => $self->get_title_for('edit'),
+ %{$self->{template_args}}
+ );
+
+}
+
+# load the second row for one or more items
+#
+# This action gets the html code for all items second rows by rendering a template for
+# the second row and sets the html code via client js.
+sub action_load_second_rows {
+ my ($self) = @_;
+
+ $self->recalc() if $self->order->is_sales; # for margin calculation
+
+ foreach my $item_id (@{ $::form->{item_ids} }) {
+ my $idx = first_index { $_ eq $item_id } @{ $::form->{orderitem_ids} };
+ my $item = $self->order->items_sorted->[$idx];
+
+ $self->js_load_second_row($item, $item_id, 0);
+ }
+
+ $self->js->run('kivi.Order.init_row_handlers') if $self->order->is_sales; # for lastcosts change-callback
+
+ $self->js->render();
+}
+
+# update description, notes and sellprice from master data
+sub action_update_row_from_master_data {
+ my ($self) = @_;
+
+ foreach my $item_id (@{ $::form->{item_ids} }) {
+ my $idx = first_index { $_ eq $item_id } @{ $::form->{orderitem_ids} };
+ my $item = $self->order->items_sorted->[$idx];
+ my $texts = get_part_texts($item->part, $self->order->language_id);
+
+ $item->description($texts->{description});
+ $item->longdescription($texts->{longdescription});
+
+ my ($price_src, $discount_src) = get_best_price_and_discount_source($self->order, $item, 1);
+
+ $item->sellprice($price_src->price);
+ $item->active_price_source($price_src);
+ $item->discount($discount_src->discount);
+ $item->active_discount_source($discount_src);
+
+ my $price_editable = $self->order->is_sales ? $::auth->assert('sales_edit_prices', 1) : $::auth->assert('purchase_edit_prices', 1);
+
+ $self->js
+ ->run('kivi.Order.set_price_and_source_text', $item_id, $price_src ->source, $price_src ->source_description, $item->sellprice_as_number, $price_editable)
+ ->run('kivi.Order.set_discount_and_source_text', $item_id, $discount_src->source, $discount_src->source_description, $item->discount_as_percent, $price_editable)
+ ->html('.row_entry:has(#item_' . $item_id . ') [name = "partnumber"] a', $item->part->partnumber)
+ ->val ('.row_entry:has(#item_' . $item_id . ') [name = "order.orderitems[].description"]', $item->description)
+ ->val ('.row_entry:has(#item_' . $item_id . ') [name = "order.orderitems[].longdescription"]', $item->longdescription);
+
+ if ($self->search_cvpartnumber) {
+ $self->get_item_cvpartnumber($item);
+ $self->js->html('.row_entry:has(#item_' . $item_id . ') [name = "cvpartnumber"]', $item->{cvpartnumber});
+ }
+ }
+
+ $self->recalc();
+ $self->js_redisplay_line_values;
+ $self->js_redisplay_amounts_and_taxes;
+
+ $self->js->render();
+}
+
+sub action_save_phone_note {
+ my ($self) = @_;
+
+ if (!$::form->{phone_note}->{subject} || !$::form->{phone_note}->{body}) {
+ return $self->js->flash('error', t8('Phone note needs a subject and a body.'))->render;
+ }
+
+ my $phone_note;
+ if ($::form->{phone_note}->{id}) {
+ $phone_note = first { $_->id == $::form->{phone_note}->{id} } @{$self->order->phone_notes};
+ return $self->js->flash('error', t8('Phone note not found for this order.'))->render if !$phone_note;
+ }
+
+ $phone_note = SL::DB::Note->new() if !$phone_note;
+ my $is_new = !$phone_note->id;
+
+ $phone_note->assign_attributes(%{ $::form->{phone_note} },
+ trans_id => $self->order->id,
+ trans_module => 'oe',
+ employee => SL::DB::Manager::Employee->current);
+
+ $phone_note->save;
+ $self->order(SL::DB::Order->new(id => $self->order->id)->load);
+
+ my $tab_as_html = $self->p->render('order/tabs/phone_notes', SELF => $self);
+
+ return $self->js
+ ->replaceWith('#phone-notes', $tab_as_html)
+ ->html('#num_phone_notes', (scalar @{$self->order->phone_notes}) ? ' (' . scalar @{$self->order->phone_notes} . ')' : '')
+ ->flash('info', $is_new ? t8('Phone note has been created.') : t8('Phone note has been updated.'))
+ ->render;
+}
+
+sub action_delete_phone_note {
+ my ($self) = @_;
+
+ my $phone_note = first { $_->id == $::form->{phone_note}->{id} } @{$self->order->phone_notes};
+
+ return $self->js->flash('error', t8('Phone note not found for this order.'))->render if !$phone_note;
+
+ $phone_note->delete;
+ $self->order(SL::DB::Order->new(id => $self->order->id)->load);
+
+ my $tab_as_html = $self->p->render('order/tabs/phone_notes', SELF => $self);
+
+ return $self->js
+ ->replaceWith('#phone-notes', $tab_as_html)
+ ->html('#num_phone_notes', (scalar @{$self->order->phone_notes}) ? ' (' . scalar @{$self->order->phone_notes} . ')' : '')
+ ->flash('info', t8('Phone note has been deleted.'))
+ ->render;
+}
+
+sub js_load_second_row {