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 $::request->{layout}->focus('#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->{CUSTOM_VARIABLES} = CVar->get_configs('module' => 'Contacts');
119 ($::form->{CUSTOM_VARIABLES_FILTER_CODE},
120 $::form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $::form->{CUSTOM_VARIABLES},
121 'include_prefix' => 'l.',
122 'filter_prefix' => 'filter.',
123 'include_value' => 'Y');
125 $::form->{title} = $::locale->text('Search contacts');
127 print $::form->parse_html_template('ct/search_contact');
129 $::lxdebug->leave_sub;
133 $main::lxdebug->enter_sub();
135 $main::auth->assert('customer_vendor_edit');
137 my $form = $main::form;
138 my %myconfig = %main::myconfig;
139 my $locale = $main::locale;
141 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
143 report_generator_set_default_sort('name', 1);
145 CT->search(\%myconfig, \%$form);
147 my $cvar_configs = CVar->get_configs('module' => 'CT');
150 if ($form->{status} eq 'all') {
151 push @options, $locale->text('All');
152 } elsif ($form->{status} eq 'orphaned') {
153 push @options, $locale->text('Orphaned');
156 push @options, $locale->text('Name') . " : $form->{name}" if $form->{name};
157 push @options, $locale->text('Contact') . " : $form->{contact}" if $form->{contact};
158 push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}| if $form->{"$form->{db}number"};
159 push @options, $locale->text('E-mail') . " : $form->{email}" if $form->{email};
160 push @options, $locale->text('Contact person (surname)') . " : $form->{cp_name}" if $form->{cp_name};
161 push @options, $locale->text('Billing/shipping address (city)') . " : $form->{addr_city}" if $form->{addr_city};
162 push @options, $locale->text('Billing/shipping address (zipcode)') . " : $form->{zipcode}" if $form->{addr_zipcode};
163 push @options, $locale->text('Billing/shipping address (street)') . " : $form->{street}" if $form->{addr_street};
164 push @options, $locale->text('Billing/shipping address (country)') . " : $form->{country}" if $form->{addr_country};
166 if ($form->{business_id}) {
167 my $business = SL::DB::Manager::Business->find_by(id => $form->{business_id});
169 my $label = $form->{IS_CUSTOMER} ? $::locale->text('Customer type') : $::locale->text('Vendor type');
170 push @options, $label . " : " . $business->description;
175 'id', 'name', "$form->{db}number", 'contact', 'phone',
176 'fax', 'email', 'taxnumber', 'street', 'zipcode' , 'city',
177 'business', 'invnumber', 'ordnumber', 'quonumber', 'salesman', 'country'
180 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
181 my @searchable_custom_variables = grep { $_->{searchable} } @{ $cvar_configs };
182 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
184 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
187 'id' => { 'text' => $locale->text('ID'), },
188 "$form->{db}number" => { 'text' => $locale->text('Number'), },
189 'name' => { 'text' => $form->{IS_CUSTOMER} ? $::locale->text('Customer Name') : $::locale->text('Vendor Name'), },
190 'contact' => { 'text' => $locale->text('Contact'), },
191 'phone' => { 'text' => $locale->text('Phone'), },
192 'fax' => { 'text' => $locale->text('Fax'), },
193 'email' => { 'text' => $locale->text('E-mail'), },
194 'cc' => { 'text' => $locale->text('Cc'), },
195 'taxnumber' => { 'text' => $locale->text('Tax Number'), },
196 'business' => { 'text' => $locale->text('Type of Business'), },
197 'invnumber' => { 'text' => $locale->text('Invoice'), },
198 'ordnumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
199 'quonumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation') : $locale->text('Request for Quotation'), },
200 'street' => { 'text' => $locale->text('Street'), },
201 'zipcode' => { 'text' => $locale->text('Zipcode'), },
202 'city' => { 'text' => $locale->text('City'), },
203 'country' => { 'text' => $locale->text('Country'), },
204 'salesman' => { 'text' => $locale->text('Salesman'), },
208 map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
210 my @hidden_variables = ( qw(
211 db status obsolete name contact email cp_name addr_street addr_zipcode
212 addr_city addr_country business_id
213 ), "$form->{db}number",
214 map({ "cvar_$_->{name}" } @searchable_custom_variables),
215 map({'cvar_'. $_->{name} .'_qtyop'} grep({$_->{type} eq 'number'} @searchable_custom_variables)),
216 map({ "l_$_" } @columns),
219 my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
220 my $callback = build_std_url('action=list_names', grep { $form->{$_} } @hidden_nondefault);
221 $form->{callback} = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
224 my $sortdir = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
225 $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
228 my ($ordertype, $quotationtype, $attachment_basename);
229 if ($form->{IS_CUSTOMER}) {
230 $form->{title} = $locale->text('Customers');
231 $ordertype = 'sales_order';
232 $quotationtype = 'sales_quotation';
233 $attachment_basename = $locale->text('customer_list');
236 $form->{title} = $locale->text('Vendors');
237 $ordertype = 'purchase_order';
238 $quotationtype = 'request_quotation';
239 $attachment_basename = $locale->text('vendor_list');
242 my $report = SL::ReportGenerator->new(\%myconfig, $form);
244 $report->set_options('top_info_text' => join("\n", @options),
245 'raw_bottom_info_text' => $form->parse_html_template('ct/list_names_bottom'),
246 'output_format' => 'HTML',
247 'title' => $form->{title},
248 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
250 $report->set_options_from_form();
251 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
253 $report->set_columns(%column_defs);
254 $report->set_column_order(@columns);
256 $report->set_export_options('list_names', @hidden_variables, qw(sort sortdir));
258 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
260 CVar->add_custom_variables_to_report('module' => 'CT',
261 'trans_id_field' => 'id',
262 'configs' => $cvar_configs,
263 'column_defs' => \%column_defs,
264 'data' => $form->{CT});
268 foreach my $ref (@{ $form->{CT} }) {
269 my $row = { map { $_ => { 'data' => '' } } @columns };
271 if ($ref->{id} ne $previous_id) {
272 $previous_id = $ref->{id};
273 map { $row->{$_}->{data} = $ref->{$_} } @columns;
275 $row->{name}->{link} = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
276 $row->{email}->{link} = 'mailto:' . E($ref->{email});
279 my $base_url = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
280 $row->{invnumber}->{link} = $base_url;
281 $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
282 $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
283 my $column = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
284 $row->{$column}->{data} = $ref->{$column};
286 $report->add_data($row);
289 $report->generate_with_headers();
291 $main::lxdebug->leave_sub();
295 $::lxdebug->enter_sub;
296 $::auth->assert('customer_vendor_edit');
298 $::form->{sortdir} = 1 unless defined $::form->{sortdir};
300 my @contacts = CT->search_contacts(
301 search_term => $::form->{search_term},
302 filter => $::form->{filter},
305 my $cvar_configs = CVar->get_configs('module' => 'Contacts');
308 cp_id vcname vcnumber cp_name cp_givenname cp_street cp_zipcode cp_city cp_phone1 cp_phone2 cp_privatphone
309 cp_mobile1 cp_mobile2 cp_fax cp_email cp_privatemail cp_abteilung cp_position cp_birthday cp_gender
312 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
313 my @searchable_custom_variables = grep { $_->{searchable} } @{ $cvar_configs };
314 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
316 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
320 @visible_columns = grep { $::form->{l}{$_} } @columns;
321 push @visible_columns, qw(cp_phone1 cp_phone2) if $::form->{l}{cp_phone};
322 push @visible_columns, qw(cp_mobile1 cp_mobile2) if $::form->{l}{cp_mobile};
324 @visible_columns = qw(vcname vcnumber cp_name cp_givenname cp_phone1 cp_phone2 cp_mobile1 cp_email);
328 'cp_id' => { 'text' => $::locale->text('ID'), },
329 'vcname' => { 'text' => $::locale->text('Customer/Vendor'), },
330 'vcnumber' => { 'text' => $::locale->text('Customer/Vendor Number'), },
331 'cp_name' => { 'text' => $::locale->text('Name'), },
332 'cp_givenname' => { 'text' => $::locale->text('Given Name'), },
333 'cp_street' => { 'text' => $::locale->text('Street'), },
334 'cp_zipcode' => { 'text' => $::locale->text('Zipcode'), },
335 'cp_city' => { 'text' => $::locale->text('City'), },
336 'cp_phone1' => { 'text' => $::locale->text('Phone1'), },
337 'cp_phone2' => { 'text' => $::locale->text('Phone2'), },
338 'cp_mobile1' => { 'text' => $::locale->text('Mobile1'), },
339 'cp_mobile2' => { 'text' => $::locale->text('Mobile2'), },
340 'cp_email' => { 'text' => $::locale->text('E-mail'), },
341 'cp_abteilung' => { 'text' => $::locale->text('Department'), },
342 'cp_position' => { 'text' => $::locale->text('Function/position'), },
343 'cp_birthday' => { 'text' => $::locale->text('Birthday'), },
344 'cp_gender' => { 'text' => $::locale->text('Gender'), },
345 'cp_fax' => { 'text' => $::locale->text('Fax'), },
346 'cp_privatphone' => { 'text' => $::locale->text('Private Phone') },
347 'cp_privatemail' => { 'text' => $::locale->text('Private E-mail') },
351 map { $column_defs{$_}->{visible} = 1 } @visible_columns;
353 my @hidden_variables = (qw(search_term filter l));
354 my $hide_vars = { map { $_ => $::form->{$_} } @hidden_variables };
355 my @hidden_nondefault = grep({ $::form->{$_} } @hidden_variables);
356 my $callback = build_std_url('action=list_contacts', join '&', map { E($_->[0]) . '=' . E($_->[1]) } @{ flatten($hide_vars) });
357 $::form->{callback} = "$callback&sort=" . E($::form->{sort});
359 map { $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=" . ($::form->{sort} eq $_ ? 1 - $::form->{sortdir} : $::form->{sortdir}) } @columns;
361 $::form->{title} = $::locale->text('Contacts');
363 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
366 push @options, $::locale->text('Search term') . ': ' . $::form->{search_term} if $::form->{search_term};
367 for (qw(cp_name cp_givenname cp_title cp_email cp_abteilung cp_project)) {
368 push @options, $column_defs{$_}{text} . ': ' . $::form->{filter}{$_} if $::form->{filter}{$_};
370 if ($::form->{filter}{status}) {
371 push @options, $::locale->text('Status') . ': ' . (
372 $::form->{filter}{status} =~ /active/ ? $::locale->text('Active') :
373 $::form->{filter}{status} =~ /orphaned/ ? $::locale->text('Orphaned') :
374 $::form->{filter}{status} =~ /all/ ? $::locale->text('All') : ''
379 $report->set_options('top_info_text' => join("\n", @options),
380 'output_format' => 'HTML',
381 'title' => $::form->{title},
382 'attachment_basename' => $::locale->text('contact_list') . strftime('_%Y%m%d', localtime time),
384 $report->set_options_from_form;
386 $report->set_columns(%column_defs);
387 $report->set_column_order(@columns);
389 $report->set_export_options('list_contacts', @hidden_variables);
391 $report->set_sort_indicator($::form->{sort}, $::form->{sortdir});
393 CVar->add_custom_variables_to_report('module' => 'Contacts',
394 'trans_id_field' => 'cp_id',
395 'configs' => $cvar_configs,
396 'column_defs' => \%column_defs,
397 'data' => \@contacts);
400 foreach my $ref (@contacts) {
401 my $row = { map { $_ => { 'data' => $ref->{$_} } } @columns };
403 $row->{vcname}->{link} = build_std_url('action=edit', 'id=' . E($ref->{vcid}), 'db=' . E($ref->{db}), 'callback', @hidden_nondefault);
404 $row->{vcnumber}->{link} = $row->{vcname}->{link};
406 for (qw(cp_email cp_privatemail)) {
407 $row->{$_}->{link} = 'mailto:' . E($ref->{$_}) if $ref->{$_};
410 $report->add_data($row);
413 $report->generate_with_headers;
415 $::lxdebug->leave_sub;
419 $main::lxdebug->enter_sub();
421 $main::auth->assert('customer_vendor_edit');
423 my $form = $main::form;
424 my %myconfig = %main::myconfig;
426 # show history button
427 $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
428 #/show hhistory button
430 CT->get_tuple(\%myconfig, \%$form);
431 CT->populate_drop_down_boxes(\%myconfig, \%$form);
433 $form->{title} = "Edit";
436 $form->{discount} *= 100;
438 $form->{homepage} = 'http://' . $form->{homepage} unless ((!$form->{homepage}) || $form->{homepage} =~ m|^https?://|);
443 $main::lxdebug->leave_sub();
448 join('; ', grep { $_ } map { $s->{"shipto$_"} } qw(name department_1 street city)) || ' '
451 sub _contacts_label {
452 join ", ", grep { $_ } $_[0]->{cp_name}, $_[0]->{cp_givenname};
456 $main::lxdebug->enter_sub();
458 $main::auth->assert('customer_vendor_edit');
460 my $form = $main::form;
461 my %myconfig = %main::myconfig;
462 my $locale = $main::locale;
464 $form->get_lists(taxzones => "ALL_TAXZONES",
465 currencies => "ALL_CURRENCIES");
466 $form->get_pricegroup(\%myconfig, { all => 1 });
468 $form->get_lists(customers => { key => "ALL_SALESMAN_CUSTOMERS", business_is_salesman => 1 }) if $::lx_office_conf{features}->{vertreter};
469 $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $::form->{FU_created_for_user}, deleted => 0 ] ]);
470 $form->{ALL_SALESMEN} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $::form->{salesman_id}, deleted => 0 ] ]);
471 $form->{USER} = SL::DB::Manager::Employee->current;
473 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
474 $form->{is_customer} = $form->{db} eq 'customer';
475 $form->{shipto_label} = \&_shipto_label;
476 $form->{contacts_label} = \&_contacts_label;
477 $form->{taxzone_id} = 0 if !$form->{id};
478 $form->{jsscript} = 1;
479 $form->{SHIPTO_ALL} = [ +{ shipto_id => '0', shiptoname => $::locale->text('All') }, @{ $form->{SHIPTO} } ];
480 $::request->{layout}->focus("#greeting");
482 $form->{title} = $form->{title_save}
483 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
485 CT->query_titles_and_greetings(\%myconfig, \%$form);
486 map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(COMPANY_GREETINGS);
488 $form->{NOTES} ||= [ ];
490 if (!$form->{'language_id'}) {
491 my $l_id = SL::DB::Default->get->{'language_id'};
493 $form->{'default_language_id'} = $l_id;
497 if (!$form->{'id'}) {
498 $form->{'currency'} = $form->get_default_currency(\%myconfig);
500 $form->{currency} = $form->{curr};
503 $::form->{CUSTOM_VARIABLES} = { };
504 my %specs = ( CT => { field => 'id', name_prefix => '', },
505 Contacts => { field => 'cp_id', name_prefix => 'cp', },
508 for my $module (keys %specs) {
509 my $spec = $specs{$module};
511 $::form->{CUSTOM_VARIABLES}->{$module} = CVar->get_custom_variables(module => $module, trans_id => $::form->{ $spec->{field} });
512 CVar->render_inputs(variables => $::form->{CUSTOM_VARIABLES}->{$module}, name_prefix => $spec->{name_prefix})
513 if scalar @{ $::form->{CUSTOM_VARIABLES}->{$module} };
517 print $form->parse_html_template('ct/form_header');
519 $main::lxdebug->leave_sub();
523 $main::lxdebug->enter_sub();
525 $main::auth->assert('customer_vendor_edit');
527 my $form = $main::form;
529 print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
530 is_customer => $form->{db} eq 'customer' });
531 $main::lxdebug->leave_sub();
535 $main::auth->assert('customer_vendor_edit');
537 $::form->isblank("name", $::locale->text("Name missing!"));
539 if ($::form->{new_salesman_id} && $::lx_office_conf{features}->{vertreter}) {
540 $::form->{salesman_id} = $::form->{new_salesman_id};
541 delete $::form->{new_salesman_id};
544 my $res = $::form->{db} eq 'customer' ? CT->save_customer(\%::myconfig, $::form) : CT->save_vendor(\%::myconfig, $::form);
547 if ($::form->{"db"} eq "customer") {
548 $::form->error($::locale->text('This customer number is already in use.'));
550 $::form->error($::locale->text('This vendor number is already in use.'));
555 sub add_transaction {
556 $main::lxdebug->enter_sub();
558 $main::auth->assert('customer_vendor_edit & ' .
559 '(general_ledger | invoice_edit | vendor_invoice_edit | ' .
560 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit)');
562 my $form = $main::form;
563 my %myconfig = %main::myconfig;
564 my $locale = $main::locale;
566 # # saving the history
567 # if(!exists $form->{addition}) {
568 # $form->{addition} = "ADD TRANSACTION";
569 # $form->save_history;
571 # # /saving the history
575 $form->{callback} = $form->escape($form->{callback}, 1);
576 my $name = $form->escape("$form->{name}", 1);
579 "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
582 $main::lxdebug->leave_sub();
585 sub save_and_ap_transaction {
586 $main::lxdebug->enter_sub();
588 $main::auth->assert('customer_vendor_edit & general_ledger');
590 my $form = $main::form;
591 my %myconfig = %main::myconfig;
593 $form->{script} = "ap.pl";
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();
605 sub save_and_ar_transaction {
606 $main::lxdebug->enter_sub();
608 $main::auth->assert('customer_vendor_edit & general_ledger');
610 my $form = $main::form;
611 my %myconfig = %main::myconfig;
613 $form->{script} = "ar.pl";
615 if(!exists $form->{addition}) {
616 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
617 $form->{addition} = "SAVED";
620 # /saving the history
622 $main::lxdebug->leave_sub();
625 sub save_and_invoice {
626 $main::lxdebug->enter_sub();
628 my $form = $main::form;
629 my %myconfig = %main::myconfig;
631 if ($form->{db} eq 'customer') {
632 $main::auth->assert('customer_vendor_edit & invoice_edit');
634 $main::auth->assert('customer_vendor_edit & vendor_invoice_edit');
637 $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
638 $form->{type} = "invoice";
640 if(!exists $form->{addition}) {
641 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
642 $form->{addition} = "SAVED";
645 # /saving the history
647 $main::lxdebug->leave_sub();
651 $main::lxdebug->enter_sub();
653 $main::auth->assert('customer_vendor_edit & request_quotation_edit');
655 my $form = $main::form;
656 my %myconfig = %main::myconfig;
658 $form->{script} = "oe.pl";
659 $form->{type} = "request_quotation";
661 if(!exists $form->{addition}) {
662 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
663 $form->{addition} = "SAVED";
666 # /saving the history
668 $main::lxdebug->leave_sub();
671 sub save_and_quotation {
672 $main::lxdebug->enter_sub();
674 $main::auth->assert('customer_vendor_edit & sales_quotation_edit');
676 my $form = $main::form;
677 my %myconfig = %main::myconfig;
679 $form->{script} = "oe.pl";
680 $form->{type} = "sales_quotation";
682 if(!exists $form->{addition}) {
683 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
684 $form->{addition} = "SAVED";
687 # /saving the history
689 $main::lxdebug->leave_sub();
693 $main::lxdebug->enter_sub();
695 my $form = $main::form;
696 my %myconfig = %main::myconfig;
698 if ($form->{db} eq 'customer') {
699 $main::auth->assert('customer_vendor_edit & sales_order_edit');
701 $main::auth->assert('customer_vendor_edit & purchase_order_edit');
704 $form->{script} = "oe.pl";
706 ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
708 if(!exists $form->{addition}) {
709 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
710 $form->{addition} = "SAVED";
713 # /saving the history
715 $main::lxdebug->leave_sub();
719 $main::lxdebug->enter_sub();
721 $main::auth->assert('customer_vendor_edit');
723 my $form = $main::form;
724 my %myconfig = %main::myconfig;
725 my $locale = $main::locale;
727 my $msg = ucfirst $form->{db};
733 if(!exists $form->{addition}) {
734 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
735 $form->{addition} = "SAVED";
738 # /saving the history
739 $form->redirect($locale->text($msg));
741 $main::lxdebug->leave_sub();
745 $main::lxdebug->enter_sub();
747 $main::auth->assert('customer_vendor_edit');
749 my $form = $main::form;
750 my %myconfig = %main::myconfig;
751 my $locale = $main::locale;
753 my $msg = ucfirst $form->{db};
759 if(!exists $form->{addition}) {
760 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
761 $form->{addition} = "SAVED";
764 # /saving the history
767 $main::lxdebug->leave_sub();
772 $main::lxdebug->enter_sub();
774 $main::auth->assert('customer_vendor_edit');
776 my $form = $main::form;
777 my %myconfig = %main::myconfig;
778 my $locale = $main::locale;
780 CT->delete(\%myconfig, \%$form);
782 my $msg = ucfirst $form->{db};
785 if(!exists $form->{addition}) {
786 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
787 $form->{addition} = "DELETED";
790 # /saving the history
791 $form->redirect($locale->text($msg));
793 $main::lxdebug->leave_sub();
797 $main::lxdebug->enter_sub();
799 $main::auth->assert('customer_vendor_edit');
801 my $form = $main::form;
806 $main::lxdebug->leave_sub();
810 $main::lxdebug->enter_sub();
812 $main::auth->assert('customer_vendor_edit');
814 my $form = $main::form;
817 $main::lxdebug->leave_sub();
821 $main::lxdebug->enter_sub();
823 $main::auth->assert('customer_vendor_edit');
825 CT->populate_drop_down_boxes(\%::myconfig, $::form);
826 CT->query_titles_and_greetings(\%::myconfig, $::form);
827 CT->get_contact(\%::myconfig, $::form) if $::form->{cp_id};
829 $::form->{CUSTOM_VARIABLES}{Contacts} = CVar->get_custom_variables(module => 'Contacts', trans_id => $::form->{cp_id});
830 CVar->render_inputs(variables => $::form->{CUSTOM_VARIABLES}{Contacts}, name_prefix => 'cp')
831 if scalar @{ $::form->{CUSTOM_VARIABLES}->{Contacts} };
833 $::form->{contacts_label} = \&_contacts_label;
835 print $::form->ajax_response_header(), $::form->parse_html_template('ct/_contact');
837 $main::lxdebug->leave_sub();
841 $main::lxdebug->enter_sub();
843 $main::auth->assert('customer_vendor_edit');
845 CT->populate_drop_down_boxes(\%::myconfig, $::form);
846 CT->get_shipto(\%::myconfig, $::form) if $::form->{shipto_id};
848 $::form->{shipto_label} = \&_shipto_label;
850 print $::form->ajax_response_header(), $::form->parse_html_template('ct/_shipto');
852 $main::lxdebug->leave_sub();
856 $::lxdebug->enter_sub;
858 $::auth->assert('customer_vendor_edit');
859 $::auth->assert('sales_all_edit');
861 CT->get_delivery(\%::myconfig, $::form );
863 print $::form->ajax_response_header,
864 $::form->parse_html_template('ct/get_delivery', {
865 is_customer => $::form->{db} eq 'customer',
868 $::lxdebug->leave_sub;
872 $::lxdebug->enter_sub;
873 $::auth->assert('customer_vendor_edit');
875 if (!$::form->{shipto_id}) {
876 flash('error', $::locale->text('No shipto selected to delete'));
879 CT->get_shipto(\%::myconfig, $::form);
881 my $shipto = SL::DB::Manager::Shipto->find_by(shipto_id => $::form->{shipto_id});
884 $shipto->detach->save;
885 flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
888 flash('info', $::locale->text('Shipto deleted.'));
890 delete $::form->{$_} for grep /^shipto/, keys %$::form;
895 $::lxdebug->leave_sub;
899 $::lxdebug->enter_sub;
900 $::auth->assert('customer_vendor_edit');
902 if (!$::form->{cp_id}) {
903 flash('error', $::locale->text('No contact selected to delete'));
906 CT->get_contact(\%::myconfig, $::form);
908 my $contact = SL::DB::Manager::Contact->find_by(cp_id => $::form->{cp_id});
910 if ($contact->used) {
911 $contact->detach->save;
912 flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
915 flash('info', $::locale->text('Contact deleted.'));
917 delete $::form->{$_} for grep /^cp_/, keys %$::form;
922 $::lxdebug->leave_sub;
925 sub ajax_autocomplete {
926 $main::lxdebug->enter_sub();
928 my $form = $main::form;
929 my %myconfig = %main::myconfig;
931 $form->{column} = 'name' unless $form->{column} =~ /^name$/;
932 $form->{vc} = 'customer' unless $form->{vc} =~ /^customer|vendor$/;
933 $form->{db} = $form->{vc}; # CT expects this
934 $form->{$form->{column}} = $form->{q} || '';
935 $form->{limit} = ($form->{limit} * 1) || 10;
936 $form->{searchitems} ||= '';
938 CT->search(\%myconfig, $form);
940 print $form->ajax_response_header(),
941 $form->parse_html_template('ct/ajax_autocomplete');
943 $main::lxdebug->leave_sub();
946 sub continue { call_sub($main::form->{nextsub}); }