1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
15 # Contributors: Reed White <alta@alta-research.com>
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.
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 #======================================================================
31 # customer/vendor module
33 #======================================================================
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!')
48 use POSIX qw(strftime);
52 use SL::Request qw(flatten);
55 use SL::Helper::Flash;
56 use SL::ReportGenerator;
58 require "bin/mozilla/common.pl";
59 require "bin/mozilla/reportgenerator.pl";
67 $main::lxdebug->enter_sub();
69 $main::auth->assert('customer_vendor_edit');
71 my $form = $main::form;
72 my %myconfig = %main::myconfig;
74 $form->{title} = "Add";
75 $form->{callback} = "$form->{script}?action=add&db=$form->{db}" unless $form->{callback};
77 CT->populate_drop_down_boxes(\%myconfig, \%$form);
82 $main::lxdebug->leave_sub();
86 $main::lxdebug->enter_sub();
88 $main::auth->assert('customer_vendor_edit');
90 my $form = $main::form;
91 my $locale = $main::locale;
93 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
95 $form->get_lists("business_types" => "ALL_BUSINESS_TYPES");
96 $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
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');
104 $form->{jsscript} = 1;
105 $form->{title} = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
106 $form->{fokus} = 'Form.name';
109 print $form->parse_html_template('ct/search');
111 $main::lxdebug->leave_sub();
115 $::lxdebug->enter_sub;
116 $::auth->assert('customer_vendor_edit');
118 $::form->{fokus} = 'Form.name';
121 print $::form->parse_html_template('ct/search_contact');
123 $::lxdebug->leave_sub;
127 $main::lxdebug->enter_sub();
129 $main::auth->assert('customer_vendor_edit');
131 my $form = $main::form;
132 my %myconfig = %main::myconfig;
133 my $locale = $main::locale;
135 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
137 report_generator_set_default_sort('name', 1);
139 CT->search(\%myconfig, \%$form);
141 my $cvar_configs = CVar->get_configs('module' => 'CT');
144 if ($form->{status} eq 'all') {
145 push @options, $locale->text('All');
146 } elsif ($form->{status} eq 'orphaned') {
147 push @options, $locale->text('Orphaned');
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};
159 if ($form->{business_id}) {
160 my $business = SL::DB::Manager::Business->find_by(id => $form->{business_id});
162 my $label = $form->{IS_CUSTOMER} ? $::locale->text('Customer type') : $::locale->text('Vendor type');
163 push @options, $label . " : " . $business->description;
168 'id', 'name', "$form->{db}number", 'contact', 'phone',
169 'fax', 'email', 'taxnumber', 'street', 'zipcode' , 'city',
170 'business', 'invnumber', 'ordnumber', 'quonumber'
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;
177 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
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'), },
199 map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
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),
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});
214 my $sortdir = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
215 $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
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');
226 $form->{title} = $locale->text('Vendors');
227 $ordertype = 'purchase_order';
228 $quotationtype = 'request_quotation';
229 $attachment_basename = $locale->text('vendor_list');
232 my $report = SL::ReportGenerator->new(\%myconfig, $form);
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),
240 $report->set_options_from_form();
241 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
243 $report->set_columns(%column_defs);
244 $report->set_column_order(@columns);
246 $report->set_export_options('list_names', @hidden_variables, qw(sort sortdir));
248 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
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});
258 foreach my $ref (@{ $form->{CT} }) {
259 my $row = { map { $_ => { 'data' => '' } } @columns };
261 if ($ref->{id} ne $previous_id) {
262 $previous_id = $ref->{id};
263 map { $row->{$_}->{data} = $ref->{$_} } @columns;
265 $row->{name}->{link} = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
266 $row->{email}->{link} = 'mailto:' . E($ref->{email});
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};
276 $report->add_data($row);
279 $report->generate_with_headers();
281 $main::lxdebug->leave_sub();
285 $::lxdebug->enter_sub;
286 $::auth->assert('customer_vendor_edit');
288 $::form->{sortdir} = 1 unless defined $::form->{sortdir};
290 my @contacts = CT->search_contacts(
291 search_term => $::form->{search_term},
292 filter => $::form->{filter},
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
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};
306 @visible_columns = qw(vcname vcnumber cp_name cp_givenname cp_phone1 cp_phone2 cp_mobile1 cp_email);
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'), },
326 map { $column_defs{$_}->{visible} = 1 } @visible_columns;
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});
334 map { $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=" . ($::form->{sort} eq $_ ? 1 - $::form->{sortdir} : $::form->{sortdir}) } @columns;
336 $::form->{title} = $::locale->text('Contacts');
338 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
340 my @options = $::locale->text('Search term') . ': ' . $::form->{search_term};
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),
347 $report->set_options_from_form;
349 $report->set_columns(%column_defs);
350 $report->set_column_order(@columns);
352 $report->set_export_options('list_contacts', @hidden_variables);
354 $report->set_sort_indicator($::form->{sort}, $::form->{sortdir});
356 foreach my $ref (@contacts) {
357 my $row = { map { $_ => { 'data' => $ref->{$_} } } @columns };
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});
363 $report->add_data($row);
366 $report->generate_with_headers;
368 $::lxdebug->leave_sub;
372 $main::lxdebug->enter_sub();
374 $main::auth->assert('customer_vendor_edit');
376 my $form = $main::form;
377 my %myconfig = %main::myconfig;
379 # show history button
380 $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
381 #/show hhistory button
383 CT->get_tuple(\%myconfig, \%$form);
384 CT->populate_drop_down_boxes(\%myconfig, \%$form);
386 $form->{title} = "Edit";
389 $form->{discount} *= 100;
391 $form->{homepage} = 'http://' . $form->{homepage} unless ((!$form->{homepage}) || $form->{homepage} =~ m|^https?://|);
396 $main::lxdebug->leave_sub();
401 join('; ', grep { $_ } map { $s->{"shipto$_"} } qw(name department_1 street city)) || ' '
404 sub _contacts_label {
405 join ", ", grep { $_ } $_[0]->{cp_name}, $_[0]->{cp_givenname};
409 $main::lxdebug->enter_sub();
411 $main::auth->assert('customer_vendor_edit');
413 my $form = $main::form;
414 my %myconfig = %main::myconfig;
415 my $locale = $main::locale;
417 $form->get_lists(taxzones => "ALL_TAXZONES",
418 currencies => "ALL_CURRENCIES");
419 $form->get_pricegroup(\%myconfig, { all => 1 });
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;
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} } ];
435 $form->{title} = $form->{title_save}
436 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
438 CT->query_titles_and_greetings(\%myconfig, \%$form);
439 map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(COMPANY_GREETINGS);
441 $form->{NOTES} ||= [ ];
443 if (!$form->{'language_id'}) {
444 my $l_id = SL::DB::Default->get->{'language_id'};
446 $form->{'default_language_id'} = $l_id;
450 if (!$form->{'id'}) {
451 $form->{'currency'} = $form->get_default_currency(\%myconfig);
453 $form->{currency} = $form->{curr};
456 $::form->{CUSTOM_VARIABLES} = { };
457 my %specs = ( CT => { field => 'id', name_prefix => '', },
458 Contacts => { field => 'cp_id', name_prefix => 'cp', },
461 for my $module (keys %specs) {
462 my $spec = $specs{$module};
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} };
470 print $form->parse_html_template('ct/form_header');
472 $main::lxdebug->leave_sub();
476 $main::lxdebug->enter_sub();
478 $main::auth->assert('customer_vendor_edit');
480 my $form = $main::form;
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();
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)');
492 $::form->isblank("name", $::locale->text("Name missing!"));
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};
499 my $res = $::form->{db} eq 'customer' ? CT->save_customer(\%::myconfig, $::form) : CT->save_vendor(\%::myconfig, $::form);
502 if ($::form->{"db"} eq "customer") {
503 $::form->error($::locale->text('This customer number is already in use.'));
505 $::form->error($::locale->text('This vendor number is already in use.'));
510 sub add_transaction {
511 $main::lxdebug->enter_sub();
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)');
517 my $form = $main::form;
518 my %myconfig = %main::myconfig;
519 my $locale = $main::locale;
521 # # saving the history
522 # if(!exists $form->{addition}) {
523 # $form->{addition} = "ADD TRANSACTION";
524 # $form->save_history;
526 # # /saving the history
530 $form->{callback} = $form->escape($form->{callback}, 1);
531 my $name = $form->escape("$form->{name}", 1);
534 "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
537 $main::lxdebug->leave_sub();
540 sub save_and_ap_transaction {
541 $main::lxdebug->enter_sub();
543 $main::auth->assert('customer_vendor_edit & general_ledger');
545 my $form = $main::form;
546 my %myconfig = %main::myconfig;
548 $form->{script} = "ap.pl";
550 if(!exists $form->{addition}) {
551 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
552 $form->{addition} = "SAVED";
555 # /saving the history
557 $main::lxdebug->leave_sub();
560 sub save_and_ar_transaction {
561 $main::lxdebug->enter_sub();
563 $main::auth->assert('customer_vendor_edit & general_ledger');
565 my $form = $main::form;
566 my %myconfig = %main::myconfig;
568 $form->{script} = "ar.pl";
570 if(!exists $form->{addition}) {
571 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
572 $form->{addition} = "SAVED";
575 # /saving the history
577 $main::lxdebug->leave_sub();
580 sub save_and_invoice {
581 $main::lxdebug->enter_sub();
583 my $form = $main::form;
584 my %myconfig = %main::myconfig;
586 if ($form->{db} eq 'customer') {
587 $main::auth->assert('customer_vendor_edit & invoice_edit');
589 $main::auth->assert('customer_vendor_edit & vendor_invoice_edit');
592 $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
593 $form->{type} = "invoice";
595 if(!exists $form->{addition}) {
596 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
597 $form->{addition} = "SAVED";
600 # /saving the history
602 $main::lxdebug->leave_sub();
606 $main::lxdebug->enter_sub();
608 $main::auth->assert('customer_vendor_edit & request_quotation_edit');
610 my $form = $main::form;
611 my %myconfig = %main::myconfig;
613 $form->{script} = "oe.pl";
614 $form->{type} = "request_quotation";
616 if(!exists $form->{addition}) {
617 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
618 $form->{addition} = "SAVED";
621 # /saving the history
623 $main::lxdebug->leave_sub();
626 sub save_and_quotation {
627 $main::lxdebug->enter_sub();
629 $main::auth->assert('customer_vendor_edit & sales_quotation_edit');
631 my $form = $main::form;
632 my %myconfig = %main::myconfig;
634 $form->{script} = "oe.pl";
635 $form->{type} = "sales_quotation";
637 if(!exists $form->{addition}) {
638 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
639 $form->{addition} = "SAVED";
642 # /saving the history
644 $main::lxdebug->leave_sub();
648 $main::lxdebug->enter_sub();
650 my $form = $main::form;
651 my %myconfig = %main::myconfig;
653 if ($form->{db} eq 'customer') {
654 $main::auth->assert('customer_vendor_edit & sales_order_edit');
656 $main::auth->assert('customer_vendor_edit & purchase_order_edit');
659 $form->{script} = "oe.pl";
661 ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
663 if(!exists $form->{addition}) {
664 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
665 $form->{addition} = "SAVED";
668 # /saving the history
670 $main::lxdebug->leave_sub();
674 $main::lxdebug->enter_sub();
676 $main::auth->assert('customer_vendor_edit');
678 my $form = $main::form;
679 my %myconfig = %main::myconfig;
680 my $locale = $main::locale;
682 my $msg = ucfirst $form->{db};
688 if(!exists $form->{addition}) {
689 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
690 $form->{addition} = "SAVED";
693 # /saving the history
694 $form->redirect($locale->text($msg));
696 $main::lxdebug->leave_sub();
700 $main::lxdebug->enter_sub();
702 $main::auth->assert('customer_vendor_edit');
704 my $form = $main::form;
705 my %myconfig = %main::myconfig;
706 my $locale = $main::locale;
708 my $msg = ucfirst $form->{db};
714 if(!exists $form->{addition}) {
715 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
716 $form->{addition} = "SAVED";
719 # /saving the history
722 $main::lxdebug->leave_sub();
727 $main::lxdebug->enter_sub();
729 $main::auth->assert('customer_vendor_edit');
731 my $form = $main::form;
732 my %myconfig = %main::myconfig;
733 my $locale = $main::locale;
735 CT->delete(\%myconfig, \%$form);
737 my $msg = ucfirst $form->{db};
740 if(!exists $form->{addition}) {
741 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
742 $form->{addition} = "DELETED";
745 # /saving the history
746 $form->redirect($locale->text($msg));
748 $main::lxdebug->leave_sub();
752 $main::lxdebug->enter_sub();
754 $main::auth->assert('customer_vendor_edit');
756 my $form = $main::form;
761 $main::lxdebug->leave_sub();
765 $main::lxdebug->enter_sub();
767 $main::auth->assert('customer_vendor_edit');
769 my $form = $main::form;
772 $main::lxdebug->leave_sub();
776 $main::lxdebug->enter_sub();
778 $main::auth->assert('customer_vendor_edit');
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};
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} };
788 $::form->{contacts_label} = \&_contacts_label;
790 print $::form->ajax_response_header(), $::form->parse_html_template('ct/_contact');
792 $main::lxdebug->leave_sub();
796 $main::lxdebug->enter_sub();
798 $main::auth->assert('customer_vendor_edit');
800 CT->populate_drop_down_boxes(\%::myconfig, $::form);
801 CT->get_shipto(\%::myconfig, $::form) if $::form->{shipto_id};
803 $::form->{shipto_label} = \&_shipto_label;
805 print $::form->ajax_response_header(), $::form->parse_html_template('ct/_shipto');
807 $main::lxdebug->leave_sub();
811 $::lxdebug->enter_sub;
813 $::auth->assert('customer_vendor_edit');
814 $::auth->assert('sales_all_edit');
816 CT->get_delivery(\%::myconfig, $::form );
818 print $::form->ajax_response_header,
819 $::form->parse_html_template('ct/get_delivery', {
820 is_customer => $::form->{db} eq 'customer',
823 $::lxdebug->leave_sub;
827 $::lxdebug->enter_sub;
828 $::auth->assert('customer_vendor_edit');
830 if (!$::form->{shipto_id}) {
831 flash('error', $::locale->text('No shipto selected to delete'));
834 CT->get_shipto(\%::myconfig, $::form);
836 my $shipto = SL::DB::Manager::Shipto->find_by(shipto_id => $::form->{shipto_id});
839 $shipto->detach->save;
840 flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
843 flash('info', $::locale->text('Shipto deleted.'));
845 delete $::form->{$_} for grep /^shipto/, keys %$::form;
850 $::lxdebug->leave_sub;
854 $::lxdebug->enter_sub;
855 $::auth->assert('customer_vendor_edit');
857 if (!$::form->{cp_id}) {
858 flash('error', $::locale->text('No contact selected to delete'));
861 CT->get_contact(\%::myconfig, $::form);
863 my $contact = SL::DB::Manager::Contact->find_by(cp_id => $::form->{cp_id});
865 if ($contact->used) {
866 $contact->detach->save;
867 flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
870 flash('info', $::locale->text('Contact deleted.'));
872 delete $::form->{$_} for grep /^cp_/, keys %$::form;
877 $::lxdebug->leave_sub;
880 sub ajax_autocomplete {
881 $main::lxdebug->enter_sub();
883 my $form = $main::form;
884 my %myconfig = %main::myconfig;
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} ||= '';
893 CT->search(\%myconfig, $form);
895 print $form->ajax_response_header(),
896 $form->parse_html_template('ct/ajax_autocomplete');
898 $main::lxdebug->leave_sub();
901 sub continue { call_sub($main::form->{nextsub}); }