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