Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 175f674c

Von Tamino Steinert vor 7 Tagen hinzugefügt

  • ID 175f674c189415fe4b769a40713c2621bc31e6b0
  • Vorgänger 9fe89a1e
  • Nachfolger 4ffbaf73

Part: separate save und save_with_render_error Funktionen

Unterschiede anzeigen:

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

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

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

  
121
  # check that the part hasn't been modified
122
  unless ( $is_new ) {
123
    $self->check_part_not_modified or
124
      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;
125
  }
126

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

  
134
  $self->parse_form;
135

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

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

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

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

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

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

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

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

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

  
......
1242 1185
  );
1243 1186
}
1244 1187

  
1188
sub save {
1189
  my ($self) = @_;
1190
  my @errors = ();
1191

  
1192
  my $is_new = !$self->part->id;
1193

  
1194
  # check that the part hasn't been modified
1195
  unless ( $is_new ) {
1196
    $self->check_part_not_modified or
1197
      return [t8('The document has been changed by another user. Please reopen it in another window and copy the changes to the new window')];
1198
  }
1199

  
1200
  if (    $is_new
1201
       && $::form->{part}{partnumber}
1202
       && SL::DB::Manager::Part->find_by(partnumber => $::form->{part}{partnumber})
1203
     ) {
1204
    return $self->js->error(t8('The partnumber is already being used'))->render;
1205
  }
1206

  
1207
  $self->parse_form;
1208

  
1209
  @errors = $self->part->validate;
1210
  return \@errors if @errors;
1211

  
1212
  if ($is_new) {
1213
    # Ensure CVars that should be enabled by default actually are when
1214
    # creating new parts.
1215
    my @default_valid_configs =
1216
      grep { ! $_->{flag_defaults_to_invalid} }
1217
      grep { $_->{module} eq 'IC' }
1218
      @{ CVar->get_configs() };
1219

  
1220
    $::form->{"cvar_" . $_->{name} . "_valid"} = 1 for @default_valid_configs;
1221
  } else {
1222
    $self->{lastcost_modified} = $self->check_lastcost_modified;
1223
  }
1224

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

  
1228
    $self->part->save(cascade => 1);
1229
    $self->part->set_lastcost_assemblies_and_assortiments if $self->{lastcost_modified};
1230

  
1231
    SL::DB::History->new(
1232
      trans_id    => $self->part->id,
1233
      snumbers    => 'partnumber_' . $self->part->partnumber,
1234
      employee_id => SL::DB::Manager::Employee->current->id,
1235
      what_done   => 'part',
1236
      addition    => 'SAVED',
1237
    )->save();
1238

  
1239
    CVar->save_custom_variables(
1240
      dbh           => $self->part->db->dbh,
1241
      module        => 'IC',
1242
      trans_id      => $self->part->id,
1243
      variables     => $::form, # $::form->{cvar} would be nicer
1244
      save_validity => 1,
1245
    );
1246

  
1247
    1;
1248
  }) || push(@errors, $self->part->db->error);
1249

  
1250
  return \@errors;
1251
}
1252

  
1253
sub save_with_render_error {
1254
  my ($self) = @_;
1255

  
1256
  # checks that depend only on submitted $::form
1257
  if (!$self->check_form) {
1258
    $self->js->render();
1259
    return 0;
1260
  }
1261

  
1262
  my $errors = $self->save();
1263

  
1264
  if (scalar @{ $errors }) {
1265
    $self->js->error(t8('The item couldn\'t be saved!'));
1266
    $self->js->error(@{ $errors });
1267
    $self->js->render();
1268
    return 0;
1269
  }
1270

  
1271
  return 1;
1272
}
1273

  
1274
sub js_reset_part_after_save {
1275
  my ($self) = @_;
1276

  
1277
  $self->js
1278
    ->val('#part_id',               $self->part->id)
1279
    ->val('#part_partnumber',       $self->part->partnumber)
1280
    ->val('#part_weight_as_number', $self->part->weight_as_number)
1281
    ->val('#last_modification',    "$self->part->last_modification");
1282
}
1245 1283

  
1246 1284
# get_set_inits for partpicker
1247 1285

  
......
2133 2171

  
2134 2172
Doesn't work for recursive items.
2135 2173

  
2174
=item C<save>
2175

  
2176
Helper function for saving the part object in $self->part. Returns a error list
2177
on failure.
2178

  
2179
=item C<save_with_render_error>
2180

  
2181
Helper function for checking the form, saving the part object and error
2182
rendering. Returns 1 on success and 0 when a error is rendered.
2183

  
2184
It can be called like this:
2185

  
2186
  $self->save_with_render_error() or return;
2187

  
2188
=item C<js_reset_part_after_save>
2189

  
2190
Helper function for updating changed values after save in the form via js. It
2191
chances 'part_id', 'part_partnumber', 'part_weight_as_number' and
2192
'last_modification'.
2193

  
2136 2194
=back
2137 2195

  
2138 2196
=head1 GET SET INITS

Auch abrufbar als: Unified diff