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