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