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