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::DB::DeliveryTerm;
56 use SL::Helper::Flash;
57 use SL::ReportGenerator;
58 use SL::MoreCommon qw(uri_encode);
60 require "bin/mozilla/common.pl";
61 require "bin/mozilla/reportgenerator.pl";
69 $main::lxdebug->enter_sub();
71 my $url = 'controller.pl?action=CustomerVendor/add&db='. ($::form->{db} eq 'vendor' ? 'vendor' : 'customer');
73 if ( $::form->{callback} ) {
74 $url .= '&callback='. uri_encode($::form->{callback});
77 print $::form->redirect_header($url);
79 $main::lxdebug->leave_sub();
83 $main::lxdebug->enter_sub();
85 $main::auth->assert('customer_vendor_edit');
87 my $form = $main::form;
88 my $locale = $main::locale;
90 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
92 $form->get_lists("business_types" => "ALL_BUSINESS_TYPES");
93 $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
95 $form->{CUSTOM_VARIABLES} = CVar->get_configs('module' => 'CT');
96 ($form->{CUSTOM_VARIABLES_FILTER_CODE},
97 $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $form->{CUSTOM_VARIABLES},
98 'include_prefix' => 'l_',
99 'include_value' => 'Y');
101 $form->{title} = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
104 print $form->parse_html_template('ct/search');
106 $main::lxdebug->leave_sub();
110 $::lxdebug->enter_sub;
111 $::auth->assert('customer_vendor_edit');
113 $::form->{CUSTOM_VARIABLES} = CVar->get_configs('module' => 'Contacts');
114 ($::form->{CUSTOM_VARIABLES_FILTER_CODE},
115 $::form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $::form->{CUSTOM_VARIABLES},
116 'include_prefix' => 'l.',
117 'filter_prefix' => 'filter.',
118 'include_value' => 'Y');
120 $::form->{title} = $::locale->text('Search contacts');
122 print $::form->parse_html_template('ct/search_contact');
124 $::lxdebug->leave_sub;
128 $main::lxdebug->enter_sub();
130 $main::auth->assert('customer_vendor_edit');
132 my $form = $main::form;
133 my %myconfig = %main::myconfig;
134 my $locale = $main::locale;
136 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
138 report_generator_set_default_sort('name', 1);
140 CT->search(\%myconfig, \%$form);
142 my $cvar_configs = CVar->get_configs('module' => 'CT');
145 if ($form->{status} eq 'all') {
146 push @options, $locale->text('All');
147 } elsif ($form->{status} eq 'orphaned') {
148 push @options, $locale->text('Orphaned');
151 push @options, $locale->text('Name') . " : $form->{name}" if $form->{name};
152 push @options, $locale->text('Contact') . " : $form->{contact}" if $form->{contact};
153 push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}| if $form->{"$form->{db}number"};
154 push @options, $locale->text('E-mail') . " : $form->{email}" if $form->{email};
155 push @options, $locale->text('Contact person (surname)') . " : $form->{cp_name}" if $form->{cp_name};
156 push @options, $locale->text('Billing/shipping address (city)') . " : $form->{addr_city}" if $form->{addr_city};
157 push @options, $locale->text('Billing/shipping address (zipcode)') . " : $form->{zipcode}" if $form->{addr_zipcode};
158 push @options, $locale->text('Billing/shipping address (street)') . " : $form->{street}" if $form->{addr_street};
159 push @options, $locale->text('Billing/shipping address (country)') . " : $form->{country}" if $form->{addr_country};
161 if ($form->{business_id}) {
162 my $business = SL::DB::Manager::Business->find_by(id => $form->{business_id});
164 my $label = $form->{IS_CUSTOMER} ? $::locale->text('Customer type') : $::locale->text('Vendor type');
165 push @options, $label . " : " . $business->description;
170 'id', 'name', "$form->{db}number", 'contact', 'phone', 'discount',
171 'fax', 'email', 'taxnumber', 'street', 'zipcode' , 'city',
172 'business', 'invnumber', 'ordnumber', 'quonumber', 'salesman', 'country'
175 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
176 my @searchable_custom_variables = grep { $_->{searchable} } @{ $cvar_configs };
177 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
179 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
182 'id' => { 'text' => $locale->text('ID'), },
183 "$form->{db}number" => { 'text' => $locale->text('Number'), },
184 'name' => { 'text' => $form->{IS_CUSTOMER} ? $::locale->text('Customer Name') : $::locale->text('Vendor Name'), },
185 'contact' => { 'text' => $locale->text('Contact'), },
186 'phone' => { 'text' => $locale->text('Phone'), },
187 'fax' => { 'text' => $locale->text('Fax'), },
188 'email' => { 'text' => $locale->text('E-mail'), },
189 'cc' => { 'text' => $locale->text('Cc'), },
190 'taxnumber' => { 'text' => $locale->text('Tax Number'), },
191 'business' => { 'text' => $locale->text('Type of Business'), },
192 'invnumber' => { 'text' => $locale->text('Invoice'), },
193 'ordnumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
194 'quonumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation') : $locale->text('Request for Quotation'), },
195 'street' => { 'text' => $locale->text('Street'), },
196 'zipcode' => { 'text' => $locale->text('Zipcode'), },
197 'city' => { 'text' => $locale->text('City'), },
198 'country' => { 'text' => $locale->text('Country'), },
199 'salesman' => { 'text' => $locale->text('Salesman'), },
200 'discount' => { 'text' => $locale->text('Discount'), },
204 map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
206 my @hidden_variables = ( qw(
207 db status obsolete name contact email cp_name addr_street addr_zipcode
208 addr_city addr_country business_id
209 ), "$form->{db}number",
210 map({ "cvar_$_->{name}" } @searchable_custom_variables),
211 map({'cvar_'. $_->{name} .'_qtyop'} grep({$_->{type} eq 'number'} @searchable_custom_variables)),
212 map({ "l_$_" } @columns),
215 my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
216 my $callback = build_std_url('action=list_names', grep { $form->{$_} } @hidden_nondefault);
217 $form->{callback} = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
220 my $sortdir = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
221 $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
224 my ($ordertype, $quotationtype, $attachment_basename);
225 if ($form->{IS_CUSTOMER}) {
226 $form->{title} = $locale->text('Customers');
227 $ordertype = 'sales_order';
228 $quotationtype = 'sales_quotation';
229 $attachment_basename = $locale->text('customer_list');
232 $form->{title} = $locale->text('Vendors');
233 $ordertype = 'purchase_order';
234 $quotationtype = 'request_quotation';
235 $attachment_basename = $locale->text('vendor_list');
238 my $report = SL::ReportGenerator->new(\%myconfig, $form);
240 $report->set_options('top_info_text' => join("\n", @options),
241 'raw_bottom_info_text' => $form->parse_html_template('ct/list_names_bottom'),
242 'output_format' => 'HTML',
243 'title' => $form->{title},
244 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
246 $report->set_options_from_form();
247 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
249 $report->set_columns(%column_defs);
250 $report->set_column_order(@columns);
252 $report->set_export_options('list_names', @hidden_variables, qw(sort sortdir));
254 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
256 CVar->add_custom_variables_to_report('module' => 'CT',
257 'trans_id_field' => 'id',
258 'configs' => $cvar_configs,
259 'column_defs' => \%column_defs,
260 'data' => $form->{CT});
264 foreach my $ref (@{ $form->{CT} }) {
265 my $row = { map { $_ => { 'data' => '' } } @columns };
267 if ($ref->{id} ne $previous_id) {
268 $previous_id = $ref->{id};
269 $ref->{discount} = $form->format_amount(\%myconfig, $ref->{discount} * 100.0, 2);
270 map { $row->{$_}->{data} = $ref->{$_} } @columns;
272 $row->{name}->{link} = build_std_url('script=controller.pl', 'action=CustomerVendor/edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
273 $row->{email}->{link} = 'mailto:' . E($ref->{email});
276 my $base_url = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
277 $row->{invnumber}->{link} = $base_url;
278 $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
279 $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
280 my $column = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
281 $row->{$column}->{data} = $ref->{$column};
283 $report->add_data($row);
286 $report->generate_with_headers();
288 $main::lxdebug->leave_sub();
292 $::lxdebug->enter_sub;
293 $::auth->assert('customer_vendor_edit');
295 $::form->{sortdir} = 1 unless defined $::form->{sortdir};
297 my @contacts = CT->search_contacts(
298 search_term => $::form->{search_term},
299 filter => $::form->{filter},
302 my $cvar_configs = CVar->get_configs('module' => 'Contacts');
305 cp_id vcname vcnumber cp_name cp_givenname cp_street cp_zipcode cp_city cp_phone1 cp_phone2 cp_privatphone
306 cp_mobile1 cp_mobile2 cp_fax cp_email cp_privatemail cp_abteilung cp_position cp_birthday cp_gender
309 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
310 my @searchable_custom_variables = grep { $_->{searchable} } @{ $cvar_configs };
311 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
313 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
317 @visible_columns = grep { $::form->{l}{$_} } @columns;
318 push @visible_columns, qw(cp_phone1 cp_phone2) if $::form->{l}{cp_phone};
319 push @visible_columns, qw(cp_mobile1 cp_mobile2) if $::form->{l}{cp_mobile};
321 @visible_columns = qw(vcname vcnumber cp_name cp_givenname cp_phone1 cp_phone2 cp_mobile1 cp_email);
325 'cp_id' => { 'text' => $::locale->text('ID'), },
326 'vcname' => { 'text' => $::locale->text('Customer/Vendor'), },
327 'vcnumber' => { 'text' => $::locale->text('Customer/Vendor Number'), },
328 'cp_name' => { 'text' => $::locale->text('Name'), },
329 'cp_givenname' => { 'text' => $::locale->text('Given Name'), },
330 'cp_street' => { 'text' => $::locale->text('Street'), },
331 'cp_zipcode' => { 'text' => $::locale->text('Zipcode'), },
332 'cp_city' => { 'text' => $::locale->text('City'), },
333 'cp_phone1' => { 'text' => $::locale->text('Phone1'), },
334 'cp_phone2' => { 'text' => $::locale->text('Phone2'), },
335 'cp_mobile1' => { 'text' => $::locale->text('Mobile1'), },
336 'cp_mobile2' => { 'text' => $::locale->text('Mobile2'), },
337 'cp_email' => { 'text' => $::locale->text('E-mail'), },
338 'cp_abteilung' => { 'text' => $::locale->text('Department'), },
339 'cp_position' => { 'text' => $::locale->text('Function/position'), },
340 'cp_birthday' => { 'text' => $::locale->text('Birthday'), },
341 'cp_gender' => { 'text' => $::locale->text('Gender'), },
342 'cp_fax' => { 'text' => $::locale->text('Fax'), },
343 'cp_privatphone' => { 'text' => $::locale->text('Private Phone') },
344 'cp_privatemail' => { 'text' => $::locale->text('Private E-mail') },
348 map { $column_defs{$_}->{visible} = 1 } @visible_columns;
350 my @hidden_variables = (qw(search_term filter l));
351 my $hide_vars = { map { $_ => $::form->{$_} } @hidden_variables };
352 my @hidden_nondefault = grep({ $::form->{$_} } @hidden_variables);
353 my $callback = build_std_url('action=list_contacts', join '&', map { E($_->[0]) . '=' . E($_->[1]) } @{ flatten($hide_vars) });
354 $::form->{callback} = "$callback&sort=" . E($::form->{sort});
356 map { $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=" . ($::form->{sort} eq $_ ? 1 - $::form->{sortdir} : $::form->{sortdir}) } @columns;
358 $::form->{title} = $::locale->text('Contacts');
360 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
363 push @options, $::locale->text('Search term') . ': ' . $::form->{search_term} if $::form->{search_term};
364 for (qw(cp_name cp_givenname cp_title cp_email cp_abteilung cp_project)) {
365 push @options, $column_defs{$_}{text} . ': ' . $::form->{filter}{$_} if $::form->{filter}{$_};
367 if ($::form->{filter}{status}) {
368 push @options, $::locale->text('Status') . ': ' . (
369 $::form->{filter}{status} =~ /active/ ? $::locale->text('Active') :
370 $::form->{filter}{status} =~ /orphaned/ ? $::locale->text('Orphaned') :
371 $::form->{filter}{status} =~ /all/ ? $::locale->text('All') : ''
376 $report->set_options('top_info_text' => join("\n", @options),
377 'output_format' => 'HTML',
378 'title' => $::form->{title},
379 'attachment_basename' => $::locale->text('contact_list') . strftime('_%Y%m%d', localtime time),
381 $report->set_options_from_form;
383 $report->set_columns(%column_defs);
384 $report->set_column_order(@columns);
386 $report->set_export_options('list_contacts', @hidden_variables);
388 $report->set_sort_indicator($::form->{sort}, $::form->{sortdir});
390 CVar->add_custom_variables_to_report('module' => 'Contacts',
391 'trans_id_field' => 'cp_id',
392 'configs' => $cvar_configs,
393 'column_defs' => \%column_defs,
394 'data' => \@contacts);
397 foreach my $ref (@contacts) {
398 my $row = { map { $_ => { 'data' => $ref->{$_} } } @columns };
400 $row->{vcname}->{link} = build_std_url('script=controller.pl', 'action=CustomerVendor/edit', 'id=' . E($ref->{vcid}), 'db=' . E($ref->{db}), 'callback', @hidden_nondefault);
401 $row->{vcnumber}->{link} = $row->{vcname}->{link};
403 for (qw(cp_email cp_privatemail)) {
404 $row->{$_}->{link} = 'mailto:' . E($ref->{$_}) if $ref->{$_};
407 $report->add_data($row);
410 $report->generate_with_headers;
412 $::lxdebug->leave_sub;
416 $main::lxdebug->enter_sub();
418 $main::auth->assert('customer_vendor_edit');
420 my $form = $main::form;
421 my %myconfig = %main::myconfig;
423 # show history button
424 $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
425 #/show hhistory button
427 CT->get_tuple(\%myconfig, \%$form);
428 CT->populate_drop_down_boxes(\%myconfig, \%$form);
430 $form->{title} = "Edit";
433 $form->{discount} *= 100;
435 $form->{homepage} = 'http://' . $form->{homepage} unless ((!$form->{homepage}) || $form->{homepage} =~ m|^https?://|);
440 $main::lxdebug->leave_sub();
445 join('; ', grep { $_ } map { $s->{"shipto$_"} } qw(name department_1 street city)) || ' '
448 sub _contacts_label {
449 join ", ", grep { $_ } $_[0]->{cp_name}, $_[0]->{cp_givenname};
453 $main::lxdebug->enter_sub();
455 $main::auth->assert('customer_vendor_edit');
457 my $form = $main::form;
458 my %myconfig = %main::myconfig;
459 my $locale = $main::locale;
461 $form->get_lists(taxzones => "ALL_TAXZONES",
462 currencies => "ALL_CURRENCIES");
463 $form->get_pricegroup(\%myconfig, { all => 1 });
465 $form->get_lists(customers => { key => "ALL_SALESMAN_CUSTOMERS", business_is_salesman => 1 }) if $::instance_conf->get_vertreter;
466 $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $::form->{FU_created_for_user}, deleted => 0 ] ]);
467 $form->{ALL_SALESMEN} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $::form->{salesman_id}, deleted => 0 ] ]);
468 $form->{USER} = SL::DB::Manager::Employee->current;
470 $form->{ALL_DELIVERY_TERMS} = SL::DB::Manager::DeliveryTerm->get_all_sorted();
472 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
473 $form->{is_customer} = $form->{db} eq 'customer';
474 $form->{shipto_label} = \&_shipto_label;
475 $form->{contacts_label} = \&_contacts_label;
476 $form->{taxzone_id} = 0 if !$form->{id};
477 $form->{SHIPTO_ALL} = [ +{ shipto_id => '0', shiptoname => $::locale->text('All') }, @{ $form->{SHIPTO} } ];
479 $form->{title} = $form->{title_save}
480 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
482 CT->query_titles_and_greetings(\%myconfig, \%$form);
483 map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(COMPANY_GREETINGS);
485 $form->{NOTES} ||= [ ];
487 if (!$form->{'language_id'}) {
488 my $l_id = SL::DB::Default->get->{'language_id'};
490 $form->{'default_language_id'} = $l_id;
494 if (!$form->{'id'}) {
495 $form->{'currency'} = $form->get_default_currency(\%myconfig);
497 $form->{currency} = $form->{curr};
500 $::form->{CUSTOM_VARIABLES} = { };
501 my %specs = ( CT => { field => 'id', name_prefix => '', },
502 Contacts => { field => 'cp_id', name_prefix => 'cp', },
505 for my $module (keys %specs) {
506 my $spec = $specs{$module};
508 $::form->{CUSTOM_VARIABLES}->{$module} = CVar->get_custom_variables(module => $module, trans_id => $::form->{ $spec->{field} });
509 CVar->render_inputs(variables => $::form->{CUSTOM_VARIABLES}->{$module}, name_prefix => $spec->{name_prefix})
510 if scalar @{ $::form->{CUSTOM_VARIABLES}->{$module} };
514 print $form->parse_html_template('ct/form_header');
516 $main::lxdebug->leave_sub();
520 $main::lxdebug->enter_sub();
522 $main::auth->assert('customer_vendor_edit');
524 my $form = $main::form;
526 print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
527 is_customer => $form->{db} eq 'customer' });
528 $main::lxdebug->leave_sub();
532 $main::auth->assert('customer_vendor_edit');
534 $::form->isblank("name", $::locale->text("Name missing!"));
536 if ($::form->{new_salesman_id} && $::instance_conf->get_vertreter) {
537 $::form->{salesman_id} = $::form->{new_salesman_id};
538 delete $::form->{new_salesman_id};
541 my $res = $::form->{db} eq 'customer' ? CT->save_customer(\%::myconfig, $::form) : CT->save_vendor(\%::myconfig, $::form);
544 if ($::form->{"db"} eq "customer") {
545 $::form->error($::locale->text('This customer number is already in use.'));
547 $::form->error($::locale->text('This vendor number is already in use.'));
552 sub add_transaction {
553 $main::lxdebug->enter_sub();
555 $main::auth->assert('customer_vendor_edit & ' .
556 '(general_ledger | invoice_edit | vendor_invoice_edit | ' .
557 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit)');
559 my $form = $main::form;
560 my %myconfig = %main::myconfig;
561 my $locale = $main::locale;
563 # # saving the history
564 # if(!exists $form->{addition}) {
565 # $form->{addition} = "ADD TRANSACTION";
566 # $form->save_history;
568 # # /saving the history
572 $form->{callback} = $form->escape($form->{callback}, 1);
573 my $name = $form->escape("$form->{name}", 1);
576 "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
579 $main::lxdebug->leave_sub();
582 sub save_and_ap_transaction {
583 $main::lxdebug->enter_sub();
585 $main::auth->assert('customer_vendor_edit & general_ledger');
587 my $form = $main::form;
588 my %myconfig = %main::myconfig;
590 $form->{script} = "ap.pl";
592 if(!exists $form->{addition}) {
593 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
594 $form->{addition} = "SAVED";
597 # /saving the history
599 $main::lxdebug->leave_sub();
602 sub save_and_ar_transaction {
603 $main::lxdebug->enter_sub();
605 $main::auth->assert('customer_vendor_edit & general_ledger');
607 my $form = $main::form;
608 my %myconfig = %main::myconfig;
610 $form->{script} = "ar.pl";
612 if(!exists $form->{addition}) {
613 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
614 $form->{addition} = "SAVED";
617 # /saving the history
619 $main::lxdebug->leave_sub();
622 sub save_and_invoice {
623 $main::lxdebug->enter_sub();
625 my $form = $main::form;
626 my %myconfig = %main::myconfig;
628 if ($form->{db} eq 'customer') {
629 $main::auth->assert('customer_vendor_edit & invoice_edit');
631 $main::auth->assert('customer_vendor_edit & vendor_invoice_edit');
634 $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
635 $form->{type} = "invoice";
637 if(!exists $form->{addition}) {
638 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
639 $form->{addition} = "SAVED";
642 # /saving the history
644 $main::lxdebug->leave_sub();
648 $main::lxdebug->enter_sub();
650 $main::auth->assert('customer_vendor_edit & request_quotation_edit');
652 my $form = $main::form;
653 my %myconfig = %main::myconfig;
655 $form->{script} = "oe.pl";
656 $form->{type} = "request_quotation";
658 if(!exists $form->{addition}) {
659 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
660 $form->{addition} = "SAVED";
663 # /saving the history
665 $main::lxdebug->leave_sub();
668 sub save_and_quotation {
669 $main::lxdebug->enter_sub();
671 $main::auth->assert('customer_vendor_edit & sales_quotation_edit');
673 my $form = $main::form;
674 my %myconfig = %main::myconfig;
676 $form->{script} = "oe.pl";
677 $form->{type} = "sales_quotation";
679 if(!exists $form->{addition}) {
680 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
681 $form->{addition} = "SAVED";
684 # /saving the history
686 $main::lxdebug->leave_sub();
690 $main::lxdebug->enter_sub();
692 my $form = $main::form;
693 my %myconfig = %main::myconfig;
695 if ($form->{db} eq 'customer') {
696 $main::auth->assert('customer_vendor_edit & sales_order_edit');
698 $main::auth->assert('customer_vendor_edit & purchase_order_edit');
701 $form->{script} = "oe.pl";
703 ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
705 if(!exists $form->{addition}) {
706 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
707 $form->{addition} = "SAVED";
710 # /saving the history
712 $main::lxdebug->leave_sub();
716 $main::lxdebug->enter_sub();
718 $main::auth->assert('customer_vendor_edit');
720 my $form = $main::form;
721 my %myconfig = %main::myconfig;
722 my $locale = $main::locale;
724 my $msg = ucfirst $form->{db};
730 if(!exists $form->{addition}) {
731 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
732 $form->{addition} = "SAVED";
735 # /saving the history
736 $form->redirect($locale->text($msg));
738 $main::lxdebug->leave_sub();
742 $main::lxdebug->enter_sub();
744 $main::auth->assert('customer_vendor_edit');
746 my $form = $main::form;
747 my %myconfig = %main::myconfig;
748 my $locale = $main::locale;
750 my $msg = ucfirst $form->{db};
756 if(!exists $form->{addition}) {
757 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
758 $form->{addition} = "SAVED";
761 # /saving the history
764 $main::lxdebug->leave_sub();
769 $main::lxdebug->enter_sub();
771 $main::auth->assert('customer_vendor_edit');
773 my $form = $main::form;
774 my %myconfig = %main::myconfig;
775 my $locale = $main::locale;
777 CT->delete(\%myconfig, \%$form);
779 my $msg = ucfirst $form->{db};
782 if(!exists $form->{addition}) {
783 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
784 $form->{addition} = "DELETED";
787 # /saving the history
788 $form->redirect($locale->text($msg));
790 $main::lxdebug->leave_sub();
794 $main::lxdebug->enter_sub();
796 $main::auth->assert('customer_vendor_edit');
798 my $form = $main::form;
803 $main::lxdebug->leave_sub();
807 $main::lxdebug->enter_sub();
809 $main::auth->assert('customer_vendor_edit');
811 my $form = $main::form;
814 $main::lxdebug->leave_sub();
818 $main::lxdebug->enter_sub();
820 $main::auth->assert('customer_vendor_edit');
822 CT->populate_drop_down_boxes(\%::myconfig, $::form);
823 CT->query_titles_and_greetings(\%::myconfig, $::form);
824 CT->get_contact(\%::myconfig, $::form) if $::form->{cp_id};
826 $::form->{CUSTOM_VARIABLES}{Contacts} = CVar->get_custom_variables(module => 'Contacts', trans_id => $::form->{cp_id});
827 CVar->render_inputs(variables => $::form->{CUSTOM_VARIABLES}{Contacts}, name_prefix => 'cp')
828 if scalar @{ $::form->{CUSTOM_VARIABLES}->{Contacts} };
830 $::form->{contacts_label} = \&_contacts_label;
832 print $::form->ajax_response_header(), $::form->parse_html_template('ct/_contact');
834 $main::lxdebug->leave_sub();
838 $main::lxdebug->enter_sub();
840 $main::auth->assert('customer_vendor_edit');
842 CT->populate_drop_down_boxes(\%::myconfig, $::form);
843 CT->get_shipto(\%::myconfig, $::form) if $::form->{shipto_id};
845 $::form->{shipto_label} = \&_shipto_label;
847 print $::form->ajax_response_header(), $::form->parse_html_template('ct/_shipto');
849 $main::lxdebug->leave_sub();
853 $::lxdebug->enter_sub;
855 $::auth->assert('customer_vendor_edit');
856 $::auth->assert('sales_all_edit');
858 CT->get_delivery(\%::myconfig, $::form );
860 print $::form->ajax_response_header,
861 $::form->parse_html_template('ct/get_delivery', {
862 is_customer => $::form->{db} eq 'customer',
865 $::lxdebug->leave_sub;
869 $::lxdebug->enter_sub;
870 $::auth->assert('customer_vendor_edit');
872 if (!$::form->{shipto_id}) {
873 flash('error', $::locale->text('No shipto selected to delete'));
876 CT->get_shipto(\%::myconfig, $::form);
878 my $shipto = SL::DB::Manager::Shipto->find_by(shipto_id => $::form->{shipto_id});
881 $shipto->detach->save;
882 flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
885 flash('info', $::locale->text('Shipto deleted.'));
887 delete $::form->{$_} for grep /^shipto/, keys %$::form;
892 $::lxdebug->leave_sub;
896 $::lxdebug->enter_sub;
897 $::auth->assert('customer_vendor_edit');
899 if (!$::form->{cp_id}) {
900 flash('error', $::locale->text('No contact selected to delete'));
903 CT->get_contact(\%::myconfig, $::form);
905 my $contact = SL::DB::Manager::Contact->find_by(cp_id => $::form->{cp_id});
907 if ($contact->used) {
908 $contact->detach->save;
909 flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
912 flash('info', $::locale->text('Contact deleted.'));
914 delete $::form->{$_} for grep /^cp_/, keys %$::form;
919 $::lxdebug->leave_sub;
922 sub ajax_autocomplete {
923 $main::lxdebug->enter_sub();
925 my $form = $main::form;
926 my %myconfig = %main::myconfig;
928 $form->{column} = 'name' unless $form->{column} =~ /^name$/;
929 $form->{vc} = 'customer' unless $form->{vc} =~ /^customer|vendor$/;
930 $form->{db} = $form->{vc}; # CT expects this
931 $form->{$form->{column}} = $form->{q} || '';
932 $form->{limit} = ($form->{limit} * 1) || 10;
933 $form->{searchitems} ||= '';
935 CT->search(\%myconfig, $form);
937 print $form->ajax_response_header(),
938 $form->parse_html_template('ct/ajax_autocomplete');
940 $main::lxdebug->leave_sub();
943 sub continue { call_sub($main::form->{nextsub}); }