Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 9de3ed1d

Von Tamino Steinert vor 7 Monaten hinzugefügt

  • ID 9de3ed1dfbdb3be842a173d2bba3d24793ca73f8
  • Vorgänger 27893755
  • Nachfolger eb2db262

Part: separate save und save_with_render_error Funktionen

Unterschiede anzeigen:

SL/Controller/Part.pm
112 112
sub action_save {
113 113
  my ($self, %params) = @_;
114 114

  
115
  # checks that depend only on submitted $::form
116
  $self->check_form or return $self->js->render;
117

  
118
  my $is_new = !$self->part->id; # $ part gets loaded here
119

  
120
  # check that the part hasn't been modified
121
  unless ( $is_new ) {
122
    $self->check_part_not_modified or
123
      return $self->js->error(t8('The document has been changed by another user. Please reopen it in another window and copy the changes to the new window'))->render;
124
  }
125

  
126
  if (    $is_new
127
       && $::form->{part}{partnumber}
128
       && SL::DB::Manager::Part->find_by(partnumber => $::form->{part}{partnumber})
129
     ) {
130
    return $self->js->error(t8('The partnumber is already being used'))->render;
131
  }
132

  
133
  $self->parse_form;
134

  
135
  my @errors = $self->part->validate;
136
  return $self->js->error(@errors)->render if @errors;
137

  
138
  if ($is_new) {
139
    # Ensure CVars that should be enabled by default actually are when
140
    # creating new parts.
141
    my @default_valid_configs =
142
      grep { ! $_->{flag_defaults_to_invalid} }
143
      grep { $_->{module} eq 'IC' }
144
      @{ CVar->get_configs() };
145

  
146
    $::form->{"cvar_" . $_->{name} . "_valid"} = 1 for @default_valid_configs;
147
  } else {
148
    $self->{lastcost_modified} = $self->check_lastcost_modified;
149
  }
150

  
151
  # $self->part has been loaded, parsed and validated without errors and is ready to be saved
152
  $self->part->db->with_transaction(sub {
153

  
154
    $self->part->save(cascade => 1);
155
    $self->part->set_lastcost_assemblies_and_assortiments if $self->{lastcost_modified};
156

  
157
    SL::DB::History->new(
158
      trans_id    => $self->part->id,
159
      snumbers    => 'partnumber_' . $self->part->partnumber,
160
      employee_id => SL::DB::Manager::Employee->current->id,
161
      what_done   => 'part',
162
      addition    => 'SAVED',
163
    )->save();
164

  
165
    CVar->save_custom_variables(
166
      dbh           => $self->part->db->dbh,
167
      module        => 'IC',
168
      trans_id      => $self->part->id,
169
      variables     => $::form, # $::form->{cvar} would be nicer
170
      save_validity => 1,
171
    );
115
  my $is_new = !$self->part->id;
172 116

  
173
    1;
174
  }) or return $self->js->error(t8('The item couldn\'t be saved!') . " " . $self->part->db->error )->render;
117
  $self->save_with_render_error() or return;
175 118

  
176 119
  flash_later('info', $is_new ? t8('The item has been created.') . " " . $self->part->displayable_name : t8('The item has been saved.'));
177 120

  
......
1208 1151
  );
1209 1152
}
1210 1153

  
1154
sub save {
1155
  my ($self) = @_;
1156
  my @errors = ();
1157

  
1158
  my $is_new = !$self->part->id;
1159

  
1160
  # check that the part hasn't been modified
1161
  unless ( $is_new ) {
1162
    $self->check_part_not_modified or
1163
      return [t8('The document has been changed by another user. Please reopen it in another window and copy the changes to the new window')];
1164
  }
1165

  
1166
  if (    $is_new
1167
       && $::form->{part}{partnumber}
1168
       && SL::DB::Manager::Part->find_by(partnumber => $::form->{part}{partnumber})
1169
     ) {
1170
    return $self->js->error(t8('The partnumber is already being used'))->render;
1171
  }
1172

  
1173
  $self->parse_form;
1174

  
1175
  @errors = $self->part->validate;
1176
  return \@errors if @errors;
1177

  
1178
  if ($is_new) {
1179
    # Ensure CVars that should be enabled by default actually are when
1180
    # creating new parts.
1181
    my @default_valid_configs =
1182
      grep { ! $_->{flag_defaults_to_invalid} }
1183
      grep { $_->{module} eq 'IC' }
1184
      @{ CVar->get_configs() };
1185

  
1186
    $::form->{"cvar_" . $_->{name} . "_valid"} = 1 for @default_valid_configs;
1187
  } else {
1188
    $self->{lastcost_modified} = $self->check_lastcost_modified;
1189
  }
1190

  
1191
  # $self->part has been loaded, parsed and validated without errors and is ready to be saved
1192
  $self->part->db->with_transaction(sub {
1193

  
1194
    $self->part->save(cascade => 1);
1195
    $self->part->set_lastcost_assemblies_and_assortiments if $self->{lastcost_modified};
1196

  
1197
    SL::DB::History->new(
1198
      trans_id    => $self->part->id,
1199
      snumbers    => 'partnumber_' . $self->part->partnumber,
1200
      employee_id => SL::DB::Manager::Employee->current->id,
1201
      what_done   => 'part',
1202
      addition    => 'SAVED',
1203
    )->save();
1204

  
1205
    CVar->save_custom_variables(
1206
      dbh           => $self->part->db->dbh,
1207
      module        => 'IC',
1208
      trans_id      => $self->part->id,
1209
      variables     => $::form, # $::form->{cvar} would be nicer
1210
      save_validity => 1,
1211
    );
1212

  
1213
    1;
1214
  }) || push(@errors, $self->part->db->error);
1215

  
1216
  return \@errors;
1217
}
1218

  
1219
sub save_with_render_error {
1220
  my ($self) = @_;
1221

  
1222
  # checks that depend only on submitted $::form
1223
  if (!$self->check_form) {
1224
    $self->js->render();
1225
    return 0;
1226
  }
1227

  
1228
  my $errors = $self->save();
1229

  
1230
  if (scalar @{ $errors }) {
1231
    $self->js->error(t8('The item couldn\'t be saved!'));
1232
    $self->js->error(@{ $errors });
1233
    $self->js->render();
1234
    return 0;
1235
  }
1236

  
1237
  return 1;
1238
}
1239

  
1240
sub js_reset_part_after_save {
1241
  my ($self) = @_;
1242

  
1243
  $self->js
1244
    ->val('#part_id',               $self->part->id)
1245
    ->val('#part_partnumber',       $self->part->partnumber)
1246
    ->val('#part_weight_as_number', $self->part->weight_as_number)
1247
    ->val('#last_modification',    "$self->part->last_modification");
1248
}
1211 1249

  
1212 1250
# get_set_inits for partpicker
1213 1251

  
......
2099 2137

  
2100 2138
Doesn't work for recursive items.
2101 2139

  
2140
=item C<save>
2141

  
2142
Helper function for saving the part object in $self->part. Returns a error list
2143
on failure.
2144

  
2145
=item C<save_with_render_error>
2146

  
2147
Helper function for checking the form, saving the part object and error
2148
rendering. Returns 1 on success and 0 when a error is rendered.
2149

  
2150
It can be called like this:
2151

  
2152
  $self->save_with_render_error() or return;
2153

  
2154
=item C<js_reset_part_after_save>
2155

  
2156
Helper function for updating changed values after save in the form via js. It
2157
chances 'part_id', 'part_partnumber', 'part_weight_as_number' and
2158
'last_modification'.
2159

  
2102 2160
=back
2103 2161

  
2104 2162
=head1 GET SET INITS

Auch abrufbar als: Unified diff