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";
64 $main::lxdebug->enter_sub();
66 $main::auth->assert('customer_vendor_edit');
68 my $form = $main::form;
69 my %myconfig = %main::myconfig;
71 $form->{title} = "Add";
72 $form->{callback} = "$form->{script}?action=add&db=$form->{db}" unless $form->{callback};
74 CT->populate_drop_down_boxes(\%myconfig, \%$form);
79 $main::lxdebug->leave_sub();
83 $main::lxdebug->enter_sub();
85 $main::auth->assert('customer_vendor_edit');
87 my $form = $main::form;
88 my $locale = $main::locale;
90 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
92 $form->get_lists("business_types" => "ALL_BUSINESS_TYPES");
93 $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
95 $form->{CUSTOM_VARIABLES} = CVar->get_configs('module' => 'CT');
96 ($form->{CUSTOM_VARIABLES_FILTER_CODE},
97 $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $form->{CUSTOM_VARIABLES},
98 'include_prefix' => 'l_',
99 'include_value' => 'Y');
101 $form->{jsscript} = 1;
102 $form->{title} = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
103 $form->{fokus} = 'Form.name';
106 print $form->parse_html_template('ct/search');
108 $main::lxdebug->leave_sub();
112 $main::lxdebug->enter_sub();
114 $main::auth->assert('customer_vendor_edit');
116 my $form = $main::form;
117 my %myconfig = %main::myconfig;
118 my $locale = $main::locale;
120 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
122 report_generator_set_default_sort('name', 1);
124 CT->search(\%myconfig, \%$form);
126 my $cvar_configs = CVar->get_configs('module' => 'CT');
129 if ($form->{status} eq 'all') {
130 push @options, $locale->text('All');
131 } elsif ($form->{status} eq 'orphaned') {
132 push @options, $locale->text('Orphaned');
135 push @options, $locale->text('Name') . " : $form->{name}" if $form->{name};
136 push @options, $locale->text('Contact') . " : $form->{contact}" if $form->{contact};
137 push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}| if $form->{"$form->{db}number"};
138 push @options, $locale->text('E-mail') . " : $form->{email}" if $form->{email};
139 push @options, $locale->text('Contact person (surname)') . " : $form->{cp_name}" if $form->{cp_name};
140 push @options, $locale->text('Billing/shipping address (city)') . " : $form->{addr_city}" if $form->{addr_city};
141 push @options, $locale->text('Billing/shipping address (zipcode)') . " : $form->{zipcode}" if $form->{addr_zipcode};
142 push @options, $locale->text('Billing/shipping address (street)') . " : $form->{street}" if $form->{addr_street};
145 'id', 'name', "$form->{db}number", 'contact', 'phone',
146 'fax', 'email', 'taxnumber', 'street', 'zipcode' , 'city',
147 'business', 'invnumber', 'ordnumber', 'quonumber'
150 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
151 my @searchable_custom_variables = grep { $_->{searchable} } @{ $cvar_configs };
152 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
154 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
157 'id' => { 'text' => $locale->text('ID'), },
158 "$form->{db}number" => { 'text' => $locale->text('Number'), },
159 'name' => { 'text' => $form->{IS_CUSTOMER} ? $::locale->text('Customer Name') : $::locale->text('Vendor Name'), },
160 'contact' => { 'text' => $locale->text('Contact'), },
161 'phone' => { 'text' => $locale->text('Phone'), },
162 'fax' => { 'text' => $locale->text('Fax'), },
163 'email' => { 'text' => $locale->text('E-mail'), },
164 'cc' => { 'text' => $locale->text('Cc'), },
165 'taxnumber' => { 'text' => $locale->text('Tax Number'), },
166 'business' => { 'text' => $locale->text('Type of Business'), },
167 'invnumber' => { 'text' => $locale->text('Invoice'), },
168 'ordnumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
169 'quonumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation') : $locale->text('Request for Quotation'), },
170 'street' => { 'text' => $locale->text('Street'), },
171 'zipcode' => { 'text' => $locale->text('Zipcode'), },
172 'city' => { 'text' => $locale->text('City'), },
176 map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
178 my @hidden_variables = (qw(db status obsolete name contact email cp_name addr_street addr_zipcode addr_city business_id),
179 "$form->{db}number", @searchable_custom_variables, map { "l_$_" } @columns);
180 my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
181 my $callback = build_std_url('action=list_names', grep { $form->{$_} } @hidden_nondefault);
182 $form->{callback} = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
185 my $sortdir = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
186 $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
189 my ($ordertype, $quotationtype, $attachment_basename);
190 if ($form->{IS_CUSTOMER}) {
191 $form->{title} = $locale->text('Customers');
192 $ordertype = 'sales_order';
193 $quotationtype = 'sales_quotation';
194 $attachment_basename = $locale->text('customer_list');
197 $form->{title} = $locale->text('Vendors');
198 $ordertype = 'purchase_order';
199 $quotationtype = 'request_quotation';
200 $attachment_basename = $locale->text('vendor_list');
203 my $report = SL::ReportGenerator->new(\%myconfig, $form);
205 $report->set_options('top_info_text' => join("\n", @options),
206 'raw_bottom_info_text' => $form->parse_html_template('ct/list_names_bottom'),
207 'output_format' => 'HTML',
208 'title' => $form->{title},
209 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
211 $report->set_options_from_form();
212 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
214 $report->set_columns(%column_defs);
215 $report->set_column_order(@columns);
217 $report->set_export_options('list_names', @hidden_variables, qw(sort sortdir));
219 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
221 CVar->add_custom_variables_to_report('module' => 'CT',
222 'trans_id_field' => 'id',
223 'configs' => $cvar_configs,
224 'column_defs' => \%column_defs,
225 'data' => $form->{CT});
229 foreach my $ref (@{ $form->{CT} }) {
230 my $row = { map { $_ => { 'data' => '' } } @columns };
232 if ($ref->{id} ne $previous_id) {
233 $previous_id = $ref->{id};
234 map { $row->{$_}->{data} = $ref->{$_} } @columns;
236 $row->{name}->{link} = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
237 $row->{email}->{link} = 'mailto:' . E($ref->{email});
240 my $base_url = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
241 $row->{invnumber}->{link} = $base_url;
242 $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
243 $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
244 my $column = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
245 $row->{$column}->{data} = $ref->{$column};
247 $report->add_data($row);
250 $report->generate_with_headers();
252 $main::lxdebug->leave_sub();
256 $main::lxdebug->enter_sub();
258 $main::auth->assert('customer_vendor_edit');
260 my $form = $main::form;
261 my %myconfig = %main::myconfig;
263 # show history button
264 $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
265 #/show hhistory button
267 CT->get_tuple(\%myconfig, \%$form);
268 CT->populate_drop_down_boxes(\%myconfig, \%$form);
270 $form->{title} = "Edit";
273 $form->{discount} *= 100;
278 $main::lxdebug->leave_sub();
282 $main::lxdebug->enter_sub();
284 $main::auth->assert('customer_vendor_edit');
286 my $form = $main::form;
287 my %myconfig = %main::myconfig;
288 my $locale = $main::locale;
290 $form->get_lists(employees => "ALL_EMPLOYEES",
291 taxzones => "ALL_TAXZONES");
292 $form->get_pricegroup(\%myconfig, { all => 1 });
294 $form->get_lists(customers => { key => "ALL_SALESMAN_CUSTOMERS", business_is_salesman => 1 }) if $::vertreter;
296 $form->{ALL_SALESMEN} = $form->{ALL_EMPLOYEES};
297 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
298 $form->{is_admin} = $myconfig{role} eq 'admin';
299 $form->{is_customer} = $form->{db} eq 'customer';
300 $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
301 $form->{shipto_label} = sub { my $s = shift(@_); join('; ', grep { $_ } map { $s->{"shipto$_"} } qw(name department_1 street city)) || ' ' };
302 $form->{contacts_label} = sub { join ", ", grep { $_ } $_[0]->{cp_name}, $_[0]->{cp_givenname} };
303 $form->{taxzone_id} = 0 if !$form->{id};
304 $form->{jsscript} = 1;
305 $form->{fokus} = "ct.greeting";
306 $form->{AJAX} = [ new CGI::Ajax( map {; "get_$_" => "$form->{script}?action=get_$_" } qw(shipto contact delivery) ) ];
308 unshift @{ $form->{SHIPTO} }, +{ shipto_id => '0', shiptoname => '' }, +{ shipto_id => '0', shiptoname => 'Alle' };
309 unshift @{ $form->{CONTACTS} }, +{ cp_id => '0', cp_name => $locale->text('New contact') };
311 $form->{title} = $form->{title_save}
312 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
314 CT->query_titles_and_greetings(\%myconfig, \%$form);
315 map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(TITLES GREETINGS COMPANY_GREETINGS DEPARTMENT);
317 $form->{NOTES} ||= [ ];
319 $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'CT', 'trans_id' => $form->{id});
321 CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
324 print $form->parse_html_template('ct/form_header');
326 $main::lxdebug->leave_sub();
330 $main::lxdebug->enter_sub();
332 $main::auth->assert('customer_vendor_edit');
334 my $form = $main::form;
336 print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
337 is_customer => $form->{db} eq 'customer' });
338 $main::lxdebug->leave_sub();
342 $main::auth->assert('customer_vendor_edit & ' .
343 '(general_ledger | invoice_edit | vendor_invoice_edit | ' .
344 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit)');
346 $::form->isblank("name", $::locale->text("Name missing!"));
348 if ($::form->{new_salesman_id} && $::vertreter) {
349 $::form->{salesman_id} = $::form->{new_salesman_id};
350 delete $::form->{new_salesman_id};
353 my $res = $::form->{db} eq 'customer' ? CT->save_customer(\%::myconfig, $::form) : CT->save_vendor(\%::myconfig, $::form);
356 if ($::form->{"db"} eq "customer") {
357 $::form->error($::locale->text('This customer number is already in use.'));
359 $::form->error($::locale->text('This vendor number is already in use.'));
364 sub add_transaction {
365 $main::lxdebug->enter_sub();
367 $main::auth->assert('customer_vendor_edit & ' .
368 '(general_ledger | invoice_edit | vendor_invoice_edit | ' .
369 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit)');
371 my $form = $main::form;
372 my %myconfig = %main::myconfig;
373 my $locale = $main::locale;
375 # # saving the history
376 # if(!exists $form->{addition}) {
377 # $form->{addition} = "ADD TRANSACTION";
378 # $form->save_history;
380 # # /saving the history
384 $form->{callback} = $form->escape($form->{callback}, 1);
385 my $name = $form->escape("$form->{name}", 1);
388 "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
391 $main::lxdebug->leave_sub();
394 sub save_and_ap_transaction {
395 $main::lxdebug->enter_sub();
397 $main::auth->assert('customer_vendor_edit & general_ledger');
399 my $form = $main::form;
400 my %myconfig = %main::myconfig;
402 $form->{script} = "ap.pl";
404 if(!exists $form->{addition}) {
405 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
406 $form->{addition} = "SAVED";
409 # /saving the history
411 $main::lxdebug->leave_sub();
414 sub save_and_ar_transaction {
415 $main::lxdebug->enter_sub();
417 $main::auth->assert('customer_vendor_edit & general_ledger');
419 my $form = $main::form;
420 my %myconfig = %main::myconfig;
422 $form->{script} = "ar.pl";
424 if(!exists $form->{addition}) {
425 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
426 $form->{addition} = "SAVED";
429 # /saving the history
431 $main::lxdebug->leave_sub();
434 sub save_and_invoice {
435 $main::lxdebug->enter_sub();
437 my $form = $main::form;
438 my %myconfig = %main::myconfig;
440 if ($form->{db} eq 'customer') {
441 $main::auth->assert('customer_vendor_edit & invoice_edit');
443 $main::auth->assert('customer_vendor_edit & vendor_invoice_edit');
446 $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
447 $form->{type} = "invoice";
449 if(!exists $form->{addition}) {
450 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
451 $form->{addition} = "SAVED";
454 # /saving the history
456 $main::lxdebug->leave_sub();
460 $main::lxdebug->enter_sub();
462 $main::auth->assert('customer_vendor_edit & request_quotation_edit');
464 my $form = $main::form;
465 my %myconfig = %main::myconfig;
467 $form->{script} = "oe.pl";
468 $form->{type} = "request_quotation";
470 if(!exists $form->{addition}) {
471 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
472 $form->{addition} = "SAVED";
475 # /saving the history
477 $main::lxdebug->leave_sub();
480 sub save_and_quotation {
481 $main::lxdebug->enter_sub();
483 $main::auth->assert('customer_vendor_edit & sales_quotation_edit');
485 my $form = $main::form;
486 my %myconfig = %main::myconfig;
488 $form->{script} = "oe.pl";
489 $form->{type} = "sales_quotation";
491 if(!exists $form->{addition}) {
492 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
493 $form->{addition} = "SAVED";
496 # /saving the history
498 $main::lxdebug->leave_sub();
502 $main::lxdebug->enter_sub();
504 my $form = $main::form;
505 my %myconfig = %main::myconfig;
507 if ($form->{db} eq 'customer') {
508 $main::auth->assert('customer_vendor_edit & sales_order_edit');
510 $main::auth->assert('customer_vendor_edit & purchase_order_edit');
513 $form->{script} = "oe.pl";
515 ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
517 if(!exists $form->{addition}) {
518 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
519 $form->{addition} = "SAVED";
522 # /saving the history
524 $main::lxdebug->leave_sub();
528 $main::lxdebug->enter_sub();
530 $main::auth->assert('customer_vendor_edit');
532 my $form = $main::form;
533 my %myconfig = %main::myconfig;
534 my $locale = $main::locale;
536 my $msg = ucfirst $form->{db};
542 if(!exists $form->{addition}) {
543 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
544 $form->{addition} = "SAVED";
547 # /saving the history
548 $form->redirect($locale->text($msg));
550 $main::lxdebug->leave_sub();
554 $main::lxdebug->enter_sub();
556 $main::auth->assert('customer_vendor_edit');
558 my $form = $main::form;
559 my %myconfig = %main::myconfig;
560 my $locale = $main::locale;
562 my $msg = ucfirst $form->{db};
568 if(!exists $form->{addition}) {
569 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
570 $form->{addition} = "SAVED";
573 # /saving the history
576 $main::lxdebug->leave_sub();
581 $main::lxdebug->enter_sub();
583 $main::auth->assert('customer_vendor_edit');
585 my $form = $main::form;
586 my %myconfig = %main::myconfig;
587 my $locale = $main::locale;
589 CT->delete(\%myconfig, \%$form);
591 my $msg = ucfirst $form->{db};
594 if(!exists $form->{addition}) {
595 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
596 $form->{addition} = "DELETED";
599 # /saving the history
600 $form->redirect($locale->text($msg));
602 $main::lxdebug->leave_sub();
606 $main::lxdebug->enter_sub();
608 $main::auth->assert('customer_vendor_edit');
610 my $form = $main::form;
615 $main::lxdebug->leave_sub();
619 $main::lxdebug->enter_sub();
621 $main::auth->assert('customer_vendor_edit');
623 my $form = $main::form;
626 $main::lxdebug->leave_sub();
630 $main::lxdebug->enter_sub();
632 $main::auth->assert('customer_vendor_edit');
634 my $form = $main::form;
635 my %myconfig = %main::myconfig;
637 CT->get_contact(\%myconfig, \%$form);
638 print $form->ajax_response_header(), join '__pjx__', map $form->{"cp_$_"},
639 qw(name title givenname phone1 phone2 email abteilung fax mobile1 mobile2 satphone satfax project privatphone privatemail birthday used gender);
640 $main::lxdebug->leave_sub();
645 $main::lxdebug->enter_sub();
647 $main::auth->assert('customer_vendor_edit');
649 my $form = $main::form;
650 my %myconfig = %main::myconfig;
652 CT->get_shipto(\%myconfig, \%$form);
653 print $form->ajax_response_header(), join('__pjx__', map($form->{"shipto$_"}, qw(name department_1 department_2 street zipcode city country contact phone fax email used)));
654 $main::lxdebug->leave_sub();
659 $::lxdebug->enter_sub;
661 $::auth->assert('customer_vendor_edit');
662 $::auth->assert('sales_all_edit');
664 CT->get_delivery(\%::myconfig, $::form );
666 print $::form->ajax_response_header,
667 $::form->parse_html_template('ct/get_delivery', {
668 is_customer => $::form->{db} eq 'customer',
671 $::lxdebug->leave_sub;
675 $main::lxdebug->enter_sub();
677 $main::auth->assert('customer_vendor_edit');
679 my $form = $main::form;
680 my %myconfig = %main::myconfig;
682 CT->get_shipto(\%myconfig, \%$form);
684 unless ($form->{shiptoused}) {
685 CT->delete_shipto($form->{shipto_id});
686 @$form{ grep /^shipto/, keys %$form } = undef;
691 $main::lxdebug->leave_sub();
695 $main::lxdebug->enter_sub();
697 $main::auth->assert('customer_vendor_edit');
699 my $form = $main::form;
700 my %myconfig = %main::myconfig;
702 CT->get_contact(\%myconfig, \%$form);
704 unless ($form->{cp_used}) {
705 CT->delete_shipto($form->{cp_id});
706 @$form{ grep /^cp_/, keys %$form } = undef;
711 $main::lxdebug->leave_sub();
714 sub ajax_autocomplete {
715 $main::lxdebug->enter_sub();
717 my $form = $main::form;
718 my %myconfig = %main::myconfig;
720 $form->{column} = 'name' unless $form->{column} =~ /^name$/;
721 $form->{vc} = 'customer' unless $form->{vc} =~ /^customer|vendor$/;
722 $form->{db} = $form->{vc}; # CT expects this
723 $form->{$form->{column}} = $form->{q} || '';
724 $form->{limit} = ($form->{limit} * 1) || 10;
725 $form->{searchitems} ||= '';
727 CT->search(\%myconfig, $form);
729 print $form->ajax_response_header(),
730 $form->parse_html_template('ct/ajax_autocomplete');
732 $main::lxdebug->leave_sub();
735 sub continue { call_sub($main::form->{nextsub}); }