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