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