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->{title} = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
107 print $form->parse_html_template('ct/search');
109 $main::lxdebug->leave_sub();
113 $::lxdebug->enter_sub;
114 $::auth->assert('customer_vendor_edit');
116 $::form->{CUSTOM_VARIABLES} = CVar->get_configs('module' => 'Contacts');
117 ($::form->{CUSTOM_VARIABLES_FILTER_CODE},
118 $::form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $::form->{CUSTOM_VARIABLES},
119 'include_prefix' => 'l.',
120 'filter_prefix' => 'filter.',
121 'include_value' => 'Y');
123 $::form->{title} = $::locale->text('Search contacts');
125 print $::form->parse_html_template('ct/search_contact');
127 $::lxdebug->leave_sub;
131 $main::lxdebug->enter_sub();
133 $main::auth->assert('customer_vendor_edit');
135 my $form = $main::form;
136 my %myconfig = %main::myconfig;
137 my $locale = $main::locale;
139 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
141 report_generator_set_default_sort('name', 1);
143 CT->search(\%myconfig, \%$form);
145 my $cvar_configs = CVar->get_configs('module' => 'CT');
148 if ($form->{status} eq 'all') {
149 push @options, $locale->text('All');
150 } elsif ($form->{status} eq 'orphaned') {
151 push @options, $locale->text('Orphaned');
154 push @options, $locale->text('Name') . " : $form->{name}" if $form->{name};
155 push @options, $locale->text('Contact') . " : $form->{contact}" if $form->{contact};
156 push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}| if $form->{"$form->{db}number"};
157 push @options, $locale->text('E-mail') . " : $form->{email}" if $form->{email};
158 push @options, $locale->text('Contact person (surname)') . " : $form->{cp_name}" if $form->{cp_name};
159 push @options, $locale->text('Billing/shipping address (city)') . " : $form->{addr_city}" if $form->{addr_city};
160 push @options, $locale->text('Billing/shipping address (zipcode)') . " : $form->{zipcode}" if $form->{addr_zipcode};
161 push @options, $locale->text('Billing/shipping address (street)') . " : $form->{street}" if $form->{addr_street};
162 push @options, $locale->text('Billing/shipping address (country)') . " : $form->{country}" if $form->{addr_country};
164 if ($form->{business_id}) {
165 my $business = SL::DB::Manager::Business->find_by(id => $form->{business_id});
167 my $label = $form->{IS_CUSTOMER} ? $::locale->text('Customer type') : $::locale->text('Vendor type');
168 push @options, $label . " : " . $business->description;
173 'id', 'name', "$form->{db}number", 'contact', 'phone', 'discount',
174 'fax', 'email', 'taxnumber', 'street', 'zipcode' , 'city',
175 'business', 'invnumber', 'ordnumber', 'quonumber', 'salesman', 'country'
178 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
179 my @searchable_custom_variables = grep { $_->{searchable} } @{ $cvar_configs };
180 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
182 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
185 'id' => { 'text' => $locale->text('ID'), },
186 "$form->{db}number" => { 'text' => $locale->text('Number'), },
187 'name' => { 'text' => $form->{IS_CUSTOMER} ? $::locale->text('Customer Name') : $::locale->text('Vendor Name'), },
188 'contact' => { 'text' => $locale->text('Contact'), },
189 'phone' => { 'text' => $locale->text('Phone'), },
190 'fax' => { 'text' => $locale->text('Fax'), },
191 'email' => { 'text' => $locale->text('E-mail'), },
192 'cc' => { 'text' => $locale->text('Cc'), },
193 'taxnumber' => { 'text' => $locale->text('Tax Number'), },
194 'business' => { 'text' => $locale->text('Type of Business'), },
195 'invnumber' => { 'text' => $locale->text('Invoice'), },
196 'ordnumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
197 'quonumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation') : $locale->text('Request for Quotation'), },
198 'street' => { 'text' => $locale->text('Street'), },
199 'zipcode' => { 'text' => $locale->text('Zipcode'), },
200 'city' => { 'text' => $locale->text('City'), },
201 'country' => { 'text' => $locale->text('Country'), },
202 'salesman' => { 'text' => $locale->text('Salesman'), },
203 'discount' => { 'text' => $locale->text('Discount'), },
207 map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
209 my @hidden_variables = ( qw(
210 db status obsolete name contact email cp_name addr_street addr_zipcode
211 addr_city addr_country business_id
212 ), "$form->{db}number",
213 map({ "cvar_$_->{name}" } @searchable_custom_variables),
214 map({'cvar_'. $_->{name} .'_qtyop'} grep({$_->{type} eq 'number'} @searchable_custom_variables)),
215 map({ "l_$_" } @columns),
218 my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
219 my $callback = build_std_url('action=list_names', grep { $form->{$_} } @hidden_nondefault);
220 $form->{callback} = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
223 my $sortdir = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
224 $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
227 my ($ordertype, $quotationtype, $attachment_basename);
228 if ($form->{IS_CUSTOMER}) {
229 $form->{title} = $locale->text('Customers');
230 $ordertype = 'sales_order';
231 $quotationtype = 'sales_quotation';
232 $attachment_basename = $locale->text('customer_list');
235 $form->{title} = $locale->text('Vendors');
236 $ordertype = 'purchase_order';
237 $quotationtype = 'request_quotation';
238 $attachment_basename = $locale->text('vendor_list');
241 my $report = SL::ReportGenerator->new(\%myconfig, $form);
243 $report->set_options('top_info_text' => join("\n", @options),
244 'raw_bottom_info_text' => $form->parse_html_template('ct/list_names_bottom'),
245 'output_format' => 'HTML',
246 'title' => $form->{title},
247 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
249 $report->set_options_from_form();
250 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
252 $report->set_columns(%column_defs);
253 $report->set_column_order(@columns);
255 $report->set_export_options('list_names', @hidden_variables, qw(sort sortdir));
257 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
259 CVar->add_custom_variables_to_report('module' => 'CT',
260 'trans_id_field' => 'id',
261 'configs' => $cvar_configs,
262 'column_defs' => \%column_defs,
263 'data' => $form->{CT});
267 foreach my $ref (@{ $form->{CT} }) {
268 my $row = { map { $_ => { 'data' => '' } } @columns };
270 if ($ref->{id} ne $previous_id) {
271 $previous_id = $ref->{id};
272 $ref->{discount} = $form->format_amount(\%myconfig, $ref->{discount} * 100.0, 2);
273 map { $row->{$_}->{data} = $ref->{$_} } @columns;
275 $row->{name}->{link} = build_std_url('script=controller.pl', 'action=CustomerVendor/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('script=controller.pl', 'action=CustomerVendor/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 $::instance_conf->get_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->{SHIPTO_ALL} = [ +{ shipto_id => '0', shiptoname => $::locale->text('All') }, @{ $form->{SHIPTO} } ];
480 $form->{title} = $form->{title_save}
481 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
483 CT->query_titles_and_greetings(\%myconfig, \%$form);
484 map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(COMPANY_GREETINGS);
486 $form->{NOTES} ||= [ ];
488 if (!$form->{'language_id'}) {
489 my $l_id = SL::DB::Default->get->{'language_id'};
491 $form->{'default_language_id'} = $l_id;
495 if (!$form->{'id'}) {
496 $form->{'currency'} = $form->get_default_currency(\%myconfig);
498 $form->{currency} = $form->{curr};
501 $::form->{CUSTOM_VARIABLES} = { };
502 my %specs = ( CT => { field => 'id', name_prefix => '', },
503 Contacts => { field => 'cp_id', name_prefix => 'cp', },
506 for my $module (keys %specs) {
507 my $spec = $specs{$module};
509 $::form->{CUSTOM_VARIABLES}->{$module} = CVar->get_custom_variables(module => $module, trans_id => $::form->{ $spec->{field} });
510 CVar->render_inputs(variables => $::form->{CUSTOM_VARIABLES}->{$module}, name_prefix => $spec->{name_prefix})
511 if scalar @{ $::form->{CUSTOM_VARIABLES}->{$module} };
515 print $form->parse_html_template('ct/form_header');
517 $main::lxdebug->leave_sub();
521 $main::lxdebug->enter_sub();
523 $main::auth->assert('customer_vendor_edit');
525 my $form = $main::form;
527 print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
528 is_customer => $form->{db} eq 'customer' });
529 $main::lxdebug->leave_sub();
533 $main::auth->assert('customer_vendor_edit');
535 $::form->isblank("name", $::locale->text("Name missing!"));
537 if ($::form->{new_salesman_id} && $::instance_conf->get_vertreter) {
538 $::form->{salesman_id} = $::form->{new_salesman_id};
539 delete $::form->{new_salesman_id};
542 my $res = $::form->{db} eq 'customer' ? CT->save_customer(\%::myconfig, $::form) : CT->save_vendor(\%::myconfig, $::form);
545 if ($::form->{"db"} eq "customer") {
546 $::form->error($::locale->text('This customer number is already in use.'));
548 $::form->error($::locale->text('This vendor number is already in use.'));
553 sub add_transaction {
554 $main::lxdebug->enter_sub();
556 $main::auth->assert('customer_vendor_edit & ' .
557 '(general_ledger | invoice_edit | vendor_invoice_edit | ' .
558 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit)');
560 my $form = $main::form;
561 my %myconfig = %main::myconfig;
562 my $locale = $main::locale;
564 # # saving the history
565 # if(!exists $form->{addition}) {
566 # $form->{addition} = "ADD TRANSACTION";
567 # $form->save_history;
569 # # /saving the history
573 $form->{callback} = $form->escape($form->{callback}, 1);
574 my $name = $form->escape("$form->{name}", 1);
577 "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
580 $main::lxdebug->leave_sub();
583 sub save_and_ap_transaction {
584 $main::lxdebug->enter_sub();
586 $main::auth->assert('customer_vendor_edit & general_ledger');
588 my $form = $main::form;
589 my %myconfig = %main::myconfig;
591 $form->{script} = "ap.pl";
593 if(!exists $form->{addition}) {
594 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
595 $form->{addition} = "SAVED";
598 # /saving the history
600 $main::lxdebug->leave_sub();
603 sub save_and_ar_transaction {
604 $main::lxdebug->enter_sub();
606 $main::auth->assert('customer_vendor_edit & general_ledger');
608 my $form = $main::form;
609 my %myconfig = %main::myconfig;
611 $form->{script} = "ar.pl";
613 if(!exists $form->{addition}) {
614 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
615 $form->{addition} = "SAVED";
618 # /saving the history
620 $main::lxdebug->leave_sub();
623 sub save_and_invoice {
624 $main::lxdebug->enter_sub();
626 my $form = $main::form;
627 my %myconfig = %main::myconfig;
629 if ($form->{db} eq 'customer') {
630 $main::auth->assert('customer_vendor_edit & invoice_edit');
632 $main::auth->assert('customer_vendor_edit & vendor_invoice_edit');
635 $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
636 $form->{type} = "invoice";
638 if(!exists $form->{addition}) {
639 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
640 $form->{addition} = "SAVED";
643 # /saving the history
645 $main::lxdebug->leave_sub();
649 $main::lxdebug->enter_sub();
651 $main::auth->assert('customer_vendor_edit & request_quotation_edit');
653 my $form = $main::form;
654 my %myconfig = %main::myconfig;
656 $form->{script} = "oe.pl";
657 $form->{type} = "request_quotation";
659 if(!exists $form->{addition}) {
660 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
661 $form->{addition} = "SAVED";
664 # /saving the history
666 $main::lxdebug->leave_sub();
669 sub save_and_quotation {
670 $main::lxdebug->enter_sub();
672 $main::auth->assert('customer_vendor_edit & sales_quotation_edit');
674 my $form = $main::form;
675 my %myconfig = %main::myconfig;
677 $form->{script} = "oe.pl";
678 $form->{type} = "sales_quotation";
680 if(!exists $form->{addition}) {
681 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
682 $form->{addition} = "SAVED";
685 # /saving the history
687 $main::lxdebug->leave_sub();
691 $main::lxdebug->enter_sub();
693 my $form = $main::form;
694 my %myconfig = %main::myconfig;
696 if ($form->{db} eq 'customer') {
697 $main::auth->assert('customer_vendor_edit & sales_order_edit');
699 $main::auth->assert('customer_vendor_edit & purchase_order_edit');
702 $form->{script} = "oe.pl";
704 ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
706 if(!exists $form->{addition}) {
707 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
708 $form->{addition} = "SAVED";
711 # /saving the history
713 $main::lxdebug->leave_sub();
717 $main::lxdebug->enter_sub();
719 $main::auth->assert('customer_vendor_edit');
721 my $form = $main::form;
722 my %myconfig = %main::myconfig;
723 my $locale = $main::locale;
725 my $msg = ucfirst $form->{db};
731 if(!exists $form->{addition}) {
732 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
733 $form->{addition} = "SAVED";
736 # /saving the history
737 $form->redirect($locale->text($msg));
739 $main::lxdebug->leave_sub();
743 $main::lxdebug->enter_sub();
745 $main::auth->assert('customer_vendor_edit');
747 my $form = $main::form;
748 my %myconfig = %main::myconfig;
749 my $locale = $main::locale;
751 my $msg = ucfirst $form->{db};
757 if(!exists $form->{addition}) {
758 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
759 $form->{addition} = "SAVED";
762 # /saving the history
765 $main::lxdebug->leave_sub();
770 $main::lxdebug->enter_sub();
772 $main::auth->assert('customer_vendor_edit');
774 my $form = $main::form;
775 my %myconfig = %main::myconfig;
776 my $locale = $main::locale;
778 CT->delete(\%myconfig, \%$form);
780 my $msg = ucfirst $form->{db};
783 if(!exists $form->{addition}) {
784 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
785 $form->{addition} = "DELETED";
788 # /saving the history
789 $form->redirect($locale->text($msg));
791 $main::lxdebug->leave_sub();
795 $main::lxdebug->enter_sub();
797 $main::auth->assert('customer_vendor_edit');
799 my $form = $main::form;
804 $main::lxdebug->leave_sub();
808 $main::lxdebug->enter_sub();
810 $main::auth->assert('customer_vendor_edit');
812 my $form = $main::form;
815 $main::lxdebug->leave_sub();
819 $main::lxdebug->enter_sub();
821 $main::auth->assert('customer_vendor_edit');
823 CT->populate_drop_down_boxes(\%::myconfig, $::form);
824 CT->query_titles_and_greetings(\%::myconfig, $::form);
825 CT->get_contact(\%::myconfig, $::form) if $::form->{cp_id};
827 $::form->{CUSTOM_VARIABLES}{Contacts} = CVar->get_custom_variables(module => 'Contacts', trans_id => $::form->{cp_id});
828 CVar->render_inputs(variables => $::form->{CUSTOM_VARIABLES}{Contacts}, name_prefix => 'cp')
829 if scalar @{ $::form->{CUSTOM_VARIABLES}->{Contacts} };
831 $::form->{contacts_label} = \&_contacts_label;
833 print $::form->ajax_response_header(), $::form->parse_html_template('ct/_contact');
835 $main::lxdebug->leave_sub();
839 $main::lxdebug->enter_sub();
841 $main::auth->assert('customer_vendor_edit');
843 CT->populate_drop_down_boxes(\%::myconfig, $::form);
844 CT->get_shipto(\%::myconfig, $::form) if $::form->{shipto_id};
846 $::form->{shipto_label} = \&_shipto_label;
848 print $::form->ajax_response_header(), $::form->parse_html_template('ct/_shipto');
850 $main::lxdebug->leave_sub();
854 $::lxdebug->enter_sub;
856 $::auth->assert('customer_vendor_edit');
857 $::auth->assert('sales_all_edit');
859 CT->get_delivery(\%::myconfig, $::form );
861 print $::form->ajax_response_header,
862 $::form->parse_html_template('ct/get_delivery', {
863 is_customer => $::form->{db} eq 'customer',
866 $::lxdebug->leave_sub;
870 $::lxdebug->enter_sub;
871 $::auth->assert('customer_vendor_edit');
873 if (!$::form->{shipto_id}) {
874 flash('error', $::locale->text('No shipto selected to delete'));
877 CT->get_shipto(\%::myconfig, $::form);
879 my $shipto = SL::DB::Manager::Shipto->find_by(shipto_id => $::form->{shipto_id});
882 $shipto->detach->save;
883 flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
886 flash('info', $::locale->text('Shipto deleted.'));
888 delete $::form->{$_} for grep /^shipto/, keys %$::form;
893 $::lxdebug->leave_sub;
897 $::lxdebug->enter_sub;
898 $::auth->assert('customer_vendor_edit');
900 if (!$::form->{cp_id}) {
901 flash('error', $::locale->text('No contact selected to delete'));
904 CT->get_contact(\%::myconfig, $::form);
906 my $contact = SL::DB::Manager::Contact->find_by(cp_id => $::form->{cp_id});
908 if ($contact->used) {
909 $contact->detach->save;
910 flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
913 flash('info', $::locale->text('Contact deleted.'));
915 delete $::form->{$_} for grep /^cp_/, keys %$::form;
920 $::lxdebug->leave_sub;
923 sub ajax_autocomplete {
924 $main::lxdebug->enter_sub();
926 my $form = $main::form;
927 my %myconfig = %main::myconfig;
929 $form->{column} = 'name' unless $form->{column} =~ /^name$/;
930 $form->{vc} = 'customer' unless $form->{vc} =~ /^customer|vendor$/;
931 $form->{db} = $form->{vc}; # CT expects this
932 $form->{$form->{column}} = $form->{q} || '';
933 $form->{limit} = ($form->{limit} * 1) || 10;
934 $form->{searchitems} ||= '';
936 CT->search(\%myconfig, $form);
938 print $form->ajax_response_header(),
939 $form->parse_html_template('ct/ajax_autocomplete');
941 $main::lxdebug->leave_sub();
944 sub continue { call_sub($main::form->{nextsub}); }