Notiz nach dem Speichern nicht wieder öffnen
[kivitendo-erp.git] / SL / Controller / CustomerVendor.pm
index e64e4b5..46270bc 100644 (file)
@@ -18,6 +18,9 @@ use SL::DB::PaymentTerm;
 use SL::DB::Pricegroup;
 use SL::DB::Contact;
 use SL::DB::FollowUp;
+use SL::DB::FollowUpLink;
+use SL::DB::History;
+use SL::DB::Currency;
 
 # safety
 __PACKAGE__->run_before(
@@ -37,6 +40,7 @@ __PACKAGE__->run_before(
     'save_and_order',
     'save_and_quotation',
     'save_and_rfq',
+    'delete',
     'delete_contact',
     'delete_shipto',
   ]
@@ -54,11 +58,11 @@ __PACKAGE__->run_before(
 __PACKAGE__->run_before(
   '_create_customer_vendor',
   only => [
-    'new',
+    'add',
   ]
 );
 
-sub action_new {
+sub action_add {
   my ($self) = @_;
 
   $self->_pre_render();
@@ -83,49 +87,68 @@ sub action_edit {
 sub _save {
   my ($self) = @_;
 
-  my $cvs_by_nr;
-  if ( $self->is_vendor() ) {
-    if ( $self->{cv}->vendornumber ) {
-      $cvs_by_nr = SL::DB::Manager::Vendor->get_all(query => [vendornumber => $self->{cv}->vendornumber]);
+  my $db = $self->{cv}->db;
+
+  $db->do_transaction(sub {
+    my $cvs_by_nr;
+    if ( $self->is_vendor() ) {
+      if ( $self->{cv}->vendornumber ) {
+        $cvs_by_nr = SL::DB::Manager::Vendor->get_all(query => [vendornumber => $self->{cv}->vendornumber]);
+      }
+    } else {
+      if ( $self->{cv}->customernumber ) {
+        $cvs_by_nr = SL::DB::Manager::Customer->get_all(query => [customernumber => $self->{cv}->customernumber]);
+      }
     }
-  }
-  else {
-    if ( $self->{cv}->customernumber ) {
-      $cvs_by_nr = SL::DB::Manager::Customer->get_all(query => [customernumber => $self->{cv}->customernumber]);
+
+    foreach my $entry (@{$cvs_by_nr}) {
+      if( $entry->id != $self->{cv}->id ) {
+        my $msg =
+          $self->is_vendor() ? $::locale->text('This vendor number is already in use.') : $::locale->text('This customer number is already in use.');
+
+        $::form->error($msg);
+      }
     }
-  }
 
-  foreach my $entry (@{$cvs_by_nr}) {
-    if( $entry->id != $self->{cv}->id ) {
-      my $msg =
-        $self->is_vendor() ? $::locale->text('This vendor number is already in use.') : $::locale->text('This customer number is already in use.');
+    $self->{cv}->save(cascade => 1);
 
-      $::form->error($msg);
+    $self->{contact}->cp_cv_id($self->{cv}->id);
+    if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
+      $self->{contact}->save();
     }
-  }
 
-  $self->{cv}->save(cascade => 1);
+    if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
 
-  $self->{contact}->cp_cv_id($self->{cv}->id);
-  if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
-    $self->{contact}->save();
-  }
+      if ( !$self->{note_followup}->follow_up_date ) {
+        $::form->error($::locale->text('Date missing!'));
+      }
 
-  if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
-    $self->{note}->trans_id($self->{cv}->id);
-    $self->{note}->save();
-    $self->{note_followup}->save();
+      $self->{note}->trans_id($self->{cv}->id);
+      $self->{note}->save();
 
-    $self->{note} = SL::DB::Note->new();
-    $self->{note_followup} = SL::DB::FollowUp->new();
-  }
+      $self->{note_followup}->save();
 
-  $self->{shipto}->trans_id($self->{cv}->id);
-  if( $self->{shipto}->shiptoname ne '' ) {
-    $self->{shipto}->save();
-  }
+      $self->{note_followup_link}->follow_up_id($self->{note_followup}->id);
+      $self->{note_followup_link}->trans_id($self->{cv}->id);
+      $self->{note_followup_link}->save();
+
+      SL::Helper::Flash::flash_later('info', $::locale->text('Follow-Up saved.'));
+    }
+
+    $self->{shipto}->trans_id($self->{cv}->id);
+    if( $self->{shipto}->shiptoname ne '' ) {
+      $self->{shipto}->save();
+    }
+
+    my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
+    SL::DB::History->new(
+      trans_id => $self->{cv}->id,
+      snumbers => $snumbers,
+      employee_id => SL::DB::Manager::Employee->current->id,
+      addition => 'SAVED',
+    )->save();
+  }) || die($db->error);
 
-  #TODO: history
 }
 
 sub action_save {
@@ -133,7 +156,21 @@ sub action_save {
 
   $self->_save();
 
-  $self->action_edit();
+  my @redirect_params = (
+    action => 'edit',
+    id     => $self->{cv}->id,
+    db     => ($self->is_vendor() ? 'vendor' : 'customer'),
+  );
+
+  if ( $self->{contact}->cp_id ) {
+    push(@redirect_params, contact_id => $self->{contact}->cp_id);
+  }
+
+  if ( $self->{shipto}->shipto_id ) {
+    push(@redirect_params, shipto_id => $self->{shipto}->shipto_id);
+  }
+
+  $self->redirect_to(@redirect_params);
 }
 
 sub action_save_and_close {
@@ -157,14 +194,15 @@ sub _transaction {
   my $name = $::form->escape($self->{cv}->name, 1);
   my $db = $self->is_vendor() ? 'vendor' : 'customer';
 
-  my $url =
-    $script .'?'.
-    'action=add&'.
-    'vc='. $db .'&'.
-    $db .'_id=' . $self->{cv}->id .'&'.
-    $db .'='. $name .'&'.
-    'type='. $::form->{type} .'&'.
-    'callback='. $callback;
+  my $url = $self->url_for(
+    controller => $script,
+    action     => 'add',
+    vc         => $db,
+    $db .'_id' => $self->{cv}->id,
+    $db        => $name,
+    type       => $::form->{type},
+    callback   => $callback,
+  );
 
   print $::form->redirect_header($url);
 }
@@ -232,15 +270,23 @@ sub action_save_and_quotation {
 sub action_delete {
   my ($self) = @_;
 
-  if( !$self->is_orphaned() ) {
-    SL::Helper::Flash::flash('error', $::locale->text('blaabla'));
+  my $db = $self->{cv}->db;
 
+  if( !$self->is_orphaned() ) {
     $self->action_edit();
-  }
-  else {
-    $self->{cv}->delete();
+  } else {
+
+    $db->do_transaction(sub {
+      $self->{cv}->delete(cascade => 1);
 
-    #TODO: history
+      my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
+      SL::DB::History->new(
+        trans_id => $self->{cv}->id,
+        snumbers => $snumbers,
+        employee_id => SL::DB::Manager::Employee->current->id,
+        addition => 'DELETED',
+      )->save();
+    }) || die($db->error);
 
     my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
     $::form->redirect($msg);
@@ -252,17 +298,22 @@ sub action_delete {
 sub action_delete_contact {
   my ($self) = @_;
 
+  my $db = $self->{contact}->db;
+
   if ( !$self->{contact}->cp_id ) {
     SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
   } else {
-    if ( $self->{contact}->used ) {
-      $self->{contact}->detach();
-      $self->{contact}->save();
-      SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
-    } else {
-      $self->{contact}->delete();
-      SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
-    }
+
+    $db->do_transaction(sub {
+      if ( $self->{contact}->used ) {
+        $self->{contact}->detach();
+        $self->{contact}->save();
+        SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
+      } else {
+        $self->{contact}->delete(cascade => 1);
+        SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
+      }
+    }) || die($db->error);
 
     $self->{contact} = SL::DB::Contact->new();
   }
@@ -273,17 +324,22 @@ sub action_delete_contact {
 sub action_delete_shipto {
   my ($self) = @_;
 
+  my $db = $self->{shipto}->db;
+
   if ( !$self->{shipto}->shipto_id ) {
     SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
   } else {
-    if ( $self->{shipto}->used ) {
-      $self->{shipto}->detach();
-      $self->{shipto}->save();
-      SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
-    } else {
-      $self->{shipto}->delete();
-      SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
-    }
+
+    $db->do_transaction(sub {
+      if ( $self->{shipto}->used ) {
+        $self->{shipto}->detach();
+        $self->{shipto}->save(cascade => 1);
+        SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
+      } else {
+        $self->{shipto}->delete(cascade => 1);
+        SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
+      }
+    }) || die($db->error);
 
     $self->{shipto} = SL::DB::Shipto->new();
   }
@@ -291,6 +347,37 @@ sub action_delete_shipto {
   $self->action_edit();
 }
 
+
+sub action_search {
+  my ($self) = @_;
+
+  my @url_params = (
+    controller => 'ct.pl',
+    action => 'search',
+    db => $self->is_vendor() ? 'vendor' : 'customer',
+  );
+
+  if ( $::form->{callback} ) {
+    push(@url_params, callback => $::form->{callback});
+  }
+
+  $self->redirect_to(@url_params);
+}
+
+
+sub action_search_contact {
+  my ($self) = @_;
+
+  my $url = 'ct.pl?action=search_contact&db=customer';
+
+  if ( $::form->{callback} ) {
+    $url .= '&callback='. $::form->escape($::form->{callback});
+  }
+
+  print $::form->redirect_header($url);
+}
+
+
 sub action_get_delivery {
   my ($self) = @_;
 
@@ -301,8 +388,7 @@ sub action_get_delivery {
     $arap = 'ap';
     $db = 'vendor';
     $qty_sign = ' * -1 AS qty';
-  }
-  else {
+  } else {
     $arap = 'ar';
     $db = 'customer';
     $qty_sign = '';
@@ -389,8 +475,7 @@ sub action_ajaj_get_contact {
 
         if ( $_ eq 'birthday' && $self->{contact}->$name ) {
           $name => $self->{contact}->$name->to_lxoffice;
-        }
-        else {
+        } else {
           $name => $self->{contact}->$name;
         }
       }
@@ -406,8 +491,7 @@ sub action_ajaj_get_contact {
       {
         if ( $_->config->type eq 'number' ) {
           $_->config->name => $::form->format_amount(\%::myconfig, $_->value, -2);
-        }
-        else {
+        } else {
           $_->config->name => $_->value;
         }
       }
@@ -506,30 +590,26 @@ sub _instantiate_args {
 
   my $curr_employee = SL::DB::Manager::Employee->current;
 
-  foreach ( 'cv.creditlimit', 'cv.discount' ) {
-    my ($namespace, $varname) = split('.', $_, 2);
-    $::form->{$namespace}->{$varname} = $::form->parse_amount(\%::myconfig, $::form->{$namespace}->{$varname});
-  }
-  $::form->{cv}->{discount} /= 100;
-
   if ( $::form->{cv}->{id} ) {
     if ( $self->is_vendor() ) {
       $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
-    }
-    else {
+    } else {
       $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
     }
-  }
-  else {
+  } else {
     if ( $self->is_vendor() ) {
       $self->{cv} = SL::DB::Vendor->new();
-    }
-    else {
+    } else {
       $self->{cv} = SL::DB::Customer->new();
     }
   }
   $self->{cv}->assign_attributes(%{$::form->{cv}});
 
+  if ( $self->is_customer() && $::form->{cv}->{taxincluded_checked} eq '' ) {
+    $self->{cv}->taxincluded_checked(undef);
+  }
+
+
   foreach my $cvar (@{$self->{cv}->cvars_by_config()}) {
     my $value = $::form->{cv_cvars}->{$cvar->config->name};
 
@@ -540,53 +620,50 @@ sub _instantiate_args {
     $cvar->value($value);
   }
 
-#  foreach my $cvar_key (keys(%{$::form->{cv_cvars}})) {
-#    my $cvar_value = $::form->{cv_cvars}->{$cvar_key};
-#    my $cvar = $self->{cv}->cvar_by_name($cvar_key);
-#    $cvar->value($cvar_value);
-#  }
-
   if ( $::form->{note}->{id} ) {
     $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
-  }
-  else {
+
+    $self->{note_followup_link} = SL::DB::Manager::FollowUpLink->get_all(
+      query => [
+        'follow_up.note_id' => $self->{note}->id,
+        trans_id => $self->{cv}->id,
+        trans_type => ($self->is_vendor() ? 'vendor' : 'customer'),
+      ],
+      with_objects => ['follow_up'],
+    )->[0];
+
+    $self->{note_followup} = $self->{note_followup_link}->follow_up;
+  } else {
     $self->{note} = SL::DB::Note->new();
+    $self->{note_followup} = SL::DB::FollowUp->new();
+    $self->{note_followup_link} = SL::DB::FollowUpLink->new();
   }
+
   $self->{note}->assign_attributes(%{$::form->{note}});
   $self->{note}->created_by($curr_employee->id);
   $self->{note}->trans_module('ct');
-  #  if ( $self->{note}->trans_id != $self->{cv}->id ) {
-  #    die($::locale->text('Error'));
-  #  }
 
-
-  $self->{note_followup} = SL::DB::FollowUp->new();
   $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
   $self->{note_followup}->note($self->{note});
   $self->{note_followup}->created_by($curr_employee->id);
 
+  $self->{note_followup_link}->trans_type($self->is_vendor() ? 'vendor' : 'customer');
+  $self->{note_followup_link}->trans_info($self->{cv}->name);
+
   if ( $::form->{shipto}->{shipto_id} ) {
     $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
-  }
-  else {
+  } else {
     $self->{shipto} = SL::DB::Shipto->new();
   }
   $self->{shipto}->assign_attributes(%{$::form->{shipto}});
   $self->{shipto}->module('CT');
-#  if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
-#    die($::locale->text('Error'));
-#  }
 
   if ( $::form->{contact}->{cp_id} ) {
     $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
-  }
-  else {
+  } else {
     $self->{contact} = SL::DB::Contact->new();
   }
   $self->{contact}->assign_attributes(%{$::form->{contact}});
-#  if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
-#    die($::locale->text('Error'));
-#  }
 
   foreach my $cvar (@{$self->{contact}->cvars_by_config()}) {
     my $value = $::form->{contact_cvars}->{$cvar->config->name};
@@ -604,13 +681,28 @@ sub _load_customer_vendor {
 
   if ( $self->is_vendor() ) {
     $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
-  }
-  else {
+  } else {
     $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
   }
 
-  $self->{note} = SL::DB::Note->new();
-  $self->{note_followup} = SL::DB::FollowUp->new();
+  if ( $::form->{note_id} ) {
+    $self->{note} = SL::DB::Note->new(id => $::form->{note_id})->load();
+
+    $self->{note_followup_link} = SL::DB::Manager::FollowUpLink->get_all(
+      query => [
+        'follow_up.note_id' => $self->{note}->id,
+        trans_id => $self->{cv}->id,
+        trans_type => ($self->is_vendor() ? 'vendor' : 'customer'),
+      ],
+      with_objects => ['follow_up'],
+    )->[0];
+
+    $self->{note_followup} = $self->{note_followup_link}->follow_up;
+  } else {
+    $self->{note} = SL::DB::Note->new();
+    $self->{note_followup} = SL::DB::FollowUp->new();
+    $self->{note_followup_link} = SL::DB::FollowUpLink->new();
+  }
 
   if ( $::form->{shipto_id} ) {
     $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
@@ -618,8 +710,7 @@ sub _load_customer_vendor {
     if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
       die($::locale->text('Error'));
     }
-  }
-  else {
+  } else {
     $self->{shipto} = SL::DB::Shipto->new();
   }
 
@@ -629,8 +720,7 @@ sub _load_customer_vendor {
     if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
       die($::locale->text('Error'));
     }
-  }
-  else {
+  } else {
     $self->{contact} = SL::DB::Contact->new();
   }
 }
@@ -640,10 +730,10 @@ sub _create_customer_vendor {
 
   if ( $self->is_vendor() ) {
     $self->{cv} = SL::DB::Vendor->new();
-  }
-  else {
+  } else {
     $self->{cv} = SL::DB::Customer->new();
   }
+  $self->{cv}->currency_id($::instance_conf->get_currency_id());
 
   $self->{note} = SL::DB::Note->new();
 
@@ -663,7 +753,7 @@ sub _pre_render {
 
   $self->{all_business} = SL::DB::Manager::Business->get_all();
 
-  $self->{all_employees} = SL::DB::Manager::Employee->get_all();
+  $self->{all_employees} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
 
   $query =
     'SELECT DISTINCT(greeting)
@@ -693,26 +783,32 @@ sub _pre_render {
     )
   ];
 
-  $query =
-    'SELECT curr
-     FROM defaults';
-  my $curr = selectall_hashref_query($::form, $dbh, $query)->[0]->{curr};
-  my @currencies = grep(
-    { $_; }
-    map(
-      { s/\s//g; $_; }
-      split(m/:/, $curr)
-    )
-  );
-  $self->{all_currencies} = \@currencies;
+  $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
 
   $self->{all_languages} = SL::DB::Manager::Language->get_all();
 
   $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all();
 
-  #Employee:
-  #TODO: ALL_SALESMAN
-  #TODO: ALL_SALESMAN_CUSTOMERS
+  if ( $::instance_conf->get_vertreter() ) {
+    $query =
+      'SELECT id
+       FROM business
+       WHERE salesman';
+    my $business_ids = [
+      map(
+        { $_->{id}; }
+        selectall_hashref_query($::form, $dbh, $query)
+      )
+    ];
+
+    if ( $business_ids->[0] ) {
+      $self->{all_salesman_customers} = SL::DB::Manager::Customer->get_all(query => [business_id => $business_ids]);
+    } else {
+      $self->{all_salesman_customers} = [];
+    }
+  } else {
+    $self->{all_salesmen} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $self->{cv}->salesman_id,  deleted => 0 ] ]);
+  }
 
   $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all();
 
@@ -736,12 +832,10 @@ sub _pre_render {
   $self->{shiptos} = $self->{cv}->shipto;
   $self->{shiptos} ||= [];
 
-  $self->{template_args} = {};
-
-  $self->{cv}->discount($self->{cv}->discount * 100);
-
+  $self->{template_args} ||= {};
 
   $::request->{layout}->add_javascripts('autocomplete_customer.js');
+  $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');
 }
 
 1;