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