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