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