Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 0c9bb179

Von Tamino Steinert vor mehr als 1 Jahr hinzugefügt

  • ID 0c9bb179c64c89f4e23f9e9c30c37134efb4beb7
  • Vorgänger 411c4ff7
  • Nachfolger 3462658a

Part: separate save und save_with_render_error Funktionen

Unterschiede anzeigen:

SL/Controller/Part.pm
107 107
sub action_save {
108 108
  my ($self, %params) = @_;
109 109

  
110
  # checks that depend only on submitted $::form
111
  $self->check_form or return $self->js->render;
112

  
113
  my $is_new = !$self->part->id; # $ part gets loaded here
114

  
115
  # check that the part hasn't been modified
116
  unless ( $is_new ) {
117
    $self->check_part_not_modified or
118
      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;
119
  }
120

  
121
  if (    $is_new
122
       && $::form->{part}{partnumber}
123
       && SL::DB::Manager::Part->find_by(partnumber => $::form->{part}{partnumber})
124
     ) {
125
    return $self->js->error(t8('The partnumber is already being used'))->render;
126
  }
127

  
128
  $self->parse_form;
129

  
130
  my @errors = $self->part->validate;
131
  return $self->js->error(@errors)->render if @errors;
132

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

  
141
    $::form->{"cvar_" . $_->{name} . "_valid"} = 1 for @default_valid_configs;
142
  } else {
143
    $self->{lastcost_modified} = $self->check_lastcost_modified;
144
  }
145

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

  
149
    $self->part->save(cascade => 1);
150
    $self->part->set_lastcost_assemblies_and_assortiments if $self->{lastcost_modified};
151

  
152
    SL::DB::History->new(
153
      trans_id    => $self->part->id,
154
      snumbers    => 'partnumber_' . $self->part->partnumber,
155
      employee_id => SL::DB::Manager::Employee->current->id,
156
      what_done   => 'part',
157
      addition    => 'SAVED',
158
    )->save();
159

  
160
    CVar->save_custom_variables(
161
      dbh           => $self->part->db->dbh,
162
      module        => 'IC',
163
      trans_id      => $self->part->id,
164
      variables     => $::form, # $::form->{cvar} would be nicer
165
      save_validity => 1,
166
    );
110
  my $is_new = !$self->part->id;
167 111

  
168
    1;
169
  }) or return $self->js->error(t8('The item couldn\'t be saved!') . " " . $self->part->db->error )->render;
112
  $self->save_with_render_error() or return;
170 113

  
171 114
  flash_later('info', $is_new ? t8('The item has been created.') . " " . $self->part->displayable_name : t8('The item has been saved.'));
172 115

  
......
1019 962
  );
1020 963
}
1021 964

  
965
sub save {
966
  my ($self) = @_;
967
  my @errors = ();
968

  
969
  my $is_new = !$self->part->id;
970

  
971
  # check that the part hasn't been modified
972
  unless ( $is_new ) {
973
    $self->check_part_not_modified or
974
      return [t8('The document has been changed by another user. Please reopen it in another window and copy the changes to the new window')];
975
  }
976

  
977
  if (    $is_new
978
       && $::form->{part}{partnumber}
979
       && SL::DB::Manager::Part->find_by(partnumber => $::form->{part}{partnumber})
980
     ) {
981
    return $self->js->error(t8('The partnumber is already being used'))->render;
982
  }
983

  
984
  $self->parse_form;
985

  
986
  @errors = $self->part->validate;
987
  return \@errors if @errors;
988

  
989
  if ($is_new) {
990
    # Ensure CVars that should be enabled by default actually are when
991
    # creating new parts.
992
    my @default_valid_configs =
993
      grep { ! $_->{flag_defaults_to_invalid} }
994
      grep { $_->{module} eq 'IC' }
995
      @{ CVar->get_configs() };
996

  
997
    $::form->{"cvar_" . $_->{name} . "_valid"} = 1 for @default_valid_configs;
998
  } else {
999
    $self->{lastcost_modified} = $self->check_lastcost_modified;
1000
  }
1001

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

  
1005
    $self->part->save(cascade => 1);
1006
    $self->part->set_lastcost_assemblies_and_assortiments if $self->{lastcost_modified};
1007

  
1008
    SL::DB::History->new(
1009
      trans_id    => $self->part->id,
1010
      snumbers    => 'partnumber_' . $self->part->partnumber,
1011
      employee_id => SL::DB::Manager::Employee->current->id,
1012
      what_done   => 'part',
1013
      addition    => 'SAVED',
1014
    )->save();
1015

  
1016
    CVar->save_custom_variables(
1017
      dbh           => $self->part->db->dbh,
1018
      module        => 'IC',
1019
      trans_id      => $self->part->id,
1020
      variables     => $::form, # $::form->{cvar} would be nicer
1021
      save_validity => 1,
1022
    );
1023

  
1024
    1;
1025
  }) || push(@errors, $self->part->db->error);
1026

  
1027
  return \@errors;
1028
}
1029

  
1030
sub save_with_render_error {
1031
  my ($self) = @_;
1032

  
1033
  # checks that depend only on submitted $::form
1034
  if (!$self->check_form) {
1035
    $self->js->render();
1036
    return 0;
1037
  }
1038

  
1039
  my $errors = $self->save();
1040

  
1041
  if (scalar @{ $errors }) {
1042
    $self->js->error(t8('The item couldn\'t be saved!'));
1043
    $self->js->error(@{ $errors });
1044
    $self->js->render();
1045
    return 0;
1046
  }
1047

  
1048
  return 1;
1049
}
1050

  
1051
sub js_reset_part_after_save {
1052
  my ($self) = @_;
1053

  
1054
  $self->js
1055
    ->val('#part_id',               $self->part->id)
1056
    ->val('#part_partnumber',       $self->part->partnumber)
1057
    ->val('#part_weight_as_number', $self->part->weight_as_number)
1058
    ->val('#last_modification',    "$self->part->last_modification");
1059
}
1022 1060

  
1023 1061
# get_set_inits for partpicker
1024 1062

  
......
1857 1895

  
1858 1896
Doesn't work for recursive items.
1859 1897

  
1898
=item C<save>
1899

  
1900
Helper function for saving the part object in $self->part. Returns a error list
1901
on failure.
1902

  
1903
=item C<save_with_render_error>
1904

  
1905
Helper function for checking the form, saving the part object and error
1906
rendering. Returns 1 on success and 0 when a error is rendered.
1907

  
1908
It can be called like this:
1909

  
1910
  $self->save_with_render_error() or return;
1911

  
1912
=item C<js_reset_part_after_save>
1913

  
1914
Helper function for updating changed values after save in the form via js. It
1915
chances 'part_id', 'part_partnumber', 'part_weight_as_number' and
1916
'last_modification'.
1917

  
1860 1918
=back
1861 1919

  
1862 1920
=head1 GET SET INITS

Auch abrufbar als: Unified diff