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