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;
164 'id', 'name', "$form->{db}number", 'contact', 'phone', 'discount',
165 'fax', 'email', 'taxnumber', 'street', 'zipcode' , 'city',
166 'business', 'payment', 'invnumber', 'ordnumber', 'quonumber', 'salesman',
170 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
171 my @searchable_custom_variables = grep { $_->{searchable} } @{ $cvar_configs };
172 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
174 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
177 'id' => { 'text' => $locale->text('ID'), },
178 "$form->{db}number" => { 'text' => $locale->text('Number'), },
179 'name' => { 'text' => $form->{IS_CUSTOMER} ? $::locale->text('Customer Name') : $::locale->text('Vendor Name'), },
180 'contact' => { 'text' => $locale->text('Contact'), },
181 'phone' => { 'text' => $locale->text('Phone'), },
182 'fax' => { 'text' => $locale->text('Fax'), },
183 'email' => { 'text' => $locale->text('E-mail'), },
184 'cc' => { 'text' => $locale->text('Cc'), },
185 'taxnumber' => { 'text' => $locale->text('Tax Number'), },
186 'business' => { 'text' => $locale->text('Type of Business'), },
187 'invnumber' => { 'text' => $locale->text('Invoice'), },
188 'ordnumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
189 'quonumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation') : $locale->text('Request for Quotation'), },
190 'street' => { 'text' => $locale->text('Street'), },
191 'zipcode' => { 'text' => $locale->text('Zipcode'), },
192 'city' => { 'text' => $locale->text('City'), },
193 'country' => { 'text' => $locale->text('Country'), },
194 'salesman' => { 'text' => $locale->text('Salesman'), },
195 'discount' => { 'text' => $locale->text('Discount'), },
196 'payment' => { 'text' => $locale->text('Payment Terms'), },
200 map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
202 my @hidden_variables = ( qw(
203 db status obsolete name contact email cp_name addr_street addr_zipcode
204 addr_city addr_country business_id salesman_id
205 ), "$form->{db}number",
206 map({ "cvar_$_->{name}" } @searchable_custom_variables),
207 map({'cvar_'. $_->{name} .'_qtyop'} grep({$_->{type} eq 'number'} @searchable_custom_variables)),
208 map({ "l_$_" } @columns),
211 my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
212 my $callback = build_std_url('action=list_names', grep { $form->{$_} } @hidden_nondefault);
213 $form->{callback} = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
216 my $sortdir = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
217 $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
220 my ($ordertype, $quotationtype, $attachment_basename);
221 if ($form->{IS_CUSTOMER}) {
222 $form->{title} = $locale->text('Customers');
223 $ordertype = 'sales_order';
224 $quotationtype = 'sales_quotation';
225 $attachment_basename = $locale->text('customer_list');
228 $form->{title} = $locale->text('Vendors');
229 $ordertype = 'purchase_order';
230 $quotationtype = 'request_quotation';
231 $attachment_basename = $locale->text('vendor_list');
234 my $report = SL::ReportGenerator->new(\%myconfig, $form);
236 $report->set_options('top_info_text' => join("\n", @options),
237 'raw_bottom_info_text' => $form->parse_html_template('ct/list_names_bottom'),
238 'output_format' => 'HTML',
239 'title' => $form->{title},
240 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
242 $report->set_options_from_form();
243 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
245 $report->set_columns(%column_defs);
246 $report->set_column_order(@columns);
248 $report->set_export_options('list_names', @hidden_variables, qw(sort sortdir));
250 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
252 CVar->add_custom_variables_to_report('module' => 'CT',
253 'trans_id_field' => 'id',
254 'configs' => $cvar_configs,
255 'column_defs' => \%column_defs,
256 'data' => $form->{CT});
260 foreach my $ref (@{ $form->{CT} }) {
261 my $row = { map { $_ => { 'data' => '' } } @columns };
263 if ($ref->{id} ne $previous_id) {
264 $previous_id = $ref->{id};
265 $ref->{discount} = $form->format_amount(\%myconfig, $ref->{discount} * 100.0, 2);
266 map { $row->{$_}->{data} = $ref->{$_} } @columns;
268 $row->{name}->{link} = build_std_url('script=controller.pl', 'action=CustomerVendor/edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
269 $row->{email}->{link} = 'mailto:' . E($ref->{email});
272 my $base_url = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
273 $row->{invnumber}->{link} = $base_url;
274 $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
275 $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
276 my $column = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
277 $row->{$column}->{data} = $ref->{$column};
279 if (my $number = SL::CTI->sanitize_number(number => $ref->{phone})) {
280 $row->{phone}->{link} = SL::CTI->call_link(number => $number);
281 $row->{phone}->{link_class} = 'cti_call_action';
284 $report->add_data($row);
287 $report->generate_with_headers();
289 $main::lxdebug->leave_sub();
293 $::lxdebug->enter_sub;
294 $::auth->assert('customer_vendor_edit');
296 $::form->{sortdir} = 1 unless defined $::form->{sortdir};
298 my @contacts = CT->search_contacts(
299 search_term => $::form->{search_term},
300 filter => $::form->{filter},
303 my $cvar_configs = CVar->get_configs('module' => 'Contacts');
306 cp_id vcname vcnumber cp_name cp_givenname cp_street cp_zipcode cp_city cp_phone1 cp_phone2 cp_privatphone
307 cp_mobile1 cp_mobile2 cp_fax cp_email cp_privatemail cp_abteilung cp_position cp_birthday cp_gender
310 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
311 my @searchable_custom_variables = grep { $_->{searchable} } @{ $cvar_configs };
312 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
314 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
318 @visible_columns = grep { $::form->{l}{$_} } @columns;
319 push @visible_columns, qw(cp_phone1 cp_phone2) if $::form->{l}{cp_phone};
320 push @visible_columns, qw(cp_mobile1 cp_mobile2) if $::form->{l}{cp_mobile};
322 @visible_columns = qw(vcname vcnumber cp_name cp_givenname cp_phone1 cp_phone2 cp_mobile1 cp_email);
326 'cp_id' => { 'text' => $::locale->text('ID'), },
327 'vcname' => { 'text' => $::locale->text('Customer/Vendor'), },
328 'vcnumber' => { 'text' => $::locale->text('Customer/Vendor Number'), },
329 'cp_name' => { 'text' => $::locale->text('Name'), },
330 'cp_givenname' => { 'text' => $::locale->text('Given Name'), },
331 'cp_street' => { 'text' => $::locale->text('Street'), },
332 'cp_zipcode' => { 'text' => $::locale->text('Zipcode'), },
333 'cp_city' => { 'text' => $::locale->text('City'), },
334 'cp_phone1' => { 'text' => $::locale->text('Phone1'), },
335 'cp_phone2' => { 'text' => $::locale->text('Phone2'), },
336 'cp_mobile1' => { 'text' => $::locale->text('Mobile1'), },
337 'cp_mobile2' => { 'text' => $::locale->text('Mobile2'), },
338 'cp_email' => { 'text' => $::locale->text('E-mail'), },
339 'cp_abteilung' => { 'text' => $::locale->text('Department'), },
340 'cp_position' => { 'text' => $::locale->text('Function/position'), },
341 'cp_birthday' => { 'text' => $::locale->text('Birthday'), },
342 'cp_gender' => { 'text' => $::locale->text('Gender'), },
343 'cp_fax' => { 'text' => $::locale->text('Fax'), },
344 'cp_privatphone' => { 'text' => $::locale->text('Private Phone') },
345 'cp_privatemail' => { 'text' => $::locale->text('Private E-mail') },
349 map { $column_defs{$_}->{visible} = 1 } @visible_columns;
351 my @hidden_variables = (qw(search_term filter l));
352 my $hide_vars = { map { $_ => $::form->{$_} } @hidden_variables };
353 my @hidden_nondefault = grep({ $::form->{$_} } @hidden_variables);
354 my $callback = build_std_url('action=list_contacts', join '&', map { E($_->[0]) . '=' . E($_->[1]) } @{ flatten($hide_vars) });
355 $::form->{callback} = "$callback&sort=" . E($::form->{sort});
357 map { $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=" . ($::form->{sort} eq $_ ? 1 - $::form->{sortdir} : $::form->{sortdir}) } @columns;
359 $::form->{title} = $::locale->text('Contacts');
361 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
364 push @options, $::locale->text('Search term') . ': ' . $::form->{search_term} if $::form->{search_term};
365 for (qw(cp_name cp_givenname cp_title cp_email cp_abteilung cp_project)) {
366 push @options, $column_defs{$_}{text} . ': ' . $::form->{filter}{$_} if $::form->{filter}{$_};
368 if ($::form->{filter}{status}) {
369 push @options, $::locale->text('Status') . ': ' . (
370 $::form->{filter}{status} =~ /active/ ? $::locale->text('Active') :
371 $::form->{filter}{status} =~ /orphaned/ ? $::locale->text('Orphaned') :
372 $::form->{filter}{status} =~ /all/ ? $::locale->text('All') : ''
377 $report->set_options('top_info_text' => join("\n", @options),
378 'output_format' => 'HTML',
379 'title' => $::form->{title},
380 'attachment_basename' => $::locale->text('contact_list') . strftime('_%Y%m%d', localtime time),
382 $report->set_options_from_form;
384 $report->set_columns(%column_defs);
385 $report->set_column_order(@columns);
387 $report->set_export_options('list_contacts', @hidden_variables);
389 $report->set_sort_indicator($::form->{sort}, $::form->{sortdir});
391 CVar->add_custom_variables_to_report('module' => 'Contacts',
392 'trans_id_field' => 'cp_id',
393 'configs' => $cvar_configs,
394 'column_defs' => \%column_defs,
395 'data' => \@contacts);
398 foreach my $ref (@contacts) {
399 my $row = { map { $_ => { 'data' => $ref->{$_} } } @columns };
401 $row->{vcname}->{link} = build_std_url('script=controller.pl', 'action=CustomerVendor/edit', 'id=' . E($ref->{vcid}), 'db=' . E($ref->{db}), 'callback', @hidden_nondefault);
402 $row->{vcnumber}->{link} = $row->{vcname}->{link};
404 for (qw(cp_email cp_privatemail)) {
405 $row->{$_}->{link} = 'mailto:' . E($ref->{$_}) if $ref->{$_};
408 for (qw(cp_phone1 cp_phone2 cp_mobile1)) {
409 next unless my $number = SL::CTI->sanitize_number(number => $ref->{$_});
411 $row->{$_}->{link} = SL::CTI->call_link(number => $number);
412 $row->{$_}->{link_class} = 'cti_call_action';
415 $report->add_data($row);
418 $report->generate_with_headers;
420 $::lxdebug->leave_sub;
423 sub continue { call_sub($main::form->{nextsub}); }