X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FController%2FCustomerVendor.pm;h=f59fd07b54a3411970187744a5b3afce921d0a4f;hb=15fa34d9610c4fb162779285473a197fffb73659;hp=e226775569b003fec5458dab704356ff2693d19a;hpb=c720367b1964146f4bd77348e19a22503440a8fc;p=kivitendo-erp.git diff --git a/SL/Controller/CustomerVendor.pm b/SL/Controller/CustomerVendor.pm index e22677556..f59fd07b5 100644 --- a/SL/Controller/CustomerVendor.pm +++ b/SL/Controller/CustomerVendor.pm @@ -6,6 +6,7 @@ use parent qw(SL::Controller::Base); use SL::JSON; use SL::DBUtils; use SL::Helper::Flash; +use SL::Locale::String; use SL::DB::Customer; use SL::DB::Vendor; @@ -18,6 +19,7 @@ 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; @@ -27,7 +29,6 @@ __PACKAGE__->run_before( $::auth->assert('customer_vendor_edit'); } ); - __PACKAGE__->run_before( '_instantiate_args', only => [ @@ -61,10 +62,14 @@ __PACKAGE__->run_before( ] ); +__PACKAGE__->run_before('normalize_name'); + + sub action_add { my ($self) = @_; $self->_pre_render(); + $self->{cv}->assign_attributes(hourly_rate => $::instance_conf->get_customer_hourly_rate) if $self->{cv}->is_customer; $self->render( 'customer_vendor/form', title => ($self->is_vendor() ? $::locale->text('Add Vendor') : $::locale->text('Add Customer')), @@ -117,12 +122,21 @@ sub _save { } if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) { + + if ( !$self->{note_followup}->follow_up_date ) { + $::form->error($::locale->text('Date missing!')); + } + $self->{note}->trans_id($self->{cv}->id); $self->{note}->save(); + $self->{note_followup}->save(); - $self->{note} = SL::DB::Note->new(); - $self->{note_followup} = SL::DB::FollowUp->new(); + $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); @@ -137,6 +151,19 @@ sub _save { employee_id => SL::DB::Manager::Employee->current->id, addition => 'SAVED', )->save(); + + if ( $::form->{delete_notes} ) { + foreach my $note_id (@{ $::form->{delete_notes} }) { + my $note = SL::DB::Note->new(id => $note_id)->load(); + if ( $note->follow_up ) { + if ( $note->follow_up->follow_up_link ) { + $note->follow_up->follow_up_link->delete(cascade => 'delete'); + } + $note->follow_up->delete(cascade => 'delete'); + } + $note->delete(cascade => 'delete'); + } + } }) || die($db->error); } @@ -144,9 +171,34 @@ sub _save { sub action_save { my ($self) = @_; - $self->_save(); + if (!$self->{cv}->name) { + flash('error', t8('Customer missing!')); + $self->_pre_render(); + $self->render( + 'customer_vendor/form', + title => ($self->is_vendor() ? t8('Edit Vendor') : t8('Edit Customer')), + %{$self->{template_args}} + ); + } else { + + $self->_save(); + + 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(action => 'edit', id => $self->{cv}->id, db => $self->is_vendor() ? 'vendor' : 'customer'); + $self->redirect_to(@redirect_params); + } } sub action_save_and_close { @@ -170,14 +222,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); } @@ -326,13 +379,17 @@ sub action_delete_shipto { sub action_search { my ($self) = @_; - my $url = 'ct.pl?action=search&db='. ($self->is_vendor() ? 'vendor' : 'customer'); + my @url_params = ( + controller => 'ct.pl', + action => 'search', + db => $self->is_vendor() ? 'vendor' : 'customer', + ); if ( $::form->{callback} ) { - $url .= '&callback='. $::from->escape($::form->{callback}); + push(@url_params, callback => $::form->{callback}); } - print $::form->redirect_header($url); + $self->redirect_to(@url_params); } @@ -342,7 +399,7 @@ sub action_search_contact { my $url = 'ct.pl?action=search_contact&db=customer'; if ( $::form->{callback} ) { - $url .= '&callback='. $::from->escape($::form->{callback}); + $url .= '&callback='. $::form->escape($::form->{callback}); } print $::form->redirect_header($url); @@ -352,6 +409,8 @@ sub action_search_contact { sub action_get_delivery { my ($self) = @_; + $::auth->assert('sales_all_edit'); + my $dbh = $::form->get_standard_dbh(); my ($arap, $db, $qty_sign); @@ -365,21 +424,24 @@ sub action_get_delivery { $qty_sign = ''; } - my $where = ' WHERE 1=1 '; + my $where = ' WHERE 1=1'; my @values; if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) { - $where .= "AND ${arap}.shipto_id = ?"; + $where .= " AND ${arap}.shipto_id = ?"; push(@values, $::form->{shipto_id}); + } else { + $where .= " AND ${arap}.${db}_id = ?"; + push(@values, $::form->{id}); } if ( $::form->{delivery_from} ) { - $where .= "AND ${arap}.transdate >= ?"; + $where .= " AND ${arap}.transdate >= ?"; push(@values, conv_date($::form->{delivery_from})); } if ( $::form->{delivery_to} ) { - $where .= "AND ${arap}.transdate <= ?"; + $where .= " AND ${arap}.transdate <= ?"; push(@values, conv_date($::form->{delivery_to})); } @@ -576,6 +638,12 @@ sub _instantiate_args { } $self->{cv}->assign_attributes(%{$::form->{cv}}); + if ( $self->is_customer() && $::form->{cv}->{taxincluded_checked} eq '' ) { + $self->{cv}->taxincluded_checked(undef); + } + + $self->{cv}->hourly_rate($::instance_conf->get_customer_hourly_rate) if $self->is_customer && !$self->{cv}->hourly_rate; + foreach my $cvar (@{$self->{cv}->cvars_by_config()}) { my $value = $::form->{cv_cvars}->{$cvar->config->name}; @@ -588,18 +656,25 @@ sub _instantiate_args { if ( $::form->{note}->{id} ) { $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load(); + $self->{note_followup} = $self->{note}->follow_up; + $self->{note_followup_link} = $self->{note_followup}->follow_up_link; } 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'); - $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 { @@ -635,8 +710,15 @@ sub _load_customer_vendor { $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} = $self->{note}->follow_up; + $self->{note_followup_link} = $self->{note_followup}->follow_up_link; + } 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(); @@ -667,6 +749,7 @@ sub _create_customer_vendor { } else { $self->{cv} = SL::DB::Customer->new(); } + $self->{cv}->currency_id($::instance_conf->get_currency_id()); $self->{note} = SL::DB::Note->new(); @@ -686,7 +769,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) @@ -722,12 +805,31 @@ sub _pre_render { $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(); + $self->{all_delivery_terms} = SL::DB::Manager::DeliveryTerm->get_all(); + $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all(); $query = @@ -748,9 +850,32 @@ sub _pre_render { $self->{shiptos} = $self->{cv}->shipto; $self->{shiptos} ||= []; - $self->{template_args} = {}; + $self->{notes} = SL::DB::Manager::Note->get_all( + query => [ + trans_id => $self->{cv}->id, + trans_module => 'ct', + ], + with_objects => ['follow_up'], + ); + + $self->{template_args} ||= {}; $::request->{layout}->add_javascripts('autocomplete_customer.js'); + $::request->{layout}->add_javascripts('kivi.CustomerVendor.js'); +} + +sub normalize_name { + my ($self) = @_; + + # check if feature is enabled (select normalize_vc_names from defaults) + return unless ($::instance_conf->get_normalize_vc_names); + + return unless $self->{cv}; + my $name = $self->{cv}->name; + $name =~ s/\s+$//; + $name =~ s/^\s+//; + $name =~ s/\s+/ /g; + $self->{cv}->name($name); } 1;