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