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., 51 Franklin Street, Fifth Floor, Boston,
30 #======================================================================
32 # customer/vendor module
34 #======================================================================
36 # $locale->text('Customers')
37 # $locale->text('Vendors')
38 # $locale->text('Add Customer')
39 # $locale->text('Add Vendor')
40 # $locale->text('Edit Customer')
41 # $locale->text('Edit Vendor')
42 # $locale->text('Customer saved!')
43 # $locale->text('Vendor saved!')
44 # $locale->text('Customer deleted!')
45 # $locale->text('Cannot delete customer!')
46 # $locale->text('Vendor deleted!')
47 # $locale->text('Cannot delete vendor!')
49 use POSIX qw(strftime);
54 use SL::Request qw(flatten);
57 use SL::DB::DeliveryTerm;
58 use SL::ReportGenerator;
59 use SL::MoreCommon qw(uri_encode);
61 require "bin/mozilla/common.pl";
62 require "bin/mozilla/reportgenerator.pl";
70 $main::lxdebug->enter_sub();
72 $main::auth->assert('customer_vendor_edit');
74 my $form = $main::form;
75 my $locale = $main::locale;
77 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
79 $form->get_lists("business_types" => "ALL_BUSINESS_TYPES",
80 "salesmen" => "ALL_SALESMEN");
81 $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
83 $form->{CUSTOM_VARIABLES} = CVar->get_configs('module' => 'CT');
84 ($form->{CUSTOM_VARIABLES_FILTER_CODE},
85 $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $form->{CUSTOM_VARIABLES},
86 'include_prefix' => 'l_',
87 'include_value' => 'Y');
89 $form->{title} = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
92 print $form->parse_html_template('ct/search');
94 $main::lxdebug->leave_sub();
98 $::lxdebug->enter_sub;
99 $::auth->assert('customer_vendor_edit');
101 $::form->{CUSTOM_VARIABLES} = CVar->get_configs('module' => 'Contacts');
102 ($::form->{CUSTOM_VARIABLES_FILTER_CODE},
103 $::form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $::form->{CUSTOM_VARIABLES},
104 'include_prefix' => 'l.',
105 'filter_prefix' => 'filter.',
106 'include_value' => 'Y');
108 $::form->{title} = $::locale->text('Search contacts');
110 print $::form->parse_html_template('ct/search_contact');
112 $::lxdebug->leave_sub;
116 $main::lxdebug->enter_sub();
118 $main::auth->assert('customer_vendor_edit');
120 my $form = $main::form;
121 my %myconfig = %main::myconfig;
122 my $locale = $main::locale;
124 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
126 report_generator_set_default_sort('name', 1);
128 CT->search(\%myconfig, \%$form);
130 my $cvar_configs = CVar->get_configs('module' => 'CT');
133 if ($form->{status} eq 'all') {
134 push @options, $locale->text('All');
135 } elsif ($form->{status} eq 'orphaned') {
136 push @options, $locale->text('Orphaned');
139 push @options, $locale->text('Name') . " : $form->{name}" if $form->{name};
140 push @options, $locale->text('Contact') . " : $form->{contact}" if $form->{contact};
141 push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}| if $form->{"$form->{db}number"};
142 push @options, $locale->text('E-mail') . " : $form->{email}" if $form->{email};
143 push @options, $locale->text('Contact person (surname)') . " : $form->{cp_name}" if $form->{cp_name};
144 push @options, $locale->text('Billing/shipping address (city)') . " : $form->{addr_city}" if $form->{addr_city};
145 push @options, $locale->text('Billing/shipping address (zipcode)') . " : $form->{addr_zipcode}" if $form->{addr_zipcode};
146 push @options, $locale->text('Billing/shipping address (street)') . " : $form->{addr_street}" if $form->{addr_street};
147 push @options, $locale->text('Billing/shipping address (country)') . " : $form->{addr_country}" if $form->{addr_country};
148 push @options, $locale->text('Billing/shipping address (GLN)') . " : $form->{addr_gln}" if $form->{addr_gln};
149 push @options, $locale->text('Quick Search') . " : $form->{all}" if $form->{all};
151 if ($form->{business_id}) {
152 my $business = SL::DB::Manager::Business->find_by(id => $form->{business_id});
154 my $label = $form->{IS_CUSTOMER} ? $::locale->text('Customer type') : $::locale->text('Vendor type');
155 push @options, $label . " : " . $business->description;
158 if ($form->{salesman_id}) {
159 my $salesman = SL::DB::Manager::Employee->find_by(id => $form->{salesman_id});
161 push @options, $locale->text('Salesman') . " : " . $salesman->name;
165 if ( $form->{insertdatefrom} or $form->{insertdateto} ) {
166 push @options, $locale->text('Insert Date');
167 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{insertdatefrom}, 1) if $form->{insertdatefrom};
168 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{insertdateto}, 1) if $form->{insertdateto};
172 'id', 'name', "$form->{db}number", 'contact', 'phone', 'discount',
173 'fax', 'email', 'taxnumber', 'street', 'zipcode' , 'city',
174 'business', 'payment', 'invnumber', 'ordnumber', 'quonumber', 'salesman',
175 'country', 'gln', 'insertdate', 'pricegroup'
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 'gln' => { 'text' => $locale->text('GLN'), },
203 'salesman' => { 'text' => $locale->text('Salesman'), },
204 'discount' => { 'text' => $locale->text('Discount'), },
205 'payment' => { 'text' => $locale->text('Payment Terms'), },
206 'insertdate' => { 'text' => $locale->text('Insert Date'), },
207 'pricegroup' => { 'text' => $locale->text('Pricegroup'), },
211 map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
213 my @hidden_variables = ( qw(
214 db status obsolete name contact email cp_name addr_street addr_zipcode
215 addr_city addr_country addr_gln business_id salesman_id insertdateto insertdatefrom all
216 ), "$form->{db}number",
217 map({ "cvar_$_->{name}" } @searchable_custom_variables),
218 map({'cvar_'. $_->{name} .'_qtyop'} grep({$_->{type} eq 'number'} @searchable_custom_variables)),
219 map({ "l_$_" } @columns),
222 my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
223 my $callback = build_std_url('action=list_names', grep { $form->{$_} } @hidden_nondefault);
224 $form->{callback} = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
227 my $sortdir = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
228 $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
231 my ($ordertype, $quotationtype, $attachment_basename);
232 if ($form->{IS_CUSTOMER}) {
233 $form->{title} = $locale->text('Customers');
234 $ordertype = 'sales_order';
235 $quotationtype = 'sales_quotation';
236 $attachment_basename = $locale->text('customer_list');
239 $form->{title} = $locale->text('Vendors');
240 $ordertype = 'purchase_order';
241 $quotationtype = 'request_quotation';
242 $attachment_basename = $locale->text('vendor_list');
245 my $report = SL::ReportGenerator->new(\%myconfig, $form);
247 $report->set_options('top_info_text' => join("\n", @options),
248 'raw_bottom_info_text' => $form->parse_html_template('ct/list_names_bottom'),
249 'output_format' => 'HTML',
250 'title' => $form->{title},
251 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
253 $report->set_options_from_form();
254 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
256 $report->set_columns(%column_defs);
257 $report->set_column_order(@columns);
259 $report->set_export_options('list_names', @hidden_variables, qw(sort sortdir));
261 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
263 CVar->add_custom_variables_to_report('module' => 'CT',
264 'trans_id_field' => 'id',
265 'configs' => $cvar_configs,
266 'column_defs' => \%column_defs,
267 'data' => $form->{CT});
271 foreach my $ref (@{ $form->{CT} }) {
272 my $row = { map { $_ => { 'data' => '' } } @columns };
274 if ($ref->{id} ne $previous_id) {
275 $previous_id = $ref->{id};
276 $ref->{discount} = $form->format_amount(\%myconfig, $ref->{discount} * 100.0, 2);
277 map { $row->{$_}->{data} = $ref->{$_} } @columns;
279 $row->{name}->{link} = build_std_url('script=controller.pl', 'action=CustomerVendor/edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
280 $row->{email}->{link} = 'mailto:' . E($ref->{email});
283 my $base_url = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
284 $row->{invnumber}->{link} = $base_url;
285 $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
286 $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
287 my $column = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
288 $row->{$column}->{data} = $ref->{$column};
290 if (my $number = SL::CTI->sanitize_number(number => $ref->{phone})) {
291 $row->{phone}->{link} = SL::CTI->call_link(number => $number);
292 $row->{phone}->{link_class} = 'cti_call_action';
295 $report->add_data($row);
298 $report->generate_with_headers();
300 $main::lxdebug->leave_sub();
304 $::lxdebug->enter_sub;
305 $::auth->assert('customer_vendor_edit');
307 $::form->{sortdir} = 1 unless defined $::form->{sortdir};
309 my @contacts = CT->search_contacts(
310 search_term => $::form->{search_term},
311 filter => $::form->{filter},
314 my $cvar_configs = CVar->get_configs('module' => 'Contacts');
317 cp_id vcname vcnumber cp_name cp_givenname cp_street cp_zipcode cp_city cp_phone1 cp_phone2 cp_privatphone
318 cp_mobile1 cp_mobile2 cp_fax cp_email cp_privatemail cp_abteilung cp_position cp_birthday cp_gender
321 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
322 my @searchable_custom_variables = grep { $_->{searchable} } @{ $cvar_configs };
323 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
325 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
329 @visible_columns = grep { $::form->{l}{$_} } @columns;
330 push @visible_columns, qw(cp_phone1 cp_phone2) if $::form->{l}{cp_phone};
331 push @visible_columns, qw(cp_mobile1 cp_mobile2) if $::form->{l}{cp_mobile};
333 @visible_columns = qw(vcname vcnumber cp_name cp_givenname cp_phone1 cp_phone2 cp_mobile1 cp_email);
337 'cp_id' => { 'text' => $::locale->text('ID'), },
338 'vcname' => { 'text' => $::locale->text('Customer/Vendor'), },
339 'vcnumber' => { 'text' => $::locale->text('Customer/Vendor Number'), },
340 'cp_name' => { 'text' => $::locale->text('Name'), },
341 'cp_givenname' => { 'text' => $::locale->text('Given Name'), },
342 'cp_street' => { 'text' => $::locale->text('Street'), },
343 'cp_zipcode' => { 'text' => $::locale->text('Zipcode'), },
344 'cp_city' => { 'text' => $::locale->text('City'), },
345 'cp_phone1' => { 'text' => $::locale->text('Phone1'), },
346 'cp_phone2' => { 'text' => $::locale->text('Phone2'), },
347 'cp_mobile1' => { 'text' => $::locale->text('Mobile1'), },
348 'cp_mobile2' => { 'text' => $::locale->text('Mobile2'), },
349 'cp_email' => { 'text' => $::locale->text('E-mail'), },
350 'cp_abteilung' => { 'text' => $::locale->text('Department'), },
351 'cp_position' => { 'text' => $::locale->text('Function/position'), },
352 'cp_birthday' => { 'text' => $::locale->text('Birthday'), },
353 'cp_gender' => { 'text' => $::locale->text('Gender'), },
354 'cp_fax' => { 'text' => $::locale->text('Fax'), },
355 'cp_privatphone' => { 'text' => $::locale->text('Private Phone') },
356 'cp_privatemail' => { 'text' => $::locale->text('Private E-mail') },
360 map { $column_defs{$_}->{visible} = 1 } @visible_columns;
362 my @hidden_variables = (qw(search_term filter l));
363 my $hide_vars = { map { $_ => $::form->{$_} } @hidden_variables };
364 my @hidden_nondefault = grep({ $::form->{$_} } @hidden_variables);
365 my $callback = build_std_url('action=list_contacts', join '&', map { E($_->[0]) . '=' . E($_->[1]) } @{ flatten($hide_vars) });
366 $::form->{callback} = "$callback&sort=" . E($::form->{sort});
368 map { $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=" . ($::form->{sort} eq $_ ? 1 - $::form->{sortdir} : $::form->{sortdir}) } @columns;
370 $::form->{title} = $::locale->text('Contacts');
372 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
375 push @options, $::locale->text('Search term') . ': ' . $::form->{search_term} if $::form->{search_term};
376 for (qw(cp_name cp_givenname cp_title cp_email cp_abteilung cp_project)) {
377 push @options, $column_defs{$_}{text} . ': ' . $::form->{filter}{$_} if $::form->{filter}{$_};
379 if ($::form->{filter}{status}) {
380 push @options, $::locale->text('Status') . ': ' . (
381 $::form->{filter}{status} =~ /active/ ? $::locale->text('Active') :
382 $::form->{filter}{status} =~ /orphaned/ ? $::locale->text('Orphaned') :
383 $::form->{filter}{status} =~ /all/ ? $::locale->text('All') : ''
388 $report->set_options('top_info_text' => join("\n", @options),
389 'output_format' => 'HTML',
390 'title' => $::form->{title},
391 'attachment_basename' => $::locale->text('contact_list') . strftime('_%Y%m%d', localtime time),
393 $report->set_options_from_form;
395 $report->set_columns(%column_defs);
396 $report->set_column_order(@columns);
398 $report->set_export_options('list_contacts', @hidden_variables);
400 $report->set_sort_indicator($::form->{sort}, $::form->{sortdir});
402 CVar->add_custom_variables_to_report('module' => 'Contacts',
403 'trans_id_field' => 'cp_id',
404 'configs' => $cvar_configs,
405 'column_defs' => \%column_defs,
406 'data' => \@contacts);
409 foreach my $ref (@contacts) {
410 my $row = { map { $_ => { 'data' => $ref->{$_} } } @columns };
412 $row->{vcname}->{link} = build_std_url('script=controller.pl', 'action=CustomerVendor/edit', 'id=' . E($ref->{vcid}), 'db=' . E($ref->{db}), 'callback', @hidden_nondefault);
413 $row->{vcnumber}->{link} = $row->{vcname}->{link};
415 for (qw(cp_email cp_privatemail)) {
416 $row->{$_}->{link} = 'mailto:' . E($ref->{$_}) if $ref->{$_};
419 for (qw(cp_phone1 cp_phone2 cp_mobile1)) {
420 next unless my $number = SL::CTI->sanitize_number(number => $ref->{$_});
422 $row->{$_}->{link} = SL::CTI->call_link(number => $number);
423 $row->{$_}->{link_class} = 'cti_call_action';
426 $report->add_data($row);
429 $report->generate_with_headers;
431 $::lxdebug->leave_sub;
434 sub continue { call_sub($main::form->{nextsub}); }