Tabs aus *.pl Dateien entfernt.
[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' => $locale->text('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   $form->get_pricegroup(\%myconfig, { all => 1 });
291
292   $form->{ALL_SALESMEN}   = $form->{ALL_EMPLOYEES};
293   $form->{taxincluded}    = ($form->{taxincluded}) ? "checked" : "";
294   $form->{is_admin}       = $myconfig{role} eq 'admin';
295   $form->{is_customer}    = $form->{db}     eq 'customer';
296   $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
297   $form->{shipto_label}   = sub { my $s = shift(@_); join('; ', grep { $_ } map { $s->{"shipto$_"} } qw(name department_1 street city)) || ' ' };
298   $form->{contacts_label} = sub { join ", ", grep { $_ } $_[0]->{cp_name}, $_[0]->{cp_givenname} };
299   $form->{taxzone_id}     = 0                                                               if !$form->{id};
300   $form->{jsscript}       = 1;
301   $form->{fokus}          = "ct.greeting";
302   $form->{AJAX}           = [ new CGI::Ajax( map {; "get_$_" => "$form->{script}?action=get_$_" } qw(shipto contact delivery) ) ];
303
304   unshift @{ $form->{SHIPTO} },   +{ shipto_id => '0', shiptoname => '' }, +{ shipto_id => '0', shiptoname => 'Alle' };
305   unshift @{ $form->{CONTACTS} }, +{ cp_id     => '0', cp_name => $locale->text('New contact') };
306
307   $form->{title} = $form->{title_save}
308                 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
309
310   CT->query_titles_and_greetings(\%myconfig, \%$form);
311   map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(TITLES GREETINGS COMPANY_GREETINGS DEPARTMENT);
312
313   $form->{NOTES} ||= [ ];
314
315   $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'CT', 'trans_id' => $form->{id});
316
317   CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
318
319   $form->header;
320   print $form->parse_html_template('ct/form_header');
321
322   $main::lxdebug->leave_sub();
323 }
324
325 sub form_footer {
326   $main::lxdebug->enter_sub();
327
328   $main::auth->assert('customer_vendor_edit');
329
330   my $form     = $main::form;
331
332   print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
333                                                        is_customer => $form->{db}     eq 'customer' });
334   $main::lxdebug->leave_sub();
335 }
336
337 sub add_transaction {
338   $main::lxdebug->enter_sub();
339
340   $main::auth->assert('customer_vendor_edit & ' .
341                 '(general_ledger         | invoice_edit         | vendor_invoice_edit | ' .
342                 ' request_quotation_edit | sales_quotation_edit | sales_order_edit    | purchase_order_edit)');
343
344   my $form     = $main::form;
345   my %myconfig = %main::myconfig;
346   my $locale   = $main::locale;
347
348 #  # saving the history
349 #  if(!exists $form->{addition}) {
350 #    $form->{addition} = "ADD TRANSACTION";
351 #    $form->save_history($form->dbconnect(\%myconfig));
352 #  }
353 #  # /saving the history
354
355   $form->isblank("name", $locale->text("Name missing!"));
356   if ($form->{"db"} eq "customer") {
357     CT->save_customer(\%myconfig, \%$form);
358   } else {
359     CT->save_vendor(\%myconfig, \%$form);
360   }
361
362   $form->{callback} = $form->escape($form->{callback}, 1);
363   my $name = $form->escape("$form->{name}", 1);
364
365   $form->{callback} =
366     "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
367   $form->redirect;
368
369   $main::lxdebug->leave_sub();
370 }
371
372 sub save_and_ap_transaction {
373   $main::lxdebug->enter_sub();
374
375   $main::auth->assert('customer_vendor_edit & general_ledger');
376
377   my $form     = $main::form;
378   my %myconfig = %main::myconfig;
379
380   $form->{script} = "ap.pl";
381   # saving the history
382   if(!exists $form->{addition}) {
383     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
384     $form->{addition} = "SAVED";
385     $form->save_history($form->dbconnect(\%myconfig));
386   }
387   # /saving the history
388   &add_transaction;
389   $main::lxdebug->leave_sub();
390 }
391
392 sub save_and_ar_transaction {
393   $main::lxdebug->enter_sub();
394
395   $main::auth->assert('customer_vendor_edit & general_ledger');
396
397   my $form     = $main::form;
398   my %myconfig = %main::myconfig;
399
400   $form->{script} = "ar.pl";
401   # saving the history
402   if(!exists $form->{addition}) {
403     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
404     $form->{addition} = "SAVED";
405     $form->save_history($form->dbconnect(\%myconfig));
406   }
407   # /saving the history
408   &add_transaction;
409   $main::lxdebug->leave_sub();
410 }
411
412 sub save_and_invoice {
413   $main::lxdebug->enter_sub();
414
415   my $form     = $main::form;
416   my %myconfig = %main::myconfig;
417
418   if ($form->{db} eq 'customer') {
419     $main::auth->assert('customer_vendor_edit & invoice_edit');
420   } else {
421     $main::auth->assert('customer_vendor_edit & vendor_invoice_edit');
422   }
423
424   $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
425   $form->{type} = "invoice";
426   # saving the history
427   if(!exists $form->{addition}) {
428     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
429     $form->{addition} = "SAVED";
430     $form->save_history($form->dbconnect(\%myconfig));
431   }
432   # /saving the history
433   &add_transaction;
434   $main::lxdebug->leave_sub();
435 }
436
437 sub save_and_rfq {
438   $main::lxdebug->enter_sub();
439
440   $main::auth->assert('customer_vendor_edit & request_quotation_edit');
441
442   my $form     = $main::form;
443   my %myconfig = %main::myconfig;
444
445   $form->{script} = "oe.pl";
446   $form->{type}   = "request_quotation";
447   # saving the history
448   if(!exists $form->{addition}) {
449     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
450     $form->{addition} = "SAVED";
451     $form->save_history($form->dbconnect(\%myconfig));
452   }
453   # /saving the history
454   &add_transaction;
455   $main::lxdebug->leave_sub();
456 }
457
458 sub save_and_quotation {
459   $main::lxdebug->enter_sub();
460
461   $main::auth->assert('customer_vendor_edit & sales_quotation_edit');
462
463   my $form     = $main::form;
464   my %myconfig = %main::myconfig;
465
466   $form->{script} = "oe.pl";
467   $form->{type}   = "sales_quotation";
468   # saving the history
469   if(!exists $form->{addition}) {
470     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
471     $form->{addition} = "SAVED";
472     $form->save_history($form->dbconnect(\%myconfig));
473   }
474   # /saving the history
475   &add_transaction;
476   $main::lxdebug->leave_sub();
477 }
478
479 sub save_and_order {
480   $main::lxdebug->enter_sub();
481
482   my $form     = $main::form;
483   my %myconfig = %main::myconfig;
484
485   if ($form->{db} eq 'customer') {
486     $main::auth->assert('customer_vendor_edit & sales_order_edit');
487   } else {
488     $main::auth->assert('customer_vendor_edit & purchase_order_edit');
489   }
490
491   $form->{script} = "oe.pl";
492   $form->{type}   =
493     ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
494   # saving the history
495   if(!exists $form->{addition}) {
496     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
497     $form->{addition} = "SAVED";
498     $form->save_history($form->dbconnect(\%myconfig));
499   }
500   # /saving the history
501   &add_transaction;
502   $main::lxdebug->leave_sub();
503 }
504
505 sub save_and_close {
506   $main::lxdebug->enter_sub();
507
508   $main::auth->assert('customer_vendor_edit');
509
510   my $form     = $main::form;
511   my %myconfig = %main::myconfig;
512   my $locale   = $main::locale;
513
514   my $msg = ucfirst $form->{db};
515   $msg .= " saved!";
516
517   $form->isblank("name", $locale->text("Name missing!"));
518   my $rc;
519   if ($form->{"db"} eq "customer") {
520     $rc = CT->save_customer(\%myconfig, \%$form);
521   } else {
522     $rc = CT->save_vendor(\%myconfig, \%$form);
523   }
524   if ($rc == 3) {
525     $form->error($locale->text('customernumber not unique!'));
526   }
527   # saving the history
528   if(!exists $form->{addition}) {
529     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
530     $form->{addition} = "SAVED";
531     $form->save_history($form->dbconnect(\%myconfig));
532   }
533   # /saving the history
534   $form->redirect($locale->text($msg));
535
536   $main::lxdebug->leave_sub();
537 }
538
539 sub save {
540   $main::lxdebug->enter_sub();
541
542   $main::auth->assert('customer_vendor_edit');
543
544   my $form     = $main::form;
545   my %myconfig = %main::myconfig;
546   my $locale   = $main::locale;
547
548   my $msg = ucfirst $form->{db};
549   $msg .= " saved!";
550
551   $form->isblank("name", $locale->text("Name missing!"));
552
553   my $res;
554   if ($form->{"db"} eq "customer") {
555     $res = CT->save_customer(\%myconfig, \%$form);
556   } else {
557     $res = CT->save_vendor(\%myconfig, \%$form);
558   }
559
560   if (3 == $res) {
561     if ($form->{"db"} eq "customer") {
562       $form->error($locale->text('This customer number is already in use.'));
563     } else {
564       $form->error($locale->text('This vendor number is already in use.'));
565     }
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($form->dbconnect(\%myconfig));
572   }
573   # /saving the history
574   &edit;
575   exit;
576   $main::lxdebug->leave_sub();
577 }
578
579 sub delete {
580   $main::lxdebug->enter_sub();
581
582   $main::auth->assert('customer_vendor_edit');
583
584   my $form     = $main::form;
585   my %myconfig = %main::myconfig;
586   my $locale   = $main::locale;
587
588   CT->delete(\%myconfig, \%$form);
589
590   my $msg = ucfirst $form->{db};
591   $msg .= " deleted!";
592   # saving the history
593   if(!exists $form->{addition}) {
594     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
595     $form->{addition} = "DELETED";
596     $form->save_history($form->dbconnect(\%myconfig));
597   }
598   # /saving the history
599   $form->redirect($locale->text($msg));
600
601   $msg = "Cannot delete $form->{db}";
602   $form->error($locale->text($msg));
603
604   $main::lxdebug->leave_sub();
605 }
606
607 sub display {
608   $main::lxdebug->enter_sub();
609
610   $main::auth->assert('customer_vendor_edit');
611
612   my $form     = $main::form;
613
614   &form_header();
615   &form_footer();
616
617   $main::lxdebug->leave_sub();
618 }
619
620 sub update {
621   $main::lxdebug->enter_sub();
622
623   $main::auth->assert('customer_vendor_edit');
624
625   my $form     = $main::form;
626
627   &display();
628   $main::lxdebug->leave_sub();
629 }
630
631 sub get_contact {
632   $main::lxdebug->enter_sub();
633
634   $main::auth->assert('customer_vendor_edit');
635
636   my $form     = $main::form;
637   my %myconfig = %main::myconfig;
638
639   CT->get_contact(\%myconfig, \%$form);
640   print $form->ajax_response_header(), join '__pjx__', map $form->{"cp_$_"},
641     qw(name title givenname phone1 phone2 email abteilung fax mobile1 mobile2 satphone satfax project privatphone privatemail birthday used gender);
642   $main::lxdebug->leave_sub();
643
644 }
645
646 sub get_shipto {
647   $main::lxdebug->enter_sub();
648
649   $main::auth->assert('customer_vendor_edit');
650
651   my $form     = $main::form;
652   my %myconfig = %main::myconfig;
653
654   CT->get_shipto(\%myconfig, \%$form);
655   print $form->ajax_response_header(),  join '__pjx__', map $form->{"shipto$_"},
656     qw(name department_1 department_2 street zipcode city country contact phone fax email used);
657   $main::lxdebug->leave_sub();
658
659 }
660
661 sub get_delivery {
662   $main::lxdebug->enter_sub();
663
664   $main::auth->assert('customer_vendor_edit');
665
666   my $form     = $main::form;
667   my %myconfig = %main::myconfig;
668
669   CT->get_delivery(\%myconfig, \%$form );
670   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
671
672   print $form->ajax_response_header(), $form->parse_html_template('ct/get_delivery');
673
674   $main::lxdebug->leave_sub();
675 }
676
677 sub delete_shipto {
678   $main::lxdebug->enter_sub();
679
680   $main::auth->assert('customer_vendor_edit');
681
682   my $form     = $main::form;
683   my %myconfig = %main::myconfig;
684
685   CT->get_shipto(\%myconfig, \%$form);
686
687   unless ($form->{shiptoused}) {
688     CT->delete_shipto($form->{shipto_id});
689     @$form{ grep /^shipto/, keys %$form } = undef;
690   }
691
692   edit();
693
694   $main::lxdebug->leave_sub();
695 }
696
697 sub delete_contact {
698   $main::lxdebug->enter_sub();
699
700   $main::auth->assert('customer_vendor_edit');
701
702   my $form     = $main::form;
703   my %myconfig = %main::myconfig;
704
705   CT->get_contact(\%myconfig, \%$form);
706
707   unless ($form->{cp_used}) {
708     CT->delete_shipto($form->{cp_id});
709     @$form{ grep /^cp_/, keys %$form } = undef;
710   }
711
712   edit();
713
714   $main::lxdebug->leave_sub();
715 }
716
717 sub ajax_autocomplete {
718   $main::lxdebug->enter_sub();
719
720   my $form     = $main::form;
721   my %myconfig = %main::myconfig;
722
723   $form->{column}          = 'name'     unless $form->{column} =~ /^name$/;
724   $form->{vc}              = 'customer' unless $form->{vc} =~ /^customer|vendor$/;
725   $form->{db}              = $form->{vc}; # CT expects this
726   $form->{$form->{column}} = $form->{q}           || '';
727   $form->{limit}           = ($form->{limit} * 1) || 10;
728   $form->{searchitems}   ||= '';
729
730   CT->search(\%myconfig, $form);
731
732   print $form->ajax_response_header(),
733         $form->parse_html_template('ct/ajax_autocomplete');
734
735   $main::lxdebug->leave_sub();
736 }
737
738 sub continue { call_sub($main::form->{nextsub}); }