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};
128 push @options, $locale->text('Contact person (surname)')
129 . " : $form->{cp_name}" if $form->{cp_name};
130 push @options, $locale->text('Billing/shipping address (city)')
131 . " : $form->{addr_city}" if $form->{addr_city};
133 push @options, $locale->text('Billing/shipping address (zipcode)')
134 . " : $form->{zipcode}" if $form->{addr_zipcode};
135 push @options, $locale->text('Billing/shipping address (street)')
136 . " : $form->{street}" if $form->{addr_street};
139 'id', 'name', "$form->{db}number", 'contact', 'phone',
140 'fax', 'email', 'taxnumber', 'street', 'zipcode' , 'city', 'business', 'invnumber',
141 'ordnumber', 'quonumber'
144 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
145 my @searchable_custom_variables = grep { $_->{searchable} } @{ $cvar_configs };
146 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
148 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
151 'id' => { 'text' => $locale->text('ID'), },
152 "$form->{db}number" => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Customer Number') : $locale->text('Vendor Number'), },
153 'name' => { 'text' => $locale->text('Name'), },
154 'contact' => { 'text' => $locale->text('Contact'), },
155 'phone' => { 'text' => $locale->text('Phone'), },
156 'fax' => { 'text' => $locale->text('Fax'), },
157 'email' => { 'text' => $locale->text('E-mail'), },
158 'cc' => { 'text' => $locale->text('Cc'), },
159 'taxnumber' => { 'text' => $locale->text('Tax Number'), },
160 'business' => { 'text' => $locale->text('Type of Business'), },
161 'invnumber' => { 'text' => $locale->text('Invoice'), },
162 'ordnumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
163 'quonumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation') : $locale->text('Request for Quotation'), },
164 'street' => { 'text' => $locale->text('Street'), },
165 'zipcode' => { 'text' => $locale->text('Zipcode'), },
166 'city' => { 'text' => $locale->text('City'), },
170 map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
172 my @hidden_variables = (qw(db status obsolete name contact email cp_name addr_city), "$form->{db}number", @searchable_custom_variables, map { "l_$_" } @columns);
173 my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
174 my $callback = build_std_url('action=list_names', grep { $form->{$_} } @hidden_nondefault);
175 $form->{callback} = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
178 my $sortdir = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
179 $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
182 my ($ordertype, $quotationtype, $attachment_basename);
183 if ($form->{IS_CUSTOMER}) {
184 $form->{title} = $locale->text('Customers');
185 $ordertype = 'sales_order';
186 $quotationtype = 'sales_quotation';
187 $attachment_basename = $locale->text('customer_list');
190 $form->{title} = $locale->text('Vendors');
191 $ordertype = 'purchase_order';
192 $quotationtype = 'request_quotation';
193 $attachment_basename = $locale->text('vendor_list');
196 my $report = SL::ReportGenerator->new(\%myconfig, $form);
198 $report->set_options('top_info_text' => join("\n", @options),
199 'raw_bottom_info_text' => $form->parse_html_template('ct/list_names_bottom'),
200 'output_format' => 'HTML',
201 'title' => $form->{title},
202 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
204 $report->set_options_from_form();
206 $report->set_columns(%column_defs);
207 $report->set_column_order(@columns);
209 $report->set_export_options('list_names', @hidden_variables);
211 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
213 CVar->add_custom_variables_to_report('module' => 'CT',
214 'trans_id_field' => 'id',
215 'configs' => $cvar_configs,
216 'column_defs' => \%column_defs,
217 'data' => $form->{CT});
221 foreach my $ref (@{ $form->{CT} }) {
222 my $row = { map { $_ => { 'data' => '' } } @columns };
224 if ($ref->{id} ne $previous_id) {
225 $previous_id = $ref->{id};
226 map { $row->{$_}->{data} = $ref->{$_} } @columns;
228 $row->{name}->{link} = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
229 $row->{email}->{link} = 'mailto:' . E($ref->{email});
232 my $base_url = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
233 $row->{invnumber}->{link} = $base_url;
234 $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
235 $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
236 my $column = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
237 $row->{$column}->{data} = $ref->{$column};
239 $report->add_data($row);
242 $report->generate_with_headers();
244 $lxdebug->leave_sub();
248 $lxdebug->enter_sub();
250 $auth->assert('customer_vendor_edit');
252 # show history button
253 $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
254 #/show hhistory button
256 CT->get_tuple(\%myconfig, \%$form);
257 CT->populate_drop_down_boxes(\%myconfig, \%$form);
259 $form->{title} = "Edit";
262 $form->{discount} *= 100;
267 $lxdebug->leave_sub();
271 $lxdebug->enter_sub();
273 $auth->assert('customer_vendor_edit');
275 $form->get_lists("employees" => "ALL_EMPLOYEES",
276 "taxzones" => "ALL_TAXZONES");
277 $form->get_pricegroup(\%myconfig, { all => 1 });
279 $form->{ALL_SALESMEN} = $form->{ALL_EMPLOYEES};
280 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
281 $form->{is_admin} = $myconfig{role} eq 'admin';
282 $form->{is_customer} = $form->{db} eq 'customer';
283 $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
284 $form->{shipto_label} = sub { my $s = shift(@_); join('; ', grep { $_ } map { $s->{"shipto$_"} } qw(name department_1 street city)) || ' ' };
285 $form->{contacts_label} = sub { join ", ", grep { $_ } $_[0]->{cp_name}, $_[0]->{cp_givenname} };
286 $form->{taxzone_id} = 0 if !$form->{id};
287 $form->{jsscript} = 1;
288 $form->{fokus} = "ct.greeting";
289 $form->{AJAX} = [ new CGI::Ajax( map {; "get_$_" => "$form->{script}?action=get_$_" } qw(shipto contact delivery) ) ];
291 unshift @{ $form->{SHIPTO} }, +{ shipto_id => '0', shiptoname => '' }, +{ shipto_id => '0', shiptoname => 'Alle' };
292 unshift @{ $form->{CONTACTS} }, +{ cp_id => '0', cp_name => $locale->text('New contact') };
294 $form->{title} = $form->{title_save}
295 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
297 CT->query_titles_and_greetings(\%myconfig, \%$form);
298 map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(TITLES GREETINGS COMPANY_GREETINGS DEPARTMENT);
300 $form->{NOTES} ||= [ ];
302 $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'CT', 'trans_id' => $form->{id});
304 CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
307 print $form->parse_html_template('ct/form_header');
309 $lxdebug->leave_sub();
313 $lxdebug->enter_sub();
315 $auth->assert('customer_vendor_edit');
317 print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
318 is_customer => $form->{db} eq 'customer' });
319 $lxdebug->leave_sub();
322 sub add_transaction {
323 $lxdebug->enter_sub();
325 $auth->assert('customer_vendor_edit & ' .
326 '(general_ledger | invoice_edit | vendor_invoice_edit | ' .
327 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit)');
329 # # saving the history
330 # if(!exists $form->{addition}) {
331 # $form->{addition} = "ADD TRANSACTION";
332 # $form->save_history($form->dbconnect(\%myconfig));
334 # # /saving the history
336 $form->isblank("name", $locale->text("Name missing!"));
337 if ($form->{"db"} eq "customer") {
338 CT->save_customer(\%myconfig, \%$form);
340 CT->save_vendor(\%myconfig, \%$form);
343 $form->{callback} = $form->escape($form->{callback}, 1);
344 $name = $form->escape("$form->{name}", 1);
347 "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
350 $lxdebug->leave_sub();
353 sub save_and_ap_transaction {
354 $lxdebug->enter_sub();
356 $auth->assert('customer_vendor_edit & general_ledger');
358 $form->{script} = "ap.pl";
360 if(!exists $form->{addition}) {
361 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
362 $form->{addition} = "SAVED";
363 $form->save_history($form->dbconnect(\%myconfig));
365 # /saving the history
367 $lxdebug->leave_sub();
370 sub save_and_ar_transaction {
371 $lxdebug->enter_sub();
373 $auth->assert('customer_vendor_edit & general_ledger');
375 $form->{script} = "ar.pl";
377 if(!exists $form->{addition}) {
378 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
379 $form->{addition} = "SAVED";
380 $form->save_history($form->dbconnect(\%myconfig));
382 # /saving the history
384 $lxdebug->leave_sub();
387 sub save_and_invoice {
388 $lxdebug->enter_sub();
390 if ($form->{db} eq 'customer') {
391 $auth->assert('customer_vendor_edit & invoice_edit');
393 $auth->assert('customer_vendor_edit & vendor_invoice_edit');
396 $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
397 $form->{type} = "invoice";
399 if(!exists $form->{addition}) {
400 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
401 $form->{addition} = "SAVED";
402 $form->save_history($form->dbconnect(\%myconfig));
404 # /saving the history
406 $lxdebug->leave_sub();
410 $lxdebug->enter_sub();
412 $auth->assert('customer_vendor_edit & request_quotation_edit');
414 $form->{script} = "oe.pl";
415 $form->{type} = "request_quotation";
417 if(!exists $form->{addition}) {
418 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
419 $form->{addition} = "SAVED";
420 $form->save_history($form->dbconnect(\%myconfig));
422 # /saving the history
424 $lxdebug->leave_sub();
427 sub save_and_quotation {
428 $lxdebug->enter_sub();
430 $auth->assert('customer_vendor_edit & sales_quotation_edit');
432 $form->{script} = "oe.pl";
433 $form->{type} = "sales_quotation";
435 if(!exists $form->{addition}) {
436 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
437 $form->{addition} = "SAVED";
438 $form->save_history($form->dbconnect(\%myconfig));
440 # /saving the history
442 $lxdebug->leave_sub();
446 $lxdebug->enter_sub();
448 if ($form->{db} eq 'customer') {
449 $auth->assert('customer_vendor_edit & sales_order_edit');
451 $auth->assert('customer_vendor_edit & purchase_order_edit');
454 $form->{script} = "oe.pl";
456 ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
458 if(!exists $form->{addition}) {
459 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
460 $form->{addition} = "SAVED";
461 $form->save_history($form->dbconnect(\%myconfig));
463 # /saving the history
465 $lxdebug->leave_sub();
469 $lxdebug->enter_sub();
471 $auth->assert('customer_vendor_edit');
473 $msg = ucfirst $form->{db};
476 $form->isblank("name", $locale->text("Name missing!"));
477 if ($form->{"db"} eq "customer") {
478 $rc = CT->save_customer(\%myconfig, \%$form);
480 $rc = CT->save_vendor(\%myconfig, \%$form);
483 $form->error($locale->text('customernumber not unique!'));
486 if(!exists $form->{addition}) {
487 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
488 $form->{addition} = "SAVED";
489 $form->save_history($form->dbconnect(\%myconfig));
491 # /saving the history
492 $form->redirect($locale->text($msg));
494 $lxdebug->leave_sub();
498 $lxdebug->enter_sub();
500 $auth->assert('customer_vendor_edit');
502 $msg = ucfirst $form->{db};
505 $form->isblank("name", $locale->text("Name missing!"));
508 if ($form->{"db"} eq "customer") {
509 $res = CT->save_customer(\%myconfig, \%$form);
511 $res = CT->save_vendor(\%myconfig, \%$form);
515 if ($form->{"db"} eq "customer") {
516 $form->error($locale->text('This customer number is already in use.'));
518 $form->error($locale->text('This vendor number is already in use.'));
522 if(!exists $form->{addition}) {
523 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
524 $form->{addition} = "SAVED";
525 $form->save_history($form->dbconnect(\%myconfig));
527 # /saving the history
530 $lxdebug->leave_sub();
534 $lxdebug->enter_sub();
536 $auth->assert('customer_vendor_edit');
538 CT->delete(\%myconfig, \%$form);
540 $msg = ucfirst $form->{db};
543 if(!exists $form->{addition}) {
544 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
545 $form->{addition} = "DELETED";
546 $form->save_history($form->dbconnect(\%myconfig));
548 # /saving the history
549 $form->redirect($locale->text($msg));
551 $msg = "Cannot delete $form->{db}";
552 $form->error($locale->text($msg));
554 $lxdebug->leave_sub();
558 $lxdebug->enter_sub();
560 $auth->assert('customer_vendor_edit');
565 $lxdebug->leave_sub();
569 $lxdebug->enter_sub();
571 $auth->assert('customer_vendor_edit');
574 $lxdebug->leave_sub();
578 $lxdebug->enter_sub();
580 $auth->assert('customer_vendor_edit');
582 CT->get_contact(\%myconfig, \%$form);
583 print $form->ajax_response_header(), join '__pjx__', map $form->{"cp_$_"},
584 qw(name greeting title givenname phone1 phone2 email abteilung fax mobile1 mobile2 satphone satfax project privatphone privatemail birthday used);
585 $lxdebug->leave_sub();
590 $lxdebug->enter_sub();
592 $auth->assert('customer_vendor_edit');
594 CT->get_shipto(\%myconfig, \%$form);
595 print $form->ajax_response_header(), join '__pjx__', map $form->{"shipto$_"},
596 qw(name department_1 department_2 street zipcode city country contact phone fax email used);
597 $lxdebug->leave_sub();
602 $lxdebug->enter_sub();
604 $auth->assert('customer_vendor_edit');
606 CT->get_delivery(\%myconfig, \%$form );
608 print $form->ajax_response_header(), $form->parse_html_template('ct/get_delivery');
610 $lxdebug->leave_sub();
614 $main::lxdebug->enter_sub();
616 $auth->assert('customer_vendor_edit');
618 CT->get_shipto(\%myconfig, \%$form);
620 unless ($form->{shiptoused}) {
621 CT->delete_shipto($form->{shipto_id});
622 @$form{ grep /^shipto/, keys %$form } = undef;
627 $main::lxdebug->leave_sub();
631 $main::lxdebug->enter_sub();
633 $auth->assert('customer_vendor_edit');
635 CT->get_contact(\%myconfig, \%$form);
637 unless ($form->{cp_used}) {
638 CT->delete_shipto($form->{cp_id});
639 @$form{ grep /^cp_/, keys %$form } = undef;
644 $main::lxdebug->leave_sub();
647 sub continue { call_sub($form->{nextsub}); }