Kundenstammdaten: Stundensatz bearbeiten können
[kivitendo-erp.git] / SL / Controller / CustomerVendor.pm
1 package SL::Controller::CustomerVendor;
2
3 use strict;
4 use parent qw(SL::Controller::Base);
5
6 use SL::JSON;
7 use SL::DBUtils;
8 use SL::Helper::Flash;
9 use SL::Locale::String;
10
11 use SL::DB::Customer;
12 use SL::DB::Vendor;
13 use SL::DB::Business;
14 use SL::DB::Employee;
15 use SL::DB::Language;
16 use SL::DB::TaxZone;
17 use SL::DB::Note;
18 use SL::DB::PaymentTerm;
19 use SL::DB::Pricegroup;
20 use SL::DB::Contact;
21 use SL::DB::FollowUp;
22 use SL::DB::FollowUpLink;
23 use SL::DB::History;
24 use SL::DB::Currency;
25
26 # safety
27 __PACKAGE__->run_before(
28   sub {
29     $::auth->assert('customer_vendor_edit');
30   }
31 );
32 __PACKAGE__->run_before(
33   '_instantiate_args',
34   only => [
35     'save',
36     'save_and_ap_transaction',
37     'save_and_ar_transaction',
38     'save_and_close',
39     'save_and_invoice',
40     'save_and_order',
41     'save_and_quotation',
42     'save_and_rfq',
43     'delete',
44     'delete_contact',
45     'delete_shipto',
46   ]
47 );
48
49 __PACKAGE__->run_before(
50   '_load_customer_vendor',
51   only => [
52     'edit',
53     'update',
54     'ajaj_get_shipto',
55     'ajaj_get_contact',
56   ]
57 );
58 __PACKAGE__->run_before(
59   '_create_customer_vendor',
60   only => [
61     'add',
62   ]
63 );
64
65 __PACKAGE__->run_before('normalize_name');
66
67
68 sub action_add {
69   my ($self) = @_;
70
71   $self->_pre_render();
72   $self->render(
73     'customer_vendor/form',
74     title => ($self->is_vendor() ? $::locale->text('Add Vendor') : $::locale->text('Add Customer')),
75     %{$self->{template_args}}
76   );
77 }
78
79 sub action_edit {
80   my ($self) = @_;
81
82   $self->_pre_render();
83   $self->render(
84     'customer_vendor/form',
85     title => ($self->is_vendor() ? $::locale->text('Edit Vendor') : $::locale->text('Edit Customer')),
86     %{$self->{template_args}}
87   );
88 }
89
90 sub _save {
91   my ($self) = @_;
92
93   my $db = $self->{cv}->db;
94
95   $db->do_transaction(sub {
96     my $cvs_by_nr;
97     if ( $self->is_vendor() ) {
98       if ( $self->{cv}->vendornumber ) {
99         $cvs_by_nr = SL::DB::Manager::Vendor->get_all(query => [vendornumber => $self->{cv}->vendornumber]);
100       }
101     } else {
102       if ( $self->{cv}->customernumber ) {
103         $cvs_by_nr = SL::DB::Manager::Customer->get_all(query => [customernumber => $self->{cv}->customernumber]);
104       }
105     }
106
107     foreach my $entry (@{$cvs_by_nr}) {
108       if( $entry->id != $self->{cv}->id ) {
109         my $msg =
110           $self->is_vendor() ? $::locale->text('This vendor number is already in use.') : $::locale->text('This customer number is already in use.');
111
112         $::form->error($msg);
113       }
114     }
115
116     $self->{cv}->save(cascade => 1);
117
118     $self->{contact}->cp_cv_id($self->{cv}->id);
119     if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
120       $self->{contact}->save();
121     }
122
123     if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
124
125       if ( !$self->{note_followup}->follow_up_date ) {
126         $::form->error($::locale->text('Date missing!'));
127       }
128
129       $self->{note}->trans_id($self->{cv}->id);
130       $self->{note}->save();
131
132       $self->{note_followup}->save();
133
134       $self->{note_followup_link}->follow_up_id($self->{note_followup}->id);
135       $self->{note_followup_link}->trans_id($self->{cv}->id);
136       $self->{note_followup_link}->save();
137
138       SL::Helper::Flash::flash_later('info', $::locale->text('Follow-Up saved.'));
139     }
140
141     $self->{shipto}->trans_id($self->{cv}->id);
142     if( $self->{shipto}->shiptoname ne '' ) {
143       $self->{shipto}->save();
144     }
145
146     my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
147     SL::DB::History->new(
148       trans_id => $self->{cv}->id,
149       snumbers => $snumbers,
150       employee_id => SL::DB::Manager::Employee->current->id,
151       addition => 'SAVED',
152     )->save();
153
154     if ( $::form->{delete_notes} ) {
155       foreach my $note_id (@{ $::form->{delete_notes} }) {
156         my $note = SL::DB::Note->new(id => $note_id)->load();
157         if ( $note->follow_up ) {
158           if ( $note->follow_up->follow_up_link ) {
159             $note->follow_up->follow_up_link->delete(cascade => 'delete');
160           }
161           $note->follow_up->delete(cascade => 'delete');
162         }
163         $note->delete(cascade => 'delete');
164       }
165     }
166   }) || die($db->error);
167
168 }
169
170 sub action_save {
171   my ($self) = @_;
172
173   if (!$self->{cv}->name) {
174     flash('error', t8('Customer missing!'));
175     $self->_pre_render();
176     $self->render(
177       'customer_vendor/form',
178       title => ($self->is_vendor() ? t8('Edit Vendor') : t8('Edit Customer')),
179       %{$self->{template_args}}
180     );
181   } else {
182
183     $self->_save();
184
185     my @redirect_params = (
186       action => 'edit',
187       id     => $self->{cv}->id,
188       db     => ($self->is_vendor() ? 'vendor' : 'customer'),
189     );
190
191     if ( $self->{contact}->cp_id ) {
192       push(@redirect_params, contact_id => $self->{contact}->cp_id);
193     }
194
195     if ( $self->{shipto}->shipto_id ) {
196       push(@redirect_params, shipto_id => $self->{shipto}->shipto_id);
197     }
198
199     $self->redirect_to(@redirect_params);
200   }
201 }
202
203 sub action_save_and_close {
204   my ($self) = @_;
205
206   $self->_save();
207
208   my $msg = $self->is_vendor() ? $::locale->text('Vendor saved') : $::locale->text('Customer saved');
209   $::form->redirect($msg);
210 }
211
212 sub _transaction {
213   my ($self, $script) = @_;
214
215   $::auth->assert('general_ledger         | invoice_edit         | vendor_invoice_edit | ' .
216                  ' request_quotation_edit | sales_quotation_edit | sales_order_edit    | purchase_order_edit');
217
218   $self->_save();
219
220   my $callback = $::form->escape($::form->{callback}, 1);
221   my $name = $::form->escape($self->{cv}->name, 1);
222   my $db = $self->is_vendor() ? 'vendor' : 'customer';
223
224   my $url = $self->url_for(
225     controller => $script,
226     action     => 'add',
227     vc         => $db,
228     $db .'_id' => $self->{cv}->id,
229     $db        => $name,
230     type       => $::form->{type},
231     callback   => $callback,
232   );
233
234   print $::form->redirect_header($url);
235 }
236
237 sub action_save_and_ar_transaction {
238   my ($self) = @_;
239
240   $main::auth->assert('general_ledger');
241
242   $self->_transaction('ar.pl');
243 }
244
245 sub action_save_and_ap_transaction {
246   my ($self) = @_;
247
248   $main::auth->assert('general_ledger');
249
250   $self->_transaction('ap.pl');
251 }
252
253 sub action_save_and_invoice {
254   my ($self) = @_;
255
256   if ( $self->is_vendor() ) {
257     $::auth->assert('vendor_invoice_edit');
258   } else {
259     $::auth->assert('invoice_edit');
260   }
261
262   $::form->{type} = 'invoice';
263   $self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
264 }
265
266 sub action_save_and_order {
267   my ($self) = @_;
268
269   if ( $self->is_vendor() ) {
270     $::auth->assert('purchase_order_edit');
271   } else {
272     $::auth->assert('sales_order_edit');
273   }
274
275   $::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
276   $self->_transaction('oe.pl');
277 }
278
279 sub action_save_and_rfq {
280   my ($self) = @_;
281
282   $::auth->assert('request_quotation_edit');
283
284   $::form->{type} = 'request_quotation';
285   $self->_transaction('oe.pl');
286 }
287
288 sub action_save_and_quotation {
289   my ($self) = @_;
290
291   $::auth->assert('sales_quotation_edit');
292
293   $::form->{type} = 'sales_quotation';
294   $self->_transaction('oe.pl');
295 }
296
297 sub action_delete {
298   my ($self) = @_;
299
300   my $db = $self->{cv}->db;
301
302   if( !$self->is_orphaned() ) {
303     $self->action_edit();
304   } else {
305
306     $db->do_transaction(sub {
307       $self->{cv}->delete(cascade => 1);
308
309       my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
310       SL::DB::History->new(
311         trans_id => $self->{cv}->id,
312         snumbers => $snumbers,
313         employee_id => SL::DB::Manager::Employee->current->id,
314         addition => 'DELETED',
315       )->save();
316     }) || die($db->error);
317
318     my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
319     $::form->redirect($msg);
320   }
321
322 }
323
324
325 sub action_delete_contact {
326   my ($self) = @_;
327
328   my $db = $self->{contact}->db;
329
330   if ( !$self->{contact}->cp_id ) {
331     SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
332   } else {
333
334     $db->do_transaction(sub {
335       if ( $self->{contact}->used ) {
336         $self->{contact}->detach();
337         $self->{contact}->save();
338         SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
339       } else {
340         $self->{contact}->delete(cascade => 1);
341         SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
342       }
343     }) || die($db->error);
344
345     $self->{contact} = SL::DB::Contact->new();
346   }
347
348   $self->action_edit();
349 }
350
351 sub action_delete_shipto {
352   my ($self) = @_;
353
354   my $db = $self->{shipto}->db;
355
356   if ( !$self->{shipto}->shipto_id ) {
357     SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
358   } else {
359
360     $db->do_transaction(sub {
361       if ( $self->{shipto}->used ) {
362         $self->{shipto}->detach();
363         $self->{shipto}->save(cascade => 1);
364         SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
365       } else {
366         $self->{shipto}->delete(cascade => 1);
367         SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
368       }
369     }) || die($db->error);
370
371     $self->{shipto} = SL::DB::Shipto->new();
372   }
373
374   $self->action_edit();
375 }
376
377
378 sub action_search {
379   my ($self) = @_;
380
381   my @url_params = (
382     controller => 'ct.pl',
383     action => 'search',
384     db => $self->is_vendor() ? 'vendor' : 'customer',
385   );
386
387   if ( $::form->{callback} ) {
388     push(@url_params, callback => $::form->{callback});
389   }
390
391   $self->redirect_to(@url_params);
392 }
393
394
395 sub action_search_contact {
396   my ($self) = @_;
397
398   my $url = 'ct.pl?action=search_contact&db=customer';
399
400   if ( $::form->{callback} ) {
401     $url .= '&callback='. $::form->escape($::form->{callback});
402   }
403
404   print $::form->redirect_header($url);
405 }
406
407
408 sub action_get_delivery {
409   my ($self) = @_;
410
411   $::auth->assert('sales_all_edit');
412
413   my $dbh = $::form->get_standard_dbh();
414
415   my ($arap, $db, $qty_sign);
416   if ( $self->is_vendor() ) {
417     $arap = 'ap';
418     $db = 'vendor';
419     $qty_sign = ' * -1 AS qty';
420   } else {
421     $arap = 'ar';
422     $db = 'customer';
423     $qty_sign = '';
424   }
425
426   my $where = ' WHERE 1=1';
427   my @values;
428
429   if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
430     $where .= " AND ${arap}.shipto_id = ?";
431     push(@values, $::form->{shipto_id});
432   } else {
433     $where .= " AND ${arap}.${db}_id = ?";
434     push(@values, $::form->{id});
435   }
436
437   if ( $::form->{delivery_from} ) {
438     $where .= " AND ${arap}.transdate >= ?";
439     push(@values, conv_date($::form->{delivery_from}));
440   }
441
442   if ( $::form->{delivery_to} ) {
443     $where .= " AND ${arap}.transdate <= ?";
444     push(@values, conv_date($::form->{delivery_to}));
445   }
446
447   my $query =
448     "SELECT
449        s.shiptoname,
450        i.qty ${qty_sign},
451        ${arap}.id,
452        ${arap}.transdate,
453        ${arap}.invnumber,
454        ${arap}.ordnumber,
455        i.description,
456        i.unit,
457        i.sellprice,
458        oe.id AS oe_id,
459        invoice
460      FROM ${arap}
461
462      LEFT JOIN shipto s
463       ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
464
465      LEFT JOIN invoice i
466        ON ${arap}.id = i.trans_id
467
468      LEFT JOIN parts p
469        ON p.id = i.parts_id
470
471      LEFT JOIN oe
472        ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '')
473
474      ${where}
475      ORDER BY ${arap}.transdate DESC LIMIT 15";
476
477   $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
478
479   $self->render('customer_vendor/get_delivery', { layout => 0 });
480 }
481
482 sub action_ajaj_get_shipto {
483   my ($self) = @_;
484
485   my $data = {
486     map(
487       {
488         my $name = 'shipto'. $_;
489         $name => $self->{shipto}->$name;
490       }
491       qw(_id name department_1 department_2 street zipcode city country contact phone fax email)
492     )
493   };
494
495   $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
496 }
497
498 sub action_ajaj_get_contact {
499   my ($self) = @_;
500
501   my $data;
502
503   $data->{contact} = {
504     map(
505       {
506         my $name = 'cp_'. $_;
507
508         if ( $_ eq 'birthday' && $self->{contact}->$name ) {
509           $name => $self->{contact}->$name->to_lxoffice;
510         } else {
511           $name => $self->{contact}->$name;
512         }
513       }
514       qw(
515         id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
516         satphone satfax project street zipcode city privatphone privatemail birthday
517       )
518     )
519   };
520
521   $data->{contact_cvars} = {
522     map(
523       {
524         if ( $_->config->type eq 'number' ) {
525           $_->config->name => $::form->format_amount(\%::myconfig, $_->value, -2);
526         } else {
527           $_->config->name => $_->value;
528         }
529       }
530       grep(
531         { $_->is_valid; }
532         @{$self->{contact}->cvars_by_config}
533       )
534     )
535   };
536
537   $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
538 }
539
540 sub action_ajaj_customer_autocomplete {
541   my ($self, %params) = @_;
542
543   my $limit = $::form->{limit} || 20;
544   my $type  = $::form->{type}  || {};
545   my $query = { ilike => '%'. $::form->{term} .'%' };
546
547   my @filter;
548   push(
549     @filter,
550     $::form->{column} ? ($::form->{column} => $query) : (or => [ customernumber => $query, name => $query ])
551   );
552
553   my $customers = SL::DB::Manager::Customer->get_all(query => [ @filter ], limit => $limit);
554   my $value_col = $::form->{column} || 'name';
555
556   my $data = [
557     map(
558       {
559         {
560           value => $_->can($value_col)->($_),
561           label => $_->displayable_name,
562           id    => $_->id,
563           customernumber => $_->customernumber,
564           name  => $_->name,
565         }
566       }
567       @{$customers}
568     )
569   ];
570
571   $self->render(\SL::JSON::to_json($data), { layout => 0, type => 'json' });
572 }
573
574 sub is_vendor {
575   return $::form->{db} eq 'vendor';
576 }
577
578 sub is_customer {
579   return $::form->{db} eq 'customer';
580 }
581
582 sub is_orphaned {
583   my ($self) = @_;
584
585   if ( defined($self->{_is_orphaned}) ) {
586     return $self->{_is_orphaned};
587   }
588
589   my $arap      = $self->is_vendor ? 'ap' : 'ar';
590   my $num_args  = 2;
591
592   my $cv = $self->is_vendor ? 'vendor' : 'customer';
593
594   my $query =
595    'SELECT a.id
596     FROM '. $arap .' AS a
597     JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
598     WHERE ct.id = ?
599
600     UNION
601
602     SELECT a.id
603     FROM oe a
604     JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
605     WHERE ct.id = ?';
606
607
608   if ( $self->is_vendor ) {
609     $query .=
610      ' UNION
611       SELECT 1 FROM makemodel mm WHERE mm.make = ?';
612     $num_args++;
613   }
614
615   my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
616
617   return $self->{_is_orphaned} = !$dummy;
618 }
619
620 sub _instantiate_args {
621   my ($self) = @_;
622
623   my $curr_employee = SL::DB::Manager::Employee->current;
624
625   if ( $::form->{cv}->{id} ) {
626     if ( $self->is_vendor() ) {
627       $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
628     } else {
629       $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
630     }
631   } else {
632     if ( $self->is_vendor() ) {
633       $self->{cv} = SL::DB::Vendor->new();
634     } else {
635       $self->{cv} = SL::DB::Customer->new();
636     }
637   }
638   $self->{cv}->assign_attributes(%{$::form->{cv}});
639
640   if ( $self->is_customer() && $::form->{cv}->{taxincluded_checked} eq '' ) {
641     $self->{cv}->taxincluded_checked(undef);
642   }
643
644   $self->{cv}->hourly_rate($::lx_office_conf{'features/customer'}->{default_hourly_rate}) if $self->is_customer && !$self->{cv}->hourly_rate;
645
646   foreach my $cvar (@{$self->{cv}->cvars_by_config()}) {
647     my $value = $::form->{cv_cvars}->{$cvar->config->name};
648
649     if ( $cvar->config->type eq 'number' ) {
650       $value = $::form->parse_amount(\%::myconfig, $value);
651     }
652
653     $cvar->value($value);
654   }
655
656   if ( $::form->{note}->{id} ) {
657     $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
658     $self->{note_followup} = $self->{note}->follow_up;
659     $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
660   } else {
661     $self->{note} = SL::DB::Note->new();
662     $self->{note_followup} = SL::DB::FollowUp->new();
663     $self->{note_followup_link} = SL::DB::FollowUpLink->new();
664   }
665
666   $self->{note}->assign_attributes(%{$::form->{note}});
667   $self->{note}->created_by($curr_employee->id);
668   $self->{note}->trans_module('ct');
669
670   $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
671   $self->{note_followup}->note($self->{note});
672   $self->{note_followup}->created_by($curr_employee->id);
673
674   $self->{note_followup_link}->trans_type($self->is_vendor() ? 'vendor' : 'customer');
675   $self->{note_followup_link}->trans_info($self->{cv}->name);
676
677   if ( $::form->{shipto}->{shipto_id} ) {
678     $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
679   } else {
680     $self->{shipto} = SL::DB::Shipto->new();
681   }
682   $self->{shipto}->assign_attributes(%{$::form->{shipto}});
683   $self->{shipto}->module('CT');
684
685   if ( $::form->{contact}->{cp_id} ) {
686     $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
687   } else {
688     $self->{contact} = SL::DB::Contact->new();
689   }
690   $self->{contact}->assign_attributes(%{$::form->{contact}});
691
692   foreach my $cvar (@{$self->{contact}->cvars_by_config()}) {
693     my $value = $::form->{contact_cvars}->{$cvar->config->name};
694
695     if ( $cvar->config->type eq 'number' ) {
696       $value = $::form->parse_amount(\%::myconfig, $value);
697     }
698
699     $cvar->value($value);
700   }
701 }
702
703 sub _load_customer_vendor {
704   my ($self) = @_;
705
706   if ( $self->is_vendor() ) {
707     $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
708   } else {
709     $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
710   }
711
712   if ( $::form->{note_id} ) {
713     $self->{note} = SL::DB::Note->new(id => $::form->{note_id})->load();
714     $self->{note_followup} = $self->{note}->follow_up;
715     $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
716   } else {
717     $self->{note} = SL::DB::Note->new();
718     $self->{note_followup} = SL::DB::FollowUp->new();
719     $self->{note_followup_link} = SL::DB::FollowUpLink->new();
720   }
721
722   if ( $::form->{shipto_id} ) {
723     $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
724
725     if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
726       die($::locale->text('Error'));
727     }
728   } else {
729     $self->{shipto} = SL::DB::Shipto->new();
730   }
731
732   if ( $::form->{contact_id} ) {
733     $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
734
735     if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
736       die($::locale->text('Error'));
737     }
738   } else {
739     $self->{contact} = SL::DB::Contact->new();
740   }
741 }
742
743 sub _create_customer_vendor {
744   my ($self) = @_;
745
746   if ( $self->is_vendor() ) {
747     $self->{cv} = SL::DB::Vendor->new();
748   } else {
749     $self->{cv} = SL::DB::Customer->new();
750   }
751   $self->{cv}->currency_id($::instance_conf->get_currency_id());
752
753   $self->{note} = SL::DB::Note->new();
754
755   $self->{note_followup} = SL::DB::FollowUp->new();
756
757   $self->{shipto} = SL::DB::Shipto->new();
758
759   $self->{contact} = SL::DB::Contact->new();
760 }
761
762 sub _pre_render {
763   my ($self) = @_;
764
765   my $dbh = $::form->get_standard_dbh();
766
767   my $query;
768
769   $self->{all_business} = SL::DB::Manager::Business->get_all();
770
771   $self->{all_employees} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
772
773   $query =
774     'SELECT DISTINCT(greeting)
775      FROM customer
776      WHERE greeting IS NOT NULL AND greeting != \'\'
777      UNION
778        SELECT DISTINCT(greeting)
779        FROM vendor
780        WHERE greeting IS NOT NULL AND greeting != \'\'
781      ORDER BY greeting';
782   $self->{all_greetings} = [
783     map(
784       { $_->{greeting}; }
785       selectall_hashref_query($::form, $dbh, $query)
786     )
787   ];
788
789   $query =
790     'SELECT DISTINCT(cp_title) AS title
791      FROM contacts
792      WHERE cp_title IS NOT NULL AND cp_title != \'\'
793      ORDER BY cp_title';
794   $self->{all_titles} = [
795     map(
796       { $_->{title}; }
797       selectall_hashref_query($::form, $dbh, $query)
798     )
799   ];
800
801   $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
802
803   $self->{all_languages} = SL::DB::Manager::Language->get_all();
804
805   $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all();
806
807   if ( $::instance_conf->get_vertreter() ) {
808     $query =
809       'SELECT id
810        FROM business
811        WHERE salesman';
812     my $business_ids = [
813       map(
814         { $_->{id}; }
815         selectall_hashref_query($::form, $dbh, $query)
816       )
817     ];
818
819     if ( $business_ids->[0] ) {
820       $self->{all_salesman_customers} = SL::DB::Manager::Customer->get_all(query => [business_id => $business_ids]);
821     } else {
822       $self->{all_salesman_customers} = [];
823     }
824   } else {
825     $self->{all_salesmen} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $self->{cv}->salesman_id,  deleted => 0 ] ]);
826   }
827
828   $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all();
829
830   $self->{all_delivery_terms} = SL::DB::Manager::DeliveryTerm->get_all();
831
832   $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all();
833
834   $query =
835     'SELECT DISTINCT(cp_abteilung) AS department
836      FROM contacts
837      WHERE cp_abteilung IS NOT NULL AND cp_abteilung != \'\'
838      ORDER BY cp_abteilung';
839   $self->{all_departments} = [
840     map(
841       { $_->{department}; }
842       selectall_hashref_query($::form, $dbh, $query)
843     )
844   ];
845
846   $self->{contacts} = $self->{cv}->contacts;
847   $self->{contacts} ||= [];
848
849   $self->{shiptos} = $self->{cv}->shipto;
850   $self->{shiptos} ||= [];
851
852   $self->{notes} = SL::DB::Manager::Note->get_all(
853     query => [
854       trans_id => $self->{cv}->id,
855       trans_module => 'ct',
856     ],
857     with_objects => ['follow_up'],
858   );
859
860   $self->{template_args} ||= {};
861
862   $::request->{layout}->add_javascripts('autocomplete_customer.js');
863   $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');
864 }
865
866 sub normalize_name {
867   my ($self) = @_;
868
869   # check if feature is enabled (select normalize_vc_names from defaults)
870   return unless ($::instance_conf->get_normalize_vc_names);
871
872   return unless $self->{cv};
873   my $name = $self->{cv}->name;
874   $name =~ s/\s+$//;
875   $name =~ s/^\s+//;
876   $name =~ s/\s+/ /g;
877   $self->{cv}->name($name);
878 }
879
880 1;