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