Zahlen beim CSV-Export ohne Tausendertrennzeichen formatieren
[kivitendo-erp.git] / bin / mozilla / ct.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors: Reed White <alta@alta-research.com>
16 #
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
21 #
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #======================================================================
30 #
31 # customer/vendor module
32 #
33 #======================================================================
34
35 # $locale->text('Customers')
36 # $locale->text('Vendors')
37 # $locale->text('Add Customer')
38 # $locale->text('Add Vendor')
39 # $locale->text('Edit Customer')
40 # $locale->text('Edit Vendor')
41 # $locale->text('Customer saved!')
42 # $locale->text('Vendor saved!')
43 # $locale->text('Customer deleted!')
44 # $locale->text('Cannot delete customer!')
45 # $locale->text('Vendor deleted!')
46 # $locale->text('Cannot delete vendor!')
47
48 use CGI::Ajax;
49 use POSIX qw(strftime);
50
51 use SL::CT;
52 use SL::CVar;
53 use SL::ReportGenerator;
54
55 require "bin/mozilla/common.pl";
56 require "bin/mozilla/reportgenerator.pl";
57
58 use strict;
59 1;
60
61 # end of main
62
63 sub add {
64   $main::lxdebug->enter_sub();
65
66   $main::auth->assert('customer_vendor_edit');
67
68   my $form     = $main::form;
69   my %myconfig = %main::myconfig;
70
71   $form->{title}    = "Add";
72   $form->{callback} = "$form->{script}?action=add&db=$form->{db}" unless $form->{callback};
73
74   CT->populate_drop_down_boxes(\%myconfig, \%$form);
75
76   &form_header;
77   &form_footer;
78
79   $main::lxdebug->leave_sub();
80 }
81
82 sub search {
83   $main::lxdebug->enter_sub();
84
85   $main::auth->assert('customer_vendor_edit');
86
87   my $form     = $main::form;
88   my $locale   = $main::locale;
89
90   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
91
92   $form->get_lists("business_types" => "ALL_BUSINESS_TYPES");
93   $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
94
95   $form->{CUSTOM_VARIABLES}                  = CVar->get_configs('module' => 'CT');
96   ($form->{CUSTOM_VARIABLES_FILTER_CODE},
97    $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables'      => $form->{CUSTOM_VARIABLES},
98                                                                            'include_prefix' => 'l_',
99                                                                            'include_value'  => 'Y');
100
101   $form->{jsscript} = 1;
102   $form->{title}    = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
103   $form->{fokus}    = 'Form.name';
104
105   $form->header();
106   print $form->parse_html_template('ct/search');
107
108   $main::lxdebug->leave_sub();
109 }
110
111 sub list_names {
112   $main::lxdebug->enter_sub();
113
114   $main::auth->assert('customer_vendor_edit');
115
116   my $form     = $main::form;
117   my %myconfig = %main::myconfig;
118   my $locale   = $main::locale;
119
120   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
121
122   report_generator_set_default_sort('name', 1);
123
124   CT->search(\%myconfig, \%$form);
125
126   my $cvar_configs = CVar->get_configs('module' => 'CT');
127
128   my @options;
129   if ($form->{status} eq 'all') {
130     push @options, $locale->text('All');
131   } elsif ($form->{status} eq 'orphaned') {
132     push @options, $locale->text('Orphaned');
133   }
134
135   push @options, $locale->text('Name') . " : $form->{name}"                                    if $form->{name};
136   push @options, $locale->text('Contact') . " : $form->{contact}"                              if $form->{contact};
137   push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}|                 if $form->{"$form->{db}number"};
138   push @options, $locale->text('E-mail') . " : $form->{email}"                                 if $form->{email};
139   push @options, $locale->text('Contact person (surname)')           . " : $form->{cp_name}"   if $form->{cp_name};
140   push @options, $locale->text('Billing/shipping address (city)')    . " : $form->{addr_city}" if $form->{addr_city};
141   push @options, $locale->text('Billing/shipping address (zipcode)') . " : $form->{zipcode}"   if $form->{addr_zipcode};
142   push @options, $locale->text('Billing/shipping address (street)')  . " : $form->{street}"    if $form->{addr_street};
143
144   my @columns = (
145     'id',        'name',      "$form->{db}number",   'contact',  'phone',
146     'fax',       'email',     'taxnumber',           'street',   'zipcode' , 'city',
147     'business',  'invnumber', 'ordnumber',           'quonumber'
148   );
149
150   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
151   my @searchable_custom_variables  = grep { $_->{searchable} }  @{ $cvar_configs };
152   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
153
154   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
155
156   my %column_defs = (
157     'id'                => { 'text' => $locale->text('ID'), },
158     "$form->{db}number" => { 'text' => $locale->text('Number'), },
159     'name'              => { 'text' => $form->{IS_CUSTOMER} ? $::locale->text('Customer Name') : $::locale->text('Vendor Name'), },
160     'contact'           => { 'text' => $locale->text('Contact'), },
161     'phone'             => { 'text' => $locale->text('Phone'), },
162     'fax'               => { 'text' => $locale->text('Fax'), },
163     'email'             => { 'text' => $locale->text('E-mail'), },
164     'cc'                => { 'text' => $locale->text('Cc'), },
165     'taxnumber'         => { 'text' => $locale->text('Tax Number'), },
166     'business'          => { 'text' => $locale->text('Type of Business'), },
167     'invnumber'         => { 'text' => $locale->text('Invoice'), },
168     'ordnumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
169     'quonumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation')   : $locale->text('Request for Quotation'), },
170     'street'            => { 'text' => $locale->text('Street'), },
171     'zipcode'           => { 'text' => $locale->text('Zipcode'), },
172     'city'              => { 'text' => $locale->text('City'), },
173     %column_defs_cvars,
174   );
175
176   map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
177
178   my @hidden_variables  = (qw(db status obsolete name contact email cp_name addr_city), "$form->{db}number", @searchable_custom_variables, map { "l_$_" } @columns);
179   my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
180   my $callback          = build_std_url('action=list_names', grep { $form->{$_} } @hidden_nondefault);
181   $form->{callback}     = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
182
183   foreach (@columns) {
184     my $sortdir              = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
185     $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
186   }
187
188   my ($ordertype, $quotationtype, $attachment_basename);
189   if ($form->{IS_CUSTOMER}) {
190     $form->{title}       = $locale->text('Customers');
191     $ordertype           = 'sales_order';
192     $quotationtype       = 'sales_quotation';
193     $attachment_basename = $locale->text('customer_list');
194
195   } else {
196     $form->{title}       = $locale->text('Vendors');
197     $ordertype           = 'purchase_order';
198     $quotationtype       = 'request_quotation';
199     $attachment_basename = $locale->text('vendor_list');
200   }
201
202   my $report = SL::ReportGenerator->new(\%myconfig, $form);
203
204   $report->set_options('top_info_text'         => join("\n", @options),
205                        'raw_bottom_info_text'  => $form->parse_html_template('ct/list_names_bottom'),
206                        'output_format'         => 'HTML',
207                        'title'                 => $form->{title},
208                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
209     );
210   $report->set_options_from_form();
211   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
212
213   $report->set_columns(%column_defs);
214   $report->set_column_order(@columns);
215
216   $report->set_export_options('list_names', @hidden_variables, qw(sort sortdir));
217
218   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
219
220   CVar->add_custom_variables_to_report('module'         => 'CT',
221                                        'trans_id_field' => 'id',
222                                        'configs'        => $cvar_configs,
223                                        'column_defs'    => \%column_defs,
224                                        'data'           => $form->{CT});
225
226   my $previous_id;
227
228   foreach my $ref (@{ $form->{CT} }) {
229     my $row = { map { $_ => { 'data' => '' } } @columns };
230
231     if ($ref->{id} ne $previous_id) {
232       $previous_id = $ref->{id};
233       map { $row->{$_}->{data} = $ref->{$_} } @columns;
234
235       $row->{name}->{link}  = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
236       $row->{email}->{link} = 'mailto:' . E($ref->{email});
237     }
238
239     my $base_url              = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
240     $row->{invnumber}->{link} = $base_url;
241     $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
242     $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
243     my $column                = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
244     $row->{$column}->{data}   = $ref->{$column};
245
246     $report->add_data($row);
247   }
248
249   $report->generate_with_headers();
250
251   $main::lxdebug->leave_sub();
252 }
253
254 sub edit {
255   $main::lxdebug->enter_sub();
256
257   $main::auth->assert('customer_vendor_edit');
258
259   my $form     = $main::form;
260   my %myconfig = %main::myconfig;
261
262   # show history button
263   $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
264   #/show hhistory button
265
266   CT->get_tuple(\%myconfig, \%$form);
267   CT->populate_drop_down_boxes(\%myconfig, \%$form);
268
269   $form->{title} = "Edit";
270
271   # format discount
272   $form->{discount} *= 100;
273
274   &form_header;
275   &form_footer;
276
277   $main::lxdebug->leave_sub();
278 }
279
280 sub form_header {
281   $main::lxdebug->enter_sub();
282
283   $main::auth->assert('customer_vendor_edit');
284
285   my $form     = $main::form;
286   my %myconfig = %main::myconfig;
287   my $locale   = $main::locale;
288
289   $form->get_lists(employees => "ALL_EMPLOYEES",
290                    taxzones  => "ALL_TAXZONES");
291   $form->get_pricegroup(\%myconfig, { all => 1 });
292
293   $form->get_lists(customers => { key => "ALL_SALESMAN_CUSTOMERS", business_is_salesman => 1 }) if $::vertreter;
294
295   $form->{ALL_SALESMEN}   = $form->{ALL_EMPLOYEES};
296   $form->{taxincluded}    = ($form->{taxincluded}) ? "checked" : "";
297   $form->{is_admin}       = $myconfig{role} eq 'admin';
298   $form->{is_customer}    = $form->{db}     eq 'customer';
299   $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
300   $form->{shipto_label}   = sub { my $s = shift(@_); join('; ', grep { $_ } map { $s->{"shipto$_"} } qw(name department_1 street city)) || ' ' };
301   $form->{contacts_label} = sub { join ", ", grep { $_ } $_[0]->{cp_name}, $_[0]->{cp_givenname} };
302   $form->{taxzone_id}     = 0                                                               if !$form->{id};
303   $form->{jsscript}       = 1;
304   $form->{fokus}          = "ct.greeting";
305   $form->{AJAX}           = [ new CGI::Ajax( map {; "get_$_" => "$form->{script}?action=get_$_" } qw(shipto contact delivery) ) ];
306
307   unshift @{ $form->{SHIPTO} },   +{ shipto_id => '0', shiptoname => '' }, +{ shipto_id => '0', shiptoname => 'Alle' };
308   unshift @{ $form->{CONTACTS} }, +{ cp_id     => '0', cp_name => $locale->text('New contact') };
309
310   $form->{title} = $form->{title_save}
311                 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
312
313   CT->query_titles_and_greetings(\%myconfig, \%$form);
314   map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(TITLES GREETINGS COMPANY_GREETINGS DEPARTMENT);
315
316   $form->{NOTES} ||= [ ];
317
318   $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'CT', 'trans_id' => $form->{id});
319
320   CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
321
322   $form->header;
323   print $form->parse_html_template('ct/form_header');
324
325   $main::lxdebug->leave_sub();
326 }
327
328 sub form_footer {
329   $main::lxdebug->enter_sub();
330
331   $main::auth->assert('customer_vendor_edit');
332
333   my $form     = $main::form;
334
335   print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
336                                                        is_customer => $form->{db}     eq 'customer' });
337   $main::lxdebug->leave_sub();
338 }
339
340 sub _do_save {
341   $main::auth->assert('customer_vendor_edit & ' .
342                       '(general_ledger         | invoice_edit         | vendor_invoice_edit | ' .
343                       ' request_quotation_edit | sales_quotation_edit | sales_order_edit    | purchase_order_edit)');
344
345   $::form->isblank("name", $::locale->text("Name missing!"));
346
347   if ($::form->{new_salesman_id} && $::vertreter) {
348     $::form->{salesman_id} = $::form->{new_salesman_id};
349     delete $::form->{new_salesman_id};
350   }
351
352   my $res = $::form->{db} eq 'customer' ? CT->save_customer(\%::myconfig, $::form) : CT->save_vendor(\%::myconfig, $::form);
353
354   if (3 == $res) {
355     if ($::form->{"db"} eq "customer") {
356       $::form->error($::locale->text('This customer number is already in use.'));
357     } else {
358       $::form->error($::locale->text('This vendor number is already in use.'));
359     }
360   }
361 }
362
363 sub add_transaction {
364   $main::lxdebug->enter_sub();
365
366   $main::auth->assert('customer_vendor_edit & ' .
367                 '(general_ledger         | invoice_edit         | vendor_invoice_edit | ' .
368                 ' request_quotation_edit | sales_quotation_edit | sales_order_edit    | purchase_order_edit)');
369
370   my $form     = $main::form;
371   my %myconfig = %main::myconfig;
372   my $locale   = $main::locale;
373
374 #  # saving the history
375 #  if(!exists $form->{addition}) {
376 #    $form->{addition} = "ADD TRANSACTION";
377 #    $form->save_history;
378 #  }
379 #  # /saving the history
380
381   _do_save();
382
383   $form->{callback} = $form->escape($form->{callback}, 1);
384   my $name = $form->escape("$form->{name}", 1);
385
386   $form->{callback} =
387     "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
388   $form->redirect;
389
390   $main::lxdebug->leave_sub();
391 }
392
393 sub save_and_ap_transaction {
394   $main::lxdebug->enter_sub();
395
396   $main::auth->assert('customer_vendor_edit & general_ledger');
397
398   my $form     = $main::form;
399   my %myconfig = %main::myconfig;
400
401   $form->{script} = "ap.pl";
402   # saving the history
403   if(!exists $form->{addition}) {
404     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
405     $form->{addition} = "SAVED";
406     $form->save_history;
407   }
408   # /saving the history
409   &add_transaction;
410   $main::lxdebug->leave_sub();
411 }
412
413 sub save_and_ar_transaction {
414   $main::lxdebug->enter_sub();
415
416   $main::auth->assert('customer_vendor_edit & general_ledger');
417
418   my $form     = $main::form;
419   my %myconfig = %main::myconfig;
420
421   $form->{script} = "ar.pl";
422   # saving the history
423   if(!exists $form->{addition}) {
424     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
425     $form->{addition} = "SAVED";
426     $form->save_history;
427   }
428   # /saving the history
429   &add_transaction;
430   $main::lxdebug->leave_sub();
431 }
432
433 sub save_and_invoice {
434   $main::lxdebug->enter_sub();
435
436   my $form     = $main::form;
437   my %myconfig = %main::myconfig;
438
439   if ($form->{db} eq 'customer') {
440     $main::auth->assert('customer_vendor_edit & invoice_edit');
441   } else {
442     $main::auth->assert('customer_vendor_edit & vendor_invoice_edit');
443   }
444
445   $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
446   $form->{type} = "invoice";
447   # saving the history
448   if(!exists $form->{addition}) {
449     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
450     $form->{addition} = "SAVED";
451     $form->save_history;
452   }
453   # /saving the history
454   &add_transaction;
455   $main::lxdebug->leave_sub();
456 }
457
458 sub save_and_rfq {
459   $main::lxdebug->enter_sub();
460
461   $main::auth->assert('customer_vendor_edit & request_quotation_edit');
462
463   my $form     = $main::form;
464   my %myconfig = %main::myconfig;
465
466   $form->{script} = "oe.pl";
467   $form->{type}   = "request_quotation";
468   # saving the history
469   if(!exists $form->{addition}) {
470     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
471     $form->{addition} = "SAVED";
472     $form->save_history;
473   }
474   # /saving the history
475   &add_transaction;
476   $main::lxdebug->leave_sub();
477 }
478
479 sub save_and_quotation {
480   $main::lxdebug->enter_sub();
481
482   $main::auth->assert('customer_vendor_edit & sales_quotation_edit');
483
484   my $form     = $main::form;
485   my %myconfig = %main::myconfig;
486
487   $form->{script} = "oe.pl";
488   $form->{type}   = "sales_quotation";
489   # saving the history
490   if(!exists $form->{addition}) {
491     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
492     $form->{addition} = "SAVED";
493     $form->save_history;
494   }
495   # /saving the history
496   &add_transaction;
497   $main::lxdebug->leave_sub();
498 }
499
500 sub save_and_order {
501   $main::lxdebug->enter_sub();
502
503   my $form     = $main::form;
504   my %myconfig = %main::myconfig;
505
506   if ($form->{db} eq 'customer') {
507     $main::auth->assert('customer_vendor_edit & sales_order_edit');
508   } else {
509     $main::auth->assert('customer_vendor_edit & purchase_order_edit');
510   }
511
512   $form->{script} = "oe.pl";
513   $form->{type}   =
514     ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
515   # saving the history
516   if(!exists $form->{addition}) {
517     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
518     $form->{addition} = "SAVED";
519     $form->save_history;
520   }
521   # /saving the history
522   &add_transaction;
523   $main::lxdebug->leave_sub();
524 }
525
526 sub save_and_close {
527   $main::lxdebug->enter_sub();
528
529   $main::auth->assert('customer_vendor_edit');
530
531   my $form     = $main::form;
532   my %myconfig = %main::myconfig;
533   my $locale   = $main::locale;
534
535   my $msg = ucfirst $form->{db};
536   $msg .= " saved!";
537
538   _do_save();
539
540   # saving the history
541   if(!exists $form->{addition}) {
542     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
543     $form->{addition} = "SAVED";
544     $form->save_history;
545   }
546   # /saving the history
547   $form->redirect($locale->text($msg));
548
549   $main::lxdebug->leave_sub();
550 }
551
552 sub save {
553   $main::lxdebug->enter_sub();
554
555   $main::auth->assert('customer_vendor_edit');
556
557   my $form     = $main::form;
558   my %myconfig = %main::myconfig;
559   my $locale   = $main::locale;
560
561   my $msg = ucfirst $form->{db};
562   $msg .= " saved!";
563
564   _do_save();
565
566   # saving the history
567   if(!exists $form->{addition}) {
568     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
569     $form->{addition} = "SAVED";
570     $form->save_history;
571   }
572   # /saving the history
573   &edit;
574
575   $main::lxdebug->leave_sub();
576   ::end_of_request();
577 }
578
579 sub delete {
580   $main::lxdebug->enter_sub();
581
582   $main::auth->assert('customer_vendor_edit');
583
584   my $form     = $main::form;
585   my %myconfig = %main::myconfig;
586   my $locale   = $main::locale;
587
588   CT->delete(\%myconfig, \%$form);
589
590   my $msg = ucfirst $form->{db};
591   $msg .= " deleted!";
592   # saving the history
593   if(!exists $form->{addition}) {
594     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
595     $form->{addition} = "DELETED";
596     $form->save_history;
597   }
598   # /saving the history
599   $form->redirect($locale->text($msg));
600
601   $main::lxdebug->leave_sub();
602 }
603
604 sub display {
605   $main::lxdebug->enter_sub();
606
607   $main::auth->assert('customer_vendor_edit');
608
609   my $form     = $main::form;
610
611   &form_header();
612   &form_footer();
613
614   $main::lxdebug->leave_sub();
615 }
616
617 sub update {
618   $main::lxdebug->enter_sub();
619
620   $main::auth->assert('customer_vendor_edit');
621
622   my $form     = $main::form;
623
624   &display();
625   $main::lxdebug->leave_sub();
626 }
627
628 sub get_contact {
629   $main::lxdebug->enter_sub();
630
631   $main::auth->assert('customer_vendor_edit');
632
633   my $form     = $main::form;
634   my %myconfig = %main::myconfig;
635
636   CT->get_contact(\%myconfig, \%$form);
637   print $form->ajax_response_header(), join '__pjx__', map $form->{"cp_$_"},
638     qw(name title givenname phone1 phone2 email abteilung fax mobile1 mobile2 satphone satfax project privatphone privatemail birthday used gender);
639   $main::lxdebug->leave_sub();
640
641 }
642
643 sub get_shipto {
644   $main::lxdebug->enter_sub();
645
646   $main::auth->assert('customer_vendor_edit');
647
648   my $form     = $main::form;
649   my %myconfig = %main::myconfig;
650
651   CT->get_shipto(\%myconfig, \%$form);
652   print $form->ajax_response_header(), join('__pjx__', map($form->{"shipto$_"}, qw(name department_1 department_2 street zipcode city country contact phone fax email used)));
653   $main::lxdebug->leave_sub();
654
655 }
656
657 sub get_delivery {
658   $::lxdebug->enter_sub;
659
660   $::auth->assert('customer_vendor_edit');
661   $::auth->assert('sales_all_edit');
662
663   CT->get_delivery(\%::myconfig, $::form );
664
665   print $::form->ajax_response_header,
666         $::form->parse_html_template('ct/get_delivery', {
667           is_customer =>  $::form->{db} eq 'customer',
668         });
669
670   $::lxdebug->leave_sub;
671 }
672
673 sub delete_shipto {
674   $main::lxdebug->enter_sub();
675
676   $main::auth->assert('customer_vendor_edit');
677
678   my $form     = $main::form;
679   my %myconfig = %main::myconfig;
680
681   CT->get_shipto(\%myconfig, \%$form);
682
683   unless ($form->{shiptoused}) {
684     CT->delete_shipto($form->{shipto_id});
685     @$form{ grep /^shipto/, keys %$form } = undef;
686   }
687
688   edit();
689
690   $main::lxdebug->leave_sub();
691 }
692
693 sub delete_contact {
694   $main::lxdebug->enter_sub();
695
696   $main::auth->assert('customer_vendor_edit');
697
698   my $form     = $main::form;
699   my %myconfig = %main::myconfig;
700
701   CT->get_contact(\%myconfig, \%$form);
702
703   unless ($form->{cp_used}) {
704     CT->delete_shipto($form->{cp_id});
705     @$form{ grep /^cp_/, keys %$form } = undef;
706   }
707
708   edit();
709
710   $main::lxdebug->leave_sub();
711 }
712
713 sub ajax_autocomplete {
714   $main::lxdebug->enter_sub();
715
716   my $form     = $main::form;
717   my %myconfig = %main::myconfig;
718
719   $form->{column}          = 'name'     unless $form->{column} =~ /^name$/;
720   $form->{vc}              = 'customer' unless $form->{vc} =~ /^customer|vendor$/;
721   $form->{db}              = $form->{vc}; # CT expects this
722   $form->{$form->{column}} = $form->{q}           || '';
723   $form->{limit}           = ($form->{limit} * 1) || 10;
724   $form->{searchitems}   ||= '';
725
726   CT->search(\%myconfig, $form);
727
728   print $form->ajax_response_header(),
729         $form->parse_html_template('ct/ajax_autocomplete');
730
731   $main::lxdebug->leave_sub();
732 }
733
734 sub continue { call_sub($main::form->{nextsub}); }