Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision f37d4ebc

Von Tamino Steinert vor 10 Monaten hinzugefügt

  • ID f37d4ebce164053004471e29ac60bfc51c766f61
  • Vorgänger 86dd8400
  • Nachfolger 9624f512

SL::Controller::RecordBase: Basisklasse für Beleg-Controller

Unterschiede anzeigen:

SL/Controller/RecordBase.pm
1
package SL::Controller::RecordBase;
2

  
3
use strict;
4
use parent qw(SL::Controller::Base);
5

  
6
use List::Util qw(first);
7
use List::MoreUtils qw(none);
8

  
9
use SL::Helper::Flash qw(flash flash_later);
10

  
11
use Rose::Object::MakeMethods::Generic(
12
 scalar => [ qw(
13
   item_ids_to_delete is_custom_shipto_to_delete
14
   ) ],
15
 'scalar --get_set_init' => [ qw(record) ],
16
);
17

  
18
# actions ----------------------------------------------------------------------
19
sub action_add {
20
  my ($self) = @_;
21

  
22
  $self->record(SL::Model::Record->update_after_new($self->record));
23

  
24
  $self->pre_render();
25

  
26
  if (!$::form->{form_validity_token}) {
27
    $::form->{form_validity_token} = SL::DB::ValidityToken->create(scope => SL::DB::ValidityToken::SCOPE_ORDER_SAVE())->token; # TODO remove scope
28
  }
29

  
30
  $self->render(
31
    $self->base_template_folder . '/form',
32
    title => $self->record->type_data->text('add'),
33
    %{$self->{template_args}}
34
  );
35
}
36

  
37
# helper -----------------------------------------------------------------------
38

  
39
# load record object from form or create a new object
40
#
41
# And assign changes from the form to this object.
42
# If the record is loaded from db, check if items are deleted in the form,
43
# remove them form the object and collect them for removing from db on saving.
44
# Then create/update items from form (via make_item) and add them.
45
sub init_record {
46
  my ($self) = @_;
47

  
48
  die "type in form needed" unless $::form->{type};
49
  my $record;
50
  if ($::form->{id}) {
51
    $record = SL::Model::Record->get_record($::form->{type}, $::form->{id});
52
  } else {
53
    $record = SL::Model::Record->create_new_record($::form->{type});
54

  
55
    my $cv_id_method = $record->type_data->properties('customervendor'). '_id';
56
    if ($::form->{$cv_id_method}) {
57
      $record->$cv_id_method($::form->{$cv_id_method});
58
      $record = SL::Model::Record->update_after_customer_vendor_change($record);
59
    };
60
  }
61

  
62
  my $object_key = $record->type_data->properties('object_key');
63
  my $items_key = $record->type_data->properties('items_key');
64
  my $form_items = delete $::form->{$object_key}->{$items_key};
65

  
66

  
67
  $record->assign_attributes(%{$::form->{$object_key}});
68

  
69
  $self->setup_custom_shipto_from_form($record, $::form);
70

  
71
  # remove deleted items
72
  $self->item_ids_to_delete([]);
73
  foreach my $idx (reverse 0..$#{$record->items}) {
74
    my $item = $record->items->[$idx];
75
    if (none { $item->id == $_->{id} } @{$form_items}) {
76
      splice @{$record->items}, $idx, 1;
77
      push @{$self->item_ids_to_delete}, $item->id;
78
    }
79
  }
80

  
81
  my @items;
82
  my $pos = 1;
83
  foreach my $form_attr (@{$form_items}) {
84
    my $item = make_item($record, $form_attr);
85
    $item->position($pos);
86
    push @items, $item;
87
    $pos++;
88
  }
89
  $record->add_items(grep {!$_->id} @items);
90

  
91
  return $record;
92
}
93

  
94
# create or update items from form
95
#
96
# Make item objects from form values. For items already existing read from db.
97
# Create a new item else. And assign attributes.
98
sub make_item {
99
  my ($record, $attr) = @_;
100

  
101
  my $item;
102
  $item = first { $_->id == $attr->{id} } @{$record->items} if $attr->{id};
103

  
104
  my $is_new = !$item;
105

  
106
  # add_custom_variables adds cvars to an orderitem with no cvars for saving, but
107
  # they cannot be retrieved via custom_variables until the order/orderitem is
108
  # saved. Adding empty custom_variables to new orderitem here solves this problem.
109
  $item ||= SL::Model::Record->create_new_record_item($record->record_type);
110

  
111
  $item->assign_attributes(%$attr);
112

  
113
  if ($is_new) {
114
    my $texts = get_part_texts($item->part, $record->language_id);
115
    $item->longdescription($texts->{longdescription})              if !defined $attr->{longdescription};
116
    $item->project_id($record->globalproject_id)                   if !defined $attr->{project_id};
117
    $item->lastcost($record->is_sales ? $item->part->lastcost : 0) if !defined $attr->{lastcost_as_number};
118
  }
119

  
120
  return $item;
121
}
122

  
123
# virtual functions ------------------------------------------------------------
124

  
125
sub base_template_folder {...}
126

  
127
sub pre_render {...}
128

  
129
sub setup_custom_shipto_from_form {...}
130

  
131
sub get_part_texts {...}
132

  
133

  
134
1;

Auch abrufbar als: Unified diff