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);
53 use SL::Request qw(flatten);
56 use SL::DB::DeliveryTerm;
57 use SL::Helper::Flash;
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->{zipcode}" if $form->{addr_zipcode};
146 push @options, $locale->text('Billing/shipping address (street)') . " : $form->{street}" if $form->{addr_street};
147 push @options, $locale->text('Billing/shipping address (country)') . " : $form->{country}" if $form->{addr_country};
149 if ($form->{business_id}) {
150 my $business = SL::DB::Manager::Business->find_by(id => $form->{business_id});
152 my $label = $form->{IS_CUSTOMER} ? $::locale->text('Customer type') : $::locale->text('Vendor type');
153 push @options, $label . " : " . $business->description;
156 if ($form->{salesman_id}) {
157 my $salesman = SL::DB::Manager::Employee->find_by(id => $form->{salesman_id});
159 push @options, $locale->text('Salesman') . " : " . $salesman->name;
163 if ( $form->{insertdatefrom} or $form->{insertdateto} ) {
164 push @options, $locale->text('Insert Date');
165 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{insertdatefrom}, 1) if $form->{insertdatefrom};
166 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{insertdateto}, 1) if $form->{insertdateto};
170 'id', 'name', "$form->{db}number", 'contact', 'phone', 'discount',
171 'fax', 'email', 'taxnumber', 'street', 'zipcode' , 'city',
172 'business', 'payment', 'invnumber', 'ordnumber', 'quonumber', 'salesman',
173 'country', 'insertdate'
176 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
177 my @searchable_custom_variables = grep { $_->{searchable} } @{ $cvar_configs };
178 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
180 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
183 'id' => { 'text' => $locale->text('ID'), },
184 "$form->{db}number" => { 'text' => $locale->text('Number'), },
185 'name' => { 'text' => $form->{IS_CUSTOMER} ? $::locale->text('Customer Name') : $::locale->text('Vendor Name'), },
186 'contact' => { 'text' => $locale->text('Contact'), },
187 'phone' => { 'text' => $locale->text('Phone'), },
188 'fax' => { 'text' => $locale->text('Fax'), },
189 'email' => { 'text' => $locale->text('E-mail'), },
190 'cc' => { 'text' => $locale->text('Cc'), },
191 'taxnumber' => { 'text' => $locale->text('Tax Number'), },
192 'business' => { 'text' => $locale->text('Type of Business'), },
193 'invnumber' => { 'text' => $locale->text('Invoice'), },
194 'ordnumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
195 'quonumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation') : $locale->text('Request for Quotation'), },
196 'street' => { 'text' => $locale->text('Street'), },
197 'zipcode' => { 'text' => $locale->text('Zipcode'), },
198 'city' => { 'text' => $locale->text('City'), },
199 'country' => { 'text' => $locale->text('Country'), },
200 'salesman' => { 'text' => $locale->text('Salesman'), },
201 'discount' => { 'text' => $locale->text('Discount'), },
202 'payment' => { 'text' => $locale->text('Payment Terms'), },
203 'insertdate' => { 'text' => $locale->text('Insert Date'), },
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 salesman_id insertdateto insertdatefrom
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 if (my $number = SL::CTI->sanitize_number(number => $ref->{phone})) {
287 $row->{phone}->{link} = SL::CTI->call_link(number => $number);
288 $row->{phone}->{link_class} = 'cti_call_action';
291 $report->add_data($row);
294 $report->generate_with_headers();
296 $main::lxdebug->leave_sub();
300 $::lxdebug->enter_sub;
301 $::auth->assert('customer_vendor_edit');
303 $::form->{sortdir} = 1 unless defined $::form->{sortdir};
305 my @contacts = CT->search_contacts(
306 search_term => $::form->{search_term},
307 filter => $::form->{filter},
310 my $cvar_configs = CVar->get_configs('module' => 'Contacts');
313 cp_id vcname vcnumber cp_name cp_givenname cp_street cp_zipcode cp_city cp_phone1 cp_phone2 cp_privatphone
314 cp_mobile1 cp_mobile2 cp_fax cp_email cp_privatemail cp_abteilung cp_position cp_birthday cp_gender
317 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
318 my @searchable_custom_variables = grep { $_->{searchable} } @{ $cvar_configs };
319 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
321 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
325 @visible_columns = grep { $::form->{l}{$_} } @columns;
326 push @visible_columns, qw(cp_phone1 cp_phone2) if $::form->{l}{cp_phone};
327 push @visible_columns, qw(cp_mobile1 cp_mobile2) if $::form->{l}{cp_mobile};
329 @visible_columns = qw(vcname vcnumber cp_name cp_givenname cp_phone1 cp_phone2 cp_mobile1 cp_email);
333 'cp_id' => { 'text' => $::locale->text('ID'), },
334 'vcname' => { 'text' => $::locale->text('Customer/Vendor'), },
335 'vcnumber' => { 'text' => $::locale->text('Customer/Vendor Number'), },
336 'cp_name' => { 'text' => $::locale->text('Name'), },
337 'cp_givenname' => { 'text' => $::locale->text('Given Name'), },
338 'cp_street' => { 'text' => $::locale->text('Street'), },
339 'cp_zipcode' => { 'text' => $::locale->text('Zipcode'), },
340 'cp_city' => { 'text' => $::locale->text('City'), },
341 'cp_phone1' => { 'text' => $::locale->text('Phone1'), },
342 'cp_phone2' => { 'text' => $::locale->text('Phone2'), },
343 'cp_mobile1' => { 'text' => $::locale->text('Mobile1'), },
344 'cp_mobile2' => { 'text' => $::locale->text('Mobile2'), },
345 'cp_email' => { 'text' => $::locale->text('E-mail'), },
346 'cp_abteilung' => { 'text' => $::locale->text('Department'), },
347 'cp_position' => { 'text' => $::locale->text('Function/position'), },
348 'cp_birthday' => { 'text' => $::locale->text('Birthday'), },
349 'cp_gender' => { 'text' => $::locale->text('Gender'), },
350 'cp_fax' => { 'text' => $::locale->text('Fax'), },
351 'cp_privatphone' => { 'text' => $::locale->text('Private Phone') },
352 'cp_privatemail' => { 'text' => $::locale->text('Private E-mail') },
356 map { $column_defs{$_}->{visible} = 1 } @visible_columns;
358 my @hidden_variables = (qw(search_term filter l));
359 my $hide_vars = { map { $_ => $::form->{$_} } @hidden_variables };
360 my @hidden_nondefault = grep({ $::form->{$_} } @hidden_variables);
361 my $callback = build_std_url('action=list_contacts', join '&', map { E($_->[0]) . '=' . E($_->[1]) } @{ flatten($hide_vars) });
362 $::form->{callback} = "$callback&sort=" . E($::form->{sort});
364 map { $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=" . ($::form->{sort} eq $_ ? 1 - $::form->{sortdir} : $::form->{sortdir}) } @columns;
366 $::form->{title} = $::locale->text('Contacts');
368 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
371 push @options, $::locale->text('Search term') . ': ' . $::form->{search_term} if $::form->{search_term};
372 for (qw(cp_name cp_givenname cp_title cp_email cp_abteilung cp_project)) {
373 push @options, $column_defs{$_}{text} . ': ' . $::form->{filter}{$_} if $::form->{filter}{$_};
375 if ($::form->{filter}{status}) {
376 push @options, $::locale->text('Status') . ': ' . (
377 $::form->{filter}{status} =~ /active/ ? $::locale->text('Active') :
378 $::form->{filter}{status} =~ /orphaned/ ? $::locale->text('Orphaned') :
379 $::form->{filter}{status} =~ /all/ ? $::locale->text('All') : ''
384 $report->set_options('top_info_text' => join("\n", @options),
385 'output_format' => 'HTML',
386 'title' => $::form->{title},
387 'attachment_basename' => $::locale->text('contact_list') . strftime('_%Y%m%d', localtime time),
389 $report->set_options_from_form;
391 $report->set_columns(%column_defs);
392 $report->set_column_order(@columns);
394 $report->set_export_options('list_contacts', @hidden_variables);
396 $report->set_sort_indicator($::form->{sort}, $::form->{sortdir});
398 CVar->add_custom_variables_to_report('module' => 'Contacts',
399 'trans_id_field' => 'cp_id',
400 'configs' => $cvar_configs,
401 'column_defs' => \%column_defs,
402 'data' => \@contacts);
405 foreach my $ref (@contacts) {
406 my $row = { map { $_ => { 'data' => $ref->{$_} } } @columns };
408 $row->{vcname}->{link} = build_std_url('script=controller.pl', 'action=CustomerVendor/edit', 'id=' . E($ref->{vcid}), 'db=' . E($ref->{db}), 'callback', @hidden_nondefault);
409 $row->{vcnumber}->{link} = $row->{vcname}->{link};
411 for (qw(cp_email cp_privatemail)) {
412 $row->{$_}->{link} = 'mailto:' . E($ref->{$_}) if $ref->{$_};
415 for (qw(cp_phone1 cp_phone2 cp_mobile1)) {
416 next unless my $number = SL::CTI->sanitize_number(number => $ref->{$_});
418 $row->{$_}->{link} = SL::CTI->call_link(number => $number);
419 $row->{$_}->{link_class} = 'cti_call_action';
422 $report->add_data($row);
425 $report->generate_with_headers;
427 $::lxdebug->leave_sub;
430 sub continue { call_sub($main::form->{nextsub}); }