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);
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(
188 db status obsolete name contact email cp_name addr_street addr_zipcode
189 addr_city business_id
190 ), "$form->{db}number",
191 map({ "cvar_$_->{name}" } @searchable_custom_variables),
192 map({ "l_$_" } @columns),
195 my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
196 my $callback = build_std_url('action=list_names', grep { $form->{$_} } @hidden_nondefault);
197 $form->{callback} = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
200 my $sortdir = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
201 $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
204 my ($ordertype, $quotationtype, $attachment_basename);
205 if ($form->{IS_CUSTOMER}) {
206 $form->{title} = $locale->text('Customers');
207 $ordertype = 'sales_order';
208 $quotationtype = 'sales_quotation';
209 $attachment_basename = $locale->text('customer_list');
212 $form->{title} = $locale->text('Vendors');
213 $ordertype = 'purchase_order';
214 $quotationtype = 'request_quotation';
215 $attachment_basename = $locale->text('vendor_list');
218 my $report = SL::ReportGenerator->new(\%myconfig, $form);
220 $report->set_options('top_info_text' => join("\n", @options),
221 'raw_bottom_info_text' => $form->parse_html_template('ct/list_names_bottom'),
222 'output_format' => 'HTML',
223 'title' => $form->{title},
224 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
226 $report->set_options_from_form();
227 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
229 $report->set_columns(%column_defs);
230 $report->set_column_order(@columns);
232 $report->set_export_options('list_names', @hidden_variables, qw(sort sortdir));
234 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
236 CVar->add_custom_variables_to_report('module' => 'CT',
237 'trans_id_field' => 'id',
238 'configs' => $cvar_configs,
239 'column_defs' => \%column_defs,
240 'data' => $form->{CT});
244 foreach my $ref (@{ $form->{CT} }) {
245 my $row = { map { $_ => { 'data' => '' } } @columns };
247 if ($ref->{id} ne $previous_id) {
248 $previous_id = $ref->{id};
249 map { $row->{$_}->{data} = $ref->{$_} } @columns;
251 $row->{name}->{link} = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
252 $row->{email}->{link} = 'mailto:' . E($ref->{email});
255 my $base_url = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
256 $row->{invnumber}->{link} = $base_url;
257 $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
258 $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
259 my $column = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
260 $row->{$column}->{data} = $ref->{$column};
262 $report->add_data($row);
265 $report->generate_with_headers();
267 $main::lxdebug->leave_sub();
271 $main::lxdebug->enter_sub();
273 $main::auth->assert('customer_vendor_edit');
275 my $form = $main::form;
276 my %myconfig = %main::myconfig;
278 # show history button
279 $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
280 #/show hhistory button
282 CT->get_tuple(\%myconfig, \%$form);
283 CT->populate_drop_down_boxes(\%myconfig, \%$form);
285 $form->{title} = "Edit";
288 $form->{discount} *= 100;
290 $form->{homepage} = 'http://' . $form->{homepage} unless $form->{homepage} =~ m|^https?://|;
295 $main::lxdebug->leave_sub();
300 join('; ', grep { $_ } map { $s->{"shipto$_"} } qw(name department_1 street city)) || ' '
303 sub _contacts_label {
304 join ", ", grep { $_ } $_[0]->{cp_name}, $_[0]->{cp_givenname};
308 $main::lxdebug->enter_sub();
310 $main::auth->assert('customer_vendor_edit');
312 my $form = $main::form;
313 my %myconfig = %main::myconfig;
314 my $locale = $main::locale;
316 $form->get_lists(employees => "ALL_EMPLOYEES",
317 taxzones => "ALL_TAXZONES",
318 currencies => "ALL_CURRENCIES");
319 $form->get_pricegroup(\%myconfig, { all => 1 });
321 $form->get_lists(customers => { key => "ALL_SALESMAN_CUSTOMERS", business_is_salesman => 1 }) if $::lx_office_conf{features}->{vertreter};
323 $form->{ALL_SALESMEN} = $form->{ALL_EMPLOYEES};
324 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
325 $form->{is_customer} = $form->{db} eq 'customer';
326 $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
327 $form->{shipto_label} = \&_shipto_label;
328 $form->{contacts_label} = \&_contacts_label;
329 $form->{taxzone_id} = 0 if !$form->{id};
330 $form->{jsscript} = 1;
331 $form->{fokus} = "ct.greeting";
332 $form->{SHIPTO_ALL} = [ +{ shipto_id => '0', shiptoname => $::locale->text('All') }, @{ $form->{SHIPTO} } ];
334 $form->{title} = $form->{title_save}
335 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
337 CT->query_titles_and_greetings(\%myconfig, \%$form);
338 map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(COMPANY_GREETINGS);
340 $form->{NOTES} ||= [ ];
342 if (!$form->{'language_id'}) {
343 my $l_id = SL::DB::Default->get->{'language_id'};
345 $form->{'default_language_id'} = $l_id;
349 if (!$form->{'id'}) {
350 $form->{'currency'} = $form->get_default_currency(\%myconfig);
352 $form->{currency} = $form->{curr};
355 $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'CT', 'trans_id' => $form->{id});
357 CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
360 print $form->parse_html_template('ct/form_header');
362 $main::lxdebug->leave_sub();
366 $main::lxdebug->enter_sub();
368 $main::auth->assert('customer_vendor_edit');
370 my $form = $main::form;
372 print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
373 is_customer => $form->{db} eq 'customer' });
374 $main::lxdebug->leave_sub();
378 $main::auth->assert('customer_vendor_edit & ' .
379 '(general_ledger | invoice_edit | vendor_invoice_edit | ' .
380 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit)');
382 $::form->isblank("name", $::locale->text("Name missing!"));
384 if ($::form->{new_salesman_id} && $::lx_office_conf{features}->{vertreter}) {
385 $::form->{salesman_id} = $::form->{new_salesman_id};
386 delete $::form->{new_salesman_id};
389 my $res = $::form->{db} eq 'customer' ? CT->save_customer(\%::myconfig, $::form) : CT->save_vendor(\%::myconfig, $::form);
392 if ($::form->{"db"} eq "customer") {
393 $::form->error($::locale->text('This customer number is already in use.'));
395 $::form->error($::locale->text('This vendor number is already in use.'));
400 sub add_transaction {
401 $main::lxdebug->enter_sub();
403 $main::auth->assert('customer_vendor_edit & ' .
404 '(general_ledger | invoice_edit | vendor_invoice_edit | ' .
405 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit)');
407 my $form = $main::form;
408 my %myconfig = %main::myconfig;
409 my $locale = $main::locale;
411 # # saving the history
412 # if(!exists $form->{addition}) {
413 # $form->{addition} = "ADD TRANSACTION";
414 # $form->save_history;
416 # # /saving the history
420 $form->{callback} = $form->escape($form->{callback}, 1);
421 my $name = $form->escape("$form->{name}", 1);
424 "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
427 $main::lxdebug->leave_sub();
430 sub save_and_ap_transaction {
431 $main::lxdebug->enter_sub();
433 $main::auth->assert('customer_vendor_edit & general_ledger');
435 my $form = $main::form;
436 my %myconfig = %main::myconfig;
438 $form->{script} = "ap.pl";
440 if(!exists $form->{addition}) {
441 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
442 $form->{addition} = "SAVED";
445 # /saving the history
447 $main::lxdebug->leave_sub();
450 sub save_and_ar_transaction {
451 $main::lxdebug->enter_sub();
453 $main::auth->assert('customer_vendor_edit & general_ledger');
455 my $form = $main::form;
456 my %myconfig = %main::myconfig;
458 $form->{script} = "ar.pl";
460 if(!exists $form->{addition}) {
461 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
462 $form->{addition} = "SAVED";
465 # /saving the history
467 $main::lxdebug->leave_sub();
470 sub save_and_invoice {
471 $main::lxdebug->enter_sub();
473 my $form = $main::form;
474 my %myconfig = %main::myconfig;
476 if ($form->{db} eq 'customer') {
477 $main::auth->assert('customer_vendor_edit & invoice_edit');
479 $main::auth->assert('customer_vendor_edit & vendor_invoice_edit');
482 $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
483 $form->{type} = "invoice";
485 if(!exists $form->{addition}) {
486 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
487 $form->{addition} = "SAVED";
490 # /saving the history
492 $main::lxdebug->leave_sub();
496 $main::lxdebug->enter_sub();
498 $main::auth->assert('customer_vendor_edit & request_quotation_edit');
500 my $form = $main::form;
501 my %myconfig = %main::myconfig;
503 $form->{script} = "oe.pl";
504 $form->{type} = "request_quotation";
506 if(!exists $form->{addition}) {
507 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
508 $form->{addition} = "SAVED";
511 # /saving the history
513 $main::lxdebug->leave_sub();
516 sub save_and_quotation {
517 $main::lxdebug->enter_sub();
519 $main::auth->assert('customer_vendor_edit & sales_quotation_edit');
521 my $form = $main::form;
522 my %myconfig = %main::myconfig;
524 $form->{script} = "oe.pl";
525 $form->{type} = "sales_quotation";
527 if(!exists $form->{addition}) {
528 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
529 $form->{addition} = "SAVED";
532 # /saving the history
534 $main::lxdebug->leave_sub();
538 $main::lxdebug->enter_sub();
540 my $form = $main::form;
541 my %myconfig = %main::myconfig;
543 if ($form->{db} eq 'customer') {
544 $main::auth->assert('customer_vendor_edit & sales_order_edit');
546 $main::auth->assert('customer_vendor_edit & purchase_order_edit');
549 $form->{script} = "oe.pl";
551 ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
553 if(!exists $form->{addition}) {
554 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
555 $form->{addition} = "SAVED";
558 # /saving the history
560 $main::lxdebug->leave_sub();
564 $main::lxdebug->enter_sub();
566 $main::auth->assert('customer_vendor_edit');
568 my $form = $main::form;
569 my %myconfig = %main::myconfig;
570 my $locale = $main::locale;
572 my $msg = ucfirst $form->{db};
578 if(!exists $form->{addition}) {
579 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
580 $form->{addition} = "SAVED";
583 # /saving the history
584 $form->redirect($locale->text($msg));
586 $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 my $msg = ucfirst $form->{db};
604 if(!exists $form->{addition}) {
605 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
606 $form->{addition} = "SAVED";
609 # /saving the history
612 $main::lxdebug->leave_sub();
617 $main::lxdebug->enter_sub();
619 $main::auth->assert('customer_vendor_edit');
621 my $form = $main::form;
622 my %myconfig = %main::myconfig;
623 my $locale = $main::locale;
625 CT->delete(\%myconfig, \%$form);
627 my $msg = ucfirst $form->{db};
630 if(!exists $form->{addition}) {
631 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
632 $form->{addition} = "DELETED";
635 # /saving the history
636 $form->redirect($locale->text($msg));
638 $main::lxdebug->leave_sub();
642 $main::lxdebug->enter_sub();
644 $main::auth->assert('customer_vendor_edit');
646 my $form = $main::form;
651 $main::lxdebug->leave_sub();
655 $main::lxdebug->enter_sub();
657 $main::auth->assert('customer_vendor_edit');
659 my $form = $main::form;
662 $main::lxdebug->leave_sub();
666 $main::lxdebug->enter_sub();
668 $main::auth->assert('customer_vendor_edit');
670 CT->populate_drop_down_boxes(\%::myconfig, $::form);
671 CT->query_titles_and_greetings(\%::myconfig, $::form);
672 CT->get_contact(\%::myconfig, $::form) if $::form->{cp_id};
674 $::form->{contacts_label} = \&_contacts_label;
676 print $::form->ajax_response_header(), $::form->parse_html_template('ct/_contact');
678 $main::lxdebug->leave_sub();
682 $main::lxdebug->enter_sub();
684 $main::auth->assert('customer_vendor_edit');
686 CT->populate_drop_down_boxes(\%::myconfig, $::form);
687 CT->get_shipto(\%::myconfig, $::form) if $::form->{shipto_id};
689 $::form->{shipto_label} = \&_shipto_label;
691 print $::form->ajax_response_header(), $::form->parse_html_template('ct/_shipto');
693 $main::lxdebug->leave_sub();
697 $::lxdebug->enter_sub;
699 $::auth->assert('customer_vendor_edit');
700 $::auth->assert('sales_all_edit');
702 CT->get_delivery(\%::myconfig, $::form );
704 print $::form->ajax_response_header,
705 $::form->parse_html_template('ct/get_delivery', {
706 is_customer => $::form->{db} eq 'customer',
709 $::lxdebug->leave_sub;
713 $main::lxdebug->enter_sub();
715 $main::auth->assert('customer_vendor_edit');
717 my $form = $main::form;
718 my %myconfig = %main::myconfig;
720 CT->get_shipto(\%myconfig, \%$form);
722 unless ($form->{shiptoused}) {
723 CT->delete_shipto($form->{shipto_id});
724 @$form{ grep /^shipto/, keys %$form } = undef;
729 $main::lxdebug->leave_sub();
733 $main::lxdebug->enter_sub();
735 $main::auth->assert('customer_vendor_edit');
737 my $form = $main::form;
738 my %myconfig = %main::myconfig;
740 CT->get_contact(\%myconfig, \%$form);
742 unless ($form->{cp_used}) {
743 CT->delete_contact($form->{cp_id});
744 @$form{ grep /^cp_/, keys %$form } = undef;
749 $main::lxdebug->leave_sub();
752 sub ajax_autocomplete {
753 $main::lxdebug->enter_sub();
755 my $form = $main::form;
756 my %myconfig = %main::myconfig;
758 $form->{column} = 'name' unless $form->{column} =~ /^name$/;
759 $form->{vc} = 'customer' unless $form->{vc} =~ /^customer|vendor$/;
760 $form->{db} = $form->{vc}; # CT expects this
761 $form->{$form->{column}} = $form->{q} || '';
762 $form->{limit} = ($form->{limit} * 1) || 10;
763 $form->{searchitems} ||= '';
765 CT->search(\%myconfig, $form);
767 print $form->ajax_response_header(),
768 $form->parse_html_template('ct/ajax_autocomplete');
770 $main::lxdebug->leave_sub();
773 sub continue { call_sub($main::form->{nextsub}); }