]> wagnertech.de Git - mfinanz.git/blobdiff - SL/Controller/Order.pm
Auftrags-Controller: customer/vendor als Methode in einer Variablen ablegen
[mfinanz.git] / SL / Controller / Order.pm
index 1669269872b7b87d321a56458b83581d821734c5..4dac30bdad847d036e426d83e9a661954e054b22 100644 (file)
@@ -82,8 +82,14 @@ sub action_update {
 sub action_save {
   my ($self) = @_;
 
-  $self->_save();
+  my $errors = $self->_save();
 
+  if (scalar @{ $errors }) {
+    $self->js->flash('error', $_) foreach @{ $errors };
+    return $self->js->render($self);
+  }
+
+  flash_later('info', 'The order has been saved');
   my @redirect_params = (
     action => 'edit',
     type   => $self->type,
@@ -127,21 +133,26 @@ sub action_add_item {
   my ($self) = @_;
 
   my $form_attr = $::form->{add_item};
-  my $item      = SL::DB::OrderItem->new;
+
+  return unless $form_attr->{parts_id};
+
+  my $item = SL::DB::OrderItem->new;
   $item->assign_attributes(%$form_attr);
 
   my $part        = SL::DB::Part->new(id => $form_attr->{parts_id})->load;
-  my $cv_discount = $self->order->customer? $self->order->customer->discount : 0.0;
+  my $cv_method   = $self->cv;
+  my $cv_discount = $self->order->$cv_method? $self->order->$cv_method->discount : 0.0;
 
   my %new_attr;
-  $new_attr{id}          = join('_', 'new', Time::HiRes::gettimeofday(), int rand 1000000000000);
   $new_attr{part}        = $part;
-  $new_attr{description} = $part->description if ! $item->{description};
-  $new_attr{qty}         = 1.0                if ! $item->{qty};
+  $new_attr{description} = $part->description if ! $item->description;
+  $new_attr{qty}         = 1.0                if ! $item->qty;
   $new_attr{unit}        = $part->unit;
-  $new_attr{sellprice}   = $part->sellprice   if ! $item->{sellprice};
-  $new_attr{discount}    = $cv_discount       if ! $item->{discount};
+  $new_attr{sellprice}   = $part->sellprice   if ! $item->sellprice;
+  $new_attr{discount}    = $cv_discount       if ! $item->discount;
+
   $item->assign_attributes(%new_attr);
+  $item->id(join('_', 'new', Time::HiRes::gettimeofday(), int rand 1000000000000)) if !$item->id;
 
   $self->order->add_items($item);
 
@@ -305,12 +316,15 @@ sub _recalc {
 sub _save {
   my ($self) = @_;
 
+  my $errors = [];
   my $db = $self->order->db;
 
   $db->do_transaction(
     sub {
       $self->order->save();
-  }) || die($db->error);
+  }) || push(@{$errors}, $db->error);
+
+  return $errors;
 }