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!')
49 use POSIX qw(strftime);
53 use SL::ReportGenerator;
55 require "bin/mozilla/common.pl";
56 require "bin/mozilla/reportgenerator.pl";
63 $lxdebug->enter_sub();
65 $auth->assert('customer_vendor_edit');
67 $form->{title} = "Add";
68 $form->{callback} = "$form->{script}?action=add&db=$form->{db}" unless $form->{callback};
70 CT->populate_drop_down_boxes(\%myconfig, \%$form);
75 $lxdebug->leave_sub();
79 $lxdebug->enter_sub();
81 $auth->assert('customer_vendor_edit');
83 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
85 $form->get_lists("business_types" => "ALL_BUSINESS_TYPES");
86 $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
88 $form->{CUSTOM_VARIABLES} = CVar->get_configs('module' => 'CT');
89 ($form->{CUSTOM_VARIABLES_FILTER_CODE},
90 $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $form->{CUSTOM_VARIABLES},
91 'include_prefix' => 'l_',
92 'include_value' => 'Y');
94 $form->{jsscript} = 1;
95 $form->{title} = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
96 $form->{fokus} = 'Form.name';
99 print $form->parse_html_template('ct/search');
101 $lxdebug->leave_sub();
105 $lxdebug->enter_sub();
107 $auth->assert('customer_vendor_edit');
109 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
111 report_generator_set_default_sort('name', 1);
113 CT->search(\%myconfig, \%$form);
115 my $cvar_configs = CVar->get_configs('module' => 'CT');
118 if ($form->{status} eq 'all') {
119 push @options, $locale->text('All');
120 } elsif ($form->{status} eq 'orphaned') {
121 push @options, $locale->text('Orphaned');
124 push @options, $locale->text('Name') . " : $form->{name}" if $form->{name};
125 push @options, $locale->text('Contact') . " : $form->{contact}" if $form->{contact};
126 push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}| if $form->{"$form->{db}number"};
127 push @options, $locale->text('E-mail') . " : $form->{email}" if $form->{email};
130 'id', 'name', "$form->{db}number", 'address', 'contact', 'phone',
131 'fax', 'email', 'taxnumber', 'sic_code', 'business', 'invnumber',
132 'ordnumber', 'quonumber'
135 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
136 my @searchable_custom_variables = grep { $_->{searchable} } @{ $cvar_configs };
137 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
139 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
142 'id' => { 'text' => $locale->text('ID'), },
143 "$form->{db}number" => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Customer Number') : $locale->text('Vendor Number'), },
144 'name' => { 'text' => $locale->text('Name'), },
145 'address' => { 'text' => $locale->text('Address'), },
146 'contact' => { 'text' => $locale->text('Contact'), },
147 'phone' => { 'text' => $locale->text('Phone'), },
148 'fax' => { 'text' => $locale->text('Fax'), },
149 'email' => { 'text' => $locale->text('E-mail'), },
150 'cc' => { 'text' => $locale->text('Cc'), },
151 'taxnumber' => { 'text' => $locale->text('Tax Number'), },
152 'sic_code' => { 'text' => $locale->text('SIC'), },
153 'business' => { 'text' => $locale->text('Type of Business'), },
154 'invnumber' => { 'text' => $locale->text('Invoice'), },
155 'ordnumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
156 'quonumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation') : $locale->text('Request for Quotation'), },
160 map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
162 my @hidden_variables = (qw(db status obsolete name contact email cp_name addr_city), "$form->{db}number", @searchable_custom_variables, map { "l_$_" } @columns);
163 my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
164 my $callback = build_std_url('action=list_names', grep { $form->{$_} } @hidden_nondefault);
165 $form->{callback} = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
168 my $sortdir = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
169 $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
172 my ($ordertype, $quotationtype, $attachment_basename);
173 if ($form->{IS_CUSTOMER}) {
174 $form->{title} = $locale->text('Customers');
175 $ordertype = 'sales_order';
176 $quotationtype = 'sales_quotation';
177 $attachment_basename = $locale->text('customer_list');
180 $form->{title} = $locale->text('Vendors');
181 $ordertype = 'purchase_order';
182 $quotationtype = 'request_quotation';
183 $attachment_basename = $locale->text('vendor_list');
186 my $report = SL::ReportGenerator->new(\%myconfig, $form);
188 $report->set_options('top_info_text' => join("\n", @options),
189 'raw_bottom_info_text' => $form->parse_html_template('ct/list_names_bottom'),
190 'output_format' => 'HTML',
191 'title' => $form->{title},
192 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
194 $report->set_options_from_form();
196 $report->set_columns(%column_defs);
197 $report->set_column_order(@columns);
199 $report->set_export_options('list_names', @hidden_variables);
201 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
203 CVar->add_custom_variables_to_report('module' => 'CT',
204 'trans_id_field' => 'id',
205 'configs' => $cvar_configs,
206 'column_defs' => \%column_defs,
207 'data' => $form->{CT});
211 foreach my $ref (@{ $form->{CT} }) {
212 my $row = { map { $_ => { 'data' => '' } } @columns };
214 if ($ref->{id} ne $previous_id) {
215 $previous_id = $ref->{id};
216 map { $row->{$_}->{data} = $ref->{$_} } @columns;
218 $row->{name}->{link} = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
219 $row->{email}->{link} = 'mailto:' . E($ref->{email});
222 my $base_url = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
223 $row->{invnumber}->{link} = $base_url;
224 $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
225 $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
226 my $column = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
227 $row->{$column}->{data} = $ref->{$column};
229 $report->add_data($row);
232 $report->generate_with_headers();
234 $lxdebug->leave_sub();
238 $lxdebug->enter_sub();
240 $auth->assert('customer_vendor_edit');
242 # show history button
243 $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
244 #/show hhistory button
246 CT->get_tuple(\%myconfig, \%$form);
247 CT->populate_drop_down_boxes(\%myconfig, \%$form);
249 $form->{title} = "Edit";
252 $form->{discount} *= 100;
257 $lxdebug->leave_sub();
261 $lxdebug->enter_sub();
263 $auth->assert('customer_vendor_edit');
265 $form->get_lists("employees" => "ALL_EMPLOYEES",
266 "taxzones" => "ALL_TAXZONES");
267 $form->get_pricegroup(\%myconfig, { all => 1 });
269 $form->{ALL_SALESMEN} = $form->{ALL_EMPLOYEES};
270 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
271 $form->{is_admin} = $myconfig{role} eq 'admin';
272 $form->{is_customer} = $form->{db} eq 'customer';
273 $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
274 $form->{shipto_label} = sub { my $s = shift(@_); join('; ', grep { $_ } map { $s->{"shipto$_"} } qw(name department_1 street city)) || ' ' };
275 $form->{contacts_label} = sub { join ", ", grep { $_ } $_[0]->{cp_name}, $_[0]->{cp_givenname} };
276 $form->{taxzone_id} = 0 if !$form->{id};
277 $form->{jsscript} = 1;
278 $form->{fokus} = "ct.greeting";
279 $form->{AJAX} = [ new CGI::Ajax( map {; "get_$_" => "$form->{script}?action=get_$_" } qw(shipto contact delivery) ) ];
281 unshift @{ $form->{SHIPTO} }, +{ shipto_id => '0', shiptoname => '' }, +{ shipto_id => '0', shiptoname => 'Alle' };
282 unshift @{ $form->{CONTACTS} }, +{ cp_id => '0', cp_name => $locale->text('New contact') };
284 $form->{title} = $form->{title_save}
285 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
287 CT->query_titles_and_greetings(\%myconfig, \%$form);
288 map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(TITLES GREETINGS COMPANY_GREETINGS DEPARTMENT);
290 $form->{NOTES} ||= [ ];
292 $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'CT', 'trans_id' => $form->{id});
294 CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
297 print $form->parse_html_template('ct/form_header');
299 $lxdebug->leave_sub();
303 $lxdebug->enter_sub();
305 $auth->assert('customer_vendor_edit');
307 print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
308 is_customer => $form->{db} eq 'customer' });
309 $lxdebug->leave_sub();
312 sub add_transaction {
313 $lxdebug->enter_sub();
315 $auth->assert('customer_vendor_edit & ' .
316 '(general_ledger | invoice_edit | vendor_invoice_edit | ' .
317 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit)');
319 # # saving the history
320 # if(!exists $form->{addition}) {
321 # $form->{addition} = "ADD TRANSACTION";
322 # $form->save_history($form->dbconnect(\%myconfig));
324 # # /saving the history
326 $form->isblank("name", $locale->text("Name missing!"));
327 if ($form->{"db"} eq "customer") {
328 CT->save_customer(\%myconfig, \%$form);
330 CT->save_vendor(\%myconfig, \%$form);
333 $form->{callback} = $form->escape($form->{callback}, 1);
334 $name = $form->escape("$form->{name}", 1);
337 "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
340 $lxdebug->leave_sub();
343 sub save_and_ap_transaction {
344 $lxdebug->enter_sub();
346 $auth->assert('customer_vendor_edit & general_ledger');
348 $form->{script} = "ap.pl";
350 if(!exists $form->{addition}) {
351 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
352 $form->{addition} = "SAVED";
353 $form->save_history($form->dbconnect(\%myconfig));
355 # /saving the history
357 $lxdebug->leave_sub();
360 sub save_and_ar_transaction {
361 $lxdebug->enter_sub();
363 $auth->assert('customer_vendor_edit & general_ledger');
365 $form->{script} = "ar.pl";
367 if(!exists $form->{addition}) {
368 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
369 $form->{addition} = "SAVED";
370 $form->save_history($form->dbconnect(\%myconfig));
372 # /saving the history
374 $lxdebug->leave_sub();
377 sub save_and_invoice {
378 $lxdebug->enter_sub();
380 if ($form->{db} eq 'customer') {
381 $auth->assert('customer_vendor_edit & invoice_edit');
383 $auth->assert('customer_vendor_edit & vendor_invoice_edit');
386 $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
387 $form->{type} = "invoice";
389 if(!exists $form->{addition}) {
390 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
391 $form->{addition} = "SAVED";
392 $form->save_history($form->dbconnect(\%myconfig));
394 # /saving the history
396 $lxdebug->leave_sub();
400 $lxdebug->enter_sub();
402 $auth->assert('customer_vendor_edit & request_quotation_edit');
404 $form->{script} = "oe.pl";
405 $form->{type} = "request_quotation";
407 if(!exists $form->{addition}) {
408 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
409 $form->{addition} = "SAVED";
410 $form->save_history($form->dbconnect(\%myconfig));
412 # /saving the history
414 $lxdebug->leave_sub();
417 sub save_and_quotation {
418 $lxdebug->enter_sub();
420 $auth->assert('customer_vendor_edit & sales_quotation_edit');
422 $form->{script} = "oe.pl";
423 $form->{type} = "sales_quotation";
425 if(!exists $form->{addition}) {
426 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
427 $form->{addition} = "SAVED";
428 $form->save_history($form->dbconnect(\%myconfig));
430 # /saving the history
432 $lxdebug->leave_sub();
436 $lxdebug->enter_sub();
438 if ($form->{db} eq 'customer') {
439 $auth->assert('customer_vendor_edit & sales_order_edit');
441 $auth->assert('customer_vendor_edit & purchase_order_edit');
444 $form->{script} = "oe.pl";
446 ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
448 if(!exists $form->{addition}) {
449 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
450 $form->{addition} = "SAVED";
451 $form->save_history($form->dbconnect(\%myconfig));
453 # /saving the history
455 $lxdebug->leave_sub();
459 $lxdebug->enter_sub();
461 $auth->assert('customer_vendor_edit');
463 $msg = ucfirst $form->{db};
466 $form->isblank("name", $locale->text("Name missing!"));
467 if ($form->{"db"} eq "customer") {
468 $rc = CT->save_customer(\%myconfig, \%$form);
470 $rc = CT->save_vendor(\%myconfig, \%$form);
473 $form->error($locale->text('customernumber not unique!'));
476 if(!exists $form->{addition}) {
477 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
478 $form->{addition} = "SAVED";
479 $form->save_history($form->dbconnect(\%myconfig));
481 # /saving the history
482 $form->redirect($locale->text($msg));
484 $lxdebug->leave_sub();
488 $lxdebug->enter_sub();
490 $auth->assert('customer_vendor_edit');
492 $msg = ucfirst $form->{db};
495 $form->isblank("name", $locale->text("Name missing!"));
498 if ($form->{"db"} eq "customer") {
499 $res = CT->save_customer(\%myconfig, \%$form);
501 $res = CT->save_vendor(\%myconfig, \%$form);
505 if ($form->{"db"} eq "customer") {
506 $form->error($locale->text('This customer number is already in use.'));
508 $form->error($locale->text('This vendor number is already in use.'));
512 if(!exists $form->{addition}) {
513 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
514 $form->{addition} = "SAVED";
515 $form->save_history($form->dbconnect(\%myconfig));
517 # /saving the history
520 $lxdebug->leave_sub();
524 $lxdebug->enter_sub();
526 $auth->assert('customer_vendor_edit');
528 CT->delete(\%myconfig, \%$form);
530 $msg = ucfirst $form->{db};
533 if(!exists $form->{addition}) {
534 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
535 $form->{addition} = "DELETED";
536 $form->save_history($form->dbconnect(\%myconfig));
538 # /saving the history
539 $form->redirect($locale->text($msg));
541 $msg = "Cannot delete $form->{db}";
542 $form->error($locale->text($msg));
544 $lxdebug->leave_sub();
548 $lxdebug->enter_sub();
550 $auth->assert('customer_vendor_edit');
555 $lxdebug->leave_sub();
559 $lxdebug->enter_sub();
561 $auth->assert('customer_vendor_edit');
564 $lxdebug->leave_sub();
568 $lxdebug->enter_sub();
570 $auth->assert('customer_vendor_edit');
572 CT->get_contact(\%myconfig, \%$form);
573 print $form->ajax_response_header(), join '__pjx__', map $form->{"cp_$_"},
574 qw(name greeting title givenname phone1 phone2 email abteilung fax mobile1 mobile2 satphone satfax project privatphone privatemail birthday used);
575 $lxdebug->leave_sub();
580 $lxdebug->enter_sub();
582 $auth->assert('customer_vendor_edit');
584 CT->get_shipto(\%myconfig, \%$form);
585 print $form->ajax_response_header(), join '__pjx__', map $form->{"shipto$_"},
586 qw(name department_1 department_2 street zipcode city country contact phone fax email used);
587 $lxdebug->leave_sub();
592 $lxdebug->enter_sub();
594 $auth->assert('customer_vendor_edit');
596 CT->get_delivery(\%myconfig, \%$form );
598 print $form->ajax_response_header(), $form->parse_html_template('ct/get_delivery');
600 $lxdebug->leave_sub();
604 $main::lxdebug->enter_sub();
606 $auth->assert('customer_vendor_edit');
608 CT->get_shipto(\%myconfig, \%$form);
610 unless ($form->{shiptoused}) {
611 CT->delete_shipto($form->{shipto_id});
612 @$form{ grep /^shipto/, keys %$form } = undef;
617 $main::lxdebug->leave_sub();
621 $main::lxdebug->enter_sub();
623 $auth->assert('customer_vendor_edit');
625 CT->get_contact(\%myconfig, \%$form);
627 unless ($form->{cp_used}) {
628 CT->delete_shipto($form->{cp_id});
629 @$form{ grep /^cp_/, keys %$form } = undef;
634 $main::lxdebug->leave_sub();
637 sub continue { call_sub($form->{nextsub}); }