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);
341 push @options, $::locale->text('Search term') . ': ' . $::form->{search_term} if $::form->{search_term};
342 for (qw(cp_name cp_givenname cp_title cp_email cp_abteilung cp_project)) {
343 push @options, $column_defs{$_}{text} . ': ' . $::form->{filter}{$_} if $::form->{filter}{$_};
345 if ($::form->{filter}{status}) {
346 push @options, $::locale->text('Status') . ': ' . (
347 $::form->{filter}{status} =~ /active/ ? $::locale->text('Active') :
348 $::form->{filter}{status} =~ /orphaned/ ? $::locale->text('Orphaned') :
349 $::form->{filter}{status} =~ /all/ ? $::locale->text('All') : ''
354 $report->set_options('top_info_text' => join("\n", @options),
355 'output_format' => 'HTML',
356 'title' => $::form->{title},
357 'attachment_basename' => $::locale->text('contact_list') . strftime('_%Y%m%d', localtime time),
359 $report->set_options_from_form;
361 $report->set_columns(%column_defs);
362 $report->set_column_order(@columns);
364 $report->set_export_options('list_contacts', @hidden_variables);
366 $report->set_sort_indicator($::form->{sort}, $::form->{sortdir});
368 foreach my $ref (@contacts) {
369 my $row = { map { $_ => { 'data' => $ref->{$_} } } @columns };
371 $row->{vcname}->{link} = build_std_url('action=edit', 'id=' . E($ref->{vcid}), 'db=' . E($ref->{db}), 'callback', @hidden_nondefault);
372 $row->{vcnumber}->{link} = $row->{vcname}->{link};
373 $row->{cp_email}->{link} = 'mailto:' . E($ref->{cp_email});
375 $report->add_data($row);
378 $report->generate_with_headers;
380 $::lxdebug->leave_sub;
384 $main::lxdebug->enter_sub();
386 $main::auth->assert('customer_vendor_edit');
388 my $form = $main::form;
389 my %myconfig = %main::myconfig;
391 # show history button
392 $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
393 #/show hhistory button
395 CT->get_tuple(\%myconfig, \%$form);
396 CT->populate_drop_down_boxes(\%myconfig, \%$form);
398 $form->{title} = "Edit";
401 $form->{discount} *= 100;
403 $form->{homepage} = 'http://' . $form->{homepage} unless ((!$form->{homepage}) || $form->{homepage} =~ m|^https?://|);
408 $main::lxdebug->leave_sub();
413 join('; ', grep { $_ } map { $s->{"shipto$_"} } qw(name department_1 street city)) || ' '
416 sub _contacts_label {
417 join ", ", grep { $_ } $_[0]->{cp_name}, $_[0]->{cp_givenname};
421 $main::lxdebug->enter_sub();
423 $main::auth->assert('customer_vendor_edit');
425 my $form = $main::form;
426 my %myconfig = %main::myconfig;
427 my $locale = $main::locale;
429 $form->get_lists(taxzones => "ALL_TAXZONES",
430 currencies => "ALL_CURRENCIES");
431 $form->get_pricegroup(\%myconfig, { all => 1 });
433 $form->get_lists(customers => { key => "ALL_SALESMAN_CUSTOMERS", business_is_salesman => 1 }) if $::lx_office_conf{features}->{vertreter};
434 $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $::form->{FU_created_for_user}, deleted => 0 ] ]);
435 $form->{ALL_SALESMEN} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $::form->{salesman_id}, deleted => 0 ] ]);
436 $form->{USER} = SL::DB::Manager::Employee->current;
438 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
439 $form->{is_customer} = $form->{db} eq 'customer';
440 $form->{shipto_label} = \&_shipto_label;
441 $form->{contacts_label} = \&_contacts_label;
442 $form->{taxzone_id} = 0 if !$form->{id};
443 $form->{jsscript} = 1;
444 $form->{fokus} = "ct.greeting";
445 $form->{SHIPTO_ALL} = [ +{ shipto_id => '0', shiptoname => $::locale->text('All') }, @{ $form->{SHIPTO} } ];
447 $form->{title} = $form->{title_save}
448 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
450 CT->query_titles_and_greetings(\%myconfig, \%$form);
451 map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(COMPANY_GREETINGS);
453 $form->{NOTES} ||= [ ];
455 if (!$form->{'language_id'}) {
456 my $l_id = SL::DB::Default->get->{'language_id'};
458 $form->{'default_language_id'} = $l_id;
462 if (!$form->{'id'}) {
463 $form->{'currency'} = $form->get_default_currency(\%myconfig);
465 $form->{currency} = $form->{curr};
468 $::form->{CUSTOM_VARIABLES} = { };
469 my %specs = ( CT => { field => 'id', name_prefix => '', },
470 Contacts => { field => 'cp_id', name_prefix => 'cp', },
473 for my $module (keys %specs) {
474 my $spec = $specs{$module};
476 $::form->{CUSTOM_VARIABLES}->{$module} = CVar->get_custom_variables(module => $module, trans_id => $::form->{ $spec->{field} });
477 CVar->render_inputs(variables => $::form->{CUSTOM_VARIABLES}->{$module}, name_prefix => $spec->{name_prefix})
478 if scalar @{ $::form->{CUSTOM_VARIABLES}->{$module} };
482 print $form->parse_html_template('ct/form_header');
484 $main::lxdebug->leave_sub();
488 $main::lxdebug->enter_sub();
490 $main::auth->assert('customer_vendor_edit');
492 my $form = $main::form;
494 print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
495 is_customer => $form->{db} eq 'customer' });
496 $main::lxdebug->leave_sub();
500 $main::auth->assert('customer_vendor_edit & ' .
501 '(general_ledger | invoice_edit | vendor_invoice_edit | ' .
502 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit)');
504 $::form->isblank("name", $::locale->text("Name missing!"));
506 if ($::form->{new_salesman_id} && $::lx_office_conf{features}->{vertreter}) {
507 $::form->{salesman_id} = $::form->{new_salesman_id};
508 delete $::form->{new_salesman_id};
511 my $res = $::form->{db} eq 'customer' ? CT->save_customer(\%::myconfig, $::form) : CT->save_vendor(\%::myconfig, $::form);
514 if ($::form->{"db"} eq "customer") {
515 $::form->error($::locale->text('This customer number is already in use.'));
517 $::form->error($::locale->text('This vendor number is already in use.'));
522 sub add_transaction {
523 $main::lxdebug->enter_sub();
525 $main::auth->assert('customer_vendor_edit & ' .
526 '(general_ledger | invoice_edit | vendor_invoice_edit | ' .
527 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit)');
529 my $form = $main::form;
530 my %myconfig = %main::myconfig;
531 my $locale = $main::locale;
533 # # saving the history
534 # if(!exists $form->{addition}) {
535 # $form->{addition} = "ADD TRANSACTION";
536 # $form->save_history;
538 # # /saving the history
542 $form->{callback} = $form->escape($form->{callback}, 1);
543 my $name = $form->escape("$form->{name}", 1);
546 "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
549 $main::lxdebug->leave_sub();
552 sub save_and_ap_transaction {
553 $main::lxdebug->enter_sub();
555 $main::auth->assert('customer_vendor_edit & general_ledger');
557 my $form = $main::form;
558 my %myconfig = %main::myconfig;
560 $form->{script} = "ap.pl";
562 if(!exists $form->{addition}) {
563 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
564 $form->{addition} = "SAVED";
567 # /saving the history
569 $main::lxdebug->leave_sub();
572 sub save_and_ar_transaction {
573 $main::lxdebug->enter_sub();
575 $main::auth->assert('customer_vendor_edit & general_ledger');
577 my $form = $main::form;
578 my %myconfig = %main::myconfig;
580 $form->{script} = "ar.pl";
582 if(!exists $form->{addition}) {
583 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
584 $form->{addition} = "SAVED";
587 # /saving the history
589 $main::lxdebug->leave_sub();
592 sub save_and_invoice {
593 $main::lxdebug->enter_sub();
595 my $form = $main::form;
596 my %myconfig = %main::myconfig;
598 if ($form->{db} eq 'customer') {
599 $main::auth->assert('customer_vendor_edit & invoice_edit');
601 $main::auth->assert('customer_vendor_edit & vendor_invoice_edit');
604 $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
605 $form->{type} = "invoice";
607 if(!exists $form->{addition}) {
608 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
609 $form->{addition} = "SAVED";
612 # /saving the history
614 $main::lxdebug->leave_sub();
618 $main::lxdebug->enter_sub();
620 $main::auth->assert('customer_vendor_edit & request_quotation_edit');
622 my $form = $main::form;
623 my %myconfig = %main::myconfig;
625 $form->{script} = "oe.pl";
626 $form->{type} = "request_quotation";
628 if(!exists $form->{addition}) {
629 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
630 $form->{addition} = "SAVED";
633 # /saving the history
635 $main::lxdebug->leave_sub();
638 sub save_and_quotation {
639 $main::lxdebug->enter_sub();
641 $main::auth->assert('customer_vendor_edit & sales_quotation_edit');
643 my $form = $main::form;
644 my %myconfig = %main::myconfig;
646 $form->{script} = "oe.pl";
647 $form->{type} = "sales_quotation";
649 if(!exists $form->{addition}) {
650 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
651 $form->{addition} = "SAVED";
654 # /saving the history
656 $main::lxdebug->leave_sub();
660 $main::lxdebug->enter_sub();
662 my $form = $main::form;
663 my %myconfig = %main::myconfig;
665 if ($form->{db} eq 'customer') {
666 $main::auth->assert('customer_vendor_edit & sales_order_edit');
668 $main::auth->assert('customer_vendor_edit & purchase_order_edit');
671 $form->{script} = "oe.pl";
673 ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
675 if(!exists $form->{addition}) {
676 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
677 $form->{addition} = "SAVED";
680 # /saving the history
682 $main::lxdebug->leave_sub();
686 $main::lxdebug->enter_sub();
688 $main::auth->assert('customer_vendor_edit');
690 my $form = $main::form;
691 my %myconfig = %main::myconfig;
692 my $locale = $main::locale;
694 my $msg = ucfirst $form->{db};
700 if(!exists $form->{addition}) {
701 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
702 $form->{addition} = "SAVED";
705 # /saving the history
706 $form->redirect($locale->text($msg));
708 $main::lxdebug->leave_sub();
712 $main::lxdebug->enter_sub();
714 $main::auth->assert('customer_vendor_edit');
716 my $form = $main::form;
717 my %myconfig = %main::myconfig;
718 my $locale = $main::locale;
720 my $msg = ucfirst $form->{db};
726 if(!exists $form->{addition}) {
727 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
728 $form->{addition} = "SAVED";
731 # /saving the history
734 $main::lxdebug->leave_sub();
739 $main::lxdebug->enter_sub();
741 $main::auth->assert('customer_vendor_edit');
743 my $form = $main::form;
744 my %myconfig = %main::myconfig;
745 my $locale = $main::locale;
747 CT->delete(\%myconfig, \%$form);
749 my $msg = ucfirst $form->{db};
752 if(!exists $form->{addition}) {
753 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
754 $form->{addition} = "DELETED";
757 # /saving the history
758 $form->redirect($locale->text($msg));
760 $main::lxdebug->leave_sub();
764 $main::lxdebug->enter_sub();
766 $main::auth->assert('customer_vendor_edit');
768 my $form = $main::form;
773 $main::lxdebug->leave_sub();
777 $main::lxdebug->enter_sub();
779 $main::auth->assert('customer_vendor_edit');
781 my $form = $main::form;
784 $main::lxdebug->leave_sub();
788 $main::lxdebug->enter_sub();
790 $main::auth->assert('customer_vendor_edit');
792 CT->populate_drop_down_boxes(\%::myconfig, $::form);
793 CT->query_titles_and_greetings(\%::myconfig, $::form);
794 CT->get_contact(\%::myconfig, $::form) if $::form->{cp_id};
796 $::form->{CUSTOM_VARIABLES}{Contacts} = CVar->get_custom_variables(module => 'Contacts', trans_id => $::form->{cp_id});
797 CVar->render_inputs(variables => $::form->{CUSTOM_VARIABLES}{Contacts}, name_prefix => 'cp')
798 if scalar @{ $::form->{CUSTOM_VARIABLES}->{Contacts} };
800 $::form->{contacts_label} = \&_contacts_label;
802 print $::form->ajax_response_header(), $::form->parse_html_template('ct/_contact');
804 $main::lxdebug->leave_sub();
808 $main::lxdebug->enter_sub();
810 $main::auth->assert('customer_vendor_edit');
812 CT->populate_drop_down_boxes(\%::myconfig, $::form);
813 CT->get_shipto(\%::myconfig, $::form) if $::form->{shipto_id};
815 $::form->{shipto_label} = \&_shipto_label;
817 print $::form->ajax_response_header(), $::form->parse_html_template('ct/_shipto');
819 $main::lxdebug->leave_sub();
823 $::lxdebug->enter_sub;
825 $::auth->assert('customer_vendor_edit');
826 $::auth->assert('sales_all_edit');
828 CT->get_delivery(\%::myconfig, $::form );
830 print $::form->ajax_response_header,
831 $::form->parse_html_template('ct/get_delivery', {
832 is_customer => $::form->{db} eq 'customer',
835 $::lxdebug->leave_sub;
839 $::lxdebug->enter_sub;
840 $::auth->assert('customer_vendor_edit');
842 if (!$::form->{shipto_id}) {
843 flash('error', $::locale->text('No shipto selected to delete'));
846 CT->get_shipto(\%::myconfig, $::form);
848 my $shipto = SL::DB::Manager::Shipto->find_by(shipto_id => $::form->{shipto_id});
851 $shipto->detach->save;
852 flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
855 flash('info', $::locale->text('Shipto deleted.'));
857 delete $::form->{$_} for grep /^shipto/, keys %$::form;
862 $::lxdebug->leave_sub;
866 $::lxdebug->enter_sub;
867 $::auth->assert('customer_vendor_edit');
869 if (!$::form->{cp_id}) {
870 flash('error', $::locale->text('No contact selected to delete'));
873 CT->get_contact(\%::myconfig, $::form);
875 my $contact = SL::DB::Manager::Contact->find_by(cp_id => $::form->{cp_id});
877 if ($contact->used) {
878 $contact->detach->save;
879 flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
882 flash('info', $::locale->text('Contact deleted.'));
884 delete $::form->{$_} for grep /^cp_/, keys %$::form;
889 $::lxdebug->leave_sub;
892 sub ajax_autocomplete {
893 $main::lxdebug->enter_sub();
895 my $form = $main::form;
896 my %myconfig = %main::myconfig;
898 $form->{column} = 'name' unless $form->{column} =~ /^name$/;
899 $form->{vc} = 'customer' unless $form->{vc} =~ /^customer|vendor$/;
900 $form->{db} = $form->{vc}; # CT expects this
901 $form->{$form->{column}} = $form->{q} || '';
902 $form->{limit} = ($form->{limit} * 1) || 10;
903 $form->{searchitems} ||= '';
905 CT->search(\%myconfig, $form);
907 print $form->ajax_response_header(),
908 $form->parse_html_template('ct/ajax_autocomplete');
910 $main::lxdebug->leave_sub();
913 sub continue { call_sub($main::form->{nextsub}); }