Artikel anlegen mit Rücksprung in Belege
[kivitendo-erp.git] / SL / Controller / Part.pm
index 20f6752..869a524 100644 (file)
@@ -16,6 +16,7 @@ use DateTime;
 use SL::DB::History;
 use SL::DB::Helper::ValidateAssembly qw(validate_assembly);
 use SL::CVar;
+use SL::MoreCommon qw(save_form);
 use Carp;
 
 use Rose::Object::MakeMethods::Generic (
@@ -40,6 +41,7 @@ __PACKAGE__->run_before('check_part_id', only   => [ qw(edit delete) ]);
 sub action_add_part {
   my ($self, %params) = @_;
 
+  $::form->{callback} = $self->url_for(action => 'add_part') unless $::form->{callback};
   $self->part( SL::DB::Part->new_part );
   $self->add;
 };
@@ -47,6 +49,7 @@ sub action_add_part {
 sub action_add_service {
   my ($self, %params) = @_;
 
+  $::form->{callback} = $self->url_for(action => 'add_service') unless $::form->{callback};
   $self->part( SL::DB::Part->new_service );
   $self->add;
 };
@@ -54,6 +57,7 @@ sub action_add_service {
 sub action_add_assembly {
   my ($self, %params) = @_;
 
+  $::form->{callback} = $self->url_for(action => 'add_assembly') unless $::form->{callback};
   $self->part( SL::DB::Part->new_assembly );
   $self->add;
 };
@@ -61,10 +65,20 @@ sub action_add_assembly {
 sub action_add_assortment {
   my ($self, %params) = @_;
 
+  $::form->{callback} = $self->url_for(action => 'add_assortment') unless $::form->{callback};
   $self->part( SL::DB::Part->new_assortment );
   $self->add;
 };
 
+sub action_add_from_record {
+  my ($self) = @_;
+
+  check_has_valid_part_type($::form->{part}{part_type});
+
+  $self->parse_form;
+  $self->add;
+}
+
 sub action_add {
   my ($self) = @_;
 
@@ -90,8 +104,11 @@ sub action_save {
       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;
   }
 
-  if ( $is_new and !$::form->{part}{partnumber} ) {
-    $self->check_next_transnumber_is_free or return $self->js->error(t8('The next partnumber in the number range already exists!'))->render;
+  if (    $is_new
+       && $::form->{part}{partnumber}
+       && SL::DB::Manager::Part->find_by(partnumber => $::form->{part}{partnumber})
+     ) {
+    return $self->js->error(t8('The partnumber is already being used'))->render;
   }
 
   $self->parse_form;
@@ -128,10 +145,16 @@ sub action_save {
     1;
   }) or return $self->js->error(t8('The item couldn\'t be saved!') . " " . $self->part->db->error )->render;
 
-  flash_later('info', $is_new ? t8('The item has been created.') : t8('The item has been saved.'));
+  ;
+  flash_later('info', $is_new ? t8('The item has been created.') . " " . $self->part->displayable_name : t8('The item has been saved.'));
 
-  # reload item, this also resets last_modification!
-  $self->redirect_to(controller => 'Part', action => 'edit', 'part.id' => $self->part->id);
+  if ( $::form->{callback} ) {
+    $self->redirect_to($::form->unescape($::form->{callback}) . '&new_parts_id=' . $self->part->id);
+
+  } else {
+    # default behaviour after save: reload item, this also resets last_modification!
+    $self->redirect_to(controller => 'Part', action => 'edit', 'part.id' => $self->part->id);
+  }
 }
 
 sub action_save_as_new {
@@ -164,11 +187,15 @@ sub action_delete {
   }) or return $self->js->error(t8('The item couldn\'t be deleted!') . " " . $self->part->db->error)->render;
 
   flash_later('info', t8('The item has been deleted.'));
-  my @redirect_params = (
-    controller => 'controller.pl',
-    action => 'LoginScreen/user_login'
-  );
-  $self->redirect_to(@redirect_params);
+  if ( $::form->{callback} ) {
+    $self->redirect_to($::form->unescape($::form->{callback}));
+  } else {
+    my @redirect_params = (
+        controller => 'controller.pl',
+        action     => 'LoginScreen/user_login'
+    );
+    $self->redirect_to(@redirect_params);
+  }
 }
 
 sub action_use_as_new {
@@ -665,8 +692,6 @@ sub parse_form {
   my $params = delete($::form->{part}) || { };
 
   delete $params->{id};
-  # never overwrite existing partnumber for parts in use, should be a read-only field in that case anyway
-  delete $params->{partnumber} if $self->part->partnumber and not $self->orphaned;
   $self->part->assign_attributes(%{ $params});
   $self->part->bin_id(undef) unless $self->part->warehouse_id;
 
@@ -1024,17 +1049,6 @@ sub form_check_partnumber_is_unique {
 }
 
 # general checking functions
-sub check_next_transnumber_is_free {
-  my ($self) = @_;
-
-  my ($next_transnumber, $count);
-  $self->part->db->with_transaction(sub {
-    $next_transnumber = $self->part->get_next_trans_number;
-    $count = SL::DB::Manager::Part->get_all_count(where => [ partnumber => $next_transnumber ]);
-    return 1;
-  }) or die $@;
-  $count ? return 0 : return 1;
-}
 
 sub check_part_id {
   die t8("Can't load item without a valid part.id") . "\n" unless $::form->{part}{id};