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);
54 use SL::ReportGenerator;
56 require "bin/mozilla/common.pl";
57 require "bin/mozilla/reportgenerator.pl";
65 $main::lxdebug->enter_sub();
67 $main::auth->assert('customer_vendor_edit');
69 my $form = $main::form;
70 my %myconfig = %main::myconfig;
72 $form->{title} = "Add";
73 $form->{callback} = "$form->{script}?action=add&db=$form->{db}" unless $form->{callback};
75 CT->populate_drop_down_boxes(\%myconfig, \%$form);
80 $main::lxdebug->leave_sub();
84 $main::lxdebug->enter_sub();
86 $main::auth->assert('customer_vendor_edit');
88 my $form = $main::form;
89 my $locale = $main::locale;
91 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
93 $form->get_lists("business_types" => "ALL_BUSINESS_TYPES");
94 $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
96 $form->{CUSTOM_VARIABLES} = CVar->get_configs('module' => 'CT');
97 ($form->{CUSTOM_VARIABLES_FILTER_CODE},
98 $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $form->{CUSTOM_VARIABLES},
99 'include_prefix' => 'l_',
100 'include_value' => 'Y');
102 $form->{jsscript} = 1;
103 $form->{title} = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
104 $form->{fokus} = 'Form.name';
107 print $form->parse_html_template('ct/search');
109 $main::lxdebug->leave_sub();
113 $main::lxdebug->enter_sub();
115 $main::auth->assert('customer_vendor_edit');
117 my $form = $main::form;
118 my %myconfig = %main::myconfig;
119 my $locale = $main::locale;
121 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
123 report_generator_set_default_sort('name', 1);
125 CT->search(\%myconfig, \%$form);
127 my $cvar_configs = CVar->get_configs('module' => 'CT');
130 if ($form->{status} eq 'all') {
131 push @options, $locale->text('All');
132 } elsif ($form->{status} eq 'orphaned') {
133 push @options, $locale->text('Orphaned');
136 push @options, $locale->text('Name') . " : $form->{name}" if $form->{name};
137 push @options, $locale->text('Contact') . " : $form->{contact}" if $form->{contact};
138 push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}| if $form->{"$form->{db}number"};
139 push @options, $locale->text('E-mail') . " : $form->{email}" if $form->{email};
140 push @options, $locale->text('Contact person (surname)') . " : $form->{cp_name}" if $form->{cp_name};
141 push @options, $locale->text('Billing/shipping address (city)') . " : $form->{addr_city}" if $form->{addr_city};
142 push @options, $locale->text('Billing/shipping address (zipcode)') . " : $form->{zipcode}" if $form->{addr_zipcode};
143 push @options, $locale->text('Billing/shipping address (street)') . " : $form->{street}" if $form->{addr_street};
145 if ($form->{business_id}) {
146 my $business = SL::DB::Manager::Business->find_by(id => $form->{business_id});
148 my $label = $form->{IS_CUSTOMER} ? $::locale->text('Customer type') : $::locale->text('Vendor type');
149 push @options, $label . " : " . $business->description;
154 'id', 'name', "$form->{db}number", 'contact', 'phone',
155 'fax', 'email', 'taxnumber', 'street', 'zipcode' , 'city',
156 'business', 'invnumber', 'ordnumber', 'quonumber'
159 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
160 my @searchable_custom_variables = grep { $_->{searchable} } @{ $cvar_configs };
161 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
163 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
166 'id' => { 'text' => $locale->text('ID'), },
167 "$form->{db}number" => { 'text' => $locale->text('Number'), },
168 'name' => { 'text' => $form->{IS_CUSTOMER} ? $::locale->text('Customer Name') : $::locale->text('Vendor Name'), },
169 'contact' => { 'text' => $locale->text('Contact'), },
170 'phone' => { 'text' => $locale->text('Phone'), },
171 'fax' => { 'text' => $locale->text('Fax'), },
172 'email' => { 'text' => $locale->text('E-mail'), },
173 'cc' => { 'text' => $locale->text('Cc'), },
174 'taxnumber' => { 'text' => $locale->text('Tax Number'), },
175 'business' => { 'text' => $locale->text('Type of Business'), },
176 'invnumber' => { 'text' => $locale->text('Invoice'), },
177 'ordnumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
178 'quonumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation') : $locale->text('Request for Quotation'), },
179 'street' => { 'text' => $locale->text('Street'), },
180 'zipcode' => { 'text' => $locale->text('Zipcode'), },
181 'city' => { 'text' => $locale->text('City'), },
185 map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
187 my @hidden_variables = (qw(db status obsolete name contact email cp_name addr_street addr_zipcode addr_city business_id),
188 "$form->{db}number", @searchable_custom_variables, map { "l_$_" } @columns);
189 my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
190 my $callback = build_std_url('action=list_names', grep { $form->{$_} } @hidden_nondefault);
191 $form->{callback} = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
194 my $sortdir = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
195 $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
198 my ($ordertype, $quotationtype, $attachment_basename);
199 if ($form->{IS_CUSTOMER}) {
200 $form->{title} = $locale->text('Customers');
201 $ordertype = 'sales_order';
202 $quotationtype = 'sales_quotation';
203 $attachment_basename = $locale->text('customer_list');
206 $form->{title} = $locale->text('Vendors');
207 $ordertype = 'purchase_order';
208 $quotationtype = 'request_quotation';
209 $attachment_basename = $locale->text('vendor_list');
212 my $report = SL::ReportGenerator->new(\%myconfig, $form);
214 $report->set_options('top_info_text' => join("\n", @options),
215 'raw_bottom_info_text' => $form->parse_html_template('ct/list_names_bottom'),
216 'output_format' => 'HTML',
217 'title' => $form->{title},
218 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
220 $report->set_options_from_form();
221 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
223 $report->set_columns(%column_defs);
224 $report->set_column_order(@columns);
226 $report->set_export_options('list_names', @hidden_variables, qw(sort sortdir));
228 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
230 CVar->add_custom_variables_to_report('module' => 'CT',
231 'trans_id_field' => 'id',
232 'configs' => $cvar_configs,
233 'column_defs' => \%column_defs,
234 'data' => $form->{CT});
238 foreach my $ref (@{ $form->{CT} }) {
239 my $row = { map { $_ => { 'data' => '' } } @columns };
241 if ($ref->{id} ne $previous_id) {
242 $previous_id = $ref->{id};
243 map { $row->{$_}->{data} = $ref->{$_} } @columns;
245 $row->{name}->{link} = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
246 $row->{email}->{link} = 'mailto:' . E($ref->{email});
249 my $base_url = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
250 $row->{invnumber}->{link} = $base_url;
251 $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
252 $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
253 my $column = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
254 $row->{$column}->{data} = $ref->{$column};
256 $report->add_data($row);
259 $report->generate_with_headers();
261 $main::lxdebug->leave_sub();
265 $main::lxdebug->enter_sub();
267 $main::auth->assert('customer_vendor_edit');
269 my $form = $main::form;
270 my %myconfig = %main::myconfig;
272 # show history button
273 $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
274 #/show hhistory button
276 CT->get_tuple(\%myconfig, \%$form);
277 CT->populate_drop_down_boxes(\%myconfig, \%$form);
279 $form->{title} = "Edit";
282 $form->{discount} *= 100;
287 $main::lxdebug->leave_sub();
291 $main::lxdebug->enter_sub();
293 $main::auth->assert('customer_vendor_edit');
295 my $form = $main::form;
296 my %myconfig = %main::myconfig;
297 my $locale = $main::locale;
299 $form->get_lists(employees => "ALL_EMPLOYEES",
300 taxzones => "ALL_TAXZONES");
301 $form->get_pricegroup(\%myconfig, { all => 1 });
303 $form->get_lists(customers => { key => "ALL_SALESMAN_CUSTOMERS", business_is_salesman => 1 }) if $::vertreter;
305 $form->{ALL_SALESMEN} = $form->{ALL_EMPLOYEES};
306 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
307 $form->{is_admin} = $myconfig{role} eq 'admin';
308 $form->{is_customer} = $form->{db} eq 'customer';
309 $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
310 $form->{shipto_label} = sub { my $s = shift(@_); join('; ', grep { $_ } map { $s->{"shipto$_"} } qw(name department_1 street city)) || ' ' };
311 $form->{contacts_label} = sub { join ", ", grep { $_ } $_[0]->{cp_name}, $_[0]->{cp_givenname} };
312 $form->{taxzone_id} = 0 if !$form->{id};
313 $form->{jsscript} = 1;
314 $form->{fokus} = "ct.greeting";
315 $form->{AJAX} = [ new CGI::Ajax( map {; "get_$_" => "$form->{script}?action=get_$_" } qw(shipto contact delivery) ) ];
317 unshift @{ $form->{SHIPTO} }, +{ shipto_id => '0', shiptoname => '' }, +{ shipto_id => '0', shiptoname => 'Alle' };
318 unshift @{ $form->{CONTACTS} }, +{ cp_id => '0', cp_name => $locale->text('New contact') };
320 $form->{title} = $form->{title_save}
321 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
323 CT->query_titles_and_greetings(\%myconfig, \%$form);
324 map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(TITLES GREETINGS COMPANY_GREETINGS DEPARTMENT);
326 $form->{NOTES} ||= [ ];
328 $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'CT', 'trans_id' => $form->{id});
330 CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
333 print $form->parse_html_template('ct/form_header');
335 $main::lxdebug->leave_sub();
339 $main::lxdebug->enter_sub();
341 $main::auth->assert('customer_vendor_edit');
343 my $form = $main::form;
345 print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
346 is_customer => $form->{db} eq 'customer' });
347 $main::lxdebug->leave_sub();
351 $main::auth->assert('customer_vendor_edit & ' .
352 '(general_ledger | invoice_edit | vendor_invoice_edit | ' .
353 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit)');
355 $::form->isblank("name", $::locale->text("Name missing!"));
357 if ($::form->{new_salesman_id} && $::vertreter) {
358 $::form->{salesman_id} = $::form->{new_salesman_id};
359 delete $::form->{new_salesman_id};
362 my $res = $::form->{db} eq 'customer' ? CT->save_customer(\%::myconfig, $::form) : CT->save_vendor(\%::myconfig, $::form);
365 if ($::form->{"db"} eq "customer") {
366 $::form->error($::locale->text('This customer number is already in use.'));
368 $::form->error($::locale->text('This vendor number is already in use.'));
373 sub add_transaction {
374 $main::lxdebug->enter_sub();
376 $main::auth->assert('customer_vendor_edit & ' .
377 '(general_ledger | invoice_edit | vendor_invoice_edit | ' .
378 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit)');
380 my $form = $main::form;
381 my %myconfig = %main::myconfig;
382 my $locale = $main::locale;
384 # # saving the history
385 # if(!exists $form->{addition}) {
386 # $form->{addition} = "ADD TRANSACTION";
387 # $form->save_history;
389 # # /saving the history
393 $form->{callback} = $form->escape($form->{callback}, 1);
394 my $name = $form->escape("$form->{name}", 1);
397 "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
400 $main::lxdebug->leave_sub();
403 sub save_and_ap_transaction {
404 $main::lxdebug->enter_sub();
406 $main::auth->assert('customer_vendor_edit & general_ledger');
408 my $form = $main::form;
409 my %myconfig = %main::myconfig;
411 $form->{script} = "ap.pl";
413 if(!exists $form->{addition}) {
414 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
415 $form->{addition} = "SAVED";
418 # /saving the history
420 $main::lxdebug->leave_sub();
423 sub save_and_ar_transaction {
424 $main::lxdebug->enter_sub();
426 $main::auth->assert('customer_vendor_edit & general_ledger');
428 my $form = $main::form;
429 my %myconfig = %main::myconfig;
431 $form->{script} = "ar.pl";
433 if(!exists $form->{addition}) {
434 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
435 $form->{addition} = "SAVED";
438 # /saving the history
440 $main::lxdebug->leave_sub();
443 sub save_and_invoice {
444 $main::lxdebug->enter_sub();
446 my $form = $main::form;
447 my %myconfig = %main::myconfig;
449 if ($form->{db} eq 'customer') {
450 $main::auth->assert('customer_vendor_edit & invoice_edit');
452 $main::auth->assert('customer_vendor_edit & vendor_invoice_edit');
455 $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
456 $form->{type} = "invoice";
458 if(!exists $form->{addition}) {
459 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
460 $form->{addition} = "SAVED";
463 # /saving the history
465 $main::lxdebug->leave_sub();
469 $main::lxdebug->enter_sub();
471 $main::auth->assert('customer_vendor_edit & request_quotation_edit');
473 my $form = $main::form;
474 my %myconfig = %main::myconfig;
476 $form->{script} = "oe.pl";
477 $form->{type} = "request_quotation";
479 if(!exists $form->{addition}) {
480 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
481 $form->{addition} = "SAVED";
484 # /saving the history
486 $main::lxdebug->leave_sub();
489 sub save_and_quotation {
490 $main::lxdebug->enter_sub();
492 $main::auth->assert('customer_vendor_edit & sales_quotation_edit');
494 my $form = $main::form;
495 my %myconfig = %main::myconfig;
497 $form->{script} = "oe.pl";
498 $form->{type} = "sales_quotation";
500 if(!exists $form->{addition}) {
501 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
502 $form->{addition} = "SAVED";
505 # /saving the history
507 $main::lxdebug->leave_sub();
511 $main::lxdebug->enter_sub();
513 my $form = $main::form;
514 my %myconfig = %main::myconfig;
516 if ($form->{db} eq 'customer') {
517 $main::auth->assert('customer_vendor_edit & sales_order_edit');
519 $main::auth->assert('customer_vendor_edit & purchase_order_edit');
522 $form->{script} = "oe.pl";
524 ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
526 if(!exists $form->{addition}) {
527 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
528 $form->{addition} = "SAVED";
531 # /saving the history
533 $main::lxdebug->leave_sub();
537 $main::lxdebug->enter_sub();
539 $main::auth->assert('customer_vendor_edit');
541 my $form = $main::form;
542 my %myconfig = %main::myconfig;
543 my $locale = $main::locale;
545 my $msg = ucfirst $form->{db};
551 if(!exists $form->{addition}) {
552 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
553 $form->{addition} = "SAVED";
556 # /saving the history
557 $form->redirect($locale->text($msg));
559 $main::lxdebug->leave_sub();
563 $main::lxdebug->enter_sub();
565 $main::auth->assert('customer_vendor_edit');
567 my $form = $main::form;
568 my %myconfig = %main::myconfig;
569 my $locale = $main::locale;
571 my $msg = ucfirst $form->{db};
577 if(!exists $form->{addition}) {
578 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
579 $form->{addition} = "SAVED";
582 # /saving the history
585 $main::lxdebug->leave_sub();
590 $main::lxdebug->enter_sub();
592 $main::auth->assert('customer_vendor_edit');
594 my $form = $main::form;
595 my %myconfig = %main::myconfig;
596 my $locale = $main::locale;
598 CT->delete(\%myconfig, \%$form);
600 my $msg = ucfirst $form->{db};
603 if(!exists $form->{addition}) {
604 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
605 $form->{addition} = "DELETED";
608 # /saving the history
609 $form->redirect($locale->text($msg));
611 $main::lxdebug->leave_sub();
615 $main::lxdebug->enter_sub();
617 $main::auth->assert('customer_vendor_edit');
619 my $form = $main::form;
624 $main::lxdebug->leave_sub();
628 $main::lxdebug->enter_sub();
630 $main::auth->assert('customer_vendor_edit');
632 my $form = $main::form;
635 $main::lxdebug->leave_sub();
639 $main::lxdebug->enter_sub();
641 $main::auth->assert('customer_vendor_edit');
643 my $form = $main::form;
644 my %myconfig = %main::myconfig;
646 CT->get_contact(\%myconfig, \%$form);
647 print $form->ajax_response_header(), join '__pjx__', map $form->{"cp_$_"},
648 qw(name title givenname phone1 phone2 email abteilung fax mobile1 mobile2 satphone satfax project privatphone privatemail birthday used gender);
649 $main::lxdebug->leave_sub();
654 $main::lxdebug->enter_sub();
656 $main::auth->assert('customer_vendor_edit');
658 my $form = $main::form;
659 my %myconfig = %main::myconfig;
661 CT->get_shipto(\%myconfig, \%$form);
662 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)));
663 $main::lxdebug->leave_sub();
668 $::lxdebug->enter_sub;
670 $::auth->assert('customer_vendor_edit');
671 $::auth->assert('sales_all_edit');
673 CT->get_delivery(\%::myconfig, $::form );
675 print $::form->ajax_response_header,
676 $::form->parse_html_template('ct/get_delivery', {
677 is_customer => $::form->{db} eq 'customer',
680 $::lxdebug->leave_sub;
684 $main::lxdebug->enter_sub();
686 $main::auth->assert('customer_vendor_edit');
688 my $form = $main::form;
689 my %myconfig = %main::myconfig;
691 CT->get_shipto(\%myconfig, \%$form);
693 unless ($form->{shiptoused}) {
694 CT->delete_shipto($form->{shipto_id});
695 @$form{ grep /^shipto/, keys %$form } = undef;
700 $main::lxdebug->leave_sub();
704 $main::lxdebug->enter_sub();
706 $main::auth->assert('customer_vendor_edit');
708 my $form = $main::form;
709 my %myconfig = %main::myconfig;
711 CT->get_contact(\%myconfig, \%$form);
713 unless ($form->{cp_used}) {
714 CT->delete_shipto($form->{cp_id});
715 @$form{ grep /^cp_/, keys %$form } = undef;
720 $main::lxdebug->leave_sub();
723 sub ajax_autocomplete {
724 $main::lxdebug->enter_sub();
726 my $form = $main::form;
727 my %myconfig = %main::myconfig;
729 $form->{column} = 'name' unless $form->{column} =~ /^name$/;
730 $form->{vc} = 'customer' unless $form->{vc} =~ /^customer|vendor$/;
731 $form->{db} = $form->{vc}; # CT expects this
732 $form->{$form->{column}} = $form->{q} || '';
733 $form->{limit} = ($form->{limit} * 1) || 10;
734 $form->{searchitems} ||= '';
736 CT->search(\%myconfig, $form);
738 print $form->ajax_response_header(),
739 $form->parse_html_template('ct/ajax_autocomplete');
741 $main::lxdebug->leave_sub();
744 sub continue { call_sub($main::form->{nextsub}); }